Update contrib.
2 * Copyright (c) 2005-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 bit string encoding/decoding
20 #include "testbitstr.h"
21 #include "tasn1normaltest.h"
27 CTestBitStr* CTestBitStr::NewL(CASN1NormalTest &aASN1Action)
29 CTestBitStr* test = new (ELeave) CTestBitStr(aASN1Action);
33 CTestBitStr::CTestBitStr(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action)
37 void CTestBitStr::GetName(TDes& aBuf)
39 aBuf.Copy(_L("Test Bit String"));
42 TBool CTestBitStr::PerformTestsL(CConsoleBase& aConsole)
47 // Test the encoding varying length bit strings by encoding 65
48 // different bit strings with a single bit set in each position. Position -1
49 // indicates the empty bit string.
50 for (TInt8 bitNum = -1; bitNum < 64; bitNum++)
57 numOctets = 1 + (bitNum / 8);
64 bitStr.SetLength(numOctets);
67 TUint8 valToEncode = 0;
70 // The most significant bit in the most significant byte is bit zero
71 valToEncode = (TUint8) (1 << (7 - (bitNum % 8)));
72 bitStr[bitNum / 8] = valToEncode;
75 // Get the encoder and decoder
76 CASN1EncBitString* encoder = CASN1EncBitString::NewLC(bitStr, bitNum + 1);
77 TASN1DecBitString decoder;
79 // Prepare an encode buffer
80 TInt totalLength = encoder->LengthDER();
81 HBufC8* encodeBuffer = HBufC8::NewMaxLC(totalLength);
82 TPtr8 tEncodeBuf = encodeBuffer->Des();
84 // Write into the encode buffer
85 TUint writeLength = 0;
86 encoder->WriteDERL(tEncodeBuf, writeLength);
88 // Read it out again and check lengths plus encoded value
90 HBufC8* decodeBuffer = decoder.ExtractOctetStringL(tEncodeBuf, readLength);
91 CleanupStack::PushL(decodeBuffer);
92 TPtr8 tDecodeBuf = decodeBuffer->Des();
94 if (writeLength != STATIC_CAST(TUint, readLength))
96 aConsole.Write(_L("ERROR!\n"));
97 iASN1Action.ReportProgressL(KErrASN1EncodingError, 1, 1);
100 else if (bitNum >= 0 && valToEncode != tDecodeBuf[bitNum / 8])
102 aConsole.Write(_L("ENCODING ERROR!\n"));
103 iASN1Action.ReportProgressL(KErrASN1EncodingError, 1, 1);
108 iASN1Action.ReportProgressL(KErrNone, bitNum + 1, 65);
111 CleanupStack::PopAndDestroy(3, encoder); // decodeBuffer, encodeBuffer, encoder