datacommsserver/esockserver/test/TE_ESock/EsockTestSection3.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 3
       
    15 // 
       
    16 //
       
    17 
       
    18 // EPOC includes
       
    19 #include <e32base.h>
       
    20 #include <in_sock.h>
       
    21 
       
    22 // Test system includes
       
    23 #include "EsockTestSection3.h"
       
    24 
       
    25 static const TInt KMicrosecondsPerSecond = 1000000;
       
    26 
       
    27 // Test step 3.1
       
    28 const TDesC& CEsockTest3_1::GetTestName()
       
    29 	{
       
    30 	// store the name of this test case
       
    31 	_LIT(ret,"Test3.1");
       
    32 	
       
    33 	return ret;
       
    34 	}
       
    35 
       
    36 CEsockTest3_1::~CEsockTest3_1()
       
    37 	{
       
    38 	}
       
    39 
       
    40 enum TVerdict CEsockTest3_1::easyTestStepPreambleL()
       
    41 	{
       
    42 	if (KErrNone != OpenMinSockets(_L("Test_3.1"),KAfInet, KSockStream, KProtocolInetTcp))
       
    43 		{
       
    44 		return EFail;
       
    45 		}
       
    46 
       
    47 	return EPass;
       
    48 	}
       
    49 
       
    50 enum TVerdict CEsockTest3_1::easyTestStepL()
       
    51 	{
       
    52 	// Test 1.x Open a socket
       
    53 	
       
    54 	// Set the address to loopback and the port to 0, IPv4 or IPv6 address
       
    55 	TInetAddr addr;
       
    56 	TESTL(GetIpAddressFromConfig(SectionName(_L("Test_3.1")), _L("ipAddress"), addr));
       
    57 	
       
    58 	addr.SetPort(0);
       
    59 	
       
    60 	TRequestStatus stat;
       
    61 	for (TInt i = 1; i <= iEsockSuite->GetSocketListCount(); i++)
       
    62 		{
       
    63 		// Connect to the loopback address on port 0
       
    64 		iEsockSuite->GetSocketHandle(i).Connect(addr, stat);
       
    65 		User::WaitForRequest(stat);
       
    66 		TESTEL(stat==KErrGeneral, stat.Int());
       
    67 		}
       
    68 	
       
    69 	// Test 1.15 Close socket
       
    70 	
       
    71 	return EPass;
       
    72 	}
       
    73 
       
    74 
       
    75 // Test step 3.2
       
    76 const TDesC& CEsockTest3_2::GetTestName()
       
    77 	{
       
    78 	// store the name of this test case
       
    79 	_LIT(ret,"Test3.2");
       
    80 	
       
    81 	return ret;
       
    82 	}
       
    83 
       
    84 CEsockTest3_2::~CEsockTest3_2()
       
    85 	{
       
    86 	}
       
    87 
       
    88 enum TVerdict CEsockTest3_2::easyTestStepPreambleL()
       
    89 	{
       
    90 	if (KErrNone != OpenMinSockets(_L("Test_3.2"),KAfInet, KSockStream, KProtocolInetTcp))
       
    91 		{
       
    92 		return EFail;
       
    93 		}
       
    94 
       
    95 	return EPass;
       
    96 	}
       
    97 
       
    98 enum TVerdict CEsockTest3_2::easyTestStepL()
       
    99 	{
       
   100 	// Test 1.x Open a socket
       
   101 	
       
   102 	// Set the address to loopback IPv4 or IPv6 address
       
   103 	TInetAddr addr;
       
   104 	TESTL(GetIpAddressFromConfig(SectionName(_L("Test_3.2")), _L("ipAddress"), addr));
       
   105 	
       
   106 	// set the port numnber
       
   107 	addr.SetPort(65537);
       
   108 	
       
   109 	TRequestStatus stat;
       
   110 	for (TInt i = 1; i <= iEsockSuite->GetSocketListCount(); i++)
       
   111 		{
       
   112 		// Connect to the loopback address on port 65537
       
   113 		iEsockSuite->GetSocketHandle(i).Connect(addr, stat);
       
   114 		User::WaitForRequest(stat);
       
   115 		TESTEL(stat==KErrGeneral, stat.Int());
       
   116 		}
       
   117 	
       
   118 	// Test 1.15 Close socket
       
   119 	
       
   120 	return EPass;
       
   121 	}
       
   122 
       
   123 
       
   124 // Test step 3.3
       
   125 const TDesC& CEsockTest3_3::GetTestName()
       
   126 	{
       
   127 	// store the name of this test case
       
   128 	_LIT(ret,"Test3.3");
       
   129 	
       
   130 	return ret;
       
   131 	}
       
   132 
       
   133 CEsockTest3_3::~CEsockTest3_3()
       
   134 	{
       
   135 	}
       
   136 
       
   137 enum TVerdict CEsockTest3_3::easyTestStepPreambleL()
       
   138 	{
       
   139 	if (KErrNone != OpenMinSockets(_L("Test_3.3"),KAfInet, KSockStream, KProtocolInetTcp))
       
   140 		{
       
   141 		return EFail;
       
   142 		}
       
   143 
       
   144 	return EPass;
       
   145 	}
       
   146 
       
   147 enum TVerdict CEsockTest3_3::easyTestStepL()
       
   148 	{
       
   149 	// Test 1.x Open a socket
       
   150 	
       
   151 	// Set the address to loopback, IPv4 or IPv6 address
       
   152 	TInetAddr addr;
       
   153 	TESTL(GetIpAddressFromConfig(SectionName(_L("Test_3.3")), _L("ipAddress"), addr));
       
   154 	
       
   155 	// Set to a non listening port
       
   156 	TInt port;
       
   157 	TESTL(GetIntFromConfig(_L("Test_3.3"), _L("port"), port));
       
   158 	
       
   159 	addr.SetPort(port);
       
   160 	
       
   161 	TRequestStatus stat;
       
   162 	for (TInt i = 1; i <= iEsockSuite->GetSocketListCount(); i++)
       
   163 		{
       
   164 		// Connect to the loopback address on the non listening port n 
       
   165 		iEsockSuite->GetSocketHandle(i).Connect(addr, stat);
       
   166 		User::WaitForRequest(stat);
       
   167 		TESTEL(stat==KErrCouldNotConnect, stat.Int());
       
   168 		}
       
   169 	
       
   170 	// Test 1.15 Close socket
       
   171 	
       
   172 	return EPass;
       
   173 	}
       
   174 
       
   175 // Test step 3.4
       
   176 const TDesC& CEsockTest3_4::GetTestName()
       
   177 	{
       
   178 	// store the name of this test case
       
   179 	_LIT(ret,"Test3.4");
       
   180 	
       
   181 	return ret;
       
   182 	}
       
   183 
       
   184 CEsockTest3_4::~CEsockTest3_4()
       
   185 	{
       
   186 	}
       
   187 
       
   188 enum TVerdict CEsockTest3_4::easyTestStepL()
       
   189 	{
       
   190 	// Test 1.x Open a socket
       
   191 	
       
   192 	// Set the address to Remote host, IPv4 or IPv6 address
       
   193 	TInetAddr addr;
       
   194 	TESTL(GetIpAddressFromConfig(SectionName(_L("Test_3.4")), _L("ipAddress"), addr));
       
   195 	
       
   196 	// Set to non listening port
       
   197 	TInt port;
       
   198 	TESTL(GetIntFromConfig(_L("Test_3.4"), _L("port"), port));
       
   199 	addr.SetPort(port);
       
   200 	
       
   201 	TRequestStatus stat;
       
   202 	for (TInt i = 1; i <= iEsockSuite->GetSocketListCount(); i++)
       
   203 		{
       
   204 		// Connect to the remote host address on the non listening port n 
       
   205 		iEsockSuite->GetSocketHandle(i).Connect(addr, stat);
       
   206 		User::WaitForRequest(stat);
       
   207 		TESTEL(stat==KErrCouldNotConnect, stat.Int());
       
   208 		}
       
   209 	
       
   210 	// Test 1.15 Close socket
       
   211 	
       
   212 	return EPass;
       
   213 	}
       
   214 
       
   215 // Test step 3.5
       
   216 const TDesC& CEsockTest3_5::GetTestName()
       
   217 	{
       
   218 	// store the name of this test case
       
   219 	_LIT(ret,"Test3.5");
       
   220 	
       
   221 	return ret;
       
   222 	}
       
   223 
       
   224 CEsockTest3_5::~CEsockTest3_5()
       
   225 	{
       
   226 	}
       
   227 
       
   228 enum TVerdict CEsockTest3_5::easyTestStepPreambleL()
       
   229 	{
       
   230 	CloseSockets();
       
   231 	if (KErrNone != OpenMinSockets(_L("Test_3.5"),KAfInet, KSockStream, KProtocolInetTcp))
       
   232 		{
       
   233 		return EFail;
       
   234 		}
       
   235 
       
   236 	return EPass;
       
   237 	}
       
   238 
       
   239 enum TVerdict CEsockTest3_5::easyTestStepL()
       
   240 	{
       
   241 	TRequestStatus stat;
       
   242 	TInetAddr addr;
       
   243 	TInt port;
       
   244 	
       
   245 	// Test 1.x Open a socket
       
   246 	
       
   247 	// Set the address to Remote host, IPv4 or IPv6 address
       
   248 	TESTL(GetIpAddressFromConfig(_L("Test_Common"), _L("ipAddress"), addr));
       
   249 	
       
   250 	// Set to echo port
       
   251 	TESTL(GetIntFromConfig(SectionName(_L("Test_3.5")), _L("port"), port));
       
   252 	addr.SetPort(port);
       
   253 	
       
   254 	// Connect to the remote host address on the echo port 
       
   255 	iEsockSuite->GetSocketHandle(1).Connect(addr, stat);
       
   256 	iEsockSuite->GetSocketHandle(1).CancelConnect();
       
   257 	User::WaitForRequest(stat);
       
   258 	TESTEL(stat==KErrCancel, stat.Int());
       
   259 	
       
   260 	// Test 1.15 Close socket
       
   261 	
       
   262 	return EPass;
       
   263 	}
       
   264 
       
   265 
       
   266 // Test step 3.6
       
   267 const TDesC& CEsockTest3_6::GetTestName()
       
   268 	{
       
   269 	// store the name of this test case
       
   270 	_LIT(ret,"Test3.6");
       
   271 	
       
   272 	return ret;
       
   273 	}
       
   274 
       
   275 CEsockTest3_6::~CEsockTest3_6()
       
   276 	{
       
   277 	}
       
   278 
       
   279 
       
   280 enum TVerdict CEsockTest3_6::subTest2(RTimer	&aWatchdog,
       
   281 									  RSocket	&aSocket,
       
   282 									  TDes8		&aBuffer,
       
   283 									  TInt		aExpectedStatus)
       
   284 	{
       
   285 	TBool finished = EFalse;
       
   286 	TInt i = 1;
       
   287 	TRequestStatus sendStatus;
       
   288 	TRequestStatus recvStatus;
       
   289 	TRequestStatus timerStatus;
       
   290 	while ((i <= iNumTries) && !finished)
       
   291 		{
       
   292 		aWatchdog.After(timerStatus, 7 * KMicrosecondsPerSecond);
       
   293 		Logger().Write(_L("Issuing recv..."));
       
   294 		aSocket.Recv(aBuffer, 0, recvStatus);
       
   295 		Logger().Write(_L("Sending..."));
       
   296         aSocket.SendTo(aBuffer, iAddr, 0, sendStatus);
       
   297 
       
   298     	User::WaitForRequest(timerStatus, recvStatus);
       
   299         aWatchdog.Cancel();
       
   300         aSocket.CancelRecv();
       
   301     	User::WaitForAnyRequest();
       
   302     	
       
   303     	finished = recvStatus == aExpectedStatus && 
       
   304         (timerStatus == KRequestPending || timerStatus == KErrNone || timerStatus == KErrCancel);
       
   305 
       
   306     	if (!finished)
       
   307 			{
       
   308 			User::After(1 * KMicrosecondsPerSecond);
       
   309 			i++;
       
   310 			}
       
   311 		}
       
   312 
       
   313 	if (!finished)
       
   314 		{
       
   315 		return EFail;
       
   316 		}
       
   317 	return EPass;
       
   318 	}
       
   319 
       
   320 
       
   321 enum TVerdict CEsockTest3_6::easyTestStepPreambleL()
       
   322 	{
       
   323     // Get the ini file data.
       
   324 	_LIT(KIniTestKey,    "Test_3.6");
       
   325 
       
   326 	// Set the address to Remote host, IPv4 or IPv6 address
       
   327 	_LIT(KIniAddressKey, "ipAddress");
       
   328 	TESTL(GetIpAddressFromConfig(KIniTestKey(), KIniAddressKey(), iAddr));
       
   329 	
       
   330 	// Set to non-existent port
       
   331 	_LIT(KIniErrPortKey, "errorPort");
       
   332 	TESTL(GetIntFromConfig(KIniTestKey(), KIniErrPortKey, iErrorPort));
       
   333 	iAddr.SetPort(iErrorPort);
       
   334 
       
   335 	// Get the valid port
       
   336 	_LIT(KIniValPortKey, "validPort");
       
   337 	TESTL(GetIntFromConfig(KIniTestKey(), KIniValPortKey, iValidPort));
       
   338 	
       
   339 	// Get the number of times to try the sub-test before giving up.
       
   340 	_LIT(KIniValTries,   "validTries");
       
   341 	TESTL(GetIntFromConfig(KIniTestKey(), KIniValTries, iNumTries));
       
   342 
       
   343 	if (KErrNone != OpenMinSockets(KIniTestKey(), KAfInet, KSockDatagram, KProtocolInetUdp))
       
   344 		{
       
   345 		return EFail;
       
   346 		}
       
   347 	
       
   348    	// Enable notification of ICMP errors.
       
   349 	RSocket &sock = iEsockSuite->GetSocketHandle(1);
       
   350 	TInt ret = sock.SetOpt(KSoUdpReceiveICMPError, KSolInetUdp, 1);
       
   351 	if (KErrNone != ret)
       
   352 		{
       
   353 		Logger().WriteFormat(_L("SetOpt to receive ICMP errors returns %d"), ret);
       
   354 		return EFail;
       
   355 		}
       
   356 
       
   357 	return EPass;
       
   358 	}
       
   359 
       
   360 
       
   361 enum TVerdict CEsockTest3_6::easyTestStepL()
       
   362 	{
       
   363 	TESTL(EPass == TestStepResult());
       
   364 	TVerdict verdict = EPass;
       
   365 
       
   366     const TInt KStringMaxLen = 40;
       
   367     TBuf<KStringMaxLen> addressBuf;
       
   368 	iAddr.Output(addressBuf);
       
   369 	Logger().Write(_L(""));
       
   370 	Logger().WriteFormat(_L("Test3.6 - Testing address = %S, errorPort = %d, validPort = %d"), &addressBuf, iErrorPort, iValidPort);
       
   371 
       
   372 	RTimer         watchdog;
       
   373 	CleanupClosePushL(watchdog);
       
   374 	watchdog.CreateLocal();
       
   375 	
       
   376     const TInt KBufferLength = 10;  
       
   377 	TBuf8<KBufferLength> buffer;
       
   378 	buffer.SetMax();
       
   379 	buffer.FillZ();
       
   380 
       
   381 	RSocket &sock = iEsockSuite->GetSocketHandle(1);
       
   382 
       
   383     // Part 1. Invalid port#, no pending receive
       
   384     //
       
   385 	// First Recv returns -34, second one will pause.
       
   386     
       
   387 	Logger().WriteFormat(_L("Test3.6 Part 1 - Address = %S, errorPort = %d"), &addressBuf, iErrorPort);
       
   388 	
       
   389 	TBool finished = EFalse;
       
   390     TInt i = 1;
       
   391 	TRequestStatus status;
       
   392 	TRequestStatus recvStatus;
       
   393 	TRequestStatus timerStatus;
       
   394 	TInt ret;
       
   395     while ((i <= iNumTries)  && !finished)
       
   396 		{
       
   397     	status = recvStatus = timerStatus = KRequestPending;
       
   398 
       
   399     	sock.SendTo(buffer, iAddr, 0, status);
       
   400     	User::WaitForRequest(status);
       
   401     	ret = status.Int();
       
   402     	Logger().WriteFormat(_L("Send returned %d"), ret);
       
   403     	TESTL(KErrNone == ret);
       
   404     	
       
   405     	// Delay to allow the connection to come up.
       
   406     	User::After(5 * KMicrosecondsPerSecond);
       
   407 
       
   408 		watchdog.After(timerStatus, 5 * KMicrosecondsPerSecond);
       
   409 		sock.Recv(buffer, 0, recvStatus);
       
   410 		User::WaitForRequest(timerStatus, recvStatus);
       
   411 		watchdog.Cancel();
       
   412 		sock.CancelRecv();
       
   413 		User::WaitForAnyRequest();
       
   414 		ret = recvStatus.Int();
       
   415 		Logger().WriteFormat(_L("1st recv returned %d"), ret);
       
   416 		
       
   417         // This Recv should complete with -34.
       
   418     	finished = recvStatus == KErrCouldNotConnect && 
       
   419     		(timerStatus == KRequestPending || timerStatus == KErrNone || timerStatus == KErrCancel);
       
   420 
       
   421         if (finished)
       
   422         	{
       
   423         	recvStatus = timerStatus = KRequestPending;
       
   424 
       
   425     		watchdog.After(timerStatus, 5 * KMicrosecondsPerSecond);
       
   426     		sock.Recv(buffer, 0, recvStatus);
       
   427     		User::WaitForRequest(timerStatus, recvStatus);
       
   428     		watchdog.Cancel();
       
   429     		sock.CancelRecv();
       
   430     		User::WaitForAnyRequest();
       
   431     		ret = recvStatus.Int();
       
   432     		Logger().WriteFormat(_L("2nd recv returned %d"), ret);
       
   433     		
       
   434     		// This time we expect the watchdog to fire, an immediate Recv completion here means the
       
   435     		// test has failed (error not cleared down).
       
   436     		
       
   437     		finished = timerStatus == KErrNone && 
       
   438     			(recvStatus == KRequestPending || recvStatus == KErrNone || recvStatus == KErrCancel);
       
   439         	}
       
   440 
       
   441         if (!finished)
       
   442         	{
       
   443         	User::After(1 * KMicrosecondsPerSecond);
       
   444         	i++;
       
   445         	}
       
   446 		}
       
   447 
       
   448     if (!finished)
       
   449     	{
       
   450     	verdict = EFail;
       
   451     	}
       
   452     TESTL(EPass == verdict);
       
   453 
       
   454     // Part 2. Invalid port#, with pending receive; should complete with -34
       
   455 	Logger().WriteFormat(_L("Test3.6 Part 2 - Address = %S, errorPort = %d"), &addressBuf, iErrorPort);
       
   456 	TVerdict subTestVerdict = subTest2(watchdog, sock, buffer, KErrCouldNotConnect);
       
   457 	if (verdict == EPass)
       
   458 		{
       
   459 		verdict = subTestVerdict;
       
   460 		}
       
   461     TESTL(EPass == verdict);
       
   462 
       
   463     // Part 3. Valid port#, with pending receive; should complete with KErrNone
       
   464 	Logger().WriteFormat(_L("Test3.6 Part 3 - Address = %S, validPort = %d"), &addressBuf, iValidPort);
       
   465 	iAddr.SetPort(iValidPort);
       
   466 	subTestVerdict = subTest2(watchdog, sock, buffer, KErrNone);
       
   467 	if (verdict == EPass)
       
   468 		{
       
   469 		verdict = subTestVerdict;
       
   470 		}
       
   471     TESTL(EPass == verdict);
       
   472 
       
   473 	// Test 1.15 Close socket
       
   474 	CleanupStack::PopAndDestroy(1, &watchdog);
       
   475     return EPass;
       
   476 	}
       
   477