os/kernelhwsrv/kerneltest/e32test/secure/t_sthread.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/secure/t_sthread.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,974 @@
     1.4 +// Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// e32test\secure\t_sthread.cpp
    1.18 +// Overview:
    1.19 +// Test the platform security aspects of the RThread class
    1.20 +// API Information:
    1.21 +// RThread
    1.22 +// Details:
    1.23 +// - Test renaming the current thread and renaming a thread from 
    1.24 +// another thread. Verify results are as expected.
    1.25 +// - Test resuming a thread from a different process and from the
    1.26 +// process that created the thread. Verify results are as expected.
    1.27 +// - Verify that other processes can not suspend a thread and that the
    1.28 +// creating process can.
    1.29 +// - Perform a variety of tests on killing, terminating and panicking
    1.30 +// a thread. Verify results are as expected.
    1.31 +// - Test setting thread priority in a variety of ways, verify results
    1.32 +// are as expected. Includes ensuring real-time priorities are
    1.33 +// unavailable to processes without capability ProtServ.
    1.34 +// - Test RequestComplete and RequestSignal on a thread in a variety 
    1.35 +// of ways, verify results are as expected.
    1.36 +// - Test SetProcessPriority on a thread in a variety of ways, verify 
    1.37 +// results are as expected.
    1.38 +// - Test the Heap, ExceptionHandler, SetExceptionHandler, 
    1.39 +// ModifyExceptionMask, RaiseException, IsExceptionHandled, 
    1.40 +// SetProtected and SetSystem methods. Verify results as expected.
    1.41 +// Platforms/Drives/Compatibility:
    1.42 +// All.
    1.43 +// Assumptions/Requirement/Pre-requisites:
    1.44 +// Failures and causes:
    1.45 +// Base Port information:
    1.46 +// 
    1.47 +//
    1.48 +
    1.49 +#include <e32test.h>
    1.50 +
    1.51 +LOCAL_D RTest test(_L("T_STHREAD"));
    1.52 +
    1.53 +_LIT(KSyncMutex,"T_STHREAD-sync-mutex");
    1.54 +RMutex SyncMutex;
    1.55 +
    1.56 +void Wait()
    1.57 +	{
    1.58 +	RMutex syncMutex;
    1.59 +	if(syncMutex.OpenGlobal(KSyncMutex)!=KErrNone)
    1.60 +		User::Invariant();
    1.61 +	syncMutex.Wait();
    1.62 +	syncMutex.Signal();
    1.63 +	syncMutex.Close();
    1.64 +	}
    1.65 +
    1.66 +enum TTestProcessFunctions
    1.67 +	{
    1.68 +	ETestProcessResume,
    1.69 +	ETestProcessSuspend,
    1.70 +	ETestProcessKill,
    1.71 +	ETestProcessTerminate,
    1.72 +	ETestProcessPanic,
    1.73 +	ETestProcessRequestComplete,
    1.74 +	ETestProcessRequestSignal,
    1.75 +	ETestProcessPriorityControlOff,
    1.76 +	ETestProcessPriorityControlOn,
    1.77 +	ETestProcessSetPriority,
    1.78 +	ETestProcessSetPrioritiesWithoutProtServ,
    1.79 +	ETestProcessSetPrioritiesWithProtServ
    1.80 +	};
    1.81 +
    1.82 +#include "testprocess.h"
    1.83 +
    1.84 +_LIT(KTestPanicCategory,"TEST PANIC");
    1.85 +_LIT(KTestThreadName,"TestName");
    1.86 +
    1.87 +
    1.88 +class RTestThread : public RThread
    1.89 +	{
    1.90 +public:
    1.91 +	void Create(TThreadFunction aFunction,TAny* aArg=0);
    1.92 +	};
    1.93 +
    1.94 +volatile TInt TestThreadCount = 0;
    1.95 +
    1.96 +TInt TestThreadNull(TAny*)
    1.97 +	{
    1.98 +	++TestThreadCount;
    1.99 +	Wait();
   1.100 +	return KErrNone;
   1.101 +	}
   1.102 +
   1.103 +void RTestThread::Create(TThreadFunction aFunction,TAny* aArg)
   1.104 +	{
   1.105 +	TInt r=RThread::Create(_L("TestThread"),aFunction,KDefaultStackSize,KDefaultStackSize,KDefaultStackSize,aArg);
   1.106 +	test(r==KErrNone);
   1.107 +	}
   1.108 +
   1.109 +
   1.110 +// these priorities are available to any process
   1.111 +void TestSetNormalApplicationPriorities(RThread& aThread)
   1.112 +	{
   1.113 +	TThreadPriority priority = aThread.Priority(); // save priority to restore before return
   1.114 +	aThread.SetPriority(EPriorityAbsoluteVeryLow);
   1.115 +	test(aThread.Priority()==EPriorityAbsoluteVeryLow);
   1.116 +	aThread.SetPriority(EPriorityAbsoluteLowNormal);
   1.117 +	test(aThread.Priority()==EPriorityAbsoluteLowNormal);
   1.118 +	aThread.SetPriority(EPriorityAbsoluteLow);
   1.119 +	test(aThread.Priority()==EPriorityAbsoluteLow);
   1.120 +	aThread.SetPriority(EPriorityAbsoluteBackgroundNormal);
   1.121 +	test(aThread.Priority()==EPriorityAbsoluteBackgroundNormal);
   1.122 +	aThread.SetPriority(EPriorityAbsoluteBackground);
   1.123 +	test(aThread.Priority()==EPriorityAbsoluteBackground);
   1.124 +	aThread.SetPriority(EPriorityAbsoluteForegroundNormal);
   1.125 +	test(aThread.Priority()==EPriorityAbsoluteForegroundNormal);
   1.126 +	aThread.SetPriority(EPriorityAbsoluteForeground);
   1.127 +	test(aThread.Priority()==EPriorityAbsoluteForeground);
   1.128 +	aThread.SetPriority(EPriorityAbsoluteHighNormal);
   1.129 +	test(aThread.Priority()==EPriorityAbsoluteHighNormal);
   1.130 +	aThread.SetPriority(EPriorityAbsoluteHigh);
   1.131 +	test(aThread.Priority()==EPriorityAbsoluteHigh);
   1.132 +	aThread.SetPriority(priority);
   1.133 +	}
   1.134 +
   1.135 +TInt TestThreadSetPriority(TAny* aArg)
   1.136 +	{
   1.137 +	RThread thisThread;
   1.138 +	thisThread.SetPriority((TThreadPriority)(reinterpret_cast<TInt>(aArg)));
   1.139 +	return KErrNone;
   1.140 +	}
   1.141 +
   1.142 +void TestSetPriorityPanic(TThreadPriority aPriority)
   1.143 +	{
   1.144 +	RTestThread thread;
   1.145 +	TRequestStatus status;
   1.146 +	thread.Create(TestThreadSetPriority, reinterpret_cast<TAny*>(aPriority));
   1.147 +	thread.Logon(status);
   1.148 +	thread.Resume();
   1.149 +	User::WaitForRequest(status);
   1.150 +	test(thread.ExitType()==EExitPanic);
   1.151 +	test(thread.ExitCategory()==_L("KERN-EXEC"));
   1.152 +	test(thread.ExitReason()==46);
   1.153 +	CLOSE_AND_WAIT(thread);
   1.154 +	}
   1.155 +
   1.156 +void TestSetPrioritySuccess(TThreadPriority aPriority)
   1.157 +	{
   1.158 +	RTestThread thread;
   1.159 +	TRequestStatus status;
   1.160 +	thread.Create(TestThreadSetPriority, reinterpret_cast<TAny*>(aPriority));
   1.161 +	thread.Logon(status);
   1.162 +	thread.Resume();
   1.163 +	User::WaitForRequest(status);
   1.164 +	test(thread.Priority()==aPriority);
   1.165 +	test(thread.ExitCategory()==_L("Kill"));
   1.166 +	test(thread.ExitReason()==0);
   1.167 +	CLOSE_AND_WAIT(thread);
   1.168 +	}
   1.169 +
   1.170 +TInt DoTestProcess(TInt aTestNum,TInt aArg1,TInt aArg2)
   1.171 +	{
   1.172 +	RThread thread;
   1.173 +	TInt r;
   1.174 +
   1.175 +	switch(aTestNum)
   1.176 +		{
   1.177 +
   1.178 +	case ETestProcessResume:
   1.179 +		{
   1.180 +		r = thread.Open(aArg1);
   1.181 +		if(r==KErrNone)
   1.182 +			thread.Resume(); // Should panic us
   1.183 +		return r;
   1.184 +		}
   1.185 +
   1.186 +	case ETestProcessSuspend:
   1.187 +		{
   1.188 +		r = thread.Open(aArg1);
   1.189 +		if(r==KErrNone)
   1.190 +			thread.Suspend(); // Should panic us
   1.191 +		return r;
   1.192 +		}
   1.193 +
   1.194 +	case ETestProcessKill:
   1.195 +		{
   1.196 +		r = thread.Open(aArg1);
   1.197 +		if(r==KErrNone)
   1.198 +			thread.Kill(999); // Should panic us
   1.199 +		return r;
   1.200 +		}
   1.201 +
   1.202 +	case ETestProcessTerminate:
   1.203 +		{
   1.204 +		r = thread.Open(aArg1);
   1.205 +		if(r==KErrNone)
   1.206 +			thread.Terminate(999); // Should panic us
   1.207 +		return r;
   1.208 +		}
   1.209 +
   1.210 +	case ETestProcessPanic:
   1.211 +		{
   1.212 +		r = thread.Open(aArg1);
   1.213 +		if(r==KErrNone)
   1.214 +			thread.Panic(KTestPanicCategory,999); // Should panic us
   1.215 +		return r;
   1.216 +		}
   1.217 +
   1.218 +	case ETestProcessSetPriority:
   1.219 +		{
   1.220 +		r = thread.Open(aArg1);
   1.221 +		if(r==KErrNone)
   1.222 +			thread.SetPriority((TThreadPriority)aArg2);
   1.223 +		return r;
   1.224 +		}
   1.225 +
   1.226 +	case ETestProcessRequestComplete:
   1.227 +		{
   1.228 +		r = thread.Open(aArg1);
   1.229 +		if(r==KErrNone)
   1.230 +			{
   1.231 +			// use a local request status because Thread::RequestComplete is
   1.232 +			// implemented to write to it in our context
   1.233 +			TRequestStatus myStatus;
   1.234 +			TRequestStatus* status = &myStatus;
   1.235 +			thread.RequestComplete(status,KErrNone);
   1.236 +			}
   1.237 +		return r;
   1.238 +		}
   1.239 +
   1.240 +	case ETestProcessRequestSignal:
   1.241 +		{
   1.242 +		r = thread.Open(aArg1);
   1.243 +		if(r==KErrNone)
   1.244 +			thread.RequestSignal();
   1.245 +		return r;
   1.246 +		}
   1.247 +
   1.248 +	case ETestProcessPriorityControlOn:
   1.249 +		User::SetPriorityControl(ETrue);
   1.250 +		// fall through...
   1.251 +	case ETestProcessPriorityControlOff:
   1.252 +		RProcess::Rendezvous(RThread().Id());
   1.253 +		Wait();
   1.254 +		return KErrNone;
   1.255 +
   1.256 +	case ETestProcessSetPrioritiesWithoutProtServ:
   1.257 +		{
   1.258 +		RThread thread;
   1.259 +		TestSetNormalApplicationPriorities(thread);
   1.260 +		TestSetPriorityPanic(EPriorityAbsoluteRealTime1);
   1.261 +		TestSetPriorityPanic(EPriorityAbsoluteRealTime2);
   1.262 +		TestSetPriorityPanic(EPriorityAbsoluteRealTime3);
   1.263 +		TestSetPriorityPanic(EPriorityAbsoluteRealTime4);
   1.264 +		TestSetPriorityPanic(EPriorityAbsoluteRealTime5);
   1.265 +		TestSetPriorityPanic(EPriorityAbsoluteRealTime6);
   1.266 +		TestSetPriorityPanic(EPriorityAbsoluteRealTime7);
   1.267 +		TestSetPriorityPanic(EPriorityAbsoluteRealTime8);	
   1.268 +		return KErrNone;
   1.269 +		}
   1.270 +		
   1.271 +	case ETestProcessSetPrioritiesWithProtServ:
   1.272 +		{
   1.273 +		RThread thread;
   1.274 +		TestSetNormalApplicationPriorities(thread);
   1.275 +		TestSetPrioritySuccess(EPriorityAbsoluteRealTime1);
   1.276 +		TestSetPrioritySuccess(EPriorityAbsoluteRealTime2);
   1.277 +		TestSetPrioritySuccess(EPriorityAbsoluteRealTime3);
   1.278 +		TestSetPrioritySuccess(EPriorityAbsoluteRealTime4);
   1.279 +		TestSetPrioritySuccess(EPriorityAbsoluteRealTime5);
   1.280 +		TestSetPrioritySuccess(EPriorityAbsoluteRealTime6);
   1.281 +		TestSetPrioritySuccess(EPriorityAbsoluteRealTime7);
   1.282 +		TestSetPrioritySuccess(EPriorityAbsoluteRealTime8);	
   1.283 +		return KErrNone;
   1.284 +		}
   1.285 +
   1.286 +	default:
   1.287 +		User::Panic(_L("T_STHREAD"),1);
   1.288 +		}
   1.289 +
   1.290 +	return KErrNone;
   1.291 +	}
   1.292 +
   1.293 +
   1.294 +
   1.295 +void TestThreadForPlatformSecurityTrap(TThreadFunction aFunction)
   1.296 +	{
   1.297 +	TBool jit = User::JustInTime();
   1.298 +	TRequestStatus logonStatus;
   1.299 +	RTestThread thread;
   1.300 +	thread.Create(aFunction,(TAny*)(TUint)RThread().Id());
   1.301 +	thread.Logon(logonStatus);
   1.302 +	User::SetJustInTime(EFalse);
   1.303 +	thread.Resume();
   1.304 +	User::WaitForRequest(logonStatus);
   1.305 +	User::SetJustInTime(jit);
   1.306 +	test(thread.ExitType()==EExitPanic);
   1.307 +	test(logonStatus==EPlatformSecurityTrap);
   1.308 +	CLOSE_AND_WAIT(thread);
   1.309 +	}
   1.310 +
   1.311 +
   1.312 +void TestProcessForPlatformSecurityTrap(TTestProcessFunctions aFunction)
   1.313 +	{
   1.314 +	TRequestStatus logonStatus2;
   1.315 +	RTestProcess process;
   1.316 +	process.Create(~0u,aFunction,RThread().Id(),EPriorityAbsoluteLow);
   1.317 +	process.Logon(logonStatus2);
   1.318 +	process.Resume();
   1.319 +	User::WaitForRequest(logonStatus2);
   1.320 +	test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
   1.321 +	test(logonStatus2==EPlatformSecurityTrap);
   1.322 +	CLOSE_AND_WAIT(process);
   1.323 +	}
   1.324 +
   1.325 +
   1.326 +void TestRename()
   1.327 +	{
   1.328 +	TName name;
   1.329 +
   1.330 +	test.Start(_L("Renaming the current thread"));
   1.331 +	name = RThread().Name();
   1.332 +	name.SetLength(KTestThreadName().Length());
   1.333 +	test(name.CompareF(KTestThreadName)!=0);
   1.334 +	User::RenameThread(KTestThreadName);
   1.335 +	name = RThread().Name();
   1.336 +	name.SetLength(KTestThreadName().Length());
   1.337 +	test(name.CompareF(KTestThreadName)==0);
   1.338 +
   1.339 +
   1.340 +	test.End();
   1.341 +	}
   1.342 +
   1.343 +
   1.344 +
   1.345 +void TestResume()
   1.346 +	{
   1.347 +	RTestProcess process;
   1.348 +	RTestThread thread;
   1.349 +	TRequestStatus logonStatus;
   1.350 +	TInt testCount = TestThreadCount;
   1.351 +
   1.352 +	test.Start(_L("Try to get another process to resume one we've created"));
   1.353 +	thread.Create(TestThreadNull);
   1.354 +	process.Create(~0u,ETestProcessResume,thread.Id());
   1.355 +	process.Logon(logonStatus);
   1.356 +	process.Resume();
   1.357 +	User::WaitForRequest(logonStatus);
   1.358 +	test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
   1.359 +	test(logonStatus==EPlatformSecurityTrap);
   1.360 +	User::After(1000000); // Give time for thread to run (if it had been resumed)...
   1.361 +	test(TestThreadCount==testCount); // it shouldn't have, so count will be unchanged.
   1.362 +
   1.363 +	test.Next(_L("Test resuming a thread we've created"));
   1.364 +	thread.Logon(logonStatus);
   1.365 +	test(logonStatus==KRequestPending);
   1.366 +	thread.Resume();
   1.367 +	User::WaitForRequest(logonStatus);
   1.368 +	test(logonStatus==KErrNone);
   1.369 +	test(TestThreadCount==testCount+1); // Thread should have run and incremented the count
   1.370 +	CLOSE_AND_WAIT(thread);
   1.371 +	CLOSE_AND_WAIT(process);
   1.372 +
   1.373 +	test.End();
   1.374 +	}
   1.375 +
   1.376 +
   1.377 +
   1.378 +TInt TestThreadCounting(TAny*)
   1.379 +	{
   1.380 +	RThread().SetPriority(EPriorityAbsoluteVeryLow);
   1.381 +	for(;;)
   1.382 +		++TestThreadCount;
   1.383 +	}
   1.384 +
   1.385 +TBool IsTestThreadRunning()
   1.386 +	{
   1.387 +	 // Thread should have been busy incrementing the count if it is running
   1.388 +	TInt testCount = TestThreadCount;
   1.389 +	User::After(100000);
   1.390 +	if(testCount!=TestThreadCount)
   1.391 +		return ETrue;
   1.392 +	User::After(1000000);
   1.393 +	return testCount!=TestThreadCount;
   1.394 +	}
   1.395 +
   1.396 +void TestSuspend()
   1.397 +	{
   1.398 +	RTestProcess process;
   1.399 +	RTestThread thread;
   1.400 +	TRequestStatus logonStatus;
   1.401 +
   1.402 +	test.Start(_L("Creating a never ending thread..."));
   1.403 +	thread.Create(TestThreadCounting);
   1.404 +	thread.Resume();
   1.405 +	test(IsTestThreadRunning());  // Thread should still be running
   1.406 +
   1.407 +	test.Next(_L("Checking other process can't supspend it"));
   1.408 +	process.Create(~0u,ETestProcessSuspend,thread.Id());
   1.409 +	process.Logon(logonStatus);
   1.410 +	process.Resume();
   1.411 +	User::WaitForRequest(logonStatus);
   1.412 +	test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
   1.413 +	test(logonStatus==EPlatformSecurityTrap);
   1.414 +	test(IsTestThreadRunning());  // Thread should still be running
   1.415 +
   1.416 +	test.Next(_L("Test suspending a thread in same process"));
   1.417 +	thread.Logon(logonStatus);
   1.418 +	thread.Suspend();
   1.419 +	test(!IsTestThreadRunning()); // Thread should have stopped...
   1.420 +	test(logonStatus==KRequestPending); // but not have died
   1.421 +	thread.LogonCancel(logonStatus);
   1.422 +	User::WaitForRequest(logonStatus);
   1.423 +	
   1.424 +	test.Next(_L("Kill thread"));
   1.425 +	thread.Kill(0);
   1.426 +	CLOSE_AND_WAIT(thread);
   1.427 +	CLOSE_AND_WAIT(process);
   1.428 +
   1.429 +	test.End();
   1.430 +	}
   1.431 +
   1.432 +
   1.433 +
   1.434 +TInt TestThreadKillSelf(TAny* )
   1.435 +	{
   1.436 +	RThread().Kill(999);
   1.437 +	return KErrGeneral;
   1.438 +	}
   1.439 +
   1.440 +TInt TestThreadTerminateSelf(TAny*)
   1.441 +	{
   1.442 +	RThread().Terminate(999);
   1.443 +	return KErrGeneral;
   1.444 +	}
   1.445 +
   1.446 +TInt TestThreadPanicSelf(TAny*)
   1.447 +	{
   1.448 +	RThread().Panic(KTestPanicCategory,999);
   1.449 +	return KErrGeneral;
   1.450 +	}
   1.451 +
   1.452 +void TestKill()
   1.453 +	{
   1.454 +	RTestProcess process;
   1.455 +	RTestThread thread;
   1.456 +	TRequestStatus logonStatus;
   1.457 +	TRequestStatus logonStatus2;
   1.458 +	TBool jit = User::JustInTime();
   1.459 +
   1.460 +	// Test RProcess::Kill()
   1.461 +
   1.462 +	test.Start(_L("Test killing an un-resumed thread created by us"));
   1.463 +	thread.Create(TestThreadNull);
   1.464 +	thread.Logon(logonStatus);
   1.465 +	thread.Kill(999);
   1.466 +	User::WaitForRequest(logonStatus);
   1.467 +	test(thread.ExitType()==EExitKill);
   1.468 +	test(logonStatus==999);
   1.469 +	CLOSE_AND_WAIT(thread);
   1.470 +
   1.471 +	test.Next(_L("Test killing a resumed thread created by us"));
   1.472 +	thread.Create(TestThreadNull);
   1.473 +	thread.Logon(logonStatus);
   1.474 +	SyncMutex.Wait();
   1.475 +	thread.Resume();
   1.476 +	thread.Kill(999);
   1.477 +	SyncMutex.Signal();
   1.478 +	User::WaitForRequest(logonStatus);
   1.479 +	test(thread.ExitType()==EExitKill);
   1.480 +	test(logonStatus==999);
   1.481 +	CLOSE_AND_WAIT(thread);
   1.482 +
   1.483 +	test.Next(_L("Try killing un-resumed thread not created by self"));
   1.484 +	thread.Create(TestThreadNull);
   1.485 +	process.Create(~0u,ETestProcessKill,thread.Id());
   1.486 +	thread.Logon(logonStatus2);
   1.487 +	process.Logon(logonStatus);
   1.488 +	process.Resume();
   1.489 +	User::WaitForRequest(logonStatus);
   1.490 +	test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
   1.491 +	test(logonStatus==EPlatformSecurityTrap);
   1.492 +	test(logonStatus2==KRequestPending); // the thread should still be alive
   1.493 +	thread.Resume();
   1.494 +	User::WaitForRequest(logonStatus2);
   1.495 +	test(logonStatus2==KErrNone);
   1.496 +	CLOSE_AND_WAIT(thread);
   1.497 +	CLOSE_AND_WAIT(process);
   1.498 +
   1.499 +	test.Next(_L("Try killing resumed thread not created by self"));
   1.500 +	thread.Create(TestThreadNull);
   1.501 +	process.Create(~0u,ETestProcessKill,thread.Id());
   1.502 +	thread.Logon(logonStatus2);
   1.503 +	process.Logon(logonStatus);
   1.504 +	SyncMutex.Wait();
   1.505 +	thread.Resume();
   1.506 +	process.Resume();
   1.507 +	User::WaitForRequest(logonStatus);
   1.508 +	test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
   1.509 +	test(logonStatus==EPlatformSecurityTrap);
   1.510 +	test(logonStatus2==KRequestPending); // the thread should still be alive
   1.511 +	SyncMutex.Signal();
   1.512 +	User::WaitForRequest(logonStatus2);
   1.513 +	test(logonStatus2==KErrNone);
   1.514 +	CLOSE_AND_WAIT(thread);
   1.515 +	CLOSE_AND_WAIT(process);
   1.516 +
   1.517 +	test.Next(_L("Test a thread killing itself"));
   1.518 +	thread.Create(TestThreadKillSelf);
   1.519 +	thread.Logon(logonStatus);
   1.520 +	thread.Resume();
   1.521 +	User::WaitForRequest(logonStatus);
   1.522 +	test(thread.ExitType()==EExitKill);
   1.523 +	test(logonStatus==999);
   1.524 +	CLOSE_AND_WAIT(thread);
   1.525 +
   1.526 +	// Test RProcess::Teminate()
   1.527 +
   1.528 +	test.Next(_L("Test terminating an un-resumed thread created by us"));
   1.529 +	thread.Create(TestThreadNull);
   1.530 +	thread.Logon(logonStatus);
   1.531 +	thread.Terminate(999);
   1.532 +	User::WaitForRequest(logonStatus);
   1.533 +	test(thread.ExitType()==EExitTerminate);
   1.534 +	test(logonStatus==999);
   1.535 +	CLOSE_AND_WAIT(thread);
   1.536 +
   1.537 +	test.Next(_L("Test terminating a resumed thread created by us"));
   1.538 +	thread.Create(TestThreadNull);
   1.539 +	thread.Logon(logonStatus);
   1.540 +	SyncMutex.Wait();
   1.541 +	thread.Resume();
   1.542 +	thread.Terminate(999);
   1.543 +	SyncMutex.Signal();
   1.544 +	User::WaitForRequest(logonStatus);
   1.545 +	test(thread.ExitType()==EExitTerminate);
   1.546 +	test(logonStatus==999);
   1.547 +	CLOSE_AND_WAIT(thread);
   1.548 +
   1.549 +	test.Next(_L("Try terminating un-resumed thread not created by self"));
   1.550 +	thread.Create(TestThreadNull);
   1.551 +	process.Create(~0u,ETestProcessTerminate,thread.Id());
   1.552 +	thread.Logon(logonStatus2);
   1.553 +	process.Logon(logonStatus);
   1.554 +	process.Resume();
   1.555 +	User::WaitForRequest(logonStatus);
   1.556 +	test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
   1.557 +	test(logonStatus==EPlatformSecurityTrap);
   1.558 +	test(logonStatus2==KRequestPending); // the thread should still be alive
   1.559 +	thread.Resume();
   1.560 +	User::WaitForRequest(logonStatus2);
   1.561 +	test(logonStatus2==KErrNone);
   1.562 +	CLOSE_AND_WAIT(thread);
   1.563 +	CLOSE_AND_WAIT(process);
   1.564 +
   1.565 +	test.Next(_L("Try terminating resumed thread not created by self"));
   1.566 +	thread.Create(TestThreadNull);
   1.567 +	process.Create(~0u,ETestProcessTerminate,thread.Id());
   1.568 +	thread.Logon(logonStatus2);
   1.569 +	process.Logon(logonStatus);
   1.570 +	SyncMutex.Wait();
   1.571 +	thread.Resume();
   1.572 +	process.Resume();
   1.573 +	User::WaitForRequest(logonStatus);
   1.574 +	test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
   1.575 +	test(logonStatus==EPlatformSecurityTrap);
   1.576 +	test(logonStatus2==KRequestPending); // the thread should still be alive
   1.577 +	SyncMutex.Signal();
   1.578 +	User::WaitForRequest(logonStatus2);
   1.579 +	test(logonStatus2==KErrNone);
   1.580 +	CLOSE_AND_WAIT(thread);
   1.581 +	CLOSE_AND_WAIT(process);
   1.582 +
   1.583 +	test.Next(_L("Test a thread terminating itself"));
   1.584 +	thread.Create(TestThreadTerminateSelf);
   1.585 +	thread.Logon(logonStatus);
   1.586 +	thread.Resume();
   1.587 +	User::WaitForRequest(logonStatus);
   1.588 +	test(thread.ExitType()==EExitTerminate);
   1.589 +	test(logonStatus==999);
   1.590 +	CLOSE_AND_WAIT(thread);
   1.591 +
   1.592 +	// Test RProcess::Panic()
   1.593 +
   1.594 +	test.Next(_L("Test panicking an un-resumed thread created by us"));
   1.595 +	thread.Create(TestThreadNull);
   1.596 +	thread.Logon(logonStatus);
   1.597 +	User::SetJustInTime(EFalse);
   1.598 +	thread.Panic(KTestPanicCategory,999);
   1.599 +	User::WaitForRequest(logonStatus);
   1.600 +	User::SetJustInTime(jit);
   1.601 +	test(thread.ExitType()==EExitPanic);
   1.602 +	test(logonStatus==999);
   1.603 +	CLOSE_AND_WAIT(thread);
   1.604 +
   1.605 +	test.Next(_L("Test panicking a resumed thread created by us"));
   1.606 +	thread.Create(TestThreadNull);
   1.607 +	thread.Logon(logonStatus);
   1.608 +	SyncMutex.Wait();
   1.609 +	thread.Resume();
   1.610 +	User::SetJustInTime(EFalse);
   1.611 +	thread.Panic(KTestPanicCategory,999);
   1.612 +	SyncMutex.Signal();
   1.613 +	User::WaitForRequest(logonStatus);
   1.614 +	User::SetJustInTime(jit);
   1.615 +	test(thread.ExitType()==EExitPanic);
   1.616 +	test(logonStatus==999);
   1.617 +	CLOSE_AND_WAIT(thread);
   1.618 +
   1.619 +	test.Next(_L("Try panicking un-resumed thread not created by self"));
   1.620 +	thread.Create(TestThreadNull);
   1.621 +	process.Create(~0u,ETestProcessPanic,thread.Id());
   1.622 +	thread.Logon(logonStatus2);
   1.623 +	process.Logon(logonStatus);
   1.624 +	process.Resume();
   1.625 +	User::WaitForRequest(logonStatus);
   1.626 +	test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
   1.627 +	test(logonStatus==EPlatformSecurityTrap);
   1.628 +	test(logonStatus2==KRequestPending); // the thread should still be alive
   1.629 +	thread.Resume();
   1.630 +	User::WaitForRequest(logonStatus2);
   1.631 +	test(logonStatus2==KErrNone);
   1.632 +	CLOSE_AND_WAIT(thread);
   1.633 +	CLOSE_AND_WAIT(process);
   1.634 +
   1.635 +	test.Next(_L("Try panicking resumed thread not created by self"));
   1.636 +	thread.Create(TestThreadNull);
   1.637 +	process.Create(~0u,ETestProcessPanic,thread.Id());
   1.638 +	thread.Logon(logonStatus2);
   1.639 +	process.Logon(logonStatus);
   1.640 +	SyncMutex.Wait();
   1.641 +	thread.Resume();
   1.642 +	process.Resume();
   1.643 +	User::WaitForRequest(logonStatus);
   1.644 +	test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
   1.645 +	test(logonStatus==EPlatformSecurityTrap);
   1.646 +	test(logonStatus2==KRequestPending); // the thread should still be alive
   1.647 +	SyncMutex.Signal();
   1.648 +	User::WaitForRequest(logonStatus2);
   1.649 +	test(logonStatus2==KErrNone);
   1.650 +	CLOSE_AND_WAIT(thread);
   1.651 +	CLOSE_AND_WAIT(process);
   1.652 +
   1.653 +	test.Next(_L("Test a thread panicking itself"));
   1.654 +	thread.Create(TestThreadPanicSelf);
   1.655 +	thread.Logon(logonStatus);
   1.656 +	User::SetJustInTime(EFalse);
   1.657 +	thread.Resume();
   1.658 +	User::WaitForRequest(logonStatus);
   1.659 +	User::SetJustInTime(jit);
   1.660 +	test(thread.ExitType()==EExitPanic);
   1.661 +	test(logonStatus==999);
   1.662 +	CLOSE_AND_WAIT(thread);
   1.663 +
   1.664 +	// 
   1.665 +
   1.666 +	test.End();
   1.667 +	}
   1.668 +
   1.669 +
   1.670 +//---------------------------------------------
   1.671 +//! @SYMTestCaseID KBASE-T_STHREAD-0120
   1.672 +//! @SYMTestCaseDesc Set thread priority
   1.673 +//! @SYMTestType UT
   1.674 +//! @SYMREQ historical, enhanced under PREQ955
   1.675 +//! @SYMTestActions Test setting all thread priority values to threads in this process,
   1.676 +//!     and in another process, resumed and not.
   1.677 +//! @SYMTestExpectedResults Confirm can set and get "normal application" thread priorities
   1.678 +//!     for threads in this process, whether resumed or not. Confirm thread is panicked
   1.679 +//!     if attempts to set priority of thread in another process. Confirm can set and get
   1.680 +//!     "real-time" thread priorities if this process has ProtServ capability, and that
   1.681 +//!     calling thread is panicked if not.
   1.682 +//! @SYMTestPriority Critical
   1.683 +//! @SYMTestStatus Implemented
   1.684 +//---------------------------------------------
   1.685 +void TestSetPriority()
   1.686 +	{
   1.687 +	RTestThread thread;
   1.688 +	RTestProcess process;
   1.689 +	TRequestStatus logonStatus;
   1.690 +	TRequestStatus logonStatus2;
   1.691 +
   1.692 +	test.Start(_L("Test changing our own threads priority"));
   1.693 +	TestSetNormalApplicationPriorities(thread);
   1.694 +
   1.695 +	test.Next(_L("Test changing priority of un-resumed thread in our process"));
   1.696 +	thread.Create(TestThreadNull);
   1.697 +	thread.Logon(logonStatus);
   1.698 +	TestSetNormalApplicationPriorities(thread);
   1.699 +
   1.700 +	test.Next(_L("Test changing priority of resumed thread in our process"));
   1.701 +	SyncMutex.Wait();
   1.702 +	thread.Resume();
   1.703 +	TestSetNormalApplicationPriorities(thread);
   1.704 +	SyncMutex.Signal();
   1.705 +	User::WaitForRequest(logonStatus);
   1.706 +	test(logonStatus==KErrNone);
   1.707 +	CLOSE_AND_WAIT(thread);
   1.708 +
   1.709 +	test.Next(_L("Try changing priority of an un-resumed thread in other process"));
   1.710 +	thread.Create(TestThreadNull);
   1.711 +	thread.Logon(logonStatus);
   1.712 +	thread.SetPriority(EPriorityAbsoluteHigh);
   1.713 +	process.Create(~0u,ETestProcessSetPriority,thread.Id(),EPriorityAbsoluteLow);
   1.714 +	process.Logon(logonStatus2);
   1.715 +	process.Resume();
   1.716 +	User::WaitForRequest(logonStatus2);
   1.717 +	test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
   1.718 +	test(logonStatus2==EPlatformSecurityTrap);
   1.719 +	test(thread.Priority()==EPriorityAbsoluteHigh); // Priority should be unaltered
   1.720 +
   1.721 +	test.Next(_L("Try changing priority of a resumed thread in other process"));
   1.722 +	process.Create(~0u,ETestProcessSetPriority,thread.Id(),EPriorityAbsoluteLow);
   1.723 +	process.Logon(logonStatus2);
   1.724 +	SyncMutex.Wait();
   1.725 +	thread.Resume();
   1.726 +	process.Resume();
   1.727 +	User::WaitForRequest(logonStatus2);
   1.728 +	test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
   1.729 +	test(logonStatus2==EPlatformSecurityTrap);
   1.730 +	test(thread.Priority()==EPriorityAbsoluteHigh); // Priority should be unaltered
   1.731 +	SyncMutex.Signal();
   1.732 +	User::WaitForRequest(logonStatus);
   1.733 +	test(logonStatus==KErrNone);
   1.734 +	CLOSE_AND_WAIT(thread);
   1.735 +
   1.736 +	test.Next(_L("Test setting thread priorities without ECapabilityProtServ"));
   1.737 +	process.Create(~(1u<<ECapabilityProtServ), ETestProcessSetPrioritiesWithoutProtServ);
   1.738 +	process.Run();
   1.739 +	
   1.740 +	test.Next(_L("Test setting thread priorities with ECapabilityProtServ"));
   1.741 +	process.Create(1<<ECapabilityProtServ, ETestProcessSetPrioritiesWithProtServ);
   1.742 +	process.Run();
   1.743 +
   1.744 +	test.End();
   1.745 +	}
   1.746 +
   1.747 +
   1.748 +TRequestStatus TestRequest;
   1.749 +
   1.750 +TInt TestThreadRequestComplete(TAny* aArg)
   1.751 +	{
   1.752 +	RThread thread;
   1.753 +	TInt r = thread.Open((TInt)aArg);
   1.754 +	if(r==KErrNone)
   1.755 +		{
   1.756 +		TRequestStatus* status = &TestRequest;
   1.757 +		thread.RequestComplete(status,KErrNone);
   1.758 +		}
   1.759 +	return r;
   1.760 +	}
   1.761 +
   1.762 +void TestRequestComplete()
   1.763 +	{
   1.764 +	RTestThread thread;
   1.765 +	RTestProcess process;
   1.766 +	TRequestStatus logonStatus;
   1.767 +
   1.768 +	test.Start(_L("Test RequestComplete on thread in current process"));
   1.769 +	TestRequest = KRequestPending;
   1.770 +	thread.Create(TestThreadRequestComplete,(TAny*)(TUint)RThread().Id());
   1.771 +	thread.Logon(logonStatus);
   1.772 +	thread.Resume();
   1.773 +	User::WaitForRequest(TestRequest);
   1.774 +	test(TestRequest==KErrNone);
   1.775 +	User::WaitForRequest(logonStatus);
   1.776 +	test(logonStatus==KErrNone);
   1.777 +	CLOSE_AND_WAIT(thread);
   1.778 +
   1.779 +	test.Next(_L("Test RequestComplete on with NULL request pointer"));
   1.780 +	test(RThread().RequestCount()==0); // No signals
   1.781 +	TRequestStatus* nullReq = 0;
   1.782 +	RThread().RequestComplete(nullReq,0);
   1.783 +	test(RThread().RequestCount()==0); // No signals
   1.784 +
   1.785 +	test.Next(_L("Test RequestComplete on thread in different process"));
   1.786 +	TestRequest = KRequestPending;
   1.787 +	process.Create(~0u,ETestProcessRequestComplete,RThread().Id(),(TInt)&TestRequest);
   1.788 +	process.Logon(logonStatus);
   1.789 +	process.Resume();
   1.790 +	User::WaitForRequest(logonStatus);
   1.791 +	test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
   1.792 +	test(logonStatus==EPlatformSecurityTrap);
   1.793 +	test(TestRequest==KRequestPending);
   1.794 +	CLOSE_AND_WAIT(process);
   1.795 +
   1.796 +	test.End();
   1.797 +	}
   1.798 +
   1.799 +
   1.800 +
   1.801 +TInt TestThreadRequestSignal(TAny* aArg)
   1.802 +	{
   1.803 +	RThread thread;
   1.804 +	TInt r = thread.Open((TInt)aArg);
   1.805 +	if(r==KErrNone)
   1.806 +		thread.RequestSignal();
   1.807 +	return r;
   1.808 +	}
   1.809 +
   1.810 +void TestRequestSignal()
   1.811 +	{
   1.812 +	RTestThread thread;
   1.813 +	RTestProcess process;
   1.814 +	TRequestStatus logonStatus;
   1.815 +	TInt count;
   1.816 +
   1.817 +	test.Start(_L("Test RequestSignal on thread in current process"));
   1.818 +	thread.Create(TestThreadRequestSignal,(TAny*)(TUint)RThread().Id());
   1.819 +	thread.Logon(logonStatus);
   1.820 +	count = RThread().RequestCount();
   1.821 +	test(count==0); // No signals yet
   1.822 +	thread.Resume();
   1.823 +	User::WaitForRequest(logonStatus);
   1.824 +	test(logonStatus==KErrNone);
   1.825 +	count = RThread().RequestCount();
   1.826 +	test(count==1); // We should have been signalled
   1.827 +	User::WaitForAnyRequest();	// eat signal
   1.828 +	CLOSE_AND_WAIT(thread);
   1.829 +
   1.830 +	test.Next(_L("Test RequestSignal on thread in different process"));
   1.831 +	process.Create(~0u,ETestProcessRequestSignal,RThread().Id(),0);
   1.832 +	process.Logon(logonStatus);
   1.833 +	process.Resume();
   1.834 +	User::WaitForRequest(logonStatus);
   1.835 +	test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
   1.836 +	test(logonStatus==EPlatformSecurityTrap);
   1.837 +	count = RThread().RequestCount();
   1.838 +	test(count==0); // We shouldn't have been signalled
   1.839 +	CLOSE_AND_WAIT(process);
   1.840 +
   1.841 +	test.End();
   1.842 +	}
   1.843 +
   1.844 +
   1.845 +
   1.846 +void TestSetProcessPriority()
   1.847 +	{
   1.848 +	RTestThread thread;
   1.849 +	RTestProcess process;
   1.850 +	TProcessPriority priority;
   1.851 +	TRequestStatus rendezvousStatus;
   1.852 +	TRequestStatus logonStatus;
   1.853 +	TInt r;
   1.854 +
   1.855 +	test.Start(_L("Test changing our own process priority"));
   1.856 +	priority = process.Priority();
   1.857 +	thread.SetProcessPriority(EPriorityLow);
   1.858 +	test(process.Priority()==EPriorityLow);
   1.859 +	thread.SetProcessPriority(EPriorityBackground);
   1.860 +	test(process.Priority()==EPriorityBackground);
   1.861 +	thread.SetProcessPriority(EPriorityForeground);
   1.862 +	test(process.Priority()==EPriorityForeground);
   1.863 +	thread.SetProcessPriority(priority);
   1.864 +
   1.865 +	test.Next(_L("Try changing other process's priority (no priority-control enabled)"));
   1.866 +	process.Create(~0u,ETestProcessPriorityControlOff);
   1.867 +	process.Rendezvous(rendezvousStatus);
   1.868 +	process.Logon(logonStatus);
   1.869 +	SyncMutex.Wait();
   1.870 +	process.Resume();
   1.871 +	User::WaitForRequest(rendezvousStatus); // Process has started
   1.872 +	r = thread.Open(rendezvousStatus.Int()); // Process returned Id of main thread as status value
   1.873 +	test(r==KErrNone);
   1.874 +	priority = process.Priority();
   1.875 +	thread.SetProcessPriority(EPriorityLow);
   1.876 +	test(process.Priority()==priority); // priority shouldn't have changed
   1.877 +	thread.SetProcessPriority(EPriorityBackground);
   1.878 +	test(process.Priority()==priority); // priority shouldn't have changed
   1.879 +	thread.SetProcessPriority(EPriorityForeground);
   1.880 +	test(process.Priority()==priority); // priority shouldn't have changed
   1.881 +	test(logonStatus==KRequestPending); // wait for process to end
   1.882 +	SyncMutex.Signal();
   1.883 +	User::WaitForRequest(logonStatus);
   1.884 +	CLOSE_AND_WAIT(thread);
   1.885 +	CLOSE_AND_WAIT(process);
   1.886 +
   1.887 +	test.Next(_L("Try changing other process's priority (priority-control enabled)"));
   1.888 +	process.Create(~0u,ETestProcessPriorityControlOn);
   1.889 +	process.Rendezvous(rendezvousStatus);
   1.890 +	process.Logon(logonStatus);
   1.891 +	SyncMutex.Wait();
   1.892 +	process.Resume();
   1.893 +	User::WaitForRequest(rendezvousStatus); // Process has started
   1.894 +	r = thread.Open(rendezvousStatus.Int()); // Process returned Id of main thread as status value
   1.895 +	test(r==KErrNone);
   1.896 +	priority = process.Priority();
   1.897 +	thread.SetProcessPriority(EPriorityForeground);
   1.898 +	test(process.Priority()==EPriorityForeground);
   1.899 +	thread.SetProcessPriority(EPriorityBackground);
   1.900 +	test(process.Priority()==EPriorityBackground);
   1.901 +	thread.SetProcessPriority(EPriorityForeground);
   1.902 +	test(process.Priority()==EPriorityForeground);
   1.903 +	thread.SetProcessPriority(EPriorityLow);
   1.904 +	test(process.Priority()==EPriorityForeground); // should still be foreground priority
   1.905 +	thread.SetProcessPriority(priority);
   1.906 +	test(logonStatus==KRequestPending); // wait for process to end
   1.907 +	SyncMutex.Signal();
   1.908 +	User::WaitForRequest(logonStatus);
   1.909 +	CLOSE_AND_WAIT(thread);
   1.910 +	CLOSE_AND_WAIT(process);
   1.911 +
   1.912 +	test.End();
   1.913 +	}
   1.914 +
   1.915 +
   1.916 +
   1.917 +GLDEF_C TInt E32Main()
   1.918 +    {
   1.919 +	TBuf16<512> cmd;
   1.920 +	User::CommandLine(cmd);
   1.921 +	if(cmd.Length() && TChar(cmd[0]).IsDigit())
   1.922 +		{
   1.923 +		TInt function = -1;
   1.924 +		TInt arg1 = -1;
   1.925 +		TInt arg2 = -1;
   1.926 +		TLex lex(cmd);
   1.927 +
   1.928 +		lex.Val(function);
   1.929 +		lex.SkipSpace();
   1.930 +		lex.Val(arg1);
   1.931 +		lex.SkipSpace();
   1.932 +		lex.Val(arg2);
   1.933 +		return DoTestProcess(function,arg1,arg2);
   1.934 +		}
   1.935 +
   1.936 +	test.Title();
   1.937 +
   1.938 +	if((!PlatSec::ConfigSetting(PlatSec::EPlatSecProcessIsolation))||(!PlatSec::ConfigSetting(PlatSec::EPlatSecEnforcement)))
   1.939 +		{
   1.940 +		test.Start(_L("TESTS NOT RUN - PlatSecProcessIsolation is not enforced"));
   1.941 +		test.End();
   1.942 +		return 0;
   1.943 +		}
   1.944 +
   1.945 +	test(SyncMutex.CreateGlobal(KSyncMutex)==KErrNone);
   1.946 +	
   1.947 +	test.Start(_L("Test Rename"));
   1.948 +	TestRename();
   1.949 +
   1.950 +	test.Next(_L("Test Resume"));
   1.951 +	TestResume();
   1.952 +
   1.953 +	test.Next(_L("Test Suspend"));
   1.954 +	TestSuspend();
   1.955 +
   1.956 +	test.Next(_L("Test Kill, Panic and Teminate"));
   1.957 +	TestKill();
   1.958 +
   1.959 +	test.Next(_L("Test SetPriority"));
   1.960 +	TestSetPriority();
   1.961 +
   1.962 +	test.Next(_L("Test RequestComplete"));
   1.963 +	TestRequestComplete();
   1.964 +
   1.965 +	test.Next(_L("Test RequestSignal"));
   1.966 +	TestRequestSignal();
   1.967 +
   1.968 +	test.Next(_L("Test SetProcessPriority"));
   1.969 +	TestSetProcessPriority();
   1.970 +
   1.971 +
   1.972 +	SyncMutex.Close();
   1.973 +	test.End();
   1.974 +
   1.975 +	return(0);
   1.976 +    }
   1.977 +