datacommsserver/esockserver/test/TE_Socket/SocketTestSection16.cpp
changeset 0 dfb7c4ff071f
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // This contains ESock Test cases from section 16
       
    15 // Testing read from socket with KSockReadPeek flag set
       
    16 // 
       
    17 //
       
    18 
       
    19 
       
    20 #include <e32base.h>
       
    21 
       
    22 #include "SocketTestSection16.h"
       
    23 
       
    24 
       
    25 // Test step 16.1
       
    26 const TDesC& CSocketTest16_1::GetTestName()
       
    27 	{
       
    28 	_LIT(ret,"Test16.1");
       
    29 	return ret;
       
    30 	}
       
    31 
       
    32 enum TVerdict CSocketTest16_1::InternalDoTestStepL( void )
       
    33 	{
       
    34 	Logger().WriteFormat(_L("Testing read from socket with KSockReadPeek flag set in different situations."));
       
    35 	
       
    36 	RSocketServ sockServ;
       
    37 	
       
    38 	RSocket     sock_UdpIn;
       
    39     RSocket     sock_UdpOut;
       
    40     TInt        nRes;
       
    41 	
       
    42     _LIT8(KSendData, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); // Sample testdata to send in these tests
       
    43 	
       
    44     CleanupClosePushL(sockServ);
       
    45     CleanupClosePushL(sock_UdpIn);
       
    46     CleanupClosePushL(sock_UdpOut);
       
    47 	
       
    48     Logger().WriteFormat(_L("connecting to the socket server..."));
       
    49     nRes = sockServ.Connect();
       
    50     TESTL(nRes == KErrNone);
       
    51 	
       
    52     //-- open UDP datagram sockets 
       
    53 	Logger().WriteFormat(_L("opening sockets..."));
       
    54     TESTL(sock_UdpIn.Open (sockServ, KAfInet, KSockDatagram, KProtocolInetUdp) == KErrNone);
       
    55 	TESTL(sock_UdpOut.Open(sockServ, KAfInet, KSockDatagram, KProtocolInetUdp) == KErrNone);
       
    56 	
       
    57     TRequestStatus  rqStat_in, rqStat_out;
       
    58 	
       
    59     //-- local host address, port #7 - the data will be sent via this address
       
    60     TInetAddr       inetAddrTo(7);
       
    61     TInetAddr       inetAddrFrom;
       
    62 	
       
    63     TBuf8<100>      tempBuf, tempBuf1;
       
    64     
       
    65     const  TInt     KIPHeaderSize = 20; //-- IP header size
       
    66     const  TInt     KUdpHeaderSize= 8;  //-- UDP header size
       
    67 	
       
    68 	
       
    69     inetAddrTo.Input(_L("127.0.0.2"));
       
    70     sock_UdpIn.Bind(inetAddrTo); //-- bind socket to address
       
    71 	
       
    72 	
       
    73     //###################################################################################################
       
    74     //--- Test normal situation with peeking data from the UDP socket, when the data arrived before peeking
       
    75     //###################################################################################################
       
    76 	
       
    77 	
       
    78     //--- 1. send the data to the udp socket
       
    79     sock_UdpOut.SendTo(KSendData, inetAddrTo, 0, rqStat_out);
       
    80 	User::WaitForRequest(rqStat_out);
       
    81 	Logger().WriteFormat(_L("UDP sent %d bytes of data, error code:%d\n"), KSendData().Size(), rqStat_out.Int());
       
    82     TESTL(rqStat_out.Int() == KErrNone);
       
    83 	
       
    84 	
       
    85     //--- 2. Peek data from UDP socket and check the size (taking into account the KIpHeaderIncluded flag)
       
    86     sock_UdpIn.RecvFrom(tempBuf, inetAddrFrom, KIpHeaderIncluded|KSockReadPeek, rqStat_in);
       
    87 	User::WaitForRequest(rqStat_in);
       
    88 	Logger().WriteFormat(_L("UDP peeked %d bytes of data, error code:%d\n"), tempBuf.Size(), rqStat_in.Int());
       
    89     TESTL(rqStat_in.Int() == KErrNone);
       
    90     TESTL(tempBuf.Size() == KIPHeaderSize + KUdpHeaderSize + KSendData().Size());
       
    91 	
       
    92     //--- 3. Peek data from UDP socket and check the size (taking into account the KIpHeaderIncluded flag)
       
    93     sock_UdpIn.RecvFrom(tempBuf1, inetAddrFrom, KIpHeaderIncluded|KSockReadPeek, rqStat_in);
       
    94 	User::WaitForRequest(rqStat_in);
       
    95 	Logger().WriteFormat(_L("UDP peeked %d bytes of data, error code:%d\n"), tempBuf1.Size(), rqStat_in.Int());
       
    96     TESTL(rqStat_in.Int() == KErrNone);
       
    97     TESTL(tempBuf1.Size() == KIPHeaderSize + KUdpHeaderSize + KSendData().Size());
       
    98 	
       
    99     //-- check, the data shall be the same
       
   100     TESTL( tempBuf.CompareF(tempBuf1) ==0);
       
   101 	
       
   102     //--- 4. read data from socket and check the size
       
   103 	
       
   104 	sock_UdpIn.RecvFrom(tempBuf, inetAddrFrom, 0, rqStat_in);
       
   105 	User::WaitForRequest(rqStat_in);
       
   106 	Logger().WriteFormat(_L("UDP read real %d bytes data from the socket, error code:%d\n"), tempBuf.Size(), rqStat_in.Int());
       
   107     TESTL(rqStat_in.Int() == KErrNone);
       
   108     TESTL(tempBuf.Size() == KSendData().Size());
       
   109 	
       
   110     //###################################################################################################
       
   111     //--- Test the situation with peeking data from the UDP socket, when the data arrived after read request.
       
   112     //###################################################################################################
       
   113 	
       
   114 	
       
   115     //--- 1. Issue a read request from the socket with KSockReadPeek flag set
       
   116     Logger().WriteFormat(_L("Issuing reading request with KSockReadPeek flag set"));
       
   117     sock_UdpIn.RecvFrom(tempBuf, inetAddrFrom, KIpHeaderIncluded|KSockReadPeek, rqStat_in);
       
   118 	
       
   119 	//--- 2. send data 
       
   120 	sock_UdpOut.SendTo(KSendData, inetAddrTo, 0, rqStat_out);
       
   121 	User::WaitForRequest(rqStat_out);
       
   122 	Logger().WriteFormat(_L("UDP sent %d bytes of data, error code:%d\n"), KSendData().Size(), rqStat_out.Int());
       
   123 	TESTL(rqStat_out.Int() == KErrNone);
       
   124 	
       
   125 	
       
   126     //--- 3. Wait for peeking data to complete, check the result
       
   127 	User::WaitForRequest(rqStat_in);
       
   128 	Logger().WriteFormat(_L("UDP peeked %d bytes of data, error code:%d\n"), tempBuf.Size(), rqStat_in.Int());
       
   129     TESTL(rqStat_in.Int() == KErrNone);
       
   130     TESTL(tempBuf.Size() == KIPHeaderSize + KUdpHeaderSize + KSendData().Size());
       
   131 	
       
   132     
       
   133     sock_UdpIn.RecvFrom(tempBuf1, inetAddrFrom, KIpHeaderIncluded|KSockReadPeek, rqStat_in);
       
   134 	User::WaitForRequest(rqStat_in);
       
   135 	Logger().WriteFormat(_L("UDP peeked %d bytes of data, error code:%d\n"), tempBuf1.Size(), rqStat_in.Int());
       
   136     TESTL(rqStat_in.Int() == KErrNone);
       
   137     TESTL(tempBuf1.Size() == KIPHeaderSize + KUdpHeaderSize + KSendData().Size());
       
   138 	
       
   139     //-- check, the data shall be the same
       
   140     TESTL( tempBuf.CompareF(tempBuf1) ==0);
       
   141 	
       
   142     //--- 4. read data from socket and check the size
       
   143 	
       
   144 	sock_UdpIn.RecvFrom(tempBuf, inetAddrFrom, 0, rqStat_in);
       
   145 	User::WaitForRequest(rqStat_in);
       
   146 	Logger().WriteFormat(_L("UDP read real %d bytes data from the socket, error code:%d\n"), tempBuf.Size(), rqStat_in.Int());
       
   147     TESTL(rqStat_in.Int() == KErrNone);
       
   148     TESTL(tempBuf.Size() == KSendData().Size());
       
   149 	
       
   150 	
       
   151     //###################################################################################################
       
   152     //--- Test the situation with peeking data from the UDP socket cancellation
       
   153     //###################################################################################################
       
   154 	
       
   155     //--- 1. Issue a read request from the socket with KSockReadPeek flag set
       
   156     Logger().WriteFormat(_L("Issuing reading request with KSockReadPeek flag set"));
       
   157     sock_UdpIn.RecvFrom(tempBuf, inetAddrFrom, KIpHeaderIncluded|KSockReadPeek, rqStat_in);
       
   158     
       
   159     Logger().WriteFormat(_L("Cancel receiving"));
       
   160     sock_UdpIn.CancelRecv();
       
   161 	
       
   162 	//--- 2. send data 
       
   163 	sock_UdpOut.SendTo(KSendData, inetAddrTo, 0, rqStat_out);
       
   164 	User::WaitForRequest(rqStat_out);
       
   165 	Logger().WriteFormat(_L("UDP sent %d bytes of data, error code:%d\n"), KSendData().Size(), rqStat_out.Int());
       
   166 	TESTL(rqStat_out.Int() == KErrNone);
       
   167 	
       
   168 	
       
   169     //--- 3. Wait for peeking data to complete, check the result
       
   170 	User::WaitForRequest(rqStat_in);
       
   171 	Logger().WriteFormat(_L("UDP peeked %d bytes of data, error code:%d\n"), tempBuf.Size(), rqStat_in.Int());
       
   172     TESTL((rqStat_in.Int() == KErrCancel) || (rqStat_in.Int() == KErrNone));
       
   173     
       
   174     //--- 4. Peek the data
       
   175     sock_UdpIn.RecvFrom(tempBuf, inetAddrFrom, KIpHeaderIncluded|KSockReadPeek, rqStat_in);
       
   176 	User::WaitForRequest(rqStat_in);
       
   177 	Logger().WriteFormat(_L("UDP peeked %d bytes of data, error code:%d\n"), tempBuf1.Size(), rqStat_in.Int());
       
   178     TESTL(rqStat_in.Int() == KErrNone);
       
   179 	
       
   180 	
       
   181 	
       
   182 	//-- close and destroy everything
       
   183 	CleanupStack::PopAndDestroy(3); //sockServ, sock_UdpIn, sock_UdpOut
       
   184 	
       
   185 	SetTestStepResult(EPass);
       
   186 	return EPass;
       
   187 	}
       
   188 
       
   189