sl@0: // Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // e32test\secure\t_sprocess.cpp sl@0: // Overview: sl@0: // Test the platform security aspects of the RProcess class sl@0: // API Information: sl@0: // RProcess sl@0: // Details: sl@0: // - Test SetJustInTime on the current process, a new un-Resumed process sl@0: // and between processes. Verify results are as expected. sl@0: // - Test process renaming and verify results are as expected. sl@0: // - Test killing, terminating and panicking different processes, verify sl@0: // results are as expected. sl@0: // - Test resuming a process from a different process, verify results. sl@0: // - Test setting process priority in a variety of ways, verify results sl@0: // are as expected. sl@0: // - Test the RProcess SetType(), SetProtected(), CommandLineLength(), sl@0: // CommandLine(), SetSystem(), SetOwner() and Owner() methods. Verify sl@0: // results are as expected. sl@0: // Platforms/Drives/Compatibility: sl@0: // All. sl@0: // Assumptions/Requirement/Pre-requisites: sl@0: // Failures and causes: sl@0: // Base Port information: sl@0: // sl@0: // sl@0: sl@0: #include sl@0: sl@0: LOCAL_D RTest test(_L("T_SPROCESS")); sl@0: sl@0: _LIT(KSyncMutext,"T_SPROCESS-sync-mutex"); sl@0: RMutex SyncMutex; sl@0: sl@0: void Wait() sl@0: { sl@0: RMutex syncMutex; sl@0: if(syncMutex.OpenGlobal(KSyncMutext)!=KErrNone) sl@0: User::Invariant(); sl@0: syncMutex.Wait(); sl@0: syncMutex.Signal(); sl@0: syncMutex.Close(); sl@0: } sl@0: sl@0: enum TTestProcessFunctions sl@0: { sl@0: ETestProcessNull, sl@0: ETestProcessSetJustInTime, sl@0: ETestProcessKill, sl@0: ETestProcessTerminate, sl@0: ETestProcessPanic, sl@0: ETestProcessKillSelf, sl@0: ETestProcessTerminateSelf, sl@0: ETestProcessPanicSelf, sl@0: ETestProcessResume, sl@0: ETestProcessPriority, sl@0: ETestProcessPriorityControlOff, sl@0: ETestProcessPriorityControlOn, sl@0: ETestProcessPriorityControlOnAndLowPriority, sl@0: ETestProcessSetPriority, sl@0: }; sl@0: sl@0: #include "testprocess.h" sl@0: sl@0: _LIT(KTestPanicCategory,"TEST PANIC"); sl@0: _LIT(KTestProcessName,"TestName"); sl@0: _LIT(KTestProcessName2,"TestName2"); sl@0: sl@0: sl@0: TInt DoTestProcess(TInt aTestNum,TInt aArg1,TInt aArg2) sl@0: { sl@0: RTestProcess process; sl@0: TInt r; sl@0: sl@0: switch(aTestNum) sl@0: { sl@0: sl@0: case ETestProcessNull: sl@0: Wait(); sl@0: return KErrNone; sl@0: sl@0: case ETestProcessSetJustInTime: sl@0: { sl@0: r = process.Open(aArg1); sl@0: if(r==KErrNone) sl@0: process.SetJustInTime(!process.JustInTime()); // Should panic us sl@0: return r; sl@0: } sl@0: sl@0: case ETestProcessResume: sl@0: { sl@0: r = process.Open(aArg1); sl@0: if(r==KErrNone) sl@0: process.Resume(); // Should panic us sl@0: return r; sl@0: } sl@0: sl@0: case ETestProcessKill: sl@0: { sl@0: r = process.Open(aArg1); sl@0: if(r==KErrNone) sl@0: process.Kill(999); sl@0: return r; sl@0: } sl@0: sl@0: case ETestProcessTerminate: sl@0: { sl@0: r = process.Open(aArg1); sl@0: if(r==KErrNone) sl@0: process.Terminate(999); sl@0: return r; sl@0: } sl@0: sl@0: case ETestProcessPanic: sl@0: { sl@0: r = process.Open(aArg1); sl@0: if(r==KErrNone) sl@0: process.Panic(KTestPanicCategory,999); sl@0: return r; sl@0: } sl@0: sl@0: case ETestProcessKillSelf: sl@0: { sl@0: RProcess().Kill(999); sl@0: return KErrNone; sl@0: } sl@0: sl@0: case ETestProcessTerminateSelf: sl@0: { sl@0: RProcess().Terminate(999); sl@0: return KErrNone; sl@0: } sl@0: sl@0: case ETestProcessPanicSelf: sl@0: { sl@0: RProcess().Panic(KTestPanicCategory,999); sl@0: return KErrNone; sl@0: } sl@0: sl@0: case ETestProcessSetPriority: sl@0: { sl@0: r = process.Open(aArg1); sl@0: if(r==KErrNone) sl@0: process.SetPriority((TProcessPriority)aArg2); sl@0: return r; sl@0: } sl@0: sl@0: sl@0: case ETestProcessPriority: sl@0: { sl@0: r = process.Open(aArg1); sl@0: if(r!=KErrNone) sl@0: return r; sl@0: TProcessPriority priority; sl@0: priority = process.Priority(); sl@0: process.SetPriority(EPriorityLow); sl@0: if(process.Priority()!=priority) // priority shouldn't have changed sl@0: return KErrGeneral; sl@0: process.SetPriority(EPriorityBackground); sl@0: if(process.Priority()!=priority) // priority shouldn't have changed sl@0: return KErrGeneral; sl@0: process.SetPriority(EPriorityForeground); sl@0: if(process.Priority()!=priority) // priority shouldn't have changed sl@0: return KErrGeneral; sl@0: return KErrNone; sl@0: } sl@0: sl@0: case ETestProcessPriorityControlOnAndLowPriority: sl@0: RProcess().SetPriority(EPriorityLow); sl@0: // fall through... sl@0: case ETestProcessPriorityControlOn: sl@0: User::SetPriorityControl(ETrue); sl@0: // fall through... sl@0: case ETestProcessPriorityControlOff: sl@0: RProcess::Rendezvous(0); sl@0: Wait(); sl@0: return KErrNone; sl@0: sl@0: default: sl@0: User::Panic(_L("T_SPROCESS"),1); sl@0: } sl@0: sl@0: return KErrNone; sl@0: } sl@0: sl@0: sl@0: sl@0: void TestProcessForPlatformSecurityTrap(TTestProcessFunctions aFunction) sl@0: { sl@0: TRequestStatus logonStatus2; sl@0: RTestProcess process; sl@0: process.Create(~0u,aFunction,RProcess().Id(),EPriorityAbsoluteLow); sl@0: process.Logon(logonStatus2); sl@0: process.Resume(); sl@0: User::WaitForRequest(logonStatus2); sl@0: test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic sl@0: test(logonStatus2==EPlatformSecurityTrap); sl@0: } sl@0: sl@0: sl@0: sl@0: void TestSetJustInTime() sl@0: { sl@0: RTestProcess process; sl@0: RTestProcess process2; sl@0: TRequestStatus logonStatus; sl@0: sl@0: test.Start(_L("Test SetJustInTime on current process")); sl@0: TBool jit = process.JustInTime(); sl@0: process.SetJustInTime(!jit); sl@0: test((process.JustInTime()!=EFalse)!=(jit!=EFalse)); sl@0: process.SetJustInTime(jit); sl@0: test((process.JustInTime()!=EFalse)==(jit!=EFalse)); sl@0: sl@0: test.Next(_L("Test SetJustInTime on a new un-Resumed process")); sl@0: process.RProcess::Create(RProcess().FileName(),_L("")); sl@0: jit = process.JustInTime(); sl@0: process.SetJustInTime(!jit); sl@0: test((process.JustInTime()!=EFalse)!=(jit!=EFalse)); sl@0: process.SetJustInTime(jit); sl@0: test((process.JustInTime()!=EFalse)==(jit!=EFalse)); sl@0: process.Kill(0); sl@0: CLOSE_AND_WAIT(process); sl@0: sl@0: test.Next(_L("Try other process using SetJustInTime on our created process")); sl@0: process2.Create(0,ETestProcessNull); sl@0: process.Create(~0u,ETestProcessSetJustInTime,process2.Id()); sl@0: process.Logon(logonStatus); sl@0: process.Resume(); sl@0: User::WaitForRequest(logonStatus); sl@0: test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic sl@0: test(logonStatus==EPlatformSecurityTrap); sl@0: CLOSE_AND_WAIT(process); sl@0: process2.Kill(0); sl@0: CLOSE_AND_WAIT(process2); sl@0: sl@0: test.Next(_L("Try other process to using SetJustInTime on us")); sl@0: process.Create(~0u,ETestProcessSetJustInTime); sl@0: process.Logon(logonStatus); sl@0: process.Resume(); sl@0: User::WaitForRequest(logonStatus); sl@0: test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic sl@0: test(logonStatus==EPlatformSecurityTrap); sl@0: CLOSE_AND_WAIT(process); sl@0: sl@0: test.End(); sl@0: } sl@0: sl@0: sl@0: sl@0: void TestRename() sl@0: { sl@0: TName name; sl@0: sl@0: test.Start(_L("Renaming the current process with User::RenameProcess")); sl@0: name = RProcess().Name(); sl@0: name.SetLength(KTestProcessName().Length()); sl@0: test(name.CompareF(KTestProcessName)!=0); sl@0: User::RenameProcess(KTestProcessName); sl@0: name = RProcess().Name(); sl@0: name.SetLength(KTestProcessName().Length()); sl@0: test(name.CompareF(KTestProcessName)==0); sl@0: sl@0: sl@0: test.End(); sl@0: } sl@0: sl@0: sl@0: sl@0: void TestKill() sl@0: { sl@0: RTestProcess process; sl@0: RTestProcess process2; sl@0: TRequestStatus logonStatus; sl@0: TRequestStatus logonStatus2; sl@0: sl@0: process2.Create(0,ETestProcessNull); sl@0: process2.Logon(logonStatus2); sl@0: sl@0: // Test RProcess::Kill() sl@0: sl@0: test.Start(_L("Test killing an un-resumed process created by us")); sl@0: process.Create(0,ETestProcessNull); sl@0: process.Logon(logonStatus); sl@0: process.Kill(999); sl@0: User::WaitForRequest(logonStatus); sl@0: test(process.ExitType()==EExitKill); sl@0: test(logonStatus==999); sl@0: CLOSE_AND_WAIT(process); sl@0: sl@0: test.Next(_L("Try killing un-resumed process not created by self")); sl@0: process.Create(~(1u< cmd; sl@0: User::CommandLine(cmd); sl@0: if(cmd.Length() && TChar(cmd[0]).IsDigit()) sl@0: { sl@0: TInt function = -1; sl@0: TInt arg1 = -1; sl@0: TInt arg2 = -1; sl@0: TLex lex(cmd); sl@0: sl@0: lex.Val(function); sl@0: lex.SkipSpace(); sl@0: lex.Val(arg1); sl@0: lex.SkipSpace(); sl@0: lex.Val(arg2); sl@0: return DoTestProcess(function,arg1,arg2); sl@0: } sl@0: sl@0: test.Title(); sl@0: sl@0: if((!PlatSec::ConfigSetting(PlatSec::EPlatSecProcessIsolation))||(!PlatSec::ConfigSetting(PlatSec::EPlatSecEnforcement))) sl@0: { sl@0: test.Start(_L("TESTS NOT RUN - PlatSecProcessIsolation is not enforced")); sl@0: test.End(); sl@0: return 0; sl@0: } sl@0: sl@0: test(SyncMutex.CreateGlobal(KSyncMutext)==KErrNone); sl@0: sl@0: test.Start(_L("Test SetJustInTime")); sl@0: TestSetJustInTime(); sl@0: sl@0: test.Next(_L("Test Rename")); sl@0: TestRename(); sl@0: sl@0: test.Next(_L("Test Kill, Panic and Teminate")); sl@0: TestKill(); sl@0: sl@0: test.Next(_L("Test Resume")); sl@0: TestResume(); sl@0: sl@0: test.Next(_L("Test SetPriority")); sl@0: TestSetPriority(); sl@0: sl@0: sl@0: SyncMutex.Close(); sl@0: test.End(); sl@0: sl@0: return(0); sl@0: } sl@0: