genericservices/taskscheduler/Test/PlatSec/platsectaskhandler.cpp
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 2004-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 <schinfo.h>
       
    17 #include <schinfointernal.h>
       
    18 #include <schtask.h>
       
    19 #include <s32file.h>
       
    20 #include <e32math.h>
       
    21 #include <e32cons.h>
       
    22 #include <e32test.h>
       
    23 
       
    24 #include "platsectaskcommon.h"
       
    25 
       
    26 
       
    27 // Constants
       
    28 _LIT(KConsoleName, "PlatSecTaskHandler");
       
    29 
       
    30 static void SignalTestExe()
       
    31 	{
       
    32 	_LIT(KSchSemaphoreName, "SCHMinimalTaskHandler");
       
    33 	RSemaphore sem;
       
    34 	TInt ret = sem.OpenGlobal(KSchSemaphoreName);
       
    35 	if (ret == KErrNone)
       
    36 		{
       
    37 		sem.Signal();
       
    38 		sem.Close();
       
    39 		}
       
    40 	}
       
    41 
       
    42 //***********************************************************************************
       
    43 LOCAL_C TInt GetRandomNumber(const TInt aLow, const TInt aHigh, TInt64& aSeed)
       
    44 	{
       
    45 	TReal initialRand = (Math::FRand(aSeed) * (aHigh - aLow));
       
    46 	TInt32 rand;
       
    47 
       
    48 	// Round to 0 decimal places, ie. the nearest whole numer
       
    49 	Math::Round(initialRand, initialRand, 0);
       
    50 	Math::Int(rand, initialRand);
       
    51 
       
    52 	return (aLow + rand);
       
    53 	}
       
    54 
       
    55 //***********************************************************************************
       
    56 LOCAL_C void DestroyArray(TAny* aArray)
       
    57 	{
       
    58 	static_cast<RArray<TCapability>*>(aArray)->Reset();
       
    59 	}
       
    60 /**
       
    61 @SYMTestCaseID          SYSLIB-SCHSVR-CT-1345
       
    62 @SYMTestCaseDesc	    Platform security Task handler test 
       
    63 @SYMTestPriority 	    High
       
    64 @SYMTestActions  	    Attempt to test the contents of a task file.
       
    65 @SYMTestExpectedResults Test must not fail
       
    66 @SYMREQ                 REQ0000
       
    67 */				
       
    68 LOCAL_C void ConstructConsoleL(RFile& aTaskFile, RTest& aTest)
       
    69 	{
       
    70 	
       
    71 	CConsoleBase* console=Console::NewL(KConsoleName, TSize(KConsFullScreen, KConsFullScreen));
       
    72 	CleanupStack::PushL(console);
       
    73 	console->Printf(_L(" contents of task file\n"));
       
    74 
       
    75 	RArray<TCapability> enforcedCaps;
       
    76 	CleanupStack::PushL(TCleanupItem(&DestroyArray,&enforcedCaps));
       
    77 
       
    78 	for (TInt i=0; i<ECapability_Limit; i++)
       
    79 		{
       
    80 		// we're checking here to see which capabilities are enforced, skipping
       
    81 		// ECapabilityMultimediaDD and ECapabilityWriteDeviceData as it is
       
    82 		// needed to write persistent schedules upon which this test relies.
       
    83 		
       
    84 		if (i == KPlatSecTestCapability)
       
    85 			continue;
       
    86 		else if (i == ECapabilityWriteDeviceData)
       
    87 			continue;
       
    88 		else if (PlatSec::IsCapabilityEnforced((TCapability)i))
       
    89 			enforcedCaps.Append((TCapability)i);
       
    90 		}
       
    91 	
       
    92 	TBool isTestCapabilityEnforced = PlatSec::IsCapabilityEnforced(KPlatSecTestCapability);
       
    93 	
       
    94 	CFileStore* store;
       
    95 	store = CDirectFileStore::FromLC(aTaskFile);
       
    96 
       
    97 	RStoreReadStream instream;
       
    98 	instream.OpenLC(*store,store->Root());
       
    99 	
       
   100 	TInt count = instream.ReadInt32L();
       
   101 	for (TInt i=0;i<count;i++)
       
   102 		{
       
   103 		CScheduledTask* task = CScheduledTask::NewLC(instream);
       
   104 		
       
   105 		aTest(task->Info().iName.CompareF(KPlatSecTaskName)==0);
       
   106 		aTest(task->Data().CompareF(KPlatSecTaskData)==0);
       
   107 		aTest(task->SecurityInfo().iSecureId==KPlatSecTestSid); //This is the SID of tschsvrplatsec
       
   108 
       
   109 		// check that client has capability it should have
       
   110 		if(isTestCapabilityEnforced)
       
   111 			aTest(task->SecurityInfo().iCaps.HasCapability(KPlatSecTestCapability));
       
   112 		
       
   113 		// check that the client has ECapabilityWriteDeviceData
       
   114 		aTest(task->SecurityInfo().iCaps.HasCapability(ECapabilityWriteDeviceData));
       
   115 		
       
   116 		// check that client doesn't have any other capabilities
       
   117 		for (TInt j=enforcedCaps.Count()-1; j>=0; --j)
       
   118 			{
       
   119 			aTest(!task->SecurityInfo().iCaps.HasCapability(enforcedCaps[j]));
       
   120 			}
       
   121 			
       
   122 		
       
   123 		CleanupStack::PopAndDestroy(task);
       
   124 		}
       
   125 		
       
   126 	console->Printf(_L("Pausing for a one second..."));
       
   127 	User::After(1000000);
       
   128 	
       
   129 	CleanupStack::PopAndDestroy(4); // instream, store, enforcedCaps, console
       
   130 	}
       
   131 
       
   132 
       
   133 //***********************************************************************************
       
   134 LOCAL_C TInt Execute()
       
   135 	{
       
   136 	TInt err = KErrNoMemory;
       
   137 	CTrapCleanup* cleanup=CTrapCleanup::New();	//can fail
       
   138 	if (cleanup)
       
   139 		{
       
   140 		_LIT(KPlatSecTestName, "PlatSectTaskHandlerTest");
       
   141 		RTest theTest(KPlatSecTestName);
       
   142 		theTest.Start(KPlatSecTestName);
       
   143 		theTest.Title();
       
   144 
       
   145 		RFile file;
       
   146 
       
   147 		// Adopt the task file from the Task Scheduler
       
   148 		err = file.AdoptFromCreator(TScheduledTaskFile::FsHandleIndex(),
       
   149 									TScheduledTaskFile::FileHandleIndex());
       
   150 		if (err != KErrNone)
       
   151 			return err;
       
   152 		
       
   153 		// The aParam is the name of a file where the relevant CTaskExCmdLine is
       
   154 		// do the executing 
       
   155 		theTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-1345 "));
       
   156 		TRAPD(err, ConstructConsoleL(file, theTest));
       
   157 		if(err == KErrNone)
       
   158 			{
       
   159 			// Sometimes we want to return a bogus error value, 
       
   160 			// sometimes we don't.
       
   161 			TTime now;
       
   162 			now.HomeTime();
       
   163 			TInt64 seed = now.Int64();
       
   164 			err = GetRandomNumber(-50, 200, seed); //20% chance of error being returned
       
   165 			}
       
   166 		
       
   167 		file.Close();// Close the file
       
   168 		
       
   169 		theTest.End();
       
   170 		theTest.Close();
       
   171 				
       
   172 		delete cleanup;
       
   173 		}
       
   174 	SignalTestExe();		
       
   175 	return err;
       
   176 	}
       
   177 
       
   178 
       
   179 //***********************************************************************************
       
   180 GLDEF_C TInt E32Main()
       
   181 	{
       
   182 	return Execute();
       
   183 	}