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 TInt encoding/decoding sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include "testint.h" sl@0: #include "tasn1normaltest.h" sl@0: #include sl@0: #include sl@0: sl@0: #include sl@0: #include sl@0: sl@0: sl@0: CTestInt* CTestInt::NewL(CASN1NormalTest &aASN1Action) sl@0: { sl@0: CTestInt* test = new (ELeave) CTestInt(aASN1Action); sl@0: return test; sl@0: } sl@0: sl@0: CTestInt::CTestInt(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action) sl@0: { sl@0: }; sl@0: sl@0: sl@0: void CTestInt::GetName(TDes& aBuf) sl@0: { sl@0: aBuf.Copy(_L("Test TInt")); sl@0: } sl@0: sl@0: void CTestInt::FillParameterArray(void) sl@0: { sl@0: iParameters->Append(CTestParameter::EInt); sl@0: } sl@0: sl@0: sl@0: sl@0: TBool CTestInt::PerformTest(CConsoleBase& aConsole, const TInt &aTest, const TInt &aTestNumber, const TInt &aTotalTests) sl@0: { sl@0: // Choose value to encode sl@0: TInt encodedValue; sl@0: sl@0: encodedValue = aTest; sl@0: sl@0: // Get encoder sl@0: CASN1EncInt* encoder = CASN1EncInt::NewLC(encodedValue); sl@0: sl@0: // Prepare a buffer sl@0: TUint length = encoder->LengthDER(); sl@0: HBufC8* buf = HBufC8::NewMaxLC(length); 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: // Read it out again sl@0: TASN1DecInteger decoder; sl@0: TInt readLength = 0; sl@0: TInt decodedValue = decoder.DecodeDERShortL(tBuf, readLength); sl@0: sl@0: // Check lengths of reads + values sl@0: if ((writeLength != STATIC_CAST(TUint, readLength)) || (decodedValue != encodedValue)) sl@0: { sl@0: aConsole.Printf(_L("\nERROR! Problem integer: %d\n"), encodedValue); 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: TBool CTestInt::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::EInt : sl@0: { sl@0: CIntTestParameter *rangeInt = REINTERPRET_CAST(CIntTestParameter*, test); sl@0: sl@0: if(!PerformTest(aConsole, rangeInt->Value(), currentTest, totalTests)) sl@0: { sl@0: return(EFalse); sl@0: } sl@0: currentTest++; sl@0: break; sl@0: } sl@0: case CTestParameter::EIntRange : sl@0: { sl@0: CIntRangeTestParameter *rangeInt = REINTERPRET_CAST(CIntRangeTestParameter*, test); sl@0: sl@0: for(TInt test = rangeInt->Start(); test <= rangeInt->Finish(); test++) sl@0: { sl@0: if(!PerformTest(aConsole, test, currentTest, totalTests)) sl@0: { sl@0: return(EFalse); sl@0: } sl@0: currentTest++; sl@0: } sl@0: break; sl@0: } sl@0: case CTestParameter::ERandom : sl@0: { sl@0: CRandomTestParameter *randomInt = REINTERPRET_CAST(CRandomTestParameter*, test); sl@0: TInt encodedValue; sl@0: sl@0: for(TInt test = 0; test <= randomInt->Interations(); test++) sl@0: { sl@0: encodedValue = Math::Rand(seed) >> (8 * (test % 4)); sl@0: if(!PerformTest(aConsole, encodedValue, currentTest, totalTests)) sl@0: { sl@0: return(EFalse); sl@0: } sl@0: currentTest++; 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: