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 sequence object encoding/decoding sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include "testsequence.h" sl@0: #include "tasn1normaltest.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #include sl@0: sl@0: CTestSequence* CTestSequence::NewL(CASN1NormalTest &aASN1Action) sl@0: { sl@0: CTestSequence* test = new (ELeave) CTestSequence(aASN1Action); sl@0: return test; sl@0: } sl@0: sl@0: CTestSequence::CTestSequence(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action) sl@0: { sl@0: }; sl@0: sl@0: sl@0: void CTestSequence::GetName(TDes& aBuf) sl@0: { sl@0: aBuf.Copy(_L("Test Sequence")); sl@0: } sl@0: sl@0: void CTestSequence::FillParameterArray(void) sl@0: { sl@0: iParameters->Append(CTestParameter::EInt); sl@0: iParameters->Append(CTestParameter::EInt); sl@0: } sl@0: sl@0: sl@0: TBool CTestSequence::PerformTest(CConsoleBase& aConsole, const TInt &aTestDepth, const TInt &aTestSize, const TInt &aTestNumber, const TInt &aTotalTests) sl@0: { sl@0: iObjectsEncoded = 1; sl@0: CASN1EncBase* encoder = MakeSequenceEncoderLC(aTestDepth, 0, aTestSize); sl@0: sl@0: // Prepare a buffer sl@0: TInt totalLength = encoder->LengthDER(); sl@0: HBufC8* buf = HBufC8::NewMaxLC(totalLength); sl@0: TPtr8 tBuf = buf->Des(); sl@0: sl@0: // Write into the buffer sl@0: TUint writeLength = 0; sl@0: encoder->WriteDERL(tBuf, writeLength); sl@0: sl@0: // Check number of objects in buffer reads OK and matches what we hoped sl@0: TInt objectsRead = ReadAndCountL(tBuf); sl@0: if (objectsRead != iObjectsEncoded) sl@0: { sl@0: aConsole.Write(_L("ERROR! Encoded sequence object count mismatch\n")); sl@0: iASN1Action.ReportProgressL(KErrASN1EncodingError, aTestNumber, aTotalTests); sl@0: CleanupStack::PopAndDestroy(2); // buf, encoder sl@0: return(EFalse); sl@0: } sl@0: else sl@0: { sl@0: iASN1Action.ReportProgressL(KErrNone, aTestNumber, aTotalTests); sl@0: CleanupStack::PopAndDestroy(2); // buf, encoder sl@0: return(ETrue); sl@0: } sl@0: } sl@0: sl@0: sl@0: CASN1EncSequence* CTestSequence::MakeSequenceEncoderLC(TInt aMaxDepth, sl@0: TInt aMinSize, sl@0: TInt aMaxSize) sl@0: { sl@0: __ASSERT_ALWAYS(aMaxDepth > 0, User::Leave(KErrArgument)); sl@0: __ASSERT_ALWAYS(aMinSize >= 0, User::Leave(KErrArgument)); sl@0: __ASSERT_ALWAYS(aMinSize <= aMaxSize, User::Leave(KErrArgument)); sl@0: sl@0: CASN1EncSequence* encoder = CASN1EncSequence::NewLC(); sl@0: sl@0: TUint targetSize = aMinSize + (Math::Random() % (aMaxSize + 1 - aMinSize)); sl@0: TUint size = 0; sl@0: while (size < targetSize) sl@0: { sl@0: const TInt KMaxChildType = 3; sl@0: const TInt childTypes = aMaxDepth > 1 ? KMaxChildType + 1 : KMaxChildType; sl@0: const TInt childType = (Math::Random() >> 5) % childTypes; sl@0: CASN1EncBase* child = 0; sl@0: CASN1EncSequence* seqEnc = 0; sl@0: switch (childType) sl@0: { sl@0: case 0: sl@0: child = CASN1EncInt::NewLC(Math::Random()); sl@0: break; sl@0: case 1: sl@0: { sl@0: RInteger i = RInteger::NewRandomL(1234, TInteger::EAllBitsRandom); sl@0: CleanupStack::PushL(i); sl@0: CASN1EncBigInt* encInt = CASN1EncBigInt::NewLC(i); sl@0: CleanupStack::Pop(); // encInt sl@0: CleanupStack::PopAndDestroy(&i); // a deep copy of the info is taken sl@0: CleanupStack::PushL(encInt); sl@0: child = encInt; sl@0: break; sl@0: } sl@0: case 2: sl@0: child = CASN1EncNull::NewLC(); sl@0: break; sl@0: case KMaxChildType: sl@0: // last case must be for next-level sequence encoder sl@0: seqEnc = MakeSequenceEncoderLC(aMaxDepth - 1, aMinSize, aMaxSize); sl@0: child = seqEnc; sl@0: break; sl@0: default: sl@0: User::Leave(KErrNotSupported); sl@0: break; sl@0: } sl@0: sl@0: if (child) sl@0: { sl@0: encoder->AddChildL(child); sl@0: CleanupStack::Pop(); // child sl@0: ++iObjectsEncoded; sl@0: ++size; sl@0: sl@0: // If we've just added a sequence, want to test ability to add things to a sequence sl@0: // *that is itself a child*, to check size changes propagate upwards to parent sl@0: if (seqEnc) sl@0: { sl@0: child = CASN1EncInt::NewLC(Math::Random()); sl@0: sl@0: seqEnc->AddChildL(child); sl@0: CleanupStack::Pop(); // child sl@0: ++iObjectsEncoded; sl@0: seqEnc = 0; sl@0: } sl@0: } sl@0: } sl@0: sl@0: return encoder; sl@0: } sl@0: sl@0: sl@0: TInt CTestSequence::ReadAndCountL(const TDesC8& aBuf) sl@0: { sl@0: TInt result = 1; sl@0: sl@0: TASN1DecGeneric decoder(aBuf); sl@0: decoder.InitL(); sl@0: sl@0: TInt bufLength = aBuf.Length(); sl@0: TInt readLength = decoder.LengthDER(); sl@0: sl@0: __ASSERT_ALWAYS(bufLength >= readLength, User::Leave(KErrCorrupt)); sl@0: if (bufLength > readLength) sl@0: { sl@0: // Read the remainder of this buffer too sl@0: result += ReadAndCountL(aBuf.Mid(readLength)); sl@0: } sl@0: sl@0: if (decoder.Encoding()[0] & 0x20 // Tagged as constructed sl@0: && decoder.LengthDERContent() > 0) sl@0: { sl@0: // Read objects from inside the sequence too sl@0: result += ReadAndCountL(decoder.GetContentDER()); sl@0: } sl@0: sl@0: return result; sl@0: } sl@0: sl@0: TBool CTestSequence::PerformTestsL(CConsoleBase& aConsole) sl@0: { sl@0: // Keep these two coprime to maximise combinations sl@0: const TInt KMaxSize = 11; sl@0: const TInt KMaxDepth = 10; sl@0: CTestParameter* test; sl@0: TInt totalTests, currentTest=0; sl@0: TInt maxDepth; sl@0: TInt maxSize; 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::EInt : sl@0: { sl@0: CIntTestParameter *intTest = REINTERPRET_CAST(CIntTestParameter*, test); sl@0: sl@0: // Get the encoder and decoder sl@0: maxDepth = intTest->Value(); sl@0: test = (*iValues)[++pos]; sl@0: if(test->GetType() != CTestParameter::EInt) sl@0: { sl@0: User::Leave(KErrNotSupported); sl@0: } sl@0: intTest = REINTERPRET_CAST(CIntTestParameter*, test); sl@0: maxSize = intTest->Value(); sl@0: if(!PerformTest(aConsole, maxDepth, maxSize, currentTest, totalTests)) sl@0: { sl@0: return(EFalse); sl@0: } sl@0: currentTest++; sl@0: break; sl@0: } sl@0: case CTestParameter::ERandom : sl@0: { sl@0: CRandomTestParameter *randomTest = REINTERPRET_CAST(CRandomTestParameter*, test); sl@0: // Get the encoder and decoder sl@0: sl@0: for(TInt test = 0; test <= randomTest->Interations(); test++) sl@0: { sl@0: sl@0: // Get the encoder and decoder sl@0: maxDepth = (test % KMaxDepth) + 1; sl@0: maxSize = test % KMaxSize; sl@0: sl@0: currentTest++; sl@0: // Don't do the too-big combinations (too slow) sl@0: if (maxDepth + maxSize > 14) continue; sl@0: if(!PerformTest(aConsole, maxDepth, maxSize, currentTest, totalTests)) sl@0: { sl@0: return(EFalse); 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: }