Update contrib.
1 // Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\secure\t_sserver.cpp
16 // Test the security aspects of server connect by handle.
18 // CSession2, CServer2
20 // - Test asynchronous server connect in various ways. Verify results
22 // - Attempt to create a protected server without and without
23 // KCapabilityProtServ. Verify results are as expected.
24 // - Attempt to connect to a serve with different Secure IDs, Vendor IDs
25 // and capabilities. Verify results are as expected.
26 // - Test creating a sub-session, sending it messages and closing it.
27 // Verify the results.
28 // - Test TIpcArgs::Set and IPC argument passing. Verify results are
30 // - Test TIpcArgs:: Also checks the integrity of the server and Kernel if client passes bad descriptor
31 // for IPC transfer and the integrity of the client and Kernel if the server does the same.
32 // - Test CServer2::RunError and CSession2::ServiceError and session
34 // Platforms/Drives/Compatibility:
36 // Assumptions/Requirement/Pre-requisites:
37 // Failures and causes:
38 // Base Port information:
43 #include "../misc/prbs.h"
45 LOCAL_D RTest test(_L("T_SSERVER"));
47 _LIT_SECURE_ID(KTestSecureId,0x101f534d);
48 _LIT_SECURE_ID(KTestSecureId2,0x101f534e);
49 _LIT_VENDOR_ID(KTestVendorId,0x01234567);
50 _LIT_VENDOR_ID(KTestVendorId2,0x01234568);
51 const TInt KTestRunErrorModifier = 111;
52 const TInt KTestServiceErrorModifier = 222;
53 _LIT(KProtectedServerName,"!T_SSERVER-protected-server");
57 enum TTestProcessFunctions
60 ETestProcessCreateProtectedServer,
63 #include "testprocess.h"
71 class RTestThread : public RThread
74 void Create(TThreadFunction aFunction,TAny* aArg=0);
77 void RTestThread::Create(TThreadFunction aFunction,TAny* aArg)
79 TInt r=RThread::Create(_L(""),aFunction,KDefaultStackSize,KDefaultStackSize,KDefaultStackSize,aArg);
89 enum TTestSessionFuntions
92 ETestArgUnspecified,ETestArgDesC8,ETestArgDesC16,ETestArgDes8,ETestArgDes16,
93 ETestResourceCountPass=0x01234567,ETestResourceCountFail,ETestServiceLeave,
94 ETestCreateSubSession,ETestCloseSubSession,ETestCloseSubSessionHandle,
95 ETestEchoArgs,ETestEmptySubSessionMessage, ETestBadClientDescRead, ETestBadClientDescWrite,
96 ETestBadServerDescRead, ETestBadServerDescWrite
99 class CTestSession : public CSession2
103 virtual void ServiceL(const RMessage2& aMessage);
104 virtual void ServiceError(const RMessage2& aMessage,TInt aError);
106 TInt CountResources();
109 TInt iSubSessionCloseHandle;
113 class CTestSecureServer : public CServer2
116 CTestSecureServer(TInt aPriority);
117 virtual CSession2* NewSessionL(const TVersion& aVersion, const RMessage2& aMessage) const;
119 void CustomL(const RMessage2& aMessage);
120 void QueryL(const RMessage2& aMessage,HBufC8*& aQueryParams);
121 TInt RunError(TInt aError);
123 friend class CTestSession;
126 CTestSession::CTestSession()
130 void CTestSession::ServiceL(const RMessage2& aMessage)
132 TInt r = KErrNotSupported;
134 buf8.Append(_L8("12345"));
136 buf16.Append(_L16("12345"));
137 TInt testArg = ((TUint)aMessage.Function())>>28;
139 TInt badDescriptorError = PlatSec::ConfigSetting(PlatSec::EPlatSecProcessIsolation)&&PlatSec::ConfigSetting(PlatSec::EPlatSecEnforcement)
140 ? KErrBadDescriptor : KErrNone;
142 switch(aMessage.Function()&0x0fffffff)
145 CActiveScheduler::Stop();
152 case ETestArgUnspecified:
153 r = aMessage.GetDesLength(testArg);
154 if(r!=KErrBadDescriptor)
156 r = aMessage.GetDesMaxLength(testArg);
157 if(r!=KErrBadDescriptor)
159 r = aMessage.Read(testArg,buf8);
160 if(r!=badDescriptorError)
162 TRAP(r,aMessage.ReadL(testArg,buf8));
163 if(r!=badDescriptorError)
165 r = aMessage.Read(testArg,buf16);
166 if(r!=badDescriptorError)
168 TRAP(r,aMessage.ReadL(testArg,buf16));
169 if(r!=badDescriptorError)
171 r = aMessage.Write(testArg,buf8);
172 if(r!=badDescriptorError)
174 TRAP(r,aMessage.WriteL(testArg,buf8));
175 if(r!=badDescriptorError)
177 r = aMessage.Write(testArg,buf16);
178 if(r!=badDescriptorError)
180 TRAP(r,aMessage.WriteL(testArg,buf16));
181 if(r!=badDescriptorError)
186 r = aMessage.GetDesLength(testArg);
189 r = aMessage.GetDesMaxLength(testArg);
192 r = aMessage.Read(testArg,buf8);
195 TRAP(r,aMessage.ReadL(testArg,buf8));
198 r = aMessage.Read(testArg,buf16);
199 if(r!=badDescriptorError)
201 TRAP(r,aMessage.ReadL(testArg,buf16));
202 if(r!=badDescriptorError)
204 r = aMessage.Write(testArg,buf8);
205 if(r!=badDescriptorError)
207 TRAP(r,aMessage.WriteL(testArg,buf8));
208 if(r!=badDescriptorError)
210 r = aMessage.Write(testArg,buf16);
211 if(r!=badDescriptorError)
213 TRAP(r,aMessage.WriteL(testArg,buf16));
214 if(r!=badDescriptorError)
219 r = aMessage.GetDesLength(testArg);
222 r = aMessage.GetDesMaxLength(testArg);
225 r = aMessage.Read(testArg,buf8);
226 if(r!=badDescriptorError)
228 TRAP(r,aMessage.ReadL(testArg,buf8));
229 if(r!=badDescriptorError)
231 r = aMessage.Read(testArg,buf16);
234 TRAP(r,aMessage.ReadL(testArg,buf16));
237 r = aMessage.Write(testArg,buf8);
238 if(r!=badDescriptorError)
240 TRAP(r,aMessage.WriteL(testArg,buf8));
241 if(r!=badDescriptorError)
243 r = aMessage.Write(testArg,buf16);
244 if(r!=badDescriptorError)
246 TRAP(r,aMessage.WriteL(testArg,buf16));
247 if(r!=badDescriptorError)
252 r = aMessage.GetDesLength(testArg);
255 r = aMessage.GetDesMaxLength(testArg);
258 r = aMessage.Read(testArg,buf8);
261 TRAP(r,aMessage.ReadL(testArg,buf8));
264 r = aMessage.Read(testArg,buf16);
265 if(r!=badDescriptorError)
267 TRAP(r,aMessage.ReadL(testArg,buf16));
268 if(r!=badDescriptorError)
270 r = aMessage.Write(testArg,buf8);
273 TRAP(r,aMessage.WriteL(testArg,buf8));
276 r = aMessage.Write(testArg,buf16);
277 if(r!=badDescriptorError)
279 TRAP(r,aMessage.WriteL(testArg,buf16));
280 if(r!=badDescriptorError)
285 r = aMessage.GetDesLength(testArg);
288 r = aMessage.GetDesMaxLength(testArg);
291 r = aMessage.Read(testArg,buf8);
292 if(r!=badDescriptorError)
294 TRAP(r,aMessage.ReadL(testArg,buf8));
295 if(r!=badDescriptorError)
297 r = aMessage.Read(testArg,buf16);
300 TRAP(r,aMessage.ReadL(testArg,buf16));
303 r = aMessage.Write(testArg,buf8);
304 if(r!=badDescriptorError)
306 TRAP(r,aMessage.WriteL(testArg,buf8));
307 if(r!=badDescriptorError)
309 r = aMessage.Write(testArg,buf16);
312 TRAP(r,aMessage.WriteL(testArg,buf16));
317 case ETestBadClientDescRead:
318 //Testing the integrity of the server and Kernel if the client passes bad descriptor for IPC transfer
320 r = aMessage.GetDesLength(testArg);
322 buff = (TUint8*)User::Alloc(r);
328 TPtr8 ptr8(buff, r, r);
329 r=aMessage.Read(testArg,ptr8);
334 case ETestBadClientDescWrite:
335 //Testing the integrity of the server and Kernel if the client passes bad descriptor for IPC transfer
337 r = aMessage.GetDesLength(testArg);
339 buff = (TUint8*)User::Alloc(r);
345 TPtr8 ptr8(buff, r, r);
346 r=aMessage.Write(testArg,ptr8);
351 case ETestBadServerDescRead:
352 case ETestBadServerDescWrite:
353 //Testing the integrity of the client and Kernel if server passes bad descriptor for IPC transfer
355 //Create a chunk with a hole between addresses 0x1000 and 0x2000
357 r=c.CreateDisconnectedLocal(0,0,0x200000);
359 r=c.Commit(0,0x1000);
361 r=c.Commit(0x2000,0x1000);
365 switch(aMessage.Function()>>28)
367 case 0:base=0x1000;len=0x500;break;
368 case 1:base=0x1001;len=0x500;break;
369 case 2:base=0x1007;len=0x500;break;
370 case 3:base=0x1ff0;len=0x100;break;
371 case 4:base=0x1ff1;len=0x100;break;
372 case 5:base=0x1ff2;len=0x100;break;
373 case 6:base=0xff3;len=0x100;break;
374 default:base=0xfff;len=0x100;break;
377 TPtr8 ptr (c.Base()+base,len,len);
378 if ((aMessage.Function()&0x0fffffff) == ETestBadServerDescRead)
379 aMessage.Read(0,ptr); //The server should panic here
381 aMessage.Write(0,ptr); //The server should panic here
385 case ETestResourceCountPass:
386 r=aMessage.Function();
387 ResourceCountMarkStart();
388 ResourceCountMarkEnd(aMessage);
391 case ETestResourceCountFail:
392 r=aMessage.Function();
393 ResourceCountMarkStart();
395 ResourceCountMarkEnd(aMessage);
398 case ETestServiceLeave:
400 User::Leave(aMessage.Int0());
403 case ETestCreateSubSession:
405 TInt reply = aMessage.Int0();
406 r = aMessage.Write(3,TPtrC8((TUint8*)&reply,sizeof(reply)));
410 case ETestCloseSubSession:
411 iSubSessionCloseHandle = aMessage.Int3();
415 case ETestCloseSubSessionHandle:
416 r = iSubSessionCloseHandle;
417 iSubSessionCloseHandle = 0;
423 reply[0] = aMessage.Int0();
424 reply[1] = aMessage.Int1();
425 reply[2] = aMessage.Int2();
426 reply[3] = aMessage.Int3();
427 r = aMessage.Write(0,TPtrC8((TUint8*)&reply,sizeof(reply)));
431 case ETestEmptySubSessionMessage:
438 if(aMessage.Handle())
439 aMessage.Complete(r);
442 aMessage.Complete(KErrGeneral);
445 aMessage.Complete(KErrNone);
449 TInt CTestSession::CountResources()
451 return iResourceCount;
454 void CTestSession::ServiceError(const RMessage2& aMessage,TInt aError)
456 if(aMessage!=iMessage)
457 aError = KErrGeneral; // We got given the right message
459 aError += KTestServiceErrorModifier; // Let test harnes know we came through this routine
460 CSession2::ServiceError(aMessage,aError);
467 CTestSecureServer::CTestSecureServer(TInt aPriority)
468 : CServer2(aPriority,EGlobalSharableSessions)
472 CSession2* CTestSecureServer::NewSessionL(const TVersion& aVersion, const RMessage2& /*aMessage*/) const
474 if(*(TInt*)&aVersion)
475 User::Leave(KErrNotSupported); // Only accept version 0.0.00
476 return new (ELeave) CTestSession();
479 TInt CTestSecureServer::RunError(TInt aError)
481 return CServer2::RunError(aError+KTestRunErrorModifier); // Let test harnes know we came through this routine
486 // CTestActiveScheduler
489 class CTestActiveScheduler : public CActiveScheduler
492 virtual void Error(TInt anError) const;
495 void CTestActiveScheduler::Error(TInt anError) const
497 User::Panic(_L("TestServer Error"),anError);
506 _LIT(KServerName,"T_SSERVER-server");
507 const TInt KServerRendezvous = KRequestPending+1;
511 CTestActiveScheduler* activeScheduler = new (ELeave) CTestActiveScheduler;
512 CActiveScheduler::Install(activeScheduler);
513 CleanupStack::PushL(activeScheduler);
515 CTestSecureServer* server = new (ELeave) CTestSecureServer(0);
516 CleanupStack::PushL(server);
518 User::LeaveIfError(server->Start(KServerName));
520 RProcess::Rendezvous(KServerRendezvous);
522 CActiveScheduler::Start();
524 CleanupStack::PopAndDestroy(2);
529 CTrapCleanup* cleanupStack = CTrapCleanup::New();
532 TRAPD(leaveError,DoStartServer())
543 class RTestSession : public RSessionBase
546 inline TInt Connect()
548 TInt r=CreateSession(KServerName,TVersion());
552 inline TInt Connect(const TSecurityPolicy* aPolicy)
554 return CreateSession(KServerName,TVersion(),-1,EIpcSession_Unsharable,aPolicy,0);
556 inline TInt Connect(TVersion aVersion,TRequestStatus* aStatus)
558 return CreateSession(KServerName,aVersion,-1,EIpcSession_Unsharable,0,aStatus);
560 inline TInt Send(TInt aFunction)
561 { return RSessionBase::SendReceive(aFunction); }
562 inline TInt Send(TInt aFunction,const TIpcArgs& aArgs)
563 { return RSessionBase::SendReceive(aFunction,aArgs); }
564 inline void Send(TInt aFunction,TRequestStatus& aStatus)
565 { RSessionBase::SendReceive(aFunction,aStatus); }
566 inline void Send(TInt aFunction,const TIpcArgs& aArgs,TRequestStatus& aStatus)
567 { RSessionBase::SendReceive(aFunction,aArgs,aStatus); }
576 class RTestSubSession : public RSubSessionBase
579 inline TInt CreateSubSession(RSessionBase& aSession,TInt aFunction,const TIpcArgs& aArgs)
580 { return RSubSessionBase::CreateSubSession(aSession,aFunction,aArgs); }
581 inline TInt CreateSubSession(RSessionBase& aSession,TInt aFunction)
582 { return RSubSessionBase::CreateSubSession(aSession,aFunction); }
583 inline void CloseSubSession(TInt aFunction)
584 { RSubSessionBase::CloseSubSession(aFunction); }
585 inline TInt Send(TInt aFunction)
586 { return RSubSessionBase::SendReceive(aFunction); }
587 inline TInt Send(TInt aFunction,const TIpcArgs& aArgs)
588 { return RSubSessionBase::SendReceive(aFunction,aArgs); }
589 inline void Send(TInt aFunction,TRequestStatus& aStatus)
590 { RSubSessionBase::SendReceive(aFunction,aStatus); }
591 inline void Send(TInt aFunction,const TIpcArgs& aArgs,TRequestStatus& aStatus)
592 { RSubSessionBase::SendReceive(aFunction,aArgs,aStatus); }
597 RTestSession Session;
600 TInt TestThreadFunction(TAny* aParam)
602 return Session.Send((TInt)aParam,TIpcArgs(TestParam));
605 void DoTest(TInt aFunction,TExitType aExitType,TInt aExitReason,TInt aParam=0)
608 title.AppendFormat(_L("Function %d"),aFunction);
612 thread.Create(TestThreadFunction,(TAny*)aFunction);
613 TRequestStatus logon;
617 User::SetJustInTime(EFalse);
619 User::WaitForRequest(logon);
620 User::SetJustInTime(ETrue);
622 TExitType exitType=thread.ExitType();
623 TInt exitReason=thread.ExitReason();
624 test(exitType==aExitType);
625 test(exitReason==aExitReason);
626 CLOSE_AND_WAIT(thread);
633 class RTestAnonymousSession : public RTestSession
636 inline TInt Connect(RServer2 aServer)
638 TInt r=CreateSession(aServer,TVersion());
640 return ShareProtected();
642 inline TInt Connect()
644 return CreateSession(_L(""),TVersion());
648 CTestSecureServer* AnonymousServer;
650 void DoStartAnonymousServer()
652 CTestActiveScheduler* activeScheduler = new (ELeave) CTestActiveScheduler;
653 CActiveScheduler::Install(activeScheduler);
654 CleanupStack::PushL(activeScheduler);
656 CTestSecureServer* server = new (ELeave) CTestSecureServer(0);
657 CleanupStack::PushL(server);
659 User::LeaveIfError(server->Start(KNullDesC));
661 AnonymousServer = server;
662 RThread::Rendezvous(KServerRendezvous);
664 CActiveScheduler::Start();
666 CleanupStack::PopAndDestroy(2);
669 TInt StartAnonymousServer(TAny* /*aPtr*/)
671 CTrapCleanup* cleanupStack = CTrapCleanup::New();
674 TRAPD(leaveError,DoStartAnonymousServer())
679 void TestConnectByHandle()
681 RTestAnonymousSession session;
682 TRequestStatus logon;
685 test.Start(_L("Starting a local server"));
687 thread.Create(StartAnonymousServer);
688 TRequestStatus rendezvous;
689 TRequestStatus svrstat;
690 thread.Rendezvous(rendezvous);
692 thread.NotifyDestruction(svrstat);
694 User::WaitForRequest(rendezvous);
695 test(rendezvous.Int()==KServerRendezvous);
698 test.Next(_L("Check connect with null name fails"));
699 test((r=session.Connect())!=KErrNone);
701 test.Next(_L("Connecting to local server"));
702 test((r=session.Connect(AnonymousServer->Server()))==KErrNone);
704 test.Next(_L("Test the connection"));
705 test((r=session.Send(EPing,TIpcArgs(1234)))==1234);
706 test((r=session.Send(EPing,TIpcArgs(2345)))==2345);
708 test.Next(_L("Shutting server down"));
709 test(logon.Int()==KRequestPending);
710 test((r=session.Send(ETestShutdown))==KErrServerTerminated);
711 test(r==KErrServerTerminated);
713 User::WaitForRequest(logon);
714 test(logon.Int()==KErrNone);
715 User::WaitForRequest(svrstat);
720 TRequestStatus SvrStat;
722 void RestartTestServer()
725 TRequestStatus rendezvous;
726 server.Create(0,ETestProcessServer);
727 server.NotifyDestruction(SvrStat);
728 server.Rendezvous(rendezvous);
730 User::WaitForRequest(rendezvous);
731 test(rendezvous==KServerRendezvous);
733 test(Session.Connect()==KErrNone);
738 TBuf8<10> buf8((TUint8*)"abcdefghij");
739 TBuf16<10> buf16((TUint16*)L"abcdefghij");
742 test.Start(_L("Testing TIpcArgs::Set"));
744 TIpcArgs a(TIpcArgs::ENothing,123,(TAny*)&buf8);
746 b.Set(0,TIpcArgs::ENothing);
748 b.Set(2,(TAny*)&buf8);
749 test(a.iFlags==b.iFlags);
750 // iArgs[0] is uninitialised so don't test
751 test(a.iArgs[1]==b.iArgs[1]);
752 test(a.iArgs[2]==b.iArgs[2]);
755 TIpcArgs a((TDesC8*)&buf8,(TDesC16*)&buf16,&buf8,&buf16);
757 b.Set(0,(TDesC8*)&buf8);
758 b.Set(1,(TDesC16*)&buf16);
761 test(a.iFlags==b.iFlags);
762 test(a.iArgs[0]==b.iArgs[0]);
763 test(a.iArgs[1]==b.iArgs[1]);
764 test(a.iArgs[2]==b.iArgs[2]);
765 test(a.iArgs[3]==b.iArgs[3]);
768 test.Next(_L("Test Unspecified argument"));
769 r = Session.Send(ETestArgUnspecified,TIpcArgs((TAny*)&buf8));
771 r = Session.Send(ETestArgUnspecified+(1<<28),TIpcArgs(0,(TAny*)&buf8));
773 r = Session.Send(ETestArgUnspecified+(2<<28),TIpcArgs(0,0,(TAny*)&buf8));
775 r = Session.Send(ETestArgUnspecified+(3<<28),TIpcArgs(0,0,0,(TAny*)&buf8));
778 test.Next(_L("Test DesC8 argument"));
779 r = Session.Send(ETestArgDesC8,TIpcArgs((TDesC8*)&buf8));
781 r = Session.Send(ETestArgDesC8+(1<<28),TIpcArgs(0,(TDesC8*)&buf8));
783 r = Session.Send(ETestArgDesC8+(2<<28),TIpcArgs(0,0,(TDesC8*)&buf8));
785 r = Session.Send(ETestArgDesC8+(3<<28),TIpcArgs(0,0,0,(TDesC8*)&buf8));
788 test.Next(_L("Test DesC16 argument"));
789 r = Session.Send(ETestArgDesC16,TIpcArgs((TDesC16*)&buf16));
791 r = Session.Send(ETestArgDesC16+(1<<28),TIpcArgs(0,(TDesC16*)&buf16));
793 r = Session.Send(ETestArgDesC16+(2<<28),TIpcArgs(0,0,(TDesC16*)&buf16));
795 r = Session.Send(ETestArgDesC16+(3<<28),TIpcArgs(0,0,0,(TDesC16*)&buf16));
798 test.Next(_L("Test Des8 argument"));
799 r = Session.Send(ETestArgDes8,TIpcArgs(&buf8));
801 r = Session.Send(ETestArgDes8+(1<<28),TIpcArgs(0,&buf8));
803 r = Session.Send(ETestArgDes8+(2<<28),TIpcArgs(0,0,&buf8));
805 r = Session.Send(ETestArgDes8+(3<<28),TIpcArgs(0,0,0,&buf8));
808 test.Next(_L("Test Des16 argument"));
809 r = Session.Send(ETestArgDes16,TIpcArgs(&buf16));
811 r = Session.Send(ETestArgDes16+(1<<28),TIpcArgs(0,&buf16));
813 r = Session.Send(ETestArgDes16+(2<<28),TIpcArgs(0,0,&buf16));
815 r = Session.Send(ETestArgDes16+(3<<28),TIpcArgs(0,0,0,&buf16));
819 test.Next(_L("Test Bad Client Descriptor"));
820 //The test should ensure that both server and kernel are safe if client passes faulty descriptor to the server.
822 //Create a chunk with a hole between addresses 0x1000 & 0x2000
824 r=c.CreateDisconnectedLocal(0,0,0x200000);
826 r=c.Commit(0,0x1000);
828 r=c.Commit(0x2000,0x1000);
831 //Each of these steps will pass bad descriptor to the server for IPC transfer in both directions.
832 //KErrBadDescriptor should be returned.
834 TPtr8 ptr (c.Base()+0x1000,0x100,0x100);
835 r = Session.Send(ETestBadClientDescRead,TIpcArgs(&ptr));
836 test(KErrBadDescriptor == r);
837 r = Session.Send(ETestBadClientDescWrite,TIpcArgs(&ptr));
838 test(KErrBadDescriptor == r);
841 TPtr8 ptr (c.Base()+0x1003,0x100,0x100);
842 r = Session.Send(ETestBadClientDescRead,TIpcArgs(&ptr));
843 test(KErrBadDescriptor == r);
844 r = Session.Send(ETestBadClientDescWrite,TIpcArgs(&ptr));
845 test(KErrBadDescriptor == r);
848 TPtr8 ptr (c.Base()+0xe00,0x500,0x500);
849 r = Session.Send(ETestBadClientDescRead,TIpcArgs(&ptr));
850 test(KErrBadDescriptor == r);
851 r = Session.Send(ETestBadClientDescWrite,TIpcArgs(&ptr));
852 test(KErrBadDescriptor == r);
855 TPtr8 ptr (c.Base()+0xdff,0x500,0x500);
856 r = Session.Send(ETestBadClientDescRead,TIpcArgs(&ptr));
857 test(KErrBadDescriptor == r);
858 r = Session.Send(ETestBadClientDescWrite,TIpcArgs(&ptr));
859 test(KErrBadDescriptor == r);
862 TPtr8 ptr (c.Base()+0x1ff1,0x500,0x500);
863 r = Session.Send(ETestBadClientDescRead,TIpcArgs(&ptr));
864 test(KErrBadDescriptor == r);
865 r = Session.Send(ETestBadClientDescWrite,TIpcArgs(&ptr));
866 test(KErrBadDescriptor == r);
869 //The next step will send random descriptors for IPC transfer. Server should return either KErrNone or
873 seed[0]=User::TickCount();
875 test.Printf(_L("The initial seed for the random function is: S0=%xh S1=%xh\n"), seed[0], seed[1]);
880 TInt badDescriptors = 0;
884 TInt descAddress = (TInt) (c.Base() + (Random(seed) % 0x1000));
885 TInt descSize=Random(seed)%0x1000;
887 TUint* descData = (TUint*)&ptr;
888 descData[0] = 0x20000000 + descSize;
889 descData[1] = descSize;
890 descData[2] = descAddress;
892 r = Session.Send(ETestBadClientDescRead,TIpcArgs(&ptr));
895 case KErrNone: noErrors++;break;
896 case KErrBadDescriptor: badDescriptors++;break;
897 default: test.Printf(_L("Error: %d returned"),r);
900 r = Session.Send(ETestBadClientDescWrite,TIpcArgs(&ptr));
903 case KErrNone: noErrors++;break;
904 case KErrBadDescriptor: badDescriptors++;break;
905 default: test.Printf(_L("Error: %d returned"),r);
908 test.Printf(_L("KErrNoError: %d KErrBadDescriptor: %d"),noErrors, badDescriptors);
911 test.Next(_L("Test Bad Server Descriptor"));
912 //The test should ensure that kernel is safe if server passes faulty descriptor for IPC transfer
914 TPtr8 ptr (c.Base(),0x1000,0x1000);
918 r = Session.Send(ETestBadServerDescRead | (i<<28),TIpcArgs(&ptr));
919 test(r==KErrServerTerminated);
921 r = Session.Send(ETestBadServerDescWrite | (i<<28),TIpcArgs(&ptr));
922 test(r==KErrServerTerminated);
923 User::WaitForRequest(SvrStat);
935 void TestSubSessions()
937 const TInt KSubSessionHandle = 0x87654321;
941 test.Start(_L("Creating a subsession with no arguments"));
942 r = sub.CreateSubSession(Session,ETestCreateSubSession);
944 test(((TInt*)&sub)[0] == (*(TInt*)&Session | CObjectIx::ENoClose)); // check sub.iSession
946 test.Next(_L("Creating a subsession with arguments"));
947 r = sub.CreateSubSession(Session,ETestCreateSubSession,TIpcArgs(KSubSessionHandle));
949 test(((TInt*)&sub)[0] == (*(TInt*)&Session | CObjectIx::ENoClose)); // check sub.iSession
951 test(((TInt*)&sub)[1]==KSubSessionHandle); // check sub.iSubSessionHandle
953 test.Next(_L("Sending message with arguments"));
955 TPtr8 replyDes((TUint8*)&reply,sizeof(reply));
956 r = sub.Send(ETestEchoArgs,TIpcArgs(&replyDes,123,456,789));
958 test(reply[0]==(TInt)&replyDes);
961 test(reply[3]==KSubSessionHandle);
963 test.Next(_L("Sending empty message"));
964 r = sub.Send(ETestEmptySubSessionMessage);
965 test(r==KSubSessionHandle);
967 test.Next(_L("Closing subsession"));
968 sub.CloseSubSession(ETestCloseSubSession);
969 test(((TInt*)&sub)[0]==0); // check sub.iSession
970 test(((TInt*)&sub)[1]==0); // check sub.iSubSessionHandle
971 r = Session.Send(ETestCloseSubSessionHandle);
972 test(r==KSubSessionHandle);
978 void TestProtectedServers()
980 RTestProcess process;
981 TRequestStatus logonStatus;
983 test.Start(_L("Trying to create a protected server without KCapabilityProtServ"));
984 process.Create(~(1u<<ECapabilityProtServ),ETestProcessCreateProtectedServer);
985 process.Logon(logonStatus);
987 User::WaitForRequest(logonStatus);
988 if(PlatSec::IsCapabilityEnforced(ECapabilityProtServ))
990 test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
991 test(logonStatus==EPlatformSecurityTrap);
995 test(process.ExitType()==EExitKill);
996 test(logonStatus==KErrNone);
998 CLOSE_AND_WAIT(process);
1000 test.Next(_L("Trying to create a protected server with KCapabilityProtServ"));
1001 process.Create(1<<ECapabilityProtServ,ETestProcessCreateProtectedServer);
1002 process.Logon(logonStatus);
1004 User::WaitForRequest(logonStatus);
1005 test(process.ExitType()==EExitKill);
1006 test(logonStatus==KErrNone);
1007 CLOSE_AND_WAIT(process);
1013 void TestIdentifiedServers()
1015 RTestSession session;
1018 test.Start(_L("Trying to connect to a server with wrong Secure ID"));
1019 _LIT_SECURITY_POLICY_S0(wrongSid,KTestSecureId2);
1020 r=session.Connect(&wrongSid);
1021 test(r==(PlatSec::ConfigSetting(PlatSec::EPlatSecEnforcement)?KErrPermissionDenied:KErrNone));
1024 test.Next(_L("Test connecting to a server with correct Secure ID"));
1025 _LIT_SECURITY_POLICY_S0(correctSid,KTestSecureId);
1026 r=session.Connect(&correctSid);
1030 test.Next(_L("Trying to connect to a server with wrong Vendor ID"));
1031 _LIT_SECURITY_POLICY_V0(wrongVid,KTestVendorId2);
1032 r=session.Connect(&wrongVid);
1033 test(r==(PlatSec::ConfigSetting(PlatSec::EPlatSecEnforcement)?KErrPermissionDenied:KErrNone));
1036 test.Next(_L("Test connecting to a server with correct Vendor ID"));
1037 _LIT_SECURITY_POLICY_V0(correctVid,KTestVendorId);
1038 r=session.Connect(&correctVid);
1042 test.Next(_L("Trying to connect to a server with wrong capabilities"));
1043 _LIT_SECURITY_POLICY_C1(wrongCaps,ECapabilityReadUserData);
1044 r=session.Connect(&wrongCaps);
1045 test(r==(PlatSec::IsCapabilityEnforced(ECapabilityReadUserData)?KErrPermissionDenied:KErrNone));
1048 test.Next(_L("Test connecting to a server with correct capabilities"));
1049 _LIT_SECURITY_POLICY_C1(correctCaps,ECapability_None);
1050 r=session.Connect(&correctCaps);
1054 test.Next(_L("Test connecting to a server without specifying a policy"));
1055 r=session.Connect(0);
1064 #include <e32panic.h>
1066 TInt TestThreadAsyncConnect(TAny*)
1068 RTestSession session;
1069 TRequestStatus status;
1070 TInt r=session.Connect(TVersion(1,1,1),&status);
1073 if(status!=KRequestPending) // server can't have created session yet because we have higher priority
1074 return status.Int();
1075 User::WaitForRequest(status);
1076 if(status!=KErrNotSupported)
1077 return status.Int();
1078 return session.Send(EPing,TIpcArgs(1234)); // Should panic us
1081 void TestAsynchronousConnect()
1083 RTestSession session;
1084 TRequestStatus status;
1087 test.Start(_L("Test successful asynchronous connect"));
1088 r=session.Connect(TVersion(0,0,0),&status);
1090 test(status==KRequestPending); // server can't have created session yet because we have higher priority
1091 User::WaitForRequest(status);
1092 test(status==KErrNone);
1093 test((r=session.Send(EPing,TIpcArgs(1234)))==1234);
1096 test.Next(_L("Test unsuccessful asynchronous connect"));
1097 r=session.Connect(TVersion(1,1,1),&status);
1099 test(status==KRequestPending); // server can't have created session yet because we have higher priority
1100 User::WaitForRequest(status);
1101 test(status==KErrNotSupported);
1104 test.Next(_L("Test using unsuccessful asynchronous connect"));
1106 thread.Create(TestThreadAsyncConnect,0);
1107 TRequestStatus logon;
1108 thread.Logon(logon);
1109 User::SetJustInTime(EFalse);
1111 User::WaitForRequest(logon);
1112 User::SetJustInTime(ETrue);
1113 TExitType exitType=thread.ExitType();
1114 TInt exitReason=thread.ExitReason();
1115 test(exitType==EExitPanic);
1116 test(exitReason==CServer2::ESessionNotConnected);
1124 TInt DoTestProcess(TInt aTestNum,TInt aArg1,TInt aArg2)
1131 case ETestProcessServer:
1132 RProcess().SetPriority(EPriorityLow);
1133 return StartServer();
1135 case ETestProcessCreateProtectedServer:
1139 TInt r = server.CreateGlobal(KProtectedServerName);
1145 User::Panic(_L("T_SSERVER"),1);
1153 GLDEF_C TInt E32Main()
1156 User::CommandLine(cmd);
1157 if(cmd.Length() && TChar(cmd[0]).IsDigit())
1169 return DoTestProcess(function,arg1,arg2);
1176 test.Start(_L("Testing Server Connect by Handle"));
1177 TestConnectByHandle();
1179 test.Next(_L("Starting test server"));
1180 RTestProcess server;
1181 TRequestStatus rendezvous;
1182 server.Create(0,ETestProcessServer);
1183 server.Rendezvous(rendezvous);
1185 User::WaitForRequest(rendezvous);
1186 test(rendezvous==KServerRendezvous);
1189 test.Next(_L("Conecting to test server"));
1190 test((r=Session.Connect())==KErrNone);
1192 test.Next(_L("Test asynchronous server connect"));
1193 TestAsynchronousConnect();
1195 test.Next(_L("Test protected servers"));
1196 TestProtectedServers();
1198 test.Next(_L("Test connecting to identified servers"));
1199 TestIdentifiedServers();
1201 test.Next(_L("Testing SubSessions"));
1204 test.Next(_L("Testing IPC argument checking"));
1207 test.Next(_L("Testing CServer2::RunError and CSession2::ServiceError()"));
1208 const TInt KTestServiceLeaveValue = 555;
1209 r = Session.Send(ETestServiceLeave,TIpcArgs(KTestServiceLeaveValue));
1210 test(r==KTestServiceLeaveValue+KTestRunErrorModifier+KTestServiceErrorModifier);
1212 test.Next(_L("Testing session resource counting"));
1214 DoTest(ETestResourceCountPass,EExitKill,ETestResourceCountPass);
1215 DoTest(ETestResourceCountFail,EExitPanic,CSession2::ESesFoundResCountHeaven);
1218 test.Next(_L("Shutting server down"));
1220 Session.Send(ETestShutdown,TIpcArgs());
1223 User::WaitForRequest(SvrStat);