sl@0: /* sl@0: * Copyright (c) 2005-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 bit string encoding/decoding sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include "testbitstr.h" sl@0: #include "tasn1normaltest.h" sl@0: #include sl@0: #include sl@0: sl@0: #include sl@0: sl@0: CTestBitStr* CTestBitStr::NewL(CASN1NormalTest &aASN1Action) sl@0: { sl@0: CTestBitStr* test = new (ELeave) CTestBitStr(aASN1Action); sl@0: return test; sl@0: } sl@0: sl@0: CTestBitStr::CTestBitStr(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action) sl@0: { sl@0: }; sl@0: sl@0: void CTestBitStr::GetName(TDes& aBuf) sl@0: { sl@0: aBuf.Copy(_L("Test Bit String")); sl@0: } sl@0: sl@0: TBool CTestBitStr::PerformTestsL(CConsoleBase& aConsole) sl@0: { sl@0: TBool pass = ETrue; sl@0: sl@0: sl@0: // Test the encoding varying length bit strings by encoding 65 sl@0: // different bit strings with a single bit set in each position. Position -1 sl@0: // indicates the empty bit string. sl@0: for (TInt8 bitNum = -1; bitNum < 64; bitNum++) sl@0: { sl@0: TBuf8<8> bitStr; sl@0: TUint numOctets; sl@0: sl@0: if (bitNum >= 0) sl@0: { sl@0: numOctets = 1 + (bitNum / 8); sl@0: } sl@0: else sl@0: { sl@0: numOctets = 0; sl@0: } sl@0: sl@0: bitStr.SetLength(numOctets); sl@0: bitStr.FillZ(); sl@0: sl@0: TUint8 valToEncode = 0; sl@0: if (bitNum >= 0 ) sl@0: { sl@0: // The most significant bit in the most significant byte is bit zero sl@0: valToEncode = (TUint8) (1 << (7 - (bitNum % 8))); sl@0: bitStr[bitNum / 8] = valToEncode; sl@0: } sl@0: sl@0: // Get the encoder and decoder sl@0: CASN1EncBitString* encoder = CASN1EncBitString::NewLC(bitStr, bitNum + 1); sl@0: TASN1DecBitString decoder; sl@0: sl@0: // Prepare an encode buffer sl@0: TInt totalLength = encoder->LengthDER(); sl@0: HBufC8* encodeBuffer = HBufC8::NewMaxLC(totalLength); sl@0: TPtr8 tEncodeBuf = encodeBuffer->Des(); sl@0: sl@0: // Write into the encode buffer sl@0: TUint writeLength = 0; sl@0: encoder->WriteDERL(tEncodeBuf, writeLength); sl@0: sl@0: // Read it out again and check lengths plus encoded value sl@0: TInt readLength = 0; sl@0: HBufC8* decodeBuffer = decoder.ExtractOctetStringL(tEncodeBuf, readLength); sl@0: CleanupStack::PushL(decodeBuffer); sl@0: TPtr8 tDecodeBuf = decodeBuffer->Des(); sl@0: sl@0: if (writeLength != STATIC_CAST(TUint, readLength)) sl@0: { sl@0: aConsole.Write(_L("ERROR!\n")); sl@0: iASN1Action.ReportProgressL(KErrASN1EncodingError, 1, 1); sl@0: pass = EFalse; sl@0: } sl@0: else if (bitNum >= 0 && valToEncode != tDecodeBuf[bitNum / 8]) sl@0: { sl@0: aConsole.Write(_L("ENCODING ERROR!\n")); sl@0: iASN1Action.ReportProgressL(KErrASN1EncodingError, 1, 1); sl@0: pass = EFalse; sl@0: } sl@0: else sl@0: { sl@0: iASN1Action.ReportProgressL(KErrNone, bitNum + 1, 65); sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(3, encoder); // decodeBuffer, encodeBuffer, encoder sl@0: } sl@0: sl@0: return pass; sl@0: } sl@0: sl@0: