sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
* This component and the accompanying materials are made available
|
sl@0
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
6 |
* which accompanies this distribution, and is available
|
sl@0
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* Initial Contributors:
|
sl@0
|
10 |
* Nokia Corporation - initial contribution.
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* Contributors:
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* Description:
|
sl@0
|
15 |
* Implementation for testing bit string encoding/decoding
|
sl@0
|
16 |
*
|
sl@0
|
17 |
*/
|
sl@0
|
18 |
|
sl@0
|
19 |
|
sl@0
|
20 |
#include "testbitstr.h"
|
sl@0
|
21 |
#include "tasn1normaltest.h"
|
sl@0
|
22 |
#include <asn1enc.h>
|
sl@0
|
23 |
#include <asn1dec.h>
|
sl@0
|
24 |
|
sl@0
|
25 |
#include <e32cons.h>
|
sl@0
|
26 |
|
sl@0
|
27 |
CTestBitStr* CTestBitStr::NewL(CASN1NormalTest &aASN1Action)
|
sl@0
|
28 |
{
|
sl@0
|
29 |
CTestBitStr* test = new (ELeave) CTestBitStr(aASN1Action);
|
sl@0
|
30 |
return test;
|
sl@0
|
31 |
}
|
sl@0
|
32 |
|
sl@0
|
33 |
CTestBitStr::CTestBitStr(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action)
|
sl@0
|
34 |
{
|
sl@0
|
35 |
};
|
sl@0
|
36 |
|
sl@0
|
37 |
void CTestBitStr::GetName(TDes& aBuf)
|
sl@0
|
38 |
{
|
sl@0
|
39 |
aBuf.Copy(_L("Test Bit String"));
|
sl@0
|
40 |
}
|
sl@0
|
41 |
|
sl@0
|
42 |
TBool CTestBitStr::PerformTestsL(CConsoleBase& aConsole)
|
sl@0
|
43 |
{
|
sl@0
|
44 |
TBool pass = ETrue;
|
sl@0
|
45 |
|
sl@0
|
46 |
|
sl@0
|
47 |
// Test the encoding varying length bit strings by encoding 65
|
sl@0
|
48 |
// different bit strings with a single bit set in each position. Position -1
|
sl@0
|
49 |
// indicates the empty bit string.
|
sl@0
|
50 |
for (TInt8 bitNum = -1; bitNum < 64; bitNum++)
|
sl@0
|
51 |
{
|
sl@0
|
52 |
TBuf8<8> bitStr;
|
sl@0
|
53 |
TUint numOctets;
|
sl@0
|
54 |
|
sl@0
|
55 |
if (bitNum >= 0)
|
sl@0
|
56 |
{
|
sl@0
|
57 |
numOctets = 1 + (bitNum / 8);
|
sl@0
|
58 |
}
|
sl@0
|
59 |
else
|
sl@0
|
60 |
{
|
sl@0
|
61 |
numOctets = 0;
|
sl@0
|
62 |
}
|
sl@0
|
63 |
|
sl@0
|
64 |
bitStr.SetLength(numOctets);
|
sl@0
|
65 |
bitStr.FillZ();
|
sl@0
|
66 |
|
sl@0
|
67 |
TUint8 valToEncode = 0;
|
sl@0
|
68 |
if (bitNum >= 0 )
|
sl@0
|
69 |
{
|
sl@0
|
70 |
// The most significant bit in the most significant byte is bit zero
|
sl@0
|
71 |
valToEncode = (TUint8) (1 << (7 - (bitNum % 8)));
|
sl@0
|
72 |
bitStr[bitNum / 8] = valToEncode;
|
sl@0
|
73 |
}
|
sl@0
|
74 |
|
sl@0
|
75 |
// Get the encoder and decoder
|
sl@0
|
76 |
CASN1EncBitString* encoder = CASN1EncBitString::NewLC(bitStr, bitNum + 1);
|
sl@0
|
77 |
TASN1DecBitString decoder;
|
sl@0
|
78 |
|
sl@0
|
79 |
// Prepare an encode buffer
|
sl@0
|
80 |
TInt totalLength = encoder->LengthDER();
|
sl@0
|
81 |
HBufC8* encodeBuffer = HBufC8::NewMaxLC(totalLength);
|
sl@0
|
82 |
TPtr8 tEncodeBuf = encodeBuffer->Des();
|
sl@0
|
83 |
|
sl@0
|
84 |
// Write into the encode buffer
|
sl@0
|
85 |
TUint writeLength = 0;
|
sl@0
|
86 |
encoder->WriteDERL(tEncodeBuf, writeLength);
|
sl@0
|
87 |
|
sl@0
|
88 |
// Read it out again and check lengths plus encoded value
|
sl@0
|
89 |
TInt readLength = 0;
|
sl@0
|
90 |
HBufC8* decodeBuffer = decoder.ExtractOctetStringL(tEncodeBuf, readLength);
|
sl@0
|
91 |
CleanupStack::PushL(decodeBuffer);
|
sl@0
|
92 |
TPtr8 tDecodeBuf = decodeBuffer->Des();
|
sl@0
|
93 |
|
sl@0
|
94 |
if (writeLength != STATIC_CAST(TUint, readLength))
|
sl@0
|
95 |
{
|
sl@0
|
96 |
aConsole.Write(_L("ERROR!\n"));
|
sl@0
|
97 |
iASN1Action.ReportProgressL(KErrASN1EncodingError, 1, 1);
|
sl@0
|
98 |
pass = EFalse;
|
sl@0
|
99 |
}
|
sl@0
|
100 |
else if (bitNum >= 0 && valToEncode != tDecodeBuf[bitNum / 8])
|
sl@0
|
101 |
{
|
sl@0
|
102 |
aConsole.Write(_L("ENCODING ERROR!\n"));
|
sl@0
|
103 |
iASN1Action.ReportProgressL(KErrASN1EncodingError, 1, 1);
|
sl@0
|
104 |
pass = EFalse;
|
sl@0
|
105 |
}
|
sl@0
|
106 |
else
|
sl@0
|
107 |
{
|
sl@0
|
108 |
iASN1Action.ReportProgressL(KErrNone, bitNum + 1, 65);
|
sl@0
|
109 |
}
|
sl@0
|
110 |
|
sl@0
|
111 |
CleanupStack::PopAndDestroy(3, encoder); // decodeBuffer, encodeBuffer, encoder
|
sl@0
|
112 |
}
|
sl@0
|
113 |
|
sl@0
|
114 |
return pass;
|
sl@0
|
115 |
}
|
sl@0
|
116 |
|
sl@0
|
117 |
|