sysstatemgmt/systemstatereferenceplugins/test/tintcustcmd/src/tcustcmd_step_devicesecuritypincheck.cpp
changeset 0 4e1aa6a622a0
child 21 ccb4f6b3db21
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "tcustcmd_step_devicesecuritypincheck.h"
       
    17 #include <ssm/ssmuiproviderdll.h>
       
    18 #include <w32std.h>
       
    19 #include <s32file.h>
       
    20 
       
    21 static TInt CustomCmdTestDeviceSecurityCheckCallBackL(TAny* aCustomCmdTestDeviceSecurityPinCheck)
       
    22 	{
       
    23 	CCustomCmdTestDeviceSecurityPinCheck* test = reinterpret_cast<CCustomCmdTestDeviceSecurityPinCheck*>(aCustomCmdTestDeviceSecurityPinCheck);
       
    24 	CleanupStack::PushL(test);
       
    25 	test->CallBackForEnterPin();
       
    26 	CleanupStack::Pop(); 
       
    27 	return KErrNone;
       
    28 	}
       
    29 	
       
    30 /*
       
    31 Call back function to enter a PIN 
       
    32 */
       
    33 void CCustomCmdTestDeviceSecurityPinCheck::CallBackForEnterPin()
       
    34 	{
       
    35 	iAsyncStopScheduler->CallBack();
       
    36 	
       
    37 	RWsSession wsSession;
       
    38 	wsSession.Connect();
       
    39 
       
    40 	TRawEvent eventDown;
       
    41 	TRawEvent eventUp;
       
    42 	
       
    43 	eventDown.Set(TRawEvent::EKeyDown, EStdKeyComma);
       
    44 	UserSvr::AddEvent(eventDown);
       
    45 	eventUp.Set(TRawEvent::EKeyUp, EStdKeyComma);
       
    46 	UserSvr::AddEvent(eventUp);
       
    47 	User::After(100000);
       
    48 
       
    49 	eventDown.Set(TRawEvent::EKeyDown, EStdKeyEnter);
       
    50 	UserSvr::AddEvent(eventDown);
       
    51 	eventUp.Set(TRawEvent::EKeyUp, EStdKeyEnter);
       
    52 	UserSvr::AddEvent(eventUp);
       
    53 	User::After(100000);
       
    54 	
       
    55 	wsSession.Flush();
       
    56 	wsSession.Close();
       
    57 	}
       
    58 	
       
    59 void CCustomCmdTestDeviceSecurityPinCheck::StopScheduler()
       
    60 	{
       
    61 	//Stop the active scheduler
       
    62 	iActiveSchedulerWait->AsyncStop();
       
    63 	}
       
    64 
       
    65 CCustomCmdTestDeviceSecurityPinCheck::~CCustomCmdTestDeviceSecurityPinCheck()
       
    66 	{
       
    67 	delete iAsyncStopScheduler;
       
    68 	delete iActiveSchedulerWait;
       
    69 	delete iActiveScheduler;
       
    70 	delete iStopSimulatekey;
       
    71 	}
       
    72 
       
    73 CCustomCmdTestDeviceSecurityPinCheck::CCustomCmdTestDeviceSecurityPinCheck():iSwp(TSsmSwp(KTestSwp, 0))
       
    74 	{
       
    75 	SetTestStepName(KTCCustomCmdTestDeviceSecurityPinCheck);
       
    76 	}
       
    77 
       
    78 TVerdict CCustomCmdTestDeviceSecurityPinCheck::doTestStepPreambleL()
       
    79 	{
       
    80 	iActiveScheduler = new(ELeave) CActiveScheduler;
       
    81 	CActiveScheduler::Install (iActiveScheduler);
       
    82 
       
    83 	iActiveSchedulerWait = new(ELeave) CActiveSchedulerWait;
       
    84 
       
    85 	iAsyncStopScheduler = new(ELeave) CAsyncCallBack(CActive::EPriorityIdle);
       
    86 	
       
    87 	//Start the test exe which defines startup related property keys
       
    88 	RProcess processHandle;
       
    89 	
       
    90 	TInt temp = processHandle.Create(KExeToDefineStartUpPS, KNullDesC);
       
    91 	processHandle.Resume();
       
    92 	processHandle.Close();
       
    93 		
       
    94 	processHandle.Create(KExeToDefineStartUpStatePS, KNullDesC);
       
    95 	processHandle.Resume();
       
    96 	processHandle.Close();
       
    97 
       
    98 	//For stopping the key simulation
       
    99 	iStopSimulatekey = CStopSimulateDevicekey::NewL(this);
       
   100  	
       
   101  	//Connect to SsmStateManager
       
   102  	const TInt connect = iClient.Connect();
       
   103 	TEST(KErrNone == connect);
       
   104 	
       
   105 	//RProperty property;
       
   106 	iProperty.Define(KCustomcmdServerSID, iSwp.Key(), RProperty::EInt);
       
   107 	
       
   108 	// Register mapping between keys and swp policy DLL (done once per ssmserver)
       
   109 	INFO_PRINTF1(_L("Registering swp..."));
       
   110 	iClient.RegisterSwpMapping(KTestSwp, KTestSwpPolicyCustomCmd);
       
   111 	
       
   112 	RegisterDeviceSecurityStatus();
       
   113 	return TestStepResult();
       
   114 	}
       
   115 	
       
   116 void CCustomCmdTestDeviceSecurityPinCheck::RegisterDeviceSecurityStatus()
       
   117 	{
       
   118 	INFO_PRINTF1(_L("RegisterDeviceSecurityStatus"));
       
   119 	
       
   120 	_LIT_SECURITY_POLICY_PASS(KAllowAllPolicy);
       
   121 	//Define the property for sim status for swp.
       
   122 	TInt ret = RProperty::Define(RProcess().SecureId(), CSsmUiSpecific::SimStatusPropertyKey(), RProperty::EInt,KAllowAllPolicy, KAllowAllPolicy);
       
   123 	TEST(KErrNone == ret || KErrAlreadyExists == ret);
       
   124 
       
   125 
       
   126 	const TUint KStartupDeviceSecurityStatusKey = {CSsmUiSpecific::SimStatusPropertyKey()};
       
   127 	
       
   128 	//register the swp mapping for Sim DeviceSecurity status with its swp policy
       
   129 	ret = iClient.RegisterSwpMapping(KStartupDeviceSecurityStatusKey, KTestSimStatusSwpPolicy);
       
   130 	TEST(KErrNone == ret || KErrAlreadyExists == ret);
       
   131 	}
       
   132 		
       
   133 TVerdict CCustomCmdTestDeviceSecurityPinCheck::doTestStepL()
       
   134 	{
       
   135 	INFO_PRINTF1(_L("doTestStepL"));
       
   136 	TInt err = 0;
       
   137 	__UHEAP_MARK;
       
   138 
       
   139 	TRAP(err,TestHandleSecCustCmdWithExeBehaviourWaitForSignal());
       
   140 	TEST(KErrNone == err);
       
   141 	TRAP(err,TestHandleSecCustCmdWithExeBehaviourDeferredWaitForSignal());	
       
   142 	TEST(KErrNone == err);
       
   143 	
       
   144 	TRAP(err,TestHandleSecCustCmdWithExeBehaviourWaitForSignalCancel());
       
   145 	TEST(KErrNone == err);	
       
   146 	TRAP(err,TestHandleSecCustCmdWithExeBehaviourDeferredWaitForSignalCancel());	
       
   147 	TEST(KErrNone == err);
       
   148 	
       
   149 	TRAP(err, TestOOMDevSecCustCmdWithExeBehaviourWFL());
       
   150 	TEST(KErrNone == err);	
       
   151 	TRAP(err, TestOOMDevSecCustCmdWithExeBehaviourDWFL());
       
   152 	TEST(KErrNone == err);	
       
   153 	
       
   154 	return TestStepResult();
       
   155 	}
       
   156 
       
   157 /**
       
   158 Old Test CaseID 		APPFWK-CUSTCMD-0021
       
   159 New Test CaseID 		DEVSRVS-SSREFPLUGINS-CUSTCMD-0026
       
   160  */
       
   161 	
       
   162 		
       
   163 void CCustomCmdTestDeviceSecurityPinCheck::TestHandleSecCustCmdWithExeBehaviourWaitForSignal()	
       
   164 	{
       
   165 	INFO_PRINTF1(_L("TestHandleSecCustCmdWithExeBehaviourWaitForSignal"));	
       
   166 	iExecutionBehaviour = ESsmWaitForSignal;
       
   167 	iStopSimulatekey->Start();
       
   168 	CreateAndExecuteCustCmd();
       
   169 	StartSimulateKey();
       
   170 	TEST(KErrNone == iStatus.Int());
       
   171 	INFO_PRINTF3(_L("DeviceSecurity PIN check completed with %d, expected %d"),iStatus.Int(),KErrNone);
       
   172 	}
       
   173 
       
   174 /**
       
   175 Old Test CaseID 		APPFWK-CUSTCMD-0026
       
   176 New Test CaseID 		DEVSRVS-SSREFPLUGINS-CUSTCMD-0026
       
   177  */
       
   178 
       
   179 void CCustomCmdTestDeviceSecurityPinCheck::TestHandleSecCustCmdWithExeBehaviourWaitForSignalCancel()	
       
   180 	{
       
   181 	INFO_PRINTF1(_L("TestHandleSecCustCmdWithExeBehaviourWaitForSignalCancelL"));
       
   182 	iExecutionBehaviour = ESsmWaitForSignal;
       
   183 	iStopSimulatekey->Start();
       
   184 	CreateAndExecuteCancelCustCmd();
       
   185 	StartSimulateKey();
       
   186 	TEST(KErrCancel == iCancelReqStatus.Int());
       
   187 	RDebug::Print(_L("TestHandleDeviceSecCustCmdWithExeBehaviourWaitForSignalCancelL completed with %d, expected %d"),iCancelReqStatus.Int(),KErrCancel);	
       
   188 	INFO_PRINTF3(_L("TestHandleDeviceSecCustCmdWithExeBehaviourWaitForSignalCancelL completed with %d, expected %d"),iCancelReqStatus.Int(),KErrCancel);	
       
   189 	}
       
   190 	
       
   191 /**
       
   192 Old Test CaseID 		APPFWK-CUSTCMD-0021
       
   193 New Test CaseID 		DEVSRVS-SSREFPLUGINS-CUSTCMD-0026
       
   194  */
       
   195 	
       
   196 void CCustomCmdTestDeviceSecurityPinCheck::TestHandleSecCustCmdWithExeBehaviourDeferredWaitForSignal()
       
   197 	{
       
   198 	INFO_PRINTF1(_L("TestHandleSecCustCmdWithExeBehaviourDeferredWaitForSignalL"));
       
   199 	iExecutionBehaviour = ESsmDeferredWaitForSignal;
       
   200 	iStopSimulatekey->Start();
       
   201 	CreateAndExecuteCustCmd();
       
   202 	StartSimulateKey();
       
   203 	TEST(KErrNone == iStatus.Int());
       
   204 	RDebug::Print(_L("TestHandleDeviceSecCustCmdWithExeBehaviourDeferredWaitForSignalL completed with %d, expected %d"),iStatus.Int(),KErrNone);	
       
   205 	INFO_PRINTF3(_L("TestHandleDeviceSecCustCmdWithExeBehaviourDeferredWaitForSignalL completed with %d, expected %d"),iStatus.Int(),KErrNone);	
       
   206 	}
       
   207 	
       
   208 /**
       
   209 Old Test CaseID 		APPFWK-CUSTCMD-0026
       
   210 New Test CaseID 		DEVSRVS-SSREFPLUGINS-CUSTCMD-0026
       
   211  */
       
   212 
       
   213 void CCustomCmdTestDeviceSecurityPinCheck::TestHandleSecCustCmdWithExeBehaviourDeferredWaitForSignalCancel()	
       
   214 	{
       
   215 	INFO_PRINTF1(_L("TestHandleSecCustCmdWithExeBehaviourDeferredWaitForSignalCancelL"));	
       
   216 	iExecutionBehaviour = ESsmDeferredWaitForSignal;
       
   217 	iStopSimulatekey->Start();
       
   218 	CreateAndExecuteCancelCustCmd();
       
   219 	StartSimulateKey();
       
   220 	TEST(KErrCancel == iCancelReqStatus.Int());
       
   221 	RDebug::Print(_L("TestHandleDeviceSecCustCmdWithExeBehaviourDeferredWaitForSignalCancelL completed with %d, expected %d"),iCancelReqStatus.Int(),KErrCancel);	
       
   222 	INFO_PRINTF3(_L("TestHandleDeviceSecCustCmdWithExeBehaviourDeferredWaitForSignalCancelL completed with %d, expected %d"),iCancelReqStatus.Int(),KErrCancel);	
       
   223 	}
       
   224 
       
   225 /**
       
   226 Old Test CaseID 		APPFWK-CUSTCMD-0028
       
   227 New Test CaseID 		DEVSRVS-SSREFPLUGINS-CUSTCMD-0026
       
   228  */
       
   229 
       
   230 // check attempt to 
       
   231 void CCustomCmdTestDeviceSecurityPinCheck::TestOOMDevSecCustCmdWithExeBehaviourWFL()
       
   232 	{
       
   233 	INFO_PRINTF1(_L("*** Starting TestOOMDevSecCustCmdwithExeBehaviourWF"));
       
   234 	TInt failRate = 1;
       
   235 	for (;;failRate++)
       
   236 	    {
       
   237 	    __UHEAP_SETFAIL(RHeap::EDeterministic,failRate);
       
   238 	    __UHEAP_MARK;
       
   239 
       
   240 	    TRAPD(ret, TestHandleSecCustCmdWithExeBehaviourWaitForSignal());
       
   241 	    	    
       
   242         TEST((ret==KErrNone || ret==KErrNoMemory));
       
   243         __UHEAP_MARKEND;
       
   244 
       
   245 	    if (ret==KErrNone)
       
   246 	        {
       
   247 	        break;
       
   248 	        }
       
   249 	    }	
       
   250 	__UHEAP_RESET;
       
   251 	INFO_PRINTF2(_L(" Executed device securitypincheck custom command %d times."),failRate);        
       
   252 	}
       
   253 	
       
   254 /**
       
   255 Old Test CaseID 		APPFWK-CUSTCMD-0028
       
   256 New Test CaseID 		DEVSRVS-SSREFPLUGINS-CUSTCMD-0026
       
   257  */
       
   258 
       
   259 void CCustomCmdTestDeviceSecurityPinCheck::TestOOMDevSecCustCmdWithExeBehaviourDWFL()
       
   260 	{
       
   261 	INFO_PRINTF1(_L("*** Starting TestOOMDevSecCustCmdWithExeBehaviourDWF"));
       
   262 	TInt failRate = 1;
       
   263 	for (;;failRate++)
       
   264 	    {
       
   265 	    __UHEAP_SETFAIL(RHeap::EDeterministic,failRate);
       
   266 	    __UHEAP_MARK;
       
   267 
       
   268 	    TRAPD(ret, TestHandleSecCustCmdWithExeBehaviourDeferredWaitForSignal());
       
   269 	    	    
       
   270         TEST((ret==KErrNone || ret==KErrNoMemory));
       
   271         __UHEAP_MARKEND;
       
   272 
       
   273 	    if (ret==KErrNone)
       
   274 	        {
       
   275 	        break;
       
   276 	        }
       
   277 	    }
       
   278 	__UHEAP_RESET;
       
   279 	INFO_PRINTF2(_L(" Executed device securitypincheck custom command %d number of times."),failRate);    
       
   280 	}
       
   281 	
       
   282 void CCustomCmdTestDeviceSecurityPinCheck::CreateAndExecuteCustCmd()
       
   283 	{
       
   284 	TInt swpValue = 0;
       
   285 	switch(iExecutionBehaviour)
       
   286 		{
       
   287 		case ESsmWaitForSignal:
       
   288 			swpValue = ERPropertyValForDeviceSecCustCmdWaitForSignal;
       
   289 		break;
       
   290 		case ESsmDeferredWaitForSignal:
       
   291 			swpValue = ERPropertyValForDeviceSecCustCmdDeferrdWaitForSignal;	
       
   292 		break;
       
   293 		case ESsmFireAndForget:
       
   294 			swpValue = ERPropertyValForDeviceSecCustCmdFireAndForget;
       
   295 		break;
       
   296 		default:
       
   297 		break;
       
   298 		}
       
   299 	// Create swps
       
   300 	INFO_PRINTF1(_L("Creating swp..."));
       
   301 	//set this property to create appropriate command by ssm.swp.policy.test.customcmd.dll
       
   302 	iSwp.Set(KTestSwp, swpValue);
       
   303 	
       
   304 	TEST(KErrNone == iProperty.Set(KCustomcmdServerSID, iSwp.Key(), iSwp.Value()));
       
   305 	
       
   306 	//Ensure the environment is clean
       
   307 	TInt value = 0;
       
   308 	TEST(KErrNone == iProperty.Get(KCustomcmdServerSID, iSwp.Key(), value));
       
   309 	TESTE(swpValue == value, value);
       
   310 	
       
   311 	//Let client request swp changes
       
   312 	INFO_PRINTF1(_L("Request several swp changes "));
       
   313 	iSwp.Set(KTestSwp, -99);
       
   314 	
       
   315 	iClient.RequestSwpChange(iSwp, iStatus);		
       
   316 
       
   317 	TEST(iStatus == KRequestPending);
       
   318 	User::WaitForRequest(iStatus);
       
   319 	User::After(500000); // Wait for 0.5sec to allow transitions to fully complete
       
   320 	TEST(iStatus.Int() == KErrNone);	
       
   321 	}
       
   322 	
       
   323 void CCustomCmdTestDeviceSecurityPinCheck::CreateAndExecuteCancelCustCmd()
       
   324 	{
       
   325 	TInt swpValue = 0;
       
   326 		switch(iExecutionBehaviour)
       
   327 		{
       
   328 		case ESsmWaitForSignal:
       
   329 			swpValue = ERPropertyValForDeviceSecCustCmdWaitForSignal;
       
   330 		break;
       
   331 		case ESsmDeferredWaitForSignal:
       
   332 			swpValue = ERPropertyValForDeviceSecCustCmdDeferrdWaitForSignal;	
       
   333 		break;
       
   334 		case ESsmFireAndForget:
       
   335 			swpValue = ERPropertyValForDeviceSecCustCmdFireAndForget;
       
   336 		break;
       
   337 		default:
       
   338 		break;
       
   339 		}
       
   340 	// Create swps
       
   341 	INFO_PRINTF1(_L("Creating swp..."));
       
   342 	//set this property to create appropriate command by ssm.swp.policy.test.customcmd.dll
       
   343 	iSwp.Set(KTestSwp, swpValue);	
       
   344 	
       
   345 	TEST(KErrNone == iProperty.Set(KCustomcmdServerSID, iSwp.Key(), iSwp.Value()));
       
   346 	
       
   347 	//Ensure the environment is clean
       
   348 	TInt value = 0;
       
   349 	TEST(KErrNone == iProperty.Get(KCustomcmdServerSID, iSwp.Key(), value));
       
   350 	TESTE(swpValue == value, value);
       
   351 	
       
   352 	//Let client request swp changes
       
   353 	INFO_PRINTF1(_L("Request several swp changes "));
       
   354 	iSwp.Set(KTestSwp, -99);
       
   355 	
       
   356 	//Transition engine will no cancel any request that is currently in execution. It will delete only quad request.
       
   357 	TRequestStatus status;
       
   358 	iClient.RequestSwpChange(iSwp, iStatus);		
       
   359 	iClient.RequestSwpChange(iSwp, iCancelReqStatus);			
       
   360 	TEST(iStatus == KRequestPending);
       
   361 	//User::After(5000); // Wait for 0.005sec to allow transitions to start
       
   362 	User::WaitForRequest(iStatus);
       
   363 	TEST(iStatus.Int() == KErrNone);
       
   364 	iClient.RequestSwpChangeCancel();
       
   365 	RDebug::Print(_L("CreateAndExecuteCancelCustCmdL completes with %d err"),iStatus.Int());
       
   366 	}
       
   367 			
       
   368 TVerdict CCustomCmdTestDeviceSecurityPinCheck::doTestStepPostambleL()
       
   369 	{
       
   370 	TEST(KErrNone == iProperty.Delete(KCustomcmdServerSID, iSwp.Key()));
       
   371 	iProperty.Close();
       
   372 	iClient.Close();
       
   373 	__UHEAP_MARKEND;
       
   374 	return TestStepResult();
       
   375 	}
       
   376 	
       
   377 void CCustomCmdTestDeviceSecurityPinCheck::StartSimulateKey()
       
   378 	{
       
   379 	//Callback function for simulating the key
       
   380 	TCallBack stop(CustomCmdTestDeviceSecurityCheckCallBackL, this);
       
   381 	if(!iAsyncStopScheduler->IsActive())
       
   382 		{
       
   383 		iAsyncStopScheduler->Set(stop);
       
   384 		}
       
   385 	iAsyncStopScheduler->CallBack();
       
   386 	iActiveSchedulerWait->Start();
       
   387 	}
       
   388 
       
   389 //
       
   390 /**
       
   391 CStopSimulateDevicekey class is used for stoping the scheduler
       
   392 */
       
   393 CStopSimulateDevicekey::~CStopSimulateDevicekey()
       
   394 	{
       
   395 	iProperty.Close();
       
   396 	Cancel();
       
   397 	}
       
   398 
       
   399 CStopSimulateDevicekey::CStopSimulateDevicekey() : CActive(CActive::EPriorityHigh)  //Standard)
       
   400 	{	
       
   401 	CActiveScheduler::Add(this);
       
   402 	}
       
   403 
       
   404 CStopSimulateDevicekey* CStopSimulateDevicekey::NewL(CCustomCmdTestDeviceSecurityPinCheck* aCustomCmdTestDeviceSecurityPinCheck)
       
   405 	{
       
   406 	CStopSimulateDevicekey* self = new (ELeave) CStopSimulateDevicekey();
       
   407 	CleanupStack::PushL(self);
       
   408 	self->Construct(aCustomCmdTestDeviceSecurityPinCheck);
       
   409 	CleanupStack::Pop();
       
   410 	return self;
       
   411 	}
       
   412 
       
   413 void CStopSimulateDevicekey::Construct(CCustomCmdTestDeviceSecurityPinCheck* aCustomCmdTestDeviceSecurityPinCheck)
       
   414 	{
       
   415 	// Define a property for stopping active scheduler 
       
   416  	TInt r = RProperty::Define(KCustomcmdServerPropertyKey, RProperty::EInt,ECapability_None, ECapability_None);								
       
   417 	iCustomCmdTestDeviceSecurityPinCheck = aCustomCmdTestDeviceSecurityPinCheck;
       
   418 	}
       
   419 		
       
   420 void CStopSimulateDevicekey::Start()
       
   421 	{  
       
   422 	iProperty.Attach(KCustomcmdServerSID,KCustomcmdServerPropertyKey);
       
   423 	iProperty.Subscribe(iStatus);
       
   424 	SetActive();				
       
   425 	}	
       
   426  
       
   427 void CStopSimulateDevicekey::RunL()
       
   428 	{	
       
   429 	TInt value;
       
   430 	TInt err = RProperty::Get(RProcess().SecureId(),KCustomcmdServerPropertyKey,value);	
       
   431 	//value will be set in policy (tcustcmd_swppolicy_customcmd) in the function HandleCleReturnValue() 
       
   432 	if( value == ERPropertyValForStopSchedulerCmdDeferredWaitForSignal || value == ERPropertyValForStopSchedulerCmdWaitForSignal)
       
   433 		{
       
   434 		RDebug::Printf("CStopSimulateDevicekey::RunL() ExecutionBehaviour = ESsmDeferredWaitForSignal");
       
   435 		iCustomCmdTestDeviceSecurityPinCheck->StopScheduler();		
       
   436 		}
       
   437 	else
       
   438 		{
       
   439 		Start();
       
   440 		}		
       
   441 	}
       
   442 	
       
   443 void CStopSimulateDevicekey::DoCancel()
       
   444 	{
       
   445 	iProperty.Cancel();
       
   446 	}
       
   447 
       
   448