os/ossrv/genericservices/taskscheduler/Test/PlatSec/TC_TSCH_PLATSEC.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     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 <csch_cli.h>
    17 #include "Thelpers.h"
    18 
    19 #include <e32base.h>
    20 #include <e32test.h>
    21 #include <f32file.h>
    22 
    23 #include "TestUtils.h"
    24 #include "platsectaskcommon.h"
    25 
    26 _LIT(KTestName,	"Task Scheduler platform security Test");
    27 
    28 _LIT(KProcess1, "tschsvrclient1");
    29 _LIT(KProcess2, "tschsvrclient2");
    30 _LIT(KProcess3, "tschsvrclient3");
    31 
    32 RTest	TheTest(KTestName);
    33 
    34 typedef CArrayFixFlat<TScheduleEntryInfo>	CSchEntryInfoArray;
    35 typedef CArrayFixFlat<TTaskInfo>			CTaskInfoArray;
    36 typedef CArrayFixFlat<TSchedulerItemRef>    CSchItemRefArray;
    37 
    38 static RScheduler	TheScheduler;
    39 static CTrapCleanup*	TheCleanup;
    40 static RFs			TheFsSession;
    41 
    42 //creates a daily schedule with StartTime of aStartTime
    43 static TInt CreateScheduleL(TSchedulerItemRef& aRef, 
    44 							RScheduler& aScheduler, 
    45 							const TTime& aStartTime)
    46 	{
    47 	CSchEntryInfoArray* entryList 
    48 		= new (ELeave) CSchEntryInfoArray(1);
    49 	CleanupStack::PushL(entryList);
    50 
    51 	TScheduleEntryInfo entry1;
    52 	entry1.iStartTime		= aStartTime;
    53 	entry1.iInterval		= 1; // TTimeIntervalDays
    54 	entry1.iIntervalType	= EDaily;
    55 	entry1.iValidityPeriod	= 30; // minutes
    56 	entryList->AppendL(entry1);
    57 	TInt res = aScheduler.CreatePersistentSchedule(aRef, *entryList);
    58 	CleanupStack::PopAndDestroy(); // entryList
    59 	return res;
    60 	}
    61 
    62 static void LaunchClient(TInt aScheduleHandle, 
    63 						TInt aTaskHandle,
    64 						const TDesC& aClient,
    65 						TInt aErrorCondition,
    66 						TInt aScheduleCount,
    67 						TInt aTaskCount)
    68 	{
    69 	// now launch process to try and access schedule
    70 	TRequestStatus stat;
    71 	RProcess client;
    72 
    73 	TBuf<30> id;
    74 	id.Append(':');
    75 	id.AppendNum(aScheduleHandle);
    76 	id.Append(':');
    77 	id.AppendNum(aTaskHandle);
    78 	id.Append(':');
    79 	id.AppendNum(aErrorCondition);
    80 	id.Append(':');
    81 	id.AppendNum(aScheduleCount);
    82 	id.Append(':');
    83 	id.AppendNum(aTaskCount);
    84 	id.Append(':');
    85 	
    86 	TInt res = client.Create(aClient, id);
    87 	TEST2(res, KErrNone);
    88 	// Asynchronous logon: completes when process terminates with process exit code
    89 	client.Logon(stat);
    90 	client.Resume();
    91 
    92 	User::WaitForRequest(stat);
    93 
    94 	TInt exitReason = client.ExitReason();
    95 	TEST2(exitReason, KErrNone);
    96 	client.Close();	
    97 	}
    98 
    99 /**
   100 @SYMTestCaseID SYSLIB-SCHSVR-CT-0025
   101 @SYMTestCaseDesc Check that schedules/tasks cannot be manipulated by unauthorised clients
   102 @SYMTestPriority High
   103 @SYMTestActions  Ensure permission denied for client with the wrong SID and without WriteDeviceData
   104                  Ensure permission granted for client with the wrong SID but with WriteDeviceData
   105 @SYMTestExpectedResults Only the schedule creator or clients with WriteDeviceData can manipulate schedules/tasks.
   106 @SYMPREQ 277 Ensure integrity of Symbian OS handsets
   107 */
   108 static void DoTest1L()
   109 	{
   110 	
   111 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0025 platformsec - API policing "));
   112 
   113 	TheTest.Next(_L("Connect to Scheduler"));
   114 	TInt res = TheScheduler.Connect();
   115 	TEST2(res, KErrNone);
   116 
   117 	TheTest.Next(_L("Registering Client"));
   118 	TEST2(SchSvrHelpers::RegisterClientL(TheScheduler), KErrNone);
   119 
   120 	// Create a schedule
   121 	TheTest.Next(_L("Creating schedule"));
   122 	TSchedulerItemRef scheduleHandle;
   123 	TTime time;
   124 	time.HomeTime();
   125 	time += TTimeIntervalHours(1);  //Task to go off one hour from now (ie we dont 
   126 									//want it going off for the purposes of this test)
   127 	User::LeaveIfError(CreateScheduleL(scheduleHandle, TheScheduler, time));  
   128 
   129 	// Add a task to the schedule
   130 	TheTest.Next(_L("Creating task for schedule"));
   131 	TTaskInfo taskInfo;
   132 	taskInfo.iName = _L("MyTaskName");
   133 	taskInfo.iPriority = 2;
   134 	taskInfo.iTaskId = 0;
   135 	taskInfo.iRepeat = 1;
   136 	HBufC* data = _L("Tasks Data").AllocLC();
   137 	res = TheScheduler.ScheduleTask(taskInfo, *data, scheduleHandle.iHandle);
   138 	CleanupStack::PopAndDestroy(); // data
   139 	TEST2(res, KErrNone);
   140 	
   141 	// Now launch client process with a different SID to ours
   142 	// and all capabilities excluding WriteDeviceData (and TCB)
   143 	// Client operations should fail with KErrPermissionDenied
   144 	TheTest.Next(_L("Launch Client1"));
   145 	LaunchClient(scheduleHandle.iHandle, 
   146 					taskInfo.iTaskId, 
   147 					KProcess1, 
   148 					KErrPermissionDenied,
   149 					0,
   150 					0);
   151 	
   152 	TheScheduler.Close();
   153 	// Now shut down the server and restart.
   154 	TheTest.Next(_L("shuting down and restarting server"));
   155 	CleanupHelpers::TestCleanupL();
   156 	res = TheScheduler.Connect();
   157 	TEST2(res, KErrNone);
   158 	TEST2(SchSvrHelpers::RegisterClientL(TheScheduler), KErrNone);
   159 	
   160 	//Check we still have 1 schedule and 1 task.
   161 	TheTest.Next(_L("Checking schedule info and launching client"));
   162 	CSchItemRefArray* refs = new (ELeave) CSchItemRefArray(3);
   163 	CleanupStack::PushL(refs);
   164 	res = TheScheduler.GetScheduleRefsL(*refs, EAllSchedules); 
   165 	TEST2(res, KErrNone);
   166 	TInt count = refs->Count();
   167 	TEST2(count, 1); 
   168 	scheduleHandle.iHandle = refs->At(0).iHandle;
   169 	
   170 	res = TheScheduler.GetTaskRefsL(*refs, EAllSchedules, EAllTasks);
   171 	TEST2(res, KErrNone);
   172 	count = refs->Count();
   173 	TEST2(count, 1); 
   174 	taskInfo.iTaskId = refs->At(0).iHandle;
   175 	
   176 	CleanupStack::PopAndDestroy(refs);
   177 	
   178 	// Client operations should fail with KErrPermissionDenied
   179 	TheTest.Next(_L("Launch client1 again"));
   180 	LaunchClient(scheduleHandle.iHandle, 
   181 					taskInfo.iTaskId, 
   182 					KProcess1, 
   183 					KErrPermissionDenied,
   184 					0,
   185 					0);
   186 
   187 	// Try with client with no 3rd UID.
   188 	// Client operations should fail with KErrPermissionDenied
   189 	TheTest.Next(_L("Launch client3"));
   190 	LaunchClient(scheduleHandle.iHandle, 
   191 					taskInfo.iTaskId, 
   192 					KProcess3, 
   193 					KErrPermissionDenied,
   194 					0,
   195 					0);
   196 	
   197 	// Now try with a client with WriteDeviceData.
   198 	// All operations should succeed
   199 	TheTest.Next(_L("Launch client2"));
   200 	LaunchClient(scheduleHandle.iHandle, 
   201 					taskInfo.iTaskId, 
   202 					KProcess2, 
   203 					KErrNone,
   204 					1,
   205 					1);
   206 	
   207 	TheScheduler.Close();
   208 	
   209 	SchSvrHelpers::Pause(TheTest);
   210 	}
   211 
   212 /**
   213 @SYMTestCaseID SYSLIB-SCHSVR-CT-0026
   214 @SYMTestCaseDesc Check that the scheduled exe receives the security info of the schedule creator.
   215 @SYMTestPriority High
   216 @SYMTestActions  Check that the scheduled exe receives the security info of the schedule creator.
   217 @SYMTestExpectedResults The scheduled exe receives the security info of the schedule creator.
   218 @SYMPREQ 277 Ensure integrity of Symbian OS handsets
   219 */
   220 static void DoTest2L()
   221 	{
   222 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0026 platformsec - TSecurityInfo validity "));
   223 	TheTest.Next(_L("Connect to Scheduler"));
   224 	TInt res = TheScheduler.Connect();
   225 	TEST2(res, KErrNone);
   226 
   227 	TheTest.Next(_L("Registering Client"));
   228 	TFileName filename;
   229 	filename = _L("PlatSecTaskHandler");
   230 	TEST2(TheScheduler.Register(filename, 27), KErrNone);
   231 
   232 	// Create a schedule
   233 	TheTest.Next(_L("Creating schedule"));
   234 	TSchedulerItemRef scheduleHandle;
   235 	TTime time;
   236 	time.HomeTime();
   237 	time += TTimeIntervalSeconds(2);
   238 	User::LeaveIfError(CreateScheduleL(scheduleHandle, TheScheduler, time));  
   239 
   240 	// Add a task to the schedule
   241 	TheTest.Next(_L("Creating task for schedule"));
   242 	TTaskInfo taskInfo;
   243 	taskInfo.iName = KPlatSecTaskName();
   244 	taskInfo.iPriority = 2;
   245 	taskInfo.iTaskId = 0;
   246 	taskInfo.iRepeat = 1;
   247 	HBufC* data = KPlatSecTaskData().AllocLC();
   248 	res = TheScheduler.ScheduleTask(taskInfo, *data, scheduleHandle.iHandle);
   249 	CleanupStack::PopAndDestroy(); // data
   250 	TEST2(res, KErrNone);
   251 	
   252 	TheTest.Next(_L("Wait for task to fire"));
   253 	TEST2(STaskSemaphore::WaitL(KDefaultTimeout), KErrNone);
   254 		
   255 	SchSvrHelpers::Pause(TheTest);
   256 	}
   257 
   258 /**
   259 @SYMTestCaseID SYSLIB-SCHSVR-CT-1885
   260 @SYMTestCaseDesc Check that TTaskInfo states correctly persisted
   261 @SYMTestPriority High
   262 @SYMTestActions Setup a schedule and task and then close the server, reconnect and check that the schedule and task
   263 				info has been persisted correctly.
   264 @SYMTestExpectedResults The test must not panic or fail.
   265 @SYMDEF INC093573
   266 */	
   267 static void DoTest3L()
   268 	{	
   269 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-1885 INC093573: Symbian provided task scheduler (RScheduler) loses parts of pending schedule inf "));
   270 	TheTest.Next(_L("Connect to Scheduler"));
   271 	TInt res = TheScheduler.Connect();
   272 	TEST2(res, KErrNone);
   273 
   274 	TheTest.Next(_L("Registering Client"));
   275 	TEST2(SchSvrHelpers::RegisterClientL(TheScheduler), KErrNone);
   276 
   277 	// Create a schedule
   278 	TheTest.Next(_L("Creating schedule"));
   279 	TSchedulerItemRef scheduleHandle;
   280 	TTime time;
   281 	time.HomeTime();
   282 	time += TTimeIntervalHours(1);  //Task to go off one hour from now (ie we dont 
   283 									//want it going off for the purposes of this test)
   284 	User::LeaveIfError(CreateScheduleL(scheduleHandle, TheScheduler, time));  
   285 
   286 	// Add a task to the schedule
   287 	TheTest.Next(_L("Creating task for schedule"));
   288 	TTaskInfo taskInfo;
   289 	taskInfo.iName = _L("INC093573");
   290 	taskInfo.iPriority = 2;
   291 	taskInfo.iTaskId = 0;
   292 	taskInfo.iRepeat = -1;
   293 	HBufC* data = _L("Tasks Data").AllocLC();
   294 	res = TheScheduler.ScheduleTask(taskInfo, *data, scheduleHandle.iHandle);
   295 	CleanupStack::PopAndDestroy(); // data
   296 	TEST2(res, KErrNone);
   297 	
   298 	TheScheduler.Close();
   299 
   300 	// Now shut down the server and restart.
   301 	TheTest.Next(_L("shuting down and restarting server"));
   302 	CleanupHelpers::TestCleanupL();
   303 	res = TheScheduler.Connect();
   304 	TEST2(res, KErrNone);
   305 	TEST2(SchSvrHelpers::RegisterClientL(TheScheduler), KErrNone);
   306 	
   307 	//Check that the task is still there with all the correct settings
   308 	TheTest.Next(_L("Checking schedule info and launching client"));
   309 	TTaskInfo returnedInfo;
   310 	TSchedulerItemRef returnedItemRef;
   311 	TTsTime returnedTsTime;
   312 	TInt size=0;
   313 	res=TheScheduler.GetTaskDataSize(0,size);
   314 	TEST2(res,KErrNone);	
   315 	HBufC* buffer=HBufC::NewL(size);
   316 	TPtr modifiableBuffer=buffer->Des();
   317 
   318 	//now get the task info and compare to the original submitted to ensure that it is properly
   319 	//externalized when server shut down.
   320 	res=TheScheduler.GetTaskInfoL(0,returnedInfo,modifiableBuffer,returnedItemRef,returnedTsTime);
   321 	TEST2(res,KErrNone);
   322 	TEST2(returnedInfo.iPriority,taskInfo.iPriority);
   323 	TEST2(returnedInfo.iRepeat,taskInfo.iRepeat);	
   324 	TEST2(returnedInfo.iTaskId,taskInfo.iTaskId);
   325 	TEST2(returnedInfo.iName.Compare(taskInfo.iName),0);			
   326 
   327 	//now clear the buffer
   328 	delete buffer;
   329 	}
   330 
   331 static TInt RunTestsL()
   332 	{
   333 	TheTest.Next(_L("Delete old files"));
   334 	SchSvrHelpers::DeleteScheduleFilesL();
   335 
   336 	TheTest.Next(_L("Create Task notification semaphore"));
   337 	//initialise task notification semaphore
   338 	STaskSemaphore sem;
   339 	sem.CreateL();
   340 
   341 	// Connect to the server
   342 	TheTest.Next(_L("===== Connect to Scheduler ====="));
   343 	TInt res = TheScheduler.Connect();
   344 	TEST2(res, KErrNone);
   345 
   346 	// Register a client with the server
   347 	TheTest.Next(_L("===== Registering Client ====="));
   348 	res = SchSvrHelpers::RegisterClientL(TheScheduler);
   349 	TEST2(res, KErrNone);
   350 	
   351 	CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
   352 	CleanupStack::PushL(scheduler);
   353 	CActiveScheduler::Install(scheduler);
   354 	
   355 	TheTest.Next(_L("Start tests"));
   356 	
   357 	if(PlatSec::IsCapabilityEnforced(ECapabilityWriteDeviceData))
   358 	 	DoTest1L();
   359  	DoTest2L();
   360 	DoTest3L();
   361 	TheTest.Next(_L("Tidying up"));
   362 	CleanupStack::PopAndDestroy(scheduler);
   363 	//close handle to semaphore
   364 	sem.Close();
   365 	
   366 	//Tidying up so next test will be clear.
   367 	TheTest.Next(_L("Delete all schedules"));
   368 	SchSvrHelpers::DeleteAllSchedulesL(TheScheduler);
   369 	SchSvrHelpers::Pause(TheTest, 2);
   370 	TheTest.Next(_L("Delete old files\n"));
   371 	SchSvrHelpers::DeleteScheduleFilesL();
   372 
   373 	TheScheduler.Close();
   374 	return KErrNone;
   375 	}
   376 
   377 GLDEF_C TInt E32Main()
   378     {
   379 	__UHEAP_MARK;
   380 	TheTest.Start(_L("TC_TSCH_PLATSEC"));
   381 	TheTest.Title();
   382 
   383 	TheCleanup = CTrapCleanup::New();
   384 	//If the previous test fails, SCHSVR.exe may stay in memory.
   385 	TRAPD(error, CleanupHelpers::TestCleanupL());
   386 	TEST2(error, KErrNone);
   387 
   388 	TheTest(TheFsSession.Connect() == KErrNone);
   389 	
   390 	TRAP(error, RunTestsL());	
   391 	TEST2(error,KErrNone);
   392 	
   393 	TRAP(error,CleanupHelpers::TestCleanupL());
   394 	TEST2(error, KErrNone);
   395 	delete TheCleanup;	
   396 	
   397 	TheFsSession.Close();
   398 	TheTest.End();
   399 	TheTest.Close();
   400 	__UHEAP_MARKEND;
   401 
   402 	return KErrNone;
   403 	}