servicediscoveryandcontrol/pnp/test/upnp/unittests/tcpserver/src/ctcpservertestupnp20.cpp
changeset 0 f5a58ecadc66
equal deleted inserted replaced
-1:000000000000 0:f5a58ecadc66
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 #include <ss_glob.h>
       
    19 #include <comms-infras/ss_thread.h>
       
    20 #include <comms-infras/ss_nodemessages_factory.h>
       
    21 #include <comms-infras/ss_nodemessages_dataclient.h>
       
    22 #include "ptcpservertests.h"
       
    23 #include "upnp_cf_msgs.h"
       
    24 #include "upnpflowbase.h"
       
    25 #include "upnpflowfactory.h"
       
    26 
       
    27 #include "CTcpServerTestUPnP20.h"
       
    28 
       
    29 using namespace Messages;
       
    30 const TUint16 KActivityNull = 0;
       
    31 const TUint KHttpDefaultPort = 80;
       
    32 
       
    33 //------------------------------------------------------------//	
       
    34 /////////////////// CTcpServerTestUPnP20 /////////////////////////
       
    35 ////// Testcase: Send the test data from the test client/////////
       
    36 //------------------------------------------------------------//
       
    37 
       
    38 _LIT8 ( KTestUPnPServiceUri,"http://127.0.0.1:80/testserviceuri1" );
       
    39 _LIT8 ( KData, "NOTIFY /testserviceuri1 HTTP/1.1\r\nHOST: 127.0.0.1:80\r\nContent-Type: text/xml\r\n\r\n");
       
    40 _LIT8 ( KExpectedResponse, "HTTP/1.1 400 Bad Request");
       
    41 
       
    42 CTcpServerTestUPnP20* CTcpServerTestUPnP20::NewL ( CProxyProvd* aProvd )
       
    43 	{
       
    44 	CTcpServerTestUPnP20* ret = new ( ELeave ) CTcpServerTestUPnP20 ( aProvd );
       
    45 	CleanupStack::PushL ( ret );
       
    46 	ret->ConstructL ();
       
    47 	CleanupStack::Pop ( ret );
       
    48 	return ret;
       
    49 	}
       
    50 
       
    51 CTcpServerTestUPnP20::CTcpServerTestUPnP20 ( CProxyProvd* aProvd ) :
       
    52 	CTcpServerTestBase ( aProvd ), iSocketHandler ( *this ), iHostAddr ( KInetAddrLoop, KHttpDefaultPort ), iState ( ECreateServer )
       
    53 	{
       
    54 	}
       
    55 	
       
    56 CTcpServerTestUPnP20::~CTcpServerTestUPnP20 ()
       
    57 	{
       
    58 	iServiceUri.Close ();
       
    59 		
       
    60 	iSocketHandler.CancelAll ();
       
    61 	iClientSocket.Close ();
       
    62 	delete iSocketOpener;
       
    63 	
       
    64 	iTimer.Close ();
       
    65 	
       
    66 	TUpnpMessage::DeRegister ();
       
    67 	}
       
    68 	
       
    69 void CTcpServerTestUPnP20::ConstructL ()
       
    70 	{
       
    71 	TUpnpMessage::RegisterL ( SockManGlobals::Get ()->SelfWorker ());
       
    72 	
       
    73 	iServiceUri.CreateL ( KTestUPnPServiceUri );
       
    74 	
       
    75 	iTimer.CreateLocal ();
       
    76 	TRAPD ( err, iSocketOpener = CSocketOpener::NewL ( *this ) );			
       
    77 	
       
    78 	User::LeaveIfError ( err );		
       
    79 	}
       
    80 
       
    81 TVerdict CTcpServerTestUPnP20::RunTestL ()
       
    82 	{
       
    83 	switch ( iState )
       
    84 		{
       
    85 		case ECreateServer:
       
    86 			{
       
    87 			const TUint8* uriPtr = NULL;
       
    88 			uriPtr = iServiceUri.Ptr ();
       
    89 			
       
    90 			const TUid reqUid = { CUPnPFlowFactory::iUid };		
       
    91 			THttpServerFlowQuery flowQuery ( uriPtr,
       
    92 										 iServiceUri.Size (),
       
    93 										 Id (),
       
    94 										 EHttpServerFlow, iChunkManager );
       
    95 			TCFPlayerRole playerRole ( TCFPlayerRole::EDataPlane );
       
    96 			
       
    97 			TCFFactory::TFindOrCreatePeer msg ( TCFPlayerRole::EDataPlane, reqUid, &flowQuery );
       
    98 			TNodeId factoryContainer = SockManGlobals::Get()->GetPlaneFC( playerRole );
       
    99 			
       
   100 			RClientInterface::OpenPostMessageClose ( Id (), TNodeCtxId ( KActivityNull, factoryContainer ), TCFFactory::TFindOrCreatePeer ( TCFPlayerRole::EDataPlane, reqUid, &flowQuery ).CRef () );
       
   101 			
       
   102 			iState  = EStartServer;
       
   103 			iStatus = KRequestPending;
       
   104 			Reschedule ();
       
   105 			return EPass;
       
   106 			}
       
   107 			
       
   108 		case EStartServer:
       
   109 			{
       
   110 			RClientInterface::OpenPostMessageClose ( TNodeCtxId ( KActivityNull, Id () ), TNodeCtxId ( KActivityNull, iTcpServer ), TCFDataClient::TStart ().CRef () );
       
   111 			
       
   112 			iState  = EOpenClientSocketAndConnect;
       
   113 			iStatus = KRequestPending;
       
   114 			Reschedule ();
       
   115 			return EPass;
       
   116 			}
       
   117 			
       
   118 		case EOpenClientSocketAndConnect:
       
   119 			{
       
   120 			iSocketOpener->MakeSocket ( KAfInet, KSockStream, KProtocolInetTcp );
       
   121 			
       
   122 			iState  = ESendData;
       
   123 			iStatus = KRequestPending;
       
   124 			Reschedule ();
       
   125 			return EPass;
       
   126 			}
       
   127 			
       
   128 		case ESendData:
       
   129 			{			
       
   130 			iSendChain.CreateL ( KData );
       
   131 			iSocketHandler.Send ( iSendChain );
       
   132 			
       
   133 			iState = ESendStopServer;
       
   134 			iStatus = KRequestPending;
       
   135 			Reschedule ();
       
   136 			return EPass;
       
   137 			}
       
   138 		
       
   139 		// just to make sure production objects are cleaned-up properly.
       
   140 		case ESendStopServer:
       
   141 			{	
       
   142 			// cleanup tcp server flow
       
   143 			RClientInterface::OpenPostMessageClose ( TNodeCtxId ( KActivityNull, Id () ), TNodeCtxId ( KActivityNull, iTcpServer ), TCFDataClient::TStop ( KErrNone ).CRef () );
       
   144 			
       
   145 			iState = EServerStopped;
       
   146 			iStatus = KRequestPending;
       
   147 			Reschedule ();
       
   148 			return EPass;
       
   149 			}
       
   150 		
       
   151 		case EServerStopped:
       
   152 			{
       
   153 			// cleanup udp server flow
       
   154 			RClientInterface::OpenPostMessageClose ( TNodeCtxId ( KActivityNull, Id () ), TNodeCtxId ( KActivityNull, iTcpServer ), TEChild::TDestroy ().CRef () );
       
   155 			
       
   156 			iTimer.After ( iStatus, 1000000 ); //1secs
       
   157 			iState = ECleanup;
       
   158 			iStatus = KRequestPending;
       
   159 			Reschedule ();
       
   160 			return EPass;
       
   161 			}
       
   162 		
       
   163 		case ECleanup:
       
   164 			{
       
   165 			return iResponse; // Here the final test case is passed back to the RSocket::Ioctl
       
   166 			}
       
   167 					
       
   168 		default:
       
   169 			{
       
   170 			iLogger.WriteFormat(_L("<i> Failed: TestCase:..... </i>"));
       
   171 			ASSERT(0);
       
   172 			return EFail;
       
   173 			}
       
   174 		}
       
   175 	}
       
   176 	
       
   177 void CTcpServerTestUPnP20::OpenComplete ( RInternalSocket& aSocket )
       
   178 	{
       
   179 	iClientSocket = aSocket;
       
   180 	iSocketHandler.Attach ( aSocket );	
       
   181 	
       
   182 	iSocketHandler.Connect ( iHostAddr );
       
   183 	}
       
   184 	
       
   185 void CTcpServerTestUPnP20::ConnectComplete ()
       
   186 	{	
       
   187 	CompleteSelf ( KErrNone );
       
   188 	}
       
   189 	
       
   190 void CTcpServerTestUPnP20::SendComplete ( TInt /*aLength*/ )
       
   191 	{
       
   192 	iSendChain.Init ();
       
   193 	iSocketHandler.Recv ();
       
   194 	}
       
   195 	
       
   196 void CTcpServerTestUPnP20::RecvComplete ( RMBufChain& aData )
       
   197 	{
       
   198 	RBuf8 responseBuf;
       
   199 	responseBuf.CreateMax ( aData.Length () );
       
   200 	aData.CopyOut ( responseBuf );
       
   201 	aData.Free ();
       
   202 	
       
   203 	if ( responseBuf.FindF ( KExpectedResponse ) == KErrNotFound )
       
   204 		{
       
   205 		iResponse = EFail; // test case failed
       
   206 		}
       
   207 	else
       
   208 		{
       
   209 		iResponse = EPass;	// test case passed
       
   210 		}
       
   211 	
       
   212 	responseBuf.Close ();
       
   213 	CompleteSelf ( KErrNone );
       
   214 	}
       
   215 	
       
   216 void CTcpServerTestUPnP20::ReceivedL ( const TRuntimeCtxId& /*aSender*/, const TNodeId& /*aRecipient*/, TSignatureBase& aMessage )
       
   217 	{
       
   218 	if ( aMessage.IsMessage<TCFFactory::TPeerFoundOrCreated> () )
       
   219 		{
       
   220 		const TCFFactory::TPeerFoundOrCreated& msg = message_cast < const TCFFactory::TPeerFoundOrCreated > ( aMessage );
       
   221 		iTcpServer = msg.iNodeId;
       
   222 		CompleteSelf ( KErrNone );
       
   223 		}
       
   224 	else if ( aMessage.IsMessage<TCFDataClient::TStarted> () 
       
   225 				|| aMessage.IsMessage<TCFDataClient::TStopped> () )
       
   226 		{
       
   227 		CompleteSelf ( KErrNone );
       
   228 		}
       
   229 	}
       
   230