-- @path ANOMM=/InterComponentCommunicationModeling/MetaModels/ANO.ecore
-- @path ICCMM=/InterComponentCommunicationModeling/MetaModels/ICCMM.ecore

module PrivilegeEscalationInterApp;
create OUT : ANOMM from IN : ICCMM;


helper context ICCMM!ExplicitTargetComponent def :haveIntentSpoofing :Boolean=
if not self.component_enforcedPermissions.notEmpty() then
  true
else
    false
endif;


helper context ICCMM!ExplicitCommunication def:getIntentSpoofingForExplicitCommunications :Sequence(String) =
	
	let ExplicitCommunicationsOfBundle : Sequence(ICCMM!ExplicitCommunication) = self.refImmediateComposite().explicit_communications in
	ExplicitCommunicationsOfBundle->iterate(n ; output : Sequence(String) = Sequence{} | 

	
if not self.receiver_app.oclIsUndefined() then
   if not self.receiver_app.Name.toString().endsWith(self.sender_app.Name.toString()) then 
        if self.intent.target_component.exported=true then
            if self.intent.target_component.haveIntentSpoofing then
                if self.intent.target_component.Type= #DynamicRegisteredReceiver then
     	          output->including(': This Bundle has Broadcast Injection in the implicit communication with this specification:' + ' '+ '/Sender Component:'+ ' ' + self.intent.SendComponentName + ' /'+ ' ' + '/Sender App:'+ ' ' + self.sender_app.Name + ' /' + '/Target Component:'+ ' ' + self.intent.Receiver + ' /' + '/Receiver App:'+ ' ' + self.receiver_app.Name + ' /') 
		        else
		 	        if self.intent.target_component.Data=true then
			            if self.intent.target_component.Type=#Activity then
				          output->including(': This Bundle has Activity Launch(with data) in the implicit communication with this specification:' + ' '+ '/Sender Component:'+ ' ' + self.intent.SendComponentName + ' /'+ ' ' + '/Sender App:'+ ' ' + self.sender_app.Name + ' /' + '/Target Component:'+ ' ' + self.intent.Receiver + ' /' + '/Receiver App:'+ ' ' + self.receiver_app.Name + ' /')
				        else
					        if self.intent.target_component.Type=#Service then
					         output->including(': This Bundle has Service Launch(with data) in the implicit communication with this specification:' + ' '+ '/Sender Component:'+ ' ' + self.intent.SendComponentName + ' /'+ ' ' + '/Sender App:'+ ' ' + self.sender_app.Name + ' /' + '/Target Component:'+ ' ' + self.intent.Receiver + ' /' + '/Receiver App:'+ ' ' + self.receiver_app.Name + ' /')
					        else
						        if self.intent.target_component.Type=#Receiver then
						          output->including(': This Bundle has Broadcast Injection(with data) in the implicit communication with this specification:' + ' '+ '/Sender Component:'+ ' ' + self.intent.SendComponentName + ' /'+ ' ' + '/Sender App:'+ ' ' + self.sender_app.Name + ' /' + '/Target Component:'+ ' ' + self.intent.Receiver + ' /' + '/Receiver App:'+ ' ' + self.receiver_app.Name + ' /')
						        else
							      output->reject(c | c.Name=self.intent.target_component.Name)
						        endif
					        endif
				        endif
			        else
				        if self.intent.target_component.Type=#Activity then
				        output->including(': This Bundle has Activity Launch(without data) in the implicit communication with this specification:' + ' '+ '/Sender Component:'+ ' ' + self.intent.SendComponentName + ' /'+ ' ' + '/Sender App:'+ ' ' + self.sender_app.Name + ' /' + '/Target Component:'+ ' ' + self.intent.Receiver + ' /' + '/Receiver App:'+ ' ' + self.receiver_app.Name + ' /')
				        else
					        if self.intent.target_component.Type=#Service then
					          output->including(': This Bundle has Service Launch(without data) in the implicit communication with this specification:' + ' '+ '/Sender Component:'+ ' ' + self.intent.SendComponentName + ' /'+ ' ' + '/Sender App:'+ ' ' + self.sender_app.Name + ' /' + '/Target Component:'+ ' ' +self.intent.Receiver + ' /' + '/Receiver App:'+ ' ' + self.receiver_app.Name + ' /')
					        else
						        if self.intent.target_component.Type=#Receiver then
						         output->including(': This Bundle has Broadcast Injection(without data) in the implicit communication with this specification:' + ' '+ '/Sender Component:'+ ' ' + self.intent.SendComponentName + ' /'+ ' ' + '/Sender App:'+ ' ' + self.sender_app.Name + ' /' + '/Target Component:'+ ' ' + self.intent.Receiver + ' /' + '/Receiver App:'+ ' ' + self.receiver_app.Name + ' /')
					            else
							      output->reject(c | c.Name=self.intent.target_component.Name)
						        endif
					        endif
				        endif
			        endif			
		        endif
	        else
	         output->reject(c | c.Name=self.intent.target_component.Name)
	        endif
        else
            if self.intent.target_component.Type= #DynamicRegisteredReceiver then
     	      output->including(': This Bundle has Broadcast Injection in the implicit communication with this specification:' + ' '+ '/Sender Component:'+ ' ' + self.intent.SendComponentName + ' /'+ ' ' + '/Sender App:'+ ' ' + self.sender_app.Name + ' /' + '/Target Component:'+ ' ' + self.intent.Receiver + ' /' + '/Receiver App:'+ ' ' + self.receiver_app.Name + ' /') 
	        else
		        if self.intent.target_component.Data=true then
			        if self.intent.target_component.Type=#Activity then
				      output->including(': This Bundle has Activity Launch(with data) in the implicit communication with this specification:' + ' '+ '/Sender Component:'+ ' ' + self.intent.SendComponentName + ' /'+ ' ' + '/Sender App:'+ ' ' + self.sender_app.Name + ' /' + '/Target Component:'+ ' ' + self.intent.Receiver + ' /' + '/Receiver App:'+ ' ' + self.receiver_app.Name + ' /')
			        else
				        if self.intent.target_component.Type=#Service then
				          output->including(': This Bundle has Service Launch(with data) in the implicit communication with this specification:' + ' '+ '/Sender Component:'+ ' ' + self.intent.SendComponentName + ' /'+ ' ' + '/Sender App:'+ ' ' + self.sender_app.Name + ' /' + '/Target Component:'+ ' ' + self.intent.Receiver + ' /' + '/Receiver App:'+ ' ' + self.receiver_app.Name + ' /')
				        else
					        if self.intent.target_component.Type=#Receiver then
					           output->including(': This Bundle has Broadcast Injection(with data) in the implicit communication with this specification:' + ' '+ '/Sender Component:'+ ' ' + self.intent.SendComponentName + ' /'+ ' ' + '/Sender App:'+ ' ' + self.sender_app.Name + ' /' + '/Target Component:'+ ' ' + self.intent.Receiver + ' /' + '/Receiver App:'+ ' ' + self.receiver_app.Name + ' /')
					         else
					          output->reject(c | c.Name=self.intent.target_component.Name)
					        endif
				        endif
			        endif
		        else
			        if self.intent.target_component.Type=#Activity then
			          output->including(': This Bundle has Activity Launch(without data) in the implicit communication with this specification:' + ' '+ '/Sender Component:'+ ' ' + self.intent.SendComponentName + ' /'+ ' ' + '/Sender App:'+ ' ' + self.sender_app.Name + ' /' + '/Target Component:'+ ' ' + self.intent.Receiver + ' /' + '/Receiver App:'+ ' ' + self.receiver_app.Name + ' /')
			        else
				        if self.intent.target_component.Type=#Service then
				         output->including(': This Bundle has Service Launch(without data) in the implicit communication with this specification:' + ' '+ '/Sender Component:'+ ' ' + self.intent.SendComponentName + ' /'+ ' ' + '/Sender App:'+ ' ' + self.sender_app.Name + ' /' + '/Target Component:'+ ' ' +self.intent.Receiver + ' /' + '/Receiver App:'+ ' ' + self.receiver_app.Name + ' /')
				        else
					        if self.intent.target_component.Type=#Receiver then
						     output->including(': This Bundle has Broadcast Injection(without data) in the implicit communication with this specification:' + ' '+ '/Sender Component:'+ ' ' + self.intent.SendComponentName + ' /'+ ' ' + '/Sender App:'+ ' ' + self.sender_app.Name + ' /' + '/Target Component:'+ ' ' + self.intent.Receiver + ' /' + '/Receiver App:'+ ' ' + self.receiver_app.Name + ' /')
					         else
					         output->reject(c | c.Name=self.intent.target_component.Name)
					        endif
				        endif
			        endif
		        endif
	        endif
        endif
	else
	     output->reject(c | c.Name=self.intent.target_component.Name)
	endif
else
 output->reject(c | c.Name=self.intent.target_component.Name)
endif
	  )->asSet()->asSequence();



rule ExplicitCommunications2IntentSpoofingCommunications
 {
	
	from
	   s:ICCMM!ExplicitCommunication 
	  (not s.intent.target_component.oclIsUndefined() )

	to
	   t:ANOMM!IntentSpoofing
	   (
  	                
	   description <-  s.getIntentSpoofingForExplicitCommunications->iterate(up; output: String = '' | output->concat(up)+'\n')     

	   )
	  do {
	t.trace<-Sequence{s};
         }
  }