Update contrib.
1 // Copyright (c) 2001-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 the License "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.
14 // e32test\secure\t_sprocess.cpp
16 // Test the platform security aspects of the RProcess class
20 // - Test SetJustInTime on the current process, a new un-Resumed process
21 // and between processes. Verify results are as expected.
22 // - Test process renaming and verify results are as expected.
23 // - Test killing, terminating and panicking different processes, verify
24 // results are as expected.
25 // - Test resuming a process from a different process, verify results.
26 // - Test setting process priority in a variety of ways, verify results
28 // - Test the RProcess SetType(), SetProtected(), CommandLineLength(),
29 // CommandLine(), SetSystem(), SetOwner() and Owner() methods. Verify
30 // results are as expected.
31 // Platforms/Drives/Compatibility:
33 // Assumptions/Requirement/Pre-requisites:
34 // Failures and causes:
35 // Base Port information:
41 LOCAL_D RTest test(_L("T_SPROCESS"));
43 _LIT(KSyncMutext,"T_SPROCESS-sync-mutex");
49 if(syncMutex.OpenGlobal(KSyncMutext)!=KErrNone)
56 enum TTestProcessFunctions
59 ETestProcessSetJustInTime,
61 ETestProcessTerminate,
64 ETestProcessTerminateSelf,
65 ETestProcessPanicSelf,
68 ETestProcessPriorityControlOff,
69 ETestProcessPriorityControlOn,
70 ETestProcessPriorityControlOnAndLowPriority,
71 ETestProcessSetPriority,
74 #include "testprocess.h"
76 _LIT(KTestPanicCategory,"TEST PANIC");
77 _LIT(KTestProcessName,"TestName");
78 _LIT(KTestProcessName2,"TestName2");
81 TInt DoTestProcess(TInt aTestNum,TInt aArg1,TInt aArg2)
89 case ETestProcessNull:
93 case ETestProcessSetJustInTime:
95 r = process.Open(aArg1);
97 process.SetJustInTime(!process.JustInTime()); // Should panic us
101 case ETestProcessResume:
103 r = process.Open(aArg1);
105 process.Resume(); // Should panic us
109 case ETestProcessKill:
111 r = process.Open(aArg1);
117 case ETestProcessTerminate:
119 r = process.Open(aArg1);
121 process.Terminate(999);
125 case ETestProcessPanic:
127 r = process.Open(aArg1);
129 process.Panic(KTestPanicCategory,999);
133 case ETestProcessKillSelf:
135 RProcess().Kill(999);
139 case ETestProcessTerminateSelf:
141 RProcess().Terminate(999);
145 case ETestProcessPanicSelf:
147 RProcess().Panic(KTestPanicCategory,999);
151 case ETestProcessSetPriority:
153 r = process.Open(aArg1);
155 process.SetPriority((TProcessPriority)aArg2);
160 case ETestProcessPriority:
162 r = process.Open(aArg1);
165 TProcessPriority priority;
166 priority = process.Priority();
167 process.SetPriority(EPriorityLow);
168 if(process.Priority()!=priority) // priority shouldn't have changed
170 process.SetPriority(EPriorityBackground);
171 if(process.Priority()!=priority) // priority shouldn't have changed
173 process.SetPriority(EPriorityForeground);
174 if(process.Priority()!=priority) // priority shouldn't have changed
179 case ETestProcessPriorityControlOnAndLowPriority:
180 RProcess().SetPriority(EPriorityLow);
182 case ETestProcessPriorityControlOn:
183 User::SetPriorityControl(ETrue);
185 case ETestProcessPriorityControlOff:
186 RProcess::Rendezvous(0);
191 User::Panic(_L("T_SPROCESS"),1);
199 void TestProcessForPlatformSecurityTrap(TTestProcessFunctions aFunction)
201 TRequestStatus logonStatus2;
202 RTestProcess process;
203 process.Create(~0u,aFunction,RProcess().Id(),EPriorityAbsoluteLow);
204 process.Logon(logonStatus2);
206 User::WaitForRequest(logonStatus2);
207 test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
208 test(logonStatus2==EPlatformSecurityTrap);
213 void TestSetJustInTime()
215 RTestProcess process;
216 RTestProcess process2;
217 TRequestStatus logonStatus;
219 test.Start(_L("Test SetJustInTime on current process"));
220 TBool jit = process.JustInTime();
221 process.SetJustInTime(!jit);
222 test((process.JustInTime()!=EFalse)!=(jit!=EFalse));
223 process.SetJustInTime(jit);
224 test((process.JustInTime()!=EFalse)==(jit!=EFalse));
226 test.Next(_L("Test SetJustInTime on a new un-Resumed process"));
227 process.RProcess::Create(RProcess().FileName(),_L(""));
228 jit = process.JustInTime();
229 process.SetJustInTime(!jit);
230 test((process.JustInTime()!=EFalse)!=(jit!=EFalse));
231 process.SetJustInTime(jit);
232 test((process.JustInTime()!=EFalse)==(jit!=EFalse));
234 CLOSE_AND_WAIT(process);
236 test.Next(_L("Try other process using SetJustInTime on our created process"));
237 process2.Create(0,ETestProcessNull);
238 process.Create(~0u,ETestProcessSetJustInTime,process2.Id());
239 process.Logon(logonStatus);
241 User::WaitForRequest(logonStatus);
242 test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
243 test(logonStatus==EPlatformSecurityTrap);
244 CLOSE_AND_WAIT(process);
246 CLOSE_AND_WAIT(process2);
248 test.Next(_L("Try other process to using SetJustInTime on us"));
249 process.Create(~0u,ETestProcessSetJustInTime);
250 process.Logon(logonStatus);
252 User::WaitForRequest(logonStatus);
253 test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
254 test(logonStatus==EPlatformSecurityTrap);
255 CLOSE_AND_WAIT(process);
266 test.Start(_L("Renaming the current process with User::RenameProcess"));
267 name = RProcess().Name();
268 name.SetLength(KTestProcessName().Length());
269 test(name.CompareF(KTestProcessName)!=0);
270 User::RenameProcess(KTestProcessName);
271 name = RProcess().Name();
272 name.SetLength(KTestProcessName().Length());
273 test(name.CompareF(KTestProcessName)==0);
283 RTestProcess process;
284 RTestProcess process2;
285 TRequestStatus logonStatus;
286 TRequestStatus logonStatus2;
288 process2.Create(0,ETestProcessNull);
289 process2.Logon(logonStatus2);
291 // Test RProcess::Kill()
293 test.Start(_L("Test killing an un-resumed process created by us"));
294 process.Create(0,ETestProcessNull);
295 process.Logon(logonStatus);
297 User::WaitForRequest(logonStatus);
298 test(process.ExitType()==EExitKill);
299 test(logonStatus==999);
300 CLOSE_AND_WAIT(process);
302 test.Next(_L("Try killing un-resumed process not created by self"));
303 process.Create(~(1u<<ECapabilityPowerMgmt),ETestProcessKill,process2.Id());
304 process.Logon(logonStatus);
306 User::WaitForRequest(logonStatus);
307 test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
308 test(logonStatus==EPlatformSecurityTrap);
309 test(logonStatus2==KRequestPending); // the process should still be alive
310 CLOSE_AND_WAIT(process);
312 test.Next(_L("Test a process killing itself"));
313 process.Create(0,ETestProcessKillSelf);
314 process.Logon(logonStatus);
316 User::WaitForRequest(logonStatus);
317 test(process.ExitType()==EExitKill);
318 test(logonStatus==999);
319 CLOSE_AND_WAIT(process);
321 test.Next(_L("Try killing running process"));
322 process.Create(~(1u<<ECapabilityPowerMgmt),ETestProcessKill,RProcess().Id());
323 process.Logon(logonStatus);
325 User::WaitForRequest(logonStatus);
326 test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
327 test(logonStatus==EPlatformSecurityTrap);
328 CLOSE_AND_WAIT(process);
330 // Test RProcess::Teminate()
332 test.Next(_L("Test terminating an un-resumed process created by us"));
333 process.Create(0,ETestProcessNull);
334 process.Logon(logonStatus);
335 process.Terminate(999);
336 User::WaitForRequest(logonStatus);
337 test(process.ExitType()==EExitTerminate);
338 test(logonStatus==999);
339 CLOSE_AND_WAIT(process);
341 test.Next(_L("Try terminating un-resumed process not created by self"));
342 process.Create(~(1u<<ECapabilityPowerMgmt),ETestProcessTerminate,process2.Id());
343 process.Logon(logonStatus);
345 User::WaitForRequest(logonStatus);
346 test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
347 test(logonStatus==EPlatformSecurityTrap);
348 test(logonStatus2==KRequestPending); // the process should still be alive
349 CLOSE_AND_WAIT(process);
351 test.Next(_L("Test a process terminating itself"));
352 process.Create(0,ETestProcessTerminateSelf);
353 process.Logon(logonStatus);
355 User::WaitForRequest(logonStatus);
356 test(process.ExitType()==EExitTerminate);
357 test(logonStatus==999);
358 CLOSE_AND_WAIT(process);
360 test.Next(_L("Try terminating running process"));
361 process.Create(~(1u<<ECapabilityPowerMgmt),ETestProcessTerminate,RProcess().Id());
362 process.Logon(logonStatus);
364 User::WaitForRequest(logonStatus);
365 test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
366 test(logonStatus==EPlatformSecurityTrap);
367 CLOSE_AND_WAIT(process);
369 // Test RProcess::Panic()
371 test.Next(_L("Test panicking an un-resumed process created by us"));
372 process.Create(0,ETestProcessNull);
373 process.Logon(logonStatus);
374 process.Panic(KTestPanicCategory,999);
375 User::WaitForRequest(logonStatus);
376 test(process.ExitType()==EExitPanic);
377 test(logonStatus==999);
378 CLOSE_AND_WAIT(process);
380 test.Next(_L("Try panicking un-resumed process not created by self"));
381 process.Create(~(1u<<ECapabilityPowerMgmt),ETestProcessPanic,process2.Id());
382 process.Logon(logonStatus);
384 User::WaitForRequest(logonStatus);
385 test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
386 test(logonStatus==EPlatformSecurityTrap);
387 test(logonStatus2==KRequestPending); // the process should still be alive
388 CLOSE_AND_WAIT(process);
390 test.Next(_L("Test a process panicking itself"));
391 process.Create(0,ETestProcessPanicSelf);
392 process.Logon(logonStatus);
394 User::WaitForRequest(logonStatus);
395 test(process.ExitType()==EExitPanic);
396 test(logonStatus==999);
397 CLOSE_AND_WAIT(process);
399 test.Next(_L("Try panicking running process"));
400 process.Create(~(1u<<ECapabilityPowerMgmt),ETestProcessPanic,RProcess().Id());
401 process.Logon(logonStatus);
403 User::WaitForRequest(logonStatus);
404 test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
405 test(logonStatus==EPlatformSecurityTrap);
406 CLOSE_AND_WAIT(process);
410 test(logonStatus2==KRequestPending); // the process should still be alive
412 User::WaitForRequest(logonStatus2);
413 test(logonStatus2==KErrNone);
414 CLOSE_AND_WAIT(process2);
416 // Checks with ECapabilityPowerMgmt
418 test.Next(_L("Test kill running process ECapabilityPowerMgmt"));
419 process2.Create(0,ETestProcessNull);
420 process2.Logon(logonStatus2);
421 process.Create((1<<ECapabilityPowerMgmt),ETestProcessKill,process2.Id());
422 process.Logon(logonStatus);
426 User::WaitForRequest(logonStatus);
427 test(process.ExitType()==EExitKill);
428 test(logonStatus==0);
429 CLOSE_AND_WAIT(process);
430 User::WaitForRequest(logonStatus2);
431 test(process2.ExitType()==EExitKill);
432 test(logonStatus2==999);
436 test.Next(_L("Test terminating running process ECapabilityPowerMgmt"));
437 process2.Create(0,ETestProcessNull);
438 process2.Logon(logonStatus2);
439 process.Create((1<<ECapabilityPowerMgmt),ETestProcessTerminate,process2.Id());
440 process.Logon(logonStatus);
444 User::WaitForRequest(logonStatus);
445 test(process.ExitType()==EExitKill);
446 test(logonStatus==0);
447 CLOSE_AND_WAIT(process);
448 User::WaitForRequest(logonStatus2);
449 test(process2.ExitType()==EExitTerminate);
450 test(logonStatus2==999);
451 CLOSE_AND_WAIT(process2);
454 test.Next(_L("Test panicking running process ECapabilityPowerMgmt"));
455 process2.Create(0,ETestProcessNull);
456 process2.Logon(logonStatus2);
457 process.Create((1<<ECapabilityPowerMgmt),ETestProcessPanic,process2.Id());
458 process.Logon(logonStatus);
462 User::WaitForRequest(logonStatus);
463 test(process.ExitType()==EExitKill);
464 test(logonStatus==0);
465 CLOSE_AND_WAIT(process);
466 User::WaitForRequest(logonStatus2);
467 test(process2.ExitType()==EExitPanic);
468 test(logonStatus2==999);
469 CLOSE_AND_WAIT(process2);
481 RTestProcess process;
482 RTestProcess process2;
483 TRequestStatus logonStatus;
484 TRequestStatus logonStatus2;
486 test.Start(_L("Try to get another process to resume one we've created"));
487 process2.Create(0,ETestProcessNull);
488 process2.Logon(logonStatus2);
489 process.Create(~0u,ETestProcessResume,process2.Id());
490 process.Logon(logonStatus);
492 User::WaitForRequest(logonStatus);
493 test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
494 test(logonStatus==EPlatformSecurityTrap);
495 User::After(1*1000*1000); // Give time for process to run (if it had been resumed)...
496 test(logonStatus2==KRequestPending); // It shouldn't have, so logon will be pending
498 CLOSE_AND_WAIT(process2);
500 // test.Next(_L("Test resuming a process we've created"));
502 // No code for this, because this whole test program wouldn't work if this wasn't OK
510 void TestSetPriority()
512 RTestProcess process;
513 RTestProcess process2;
514 TProcessPriority priority;
515 TRequestStatus rendezvousStatus;
516 TRequestStatus logonStatus;
518 test.Start(_L("Test changing our own process priority"));
519 priority = process.Priority();
520 process.SetPriority(EPriorityLow);
521 test(process.Priority()==EPriorityLow);
522 process.SetPriority(EPriorityBackground);
523 test(process.Priority()==EPriorityBackground);
524 process.SetPriority(EPriorityForeground);
525 test(process.Priority()==EPriorityForeground);
526 process.SetPriority(priority);
528 test.Next(_L("Test changing unresumed process priority (which we created)"));
529 process.Create(0,ETestProcessNull);
530 priority = process.Priority();
531 process.SetPriority(EPriorityLow);
532 test(process.Priority()==EPriorityLow);
533 process.SetPriority(EPriorityBackground);
534 test(process.Priority()==EPriorityBackground);
535 process.SetPriority(EPriorityForeground);
536 test(process.Priority()==EPriorityForeground);
537 process.SetPriority(priority);
539 CLOSE_AND_WAIT(process);
541 test.Next(_L("Try other process changing the priority of our created process"));
542 process2.Create(0,ETestProcessNull);
543 process.Create(~0u,ETestProcessPriority,process2.Id());
544 process.Logon(logonStatus);
546 User::WaitForRequest(logonStatus);
547 test(logonStatus==KErrNone);
548 CLOSE_AND_WAIT(process);
550 CLOSE_AND_WAIT(process2);
552 test.Next(_L("Try changing other process's priority (no priority-control enabled)"));
553 process.Create(~0u,ETestProcessPriorityControlOff);
554 process.Rendezvous(rendezvousStatus);
555 process.Logon(logonStatus);
558 User::WaitForRequest(rendezvousStatus); // Process has started
559 priority = process.Priority();
560 TInt result = process.SetPriority(EPriorityLow);
561 test(result == KErrPermissionDenied);
562 test(process.Priority()==priority); // priority shouldn't have changed
563 process.SetPriority(EPriorityBackground);
564 test(result == KErrPermissionDenied);
565 test(process.Priority()==priority); // priority shouldn't have changed
566 process.SetPriority(EPriorityForeground);
567 test(result == KErrPermissionDenied);
568 test(process.Priority()==priority); // priority shouldn't have changed
569 test(logonStatus==KRequestPending); // wait for process to end
571 User::WaitForRequest(logonStatus);
572 CLOSE_AND_WAIT(process);
574 test.Next(_L("Try changing other process's priority (priority-control enabled)"));
575 process.Create(~0u,ETestProcessPriorityControlOn);
576 process.Rendezvous(rendezvousStatus);
577 process.Logon(logonStatus);
580 User::WaitForRequest(rendezvousStatus); // Process has started
581 priority = process.Priority();
582 result = process.SetPriority(EPriorityForeground);
583 test(result == KErrNone);
584 test(process.Priority()==EPriorityForeground);
585 result = process.SetPriority(EPriorityBackground);
586 test(result == KErrNone);
587 test(process.Priority()==EPriorityBackground);
588 result = process.SetPriority(EPriorityForeground);
589 test(result == KErrNone);
590 test(process.Priority()==EPriorityForeground);
591 result = process.SetPriority(EPriorityLow);
592 test(result == KErrPermissionDenied);
593 test(process.Priority()==EPriorityForeground); // should still be foreground priority
594 result = process.SetPriority(EPriorityHigh);
595 test(result == KErrNone);
596 test(process.Priority()==EPriorityHigh);
597 result = process.SetPriority(priority);
598 test(result == KErrNone);
599 test(logonStatus==KRequestPending); // wait for process to end
601 User::WaitForRequest(logonStatus);
602 CLOSE_AND_WAIT(process);
604 test.Next(_L("Try changing other process's priority (priority-control enabled and low priority)"));
605 process.Create(~0u,ETestProcessPriorityControlOnAndLowPriority);
606 process.Rendezvous(rendezvousStatus);
607 process.Logon(logonStatus);
610 User::WaitForRequest(rendezvousStatus); // Process has started
611 test(process.Priority()==EPriorityLow);
612 result = process.SetPriority(EPriorityForeground);
613 test(result == KErrPermissionDenied);
614 test(process.Priority()==EPriorityLow);
615 result = process.SetPriority(EPriorityBackground);
616 test(result == KErrPermissionDenied);
617 test(process.Priority()==EPriorityLow);
618 result = process.SetPriority(EPriorityForeground);
619 test(result == KErrPermissionDenied);
620 test(process.Priority()==EPriorityLow);
621 result = process.SetPriority(EPriorityLow);
622 test(result == KErrPermissionDenied);
623 test(process.Priority()==EPriorityLow);
624 result = process.SetPriority(EPriorityHigh);
625 test(result == KErrPermissionDenied);
626 test(process.Priority()==EPriorityLow);
627 test(logonStatus==KRequestPending); // wait for process to end
629 User::WaitForRequest(logonStatus);
630 CLOSE_AND_WAIT(process);
637 GLDEF_C TInt E32Main()
640 User::CommandLine(cmd);
641 if(cmd.Length() && TChar(cmd[0]).IsDigit())
653 return DoTestProcess(function,arg1,arg2);
658 if((!PlatSec::ConfigSetting(PlatSec::EPlatSecProcessIsolation))||(!PlatSec::ConfigSetting(PlatSec::EPlatSecEnforcement)))
660 test.Start(_L("TESTS NOT RUN - PlatSecProcessIsolation is not enforced"));
665 test(SyncMutex.CreateGlobal(KSyncMutext)==KErrNone);
667 test.Start(_L("Test SetJustInTime"));
670 test.Next(_L("Test Rename"));
673 test.Next(_L("Test Kill, Panic and Teminate"));
676 test.Next(_L("Test Resume"));
679 test.Next(_L("Test SetPriority"));