kerneltest/e32test/system/t_svr_connect.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2005-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\system\t_svr_connect.cpp
       
    15 // Overview:
       
    16 // Tests correct operation of server when interleaving connect/disconnect/
       
    17 // other messages and creating the user-side session object (setting the
       
    18 // session cookie) in interesting manners.
       
    19 // Tests that clients/servers are panicked when performing illegal operations
       
    20 // w.r.t. server connection, i.e. a client thread may not send more than one
       
    21 // connect message simultaneously, nor may it send another connect message
       
    22 // once a connect message has been successfully completed. Similarly, a server
       
    23 // may not set the cookie twice nor may it set the cookie to be NULL. Also, a
       
    24 // server may only set the cookie from a connect message and from no other.
       
    25 // API Information:
       
    26 // RServer2
       
    27 // Details:
       
    28 // - Test asynchronous server connect in various ways. Verify results
       
    29 // are as expected.
       
    30 // - Test illegal client/server behaviour. Verify threads are panicked,
       
    31 // as expected.
       
    32 // Platforms/Drives/Compatibility:
       
    33 // All.
       
    34 // Assumptions/Requirement/Pre-requisites:
       
    35 // None.
       
    36 // Failures and causes:
       
    37 // Failure of this test would be caused by an incorrect modification of
       
    38 // the internal client/server mechanism in the kernel.
       
    39 // Base Port information:
       
    40 // This is a unit test of the client/server mechanism within the kernel.
       
    41 // It should be invariant to base ports. If the kernel proper has not been
       
    42 // modified, this test should not fail with any new base port.
       
    43 // 
       
    44 //
       
    45 
       
    46 #include <e32test.h>
       
    47 #include "../misc/int_svr_calls.h"
       
    48 #include <e32kpan.h>
       
    49 
       
    50 _LIT(KTestName, "t_svr_connect");
       
    51 LOCAL_D RTest test(KTestName);
       
    52 LOCAL_D RTest test_client(KTestName);
       
    53 LOCAL_D RTest test_server(KTestName);
       
    54 
       
    55 _LIT(KServerName, "t_svr_connect");
       
    56 _LIT8(KServerName8, "t_svr_connect");
       
    57 _LIT(KMainThread, "main thread");
       
    58 _LIT(KServerThread, "server thread");
       
    59 _LIT(KClientThread, "client thread");
       
    60 _LIT(KClientThread2, "client thread2");
       
    61 _LIT(KClientThread3, "client thread3");
       
    62 
       
    63 class RTestServer : public RSessionBase
       
    64 	{
       
    65 public:
       
    66 	inline static TInt CreateSession(TInt aMsgSlots) { return SessionCreate(KServerName8,aMsgSlots,NULL,EIpcSession_Sharable); }
       
    67 	};
       
    68 
       
    69 // Messages to send:
       
    70 //
       
    71 //		connect,				1
       
    72 //		a.n.other,				2
       
    73 //		disconnect				3
       
    74 //
       
    75 // Things to do in the server:
       
    76 //
       
    77 //		recieve connect			4
       
    78 //		set cookie				5
       
    79 //		complete connect		6
       
    80 //		receive a.n.other		7
       
    81 //		complete a.n.other		8
       
    82 //		receive disconnect		9
       
    83 //		complete disconnect		10
       
    84 //		
       
    85 
       
    86 enum TMsgType
       
    87 	{
       
    88 	EConnect = -1,
       
    89 	EDisConnect = -2,
       
    90 	EANOther = 1,
       
    91 	EANOther2 = 2,
       
    92 	EMsgCount = 4
       
    93 	};
       
    94 
       
    95 enum TServerControl
       
    96 	{
       
    97 	EReceiveMsg = 0,
       
    98 	EReceiveBlocked = 1,
       
    99 	EWaitForReceive = 2,
       
   100 	ESetCookie = 3,
       
   101 	ESetNullCookie = 4,
       
   102 	ECompleteMsg = 5,
       
   103 	EServerDie = 6
       
   104 	};
       
   105 
       
   106 enum TClientControl
       
   107 	{
       
   108 	ESendMsg = 0,
       
   109 	EWaitMsg = 1,
       
   110 	ECreateSession = 2,
       
   111 	EClientDie = 3,
       
   112 	ESetCritical = 4
       
   113 	};
       
   114 
       
   115 static TMsgType TheMsgType;
       
   116 static TServerControl TheServerControl;
       
   117 static TClientControl TheClientControl;
       
   118 static RSemaphore ServerSemaphore;
       
   119 static RSemaphore ClientSemaphore;
       
   120 static RSemaphore TaskCompletionSemaphore;
       
   121 static RMessage2 Msgs[EMsgCount];
       
   122 static RServer2 Server;
       
   123 static RThread ServerThread;
       
   124 static RThread ClientThread;
       
   125 static RTestServer ServerHandle;
       
   126 static TRequestStatus ConnectStatus;
       
   127 static TRequestStatus ANOtherStatus;
       
   128 static TRequestStatus ANOther2Status;
       
   129 static TRequestStatus ServerReceiveStatus;
       
   130 static TInt TheNumberOfMsgSlots;
       
   131 static TInt ErrorExpected;
       
   132 static User::TCritical CriticalLevel;
       
   133 
       
   134 
       
   135 static const TAny* const KSessionCookie = (const TAny*)0x12345;
       
   136 static TRequestStatus* volatile KNullReference = 0;
       
   137 
       
   138 void DoServerAction();
       
   139 void DoClientAction();
       
   140 TInt Index();
       
   141 
       
   142 TInt TestThreadServer(TAny*)
       
   143 	{
       
   144 	test_server(Server.CreateGlobal(KServerName) == KErrNone);
       
   145 	RThread::Rendezvous(KErrNone);
       
   146 
       
   147 	for (;;)
       
   148 		{
       
   149 		ServerSemaphore.Wait();
       
   150 		DoServerAction();
       
   151 		TaskCompletionSemaphore.Signal();
       
   152 		}
       
   153 	}
       
   154 
       
   155 TInt Index()
       
   156 	{
       
   157 	switch (TheMsgType)
       
   158 		{
       
   159 	case EConnect:
       
   160 		return 0;
       
   161 	case EDisConnect:
       
   162 		return 3;
       
   163 	case EANOther:
       
   164 		return 1;
       
   165 	case EANOther2:
       
   166 		return 2;
       
   167 	default:
       
   168 		return -1;
       
   169 		};
       
   170 	}
       
   171 
       
   172 TRequestStatus& RequestStatus()
       
   173 	{
       
   174 	switch (TheMsgType)
       
   175 		{
       
   176 	case EConnect:
       
   177 		return ConnectStatus;
       
   178 	case EANOther:
       
   179 		return ANOtherStatus;
       
   180 	case EANOther2:
       
   181 		return ANOther2Status;
       
   182 	default:
       
   183 		User::Invariant();
       
   184 		};
       
   185 	return *(TRequestStatus*)KNullReference;
       
   186 	}
       
   187 
       
   188 void DoServerAction()
       
   189 	{
       
   190 	switch (TheServerControl)
       
   191 		{
       
   192 	case EReceiveMsg:
       
   193 		{
       
   194 		RMessage2& msg = Msgs[Index()];
       
   195 		Server.Receive(msg);
       
   196 		test_server(msg.Function() == TheMsgType);
       
   197 		}
       
   198 		break;
       
   199 	case EReceiveBlocked:
       
   200 		{
       
   201 		RMessage2& msg = Msgs[Index()];
       
   202 		Server.Receive(msg, ServerReceiveStatus);
       
   203 		test_server(ServerReceiveStatus.Int() == KRequestPending);
       
   204 		}
       
   205 		break;
       
   206 	case EWaitForReceive:
       
   207 		{
       
   208 		User::WaitForRequest(ServerReceiveStatus);
       
   209 		test_server(ServerReceiveStatus.Int() == KErrNone);
       
   210 		test_server(Msgs[Index()].Function() == TheMsgType);
       
   211 		}
       
   212 		break;
       
   213 	case ESetCookie:
       
   214 		{
       
   215 		SetSessionPtr(Msgs[Index()].Handle(), KSessionCookie);
       
   216 		}
       
   217 		break;
       
   218 	case ESetNullCookie:
       
   219 		{
       
   220 		SetSessionPtr(Msgs[Index()].Handle(), NULL);
       
   221 		}
       
   222 		break;
       
   223 	case ECompleteMsg:
       
   224 		{
       
   225 		Msgs[Index()].Complete(KErrNone);
       
   226 		}
       
   227 		break;
       
   228 	case EServerDie:
       
   229 		{
       
   230 		User::Exit(0);
       
   231 		}
       
   232 		break;
       
   233 	default:
       
   234 		{
       
   235 		}
       
   236 		break;
       
   237 		};
       
   238 	}
       
   239 
       
   240 void StartServer()
       
   241 	{
       
   242 	ServerThread.Create(KServerThread, TestThreadServer, KDefaultStackSize, 4096, 4096, NULL);
       
   243 
       
   244 	TRequestStatus started;
       
   245 	ServerThread.Rendezvous(started);
       
   246 
       
   247 	ServerThread.Resume();
       
   248 	User::WaitForRequest(started);
       
   249 
       
   250 	test(started.Int() == KErrNone);
       
   251 	}
       
   252 
       
   253 void StopServer()
       
   254 	{
       
   255 	TRequestStatus logon;
       
   256 	ServerThread.Logon(logon);
       
   257 
       
   258 	TheServerControl = EServerDie;
       
   259 	ServerSemaphore.Signal();
       
   260 
       
   261 	User::WaitForRequest(logon);
       
   262 	test(logon.Int() == KErrNone);
       
   263 
       
   264 	CLOSE_AND_WAIT(ServerThread);
       
   265 	}
       
   266 
       
   267 TInt TestThreadClient(TAny*)
       
   268 	{
       
   269 	RThread::Rendezvous(KErrNone);
       
   270 
       
   271 	for (;;)
       
   272 		{
       
   273 		ClientSemaphore.Wait();
       
   274 		DoClientAction();
       
   275 		TaskCompletionSemaphore.Signal();
       
   276 		}
       
   277 	}
       
   278 
       
   279 void DoClientAction()
       
   280 	{
       
   281 	switch (TheClientControl)
       
   282 		{
       
   283 	case ESendMsg:
       
   284 		{
       
   285 		if (TheMsgType == EDisConnect)
       
   286 			ServerHandle.Close();
       
   287 		else
       
   288 			SessionSend(ServerHandle.Handle(), TheMsgType, NULL, &RequestStatus());
       
   289 		}
       
   290 		break;
       
   291 	case EWaitMsg:
       
   292 		{
       
   293 		User::WaitForRequest(RequestStatus());
       
   294 		}
       
   295 		break;
       
   296 	case ECreateSession:
       
   297 		{
       
   298 		TInt err = ServerHandle.SetReturnedHandle(RTestServer::CreateSession(TheNumberOfMsgSlots));
       
   299 		
       
   300 		if (err != ErrorExpected)
       
   301 			{
       
   302 			test_client.Printf(_L("Error returned = %d\n"),err);
       
   303 			test_client(EFalse,__LINE__);
       
   304 			}
       
   305 		}
       
   306 		break;
       
   307 	case EClientDie:
       
   308 		{
       
   309 		User::SetCritical(User::ENotCritical);
       
   310 		User::Exit(0);
       
   311 		}
       
   312 		break;
       
   313 	case ESetCritical:
       
   314 		{
       
   315 		User::SetCritical(CriticalLevel);
       
   316 		break;
       
   317 		}
       
   318 	default:
       
   319 		{
       
   320 		}
       
   321 		break;
       
   322 		};
       
   323 	}
       
   324 
       
   325 // I'm lazy and I haven't completed all the IPC the client thread does,
       
   326 // so even when the client thread panics, the DObject is still kept alive
       
   327 // until the server goes away. Therefore if I want another client I rename.
       
   328 void StartClient(const TDesC& aName)
       
   329 	{
       
   330 	ClientThread.Create(aName, TestThreadClient, KDefaultStackSize, 4096, 4096, NULL);
       
   331 
       
   332 	TRequestStatus started;
       
   333 	ClientThread.Rendezvous(started);
       
   334 
       
   335 	ClientThread.Resume();
       
   336 	User::WaitForRequest(started);
       
   337 
       
   338 	test(started.Int() == KErrNone);
       
   339 	}
       
   340 
       
   341 void StartClient()
       
   342 	{
       
   343 	StartClient(KClientThread);
       
   344 	}
       
   345 
       
   346 void StopClient()
       
   347 	{
       
   348 	TRequestStatus logon;
       
   349 	ClientThread.Logon(logon);
       
   350 
       
   351 	TheClientControl = EClientDie;
       
   352 	ClientSemaphore.Signal();
       
   353 
       
   354 	User::WaitForRequest(logon);
       
   355 	test(logon.Int() == KErrNone);
       
   356 
       
   357 	CLOSE_AND_WAIT(ClientThread);
       
   358 	}
       
   359 
       
   360 void CreateSemaphores()
       
   361 	{
       
   362 	TInt err = ServerSemaphore.CreateLocal(0);
       
   363 	test(err == KErrNone);
       
   364 	err = ClientSemaphore.CreateLocal(0);
       
   365 	test(err == KErrNone);
       
   366 	err = TaskCompletionSemaphore.CreateLocal(0);
       
   367 	test(err == KErrNone);
       
   368 	}
       
   369 
       
   370 void CloseSemaphores()
       
   371 	{
       
   372 	ServerSemaphore.Close();
       
   373 	ClientSemaphore.Close();
       
   374 	TaskCompletionSemaphore.Close();
       
   375 	}
       
   376 
       
   377 void CreateSession(TInt aErrorExpected=KErrNone, TInt aMsgSlots=-1)
       
   378 	{
       
   379 	TheClientControl = ECreateSession;
       
   380 	TheNumberOfMsgSlots = aMsgSlots;
       
   381 	ErrorExpected=aErrorExpected;
       
   382 	ClientSemaphore.Signal();
       
   383 	TaskCompletionSemaphore.Wait();
       
   384 	}
       
   385 
       
   386 void SendMsg(TMsgType aType)
       
   387 	{
       
   388 	TheClientControl = ESendMsg;
       
   389 	TheMsgType = aType;
       
   390 	ClientSemaphore.Signal();
       
   391 	TaskCompletionSemaphore.Wait();
       
   392 	}
       
   393 
       
   394 void SendMsg_NoWait(TMsgType aType)
       
   395 	{
       
   396 	TheClientControl = ESendMsg;
       
   397 	TheMsgType = aType;
       
   398 	ClientSemaphore.Signal();
       
   399 	}
       
   400 
       
   401 void WaitMsg(TMsgType aType)
       
   402 	{
       
   403 	TheClientControl = EWaitMsg;
       
   404 	TheMsgType = aType;
       
   405 	ClientSemaphore.Signal();
       
   406 	TaskCompletionSemaphore.Wait();
       
   407 	}
       
   408 
       
   409 void ReceiveBlocked(TMsgType aType)
       
   410 	{
       
   411 	TheServerControl = EReceiveBlocked;
       
   412 	TheMsgType = aType;
       
   413 	ServerSemaphore.Signal();
       
   414 	TaskCompletionSemaphore.Wait();
       
   415 	}
       
   416 
       
   417 void WaitForReceive(TMsgType aType)
       
   418 	{
       
   419 	TheServerControl = EWaitForReceive;
       
   420 	TheMsgType = aType;
       
   421 	ServerSemaphore.Signal();
       
   422 	TaskCompletionSemaphore.Wait();
       
   423 	}
       
   424 
       
   425 void ReceiveMsg(TMsgType aType)
       
   426 	{
       
   427 	TheServerControl = EReceiveMsg;
       
   428 	TheMsgType = aType;
       
   429 	ServerSemaphore.Signal();
       
   430 	TaskCompletionSemaphore.Wait();
       
   431 	}
       
   432 
       
   433 void CompleteMsg(TMsgType aType)
       
   434 	{
       
   435 	TheServerControl = ECompleteMsg;
       
   436 	TheMsgType = aType;
       
   437 	ServerSemaphore.Signal();
       
   438 	TaskCompletionSemaphore.Wait();
       
   439 	}
       
   440 
       
   441 void SetCookie()
       
   442 	{
       
   443 	TheServerControl = ESetCookie;
       
   444 	TheMsgType = EConnect;
       
   445 	ServerSemaphore.Signal();
       
   446 	TaskCompletionSemaphore.Wait();
       
   447 	}
       
   448 
       
   449 void SetCookie_NoWait()
       
   450 	{
       
   451 	TheServerControl = ESetCookie;
       
   452 	TheMsgType = EConnect;
       
   453 	ServerSemaphore.Signal();
       
   454 	}
       
   455 
       
   456 void SetNullCookie()
       
   457 	{
       
   458 	TheServerControl = ESetNullCookie;
       
   459 	TheMsgType = EConnect;
       
   460 	ServerSemaphore.Signal();
       
   461 	}
       
   462 
       
   463 void SetBadCookie(TMsgType aType)
       
   464 	{
       
   465 	TheServerControl = ESetCookie;
       
   466 	test(aType != EConnect);
       
   467 	TheMsgType = aType;
       
   468 	ServerSemaphore.Signal();
       
   469 	}
       
   470 void SetClientCritical(User::TCritical aCritical)
       
   471 	{	
       
   472 	TheClientControl = ESetCritical;
       
   473 	CriticalLevel=aCritical;
       
   474 	ClientSemaphore.Signal();
       
   475 	TaskCompletionSemaphore.Wait();
       
   476 	}
       
   477 	
       
   478 void Test1()
       
   479 	{
       
   480 	test.Next(_L("Create session with test server"));
       
   481 	CreateSession();
       
   482 	test.Next(_L("Send connect message"));
       
   483 	SendMsg(EConnect);
       
   484 	test.Next(_L("Send A.N.Other message"));
       
   485 	SendMsg(EANOther);
       
   486 	test.Next(_L("Sending disconnect message"));
       
   487 	SendMsg(EDisConnect);
       
   488 	test.Next(_L("Receive A.N.Other message"));
       
   489 	ReceiveMsg(EANOther);
       
   490 	test(Msgs[Index()].Session() == NULL);
       
   491 	test.Next(_L("Receive disconnect message"));
       
   492 	ReceiveMsg(EDisConnect);
       
   493 	test.Next(_L("Check the session has gone"));
       
   494 	test(Msgs[Index()].Session() == NULL);
       
   495 	test.Next(_L("Set up receive for next test"));
       
   496 	ReceiveBlocked(EConnect);
       
   497 	}
       
   498 
       
   499 void Test2()
       
   500 	{
       
   501 	test.Next(_L("Create session with test server"));
       
   502 	CreateSession();
       
   503 	test.Next(_L("Send connect message"));
       
   504 	SendMsg(EConnect);
       
   505 	test.Next(_L("Send A.N.Other message"));
       
   506 	SendMsg(EANOther);
       
   507 	test.Next(_L("Receive connect message"));
       
   508 	WaitForReceive(EConnect);
       
   509 	test(Msgs[Index()].Session() == NULL);
       
   510 	test.Next(_L("Receive A.N.Other message"));
       
   511 	ReceiveMsg(EANOther);
       
   512 	test(Msgs[Index()].Session() == NULL);
       
   513 	test.Next(_L("Sending disconnect message"));
       
   514 	SendMsg(EDisConnect);
       
   515 	test.Next(_L("Await disconnect message"));
       
   516 	ReceiveBlocked(EDisConnect);
       
   517 	test.Next(_L("Set cookie"));
       
   518 	SetCookie();
       
   519 	test.Next(_L("Check disconnect message received"));
       
   520 	WaitForReceive(EDisConnect);
       
   521 	test(Msgs[Index()].Session() == KSessionCookie);
       
   522 	test.Next(_L("Complete connect message"));
       
   523 	CompleteMsg(EConnect);
       
   524 	}
       
   525 
       
   526 void Test2a()
       
   527 	{
       
   528 	CreateSession();
       
   529 	SendMsg(EConnect);
       
   530 	SendMsg(EANOther);
       
   531 	ReceiveMsg(EConnect);
       
   532 	test(Msgs[Index()].Session() == NULL);
       
   533 	ReceiveMsg(EANOther);
       
   534 	test(Msgs[Index()].Session() == NULL);
       
   535 	SendMsg(EDisConnect);
       
   536 	ReceiveBlocked(EDisConnect);
       
   537 	CompleteMsg(EConnect);
       
   538 	WaitForReceive(EDisConnect);
       
   539 	test(Msgs[Index()].Session() == NULL);
       
   540 	}
       
   541 
       
   542 void Test2b()
       
   543 	{
       
   544 	CreateSession();
       
   545 	SendMsg(EConnect);
       
   546 	SendMsg(EANOther);
       
   547 	ReceiveMsg(EConnect);
       
   548 	test(Msgs[Index()].Session() == NULL);
       
   549 	SetCookie();
       
   550 	ReceiveMsg(EANOther);
       
   551 	test(Msgs[Index()].Session() == KSessionCookie);
       
   552 	SendMsg(EDisConnect);
       
   553 	ReceiveMsg(EDisConnect);
       
   554 	test(Msgs[Index()].Session() == KSessionCookie);
       
   555 	}
       
   556 
       
   557 void Test3()
       
   558 	{
       
   559 	CreateSession();
       
   560 	SendMsg(EConnect);
       
   561 	ReceiveMsg(EConnect);
       
   562 	test(Msgs[Index()].Session() == NULL);
       
   563 	SendMsg(EANOther);
       
   564 	ReceiveMsg(EANOther);
       
   565 	test(Msgs[Index()].Session() == NULL);
       
   566 	SendMsg(EDisConnect);
       
   567 	ReceiveBlocked(EDisConnect);
       
   568 	SetCookie();
       
   569 	WaitForReceive(EDisConnect);
       
   570 	test(Msgs[Index()].Session() == KSessionCookie);
       
   571 	CompleteMsg(EConnect);
       
   572 	}
       
   573 
       
   574 void Test3a()
       
   575 	{
       
   576 	CreateSession();
       
   577 	SendMsg(EConnect);
       
   578 	ReceiveMsg(EConnect);
       
   579 	test(Msgs[Index()].Session() == NULL);
       
   580 	SendMsg(EANOther);
       
   581 	ReceiveMsg(EANOther);
       
   582 	test(Msgs[Index()].Session() == NULL);
       
   583 	SendMsg(EDisConnect);
       
   584 	ReceiveBlocked(EDisConnect);
       
   585 	CompleteMsg(EConnect);
       
   586 	WaitForReceive(EDisConnect);
       
   587 	test(Msgs[Index()].Session() == NULL);
       
   588 	}
       
   589 
       
   590 void Test3b()
       
   591 	{
       
   592 	CreateSession();
       
   593 	SendMsg(EConnect);
       
   594 	ReceiveMsg(EConnect);
       
   595 	test(Msgs[Index()].Session() == NULL);
       
   596 	SetCookie();
       
   597 	SendMsg(EANOther);
       
   598 	ReceiveMsg(EANOther);
       
   599 	test(Msgs[Index()].Session() == KSessionCookie);
       
   600 	SendMsg(EDisConnect);
       
   601 	ReceiveMsg(EDisConnect);
       
   602 	test(Msgs[Index()].Session() == KSessionCookie);
       
   603 	}
       
   604 
       
   605 void Test4()
       
   606 	{
       
   607 	CreateSession();
       
   608 	SendMsg(EANOther);
       
   609 	SendMsg(EConnect);
       
   610 	SendMsg(EANOther2);
       
   611 	SendMsg(EDisConnect);
       
   612 	ReceiveMsg(EANOther);
       
   613 	test(Msgs[Index()].Session() == NULL);
       
   614 	ReceiveMsg(EANOther2);
       
   615 	test(Msgs[Index()].Session() == NULL);
       
   616 	ReceiveMsg(EDisConnect);
       
   617 	test(Msgs[Index()].Session() == NULL);
       
   618 	}
       
   619 
       
   620 void Test5()
       
   621 	{
       
   622 	CreateSession();
       
   623 	SendMsg(EANOther);
       
   624 	SendMsg(EConnect);
       
   625 	SendMsg(EANOther2);
       
   626 	ReceiveMsg(EANOther);
       
   627 	test(Msgs[Index()].Session() == NULL);
       
   628 	SendMsg(EDisConnect);
       
   629 	ReceiveMsg(EANOther2);
       
   630 	test(Msgs[Index()].Session() == NULL);
       
   631 	ReceiveMsg(EDisConnect);
       
   632 	test(Msgs[Index()].Session() == NULL);
       
   633 	ReceiveBlocked(EANOther);
       
   634 	}
       
   635 
       
   636 void Test6()
       
   637 	{
       
   638 	CreateSession();
       
   639 	SendMsg(EANOther);
       
   640 	SendMsg(EConnect);
       
   641 	SendMsg(EANOther2);
       
   642 	WaitForReceive(EANOther);
       
   643 	test(Msgs[Index()].Session() == NULL);
       
   644 	ReceiveMsg(EConnect);
       
   645 	test(Msgs[Index()].Session() == NULL);
       
   646 	ReceiveMsg(EANOther2);
       
   647 	test(Msgs[Index()].Session() == NULL);
       
   648 	SendMsg(EDisConnect);
       
   649 	ReceiveBlocked(EDisConnect);
       
   650 	SetCookie();
       
   651 	WaitForReceive(EDisConnect);
       
   652 	test(Msgs[Index()].Session() == KSessionCookie);
       
   653 	CompleteMsg(EConnect);
       
   654 	}
       
   655 
       
   656 void Test6a()
       
   657 	{
       
   658 	CreateSession();
       
   659 	SendMsg(EANOther);
       
   660 	SendMsg(EConnect);
       
   661 	SendMsg(EANOther2);
       
   662 	ReceiveMsg(EANOther);
       
   663 	test(Msgs[Index()].Session() == NULL);
       
   664 	ReceiveMsg(EConnect);
       
   665 	test(Msgs[Index()].Session() == NULL);
       
   666 	ReceiveMsg(EANOther2);
       
   667 	test(Msgs[Index()].Session() == NULL);
       
   668 	SendMsg(EDisConnect);
       
   669 	ReceiveBlocked(EDisConnect);
       
   670 	CompleteMsg(EConnect);
       
   671 	WaitForReceive(EDisConnect);
       
   672 	test(Msgs[Index()].Session() == NULL);
       
   673 	}
       
   674 
       
   675 void Test6b()
       
   676 	{
       
   677 	CreateSession();
       
   678 	SendMsg(EANOther);
       
   679 	SendMsg(EConnect);
       
   680 	SendMsg(EANOther2);
       
   681 	ReceiveMsg(EANOther);
       
   682 	test(Msgs[Index()].Session() == NULL);
       
   683 	ReceiveMsg(EConnect);
       
   684 	test(Msgs[Index()].Session() == NULL);
       
   685 	SetCookie();
       
   686 	ReceiveMsg(EANOther2);
       
   687 	test(Msgs[Index()].Session() == KSessionCookie);
       
   688 	SendMsg(EDisConnect);
       
   689 	ReceiveMsg(EDisConnect);
       
   690 	test(Msgs[Index()].Session() == KSessionCookie);
       
   691 	}
       
   692 
       
   693 void Test7()
       
   694 	{
       
   695 	CreateSession();
       
   696 	SendMsg(EANOther);
       
   697 	SendMsg(EConnect);
       
   698 	ReceiveMsg(EANOther);
       
   699 	test(Msgs[Index()].Session() == NULL);
       
   700 	SendMsg(EANOther2);
       
   701 	ReceiveMsg(EConnect);
       
   702 	test(Msgs[Index()].Session() == NULL);
       
   703 	SetCookie();
       
   704 	ReceiveMsg(EANOther2);
       
   705 	test(Msgs[Index()].Session() == KSessionCookie);
       
   706 	SendMsg(EDisConnect);
       
   707 	ReceiveMsg(EDisConnect);
       
   708 	test(Msgs[Index()].Session() == KSessionCookie);
       
   709 	}
       
   710 
       
   711 void Test8()
       
   712 	{
       
   713 	CreateSession();
       
   714 	SendMsg(EANOther);
       
   715 	SendMsg(EConnect);
       
   716 	ReceiveMsg(EANOther);
       
   717 	test(Msgs[Index()].Session() == NULL);
       
   718 	ReceiveMsg(EConnect);
       
   719 	test(Msgs[Index()].Session() == NULL);
       
   720 	SetCookie();
       
   721 	SendMsg(EANOther2);
       
   722 	ReceiveMsg(EANOther2);
       
   723 	test(Msgs[Index()].Session() == KSessionCookie);
       
   724 	SendMsg(EDisConnect);
       
   725 	ReceiveMsg(EDisConnect);
       
   726 	test(Msgs[Index()].Session() == KSessionCookie);
       
   727 	}
       
   728 
       
   729 void Test9()
       
   730 	{
       
   731 	CreateSession();
       
   732 	SendMsg(EANOther);
       
   733 	ReceiveMsg(EANOther);
       
   734 	test(Msgs[Index()].Session() == NULL);
       
   735 	SendMsg(EConnect);
       
   736 	ReceiveMsg(EConnect);
       
   737 	test(Msgs[Index()].Session() == NULL);
       
   738 	SetCookie();
       
   739 	SendMsg(EANOther2);
       
   740 	ReceiveMsg(EANOther2);
       
   741 	test(Msgs[Index()].Session() == KSessionCookie);
       
   742 	SendMsg(EDisConnect);
       
   743 	ReceiveMsg(EDisConnect);
       
   744 	test(Msgs[Index()].Session() == KSessionCookie);
       
   745 	}
       
   746 
       
   747 void Test10()
       
   748 	{
       
   749 	// Try connecting with too many message slots
       
   750 	// (Check for DEF091903 regression)
       
   751 	CreateSession(KErrArgument, 1000);
       
   752 	}
       
   753 
       
   754 _LIT(KKernExec, "KERN-EXEC");
       
   755 
       
   756 void CheckClientDeath(TInt aReason)
       
   757 	{
       
   758 	TRequestStatus logon;
       
   759 
       
   760 	ClientThread.Logon(logon);
       
   761 	User::WaitForRequest(logon);
       
   762 
       
   763 	test(ClientThread.ExitType() == EExitPanic);
       
   764 	test(ClientThread.ExitCategory() == KKernExec);
       
   765 	test(ClientThread.ExitReason() == aReason);
       
   766 
       
   767 	ClientThread.Close();
       
   768 	ClientThread.SetHandle(KNullHandle);
       
   769 	}
       
   770 
       
   771 void CheckServerDeath(TInt aReason)
       
   772 	{
       
   773 	TRequestStatus logon;
       
   774 
       
   775 	ServerThread.Logon(logon);
       
   776 	User::WaitForRequest(logon);
       
   777 
       
   778 	test(ServerThread.ExitType() == EExitPanic);
       
   779 	test(ServerThread.ExitCategory() == KKernExec);
       
   780 	test(ServerThread.ExitReason() == aReason);
       
   781 
       
   782 	CLOSE_AND_WAIT(ServerThread);
       
   783 	ServerThread.SetHandle(KNullHandle);
       
   784 	}
       
   785 
       
   786 void TestNaughty()
       
   787 	{
       
   788 	TBool jit = User::JustInTime();
       
   789 	User::SetJustInTime(EFalse);
       
   790 
       
   791 	SetClientCritical(User::ENotCritical);
       
   792 	test.Next(_L("Two connect msgs at once is naughty"));
       
   793 	CreateSession();
       
   794 	SendMsg(EConnect);
       
   795 	ReceiveMsg(EConnect);
       
   796 	SetCookie();
       
   797 	SendMsg_NoWait(EConnect);
       
   798 	CheckClientDeath(ERequestAlreadyPending);
       
   799 	StartClient(KClientThread2);
       
   800 	test.Next(_L("Another connect message after a sucessful connect msg is naughty"));
       
   801 	CreateSession();
       
   802 	SendMsg(EConnect);
       
   803 	ReceiveMsg(EConnect);
       
   804 	SetCookie();
       
   805 	CompleteMsg(EConnect);
       
   806 	SendMsg_NoWait(EConnect);
       
   807 	CheckClientDeath(ESessionAlreadyConnected);
       
   808 	StartClient(KClientThread3);
       
   809 
       
   810 	test.Next(_L("A null session cookie is naughty"));
       
   811 	CreateSession();
       
   812 	SendMsg(EConnect);
       
   813 	ReceiveMsg(EConnect);
       
   814 	SetNullCookie();
       
   815 	CheckServerDeath(ESessionNullCookie);
       
   816 	StartServer();
       
   817 
       
   818 	test.Next(_L("Setting the session cookie twice is naughty"));
       
   819 	CreateSession();
       
   820 	SendMsg(EConnect);
       
   821 	ReceiveMsg(EConnect);
       
   822 	SetCookie();
       
   823 	SetCookie_NoWait();
       
   824 	CheckServerDeath(ESessionCookieAlreadySet);
       
   825 	StartServer();
       
   826 
       
   827 	test.Next(_L("Trying to set the session cookie from a non-connect message is naughty"));
       
   828 	CreateSession();
       
   829 	SendMsg(EConnect);
       
   830 	ReceiveMsg(EConnect);
       
   831 	SendMsg(EANOther);
       
   832 	ReceiveMsg(EANOther);
       
   833 	SetBadCookie(EANOther);
       
   834 	CheckServerDeath(ESessionInvalidCookieMsg);
       
   835 	StartServer();
       
   836 
       
   837 	User::SetJustInTime(jit);
       
   838 	}
       
   839 
       
   840 TInt E32Main()
       
   841     {
       
   842 	User::RenameThread(KMainThread);
       
   843 
       
   844 	__UHEAP_MARK;
       
   845 
       
   846 	test.Title();
       
   847 
       
   848 	test.Start(_L("Creating semaphores"));
       
   849 	CreateSemaphores();
       
   850 
       
   851 	test.Next(_L("Starting test server"));
       
   852 	StartServer();
       
   853 
       
   854 	test.Next(_L("Starting test client"));
       
   855 	StartClient();
       
   856 	SetClientCritical(User::EProcessCritical);
       
   857 	// test combinations of receiving messages with messages sent by the client in the
       
   858 	// correct order (w.r.t to each other, but not necessarily to when messages are received)
       
   859 	test.Next(_L("Sending in order"));
       
   860 	test.Next(_L("1"));
       
   861 	Test1();
       
   862 	test.Next(_L("2"));
       
   863 	Test2();
       
   864 	test.Next(_L("2a"));
       
   865 	Test2a();
       
   866 	test.Next(_L("2b"));
       
   867 	Test2b();
       
   868 	test.Next(_L("3"));
       
   869 	Test3();
       
   870 	test.Next(_L("3a"));
       
   871 	Test3a();
       
   872 	test.Next(_L("3b"));
       
   873 	Test3b();
       
   874 
       
   875 	// test combinations of receiving messages with messages sent by the client in the
       
   876 	// wrong order (w.r.t to each other, but not necessarily to when messages are received)
       
   877 	test.Next(_L("Sending out of order"));
       
   878 	test.Next(_L("4"));
       
   879 	Test4();
       
   880 	test.Next(_L("5"));
       
   881 	Test5();
       
   882 	test.Next(_L("6"));
       
   883 	Test6();
       
   884 	test.Next(_L("6a"));
       
   885 	Test6a();
       
   886 	test.Next(_L("6b"));
       
   887 	Test6b();
       
   888 	test.Next(_L("7"));
       
   889 	Test7();
       
   890 	test.Next(_L("8"));
       
   891 	Test8();
       
   892 	test.Next(_L("9"));
       
   893 	Test9();
       
   894 	test.Next(_L("10"));
       
   895 	Test10();
       
   896 
       
   897 	test.Next(_L("Test other naughty behaviour is trapped"));
       
   898 	TestNaughty();
       
   899 
       
   900 	test.Next(_L("Stopping test client"));
       
   901 	StopClient();
       
   902 
       
   903 	test.Next(_L("Stopping test server"));
       
   904 	StopServer();
       
   905 
       
   906 	test.Next(_L("Closing semaphores"));
       
   907 	CloseSemaphores();
       
   908 
       
   909 	test.End();
       
   910 	test.Close();
       
   911 
       
   912 	__UHEAP_MARKEND;
       
   913 
       
   914 	return KErrNone;
       
   915     }