Update contrib.
1 // Copyright (c) 2007-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 <e32property.h>
24 #include <schinfointernal.h>
25 #include "TestUtils.h"
27 _LIT(KTestName, "Task Scheduler Robustness Test");
28 _LIT(KTaskScheduler, "schexe");
30 RTest TheTest(KTestName);
32 typedef CArrayFixFlat<TTaskInfo> CTaskInfoArray;
33 typedef CArrayFixFlat<TSchedulerItemRef> CSchItemRefArray;
34 typedef CArrayFixFlat<TTaskSchedulerCondition> CSchConditionArray;
36 static RScheduler TheScheduler;
37 static CTrapCleanup* TheCleanup;
38 static RFs TheFsSession;
40 const TInt KTestKey1 = 1;
42 _LIT(KSeparator, "|"); // Invalid filepath char used to separate filenames
44 // This function launches the TPropertyDefine process which
45 // has WriteDeviceData Capabilities enabling it to create the P&S
46 // variables used by this test.
47 static void LaunchHelperL(TUid aCategory, TInt aKey, TInt aAttr)
49 _LIT(KConditionHelper, "TPropertyDefine");
54 args.AppendNum(aCategory.iUid);
55 args.Append(KSeparator);
57 args.Append(KSeparator);
58 args.AppendNum(aAttr);
60 User::LeaveIfError(p.Create(KConditionHelper, args,EOwnerProcess));
62 // Asynchronous logon: completes when process terminates with process exit code
66 User::WaitForRequest(stat);
67 TInt exitReason = p.ExitReason();
69 User::LeaveIfError(exitReason);
73 static void CreateTestVariables()
75 LaunchHelperL(KUidSystemCategory, KTestKey1,RProperty::EInt);
78 static void ResetVariablesL(TInt aKey1Val)
80 User::LeaveIfError(RProperty::Set(KUidSystemCategory, KTestKey1,aKey1Val));
84 // single condition with default time set to 1 year in the future
85 static TInt CreateScheduleL(TSchedulerItemRef& aRef,
86 RScheduler& aScheduler,
87 const TUid& aConditionUID,
90 aRef.iName = _L("Schedule created using CreateScheduleSingle");
92 CSchConditionArray* conditionList = new (ELeave) CSchConditionArray(1);
93 CleanupStack::PushL(conditionList);
95 //create a single condition
96 TTaskSchedulerCondition condition1;
97 condition1.iCategory = aConditionUID;
98 condition1.iKey = aConditionUInt;
99 condition1.iState = 10;
100 condition1.iType = TTaskSchedulerCondition::EEquals;
102 conditionList->AppendL(condition1);
104 //create a persistent schedule
105 TTime time = SchSvrHelpers::TimeBasedOnOffset(0, 0, 0, 0, 0, 1); //1 year in the future
106 TInt res = aScheduler.CreatePersistentSchedule(aRef, *conditionList, time);
107 CleanupStack::PopAndDestroy(); // conditionList
111 //Add a single task to a schedule
112 static TInt AddTaskToScheduleL(const TDesC& aName,
115 RScheduler& aScheduler)
118 taskInfo.iTaskId = aNewId;
119 taskInfo.iName = aName;
120 taskInfo.iPriority = 2;
121 taskInfo.iRepeat = 0;
122 HBufC* data = _L("the data").AllocLC();
123 TInt res = aScheduler.ScheduleTask(taskInfo, *data, aScheduleId);
124 aNewId = taskInfo.iTaskId;
126 CleanupStack::PopAndDestroy(); // data
131 static TInt ScheduleTaskL()
133 //reset the p&s variables before creating the schedule
137 TSchedulerItemRef ref1;
138 TheTest.Printf(_L("Create a schedule\n"));
139 TInt res = CreateScheduleL(ref1, TheScheduler, KUidSystemCategory, KTestKey1);
140 TEST2(res, KErrNone);
142 //Add task to the schedule
144 _LIT(KName1, "Test Task");
145 TheTest.Printf(_L("Schedule a task\n"));
147 res = AddTaskToScheduleL(KName1, task1, ref1.iHandle, TheScheduler);
148 TEST2(res, KErrNone);
153 static void ExecuteTaskL()
155 TheTest.Printf(_L("Execute Task\n"));
156 //Set property causing schedule to be run
157 User::LeaveIfError(RProperty::Set(KUidSystemCategory, KTestKey1,10));
159 //Pause to wait for the task to be executed
160 SchSvrHelpers::Pause(TheTest, 2);
164 static TInt ScheduleAndExecuteTaskL()
166 TInt res = ScheduleTaskL();
173 LOCAL_C void AddTaskFunctionL()
175 RScheduler localScheduler;
176 // Connect to the server
177 TInt res = localScheduler.Connect();
178 TEST2(res, KErrNone);
179 //Schedule a task and execute it
180 //reset the p&s variables before creating the schedule
184 TSchedulerItemRef ref1;
185 ref1.iName = _L("Schedule created using CreateScheduleSingle");
186 CSchConditionArray* conditionList = new (ELeave) CSchConditionArray(1);
187 CleanupStack::PushL(conditionList);
189 //create a single condition
190 TTaskSchedulerCondition condition1;
191 condition1.iCategory = KUidSystemCategory;
192 condition1.iKey = KTestKey1;
193 condition1.iState = 10;
194 condition1.iType = TTaskSchedulerCondition::EEquals;
196 conditionList->AppendL(condition1);
198 //create a persistent schedule
199 TTime time = SchSvrHelpers::TimeBasedOnOffset(0, 0, 0, 0, 0, 1); //1 year in the future
200 res = localScheduler.CreatePersistentSchedule(ref1, *conditionList, time);
201 CleanupStack::PopAndDestroy(); // conditionList
203 //Add task to the schedule
205 _LIT(KName1, "Test Task");
208 taskInfo.iTaskId = task1;
209 taskInfo.iName = KName1;
210 taskInfo.iPriority = 2;
211 taskInfo.iRepeat = 0;
212 HBufC* data = _L("the data").AllocLC();
214 TInt ret = localScheduler.ScheduleTask(taskInfo, *data, ref1.iHandle);
215 TEST2(ret, 0);//EPanicNotRegistered == 0
217 task1 = taskInfo.iTaskId;
219 CleanupStack::PopAndDestroy(); // data
221 //Tidying up so next test will be clear.
222 SchSvrHelpers::DeleteAllSchedulesL(localScheduler);
224 localScheduler.Close();
227 // Helper function for DEF124488
228 LOCAL_C TInt TestPanicThread(TAny*)
230 CTrapCleanup* cleanup = CTrapCleanup::New();
235 TRAPD(err,AddTaskFunctionL())
243 @SYMTestCaseID SYSLIB-SCHSVR-CT-3369
244 @SYMTestCaseDesc Test deletion of temporary files with non existent client
245 @SYMTestPriority High
246 @SYMTestActions Schedule a task with a client that does not exist.
247 Ensure that all temporary files are deleted after
249 @SYMTestExpectedResults All temporary files should be deleted by task scheduler
252 static void DoTest1L()
254 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-3369 Test handling of non existent client "));
255 // Connect to the server
256 TheTest.Next(_L("===== Connect to Scheduler ====="));
257 TInt res = TheScheduler.Connect();
258 TEST2(res, KErrNone);
260 // Register a client with the server - this client does not exist
261 TheTest.Next(_L("===== Registering Client ====="));
262 res = SchSvrHelpers::RegisterNonExistentClient(TheScheduler);
263 TEST2(res, KErrNone);
265 //Schedule a task and execute it
266 ScheduleAndExecuteTaskL();
268 // Check for left task files after scheduled tasks completed
269 // To access private data cage, uses SchSvrHelplers::CheckTaskFilesL()
270 TheTest.Next(_L("Now checking no files left when tasks completed"));
271 TInt err = SchSvrHelpers::CheckTaskFilesL();
273 // If there's any task files left, test fails with error code KErrGeneral
274 TEST(err == KErrNone);
275 TheTest.Next(_L("All files deleted as expected..."));
277 //Tidying up so next test will be clear.
278 TheTest.Next(_L("Delete all schedules"));
279 SchSvrHelpers::DeleteAllSchedulesL(TheScheduler);
281 TheScheduler.Close();
286 @SYMTestCaseID SYSLIB-SCHSVR-CT-3370
287 @SYMTestCaseDesc Test deletion of temporary files with faulty client
288 @SYMTestPriority High
289 @SYMTestActions Schedule a task with a client that panics and does not
290 release the temporary file handle.
291 Ensure that all temporary files are deleted after schedule excecutes
292 @SYMTestExpectedResults All temporary files should be deleted by task scheduler
295 static void DoTest2L()
297 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-3370 Test handling of panicing client "));
299 // Connect to the server
300 TheTest.Next(_L("===== Connect to Scheduler ====="));
301 TInt res = TheScheduler.Connect();
302 TEST2(res, KErrNone);
304 // Register a client with the server - this client panics
305 //after calling RFile::AdoptFromClient
306 TheTest.Next(_L("===== Registering Client ====="));
307 res = SchSvrHelpers::RegisterPanicingClient(TheScheduler);
308 TEST2(res, KErrNone);
310 //Schedule a task and execute it - we expect the client to panic
311 ScheduleAndExecuteTaskL();
313 // Check for left task files after scheduled tasks completed
314 // To access private data cage, uses SchSvrHelplers::CheckTaskFilesL()
315 TheTest.Next(_L("Now checking no files left when tasks completed"));
316 TInt err = SchSvrHelpers::CheckTaskFilesL();
318 // If there's any task files left, test fails with error code KErrGeneral
319 TEST(err == KErrNone);
321 TheTest.Next(_L("All files deleted as expected..."));
323 //Tidying up so next test will be clear.
324 TheTest.Next(_L("Delete all schedules"));
325 SchSvrHelpers::DeleteAllSchedulesL(TheScheduler);
327 TheScheduler.Close();
331 @SYMTestCaseID SYSLIB-SCHSVR-CT-3371
332 @SYMTestCaseDesc Test deletion of temporary files on task scheduler startup
333 @SYMTestPriority High
334 @SYMTestActions Create temporary files in the task schedulers private data cage.
335 Start the task scheduler and verify that these files are deleted.
336 @SYMTestExpectedResults All temporary files should be deleted by task scheduler on startup
339 static void DoTest3L()
341 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-3371 Test deletion of temporary files on startup "));
343 //Connect to the scheduler
344 TInt res = TheScheduler.Connect();
345 TEST2(res, KErrNone);
347 // Kill the server to ensure we restart it when we connect
348 res = CleanupHelpers::KillProcess(KTaskScheduler);
349 TEST2(res, KErrNone);
350 TheScheduler.Close();
352 // Create task files to test cleanup
353 // To access private data cage, uses SchSvrHelplers::CreateTaskFilesL()
354 TheTest.Next(_L("Creating dummy task files"));
355 res = SchSvrHelpers::CreateTaskFilesL();
357 //Restart the scheduler which should clean up temp files on startup
358 TheTest.Next(_L("===== Connect to Scheduler ====="));
359 res = TheScheduler.Connect();
360 TEST2(res, KErrNone);
362 //wait for the server to start up
363 SchSvrHelpers::Pause(TheTest, 2);
365 TheScheduler.Close();
367 // Check for left task files after scheduled tasks completed
368 // To access private data cage, uses SchSvrHelplers::CheckTaskFilesL()
369 TheTest.Next(_L("Now checking no files left after task scheduler starts"));
370 res = SchSvrHelpers::CheckTaskFilesL();
372 TEST2(res, KErrNone);
374 TheTest.Next(_L("All files deleted as expected..."));
378 @SYMTestCaseID SYSLIB-SCHSVR-CT-3402
379 @SYMTestCaseDesc Test memory cleanup on Task Scheduler exit
380 @SYMTestPriority High
381 @SYMTestActions Start the scheduler and register a client.
382 Execute a schedule and then terminate the scheduler.
383 When the scheduler is restarted it should exit as there are no
384 pending schedules. On exit all allocated memory should be freed
385 @SYMTestExpectedResults All allocated memory should be freed when the scheduler exits
388 static void DoTest4L()
391 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-3402 Test memory cleanup on Task Scheduler exit "));
393 // Connect to the server
394 TheTest.Next(_L("===== Connect to Scheduler ====="));
395 TInt res = TheScheduler.Connect();
396 TEST2(res, KErrNone);
398 // Register a client with the server
399 TheTest.Next(_L("===== Registering Client ====="));
400 res = SchSvrHelpers::RegisterClientL(TheScheduler);
401 TEST2(res, KErrNone);
403 //Schedule a task and execute it
404 ScheduleAndExecuteTaskL();
407 res = CleanupHelpers::KillProcess(KTaskScheduler);
408 TEST2(res, KErrNone);
410 TheScheduler.Close();
412 //Restarting with a registered client and no schedule should
413 //cause the server to exit. Ther server should free all allocated
414 //memory. If all memory is not freed, heap check macros within
415 // the task scheduler code will cause a panic
416 SchSvrHelpers::LaunchTaskSchedulerL();
418 //wait for the server to exit
419 SchSvrHelpers::Pause(TheTest, 2);
421 //Verify that the server has already exited - there are two valid
422 //error codes depending on how quickly the process is cleaned up
423 //KErrDied - Process is dead but hasn't been cleaned up yet by the kernel
424 //KErrNotFound - Process has been cleaned up
425 res = CleanupHelpers::KillProcess(KTaskScheduler);
427 TEST((res == KErrDied)||(res == KErrNotFound));
431 TheTest.Next(_L("All memory freed..."));
436 @SYMTestCaseID SYSLIB-SCHSVR-CT-3412
437 @SYMTestCaseDesc Test Task Scheduler startup with pending schedule
438 @SYMTestPriority High
439 @SYMTestActions Start the scheduler and register a client.
440 Create a scheduled task and then terminate the scheduler without executing the task.
441 When the scheduler is restarted it should not exit as there is a
442 pending schedule. Verify that the scheduler is still active by executing
444 @SYMTestExpectedResults The task scheduler should not exit and the schedule should execute
447 static void DoTest5L()
450 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-3412 Test Task Scheduler startup with pending schedule "));
452 // Connect to the server
453 TheTest.Next(_L("===== Connect to Scheduler ====="));
454 TInt res = TheScheduler.Connect();
455 TEST2(res, KErrNone);
457 // Register a client with the server
458 TheTest.Next(_L("===== Registering Client ====="));
459 res = SchSvrHelpers::RegisterClientL(TheScheduler);
460 TEST2(res, KErrNone);
466 res = CleanupHelpers::KillProcess(KTaskScheduler);
467 TEST2(res, KErrNone);
468 TheScheduler.Close();
470 TheTest.Next(_L("Create Task notification semaphore"));
471 //initialise task notification semaphore
475 //Restart the scheduler - task scheduler should not exit as there is a
477 res = SchSvrHelpers::LaunchTaskSchedulerL();
478 TEST2(res, KErrNone);
480 //Execute task and wait for it to run - this would not succeed
481 //if task scheduler had exited above
483 TEST2(STaskSemaphore::WaitL(KDefaultTimeout), KErrNone);
485 //Kill the process and verify that the scheduler was active
486 //If the task scheduler isnt active when we try to kill it
487 //KillProcess would return KErrDied
488 res = CleanupHelpers::KillProcess(KTaskScheduler);
489 TEST2(res, KErrNone);
491 //close handle to semaphore
499 @SYMTestCaseID SYSLIB-SCHSVR-CT-4010
500 @SYMTestCaseDesc Test that adding a task using an unregistered client panics the client and does not crash the server.
501 @SYMTestPriority High
502 @SYMTestActions Schedule a task with a client that has not been registered.
503 @SYMTestExpectedResults Client should be panicked.
506 static void DoTest6L()
508 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SCHSVR-CT-4010 Test handling of unregistered client Should Panic Client thread "));
511 _LIT(KThreadName, "PanicClientThread");
513 testThread.Create(KThreadName, TestPanicThread, KDefaultStackSize, 0x1000, 0x100000, NULL);
515 TRequestStatus requestStatus;
516 // Request notification when the thread terminates
517 testThread.Logon(requestStatus);
519 TBool justInTime=User::JustInTime();
520 User::SetJustInTime(EFalse);
521 // Let the thread execute
524 // Wait for termination
525 User::WaitForRequest(requestStatus);
526 User::SetJustInTime(justInTime);
528 TEST2(testThread.ExitReason(), 0);
533 static TInt RunTestsL()
535 TheTest.Next(_L("Delete old files"));
536 SchSvrHelpers::DeleteScheduleFilesL();
538 //create P&S variables for the test
539 CreateTestVariables();
541 TheTest.Next(_L("Start tests"));
543 CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
544 CleanupStack::PushL(scheduler);
545 CActiveScheduler::Install(scheduler);
554 TheTest.Next(_L("Tidying up"));
555 CleanupStack::PopAndDestroy(scheduler);
560 GLDEF_C TInt E32Main()
563 TheTest.Start(_L("TC_TSCH_ROBUSTNESS"));
565 TheCleanup = CTrapCleanup::New();
567 //If the previous test fails, SCHSVR.exe may stay in memory.
568 TRAPD(error,CleanupHelpers::TestCleanupL());
569 TEST2(error, KErrNone);
570 TheTest(TheFsSession.Connect() == KErrNone);;
571 TRAP(error, RunTestsL());
572 TEST2(error, KErrNone);
573 TRAP(error,CleanupHelpers::TestCleanupL());
574 TEST2(error, KErrNone);
577 TheFsSession.Close();