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 sequence object encoding/decoding
20 #include "testsequence.h"
21 #include "tasn1normaltest.h"
29 CTestSequence* CTestSequence::NewL(CASN1NormalTest &aASN1Action)
31 CTestSequence* test = new (ELeave) CTestSequence(aASN1Action);
35 CTestSequence::CTestSequence(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action)
40 void CTestSequence::GetName(TDes& aBuf)
42 aBuf.Copy(_L("Test Sequence"));
45 void CTestSequence::FillParameterArray(void)
47 iParameters->Append(CTestParameter::EInt);
48 iParameters->Append(CTestParameter::EInt);
52 TBool CTestSequence::PerformTest(CConsoleBase& aConsole, const TInt &aTestDepth, const TInt &aTestSize, const TInt &aTestNumber, const TInt &aTotalTests)
55 CASN1EncBase* encoder = MakeSequenceEncoderLC(aTestDepth, 0, aTestSize);
58 TInt totalLength = encoder->LengthDER();
59 HBufC8* buf = HBufC8::NewMaxLC(totalLength);
60 TPtr8 tBuf = buf->Des();
62 // Write into the buffer
63 TUint writeLength = 0;
64 encoder->WriteDERL(tBuf, writeLength);
66 // Check number of objects in buffer reads OK and matches what we hoped
67 TInt objectsRead = ReadAndCountL(tBuf);
68 if (objectsRead != iObjectsEncoded)
70 aConsole.Write(_L("ERROR! Encoded sequence object count mismatch\n"));
71 iASN1Action.ReportProgressL(KErrASN1EncodingError, aTestNumber, aTotalTests);
72 CleanupStack::PopAndDestroy(2); // buf, encoder
77 iASN1Action.ReportProgressL(KErrNone, aTestNumber, aTotalTests);
78 CleanupStack::PopAndDestroy(2); // buf, encoder
84 CASN1EncSequence* CTestSequence::MakeSequenceEncoderLC(TInt aMaxDepth,
88 __ASSERT_ALWAYS(aMaxDepth > 0, User::Leave(KErrArgument));
89 __ASSERT_ALWAYS(aMinSize >= 0, User::Leave(KErrArgument));
90 __ASSERT_ALWAYS(aMinSize <= aMaxSize, User::Leave(KErrArgument));
92 CASN1EncSequence* encoder = CASN1EncSequence::NewLC();
94 TUint targetSize = aMinSize + (Math::Random() % (aMaxSize + 1 - aMinSize));
96 while (size < targetSize)
98 const TInt KMaxChildType = 3;
99 const TInt childTypes = aMaxDepth > 1 ? KMaxChildType + 1 : KMaxChildType;
100 const TInt childType = (Math::Random() >> 5) % childTypes;
101 CASN1EncBase* child = 0;
102 CASN1EncSequence* seqEnc = 0;
106 child = CASN1EncInt::NewLC(Math::Random());
110 RInteger i = RInteger::NewRandomL(1234, TInteger::EAllBitsRandom);
111 CleanupStack::PushL(i);
112 CASN1EncBigInt* encInt = CASN1EncBigInt::NewLC(i);
113 CleanupStack::Pop(); // encInt
114 CleanupStack::PopAndDestroy(&i); // a deep copy of the info is taken
115 CleanupStack::PushL(encInt);
120 child = CASN1EncNull::NewLC();
123 // last case must be for next-level sequence encoder
124 seqEnc = MakeSequenceEncoderLC(aMaxDepth - 1, aMinSize, aMaxSize);
128 User::Leave(KErrNotSupported);
134 encoder->AddChildL(child);
135 CleanupStack::Pop(); // child
139 // If we've just added a sequence, want to test ability to add things to a sequence
140 // *that is itself a child*, to check size changes propagate upwards to parent
143 child = CASN1EncInt::NewLC(Math::Random());
145 seqEnc->AddChildL(child);
146 CleanupStack::Pop(); // child
157 TInt CTestSequence::ReadAndCountL(const TDesC8& aBuf)
161 TASN1DecGeneric decoder(aBuf);
164 TInt bufLength = aBuf.Length();
165 TInt readLength = decoder.LengthDER();
167 __ASSERT_ALWAYS(bufLength >= readLength, User::Leave(KErrCorrupt));
168 if (bufLength > readLength)
170 // Read the remainder of this buffer too
171 result += ReadAndCountL(aBuf.Mid(readLength));
174 if (decoder.Encoding()[0] & 0x20 // Tagged as constructed
175 && decoder.LengthDERContent() > 0)
177 // Read objects from inside the sequence too
178 result += ReadAndCountL(decoder.GetContentDER());
184 TBool CTestSequence::PerformTestsL(CConsoleBase& aConsole)
186 // Keep these two coprime to maximise combinations
187 const TInt KMaxSize = 11;
188 const TInt KMaxDepth = 10;
189 CTestParameter* test;
190 TInt totalTests, currentTest=0;
194 if(!CountTests(totalTests)) return(EFalse);
196 for(TInt pos = 0; pos < iValues->Count(); pos++)
198 test = (*iValues)[pos];
199 switch(test->GetType())
201 case CTestParameter::EInt :
203 CIntTestParameter *intTest = REINTERPRET_CAST(CIntTestParameter*, test);
205 // Get the encoder and decoder
206 maxDepth = intTest->Value();
207 test = (*iValues)[++pos];
208 if(test->GetType() != CTestParameter::EInt)
210 User::Leave(KErrNotSupported);
212 intTest = REINTERPRET_CAST(CIntTestParameter*, test);
213 maxSize = intTest->Value();
214 if(!PerformTest(aConsole, maxDepth, maxSize, currentTest, totalTests))
221 case CTestParameter::ERandom :
223 CRandomTestParameter *randomTest = REINTERPRET_CAST(CRandomTestParameter*, test);
224 // Get the encoder and decoder
226 for(TInt test = 0; test <= randomTest->Interations(); test++)
229 // Get the encoder and decoder
230 maxDepth = (test % KMaxDepth) + 1;
231 maxSize = test % KMaxSize;
234 // Don't do the too-big combinations (too slow)
235 if (maxDepth + maxSize > 14) continue;
236 if(!PerformTest(aConsole, maxDepth, maxSize, currentTest, totalTests))
249 iASN1Action.ReportProgressL(KErrNone, totalTests, totalTests);