|
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 * testinteger.cpp |
|
16 * Implementation for testing TInteger encoding/decoding |
|
17 * |
|
18 */ |
|
19 |
|
20 |
|
21 #include "testbigint.h" |
|
22 #include "tasn1normaltest.h" |
|
23 #include <asn1enc.h> |
|
24 #include <asn1dec.h> |
|
25 |
|
26 #include <e32cons.h> |
|
27 |
|
28 |
|
29 CTestBigInt* CTestBigInt::NewL(CASN1NormalTest &aASN1Action) |
|
30 { |
|
31 CTestBigInt* test = new (ELeave) CTestBigInt(aASN1Action); |
|
32 return test; |
|
33 } |
|
34 |
|
35 CTestBigInt::CTestBigInt(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action) |
|
36 { |
|
37 }; |
|
38 |
|
39 void CTestBigInt::GetName(TDes& aBuf) |
|
40 { |
|
41 aBuf.Copy(_L("Test BigInteger")); |
|
42 } |
|
43 |
|
44 void CTestBigInt::FillParameterArray(void) |
|
45 { |
|
46 iParameters->Append(CTestParameter::EInt); |
|
47 } |
|
48 |
|
49 |
|
50 // This function can leave |
|
51 TBool CTestBigInt::PerformTest(CConsoleBase& aConsole, const RInteger& aTest, const TInt &aTestNumber, const TInt &aTotalTests) |
|
52 { |
|
53 // Make the value to encode |
|
54 RInteger encodedValue = RInteger::NewL(0); |
|
55 CleanupStack::PushL(encodedValue); |
|
56 encodedValue.CopyL(aTest); |
|
57 |
|
58 // Make the encoder |
|
59 CASN1EncBigInt* encoder = CASN1EncBigInt::NewLC(encodedValue); |
|
60 |
|
61 // Prepare a buffer |
|
62 HBufC8* buf = HBufC8::NewMaxLC(encoder->LengthDER()); |
|
63 TPtr8 tBuf = buf->Des(); |
|
64 |
|
65 // Write into the buffer |
|
66 TUint writeLength = 0; |
|
67 encoder->WriteDERL(tBuf, writeLength); |
|
68 |
|
69 // Read it out again |
|
70 TASN1DecInteger decoder; |
|
71 TInt readLength = 0; |
|
72 RInteger decodedValue = decoder.DecodeDERLongL(tBuf, readLength); |
|
73 CleanupStack::PushL(decodedValue); |
|
74 |
|
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); |
|
79 if (aTest > null) |
|
80 { |
|
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) |
|
85 { |
|
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) |
|
89 { |
|
90 negEncodeErr = ETrue; |
|
91 } |
|
92 } |
|
93 else |
|
94 { |
|
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) |
|
98 { |
|
99 negEncodeErr = ETrue; |
|
100 } |
|
101 } |
|
102 CleanupStack::PopAndDestroy(); //originalValue |
|
103 } |
|
104 CleanupStack::PopAndDestroy(&null); //null |
|
105 |
|
106 // Check lengths of reads + values |
|
107 if ((writeLength != STATIC_CAST(TUint, readLength)) || !(decodedValue == encodedValue) || (negEncodeErr)) |
|
108 //if(1) |
|
109 { |
|
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 |
|
116 return(EFalse); |
|
117 } |
|
118 else |
|
119 { |
|
120 iASN1Action.ReportProgressL(KErrNone, aTestNumber, aTotalTests); |
|
121 CleanupStack::PopAndDestroy(4, &encodedValue); // decodedValue, buf, encoder, encodedValue |
|
122 return(ETrue); |
|
123 } |
|
124 } |
|
125 |
|
126 |
|
127 void CTestBigInt::OutputIntegerL(CConsoleBase& aConsole, RInteger& aInt) |
|
128 { |
|
129 aConsole.Printf(_L("Bytes() = ")); |
|
130 TBuf<10> bytes; |
|
131 bytes.AppendNum(aInt.ByteCount()); |
|
132 bytes.Append(_L(", ")); |
|
133 aConsole.Printf(bytes); |
|
134 |
|
135 aConsole.Printf(_L("Bits() = ")); |
|
136 TBuf<10> bits; |
|
137 bits.AppendNum(aInt.BitCount()); |
|
138 bits.Append(_L(", ")); |
|
139 aConsole.Printf(bits); |
|
140 |
|
141 HBufC8* buf = aInt.BufferLC(); |
|
142 TPtr8 ptr = buf->Des(); |
|
143 TInt size = ptr.Length(); |
|
144 for (TInt i = 0; i < size; ++i) |
|
145 { |
|
146 TBuf<10> tbuf; |
|
147 tbuf.AppendNumFixedWidth(ptr[i], EHex, 2); |
|
148 aConsole.Printf(tbuf); |
|
149 } |
|
150 |
|
151 RInteger null = RInteger::NewL(0); |
|
152 if (aInt < null) |
|
153 { |
|
154 ASSERT(EFalse); // Shouldn't happen for new crypto api - no signed integers |
|
155 aConsole.Printf(_L(" (-ve)")); |
|
156 } |
|
157 else |
|
158 { |
|
159 aConsole.Printf(_L(" (+ve)")); |
|
160 } |
|
161 |
|
162 null.Close(); |
|
163 |
|
164 aConsole.Printf(_L("\n")); |
|
165 CleanupStack::PopAndDestroy(); // buf; |
|
166 } |
|
167 |
|
168 |
|
169 TBool CTestBigInt::PerformTestsL(CConsoleBase& aConsole) |
|
170 { |
|
171 const TInt KMaxBits = 2048; |
|
172 CTestParameter* test; |
|
173 TInt totalTests, currentTest=0; |
|
174 |
|
175 if(!CountTests(totalTests)) return(EFalse); |
|
176 |
|
177 for(TInt pos = 0; pos < iValues->Count(); pos++) |
|
178 { |
|
179 test = (*iValues)[pos]; |
|
180 |
|
181 switch(test->GetType()) |
|
182 { |
|
183 case CTestParameter::EInt : |
|
184 { |
|
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); |
|
191 else |
|
192 { |
|
193 CleanupStack::PopAndDestroy(&encodedValue); |
|
194 return(EFalse); |
|
195 }; |
|
196 currentTest++; |
|
197 break; |
|
198 } |
|
199 case CTestParameter::EIntRange : |
|
200 { |
|
201 CIntRangeTestParameter *rangeInt = REINTERPRET_CAST(CIntRangeTestParameter*, test); |
|
202 for(TInt test = rangeInt->Start(); test <= rangeInt->Finish(); test++) |
|
203 { |
|
204 RInteger encodedValue; |
|
205 encodedValue = RInteger::NewL(test); |
|
206 CleanupStack::PushL(encodedValue); |
|
207 if(PerformTest(aConsole, encodedValue, currentTest, totalTests)) |
|
208 CleanupStack::PopAndDestroy(&encodedValue); |
|
209 else |
|
210 { |
|
211 CleanupStack::PopAndDestroy(&encodedValue); |
|
212 return(EFalse); |
|
213 } |
|
214 currentTest++; |
|
215 }; |
|
216 break; |
|
217 } |
|
218 case CTestParameter::ERandom : |
|
219 { |
|
220 CRandomTestParameter *randomInt = REINTERPRET_CAST(CRandomTestParameter*, test); |
|
221 for(TInt test = 0; test <= randomInt->Interations(); test++) |
|
222 { |
|
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); |
|
228 else |
|
229 { |
|
230 CleanupStack::PopAndDestroy(&encodedValue); |
|
231 return(EFalse); |
|
232 } |
|
233 currentTest++; |
|
234 } |
|
235 break; |
|
236 } |
|
237 default: |
|
238 { |
|
239 return EFalse; |
|
240 } |
|
241 } |
|
242 } |
|
243 iASN1Action.ReportProgressL(KErrNone, totalTests, totalTests); |
|
244 return(ETrue); |
|
245 }; |