pkiutilities/ocsp/test/main.cpp
changeset 0 164170e6151a
equal deleted inserted replaced
-1:000000000000 0:164170e6151a
       
     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 // Top-level test control implementation
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "main.h"
       
    19 #include "comms.h"
       
    20 #include "script.h"
       
    21 #include "logger.h"
       
    22 #include "result.h"
       
    23 #include "panic.h"
       
    24 #include "tcertutils.h"
       
    25 #include "bautils.h" 
       
    26 
       
    27 
       
    28 _LIT(KRunStep, "RUN_TEST_STEP");
       
    29 
       
    30 // Optional 3rd argument to turn on OOM tests
       
    31 _LIT(KOOMArgument, "OOM");
       
    32 
       
    33 // First script test to run
       
    34 #ifdef _DEBUG
       
    35 const TInt KMinTransaction = 1;
       
    36 #endif
       
    37 
       
    38 // First heap fail point to try
       
    39 const TInt KMinHeapFailPoint = 1;
       
    40 
       
    41 // Number of allocations to test for before bailing out
       
    42 const TInt KMaxHeapFailPoint = 100000;
       
    43 
       
    44 // Number of consecutive successful completions before we belive we've finished
       
    45 // an OOM test
       
    46 const TInt KCompletions = 5;
       
    47 
       
    48 // Where to find file cert store files
       
    49 
       
    50 _LIT(KCertStoreCertsFilePath, "\\private\\101f72a6\\cacerts.dat");
       
    51 _LIT(KCertStoreClientsFilePath, "\\private\\101f72a6\\certapps.dat");
       
    52 
       
    53 
       
    54 CTOCSPMain* CTOCSPMain::NewLC(const TDesC& aScriptFile, const TDesC& aLogFile)
       
    55 	{
       
    56 	CTOCSPMain* self = new (ELeave) CTOCSPMain(aScriptFile);
       
    57 	CleanupStack::PushL(self);
       
    58 	self->ConstructL(aLogFile);
       
    59 	return self;
       
    60 	}
       
    61 
       
    62 CTOCSPMain::CTOCSPMain(const TDesC& aScriptFile) :
       
    63 	CActive(EPriorityNormal),
       
    64 	iScriptFile(aScriptFile)
       
    65 	{
       
    66 	CActiveScheduler::Add(this);
       
    67 	}
       
    68 
       
    69 void CTOCSPMain::ConstructL(const TDesC& aLogFile)
       
    70 	{	
       
    71 	iLog = CTOCSPLogger::NewL(aLogFile);
       
    72 	User::LeaveIfError(iFs.Connect());
       
    73 
       
    74 	// this is the name of the test file we will write to from the script
       
    75 	iTocspTestFile.Copy(_L("\\system\\tocsp\\tocsptestfile.txt"));
       
    76 
       
    77 	if (BaflUtils::FolderExists(iFs, iTocspTestFile))
       
    78 		{		
       
    79 		if (BaflUtils::FileExists( iFs, iTocspTestFile ))
       
    80 			{
       
    81 			// delete the test file if it exists before starting the tests
       
    82 			iFs.Delete(iTocspTestFile);	
       
    83 			}
       
    84 		}
       
    85 	}
       
    86 
       
    87 CTOCSPMain::~CTOCSPMain()
       
    88 	{
       
    89 	Cancel();
       
    90 	delete iLog;
       
    91 	delete iTest;
       
    92 	delete iResult;
       
    93 	if (BaflUtils::FolderExists(iFs, iTocspTestFile))
       
    94 			{		
       
    95 			if (BaflUtils::FileExists( iFs, iTocspTestFile ))
       
    96 				{
       
    97 				// delete the test file if it exists at the end of the tests
       
    98 				iFs.Delete(iTocspTestFile);
       
    99 				}
       
   100 			}
       
   101 	iFs.Close();
       
   102 	}
       
   103 
       
   104 void CTOCSPMain::RunNormalTestsL()
       
   105 	{
       
   106 	iLog->LogL(_L("<html><body><pre>\r\n"), ETrue);
       
   107 	TPtrC scriptcontents;
       
   108 	TPtrC inicontents;
       
   109 	TPtrC runTestStep;
       
   110 	TPtrC iniFileName;
       
   111 	TPtrC iniSectionName;
       
   112 	TBuf<512> iniFilePath;
       
   113 	
       
   114 	TInt err = KErrNone;
       
   115 	TInt pos = 0;
       
   116 	
       
   117 	iLog->LogL(_L("Starting normal OCSP tests...\n"), ETrue);
       
   118 
       
   119     iError = KErrNone;
       
   120 
       
   121 	ResetTestL();
       
   122 
       
   123 	__UHEAP_MARK;
       
   124 
       
   125 //	TBool runStep = EFalse;
       
   126 	
       
   127 //	To check if the input file is .script file
       
   128 //	IF a script file
       
   129 //	Reads in the contents of the script file and loads it on 'scriptcontents'
       
   130 //	Extracs RUN_TEST_STEP line onto 'runTestStep'
       
   131 //	Extracts .ini file name and the appropriate .ini section name
       
   132 //	Generates the .ini file path
       
   133 //	Reads in the contents of the ini file and loads it on 'inicontents'
       
   134 //	Extracts the approritate section part and sets iSectionData
       
   135 	
       
   136 	iScriptFlag = TEFparser::FileType(iScriptFile);
       
   137 	
       
   138 	if(iScriptFlag)
       
   139 		{
       
   140 		do
       
   141 			// get the run test step lines from the script file
       
   142 			{
       
   143 			err = TEFparser::ReadFileLC(iFs, iScriptFile, scriptcontents);
       
   144 			runTestStep.Set(TEFparser::GetRunTestStep(scriptcontents, KRunStep, pos, err));
       
   145 			if(err == KErrNone)
       
   146 				{
       
   147 				// get the ini file info for the current test step
       
   148 				err = TEFparser::GetIniFileInfo(runTestStep, iniFileName, iniSectionName);
       
   149 				if (err == KErrNone)
       
   150 					{
       
   151 					// get the path to the ini file for the current test step
       
   152 					err = TEFparser::GetiniPath(iniFileName, iScriptFile, iniFilePath);
       
   153 					if (err == KErrNone)
       
   154 						{
       
   155 						// get the data in section for of the ini file for the current test step and write it into the
       
   156 						// test file
       
   157 						err = TEFparser::GetSectionData(iniFilePath, iniSectionName, iTocspTestFile, iFs);
       
   158 						if (err != KErrNone)
       
   159 							{
       
   160 							ReportErrorL();
       
   161 							}
       
   162 						CleanupStack::PopAndDestroy();
       
   163 						}	
       
   164 						else
       
   165 						{
       
   166 							ReportErrorL();
       
   167 						}
       
   168 					}
       
   169 					else
       
   170 					{
       
   171 						ReportErrorL();
       
   172 					}
       
   173 				}
       
   174 			else
       
   175 				{
       
   176 				CleanupStack::PopAndDestroy();
       
   177 				}
       
   178 			}while(err == KErrNone);
       
   179 		// execute the tests for the complete test file with all the ini file section data in
       
   180 		iResult = CTOCSPResult::NewL();
       
   181 		iTest = CTOCSPScript::NewL(*iLog, *iResult, iTocspTestFile);
       
   182 		iTest->SetVerbose(ETrue);
       
   183 		iState = ERunningNormalTest;
       
   184 		iTest->RunTestL(iStatus);
       
   185 		SetActive();
       
   186 		CActiveScheduler::Start();
       
   187 		}
       
   188 	else
       
   189 		{
       
   190 		iResult = CTOCSPResult::NewL();
       
   191 		iTest = CTOCSPScript::NewL(*iLog, *iResult, iScriptFile);
       
   192 		iTest->SetVerbose(ETrue);
       
   193 		iState = ERunningNormalTest;
       
   194 		iTest->RunTestL(iStatus);
       
   195 		SetActive();
       
   196 		CActiveScheduler::Start();
       
   197 		}
       
   198 		
       
   199 	
       
   200 	}
       
   201 
       
   202 void CTOCSPMain::RunOOMTestsL()
       
   203 	{
       
   204 #ifdef _DEBUG
       
   205 	iLog->LogL(_L("\nStarting OCSP OOM tests from script...\n"), ETrue);
       
   206 
       
   207     iError = KErrNone;
       
   208 
       
   209 	iTransaction = KMinTransaction;
       
   210 	iFailPoint = KMinHeapFailPoint;
       
   211 	iCompletions = 0;
       
   212 	ResetTestL();
       
   213 	StartNextOOMTestL();
       
   214 
       
   215     // Run async code
       
   216 	CActiveScheduler::Start();
       
   217 
       
   218 	ReportErrorL();
       
   219 #else
       
   220 	iLog->LogL(_L("No OOM tests run on release build.\n"), ETrue);
       
   221 	// reports a single test run to satisfy corebuilder log parsing requirements
       
   222 	iLog->LogL(_L("0 tests failed out of 1\n"));
       
   223 #endif
       
   224 	}
       
   225 
       
   226 // Report any error and raise it to the client
       
   227 void CTOCSPMain::ReportErrorL()
       
   228 	{
       
   229 	if (iError != KErrNone)
       
   230 		{	
       
   231 		iLog->LogL(_L("Exiting with leave code: "));
       
   232 		iLog->LogL(iError);
       
   233 		iLog->NewLineL();
       
   234 		iLog->LogL(_L("1 tests failed out of 1 (unknown)\n"));
       
   235 		}
       
   236 	}
       
   237 
       
   238 TInt CTOCSPMain::RunError(TInt aError)
       
   239 	{
       
   240 	if (iState == ERunningNormalTest || iState == ERunningOOMTest)
       
   241 		{
       
   242 		// attempt to clean up after ourselves
       
   243 		delete iTest;
       
   244 		iTest = NULL;
       
   245 
       
   246 		delete iResult;
       
   247 		iResult = NULL;
       
   248 
       
   249 		__UHEAP_MARKEND;
       
   250 		}
       
   251 
       
   252 	iError = aError;
       
   253 	CActiveScheduler::Stop();	
       
   254 	return KErrNone;
       
   255 	}
       
   256 
       
   257 void CTOCSPMain::DoCancel()
       
   258 	{
       
   259 	switch (iState)
       
   260 		{
       
   261 		case ERunningNormalTest:
       
   262 		case ERunningOOMTest:
       
   263 			iTest->Cancel();
       
   264 			break;
       
   265 
       
   266 		default:
       
   267 			Panic(KErrCorrupt);
       
   268 		}
       
   269 	}
       
   270 
       
   271 void CTOCSPMain::RunL()
       
   272 	{
       
   273 	switch (iState)
       
   274 		{
       
   275 		case ERunningNormalTest:
       
   276 			FinishedNormalTestL();
       
   277 			break;
       
   278 
       
   279 		case ERunningOOMTest:
       
   280 			FinishedOOMTestL();
       
   281 			break;
       
   282 
       
   283 		default:
       
   284 			Panic(KErrCorrupt);
       
   285 		}
       
   286 	}
       
   287 
       
   288 void CTOCSPMain::FinishedNormalTestL()
       
   289 	{
       
   290 	delete iTest;
       
   291 	iTest = NULL;
       
   292 	
       
   293 	iLog->NewLineL();
       
   294 	if (iStatus == KErrNone)
       
   295 		{
       
   296 		iLog->LogL(_L("Tests completed OK"), ETrue);
       
   297 		iLog->NewLineL();
       
   298 		}
       
   299 	else
       
   300 		{
       
   301 		iLog->NewLineL();
       
   302 		iLog->LogL(_L("ERROR: Leave code = "), ETrue);
       
   303 		iLog->LogL(iStatus.Int());
       
   304 		iLog->NewLineL();
       
   305 		User::Leave(iStatus.Int());
       
   306 		}
       
   307 	
       
   308 	iResult->LogSummaryL(*iLog);
       
   309 
       
   310 	delete iResult;
       
   311 	iResult = NULL;
       
   312 	
       
   313 	__UHEAP_MARKEND;
       
   314 
       
   315 	CActiveScheduler::Stop();
       
   316 	}
       
   317 
       
   318 void CTOCSPMain::StartNextOOMTestL()
       
   319 	{
       
   320 	if (iFailPoint == 1)
       
   321 		{
       
   322 		iLog->LogL(_L("*** Testing transaction "), ETrue);
       
   323 		iLog->LogL(iTransaction, ETrue);
       
   324 		iLog->LogL(_L("\n"), ETrue);
       
   325 		}
       
   326 
       
   327 	iLog->LogL(_L("\n"));
       
   328 	iLog->LogL(iFailPoint);
       
   329 	iLog->LogL(_L(" "));
       
   330 
       
   331 	__UHEAP_MARK;
       
   332 
       
   333 	iResult = CTOCSPResult::NewL();
       
   334 	iTest = CTOCSPScript::NewL(*iLog, *iResult, iTocspTestFile, iTransaction);
       
   335 
       
   336 	iTest->SetVerbose(EFalse);
       
   337 	iTest->RunTestL(iStatus);
       
   338 	iState = ERunningOOMTest;
       
   339 	SetActive();
       
   340 
       
   341 	__UHEAP_SETFAIL(RHeap::EFailNext, iFailPoint);
       
   342 	}
       
   343 
       
   344 void CTOCSPMain::FinishedOOMTestL()
       
   345 	{
       
   346 	__UHEAP_SETFAIL(RHeap::ENone, 0);
       
   347 
       
   348 	TBool more = ETrue;
       
   349 
       
   350 	if (iTest)
       
   351 		{
       
   352 		more = CheckOOMTestResultL();
       
   353 		}
       
   354 
       
   355 	delete iTest;
       
   356 	iTest = NULL;
       
   357 	delete iResult;
       
   358 	iResult = NULL;
       
   359 
       
   360 	__UHEAP_MARKEND;
       
   361 
       
   362 	if (more)
       
   363 		{
       
   364 		ResetTestL();
       
   365 		StartNextOOMTestL();
       
   366 		}
       
   367 	else
       
   368 		{
       
   369 		CActiveScheduler::Stop();
       
   370 		}
       
   371 	}
       
   372 
       
   373 TBool CTOCSPMain::CheckOOMTestResultL()
       
   374 	{
       
   375 	if (iStatus != KErrNone &&
       
   376 		iStatus != KErrNoMemory  &&
       
   377 		iStatus != KErrDiskFull)
       
   378 		{
       
   379 		// error, report and exit
       
   380 		iLog->LogL(_L("\nHeap fail test (fail at "), ETrue);
       
   381 		iLog->LogL(iFailPoint);
       
   382 		iLog->LogL(_L(") exited with unexpected return code "));
       
   383 		iLog->LogL(iStatus.Int());
       
   384 		iLog->LogL(_L("\n"));
       
   385 		iLog->LogL(_L("1 tests failed out of 1\n"));
       
   386 		User::Leave(iStatus.Int());
       
   387 		}
       
   388 
       
   389 	// Count number of successive successful completions
       
   390 	if (iStatus == KErrNone)
       
   391 		{
       
   392 		++iCompletions;
       
   393 		}
       
   394 	else
       
   395 		{
       
   396 		iCompletions = 0;
       
   397 		}
       
   398 	
       
   399 	if (iCompletions == KCompletions)
       
   400 		{
       
   401 		// finished a round of testing
       
   402 		if (iTransaction < iTest->TransactionCount())
       
   403 			{
       
   404 			// more transactions
       
   405 			iFailPoint = KMinHeapFailPoint;
       
   406 			++iTransaction;
       
   407 			iCompletions = 0;
       
   408 			iLog->NewLineL();
       
   409 			}
       
   410 		else
       
   411 			{
       
   412 			// we're finished
       
   413 			iLog->LogL(_L("0 tests failed out of 1\n"));
       
   414 			return EFalse;
       
   415 			}
       
   416 		}
       
   417 	else
       
   418 		{
       
   419 		// go round again
       
   420 		++iFailPoint;
       
   421 		if (iFailPoint > KMaxHeapFailPoint)
       
   422 			{
       
   423 			iLog->LogL(_L("\nHeap fail test exceeded "), ETrue);
       
   424 			iLog->LogL(KMaxHeapFailPoint);
       
   425 			iLog->LogL(_L(" allocations\n"));
       
   426 			iLog->LogL(_L("1 tests failed out of 1\n"));
       
   427 			User::Leave(KErrGeneral);		  
       
   428 			}
       
   429 		}
       
   430 
       
   431 	return ETrue;
       
   432 	}
       
   433 
       
   434 void CTOCSPMain::ResetTestL()
       
   435 	{
       
   436 	User::After(5 * 1000 * 1000);
       
   437 	// We could use CCertUtils to remove the certs, but it's a lot faster just
       
   438 	// to delete the files
       
   439 	TDriveUnit sysDrive (RFs::GetSystemDrive());
       
   440 	TDriveName driveName(sysDrive.Name());
       
   441 	//file cert store files
       
   442 	TBuf<128> certStoreFilePath (driveName);
       
   443 	certStoreFilePath.Append(KCertStoreCertsFilePath);
       
   444 	
       
   445 	TInt error;
       
   446 	error = iFs.Delete(certStoreFilePath);
       
   447 	if (error != KErrNone && error != KErrNotFound && error != KErrPathNotFound)
       
   448 	{
       
   449 		// Handle a KErrInUse error. We try to delete up to 10 times, 
       
   450 		// and wait 1 sec between two attemps
       
   451 		if( KErrInUse == error) 
       
   452 		{
       
   453 			TInt tryCount = 10;
       
   454 			do 
       
   455 			{
       
   456 				User::After(1000000);	
       
   457 				error = iFs.Delete(certStoreFilePath);
       
   458 				
       
   459 			} while(0 != tryCount-- && error != KErrNone);
       
   460 			
       
   461 			if( 0 == tryCount ) 
       
   462 			{
       
   463 				User::Leave(error);
       
   464 			}
       
   465 			
       
   466 		}
       
   467 		else 
       
   468 		{
       
   469 			User::Leave(error);	
       
   470 		}
       
   471 	
       
   472 	}
       
   473 
       
   474 	//file cert store files
       
   475 	certStoreFilePath.Copy(driveName);
       
   476 	certStoreFilePath.Append(KCertStoreClientsFilePath);
       
   477 	
       
   478 	error = iFs.Delete(certStoreFilePath);
       
   479 	if (error != KErrNone && error != KErrNotFound && error != KErrPathNotFound)
       
   480 		{
       
   481 		User::Leave(error);
       
   482 		}
       
   483 	}
       
   484 
       
   485 LOCAL_C void RunScriptTestL()
       
   486     {
       
   487 	// Start comms stuff - only necessary on WINS where this is an executable
       
   488 #ifdef __WINS__
       
   489 	InitCommsL();
       
   490 #endif
       
   491 
       
   492 	CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
       
   493 	CleanupStack::PushL(scheduler);
       
   494 	CActiveScheduler::Install(scheduler);
       
   495 
       
   496 	// Get command line
       
   497 	HBufC* argv = HBufC::NewLC(User::CommandLineLength());
       
   498 	TPtr cmd(argv->Des());
       
   499 	User::CommandLine(cmd);
       
   500 
       
   501 	// Validate command line
       
   502 	TLex argumentsValidation(cmd);	
       
   503 	TInt argc = 0;
       
   504 	while (argumentsValidation.NextToken().Length()) argc++;
       
   505 
       
   506 	if (argc < 2 || argc > 3)
       
   507 		{
       
   508 		User::Leave(KErrArgument);
       
   509 		}
       
   510 
       
   511 	// Command line had 2 args - OK
       
   512 	TLex arguments(cmd);
       
   513 	TPtrC inputFile(arguments.NextToken());
       
   514 	TPtrC logFile(arguments.NextToken());
       
   515 
       
   516 	CTOCSPMain *main = CTOCSPMain::NewLC(inputFile, logFile);
       
   517 
       
   518 	TBool memoryTests = EFalse;
       
   519 
       
   520 	if (argc == 3)
       
   521 		{
       
   522 		TPtrC memArg(arguments.NextToken());
       
   523 		if (memArg != KOOMArgument)
       
   524 			{
       
   525 			User::Leave(KErrArgument);
       
   526 			}
       
   527 		memoryTests = ETrue;
       
   528 		}
       
   529 
       
   530 	if (memoryTests)
       
   531 		{
       
   532 		main->RunOOMTestsL();
       
   533 		}
       
   534 	else
       
   535 		{
       
   536 		main->RunNormalTestsL();
       
   537 		}
       
   538 
       
   539 	CleanupStack::PopAndDestroy(main);
       
   540 
       
   541 	CleanupStack::PopAndDestroy(argv);
       
   542 	CleanupStack::PopAndDestroy(scheduler);
       
   543     }
       
   544 
       
   545 // Entry point
       
   546 GLDEF_C TInt E32Main()
       
   547     {
       
   548 	__UHEAP_MARK;
       
   549 	CTrapCleanup* cleanup=CTrapCleanup::New();
       
   550 
       
   551 	TRAPD(error,RunScriptTestL()); 
       
   552 	__ASSERT_ALWAYS(!error, User::Panic(_L("OCSP TEST ERROR"), error));
       
   553 
       
   554 	delete cleanup;
       
   555 	__UHEAP_MARKEND;
       
   556 	return 0;
       
   557     }