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_sserver.cpp sl@0: // Overview: sl@0: // Test the security aspects of server connect by handle. sl@0: // API Information: sl@0: // CSession2, CServer2 sl@0: // Details: sl@0: // - Test asynchronous server connect in various ways. Verify results sl@0: // are as expected. sl@0: // - Attempt to create a protected server without and without sl@0: // KCapabilityProtServ. Verify results are as expected. sl@0: // - Attempt to connect to a serve with different Secure IDs, Vendor IDs sl@0: // and capabilities. Verify results are as expected. sl@0: // - Test creating a sub-session, sending it messages and closing it. sl@0: // Verify the results. sl@0: // - Test TIpcArgs::Set and IPC argument passing. Verify results are sl@0: // as expected. sl@0: // - Test TIpcArgs:: Also checks the integrity of the server and Kernel if client passes bad descriptor sl@0: // for IPC transfer and the integrity of the client and Kernel if the server does the same. sl@0: // - Test CServer2::RunError and CSession2::ServiceError and session sl@0: // resource counting. 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: #include "../misc/prbs.h" sl@0: sl@0: LOCAL_D RTest test(_L("T_SSERVER")); sl@0: sl@0: _LIT_SECURE_ID(KTestSecureId,0x101f534d); sl@0: _LIT_SECURE_ID(KTestSecureId2,0x101f534e); sl@0: _LIT_VENDOR_ID(KTestVendorId,0x01234567); sl@0: _LIT_VENDOR_ID(KTestVendorId2,0x01234568); sl@0: const TInt KTestRunErrorModifier = 111; sl@0: const TInt KTestServiceErrorModifier = 222; sl@0: _LIT(KProtectedServerName,"!T_SSERVER-protected-server"); sl@0: sl@0: sl@0: sl@0: enum TTestProcessFunctions sl@0: { sl@0: ETestProcessServer, sl@0: ETestProcessCreateProtectedServer, sl@0: }; sl@0: sl@0: #include "testprocess.h" sl@0: sl@0: sl@0: sl@0: // sl@0: // RTestThread 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: // sl@0: // CTestSession sl@0: // sl@0: sl@0: enum TTestSessionFuntions sl@0: { sl@0: ETestShutdown,EPing, sl@0: ETestArgUnspecified,ETestArgDesC8,ETestArgDesC16,ETestArgDes8,ETestArgDes16, sl@0: ETestResourceCountPass=0x01234567,ETestResourceCountFail,ETestServiceLeave, sl@0: ETestCreateSubSession,ETestCloseSubSession,ETestCloseSubSessionHandle, sl@0: ETestEchoArgs,ETestEmptySubSessionMessage, ETestBadClientDescRead, ETestBadClientDescWrite, sl@0: ETestBadServerDescRead, ETestBadServerDescWrite sl@0: }; sl@0: sl@0: class CTestSession : public CSession2 sl@0: { sl@0: public: sl@0: CTestSession(); sl@0: virtual void ServiceL(const RMessage2& aMessage); sl@0: virtual void ServiceError(const RMessage2& aMessage,TInt aError); sl@0: private: sl@0: TInt CountResources(); sl@0: private: sl@0: TInt iResourceCount; sl@0: TInt iSubSessionCloseHandle; sl@0: RMessage2 iMessage; sl@0: }; sl@0: sl@0: class CTestSecureServer : public CServer2 sl@0: { sl@0: public: sl@0: CTestSecureServer(TInt aPriority); sl@0: virtual CSession2* NewSessionL(const TVersion& aVersion, const RMessage2& aMessage) const; sl@0: private: sl@0: void CustomL(const RMessage2& aMessage); sl@0: void QueryL(const RMessage2& aMessage,HBufC8*& aQueryParams); sl@0: TInt RunError(TInt aError); sl@0: private: sl@0: friend class CTestSession; sl@0: }; sl@0: sl@0: CTestSession::CTestSession() sl@0: : CSession2() sl@0: {} sl@0: sl@0: void CTestSession::ServiceL(const RMessage2& aMessage) sl@0: { sl@0: TInt r = KErrNotSupported; sl@0: TBuf8<100> buf8; sl@0: buf8.Append(_L8("12345")); sl@0: TBuf16<100> buf16; sl@0: buf16.Append(_L16("12345")); sl@0: TInt testArg = ((TUint)aMessage.Function())>>28; sl@0: sl@0: TInt badDescriptorError = PlatSec::ConfigSetting(PlatSec::EPlatSecProcessIsolation)&&PlatSec::ConfigSetting(PlatSec::EPlatSecEnforcement) sl@0: ? KErrBadDescriptor : KErrNone; sl@0: sl@0: switch(aMessage.Function()&0x0fffffff) sl@0: { sl@0: case ETestShutdown: sl@0: CActiveScheduler::Stop(); sl@0: return; sl@0: sl@0: case EPing: sl@0: r=aMessage.Int0(); sl@0: break; sl@0: sl@0: case ETestArgUnspecified: sl@0: r = aMessage.GetDesLength(testArg); sl@0: if(r!=KErrBadDescriptor) sl@0: goto fail; sl@0: r = aMessage.GetDesMaxLength(testArg); sl@0: if(r!=KErrBadDescriptor) sl@0: goto fail; sl@0: r = aMessage.Read(testArg,buf8); sl@0: if(r!=badDescriptorError) sl@0: goto fail; sl@0: TRAP(r,aMessage.ReadL(testArg,buf8)); sl@0: if(r!=badDescriptorError) sl@0: goto fail; sl@0: r = aMessage.Read(testArg,buf16); sl@0: if(r!=badDescriptorError) sl@0: goto fail; sl@0: TRAP(r,aMessage.ReadL(testArg,buf16)); sl@0: if(r!=badDescriptorError) sl@0: goto fail; sl@0: r = aMessage.Write(testArg,buf8); sl@0: if(r!=badDescriptorError) sl@0: goto fail; sl@0: TRAP(r,aMessage.WriteL(testArg,buf8)); sl@0: if(r!=badDescriptorError) sl@0: goto fail; sl@0: r = aMessage.Write(testArg,buf16); sl@0: if(r!=badDescriptorError) sl@0: goto fail; sl@0: TRAP(r,aMessage.WriteL(testArg,buf16)); sl@0: if(r!=badDescriptorError) sl@0: goto fail; sl@0: goto pass; sl@0: sl@0: case ETestArgDesC8: sl@0: r = aMessage.GetDesLength(testArg); sl@0: if(r<0) sl@0: goto fail; sl@0: r = aMessage.GetDesMaxLength(testArg); sl@0: if(r<0) sl@0: goto fail; sl@0: r = aMessage.Read(testArg,buf8); sl@0: if(r!=KErrNone) sl@0: goto fail; sl@0: TRAP(r,aMessage.ReadL(testArg,buf8)); sl@0: if(r!=KErrNone) sl@0: goto fail; sl@0: r = aMessage.Read(testArg,buf16); sl@0: if(r!=badDescriptorError) sl@0: goto fail; sl@0: TRAP(r,aMessage.ReadL(testArg,buf16)); sl@0: if(r!=badDescriptorError) sl@0: goto fail; sl@0: r = aMessage.Write(testArg,buf8); sl@0: if(r!=badDescriptorError) sl@0: goto fail; sl@0: TRAP(r,aMessage.WriteL(testArg,buf8)); sl@0: if(r!=badDescriptorError) sl@0: goto fail; sl@0: r = aMessage.Write(testArg,buf16); sl@0: if(r!=badDescriptorError) sl@0: goto fail; sl@0: TRAP(r,aMessage.WriteL(testArg,buf16)); sl@0: if(r!=badDescriptorError) sl@0: goto fail; sl@0: goto pass; sl@0: sl@0: case ETestArgDesC16: sl@0: r = aMessage.GetDesLength(testArg); sl@0: if(r<0) sl@0: goto fail; sl@0: r = aMessage.GetDesMaxLength(testArg); sl@0: if(r<0) sl@0: goto fail; sl@0: r = aMessage.Read(testArg,buf8); sl@0: if(r!=badDescriptorError) sl@0: goto fail; sl@0: TRAP(r,aMessage.ReadL(testArg,buf8)); sl@0: if(r!=badDescriptorError) sl@0: goto fail; sl@0: r = aMessage.Read(testArg,buf16); sl@0: if(r!=KErrNone) sl@0: goto fail; sl@0: TRAP(r,aMessage.ReadL(testArg,buf16)); sl@0: if(r!=KErrNone) sl@0: goto fail; sl@0: r = aMessage.Write(testArg,buf8); sl@0: if(r!=badDescriptorError) sl@0: goto fail; sl@0: TRAP(r,aMessage.WriteL(testArg,buf8)); sl@0: if(r!=badDescriptorError) sl@0: goto fail; sl@0: r = aMessage.Write(testArg,buf16); sl@0: if(r!=badDescriptorError) sl@0: goto fail; sl@0: TRAP(r,aMessage.WriteL(testArg,buf16)); sl@0: if(r!=badDescriptorError) sl@0: goto fail; sl@0: goto pass; sl@0: sl@0: case ETestArgDes8: sl@0: r = aMessage.GetDesLength(testArg); sl@0: if(r<0) sl@0: goto fail; sl@0: r = aMessage.GetDesMaxLength(testArg); sl@0: if(r<0) sl@0: goto fail; sl@0: r = aMessage.Read(testArg,buf8); sl@0: if(r!=KErrNone) sl@0: goto fail; sl@0: TRAP(r,aMessage.ReadL(testArg,buf8)); sl@0: if(r!=KErrNone) sl@0: goto fail; sl@0: r = aMessage.Read(testArg,buf16); sl@0: if(r!=badDescriptorError) sl@0: goto fail; sl@0: TRAP(r,aMessage.ReadL(testArg,buf16)); sl@0: if(r!=badDescriptorError) sl@0: goto fail; sl@0: r = aMessage.Write(testArg,buf8); sl@0: if(r!=KErrNone) sl@0: goto fail; sl@0: TRAP(r,aMessage.WriteL(testArg,buf8)); sl@0: if(r!=KErrNone) sl@0: goto fail; sl@0: r = aMessage.Write(testArg,buf16); sl@0: if(r!=badDescriptorError) sl@0: goto fail; sl@0: TRAP(r,aMessage.WriteL(testArg,buf16)); sl@0: if(r!=badDescriptorError) sl@0: goto fail; sl@0: goto pass; sl@0: sl@0: case ETestArgDes16: sl@0: r = aMessage.GetDesLength(testArg); sl@0: if(r<0) sl@0: goto fail; sl@0: r = aMessage.GetDesMaxLength(testArg); sl@0: if(r<0) sl@0: goto fail; sl@0: r = aMessage.Read(testArg,buf8); sl@0: if(r!=badDescriptorError) sl@0: goto fail; sl@0: TRAP(r,aMessage.ReadL(testArg,buf8)); sl@0: if(r!=badDescriptorError) sl@0: goto fail; sl@0: r = aMessage.Read(testArg,buf16); sl@0: if(r!=KErrNone) sl@0: goto fail; sl@0: TRAP(r,aMessage.ReadL(testArg,buf16)); sl@0: if(r!=KErrNone) sl@0: goto fail; sl@0: r = aMessage.Write(testArg,buf8); sl@0: if(r!=badDescriptorError) sl@0: goto fail; sl@0: TRAP(r,aMessage.WriteL(testArg,buf8)); sl@0: if(r!=badDescriptorError) sl@0: goto fail; sl@0: r = aMessage.Write(testArg,buf16); sl@0: if(r!=KErrNone) sl@0: goto fail; sl@0: TRAP(r,aMessage.WriteL(testArg,buf16)); sl@0: if(r!=KErrNone) sl@0: goto fail; sl@0: goto pass; sl@0: sl@0: case ETestBadClientDescRead: sl@0: //Testing the integrity of the server and Kernel if the client passes bad descriptor for IPC transfer sl@0: { sl@0: r = aMessage.GetDesLength(testArg); sl@0: TUint8* buff; sl@0: buff = (TUint8*)User::Alloc(r); sl@0: if (!buff) sl@0: { sl@0: r=KErrNoMemory; sl@0: break; sl@0: } sl@0: TPtr8 ptr8(buff, r, r); sl@0: r=aMessage.Read(testArg,ptr8); sl@0: User::Free(buff); sl@0: } sl@0: break; sl@0: sl@0: case ETestBadClientDescWrite: sl@0: //Testing the integrity of the server and Kernel if the client passes bad descriptor for IPC transfer sl@0: { sl@0: r = aMessage.GetDesLength(testArg); sl@0: TUint8* buff; sl@0: buff = (TUint8*)User::Alloc(r); sl@0: if (!buff) sl@0: { sl@0: r=KErrNoMemory; sl@0: break; sl@0: } sl@0: TPtr8 ptr8(buff, r, r); sl@0: r=aMessage.Write(testArg,ptr8); sl@0: User::Free(buff); sl@0: } sl@0: break; sl@0: sl@0: case ETestBadServerDescRead: sl@0: case ETestBadServerDescWrite: sl@0: //Testing the integrity of the client and Kernel if server passes bad descriptor for IPC transfer sl@0: { sl@0: //Create a chunk with a hole between addresses 0x1000 and 0x2000 sl@0: RChunk c; sl@0: r=c.CreateDisconnectedLocal(0,0,0x200000); sl@0: test(r==KErrNone); sl@0: r=c.Commit(0,0x1000); sl@0: test(r==KErrNone); sl@0: r=c.Commit(0x2000,0x1000); sl@0: test(r==KErrNone); sl@0: sl@0: TInt base,len; sl@0: switch(aMessage.Function()>>28) sl@0: { sl@0: case 0:base=0x1000;len=0x500;break; sl@0: case 1:base=0x1001;len=0x500;break; sl@0: case 2:base=0x1007;len=0x500;break; sl@0: case 3:base=0x1ff0;len=0x100;break; sl@0: case 4:base=0x1ff1;len=0x100;break; sl@0: case 5:base=0x1ff2;len=0x100;break; sl@0: case 6:base=0xff3;len=0x100;break; sl@0: default:base=0xfff;len=0x100;break; sl@0: } sl@0: sl@0: TPtr8 ptr (c.Base()+base,len,len); sl@0: if ((aMessage.Function()&0x0fffffff) == ETestBadServerDescRead) sl@0: aMessage.Read(0,ptr); //The server should panic here sl@0: else sl@0: aMessage.Write(0,ptr); //The server should panic here sl@0: } sl@0: break; sl@0: sl@0: case ETestResourceCountPass: sl@0: r=aMessage.Function(); sl@0: ResourceCountMarkStart(); sl@0: ResourceCountMarkEnd(aMessage); sl@0: break; sl@0: sl@0: case ETestResourceCountFail: sl@0: r=aMessage.Function(); sl@0: ResourceCountMarkStart(); sl@0: ++iResourceCount; sl@0: ResourceCountMarkEnd(aMessage); sl@0: break; sl@0: sl@0: case ETestServiceLeave: sl@0: iMessage = aMessage; sl@0: User::Leave(aMessage.Int0()); sl@0: break; sl@0: sl@0: case ETestCreateSubSession: sl@0: { sl@0: TInt reply = aMessage.Int0(); sl@0: r = aMessage.Write(3,TPtrC8((TUint8*)&reply,sizeof(reply))); sl@0: } sl@0: break; sl@0: sl@0: case ETestCloseSubSession: sl@0: iSubSessionCloseHandle = aMessage.Int3(); sl@0: r = KErrNone; sl@0: break; sl@0: sl@0: case ETestCloseSubSessionHandle: sl@0: r = iSubSessionCloseHandle; sl@0: iSubSessionCloseHandle = 0; sl@0: break; sl@0: sl@0: case ETestEchoArgs: sl@0: { sl@0: TInt reply[4]; sl@0: reply[0] = aMessage.Int0(); sl@0: reply[1] = aMessage.Int1(); sl@0: reply[2] = aMessage.Int2(); sl@0: reply[3] = aMessage.Int3(); sl@0: r = aMessage.Write(0,TPtrC8((TUint8*)&reply,sizeof(reply))); sl@0: } sl@0: break; sl@0: sl@0: case ETestEmptySubSessionMessage: sl@0: r = aMessage.Int3(); sl@0: break; sl@0: sl@0: default: sl@0: break; sl@0: } sl@0: if(aMessage.Handle()) sl@0: aMessage.Complete(r); sl@0: return; sl@0: fail: sl@0: aMessage.Complete(KErrGeneral); sl@0: return; sl@0: pass: sl@0: aMessage.Complete(KErrNone); sl@0: return; sl@0: } sl@0: sl@0: TInt CTestSession::CountResources() sl@0: { sl@0: return iResourceCount; sl@0: } sl@0: sl@0: void CTestSession::ServiceError(const RMessage2& aMessage,TInt aError) sl@0: { sl@0: if(aMessage!=iMessage) sl@0: aError = KErrGeneral; // We got given the right message sl@0: else sl@0: aError += KTestServiceErrorModifier; // Let test harnes know we came through this routine sl@0: CSession2::ServiceError(aMessage,aError); sl@0: } sl@0: sl@0: // sl@0: // CTestSecureServer sl@0: // sl@0: sl@0: CTestSecureServer::CTestSecureServer(TInt aPriority) sl@0: : CServer2(aPriority,EGlobalSharableSessions) sl@0: { sl@0: } sl@0: sl@0: CSession2* CTestSecureServer::NewSessionL(const TVersion& aVersion, const RMessage2& /*aMessage*/) const sl@0: { sl@0: if(*(TInt*)&aVersion) sl@0: User::Leave(KErrNotSupported); // Only accept version 0.0.00 sl@0: return new (ELeave) CTestSession(); sl@0: } sl@0: sl@0: TInt CTestSecureServer::RunError(TInt aError) sl@0: { sl@0: return CServer2::RunError(aError+KTestRunErrorModifier); // Let test harnes know we came through this routine 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_SSERVER-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: CTestSecureServer* server = new (ELeave) CTestSecureServer(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: { sl@0: TInt r=CreateSession(KServerName,TVersion()); sl@0: if(r) return r; sl@0: return ShareAuto(); sl@0: } sl@0: inline TInt Connect(const TSecurityPolicy* aPolicy) sl@0: { sl@0: return CreateSession(KServerName,TVersion(),-1,EIpcSession_Unsharable,aPolicy,0); sl@0: } sl@0: inline TInt Connect(TVersion aVersion,TRequestStatus* aStatus) sl@0: { sl@0: return CreateSession(KServerName,aVersion,-1,EIpcSession_Unsharable,0,aStatus); sl@0: } 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: // sl@0: // RTestSubSession sl@0: // sl@0: sl@0: class RTestSubSession : public RSubSessionBase sl@0: { sl@0: public: sl@0: inline TInt CreateSubSession(RSessionBase& aSession,TInt aFunction,const TIpcArgs& aArgs) sl@0: { return RSubSessionBase::CreateSubSession(aSession,aFunction,aArgs); } sl@0: inline TInt CreateSubSession(RSessionBase& aSession,TInt aFunction) sl@0: { return RSubSessionBase::CreateSubSession(aSession,aFunction); } sl@0: inline void CloseSubSession(TInt aFunction) sl@0: { RSubSessionBase::CloseSubSession(aFunction); } sl@0: inline TInt Send(TInt aFunction) sl@0: { return RSubSessionBase::SendReceive(aFunction); } sl@0: inline TInt Send(TInt aFunction,const TIpcArgs& aArgs) sl@0: { return RSubSessionBase::SendReceive(aFunction,aArgs); } sl@0: inline void Send(TInt aFunction,TRequestStatus& aStatus) sl@0: { RSubSessionBase::SendReceive(aFunction,aStatus); } sl@0: inline void Send(TInt aFunction,const TIpcArgs& aArgs,TRequestStatus& aStatus) sl@0: { RSubSessionBase::SendReceive(aFunction,aArgs,aStatus); } sl@0: }; sl@0: sl@0: sl@0: sl@0: RTestSession Session; sl@0: TInt TestParam = 0; sl@0: sl@0: TInt TestThreadFunction(TAny* aParam) sl@0: { sl@0: return Session.Send((TInt)aParam,TIpcArgs(TestParam)); sl@0: } sl@0: sl@0: void DoTest(TInt aFunction,TExitType aExitType,TInt aExitReason,TInt aParam=0) sl@0: { sl@0: TBuf<256> title; sl@0: title.AppendFormat(_L("Function %d"),aFunction); sl@0: test.Next(title); sl@0: sl@0: RTestThread thread; sl@0: thread.Create(TestThreadFunction,(TAny*)aFunction); sl@0: TRequestStatus logon; sl@0: thread.Logon(logon); sl@0: TestParam = aParam; sl@0: sl@0: User::SetJustInTime(EFalse); sl@0: thread.Resume(); sl@0: User::WaitForRequest(logon); sl@0: User::SetJustInTime(ETrue); sl@0: sl@0: TExitType exitType=thread.ExitType(); sl@0: TInt exitReason=thread.ExitReason(); sl@0: test(exitType==aExitType); sl@0: test(exitReason==aExitReason); sl@0: CLOSE_AND_WAIT(thread); sl@0: } sl@0: sl@0: // sl@0: // Anonymous server sl@0: // sl@0: sl@0: class RTestAnonymousSession : public RTestSession sl@0: { sl@0: public: sl@0: inline TInt Connect(RServer2 aServer) sl@0: { sl@0: TInt r=CreateSession(aServer,TVersion()); sl@0: if(r) return r; sl@0: return ShareProtected(); sl@0: } sl@0: inline TInt Connect() sl@0: { sl@0: return CreateSession(_L(""),TVersion()); sl@0: } sl@0: }; sl@0: sl@0: CTestSecureServer* AnonymousServer; sl@0: sl@0: void DoStartAnonymousServer() sl@0: { sl@0: CTestActiveScheduler* activeScheduler = new (ELeave) CTestActiveScheduler; sl@0: CActiveScheduler::Install(activeScheduler); sl@0: CleanupStack::PushL(activeScheduler); sl@0: sl@0: CTestSecureServer* server = new (ELeave) CTestSecureServer(0); sl@0: CleanupStack::PushL(server); sl@0: sl@0: User::LeaveIfError(server->Start(KNullDesC)); sl@0: sl@0: AnonymousServer = server; sl@0: RThread::Rendezvous(KServerRendezvous); sl@0: sl@0: CActiveScheduler::Start(); sl@0: sl@0: CleanupStack::PopAndDestroy(2); sl@0: } sl@0: sl@0: TInt StartAnonymousServer(TAny* /*aPtr*/) sl@0: { sl@0: CTrapCleanup* cleanupStack = CTrapCleanup::New(); sl@0: if(!cleanupStack) sl@0: return KErrNoMemory; sl@0: TRAPD(leaveError,DoStartAnonymousServer()) sl@0: delete cleanupStack; sl@0: return leaveError; sl@0: } sl@0: sl@0: void TestConnectByHandle() sl@0: { sl@0: RTestAnonymousSession session; sl@0: TRequestStatus logon; sl@0: TInt r; sl@0: sl@0: test.Start(_L("Starting a local server")); sl@0: RTestThread thread; sl@0: thread.Create(StartAnonymousServer); sl@0: TRequestStatus rendezvous; sl@0: TRequestStatus svrstat; sl@0: thread.Rendezvous(rendezvous); sl@0: thread.Logon(logon); sl@0: thread.NotifyDestruction(svrstat); sl@0: thread.Resume(); sl@0: User::WaitForRequest(rendezvous); sl@0: test(rendezvous.Int()==KServerRendezvous); sl@0: thread.Close(); sl@0: sl@0: test.Next(_L("Check connect with null name fails")); sl@0: test((r=session.Connect())!=KErrNone); sl@0: sl@0: test.Next(_L("Connecting to local server")); sl@0: test((r=session.Connect(AnonymousServer->Server()))==KErrNone); sl@0: sl@0: test.Next(_L("Test the connection")); sl@0: test((r=session.Send(EPing,TIpcArgs(1234)))==1234); sl@0: test((r=session.Send(EPing,TIpcArgs(2345)))==2345); sl@0: sl@0: test.Next(_L("Shutting server down")); sl@0: test(logon.Int()==KRequestPending); sl@0: test((r=session.Send(ETestShutdown))==KErrServerTerminated); sl@0: test(r==KErrServerTerminated); sl@0: session.Close(); sl@0: User::WaitForRequest(logon); sl@0: test(logon.Int()==KErrNone); sl@0: User::WaitForRequest(svrstat); sl@0: sl@0: test.End(); sl@0: } sl@0: sl@0: TRequestStatus SvrStat; sl@0: sl@0: void RestartTestServer() sl@0: { sl@0: RTestProcess server; sl@0: TRequestStatus rendezvous; sl@0: server.Create(0,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: test(Session.Connect()==KErrNone); sl@0: } sl@0: sl@0: void TestIpc() sl@0: { sl@0: TBuf8<10> buf8((TUint8*)"abcdefghij"); sl@0: TBuf16<10> buf16((TUint16*)L"abcdefghij"); sl@0: TInt r; sl@0: sl@0: test.Start(_L("Testing TIpcArgs::Set")); sl@0: { sl@0: TIpcArgs a(TIpcArgs::ENothing,123,(TAny*)&buf8); sl@0: TIpcArgs b; sl@0: b.Set(0,TIpcArgs::ENothing); sl@0: b.Set(1,123); sl@0: b.Set(2,(TAny*)&buf8); sl@0: test(a.iFlags==b.iFlags); sl@0: // iArgs[0] is uninitialised so don't test sl@0: test(a.iArgs[1]==b.iArgs[1]); sl@0: test(a.iArgs[2]==b.iArgs[2]); sl@0: } sl@0: { sl@0: TIpcArgs a((TDesC8*)&buf8,(TDesC16*)&buf16,&buf8,&buf16); sl@0: TIpcArgs b; sl@0: b.Set(0,(TDesC8*)&buf8); sl@0: b.Set(1,(TDesC16*)&buf16); sl@0: b.Set(2,&buf8); sl@0: b.Set(3,&buf16); sl@0: test(a.iFlags==b.iFlags); sl@0: test(a.iArgs[0]==b.iArgs[0]); sl@0: test(a.iArgs[1]==b.iArgs[1]); sl@0: test(a.iArgs[2]==b.iArgs[2]); sl@0: test(a.iArgs[3]==b.iArgs[3]); sl@0: } sl@0: sl@0: test.Next(_L("Test Unspecified argument")); sl@0: r = Session.Send(ETestArgUnspecified,TIpcArgs((TAny*)&buf8)); sl@0: test(r==KErrNone); sl@0: r = Session.Send(ETestArgUnspecified+(1<<28),TIpcArgs(0,(TAny*)&buf8)); sl@0: test(r==KErrNone); sl@0: r = Session.Send(ETestArgUnspecified+(2<<28),TIpcArgs(0,0,(TAny*)&buf8)); sl@0: test(r==KErrNone); sl@0: r = Session.Send(ETestArgUnspecified+(3<<28),TIpcArgs(0,0,0,(TAny*)&buf8)); sl@0: test(r==KErrNone); sl@0: sl@0: test.Next(_L("Test DesC8 argument")); sl@0: r = Session.Send(ETestArgDesC8,TIpcArgs((TDesC8*)&buf8)); sl@0: test(r==KErrNone); sl@0: r = Session.Send(ETestArgDesC8+(1<<28),TIpcArgs(0,(TDesC8*)&buf8)); sl@0: test(r==KErrNone); sl@0: r = Session.Send(ETestArgDesC8+(2<<28),TIpcArgs(0,0,(TDesC8*)&buf8)); sl@0: test(r==KErrNone); sl@0: r = Session.Send(ETestArgDesC8+(3<<28),TIpcArgs(0,0,0,(TDesC8*)&buf8)); sl@0: test(r==KErrNone); sl@0: sl@0: test.Next(_L("Test DesC16 argument")); sl@0: r = Session.Send(ETestArgDesC16,TIpcArgs((TDesC16*)&buf16)); sl@0: test(r==KErrNone); sl@0: r = Session.Send(ETestArgDesC16+(1<<28),TIpcArgs(0,(TDesC16*)&buf16)); sl@0: test(r==KErrNone); sl@0: r = Session.Send(ETestArgDesC16+(2<<28),TIpcArgs(0,0,(TDesC16*)&buf16)); sl@0: test(r==KErrNone); sl@0: r = Session.Send(ETestArgDesC16+(3<<28),TIpcArgs(0,0,0,(TDesC16*)&buf16)); sl@0: test(r==KErrNone); sl@0: sl@0: test.Next(_L("Test Des8 argument")); sl@0: r = Session.Send(ETestArgDes8,TIpcArgs(&buf8)); sl@0: test(r==KErrNone); sl@0: r = Session.Send(ETestArgDes8+(1<<28),TIpcArgs(0,&buf8)); sl@0: test(r==KErrNone); sl@0: r = Session.Send(ETestArgDes8+(2<<28),TIpcArgs(0,0,&buf8)); sl@0: test(r==KErrNone); sl@0: r = Session.Send(ETestArgDes8+(3<<28),TIpcArgs(0,0,0,&buf8)); sl@0: test(r==KErrNone); sl@0: sl@0: test.Next(_L("Test Des16 argument")); sl@0: r = Session.Send(ETestArgDes16,TIpcArgs(&buf16)); sl@0: test(r==KErrNone); sl@0: r = Session.Send(ETestArgDes16+(1<<28),TIpcArgs(0,&buf16)); sl@0: test(r==KErrNone); sl@0: r = Session.Send(ETestArgDes16+(2<<28),TIpcArgs(0,0,&buf16)); sl@0: test(r==KErrNone); sl@0: r = Session.Send(ETestArgDes16+(3<<28),TIpcArgs(0,0,0,&buf16)); sl@0: test(r==KErrNone); sl@0: sl@0: sl@0: test.Next(_L("Test Bad Client Descriptor")); sl@0: //The test should ensure that both server and kernel are safe if client passes faulty descriptor to the server. sl@0: { sl@0: //Create a chunk with a hole between addresses 0x1000 & 0x2000 sl@0: RChunk c; sl@0: r=c.CreateDisconnectedLocal(0,0,0x200000); sl@0: test(r==KErrNone); sl@0: r=c.Commit(0,0x1000); sl@0: test(r==KErrNone); sl@0: r=c.Commit(0x2000,0x1000); sl@0: test(r==KErrNone); sl@0: sl@0: //Each of these steps will pass bad descriptor to the server for IPC transfer in both directions. sl@0: //KErrBadDescriptor should be returned. sl@0: { sl@0: TPtr8 ptr (c.Base()+0x1000,0x100,0x100); sl@0: r = Session.Send(ETestBadClientDescRead,TIpcArgs(&ptr)); sl@0: test(KErrBadDescriptor == r); sl@0: r = Session.Send(ETestBadClientDescWrite,TIpcArgs(&ptr)); sl@0: test(KErrBadDescriptor == r); sl@0: } sl@0: { sl@0: TPtr8 ptr (c.Base()+0x1003,0x100,0x100); sl@0: r = Session.Send(ETestBadClientDescRead,TIpcArgs(&ptr)); sl@0: test(KErrBadDescriptor == r); sl@0: r = Session.Send(ETestBadClientDescWrite,TIpcArgs(&ptr)); sl@0: test(KErrBadDescriptor == r); sl@0: } sl@0: { sl@0: TPtr8 ptr (c.Base()+0xe00,0x500,0x500); sl@0: r = Session.Send(ETestBadClientDescRead,TIpcArgs(&ptr)); sl@0: test(KErrBadDescriptor == r); sl@0: r = Session.Send(ETestBadClientDescWrite,TIpcArgs(&ptr)); sl@0: test(KErrBadDescriptor == r); sl@0: } sl@0: { sl@0: TPtr8 ptr (c.Base()+0xdff,0x500,0x500); sl@0: r = Session.Send(ETestBadClientDescRead,TIpcArgs(&ptr)); sl@0: test(KErrBadDescriptor == r); sl@0: r = Session.Send(ETestBadClientDescWrite,TIpcArgs(&ptr)); sl@0: test(KErrBadDescriptor == r); sl@0: } sl@0: { sl@0: TPtr8 ptr (c.Base()+0x1ff1,0x500,0x500); sl@0: r = Session.Send(ETestBadClientDescRead,TIpcArgs(&ptr)); sl@0: test(KErrBadDescriptor == r); sl@0: r = Session.Send(ETestBadClientDescWrite,TIpcArgs(&ptr)); sl@0: test(KErrBadDescriptor == r); sl@0: } sl@0: sl@0: //The next step will send random descriptors for IPC transfer. Server should return either KErrNone or sl@0: //KErrBadDescriptor sl@0: { sl@0: TUint seed[2]; sl@0: seed[0]=User::TickCount(); sl@0: seed[1]=0; sl@0: test.Printf(_L("The initial seed for the random function is: S0=%xh S1=%xh\n"), seed[0], seed[1]); sl@0: sl@0: TInt i; sl@0: TPtr8 ptr (0,0,0); sl@0: TInt noErrors = 0; sl@0: TInt badDescriptors = 0; sl@0: sl@0: for (i=0;i<100;i++) sl@0: { sl@0: TInt descAddress = (TInt) (c.Base() + (Random(seed) % 0x1000)); sl@0: TInt descSize=Random(seed)%0x1000; sl@0: sl@0: TUint* descData = (TUint*)&ptr; sl@0: descData[0] = 0x20000000 + descSize; sl@0: descData[1] = descSize; sl@0: descData[2] = descAddress; sl@0: sl@0: r = Session.Send(ETestBadClientDescRead,TIpcArgs(&ptr)); sl@0: switch (r) sl@0: { sl@0: case KErrNone: noErrors++;break; sl@0: case KErrBadDescriptor: badDescriptors++;break; sl@0: default: test.Printf(_L("Error: %d returned"),r); sl@0: } sl@0: sl@0: r = Session.Send(ETestBadClientDescWrite,TIpcArgs(&ptr)); sl@0: switch (r) sl@0: { sl@0: case KErrNone: noErrors++;break; sl@0: case KErrBadDescriptor: badDescriptors++;break; sl@0: default: test.Printf(_L("Error: %d returned"),r); sl@0: } sl@0: } sl@0: test.Printf(_L("KErrNoError: %d KErrBadDescriptor: %d"),noErrors, badDescriptors); sl@0: } sl@0: sl@0: test.Next(_L("Test Bad Server Descriptor")); sl@0: //The test should ensure that kernel is safe if server passes faulty descriptor for IPC transfer sl@0: { sl@0: TPtr8 ptr (c.Base(),0x1000,0x1000); sl@0: TInt i; sl@0: for (i=0;i<=7;i++) sl@0: { sl@0: r = Session.Send(ETestBadServerDescRead | (i<<28),TIpcArgs(&ptr)); sl@0: test(r==KErrServerTerminated); sl@0: RestartTestServer(); sl@0: r = Session.Send(ETestBadServerDescWrite | (i<<28),TIpcArgs(&ptr)); sl@0: test(r==KErrServerTerminated); sl@0: User::WaitForRequest(SvrStat); sl@0: RestartTestServer(); sl@0: } sl@0: } sl@0: sl@0: c.Close(); sl@0: } sl@0: test.End(); sl@0: } sl@0: sl@0: sl@0: sl@0: void TestSubSessions() sl@0: { sl@0: const TInt KSubSessionHandle = 0x87654321; sl@0: RTestSubSession sub; sl@0: TInt r; sl@0: sl@0: test.Start(_L("Creating a subsession with no arguments")); sl@0: r = sub.CreateSubSession(Session,ETestCreateSubSession); sl@0: test(r==KErrNone); sl@0: test(((TInt*)&sub)[0] == (*(TInt*)&Session | CObjectIx::ENoClose)); // check sub.iSession sl@0: sl@0: test.Next(_L("Creating a subsession with arguments")); sl@0: r = sub.CreateSubSession(Session,ETestCreateSubSession,TIpcArgs(KSubSessionHandle)); sl@0: test(r==KErrNone); sl@0: test(((TInt*)&sub)[0] == (*(TInt*)&Session | CObjectIx::ENoClose)); // check sub.iSession sl@0: sl@0: test(((TInt*)&sub)[1]==KSubSessionHandle); // check sub.iSubSessionHandle sl@0: sl@0: test.Next(_L("Sending message with arguments")); sl@0: TInt reply[4] = {0}; sl@0: TPtr8 replyDes((TUint8*)&reply,sizeof(reply)); sl@0: r = sub.Send(ETestEchoArgs,TIpcArgs(&replyDes,123,456,789)); sl@0: test(r==KErrNone); sl@0: test(reply[0]==(TInt)&replyDes); sl@0: test(reply[1]==123); sl@0: test(reply[2]==456); sl@0: test(reply[3]==KSubSessionHandle); sl@0: sl@0: test.Next(_L("Sending empty message")); sl@0: r = sub.Send(ETestEmptySubSessionMessage); sl@0: test(r==KSubSessionHandle); sl@0: sl@0: test.Next(_L("Closing subsession")); sl@0: sub.CloseSubSession(ETestCloseSubSession); sl@0: test(((TInt*)&sub)[0]==0); // check sub.iSession sl@0: test(((TInt*)&sub)[1]==0); // check sub.iSubSessionHandle sl@0: r = Session.Send(ETestCloseSubSessionHandle); sl@0: test(r==KSubSessionHandle); sl@0: sl@0: test.End(); sl@0: } sl@0: sl@0: sl@0: void TestProtectedServers() sl@0: { sl@0: RTestProcess process; sl@0: TRequestStatus logonStatus; sl@0: sl@0: test.Start(_L("Trying to create a protected server without KCapabilityProtServ")); sl@0: process.Create(~(1u< sl@0: sl@0: TInt TestThreadAsyncConnect(TAny*) sl@0: { sl@0: RTestSession session; sl@0: TRequestStatus status; sl@0: TInt r=session.Connect(TVersion(1,1,1),&status); sl@0: if(r!=KErrNone) sl@0: return r; sl@0: if(status!=KRequestPending) // server can't have created session yet because we have higher priority sl@0: return status.Int(); sl@0: User::WaitForRequest(status); sl@0: if(status!=KErrNotSupported) sl@0: return status.Int(); sl@0: return session.Send(EPing,TIpcArgs(1234)); // Should panic us sl@0: } sl@0: sl@0: void TestAsynchronousConnect() sl@0: { sl@0: RTestSession session; sl@0: TRequestStatus status; sl@0: TInt r; sl@0: sl@0: test.Start(_L("Test successful asynchronous connect")); sl@0: r=session.Connect(TVersion(0,0,0),&status); sl@0: test(r==KErrNone); sl@0: test(status==KRequestPending); // server can't have created session yet because we have higher priority sl@0: User::WaitForRequest(status); sl@0: test(status==KErrNone); sl@0: test((r=session.Send(EPing,TIpcArgs(1234)))==1234); sl@0: session.Close(); sl@0: sl@0: test.Next(_L("Test unsuccessful asynchronous connect")); sl@0: r=session.Connect(TVersion(1,1,1),&status); sl@0: test(r==KErrNone); sl@0: test(status==KRequestPending); // server can't have created session yet because we have higher priority sl@0: User::WaitForRequest(status); sl@0: test(status==KErrNotSupported); sl@0: session.Close(); sl@0: sl@0: test.Next(_L("Test using unsuccessful asynchronous connect")); sl@0: RTestThread thread; sl@0: thread.Create(TestThreadAsyncConnect,0); sl@0: TRequestStatus logon; sl@0: thread.Logon(logon); sl@0: User::SetJustInTime(EFalse); sl@0: thread.Resume(); sl@0: User::WaitForRequest(logon); sl@0: User::SetJustInTime(ETrue); sl@0: TExitType exitType=thread.ExitType(); sl@0: TInt exitReason=thread.ExitReason(); sl@0: test(exitType==EExitPanic); sl@0: test(exitReason==CServer2::ESessionNotConnected); sl@0: thread.Close(); sl@0: sl@0: test.End(); sl@0: } sl@0: sl@0: sl@0: sl@0: TInt DoTestProcess(TInt aTestNum,TInt aArg1,TInt aArg2) sl@0: { sl@0: (void)aArg1; sl@0: (void)aArg2; sl@0: switch(aTestNum) sl@0: { sl@0: sl@0: case ETestProcessServer: sl@0: RProcess().SetPriority(EPriorityLow); sl@0: return StartServer(); sl@0: sl@0: case ETestProcessCreateProtectedServer: sl@0: { sl@0: test.Title(); sl@0: RServer2 server; sl@0: TInt r = server.CreateGlobal(KProtectedServerName); sl@0: server.Close(); sl@0: return r; sl@0: } sl@0: sl@0: default: sl@0: User::Panic(_L("T_SSERVER"),1); sl@0: } sl@0: sl@0: return KErrNone; 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: 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: TInt r; sl@0: sl@0: test.Title(); sl@0: sl@0: test.Start(_L("Testing Server Connect by Handle")); sl@0: TestConnectByHandle(); sl@0: sl@0: test.Next(_L("Starting test server")); sl@0: RTestProcess server; sl@0: TRequestStatus rendezvous; sl@0: server.Create(0,ETestProcessServer); 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("Conecting to test server")); sl@0: test((r=Session.Connect())==KErrNone); sl@0: sl@0: test.Next(_L("Test asynchronous server connect")); sl@0: TestAsynchronousConnect(); sl@0: sl@0: test.Next(_L("Test protected servers")); sl@0: TestProtectedServers(); sl@0: sl@0: test.Next(_L("Test connecting to identified servers")); sl@0: TestIdentifiedServers(); sl@0: sl@0: test.Next(_L("Testing SubSessions")); sl@0: TestSubSessions(); sl@0: sl@0: test.Next(_L("Testing IPC argument checking")); sl@0: TestIpc(); sl@0: sl@0: test.Next(_L("Testing CServer2::RunError and CSession2::ServiceError()")); sl@0: const TInt KTestServiceLeaveValue = 555; sl@0: r = Session.Send(ETestServiceLeave,TIpcArgs(KTestServiceLeaveValue)); sl@0: test(r==KTestServiceLeaveValue+KTestRunErrorModifier+KTestServiceErrorModifier); sl@0: sl@0: test.Next(_L("Testing session resource counting")); sl@0: test.Start(_L("")); sl@0: DoTest(ETestResourceCountPass,EExitKill,ETestResourceCountPass); sl@0: DoTest(ETestResourceCountFail,EExitPanic,CSession2::ESesFoundResCountHeaven); sl@0: test.End(); sl@0: sl@0: test.Next(_L("Shutting server down")); sl@0: { sl@0: Session.Send(ETestShutdown,TIpcArgs()); sl@0: Session.Close(); sl@0: } sl@0: User::WaitForRequest(SvrStat); sl@0: sl@0: test.End(); sl@0: return(0); sl@0: } sl@0: