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