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_smessage.cpp sl@0: // Overview: sl@0: // Test RMessagePtr2 and server message passing sl@0: // API Information: sl@0: // RMessagePtr2, RMessage2 sl@0: // Details: sl@0: // - Start a test server and open a server session. Perform various IPC tests sl@0: // and verify results are as expected. sl@0: // - Start a thread and request the server kill, terminate and panic the client sl@0: // thread. Verify results are as expected. sl@0: // - Perform various tests of setting the process priority via the sl@0: // RMessagePtr2::SetProcessPriority() method. Verify results are as expected. sl@0: // - Verify the ability to open a handle on the client thread using the sl@0: // RMessagePtr2::Client() method. sl@0: // - Verify message passing via an RMessage2 object. sl@0: // - Verify the RMessagePtr2::Handle and RMessagePtr::Session methods work sl@0: // 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_SMESSAGE")); sl@0: sl@0: enum TTestProcessFunctions sl@0: { sl@0: ETestProcessServer, sl@0: }; sl@0: sl@0: #include "testprocess.h" sl@0: sl@0: TInt StartServer(); sl@0: sl@0: TInt DoTestProcess(TInt aTestNum,TInt aArg1,TInt aArg2) sl@0: { sl@0: (void)aArg1; sl@0: (void)aArg2; sl@0: sl@0: switch(aTestNum) sl@0: { sl@0: sl@0: case ETestProcessServer: sl@0: return StartServer(); sl@0: sl@0: default: sl@0: User::Panic(_L("T_SMESSAGE"),1); sl@0: } sl@0: sl@0: return KErrNone; sl@0: } sl@0: sl@0: sl@0: sl@0: class RTestThread : public RThread sl@0: { sl@0: public: sl@0: void Create(TThreadFunction aFunction,TAny* aArg=0); sl@0: }; sl@0: sl@0: void RTestThread::Create(TThreadFunction aFunction,TAny* aArg) sl@0: { sl@0: TInt r=RThread::Create(_L(""),aFunction,KDefaultStackSize,KDefaultStackSize,KDefaultStackSize,aArg); sl@0: test(r==KErrNone); sl@0: } sl@0: sl@0: sl@0: // sl@0: // CTestSession sl@0: // sl@0: sl@0: class CTestSession : public CSession2 sl@0: { sl@0: public: sl@0: enum {EShutdown,ETestIpc,ETestKill,ETestTerminate,ETestPanic, sl@0: ETestSetProcessPriority,ETestClient,ETestRMessage,ETestHandle,ETestSession}; sl@0: public: sl@0: CTestSession(); sl@0: virtual void ServiceL(const RMessage2& aMessage); sl@0: public: sl@0: }; sl@0: sl@0: CTestSession::CTestSession() sl@0: : CSession2() sl@0: { sl@0: } sl@0: sl@0: const TInt KTestDataMaxLength8 = 20; sl@0: const TInt KTestDataMaxLength16 = 40; sl@0: _LIT8(KTestData8,"12345678"); sl@0: _LIT16(KTestData16,"1234567890123456"); sl@0: _LIT(KTestPanicCategory,"TEST PANIC"); sl@0: sl@0: void CTestSession::ServiceL(const RMessage2& aMessage) sl@0: { sl@0: RMessagePtr2 m(aMessage); sl@0: switch (aMessage.Function()) sl@0: { sl@0: case CTestSession::ETestIpc: sl@0: { sl@0: TInt r,l,ml; sl@0: // 8 bit descriptors sl@0: TBuf8 buf8; sl@0: buf8.FillZ(); sl@0: // GetDesLength sl@0: l = m.GetDesLength(0); sl@0: if(l!=KTestData8().Length()) m.Complete(l<0 ? l : KErrGeneral); sl@0: // Read sl@0: r = m.Read(0,buf8,1); sl@0: if(r!=KErrNone) m.Complete(r); sl@0: if(buf8.Compare(KTestData8().Right(l-1))) m.Complete(KErrGeneral); sl@0: // ReadL sl@0: buf8.Zero(); sl@0: buf8.FillZ(); sl@0: m.ReadL(0,buf8,1); sl@0: if(buf8.Compare(KTestData8().Right(l-1))) m.Complete(KErrGeneral); sl@0: // GetDesMaxLength sl@0: ml = m.GetDesMaxLength(2); sl@0: if(ml!=KTestDataMaxLength8) m.Complete(ml<0 ? ml : KErrGeneral); sl@0: // Write sl@0: r = m.Write(2,KTestData8(),0); sl@0: if(r!=KErrNone) m.Complete(r); sl@0: // WriteL sl@0: m.WriteL(2,KTestData8(),1); sl@0: // 16 bit descriptors sl@0: TBuf16 buf16; sl@0: buf16.FillZ(); sl@0: // GetDesLength sl@0: l = m.GetDesLength(1); sl@0: if(l!=KTestData16().Length()) m.Complete(l<0 ? l : KErrGeneral); sl@0: // Read sl@0: r = m.Read(1,buf16,1); sl@0: if(r!=KErrNone) m.Complete(r); sl@0: if(buf16.Compare(KTestData16().Right(l-1))) m.Complete(KErrGeneral); sl@0: // ReadL sl@0: buf16.Zero(); sl@0: buf16.FillZ(); sl@0: m.ReadL(1,buf16,1); sl@0: if(buf16.Compare(KTestData16().Right(l-1))) m.Complete(KErrGeneral); sl@0: // GetDesMaxLength sl@0: ml = m.GetDesMaxLength(3); sl@0: if(ml!=KTestDataMaxLength16) m.Complete(ml<0 ? ml : KErrGeneral); sl@0: // Write sl@0: r = m.Write(3,KTestData16(),0); sl@0: if(r!=KErrNone) m.Complete(r); sl@0: // WriteL sl@0: m.WriteL(3,KTestData16(),1); sl@0: // done sl@0: aMessage.Complete(KErrNone); sl@0: } sl@0: break; sl@0: sl@0: case CTestSession::ETestKill: sl@0: m.Kill(999); sl@0: break; sl@0: sl@0: case CTestSession::ETestTerminate: sl@0: m.Terminate(999); sl@0: break; sl@0: sl@0: case CTestSession::ETestPanic: sl@0: m.Panic(KTestPanicCategory,999); sl@0: break; sl@0: sl@0: case ETestSetProcessPriority: sl@0: m.Complete(m.SetProcessPriority((TProcessPriority)aMessage.Int0())); sl@0: break; sl@0: sl@0: case ETestClient: sl@0: { sl@0: RThread client; sl@0: m.Client(client); sl@0: m.Complete(client.Id()); sl@0: client.Close(); sl@0: } sl@0: break; sl@0: sl@0: case ETestRMessage: sl@0: { sl@0: RMessage2 message2(m); sl@0: if (Mem::Compare((TUint8*)&message2,sizeof(message2),(TUint8*)&aMessage,sizeof(aMessage))) sl@0: m.Complete(KErrGeneral); sl@0: else sl@0: m.Complete(KErrNone); sl@0: } sl@0: break; sl@0: sl@0: case ETestHandle: sl@0: if (m.Handle()!=aMessage.Handle()) sl@0: m.Complete(KErrGeneral); sl@0: else sl@0: m.Complete(KErrNone); sl@0: break; sl@0: sl@0: case CTestSession::ETestSession: sl@0: if (aMessage.Session()!=this) sl@0: m.Complete(KErrGeneral); sl@0: else sl@0: m.Complete(KErrNone); sl@0: break; sl@0: sl@0: case CTestSession::EShutdown: sl@0: CActiveScheduler::Stop(); sl@0: break; sl@0: sl@0: default: sl@0: m.Complete(KErrNotSupported); sl@0: break; sl@0: } sl@0: } sl@0: sl@0: sl@0: sl@0: // sl@0: // CTestServer sl@0: // sl@0: sl@0: class CTestServer : public CServer2 sl@0: { sl@0: public: sl@0: CTestServer(TInt aPriority); sl@0: virtual CSession2* NewSessionL(const TVersion& aVersion,const RMessage2& aMessage) const; sl@0: }; sl@0: sl@0: CTestServer::CTestServer(TInt aPriority) sl@0: : CServer2(aPriority) sl@0: { sl@0: } sl@0: sl@0: CSession2* CTestServer::NewSessionL(const TVersion& /*aVersion*/,const RMessage2& /*aMessage*/) const sl@0: { sl@0: return new (ELeave) CTestSession(); sl@0: } sl@0: sl@0: sl@0: sl@0: // sl@0: // CTestActiveScheduler sl@0: // sl@0: sl@0: class CTestActiveScheduler : public CActiveScheduler sl@0: { sl@0: public: sl@0: virtual void Error(TInt anError) const; sl@0: }; sl@0: sl@0: void CTestActiveScheduler::Error(TInt anError) const sl@0: { sl@0: User::Panic(_L("TestServer Error"),anError); sl@0: } sl@0: sl@0: sl@0: sl@0: // sl@0: // Server thread sl@0: // sl@0: sl@0: _LIT(KServerName,"T_SMESSAGE-server"); sl@0: const TInt KServerRendezvous = KRequestPending+1; sl@0: sl@0: void DoStartServer() sl@0: { sl@0: CTestActiveScheduler* activeScheduler = new (ELeave) CTestActiveScheduler; sl@0: CActiveScheduler::Install(activeScheduler); sl@0: CleanupStack::PushL(activeScheduler); sl@0: sl@0: CTestServer* server = new (ELeave) CTestServer(0); sl@0: CleanupStack::PushL(server); sl@0: sl@0: User::LeaveIfError(server->Start(KServerName)); sl@0: sl@0: RProcess::Rendezvous(KServerRendezvous); sl@0: sl@0: CActiveScheduler::Start(); sl@0: sl@0: CleanupStack::PopAndDestroy(2); sl@0: } sl@0: sl@0: TInt StartServer() sl@0: { sl@0: CTrapCleanup* cleanupStack = CTrapCleanup::New(); sl@0: if(!cleanupStack) sl@0: return KErrNoMemory; sl@0: TRAPD(leaveError,DoStartServer()) sl@0: delete cleanupStack; sl@0: return leaveError; sl@0: } sl@0: sl@0: sl@0: sl@0: // sl@0: // RTestSession sl@0: // sl@0: sl@0: class RTestSession : public RSessionBase sl@0: { sl@0: public: sl@0: inline TInt Connect() sl@0: { return CreateSession(KServerName,TVersion());} sl@0: inline TInt Send(TInt aFunction) sl@0: { return RSessionBase::SendReceive(aFunction); } sl@0: inline TInt Send(TInt aFunction,const TIpcArgs& aArgs) sl@0: { return RSessionBase::SendReceive(aFunction,aArgs); } sl@0: inline void Send(TInt aFunction,TRequestStatus& aStatus) sl@0: { RSessionBase::SendReceive(aFunction,aStatus); } sl@0: inline void Send(TInt aFunction,const TIpcArgs& aArgs,TRequestStatus& aStatus) sl@0: { RSessionBase::SendReceive(aFunction,aArgs,aStatus); } sl@0: }; sl@0: sl@0: sl@0: sl@0: TInt TestThreadServerKill(TAny* aArg) sl@0: { sl@0: RTestSession Session; sl@0: TInt r = Session.Connect(); sl@0: if(r!=KErrNone) sl@0: return KErrGeneral; sl@0: User::SetJustInTime(EFalse); sl@0: r = Session.Send((TInt)aArg); sl@0: Session.Close(); sl@0: return KErrGeneral; sl@0: } sl@0: sl@0: sl@0: RTestSession Session; sl@0: sl@0: void TestIPC() sl@0: { sl@0: TBuf8 buf8; sl@0: TBuf16 buf16; sl@0: TInt r = Session.Send(CTestSession::ETestIpc,TIpcArgs(&KTestData8,&KTestData16,&buf8,&buf16)); sl@0: test(r==KErrNone); sl@0: TInt l = KTestData8().Length(); sl@0: test(buf8.Length()==l+1); sl@0: test(KTestData8().Compare(buf8.Right(l))==0); sl@0: l = KTestData16().Length(); sl@0: test(buf16.Length()==l+1); sl@0: test(KTestData16().Compare(buf16.Right(l))==0); sl@0: } sl@0: sl@0: sl@0: sl@0: void TestKill() sl@0: { sl@0: RTestThread thread; sl@0: TRequestStatus logonStatus; sl@0: sl@0: test.Start(_L("Test Kill")); sl@0: thread.Create(TestThreadServerKill,(TAny*)CTestSession::ETestKill); sl@0: thread.Logon(logonStatus); sl@0: thread.Resume(); sl@0: User::WaitForRequest(logonStatus); sl@0: User::SetJustInTime(ETrue); sl@0: test(thread.ExitType()==EExitKill); sl@0: test(logonStatus==999); sl@0: CLOSE_AND_WAIT(thread); sl@0: sl@0: test.Next(_L("Test Terminate")); sl@0: thread.Create(TestThreadServerKill,(TAny*)CTestSession::ETestTerminate); sl@0: thread.Logon(logonStatus); sl@0: thread.Resume(); sl@0: User::WaitForRequest(logonStatus); sl@0: User::SetJustInTime(ETrue); sl@0: test(thread.ExitType()==EExitTerminate); sl@0: test(logonStatus==999); sl@0: CLOSE_AND_WAIT(thread); sl@0: sl@0: test.Next(_L("Test Panic")); sl@0: thread.Create(TestThreadServerKill,(TAny*)CTestSession::ETestPanic); sl@0: thread.Logon(logonStatus); sl@0: thread.Resume(); sl@0: User::WaitForRequest(logonStatus); sl@0: User::SetJustInTime(ETrue); sl@0: test(thread.ExitType()==EExitPanic); sl@0: test(logonStatus==999); sl@0: CLOSE_AND_WAIT(thread); sl@0: sl@0: test.End(); sl@0: } sl@0: sl@0: sl@0: sl@0: void TestSetProcessPriority() sl@0: { sl@0: RProcess process; sl@0: TProcessPriority priority(process.Priority()); sl@0: TInt r; sl@0: sl@0: test.Start(_L("Try changing priority when Priority Control disabled")); sl@0: r = Session.Send(CTestSession::ETestSetProcessPriority,TIpcArgs(EPriorityBackground)); sl@0: test(r==KErrPermissionDenied); sl@0: test(process.Priority()==priority); sl@0: sl@0: r = Session.Send(CTestSession::ETestSetProcessPriority,TIpcArgs(EPriorityForeground)); sl@0: test(r==KErrPermissionDenied); sl@0: test(process.Priority()==priority); sl@0: sl@0: r = Session.Send(CTestSession::ETestSetProcessPriority,TIpcArgs(EPriorityLow)); sl@0: test(r==KErrPermissionDenied); sl@0: test(process.Priority()==priority); sl@0: sl@0: r = Session.Send(CTestSession::ETestSetProcessPriority,TIpcArgs(EPriorityHigh)); sl@0: test(r==KErrPermissionDenied); sl@0: test(process.Priority()==priority); sl@0: sl@0: test.Next(_L("Test changing priority when Priority Control enabled")); sl@0: User::SetPriorityControl(ETrue); sl@0: sl@0: r = Session.Send(CTestSession::ETestSetProcessPriority,TIpcArgs(EPriorityBackground)); sl@0: test(r==KErrNone); sl@0: test(process.Priority()==EPriorityBackground); sl@0: sl@0: r = Session.Send(CTestSession::ETestSetProcessPriority,TIpcArgs(EPriorityForeground)); sl@0: test(r==KErrNone); sl@0: test(process.Priority()==EPriorityForeground); sl@0: sl@0: r = Session.Send(CTestSession::ETestSetProcessPriority,TIpcArgs(EPriorityLow)); sl@0: test(r==KErrPermissionDenied); sl@0: test(process.Priority()==EPriorityForeground); sl@0: sl@0: r = Session.Send(CTestSession::ETestSetProcessPriority,TIpcArgs(EPriorityHigh)); sl@0: test(r==KErrPermissionDenied); sl@0: test(process.Priority()==EPriorityForeground); sl@0: sl@0: User::SetPriorityControl(EFalse); sl@0: sl@0: process.SetPriority(priority); sl@0: sl@0: test.End(); sl@0: } sl@0: sl@0: sl@0: sl@0: GLDEF_C TInt E32Main() sl@0: { sl@0: TBuf16<512> 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: 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: test.Start(_L("Starting test server")); sl@0: RTestProcess server; sl@0: TRequestStatus rendezvous; sl@0: TRequestStatus svrstat; sl@0: server.Create(ETestProcessServer); sl@0: server.NotifyDestruction(svrstat); sl@0: server.Rendezvous(rendezvous); sl@0: server.Resume(); sl@0: User::WaitForRequest(rendezvous); sl@0: test(rendezvous==KServerRendezvous); sl@0: server.Close(); sl@0: sl@0: test.Next(_L("Openning server session")); sl@0: TInt r = Session.Connect(); sl@0: test(r==KErrNone); sl@0: sl@0: test.Next(_L("Test IPC data transfer")); sl@0: TestIPC(); sl@0: sl@0: test.Next(_L("Test RMessagePtr2::Kill, Panic and Teminate")); sl@0: TestKill(); sl@0: sl@0: test.Next(_L("Test RMessagePtr2::SetProcessPriority")); sl@0: TestSetProcessPriority(); sl@0: sl@0: test.Next(_L("Test RMessagePtr2::Client")); sl@0: test(Session.Send(CTestSession::ETestClient)==(TInt)RThread().Id()); sl@0: sl@0: test.Next(_L("Test RMessagePtr2::RMessage2")); sl@0: test(Session.Send(CTestSession::ETestRMessage,TIpcArgs(111111,222222,333333,444444))==KErrNone); sl@0: sl@0: test.Next(_L("Test RMessagePtr2::Handle")); sl@0: test(Session.Send(CTestSession::ETestHandle,TIpcArgs())==KErrNone); sl@0: sl@0: test.Next(_L("Test RMessagePtr2::Session")); sl@0: test(Session.Send(CTestSession::ETestSession,TIpcArgs())==KErrNone); sl@0: sl@0: test.Next(_L("Stopping test server")); sl@0: Session.Send(CTestSession::EShutdown); sl@0: Session.Close(); sl@0: User::WaitForRequest(svrstat); sl@0: test(svrstat == KErrNone); sl@0: sl@0: test.End(); sl@0: return(0); sl@0: } sl@0: