sl@0: /* sl@0: * Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * Implementation for testing octet string encoding/decoding sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include "testoctetstr.h" sl@0: #include "tasn1normaltest.h" sl@0: #include sl@0: #include sl@0: sl@0: #include sl@0: #include sl@0: sl@0: const TInt KMaxStringLength = 1000; sl@0: sl@0: CTestOctetString* CTestOctetString::NewL(CASN1NormalTest &aASN1Action) sl@0: { sl@0: CTestOctetString* test = new (ELeave) CTestOctetString(aASN1Action); sl@0: test->ConstructL(); sl@0: return test; sl@0: } sl@0: sl@0: CTestOctetString::CTestOctetString(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action) sl@0: { sl@0: }; sl@0: sl@0: void CTestOctetString::ConstructL() sl@0: { sl@0: toEncodeBuf = HBufC8::NewMaxLC(KMaxStringLength); sl@0: encodedBuf = HBufC8::NewMaxLC(KMaxStringLength + 10); // extra 10 for tag/length sl@0: CleanupStack::Pop(2); sl@0: }; sl@0: sl@0: void CTestOctetString::FillParameterArray(void) sl@0: { sl@0: iParameters->Append(CTestParameter::EString); sl@0: } sl@0: sl@0: sl@0: CTestOctetString::~CTestOctetString() sl@0: { sl@0: delete toEncodeBuf; sl@0: delete encodedBuf; sl@0: } sl@0: sl@0: void CTestOctetString::GetName(TDes& aBuf) sl@0: { sl@0: aBuf.Copy(_L("Test Octet String")); sl@0: } sl@0: sl@0: sl@0: TBool CTestOctetString::PerformTest(CConsoleBase& aConsole, const TPtr8 &toEncodePtr, const TInt &aTestNumber, const TInt &aTotalTests) sl@0: { sl@0: TPtr8 encodedPtr = encodedBuf->Des(); sl@0: sl@0: // Make the encoder sl@0: CASN1EncOctetString* encoder = CASN1EncOctetString::NewLC(toEncodePtr); sl@0: sl@0: // Do the encoding sl@0: TUint writeLength = 0; sl@0: encoder->WriteDERL(encodedPtr, writeLength); sl@0: sl@0: // Read it out again sl@0: TASN1DecOctetString decoder; sl@0: TInt readLength = 0; sl@0: HBufC8* readBuf = decoder.DecodeDERL(encodedPtr, readLength); sl@0: CleanupStack::PushL(readBuf); sl@0: sl@0: // Check lengths of reads + values sl@0: if ((writeLength != STATIC_CAST(TUint, readLength)) || (*readBuf != toEncodePtr)) sl@0: { sl@0: aConsole.Printf(_L("\nERROR!\n")); sl@0: iASN1Action.ReportProgressL(KErrASN1EncodingError, aTestNumber, aTotalTests); sl@0: CleanupStack::PopAndDestroy(2); // encoder, readBuf sl@0: return(ETrue); sl@0: } sl@0: else sl@0: { sl@0: iASN1Action.ReportProgressL(KErrNone, aTestNumber, aTotalTests); sl@0: CleanupStack::PopAndDestroy(2); // encoder, readBuf sl@0: return(ETrue); sl@0: } sl@0: } sl@0: sl@0: sl@0: TBool CTestOctetString::PerformTestsL(CConsoleBase& aConsole) sl@0: { sl@0: TInt nLow = Math::Random(); sl@0: TInt64 nHigh((TInt)Math::Random()); sl@0: TInt64 seed((nHigh << 32) + nLow); sl@0: CTestParameter* test; sl@0: TInt totalTests, currentTest=0; sl@0: sl@0: if(!CountTests(totalTests)) return(EFalse); sl@0: sl@0: for(TInt pos = 0; pos < iValues->Count(); pos++) sl@0: { sl@0: test = (*iValues)[pos]; sl@0: switch(test->GetType()) sl@0: { sl@0: case CTestParameter::EString : sl@0: { sl@0: CStringTestParameter *testString = REINTERPRET_CAST(CStringTestParameter*, test); sl@0: HBufC* value = HBufC::NewLC(KMaxStringLength); sl@0: TPtr tBuf = value->Des(); sl@0: sl@0: testString->GetValue(tBuf); sl@0: sl@0: if((tBuf.Length() % 2) != 0) sl@0: User::Leave(KErrArgument); sl@0: sl@0: TPtr8 toEncodePtr(toEncodeBuf->Des()); sl@0: TUint theOctet; sl@0: TInt octetPos=0; sl@0: sl@0: for(TInt octet = 0; octet < tBuf.Length(); octet+=2) sl@0: { sl@0: TLex lex(tBuf.Mid(octet, 2)); sl@0: sl@0: if(lex.Val(theOctet, EHex)!=KErrNone) sl@0: User::Leave(KErrArgument); sl@0: toEncodePtr[octetPos++] = STATIC_CAST(TUint8, theOctet); sl@0: }; sl@0: if(PerformTest(aConsole, toEncodePtr, currentTest, totalTests)) sl@0: { sl@0: CleanupStack::PopAndDestroy(); sl@0: currentTest++; sl@0: } sl@0: else sl@0: { sl@0: CleanupStack::PopAndDestroy(); sl@0: return(EFalse); sl@0: } sl@0: break; sl@0: } sl@0: case CTestParameter::ERandom : sl@0: { sl@0: CRandomTestParameter *randomInt = REINTERPRET_CAST(CRandomTestParameter*, test); sl@0: sl@0: for (TInt count = 0; count <= randomInt->Interations(); ++count) sl@0: { sl@0: // Descriptor for part of buffer we'll use; fill with noise sl@0: TInt toEncodeLength = count % KMaxStringLength; sl@0: TPtr8 toEncodePtr(toEncodeBuf->Des()); sl@0: for (TInt i = 0; i < toEncodeLength; ++i) sl@0: { sl@0: toEncodePtr[i] = STATIC_CAST(TUint8, Math::Rand(seed)); sl@0: } sl@0: if(!PerformTest(aConsole, toEncodePtr, currentTest, totalTests)) sl@0: return(EFalse); sl@0: currentTest++; sl@0: } sl@0: sl@0: break; sl@0: } sl@0: default: sl@0: { sl@0: return EFalse; sl@0: } sl@0: } sl@0: } sl@0: iASN1Action.ReportProgressL(KErrNone, totalTests, totalTests); sl@0: return(ETrue); sl@0: }; sl@0: