messagingfw/wappushfw/tpushscriptbased/t_utils/httptestutils.cpp
changeset 62 db3f5fa34ec7
parent 0 8e480a14352b
equal deleted inserted replaced
60:9f5ae1728557 62:db3f5fa34ec7
       
     1 // Copyright (c) 2007-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 "httptestutils.h"
       
    17 #include "httptestcore.h"
       
    18 
       
    19 #if !defined __E32BASE_H__
       
    20 #include <e32base.h>
       
    21 #endif
       
    22 
       
    23 // for StartC32()
       
    24 #if !defined __C32COMM_H__
       
    25 #include <c32comm.h>
       
    26 #endif
       
    27 #if !defined __IN_SOCK_H__
       
    28 #include <in_sock.h>
       
    29 #endif
       
    30 
       
    31 
       
    32 //
       
    33 //	Constants
       
    34 _LIT(KTestPanic, "HttpTestUtils");
       
    35 const TInt KFailedTestsGranularity = 10;
       
    36 const TInt KMaxLogEntrySize = 256;
       
    37 #if defined (__WINS__)
       
    38 #define PDD_NAME		_L("ECDRV")
       
    39 #else
       
    40 #define PDD_NAME		_L("EUART1")
       
    41 #define PDD2_NAME		_L("EUART2")
       
    42 #define PDD3_NAME		_L("EUART3")
       
    43 #define PDD4_NAME		_L("EUART4")
       
    44 #endif
       
    45 #define LDD_NAME		_L("ECOMM")
       
    46 
       
    47 const TInt KMaxTempBuffize = 256;
       
    48 
       
    49 _LIT(KTestFileInputBase, "HttpTest");
       
    50 _LIT(KPathSep,"\\");
       
    51 
       
    52 //
       
    53 //	CHTTPTestUtils
       
    54 //
       
    55 CHTTPTestUtils::CHTTPTestUtils(const TDesC& aTitle)
       
    56 	: iTest(aTitle)
       
    57 //
       
    58 //	Default c'tor
       
    59 	{
       
    60 	iTest.Title();
       
    61 	iCanStartTest = ETrue;
       
    62 	}
       
    63 
       
    64 CHTTPTestUtils::~CHTTPTestUtils()
       
    65 //
       
    66 //	D'tor
       
    67 	{
       
    68 	TTime endtime;
       
    69 	endtime.UniversalTime();
       
    70 
       
    71 	// Do resource handle leak test?
       
    72 	if (iDoResourceLeakTest)
       
    73 		ResourceLeakTest();
       
    74 
       
    75 	//	End of tests - see if failed or ok
       
    76 	if (iFailedTests->Count())
       
    77 		{
       
    78 		TestHarnessFailed();
       
    79 		}
       
    80 	else
       
    81 		{
       
    82 		TestHarnessComplete();
       
    83 		}
       
    84 	//	Log finish time
       
    85 	TDateTime t = endtime.DateTime();
       
    86 	LogIt(_L("Ended @ %d:%d:%d:%d"),t.Hour(),t.Minute(),t.Second(),t.MicroSecond());
       
    87 	TTime difftime(endtime.Int64() - iStartTime.Int64());
       
    88 	t = difftime.DateTime();
       
    89 	LogIt(_L("Execution time %d:%d:%d:%d"),t.Hour(),t.Minute(),t.Second(),t.MicroSecond());
       
    90 
       
    91 	//	Close test harness
       
    92 	iTest.Close();
       
    93 
       
    94 	//	Cleanup
       
    95 	iFailedTests->ResetAndDestroy();
       
    96 	delete iFailedTests;
       
    97 
       
    98 	// Close fileserver connection
       
    99 	iFs.Close();
       
   100 	}
       
   101 
       
   102 EXPORT_C CHTTPTestUtils* CHTTPTestUtils::NewLC(const TDesC& aTitle)
       
   103 //
       
   104 //	Static factory c'tor
       
   105 	{
       
   106 	CHTTPTestUtils* self = new (ELeave) CHTTPTestUtils(aTitle);
       
   107 	CleanupStack::PushL(self);
       
   108 	self->ConstructL(aTitle);
       
   109 	return self;
       
   110 	}
       
   111 
       
   112 EXPORT_C CHTTPTestUtils* CHTTPTestUtils::NewL(const TDesC& aTitle)
       
   113 //
       
   114 //	Static factiry c'tor
       
   115 	{
       
   116 	CHTTPTestUtils* self = CHTTPTestUtils::NewLC(aTitle);
       
   117 	CleanupStack::Pop();
       
   118 	return self;
       
   119 	}
       
   120 
       
   121 
       
   122 void CHTTPTestUtils::CreateLogFiles(const TDesC& aTestName)
       
   123 	{
       
   124 	TBuf<128> logName(aTestName);
       
   125 	DefaultLogFileName(logName);
       
   126 	__OPEN_LOG2(logName);
       
   127 	logName.Append(_L(".SUMMARY"));
       
   128 
       
   129 	TInt error = iTestSummaryLogger.Connect();
       
   130 	if(error == KErrNone)
       
   131 		{
       
   132 		TParse p;
       
   133 		p.Set(logName, NULL, NULL);
       
   134 		iTestSummaryLogger.CreateLog(KHttpLogsDirName, p.NameAndExt(), EFileLoggingModeOverwrite);
       
   135 		iTestSummaryLogger.SetDateAndTime(ETrue, ETrue);
       
   136 		iTestSummaryLogger.Write(KTestHeader);
       
   137 		}
       
   138 	else
       
   139 		User::InfoPrint(_L("Log file create failed"));
       
   140 	}
       
   141 
       
   142 void CHTTPTestUtils::ConstructL(const TDesC& aTitle)
       
   143 //
       
   144 //	Non-trivial c'tor
       
   145 	{
       
   146 	iCurrentTest=NULL;
       
   147 	//	Create iFailedTests
       
   148 	iFailedTests = new (ELeave) CArrayPtrFlat<CTestInfo> (KFailedTestsGranularity);
       
   149 
       
   150 	//	Start up logging server connection
       
   151 	CreateLogFiles(aTitle);
       
   152 
       
   153 	iStartTime.UniversalTime();
       
   154 	TDateTime t = iStartTime.DateTime();
       
   155 	LogIt(_L("Started @ %d:%d:%d:%d"),t.Hour(),t.Minute(),t.Second(),t.MicroSecond());
       
   156 
       
   157 	// Find number of open resource handles
       
   158 	TInt processHandleCount=0;
       
   159 	RThread().HandleCount(processHandleCount,iStartHandleCount);
       
   160 
       
   161 	// Connect to file server for test scripts, ini-files etc.
       
   162 	User::LeaveIfError(iFs.Connect());
       
   163 	}
       
   164 
       
   165 EXPORT_C void CHTTPTestUtils::JustStartTestL(const TDesC& aName)
       
   166 	{
       
   167 	if (iCanStartTest)
       
   168 		{
       
   169 		iCurrentTest= CTestInfo::NewL(aName, iTestCount, 0);
       
   170 		//	Stop new test being started until this one has ended
       
   171 		iCanStartTest = EFalse;
       
   172 		}
       
   173 	else
       
   174 		{
       
   175 		//	Panic client - bad usage - not allowed to nest tests
       
   176 		Panic(EBadStartTest);
       
   177 		}
       
   178 
       
   179 	}
       
   180 EXPORT_C void CHTTPTestUtils::StartTestL(const TDesC& aName)
       
   181 //
       
   182 //	Logs start of test aName
       
   183 	{
       
   184 	if (iCanStartTest)
       
   185 		{
       
   186 		// Add this test to failed test list - set errorcode to zero
       
   187 		CTestInfo* temp = CTestInfo::NewLC(aName, iTestCount + 1, 0);
       
   188 		iFailedTests->AppendL(temp);
       
   189 		CleanupStack::Pop();	//	temp
       
   190 		iCurrentTest= temp;
       
   191 
       
   192 		// Increase RTest's test depth by Start-ing - increment test count
       
   193 		iTest.Start(aName);
       
   194 		++iTestCount;
       
   195 
       
   196 		// Stop new test being started until this one has ended
       
   197 		iCanStartTest = EFalse;
       
   198 
       
   199 		TBuf<KMaxFileName + 4> buf;
       
   200 		buf.Format(KTestStartingWithDesc, iTestCount, &aName);
       
   201 		WriteComment(buf);
       
   202 
       
   203 		// Reset iStepNumber - start at 1
       
   204 		iStepNumber = 1;
       
   205 		}
       
   206 	else
       
   207 		{
       
   208 		// Panic client - bad usage - not allowed to nest tests
       
   209 		Panic(EBadStartTest);
       
   210 		}
       
   211 	}
       
   212 
       
   213 EXPORT_C void CHTTPTestUtils::NextStep(const TDesC& aStepName)
       
   214 //
       
   215 //	Logs the next step in a test - for informative use.
       
   216 	{
       
   217 	if (!iCanStartTest)
       
   218 		{
       
   219 		TBuf<KMaxFileName + 4> buf;
       
   220 		buf.Format(KNextTestStepWithDesc, iTestCount, iStepNumber, &aStepName);
       
   221 		WriteComment(buf);
       
   222 		iTest.Next(aStepName);
       
   223 		++iStepNumber;
       
   224 		}
       
   225 	else
       
   226 		{
       
   227 		//	Panic client - bad usage - test not started
       
   228 		Panic(EBadStartTest);
       
   229 		}
       
   230 	}
       
   231 
       
   232 EXPORT_C void CHTTPTestUtils::JustEndTest()
       
   233 	{
       
   234 	//	Allow new test to start
       
   235 	iCurrentTest = NULL;
       
   236 	iCanStartTest = ETrue;
       
   237 	}
       
   238 
       
   239 EXPORT_C void CHTTPTestUtils::EndTest(TInt aErrorCode)
       
   240 //
       
   241 //	Logs end of test
       
   242 	{
       
   243 	if (!iCanStartTest)
       
   244 		{
       
   245 		//	Get ptr to this test's entry in failed list - will be the last entry
       
   246 		TBuf<KMaxFileName + 4> buf;
       
   247 		TInt index = iFailedTests->Count();
       
   248 	    CTestInfo* ptr = iFailedTests->At(--index);
       
   249 		if (aErrorCode)
       
   250 			{
       
   251 			//	Set the error code
       
   252 			ptr->SetErrorCode(aErrorCode);
       
   253 			buf.Format(KTestFailed, iTestCount, aErrorCode);
       
   254 			iTest.Printf(KRTestFailed, aErrorCode);
       
   255 			}
       
   256 		else
       
   257 			{
       
   258 			//	Remove entry from list of failed tests
       
   259 			delete ptr;
       
   260 			iFailedTests->Delete(index);
       
   261 			buf.Format(KTestPassed, iTestCount);
       
   262 			}
       
   263 		WriteComment(buf);
       
   264 
       
   265 		// Decrease RTest's test depth by End-ing
       
   266 		iTest.End();
       
   267 
       
   268 		//	Allow new test to start. There's no iCurrentTest until StartTestL is invoked again
       
   269 		iCurrentTest = NULL;
       
   270 		iCanStartTest = ETrue;
       
   271 		}
       
   272 	else
       
   273 		{
       
   274 		//	Panic client - bad usage - test not started
       
   275 		Panic(EBadEndTest);
       
   276 		}
       
   277 	}
       
   278 
       
   279 EXPORT_C void CHTTPTestUtils::LogIt(TRefByValue<const TDesC> aFmt, ...)
       
   280 
       
   281 //	Messages to the front end emulator and to the WAP log
       
   282 	{
       
   283 	VA_LIST list;
       
   284 	VA_START(list,aFmt);
       
   285 	TBuf<2*KMaxFileName> buf;
       
   286 	buf.Zero();
       
   287 	if(iCurrentTest)
       
   288 		buf.Append(iCurrentTest->Name());
       
   289 	buf.Append(_L(">  "));
       
   290 	buf.AppendFormatList(aFmt,list,this);
       
   291 	VA_END(list);
       
   292 	//write to the console and file, if not silent
       
   293 	if (!iSilent)
       
   294 		WriteComment(buf);
       
   295 	else	// otherwise log to the file only
       
   296 		__LOG(buf);
       
   297 	}
       
   298 
       
   299 EXPORT_C void CHTTPTestUtils::Overflow(TDes& aDes)
       
   300 	{
       
   301 	_LIT(KErrOverflowMsg, "...");
       
   302 	if( aDes.MaxLength() >= KErrOverflowMsg().Length() + aDes.Length() )
       
   303 		aDes.Append(KErrOverflowMsg);
       
   304 	}
       
   305 
       
   306 EXPORT_C void CHTTPTestUtils::operator()(TInt aResult,TInt aLineNum)
       
   307 //
       
   308 //	Overload operator ()
       
   309 	{
       
   310 	iTest(aResult, aLineNum);
       
   311 	}
       
   312 
       
   313 EXPORT_C void CHTTPTestUtils::operator()(TInt aResult)
       
   314 //
       
   315 //	Overload operator ()
       
   316 	{
       
   317 	iTest(aResult);
       
   318 	}
       
   319 
       
   320 EXPORT_C void CHTTPTestUtils::PressAnyKey()
       
   321 //
       
   322 //	Request a key press from user and wait - unless we are running a script or we are being silent
       
   323 	{
       
   324 	if (!iScriptRunning && !iSilent)
       
   325 		{
       
   326 		iTest.Printf(TRefByValue<const TDesC>_L("\nPress a key"));	
       
   327 		iTest.Getch();
       
   328 		}
       
   329 	}
       
   330 
       
   331 EXPORT_C void CHTTPTestUtils::DumpData(const TDesC8& aData, TBool logIt)
       
   332 //
       
   333 //	Do a formatted dump of binary data, optionally logging it
       
   334 	{
       
   335 	// Iterate the supplied block of data in blocks of 16 bytes
       
   336 	TInt pos = 0;
       
   337 	TBuf<KMaxLogEntrySize> logLine;
       
   338 	TBuf<KMaxLogEntrySize> anEntry;
       
   339 	while (pos < aData.Length())
       
   340 		{
       
   341 		anEntry.Format(TRefByValue<const TDesC>_L("%04x : "), pos);
       
   342 		logLine.Append(anEntry);
       
   343 
       
   344 		// Hex output
       
   345 		TInt offset;
       
   346 		for (offset = 0; offset < 16; offset++)
       
   347 			{
       
   348 			if (pos + offset < aData.Length())
       
   349 				{
       
   350 				TInt nextByte = aData[pos + offset];
       
   351 				anEntry.Format(TRefByValue<const TDesC>_L("%02x "), nextByte);
       
   352 				logLine.Append(anEntry);
       
   353 				}
       
   354 			else
       
   355 				{
       
   356 				anEntry.Format(TRefByValue<const TDesC>_L("   "));
       
   357 				logLine.Append(anEntry);
       
   358 				}
       
   359 			}
       
   360 			anEntry.Format(TRefByValue<const TDesC>_L(": "));
       
   361 			logLine.Append(anEntry);
       
   362 
       
   363 		// Char output
       
   364 		for (offset = 0; offset < 16; offset++)
       
   365 			{
       
   366 			if (pos + offset < aData.Length())
       
   367 				{
       
   368 				TInt nextByte = aData[pos + offset];
       
   369 				if ((nextByte >= 32) && (nextByte <= 127))
       
   370 					{
       
   371 					anEntry.Format(TRefByValue<const TDesC>_L("%c"), nextByte);
       
   372 					logLine.Append(anEntry);
       
   373 					}
       
   374 				else
       
   375 					{
       
   376 					anEntry.Format(TRefByValue<const TDesC>_L("."));
       
   377 					logLine.Append(anEntry);
       
   378 					}
       
   379 				}
       
   380 			else
       
   381 				{
       
   382 				anEntry.Format(TRefByValue<const TDesC>_L(" "));
       
   383 				logLine.Append(anEntry);
       
   384 				}
       
   385 			}
       
   386 			if (logIt)
       
   387 				{
       
   388 				LogIt(TRefByValue<const TDesC>_L("%S"), &logLine);
       
   389 				}
       
   390 			else if (!iSilent)
       
   391 				{
       
   392 				iTest.Printf(TRefByValue<const TDesC>_L("%S\n"), &logLine);	
       
   393 				}
       
   394 			logLine.Zero();
       
   395 
       
   396 		// Advance to next 16 byte segment
       
   397 		pos += 16;
       
   398 		}
       
   399 	}
       
   400 
       
   401 EXPORT_C void CHTTPTestUtils::GetAnEntry(const TDesC& ourPrompt, TDes& currentstring)
       
   402 //
       
   403 //	Get an input string from the user, displaying a supplied prompt and default string value
       
   404 	{
       
   405 	// If we're scripting, try reading from script first
       
   406 	TInt readScriptErr = KErrNotFound;
       
   407 	if (iScriptRunning)
       
   408 		{
       
   409 		readScriptErr = ReadLineFromScript(currentstring);
       
   410 		}
       
   411 	if (!readScriptErr)
       
   412 		return;
       
   413 
       
   414 	// Either not scripting, or hit end of script - continue with user input
       
   415 	TBuf16<KMaxUserEntrySize> ourLine;
       
   416 	TBuf<KMaxUserEntrySize> tempstring;				//tempstring is a unicode descriptor
       
   417 										//create a temporary buffer where the
       
   418 										//unicode strings are stored in order to 
       
   419 										//be displayed
       
   420 	ourLine.Zero ();
       
   421 	tempstring.Copy(currentstring);		//Copy current string to Unicode buffer
       
   422 	TKeyCode key = EKeyNull;						//current string buffer is 8 bits wide.
       
   423 										//Unicode string bufffer (tempstring) is 16 bits wide.
       
   424 	for (;;)
       
   425 		{
       
   426 		if (ourLine.Length () == 0)
       
   427 			{
       
   428 			iTest.Console()->SetPos (0, iTest.Console()->WhereY ());
       
   429 			iTest.Console()->Printf (_L ("%S"), &ourPrompt);
       
   430 			if (tempstring.Length () != 0)						//get tempstring's number of items
       
   431 				iTest.Console()->Printf (_L (" = %S"), &tempstring);	//if not zero print them to iTest.Console()
       
   432 			iTest.Console()->Printf (_L (" : "));
       
   433 			iTest.Console()->ClearToEndOfLine ();
       
   434 			}
       
   435 		key = iTest.Getch();
       
   436 		
       
   437 		  if (key == EKeyBackspace)
       
   438 				{
       
   439 					if (ourLine.Length() !=0)
       
   440 					{
       
   441 						ourLine.SetLength(ourLine.Length()-1);
       
   442 						iTest.Console()->Printf (_L ("%c"), key);
       
   443 						iTest.Console()->SetPos(iTest.Console()->WhereX(),iTest.Console()->WhereY());
       
   444 						iTest.Console()->ClearToEndOfLine();
       
   445 					}	// end if (ourLine.Length() !=0)
       
   446 				}	// end if (key == KeyBackSpace)
       
   447 		  
       
   448 		  		  
       
   449 		  if (key == EKeyDelete) 			
       
   450 				{
       
   451 					ourLine.Zero();
       
   452 					iTest.Console()->SetPos (0, iTest.Console()->WhereY ());
       
   453 					iTest.Console()->ClearToEndOfLine ();
       
   454 					tempstring.Copy(ourLine);
       
   455 					break;
       
   456 				}
       
   457 		  
       
   458 		  if (key == EKeyEnter)
       
   459 			break;
       
   460 		
       
   461 		  if (key < 32)
       
   462 			{
       
   463 			continue;
       
   464 			}
       
   465 		
       
   466 		ourLine.Append (key);
       
   467 		iTest.Console()->Printf (_L ("%c"), key);
       
   468 		iTest.Console()->SetPos(iTest.Console()->WhereX(),iTest.Console()->WhereY());
       
   469 		iTest.Console()->ClearToEndOfLine();
       
   470 		if (ourLine.Length () == ourLine.MaxLength ())
       
   471 			break;
       
   472 		}	// end of for statement
       
   473 
       
   474 	if ((key == EKeyEnter) && (ourLine.Length () == 0))
       
   475 		tempstring.Copy (currentstring);				//copy contents of 8 bit "ourLine" descriptor
       
   476 	
       
   477 	iTest.Console()->SetPos (0, iTest.Console()->WhereY ());		
       
   478 	iTest.Console()->ClearToEndOfLine ();
       
   479 	iTest.Console()->Printf (_L ("%S"), &ourPrompt);
       
   480 	
       
   481 	if ((key == EKeyEnter) && (ourLine.Length() !=0))
       
   482 		tempstring.Copy(ourLine);
       
   483 	if (tempstring.Length () != 0)						//if temstring length is not zero
       
   484 		{
       
   485 		iTest.Console()->Printf (_L (" = %S\n"), &tempstring);	//print the contents to iTest.Console()
       
   486 		LogIt(_L ("%S = %S\n"), &ourPrompt, &tempstring);
       
   487 		}
       
   488 
       
   489 	else
       
   490 		//iTest.Console()->Printf (_L (" is empty"));
       
   491 	iTest.Console()->Printf (_L ("\n"));
       
   492 	currentstring.Copy(tempstring);						//copy 16 bit tempstring descriptor back 
       
   493 	}
       
   494 
       
   495 
       
   496 EXPORT_C TInt CHTTPTestUtils::GetSelection(const TDesC& ourPrompt, const TDesC& validChoices)
       
   497 //
       
   498 //	Present the user with a list of options, and get their selection
       
   499 	{
       
   500 	// If we're scripting, try reading from script first
       
   501 	TInt readScriptErr = KErrNotFound;
       
   502 	if (iScriptRunning)
       
   503 		{
       
   504 		TBuf<1> oneCharBuf;
       
   505 		readScriptErr = ReadLineFromScript(oneCharBuf);
       
   506 		if (!readScriptErr)
       
   507 			{
       
   508 			return validChoices.Locate((TChar)oneCharBuf[0]);
       
   509 			}
       
   510 		}
       
   511 
       
   512 	// Either not scripting, or hit end of script - continue with user input
       
   513 	TKeyCode key = EKeyNull;
       
   514 	iTest.Console()->SetPos (0, iTest.Console()->WhereY ());
       
   515 	Print(ourPrompt);
       
   516 	iTest.Console()->Printf(_L("[%S] :"), &validChoices);
       
   517 	TInt retVal = KErrNotFound;
       
   518 	while (retVal == KErrNotFound)
       
   519 		{
       
   520 		key = iTest.Getch();
       
   521 
       
   522 		// Check that key is in the list of valid choices
       
   523 		retVal = validChoices.Locate((TChar)key);
       
   524 		}
       
   525 	iTest.Console()->Printf(_L("%c\n\n"), key);
       
   526 	return retVal;
       
   527 	}
       
   528 
       
   529 
       
   530 EXPORT_C void CHTTPTestUtils::SetScript(RFile& scriptFile)
       
   531 //
       
   532 //	Sets the file to be used for a test script - ie. a file that contains commands used by
       
   533 //  GetEntry() and GetSelection()
       
   534 	{
       
   535 	iScriptFile = &scriptFile;
       
   536 	iScriptRunning = ETrue;
       
   537 	LogIt(_L("***SCRIPT STARTING***\n"));
       
   538 	}
       
   539 
       
   540 EXPORT_C void CHTTPTestUtils::InitCommsL()
       
   541 	{
       
   542 	TInt ret = User::LoadPhysicalDevice(PDD_NAME);
       
   543 	User::LeaveIfError(ret == KErrAlreadyExists?KErrNone:ret);
       
   544 
       
   545 #ifndef __WINS__
       
   546 	ret = User::LoadPhysicalDevice(PDD2_NAME);
       
   547 	ret = User::LoadPhysicalDevice(PDD3_NAME);
       
   548 	ret = User::LoadPhysicalDevice(PDD4_NAME);
       
   549 #endif
       
   550 
       
   551 	ret = User::LoadLogicalDevice(LDD_NAME);
       
   552 	User::LeaveIfError(ret == KErrAlreadyExists?KErrNone:ret);
       
   553 	ret = StartC32();
       
   554 	User::LeaveIfError(ret == KErrAlreadyExists?KErrNone:ret);
       
   555 	}
       
   556 
       
   557 EXPORT_C void CHTTPTestUtils::HoldOpenConnectionL()
       
   558 	{
       
   559 	RSocketServ sockSrvHnd;
       
   560 	TInt err = sockSrvHnd.Connect();
       
   561 	if (err != KErrNone)
       
   562 		{
       
   563 		User::Leave(err);
       
   564 		}
       
   565 
       
   566 	// Start UDP protocol stack
       
   567 	TProtocolDesc desc;
       
   568 	_LIT(KUdp, "udp");
       
   569 	err = sockSrvHnd.FindProtocol(KUdp(), desc);
       
   570 	User::LeaveIfError(err);
       
   571 	TRequestStatus stat;
       
   572 	sockSrvHnd.StartProtocol(desc.iAddrFamily, desc.iSockType, desc.iProtocol, stat);
       
   573 	User::WaitForRequest(stat);
       
   574 	}
       
   575 
       
   576 EXPORT_C void CHTTPTestUtils::ReleaseConnectionL()
       
   577 	{
       
   578 	RSocketServ sockSrvHnd;
       
   579 	TInt err = sockSrvHnd.Connect();
       
   580 	if (err != KErrNone)
       
   581 		{
       
   582 		User::Leave(err);
       
   583 		}
       
   584 
       
   585 	// Stop UDP protocol stack
       
   586 	TProtocolDesc desc;
       
   587 	_LIT(KUdp, "udp");
       
   588 	err = sockSrvHnd.FindProtocol(KUdp(), desc);
       
   589 	User::LeaveIfError(err);
       
   590 	TRequestStatus stat;
       
   591 	sockSrvHnd.StopProtocol(desc.iAddrFamily, desc.iSockType, desc.iProtocol, stat);
       
   592 	User::WaitForRequest(stat);
       
   593 	}
       
   594 
       
   595 TInt CHTTPTestUtils::ReadLineFromScript(TDes& aBuffer)
       
   596 //
       
   597 // Reads the next line from the script file, and sets the passed-in descriptor with its contents.
       
   598 // Returns KErrNone if reading succeeded; KErrNotFound if the EOF was reached. When EOF is reached,
       
   599 // the file is closed.
       
   600 	{
       
   601 	// *********************************
       
   602 	// Assume script is 8-bit text file
       
   603 	// *********************************
       
   604 	TBool isAComment = ETrue;
       
   605 	TInt err = KErrNone;
       
   606 	TBuf<512> line;
       
   607 	while (isAComment && !err)
       
   608 		{
       
   609 		TFileText text;
       
   610 		text.Set(*iScriptFile);
       
   611 		line.SetLength(0);
       
   612 		for(;;)
       
   613 			{
       
   614 			TBuf8<2> c;
       
   615 			err = iScriptFile->Read(c,1);
       
   616 			if (err && err != KErrEof)
       
   617 				{
       
   618 				iTest.Printf(_L("Error reading file: %d\n"), err);
       
   619 				break;
       
   620 				}
       
   621 			if (c.Length() == 0)
       
   622 				{
       
   623 				err = KErrEof;
       
   624 				break;
       
   625 				}
       
   626 			else
       
   627 				{
       
   628 				if (c[0] == '\n') // break out if it is CR
       
   629 					break;
       
   630 				else if (c[0] != 0x0d) // otherwise append the char, _unless_ it is a LF
       
   631 					line.Append(c[0]);
       
   632 				}
       
   633 			}
       
   634 		if (err == KErrNone && line.Locate('/') != 0) // comment (only works if it's the first character)
       
   635 			{
       
   636 			isAComment = EFalse;
       
   637 			}
       
   638 		}
       
   639 
       
   640 	// The line read is not a comment, or have hit end of file
       
   641 	if (!err)
       
   642 		{
       
   643 		// copy to passed in descriptor, but do not allow an overflow
       
   644 		aBuffer.Copy(line.Left(aBuffer.MaxLength()));
       
   645 		LogIt(_L("***SCRIPT : read command '%S' ***\n"), &aBuffer);
       
   646 		}
       
   647 	else
       
   648 		{
       
   649 		iScriptFile->Close();
       
   650 		err = KErrNotFound;
       
   651 		iScriptRunning = EFalse;
       
   652 		LogIt(_L("***SCRIPT ENDED***\n"));
       
   653 		}
       
   654 	return err;
       
   655 	}
       
   656 
       
   657 void CHTTPTestUtils::Panic(TInt aPanic)
       
   658 //
       
   659 //	Panic the client program.
       
   660 	{
       
   661 	User::Panic(KTestPanic,aPanic);
       
   662 	}
       
   663 
       
   664 void CHTTPTestUtils::TestHarnessComplete()
       
   665 //
       
   666 //	Test harness completed without failures
       
   667 	{
       
   668 	WriteComment(KTestHarnessCompleted);
       
   669 	}
       
   670 
       
   671 void CHTTPTestUtils::TestHarnessFailed()
       
   672 //
       
   673 //	Test harness has a failure - log information
       
   674 	{
       
   675 	TBuf<KMaxFileName + 4> buf;
       
   676 	buf.Format(KTestHarnessFailed, iFailedTests->Count());
       
   677 	WriteComment(buf);
       
   678 	//	Log fialed tests' information
       
   679 	for (TInt ii=0; ii<iFailedTests->Count(); ++ii)
       
   680 		{
       
   681 		CTestInfo* failed = iFailedTests->At(ii);
       
   682 		TPtrC name = failed->Name();
       
   683 		LogIt(KTestFailInfo, failed->Number(), &name, failed->ErrorCode());
       
   684 		}
       
   685 	}
       
   686 
       
   687 void CHTTPTestUtils::ResourceLeakTest()
       
   688 //
       
   689 // Creates a new test that fails if any there are any leaked resource handles
       
   690 	{
       
   691 	// Start new test
       
   692 	_LIT(KResourceTestName, "Resource Handle Leak Test");
       
   693 	TRAPD(error,StartTestL(KResourceTestName));
       
   694 
       
   695 	if (error !=KErrNone)
       
   696 		Panic(EBadStartTest);
       
   697 
       
   698 	//	Find number of opened handles
       
   699 	TInt processHandleCount=0;
       
   700 	TInt threadHandleCount=0;
       
   701 	RThread().HandleCount(processHandleCount,threadHandleCount);
       
   702 	TInt err = KErrNone;
       
   703 	TInt openHandleCount = iStartHandleCount-threadHandleCount;
       
   704 	if ( openHandleCount !=0)
       
   705 		{
       
   706 		err = KErrGeneral;
       
   707 		LogIt(_L("Number leaked handles is %D"), openHandleCount);
       
   708 		}
       
   709 	EndTest(err);
       
   710 	}
       
   711 
       
   712 
       
   713 EXPORT_C
       
   714 TInt CHTTPTestUtils::ResolveFile(const TDesC& aComponent, const TDesC& aFileName, TParse& aParseOut)
       
   715 	{
       
   716 	TFileName* savedPath = new TFileName;
       
   717 	TFileName* fileName = new TFileName;
       
   718 	if ((savedPath == NULL) || (fileName == NULL))
       
   719 		return KErrNoMemory;
       
   720 
       
   721 	fileName->Append(KPathSep);
       
   722 	fileName->Append(KTestFileInputBase);
       
   723 	fileName->Append(KPathSep);
       
   724 	fileName->Append(aComponent);
       
   725 	fileName->Append(KPathSep);
       
   726 	fileName->Append(aFileName);
       
   727 	
       
   728 	// file finder will look in the session drive first, then Y->A,Z
       
   729 	// so set session drive to Y (save old and restore it afterwards)
       
   730 	iFs.SessionPath(*savedPath);
       
   731 	_LIT(KTopDrive,"Y:\\");
       
   732 	iFs.SetSessionPath(KTopDrive);
       
   733     TFindFile file_finder(iFs);
       
   734     TInt err = file_finder.FindByDir(*fileName,KNullDesC);
       
   735 	if(err==KErrNone)
       
   736 		aParseOut.Set(file_finder.File(),NULL,NULL);
       
   737 	iFs.SetSessionPath(*savedPath);
       
   738 	delete savedPath;
       
   739 	delete fileName;
       
   740 	return(err);
       
   741 	}
       
   742 
       
   743 EXPORT_C
       
   744 HBufC8* CHTTPTestUtils::ReadFileLC(const TDesC& aFile)
       
   745 	{
       
   746 	//Returns a HBufC8 with the contents of aFile
       
   747 	RFile file;
       
   748 	User::LeaveIfError(file.Open(iFs, aFile, EFileShareAny | EFileRead));
       
   749 	CleanupClosePushL(file);
       
   750 
       
   751 	TInt size = 0;
       
   752 	TInt err = file.Size(size);
       
   753 
       
   754 	if (err)
       
   755 		{
       
   756 		iTest.Printf(_L("ReadFileLC(%S) leaving with %d. Unable to size file\n"), &aFile, err);
       
   757 		User::Leave(err);
       
   758 		}
       
   759 
       
   760 	HBufC8* contents = HBufC8::NewLC(size + 4);
       
   761 	TPtr8 contentsPtr(contents->Des());
       
   762 	err = file.Read(0, contentsPtr);
       
   763 
       
   764 	if (err)
       
   765 		{
       
   766 		iTest.Printf(_L("ReadFileLC(%S) leaving with %d. Unable to read file\n"), &aFile, err);
       
   767 		User::Leave(err);
       
   768 		}
       
   769 
       
   770 	CleanupStack::Pop(); //contents
       
   771 	CleanupStack::PopAndDestroy(); //file
       
   772 	CleanupStack::PushL(contents);
       
   773 
       
   774 	return contents;
       
   775 	}
       
   776 
       
   777 
       
   778 
       
   779 
       
   780 //
       
   781 //	CTestInfo
       
   782 //
       
   783 CHTTPTestUtils::CTestInfo::CTestInfo()
       
   784 //
       
   785 //	Default c'tor
       
   786 	{
       
   787 	}
       
   788 
       
   789 CHTTPTestUtils::CTestInfo::~CTestInfo()
       
   790 //
       
   791 //	D'tor
       
   792 	{
       
   793 	delete iName;
       
   794 	}
       
   795 
       
   796 CHTTPTestUtils::CTestInfo* CHTTPTestUtils::CTestInfo::NewLC(const TDesC& aName, TInt aNumber, TInt aErrorCode)
       
   797 //
       
   798 //	Static factory c'tor
       
   799 	{
       
   800 	CTestInfo* self = new (ELeave) CTestInfo();
       
   801 	CleanupStack::PushL(self);
       
   802 	self->ConstructL(aName, aNumber, aErrorCode);
       
   803 	return self;
       
   804 	}
       
   805 
       
   806 CHTTPTestUtils::CTestInfo* CHTTPTestUtils::CTestInfo::NewL(const TDesC& aName, TInt aNumber, TInt aErrorCode)
       
   807 //
       
   808 //	Static factory c'tor
       
   809 	{
       
   810 	CTestInfo* self = NewLC(aName, aNumber, aErrorCode);
       
   811 	CleanupStack::Pop();	//	self
       
   812 	return self;
       
   813 	}
       
   814 
       
   815 void CHTTPTestUtils::CTestInfo::ConstructL(const TDesC& aName, TInt aNumber, TInt aErrorCode)
       
   816 //
       
   817 //	Non-trivial c'tor
       
   818 	{
       
   819 	iName = aName.AllocLC();
       
   820 	CleanupStack::Pop();	//	iName
       
   821 
       
   822 	iNumber = aNumber;
       
   823 	iErrorCode = aErrorCode;
       
   824 	}
       
   825 
       
   826 void CHTTPTestUtils::CTestInfo::SetNameL(const TDesC& aName)
       
   827 //
       
   828 //	Sets iName
       
   829 	{
       
   830 	HBufC* temp = aName.AllocLC();
       
   831 	CleanupStack::Pop();	//	temp
       
   832 	delete iName;
       
   833 	iName = temp;
       
   834 	}
       
   835 
       
   836 void CHTTPTestUtils::CTestInfo::SetNumber(TInt aNumber)
       
   837 //
       
   838 //	Sets iNumber
       
   839 	{
       
   840 	iNumber = aNumber;
       
   841 	}
       
   842 
       
   843 void CHTTPTestUtils::CTestInfo::SetErrorCode(TInt aErrorCode)
       
   844 //
       
   845 //	Sets iErrorCode
       
   846 	{
       
   847 	iErrorCode = aErrorCode;
       
   848 	}
       
   849 
       
   850 #ifndef EKA2
       
   851 GLDEF_C TInt E32Dll(TDllReason /*aReason*/)
       
   852 //
       
   853 //	Main Dll entry point
       
   854 	{
       
   855 	return(KErrNone);
       
   856 	}
       
   857 #endif // EKA2
       
   858 
       
   859 void CHTTPTestUtils::Print(const TDesC& aDescriptor)
       
   860 	{
       
   861 	TBuf<KMaxTempBuffize> chunkOfPrompt;
       
   862 	TInt length = aDescriptor.Length();
       
   863 	TInt num=length/KMaxTempBuffize;
       
   864 	TInt rest=length%KMaxTempBuffize;
       
   865 	TInt iter;
       
   866 	if(num>0)
       
   867 		for(iter=0;iter<num;++iter)
       
   868 			{
       
   869 			chunkOfPrompt.Copy(aDescriptor.Mid(iter*KMaxTempBuffize,KMaxTempBuffize-1));
       
   870 			iTest.Console()->Printf(_L("%S "), & chunkOfPrompt);
       
   871 			}
       
   872 	chunkOfPrompt.Copy(aDescriptor.Mid(length-rest,rest));
       
   873 	iTest.Console()->Printf(_L("%S "), & chunkOfPrompt);	
       
   874 	}