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.
16 * Implementation for testing TInteger encoding/decoding
21 #include "testbigint.h"
22 #include "tasn1normaltest.h"
29 CTestBigInt* CTestBigInt::NewL(CASN1NormalTest &aASN1Action)
31 CTestBigInt* test = new (ELeave) CTestBigInt(aASN1Action);
35 CTestBigInt::CTestBigInt(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action)
39 void CTestBigInt::GetName(TDes& aBuf)
41 aBuf.Copy(_L("Test BigInteger"));
44 void CTestBigInt::FillParameterArray(void)
46 iParameters->Append(CTestParameter::EInt);
50 // This function can leave
51 TBool CTestBigInt::PerformTest(CConsoleBase& aConsole, const RInteger& aTest, const TInt &aTestNumber, const TInt &aTotalTests)
53 // Make the value to encode
54 RInteger encodedValue = RInteger::NewL(0);
55 CleanupStack::PushL(encodedValue);
56 encodedValue.CopyL(aTest);
59 CASN1EncBigInt* encoder = CASN1EncBigInt::NewLC(encodedValue);
62 HBufC8* buf = HBufC8::NewMaxLC(encoder->LengthDER());
63 TPtr8 tBuf = buf->Des();
65 // Write into the buffer
66 TUint writeLength = 0;
67 encoder->WriteDERL(tBuf, writeLength);
70 TASN1DecInteger decoder;
72 RInteger decodedValue = decoder.DecodeDERLongL(tBuf, readLength);
73 CleanupStack::PushL(decodedValue);
75 //Check that a positive integer has not been encoded as negative - DEF038956
76 TBool negEncodeErr = EFalse;
77 RInteger null = RInteger::NewL(0);
78 CleanupStack::PushL(null);
81 HBufC8* originalValue = aTest.BufferLC();
82 TPtrC8 tOriginalValue = originalValue->Des();
83 //determine which byte to check (should a leading zero byte have been added)
84 if ((*originalValue)[0] & 0x80)
86 //for example 000000FF, aTest value FF, *buf 020200FF need to confirm (*buf)[2] is 00
87 //encoder->LengthDER() = 4, tOriginalValue.Length() = 1
88 if ((*buf)[encoder->LengthDER() - tOriginalValue.Length() - 1] != 0)
95 //for example 0000007F, aTest value 7F, *buf 02027F need to confirm leading bit (*buf)[2] not set
96 //encoder->LengthDER() = 3, tOriginalValue.Length() = 1
97 if ((*buf)[encoder->LengthDER() - tOriginalValue.Length()] & 0x80)
102 CleanupStack::PopAndDestroy(); //originalValue
104 CleanupStack::PopAndDestroy(&null); //null
106 // Check lengths of reads + values
107 if ((writeLength != STATIC_CAST(TUint, readLength)) || !(decodedValue == encodedValue) || (negEncodeErr))
110 aConsole.Printf(_L("\nERROR! Problem integer: \n"));
111 OutputIntegerL(aConsole, encodedValue);
112 OutputIntegerL(aConsole, decodedValue);
113 OutputEncodingL(aConsole, tBuf);
114 iASN1Action.ReportProgressL(KErrASN1EncodingError, aTestNumber, aTotalTests);
115 CleanupStack::PopAndDestroy(4, &encodedValue); // decodedValue, buf, encoder, encodedValue
120 iASN1Action.ReportProgressL(KErrNone, aTestNumber, aTotalTests);
121 CleanupStack::PopAndDestroy(4, &encodedValue); // decodedValue, buf, encoder, encodedValue
127 void CTestBigInt::OutputIntegerL(CConsoleBase& aConsole, RInteger& aInt)
129 aConsole.Printf(_L("Bytes() = "));
131 bytes.AppendNum(aInt.ByteCount());
132 bytes.Append(_L(", "));
133 aConsole.Printf(bytes);
135 aConsole.Printf(_L("Bits() = "));
137 bits.AppendNum(aInt.BitCount());
138 bits.Append(_L(", "));
139 aConsole.Printf(bits);
141 HBufC8* buf = aInt.BufferLC();
142 TPtr8 ptr = buf->Des();
143 TInt size = ptr.Length();
144 for (TInt i = 0; i < size; ++i)
147 tbuf.AppendNumFixedWidth(ptr[i], EHex, 2);
148 aConsole.Printf(tbuf);
151 RInteger null = RInteger::NewL(0);
154 ASSERT(EFalse); // Shouldn't happen for new crypto api - no signed integers
155 aConsole.Printf(_L(" (-ve)"));
159 aConsole.Printf(_L(" (+ve)"));
164 aConsole.Printf(_L("\n"));
165 CleanupStack::PopAndDestroy(); // buf;
169 TBool CTestBigInt::PerformTestsL(CConsoleBase& aConsole)
171 const TInt KMaxBits = 2048;
172 CTestParameter* test;
173 TInt totalTests, currentTest=0;
175 if(!CountTests(totalTests)) return(EFalse);
177 for(TInt pos = 0; pos < iValues->Count(); pos++)
179 test = (*iValues)[pos];
181 switch(test->GetType())
183 case CTestParameter::EInt :
185 CIntTestParameter *rangeInt = REINTERPRET_CAST(CIntTestParameter*, test);
186 RInteger encodedValue;
187 encodedValue = RInteger::NewL(rangeInt->Value());
188 CleanupStack::PushL(encodedValue);
189 if(PerformTest(aConsole, encodedValue, currentTest, totalTests))
190 CleanupStack::PopAndDestroy(&encodedValue);
193 CleanupStack::PopAndDestroy(&encodedValue);
199 case CTestParameter::EIntRange :
201 CIntRangeTestParameter *rangeInt = REINTERPRET_CAST(CIntRangeTestParameter*, test);
202 for(TInt test = rangeInt->Start(); test <= rangeInt->Finish(); test++)
204 RInteger encodedValue;
205 encodedValue = RInteger::NewL(test);
206 CleanupStack::PushL(encodedValue);
207 if(PerformTest(aConsole, encodedValue, currentTest, totalTests))
208 CleanupStack::PopAndDestroy(&encodedValue);
211 CleanupStack::PopAndDestroy(&encodedValue);
218 case CTestParameter::ERandom :
220 CRandomTestParameter *randomInt = REINTERPRET_CAST(CRandomTestParameter*, test);
221 for(TInt test = 0; test <= randomInt->Interations(); test++)
223 RInteger encodedValue;
224 encodedValue = RInteger::NewRandomL((test % KMaxBits) + 1, TInteger::EAllBitsRandom);
225 CleanupStack::PushL(encodedValue);
226 if(PerformTest(aConsole, encodedValue, currentTest, totalTests))
227 CleanupStack::PopAndDestroy(&encodedValue);
230 CleanupStack::PopAndDestroy(&encodedValue);
243 iASN1Action.ReportProgressL(KErrNone, totalTests, totalTests);