cryptoservices/certificateandkeymgmt/tasn1/testboolean.cpp
changeset 0 2c201484c85f
child 8 35751d3474b7
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /*
       
     2 * Copyright (c) 2001-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 boolean object encoding/decoding
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include "testboolean.h"
       
    21 #include "tasn1normaltest.h"
       
    22 #include <asn1enc.h>
       
    23 #include <asn1dec.h>
       
    24 
       
    25 #include <e32cons.h>
       
    26 
       
    27 CTestBoolean* CTestBoolean::NewL(CASN1NormalTest &aASN1Action)
       
    28 	{
       
    29 	CTestBoolean* test = new (ELeave) CTestBoolean(aASN1Action);
       
    30 	return test;
       
    31 	}
       
    32 
       
    33 CTestBoolean::CTestBoolean(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action)
       
    34 	{
       
    35 	};
       
    36 
       
    37 void CTestBoolean::GetName(TDes& aBuf)
       
    38 	{
       
    39 	aBuf.Copy(_L("Test Boolean"));
       
    40 	}
       
    41 
       
    42 void CTestBoolean::FillParameterArray(void)
       
    43 	{
       
    44 	iParameters->Append(CTestParameter::EInt);
       
    45 	}
       
    46 
       
    47  
       
    48 TBool CTestBoolean::PerformTest(CConsoleBase& aConsole, const TBool& aTest)
       
    49 	{
       
    50 	// Make the encoder
       
    51 	CASN1EncBoolean* encoder = CASN1EncBoolean::NewLC(aTest);
       
    52 
       
    53 	// Prepare a buffer
       
    54 	TInt length = encoder->LengthDER();
       
    55 	HBufC8* buf = HBufC8::NewMaxLC(length);
       
    56 	TPtr8 tBuf = buf->Des();
       
    57 
       
    58 	// Write into the buffer
       
    59 	TUint writeLength = 0;
       
    60 	encoder->WriteDERL(tBuf, writeLength);
       
    61 	
       
    62 	// Read it out again + check lengths
       
    63 	TASN1DecBoolean decoder;
       
    64 	TInt readLength = 0;
       
    65 	TBool decodedValue = decoder.DecodeDERL(tBuf, readLength);
       
    66 	
       
    67 	if (writeLength != STATIC_CAST(TUint, readLength) || decodedValue != aTest)
       
    68 		{
       
    69 		aConsole.Write(_L("ERROR!\n"));
       
    70 		iASN1Action.ReportProgressL(KErrASN1EncodingError, 1, 1);
       
    71 		CleanupStack::PopAndDestroy(2); // buf, encoder
       
    72 		return(EFalse);
       
    73 		}
       
    74 	else
       
    75 		{
       
    76 		iASN1Action.ReportProgressL(KErrNone, 1, 1);
       
    77 		CleanupStack::PopAndDestroy(2); // buf, encoder
       
    78 		return(ETrue);
       
    79 		}
       
    80 	}
       
    81 
       
    82 
       
    83 TBool CTestBoolean::PerformTestsL(CConsoleBase& aConsole)
       
    84 	{
       
    85 	CTestParameter* test;
       
    86 	TInt totalTests;
       
    87 
       
    88 	if(!CountTests(totalTests)) return(EFalse);
       
    89 
       
    90 	for(TInt pos = 0; pos < iValues->Count(); pos++)
       
    91 		{
       
    92 		test = (*iValues)[pos];
       
    93 
       
    94 		switch(test->GetType())
       
    95 			{
       
    96 			case CTestParameter::EInt :
       
    97 				{
       
    98 				CIntTestParameter *rangeInt = REINTERPRET_CAST(CIntTestParameter*, test);
       
    99 
       
   100 				if(!PerformTest(aConsole, rangeInt->Value()))
       
   101 					return(EFalse);
       
   102 				break;
       
   103 				}
       
   104 			case CTestParameter::EIntRange :
       
   105 				{
       
   106 				CIntRangeTestParameter *rangeInt = REINTERPRET_CAST(CIntRangeTestParameter*, test);
       
   107 
       
   108 				for(TInt test = rangeInt->Start(); test <= rangeInt->Finish(); test++)
       
   109 					{
       
   110 					if(!PerformTest(aConsole, test))
       
   111 						return(EFalse);
       
   112 					}
       
   113 				break;
       
   114 				}
       
   115 			default:
       
   116 				{
       
   117 				return EFalse;
       
   118 				}
       
   119 			}
       
   120 		}
       
   121 	iASN1Action.ReportProgressL(KErrNone, totalTests, totalTests);
       
   122 	return(ETrue);
       
   123 	}