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_suser.cpp
16 // Test the platform security aspects of the User and UserSvr classes.
20 // - Attempt to get and set the machine configuration with and without
21 // the proper privileges. Verify results are as expected.
22 // - Test various critical threads and processes with different capabilities,
23 // verify results are as expected.
24 // - Verify that the SetPriorityControl and PriorityControl methods work as
26 // - Verify that the UserSvr::CaptureEventHook, ReleaseEventHook, RequestEvent,
27 // and RequestEventCancel work as expected.
28 // - Test handled and unhandled exceptions work as expected.
29 // - Test ResetInactivityTime() with different capabilities. Verify results.
30 // - Test SetHomeTime() with different capabilities. Verify results.
31 // - Test SetMemoryThresholds() with different capabilities. Verify results.
32 // Platforms/Drives/Compatibility:
34 // Assumptions/Requirement/Pre-requisites:
35 // Failures and causes:
36 // Base Port information:
40 #define __E32TEST_EXTENSION__
44 #include <nkern/nk_trace.h>
46 LOCAL_D RTest test(_L("T_SUSER"));
48 _LIT(KSyncSemaphoreName,"T_SUSER-SyncSemaphore");
49 RSemaphore SyncSemaphore;
55 if(sem.OpenGlobal(KSyncSemaphoreName,EOwnerThread)!=KErrNone)
65 if(sem.OpenGlobal(KSyncSemaphoreName,EOwnerThread)!=KErrNone)
72 class RTestThread : public RThread
75 void Create(TThreadFunction aFunction,TInt aArg=0);
78 TInt GetCriticalValue(TAny* aArg)
80 TUint id = (TUint)aArg;
82 TInt r = thread.Open(TThreadId(id));
84 r = (TInt)User::Critical(thread);
89 void SetThreadCritical(User::TCritical aCritical)
91 // set thread critical as specified
92 if(User::SetCritical(aCritical)!=KErrNone)
94 // check critical value was as we set
95 if(User::Critical()!=aCritical)
97 // check from another thread
99 thread.Create(GetCriticalValue, (TUint)thread.Id());
100 TRequestStatus logonStatus;
101 thread.Logon(logonStatus);
103 User::WaitForRequest(logonStatus);
104 if (logonStatus!=(TInt)aCritical)
108 TInt TestThreadProcessCritical(TAny* aArg)
110 // check thread is not critical
111 if(User::Critical()!=User::ENotCritical)
113 // set thread as process critical
114 SetThreadCritical(User::EProcessCritical);
115 // complete rendezvous to let test code know we got this far ok
116 RProcess::Rendezvous(KErrNone);
117 // Kill this thread which should also kill the process
118 switch((TExitType)(TInt)aArg)
124 RThread().Terminate(999);
127 User::Panic(_L("TestPanic"),999);
135 TInt TestThreadSystemCritical(TAny*)
137 User::TCritical critical = User::Critical();
138 // check thread is not already system critical
139 if(User::Critical()==User::ESystemCritical)
141 // set thread as system critical
142 SetThreadCritical(User::ESystemCritical);
143 // Can't test system critical thread dying so put back to normal and end
144 SetThreadCritical(critical);
145 // complete rendezvous to let test code know we got this far ok
146 RProcess::Rendezvous(KErrNone);
150 TInt TestThreadSystemPermanent(TAny*)
152 User::TCritical critical = User::Critical();
153 // check thread is not already system permanent
154 if(User::Critical()==User::ESystemPermanent)
156 // set thread as system permanent
157 SetThreadCritical(User::ESystemPermanent);
158 // Can't test system permanent thread dying so put back to normal and end
159 SetThreadCritical(critical);
160 // complete rendezvous to let test code know we got this far ok
161 RProcess::Rendezvous(KErrNone);
165 TInt TestThreadSystemProcessCritical(TAny*)
167 User::TCritical critical = User::ProcessCritical();
168 // check thread is not already system critical
169 if(User::ProcessCritical()==User::ESystemCritical)
171 // set thread as system critical
172 if(User::SetProcessCritical(User::ESystemCritical)!=KErrNone)
174 // check critical value was as we set
175 if(User::ProcessCritical()!=User::ESystemCritical)
177 // complete rendezvous to let test code know we got this far ok
178 RProcess::Rendezvous(KErrNone);
179 // wait for main test thread to tell us to continue...
181 // Can't test system critical thread dying so put back to normal and end
182 if(User::SetProcessCritical(critical)!=KErrNone)
184 if(User::ProcessCritical()!=critical)
189 TInt TestThreadSystemProcessPermanent(TAny*)
191 User::TCritical critical = User::ProcessCritical();
192 // check thread is not already system permanent
193 if(User::ProcessCritical()==User::ESystemPermanent)
195 // set thread as system permanent
196 if(User::SetProcessCritical(User::ESystemPermanent)!=KErrNone)
198 // check critical value was as we set
199 if(User::ProcessCritical()!=User::ESystemPermanent)
201 // complete rendezvous to let test code know we got this far ok
202 RProcess::Rendezvous(KErrNone);
203 // wait for main test thread to tell us to continue...
205 // Can't test system permanent thread dying so put back to normal and end
206 if(User::SetProcessCritical(critical)!=KErrNone)
208 if(User::ProcessCritical()!=critical)
213 TInt TestThreadTraceKallthreadssystem(TAny*)
215 TUint32 mask = UserSvr::DebugMask(DEBUGMASKWORD2);
216 // check thread does not already have KALLTHREADSSYSTEM bit set
217 if (mask & (1 << (KALLTHREADSSYSTEM%32)))
219 // set KALLTHREADSSYSTEM bit
220 User::SetDebugMask(mask | (1 << (KALLTHREADSSYSTEM%32)), DEBUGMASKWORD2);
221 // check KALLTHREADSSYSTEM bit was as we set
222 if(!(UserSvr::DebugMask(DEBUGMASKWORD2) & (1 << (KALLTHREADSSYSTEM%32))))
224 // restore original mask
225 User::SetDebugMask(mask, DEBUGMASKWORD2);
226 if(UserSvr::DebugMask(DEBUGMASKWORD2) & (1 << (KALLTHREADSSYSTEM%32)))
228 // complete rendezvous to let test code know we got this far ok
229 RProcess::Rendezvous(KErrNone);
233 TInt TestThreadAllThreadsCritical(TAny* aArg)
235 // check that thread was created process critical
236 if(User::Critical()!=User::EProcessCritical)
238 // complete rendezvous to let test code know we got this far ok
239 RProcess::Rendezvous(KErrNone);
240 // Kill this thread which should also kill the process
241 switch((TExitType)(TInt)aArg)
247 RThread().Terminate(999);
250 User::Panic(_L("TestPanic"),999);
258 TInt TestAllThreadsCritical(TExitType aExitType)
260 // check process does not already have all threads critical
261 if(User::ProcessCritical()==User::EAllThreadsCritical)
262 User::Panic(_L("TestAllThreadsCritical"),__LINE__);
263 // set process as all threads critical
264 if(User::SetProcessCritical(User::EAllThreadsCritical)!=KErrNone)
265 User::Panic(_L("TestAllThreadsCritical"),__LINE__);
266 // check critical value was as we set
267 if(User::ProcessCritical()!=User::EAllThreadsCritical)
268 User::Panic(_L("TestAllThreadsCritical"),__LINE__);
269 // spawn a thread that exits in the specifed way
271 thread.Create(TestThreadAllThreadsCritical,aExitType);
272 TRequestStatus logonStatus;
273 thread.Logon(logonStatus);
275 User::WaitForRequest(logonStatus);
279 void RTestThread::Create(TThreadFunction aFunction,TInt aArg)
281 TInt r=RThread::Create(_L(""),aFunction,KDefaultStackSize,KDefaultStackSize,KDefaultStackSize,(TAny*)aArg);
287 enum TTestProcessFunctions
289 ETestProcessMachineConfigGet,
290 ETestProcessMachineConfigSet,
291 ETestProcessProcessCriticalNormalEnd,
292 ETestProcessProcessCriticalKill,
293 ETestProcessProcessCriticalTerminate,
294 ETestProcessProcessCriticalPanic,
295 ETestProcessAllThreadsCriticalNormalEnd,
296 ETestProcessAllThreadsCriticalKill,
297 ETestProcessAllThreadsCriticalTerminate,
298 ETestProcessAllThreadsCriticalPanic,
299 ETestProcessSystemCritical,
300 ETestProcessSystemPermanent,
301 ETestProcessSystemProcessCritical,
302 ETestProcessSystemProcessPermanent,
303 ETestProcessCaptureEventHook,
304 ETestProcessReleaseEventHook,
305 ETestProcessRequestEvent,
306 ETestProcessRequestEventCancel,
307 ETestProcessSetHomeTime,
308 ETestProcessSetMemoryThresholds,
309 ETestProcessSetUTCOffset,
310 ETestProcessSetUTCTime,
311 ETestProcessSetUTCTimeAndOffset,
312 ETestProcessTraceKallthreadssystem,
313 ETestProcessLocaleSet,
314 ETestProcessUserSetCurrencySymbol,
315 ETestProcessChangeLocale,
316 ETestProcessSaveSystemSettings,
317 ETestProcessSetCurrencySymbol,
318 ETestProcessAddEventESwitchOff,
319 ETestProcessAddEventECaseOpen,
320 ETestProcessAddEventECaseClose
323 #include "testprocess.h"
325 const TInt KMachineConfigSize = 1024;
326 TBuf8<KMachineConfigSize> MachineConfig;
328 TInt DoTestProcess(TInt aTestNum,TInt aArg1,TInt aArg2)
334 me.SetPriority(EPriorityLess);
339 case ETestProcessMachineConfigGet:
342 TInt r=User::MachineConfiguration(MachineConfig,size);
345 MachineConfig.SetLength(size);
349 case ETestProcessMachineConfigSet:
352 TInt r=User::MachineConfiguration(MachineConfig,size);
355 MachineConfig.SetLength(size);
357 r=User::SetMachineConfiguration(MachineConfig);
363 case ETestProcessProcessCriticalNormalEnd:
366 thread.Create(TestThreadProcessCritical,-1);
367 TRequestStatus logonStatus;
368 thread.Logon(logonStatus);
370 User::WaitForRequest(logonStatus);
374 case ETestProcessProcessCriticalKill:
377 thread.Create(TestThreadProcessCritical,EExitKill);
378 TRequestStatus logonStatus;
379 thread.Logon(logonStatus);
381 User::WaitForRequest(logonStatus);
385 case ETestProcessProcessCriticalTerminate:
388 thread.Create(TestThreadProcessCritical,EExitTerminate);
389 TRequestStatus logonStatus;
390 thread.Logon(logonStatus);
392 User::WaitForRequest(logonStatus);
396 case ETestProcessProcessCriticalPanic:
399 thread.Create(TestThreadProcessCritical,EExitPanic);
400 TRequestStatus logonStatus;
401 thread.Logon(logonStatus);
403 User::WaitForRequest(logonStatus);
407 case ETestProcessAllThreadsCriticalNormalEnd:
408 return TestAllThreadsCritical((TExitType)-1);
410 case ETestProcessAllThreadsCriticalKill:
411 return TestAllThreadsCritical(EExitKill);
413 case ETestProcessAllThreadsCriticalTerminate:
414 return TestAllThreadsCritical(EExitTerminate);
416 case ETestProcessAllThreadsCriticalPanic:
417 return TestAllThreadsCritical(EExitPanic);
419 case ETestProcessSystemCritical:
420 return TestThreadSystemCritical(NULL);
422 case ETestProcessSystemPermanent:
423 return TestThreadSystemPermanent(NULL);
425 case ETestProcessSystemProcessCritical:
426 return TestThreadSystemProcessCritical(NULL);
428 case ETestProcessSystemProcessPermanent:
429 return TestThreadSystemProcessPermanent(NULL);
431 case ETestProcessCaptureEventHook:
432 UserSvr::CaptureEventHook();
435 case ETestProcessReleaseEventHook:
436 UserSvr::ReleaseEventHook();
439 case ETestProcessRequestEvent:
442 TRequestStatus status;
443 UserSvr::RequestEvent(event,status);
447 case ETestProcessRequestEventCancel:
448 UserSvr::RequestEventCancel();
451 case ETestProcessSetHomeTime:
455 User::SetHomeTime(time);
459 case ETestProcessSetUTCOffset:
461 User::SetUTCOffset(0);
465 case ETestProcessSetUTCTime:
468 time.UniversalTime();
469 User::SetUTCTime(time);
473 case ETestProcessSetUTCTimeAndOffset:
476 time.UniversalTime();
477 User::SetUTCTimeAndOffset(time,0);
481 case ETestProcessSetMemoryThresholds:
483 return UserSvr::SetMemoryThresholds(0,KMaxTInt);
486 case ETestProcessTraceKallthreadssystem:
487 return TestThreadTraceKallthreadssystem(NULL);
489 case ETestProcessLocaleSet:
490 return TLocale().Set();
492 case ETestProcessUserSetCurrencySymbol:
493 return User::SetCurrencySymbol(TCurrencySymbol());
495 case ETestProcessChangeLocale:
496 return UserSvr::ChangeLocale(KNullDesC);
498 case ETestProcessSaveSystemSettings:
500 TExtendedLocale locale;
501 locale.LoadSystemSettings();
502 return locale.SaveSystemSettings();
505 case ETestProcessSetCurrencySymbol:
507 TExtendedLocale locale;
508 locale.LoadSystemSettings();
509 return locale.SetCurrencySymbol(TCurrencySymbol());
512 case ETestProcessAddEventESwitchOff:
515 event.Set(TRawEvent::ESwitchOff);
516 return UserSvr::AddEvent(event);
519 case ETestProcessAddEventECaseOpen:
522 event.Set(TRawEvent::ECaseOpen);
523 return UserSvr::AddEvent(event);
526 case ETestProcessAddEventECaseClose:
529 event.Set(TRawEvent::ECaseClose);
530 return UserSvr::AddEvent(event);
534 User::Panic(_L("T_SUSER"),1);
542 void TestMachineConfiguration()
544 RTestProcess process;
545 TRequestStatus logonStatus;
547 test.Start(_L("Try getting machine-config without ECapabilityReadDeviceData"));
548 process.Create(~(1u<<ECapabilityReadDeviceData),ETestProcessMachineConfigGet);
549 process.Logon(logonStatus);
551 User::WaitForRequest(logonStatus);
552 test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
553 test(logonStatus==EPlatformSecurityTrap);
554 CLOSE_AND_WAIT(process);
556 test.Next(_L("Try setting machine-config without ECapabilityWriteDeviceData"));
557 process.Create(~(1u<<ECapabilityWriteDeviceData),ETestProcessMachineConfigSet);
558 process.Logon(logonStatus);
560 User::WaitForRequest(logonStatus);
561 test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
562 test(logonStatus==EPlatformSecurityTrap);
563 CLOSE_AND_WAIT(process);
565 test.Next(_L("Test getting machine-config with ECapabilityReadDeviceData"));
566 process.Create(1<<ECapabilityReadDeviceData,ETestProcessMachineConfigGet);
567 process.Logon(logonStatus);
569 User::WaitForRequest(logonStatus);
570 test(process.ExitType()==EExitKill);
571 test(logonStatus==KErrNone);
572 CLOSE_AND_WAIT(process);
574 test.Next(_L("Test setting machine-conig with ECapabilityWriteDeviceData"));
575 process.Create((1<<ECapabilityWriteDeviceData)|(1<<ECapabilityReadDeviceData),ETestProcessMachineConfigSet);
576 process.Logon(logonStatus);
578 User::WaitForRequest(logonStatus);
579 test(process.ExitType()==EExitKill);
580 test(logonStatus==KErrNone);
581 CLOSE_AND_WAIT(process);
588 void TestSetCritical()
590 RTestProcess process;
591 TRequestStatus rendezvousStatus;
592 TRequestStatus logonStatus;
594 test.Start(_L("Test process critical thread exiting normally"));
595 process.Create(ETestProcessProcessCriticalNormalEnd);
596 process.Logon(logonStatus);
597 process.Rendezvous(rendezvousStatus);
599 User::WaitForRequest(rendezvousStatus);
600 test(rendezvousStatus==KErrNone);
601 User::WaitForRequest(logonStatus);
602 test(process.ExitType()==EExitKill);
603 test(logonStatus==KErrNone);
604 CLOSE_AND_WAIT(process);
606 test.Next(_L("Test process critical thread being killed"));
607 process.Create(ETestProcessProcessCriticalKill);
608 process.Logon(logonStatus);
609 process.Rendezvous(rendezvousStatus);
611 User::WaitForRequest(rendezvousStatus);
612 test(rendezvousStatus==KErrNone);
613 User::WaitForRequest(logonStatus);
614 test(process.ExitType()==EExitKill);
615 test(logonStatus==KErrNone); // Killed thread doesn't take down process
616 CLOSE_AND_WAIT(process);
618 test.Next(_L("Test process critical thread being terminated"));
619 process.Create(ETestProcessProcessCriticalTerminate);
620 process.Logon(logonStatus);
621 process.Rendezvous(rendezvousStatus);
623 User::WaitForRequest(rendezvousStatus);
624 test(rendezvousStatus==KErrNone);
625 User::WaitForRequest(logonStatus);
626 test(process.ExitType()==EExitTerminate);
627 test(logonStatus==999);
628 CLOSE_AND_WAIT(process);
630 test.Next(_L("Test process critical thread being panicked"));
631 process.Create(ETestProcessProcessCriticalPanic);
632 process.Logon(logonStatus);
633 process.Rendezvous(rendezvousStatus);
635 User::WaitForRequest(rendezvousStatus);
636 test(rendezvousStatus==KErrNone);
637 User::WaitForRequest(logonStatus);
638 test(process.ExitType()==EExitPanic);
639 test(logonStatus==999);
640 CLOSE_AND_WAIT(process);
642 test.Next(_L("Test all threads critical process with thread exiting normally"));
643 process.Create(ETestProcessAllThreadsCriticalNormalEnd);
644 process.Logon(logonStatus);
645 process.Rendezvous(rendezvousStatus);
647 User::WaitForRequest(rendezvousStatus);
648 test(rendezvousStatus==KErrNone);
649 test(User::ProcessCritical(process) == User::EAllThreadsCritical);
650 User::WaitForRequest(logonStatus);
651 test(process.ExitType()==EExitKill);
652 test(logonStatus==KErrNone);
653 CLOSE_AND_WAIT(process);
655 test.Next(_L("Test all threads critical process with thread being killed"));
656 process.Create(ETestProcessAllThreadsCriticalKill);
657 process.Logon(logonStatus);
658 process.Rendezvous(rendezvousStatus);
660 User::WaitForRequest(rendezvousStatus);
661 test(rendezvousStatus==KErrNone);
662 test(User::ProcessCritical(process) == User::EAllThreadsCritical);
663 User::WaitForRequest(logonStatus);
664 test(process.ExitType()==EExitKill);
665 test(logonStatus==KErrNone); // Killed thread doesn't take down process
666 CLOSE_AND_WAIT(process);
668 test.Next(_L("Test all threads critical process with thread being terminated"));
669 process.Create(ETestProcessAllThreadsCriticalTerminate);
670 process.Logon(logonStatus);
671 process.Rendezvous(rendezvousStatus);
673 User::WaitForRequest(rendezvousStatus);
674 test(rendezvousStatus==KErrNone);
675 test(User::ProcessCritical(process) == User::EAllThreadsCritical);
676 User::WaitForRequest(logonStatus);
677 test(process.ExitType()==EExitTerminate);
678 test(logonStatus==999);
679 CLOSE_AND_WAIT(process);
681 test.Next(_L("Test all threads critical process with thread being panicked"));
682 process.Create(ETestProcessAllThreadsCriticalPanic);
683 process.Logon(logonStatus);
684 process.Rendezvous(rendezvousStatus);
686 User::WaitForRequest(rendezvousStatus);
687 test(rendezvousStatus==KErrNone);
688 test(User::ProcessCritical(process) == User::EAllThreadsCritical);
689 User::WaitForRequest(logonStatus);
690 test.Printf(_L("Exit type == %d\n"), process.ExitType());
691 test(process.ExitType()==EExitPanic);
692 test(logonStatus==999);
693 CLOSE_AND_WAIT(process);
695 test.Next(_L("Try to setup a system critical thread without ECapabilityProtServ"));
696 process.Create(~(1u<<ECapabilityProtServ),ETestProcessSystemCritical);
697 process.Logon(logonStatus);
698 process.Rendezvous(rendezvousStatus);
700 User::WaitForRequest(rendezvousStatus);
701 test(rendezvousStatus==EPlatformSecurityTrap);
702 User::WaitForRequest(logonStatus);
703 test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
704 test(logonStatus==EPlatformSecurityTrap);
705 CLOSE_AND_WAIT(process);
707 test.Next(_L("Test setup of a system critical thread with ECapabilityProtServ"));
708 process.Create(1<<ECapabilityProtServ,ETestProcessSystemCritical);
709 process.Logon(logonStatus);
710 process.Rendezvous(rendezvousStatus);
712 User::WaitForRequest(rendezvousStatus);
713 test(rendezvousStatus==KErrNone);
714 User::WaitForRequest(logonStatus);
715 test(logonStatus==KErrNone);
716 CLOSE_AND_WAIT(process);
718 test.Next(_L("Try to setup a system permanent thread without ECapabilityProtServ"));
719 process.Create(~(1u<<ECapabilityProtServ),ETestProcessSystemPermanent);
720 process.Logon(logonStatus);
721 process.Rendezvous(rendezvousStatus);
723 User::WaitForRequest(rendezvousStatus);
724 test(rendezvousStatus==EPlatformSecurityTrap);
725 User::WaitForRequest(logonStatus);
726 test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
727 test(logonStatus==EPlatformSecurityTrap);
728 CLOSE_AND_WAIT(process);
730 test.Next(_L("Test setup of a system permanent thread with ECapabilityProtServ"));
731 process.Create(1<<ECapabilityProtServ,ETestProcessSystemPermanent);
732 process.Logon(logonStatus);
733 process.Rendezvous(rendezvousStatus);
735 User::WaitForRequest(rendezvousStatus);
736 test(rendezvousStatus==KErrNone);
737 User::WaitForRequest(logonStatus);
738 test(logonStatus==KErrNone);
739 CLOSE_AND_WAIT(process);
741 test.Next(_L("Try to setup a system critical process without ECapabilityProtServ"));
742 process.Create(~(1u<<ECapabilityProtServ),ETestProcessSystemProcessCritical);
743 process.Logon(logonStatus);
744 process.Rendezvous(rendezvousStatus);
746 User::WaitForRequest(rendezvousStatus);
747 test(rendezvousStatus==EPlatformSecurityTrap);
748 test(User::ProcessCritical(process) == User::ENotCritical);
749 User::WaitForRequest(logonStatus);
750 test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
751 test(logonStatus==EPlatformSecurityTrap);
752 CLOSE_AND_WAIT(process);
754 test.Next(_L("Test setup of a system critical process with ECapabilityProtServ"));
755 process.Create(1<<ECapabilityProtServ,ETestProcessSystemProcessCritical);
756 process.Logon(logonStatus);
757 process.Rendezvous(rendezvousStatus);
759 User::WaitForRequest(rendezvousStatus);
760 test(rendezvousStatus==KErrNone);
761 test(User::ProcessCritical(process) == User::ESystemCritical);
763 User::WaitForRequest(logonStatus);
764 test(logonStatus==KErrNone);
765 CLOSE_AND_WAIT(process);
767 test.Next(_L("Try to setup a system permanent process without ECapabilityProtServ"));
768 process.Create(~(1u<<ECapabilityProtServ),ETestProcessSystemProcessPermanent);
769 process.Logon(logonStatus);
770 process.Rendezvous(rendezvousStatus);
772 User::WaitForRequest(rendezvousStatus);
773 test(rendezvousStatus==EPlatformSecurityTrap);
774 test(User::ProcessCritical(process) == User::ENotCritical);
775 User::WaitForRequest(logonStatus);
776 test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
777 test(logonStatus==EPlatformSecurityTrap);
778 CLOSE_AND_WAIT(process);
780 test.Next(_L("Test setup of a system permanent process with ECapabilityProtServ"));
781 process.Create(1<<ECapabilityProtServ,ETestProcessSystemProcessPermanent);
782 process.Logon(logonStatus);
783 process.Rendezvous(rendezvousStatus);
785 User::WaitForRequest(rendezvousStatus);
786 test(rendezvousStatus==KErrNone);
787 test(User::ProcessCritical(process) == User::ESystemPermanent);
789 User::WaitForRequest(logonStatus);
790 test(logonStatus==KErrNone);
791 CLOSE_AND_WAIT(process);
793 test.Next(_L("Try to setup a trace bit KALLTHREADSSYSTEM process without ECapabilityProtServ"));
794 process.Create(~(1u<<ECapabilityProtServ),ETestProcessTraceKallthreadssystem);
795 process.Logon(logonStatus);
796 process.Rendezvous(rendezvousStatus);
798 User::WaitForRequest(rendezvousStatus);
799 test(rendezvousStatus==EPlatformSecurityTrap);
800 User::WaitForRequest(logonStatus);
801 test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
802 test(logonStatus==EPlatformSecurityTrap);
803 CLOSE_AND_WAIT(process);
805 test.Next(_L("Test setup of a trace bit KALLTHREADSSYSTEM process with ECapabilityProtServ"));
806 process.Create(1<<ECapabilityProtServ,ETestProcessTraceKallthreadssystem);
807 process.Logon(logonStatus);
808 process.Rendezvous(rendezvousStatus);
810 User::WaitForRequest(rendezvousStatus);
811 test(rendezvousStatus==KErrNone);
812 User::WaitForRequest(logonStatus);
813 test(logonStatus==KErrNone);
814 CLOSE_AND_WAIT(process);
821 TUint KTestUid = 0x87654321;
823 void SetAbsoluteTimeout(RTimer& aTimer, TUint aUs, TRequestStatus& aStatus)
827 wakeup += TTimeIntervalMicroSeconds(aUs);
828 aTimer.At(aStatus, wakeup);
833 RTestProcess process;
834 TRequestStatus logonStatus;
836 test.Start(_L("Try UserSvr::CaptureEventHook()"));
837 process.Create(~0u,ETestProcessCaptureEventHook);
838 process.Logon(logonStatus);
840 User::WaitForRequest(logonStatus);
841 test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
842 test(logonStatus==EPlatformSecurityTrap);
843 CLOSE_AND_WAIT(process);
845 test.Next(_L("Try UserSvr::ReleaseEventHook()"));
846 process.Create(~0u,ETestProcessReleaseEventHook);
847 process.Logon(logonStatus);
849 User::WaitForRequest(logonStatus);
850 test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
851 test(logonStatus==EEventNotCaptured);
852 CLOSE_AND_WAIT(process);
854 test.Next(_L("Try UserSvr::RequestEvent()"));
855 process.Create(~0u,ETestProcessRequestEvent);
856 process.Logon(logonStatus);
858 User::WaitForRequest(logonStatus);
859 test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
860 test(logonStatus==EEventNotCaptured);
861 CLOSE_AND_WAIT(process);
863 test.Next(_L("Try UserSvr::RequestEventCancel()"));
864 process.Create(~0u,ETestProcessRequestEventCancel);
865 process.Logon(logonStatus);
867 User::WaitForRequest(logonStatus);
868 test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
869 test(logonStatus==EEventNotCaptured);
870 CLOSE_AND_WAIT(process);
874 test.Next(_L("Try UserSvr::AddEvent(ECaseOpen) without ECapabilityPowerMgmt"));
875 process.Create(~(1u<<ECapabilityPowerMgmt),ETestProcessAddEventECaseOpen);
876 process.Logon(logonStatus);
878 User::WaitForRequest(logonStatus);
879 test(process.ExitType()==EExitKill);
880 test(logonStatus==KErrPermissionDenied);
881 CLOSE_AND_WAIT(process);
883 test.Next(_L("Try UserSvr::AddEvent(ECaseOpen) without ECapabilitySwEvent"));
884 process.Create(~(1u<<ECapabilitySwEvent),ETestProcessAddEventECaseOpen);
885 process.Logon(logonStatus);
887 User::WaitForRequest(logonStatus);
888 test(process.ExitType()==EExitKill);
889 test(logonStatus==KErrPermissionDenied);
890 CLOSE_AND_WAIT(process);
892 test.Next(_L("Calling UserSvr::AddEvent(ECaseOpen) with ECapabilityPowerMgmt & ECapabilitySwEvent"));
893 process.Create((1u<<ECapabilitySwEvent)|(1u<<ECapabilityPowerMgmt),ETestProcessAddEventECaseOpen);
894 process.Logon(logonStatus);
896 User::WaitForRequest(logonStatus);
897 test(process.ExitType()==EExitKill);
898 test(logonStatus==KErrNone);
899 CLOSE_AND_WAIT(process);
903 test.Next(_L("Try UserSvr::AddEvent(ECaseClose) without ECapabilityPowerMgmt"));
904 process.Create(~(1u<<ECapabilityPowerMgmt),ETestProcessAddEventECaseClose);
905 process.Logon(logonStatus);
907 User::WaitForRequest(logonStatus);
908 test(process.ExitType()==EExitKill);
909 test(logonStatus==KErrPermissionDenied);
910 CLOSE_AND_WAIT(process);
912 test.Next(_L("Try UserSvr::AddEvent(ECaseClose) without ECapabilitySwEvent"));
913 process.Create(~(1u<<ECapabilitySwEvent),ETestProcessAddEventECaseClose);
914 process.Logon(logonStatus);
916 User::WaitForRequest(logonStatus);
917 test(process.ExitType()==EExitKill);
918 test(logonStatus==KErrPermissionDenied);
919 CLOSE_AND_WAIT(process);
921 test.Next(_L("Calling UserSvr::AddEvent(ECaseClose) with ECapabilityPowerMgmt & ECapabilitySwEvent"));
922 process.Create((1u<<ECapabilitySwEvent)|(1u<<ECapabilityPowerMgmt),ETestProcessAddEventECaseClose);
923 process.Logon(logonStatus);
925 User::WaitForRequest(logonStatus);
926 test(process.ExitType()==EExitKill);
927 test(logonStatus==KErrNone);
928 CLOSE_AND_WAIT(process);
931 test.Next(_L("Try UserSvr::AddEvent(ESwitchOff) without ECapabilityPowerMgmt"));
932 process.Create(~(1u<<ECapabilityPowerMgmt),ETestProcessAddEventESwitchOff);
933 process.Logon(logonStatus);
935 User::WaitForRequest(logonStatus);
936 test(process.ExitType()==EExitKill);
937 test(logonStatus==KErrPermissionDenied);
938 CLOSE_AND_WAIT(process);
940 test.Next(_L("Try UserSvr::AddEvent(ESwitchOff) without ECapabilitySwEvent"));
941 process.Create(~(1u<<ECapabilitySwEvent),ETestProcessAddEventESwitchOff);
942 process.Logon(logonStatus);
944 User::WaitForRequest(logonStatus);
945 test(process.ExitType()==EExitKill);
946 test(logonStatus==KErrPermissionDenied);
947 CLOSE_AND_WAIT(process);
949 test.Next(_L("Calling UserSvr::AddEvent(ESwitchOff) with ECapabilityPowerMgmt & ECapabilitySwEvent"));
950 TRequestStatus absstatus;
952 TInt r = abstimer.CreateLocal();
953 test (r == KErrNone);
954 SetAbsoluteTimeout(abstimer, 5000000, absstatus); // 5 sec
955 process.Create((1u<<ECapabilitySwEvent)|(1u<<ECapabilityPowerMgmt),ETestProcessAddEventESwitchOff);
956 process.Logon(logonStatus);
958 User::WaitForRequest(absstatus);
960 User::WaitForRequest(logonStatus);
961 test(process.ExitType()==EExitKill);
962 test(logonStatus==KErrNone);
963 CLOSE_AND_WAIT(process);
969 const TInt KThreadCompleteOk = 0x80000002;
970 TInt TestExceptionResult = KErrGeneral;
972 void ExceptionHandler(TExcType /*aType*/)
974 TestExceptionResult = KErrNone;
977 TInt TestExceptionThread(TAny* aArg)
979 TestExceptionResult = KErrGeneral;
981 User::SetExceptionHandler(ExceptionHandler,KExceptionAbort);
983 if(User::ExceptionHandler()!=ExceptionHandler)
986 if(User::IsExceptionHandled(EExcAbort))
987 User::ModifyExceptionMask(KExceptionAbort,0);
989 if(User::IsExceptionHandled(EExcAbort))
992 User::ModifyExceptionMask(0, KExceptionAbort);
993 if(!User::IsExceptionHandled((TExcType)EExcAbort))
996 if(User::RaiseException((TExcType)(TInt)aArg)!=KErrNone)
999 return KThreadCompleteOk;
1002 void TestException()
1005 TRequestStatus logonStatus;
1007 test.Start(_L("Test handled exceptions"));
1008 thread.Create(TestExceptionThread,EExcAbort);
1009 thread.Logon(logonStatus);
1011 User::WaitForRequest(logonStatus);
1012 test(logonStatus==KThreadCompleteOk);
1013 test(TestExceptionResult==KErrNone);
1015 test.Next(_L("Test unhandled exceptions"));
1016 thread.Create(TestExceptionThread,EExcKill);
1017 thread.Logon(logonStatus);
1018 TInt jit = User::JustInTime();
1019 User::SetJustInTime(EFalse);
1021 User::WaitForRequest(logonStatus);
1022 User::SetJustInTime(jit);
1023 test(logonStatus==ECausedException);
1024 test(TestExceptionResult==KErrGeneral);
1029 void TestSetHomeTime()
1031 RTestProcess process;
1032 TRequestStatus logonStatus;
1034 test.Start(_L("Try call without ECapabilityWriteDeviceData"));
1035 process.Create(~(1u<<ECapabilityWriteDeviceData),ETestProcessSetHomeTime,KTestUid);
1036 process.Logon(logonStatus);
1038 User::WaitForRequest(logonStatus);
1039 test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
1040 test(logonStatus==EPlatformSecurityTrap);
1041 CLOSE_AND_WAIT(process);
1043 test.Next(_L("Test call with ECapabilityWriteDeviceData"));
1044 process.Create(1<<ECapabilityWriteDeviceData,ETestProcessSetHomeTime,KTestUid);
1045 process.Logon(logonStatus);
1047 User::WaitForRequest(logonStatus);
1048 test(process.ExitType()==EExitKill);
1049 test(logonStatus==0);
1050 CLOSE_AND_WAIT(process);
1057 void TestSetUTCOffset()
1059 RTestProcess process;
1060 TRequestStatus logonStatus;
1062 test.Start(_L("Try call without ECapabilityWriteDeviceData"));
1063 process.Create(~(1u<<ECapabilityWriteDeviceData),ETestProcessSetUTCOffset,KTestUid);
1064 process.Logon(logonStatus);
1066 User::WaitForRequest(logonStatus);
1067 test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
1068 test(logonStatus==EPlatformSecurityTrap);
1069 CLOSE_AND_WAIT(process);
1071 test.Next(_L("Test call with ECapabilityWriteDeviceData"));
1072 process.Create(1<<ECapabilityWriteDeviceData,ETestProcessSetUTCOffset,KTestUid);
1073 process.Logon(logonStatus);
1075 User::WaitForRequest(logonStatus);
1076 test(process.ExitType()==EExitKill);
1077 test(logonStatus==0);
1078 CLOSE_AND_WAIT(process);
1085 void TestSetUTCTime()
1087 RTestProcess process;
1088 TRequestStatus logonStatus;
1090 test.Start(_L("Try call without ECapabilityWriteDeviceData"));
1091 process.Create(~(1u<<ECapabilityWriteDeviceData),ETestProcessSetUTCTime,KTestUid);
1092 process.Logon(logonStatus);
1094 User::WaitForRequest(logonStatus);
1095 test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
1096 test(logonStatus==EPlatformSecurityTrap);
1097 CLOSE_AND_WAIT(process);
1099 test.Next(_L("Test call with ECapabilityWriteDeviceData"));
1100 process.Create(1<<ECapabilityWriteDeviceData,ETestProcessSetUTCTime,KTestUid);
1101 process.Logon(logonStatus);
1103 User::WaitForRequest(logonStatus);
1104 test(process.ExitType()==EExitKill);
1105 test(logonStatus==0);
1106 CLOSE_AND_WAIT(process);
1113 void TestSetUTCTimeAndOffset()
1115 RTestProcess process;
1116 TRequestStatus logonStatus;
1118 test.Start(_L("Try call without ECapabilityWriteDeviceData"));
1119 process.Create(~(1u<<ECapabilityWriteDeviceData),ETestProcessSetUTCTimeAndOffset,KTestUid);
1120 process.Logon(logonStatus);
1122 User::WaitForRequest(logonStatus);
1123 test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
1124 test(logonStatus==EPlatformSecurityTrap);
1125 CLOSE_AND_WAIT(process);
1127 test.Next(_L("Test call with ECapabilityWriteDeviceData"));
1128 process.Create(1<<ECapabilityWriteDeviceData,ETestProcessSetUTCTimeAndOffset,KTestUid);
1129 process.Logon(logonStatus);
1131 User::WaitForRequest(logonStatus);
1132 test(process.ExitType()==EExitKill);
1133 test(logonStatus==0);
1134 CLOSE_AND_WAIT(process);
1141 void TestSetMemoryThresholds()
1143 RTestProcess process;
1144 TRequestStatus logonStatus;
1146 test.Start(_L("Try call without ECapabilityWriteDeviceData"));
1147 process.Create(~(1u<<ECapabilityWriteDeviceData),ETestProcessSetMemoryThresholds,KTestUid);
1148 process.Logon(logonStatus);
1150 User::WaitForRequest(logonStatus);
1151 test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
1152 test(logonStatus==EPlatformSecurityTrap);
1153 CLOSE_AND_WAIT(process);
1155 test.Next(_L("Test call with ECapabilityWriteDeviceData"));
1156 process.Create(1<<ECapabilityWriteDeviceData,ETestProcessSetMemoryThresholds,KTestUid);
1157 process.Logon(logonStatus);
1159 User::WaitForRequest(logonStatus);
1160 test(process.ExitType()==EExitKill);
1161 test(logonStatus==0);
1162 CLOSE_AND_WAIT(process);
1169 void TestWithWriteDeviceData(TTestProcessFunctions aFunction)
1171 RTestProcess process;
1172 TRequestStatus logonStatus;
1174 test.Start(_L("Try call without ECapabilityWriteDeviceData"));
1175 process.Create(~(1u<<ECapabilityWriteDeviceData),aFunction,KTestUid);
1176 process.Logon(logonStatus);
1178 User::WaitForRequest(logonStatus);
1179 test(process.ExitType()==EExitKill);
1180 test(logonStatus==KErrPermissionDenied);
1181 CLOSE_AND_WAIT(process);
1183 test.Next(_L("Test call with ECapabilityWriteDeviceData"));
1184 process.Create(1<<ECapabilityWriteDeviceData,aFunction,KTestUid);
1185 process.Logon(logonStatus);
1187 User::WaitForRequest(logonStatus);
1188 test(process.ExitType()==EExitKill);
1189 test(logonStatus==0);
1190 CLOSE_AND_WAIT(process);
1197 GLDEF_C TInt E32Main()
1200 User::CommandLine(cmd);
1201 if(cmd.Length() && TChar(cmd[0]).IsDigit())
1213 return DoTestProcess(function,arg1,arg2);
1218 if(!PlatSec::ConfigSetting(PlatSec::EPlatSecEnforcement))
1220 test.Start(_L("TESTS NOT RUN - EPlatSecEnforcement is OFF"));
1225 test_KErrNone(SyncSemaphore.CreateGlobal(KSyncSemaphoreName,0));
1227 test.Start(_L("Test MachineConfiguration()"));
1228 TestMachineConfiguration();
1230 test.Next(_L("Test SetCritical()"));
1233 test.Next(_L("Test Set/PriorityControl()"));
1234 User::SetPriorityControl(ETrue);
1235 test(User::PriorityControl());
1236 User::SetPriorityControl(EFalse);
1237 test(!User::PriorityControl());
1239 test.Next(_L("Test Event functions"));
1242 test.Next(_L("Test Exception functions"));
1245 test.Next(_L("Test SetHomeTime()"));
1248 test.Next(_L("Test SetUTCOffset()"));
1251 test.Next(_L("Test SetUTCTime()"));
1254 test.Next(_L("Test SetUTCTimeAndOffset()"));
1255 TestSetUTCTimeAndOffset();
1257 test.Next(_L("Test SetMemoryThresholds"));
1258 TestSetMemoryThresholds();
1260 test.Next(_L("Test Locale::Set"));
1261 TestWithWriteDeviceData(ETestProcessLocaleSet);
1263 test.Next(_L("Test User::SetCurrencySymbol"));
1264 TestWithWriteDeviceData(ETestProcessUserSetCurrencySymbol);
1266 test.Next(_L("Test UserSvr::ChangeLocale"));
1267 TestWithWriteDeviceData(ETestProcessChangeLocale);
1269 test.Next(_L("Test TExtendedLocale::SaveSystemSettings"));
1270 TestWithWriteDeviceData(ETestProcessSaveSystemSettings);
1272 test.Next(_L("Test TExtendedLocale::SetCurrencySymbol"));
1273 TestWithWriteDeviceData(ETestProcessSetCurrencySymbol);
1275 SyncSemaphore.Close();