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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
23 #include "TestUtils.h"
24 #include "platsectaskcommon.h"
26 _LIT(KTestName, "Task Scheduler platform security Test");
28 _LIT(KProcess1, "tschsvrclient1");
29 _LIT(KProcess2, "tschsvrclient2");
30 _LIT(KProcess3, "tschsvrclient3");
32 RTest TheTest(KTestName);
34 typedef CArrayFixFlat<TScheduleEntryInfo> CSchEntryInfoArray;
35 typedef CArrayFixFlat<TTaskInfo> CTaskInfoArray;
36 typedef CArrayFixFlat<TSchedulerItemRef> CSchItemRefArray;
38 static RScheduler TheScheduler;
39 static CTrapCleanup* TheCleanup;
40 static RFs TheFsSession;
42 //creates a daily schedule with StartTime of aStartTime
43 static TInt CreateScheduleL(TSchedulerItemRef& aRef,
44 RScheduler& aScheduler,
45 const TTime& aStartTime)
47 CSchEntryInfoArray* entryList
48 = new (ELeave) CSchEntryInfoArray(1);
49 CleanupStack::PushL(entryList);
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
62 static void LaunchClient(TInt aScheduleHandle,
69 // now launch process to try and access schedule
75 id.AppendNum(aScheduleHandle);
77 id.AppendNum(aTaskHandle);
79 id.AppendNum(aErrorCondition);
81 id.AppendNum(aScheduleCount);
83 id.AppendNum(aTaskCount);
86 TInt res = client.Create(aClient, id);
88 // Asynchronous logon: completes when process terminates with process exit code
92 User::WaitForRequest(stat);
94 TInt exitReason = client.ExitReason();
95 TEST2(exitReason, KErrNone);
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
108 static void DoTest1L()
111 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-0025 platformsec - API policing "));
113 TheTest.Next(_L("Connect to Scheduler"));
114 TInt res = TheScheduler.Connect();
115 TEST2(res, KErrNone);
117 TheTest.Next(_L("Registering Client"));
118 TEST2(SchSvrHelpers::RegisterClientL(TheScheduler), KErrNone);
121 TheTest.Next(_L("Creating schedule"));
122 TSchedulerItemRef scheduleHandle;
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));
129 // Add a task to the schedule
130 TheTest.Next(_L("Creating task for schedule"));
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);
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,
148 KErrPermissionDenied,
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);
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();
168 scheduleHandle.iHandle = refs->At(0).iHandle;
170 res = TheScheduler.GetTaskRefsL(*refs, EAllSchedules, EAllTasks);
171 TEST2(res, KErrNone);
172 count = refs->Count();
174 taskInfo.iTaskId = refs->At(0).iHandle;
176 CleanupStack::PopAndDestroy(refs);
178 // Client operations should fail with KErrPermissionDenied
179 TheTest.Next(_L("Launch client1 again"));
180 LaunchClient(scheduleHandle.iHandle,
183 KErrPermissionDenied,
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,
193 KErrPermissionDenied,
197 // Now try with a client with WriteDeviceData.
198 // All operations should succeed
199 TheTest.Next(_L("Launch client2"));
200 LaunchClient(scheduleHandle.iHandle,
207 TheScheduler.Close();
209 SchSvrHelpers::Pause(TheTest);
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
220 static void DoTest2L()
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);
227 TheTest.Next(_L("Registering Client"));
229 filename = _L("PlatSecTaskHandler");
230 TEST2(TheScheduler.Register(filename, 27), KErrNone);
233 TheTest.Next(_L("Creating schedule"));
234 TSchedulerItemRef scheduleHandle;
237 time += TTimeIntervalSeconds(2);
238 User::LeaveIfError(CreateScheduleL(scheduleHandle, TheScheduler, time));
240 // Add a task to the schedule
241 TheTest.Next(_L("Creating task for schedule"));
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);
252 TheTest.Next(_L("Wait for task to fire"));
253 TEST2(STaskSemaphore::WaitL(KDefaultTimeout), KErrNone);
255 SchSvrHelpers::Pause(TheTest);
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.
267 static void DoTest3L()
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);
274 TheTest.Next(_L("Registering Client"));
275 TEST2(SchSvrHelpers::RegisterClientL(TheScheduler), KErrNone);
278 TheTest.Next(_L("Creating schedule"));
279 TSchedulerItemRef scheduleHandle;
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));
286 // Add a task to the schedule
287 TheTest.Next(_L("Creating task for schedule"));
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);
298 TheScheduler.Close();
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);
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;
313 res=TheScheduler.GetTaskDataSize(0,size);
315 HBufC* buffer=HBufC::NewL(size);
316 TPtr modifiableBuffer=buffer->Des();
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);
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);
327 //now clear the buffer
331 static TInt RunTestsL()
333 TheTest.Next(_L("Delete old files"));
334 SchSvrHelpers::DeleteScheduleFilesL();
336 TheTest.Next(_L("Create Task notification semaphore"));
337 //initialise task notification semaphore
341 // Connect to the server
342 TheTest.Next(_L("===== Connect to Scheduler ====="));
343 TInt res = TheScheduler.Connect();
344 TEST2(res, KErrNone);
346 // Register a client with the server
347 TheTest.Next(_L("===== Registering Client ====="));
348 res = SchSvrHelpers::RegisterClientL(TheScheduler);
349 TEST2(res, KErrNone);
351 CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
352 CleanupStack::PushL(scheduler);
353 CActiveScheduler::Install(scheduler);
355 TheTest.Next(_L("Start tests"));
357 if(PlatSec::IsCapabilityEnforced(ECapabilityWriteDeviceData))
361 TheTest.Next(_L("Tidying up"));
362 CleanupStack::PopAndDestroy(scheduler);
363 //close handle to semaphore
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();
373 TheScheduler.Close();
377 GLDEF_C TInt E32Main()
380 TheTest.Start(_L("TC_TSCH_PLATSEC"));
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);
388 TheTest(TheFsSession.Connect() == KErrNone);
390 TRAP(error, RunTestsL());
391 TEST2(error,KErrNone);
393 TRAP(error,CleanupHelpers::TestCleanupL());
394 TEST2(error, KErrNone);
397 TheFsSession.Close();