datacommsserver/esockserver/test/TE_IPCTest/Test05IPCIllegalOperations.cpp
changeset 0 dfb7c4ff071f
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     1 // Copyright (c) 2004-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 "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 // Contains IPC Test 05
       
    15 // 
       
    16 //
       
    17 
       
    18 // EPOC includes
       
    19 #include <e32base.h>
       
    20 #include <es_sock.h>
       
    21 #include <ss_std.h>
       
    22 
       
    23 #include <elements/sd_std.h>
       
    24 
       
    25 #include "Test05IPCIllegalOperations.h"
       
    26 
       
    27 using namespace Den;
       
    28 
       
    29 const TDesC& CTest05IPCIllegalOperations::GetTestName()
       
    30 	{
       
    31 	_LIT(ret,"IPCTest05");
       
    32 	return ret;
       
    33 	}
       
    34 // constructor
       
    35 CTest05IPCIllegalOperations::CTest05IPCIllegalOperations()
       
    36 	{
       
    37     iThreadStackSize = 1024;
       
    38 	}
       
    39 
       
    40 
       
    41 // This is thread that should fail as it attempts to accept
       
    42 // with connected socket
       
    43 TInt CTest05IPCIllegalOperations::fAcceptWithConnectedSocket(TAny* aInput)
       
    44 	{
       
    45 	TInt err;
       
    46 	RSocketServ aSockServer;
       
    47 	TRequestStatus aStatAccept, aStatConnect;
       
    48 	RSocket aNewConn, aServerSock;
       
    49 	RSocket aClientSock;
       
    50 	TSockAddr aAddr;
       
    51     CTest05IPCIllegalOperations* pTestObject =
       
    52 								(CTest05IPCIllegalOperations*)aInput;
       
    53 	
       
    54 	//-------------------substep 01-----------------------------	
       
    55 	// Connect to Socket server, and create client/socket connection 
       
    56 	if ( (err = OptimalConnect(aSockServer)) != KErrNone )
       
    57 		return err;
       
    58 
       
    59 	if ( (err=aServerSock.Open(aSockServer, pTestObject->iProt)) != KErrNone)
       
    60 		{
       
    61 		aServerSock.Close();
       
    62 		aSockServer.Close();
       
    63 		return err;
       
    64 		}
       
    65 
       
    66 	if ( (err=aServerSock.SetLocalPort(1)) != KErrNone)
       
    67 		{
       
    68 		aServerSock.Close();
       
    69 		aSockServer.Close();
       
    70 		return err;
       
    71 		}
       
    72 
       
    73 	if ( (err=aServerSock.Listen(1)) != KErrNone)
       
    74 		{
       
    75 		aServerSock.Close();
       
    76 		aSockServer.Close();
       
    77 		return err;
       
    78 		}
       
    79 	
       
    80 	if ( (err=aNewConn.Open(aSockServer)) != KErrNone)
       
    81 		{
       
    82 		aServerSock.Close();
       
    83 		aSockServer.Close();
       
    84 		return err;
       
    85 		}
       
    86 	aServerSock.Accept(aNewConn,aStatAccept);
       
    87 	
       
    88 	if ( (err=aClientSock.Open(aSockServer, pTestObject->iProt)) != KErrNone)
       
    89 		{
       
    90 		aNewConn.Close();
       
    91 		aServerSock.Close();
       
    92 		aSockServer.Close();
       
    93 		return err;
       
    94 		}
       
    95 
       
    96 	aAddr.SetPort(1);
       
    97 	aClientSock.Connect(aAddr, aStatConnect);
       
    98 	User::WaitForRequest(aStatConnect);
       
    99 	User::WaitForRequest(aStatAccept);
       
   100 
       
   101 	err = aStatAccept.Int();
       
   102     if (err != KErrNone)
       
   103 		{
       
   104 		aClientSock.Close();
       
   105 		aNewConn.Close();
       
   106 		aServerSock.Close();
       
   107 		aSockServer.Close();
       
   108 		return err;
       
   109 		}
       
   110 	
       
   111     err = aStatConnect.Int();
       
   112     if (err != KErrNone)
       
   113 		{
       
   114 		aClientSock.Close();
       
   115 		aNewConn.Close();
       
   116 		aServerSock.Close();
       
   117 		aSockServer.Close();
       
   118 		return err;
       
   119 		}
       
   120 	
       
   121 	//-------------------substep 02-----------------------------	
       
   122 	// Try to assosiate the same blank socket (that is already connected
       
   123 	// to the client socket) to the server socket. 
       
   124 	// This should fail and panic this thread. 
       
   125 	// Main thread will then examine the exit status of this thread.
       
   126 	aServerSock.Accept(aNewConn, aStatAccept); 
       
   127 	
       
   128 	User::WaitForRequest(aStatAccept);
       
   129 	err = aStatAccept.Int();
       
   130 	
       
   131 	aClientSock.Close();
       
   132 	aNewConn.Close();
       
   133 	aServerSock.Close();
       
   134 	aSockServer.Close();
       
   135 	return err;
       
   136 	}
       
   137 
       
   138 // This is thread that should fail as it attempts to accept
       
   139 // with unopened socket
       
   140 TInt CTest05IPCIllegalOperations::fAcceptWithUnopendSocket(TAny* aInput)
       
   141 	{
       
   142 	TInt err;
       
   143 	RSocketServ aSockServer;
       
   144 	RSocket aNewConn, aServerSock;
       
   145 	TRequestStatus aStat;
       
   146     CTest05IPCIllegalOperations* pTestObject =
       
   147 								(CTest05IPCIllegalOperations*)aInput;
       
   148 	
       
   149 	//-------------------substep 01-----------------------------	
       
   150 	// Connect to Socket server, and create server socket
       
   151 	if ( (err = OptimalConnect(aSockServer)) != KErrNone )
       
   152 		return EFail;
       
   153 
       
   154 	if ( (err=aServerSock.Open(aSockServer, pTestObject->iProt)) != KErrNone)
       
   155 		{
       
   156 		aSockServer.Close();
       
   157 		return err;
       
   158 		}
       
   159 	
       
   160 	if ( (err=aServerSock.SetLocalPort(1)) != KErrNone)
       
   161 		{
       
   162 		aServerSock.Close();
       
   163 		aSockServer.Close();
       
   164 		return err;
       
   165 		}
       
   166 
       
   167 	if ( (err=aServerSock.Listen(1)) != KErrNone)
       
   168 		{
       
   169 		aServerSock.Close();
       
   170 		aSockServer.Close();
       
   171 		return err;
       
   172 		}
       
   173 	
       
   174 	//-------------------substep 02-----------------------------	
       
   175 	// Try to accept connection with a blank socket that is not opened.
       
   176 	// This should fail and panic this thread. 
       
   177 	// Main thread will then examine the exit status of this thread.
       
   178 	aServerSock.Accept(aNewConn, aStat);
       
   179 	User::WaitForRequest(aStat);
       
   180 	aServerSock.Close();
       
   181 	aSockServer.Close();
       
   182 	return KErrNone;
       
   183 	}
       
   184 
       
   185 // 
       
   186 enum TVerdict CTest05IPCIllegalOperations::InternalDoTestStepL(void)
       
   187 	{
       
   188 	//-------------------substep 01-----------------------------	
       
   189 	_LIT(aLog01,"01 Create and start thread that will try to accept null socket:");
       
   190 	Logger().Write(aLog01);
       
   191 
       
   192 	RThread aThread;
       
   193 	CleanupClosePushL(aThread);
       
   194 	
       
   195 	TRequestStatus aStatus;
       
   196 	TInt err = aThread.Create(_L("IPCTest05Thread1"), 
       
   197 		fAcceptWithUnopendSocket, 
       
   198 		KDefaultStackSize, 
       
   199 		KDefaultHeapSize, 
       
   200 		KDefaultHeapSize, 
       
   201 		(TAny*)this,
       
   202 		EOwnerProcess);
       
   203 
       
   204 	if (err != KErrNone)
       
   205 		{
       
   206 		_LIT(aLog,"Error:Could not create thread. err = %d");
       
   207 		Logger().WriteFormat(aLog, err);
       
   208 		User::Leave(EFail);
       
   209 		}
       
   210 
       
   211 	aThread.Logon(aStatus);
       
   212 	TBool aOldPanic = User::JustInTime();
       
   213 	User::SetJustInTime(EFalse);
       
   214 	aThread.Resume();
       
   215 	
       
   216 	//-------------------substep 02-----------------------------	
       
   217 	User::WaitForRequest(aStatus);
       
   218 	User::SetJustInTime(aOldPanic);
       
   219 	
       
   220 	TPtrC testCategory;
       
   221 	testCategory.Set(aThread.ExitCategory());
       
   222 	TExitType exitType = aThread.ExitType();
       
   223 	TInt reason = aThread.ExitReason();
       
   224 	
       
   225 	if (testCategory == KESockClientPanic &&
       
   226 			exitType == EExitPanic &&
       
   227 			reason == ESockBadHandle)
       
   228 		{
       
   229 		_LIT(aLog, "02 Thread panic: Category='%S', type=%d, reason=%d");
       
   230 		Logger().WriteFormat(aLog, &testCategory, exitType, reason);
       
   231 		}
       
   232 	else
       
   233 		{
       
   234 		_LIT(aLog, "02 Thread panic: Category='%S', type=%d, reason=%d <- wrong panic");
       
   235 		Logger().WriteFormat(aLog, &testCategory, exitType, reason);
       
   236 		User::Leave(EFail);
       
   237 		}
       
   238 	CleanupStack::PopAndDestroy(&aThread);
       
   239 	
       
   240 	//-------------------substep 03-----------------------------	
       
   241 	_LIT(aLog03,"03 Create and start thread that will try to accept already connected socket:");
       
   242 	Logger().Write(aLog03);
       
   243 	
       
   244 	CleanupClosePushL(aThread);
       
   245 	err = aThread.Create(_L("IPCTest05Thread2"), 
       
   246 		fAcceptWithConnectedSocket, 
       
   247 					   KDefaultStackSize, 
       
   248 					   KDefaultHeapSize, 
       
   249 					   KDefaultHeapSize, 
       
   250 					   (TAny*)this,
       
   251 					   EOwnerProcess);
       
   252 	if (err != KErrNone)
       
   253 		{
       
   254 		_LIT(aLog,"Error:Could not create thread. err = %d");
       
   255 		Logger().WriteFormat(aLog, err);
       
   256 		User::Leave(EFail);
       
   257 		}
       
   258 	aThread.Logon(aStatus);
       
   259 	aOldPanic = User::JustInTime();
       
   260 	User::SetJustInTime(EFalse);
       
   261 	aThread.Resume();
       
   262 	
       
   263 	//-------------------substep 04-----------------------------	
       
   264 	User::WaitForRequest(aStatus);
       
   265 	User::SetJustInTime(aOldPanic);
       
   266 
       
   267 	testCategory.Set(aThread.ExitCategory());
       
   268 	exitType = aThread.ExitType();
       
   269 	reason = aThread.ExitReason();
       
   270 	
       
   271 	if (testCategory == KESockClientPanic &&
       
   272 			exitType == EExitPanic &&
       
   273 			reason == EAcceptTwice)
       
   274 		{
       
   275 		_LIT(aLog, "04 Thread panic: Category='%S', type=%d, reason=%d");
       
   276 		Logger().WriteFormat(aLog, &testCategory, exitType, reason);
       
   277 		}
       
   278 	else
       
   279 		{
       
   280 		_LIT(aLog, "04 Thread panic: Category='%S', type=%d, reason=%d <- wrong panic");
       
   281 		Logger().WriteFormat(aLog, &testCategory, exitType, reason);
       
   282 		User::Leave(EFail);
       
   283 		}
       
   284 	CleanupStack::PopAndDestroy(&aThread);
       
   285 	
       
   286 	return EPass;
       
   287 	}
       
   288