cryptoservices/certificateandkeymgmt/tasn1/testgeneralizedtime.cpp
changeset 0 2c201484c85f
child 8 35751d3474b7
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /*
       
     2 * Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     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".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 * Implementation for testing generalized time object encoding/decoding
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include "testgeneralizedtime.h"
       
    21 #include "tasn1normaltest.h"
       
    22 #include <asn1enc.h>
       
    23 #include <asn1dec.h>
       
    24 
       
    25 #include <e32cons.h>
       
    26 #include <e32math.h>
       
    27 
       
    28 
       
    29 CTestGeneralizedTime* CTestGeneralizedTime::NewL(CASN1NormalTest &aASN1Action)
       
    30 	{
       
    31 	CTestGeneralizedTime* test = new (ELeave) CTestGeneralizedTime(aASN1Action);
       
    32 	return test;
       
    33 	}
       
    34 
       
    35 CTestGeneralizedTime::CTestGeneralizedTime(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action) 
       
    36 	{
       
    37 	};
       
    38 
       
    39 
       
    40 void CTestGeneralizedTime::GetName(TDes& aBuf)
       
    41 	{
       
    42 	aBuf.Copy(_L("Test Generalized Time"));
       
    43 	}
       
    44 
       
    45 
       
    46 void CTestGeneralizedTime::FillParameterArray(void)
       
    47 	{
       
    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);
       
    57 	}
       
    58 
       
    59 TBool CTestGeneralizedTime::ValidateOutcome(CConsoleBase& aConsole, const TTime& originalTime, const TTime& decodedTime, const TInt &aTestNumber, 
       
    60 		const TInt &aTotalTests)
       
    61 	{
       
    62 	if (originalTime != decodedTime)
       
    63 		{
       
    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);
       
    70 		return(EFalse);
       
    71 		}
       
    72 	else
       
    73 		{
       
    74 		iASN1Action.ReportProgressL(KErrNone, aTestNumber, aTotalTests);
       
    75 		return(ETrue);
       
    76 		}
       
    77 	}
       
    78 
       
    79 TBool CTestGeneralizedTime::PerformTestWithEncoderL(CConsoleBase& aConsole, const TTime &aTest, const TInt &aTestNumber, 
       
    80 		const TInt &aTotalTests)
       
    81 	{
       
    82 		TUint writeLength;
       
    83 		TInt readLength;
       
    84 		TTime decodedTime, originalTime;
       
    85 	
       
    86 		originalTime = aTest;
       
    87 		CASN1EncGeneralizedTime* encoder = CASN1EncGeneralizedTime::NewLC(aTest);
       
    88 			
       
    89 		// Prepare a buffer
       
    90 		TInt totalLength = encoder->LengthDER();
       
    91 		HBufC8* buf = HBufC8::NewMaxLC(totalLength);
       
    92 		TPtr8 tBuf = buf->Des();
       
    93 			
       
    94 		// Write into the buffer
       
    95 		writeLength = 0;
       
    96 		encoder->WriteDERL(tBuf, writeLength);
       
    97 			
       
    98 		// Read it out again + check lengths
       
    99 		TASN1DecGeneralizedTime decoder;
       
   100 		readLength = 0;
       
   101 		decodedTime = decoder.DecodeDERL(tBuf, readLength);
       
   102 
       
   103 		CleanupStack::PopAndDestroy(2); // buf, encoder
       
   104 
       
   105 		if(writeLength != STATIC_CAST(TUint, readLength))
       
   106 			return EFalse;
       
   107 		
       
   108 		return ValidateOutcome(aConsole, originalTime, decodedTime, aTestNumber, aTotalTests);
       
   109 	}
       
   110 
       
   111 TBool CTestGeneralizedTime::PerformTestWithoutEncoderL(CConsoleBase& aConsole, const TTime &aTest, const TInt &aTestNumber,
       
   112 		const TInt &aTotalTests, const TDesC& fraction, const TDesC& offset)
       
   113 	{
       
   114 	
       
   115 	TTime decodedTime, originalTime = aTest;
       
   116 	
       
   117 	
       
   118 	//we construct the encoded time manually and pass it to decoder,
       
   119 			
       
   120 		_LIT(KTimeFormat, "%F%Y%M%D%H%T%S");
       
   121 		TBuf<32> timeString;
       
   122 		aTest.FormatL(timeString, KTimeFormat);
       
   123 		TInt fracSec = 0, i = 0;
       
   124 		if(fraction != KNullDesC)
       
   125 			{
       
   126 				timeString.Append('.');
       
   127 				timeString.Append(fraction);
       
   128 				TInt multiplier = 100000;
       
   129 			
       
   130 				// parse fraction of second
       
   131 				while(i < fraction.Length())
       
   132 					{
       
   133 												
       
   134 					fracSec += (fraction[i++]-'0') * multiplier;
       
   135 					multiplier /= 10;
       
   136 					
       
   137 					}			
       
   138 				TTimeIntervalMicroSeconds intervalMs = fracSec;
       
   139 				originalTime += intervalMs;
       
   140 			}
       
   141 		if(offset != KNullDesC)
       
   142 			{
       
   143 				timeString.Append(offset);
       
   144 				TTimeIntervalHours intervalHH = (offset[1]-'0') * 10 + (offset[2]-'0');
       
   145 				TTimeIntervalMinutes intervalMM = (offset[3]-'0') * 10 + (offset[4]-'0');
       
   146 				switch(offset[0])
       
   147 				{
       
   148 					case '+':
       
   149 						originalTime -= intervalHH;
       
   150 						originalTime -= intervalMM;
       
   151 						break;
       
   152 					case '-':
       
   153 						originalTime += intervalHH;
       
   154 						originalTime += intervalMM;
       
   155 						break;
       
   156 					default:
       
   157 					// Malformed offset
       
   158 						return EFalse;
       
   159 				}
       
   160 			}
       
   161 				
       
   162 		//convert to 8 bit
       
   163 		TBuf8<32> timeString8bit;
       
   164 		timeString8bit.Copy(timeString);
       
   165 		
       
   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();
       
   170 
       
   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
       
   175 			
       
   176 		//Decode and read it out for later test on decoder
       
   177 		TASN1DecGeneralizedTime decoder;
       
   178 	
       
   179 		TASN1DecGeneric gen(encodedTimePtr);
       
   180 		gen.InitL();
       
   181 		decodedTime = decoder.DecodeDERL(gen);
       
   182 
       
   183 		CleanupStack::PopAndDestroy(encodedTime); // buf
       
   184 						
       
   185 		
       
   186 		return ValidateOutcome(aConsole, originalTime, decodedTime, aTestNumber, aTotalTests);
       
   187 
       
   188 	}
       
   189 
       
   190 
       
   191 // Is there a built-in function to do this for us?
       
   192 void CTestGeneralizedTime::OutputTime(CConsoleBase& aConsole, const TTime& aTime)
       
   193 	{
       
   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());
       
   197 	}
       
   198 
       
   199 
       
   200 TBool CTestGeneralizedTime::PerformTestsL(CConsoleBase& aConsole)
       
   201 	{
       
   202 	TInt nLow = Math::Random();
       
   203 	TInt64 nHigh((TInt)Math::Random());
       
   204 	TInt64 iSeed((nHigh << 32) + nLow);
       
   205 
       
   206 	CTestParameter* test;
       
   207 	TInt totalTests, currentTest=0;
       
   208 	TInt year, month, day, hour, minute, second;
       
   209 	TBuf<3> fracSecond;
       
   210 	TBuf<5> offset;
       
   211 	TBuf<15> typeOfTest;
       
   212 	CStringTestParameter *testString;
       
   213 	
       
   214 	if(!CountTests(totalTests)) return(EFalse);
       
   215 
       
   216 	for(TInt pos = 0; pos < iValues->Count(); pos++)
       
   217 		{
       
   218 		typeOfTest.Zero();
       
   219 		test = (*iValues)[pos];
       
   220 				
       
   221 		if(test->GetType() == CTestParameter::EString)
       
   222 			{
       
   223 				//get the type of generalizedTime we are testing
       
   224 				testString = REINTERPRET_CAST(CStringTestParameter*, test);
       
   225 				testString->GetValue(typeOfTest);
       
   226 			
       
   227 				// gets year
       
   228 				test = (*iValues)[++pos];
       
   229 				if(test->GetType() != CTestParameter::EInt)
       
   230 					return(EFalse);
       
   231 				CIntTestParameter *testInt = REINTERPRET_CAST(CIntTestParameter*, test);
       
   232 				year = testInt->Value();
       
   233 
       
   234 				// gets month
       
   235 				test = (*iValues)[++pos];
       
   236 				if(test->GetType() != CTestParameter::EInt) 
       
   237 					return(EFalse);
       
   238 				testInt = REINTERPRET_CAST(CIntTestParameter*, test);
       
   239 				month = testInt->Value();
       
   240 
       
   241 				// gets day
       
   242 				test = (*iValues)[++pos];
       
   243 				if(test->GetType() != CTestParameter::EInt) 
       
   244 					return(EFalse);
       
   245 				testInt = REINTERPRET_CAST(CIntTestParameter*, test);
       
   246 				day = testInt->Value();
       
   247 
       
   248 				// gets hour
       
   249 				test = (*iValues)[++pos];
       
   250 				if(test->GetType() != CTestParameter::EInt) 
       
   251 					return(EFalse);
       
   252 				testInt = REINTERPRET_CAST(CIntTestParameter*, test);
       
   253 				hour = testInt->Value();
       
   254 
       
   255 				// gets minute
       
   256 				test = (*iValues)[++pos];
       
   257 				if(test->GetType() != CTestParameter::EInt) 
       
   258 					return(EFalse);
       
   259 				testInt = REINTERPRET_CAST(CIntTestParameter*, test);
       
   260 				minute = testInt->Value();
       
   261 
       
   262 				// gets second
       
   263 				test = (*iValues)[++pos];
       
   264 				if(test->GetType() != CTestParameter::EInt) 
       
   265 					return(EFalse);
       
   266 				testInt = REINTERPRET_CAST(CIntTestParameter*, test);
       
   267 				second = testInt->Value();
       
   268 
       
   269 				TDateTime dateTime(year, TMonth(month - 1), TMonth(day - 1), hour, minute, second, 0);
       
   270 				TTime encodedTime = dateTime;
       
   271 								
       
   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)	
       
   278 					{
       
   279 						pos += 2; //ignore fraction of second and offset
       
   280 						
       
   281 						if(!PerformTestWithEncoderL(aConsole, encodedTime, currentTest, totalTests))
       
   282 							return(EFalse);
       
   283 							
       
   284 					}
       
   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
       
   288 					{
       
   289 					// gets fraction of second
       
   290 					
       
   291 						test = (*iValues)[++pos];
       
   292 						testString = REINTERPRET_CAST(CStringTestParameter*, test);
       
   293 						testString->GetValue(fracSecond);
       
   294 						
       
   295 						// gets offset
       
   296 						test = (*iValues)[++pos];
       
   297 						testString = REINTERPRET_CAST(CStringTestParameter*, test);
       
   298 						testString->GetValue(offset);
       
   299 		
       
   300 						if(!PerformTestWithoutEncoderL(aConsole, encodedTime, currentTest, totalTests, fracSecond, offset))
       
   301 							return(EFalse);
       
   302 							
       
   303 					}
       
   304 				else
       
   305 					User::Leave(KErrNotSupported);
       
   306 				currentTest++;
       
   307 			}
       
   308 		else if(test->GetType() == CTestParameter::ERandom)
       
   309 			{
       
   310 			CRandomTestParameter *randomInt = REINTERPRET_CAST(CRandomTestParameter*, test);
       
   311 
       
   312 			for(TInt test = 0; test <= randomInt->Interations(); test++)
       
   313 				{
       
   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))
       
   324 					return(EFalse);
       
   325 					;
       
   326 				currentTest++;
       
   327 				}
       
   328 			}
       
   329 		else
       
   330 			return(EFalse);
       
   331 		};
       
   332 
       
   333 	
       
   334 
       
   335 	iASN1Action.ReportProgressL(KErrNone, totalTests, totalTests);
       
   336 	return(ETrue);
       
   337 	};