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