cryptoservices/certificateandkeymgmt/tasn1/testbitstr.cpp
changeset 0 2c201484c85f
child 8 35751d3474b7
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /*
       
     2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     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".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 * Implementation for testing bit string encoding/decoding
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include "testbitstr.h"
       
    21 #include "tasn1normaltest.h"
       
    22 #include <asn1enc.h>
       
    23 #include <asn1dec.h>
       
    24 
       
    25 #include <e32cons.h>
       
    26 
       
    27 CTestBitStr* CTestBitStr::NewL(CASN1NormalTest &aASN1Action)
       
    28 	{
       
    29 	CTestBitStr* test = new (ELeave) CTestBitStr(aASN1Action);
       
    30 	return test;
       
    31 	}
       
    32 
       
    33 CTestBitStr::CTestBitStr(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action)
       
    34 	{
       
    35 	};
       
    36 
       
    37 void CTestBitStr::GetName(TDes& aBuf)
       
    38 	{
       
    39 	aBuf.Copy(_L("Test Bit String"));
       
    40 	}
       
    41 
       
    42 TBool CTestBitStr::PerformTestsL(CConsoleBase& aConsole)
       
    43 	{
       
    44 	TBool pass = ETrue;
       
    45 	
       
    46 	
       
    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++)
       
    51 		{
       
    52 		TBuf8<8> bitStr;				
       
    53 		TUint numOctets;
       
    54 		
       
    55 	    if (bitNum >= 0)
       
    56 	    	{
       
    57 	    	numOctets = 1 + (bitNum / 8);
       
    58 	    	}
       
    59 	    else 
       
    60 	    	{
       
    61 	    	numOctets = 0;
       
    62 	    	}
       
    63 		
       
    64 		bitStr.SetLength(numOctets);
       
    65 		bitStr.FillZ();
       
    66 				
       
    67 		TUint8 valToEncode = 0;
       
    68 		if (bitNum >= 0 )
       
    69 			{
       
    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;
       
    73 			}
       
    74 			
       
    75 		// Get the encoder and decoder
       
    76 		CASN1EncBitString* encoder = CASN1EncBitString::NewLC(bitStr, bitNum + 1);
       
    77 		TASN1DecBitString decoder;
       
    78 
       
    79 		// Prepare an encode buffer
       
    80 		TInt totalLength = encoder->LengthDER();
       
    81 		HBufC8* encodeBuffer = HBufC8::NewMaxLC(totalLength);		
       
    82 		TPtr8 tEncodeBuf = encodeBuffer->Des();		
       
    83 	
       
    84 		// Write into the encode buffer
       
    85 		TUint writeLength = 0;
       
    86 		encoder->WriteDERL(tEncodeBuf, writeLength);
       
    87 
       
    88 		// Read it out again and check lengths plus encoded value
       
    89 		TInt readLength = 0;
       
    90 		HBufC8* decodeBuffer = decoder.ExtractOctetStringL(tEncodeBuf, readLength);
       
    91 		CleanupStack::PushL(decodeBuffer);
       
    92 		TPtr8 tDecodeBuf = decodeBuffer->Des();
       
    93 						
       
    94 		if (writeLength != STATIC_CAST(TUint, readLength))
       
    95 			{
       
    96 			aConsole.Write(_L("ERROR!\n"));
       
    97 			iASN1Action.ReportProgressL(KErrASN1EncodingError, 1, 1);
       
    98 			pass = EFalse;			
       
    99 			}
       
   100 		else if (bitNum >= 0 && valToEncode != tDecodeBuf[bitNum / 8])
       
   101 			{
       
   102 			aConsole.Write(_L("ENCODING ERROR!\n"));
       
   103 			iASN1Action.ReportProgressL(KErrASN1EncodingError, 1, 1);
       
   104 			pass = EFalse;			
       
   105 			}			
       
   106 		else
       
   107 			{
       
   108 			iASN1Action.ReportProgressL(KErrNone, bitNum + 1, 65);
       
   109 			}	
       
   110 			
       
   111 		CleanupStack::PopAndDestroy(3, encoder); // decodeBuffer, encodeBuffer, encoder			
       
   112 		}
       
   113 		
       
   114 	return pass;
       
   115 	}