servicediscoveryandcontrol/pnp/test/upnp/unittests/upnpmessagetest/src/cupnprequestparsertest.cpp
changeset 0 f5a58ecadc66
equal deleted inserted replaced
-1:000000000000 0:f5a58ecadc66
       
     1 // Copyright (c) 2008-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 //
       
    15 
       
    16 #include <http/thttphdrfielditer.h>
       
    17 #include <httpstringconstants.h>
       
    18 #include <http/thttptable.h>
       
    19 #include <stringpool.h>
       
    20 #include <utf.h>
       
    21 #include <http/framework/cheadercodecplugin.h>
       
    22 #include <upnp/tupnptable.h>
       
    23 
       
    24 
       
    25 #include "cupnprequestparsertest.h"
       
    26 #include "rhttpheaders.h"
       
    27 
       
    28 _LIT8(KUpnpCodecName, "HTTP/UPnP");
       
    29 
       
    30 CUpnpRequestParserTest* CUpnpRequestParserTest::NewL()
       
    31 	{
       
    32 	return (new(ELeave) CUpnpRequestParserTest());	
       
    33 	}
       
    34 
       
    35 
       
    36 CUpnpRequestParserTest::CUpnpRequestParserTest()
       
    37 	{
       
    38 	SetTestStepName(KRequestParserTest);
       
    39 	}
       
    40 
       
    41 
       
    42 CUpnpRequestParserTest::~CUpnpRequestParserTest()
       
    43 	{
       
    44 	}
       
    45 
       
    46 
       
    47 TVerdict CUpnpRequestParserTest::doTestStepPreambleL()
       
    48 	{
       
    49 #ifdef __CFLOG_ACTIVE
       
    50 	__CFLOG_CREATEL;
       
    51 	__CFLOG_OPEN;
       
    52 #endif
       
    53 	
       
    54 	iIsStarted = EFalse;
       
    55 	iError = KErrNone;
       
    56 	iSched = new(ELeave) CActiveScheduler;
       
    57 	CActiveScheduler::Install(iSched);
       
    58 
       
    59 	//iMBufMgr = CMBufManager::NewL(256 * 1024, iMBufSzAllctr);
       
    60 	//iMBufSzAllctr->AddL(KMBuf_MBufSize, (256 * 1024/(KMBuf_MBufSize+sizeof(RMBuf)))-1, KMBuf_MinGrowth, KMBuf_GrowthThreshold);
       
    61 	iMBufMgr = CChunkManager::NewL ( 256 * 1024 );
       
    62 	iMBufMgr->AddPoolL(KMBuf_MBufSize, (256 * 1024/(KMBuf_MBufSize+sizeof(RMBuf)))-1, KMBuf_MinGrowth, KMBuf_GrowthThreshold);
       
    63 	
       
    64 	iStringPool.OpenL(TUPnPTable::Table());
       
    65 	iStringPool.OpenL(THTTPTable::Table());
       
    66 	iCodec = CHeaderCodecPlugin::NewL(KUpnpCodecName, iStringPool);
       
    67 	iRequest = CRequest::NewL(*iCodec, iStringPool);	
       
    68 	iRequestParser = CUpnpRequestParser::NewL(*this);
       
    69 
       
    70 	iDataChunk = NULL;
       
    71 	if(!iDataChunkChain.IsEmpty())
       
    72 		iDataChunkChain.Free();
       
    73 	
       
    74 	GetIntFromConfig( ConfigSection(), KDataChunkCount, iDataChunkCount );
       
    75 	
       
    76 	TInt dataChunkCountIter = 1;
       
    77 	
       
    78 	while( dataChunkCountIter <= iDataChunkCount )
       
    79 		{
       
    80 		TPtrC dataChunk;
       
    81 		TBuf<10> buf(KNullDesC);
       
    82 		
       
    83 		// The below line is to create formats such as
       
    84 		// DataChunk1, DataChunk2, DataChunk3...
       
    85 		buf.Format(KDataChunk, dataChunkCountIter);
       
    86 		
       
    87 		GetStringFromConfig(ConfigSection(), buf, dataChunk);
       
    88 		RBuf tt;
       
    89 		tt.CreateL(dataChunk);
       
    90 		CleanupClosePushL(tt);
       
    91 		TRAPD(error, NegateEscapeEncodeL(tt));
       
    92 		if(error != KErrNone)
       
    93 			{
       
    94 			SetTestStepResult(EFail);
       
    95 			return TestStepResult();
       
    96 			}
       
    97 		dataChunkCountIter++;
       
    98 		CleanupStack::PopAndDestroy(&tt);
       
    99 		}
       
   100 	
       
   101 	TPtrC8 dataChunk8(iDataChunkArray[0]->Des());
       
   102 	RMemoryAllocator allocator ( iMBufMgr );	
       
   103 	TRAPD(error, iDataChunkChain.CreateL(dataChunk8, allocator));
       
   104 	if(error != KErrNone)	
       
   105 		{
       
   106 		SetTestStepResult(EFail);
       
   107 		return TestStepResult();
       
   108 		}
       
   109 	
       
   110 	return TestStepResult();
       
   111 	}
       
   112 
       
   113 
       
   114 TVerdict CUpnpRequestParserTest::doTestStepL()
       
   115 	{
       
   116 	TPtrC dataChunk;
       
   117 		
       
   118     if (
       
   119     	!GetStringFromConfig(ConfigSection(), KDataChunk1, dataChunk)	||
       
   120     	!GetBoolFromConfig(ConfigSection(), KParseExcessData, iNeedToParseExcessData)	||
       
   121     	!GetIntFromConfig(ConfigSection(), KExpectedRequests, iExpectedRequests)	||
       
   122     	!GetIntFromConfig( ConfigSection(), KDataChunkCount, iDataChunkCount )
       
   123     	)
       
   124 		{
       
   125 		ERR_PRINTF5(_L("===> Problem in reading values from ini.			\
       
   126 						\nExpected fields are: \n%S\n%S\n%S\n%S <==="
       
   127 					  ), &KDataChunk1, &KParseExcessData, &KExpectedRequests, 
       
   128 					  &KDataChunkCount );
       
   129 		SetTestStepResult(EFail);
       
   130 		return TestStepResult();
       
   131 		}
       
   132 	
       
   133 	SetTestStepResult(EPass);	
       
   134 
       
   135 	iRequestParser->ParseRequest(iDataChunkChain, iRequest);
       
   136 	if(iError == KErrNone)
       
   137 		{
       
   138 		iIsStarted = ETrue;
       
   139 		CActiveScheduler::Start();
       
   140 		}
       
   141 	else
       
   142 		{
       
   143 		SetTestStepResult(EFail);	
       
   144 		}	
       
   145 
       
   146 	return TestStepResult();
       
   147 	}
       
   148 
       
   149 
       
   150 TVerdict CUpnpRequestParserTest::doTestStepPostambleL()
       
   151 	{
       
   152 	if(iRequestParser)
       
   153 		delete iRequestParser;
       
   154 
       
   155 	if(iRequest)
       
   156 		delete iRequest;
       
   157 	
       
   158 	iStringPool.Close();
       
   159 	
       
   160 	if(!iDataChunkChain.IsEmpty())
       
   161 		{
       
   162 		iDataChunkChain.Free();
       
   163 		}
       
   164 	
       
   165 	if(iMBufMgr)
       
   166 		delete iMBufMgr;
       
   167 		
       
   168 	delete iCodec;
       
   169 	delete iSched;
       
   170 	
       
   171 	iDataChunkArray.ResetAndDestroy();
       
   172 	iDataChunkArray.Close();
       
   173 
       
   174 #ifdef __CFLOG_ACTIVE
       
   175 	__CFLOG_CLOSE;
       
   176 	__CFLOG_DELETE;
       
   177 #endif
       
   178 	
       
   179 	REComSession::FinalClose();
       
   180 	return TestStepResult();
       
   181 	}
       
   182 
       
   183 
       
   184 void CUpnpRequestParserTest::GotBodyData()
       
   185 	{
       
   186 	INFO_PRINTF1(_L("\n********** GOT REQUEST BODY **********"));
       
   187 	
       
   188 	iDataSupplier = iRequest->Body();
       
   189 		
       
   190 	INFO_PRINTF2(_L("\n===> BODY CHUNK LENGTH: %d <==="), iDataSupplier->OverallDataSize());
       
   191 	
       
   192 	TPtrC8 ptr;
       
   193 	iDataSupplier->GetNextDataPart(ptr);
       
   194 	
       
   195 	TBuf8<1024> buf8(ptr);
       
   196 	TBuf<1024> buf;
       
   197 	buf.Copy(buf8);
       
   198 	
       
   199 	INFO_PRINTF2(_L("\n===> BODY: %S <==="), &buf);
       
   200 	
       
   201 	iDataSupplier->ReleaseData();
       
   202 	iDataSupplier->Reset();
       
   203 	iDataSupplier->ReleaseData();
       
   204 	}
       
   205 
       
   206 
       
   207 void CUpnpRequestParserTest::DataParsed()
       
   208 	{
       
   209 	INFO_PRINTF1(_L("\n********** REQUEST DATA PARSED **********"));
       
   210 	
       
   211 	RMemoryAllocator allocator( iMBufMgr );
       
   212 	
       
   213 	if( (iDataChunkArray.Count() > 0) && (iDataChunkCount > 1) && (iDataChunkPtr.Length() == 0) )
       
   214 		{
       
   215 		HBufC8* firstChunk = iDataChunkArray[0];
       
   216 		iDataChunkArray.Remove(0);
       
   217 		delete firstChunk;
       
   218 		}
       
   219 	
       
   220 	if( (iDataChunkArray.Count() > 0) && (iDataChunkCount > 1) && (iDataChunkPtr.Length() == 0) )
       
   221 		{
       
   222 		TPtrC8 dataChunk8(iDataChunkArray[0]->Des());
       
   223 		if(!iDataChunkChain.IsEmpty())
       
   224 			{
       
   225 			iDataChunkChain.Free();
       
   226 			}
       
   227 		
       
   228 		TRAPD(error, iDataChunkChain.CreateL(dataChunk8, allocator));
       
   229 		if(error != KErrNone)
       
   230 			{
       
   231 			ParserError(error);
       
   232 			return;
       
   233 			}
       
   234 
       
   235 		iRequestParser->ParseRequest(iDataChunkChain, iRequest);
       
   236 		}
       
   237 
       
   238 	if ( (iNeedToParseExcessData) && (iDataChunkPtr.Length() > 0))
       
   239 		{
       
   240 		if(iRequestParser)
       
   241 			{
       
   242 			iRequestParser->ResetParser();
       
   243 			iRequest->Reset();
       
   244 			}
       
   245 
       
   246 		if(!iDataChunkChain.IsEmpty())
       
   247 			{
       
   248 			iDataChunkChain.Free();
       
   249 			}
       
   250 		
       
   251 		TRAPD(error, iDataChunkChain.CreateL(iDataChunkPtr, allocator));
       
   252 		if(error != KErrNone)
       
   253 			{
       
   254 			ParserError(error);
       
   255 			return;
       
   256 			}
       
   257 		
       
   258 		if(iRequestParser)
       
   259 			{
       
   260 			iRequestParser->ParseRequest(iDataChunkChain, iRequest);
       
   261 			}
       
   262 		iDataChunkPtr.Set(KNullDesC8);
       
   263 		}
       
   264 	}
       
   265 
       
   266 
       
   267 void CUpnpRequestParserTest::GotHeaders()
       
   268 	{
       
   269 	INFO_PRINTF1(_L("\n********** GOT REQUEST HEADERS **********"));
       
   270 	
       
   271 	TInt version = iRequest->Handle().IsHTTP10();
       
   272 			
       
   273 	const TDesC8& requestMethod8 = iRequest->Handle().Method().DesC();
       
   274 	TBuf<KMaxHeaderNameLen> requestMethod;
       
   275 	requestMethod.Copy(requestMethod8);
       
   276 	
       
   277 	const TDesC8& requestUri8 = iRequest->Handle().URI().UriDes();
       
   278 	TBuf<KMaxHeaderNameLen> requestUri;
       
   279 	requestUri.Copy(requestUri8);
       
   280 	
       
   281 	TVersion ver = iRequest->Version();
       
   282 	
       
   283 	INFO_PRINTF5(_L("\n===> REQUEST START LINE: %S %S HTTP/%d.%d<==="), &requestMethod, &requestUri, ver.iMajor, ver.iMinor);
       
   284 
       
   285 	RHTTPHeaders hdr = iRequest->Handle().GetHeaderCollection();
       
   286 	THTTPHdrFieldIter it = hdr.Fields();
       
   287 		
       
   288 	TBuf<KMaxHeaderNameLen>  fieldName16;
       
   289 	TBuf<KMaxHeaderValueLen> fieldVal16;
       
   290 
       
   291 	while (it.AtEnd() == EFalse)
       
   292 		{
       
   293 		RStringTokenF fieldName = it();
       
   294 		RStringF fieldNameStr = iStringPool.StringF(fieldName);
       
   295 		TRAP_IGNORE(iRequest->Handle().GetHeaderCollection().FieldPartsL(fieldNameStr));
       
   296 		
       
   297 		TPtrC8 rawData8;
       
   298 		hdr.GetRawField(fieldNameStr, rawData8);
       
   299 
       
   300 		const TDesC8& fieldNameDesC = fieldNameStr.DesC();
       
   301 		fieldName16.Copy(fieldNameDesC.Left(KMaxHeaderNameLen));
       
   302 		fieldVal16.Copy(rawData8);
       
   303 		INFO_PRINTF3(_L("\n===> %S: %S <==="), &fieldName16, &fieldVal16);
       
   304 
       
   305 		++it;
       
   306 		}
       
   307 	}
       
   308 
       
   309 
       
   310 void CUpnpRequestParserTest::ParsingComplete(RMemChunk& aExcessData)
       
   311 	{
       
   312 	INFO_PRINTF1(_L("\n********** REQUEST PARSING COMPLETED **********"));
       
   313 	
       
   314 	if(--iExpectedRequests == 0)
       
   315 		{
       
   316 		CActiveScheduler::Stop();
       
   317 		iIsStarted = EFalse;	
       
   318 		}
       
   319 
       
   320 	TInt len = aExcessData.Length();
       
   321 	if(len > 0)
       
   322 		{
       
   323 		iExcessData8.SetLength(len);
       
   324 		iExcessData8.FillZ(len);
       
   325 		aExcessData.CopyOut(iExcessData8);
       
   326 
       
   327 #ifdef __WINSCW__
       
   328 		TBuf<KMaxMsgLen> excessData16;
       
   329 		excessData16.Copy(iExcessData8);
       
   330 		INFO_PRINTF2(_L("\n===> EXCESS DATA: %S <==="), &excessData16);
       
   331 #endif	
       
   332 
       
   333 		if(iNeedToParseExcessData)
       
   334 			{
       
   335 			iRequest->Reset();
       
   336 			iDataChunkPtr.Set(iExcessData8);
       
   337 			}
       
   338 		}
       
   339 	}
       
   340 
       
   341 	
       
   342 void CUpnpRequestParserTest::ParserError(TInt aError)
       
   343 	{
       
   344 	INFO_PRINTF1(_L("\n********** REQUEST PARSER ERROR **********"));
       
   345 	INFO_PRINTF2(_L("\n===> Error: %d <==="), aError);
       
   346 	SetTestStepResult(EFail);
       
   347 	if(iIsStarted)
       
   348 		{
       
   349 		CActiveScheduler::Stop();
       
   350 		}
       
   351 	else
       
   352 		{
       
   353 		iError = aError;
       
   354 		}	
       
   355 	}
       
   356 
       
   357 	
       
   358 void CUpnpRequestParserTest::NegateEscapeEncodeL(const TDesC& aDes)
       
   359 	{
       
   360 	HBufC* buf = HBufC::NewL(KMaxMsgLen);
       
   361 	CleanupStack::PushL(buf);
       
   362 	TPtr tmpBuf(buf->Des());
       
   363 	TPtrC ptr(aDes);
       
   364 	
       
   365 	while(ptr.Length() > 0)
       
   366 		{
       
   367 		TInt pos = ptr.Locate(backSlash);
       
   368 		if(pos == KErrNotFound)
       
   369 			{
       
   370 			tmpBuf.Append(ptr);
       
   371 			break;
       
   372 			}
       
   373 		else
       
   374 			{
       
   375 			tmpBuf.Append(ptr.Left(pos));
       
   376 			tmpBuf.Append(KCRLF);
       
   377 			ptr.Set(ptr.Mid(pos+4));
       
   378 			}
       
   379 		}
       
   380 	iDataChunk = CnvUtfConverter::ConvertFromUnicodeToUtf8L(*buf);
       
   381 	TRAPD(err, iDataChunkArray.AppendL(iDataChunk));
       
   382 	if(err != KErrNone)
       
   383 		{
       
   384 		delete iDataChunk;
       
   385 		iDataChunk = NULL;
       
   386 		User::Leave(err);
       
   387 		}
       
   388 	CleanupStack::PopAndDestroy(buf);
       
   389 	}