datacommsserver/esockserver/test/TE_ESock/EsockTestSection29.cpp
changeset 0 dfb7c4ff071f
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     1 // Copyright (c) 2003-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 // This contains ESock Test cases from section 29
       
    15 // 
       
    16 //
       
    17 
       
    18 
       
    19 // EPOC includes
       
    20 #include <e32base.h>
       
    21 #include <in_sock.h>
       
    22 #include <es_sock.h>
       
    23 #include <nifman.h>
       
    24 #include <commdbconnpref.h>
       
    25 
       
    26 #ifdef SYMBIAN_OLD_EXPORT_LOCATION
       
    27 #include "es_dummy.h"
       
    28 #else
       
    29 #include <test/es_dummy.h>
       
    30 #endif
       
    31 
       
    32 #include <es_sock.h>
       
    33 #include <es_mbuf.h>
       
    34 
       
    35 #include "EsockTestSection29.h"
       
    36 
       
    37 // Test system includes
       
    38 #include <comms-infras/esocktestutils.h>
       
    39 
       
    40 
       
    41 TVerdict CEsockTest29::easyTestStepL()
       
    42 	{
       
    43 
       
    44 	// Open Sockets, one to manipulate mBufPool and one to send/revieve with tcp or udp
       
    45 	TESTL( OpenSockets() == KErrNone );
       
    46 
       
    47 	// Bind Test Socket To local Address
       
    48 	TInetAddr local;
       
    49 	SetPort( local, KPort, KLocal );
       
    50 	iTestSocket.Bind( local );
       
    51 	
       
    52 	// Connect Test Socket to LoopBack Address
       
    53 	TInetAddr loopBack;
       
    54 	SetPort( loopBack, KPort, KLoopback );
       
    55 	ConnectSocketL( iTestSocket, loopBack );
       
    56 	
       
    57 	// Gobble all the Buf's in the MBuf Pool return the number of mBufs in Use
       
    58 	TInt totalUsedBufs = iDummySocket.SetOpt( KDummyOptionSetGobbleMBufs, 0, 0 );
       
    59 	TInt bufsToBeFreed = 0;
       
    60 	
       
    61 	// Data to be used during socket Send
       
    62 	GetIntFromConfig(KSection29, KBufSize, iBufSize);
       
    63 	TBool keepSending = ETrue;
       
    64 	TBuf8<4500> recvBuf;
       
    65 	TBuf8<4500> sendData;
       
    66 	sendData.Fill(TChar('z'), iBufSize );
       
    67 	TRequestStatus sendStatus;
       
    68 	TRequestStatus recvStatus;
       
    69 	const TInt KOneSecond = 1000000;
       
    70 	
       
    71 	
       
    72 	
       
    73 	for( TInt freeBufCounter = 0; freeBufCounter < totalUsedBufs && keepSending; freeBufCounter += bufsToBeFreed )
       
    74 	// OOB loop that will gradually release more MBufs
       
    75 		{
       
    76 		
       
    77 		// release some mBufs and send
       
    78 		iDummySocket.SetOpt( KDummyOptionSetFreeSomeMBufs, bufsToBeFreed , 0);
       
    79 		iTestSocket.Send( sendData, 0, sendStatus );
       
    80 		
       
    81 		// Wait for a second to allow time for server/protocol to send
       
    82 		User::After( KOneSecond );
       
    83 		
       
    84 		// Cancel the send and start to recv
       
    85 		iTestSocket.CancelSend();
       
    86 		User::WaitForRequest(sendStatus);
       
    87 		
       
    88 		iTestSocket.Recv(recvBuf, 0, recvStatus);
       
    89 		
       
    90 		// Wait for a second to allow time for server/protocol to recieve
       
    91 		User::After( KOneSecond );
       
    92 		iTestSocket.CancelRecv();
       
    93 		User::WaitForRequest( recvStatus );
       
    94 		
       
    95 		if( freeBufCounter % 5 == 0 )
       
    96 			{
       
    97 			INFO_PRINTF2( _L("freeBufs now >= %d "),  freeBufCounter );
       
    98 			}
       
    99 		
       
   100 		if( recvBuf.Compare(sendData) == 0 )
       
   101 		// if we have recieved data then exit the loop
       
   102 			{
       
   103 			keepSending = EFalse;
       
   104 			}
       
   105 			
       
   106 		bufsToBeFreed = 2;
       
   107 		
       
   108 		}
       
   109 	
       
   110 	// Now do a regular send for good measure to make sure everything is ok
       
   111 	sendData.Fill(TChar('c'), iBufSize );
       
   112 	INFO_PRINTF1( _L("End of Loop, Now sending....") );
       
   113 	iTestSocket.Send( sendData, 0, sendStatus );
       
   114 	User::WaitForRequest( sendStatus );
       
   115 	INFO_PRINTF2( _L("Send Returned %d, now recieving...."), sendStatus.Int() );
       
   116 	iTestSocket.Recv( recvBuf, 0, recvStatus );
       
   117 	User::After( KOneSecond ); 
       
   118 	iTestSocket.CancelRecv();
       
   119 	User::WaitForRequest( recvStatus );
       
   120 	
       
   121 	INFO_PRINTF2( _L("Recieve returned %d ") , recvStatus.Int() );
       
   122 	if( recvBuf.Compare(sendData) != 0 )
       
   123 		{
       
   124 		return EFail;
       
   125 		}
       
   126 	
       
   127 	return EPass;
       
   128 	
       
   129 	}
       
   130 
       
   131 
       
   132 TInt CEsockTest29::OpenSockets()
       
   133 // Func to open the sockets
       
   134 	{
       
   135 	iSockServ.Connect();
       
   136 	GetStringFromConfig(KSection29, KTestSocketType, iProtocol);
       
   137 	TInt err = OpenDummySocket();
       
   138 	if(err != KErrNone)	
       
   139 		{
       
   140 		return err;
       
   141 		}
       
   142 	if( iProtocol.Compare(KUdp) == 0 )
       
   143 		{
       
   144 		err = OpenTestUdpSocket();
       
   145 		INFO_PRINTF1( _L("UDP Test Socket Opened") );
       
   146 		}
       
   147 	else
       
   148 		{
       
   149 		err = OpenTestTcpSocket();	
       
   150 		INFO_PRINTF1( _L("TCP Test Socket Opened") );	
       
   151 		}
       
   152 	return err;
       
   153 	}
       
   154 
       
   155 
       
   156 CEsockTest29::~CEsockTest29()
       
   157 	{
       
   158 	iTestSocket.Close();
       
   159 	iDummySocket.Close();
       
   160 	iSockServ.Close();
       
   161 	}
       
   162 
       
   163 	
       
   164 TInt CEsockTest29::OpenTestTcpSocket()
       
   165 	{
       
   166 	return iTestSocket.Open(iSockServ, KAfInet, KSockStream, KProtocolInetTcp);
       
   167 	}
       
   168 
       
   169 	
       
   170 TInt CEsockTest29::OpenTestUdpSocket()
       
   171 	{
       
   172 	return iTestSocket.Open(iSockServ, KAfInet, KSockDatagram, KProtocolInetUdp);
       
   173 	}
       
   174 
       
   175 
       
   176 TInt CEsockTest29::OpenDummySocket()
       
   177 	{
       
   178 	return iDummySocket.Open(iSockServ,_L("Dummy Protocol 3"));
       
   179 	}
       
   180 
       
   181 
       
   182 void CEsockTest29::SetPort(TInetAddr &aAddr, const TInt aPort, const TDesC &aIPAddr)
       
   183 	{
       
   184 	aAddr.SetPort(aPort);
       
   185 	aAddr.Input(aIPAddr);
       
   186 	}
       
   187 	
       
   188 
       
   189 void CEsockTest29::ConnectSocketL( RSocket &aSocket, TInetAddr &aAddr )
       
   190 // Connect aSocket to aAddr
       
   191 	{
       
   192 	TRequestStatus connectSocketStatus;
       
   193  	aSocket.Connect( aAddr, connectSocketStatus );
       
   194  	User::WaitForRequest( connectSocketStatus );
       
   195  	if(connectSocketStatus != KErrNone)
       
   196 	 	{
       
   197 	 	User::Leave(KErrCouldNotConnect);
       
   198 	 	}
       
   199  	}
       
   200  	
       
   201 const TDesC& CEsockTest29::GetTestName()
       
   202 	{
       
   203 	// store the name of this test case
       
   204 	_LIT(ret,"Test29");
       
   205 	return ret;
       
   206 	}
       
   207 	
       
   208 
       
   209 /* --------------------------------------------------------------------------------------------
       
   210 	ESOCK Test 29_1 uses the above Loop with a data size of 100 (less than MTU) and a Udp Socket
       
   211 	changing the OOB loop to do this is done with the use of ini files
       
   212    -------------------------------------------------------------------------------------------*/
       
   213 
       
   214 
       
   215 /*--------------------------------------------------------------------------------------------------
       
   216 	ESOCK Test 29_2 uses the above Loop with a data size of 4500 (Greater than MTU) and a Udp Socket
       
   217   ------------------------------------------------------------------------------------------------*/
       
   218 
       
   219 
       
   220 /*--------------------------------------------------------------------------------------------------
       
   221 	ESOCK Tests 29_3 to 29_9 are left open for further OOB loop tests
       
   222   ------------------------------------------------------------------------------------------------*/
       
   223 
       
   224 	
       
   225 	
       
   226 /* Test 29.10 */
       
   227 
       
   228 // Test step 29.10
       
   229 const TDesC& CEsockTest29_10::GetTestName()
       
   230 	{
       
   231 	_LIT(ret,"Test29.10");
       
   232 	return ret;
       
   233 	}
       
   234 
       
   235 CEsockTest29_10::~CEsockTest29_10()
       
   236 	{
       
   237 	}
       
   238 
       
   239 enum TVerdict CEsockTest29_10::easyTestStepL( void )
       
   240 	{
       
   241 	SetTestStepResult(EFail);
       
   242 	TInt ret;
       
   243 	const TUint KOneSecond = 1000000;
       
   244 	
       
   245 	Logger().WriteFormat(_L("Send/ Recv on PDummy3 socket but no avail MBuf Memory, mBuf becomes avail after send"));
       
   246 	RSocketServ ss;
       
   247 	ret = ss.Connect();
       
   248 	TESTL(KErrNone == ret);	
       
   249     
       
   250     // open dummy prot 3
       
   251 	Logger().WriteFormat(_L("Openning Dummy Protocol 3"));             
       
   252 	RSocket sock;                                                  
       
   253 	ret = sock.Open(ss,_L("Dummy Protocol 3"));
       
   254 	TESTL(KErrNone == ret);
       
   255 	
       
   256 	// connect socket
       
   257 	TSockAddr addr;
       
   258 	TRequestStatus connStat;
       
   259 	sock.Connect(addr, connStat);
       
   260 	User::WaitForRequest(connStat);
       
   261 	TESTL(KErrNone == connStat.Int());
       
   262 	
       
   263 	// send packet 1
       
   264 	TRequestStatus stat;
       
   265 	Logger().WriteFormat(_L("Attempting to create a packet to receive later"));	
       
   266 	_LIT8(sendDataOne, "First Send");
       
   267 	sock.Send(sendDataOne,0,stat);
       
   268 	User::WaitForRequest(stat);
       
   269 	TESTL(KErrNone == stat.Int());	
       
   270 	// wait for packets to go through esock
       
   271 	User::After(KOneSecond * 2);
       
   272 	// gobble mBufs
       
   273 	TInt nFree = 0;
       
   274 	sock.GetOpt(KDummyOptionGetMBufFreeSpace, 0, nFree);
       
   275 	Logger().WriteFormat(_L("Attempting to set KDummyOptionSetGobbleMBufs Option in Protocol"));
       
   276 	while (nFree > 0)
       
   277 		{
       
   278 		ret = sock.SetOpt(KDummyOptionSetGobbleMBufs, 0, 0);
       
   279 		sock.GetOpt(KDummyOptionGetMBufFreeSpace, 0, nFree);
       
   280 		}
       
   281 	Logger().WriteFormat(_L("Socket::SetOpt KDummyOptionSetGobbleMBufs returned %d"), ret);
       
   282 	
       
   283 	// we are running in high priority and allocating in a loop.
       
   284 	// so kernel may not be able to enlarge the pools.
       
   285 	// wait for kernel to enlarge the pool
       
   286 	// We have to come out from the loop as well.
       
   287     User::After(KOneSecond * 2);
       
   288 	
       
   289     sock.GetOpt(KDummyOptionGetMBufFreeSpace, 0, nFree);
       
   290     Logger().WriteFormat(_L("Attempting to set KDummyOptionSetGobbleMBufs Option in Protocol"));
       
   291     while (nFree > 0)
       
   292         {
       
   293         ret = sock.SetOpt(KDummyOptionSetGobbleMBufs, 0, 0);
       
   294         sock.GetOpt(KDummyOptionGetMBufFreeSpace, 0, nFree);
       
   295         }
       
   296     Logger().WriteFormat(_L("Socket::SetOpt KDummyOptionSetGobbleMBufs returned %d"), ret);
       
   297     // Once again
       
   298     User::After(KOneSecond * 2);
       
   299     sock.GetOpt(KDummyOptionGetMBufFreeSpace, 0, nFree);
       
   300     Logger().WriteFormat(_L("Attempting to set KDummyOptionSetGobbleMBufs Option in Protocol"));
       
   301     while (nFree > 0)
       
   302         {
       
   303         ret = sock.SetOpt(KDummyOptionSetGobbleMBufs, 0, 0);
       
   304         sock.GetOpt(KDummyOptionGetMBufFreeSpace, 0, nFree);
       
   305         }
       
   306     Logger().WriteFormat(_L("Socket::SetOpt KDummyOptionSetGobbleMBufs returned %d"), ret);
       
   307     // Once again
       
   308     User::After(KOneSecond * 2);
       
   309     sock.GetOpt(KDummyOptionGetMBufFreeSpace, 0, nFree);
       
   310     Logger().WriteFormat(_L("Attempting to set KDummyOptionSetGobbleMBufs Option in Protocol"));
       
   311     while (nFree > 0)
       
   312         {
       
   313         ret = sock.SetOpt(KDummyOptionSetGobbleMBufs, 0, 0);
       
   314         sock.GetOpt(KDummyOptionGetMBufFreeSpace, 0, nFree);
       
   315         }
       
   316     // Once again
       
   317     User::After(KOneSecond * 2);
       
   318     sock.GetOpt(KDummyOptionGetMBufFreeSpace, 0, nFree);
       
   319     Logger().WriteFormat(_L("Attempting to set KDummyOptionSetGobbleMBufs Option in Protocol"));
       
   320     while (nFree > 0)
       
   321         {
       
   322         ret = sock.SetOpt(KDummyOptionSetGobbleMBufs, 0, 0);
       
   323         sock.GetOpt(KDummyOptionGetMBufFreeSpace, 0, nFree);
       
   324         }
       
   325 
       
   326 	
       
   327 	// send packet 2
       
   328 	User::After(KOneSecond);
       
   329 	TRequestStatus stat2;
       
   330 	_LIT8(sendDataTwo, "Second Send");
       
   331 	Logger().WriteFormat(_L("Sending Data - Should never complete"));             
       
   332 	sock.Send(sendDataTwo,0,stat2);
       
   333 
       
   334 	User::After(KOneSecond);
       
   335 	Logger().WriteFormat(_L("Now cancel the Send"));     
       
   336 	sock.CancelSend();
       
   337 	User::WaitForRequest(stat2);
       
   338 	TESTL(stat2 == KErrCancel);
       
   339 	
       
   340 	Logger().WriteFormat(_L("Receiving Data -- expected to pick up sendDataOne"));
       
   341 	TBuf8<100> buf;          
       
   342 	TRequestStatus stat3;
       
   343 	sock.Recv(buf, 0, stat3);
       
   344 	User::After(KOneSecond);
       
   345 	User::WaitForRequest(stat3);
       
   346 	TESTL(buf.Compare(sendDataOne) == 0);
       
   347 		
       
   348 	// send packet 3
       
   349 	_LIT8(sendDataThree, "Third Send");
       
   350 	Logger().WriteFormat(_L("Sending Data ... again"));             
       
   351 	TRequestStatus stat4;
       
   352 	sock.Send(sendDataThree,0,stat4);
       
   353 	User::After(1000);
       
   354 			
       
   355 	// free memory
       
   356 	Logger().WriteFormat(_L("Now free memory - should get send and receive completion"));	
       
   357 	Logger().WriteFormat(_L("Attempting to set KDummyOptionSetFreeMBufs Option in Protocol"));	
       
   358 	ret = sock.SetOpt(KDummyOptionSetFreeMBufs, 0, 0);
       
   359 	Logger().WriteFormat(_L("Socket::SetOpt KDummyOptionSetFreeMBufs returned %d"), ret);
       
   360 	TESTL(KErrNone == ret);	
       
   361 
       
   362 	Logger().WriteFormat(_L("Sending Data - Should now complete"));             
       
   363 	User::WaitForRequest(stat4);
       
   364 	TESTL(stat4.Int() == KErrNone);
       
   365 
       
   366 	// recieve data and compare contents to sent data
       
   367 	Logger().WriteFormat(_L("Receiving Data"));
       
   368 	sock.Recv(buf, 0, stat);
       
   369 	User::WaitForRequest(stat);
       
   370 	Logger().WriteFormat(_L("Recv has returned %d"), stat.Int());
       
   371 	TBuf<100> buf16;
       
   372 	buf16.Copy(buf);
       
   373 	if(buf.Compare(sendDataThree) == 0)
       
   374 		{
       
   375 		SetTestStepResult(EPass);
       
   376 		}
       
   377 		
       
   378 	Logger().WriteFormat(_L("Data Recv'ed is '%S'"), &buf16);
       
   379 	sock.Close();
       
   380 	ss.Close();
       
   381 	return TestStepResult();
       
   382 	}
       
   383 
       
   384 /* Test 29.11 */
       
   385 
       
   386 // Test step 29.11
       
   387 const TDesC& CEsockTest29_11::GetTestName()
       
   388 	{
       
   389 	_LIT(ret,"Test29.11");
       
   390 	return ret;
       
   391 	}
       
   392 
       
   393 CEsockTest29_11::~CEsockTest29_11()
       
   394 	{
       
   395 	}
       
   396 
       
   397 enum TVerdict CEsockTest29_11::easyTestStepL( void )
       
   398 	{
       
   399 	TVerdict verdict = EFail;
       
   400 	TInt ret;
       
   401 	
       
   402 	Logger().WriteFormat(_L("Send/ Recv on PDummy3 socket but no avail MBuf Memory, mBuf becomes avail after send"));
       
   403 	RSocketServ ss;
       
   404 	ret = ss.Connect();
       
   405 	TESTL(KErrNone == ret);	
       
   406 	CleanupClosePushL(ss);
       
   407     
       
   408     // open dummy prot 3
       
   409 	Logger().WriteFormat(_L("Openning Dummy Protocol 3"));             
       
   410 	RSocket sock;                                                  
       
   411 	ret = sock.Open(ss,_L("Dummy Protocol 3"));
       
   412 	TESTL(KErrNone == ret);
       
   413 	
       
   414 	// connecti socket
       
   415 	TSockAddr addr;
       
   416 	TRequestStatus stat;
       
   417 	sock.Connect(addr, stat);
       
   418 	User::WaitForRequest(stat);
       
   419 	TESTL(KErrNone == stat.Int());	
       
   420 	
       
   421 	// drain MBuf Pool
       
   422 	Logger().WriteFormat(_L("Attempting to set KDummyOptionSetGobbleMBufs Option in Protocol"));	
       
   423 	ret = sock.SetOpt(KDummyOptionSetGobbleMBufs, 0, 0);
       
   424 	Logger().WriteFormat(_L("Socket::SetOpt KDummyOptionSetFreeMBufs returned %d"), ret);
       
   425 	
       
   426 	// send data and wait for 1/10 seconds
       
   427 	Logger().WriteFormat(_L("Sending Data"));     
       
   428 	_LIT8( sendData, "bla bla bla bla");        
       
   429 	sock.Send(sendData,0,stat);
       
   430 	const TUint KTenMilliSecs = 10000; 
       
   431 	User::After(KTenMilliSecs);
       
   432 	
       
   433 	// free all mbufs
       
   434 	Logger().WriteFormat(_L("Attempting to set KDummyOptionSetFreeMBufs Option in Protocol"));	
       
   435 	ret = sock.SetOpt(KDummyOptionSetFreeMBufs, 0, 0);
       
   436 	TESTL(ret == KErrNone);
       
   437 	Logger().WriteFormat(_L("Socket::SetOpt KDummyOptionSetFreeMBufs returned %d"), ret);
       
   438 
       
   439 	// wait for send to return
       
   440 	User::WaitForRequest(stat);
       
   441 	Logger().WriteFormat(_L("Send has returned %d"), stat.Int());
       
   442 	if(stat.Int() != KErrNone)
       
   443 		{
       
   444 		verdict = EFail;
       
   445 		}
       
   446 	
       
   447 	// recieve data and verify that it is the same as send data
       
   448 	TBuf8<20> recvBuf;
       
   449 	TRequestStatus recvStatus;
       
   450 	sock.Recv(recvBuf, 0, recvStatus);
       
   451 	User::WaitForRequest(recvStatus);
       
   452 	Logger().WriteFormat(_L("recieving data on PDummy3 has returned %d"), recvStatus.Int());
       
   453 	if(recvStatus.Int() != KErrNone)
       
   454 		{
       
   455 		verdict = EFail;
       
   456 		}
       
   457 	Logger().WriteFormat(_L("Comparing Recieved data and Sent data"), recvStatus.Int());		
       
   458 	if(recvBuf.Compare(sendData) == 0)
       
   459 		{
       
   460 		verdict = EPass; 
       
   461 		}
       
   462 	
       
   463 	sock.Close();
       
   464 	CleanupStack::PopAndDestroy(&ss);
       
   465     SetTestStepResult(verdict);
       
   466 	return verdict;
       
   467 
       
   468 	}
       
   469