datacommsserver/esockserver/test/TE_ESock/EsockTestSection5.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 5
       
    15 // EPOC includes
       
    16 // 
       
    17 //
       
    18 
       
    19 #include <e32base.h>
       
    20 #include <in_sock.h>
       
    21 #include <e32math.h>
       
    22 
       
    23 //anonymous namespace
       
    24 namespace
       
    25     {
       
    26     //once the aRefBoolFlag set to false it never gets set again...
       
    27     void setResultFlagBool(const TBool aFlag, TBool& aRefBoolFlag)
       
    28         {
       
    29         if (aRefBoolFlag)
       
    30             {
       
    31             aRefBoolFlag = aFlag;
       
    32             }
       
    33         }
       
    34     
       
    35     //once the aRefBoolFlag set to false it never gets set again...
       
    36      void setResultFlagInt(const TInt aFlag, TBool& aRefBoolFlag)
       
    37         {
       
    38         if (aRefBoolFlag)
       
    39             {
       
    40             if (aFlag != KErrNone)
       
    41                 {
       
    42                     aRefBoolFlag = EFalse;
       
    43                 }
       
    44             }
       
    45         }
       
    46         
       
    47         const TUint KMaxTestStepCounter = 5;
       
    48     }
       
    49 // Test system includes
       
    50 #include "EsockTestSection5.h"
       
    51 
       
    52 // Test step SendData
       
    53 // this test is used by the integration test scripts
       
    54 // and is based on esock test 5.3
       
    55 // This test step sends TCP and UDP (if enabled in ini file) data
       
    56 const TDesC& CEsockSendData::GetTestName()
       
    57 	{
       
    58 	// store the name of this test case
       
    59 	_LIT(ret,"SendData");
       
    60 	
       
    61 	return ret;
       
    62 	}
       
    63 
       
    64 CEsockSendData::~CEsockSendData()
       
    65 	{
       
    66 	}
       
    67 
       
    68 enum TVerdict CEsockSendData::easyTestStepL()
       
    69 	{
       
    70 	TInetAddr addrRemote, addr;
       
    71 	TRequestStatus stat(KErrNone);
       
    72 	TInt connectTimer, Port;
       
    73 	TBool UdpEnable;
       
    74 	
       
    75 	TInt testStepCounter = 0;
       
    76 	TBool testStepResult;
       
    77 	TBool socketConnected = EFalse;
       
    78 	TBool udpBindOK = EFalse;
       
    79 	
       
    80 	do
       
    81 	    {
       
    82 	    testStepResult = ETrue;
       
    83 	    
       
    84     	// get ip address to connect to (on echo port)
       
    85     	TESTL(GetIpAddressFromConfig(_L("Test_Common"), _L("ipAddress"), addrRemote));
       
    86     	
       
    87     	// get the port number
       
    88     	TESTL(GetIntFromConfig(_L("SendData"), _L("Port"), Port));
       
    89     	addrRemote.SetPort(Port);
       
    90     	
       
    91     	// format for display 
       
    92     	TBuf<39> AddrBuf;
       
    93     	addrRemote.OutputWithScope(AddrBuf);
       
    94     	
       
    95     	// check if UDP test enabled
       
    96     	TBool res = GetBoolFromConfig(_L("SendData"), _L("UdpEnable"), UdpEnable );
       
    97     	
       
    98     	if (( res == EFalse ) || ( UdpEnable == EFalse ))
       
    99     		{
       
   100     		Logger().WriteFormat( _L("UDP test disabled"));
       
   101     		UdpEnable = EFalse;
       
   102     		}
       
   103     	
       
   104     	// get a value for the connection guard timer 
       
   105     	TESTL(GetIntFromConfig(_L("SendData"), _L("connectTimer"), connectTimer));
       
   106     	
       
   107     	// create timer to use as a guard timer
       
   108     	RTimer GuardTimer;
       
   109     	GuardTimer.CreateLocal();			// for this thread
       
   110     	TRequestStatus TimerStatus;			// status to monitor timer
       
   111     	TInt RepeatCount = 0;				// repeat counter 
       
   112     	TInt WaitTimeout = 5000000;
       
   113 
       
   114         if (!socketConnected)
       
   115             {
       
   116         	// try 5 times (sometimes it fails because of test network)
       
   117         	for(RepeatCount=0; RepeatCount <=5;  RepeatCount++)
       
   118         		{
       
   119         		// connect
       
   120         		(iEsockSuite->GetSocketHandle(1)).Connect(addrRemote, stat);
       
   121         		
       
   122         		// start timer
       
   123         		GuardTimer.After(TimerStatus,connectTimer * 1000000); 
       
   124         		
       
   125         		// wait for either timer or connect
       
   126         		User::WaitForRequest(stat, TimerStatus);
       
   127         		GuardTimer.Cancel();
       
   128         		User::WaitForAnyRequest();
       
   129         		
       
   130         		// check connect result
       
   131         		if (stat.Int() == KRequestPending)
       
   132         			{
       
   133         			(iEsockSuite->GetSocketHandle(1)).CancelConnect();
       
   134         			Logger().WriteFormat( _L("Socket connect to %S port %d has not completed within %d seconds (connectTimer from ini) "), 
       
   135         				&AddrBuf,
       
   136         				Port,
       
   137         				connectTimer ); 
       
   138         			User::After(WaitTimeout);
       
   139         			continue; // next attempt
       
   140         			}
       
   141         		else if(stat.Int() != KErrNone)
       
   142         			{
       
   143         			Logger().WriteFormat( _L("Socket connect failed with error %d"), stat.Int());
       
   144         			if(RepeatCount <= 5)
       
   145         				{
       
   146         				Logger().WriteFormat( _L("Try to reconnect. RepeatCount %d"), RepeatCount);
       
   147         				User::After(WaitTimeout);
       
   148         				continue; // next attempt
       
   149         				}
       
   150         		    else
       
   151         		        {
       
   152         		        setResultFlagInt(stat.Int(), testStepResult);
       
   153         		        }
       
   154         			}
       
   155         		else
       
   156         		    {
       
   157         		    socketConnected = ETrue;
       
   158         		    }
       
   159         		
       
   160         		break; // exit "for"
       
   161         		}
       
   162         	
       
   163         	TESTEL((stat.Int()==KErrNone),stat.Int());
       
   164             }
       
   165     	
       
   166     	// get number of packets and size of packets 
       
   167     	TInt PacketSize = 0;
       
   168     	TESTL(GetIntFromConfig(_L("SendData"), _L("PacketSize"), PacketSize));
       
   169     	TInt NoPackets = 0;
       
   170     	TESTL(GetIntFromConfig(_L("SendData"), _L("NoPackets"), NoPackets));
       
   171     	Logger().WriteFormat( _L("Sending to %S port %d TCP data %d packets of %d bytes = %d"), 
       
   172     		&AddrBuf, Port, NoPackets, PacketSize, NoPackets * PacketSize);
       
   173     	
       
   174     	// set up data buffers
       
   175     	HBufC8 * writebuf = HBufC8::NewMaxLC( PacketSize );
       
   176     	HBufC8 * readbuf  = HBufC8::NewMaxLC( PacketSize );
       
   177     	TPtr8 ptrWritebuf = writebuf->Des();
       
   178     	TPtr8 ptrReadbuf = readbuf->Des();
       
   179     	
       
   180     	// small buffer for data pattern to be sent
       
   181     	TBuf8<50> Data;
       
   182     	
       
   183     	// send data (NoPackets packets of PacketSize bytes)
       
   184     	TInt recvCount = 0;
       
   185     	for (TInt i = 0; i < NoPackets; i++)
       
   186     		{
       
   187     		// initialise data
       
   188     		Data.Format(_L8("TCP-packet:%d helloworld"),i);
       
   189     		ptrWritebuf.Repeat( Data );
       
   190     		
       
   191     		// write data 
       
   192     		GuardTimer.After(TimerStatus,connectTimer * 1000000); 
       
   193     		(iEsockSuite->GetSocketHandle(1)).Write(ptrWritebuf, stat);
       
   194     		User::WaitForRequest(stat, TimerStatus);
       
   195     		GuardTimer.Cancel();
       
   196     		User::WaitForAnyRequest();
       
   197     		
       
   198     		if (stat == KRequestPending)
       
   199     			{
       
   200     			(iEsockSuite->GetSocketHandle(1)).CancelWrite();
       
   201     			Logger().WriteFormat(_L("Timeout on Tcp Send packet %d after %d seconds"), i, connectTimer);
       
   202     			setResultFlagInt(stat.Int(), testStepResult);
       
   203     			}
       
   204     		else if (stat != KErrNone)
       
   205     			{
       
   206     			Logger().WriteFormat(_L("Tcp Send packet %d failed with error %d"), i, stat.Int());
       
   207     			setResultFlagInt(stat.Int(), testStepResult);
       
   208     			}
       
   209     		TESTEL( stat == KErrNone, stat.Int() );
       
   210     		
       
   211     		// read data (from echo server)
       
   212     		GuardTimer.After(TimerStatus,connectTimer * 1000000); 
       
   213     		(iEsockSuite->GetSocketHandle(1)).Read(ptrReadbuf, stat);
       
   214     		User::WaitForRequest(stat, TimerStatus);
       
   215     		GuardTimer.Cancel();
       
   216     		User::WaitForAnyRequest();
       
   217     		
       
   218     		TESTE( stat == KErrNone, stat.Int() );
       
   219     		if (stat == KRequestPending)
       
   220     			{
       
   221     			(iEsockSuite->GetSocketHandle(1)).CancelRead();
       
   222     			Logger().WriteFormat(_L("Timeout on Tcp Recv packet %d after %d seconds"), i, connectTimer);
       
   223     			setResultFlagInt(stat.Int(), testStepResult);
       
   224     			}
       
   225     		else if (stat != KErrNone)
       
   226     			{
       
   227     			Logger().WriteFormat(_L("Tcp Recv packet %d failed with error %d"), i, stat.Int());
       
   228     			setResultFlagInt(stat.Int(), testStepResult);
       
   229     			}
       
   230     		else
       
   231     			{
       
   232     			// compare the data
       
   233     			TEST( ptrWritebuf.Compare( ptrReadbuf ) == 0);
       
   234     			recvCount+=ptrReadbuf.Length();
       
   235     			}
       
   236     		}
       
   237     	
       
   238     	// check the total received.
       
   239     	TEST(recvCount==(NoPackets*PacketSize));
       
   240     	
       
   241     	//** UDP STARTS HERE
       
   242     	// test UDP if required
       
   243     	if ( UdpEnable )
       
   244     		{
       
   245     		TESTL(GetIntFromConfig(_L("SendData"), _L("UdpPacketSize"), PacketSize));
       
   246     		
       
   247     		// get the port number
       
   248     		// Can't send back on Echo port as it would enter an infinite loop
       
   249     		if (!GetIntFromConfig(_L("SendData"), _L("UdpPort"), Port))
       
   250     			// use default
       
   251     			Port = 9200;
       
   252     		
       
   253     		Logger().WriteFormat( _L("Sending to %S Port %d UDP data %d packets of %d bytes = %d"), 
       
   254     			&AddrBuf, Port, NoPackets, PacketSize, NoPackets * PacketSize);
       
   255     		
       
   256     		
       
   257     		// and make some new buffers the correct size
       
   258     		HBufC8 * UdpWritebuf = HBufC8::NewMaxLC( PacketSize );
       
   259     		HBufC8 * UdpReadbuf  = HBufC8::NewMaxLC( PacketSize );
       
   260     		TPtr8 ptrUdpWritebuf = UdpWritebuf->Des();
       
   261     		TPtr8 ptrUdpReadbuf = UdpReadbuf->Des();
       
   262     		
       
   263     		// open a udp socket
       
   264     		RSocket UdpSock;
       
   265     		TInt nRet = UdpSock.Open(iEsockSuite->iSocketServer, KAfInet, KSockDatagram, KProtocolInetUdp);
       
   266     		if (nRet != KErrNone)
       
   267     			{
       
   268     			Logger().WriteFormat(_L("Failed to open socket: return value = <%d>"), nRet);
       
   269     			setResultFlagBool(EFail, testStepResult);
       
   270     			//return EFail;  
       
   271     			}
       
   272     		
       
   273     		if (testStepResult)
       
   274     		    {
       
   275             		addr.SetPort(Port);
       
   276             		
       
   277             		// Bind
       
   278             		TInt result = UdpSock.SetOpt(KSoUdpSynchronousSend, KSolInetUdp, 1 );
       
   279             		
       
   280             		UdpSock.SetOpt(KSoReuseAddr, KSolInetIp, KMaxTestStepCounter);
       
   281             		
       
   282             		TESTEL( KErrNone ==result,result);
       
   283             		
       
   284             		if (!udpBindOK)
       
   285             		    {
       
   286                 		result = UdpSock.Bind(addr);
       
   287                 		TESTEL( KErrNone ==result,result);
       
   288                 		udpBindOK = ETrue;
       
   289             		    }
       
   290             		
       
   291             		
       
   292             		// write & read back the data
       
   293             		recvCount = 0;
       
   294             		for (TInt j = 0; j < NoPackets; j++)
       
   295             			{
       
   296             			// initialise data
       
   297             			Data.Format(_L8("UDP-packet:%d helloworld"),j);
       
   298             			ptrUdpWritebuf.Repeat( Data );
       
   299             			
       
   300             			// write data 
       
   301             			GuardTimer.After(TimerStatus,connectTimer * 1000000); 
       
   302             			UdpSock.SendTo(ptrUdpWritebuf, addrRemote, 0, stat);
       
   303             			User::WaitForRequest(stat, TimerStatus);
       
   304             			GuardTimer.Cancel();
       
   305             			User::WaitForAnyRequest();
       
   306             			
       
   307             			if (stat == KRequestPending)
       
   308             				{
       
   309             				UdpSock.CancelSend();
       
   310             				Logger().WriteFormat(_L("Timeout on Udp Send packet %d after %d seconds"), j, connectTimer);
       
   311             				setResultFlagInt(stat.Int(), testStepResult);
       
   312                             
       
   313             				}
       
   314             			else if (stat != KErrNone)
       
   315             				{
       
   316             				Logger().WriteFormat(_L("Udp Send packet %d failed with error %d"), j, stat.Int());
       
   317             				setResultFlagInt(stat.Int(), testStepResult);
       
   318             				}
       
   319             			
       
   320             			// read data (from echo server)
       
   321             			GuardTimer.After(TimerStatus,connectTimer * 1000000); 
       
   322             			UdpSock.RecvFrom(ptrUdpReadbuf, addr, 0, stat);
       
   323             			User::WaitForRequest(stat, TimerStatus);
       
   324             			GuardTimer.Cancel();
       
   325             			User::WaitForAnyRequest();
       
   326             			
       
   327             			if (stat == KRequestPending)
       
   328             				{
       
   329             				UdpSock.CancelRecv();
       
   330             				Logger().WriteFormat(_L("Timeout on Udp Recv packet %d after %d seconds"), j, connectTimer);
       
   331             				setResultFlagInt(stat.Int(), testStepResult);
       
   332             				}
       
   333             			else if (stat.Int() != KErrNone) 
       
   334             				{
       
   335             				Logger().WriteFormat(_L("Udp Recv packet %d failed with error %d"), j, stat.Int());
       
   336             				setResultFlagInt(stat.Int(), testStepResult);
       
   337             				}
       
   338             			//TEST( stat == KErrNone );  ** We could have packet loss with UDP in real life
       
   339             			else
       
   340             				{
       
   341             				// compare the data
       
   342             				TEST( ptrUdpWritebuf.Compare( ptrUdpReadbuf ) == 0);
       
   343             				recvCount += ptrUdpReadbuf.Length();
       
   344             				}
       
   345             			}
       
   346             		
       
   347             		Logger().WriteFormat(_L("Udp data sent %d bytes, data received %d bytes"),(NoPackets*PacketSize),recvCount);
       
   348             		
       
   349             		// check the total received (95 per cent is allowable for us)
       
   350             		//TESTE( recvCount >= (NoPackets*PacketSize), (NoPackets*PacketSize) );
       
   351             				
       
   352             		// close the UDP socket
       
   353                     UdpSock.Close();
       
   354     		    }
       
   355             
       
   356     		// get rid of the old buffers 
       
   357     		CleanupStack::PopAndDestroy();	//	UdpWritebuf;
       
   358     		CleanupStack::PopAndDestroy();	//	UdpReadbuf;
       
   359     		}
       
   360     		
       
   361     	CleanupStack::PopAndDestroy();	//	writebuf;
       
   362     	CleanupStack::PopAndDestroy();	//	readbuf;
       
   363     	
       
   364     	
       
   365     	if (!testStepResult)
       
   366     	    {
       
   367     	    Logger().WriteFormat(_L("TestStep failed %d time(s) from %d. Trying further..."), testStepCounter, KMaxTestStepCounter);
       
   368     	    
       
   369     	    ++testStepCounter;
       
   370     	    }
       
   371     	
       
   372 	    }
       
   373 	    while ( (testStepCounter < KMaxTestStepCounter) &&
       
   374 	          (!testStepResult) ); //big while...
       
   375          
       
   376 
       
   377     if ( testStepResult )
       
   378         {
       
   379 	    return EPass;
       
   380         }
       
   381     else
       
   382         {
       
   383         return EFail;
       
   384         }
       
   385 	}
       
   386 	
       
   387 	
       
   388 // Test step SendTcpData
       
   389 // this test is used by the integration test scripts
       
   390 // and is based on esock test 5.3
       
   391 // This test step sends TCP data only
       
   392 const TDesC& CEsockSendTcpData::GetTestName()
       
   393 	{
       
   394 	// store the name of this test case
       
   395 	_LIT(ret,"SendTcpData");
       
   396 	
       
   397 	return ret;
       
   398 	}
       
   399 
       
   400 CEsockSendTcpData::~CEsockSendTcpData()
       
   401 	{
       
   402 	}
       
   403 
       
   404 enum TVerdict CEsockSendTcpData::easyTestStepL()
       
   405 	{
       
   406 	TInetAddr addrRemote;
       
   407 	TRequestStatus stat;
       
   408 	TInt connectTimer, Port;
       
   409 	
       
   410 	// get ip address to connect to (on echo port)
       
   411 	TESTL(GetIpAddressFromConfig(_L("Test_Common"), _L("ipAddress"), addrRemote));
       
   412 	
       
   413 	// get the port number
       
   414 	TESTL(GetIntFromConfig(_L("SendData"), _L("Port"), Port));
       
   415 	addrRemote.SetPort(Port);
       
   416 	
       
   417 	// format for display 
       
   418 	TBuf<39> AddrBuf;
       
   419 	addrRemote.OutputWithScope(AddrBuf);
       
   420 	
       
   421 	// get a value for the connection guard timer 
       
   422 	TESTL(GetIntFromConfig(_L("SendData"), _L("connectTimer"), connectTimer));
       
   423 	
       
   424 	// create timer to use as a guard timer
       
   425 	RTimer GuardTimer;
       
   426 	GuardTimer.CreateLocal();			// for this thread
       
   427 	TRequestStatus TimerStatus;			// status to monitor timer
       
   428 	
       
   429     // connect
       
   430     (iEsockSuite->GetSocketHandle(1)).Connect(addrRemote, stat);
       
   431 	
       
   432 	// start timer
       
   433 	GuardTimer.After(TimerStatus,connectTimer * 1000000); 
       
   434 	
       
   435 	// wait for either timer or connect
       
   436 	User::WaitForRequest(stat, TimerStatus);
       
   437 	GuardTimer.Cancel();
       
   438 	User::WaitForAnyRequest();
       
   439 	
       
   440 	// check connect result
       
   441 	if (stat!=KErrNone)
       
   442 		{
       
   443 		// either connect failed or timer expired
       
   444 		// because it may be the timer cancel connect
       
   445 		(iEsockSuite->GetSocketHandle(1)).CancelConnect();
       
   446 		
       
   447 		Logger().WriteFormat( _L("Socket connect has not completed within %d seconds (connectTimer from ini) "), connectTimer ); 
       
   448 		
       
   449 		// report this failure and leave
       
   450 		TESTL((stat==KErrNone));
       
   451 		}
       
   452 	
       
   453 	// get number of packets and size of packets 
       
   454 	TInt PacketSize = 0;
       
   455 	TESTL(GetIntFromConfig(_L("SendData"), _L("PacketSize"), PacketSize));
       
   456 	TInt NoPackets = 0;
       
   457 	TESTL(GetIntFromConfig(_L("SendData"), _L("NoPackets"), NoPackets));
       
   458 	Logger().WriteFormat( _L("Sending to %S TCP data %d packets of %d bytes = %d"), 
       
   459 		&AddrBuf, NoPackets, PacketSize, NoPackets * PacketSize);
       
   460 	
       
   461 	// set up data buffers
       
   462 	HBufC8 * writebuf = HBufC8::NewMaxLC( PacketSize );
       
   463 	HBufC8 * readbuf  = HBufC8::NewMaxLC( PacketSize );
       
   464 	TPtr8 ptrWritebuf = writebuf->Des();
       
   465 	TPtr8 ptrReadbuf = readbuf->Des();
       
   466 	
       
   467 	// initialise data
       
   468 	TBuf8<50> Data;
       
   469 	
       
   470 	// send data (NoPackets packets of PacketSize bytes)
       
   471 	TInt recvCount = 0;
       
   472 	for (TInt i = 0; i < NoPackets; i++)
       
   473 		{
       
   474 		// initialise data
       
   475 		Data.Format(_L8("TCP-packet:%d helloworld"),i);
       
   476 		ptrWritebuf.Repeat( Data );
       
   477 		
       
   478 		// write data 
       
   479 		GuardTimer.After(TimerStatus,connectTimer * 1000000); 
       
   480 		(iEsockSuite->GetSocketHandle(1)).Write(ptrWritebuf, stat);
       
   481 		User::WaitForRequest(stat, TimerStatus);
       
   482 		GuardTimer.Cancel();
       
   483 		User::WaitForAnyRequest();
       
   484 		
       
   485 		if (stat != KErrNone) Logger().WriteFormat(_L("Timeout on Tcp Send after %d seconds"), connectTimer);
       
   486 		TESTL( stat == KErrNone );
       
   487 		
       
   488 		// read data (from echo server)
       
   489 		GuardTimer.After(TimerStatus,connectTimer * 1000000); 
       
   490 		(iEsockSuite->GetSocketHandle(1)).Read(ptrReadbuf, stat);
       
   491 		User::WaitForRequest(stat, TimerStatus);
       
   492 		GuardTimer.Cancel();
       
   493 		User::WaitForAnyRequest();
       
   494 		
       
   495 		if (stat != KErrNone) Logger().WriteFormat(_L("Timeout on Tcp Recv after %d seconds"), connectTimer);
       
   496 		TESTL( stat == KErrNone );
       
   497 		
       
   498 		// compare the data
       
   499 		TESTL( ptrWritebuf.Compare( ptrReadbuf ) == 0);
       
   500 		
       
   501 		recvCount+=ptrReadbuf.Length();
       
   502 		}
       
   503 	
       
   504 	// check the total received.
       
   505 	TESTL(recvCount==(NoPackets*PacketSize));
       
   506 	
       
   507 	CleanupStack::PopAndDestroy();	//	writebuf;
       
   508 	CleanupStack::PopAndDestroy();	//	readbuf;
       
   509 	
       
   510 	return EPass;
       
   511 	}
       
   512 	
       
   513 // Open Udp Socket
       
   514 const TDesC& CEsockOpenUdpSocket::GetTestName()
       
   515 	{
       
   516 	// store the name of this test case
       
   517 	_LIT(ret,"OpenUdpSocket");
       
   518 	
       
   519 	return ret;
       
   520 	}
       
   521 
       
   522 CEsockOpenUdpSocket::~CEsockOpenUdpSocket()
       
   523 	{
       
   524 	}
       
   525 
       
   526 enum TVerdict CEsockOpenUdpSocket::easyTestStepL()
       
   527 	{
       
   528 	TInt nRet;
       
   529 	TInt nSockets; // number of sockets to open
       
   530 	TInetAddr addr;
       
   531 	TBuf<32> buf;
       
   532 	TInt i;
       
   533 	
       
   534 	RSocket sock;
       
   535 	
       
   536 	// get number of sockets from script
       
   537 	TESTL(GetIntFromConfig(SectionName(_L("Test_1.2")), _L("numSockets"), nSockets));
       
   538 	
       
   539 	Logger().WriteFormat(_L("Open <%d> UDP socket(s)"), nSockets);
       
   540 	for (i = 0; i < nSockets; i++)
       
   541 		{
       
   542 		
       
   543 		// open a UDP socket
       
   544 		nRet = sock.Open(iEsockSuite->iSocketServer, KAfInet, KSockDatagram, KProtocolInetUdp);
       
   545 		
       
   546 		if (nRet != KErrNone)
       
   547 			{
       
   548 			
       
   549 			Logger().WriteFormat(_L("Failed to open socket: return value = <%d>"), nRet);
       
   550 			return EFail;  // do not try to open more sockets
       
   551 			}
       
   552 		
       
   553 		// store socket handle
       
   554 		iEsockSuite->AddSocketToListL(sock);
       
   555 		}
       
   556 	
       
   557 	for (i = 0; i < nSockets; i++)
       
   558 		{
       
   559 		// Get local address of the socket (Empty)
       
   560 		(iEsockSuite->GetSocketHandle(i + 1)).LocalName(addr);
       
   561 		
       
   562 		// Check length of buffer, should be set to (Zero)
       
   563 		addr.OutputWithScope(buf);
       
   564 		TESTL(buf.Length() == 0);
       
   565 		}
       
   566 	
       
   567 	return EPass;
       
   568 	}
       
   569 
       
   570 // send Udp Data
       
   571 const TDesC& CEsockSendUdpData::GetTestName()
       
   572 	{
       
   573 	// store the name of this test case
       
   574 	_LIT(ret,"SendUdpData");
       
   575 	
       
   576 	return ret;
       
   577 	}
       
   578 
       
   579 CEsockSendUdpData::~CEsockSendUdpData()
       
   580 	{
       
   581 	}
       
   582 
       
   583 enum TVerdict CEsockSendUdpData::easyTestStepL()
       
   584 	{
       
   585 	TInt testStepCounter = 0;
       
   586 	TBool bindIsOK = EFalse;
       
   587 	TBool testStepResult;
       
   588 	
       
   589 	TInetAddr addrRemote, addr;
       
   590 	TRequestStatus stat;
       
   591 	TInt connectTimer, Port;
       
   592 	TInt result;
       
   593 	
       
   594 	do
       
   595 	    {
       
   596 	    testStepResult = ETrue;
       
   597 	    
       
   598     	// get ip address to connect to (on echo port)
       
   599     	TESTL(GetIpAddressFromConfig(_L("Test_Common"), _L("ipAddress"), addrRemote));
       
   600     	
       
   601     	// get the port number
       
   602     	TESTL(GetIntFromConfig(_L("SendData"), _L("Port"), Port));
       
   603     	addrRemote.SetPort(Port);
       
   604     	
       
   605     	// get the port number
       
   606     	if(!GetIntFromConfig(_L("SendData"), _L("UdpPort"), Port))
       
   607     		Port = 9200;  // WAP UDP by default
       
   608     	
       
   609     	// Can't send back on Echo port as it would enter an infinite loop
       
   610     	addr.SetPort(Port);
       
   611     	
       
   612     	// format for display 
       
   613     	TBuf<39> AddrBuf;
       
   614     	addrRemote.OutputWithScope(AddrBuf);
       
   615     	
       
   616     	// get a value for the connection guard timer 
       
   617     	TESTL(GetIntFromConfig(_L("SendData"), _L("connectTimer"), connectTimer));
       
   618     	
       
   619     	// create timer to use as a guard timer
       
   620     	RTimer GuardTimer;
       
   621     	GuardTimer.CreateLocal();			// for this thread
       
   622     	TRequestStatus TimerStatus;			// status to monitor timer
       
   623     	
       
   624     	// get number of packets and size of packets 
       
   625     	TInt PacketSize = 0;
       
   626     	TESTL(GetIntFromConfig(_L("SendData"), _L("UdpPacketSize"), PacketSize));
       
   627     	TInt NoPackets = 0;
       
   628     	TESTL(GetIntFromConfig(_L("SendData"), _L("NoPackets"), NoPackets));
       
   629     	Logger().WriteFormat( _L("Send Udp Data to %S port %d in %d packets of %d bytes = %d"), 
       
   630     		&AddrBuf, Port, NoPackets, PacketSize, NoPackets * PacketSize);
       
   631     	
       
   632     	// and make some new buffers the correct size
       
   633     	HBufC8 * UdpWritebuf = HBufC8::NewMaxLC( PacketSize );
       
   634     	HBufC8 * UdpReadbuf  = HBufC8::NewMaxLC( PacketSize );
       
   635     	TPtr8 ptrUdpWritebuf = UdpWritebuf->Des();
       
   636     	TPtr8 ptrUdpReadbuf = UdpReadbuf->Des();
       
   637     	
       
   638     	result = (iEsockSuite->GetSocketHandle(1)).SetOpt(KSoUdpSynchronousSend, KSolInetUdp, 1 );
       
   639     	
       
   640     	// Bind
       
   641     	if (!bindIsOK)
       
   642     	    {
       
   643     	    result = (iEsockSuite->GetSocketHandle(1)).Bind(addr);
       
   644     	    TESTEL( KErrNone ==result,result);
       
   645     	    bindIsOK = ETrue;
       
   646     	    }
       
   647     	
       
   648     	TBuf8<50> Data;
       
   649     	
       
   650     	// write & read back the data
       
   651     	TInt recvCount = 0;
       
   652     	for (TInt i = 0; i < NoPackets; i++)
       
   653     		{
       
   654     		// initialise data
       
   655     		Data.Format(_L8("UDP-packet:%d helloworld"),i);
       
   656     		ptrUdpWritebuf.Repeat( Data );
       
   657     		
       
   658     		// write data 
       
   659     		GuardTimer.After(TimerStatus,connectTimer * 1000000); 
       
   660     		(iEsockSuite->GetSocketHandle(1)).SendTo(ptrUdpWritebuf, addrRemote, 0, stat);
       
   661     		User::WaitForRequest(stat, TimerStatus);
       
   662     		GuardTimer.Cancel();
       
   663     		User::WaitForAnyRequest();
       
   664     		
       
   665     		if (stat.Int() != KErrNone)
       
   666     		    {
       
   667     			Logger().WriteFormat(_L("Error on Udp Send after %d seconds, reason"), connectTimer, stat.Int());
       
   668     			setResultFlagInt(stat.Int(), testStepResult);
       
   669     		    }
       
   670     		
       
   671     		//TESTE( stat.Int() == KErrNone, stat.Int() );
       
   672     		
       
   673     		// read data (from echo server)
       
   674     		GuardTimer.After(TimerStatus,connectTimer * 1000000); 
       
   675     		(iEsockSuite->GetSocketHandle(1)).RecvFrom(ptrUdpReadbuf, addr, 0, stat);
       
   676     		User::WaitForRequest(stat, TimerStatus);
       
   677     		GuardTimer.Cancel();
       
   678     		User::WaitForAnyRequest();
       
   679     		
       
   680     		if (stat.Int() != KErrNone) 
       
   681     			{
       
   682     			Logger().WriteFormat(_L("Error on Udp Recv (packet %d) after %d seconds, reason %d"), i, connectTimer, stat.Int());
       
   683     			(iEsockSuite->GetSocketHandle(1)).CancelRecv();
       
   684     			setResultFlagInt(stat.Int(), testStepResult);
       
   685     			//TESTE( (stat.Int() == KErrNone), stat.Int() );  ** this is UDP, packet loss possible
       
   686     			}
       
   687     		else
       
   688     			{
       
   689     			// compare the data
       
   690     			TEST( ptrUdpWritebuf.Compare( ptrUdpReadbuf ) == 0 );
       
   691     			recvCount += ptrUdpReadbuf.Length();
       
   692     			}
       
   693     		
       
   694     		}
       
   695     	// Log what the % of the data was received!
       
   696     	Logger().WriteFormat(_L("Number of UDP packets sent %d, Number received %d"),NoPackets,recvCount);
       
   697     	
       
   698     	
       
   699     	// get rid of the old buffers 
       
   700     	CleanupStack::PopAndDestroy();	//	UdpWritebuf;
       
   701     	CleanupStack::PopAndDestroy();	//	UdpReadbuf;
       
   702     	
       
   703     	
       
   704 	    if (!testStepResult)
       
   705     	    {
       
   706     	    Logger().WriteFormat(_L("TestStep failed %d time(s) from %d. Trying further..."), testStepCounter, KMaxTestStepCounter);
       
   707     	    
       
   708     	    ++testStepCounter;
       
   709     	    }
       
   710     	    
       
   711 	    }
       
   712 	    while ( (testStepCounter < KMaxTestStepCounter) &&
       
   713 	          (!testStepResult) ); //big while...
       
   714      
       
   715 
       
   716     if ( testStepResult )
       
   717         {
       
   718 	    return EPass;
       
   719         }
       
   720     else
       
   721         {
       
   722         return EFail;
       
   723         }
       
   724 	}
       
   725 	
       
   726 	
       
   727 // Test step 5.1
       
   728 const TDesC& CEsockTest5_1::GetTestName()
       
   729 	{
       
   730 	// store the name of this test case
       
   731 	_LIT(ret,"Test5.1");
       
   732 	
       
   733 	return ret;
       
   734 	}
       
   735 
       
   736 CEsockTest5_1::~CEsockTest5_1()
       
   737 	{
       
   738 	}
       
   739 
       
   740 enum TVerdict CEsockTest5_1::easyTestStepPreambleL()
       
   741 	{
       
   742 	TInt connections;
       
   743 	TRAPD(ret, connections = OpenConnectionsL(_L("Test_5.1"), iSockIndex2, iSockIndex3));
       
   744 	if (KErrNone != ret)
       
   745 		{
       
   746 		return EFail;
       
   747 		}
       
   748 	if (0 >= connections)
       
   749 		{
       
   750 		return EFail;
       
   751 		}
       
   752 
       
   753 	return EPass;
       
   754 	}
       
   755 
       
   756 enum TVerdict CEsockTest5_1::easyTestStepL()
       
   757 	{
       
   758 	TESTL(EPass == TestStepResult());
       
   759 	
       
   760 	// set up data buffers
       
   761 	TBuf8<100> wbuf, rbuf1;
       
   762 	TBuf8<20> rbuf2;
       
   763 	wbuf.SetLength(20);
       
   764 	StripeDes(wbuf, 0, wbuf.Length(), '@', 'Z');
       
   765 	
       
   766 	// for each pair of connected sockets
       
   767 	TRequestStatus stat;
       
   768 	TInt i;
       
   769 	for (i = 0; i < iNumSockets; i++)
       
   770 		{
       
   771 		// send data from client to server (20 bytes)
       
   772 		iEsockSuite->GetSocketHandle(iSockIndex2 + (i*2)).Write(wbuf, stat);
       
   773 		User::WaitForRequest(stat);
       
   774 		TESTEL(stat==KErrNone, stat.Int());
       
   775 		
       
   776 		// read data (server)
       
   777 		iEsockSuite->GetSocketHandle(iSockIndex3 + (i*2)).Read(rbuf2, stat);
       
   778 		User::WaitForRequest(stat);
       
   779 		TESTEL(stat==KErrNone, stat.Int());
       
   780 		TESTL(rbuf2==wbuf);
       
   781 		}
       
   782 	
       
   783 	// send data from server to client (100 bytes)
       
   784 	wbuf.SetMax();
       
   785 	StripeDes(wbuf, 0, wbuf.Length(), '@', 'Z');
       
   786 	
       
   787 	// for each pair of connected sockets
       
   788 	for (i = 0; i < iNumSockets; i++)
       
   789 		{
       
   790 		// send data from server to client
       
   791 		(iEsockSuite->GetSocketHandle(iSockIndex2 + (i*2))).Write(wbuf, stat);
       
   792 		User::WaitForRequest(stat);
       
   793 		TESTEL(stat==KErrNone, stat.Int());
       
   794 		
       
   795 		// read data (client)
       
   796 		(iEsockSuite->GetSocketHandle(iSockIndex3 + (i*2))).Read(rbuf1, stat);
       
   797 		User::WaitForRequest(stat);
       
   798 		TESTEL(stat==KErrNone, stat.Int());
       
   799 		
       
   800 		// check received data
       
   801 		TESTL(rbuf1==wbuf);
       
   802 		}
       
   803 	
       
   804 	return EPass;
       
   805 	}
       
   806 
       
   807 
       
   808 // Test step 5.2
       
   809 const TDesC& CEsockTest5_2::GetTestName()
       
   810 	{
       
   811 	// store the name of this test case
       
   812 	_LIT(ret,"Test5.2");
       
   813 	
       
   814 	return ret;
       
   815 	}
       
   816 
       
   817 CEsockTest5_2::~CEsockTest5_2()
       
   818 	{
       
   819 	}
       
   820 
       
   821 enum TVerdict CEsockTest5_2::easyTestStepPreambleL()
       
   822 	{
       
   823 	TInt connections;
       
   824 	TRAPD(ret, connections = OpenConnectionsL(_L("Test_5.2"), iSockIndex2, iSockIndex3));
       
   825 	if (KErrNone != ret)
       
   826 		{
       
   827 		return EFail;
       
   828 		}
       
   829 	if (0 >= connections)
       
   830 		{
       
   831 		return EFail;
       
   832 		}
       
   833 
       
   834 	return EPass;
       
   835 	}
       
   836 
       
   837 enum TVerdict CEsockTest5_2::easyTestStepL()
       
   838 	{
       
   839 	TESTL(EPass == TestStepResult());
       
   840 	
       
   841 	// set up data buffers
       
   842 	TBuf8<1024> wbuf;
       
   843 	
       
   844 	StripeDes(wbuf, 0, wbuf.MaxLength(), '@', 'Z');
       
   845 	wbuf.SetLength(20);
       
   846 	
       
   847 	// send data from client to server (20 bytes)
       
   848 	TRequestStatus stat;
       
   849 	iEsockSuite->GetSocketHandle(iSockIndex2).Send(wbuf, 0, stat);
       
   850 	User::WaitForRequest(stat);
       
   851 	TESTEL(stat==KErrNone, stat.Int());
       
   852 	
       
   853 	// read data 
       
   854 	TBuf8<20> rbuf2;
       
   855 	iEsockSuite->GetSocketHandle(iSockIndex3).Recv(rbuf2, 0, stat);
       
   856 	User::WaitForRequest(stat);
       
   857 	TESTEL(stat==KErrNone, stat.Int());
       
   858 	
       
   859 	TESTL(wbuf==rbuf2);
       
   860 	
       
   861 	// send data from server to client (20 packets of 1024 bytes)
       
   862 	wbuf.SetLength(1024);
       
   863 	TInt recvCount = 0;
       
   864 	TBuf8<1024> rbuf1;
       
   865 	for (TInt i = 0; i < 20; i++)
       
   866 		{
       
   867 		iEsockSuite->GetSocketHandle(iSockIndex3).Send(wbuf, 0, stat);
       
   868 		User::WaitForRequest(stat);
       
   869 		TESTL(stat==KErrNone);
       
   870 		
       
   871 		iEsockSuite->GetSocketHandle(iSockIndex2).Recv(rbuf1, 0, stat);
       
   872 		User::WaitForRequest(stat);
       
   873 		TESTEL(stat==KErrNone, stat.Int());
       
   874 		
       
   875 		recvCount+=rbuf1.Length();
       
   876 		}
       
   877 	
       
   878 	// check number of bytes received
       
   879 	TESTL(recvCount==(20*1024));
       
   880 	
       
   881 	// send data from client to server (20 packets of 512 bytes)
       
   882 	wbuf.SetLength(512);
       
   883 	recvCount = 0;
       
   884 	TBuf8<512> rbuf3;
       
   885 	for (TInt ii = 0; ii < 20; ii++)
       
   886 		{
       
   887 		iEsockSuite->GetSocketHandle(iSockIndex2).Send(wbuf, 0, stat);
       
   888 		User::WaitForRequest(stat);
       
   889 		TESTL(stat==KErrNone);
       
   890 		
       
   891 		iEsockSuite->GetSocketHandle(iSockIndex3).Recv(rbuf3, 0, stat);
       
   892 		User::WaitForRequest(stat);
       
   893 		TESTEL(stat==KErrNone, stat.Int());
       
   894 		
       
   895 		recvCount+=rbuf3.Length();
       
   896 		}
       
   897 	
       
   898 	// check number of bytes received
       
   899 	TESTL(recvCount==(20*512));
       
   900 	
       
   901 	return EPass;
       
   902 	}
       
   903 
       
   904 
       
   905 // Test step 5.3
       
   906 const TDesC& CEsockTest5_3::GetTestName()
       
   907 	{
       
   908 	// store the name of this test case
       
   909 	_LIT(ret,"Test5.3");
       
   910 	
       
   911 	return ret;
       
   912 	}
       
   913 
       
   914 CEsockTest5_3::~CEsockTest5_3()
       
   915 	{
       
   916 	}
       
   917 
       
   918 enum TVerdict CEsockTest5_3::easyTestStepPreambleL()
       
   919 	{
       
   920 	CloseSockets();
       
   921 	if (KErrNone != OpenMinSockets(_L("Test_5.3"),KAfInet, KSockStream, KProtocolInetTcp))
       
   922 		{
       
   923 		return EFail;
       
   924 		}
       
   925 
       
   926 	// get ip address to connect to (on echo port)
       
   927 	TInetAddr addrRemote;
       
   928 	if (!GetIpAddressFromConfig(_L("Test_Common"), _L("ipAddress"), addrRemote))
       
   929 		{
       
   930 		return EFail;
       
   931 		}
       
   932 	addrRemote.SetPort(7);
       
   933 	
       
   934 	// get log level
       
   935 	if (!GetIntFromConfig(_L("Test_5.3"), _L("logLevel"), iLogLevel))
       
   936 		{
       
   937 		iLogLevel = 0;
       
   938 		}
       
   939 
       
   940 	// connect
       
   941 	if (iLogLevel > 0)
       
   942 		{
       
   943 		Logger().Write(_L("Connect to remote host..."));
       
   944 		}
       
   945 	TRequestStatus stat;
       
   946 	(iEsockSuite->GetSocketHandle(1)).Connect(addrRemote, stat);
       
   947 	User::WaitForRequest(stat);
       
   948 	if (KErrNone != stat.Int())
       
   949 		{
       
   950 		Logger().WriteFormat(_L("Connect returned %d"), stat.Int());
       
   951 		return EFail;
       
   952 		}
       
   953 	if (iLogLevel > 0)
       
   954 		{
       
   955 		Logger().Write(_L("completed"));
       
   956 		}
       
   957 
       
   958 	return EPass;
       
   959 	}
       
   960 
       
   961 enum TVerdict CEsockTest5_3::easyTestStepL()
       
   962 	{
       
   963 	TESTL(EPass == TestStepResult());
       
   964 	
       
   965 	// set up data buffers
       
   966 	TBuf8<512> wbuf;
       
   967 	StripeDes(wbuf, 0, wbuf.MaxLength(), '@', 'Z');
       
   968 	
       
   969 	// send data (20 bytes)
       
   970 	wbuf.SetLength(20);
       
   971 	TRequestStatus stat;
       
   972 	if (iLogLevel > 0)
       
   973 		{
       
   974 		Logger().Write(_L("Sending..."));
       
   975 		}
       
   976 	RSocket& sock = iEsockSuite->GetSocketHandle(1);
       
   977 	sock.Write(wbuf, stat);
       
   978 	User::WaitForRequest(stat);
       
   979 	TInt ret = stat.Int();
       
   980 	TESTEL(KErrNone == ret, ret);
       
   981 	
       
   982 	// read data (from echo server)
       
   983 	if (iLogLevel > 0)
       
   984 		{
       
   985 		Logger().Write(_L("Receiving..."));
       
   986 		}
       
   987 	TBuf8<20> rbuf2;
       
   988 	sock.Read(rbuf2, stat);
       
   989 	User::WaitForRequest(stat);
       
   990 	ret = stat.Int();
       
   991 	TESTEL(KErrNone == ret, ret);
       
   992 	
       
   993 	TESTL(rbuf2==wbuf);
       
   994 	
       
   995 	// send data (20 packets of 512 bytes)
       
   996 	wbuf.SetMax();
       
   997 	TInt sendCount = 0;
       
   998 
       
   999 	if (iLogLevel > 0)
       
  1000 		{
       
  1001 		Logger().Write(_L("Send loop..."));
       
  1002 		}
       
  1003 	for (TInt i = 0; i < 20; i++)
       
  1004 		{
       
  1005 		sock.Write(wbuf, stat);
       
  1006 		User::WaitForRequest(stat);
       
  1007 		ret = stat.Int();
       
  1008 		TESTEL(KErrNone == ret, ret);
       
  1009 		sendCount += wbuf.Length();
       
  1010 		}
       
  1011 
       
  1012 	TBuf8<128> rbuf1;
       
  1013 	TInt recvCount = 0;
       
  1014 	ret = KErrNone;
       
  1015 	while (recvCount < sendCount)
       
  1016 		{
       
  1017 		sock.Read(rbuf1, stat);
       
  1018 		User::WaitForRequest(stat);
       
  1019 		ret = stat.Int();
       
  1020 		
       
  1021 		if (KErrNone != ret)
       
  1022 			{
       
  1023 			break;
       
  1024 			}
       
  1025 		
       
  1026 		recvCount += rbuf1.Length();
       
  1027 		if (iLogLevel > 0)
       
  1028 			{
       
  1029 			Logger().WriteFormat(_L("Received %d octets"), rbuf1.Length());
       
  1030 			}
       
  1031 		}
       
  1032 	TESTEL(sendCount == recvCount, recvCount);
       
  1033 	
       
  1034 	if (iLogLevel > 0)
       
  1035 		{
       
  1036 		Logger().Write(_L("End Test 5.3"));
       
  1037 		}
       
  1038 	
       
  1039 	return EPass;
       
  1040 	}
       
  1041 
       
  1042 
       
  1043 // Test step 5.4
       
  1044 const TDesC& CEsockTest5_4::GetTestName()
       
  1045 	{
       
  1046 	// store the name of this test case
       
  1047 	_LIT(ret,"Test5.4");
       
  1048 	
       
  1049 	return ret;
       
  1050 	}
       
  1051 
       
  1052 CEsockTest5_4::~CEsockTest5_4()
       
  1053 	{
       
  1054 	}
       
  1055 
       
  1056 enum TVerdict CEsockTest5_4::easyTestStepPreambleL()
       
  1057 	{
       
  1058 	CloseSockets();
       
  1059 	if (KErrNone != OpenMinSockets(_L("Test_5.4"),KAfInet, KSockStream, KProtocolInetTcp))
       
  1060 		{
       
  1061 		return EFail;
       
  1062 		}
       
  1063 
       
  1064 	// get ip address to connect to (on echo port)
       
  1065 	TInetAddr addrRemote;
       
  1066 	if (!GetIpAddressFromConfig(_L("Test_Common"), _L("ipAddress"), addrRemote))
       
  1067 		{
       
  1068 		return EFail;
       
  1069 		}
       
  1070 	addrRemote.SetPort(7);
       
  1071 	
       
  1072 	// get log level
       
  1073 	if (!GetIntFromConfig(_L("Test_5.4"), _L("logLevel"), iLogLevel))
       
  1074 		{
       
  1075 		iLogLevel = 0;
       
  1076 		}
       
  1077 
       
  1078 	// connect
       
  1079 	if (iLogLevel > 0)
       
  1080 		{
       
  1081 		Logger().Write(_L("Connect to remote host..."));
       
  1082 		}
       
  1083 	TRequestStatus stat;
       
  1084 	(iEsockSuite->GetSocketHandle(1)).Connect(addrRemote, stat);
       
  1085 	User::WaitForRequest(stat);
       
  1086 	if (KErrNone != stat.Int())
       
  1087 		{
       
  1088 		Logger().WriteFormat(_L("Connect returned %d"), stat.Int());
       
  1089 		return EFail;
       
  1090 		}
       
  1091 	if (iLogLevel > 0)
       
  1092 		{
       
  1093 		Logger().Write(_L("completed"));
       
  1094 		}
       
  1095 	return EPass;
       
  1096 	}
       
  1097 
       
  1098 enum TVerdict CEsockTest5_4::easyTestStepL()
       
  1099 	{
       
  1100 	TESTL(EPass == TestStepResult());
       
  1101 	
       
  1102 	// set up data buffers
       
  1103 	TBuf8<512> wbuf;
       
  1104 	TBuf8<128> rbuf;
       
  1105 	StripeDes(wbuf, 0, wbuf.MaxLength(), '@', 'Z');
       
  1106 	
       
  1107 	// send data (30 bytes)
       
  1108 	wbuf.SetLength(30);
       
  1109 	TRequestStatus stat;
       
  1110 	if (iLogLevel > 0)
       
  1111 		{
       
  1112 		Logger().Write(_L("Sending..."));
       
  1113 		}
       
  1114 	RSocket& sock = iEsockSuite->GetSocketHandle(1);
       
  1115 	sock.Send(wbuf, 0, stat);
       
  1116 	User::WaitForRequest(stat);
       
  1117 	TInt ret = stat.Int();
       
  1118 	TESTEL(KErrNone == ret, ret);
       
  1119 	
       
  1120 	// read data (from echo server)
       
  1121 	if (iLogLevel > 0)
       
  1122 		{
       
  1123 		Logger().Write(_L("Receiving..."));
       
  1124 		}
       
  1125 	TSockXfrLength xfrLen;
       
  1126 	sock.RecvOneOrMore(rbuf, 0, stat, xfrLen);
       
  1127 	User::WaitForRequest(stat);
       
  1128 	ret = stat.Int();
       
  1129 	TESTEL(KErrNone == ret, ret);
       
  1130 	TInt len = xfrLen();
       
  1131 	TESTEL(30 == len, len);
       
  1132 	
       
  1133 	TESTL(rbuf==wbuf);
       
  1134 	
       
  1135 	// send data (20 packets of 512 bytes)
       
  1136 	wbuf.SetMax();
       
  1137 	TInt sendCount = 0;
       
  1138 
       
  1139 	if (iLogLevel > 0)
       
  1140 		{
       
  1141 		Logger().Write(_L("Send loop..."));
       
  1142 		}
       
  1143 	for (TInt i = 0; i < 20; i++)
       
  1144 		{
       
  1145 		sock.Send(wbuf, 0, stat);
       
  1146 		User::WaitForRequest(stat);
       
  1147 		ret = stat.Int();
       
  1148 		TESTEL(KErrNone == ret, ret);
       
  1149 		sendCount += wbuf.Length();
       
  1150 		}
       
  1151 
       
  1152 	TInt recvCount = 0;
       
  1153 	while (recvCount < sendCount)
       
  1154 		{
       
  1155 		sock.RecvOneOrMore(rbuf, 0, stat, xfrLen);
       
  1156 		User::WaitForRequest(stat);
       
  1157 		ret = stat.Int();
       
  1158 
       
  1159 		if (KErrNone != ret)
       
  1160 			{
       
  1161 			break;
       
  1162 			}
       
  1163 
       
  1164 		recvCount += xfrLen();
       
  1165 		if (iLogLevel > 0)
       
  1166 			{
       
  1167 			Logger().WriteFormat(_L("Received %d octets"), xfrLen());
       
  1168 			}
       
  1169 		}
       
  1170 	TESTEL(sendCount == recvCount, recvCount);
       
  1171 	
       
  1172 	if (iLogLevel > 0)
       
  1173 		{
       
  1174 		Logger().Write(_L("End Test 5.4"));
       
  1175 		}
       
  1176 	return EPass;
       
  1177 	}
       
  1178 
       
  1179 
       
  1180 // Test step 5.5
       
  1181 const TDesC& CEsockTest5_5::GetTestName()
       
  1182 	{
       
  1183 	// store the name of this test case
       
  1184 	_LIT(ret,"Test5.5");
       
  1185 	
       
  1186 	return ret;
       
  1187 	}
       
  1188 
       
  1189 CEsockTest5_5::~CEsockTest5_5()
       
  1190 	{
       
  1191 	}
       
  1192 
       
  1193 enum TVerdict CEsockTest5_5::easyTestStepPreambleL()
       
  1194 	{
       
  1195 	TInt connections;
       
  1196 	TRAPD(ret, connections = OpenConnectionsL(_L("Test_5.5"), iSockIndex2, iSockIndex3));
       
  1197 	if (KErrNone != ret)
       
  1198 		{
       
  1199 		return EFail;
       
  1200 		}
       
  1201 	if (0 >= connections)
       
  1202 		{
       
  1203 		return EFail;
       
  1204 		}
       
  1205 
       
  1206 	return EPass;
       
  1207 	}
       
  1208 
       
  1209 enum TVerdict CEsockTest5_5::easyTestStepL()
       
  1210 	{
       
  1211 	TESTL(EPass == TestStepResult());
       
  1212 	
       
  1213 	const TInt KBufSize = 15000;
       
  1214 	// Constructs an empty 8 bit modifiable buffer descriptor. It contains no data.
       
  1215 	typedef TBuf8<KBufSize> TBuffer;
       
  1216 	
       
  1217 	TBuffer* wtemp=new (ELeave) TBuffer;
       
  1218 	CleanupStack::PushL(wtemp);
       
  1219 	TBuffer& wbuf=*wtemp;
       
  1220 	
       
  1221 	StripeDes(wbuf, 0, wbuf.MaxLength(), '@', 'Z');
       
  1222 	wbuf.SetMax();
       
  1223 
       
  1224 	TRequestStatus wstat;
       
  1225 	(iEsockSuite->GetSocketHandle(iSockIndex2)).Write(wbuf, wstat);
       
  1226 	(iEsockSuite->GetSocketHandle(iSockIndex2)).CancelWrite();
       
  1227 	User::WaitForRequest(wstat);
       
  1228 	TESTEL(wstat==KErrNone || wstat==KErrCancel, wstat.Int());
       
  1229 	
       
  1230 	Logger().WriteFormat(_L("wstat =  %d"),wstat.Int());
       
  1231 	
       
  1232 	TBuf8<1024> wbuf2, rbuf;
       
  1233 	
       
  1234 	StripeDes(wbuf2, 0, wbuf2.MaxLength(), '@', 'Z');
       
  1235 	wbuf2.SetMax();
       
  1236 	
       
  1237 	iEsockSuite->GetSocketHandle(iSockIndex2).Write(wbuf2, wstat);
       
  1238 	User::WaitForRequest(wstat);
       
  1239 	TESTL(wstat==KErrNone);
       
  1240 
       
  1241 	TRequestStatus rstat;
       
  1242 	iEsockSuite->GetSocketHandle(iSockIndex3).Read(rbuf, rstat);
       
  1243 	User::WaitForRequest(rstat);	
       
  1244 	TESTEL(rstat==KErrNone, rstat.Int());
       
  1245 	
       
  1246 	TESTL(rbuf==wbuf2);
       
  1247 	
       
  1248 	CleanupStack::PopAndDestroy();
       
  1249 	
       
  1250 	// shutdown the client socket - do not wait for completion
       
  1251 	return EPass;
       
  1252 	}
       
  1253 
       
  1254 
       
  1255 // Test step 5.6
       
  1256 const TDesC& CEsockTest5_6::GetTestName()
       
  1257 	{
       
  1258 	// store the name of this test case
       
  1259 	_LIT(ret,"Test5.6");
       
  1260 	
       
  1261 	return ret;
       
  1262 	}
       
  1263 
       
  1264 CEsockTest5_6::~CEsockTest5_6()
       
  1265 	{
       
  1266 	}
       
  1267 
       
  1268 enum TVerdict CEsockTest5_6::easyTestStepPreambleL()
       
  1269 	{
       
  1270 	TInt connections;
       
  1271 	TRAPD(ret, connections = OpenConnectionsL(_L("Test_5.6"), iSockIndex2, iSockIndex3));
       
  1272 	if (KErrNone != ret)
       
  1273 		{
       
  1274 		return EFail;
       
  1275 		}
       
  1276 	if (0 >= connections)
       
  1277 		{
       
  1278 		return EFail;
       
  1279 		}
       
  1280 
       
  1281 	return EPass;
       
  1282 	}
       
  1283 
       
  1284 enum TVerdict CEsockTest5_6::easyTestStepL()
       
  1285 	{
       
  1286 	TESTL(EPass == TestStepResult());
       
  1287 	
       
  1288 	const TInt KBufSize = 15000;
       
  1289 	// Constructs an empty 8 bit modifiable buffer descriptor. It contains no data.
       
  1290 	typedef TBuf8<KBufSize> TBuffer;
       
  1291 	
       
  1292 	TBuffer* wtemp=new (ELeave) TBuffer;
       
  1293 	CleanupStack::PushL(wtemp);
       
  1294 	TBuffer& wbuf=*wtemp;
       
  1295 	
       
  1296 	StripeDes(wbuf, 0, wbuf.MaxLength(), '@', 'Z');
       
  1297 	wbuf.SetMax();
       
  1298 	
       
  1299 	TRequestStatus wstat;
       
  1300 	(iEsockSuite->GetSocketHandle(iSockIndex2)).Send(wbuf, 0, wstat);	
       
  1301 	(iEsockSuite->GetSocketHandle(iSockIndex2)).CancelSend();
       
  1302 	User::WaitForRequest(wstat);
       
  1303 	TESTEL(wstat==KErrNone || wstat==KErrCancel, wstat.Int());
       
  1304 	
       
  1305 	Logger().WriteFormat(_L("wstat =  %d"),wstat.Int());
       
  1306 	
       
  1307 	TBuf8<1024> wbuf2, rbuf;
       
  1308 	
       
  1309 	StripeDes(wbuf2, 0, wbuf2.MaxLength(), '@', 'Z');
       
  1310 	wbuf2.SetMax();
       
  1311 	
       
  1312 	(iEsockSuite->GetSocketHandle(iSockIndex2)).Send(wbuf2, 0, wstat);
       
  1313 	User::WaitForRequest(wstat);
       
  1314 	TESTL(wstat==KErrNone);
       
  1315 
       
  1316 	TRequestStatus rstat;
       
  1317 	(iEsockSuite->GetSocketHandle(iSockIndex3)).Recv(rbuf, 0, rstat);
       
  1318 	User::WaitForRequest(rstat);
       
  1319 	TESTEL(rstat==KErrNone, rstat.Int());
       
  1320 	
       
  1321 	TESTL(rbuf==wbuf2);
       
  1322 	
       
  1323 	CleanupStack::PopAndDestroy();
       
  1324 	
       
  1325 	// shutdown the client socket - do not wait for completion
       
  1326 	return EPass;
       
  1327 	}
       
  1328 
       
  1329 
       
  1330 // Test step 5.7
       
  1331 const TDesC& CEsockTest5_7::GetTestName()
       
  1332 	{
       
  1333 	// store the name of this test case
       
  1334 	_LIT(ret,"Test5.7");
       
  1335 	
       
  1336 	return ret;
       
  1337 	}
       
  1338 
       
  1339 CEsockTest5_7::~CEsockTest5_7()
       
  1340 	{
       
  1341 	}
       
  1342 
       
  1343 enum TVerdict CEsockTest5_7::easyTestStepPreambleL()
       
  1344 	{
       
  1345 	TInt connections;
       
  1346 	TRAPD(ret, connections = OpenConnectionsL(_L("Test_5.7"), iSockIndex2, iSockIndex3));
       
  1347 	if (KErrNone != ret)
       
  1348 		{
       
  1349 		return EFail;
       
  1350 		}
       
  1351 	if (0 >= connections)
       
  1352 		{
       
  1353 		return EFail;
       
  1354 		}
       
  1355 
       
  1356 	return EPass;
       
  1357 	}
       
  1358 
       
  1359 enum TVerdict CEsockTest5_7::easyTestStepL()
       
  1360 	{
       
  1361 	TESTL(EPass == TestStepResult());
       
  1362 	
       
  1363 	TBuf8<24> wbuf, rbuf;
       
  1364 	StripeDes(wbuf, 0, wbuf.MaxLength(), '1', '9');
       
  1365 	// send data (20 bytes)
       
  1366 	wbuf.SetLength(20);
       
  1367 	
       
  1368 	TRequestStatus wstat;
       
  1369 	iEsockSuite->GetSocketHandle(iSockIndex2).Write(wbuf, wstat);
       
  1370 	User::WaitForRequest(wstat);
       
  1371 	TESTL(wstat==KErrNone);
       
  1372 	
       
  1373 	// read the data - deliberately trying to read more than was sent so as to block indefinitely
       
  1374 	TRequestStatus rstat;
       
  1375 	iEsockSuite->GetSocketHandle(iSockIndex3).Read(rbuf, rstat);
       
  1376 	User::After(1000000);	// wait a second so that initial 20 bytes can be recv'd
       
  1377 	// cancel read before it completes
       
  1378 	iEsockSuite->GetSocketHandle(iSockIndex3).CancelRead();
       
  1379 	User::WaitForRequest(rstat);
       
  1380 	TESTL(rstat==KErrCancel);
       
  1381 	
       
  1382 	TBuf8<1024> wbuf2, rbuf2;
       
  1383 	StripeDes(wbuf2, 0, wbuf2.MaxLength(), '@', 'Z');
       
  1384 	wbuf2.SetMax();
       
  1385 	
       
  1386 	iEsockSuite->GetSocketHandle(iSockIndex2).Write(wbuf2, wstat);
       
  1387 	User::WaitForRequest(wstat);
       
  1388 	TESTL(wstat==KErrNone);
       
  1389 	iEsockSuite->GetSocketHandle(iSockIndex3).Read(rbuf2, rstat);
       
  1390 	User::WaitForRequest(rstat);
       
  1391 	TESTL(rstat==KErrNone);
       
  1392 	
       
  1393 	TESTL(wbuf2==rbuf2);
       
  1394 	
       
  1395 	// shutdown the client socket - do not wait for completion
       
  1396 	return EPass;
       
  1397 	}
       
  1398 
       
  1399 
       
  1400 // Test step 5.8
       
  1401 const TDesC& CEsockTest5_8::GetTestName()
       
  1402 	{
       
  1403 	// store the name of this test case
       
  1404 	_LIT(ret,"Test5.8");
       
  1405 	
       
  1406 	return ret;
       
  1407 	}
       
  1408 
       
  1409 CEsockTest5_8::~CEsockTest5_8()
       
  1410 	{
       
  1411 	}
       
  1412 
       
  1413 enum TVerdict CEsockTest5_8::easyTestStepPreambleL()
       
  1414 	{
       
  1415 	TInt connections;
       
  1416 	TRAPD(ret, connections = OpenConnectionsL(_L("Test_5.8"), iSockIndex2, iSockIndex3));
       
  1417 	if (KErrNone != ret)
       
  1418 		{
       
  1419 		return EFail;
       
  1420 		}
       
  1421 	if (0 >= connections)
       
  1422 		{
       
  1423 		return EFail;
       
  1424 		}
       
  1425 
       
  1426 	return EPass;
       
  1427 	}
       
  1428 
       
  1429 enum TVerdict CEsockTest5_8::easyTestStepL()
       
  1430 	{
       
  1431 	TESTL(EPass == TestStepResult());
       
  1432 
       
  1433 	TBuf8<24> wbuf, rbuf;
       
  1434 	StripeDes(wbuf, 0, wbuf.MaxLength(), '1', '9');
       
  1435 	// send data (20 bytes)
       
  1436 	wbuf.SetLength(20);
       
  1437 	
       
  1438 	TRequestStatus wstat;
       
  1439 	iEsockSuite->GetSocketHandle(iSockIndex2).Send(wbuf, 0, wstat);
       
  1440 	User::WaitForRequest(wstat);
       
  1441 	TESTL(wstat==KErrNone);
       
  1442 	// read the data - deliberately trying to read more than was sent so as to block indefinitely
       
  1443 	
       
  1444 	TRequestStatus rstat;
       
  1445 	iEsockSuite->GetSocketHandle(iSockIndex3).Recv(rbuf, 0, rstat);
       
  1446 	User::After(1000000);	// wait a second so that initial 20 bytes can be recv'd
       
  1447 	// cancel read before it completes
       
  1448 	iEsockSuite->GetSocketHandle(iSockIndex3).CancelRecv();
       
  1449 	User::WaitForRequest(rstat);
       
  1450 	TESTL(rstat==KErrCancel);
       
  1451 	
       
  1452 	TBuf8<1024> wbuf2, rbuf2;
       
  1453 	StripeDes(wbuf2, 0, wbuf2.MaxLength(), '@', 'Z');
       
  1454 	wbuf2.SetMax();
       
  1455 	
       
  1456 	iEsockSuite->GetSocketHandle(iSockIndex2).Send(wbuf2, 0, wstat);
       
  1457 	User::WaitForRequest(wstat);
       
  1458 	TESTL(wstat==KErrNone);
       
  1459 	iEsockSuite->GetSocketHandle(iSockIndex3).Recv(rbuf2, 0, rstat);
       
  1460 	User::WaitForRequest(rstat);
       
  1461 	TESTL(rstat==KErrNone);
       
  1462 	
       
  1463 	TESTL(wbuf2==rbuf2);
       
  1464 	
       
  1465 	// shutdown the client socket - do not wait for completion
       
  1466 	return EPass;
       
  1467 	}
       
  1468 
       
  1469 // Test step 5.9
       
  1470 // Open a TCP socket along with a RawIP socket on TCP protocol. Write some data to the 
       
  1471 // TCP socke and perform a read on the RawIP socket, this should not fail.
       
  1472 const TDesC& CEsockTest5_9::GetTestName()
       
  1473 	{		
       
  1474 	// store the name of this test case
       
  1475 	_LIT(ret,"Test5.9");
       
  1476 	
       
  1477 	return ret;
       
  1478 	}
       
  1479 
       
  1480 CEsockTest5_9::~CEsockTest5_9()
       
  1481 	{
       
  1482 	}
       
  1483 
       
  1484 enum TVerdict CEsockTest5_9::easyTestStepPreambleL()
       
  1485 	{
       
  1486 	CloseSockets();
       
  1487 	if (KErrNone != OpenMinSockets(_L("Test_5.9"),KAfInet, KSockStream, KProtocolInetTcp))
       
  1488 		{
       
  1489 		return EFail;
       
  1490 		}
       
  1491 
       
  1492 	TInetAddr addrRemote;
       
  1493 	// get ip address to connect to (on echo port)
       
  1494 	if (!GetIpAddressFromConfig(_L("Test_Common"), _L("ipAddress"), addrRemote))
       
  1495 		{
       
  1496 		return EFail;
       
  1497 		}
       
  1498 	addrRemote.SetPort(7);	
       
  1499 		
       
  1500 	// connect
       
  1501 	TRequestStatus stat;
       
  1502 	(iEsockSuite->GetSocketHandle(1)).Connect(addrRemote, stat);
       
  1503 	User::WaitForRequest(stat);
       
  1504 	TInt ret = stat.Int();
       
  1505 	if (KErrNone != ret)
       
  1506 		{
       
  1507 		Logger().WriteFormat(_L("Connect returned %d"), ret);
       
  1508 		return EFail;
       
  1509 		}
       
  1510 
       
  1511 	return EPass;
       
  1512 	}
       
  1513 		
       
  1514 TVerdict CEsockTest5_9::easyTestStepL()
       
  1515 	{
       
  1516 	TESTL(EPass == TestStepResult());
       
  1517 	
       
  1518 	// prepare for test
       
  1519 	// set up data buffers
       
  1520 	TBuf8<512> wbuf;
       
  1521 	StripeDes(wbuf, 0, wbuf.MaxLength(), '@', 'Z');
       
  1522 	
       
  1523 	// setup data (30 bytes) for send
       
  1524 	wbuf.SetLength(30);
       
  1525 	
       
  1526 	// open RawIP socket
       
  1527 	RSocket rawsock;
       
  1528 	CleanupClosePushL(rawsock);
       
  1529 	_LIT(KRawIp, "rawip");
       
  1530 	TInt ret = rawsock.Open(iEsockSuite->iSocketServer, KRawIp);
       
  1531 	TESTEL(KErrNone == ret, ret);
       
  1532 		
       
  1533 	// bind RawIP socket to TCP protocol
       
  1534 	ret = rawsock.SetLocalPort(KProtocolInetTcp);
       
  1535 	TESTEL(KErrNone == ret, ret);
       
  1536 	
       
  1537 	// prepare to receive data
       
  1538 	TBuf8<30> recvBuf;
       
  1539 	recvBuf.Zero();
       
  1540 	TRequestStatus receiveStat;
       
  1541 	rawsock.Read(recvBuf, receiveStat);
       
  1542 	
       
  1543 	// now there is an outstanding receive send to the echo server
       
  1544 	TRequestStatus stat;
       
  1545 	(iEsockSuite->GetSocketHandle(1)).Write(wbuf, stat);
       
  1546 	User::WaitForRequest(stat);
       
  1547 	ret = stat.Int();
       
  1548 	TESTEL(KErrNone == ret, ret);
       
  1549 	
       
  1550 	// check that we have received the echo'ed data
       
  1551 	User::WaitForRequest(receiveStat);
       
  1552 	ret = receiveStat.Int();
       
  1553 	TESTEL(KErrNone == ret, ret);
       
  1554 	TESTL(recvBuf.Length() != 0);
       
  1555 	
       
  1556 	CleanupStack::PopAndDestroy(&rawsock);
       
  1557 	return EPass;
       
  1558 	}
       
  1559