First public contribution.
2 * Copyright (c) 2001-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.
15 * Implementation for testing generalized time object encoding/decoding
20 #include "testgeneralizedtime.h"
21 #include "tasn1normaltest.h"
29 CTestGeneralizedTime* CTestGeneralizedTime::NewL(CASN1NormalTest &aASN1Action)
31 CTestGeneralizedTime* test = new (ELeave) CTestGeneralizedTime(aASN1Action);
35 CTestGeneralizedTime::CTestGeneralizedTime(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action)
40 void CTestGeneralizedTime::GetName(TDes& aBuf)
42 aBuf.Copy(_L("Test Generalized Time"));
46 void CTestGeneralizedTime::FillParameterArray(void)
48 iParameters->Append(CTestParameter::EString);
49 iParameters->Append(CTestParameter::EInt);
50 iParameters->Append(CTestParameter::EInt);
51 iParameters->Append(CTestParameter::EInt);
52 iParameters->Append(CTestParameter::EInt);
53 iParameters->Append(CTestParameter::EInt);
54 iParameters->Append(CTestParameter::EInt);
55 iParameters->Append(CTestParameter::EString);
56 iParameters->Append(CTestParameter::EString);
59 TBool CTestGeneralizedTime::ValidateOutcome(CConsoleBase& aConsole, const TTime& originalTime, const TTime& decodedTime, const TInt &aTestNumber,
60 const TInt &aTotalTests)
62 if (originalTime != decodedTime)
64 aConsole.Write(_L("ERROR!\nEncoded time = "));
65 OutputTime(aConsole, originalTime);
66 aConsole.Write(_L("Decoded time = "));
67 OutputTime(aConsole, decodedTime);
68 aConsole.Write(_L("\n"));
69 iASN1Action.ReportProgressL(KErrASN1EncodingError, aTestNumber, aTotalTests);
74 iASN1Action.ReportProgressL(KErrNone, aTestNumber, aTotalTests);
79 TBool CTestGeneralizedTime::PerformTestWithEncoderL(CConsoleBase& aConsole, const TTime &aTest, const TInt &aTestNumber,
80 const TInt &aTotalTests)
84 TTime decodedTime, originalTime;
87 CASN1EncGeneralizedTime* encoder = CASN1EncGeneralizedTime::NewLC(aTest);
90 TInt totalLength = encoder->LengthDER();
91 HBufC8* buf = HBufC8::NewMaxLC(totalLength);
92 TPtr8 tBuf = buf->Des();
94 // Write into the buffer
96 encoder->WriteDERL(tBuf, writeLength);
98 // Read it out again + check lengths
99 TASN1DecGeneralizedTime decoder;
101 decodedTime = decoder.DecodeDERL(tBuf, readLength);
103 CleanupStack::PopAndDestroy(2); // buf, encoder
105 if(writeLength != STATIC_CAST(TUint, readLength))
108 return ValidateOutcome(aConsole, originalTime, decodedTime, aTestNumber, aTotalTests);
111 TBool CTestGeneralizedTime::PerformTestWithoutEncoderL(CConsoleBase& aConsole, const TTime &aTest, const TInt &aTestNumber,
112 const TInt &aTotalTests, const TDesC& fraction, const TDesC& offset)
115 TTime decodedTime, originalTime = aTest;
118 //we construct the encoded time manually and pass it to decoder,
120 _LIT(KTimeFormat, "%F%Y%M%D%H%T%S");
122 aTest.FormatL(timeString, KTimeFormat);
123 TInt fracSec = 0, i = 0;
124 if(fraction != KNullDesC)
126 timeString.Append('.');
127 timeString.Append(fraction);
128 TInt multiplier = 100000;
130 // parse fraction of second
131 while(i < fraction.Length())
134 fracSec += (fraction[i++]-'0') * multiplier;
138 TTimeIntervalMicroSeconds intervalMs = fracSec;
139 originalTime += intervalMs;
141 if(offset != KNullDesC)
143 timeString.Append(offset);
144 TTimeIntervalHours intervalHH = (offset[1]-'0') * 10 + (offset[2]-'0');
145 TTimeIntervalMinutes intervalMM = (offset[3]-'0') * 10 + (offset[4]-'0');
149 originalTime -= intervalHH;
150 originalTime -= intervalMM;
153 originalTime += intervalHH;
154 originalTime += intervalMM;
163 TBuf8<32> timeString8bit;
164 timeString8bit.Copy(timeString);
166 //create a buf for storage of encoded time
167 TInt totalLength = timeString8bit.Length() + 2;
168 HBufC8* encodedTime = HBufC8::NewLC(totalLength);
169 TPtr8 encodedTimePtr = encodedTime->Des();
171 //construct an encoded time manually
172 encodedTimePtr.Append(STATIC_CAST(TUint8, 0) | 0x1b); //add tag
173 encodedTimePtr.Append(STATIC_CAST(TUint8, timeString8bit.Length())); //add length
174 encodedTimePtr.Append(timeString8bit); // add content
176 //Decode and read it out for later test on decoder
177 TASN1DecGeneralizedTime decoder;
179 TASN1DecGeneric gen(encodedTimePtr);
181 decodedTime = decoder.DecodeDERL(gen);
183 CleanupStack::PopAndDestroy(encodedTime); // buf
186 return ValidateOutcome(aConsole, originalTime, decodedTime, aTestNumber, aTotalTests);
191 // Is there a built-in function to do this for us?
192 void CTestGeneralizedTime::OutputTime(CConsoleBase& aConsole, const TTime& aTime)
194 TDateTime dt(aTime.DateTime());
195 aConsole.Printf(_L("Y = %d, M = %d, D = %d, H = %d, M = %d, S = %d, mS = %d\n"),
196 dt.Year(), dt.Month(), dt.Day(), dt.Hour(), dt.Minute(), dt.Second(), dt.MicroSecond());
200 TBool CTestGeneralizedTime::PerformTestsL(CConsoleBase& aConsole)
202 TInt nLow = Math::Random();
203 TInt64 nHigh((TInt)Math::Random());
204 TInt64 iSeed((nHigh << 32) + nLow);
206 CTestParameter* test;
207 TInt totalTests, currentTest=0;
208 TInt year, month, day, hour, minute, second;
212 CStringTestParameter *testString;
214 if(!CountTests(totalTests)) return(EFalse);
216 for(TInt pos = 0; pos < iValues->Count(); pos++)
219 test = (*iValues)[pos];
221 if(test->GetType() == CTestParameter::EString)
223 //get the type of generalizedTime we are testing
224 testString = REINTERPRET_CAST(CStringTestParameter*, test);
225 testString->GetValue(typeOfTest);
228 test = (*iValues)[++pos];
229 if(test->GetType() != CTestParameter::EInt)
231 CIntTestParameter *testInt = REINTERPRET_CAST(CIntTestParameter*, test);
232 year = testInt->Value();
235 test = (*iValues)[++pos];
236 if(test->GetType() != CTestParameter::EInt)
238 testInt = REINTERPRET_CAST(CIntTestParameter*, test);
239 month = testInt->Value();
242 test = (*iValues)[++pos];
243 if(test->GetType() != CTestParameter::EInt)
245 testInt = REINTERPRET_CAST(CIntTestParameter*, test);
246 day = testInt->Value();
249 test = (*iValues)[++pos];
250 if(test->GetType() != CTestParameter::EInt)
252 testInt = REINTERPRET_CAST(CIntTestParameter*, test);
253 hour = testInt->Value();
256 test = (*iValues)[++pos];
257 if(test->GetType() != CTestParameter::EInt)
259 testInt = REINTERPRET_CAST(CIntTestParameter*, test);
260 minute = testInt->Value();
263 test = (*iValues)[++pos];
264 if(test->GetType() != CTestParameter::EInt)
266 testInt = REINTERPRET_CAST(CIntTestParameter*, test);
267 second = testInt->Value();
269 TDateTime dateTime(year, TMonth(month - 1), TMonth(day - 1), hour, minute, second, 0);
270 TTime encodedTime = dateTime;
272 if(typeOfTest == KEncoder)
273 //we are testing with encoder as well as decoder
274 //Since encoder only accepts TTime as input, we could only pass
275 //in standard zulu time (asn.1 generalizedTime in
276 //second form without optional fraction of second and offset
277 // hours and minutes)
279 pos += 2; //ignore fraction of second and offset
281 if(!PerformTestWithEncoderL(aConsole, encodedTime, currentTest, totalTests))
285 else if(typeOfTest == KNoEncoder)
286 // Here we test decoding asn.1 generalized Time in any forms other than standard zulu,
287 // with optional fraction of seconds and/or offset hours and minutes
289 // gets fraction of second
291 test = (*iValues)[++pos];
292 testString = REINTERPRET_CAST(CStringTestParameter*, test);
293 testString->GetValue(fracSecond);
296 test = (*iValues)[++pos];
297 testString = REINTERPRET_CAST(CStringTestParameter*, test);
298 testString->GetValue(offset);
300 if(!PerformTestWithoutEncoderL(aConsole, encodedTime, currentTest, totalTests, fracSecond, offset))
305 User::Leave(KErrNotSupported);
308 else if(test->GetType() == CTestParameter::ERandom)
310 CRandomTestParameter *randomInt = REINTERPRET_CAST(CRandomTestParameter*, test);
312 for(TInt test = 0; test <= randomInt->Interations(); test++)
314 TInt nLow = Math::Rand(iSeed);
315 TInt64 nHigh = Math::Rand(iSeed);
316 TInt64 time64((nHigh << 32) + nLow);
317 // Set to random time, but with 4-digit year and microseconds zero
318 TTime encodedTime(time64);
319 TDateTime dateTime(encodedTime.DateTime());
320 dateTime.SetYear(dateTime.Year() % 10000);
321 dateTime.SetMicroSecond(0);
322 encodedTime = dateTime;
323 if(!PerformTestWithEncoderL(aConsole, encodedTime, currentTest, totalTests))
335 iASN1Action.ReportProgressL(KErrNone, totalTests, totalTests);