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