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 octet string encoding/decoding
20 #include "testoctetstr.h"
21 #include "tasn1normaltest.h"
28 const TInt KMaxStringLength = 1000;
30 CTestOctetString* CTestOctetString::NewL(CASN1NormalTest &aASN1Action)
32 CTestOctetString* test = new (ELeave) CTestOctetString(aASN1Action);
37 CTestOctetString::CTestOctetString(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action)
41 void CTestOctetString::ConstructL()
43 toEncodeBuf = HBufC8::NewMaxLC(KMaxStringLength);
44 encodedBuf = HBufC8::NewMaxLC(KMaxStringLength + 10); // extra 10 for tag/length
48 void CTestOctetString::FillParameterArray(void)
50 iParameters->Append(CTestParameter::EString);
54 CTestOctetString::~CTestOctetString()
60 void CTestOctetString::GetName(TDes& aBuf)
62 aBuf.Copy(_L("Test Octet String"));
66 TBool CTestOctetString::PerformTest(CConsoleBase& aConsole, const TPtr8 &toEncodePtr, const TInt &aTestNumber, const TInt &aTotalTests)
68 TPtr8 encodedPtr = encodedBuf->Des();
71 CASN1EncOctetString* encoder = CASN1EncOctetString::NewLC(toEncodePtr);
74 TUint writeLength = 0;
75 encoder->WriteDERL(encodedPtr, writeLength);
78 TASN1DecOctetString decoder;
80 HBufC8* readBuf = decoder.DecodeDERL(encodedPtr, readLength);
81 CleanupStack::PushL(readBuf);
83 // Check lengths of reads + values
84 if ((writeLength != STATIC_CAST(TUint, readLength)) || (*readBuf != toEncodePtr))
86 aConsole.Printf(_L("\nERROR!\n"));
87 iASN1Action.ReportProgressL(KErrASN1EncodingError, aTestNumber, aTotalTests);
88 CleanupStack::PopAndDestroy(2); // encoder, readBuf
93 iASN1Action.ReportProgressL(KErrNone, aTestNumber, aTotalTests);
94 CleanupStack::PopAndDestroy(2); // encoder, readBuf
100 TBool CTestOctetString::PerformTestsL(CConsoleBase& aConsole)
102 TInt nLow = Math::Random();
103 TInt64 nHigh((TInt)Math::Random());
104 TInt64 seed((nHigh << 32) + nLow);
105 CTestParameter* test;
106 TInt totalTests, currentTest=0;
108 if(!CountTests(totalTests)) return(EFalse);
110 for(TInt pos = 0; pos < iValues->Count(); pos++)
112 test = (*iValues)[pos];
113 switch(test->GetType())
115 case CTestParameter::EString :
117 CStringTestParameter *testString = REINTERPRET_CAST(CStringTestParameter*, test);
118 HBufC* value = HBufC::NewLC(KMaxStringLength);
119 TPtr tBuf = value->Des();
121 testString->GetValue(tBuf);
123 if((tBuf.Length() % 2) != 0)
124 User::Leave(KErrArgument);
126 TPtr8 toEncodePtr(toEncodeBuf->Des());
130 for(TInt octet = 0; octet < tBuf.Length(); octet+=2)
132 TLex lex(tBuf.Mid(octet, 2));
134 if(lex.Val(theOctet, EHex)!=KErrNone)
135 User::Leave(KErrArgument);
136 toEncodePtr[octetPos++] = STATIC_CAST(TUint8, theOctet);
138 if(PerformTest(aConsole, toEncodePtr, currentTest, totalTests))
140 CleanupStack::PopAndDestroy();
145 CleanupStack::PopAndDestroy();
150 case CTestParameter::ERandom :
152 CRandomTestParameter *randomInt = REINTERPRET_CAST(CRandomTestParameter*, test);
154 for (TInt count = 0; count <= randomInt->Interations(); ++count)
156 // Descriptor for part of buffer we'll use; fill with noise
157 TInt toEncodeLength = count % KMaxStringLength;
158 TPtr8 toEncodePtr(toEncodeBuf->Des());
159 for (TInt i = 0; i < toEncodeLength; ++i)
161 toEncodePtr[i] = STATIC_CAST(TUint8, Math::Rand(seed));
163 if(!PerformTest(aConsole, toEncodePtr, currentTest, totalTests))
176 iASN1Action.ReportProgressL(KErrNone, totalTests, totalTests);