os/security/cryptoservices/certificateandkeymgmt/tx509/SyntaxTest.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     5 * under the terms of the License "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: 
    15 *
    16 */
    17 
    18 
    19 #include "SyntaxTest.h"
    20 #include "wtlscert.h"
    21 #include "t_input.h"
    22 
    23 TInt CSyntaxTest::nInstances = 0;
    24 _LIT(KSyntaxLogFile, "X509SyntaxLog.txt");
    25 _LIT(KPathStart, "<path>");
    26 
    27 //////////////////////////////////////////////////////////////////////
    28 // Construction/Destruction
    29 //////////////////////////////////////////////////////////////////////
    30 
    31 CTestAction* CSyntaxTest::NewL(RFs& aFs, CConsoleBase& aConsole, 
    32 		Output& aOut, const TTestActionSpec& aTestActionSpec)
    33 	{
    34 	CTestAction* self = CSyntaxTest::NewLC(aFs, aConsole, aOut, aTestActionSpec);
    35 	CleanupStack::Pop(self);
    36 	return self;
    37 	}
    38 
    39 CTestAction* CSyntaxTest::NewLC(RFs& aFs, CConsoleBase& aConsole, 
    40 		Output& aOut, const TTestActionSpec& aTestActionSpec)
    41 	{
    42 	CSyntaxTest* self = new(ELeave) CSyntaxTest(aFs, aConsole, aOut);
    43 	CleanupStack::PushL(self);
    44 	self->ConstructL(aTestActionSpec);
    45 	return self;
    46 	}
    47 
    48 CSyntaxTest::CSyntaxTest(RFs& aFs, 
    49 								 CConsoleBase& aConsole,
    50 								 Output& aOut)
    51 : CTestAction(aConsole, aOut), iFs(aFs)
    52 	{
    53 	nFileNumber = 0;
    54 	}
    55 
    56 CSyntaxTest::~CSyntaxTest(void)
    57 	{
    58 	delete iDirList;
    59 	delete iSyntaxOut;
    60 	iLogFile.Close();
    61 	delete iWriter;
    62 	iExpectedResults->ResetAndDestroy();
    63 	delete iExpectedResults;
    64 	};
    65 
    66 void CSyntaxTest::ConstructL(const TTestActionSpec& aTestActionSpec)
    67 	{
    68 	CTestAction::ConstructL(aTestActionSpec);
    69 
    70 	if(nInstances==0)
    71 		{
    72 		nInstances++;
    73 		HBufC* body = HBufC::NewLC(aTestActionSpec.iActionBody.Length());
    74 		iExpectedResults = new (ELeave)RPointerArray<CSyntaxResult>;
    75 		body->Des().Copy(aTestActionSpec.iActionBody);
    76 		TPtrC chainBuf = Input::ParseElement(*body, KPathStart);
    77 		iPath.Copy(chainBuf);
    78 		CleanupStack::PopAndDestroy();
    79 		User::LeaveIfError(iLogFile.Replace(iFs,KSyntaxLogFile,EFileWrite));
    80 		iSyntaxOut = new(ELeave) FileOutput(iLogFile);
    81 		iWriter = new(ELeave) CertWriter(iSyntaxOut);
    82 		LoadResultsL();
    83 		}
    84 	else
    85 		{
    86 		SetScriptError(ESyntax, _L("Only one syntax test can be run in each script"));
    87 		iFinished = ETrue;		
    88 		}
    89 	}
    90 
    91 TBool CSyntaxTest::LoadResultsL()
    92 	{
    93 	RFile resultsFile;
    94 	TFileName fullPath;
    95 	TInt err, fileSize;
    96 	HBufC8 *fileInfo;
    97 
    98 	fullPath.Append(iPath);
    99 	fullPath.Append(KResultsFile);
   100 
   101 	err = resultsFile.Open(iFs, fullPath, EFileRead);
   102 	if (err != KErrNone)
   103 		{
   104 		iConsole.Printf(_L("Error opening results file : "));
   105 		iConsole.Printf(fullPath);
   106 		iConsole.Printf(_L("\n"));
   107 		return(EFalse);
   108 		}
   109 
   110 	CleanupClosePushL(resultsFile);
   111 	resultsFile.Size(fileSize);
   112 	CleanupStack::PopAndDestroy(); // resultsFile
   113 	
   114 	fileInfo = HBufC8::NewLC(fileSize);
   115 	TPtr8 fileInfoPtr(fileInfo->Des());
   116 	fileInfoPtr.SetLength(fileSize);
   117 
   118 	RFileReadStream fileStream;
   119 	User::LeaveIfError(fileStream.Open(iFs, fullPath, EFileStream));
   120 	CleanupClosePushL(fileStream);
   121 	fileStream.ReadL(fileInfoPtr, fileSize);
   122 
   123 	TLex8 theLex(fileInfoPtr);
   124 	CSyntaxResult *syntaxResult = NULL;
   125 	
   126 	do
   127 		{
   128 		syntaxResult = CSyntaxResult::NewLC();
   129 		syntaxResult->SetFilename(theLex.NextToken());
   130 		syntaxResult->SetResult(theLex.NextToken());
   131 		iExpectedResults->Append(syntaxResult);
   132 		CleanupStack::Pop(); // syntaxResult
   133 		}
   134 	while(!theLex.Eos());
   135 
   136 	CleanupStack::PopAndDestroy(2);  // fileinfo & fileStream
   137 	return(ETrue);
   138 	};
   139 
   140 
   141 void CSyntaxTest::DoPerformPrerequisite(TRequestStatus& aStatus)
   142 	{
   143 	HBufC *searchPath = HBufC::NewLC(iPath.Size() + 1);
   144 	TPtr searchPathPtr(searchPath->Des());
   145 	searchPathPtr.Copy(iPath);
   146 	searchPathPtr.Append(_L("*"));
   147     TInt err = iFs.GetDir(searchPathPtr, KEntryAttNormal, ESortByName, iDirList);
   148 	if (err != KErrNone)
   149 		{
   150 		iConsole.Printf(_L("Error getting directory "));
   151 		iConsole.Printf(searchPathPtr);
   152 		iConsole.Printf(_L("\n"));
   153 		iOut.writeString(_L("Error getting directory "));
   154 		iOut.writeString(searchPathPtr);
   155 		iOut.writeNewLine();
   156 		iFinished = ETrue;
   157 		TRequestStatus* status = &aStatus;
   158 		User::RequestComplete(status, KErrNone);
   159 		SetScriptError(EFileNotFound, searchPathPtr);
   160 		}
   161 	else
   162 		{
   163 		iConsole.Printf(_L("Please view "));
   164 		iConsole.Printf(KSyntaxLogFile);
   165 		iConsole.Printf(_L(" for results\n"));
   166 		iOut.writeString(_L("Please view "));
   167 		iOut.writeString(KSyntaxLogFile);
   168 		iOut.writeString(_L(" for results"));
   169 		iOut.writeNewLine();
   170 		iActionState = EAction;
   171 		TRequestStatus* status = &aStatus;
   172 		User::RequestComplete(status, KErrNone);
   173 		iResult = ETrue;
   174 		}
   175 	CleanupStack::PopAndDestroy(); // searchPath
   176 	}
   177 
   178 void CSyntaxTest::DoPerformPostrequisite(TRequestStatus& aStatus)
   179 	{
   180 	TRequestStatus* status = &aStatus;
   181 	iFinished = ETrue;
   182 	User::RequestComplete(status, KErrNone);
   183 	}
   184 
   185 void CSyntaxTest::ReadAndSyntaxCheckL(const TDesC &aFilename)
   186 	{
   187 	HBufC8* buf = Input::ReadFileLC(aFilename, iPath, iFs);
   188 	CX509Certificate* cert = CX509Certificate::NewLC(buf->Des());
   189 	iWriter->WriteCert(*cert);
   190 	
   191 	TestInternalizeExternalizeL(cert);
   192 	
   193 	CleanupStack::PopAndDestroy(2, buf);
   194 	};
   195 
   196 //////////////////////////////////////////////////////////////////////////////////
   197 //	Test fix for INC023303: CX509Certificate externalize - internalize don't work 
   198 //////////////////////////////////////////////////////////////////////////////////
   199 void CSyntaxTest::TestInternalizeExternalizeL(CX509Certificate* aCert)
   200 	{
   201 	RFileWriteStream newwriter;
   202 	newwriter.PushL();
   203 	User::LeaveIfError(newwriter.Replace(iFs,_L("x509stream"),EFileStream));
   204 	
   205 	aCert->ExternalizeL(newwriter);
   206 	newwriter.CommitL();
   207 	CleanupStack::PopAndDestroy(1);//newwriter
   208 	
   209 	RFileReadStream newreader;
   210 	newreader.PushL();
   211 	newreader.Open(iFs,_L("x509stream"),EFileStream);
   212 	CX509Certificate* readCert=CX509Certificate::NewLC(newreader); //Use the stream to create new cert
   213 
   214 	if (!readCert->IsEqualL(*aCert))
   215 		User::Leave(KErrGeneral);
   216 
   217 //	iFs.Delete(_L("x509stream"));
   218 	CleanupStack::PopAndDestroy(2);	//	readCert, newreader
   219 	}
   220 
   221 //////////////////////////////////////////////////////////////////////////////////
   222 
   223 void CSyntaxTest::PerformAction(TRequestStatus& aStatus)
   224 	{
   225 	TBuf<256> filename = (*iDirList)[nFileNumber].iName;
   226 
   227 	// dont try and do the results.txt file
   228 	if(filename.CompareF(KResultsFile)!=0)
   229 		{
   230 		TRAPD(error, ReadAndSyntaxCheckL(filename));
   231 
   232 		if(error == KErrNoMemory)
   233 			User::Leave(error);
   234 		if(!CheckResult(filename, error))
   235 			iResult = EFalse;
   236 		}
   237 
   238 	nFileNumber++;
   239 	if(nFileNumber == iDirList->Count())
   240 		{
   241 		iActionState = EPostrequisite;
   242 		};
   243 	TRequestStatus* status = &aStatus;
   244 	User::RequestComplete(status, KErrNone);
   245 	}
   246 
   247 void CSyntaxTest::DoReportAction()
   248 	{
   249 	iConsole.Printf(_L("\n"));
   250 	}
   251 
   252 void CSyntaxTest::DoCheckResult(TInt /*aError*/)
   253 	{
   254 	}
   255 
   256 
   257 TBool CSyntaxTest::CheckResult(const TDesC &aFilename, const TInt &aError)
   258 	{
   259 	CSyntaxResult *syntaxResult;
   260 	TPtrC filename;
   261 
   262 	iConsole.Printf(aFilename);
   263 	iOut.writeString(aFilename);
   264 
   265 	for(TInt element = 0; element < iExpectedResults->Count(); element++)
   266 		{
   267 		syntaxResult = (*iExpectedResults)[element];
   268 	
   269 		syntaxResult->GetFilename(filename);
   270 		if(filename.CompareF(aFilename)==0)
   271 			{
   272 			
   273 			if(syntaxResult->Result() != aError)
   274 				{	
   275 				iConsole.Printf(_L(" FAILED. Expecting "));
   276 				iOut.writeString(_L(" FAILED. Expecting "));
   277 				PrintError(syntaxResult->Result());
   278 				iConsole.Printf(_L("  Recieved "));
   279 				iOut.writeString(_L(" Recieved "));
   280 				PrintError(aError);
   281 				iConsole.Printf(_L("\n"));
   282 				iOut.writeNewLine();
   283 				return(EFalse);
   284 				}
   285 			else
   286 				{
   287 				iConsole.Printf(_L(" Success\n"));
   288 				iOut.writeString(_L(" Success "));
   289 				iOut.writeNewLine();
   290 				return(ETrue);
   291 				}
   292 			}
   293 		}
   294 
   295 	iConsole.Printf(_L(" FAILED to find expected result\n"));
   296 	iOut.writeString(_L(" FAILED to find expected result"));
   297 	iOut.writeNewLine();
   298 	return(EFalse);
   299 	};
   300 
   301 void CSyntaxTest::PrintError(const TInt &aError)
   302 	{
   303 	switch(aError) 
   304 		{
   305 		case KErrArgument:
   306 			{
   307 			iConsole.Printf(_L(" Wrongly encoded"));
   308 			iOut.writeString(_L(" Wrongly encoded"));
   309 			break;
   310 			}
   311 		case KErrNotSupported:
   312 			{
   313 			iConsole.Printf(_L(" Not Supported"));
   314 			iOut.writeString(_L(" Not Supported"));
   315 			break;
   316 			}
   317 		case KErrNone:
   318 			{
   319 			iConsole.Printf(_L(" None "));
   320 			iOut.writeString(_L(" None "));
   321 			break;
   322 			}
   323 		default:
   324 			{
   325 			iConsole.Printf(_L(" UNKNOWN "));
   326 			iOut.writeString(_L(" UNKNOWN "));
   327 			}
   328 		};
   329 	}
   330 
   331 void CSyntaxTest::Reset()
   332     {
   333     // nothing to do
   334     }