First public contribution.
2 * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
19 #include "SyntaxTest.h"
23 TInt CSyntaxTest::nInstances = 0;
24 _LIT(KSyntaxLogFile, "X509SyntaxLog.txt");
25 _LIT(KPathStart, "<path>");
27 //////////////////////////////////////////////////////////////////////
28 // Construction/Destruction
29 //////////////////////////////////////////////////////////////////////
31 CTestAction* CSyntaxTest::NewL(RFs& aFs, CConsoleBase& aConsole,
32 Output& aOut, const TTestActionSpec& aTestActionSpec)
34 CTestAction* self = CSyntaxTest::NewLC(aFs, aConsole, aOut, aTestActionSpec);
35 CleanupStack::Pop(self);
39 CTestAction* CSyntaxTest::NewLC(RFs& aFs, CConsoleBase& aConsole,
40 Output& aOut, const TTestActionSpec& aTestActionSpec)
42 CSyntaxTest* self = new(ELeave) CSyntaxTest(aFs, aConsole, aOut);
43 CleanupStack::PushL(self);
44 self->ConstructL(aTestActionSpec);
48 CSyntaxTest::CSyntaxTest(RFs& aFs,
49 CConsoleBase& aConsole,
51 : CTestAction(aConsole, aOut), iFs(aFs)
56 CSyntaxTest::~CSyntaxTest(void)
62 iExpectedResults->ResetAndDestroy();
63 delete iExpectedResults;
66 void CSyntaxTest::ConstructL(const TTestActionSpec& aTestActionSpec)
68 CTestAction::ConstructL(aTestActionSpec);
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);
78 CleanupStack::PopAndDestroy();
79 User::LeaveIfError(iLogFile.Replace(iFs,KSyntaxLogFile,EFileWrite));
80 iSyntaxOut = new(ELeave) FileOutput(iLogFile);
81 iWriter = new(ELeave) CertWriter(iSyntaxOut);
86 SetScriptError(ESyntax, _L("Only one syntax test can be run in each script"));
91 TBool CSyntaxTest::LoadResultsL()
98 fullPath.Append(iPath);
99 fullPath.Append(KResultsFile);
101 err = resultsFile.Open(iFs, fullPath, EFileRead);
104 iConsole.Printf(_L("Error opening results file : "));
105 iConsole.Printf(fullPath);
106 iConsole.Printf(_L("\n"));
110 CleanupClosePushL(resultsFile);
111 resultsFile.Size(fileSize);
112 CleanupStack::PopAndDestroy(); // resultsFile
114 fileInfo = HBufC8::NewLC(fileSize);
115 TPtr8 fileInfoPtr(fileInfo->Des());
116 fileInfoPtr.SetLength(fileSize);
118 RFileReadStream fileStream;
119 User::LeaveIfError(fileStream.Open(iFs, fullPath, EFileStream));
120 CleanupClosePushL(fileStream);
121 fileStream.ReadL(fileInfoPtr, fileSize);
123 TLex8 theLex(fileInfoPtr);
124 CSyntaxResult *syntaxResult = NULL;
128 syntaxResult = CSyntaxResult::NewLC();
129 syntaxResult->SetFilename(theLex.NextToken());
130 syntaxResult->SetResult(theLex.NextToken());
131 iExpectedResults->Append(syntaxResult);
132 CleanupStack::Pop(); // syntaxResult
134 while(!theLex.Eos());
136 CleanupStack::PopAndDestroy(2); // fileinfo & fileStream
141 void CSyntaxTest::DoPerformPrerequisite(TRequestStatus& aStatus)
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);
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);
157 TRequestStatus* status = &aStatus;
158 User::RequestComplete(status, KErrNone);
159 SetScriptError(EFileNotFound, searchPathPtr);
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"));
170 iActionState = EAction;
171 TRequestStatus* status = &aStatus;
172 User::RequestComplete(status, KErrNone);
175 CleanupStack::PopAndDestroy(); // searchPath
178 void CSyntaxTest::DoPerformPostrequisite(TRequestStatus& aStatus)
180 TRequestStatus* status = &aStatus;
182 User::RequestComplete(status, KErrNone);
185 void CSyntaxTest::ReadAndSyntaxCheckL(const TDesC &aFilename)
187 HBufC8* buf = Input::ReadFileLC(aFilename, iPath, iFs);
188 CX509Certificate* cert = CX509Certificate::NewLC(buf->Des());
189 iWriter->WriteCert(*cert);
191 TestInternalizeExternalizeL(cert);
193 CleanupStack::PopAndDestroy(2, buf);
196 //////////////////////////////////////////////////////////////////////////////////
197 // Test fix for INC023303: CX509Certificate externalize - internalize don't work
198 //////////////////////////////////////////////////////////////////////////////////
199 void CSyntaxTest::TestInternalizeExternalizeL(CX509Certificate* aCert)
201 RFileWriteStream newwriter;
203 User::LeaveIfError(newwriter.Replace(iFs,_L("x509stream"),EFileStream));
205 aCert->ExternalizeL(newwriter);
207 CleanupStack::PopAndDestroy(1);//newwriter
209 RFileReadStream newreader;
211 newreader.Open(iFs,_L("x509stream"),EFileStream);
212 CX509Certificate* readCert=CX509Certificate::NewLC(newreader); //Use the stream to create new cert
214 if (!readCert->IsEqualL(*aCert))
215 User::Leave(KErrGeneral);
217 // iFs.Delete(_L("x509stream"));
218 CleanupStack::PopAndDestroy(2); // readCert, newreader
221 //////////////////////////////////////////////////////////////////////////////////
223 void CSyntaxTest::PerformAction(TRequestStatus& aStatus)
225 TBuf<256> filename = (*iDirList)[nFileNumber].iName;
227 // dont try and do the results.txt file
228 if(filename.CompareF(KResultsFile)!=0)
230 TRAPD(error, ReadAndSyntaxCheckL(filename));
232 if(error == KErrNoMemory)
234 if(!CheckResult(filename, error))
239 if(nFileNumber == iDirList->Count())
241 iActionState = EPostrequisite;
243 TRequestStatus* status = &aStatus;
244 User::RequestComplete(status, KErrNone);
247 void CSyntaxTest::DoReportAction()
249 iConsole.Printf(_L("\n"));
252 void CSyntaxTest::DoCheckResult(TInt /*aError*/)
257 TBool CSyntaxTest::CheckResult(const TDesC &aFilename, const TInt &aError)
259 CSyntaxResult *syntaxResult;
262 iConsole.Printf(aFilename);
263 iOut.writeString(aFilename);
265 for(TInt element = 0; element < iExpectedResults->Count(); element++)
267 syntaxResult = (*iExpectedResults)[element];
269 syntaxResult->GetFilename(filename);
270 if(filename.CompareF(aFilename)==0)
273 if(syntaxResult->Result() != aError)
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 "));
281 iConsole.Printf(_L("\n"));
287 iConsole.Printf(_L(" Success\n"));
288 iOut.writeString(_L(" Success "));
295 iConsole.Printf(_L(" FAILED to find expected result\n"));
296 iOut.writeString(_L(" FAILED to find expected result"));
301 void CSyntaxTest::PrintError(const TInt &aError)
307 iConsole.Printf(_L(" Wrongly encoded"));
308 iOut.writeString(_L(" Wrongly encoded"));
311 case KErrNotSupported:
313 iConsole.Printf(_L(" Not Supported"));
314 iOut.writeString(_L(" Not Supported"));
319 iConsole.Printf(_L(" None "));
320 iOut.writeString(_L(" None "));
325 iConsole.Printf(_L(" UNKNOWN "));
326 iOut.writeString(_L(" UNKNOWN "));
331 void CSyntaxTest::Reset()