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