Update contrib.
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 object identifier encoding/decoding
21 #include "tasn1normaltest.h"
28 const TInt KMinOIDTerms = 2;
29 const TInt KMaxOIDTerms = KNumberOfIDs;
30 const TInt KMaxTermSize = 999999;
31 const TInt KMaxTermDigits = 6;
32 const TInt KMaxTermBits = 20; // Bits in KMaxTermsSize
33 const TInt KMaxBufSize = (KMaxTermDigits * (KMaxOIDTerms - 2)) + 2 + KMaxOIDTerms;
35 CTestOID* CTestOID::NewL(CASN1NormalTest &aASN1Action)
37 CTestOID* test = new (ELeave) CTestOID(aASN1Action);
41 CTestOID::CTestOID(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action)
46 void CTestOID::GetName(TDes& aBuf)
48 aBuf.Copy(_L("Test Object Identifier"));
52 void CTestOID::FillParameterArray(void)
54 iParameters->Append(CTestParameter::EString);
57 TBool CTestOID::PerformTest(CConsoleBase& aConsole, HBufC &aTest, const TInt &aTestNumber, const TInt &aTotalTests)
59 // Get data, place in encoder
60 CASN1EncObjectIdentifier* encoder = CASN1EncObjectIdentifier::NewLC(aTest);
63 TInt totalLength = encoder->LengthDER();
64 HBufC8* buf = HBufC8::NewMaxLC(totalLength);
65 TPtr8 tBuf = buf->Des();
67 // Write into the buffer
68 TUint writeLength = 0;
69 encoder->WriteDERL(tBuf, writeLength);
71 // Read it out again + check lengths
72 TASN1DecObjectIdentifier decoder;
74 HBufC* decodedData = decoder.DecodeDERL(tBuf, readLength);
75 CleanupStack::PushL(decodedData);
77 if (writeLength != STATIC_CAST(TUint, readLength) || aTest != *decodedData)
79 aConsole.Write(_L("ERROR!\nData to encode: "));
80 aConsole.Write(aTest);
81 aConsole.Write(_L("\nDecoded data: "));
82 aConsole.Write(*decodedData);
83 aConsole.Write(_L("\n"));
84 OutputEncodingL(aConsole, tBuf);
85 iASN1Action.ReportProgressL(KErrASN1EncodingError, aTestNumber, aTotalTests);
86 CleanupStack::PopAndDestroy(3); // decodedData, buf, encoder
91 iASN1Action.ReportProgressL(KErrNone, aTestNumber, aTotalTests);
92 CleanupStack::PopAndDestroy(3); // decodedData, buf, encoder
98 TBool CTestOID::PerformTestsL(CConsoleBase& aConsole)
100 CTestParameter* test;
101 TInt totalTests, currentTest=0;
103 if(!CountTests(totalTests)) return(EFalse);
105 for(TInt pos = 0; pos < iValues->Count(); pos++)
107 test = (*iValues)[pos];
108 switch(test->GetType())
110 case CTestParameter::EString :
112 CStringTestParameter *testString = REINTERPRET_CAST(CStringTestParameter*, test);
113 HBufC* value = HBufC::NewLC(KMaxBufSize);
114 TPtr tBuf = value->Des();
116 testString->GetValue(tBuf);
117 if(PerformTest(aConsole, *value, currentTest, totalTests))
120 CleanupStack::PopAndDestroy();
124 CleanupStack::PopAndDestroy();
129 case CTestParameter::ERandom :
131 CRandomTestParameter *testRandom = REINTERPRET_CAST(CRandomTestParameter*, test);
134 for(TInt test = 0; test <= testRandom->Interations(); test++)
136 value = HBufC::NewLC(KMaxBufSize);
138 TPtr tBuf = value->Des();
139 tBuf.AppendNum(static_cast<TUint>(Math::Random() % 3), EDecimal);
141 tBuf.AppendNum(static_cast<TUint>(Math::Random() % 40), EDecimal);
143 TInt targetTerms = (test % (1 + KMaxOIDTerms - KMinOIDTerms)) + KMinOIDTerms;
144 for (TInt terms = 2; terms < targetTerms; ++terms)
146 TInt term = Math::Random() % (KMaxTermSize >> (test % KMaxTermBits));
148 tBuf.AppendNum(term, EDecimal);
150 if(PerformTest(aConsole, *value, currentTest, totalTests))
152 CleanupStack::PopAndDestroy();
157 CleanupStack::PopAndDestroy();
169 iASN1Action.ReportProgressL(KErrNone, totalTests, totalTests);