Update contrib.
1 // Copyright (c) 2006-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_sprioritycap.cpp
16 // Test the platform security aspects of the RThread class as affected by the process priority
17 // specified in the MMP file, mainly capping of higher thread priorities without ProtServ.
19 // Process priorities windowserver, fileserver, supervisor and realtimeserver set with
22 // # 'epocprocesspriority' keyword in MMP files
23 // # 'priority' keyword in OBEY (OBY/IBY) files
25 // - Tests that the desired thread prioritisation results are obtained for process priorities
26 // SystemServer and RealTimeServer (established by separate MMP files):
27 // # without ECapabilityProtServ - priorities capped to SystemServer/More.
28 // # with ECapabilityProtServ - higher, "real-time" priorities obtainable.
29 // - Tests effect of reduction of SystemServer/More from nanothread priority 24 to 23,
30 // i.e. same priority as AbsoluteHigh
31 // Platforms/Drives/Compatibility:
33 // Assumptions/Requirement/Pre-requisites:
34 // Failures and causes:
35 // Base Port information:
40 LOCAL_D RTest test(_L("T_SPRIORITYCAP"));
42 TInt threadMutexAcquireOrder; // where 132 = thread 1 acquires mutex, then thread 3 then thread 2
43 _LIT(KTestPanicCategory,"TEST PANIC");
45 class RTestThread : public RThread
48 void Create(TThreadFunction aFunction,TAny* aArg=0);
51 void RTestThread::Create(TThreadFunction aFunction,TAny* aThreadNumber)
53 TInt threadNumber = reinterpret_cast<TInt>(aThreadNumber);
54 ASSERT((threadNumber > 0) && (threadNumber < 10));
55 TBuf<20> threadName = _L("TestThread_");
56 threadName.AppendNum(threadNumber);
57 TInt r=RThread::Create(threadName,aFunction,KDefaultStackSize,KDefaultStackSize,KDefaultStackSize,aThreadNumber);
61 TInt TestThreadWaitMutex(TAny* aThreadNumber)
63 TInt threadNumber = reinterpret_cast<TInt>(aThreadNumber);
64 ASSERT((threadNumber > 0) && (threadNumber < 10));
66 thisThread.Rendezvous(KErrNone);
68 threadMutexAcquireOrder = threadMutexAcquireOrder*10 + threadNumber;
73 // Create three threads with priority 1, 2 and 1, each waiting on a mutex that is already held by the
74 // main thread. Signal the mutex from the main thread and return the order the threads acquire it
75 // as an integer where 132 = thread 1 acquires mutex, then thread 3 then thread 2.
77 // In order to work this test requires the three threads to wait on the mutex in the order listed.
78 // This cannot be guaranteed, but the following points make it a near-certainty:
79 // (1) This main thread runs at lowest priority
80 // (2) The three threads initially resume at higher, decreasing priorities.
81 // (3) We Rendezvous() with the thread just before it waits on the mutex
82 // (4) Wait some time before creating next thread
83 // (5) Actual priorities are set after all test threads are waiting
84 TInt TestThreadMutexAcquireOrder(TThreadPriority aPriorityThread1, TThreadPriority aPriorityThread2)
89 TRequestStatus logonStatus1;
90 TRequestStatus logonStatus2;
91 TRequestStatus logonStatus3;
92 TRequestStatus rendezvousStatus;
94 threadMutexAcquireOrder = 0; // global variable to hold order in which threads obtain mutex
97 thisThread.SetPriority(EPriorityAbsoluteVeryLow);
99 // create the SyncMutex global variable and hold it initially
100 if(SyncMutex.CreateLocal()!=KErrNone)
104 thread1.Create(TestThreadWaitMutex, reinterpret_cast<TAny*>(1));
105 thread1.Logon(logonStatus1);
106 thread1.SetPriority(EPriorityAbsoluteHigh);
107 thread1.Rendezvous(rendezvousStatus);
109 User::WaitForRequest(rendezvousStatus);
112 thread2.Create(TestThreadWaitMutex, reinterpret_cast<TAny*>(2));
113 thread2.Logon(logonStatus2);
114 thread2.SetPriority(EPriorityAbsoluteForeground);
115 thread2.Rendezvous(rendezvousStatus);
117 User::WaitForRequest(rendezvousStatus);
120 thread3.Create(TestThreadWaitMutex, reinterpret_cast<TAny*>(3));
121 thread3.Logon(logonStatus3);
122 thread3.SetPriority(EPriorityAbsoluteBackground);
123 thread3.Rendezvous(rendezvousStatus);
125 User::WaitForRequest(rendezvousStatus);
128 thread1.SetPriority(aPriorityThread1);
129 thread2.SetPriority(aPriorityThread2);
130 thread3.SetPriority(aPriorityThread1);
134 User::WaitForRequest(logonStatus1);
135 User::WaitForRequest(logonStatus2);
136 User::WaitForRequest(logonStatus3);
137 test(thread1.ExitType()==EExitKill);
138 test(logonStatus1==KErrNone);
139 test(thread2.ExitType()==EExitKill);
140 test(logonStatus2==KErrNone);
141 test(thread3.ExitType()==EExitKill);
142 test(logonStatus3==KErrNone);
149 return threadMutexAcquireOrder;
153 enum TTestProcessFunctions
155 ETestProcessThreadPrioritiesEqual,
156 ETestProcessThreadPrioritiesHighLow
159 #include "testprocess.h"
161 TInt DoTestProcess(TInt aTestNum,TInt aArg1,TInt aArg2)
168 case ETestProcessThreadPrioritiesEqual:
170 TInt acquireOrder = TestThreadMutexAcquireOrder((TThreadPriority)aArg1, (TThreadPriority)aArg2);
171 if (acquireOrder != 123)
173 thread.Panic(KTestPanicCategory,999);
178 case ETestProcessThreadPrioritiesHighLow:
180 TInt acquireOrder = TestThreadMutexAcquireOrder((TThreadPriority)aArg1, (TThreadPriority)aArg2);
181 if (acquireOrder != 132)
183 thread.Panic(KTestPanicCategory,999);
189 User::Panic(_L("T_SPRIORITYCAP"),1);
196 //---------------------------------------------
197 //! @SYMTestCaseID KBASE-T_SPRIORITYCAP-0121
198 //! @SYMTestCaseDesc Check prioritisation of threads with ProtServ capability
201 //! @SYMTestActions Create sets of three threads with various priorities and have them wait on
202 //! a mutex. Signal the mutex to see whether the threads obtain it in priority or wait order.
203 //! Note: 2 MMP files build test exe with RealTimeServer and WindowServer process priorities.
204 //! Test creates copy of this executable with/without required capabilities.
205 //! @SYMTestExpectedResults All thread priorities are obtainable to processes with ProtServ, so
206 //! confirm they are correctly mapped to absolute priorities, that MuchMore > More etc. and
207 //! that SystemServer/More is correctly mapped for SYMBIAN_CURB_SYSTEMSERVER_PRIORITIES macro.
208 //! @SYMTestPriority Critical
209 //! @SYMTestStatus Implemented
210 //---------------------------------------------
211 void TestPriorityMappingWithProtServ()
213 const TUint32 capability = 1u<<ECapabilityProtServ; // only ProtServ capability
214 RTestProcess process;
216 TProcessPriority processPriority = process.Priority();
217 // only call with the following process priorities
218 ASSERT((processPriority == EPriorityWindowServer) || (processPriority == EPriorityFileServer)
219 || (processPriority == EPrioritySupervisor) || (processPriority == EPriorityRealTimeServer));
221 test.Start(_L("Test EPriorityRealTime is greater than EPriorityMuchMore"));
222 process.Create(capability,ETestProcessThreadPrioritiesHighLow,EPriorityRealTime,EPriorityMuchMore);
225 test.Next(_L("Test EPriorityMuchMore is greater than EPriorityMore"));
226 process.Create(capability,ETestProcessThreadPrioritiesHighLow,EPriorityMuchMore,EPriorityMore);
229 test.Next(_L("Test EPriorityMore is greater than EPriorityNormal"));
230 process.Create(capability,ETestProcessThreadPrioritiesHighLow,EPriorityMore,EPriorityNormal);
233 test.Next(_L("Test EPriorityNormal is greater than EPriorityLess"));
234 process.Create(capability,ETestProcessThreadPrioritiesHighLow,EPriorityNormal,EPriorityLess);
237 test.Next(_L("Test EPriorityLess is greater than EPriorityMuchLess"));
238 process.Create(capability,ETestProcessThreadPrioritiesHighLow,EPriorityLess,EPriorityMuchLess);
241 test.Next(_L("Test EPriorityMore versus independent capping priority"));
242 process.Create(capability,
243 (processPriority == EPriorityRealTimeServer) ? ETestProcessThreadPrioritiesHighLow : ETestProcessThreadPrioritiesEqual,
245 #ifdef SYMBIAN_CURB_SYSTEMSERVER_PRIORITIES
246 EPriorityAbsoluteHigh
248 EPriorityAbsoluteRealTime1
257 //---------------------------------------------
258 //! @SYMTestCaseID KBASE-T_SPRIORITYCAP-0122
259 //! @SYMTestCaseDesc Check prioritisation of threads without ProtServ capability
262 //! @SYMTestActions Create sets of three threads with various priorities and have them wait on
263 //! a mutex. Signal the mutex to see whether the threads obtain it in priority or wait order.
264 //! Note: 2 MMP files build test exe with RealTimeServer and WindowServer process priorities.
265 //! Test creates copy of this executable with/without required capabilities.
266 //! @SYMTestExpectedResults Confirm thread priorities are capped at SystemServer/More without ProtServ,
267 //! so many priority enumerations will map to the same absolute priority. Confirm that
268 //! SystemServer/More is correctly mapped for SYMBIAN_CURB_SYSTEMSERVER_PRIORITIES macro.
269 //! @SYMTestPriority Critical
270 //! @SYMTestStatus Implemented
271 //---------------------------------------------
272 void TestPriorityMappingWithoutProtServ()
274 const TUint32 capability = ~(1u<<ECapabilityProtServ); // all capabilities except ProtServ
275 RTestProcess process;
277 TProcessPriority processPriority = process.Priority();
278 // only call with the following process priorities
279 ASSERT((processPriority == EPriorityWindowServer) || (processPriority == EPriorityFileServer)
280 || (processPriority == EPrioritySupervisor) || (processPriority == EPriorityRealTimeServer));
282 test.Start(_L("Test EPriorityRealTime and EPriorityMuchMore are capped and equal"));
283 process.Create(capability,ETestProcessThreadPrioritiesEqual,EPriorityRealTime,EPriorityMuchMore);
286 test.Next(_L("Test EPriorityMuchMore and EPriorityMore are capped and equal"));
287 process.Create(capability,ETestProcessThreadPrioritiesEqual,EPriorityMuchMore,EPriorityMore);
290 if (processPriority == EPriorityRealTimeServer)
292 test.Next(_L("Test EPriorityMore and EPriorityMuchLess are capped and equal"));
293 process.Create(capability,ETestProcessThreadPrioritiesEqual,EPriorityMore,EPriorityMuchLess);
296 test.Next(_L("Test EPriorityNormal and EPriorityMuchLess are capped and equal"));
297 process.Create(capability,ETestProcessThreadPrioritiesEqual,EPriorityNormal,EPriorityMuchLess);
300 test.Next(_L("Test EPriorityLess and EPriorityMuchLess are capped and equal"));
301 process.Create(capability,ETestProcessThreadPrioritiesEqual,EPriorityLess,EPriorityMuchLess);
305 test.Next(_L("Test EPriorityMore versus EPriorityAbsoluteHigh"));
306 process.Create(capability,
307 #ifdef SYMBIAN_CURB_SYSTEMSERVER_PRIORITIES
308 ETestProcessThreadPrioritiesEqual,
310 ETestProcessThreadPrioritiesHighLow,
312 EPriorityMore,EPriorityAbsoluteHigh);
319 GLDEF_C TInt E32Main()
322 User::CommandLine(cmd);
323 if(cmd.Length() && TChar(cmd[0]).IsDigit())
335 return DoTestProcess(function,arg1,arg2);
340 if(!PlatSec::ConfigSetting(PlatSec::EPlatSecEnforcement))
342 test.Start(_L("TESTS NOT RUN - EPlatSecEnforcement is OFF"));
347 test.Next(_L("Test thread priority mappings for processes with ECapabilityProtServ"));
348 TestPriorityMappingWithProtServ();
350 test.Start(_L("Test thread priority mappings for processes without ECapabilityProtServ"));
351 TestPriorityMappingWithoutProtServ();