kerneltest/e32test/secure/t_sserver.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // e32test\secure\t_sserver.cpp
       
    15 // Overview:
       
    16 // Test the security aspects of server connect by handle.
       
    17 // API Information:
       
    18 // CSession2, CServer2
       
    19 // Details:
       
    20 // - Test asynchronous server connect in various ways. Verify results
       
    21 // are as expected.
       
    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
       
    29 // as expected.
       
    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
       
    33 // resource counting.
       
    34 // Platforms/Drives/Compatibility:
       
    35 // All.
       
    36 // Assumptions/Requirement/Pre-requisites:
       
    37 // Failures and causes:
       
    38 // Base Port information:
       
    39 // 
       
    40 //
       
    41 
       
    42 #include <e32test.h>
       
    43 #include "../misc/prbs.h"
       
    44 
       
    45 LOCAL_D RTest test(_L("T_SSERVER"));
       
    46 
       
    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");
       
    54 
       
    55 
       
    56 
       
    57 enum TTestProcessFunctions
       
    58 	{
       
    59 	ETestProcessServer,
       
    60 	ETestProcessCreateProtectedServer,
       
    61 	};
       
    62 
       
    63 #include "testprocess.h"
       
    64 
       
    65 
       
    66 
       
    67 //
       
    68 // RTestThread
       
    69 //
       
    70 
       
    71 class RTestThread : public RThread
       
    72 	{
       
    73 public:
       
    74 	void Create(TThreadFunction aFunction,TAny* aArg=0);
       
    75 	};
       
    76 
       
    77 void RTestThread::Create(TThreadFunction aFunction,TAny* aArg)
       
    78 	{
       
    79 	TInt r=RThread::Create(_L(""),aFunction,KDefaultStackSize,KDefaultStackSize,KDefaultStackSize,aArg);
       
    80 	test(r==KErrNone);
       
    81 	}
       
    82 
       
    83 
       
    84 
       
    85 //
       
    86 // CTestSession
       
    87 //
       
    88 
       
    89 enum TTestSessionFuntions
       
    90 	{
       
    91 	ETestShutdown,EPing,
       
    92 	ETestArgUnspecified,ETestArgDesC8,ETestArgDesC16,ETestArgDes8,ETestArgDes16,
       
    93 	ETestResourceCountPass=0x01234567,ETestResourceCountFail,ETestServiceLeave,
       
    94 	ETestCreateSubSession,ETestCloseSubSession,ETestCloseSubSessionHandle,
       
    95 	ETestEchoArgs,ETestEmptySubSessionMessage, ETestBadClientDescRead, ETestBadClientDescWrite,
       
    96 	ETestBadServerDescRead, ETestBadServerDescWrite
       
    97 	};
       
    98 
       
    99 class CTestSession : public CSession2
       
   100 	{
       
   101 public:
       
   102 	CTestSession();
       
   103 	virtual void ServiceL(const RMessage2& aMessage);
       
   104 	virtual void ServiceError(const RMessage2& aMessage,TInt aError);
       
   105 private:
       
   106 	TInt CountResources();
       
   107 private:
       
   108 	TInt iResourceCount;
       
   109 	TInt iSubSessionCloseHandle;
       
   110 	RMessage2 iMessage;
       
   111 	};
       
   112 
       
   113 class CTestSecureServer : public CServer2
       
   114 	{
       
   115 public:
       
   116 	CTestSecureServer(TInt aPriority);
       
   117 	virtual CSession2* NewSessionL(const TVersion& aVersion, const RMessage2& aMessage) const;
       
   118 private:
       
   119 	void CustomL(const RMessage2& aMessage);
       
   120 	void QueryL(const RMessage2& aMessage,HBufC8*& aQueryParams);
       
   121 	TInt RunError(TInt aError);
       
   122 private:
       
   123 	friend class CTestSession;
       
   124 	};
       
   125 
       
   126 CTestSession::CTestSession()
       
   127 	: CSession2()
       
   128 	{}
       
   129 
       
   130 void CTestSession::ServiceL(const RMessage2& aMessage)
       
   131 	{
       
   132 	TInt r = KErrNotSupported;
       
   133 	TBuf8<100> buf8;
       
   134 	buf8.Append(_L8("12345"));
       
   135 	TBuf16<100> buf16;
       
   136 	buf16.Append(_L16("12345"));
       
   137 	TInt testArg = ((TUint)aMessage.Function())>>28;
       
   138 
       
   139 	TInt badDescriptorError = PlatSec::ConfigSetting(PlatSec::EPlatSecProcessIsolation)&&PlatSec::ConfigSetting(PlatSec::EPlatSecEnforcement)
       
   140 		? KErrBadDescriptor : KErrNone;
       
   141 
       
   142 	switch(aMessage.Function()&0x0fffffff)
       
   143 		{
       
   144 		case ETestShutdown:
       
   145 			CActiveScheduler::Stop();
       
   146 			return;
       
   147 
       
   148 		case EPing:
       
   149 			r=aMessage.Int0();
       
   150 			break;
       
   151 
       
   152 		case ETestArgUnspecified:
       
   153 			r = aMessage.GetDesLength(testArg);
       
   154 			if(r!=KErrBadDescriptor)
       
   155 				goto fail;
       
   156 			r = aMessage.GetDesMaxLength(testArg);
       
   157 			if(r!=KErrBadDescriptor)
       
   158 				goto fail;
       
   159 			r = aMessage.Read(testArg,buf8);
       
   160 			if(r!=badDescriptorError)
       
   161 				goto fail;
       
   162 			TRAP(r,aMessage.ReadL(testArg,buf8));
       
   163 			if(r!=badDescriptorError)
       
   164 				goto fail;
       
   165 			r = aMessage.Read(testArg,buf16);
       
   166 			if(r!=badDescriptorError)
       
   167 				goto fail;
       
   168 			TRAP(r,aMessage.ReadL(testArg,buf16));
       
   169 			if(r!=badDescriptorError)
       
   170 				goto fail;
       
   171 			r = aMessage.Write(testArg,buf8);
       
   172 			if(r!=badDescriptorError)
       
   173 				goto fail;
       
   174 			TRAP(r,aMessage.WriteL(testArg,buf8));
       
   175 			if(r!=badDescriptorError)
       
   176 				goto fail;
       
   177 			r = aMessage.Write(testArg,buf16);
       
   178 			if(r!=badDescriptorError)
       
   179 				goto fail;
       
   180 			TRAP(r,aMessage.WriteL(testArg,buf16));
       
   181 			if(r!=badDescriptorError)
       
   182 				goto fail;
       
   183 			goto pass;
       
   184 
       
   185 		case ETestArgDesC8:
       
   186 			r = aMessage.GetDesLength(testArg);
       
   187 			if(r<0)
       
   188 				goto fail;
       
   189 			r = aMessage.GetDesMaxLength(testArg);
       
   190 			if(r<0)
       
   191 				goto fail;
       
   192 			r = aMessage.Read(testArg,buf8);
       
   193 			if(r!=KErrNone)
       
   194 				goto fail;
       
   195 			TRAP(r,aMessage.ReadL(testArg,buf8));
       
   196 			if(r!=KErrNone)
       
   197 				goto fail;
       
   198 			r = aMessage.Read(testArg,buf16);
       
   199 			if(r!=badDescriptorError)
       
   200 				goto fail;
       
   201 			TRAP(r,aMessage.ReadL(testArg,buf16));
       
   202 			if(r!=badDescriptorError)
       
   203 				goto fail;
       
   204 			r = aMessage.Write(testArg,buf8);
       
   205 			if(r!=badDescriptorError)
       
   206 				goto fail;
       
   207 			TRAP(r,aMessage.WriteL(testArg,buf8));
       
   208 			if(r!=badDescriptorError)
       
   209 				goto fail;
       
   210 			r = aMessage.Write(testArg,buf16);
       
   211 			if(r!=badDescriptorError)
       
   212 				goto fail;
       
   213 			TRAP(r,aMessage.WriteL(testArg,buf16));
       
   214 			if(r!=badDescriptorError)
       
   215 				goto fail;
       
   216 			goto pass;
       
   217 
       
   218 		case ETestArgDesC16:
       
   219 			r = aMessage.GetDesLength(testArg);
       
   220 			if(r<0)
       
   221 				goto fail;
       
   222 			r = aMessage.GetDesMaxLength(testArg);
       
   223 			if(r<0)
       
   224 				goto fail;
       
   225 			r = aMessage.Read(testArg,buf8);
       
   226 			if(r!=badDescriptorError)
       
   227 				goto fail;
       
   228 			TRAP(r,aMessage.ReadL(testArg,buf8));
       
   229 			if(r!=badDescriptorError)
       
   230 				goto fail;
       
   231 			r = aMessage.Read(testArg,buf16);
       
   232 			if(r!=KErrNone)
       
   233 				goto fail;
       
   234 			TRAP(r,aMessage.ReadL(testArg,buf16));
       
   235 			if(r!=KErrNone)
       
   236 				goto fail;
       
   237 			r = aMessage.Write(testArg,buf8);
       
   238 			if(r!=badDescriptorError)
       
   239 				goto fail;
       
   240 			TRAP(r,aMessage.WriteL(testArg,buf8));
       
   241 			if(r!=badDescriptorError)
       
   242 				goto fail;
       
   243 			r = aMessage.Write(testArg,buf16);
       
   244 			if(r!=badDescriptorError)
       
   245 				goto fail;
       
   246 			TRAP(r,aMessage.WriteL(testArg,buf16));
       
   247 			if(r!=badDescriptorError)
       
   248 				goto fail;
       
   249 			goto pass;
       
   250 
       
   251 		case ETestArgDes8:
       
   252 			r = aMessage.GetDesLength(testArg);
       
   253 			if(r<0)
       
   254 				goto fail;
       
   255 			r = aMessage.GetDesMaxLength(testArg);
       
   256 			if(r<0)
       
   257 				goto fail;
       
   258 			r = aMessage.Read(testArg,buf8);
       
   259 			if(r!=KErrNone)
       
   260 				goto fail;
       
   261 			TRAP(r,aMessage.ReadL(testArg,buf8));
       
   262 			if(r!=KErrNone)
       
   263 				goto fail;
       
   264 			r = aMessage.Read(testArg,buf16);
       
   265 			if(r!=badDescriptorError)
       
   266 				goto fail;
       
   267 			TRAP(r,aMessage.ReadL(testArg,buf16));
       
   268 			if(r!=badDescriptorError)
       
   269 				goto fail;
       
   270 			r = aMessage.Write(testArg,buf8);
       
   271 			if(r!=KErrNone)
       
   272 				goto fail;
       
   273 			TRAP(r,aMessage.WriteL(testArg,buf8));
       
   274 			if(r!=KErrNone)
       
   275 				goto fail;
       
   276 			r = aMessage.Write(testArg,buf16);
       
   277 			if(r!=badDescriptorError)
       
   278 				goto fail;
       
   279 			TRAP(r,aMessage.WriteL(testArg,buf16));
       
   280 			if(r!=badDescriptorError)
       
   281 				goto fail;
       
   282 			goto pass;
       
   283 
       
   284 		case ETestArgDes16:
       
   285 			r = aMessage.GetDesLength(testArg);
       
   286 			if(r<0)
       
   287 				goto fail;
       
   288 			r = aMessage.GetDesMaxLength(testArg);
       
   289 			if(r<0)
       
   290 				goto fail;
       
   291 			r = aMessage.Read(testArg,buf8);
       
   292 			if(r!=badDescriptorError)
       
   293 				goto fail;
       
   294 			TRAP(r,aMessage.ReadL(testArg,buf8));
       
   295 			if(r!=badDescriptorError)
       
   296 				goto fail;
       
   297 			r = aMessage.Read(testArg,buf16);
       
   298 			if(r!=KErrNone)
       
   299 				goto fail;
       
   300 			TRAP(r,aMessage.ReadL(testArg,buf16));
       
   301 			if(r!=KErrNone)
       
   302 				goto fail;
       
   303 			r = aMessage.Write(testArg,buf8);
       
   304 			if(r!=badDescriptorError)
       
   305 				goto fail;
       
   306 			TRAP(r,aMessage.WriteL(testArg,buf8));
       
   307 			if(r!=badDescriptorError)
       
   308 				goto fail;
       
   309 			r = aMessage.Write(testArg,buf16);
       
   310 			if(r!=KErrNone)
       
   311 				goto fail;
       
   312 			TRAP(r,aMessage.WriteL(testArg,buf16));
       
   313 			if(r!=KErrNone)
       
   314 				goto fail;
       
   315 			goto pass;
       
   316 		
       
   317 		case ETestBadClientDescRead:
       
   318 			//Testing the integrity of the server and Kernel if the client passes bad descriptor for IPC transfer
       
   319 			{
       
   320 			r = aMessage.GetDesLength(testArg);
       
   321 			TUint8* buff;
       
   322 			buff = (TUint8*)User::Alloc(r);
       
   323 			if (!buff)
       
   324 				{
       
   325 				r=KErrNoMemory;
       
   326 				break;
       
   327 				}
       
   328 			TPtr8 ptr8(buff, r, r);
       
   329 			r=aMessage.Read(testArg,ptr8);
       
   330 			User::Free(buff);
       
   331 			}
       
   332 			break;
       
   333 
       
   334 		case ETestBadClientDescWrite:
       
   335 			//Testing the integrity of the server and Kernel if the client passes bad descriptor for IPC transfer
       
   336 			{
       
   337 			r = aMessage.GetDesLength(testArg);
       
   338 			TUint8* buff;
       
   339 			buff = (TUint8*)User::Alloc(r);
       
   340 			if (!buff)
       
   341 				{
       
   342 				r=KErrNoMemory;
       
   343 				break;
       
   344 				}
       
   345 			TPtr8 ptr8(buff, r, r);
       
   346 			r=aMessage.Write(testArg,ptr8);
       
   347 			User::Free(buff);
       
   348 			}
       
   349 			break;
       
   350 
       
   351 		case ETestBadServerDescRead:
       
   352 		case ETestBadServerDescWrite:
       
   353 			//Testing the integrity of the client and Kernel if server passes bad descriptor for IPC transfer
       
   354 			{
       
   355 			//Create a chunk with a hole between addresses 0x1000 and 0x2000
       
   356 			RChunk c;
       
   357 			r=c.CreateDisconnectedLocal(0,0,0x200000);
       
   358 			test(r==KErrNone);
       
   359 			r=c.Commit(0,0x1000);
       
   360 			test(r==KErrNone);
       
   361 			r=c.Commit(0x2000,0x1000);
       
   362 			test(r==KErrNone);
       
   363 			
       
   364 			TInt base,len;
       
   365 			switch(aMessage.Function()>>28)
       
   366 				{ 
       
   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;
       
   375 				}
       
   376 
       
   377 			TPtr8 ptr (c.Base()+base,len,len);
       
   378 			if ((aMessage.Function()&0x0fffffff) == ETestBadServerDescRead)
       
   379 				aMessage.Read(0,ptr);	//The server should panic here
       
   380 			else
       
   381 				aMessage.Write(0,ptr);	//The server should panic here
       
   382 			}
       
   383 			break;
       
   384 
       
   385 		case ETestResourceCountPass:
       
   386 			r=aMessage.Function();
       
   387 			ResourceCountMarkStart();
       
   388 			ResourceCountMarkEnd(aMessage);
       
   389 			break;
       
   390 
       
   391 		case ETestResourceCountFail:
       
   392 			r=aMessage.Function();
       
   393 			ResourceCountMarkStart();
       
   394 			++iResourceCount;
       
   395 			ResourceCountMarkEnd(aMessage);
       
   396 			break;
       
   397 
       
   398 		case ETestServiceLeave:
       
   399 			iMessage = aMessage;
       
   400 			User::Leave(aMessage.Int0());
       
   401 			break;
       
   402 
       
   403 		case ETestCreateSubSession:
       
   404 			{
       
   405 			TInt reply = aMessage.Int0();
       
   406 			r = aMessage.Write(3,TPtrC8((TUint8*)&reply,sizeof(reply)));
       
   407 			}
       
   408 			break;
       
   409 
       
   410 		case ETestCloseSubSession:
       
   411 			iSubSessionCloseHandle = aMessage.Int3();
       
   412 			r = KErrNone;
       
   413 			break;
       
   414 
       
   415 		case ETestCloseSubSessionHandle:
       
   416 			r = iSubSessionCloseHandle;
       
   417 			iSubSessionCloseHandle = 0;
       
   418 			break;
       
   419 
       
   420 		case ETestEchoArgs:
       
   421 			{
       
   422 			TInt reply[4];
       
   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)));
       
   428 			}
       
   429 			break;
       
   430 
       
   431 		case ETestEmptySubSessionMessage:
       
   432 			r = aMessage.Int3();
       
   433 			break;
       
   434 
       
   435 		default:
       
   436 			break;
       
   437 		}
       
   438 	if(aMessage.Handle())
       
   439 		aMessage.Complete(r);
       
   440 	return;
       
   441 fail:
       
   442 	aMessage.Complete(KErrGeneral);
       
   443 	return;
       
   444 pass:
       
   445 	aMessage.Complete(KErrNone);
       
   446 	return;
       
   447 	}
       
   448 
       
   449 TInt CTestSession::CountResources()
       
   450 	{
       
   451 	return iResourceCount;
       
   452 	}
       
   453 
       
   454 void CTestSession::ServiceError(const RMessage2& aMessage,TInt aError)
       
   455 	{
       
   456 	if(aMessage!=iMessage) 
       
   457 		aError = KErrGeneral;   // We got given the right message
       
   458 	else
       
   459 		aError += KTestServiceErrorModifier;   // Let test harnes know we came through this routine
       
   460 	CSession2::ServiceError(aMessage,aError);
       
   461 	}
       
   462 
       
   463 //
       
   464 // CTestSecureServer
       
   465 //
       
   466 
       
   467 CTestSecureServer::CTestSecureServer(TInt aPriority)
       
   468 	: CServer2(aPriority,EGlobalSharableSessions)
       
   469 	{
       
   470 	}
       
   471 
       
   472 CSession2* CTestSecureServer::NewSessionL(const TVersion& aVersion, const RMessage2& /*aMessage*/) const
       
   473 	{
       
   474 	if(*(TInt*)&aVersion)
       
   475 		User::Leave(KErrNotSupported); // Only accept version 0.0.00
       
   476 	return new (ELeave) CTestSession();
       
   477 	}
       
   478 
       
   479 TInt CTestSecureServer::RunError(TInt aError)
       
   480 	{
       
   481 	return CServer2::RunError(aError+KTestRunErrorModifier);    // Let test harnes know we came through this routine
       
   482 	}
       
   483 
       
   484 
       
   485 //
       
   486 // CTestActiveScheduler
       
   487 //
       
   488 
       
   489 class CTestActiveScheduler : public CActiveScheduler
       
   490 	{
       
   491 public:
       
   492 	virtual void Error(TInt anError) const;
       
   493 	};
       
   494 
       
   495 void CTestActiveScheduler::Error(TInt anError) const
       
   496 	{
       
   497 	User::Panic(_L("TestServer Error"),anError);
       
   498 	}
       
   499 
       
   500 
       
   501 
       
   502 //
       
   503 // Server thread
       
   504 //
       
   505 
       
   506 _LIT(KServerName,"T_SSERVER-server");
       
   507 const TInt KServerRendezvous = KRequestPending+1;
       
   508 
       
   509 void DoStartServer()
       
   510 	{
       
   511 	CTestActiveScheduler* activeScheduler = new (ELeave) CTestActiveScheduler;
       
   512 	CActiveScheduler::Install(activeScheduler);
       
   513 	CleanupStack::PushL(activeScheduler);
       
   514 
       
   515 	CTestSecureServer* server = new (ELeave) CTestSecureServer(0);
       
   516 	CleanupStack::PushL(server);
       
   517 
       
   518 	User::LeaveIfError(server->Start(KServerName));
       
   519 
       
   520 	RProcess::Rendezvous(KServerRendezvous);
       
   521 
       
   522 	CActiveScheduler::Start();
       
   523 
       
   524 	CleanupStack::PopAndDestroy(2);
       
   525 	}
       
   526 
       
   527 TInt StartServer()
       
   528 	{
       
   529 	CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
   530 	if(!cleanupStack)
       
   531 		return KErrNoMemory;
       
   532 	TRAPD(leaveError,DoStartServer())
       
   533 	delete cleanupStack;
       
   534 	return leaveError;
       
   535 	}
       
   536 
       
   537 
       
   538 
       
   539 //
       
   540 // RTestSession
       
   541 //
       
   542 
       
   543 class RTestSession : public RSessionBase
       
   544 	{
       
   545 public:
       
   546 	inline TInt Connect()
       
   547 		{
       
   548 		TInt r=CreateSession(KServerName,TVersion());
       
   549 		if(r) return r;
       
   550 		return ShareAuto();
       
   551 		}
       
   552 	inline TInt Connect(const TSecurityPolicy* aPolicy)
       
   553 		{
       
   554 		return CreateSession(KServerName,TVersion(),-1,EIpcSession_Unsharable,aPolicy,0);
       
   555 		}
       
   556 	inline TInt Connect(TVersion aVersion,TRequestStatus* aStatus)
       
   557 		{
       
   558 		return CreateSession(KServerName,aVersion,-1,EIpcSession_Unsharable,0,aStatus);
       
   559 		}
       
   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); }
       
   568 	};
       
   569 
       
   570 
       
   571 
       
   572 //
       
   573 // RTestSubSession
       
   574 //
       
   575 
       
   576 class RTestSubSession : public RSubSessionBase
       
   577 	{
       
   578 public:
       
   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); }
       
   593 	};
       
   594 
       
   595 
       
   596 
       
   597 RTestSession Session;
       
   598 TInt TestParam = 0;
       
   599 
       
   600 TInt TestThreadFunction(TAny* aParam)
       
   601 	{
       
   602 	return Session.Send((TInt)aParam,TIpcArgs(TestParam));
       
   603 	}
       
   604 
       
   605 void DoTest(TInt aFunction,TExitType aExitType,TInt aExitReason,TInt aParam=0)
       
   606 	{
       
   607 	TBuf<256> title;
       
   608 	title.AppendFormat(_L("Function %d"),aFunction);
       
   609 	test.Next(title);
       
   610 
       
   611 	RTestThread thread;
       
   612 	thread.Create(TestThreadFunction,(TAny*)aFunction);
       
   613 	TRequestStatus logon;
       
   614 	thread.Logon(logon);
       
   615 	TestParam = aParam;
       
   616 
       
   617 	User::SetJustInTime(EFalse);
       
   618 	thread.Resume();
       
   619 	User::WaitForRequest(logon);
       
   620 	User::SetJustInTime(ETrue);
       
   621 
       
   622 	TExitType exitType=thread.ExitType();
       
   623 	TInt exitReason=thread.ExitReason();
       
   624 	test(exitType==aExitType);
       
   625 	test(exitReason==aExitReason);
       
   626 	CLOSE_AND_WAIT(thread);
       
   627 	}
       
   628 
       
   629 //
       
   630 // Anonymous server
       
   631 //
       
   632 
       
   633 class RTestAnonymousSession : public RTestSession
       
   634 	{
       
   635 public:
       
   636 	inline TInt Connect(RServer2 aServer)
       
   637 		{
       
   638 		TInt r=CreateSession(aServer,TVersion());
       
   639 		if(r) return r;
       
   640 		return ShareProtected();
       
   641 		}
       
   642 	inline TInt Connect()
       
   643 		{
       
   644 		return CreateSession(_L(""),TVersion());
       
   645 		}
       
   646 	};
       
   647 
       
   648 CTestSecureServer* AnonymousServer;
       
   649 
       
   650 void DoStartAnonymousServer()
       
   651 	{
       
   652 	CTestActiveScheduler* activeScheduler = new (ELeave) CTestActiveScheduler;
       
   653 	CActiveScheduler::Install(activeScheduler);
       
   654 	CleanupStack::PushL(activeScheduler);
       
   655 
       
   656 	CTestSecureServer* server = new (ELeave) CTestSecureServer(0);
       
   657 	CleanupStack::PushL(server);
       
   658 
       
   659 	User::LeaveIfError(server->Start(KNullDesC));
       
   660 
       
   661 	AnonymousServer = server;
       
   662 	RThread::Rendezvous(KServerRendezvous);
       
   663 
       
   664 	CActiveScheduler::Start();
       
   665 
       
   666 	CleanupStack::PopAndDestroy(2);
       
   667 	}
       
   668 
       
   669 TInt StartAnonymousServer(TAny* /*aPtr*/)
       
   670 	{
       
   671 	CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
   672 	if(!cleanupStack)
       
   673 		return KErrNoMemory;
       
   674 	TRAPD(leaveError,DoStartAnonymousServer())
       
   675 	delete cleanupStack;
       
   676 	return leaveError;
       
   677 	}
       
   678 
       
   679 void TestConnectByHandle()
       
   680 	{
       
   681 	RTestAnonymousSession session;
       
   682 	TRequestStatus logon;
       
   683 	TInt r;
       
   684 
       
   685 	test.Start(_L("Starting a local server"));
       
   686 	RTestThread thread;
       
   687 	thread.Create(StartAnonymousServer);
       
   688 	TRequestStatus rendezvous;
       
   689 	TRequestStatus svrstat;
       
   690 	thread.Rendezvous(rendezvous);
       
   691 	thread.Logon(logon);
       
   692 	thread.NotifyDestruction(svrstat);
       
   693 	thread.Resume();
       
   694 	User::WaitForRequest(rendezvous);
       
   695 	test(rendezvous.Int()==KServerRendezvous);
       
   696 	thread.Close();
       
   697 
       
   698 	test.Next(_L("Check connect with null name fails"));
       
   699 	test((r=session.Connect())!=KErrNone);
       
   700 
       
   701 	test.Next(_L("Connecting to local server"));
       
   702 	test((r=session.Connect(AnonymousServer->Server()))==KErrNone);
       
   703 
       
   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);
       
   707 
       
   708 	test.Next(_L("Shutting server down"));
       
   709 	test(logon.Int()==KRequestPending);
       
   710 	test((r=session.Send(ETestShutdown))==KErrServerTerminated);
       
   711 	test(r==KErrServerTerminated);
       
   712 	session.Close();
       
   713 	User::WaitForRequest(logon);
       
   714 	test(logon.Int()==KErrNone);
       
   715 	User::WaitForRequest(svrstat);
       
   716 
       
   717 	test.End();
       
   718 	}
       
   719 
       
   720 TRequestStatus SvrStat;
       
   721 
       
   722 void RestartTestServer()
       
   723 	{
       
   724 	RTestProcess server;
       
   725 	TRequestStatus rendezvous;
       
   726 	server.Create(0,ETestProcessServer);
       
   727 	server.NotifyDestruction(SvrStat);
       
   728 	server.Rendezvous(rendezvous);
       
   729 	server.Resume();
       
   730 	User::WaitForRequest(rendezvous);
       
   731 	test(rendezvous==KServerRendezvous);
       
   732 	server.Close();
       
   733 	test(Session.Connect()==KErrNone);
       
   734 	}
       
   735 
       
   736 void TestIpc()
       
   737 	{
       
   738 	TBuf8<10> buf8((TUint8*)"abcdefghij");
       
   739 	TBuf16<10> buf16((TUint16*)L"abcdefghij");
       
   740 	TInt r;
       
   741 
       
   742 	test.Start(_L("Testing TIpcArgs::Set"));
       
   743 	{
       
   744 	TIpcArgs a(TIpcArgs::ENothing,123,(TAny*)&buf8);
       
   745 	TIpcArgs b;
       
   746 	b.Set(0,TIpcArgs::ENothing);
       
   747 	b.Set(1,123);
       
   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]);
       
   753 	}
       
   754 	{
       
   755 	TIpcArgs a((TDesC8*)&buf8,(TDesC16*)&buf16,&buf8,&buf16);
       
   756 	TIpcArgs b;
       
   757 	b.Set(0,(TDesC8*)&buf8);
       
   758 	b.Set(1,(TDesC16*)&buf16);
       
   759 	b.Set(2,&buf8);
       
   760 	b.Set(3,&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]);
       
   766 	}
       
   767 
       
   768 	test.Next(_L("Test Unspecified argument"));
       
   769 	r = Session.Send(ETestArgUnspecified,TIpcArgs((TAny*)&buf8));
       
   770 	test(r==KErrNone);
       
   771 	r = Session.Send(ETestArgUnspecified+(1<<28),TIpcArgs(0,(TAny*)&buf8));
       
   772 	test(r==KErrNone);
       
   773 	r = Session.Send(ETestArgUnspecified+(2<<28),TIpcArgs(0,0,(TAny*)&buf8));
       
   774 	test(r==KErrNone);
       
   775 	r = Session.Send(ETestArgUnspecified+(3<<28),TIpcArgs(0,0,0,(TAny*)&buf8));
       
   776 	test(r==KErrNone);
       
   777 
       
   778 	test.Next(_L("Test DesC8 argument"));
       
   779 	r = Session.Send(ETestArgDesC8,TIpcArgs((TDesC8*)&buf8));
       
   780 	test(r==KErrNone);
       
   781 	r = Session.Send(ETestArgDesC8+(1<<28),TIpcArgs(0,(TDesC8*)&buf8));
       
   782 	test(r==KErrNone);
       
   783 	r = Session.Send(ETestArgDesC8+(2<<28),TIpcArgs(0,0,(TDesC8*)&buf8));
       
   784 	test(r==KErrNone);
       
   785 	r = Session.Send(ETestArgDesC8+(3<<28),TIpcArgs(0,0,0,(TDesC8*)&buf8));
       
   786 	test(r==KErrNone);
       
   787 
       
   788 	test.Next(_L("Test DesC16 argument"));
       
   789 	r = Session.Send(ETestArgDesC16,TIpcArgs((TDesC16*)&buf16));
       
   790 	test(r==KErrNone);
       
   791 	r = Session.Send(ETestArgDesC16+(1<<28),TIpcArgs(0,(TDesC16*)&buf16));
       
   792 	test(r==KErrNone);
       
   793 	r = Session.Send(ETestArgDesC16+(2<<28),TIpcArgs(0,0,(TDesC16*)&buf16));
       
   794 	test(r==KErrNone);
       
   795 	r = Session.Send(ETestArgDesC16+(3<<28),TIpcArgs(0,0,0,(TDesC16*)&buf16));
       
   796 	test(r==KErrNone);
       
   797 
       
   798 	test.Next(_L("Test Des8 argument"));
       
   799 	r = Session.Send(ETestArgDes8,TIpcArgs(&buf8));
       
   800 	test(r==KErrNone);
       
   801 	r = Session.Send(ETestArgDes8+(1<<28),TIpcArgs(0,&buf8));
       
   802 	test(r==KErrNone);
       
   803 	r = Session.Send(ETestArgDes8+(2<<28),TIpcArgs(0,0,&buf8));
       
   804 	test(r==KErrNone);
       
   805 	r = Session.Send(ETestArgDes8+(3<<28),TIpcArgs(0,0,0,&buf8));
       
   806 	test(r==KErrNone);
       
   807 
       
   808 	test.Next(_L("Test Des16 argument"));
       
   809 	r = Session.Send(ETestArgDes16,TIpcArgs(&buf16));
       
   810 	test(r==KErrNone);
       
   811 	r = Session.Send(ETestArgDes16+(1<<28),TIpcArgs(0,&buf16));
       
   812 	test(r==KErrNone);
       
   813 	r = Session.Send(ETestArgDes16+(2<<28),TIpcArgs(0,0,&buf16));
       
   814 	test(r==KErrNone);
       
   815 	r = Session.Send(ETestArgDes16+(3<<28),TIpcArgs(0,0,0,&buf16));
       
   816 	test(r==KErrNone);
       
   817 
       
   818 
       
   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.
       
   821 	{
       
   822 	//Create a chunk with a hole between addresses 0x1000 & 0x2000
       
   823 	RChunk c;
       
   824 	r=c.CreateDisconnectedLocal(0,0,0x200000);
       
   825 	test(r==KErrNone);
       
   826 	r=c.Commit(0,0x1000);
       
   827 	test(r==KErrNone);
       
   828 	r=c.Commit(0x2000,0x1000);
       
   829 	test(r==KErrNone);
       
   830 
       
   831 	//Each of these steps will pass bad descriptor to the server for IPC transfer in both directions.
       
   832 	//KErrBadDescriptor should be returned.
       
   833 	{
       
   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);
       
   839 	}
       
   840 	{
       
   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);
       
   846 	}
       
   847 	{
       
   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);
       
   853 	}
       
   854 	{
       
   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);
       
   860 	}
       
   861 	{
       
   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);
       
   867 	}
       
   868 
       
   869 	//The next step will send random descriptors for IPC transfer. Server should return either KErrNone or
       
   870 	//KErrBadDescriptor
       
   871 	{	
       
   872 	TUint seed[2];
       
   873 	seed[0]=User::TickCount();
       
   874 	seed[1]=0;
       
   875 	test.Printf(_L("The initial seed for the random function is:  S0=%xh S1=%xh\n"), seed[0], seed[1]);
       
   876 
       
   877 	TInt i;
       
   878 	TPtr8 ptr (0,0,0);
       
   879 	TInt noErrors = 0;
       
   880 	TInt badDescriptors = 0;
       
   881 
       
   882 	for (i=0;i<100;i++)
       
   883 		{
       
   884 		TInt descAddress = (TInt) (c.Base() + (Random(seed) % 0x1000));
       
   885 		TInt descSize=Random(seed)%0x1000;
       
   886 
       
   887 		TUint* descData = (TUint*)&ptr;
       
   888 		descData[0] = 0x20000000 + descSize;
       
   889 		descData[1] = descSize;
       
   890 		descData[2] = descAddress;
       
   891 
       
   892 		r = Session.Send(ETestBadClientDescRead,TIpcArgs(&ptr));
       
   893 		switch (r)
       
   894 			{
       
   895 			case KErrNone:			noErrors++;break;
       
   896 			case KErrBadDescriptor: badDescriptors++;break;
       
   897 			default:				test.Printf(_L("Error: %d returned"),r);
       
   898 			}
       
   899 
       
   900 		r = Session.Send(ETestBadClientDescWrite,TIpcArgs(&ptr));
       
   901 		switch (r)
       
   902 			{
       
   903 			case KErrNone:			noErrors++;break;
       
   904 			case KErrBadDescriptor: badDescriptors++;break;
       
   905 			default:				test.Printf(_L("Error: %d returned"),r);
       
   906 			}
       
   907 		}
       
   908 	test.Printf(_L("KErrNoError: %d  KErrBadDescriptor: %d"),noErrors, badDescriptors);
       
   909 	}
       
   910 
       
   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
       
   913 	{
       
   914 	TPtr8 ptr (c.Base(),0x1000,0x1000);
       
   915 	TInt i;
       
   916 	for (i=0;i<=7;i++)
       
   917 		{
       
   918 		r = Session.Send(ETestBadServerDescRead | (i<<28),TIpcArgs(&ptr));
       
   919 		test(r==KErrServerTerminated);
       
   920 		RestartTestServer();
       
   921 		r = Session.Send(ETestBadServerDescWrite | (i<<28),TIpcArgs(&ptr));
       
   922 		test(r==KErrServerTerminated);
       
   923 		User::WaitForRequest(SvrStat);
       
   924 		RestartTestServer();
       
   925 		}
       
   926 	}
       
   927 
       
   928 	c.Close();
       
   929 	}
       
   930 	test.End();
       
   931 	}
       
   932 
       
   933 
       
   934 
       
   935 void TestSubSessions()
       
   936 	{
       
   937 	const TInt KSubSessionHandle = 0x87654321;
       
   938 	RTestSubSession sub;
       
   939 	TInt r;
       
   940 
       
   941 	test.Start(_L("Creating a subsession with no arguments"));
       
   942 	r = sub.CreateSubSession(Session,ETestCreateSubSession);	
       
   943 	test(r==KErrNone);
       
   944 	test(((TInt*)&sub)[0] == (*(TInt*)&Session | CObjectIx::ENoClose));  // check sub.iSession
       
   945 
       
   946 	test.Next(_L("Creating a subsession with arguments"));
       
   947 	r = sub.CreateSubSession(Session,ETestCreateSubSession,TIpcArgs(KSubSessionHandle));	
       
   948 	test(r==KErrNone);
       
   949 	test(((TInt*)&sub)[0] == (*(TInt*)&Session | CObjectIx::ENoClose));  // check sub.iSession
       
   950 
       
   951 	test(((TInt*)&sub)[1]==KSubSessionHandle);   // check sub.iSubSessionHandle
       
   952 
       
   953 	test.Next(_L("Sending message with arguments"));
       
   954 	TInt reply[4] = {0};
       
   955 	TPtr8 replyDes((TUint8*)&reply,sizeof(reply));
       
   956 	r = sub.Send(ETestEchoArgs,TIpcArgs(&replyDes,123,456,789));
       
   957 	test(r==KErrNone);
       
   958 	test(reply[0]==(TInt)&replyDes);
       
   959 	test(reply[1]==123);
       
   960 	test(reply[2]==456);
       
   961 	test(reply[3]==KSubSessionHandle);
       
   962 
       
   963 	test.Next(_L("Sending empty message"));
       
   964 	r = sub.Send(ETestEmptySubSessionMessage);
       
   965 	test(r==KSubSessionHandle);
       
   966 
       
   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);
       
   973 
       
   974 	test.End();
       
   975 	}
       
   976 
       
   977 
       
   978 void TestProtectedServers()
       
   979 	{
       
   980 	RTestProcess process;
       
   981 	TRequestStatus logonStatus;
       
   982 	
       
   983 	test.Start(_L("Trying to create a protected server without KCapabilityProtServ"));
       
   984 	process.Create(~(1u<<ECapabilityProtServ),ETestProcessCreateProtectedServer);
       
   985 	process.Logon(logonStatus);
       
   986 	process.Resume();
       
   987 	User::WaitForRequest(logonStatus);
       
   988 	if(PlatSec::IsCapabilityEnforced(ECapabilityProtServ))
       
   989 		{
       
   990 		test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
       
   991 		test(logonStatus==EPlatformSecurityTrap);
       
   992 		}
       
   993 	else
       
   994 		{
       
   995 		test(process.ExitType()==EExitKill);
       
   996 		test(logonStatus==KErrNone);
       
   997 		}
       
   998 	CLOSE_AND_WAIT(process);
       
   999 
       
  1000 	test.Next(_L("Trying to create a protected server with KCapabilityProtServ"));
       
  1001 	process.Create(1<<ECapabilityProtServ,ETestProcessCreateProtectedServer);
       
  1002 	process.Logon(logonStatus);
       
  1003 	process.Resume();
       
  1004 	User::WaitForRequest(logonStatus);
       
  1005 	test(process.ExitType()==EExitKill);
       
  1006 	test(logonStatus==KErrNone);
       
  1007 	CLOSE_AND_WAIT(process);
       
  1008 
       
  1009 	test.End();
       
  1010 	}
       
  1011 
       
  1012 
       
  1013 void TestIdentifiedServers()
       
  1014 	{
       
  1015 	RTestSession session;
       
  1016 	TInt r;
       
  1017 	
       
  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));
       
  1022 	session.Close();
       
  1023 
       
  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);
       
  1027 	test(r==KErrNone);
       
  1028 	session.Close();
       
  1029 
       
  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));
       
  1034 	session.Close();
       
  1035 
       
  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);
       
  1039 	test(r==KErrNone);
       
  1040 	session.Close();
       
  1041 
       
  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));
       
  1046 	session.Close();
       
  1047 
       
  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);
       
  1051 	test(r==KErrNone);
       
  1052 	session.Close();
       
  1053 
       
  1054 	test.Next(_L("Test connecting to a server without specifying a policy"));
       
  1055 	r=session.Connect(0);
       
  1056 	test(r==KErrNone);
       
  1057 	session.Close();
       
  1058 
       
  1059 	test.End();
       
  1060 	}
       
  1061 
       
  1062 
       
  1063 
       
  1064 #include <e32panic.h>
       
  1065 
       
  1066 TInt TestThreadAsyncConnect(TAny*)
       
  1067 	{
       
  1068 	RTestSession session;
       
  1069 	TRequestStatus status;
       
  1070 	TInt r=session.Connect(TVersion(1,1,1),&status);
       
  1071 	if(r!=KErrNone)
       
  1072 		return r;
       
  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
       
  1079 	}
       
  1080 
       
  1081 void TestAsynchronousConnect()
       
  1082 	{
       
  1083 	RTestSession session;
       
  1084 	TRequestStatus status;
       
  1085 	TInt r;
       
  1086 	
       
  1087 	test.Start(_L("Test successful asynchronous connect"));
       
  1088 	r=session.Connect(TVersion(0,0,0),&status);
       
  1089 	test(r==KErrNone);
       
  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);
       
  1094 	session.Close();
       
  1095 
       
  1096 	test.Next(_L("Test unsuccessful asynchronous connect"));
       
  1097 	r=session.Connect(TVersion(1,1,1),&status);
       
  1098 	test(r==KErrNone);
       
  1099 	test(status==KRequestPending);   // server can't have created session yet because we have higher priority
       
  1100 	User::WaitForRequest(status);
       
  1101 	test(status==KErrNotSupported);
       
  1102 	session.Close();
       
  1103 
       
  1104 	test.Next(_L("Test using unsuccessful asynchronous connect"));
       
  1105 	RTestThread thread;
       
  1106 	thread.Create(TestThreadAsyncConnect,0);
       
  1107 	TRequestStatus logon;
       
  1108 	thread.Logon(logon);
       
  1109 	User::SetJustInTime(EFalse);
       
  1110 	thread.Resume();
       
  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);
       
  1117 	thread.Close();
       
  1118 
       
  1119 	test.End();
       
  1120 	}
       
  1121 
       
  1122 
       
  1123 
       
  1124 TInt DoTestProcess(TInt aTestNum,TInt aArg1,TInt aArg2)
       
  1125 	{
       
  1126 	(void)aArg1;
       
  1127 	(void)aArg2;
       
  1128 	switch(aTestNum)
       
  1129 		{
       
  1130 
       
  1131 	case ETestProcessServer:
       
  1132 		RProcess().SetPriority(EPriorityLow);
       
  1133 		return StartServer();
       
  1134 
       
  1135 	case ETestProcessCreateProtectedServer:
       
  1136 		{
       
  1137 		test.Title();
       
  1138 		RServer2 server;
       
  1139 		TInt r = server.CreateGlobal(KProtectedServerName);
       
  1140 		server.Close();
       
  1141 		return r;
       
  1142 		}
       
  1143 
       
  1144 	default:
       
  1145 		User::Panic(_L("T_SSERVER"),1);
       
  1146 		}
       
  1147 
       
  1148 	return KErrNone;
       
  1149 	}
       
  1150 
       
  1151 
       
  1152 
       
  1153 GLDEF_C TInt E32Main()
       
  1154     {
       
  1155 	TBuf16<512> cmd;
       
  1156 	User::CommandLine(cmd);
       
  1157 	if(cmd.Length() && TChar(cmd[0]).IsDigit())
       
  1158 		{
       
  1159 		TInt function = -1;
       
  1160 		TInt arg1 = -1;
       
  1161 		TInt arg2 = -1;
       
  1162 		TLex lex(cmd);
       
  1163 
       
  1164 		lex.Val(function);
       
  1165 		lex.SkipSpace();
       
  1166 		lex.Val(arg1);
       
  1167 		lex.SkipSpace();
       
  1168 		lex.Val(arg2);
       
  1169 		return DoTestProcess(function,arg1,arg2);
       
  1170 		}
       
  1171 
       
  1172 	TInt r;
       
  1173 
       
  1174 	test.Title();
       
  1175 
       
  1176 	test.Start(_L("Testing Server Connect by Handle"));
       
  1177 	TestConnectByHandle();
       
  1178 
       
  1179 	test.Next(_L("Starting test server"));
       
  1180 	RTestProcess server;
       
  1181 	TRequestStatus rendezvous;
       
  1182 	server.Create(0,ETestProcessServer);
       
  1183 	server.Rendezvous(rendezvous);
       
  1184 	server.Resume();
       
  1185 	User::WaitForRequest(rendezvous);
       
  1186 	test(rendezvous==KServerRendezvous);
       
  1187 	server.Close();
       
  1188 
       
  1189 	test.Next(_L("Conecting to test server"));
       
  1190 	test((r=Session.Connect())==KErrNone);
       
  1191 
       
  1192 	test.Next(_L("Test asynchronous server connect"));
       
  1193 	TestAsynchronousConnect();
       
  1194 
       
  1195 	test.Next(_L("Test protected servers"));
       
  1196 	TestProtectedServers();
       
  1197 
       
  1198 	test.Next(_L("Test connecting to identified servers"));
       
  1199 	TestIdentifiedServers();
       
  1200 
       
  1201 	test.Next(_L("Testing SubSessions"));
       
  1202 	TestSubSessions();
       
  1203 
       
  1204 	test.Next(_L("Testing IPC argument checking"));
       
  1205 	TestIpc();
       
  1206 
       
  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);
       
  1211 
       
  1212 	test.Next(_L("Testing session resource counting"));
       
  1213 	test.Start(_L(""));
       
  1214 	DoTest(ETestResourceCountPass,EExitKill,ETestResourceCountPass);
       
  1215 	DoTest(ETestResourceCountFail,EExitPanic,CSession2::ESesFoundResCountHeaven);
       
  1216 	test.End();
       
  1217 
       
  1218 	test.Next(_L("Shutting server down"));
       
  1219 	{
       
  1220 	Session.Send(ETestShutdown,TIpcArgs());
       
  1221 	Session.Close();
       
  1222 	}
       
  1223 	User::WaitForRequest(SvrStat);
       
  1224 
       
  1225 	test.End();
       
  1226 	return(0);
       
  1227     }
       
  1228