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