datacommsserver/esockserver/test/TE_ESock/EsockTestSection9.cpp
changeset 0 dfb7c4ff071f
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     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 "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 9
       
    15 // 
       
    16 //
       
    17 
       
    18 // EPOC includes
       
    19 #include <e32base.h>
       
    20 #include <in_sock.h>
       
    21 
       
    22 // Test system includes
       
    23 #include "EsockTestSection9.h"
       
    24 
       
    25 
       
    26 // Test step 9.1
       
    27 const TDesC& CEsockTest9_1::GetTestName()
       
    28 	{
       
    29 	// store the name of this test case
       
    30 	_LIT(ret,"Test9.1");
       
    31 	
       
    32 	return ret;
       
    33 	}
       
    34 
       
    35 CEsockTest9_1::~CEsockTest9_1()
       
    36 	{
       
    37 	}
       
    38 
       
    39 enum TVerdict CEsockTest9_1::easyTestStepPreambleL()
       
    40 	{
       
    41 	if (KErrNone != OpenMinSockets(_L("Test_9.1"),KAfInet, KSockStream, KProtocolInetTcp))
       
    42 		{
       
    43 		return EFail;
       
    44 		}
       
    45 	
       
    46 	return EPass;
       
    47 	}
       
    48 
       
    49 enum TVerdict CEsockTest9_1::easyTestStepL()
       
    50 	{
       
    51 	Logger().WriteFormat(_L("TE_ESock: Test 9.1"));
       
    52 	
       
    53 	// List Route(s)
       
    54 	ListRoutes();
       
    55 	
       
    56 	return EPass;
       
    57 	}
       
    58 
       
    59 
       
    60 // Test step 9.2
       
    61 const TDesC& CEsockTest9_2::GetTestName()
       
    62 	{
       
    63 	// store the name of this test case
       
    64 	_LIT(ret,"Test9.2");
       
    65 	
       
    66 	return ret;
       
    67 	}
       
    68 
       
    69 CEsockTest9_2::~CEsockTest9_2()
       
    70 	{
       
    71 	}
       
    72 
       
    73 enum TVerdict CEsockTest9_2::easyTestStepPreambleL()
       
    74 	{
       
    75 	if (KErrNone != OpenMinSockets(_L("Test_9.2"),KAfInet, KSockStream, KProtocolInetTcp))
       
    76 		{
       
    77 		return EFail;
       
    78 		}
       
    79 	
       
    80 	return EPass;
       
    81 	}
       
    82 
       
    83 enum TVerdict CEsockTest9_2::easyTestStepL()
       
    84 	{
       
    85 	Logger().WriteFormat(_L("TE_ESock: test 9.2"));
       
    86 	TESTL(EPass == TestStepResult());
       
    87 	
       
    88 	// ipv4
       
    89 	// List Route(s)
       
    90 	ListRoutes();
       
    91 
       
    92 	TInt ret = SetRoute(KSoInetAddRoute, IPADDR(10,158,7,52),IPADDR(255,0,0,0),IPADDR(10,158,7,73), IPADDR(127,0,0,1), 0);
       
    93 	TESTEL(KErrNone == ret, ret);
       
    94 	
       
    95 	// List Route(s)
       
    96 	ListRoutes();
       
    97 	
       
    98 	// Delete Route
       
    99 	ret = SetRoute(KSoInetDeleteRoute, IPADDR(10,158,7,52),IPADDR(255,0,0,0),IPADDR(10,158,7,73), IPADDR(127,0,0,1), 0);
       
   100 	TESTEL(KErrNone == ret, ret);
       
   101 	
       
   102 	// List Route(s)
       
   103 	ListRoutes();
       
   104 	
       
   105 	return EPass;
       
   106 	}
       
   107 
       
   108 // Test step 9.3
       
   109 const TDesC& CEsockTest9_3::GetTestName()
       
   110 	{
       
   111 	// store the name of this test case
       
   112 	_LIT(ret,"Test9.3");
       
   113 	
       
   114 	return ret;
       
   115 	}
       
   116 
       
   117 CEsockTest9_3::~CEsockTest9_3()
       
   118 	{
       
   119 	}
       
   120 
       
   121 enum TVerdict CEsockTest9_3::easyTestStepPreambleL()
       
   122 	{
       
   123 	if (KErrNone != OpenMinSockets(_L("Test_9.3"),KAfInet, KSockStream, KProtocolInetTcp))
       
   124 		{
       
   125 		return EFail;
       
   126 		}
       
   127 	
       
   128 	return EPass;
       
   129 	}
       
   130 
       
   131 enum TVerdict CEsockTest9_3::easyTestStepL()
       
   132 	{
       
   133 	Logger().WriteFormat(_L("TE_ESock: test 9.3"));
       
   134 	TESTL(EPass == TestStepResult());
       
   135 	
       
   136 	// ipv6
       
   137 	// List Route(s)
       
   138 	ListRoutes();
       
   139 	
       
   140 	TPckgBuf<TSoInetRouteInfo> opt;
       
   141 	opt().iType = ERtUser;
       
   142 	opt().iState = ERtReady;
       
   143 	opt().iIfAddr.SetFamily(KAFUnspec);
       
   144 	opt().iMetric = 1;
       
   145 	(TInetAddr::Cast(opt().iIfAddr)).Input(_L("::1"));
       
   146 	(TInetAddr::Cast(opt().iGateway)).Input(_L("fe80::dead:beef"));
       
   147 	(TInetAddr::Cast(opt().iDstAddr)).Input(_L("2001:618:400:61::dead:fee"));
       
   148 	(TInetAddr::Cast(opt().iNetMask)).Input(_L("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
       
   149 	// Add Route
       
   150 	TInt ret = iEsockSuite->GetSocketHandle(1).SetOpt(KSoInetAddRoute, KSolInetRtCtrl, opt);
       
   151 	TESTEL(KErrNone == ret, ret);
       
   152 	
       
   153 	// List Route(s)
       
   154 	ListRoutes();
       
   155 	
       
   156 	// Delete Route
       
   157 	ret = iEsockSuite->GetSocketHandle(1).SetOpt(KSoInetDeleteRoute, KSolInetRtCtrl, opt);
       
   158 	TESTEL(KErrNone == ret, ret);
       
   159 	
       
   160 	// List Route(s)
       
   161 	ListRoutes();
       
   162 	
       
   163 	return EPass;
       
   164 	}
       
   165 
       
   166 // Test step 9.4
       
   167 const TDesC& CEsockTest9_4::GetTestName()
       
   168 	{
       
   169 	// store the name of this test case
       
   170 	_LIT(ret,"Test9.4");
       
   171 	
       
   172 	return ret;
       
   173 	}
       
   174 
       
   175 CEsockTest9_4::~CEsockTest9_4()
       
   176 	{
       
   177 	}
       
   178 
       
   179 enum TVerdict CEsockTest9_4::easyTestStepPreambleL()
       
   180 	{
       
   181 	if (KErrNone != OpenMinSockets(_L("Test_9.4"),KAfInet, KSockStream, KProtocolInetTcp))
       
   182 		{
       
   183 		return EFail;
       
   184 		}
       
   185 	
       
   186 	return EPass;
       
   187 	}
       
   188 
       
   189 enum TVerdict CEsockTest9_4::easyTestStepL()
       
   190 	{
       
   191 	Logger().WriteFormat(_L("TE_ESock: test 9.4"));
       
   192 	TESTL(EPass == TestStepResult());
       
   193 	
       
   194 	// ipv4
       
   195 	// List Route(s)
       
   196 	ListRoutes();
       
   197 	
       
   198 	// Add Route
       
   199 	TInt ret = SetRoute(KSoInetAddRoute, IPADDR(10,158,7,52),IPADDR(255,0,0,0),IPADDR(10,158,7,73), IPADDR(127,0,0,1), 0);
       
   200 	TESTEL(KErrNone == ret, ret);
       
   201 	
       
   202 	// List Route(s)
       
   203 	ListRoutes();
       
   204 	
       
   205 	// Change Route
       
   206 	ret = SetRoute(KSoInetChangeRoute, IPADDR(10,158,7,52),IPADDR(255,0,0,0),IPADDR(10,158,7,70), IPADDR(127,0,0,1), 0);
       
   207 	TESTEL(KErrNotFound == ret, ret);
       
   208 	
       
   209 	// List Route(s)
       
   210 	ListRoutes();
       
   211 	
       
   212 	return EPass;
       
   213 	}
       
   214 
       
   215 // Test step 9.5
       
   216 const TDesC& CEsockTest9_5::GetTestName()
       
   217 	{
       
   218 	// store the name of this test case
       
   219 	_LIT(ret,"Test9.5");
       
   220 	
       
   221 	return ret;
       
   222 	}
       
   223 
       
   224 CEsockTest9_5::~CEsockTest9_5()
       
   225 	{
       
   226 	}
       
   227 
       
   228 enum TVerdict CEsockTest9_5::easyTestStepPreambleL()
       
   229 	{
       
   230 	if (KErrNone != OpenMinSockets(_L("Test_9.5"),KAfInet, KSockStream, KProtocolInetTcp))
       
   231 		{
       
   232 		return EFail;
       
   233 		}
       
   234 	
       
   235 	return EPass;
       
   236 	}
       
   237 
       
   238 enum TVerdict CEsockTest9_5::easyTestStepL()
       
   239 	{
       
   240 	Logger().WriteFormat(_L("TE_ESock: test 9.5"));
       
   241 	TESTL(EPass == TestStepResult());
       
   242 	
       
   243 	// ipv6
       
   244 	// List Route(s)
       
   245 	ListRoutes();
       
   246 	
       
   247 	TPckgBuf<TSoInetRouteInfo> opt;
       
   248 	opt().iType = ERtUser;
       
   249 	opt().iState = ERtReady;
       
   250 	opt().iIfAddr.SetFamily(KAFUnspec);
       
   251 	opt().iMetric = 1;
       
   252 	(TInetAddr::Cast(opt().iIfAddr)).Input(_L("::1"));
       
   253 	(TInetAddr::Cast(opt().iGateway)).Input(_L("fe80::dead:beef"));
       
   254 	(TInetAddr::Cast(opt().iDstAddr)).Input(_L("2001:618:400:61::dead:fee"));
       
   255 	(TInetAddr::Cast(opt().iNetMask)).Input(_L("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
       
   256 	// Add Route
       
   257 	TInt ret = iEsockSuite->GetSocketHandle(1).SetOpt(KSoInetAddRoute, KSolInetRtCtrl, opt);
       
   258 	TESTEL(KErrNone == ret, ret);
       
   259 	
       
   260 	// List Route(s)
       
   261 	ListRoutes();
       
   262 	
       
   263 	(TInetAddr::Cast(opt().iIfAddr)).Input(_L("::1"));
       
   264 	(TInetAddr::Cast(opt().iGateway)).Input(_L("ffff::new:beef"));
       
   265 	// Change Route
       
   266 	ret = iEsockSuite->GetSocketHandle(1).SetOpt(KSoInetChangeRoute, KSolInetRtCtrl, opt);
       
   267 	TESTEL(KErrNotFound == ret, ret);
       
   268 	
       
   269 	// List Route(s)
       
   270 	ListRoutes();
       
   271 	
       
   272 	return EPass;
       
   273 	}
       
   274 
       
   275 
       
   276 const TInt KTimerPeriod = 10 * 1000000; // Guard timer period, in microseconds.
       
   277 
       
   278 CEsockTest9_6::CEsockTest9_6()
       
   279 	{
       
   280 	iGuardTimer.CreateLocal();	// Set up the timer
       
   281 	}
       
   282 
       
   283 // Test step 9.5
       
   284 const TDesC& CEsockTest9_6::GetTestName()
       
   285 	{
       
   286 	// store the name of this test case
       
   287 	_LIT(ret,"Test9.6");
       
   288 	
       
   289 	return ret;
       
   290 	}
       
   291 
       
   292 CEsockTest9_6::~CEsockTest9_6()
       
   293 	{
       
   294 	iGuardTimer.Close();		// Cleanup the timer
       
   295 	}
       
   296 
       
   297 /**
       
   298    Create a UDP socket. Perform a zero-length SendTo, then attempt to receive the zero-length datagram.
       
   299 
       
   300    @return EPass on success, EFail on failure
       
   301 */
       
   302 TVerdict CEsockTest9_6::easyTestStepL()
       
   303 	{
       
   304 	Logger().WriteFormat(_L("TE_ESock: test 9.6"));
       
   305 	
       
   306 	TInt cleanupCount = 0;		// Number of items we've put on Cleanup Stack - remember to increment this after every Push operation.
       
   307 	
       
   308 	RSocket sendSocket;
       
   309 
       
   310 	CleanupClosePushL( sendSocket );
       
   311 	++cleanupCount;
       
   312 
       
   313 	TSockAddr sendSocketAddress;
       
   314 	Logger().WriteFormat(_L("Creating Send Socket"));
       
   315 	if( !CreateUdpSocket( sendSocket, sendSocketAddress ) )
       
   316 		{
       
   317 		CleanupStack::PopAndDestroy( cleanupCount );
       
   318 		return EFail;
       
   319 		}
       
   320 
       
   321 
       
   322 	RSocket recvSocket;
       
   323 
       
   324 	CleanupClosePushL( recvSocket );
       
   325 	++cleanupCount;
       
   326 
       
   327 	TSockAddr recvSocketAddress;
       
   328 	Logger().WriteFormat(_L("Creating Receive Socket"));
       
   329 	if( !CreateUdpSocket( recvSocket, recvSocketAddress ) )
       
   330 		{
       
   331 		CleanupStack::PopAndDestroy( cleanupCount );
       
   332 		return EFail;
       
   333 		}
       
   334 
       
   335 
       
   336 
       
   337 	HBufC8 *udpWriteBuffer = HBufC8::NewLC( 0 ); // Empty buffer for zero-length write
       
   338 	++cleanupCount;
       
   339 
       
   340 	HBufC8 *udpReadBuffer  = HBufC8::NewMaxLC( 32 ); // Non-Empty buffer for reads - 32 is an unimportant magic number
       
   341 	++cleanupCount;
       
   342 
       
   343 	TPtrC8  ptrWritebuf( udpWriteBuffer->Des() );
       
   344 	TPtr8   ptrReadbuf( udpReadBuffer->Des() );
       
   345 	TSockAddr toAddress( recvSocketAddress );
       
   346 
       
   347 	TBool isOk = PerformSend( sendSocket, ptrWritebuf, toAddress );
       
   348 	if( isOk )
       
   349 		{
       
   350 		TSockAddr fromAddress;
       
   351 		isOk = PerformRecv( recvSocket, ptrReadbuf, fromAddress );
       
   352 		if( isOk )
       
   353 			{
       
   354 			Logger().WriteFormat(_L("Receieved %d bytes"), udpReadBuffer->Length());
       
   355 			if( udpReadBuffer->Length() != 0 )
       
   356 				{
       
   357 				isOk = EFalse;
       
   358 				}
       
   359 			}
       
   360 		}
       
   361 	
       
   362 	CleanupStack::PopAndDestroy( cleanupCount );
       
   363 
       
   364 	return isOk ? EPass : EFail;
       
   365 	}
       
   366 
       
   367 // Work around for timer oddness....
       
   368 /**
       
   369    Wait until either a request completes, or the specified period has elapsed
       
   370 
       
   371    @param aRequestStatus  Request to wait on
       
   372    @param aPeriod        Timeout in microseconds
       
   373 */
       
   374 void CEsockTest9_6::WaitFor( TRequestStatus& aRequestStatus, TInt aPeriod )
       
   375 	{
       
   376 	// Set up the timer
       
   377 	TRequestStatus timerStatus;
       
   378 	iGuardTimer.After( timerStatus, aPeriod );
       
   379 	// Wait for either the supplied event, or the timer, to complete
       
   380 	User::WaitForRequest( aRequestStatus, timerStatus );
       
   381 	if( timerStatus == KRequestPending )
       
   382 		{
       
   383 		// This is lifted from SockBench to solve some apparently
       
   384 		// strange behaviour with the timer.
       
   385 		
       
   386 		// Since we are still waiting for this timer, cancel it and
       
   387 		// wait for the request to be flagged.  I believe this is
       
   388 		// needed since the kernel may have setup a semaphore on the
       
   389 		// 'TimerStatus' Request Status.  The Cancel() will flag the
       
   390 		// status change (or maybe the timer will expire anyway) and
       
   391 		// if we don't clear it, we may crash later when we return
       
   392 		// to the Active Scheduler (since the TRequestStatus and
       
   393 		// RTimer won't exist).
       
   394 		//
       
   395 		iGuardTimer.Cancel();
       
   396 		User::WaitForRequest( timerStatus );
       
   397 		}
       
   398 	iGuardTimer.Cancel();
       
   399 	}
       
   400 
       
   401 
       
   402 /**
       
   403    Send data on a socket via SendTo
       
   404 
       
   405    @param aSocket  The socket to send the data on
       
   406    @param aDesc    The data to send
       
   407    @param anAddress The address to send to
       
   408    @return ETrue on success, EFalse on failure
       
   409 */
       
   410 TBool CEsockTest9_6::PerformSend( RSocket& aSocket, const TDesC8& aDesc, TSockAddr& anAddress )
       
   411 	{
       
   412 	TRequestStatus socketStatus;
       
   413 
       
   414 	aSocket.SendTo( aDesc, anAddress, 0, socketStatus );
       
   415 	WaitFor( socketStatus, KTimerPeriod );
       
   416 
       
   417 	Logger().WriteFormat(_L("UDP Send returns %d"), socketStatus.Int() );
       
   418 
       
   419 	if( socketStatus == KRequestPending )
       
   420 		{
       
   421 		aSocket.CancelSend();
       
   422 		Logger().WriteFormat(_L("Timeout on UDP Send"));
       
   423 		User::WaitForRequest( socketStatus );
       
   424 		return EFalse;
       
   425 		}
       
   426 
       
   427 	if( socketStatus != KErrNone )
       
   428 		{
       
   429 		Logger().WriteFormat(_L("UDP Send failed with error %d"), socketStatus.Int() );
       
   430 		return EFalse;
       
   431 		}
       
   432 
       
   433 	return ETrue;
       
   434 	}
       
   435 
       
   436 /**
       
   437    Receive data on a socket via RecvFrom
       
   438 
       
   439    @param aSocket The socket to recevie data from
       
   440    @param aDesc Descriptor to receive the data into
       
   441    @param anAddress On success, contains the address from which data was received
       
   442    @return ETrue on success, EFalse on failure
       
   443 */
       
   444 TBool CEsockTest9_6::PerformRecv( RSocket& aSocket, TDes8& aDesc, TSockAddr& anAddress )
       
   445 	{
       
   446 	TRequestStatus socketStatus;
       
   447 
       
   448 	aSocket.RecvFrom( aDesc, anAddress, 0, socketStatus );
       
   449 	WaitFor( socketStatus, KTimerPeriod );
       
   450 
       
   451 	Logger().WriteFormat(_L("UDP Recv returns %d"), socketStatus.Int() );
       
   452 
       
   453 	if( socketStatus == KRequestPending )
       
   454 		{
       
   455 		aSocket.CancelRecv();
       
   456 		Logger().WriteFormat(_L("Timeout on UDP Recv"));
       
   457 		User::WaitForRequest( socketStatus );
       
   458 		return EFalse;
       
   459 		}
       
   460 
       
   461 	if( socketStatus != KErrNone )
       
   462 		{
       
   463 		Logger().WriteFormat(_L("UDP Recv failed with error %d"), socketStatus.Int() );
       
   464 		return EFalse;
       
   465 		}
       
   466 
       
   467 	return ETrue;
       
   468 	}
       
   469 
       
   470 /**
       
   471    Create a UDP Socket and bind it to an arbitrary address
       
   472 
       
   473    @param aSocket The socket
       
   474    @param aAddress On return, contains the local socket name
       
   475    @return ETrue on success, EFalse otherwise
       
   476 */
       
   477 TBool CEsockTest9_6::CreateUdpSocket( RSocket& aSocket, TSockAddr& aAddress )
       
   478 	{
       
   479 	// Open the socket
       
   480 	TInt nRet = aSocket.Open( iEsockSuite->iSocketServer, KAfInet, KSockDatagram, KProtocolInetUdp );
       
   481 	if( KErrNone != nRet )
       
   482 		{
       
   483 		Logger().WriteFormat(_L("Failed to open socket: return value = <%d>"), nRet );
       
   484 		return EFalse;
       
   485 		}
       
   486 
       
   487 	// Bind to any address.
       
   488 	//TInetAddr myAddress( KInetAddrAny, KInetPortAny );
       
   489 	TInetAddr myAddress( KInetAddrLoop, KInetPortAny );
       
   490 	nRet = aSocket.Bind( myAddress );
       
   491 	if( KErrNone != nRet )
       
   492 		{
       
   493 		Logger().WriteFormat(_L("Failed to bind socket: return value = <%d>"), nRet );
       
   494 		return EFalse;
       
   495 		}
       
   496 
       
   497 	// Retrieve the bound address
       
   498 	aSocket.LocalName( aAddress );
       
   499 
       
   500 	// Happy shiny
       
   501 	return ETrue;
       
   502 	}
       
   503 
       
   504