sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 2001-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 |
* Implementation for testing generalized time object encoding/decoding
|
sl@0
|
16 |
*
|
sl@0
|
17 |
*/
|
sl@0
|
18 |
|
sl@0
|
19 |
|
sl@0
|
20 |
#include "testgeneralizedtime.h"
|
sl@0
|
21 |
#include "tasn1normaltest.h"
|
sl@0
|
22 |
#include <asn1enc.h>
|
sl@0
|
23 |
#include <asn1dec.h>
|
sl@0
|
24 |
|
sl@0
|
25 |
#include <e32cons.h>
|
sl@0
|
26 |
#include <e32math.h>
|
sl@0
|
27 |
|
sl@0
|
28 |
|
sl@0
|
29 |
CTestGeneralizedTime* CTestGeneralizedTime::NewL(CASN1NormalTest &aASN1Action)
|
sl@0
|
30 |
{
|
sl@0
|
31 |
CTestGeneralizedTime* test = new (ELeave) CTestGeneralizedTime(aASN1Action);
|
sl@0
|
32 |
return test;
|
sl@0
|
33 |
}
|
sl@0
|
34 |
|
sl@0
|
35 |
CTestGeneralizedTime::CTestGeneralizedTime(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action)
|
sl@0
|
36 |
{
|
sl@0
|
37 |
};
|
sl@0
|
38 |
|
sl@0
|
39 |
|
sl@0
|
40 |
void CTestGeneralizedTime::GetName(TDes& aBuf)
|
sl@0
|
41 |
{
|
sl@0
|
42 |
aBuf.Copy(_L("Test Generalized Time"));
|
sl@0
|
43 |
}
|
sl@0
|
44 |
|
sl@0
|
45 |
|
sl@0
|
46 |
void CTestGeneralizedTime::FillParameterArray(void)
|
sl@0
|
47 |
{
|
sl@0
|
48 |
iParameters->Append(CTestParameter::EString);
|
sl@0
|
49 |
iParameters->Append(CTestParameter::EInt);
|
sl@0
|
50 |
iParameters->Append(CTestParameter::EInt);
|
sl@0
|
51 |
iParameters->Append(CTestParameter::EInt);
|
sl@0
|
52 |
iParameters->Append(CTestParameter::EInt);
|
sl@0
|
53 |
iParameters->Append(CTestParameter::EInt);
|
sl@0
|
54 |
iParameters->Append(CTestParameter::EInt);
|
sl@0
|
55 |
iParameters->Append(CTestParameter::EString);
|
sl@0
|
56 |
iParameters->Append(CTestParameter::EString);
|
sl@0
|
57 |
}
|
sl@0
|
58 |
|
sl@0
|
59 |
TBool CTestGeneralizedTime::ValidateOutcome(CConsoleBase& aConsole, const TTime& originalTime, const TTime& decodedTime, const TInt &aTestNumber,
|
sl@0
|
60 |
const TInt &aTotalTests)
|
sl@0
|
61 |
{
|
sl@0
|
62 |
if (originalTime != decodedTime)
|
sl@0
|
63 |
{
|
sl@0
|
64 |
aConsole.Write(_L("ERROR!\nEncoded time = "));
|
sl@0
|
65 |
OutputTime(aConsole, originalTime);
|
sl@0
|
66 |
aConsole.Write(_L("Decoded time = "));
|
sl@0
|
67 |
OutputTime(aConsole, decodedTime);
|
sl@0
|
68 |
aConsole.Write(_L("\n"));
|
sl@0
|
69 |
iASN1Action.ReportProgressL(KErrASN1EncodingError, aTestNumber, aTotalTests);
|
sl@0
|
70 |
return(EFalse);
|
sl@0
|
71 |
}
|
sl@0
|
72 |
else
|
sl@0
|
73 |
{
|
sl@0
|
74 |
iASN1Action.ReportProgressL(KErrNone, aTestNumber, aTotalTests);
|
sl@0
|
75 |
return(ETrue);
|
sl@0
|
76 |
}
|
sl@0
|
77 |
}
|
sl@0
|
78 |
|
sl@0
|
79 |
TBool CTestGeneralizedTime::PerformTestWithEncoderL(CConsoleBase& aConsole, const TTime &aTest, const TInt &aTestNumber,
|
sl@0
|
80 |
const TInt &aTotalTests)
|
sl@0
|
81 |
{
|
sl@0
|
82 |
TUint writeLength;
|
sl@0
|
83 |
TInt readLength;
|
sl@0
|
84 |
TTime decodedTime, originalTime;
|
sl@0
|
85 |
|
sl@0
|
86 |
originalTime = aTest;
|
sl@0
|
87 |
CASN1EncGeneralizedTime* encoder = CASN1EncGeneralizedTime::NewLC(aTest);
|
sl@0
|
88 |
|
sl@0
|
89 |
// Prepare a buffer
|
sl@0
|
90 |
TInt totalLength = encoder->LengthDER();
|
sl@0
|
91 |
HBufC8* buf = HBufC8::NewMaxLC(totalLength);
|
sl@0
|
92 |
TPtr8 tBuf = buf->Des();
|
sl@0
|
93 |
|
sl@0
|
94 |
// Write into the buffer
|
sl@0
|
95 |
writeLength = 0;
|
sl@0
|
96 |
encoder->WriteDERL(tBuf, writeLength);
|
sl@0
|
97 |
|
sl@0
|
98 |
// Read it out again + check lengths
|
sl@0
|
99 |
TASN1DecGeneralizedTime decoder;
|
sl@0
|
100 |
readLength = 0;
|
sl@0
|
101 |
decodedTime = decoder.DecodeDERL(tBuf, readLength);
|
sl@0
|
102 |
|
sl@0
|
103 |
CleanupStack::PopAndDestroy(2); // buf, encoder
|
sl@0
|
104 |
|
sl@0
|
105 |
if(writeLength != STATIC_CAST(TUint, readLength))
|
sl@0
|
106 |
return EFalse;
|
sl@0
|
107 |
|
sl@0
|
108 |
return ValidateOutcome(aConsole, originalTime, decodedTime, aTestNumber, aTotalTests);
|
sl@0
|
109 |
}
|
sl@0
|
110 |
|
sl@0
|
111 |
TBool CTestGeneralizedTime::PerformTestWithoutEncoderL(CConsoleBase& aConsole, const TTime &aTest, const TInt &aTestNumber,
|
sl@0
|
112 |
const TInt &aTotalTests, const TDesC& fraction, const TDesC& offset)
|
sl@0
|
113 |
{
|
sl@0
|
114 |
|
sl@0
|
115 |
TTime decodedTime, originalTime = aTest;
|
sl@0
|
116 |
|
sl@0
|
117 |
|
sl@0
|
118 |
//we construct the encoded time manually and pass it to decoder,
|
sl@0
|
119 |
|
sl@0
|
120 |
_LIT(KTimeFormat, "%F%Y%M%D%H%T%S");
|
sl@0
|
121 |
TBuf<32> timeString;
|
sl@0
|
122 |
aTest.FormatL(timeString, KTimeFormat);
|
sl@0
|
123 |
TInt fracSec = 0, i = 0;
|
sl@0
|
124 |
if(fraction != KNullDesC)
|
sl@0
|
125 |
{
|
sl@0
|
126 |
timeString.Append('.');
|
sl@0
|
127 |
timeString.Append(fraction);
|
sl@0
|
128 |
TInt multiplier = 100000;
|
sl@0
|
129 |
|
sl@0
|
130 |
// parse fraction of second
|
sl@0
|
131 |
while(i < fraction.Length())
|
sl@0
|
132 |
{
|
sl@0
|
133 |
|
sl@0
|
134 |
fracSec += (fraction[i++]-'0') * multiplier;
|
sl@0
|
135 |
multiplier /= 10;
|
sl@0
|
136 |
|
sl@0
|
137 |
}
|
sl@0
|
138 |
TTimeIntervalMicroSeconds intervalMs = fracSec;
|
sl@0
|
139 |
originalTime += intervalMs;
|
sl@0
|
140 |
}
|
sl@0
|
141 |
if(offset != KNullDesC)
|
sl@0
|
142 |
{
|
sl@0
|
143 |
timeString.Append(offset);
|
sl@0
|
144 |
TTimeIntervalHours intervalHH = (offset[1]-'0') * 10 + (offset[2]-'0');
|
sl@0
|
145 |
TTimeIntervalMinutes intervalMM = (offset[3]-'0') * 10 + (offset[4]-'0');
|
sl@0
|
146 |
switch(offset[0])
|
sl@0
|
147 |
{
|
sl@0
|
148 |
case '+':
|
sl@0
|
149 |
originalTime -= intervalHH;
|
sl@0
|
150 |
originalTime -= intervalMM;
|
sl@0
|
151 |
break;
|
sl@0
|
152 |
case '-':
|
sl@0
|
153 |
originalTime += intervalHH;
|
sl@0
|
154 |
originalTime += intervalMM;
|
sl@0
|
155 |
break;
|
sl@0
|
156 |
default:
|
sl@0
|
157 |
// Malformed offset
|
sl@0
|
158 |
return EFalse;
|
sl@0
|
159 |
}
|
sl@0
|
160 |
}
|
sl@0
|
161 |
|
sl@0
|
162 |
//convert to 8 bit
|
sl@0
|
163 |
TBuf8<32> timeString8bit;
|
sl@0
|
164 |
timeString8bit.Copy(timeString);
|
sl@0
|
165 |
|
sl@0
|
166 |
//create a buf for storage of encoded time
|
sl@0
|
167 |
TInt totalLength = timeString8bit.Length() + 2;
|
sl@0
|
168 |
HBufC8* encodedTime = HBufC8::NewLC(totalLength);
|
sl@0
|
169 |
TPtr8 encodedTimePtr = encodedTime->Des();
|
sl@0
|
170 |
|
sl@0
|
171 |
//construct an encoded time manually
|
sl@0
|
172 |
encodedTimePtr.Append(STATIC_CAST(TUint8, 0) | 0x1b); //add tag
|
sl@0
|
173 |
encodedTimePtr.Append(STATIC_CAST(TUint8, timeString8bit.Length())); //add length
|
sl@0
|
174 |
encodedTimePtr.Append(timeString8bit); // add content
|
sl@0
|
175 |
|
sl@0
|
176 |
//Decode and read it out for later test on decoder
|
sl@0
|
177 |
TASN1DecGeneralizedTime decoder;
|
sl@0
|
178 |
|
sl@0
|
179 |
TASN1DecGeneric gen(encodedTimePtr);
|
sl@0
|
180 |
gen.InitL();
|
sl@0
|
181 |
decodedTime = decoder.DecodeDERL(gen);
|
sl@0
|
182 |
|
sl@0
|
183 |
CleanupStack::PopAndDestroy(encodedTime); // buf
|
sl@0
|
184 |
|
sl@0
|
185 |
|
sl@0
|
186 |
return ValidateOutcome(aConsole, originalTime, decodedTime, aTestNumber, aTotalTests);
|
sl@0
|
187 |
|
sl@0
|
188 |
}
|
sl@0
|
189 |
|
sl@0
|
190 |
|
sl@0
|
191 |
// Is there a built-in function to do this for us?
|
sl@0
|
192 |
void CTestGeneralizedTime::OutputTime(CConsoleBase& aConsole, const TTime& aTime)
|
sl@0
|
193 |
{
|
sl@0
|
194 |
TDateTime dt(aTime.DateTime());
|
sl@0
|
195 |
aConsole.Printf(_L("Y = %d, M = %d, D = %d, H = %d, M = %d, S = %d, mS = %d\n"),
|
sl@0
|
196 |
dt.Year(), dt.Month(), dt.Day(), dt.Hour(), dt.Minute(), dt.Second(), dt.MicroSecond());
|
sl@0
|
197 |
}
|
sl@0
|
198 |
|
sl@0
|
199 |
|
sl@0
|
200 |
TBool CTestGeneralizedTime::PerformTestsL(CConsoleBase& aConsole)
|
sl@0
|
201 |
{
|
sl@0
|
202 |
TInt nLow = Math::Random();
|
sl@0
|
203 |
TInt64 nHigh((TInt)Math::Random());
|
sl@0
|
204 |
TInt64 iSeed((nHigh << 32) + nLow);
|
sl@0
|
205 |
|
sl@0
|
206 |
CTestParameter* test;
|
sl@0
|
207 |
TInt totalTests, currentTest=0;
|
sl@0
|
208 |
TInt year, month, day, hour, minute, second;
|
sl@0
|
209 |
TBuf<3> fracSecond;
|
sl@0
|
210 |
TBuf<5> offset;
|
sl@0
|
211 |
TBuf<15> typeOfTest;
|
sl@0
|
212 |
CStringTestParameter *testString;
|
sl@0
|
213 |
|
sl@0
|
214 |
if(!CountTests(totalTests)) return(EFalse);
|
sl@0
|
215 |
|
sl@0
|
216 |
for(TInt pos = 0; pos < iValues->Count(); pos++)
|
sl@0
|
217 |
{
|
sl@0
|
218 |
typeOfTest.Zero();
|
sl@0
|
219 |
test = (*iValues)[pos];
|
sl@0
|
220 |
|
sl@0
|
221 |
if(test->GetType() == CTestParameter::EString)
|
sl@0
|
222 |
{
|
sl@0
|
223 |
//get the type of generalizedTime we are testing
|
sl@0
|
224 |
testString = REINTERPRET_CAST(CStringTestParameter*, test);
|
sl@0
|
225 |
testString->GetValue(typeOfTest);
|
sl@0
|
226 |
|
sl@0
|
227 |
// gets year
|
sl@0
|
228 |
test = (*iValues)[++pos];
|
sl@0
|
229 |
if(test->GetType() != CTestParameter::EInt)
|
sl@0
|
230 |
return(EFalse);
|
sl@0
|
231 |
CIntTestParameter *testInt = REINTERPRET_CAST(CIntTestParameter*, test);
|
sl@0
|
232 |
year = testInt->Value();
|
sl@0
|
233 |
|
sl@0
|
234 |
// gets month
|
sl@0
|
235 |
test = (*iValues)[++pos];
|
sl@0
|
236 |
if(test->GetType() != CTestParameter::EInt)
|
sl@0
|
237 |
return(EFalse);
|
sl@0
|
238 |
testInt = REINTERPRET_CAST(CIntTestParameter*, test);
|
sl@0
|
239 |
month = testInt->Value();
|
sl@0
|
240 |
|
sl@0
|
241 |
// gets day
|
sl@0
|
242 |
test = (*iValues)[++pos];
|
sl@0
|
243 |
if(test->GetType() != CTestParameter::EInt)
|
sl@0
|
244 |
return(EFalse);
|
sl@0
|
245 |
testInt = REINTERPRET_CAST(CIntTestParameter*, test);
|
sl@0
|
246 |
day = testInt->Value();
|
sl@0
|
247 |
|
sl@0
|
248 |
// gets hour
|
sl@0
|
249 |
test = (*iValues)[++pos];
|
sl@0
|
250 |
if(test->GetType() != CTestParameter::EInt)
|
sl@0
|
251 |
return(EFalse);
|
sl@0
|
252 |
testInt = REINTERPRET_CAST(CIntTestParameter*, test);
|
sl@0
|
253 |
hour = testInt->Value();
|
sl@0
|
254 |
|
sl@0
|
255 |
// gets minute
|
sl@0
|
256 |
test = (*iValues)[++pos];
|
sl@0
|
257 |
if(test->GetType() != CTestParameter::EInt)
|
sl@0
|
258 |
return(EFalse);
|
sl@0
|
259 |
testInt = REINTERPRET_CAST(CIntTestParameter*, test);
|
sl@0
|
260 |
minute = testInt->Value();
|
sl@0
|
261 |
|
sl@0
|
262 |
// gets second
|
sl@0
|
263 |
test = (*iValues)[++pos];
|
sl@0
|
264 |
if(test->GetType() != CTestParameter::EInt)
|
sl@0
|
265 |
return(EFalse);
|
sl@0
|
266 |
testInt = REINTERPRET_CAST(CIntTestParameter*, test);
|
sl@0
|
267 |
second = testInt->Value();
|
sl@0
|
268 |
|
sl@0
|
269 |
TDateTime dateTime(year, TMonth(month - 1), TMonth(day - 1), hour, minute, second, 0);
|
sl@0
|
270 |
TTime encodedTime = dateTime;
|
sl@0
|
271 |
|
sl@0
|
272 |
if(typeOfTest == KEncoder)
|
sl@0
|
273 |
//we are testing with encoder as well as decoder
|
sl@0
|
274 |
//Since encoder only accepts TTime as input, we could only pass
|
sl@0
|
275 |
//in standard zulu time (asn.1 generalizedTime in
|
sl@0
|
276 |
//second form without optional fraction of second and offset
|
sl@0
|
277 |
// hours and minutes)
|
sl@0
|
278 |
{
|
sl@0
|
279 |
pos += 2; //ignore fraction of second and offset
|
sl@0
|
280 |
|
sl@0
|
281 |
if(!PerformTestWithEncoderL(aConsole, encodedTime, currentTest, totalTests))
|
sl@0
|
282 |
return(EFalse);
|
sl@0
|
283 |
|
sl@0
|
284 |
}
|
sl@0
|
285 |
else if(typeOfTest == KNoEncoder)
|
sl@0
|
286 |
// Here we test decoding asn.1 generalized Time in any forms other than standard zulu,
|
sl@0
|
287 |
// with optional fraction of seconds and/or offset hours and minutes
|
sl@0
|
288 |
{
|
sl@0
|
289 |
// gets fraction of second
|
sl@0
|
290 |
|
sl@0
|
291 |
test = (*iValues)[++pos];
|
sl@0
|
292 |
testString = REINTERPRET_CAST(CStringTestParameter*, test);
|
sl@0
|
293 |
testString->GetValue(fracSecond);
|
sl@0
|
294 |
|
sl@0
|
295 |
// gets offset
|
sl@0
|
296 |
test = (*iValues)[++pos];
|
sl@0
|
297 |
testString = REINTERPRET_CAST(CStringTestParameter*, test);
|
sl@0
|
298 |
testString->GetValue(offset);
|
sl@0
|
299 |
|
sl@0
|
300 |
if(!PerformTestWithoutEncoderL(aConsole, encodedTime, currentTest, totalTests, fracSecond, offset))
|
sl@0
|
301 |
return(EFalse);
|
sl@0
|
302 |
|
sl@0
|
303 |
}
|
sl@0
|
304 |
else
|
sl@0
|
305 |
User::Leave(KErrNotSupported);
|
sl@0
|
306 |
currentTest++;
|
sl@0
|
307 |
}
|
sl@0
|
308 |
else if(test->GetType() == CTestParameter::ERandom)
|
sl@0
|
309 |
{
|
sl@0
|
310 |
CRandomTestParameter *randomInt = REINTERPRET_CAST(CRandomTestParameter*, test);
|
sl@0
|
311 |
|
sl@0
|
312 |
for(TInt test = 0; test <= randomInt->Interations(); test++)
|
sl@0
|
313 |
{
|
sl@0
|
314 |
TInt nLow = Math::Rand(iSeed);
|
sl@0
|
315 |
TInt64 nHigh = Math::Rand(iSeed);
|
sl@0
|
316 |
TInt64 time64((nHigh << 32) + nLow);
|
sl@0
|
317 |
// Set to random time, but with 4-digit year and microseconds zero
|
sl@0
|
318 |
TTime encodedTime(time64);
|
sl@0
|
319 |
TDateTime dateTime(encodedTime.DateTime());
|
sl@0
|
320 |
dateTime.SetYear(dateTime.Year() % 10000);
|
sl@0
|
321 |
dateTime.SetMicroSecond(0);
|
sl@0
|
322 |
encodedTime = dateTime;
|
sl@0
|
323 |
if(!PerformTestWithEncoderL(aConsole, encodedTime, currentTest, totalTests))
|
sl@0
|
324 |
return(EFalse);
|
sl@0
|
325 |
;
|
sl@0
|
326 |
currentTest++;
|
sl@0
|
327 |
}
|
sl@0
|
328 |
}
|
sl@0
|
329 |
else
|
sl@0
|
330 |
return(EFalse);
|
sl@0
|
331 |
};
|
sl@0
|
332 |
|
sl@0
|
333 |
|
sl@0
|
334 |
|
sl@0
|
335 |
iASN1Action.ReportProgressL(KErrNone, totalTests, totalTests);
|
sl@0
|
336 |
return(ETrue);
|
sl@0
|
337 |
};
|
sl@0
|
338 |
|