1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/tasn1/testbitstr.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,117 @@
1.4 +/*
1.5 +* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +* Implementation for testing bit string encoding/decoding
1.19 +*
1.20 +*/
1.21 +
1.22 +
1.23 +#include "testbitstr.h"
1.24 +#include "tasn1normaltest.h"
1.25 +#include <asn1enc.h>
1.26 +#include <asn1dec.h>
1.27 +
1.28 +#include <e32cons.h>
1.29 +
1.30 +CTestBitStr* CTestBitStr::NewL(CASN1NormalTest &aASN1Action)
1.31 + {
1.32 + CTestBitStr* test = new (ELeave) CTestBitStr(aASN1Action);
1.33 + return test;
1.34 + }
1.35 +
1.36 +CTestBitStr::CTestBitStr(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action)
1.37 + {
1.38 + };
1.39 +
1.40 +void CTestBitStr::GetName(TDes& aBuf)
1.41 + {
1.42 + aBuf.Copy(_L("Test Bit String"));
1.43 + }
1.44 +
1.45 +TBool CTestBitStr::PerformTestsL(CConsoleBase& aConsole)
1.46 + {
1.47 + TBool pass = ETrue;
1.48 +
1.49 +
1.50 + // Test the encoding varying length bit strings by encoding 65
1.51 + // different bit strings with a single bit set in each position. Position -1
1.52 + // indicates the empty bit string.
1.53 + for (TInt8 bitNum = -1; bitNum < 64; bitNum++)
1.54 + {
1.55 + TBuf8<8> bitStr;
1.56 + TUint numOctets;
1.57 +
1.58 + if (bitNum >= 0)
1.59 + {
1.60 + numOctets = 1 + (bitNum / 8);
1.61 + }
1.62 + else
1.63 + {
1.64 + numOctets = 0;
1.65 + }
1.66 +
1.67 + bitStr.SetLength(numOctets);
1.68 + bitStr.FillZ();
1.69 +
1.70 + TUint8 valToEncode = 0;
1.71 + if (bitNum >= 0 )
1.72 + {
1.73 + // The most significant bit in the most significant byte is bit zero
1.74 + valToEncode = (TUint8) (1 << (7 - (bitNum % 8)));
1.75 + bitStr[bitNum / 8] = valToEncode;
1.76 + }
1.77 +
1.78 + // Get the encoder and decoder
1.79 + CASN1EncBitString* encoder = CASN1EncBitString::NewLC(bitStr, bitNum + 1);
1.80 + TASN1DecBitString decoder;
1.81 +
1.82 + // Prepare an encode buffer
1.83 + TInt totalLength = encoder->LengthDER();
1.84 + HBufC8* encodeBuffer = HBufC8::NewMaxLC(totalLength);
1.85 + TPtr8 tEncodeBuf = encodeBuffer->Des();
1.86 +
1.87 + // Write into the encode buffer
1.88 + TUint writeLength = 0;
1.89 + encoder->WriteDERL(tEncodeBuf, writeLength);
1.90 +
1.91 + // Read it out again and check lengths plus encoded value
1.92 + TInt readLength = 0;
1.93 + HBufC8* decodeBuffer = decoder.ExtractOctetStringL(tEncodeBuf, readLength);
1.94 + CleanupStack::PushL(decodeBuffer);
1.95 + TPtr8 tDecodeBuf = decodeBuffer->Des();
1.96 +
1.97 + if (writeLength != STATIC_CAST(TUint, readLength))
1.98 + {
1.99 + aConsole.Write(_L("ERROR!\n"));
1.100 + iASN1Action.ReportProgressL(KErrASN1EncodingError, 1, 1);
1.101 + pass = EFalse;
1.102 + }
1.103 + else if (bitNum >= 0 && valToEncode != tDecodeBuf[bitNum / 8])
1.104 + {
1.105 + aConsole.Write(_L("ENCODING ERROR!\n"));
1.106 + iASN1Action.ReportProgressL(KErrASN1EncodingError, 1, 1);
1.107 + pass = EFalse;
1.108 + }
1.109 + else
1.110 + {
1.111 + iASN1Action.ReportProgressL(KErrNone, bitNum + 1, 65);
1.112 + }
1.113 +
1.114 + CleanupStack::PopAndDestroy(3, encoder); // decodeBuffer, encodeBuffer, encoder
1.115 + }
1.116 +
1.117 + return pass;
1.118 + }
1.119 +
1.120 +