os/security/cryptoservices/certificateandkeymgmt/tx509/SyntaxTest.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/tx509/SyntaxTest.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,334 @@
     1.4 +/*
     1.5 +* Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +#include "SyntaxTest.h"
    1.23 +#include "wtlscert.h"
    1.24 +#include "t_input.h"
    1.25 +
    1.26 +TInt CSyntaxTest::nInstances = 0;
    1.27 +_LIT(KSyntaxLogFile, "X509SyntaxLog.txt");
    1.28 +_LIT(KPathStart, "<path>");
    1.29 +
    1.30 +//////////////////////////////////////////////////////////////////////
    1.31 +// Construction/Destruction
    1.32 +//////////////////////////////////////////////////////////////////////
    1.33 +
    1.34 +CTestAction* CSyntaxTest::NewL(RFs& aFs, CConsoleBase& aConsole, 
    1.35 +		Output& aOut, const TTestActionSpec& aTestActionSpec)
    1.36 +	{
    1.37 +	CTestAction* self = CSyntaxTest::NewLC(aFs, aConsole, aOut, aTestActionSpec);
    1.38 +	CleanupStack::Pop(self);
    1.39 +	return self;
    1.40 +	}
    1.41 +
    1.42 +CTestAction* CSyntaxTest::NewLC(RFs& aFs, CConsoleBase& aConsole, 
    1.43 +		Output& aOut, const TTestActionSpec& aTestActionSpec)
    1.44 +	{
    1.45 +	CSyntaxTest* self = new(ELeave) CSyntaxTest(aFs, aConsole, aOut);
    1.46 +	CleanupStack::PushL(self);
    1.47 +	self->ConstructL(aTestActionSpec);
    1.48 +	return self;
    1.49 +	}
    1.50 +
    1.51 +CSyntaxTest::CSyntaxTest(RFs& aFs, 
    1.52 +								 CConsoleBase& aConsole,
    1.53 +								 Output& aOut)
    1.54 +: CTestAction(aConsole, aOut), iFs(aFs)
    1.55 +	{
    1.56 +	nFileNumber = 0;
    1.57 +	}
    1.58 +
    1.59 +CSyntaxTest::~CSyntaxTest(void)
    1.60 +	{
    1.61 +	delete iDirList;
    1.62 +	delete iSyntaxOut;
    1.63 +	iLogFile.Close();
    1.64 +	delete iWriter;
    1.65 +	iExpectedResults->ResetAndDestroy();
    1.66 +	delete iExpectedResults;
    1.67 +	};
    1.68 +
    1.69 +void CSyntaxTest::ConstructL(const TTestActionSpec& aTestActionSpec)
    1.70 +	{
    1.71 +	CTestAction::ConstructL(aTestActionSpec);
    1.72 +
    1.73 +	if(nInstances==0)
    1.74 +		{
    1.75 +		nInstances++;
    1.76 +		HBufC* body = HBufC::NewLC(aTestActionSpec.iActionBody.Length());
    1.77 +		iExpectedResults = new (ELeave)RPointerArray<CSyntaxResult>;
    1.78 +		body->Des().Copy(aTestActionSpec.iActionBody);
    1.79 +		TPtrC chainBuf = Input::ParseElement(*body, KPathStart);
    1.80 +		iPath.Copy(chainBuf);
    1.81 +		CleanupStack::PopAndDestroy();
    1.82 +		User::LeaveIfError(iLogFile.Replace(iFs,KSyntaxLogFile,EFileWrite));
    1.83 +		iSyntaxOut = new(ELeave) FileOutput(iLogFile);
    1.84 +		iWriter = new(ELeave) CertWriter(iSyntaxOut);
    1.85 +		LoadResultsL();
    1.86 +		}
    1.87 +	else
    1.88 +		{
    1.89 +		SetScriptError(ESyntax, _L("Only one syntax test can be run in each script"));
    1.90 +		iFinished = ETrue;		
    1.91 +		}
    1.92 +	}
    1.93 +
    1.94 +TBool CSyntaxTest::LoadResultsL()
    1.95 +	{
    1.96 +	RFile resultsFile;
    1.97 +	TFileName fullPath;
    1.98 +	TInt err, fileSize;
    1.99 +	HBufC8 *fileInfo;
   1.100 +
   1.101 +	fullPath.Append(iPath);
   1.102 +	fullPath.Append(KResultsFile);
   1.103 +
   1.104 +	err = resultsFile.Open(iFs, fullPath, EFileRead);
   1.105 +	if (err != KErrNone)
   1.106 +		{
   1.107 +		iConsole.Printf(_L("Error opening results file : "));
   1.108 +		iConsole.Printf(fullPath);
   1.109 +		iConsole.Printf(_L("\n"));
   1.110 +		return(EFalse);
   1.111 +		}
   1.112 +
   1.113 +	CleanupClosePushL(resultsFile);
   1.114 +	resultsFile.Size(fileSize);
   1.115 +	CleanupStack::PopAndDestroy(); // resultsFile
   1.116 +	
   1.117 +	fileInfo = HBufC8::NewLC(fileSize);
   1.118 +	TPtr8 fileInfoPtr(fileInfo->Des());
   1.119 +	fileInfoPtr.SetLength(fileSize);
   1.120 +
   1.121 +	RFileReadStream fileStream;
   1.122 +	User::LeaveIfError(fileStream.Open(iFs, fullPath, EFileStream));
   1.123 +	CleanupClosePushL(fileStream);
   1.124 +	fileStream.ReadL(fileInfoPtr, fileSize);
   1.125 +
   1.126 +	TLex8 theLex(fileInfoPtr);
   1.127 +	CSyntaxResult *syntaxResult = NULL;
   1.128 +	
   1.129 +	do
   1.130 +		{
   1.131 +		syntaxResult = CSyntaxResult::NewLC();
   1.132 +		syntaxResult->SetFilename(theLex.NextToken());
   1.133 +		syntaxResult->SetResult(theLex.NextToken());
   1.134 +		iExpectedResults->Append(syntaxResult);
   1.135 +		CleanupStack::Pop(); // syntaxResult
   1.136 +		}
   1.137 +	while(!theLex.Eos());
   1.138 +
   1.139 +	CleanupStack::PopAndDestroy(2);  // fileinfo & fileStream
   1.140 +	return(ETrue);
   1.141 +	};
   1.142 +
   1.143 +
   1.144 +void CSyntaxTest::DoPerformPrerequisite(TRequestStatus& aStatus)
   1.145 +	{
   1.146 +	HBufC *searchPath = HBufC::NewLC(iPath.Size() + 1);
   1.147 +	TPtr searchPathPtr(searchPath->Des());
   1.148 +	searchPathPtr.Copy(iPath);
   1.149 +	searchPathPtr.Append(_L("*"));
   1.150 +    TInt err = iFs.GetDir(searchPathPtr, KEntryAttNormal, ESortByName, iDirList);
   1.151 +	if (err != KErrNone)
   1.152 +		{
   1.153 +		iConsole.Printf(_L("Error getting directory "));
   1.154 +		iConsole.Printf(searchPathPtr);
   1.155 +		iConsole.Printf(_L("\n"));
   1.156 +		iOut.writeString(_L("Error getting directory "));
   1.157 +		iOut.writeString(searchPathPtr);
   1.158 +		iOut.writeNewLine();
   1.159 +		iFinished = ETrue;
   1.160 +		TRequestStatus* status = &aStatus;
   1.161 +		User::RequestComplete(status, KErrNone);
   1.162 +		SetScriptError(EFileNotFound, searchPathPtr);
   1.163 +		}
   1.164 +	else
   1.165 +		{
   1.166 +		iConsole.Printf(_L("Please view "));
   1.167 +		iConsole.Printf(KSyntaxLogFile);
   1.168 +		iConsole.Printf(_L(" for results\n"));
   1.169 +		iOut.writeString(_L("Please view "));
   1.170 +		iOut.writeString(KSyntaxLogFile);
   1.171 +		iOut.writeString(_L(" for results"));
   1.172 +		iOut.writeNewLine();
   1.173 +		iActionState = EAction;
   1.174 +		TRequestStatus* status = &aStatus;
   1.175 +		User::RequestComplete(status, KErrNone);
   1.176 +		iResult = ETrue;
   1.177 +		}
   1.178 +	CleanupStack::PopAndDestroy(); // searchPath
   1.179 +	}
   1.180 +
   1.181 +void CSyntaxTest::DoPerformPostrequisite(TRequestStatus& aStatus)
   1.182 +	{
   1.183 +	TRequestStatus* status = &aStatus;
   1.184 +	iFinished = ETrue;
   1.185 +	User::RequestComplete(status, KErrNone);
   1.186 +	}
   1.187 +
   1.188 +void CSyntaxTest::ReadAndSyntaxCheckL(const TDesC &aFilename)
   1.189 +	{
   1.190 +	HBufC8* buf = Input::ReadFileLC(aFilename, iPath, iFs);
   1.191 +	CX509Certificate* cert = CX509Certificate::NewLC(buf->Des());
   1.192 +	iWriter->WriteCert(*cert);
   1.193 +	
   1.194 +	TestInternalizeExternalizeL(cert);
   1.195 +	
   1.196 +	CleanupStack::PopAndDestroy(2, buf);
   1.197 +	};
   1.198 +
   1.199 +//////////////////////////////////////////////////////////////////////////////////
   1.200 +//	Test fix for INC023303: CX509Certificate externalize - internalize don't work 
   1.201 +//////////////////////////////////////////////////////////////////////////////////
   1.202 +void CSyntaxTest::TestInternalizeExternalizeL(CX509Certificate* aCert)
   1.203 +	{
   1.204 +	RFileWriteStream newwriter;
   1.205 +	newwriter.PushL();
   1.206 +	User::LeaveIfError(newwriter.Replace(iFs,_L("x509stream"),EFileStream));
   1.207 +	
   1.208 +	aCert->ExternalizeL(newwriter);
   1.209 +	newwriter.CommitL();
   1.210 +	CleanupStack::PopAndDestroy(1);//newwriter
   1.211 +	
   1.212 +	RFileReadStream newreader;
   1.213 +	newreader.PushL();
   1.214 +	newreader.Open(iFs,_L("x509stream"),EFileStream);
   1.215 +	CX509Certificate* readCert=CX509Certificate::NewLC(newreader); //Use the stream to create new cert
   1.216 +
   1.217 +	if (!readCert->IsEqualL(*aCert))
   1.218 +		User::Leave(KErrGeneral);
   1.219 +
   1.220 +//	iFs.Delete(_L("x509stream"));
   1.221 +	CleanupStack::PopAndDestroy(2);	//	readCert, newreader
   1.222 +	}
   1.223 +
   1.224 +//////////////////////////////////////////////////////////////////////////////////
   1.225 +
   1.226 +void CSyntaxTest::PerformAction(TRequestStatus& aStatus)
   1.227 +	{
   1.228 +	TBuf<256> filename = (*iDirList)[nFileNumber].iName;
   1.229 +
   1.230 +	// dont try and do the results.txt file
   1.231 +	if(filename.CompareF(KResultsFile)!=0)
   1.232 +		{
   1.233 +		TRAPD(error, ReadAndSyntaxCheckL(filename));
   1.234 +
   1.235 +		if(error == KErrNoMemory)
   1.236 +			User::Leave(error);
   1.237 +		if(!CheckResult(filename, error))
   1.238 +			iResult = EFalse;
   1.239 +		}
   1.240 +
   1.241 +	nFileNumber++;
   1.242 +	if(nFileNumber == iDirList->Count())
   1.243 +		{
   1.244 +		iActionState = EPostrequisite;
   1.245 +		};
   1.246 +	TRequestStatus* status = &aStatus;
   1.247 +	User::RequestComplete(status, KErrNone);
   1.248 +	}
   1.249 +
   1.250 +void CSyntaxTest::DoReportAction()
   1.251 +	{
   1.252 +	iConsole.Printf(_L("\n"));
   1.253 +	}
   1.254 +
   1.255 +void CSyntaxTest::DoCheckResult(TInt /*aError*/)
   1.256 +	{
   1.257 +	}
   1.258 +
   1.259 +
   1.260 +TBool CSyntaxTest::CheckResult(const TDesC &aFilename, const TInt &aError)
   1.261 +	{
   1.262 +	CSyntaxResult *syntaxResult;
   1.263 +	TPtrC filename;
   1.264 +
   1.265 +	iConsole.Printf(aFilename);
   1.266 +	iOut.writeString(aFilename);
   1.267 +
   1.268 +	for(TInt element = 0; element < iExpectedResults->Count(); element++)
   1.269 +		{
   1.270 +		syntaxResult = (*iExpectedResults)[element];
   1.271 +	
   1.272 +		syntaxResult->GetFilename(filename);
   1.273 +		if(filename.CompareF(aFilename)==0)
   1.274 +			{
   1.275 +			
   1.276 +			if(syntaxResult->Result() != aError)
   1.277 +				{	
   1.278 +				iConsole.Printf(_L(" FAILED. Expecting "));
   1.279 +				iOut.writeString(_L(" FAILED. Expecting "));
   1.280 +				PrintError(syntaxResult->Result());
   1.281 +				iConsole.Printf(_L("  Recieved "));
   1.282 +				iOut.writeString(_L(" Recieved "));
   1.283 +				PrintError(aError);
   1.284 +				iConsole.Printf(_L("\n"));
   1.285 +				iOut.writeNewLine();
   1.286 +				return(EFalse);
   1.287 +				}
   1.288 +			else
   1.289 +				{
   1.290 +				iConsole.Printf(_L(" Success\n"));
   1.291 +				iOut.writeString(_L(" Success "));
   1.292 +				iOut.writeNewLine();
   1.293 +				return(ETrue);
   1.294 +				}
   1.295 +			}
   1.296 +		}
   1.297 +
   1.298 +	iConsole.Printf(_L(" FAILED to find expected result\n"));
   1.299 +	iOut.writeString(_L(" FAILED to find expected result"));
   1.300 +	iOut.writeNewLine();
   1.301 +	return(EFalse);
   1.302 +	};
   1.303 +
   1.304 +void CSyntaxTest::PrintError(const TInt &aError)
   1.305 +	{
   1.306 +	switch(aError) 
   1.307 +		{
   1.308 +		case KErrArgument:
   1.309 +			{
   1.310 +			iConsole.Printf(_L(" Wrongly encoded"));
   1.311 +			iOut.writeString(_L(" Wrongly encoded"));
   1.312 +			break;
   1.313 +			}
   1.314 +		case KErrNotSupported:
   1.315 +			{
   1.316 +			iConsole.Printf(_L(" Not Supported"));
   1.317 +			iOut.writeString(_L(" Not Supported"));
   1.318 +			break;
   1.319 +			}
   1.320 +		case KErrNone:
   1.321 +			{
   1.322 +			iConsole.Printf(_L(" None "));
   1.323 +			iOut.writeString(_L(" None "));
   1.324 +			break;
   1.325 +			}
   1.326 +		default:
   1.327 +			{
   1.328 +			iConsole.Printf(_L(" UNKNOWN "));
   1.329 +			iOut.writeString(_L(" UNKNOWN "));
   1.330 +			}
   1.331 +		};
   1.332 +	}
   1.333 +
   1.334 +void CSyntaxTest::Reset()
   1.335 +    {
   1.336 +    // nothing to do
   1.337 +    }