datacommsserver/esockserver/test/TE_IPCTest/Test07IPCDataTransferMemoryLeak.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 07
       
    15 // 
       
    16 //
       
    17 
       
    18 // EPOC includes
       
    19 #include <e32base.h>
       
    20 
       
    21 // Test system includes
       
    22 #include "Test07IPCDataTransferMemoryLeak.h"
       
    23 
       
    24 const TDesC& CTest07IPCDataTransferMemoryLeak::GetTestName()
       
    25 	{
       
    26 	_LIT(ret,"IPCTest07");
       
    27 	return ret;
       
    28 	}
       
    29 
       
    30 enum TVerdict CTest07IPCDataTransferMemoryLeak::InternalDoTestStepL()
       
    31 	{
       
    32 #if defined (_DEBUG_SOCKET_FUNCTIONS)
       
    33 	// Server memory leak checking - this is a contentious thing because the
       
    34 	// only sure "leak" is when dynamic memory is 100% orphaned, ie no pointer
       
    35 	// from the root class to it, which we can't determine. The checking in this
       
    36 	// code used to be at the level of the socket operations, ie like:
       
    37 	//		iSockServer.__DbgMarkHeap();
       
    38 	//		ret = socket.DoSomething();
       
    39 	//		if(ret == KErrNoMemory)
       
    40 	//			iSockServer.__DbgMarkCheck();
       
    41 	// but this explicitly requires that upon a OOM condition the SAP will unwind
       
    42 	// any allocations, such as recv queues, and this is not true and isn't stated
       
    43 	// in any "contract" - it used to work purely because the recv queue was the
       
    44 	// last thing allocated, but now the shim introduces a later allocation.
       
    45 	
       
    46 	// So here we move the heap mark/check outside all of the socket handling, on
       
    47 	// the assumption that once all the SAPs are closed then all memory will have
       
    48 	// been returned. This is still a weak assumption, eg what if the protocol keeps
       
    49 	// a dynamic array of SAPs, then even once the SAPs have been freed this still
       
    50 	// preserves a "high water mark" (unless the array is compressed)
       
    51 	
       
    52 	TInt err;
       
    53 	RSocketServ heapSockServer;
       
    54 	
       
    55 	CleanupClosePushL (heapSockServer);
       
    56 	
       
    57 	if((err = OptimalConnect(heapSockServer)) != KErrNone)
       
    58 		{
       
    59 		Logger().WriteFormat(_L("Connecting to socket server failed with %d"), err);
       
    60 		User::Leave(EFail);
       
    61 		}
       
    62 	
       
    63 //	heapSockServer.__DbgMarkHeap();
       
    64 	
       
    65 	//-------------------substep 00-----------------------------
       
    66 	Logger().Write(_L("00 Open socket server:"));
       
    67 	
       
    68 	RSocketServ ss;
       
    69 	CleanupClosePushL(ss);
       
    70 	if((err = OptimalConnect(ss)) != KErrNone)
       
    71 		{
       
    72 		Logger().WriteFormat(_L("Connecting to socket server failed with %d"), err);
       
    73 		User::Leave(EFail);
       
    74 		}
       
    75 	
       
    76 	//-------------------substep 01-----------------------------
       
    77 	Logger().Write(_L("01 Create a single client/server connection:"));
       
    78 	
       
    79 	RSocket clientSock, newConn, serverSock;
       
    80 	TRequestStatus acceptStat, connectStat;
       
    81 	TSockAddr addr;
       
    82 	
       
    83 	CleanupClosePushL (clientSock);
       
    84 	CleanupClosePushL (newConn);
       
    85 	CleanupClosePushL (serverSock);
       
    86 	
       
    87 	if((err = serverSock.Open(ss, iProt)) != KErrNone)
       
    88 		{
       
    89 		Logger().WriteFormat(_L("Error: Could not open server socket. err = %d"), err);
       
    90 		User::Leave(EFail);		
       
    91 		}
       
    92 	
       
    93 	if((err = serverSock.SetLocalPort(1)) != KErrNone)
       
    94 		{
       
    95 		Logger().WriteFormat(_L("Error: Could not set port. err = %d"), err);
       
    96 		User::Leave(EFail);
       
    97 		}
       
    98 	
       
    99 	if((err = serverSock.Listen(1)) != KErrNone)
       
   100 		{
       
   101 		Logger().WriteFormat(_L("Error: Could not set up socket to listen. err = %d"), err);
       
   102 		User::Leave(EFail);		
       
   103 		}
       
   104 	
       
   105 	if((err = newConn.Open(ss)) != KErrNone)
       
   106 		{
       
   107 		Logger().WriteFormat(_L("Error: Could not open blank socket. err = %d"), err);
       
   108 		User::Leave(EFail);		
       
   109 		}
       
   110 	
       
   111 	serverSock.Accept(newConn, acceptStat);
       
   112 	
       
   113 	if((err = clientSock.Open(ss, iProt)) != KErrNone)
       
   114 		{
       
   115 		Logger().WriteFormat(_L("Error: Could not open socket. err = %d"), err);
       
   116 		User::Leave(EFail);		
       
   117 		}
       
   118 	
       
   119 	addr.SetPort(1);
       
   120 	clientSock.Connect(addr, connectStat);
       
   121 	
       
   122 	User::WaitForRequest(connectStat);
       
   123 	User::WaitForRequest(acceptStat);
       
   124 	TVerdict verdict = EPass;
       
   125 	
       
   126 	err = connectStat.Int();
       
   127 	if (err != KErrNone)
       
   128 		{
       
   129 		Logger().WriteFormat(_L("Error: Client socket not connected. err = %d"), err);
       
   130 		verdict = EFail;	       
       
   131 		}
       
   132 	
       
   133 	err = acceptStat.Int();
       
   134 	if (acceptStat != KErrNone)
       
   135 		{
       
   136 		Logger().WriteFormat(_L("Error: Connection not accepted on server side. err = %d"), err);
       
   137 		verdict = EFail;		
       
   138 		}
       
   139 	
       
   140 	if (verdict == EFail)
       
   141 		User::Leave(EFail);
       
   142 	
       
   143 	//-------------------substep 02-----------------------------
       
   144 	Logger().Write(_L("02 Set heap to fails after N=0 allocations..."));
       
   145 	Logger().Write(_L("   ...Call Write method. If it returns error, check memory leak."));
       
   146 	Logger().Write(_L("   ...Increase N and repeat the same until write method returns NoError:"));
       
   147 	
       
   148 	TBuf8<1> out, in;
       
   149 	TInt failure = 0;
       
   150 	TRequestStatus writeStat, readStat;
       
   151 	
       
   152 	in.SetMax();
       
   153 	out.SetMax();
       
   154 	
       
   155 	clientSock.Read(in, readStat);
       
   156 	
       
   157 	do
       
   158 		{
       
   159 		ss.__DbgFailNext(failure);
       
   160 		
       
   161 		newConn.Write(out, writeStat);
       
   162 		User::WaitForRequest(writeStat);
       
   163 		
       
   164 		failure++;
       
   165 		}
       
   166         while(writeStat.Int() != KErrNone);
       
   167 		
       
   168 	// flush the hanging __DbgFailNext
       
   169 	ss.__DbgFailNext(-1);
       
   170 	Logger().WriteFormat(_L("Info: Loop has passed %d times"), failure - 1);
       
   171 	
       
   172 	User::WaitForRequest(readStat);
       
   173 	err = readStat.Int();
       
   174 	// Depending upon the number of lazy allocs the read may already have been non-fatally failed - try again now we're out of OOM
       
   175 	if (err == KErrNoMemory)
       
   176 		{
       
   177 		clientSock.Read(in, readStat);
       
   178 		User::WaitForRequest(readStat);
       
   179 		err = readStat.Int();
       
   180 		}
       
   181 	if (err != KErrNone)
       
   182 		{
       
   183 		Logger().WriteFormat(_L("Read returned err=%d"), err);
       
   184 		User::Leave(EFail);
       
   185 		}
       
   186 	
       
   187 	//-------------------substep 03-----------------------------	
       
   188 	Logger().Write(_L("03 Set heap to fails after N=0 allocations..."));
       
   189 	Logger().Write(_L("   ...Call Read method. If it returns error, check memory leak."));
       
   190 	Logger().Write(_L("   ...Increase N and repeat the same until read method returns NoError:"));
       
   191 	failure = 0;
       
   192 	
       
   193 	newConn.Write(out, writeStat);
       
   194 	
       
   195 	do
       
   196 		{
       
   197 		ss.__DbgFailNext(failure);
       
   198 		
       
   199 		clientSock.Read(in, readStat);
       
   200 		User::WaitForRequest(readStat);
       
   201 		
       
   202 		failure++;
       
   203 		}
       
   204 		while(readStat.Int() != KErrNone);
       
   205 		
       
   206 	// To flush the hanging __DbgFailNext
       
   207 	ss.__DbgFailNext(-1);
       
   208 	
       
   209 	Logger().WriteFormat(_L("Info: Loop has passed %d times"), failure - 1);
       
   210 	
       
   211 	User::WaitForRequest(writeStat);
       
   212 	err = writeStat.Int();
       
   213 	if(err != KErrNone)
       
   214 		{
       
   215 		Logger().WriteFormat(_L("Write returned err=%d"), err);
       
   216 		User::Leave(EFail);
       
   217 		}
       
   218 	
       
   219 	//-------------------substep 05-----------------------------
       
   220 	Logger().Write(_L("04 Cleanup stack. Close the sockets & socket server"));
       
   221 	
       
   222 	CleanupStack::PopAndDestroy(4);
       
   223 	
       
   224 //	heapSockServer.__DbgCheckHeap(0);
       
   225 	
       
   226 	CleanupStack::PopAndDestroy();
       
   227 	
       
   228 	Logger().WriteFormat(_L("Got here OK"));
       
   229 	
       
   230 	return EPass;
       
   231 #else
       
   232 	Logger().WriteFormat(_L("Info: Test Disabled on release build"));
       
   233 	return EPass;
       
   234 #endif
       
   235 }
       
   236