cryptoservices/certificateandkeymgmt/tasn1/testint.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 TInt encoding/decoding
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include "testint.h"
       
    21 #include "tasn1normaltest.h"
       
    22 #include <asn1enc.h>
       
    23 #include <asn1dec.h>
       
    24 
       
    25 #include <e32math.h>
       
    26 #include <e32cons.h>
       
    27 
       
    28 
       
    29 CTestInt* CTestInt::NewL(CASN1NormalTest &aASN1Action)
       
    30 	{
       
    31 	CTestInt* test = new (ELeave) CTestInt(aASN1Action);
       
    32 	return test;
       
    33 	}
       
    34 
       
    35 CTestInt::CTestInt(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action)
       
    36 	{
       
    37 	};
       
    38 
       
    39 
       
    40 void CTestInt::GetName(TDes& aBuf)
       
    41 	{
       
    42 	aBuf.Copy(_L("Test TInt"));
       
    43 	}
       
    44 
       
    45 void CTestInt::FillParameterArray(void)
       
    46 	{
       
    47 	iParameters->Append(CTestParameter::EInt);
       
    48 	}
       
    49 
       
    50 
       
    51 
       
    52 TBool CTestInt::PerformTest(CConsoleBase& aConsole, const TInt &aTest, const TInt &aTestNumber, const TInt &aTotalTests)
       
    53 	{
       
    54 	// Choose value to encode
       
    55 	TInt encodedValue;
       
    56 
       
    57 	encodedValue = aTest;
       
    58 
       
    59 	// Get encoder
       
    60 	CASN1EncInt* encoder = CASN1EncInt::NewLC(encodedValue);
       
    61 	
       
    62 	// Prepare a buffer
       
    63 	TUint length = encoder->LengthDER();
       
    64 	HBufC8* buf = HBufC8::NewMaxLC(length);
       
    65 	TPtr8 tBuf = buf->Des();
       
    66 
       
    67 	// Write into the buffer
       
    68 	TUint writeLength = 0;
       
    69 	encoder->WriteDERL(tBuf, writeLength);
       
    70 	
       
    71 	// Read it out again
       
    72 	TASN1DecInteger decoder;
       
    73 	TInt readLength = 0;
       
    74 	TInt decodedValue = decoder.DecodeDERShortL(tBuf, readLength);
       
    75 	
       
    76 	// Check lengths of reads + values
       
    77 	if ((writeLength != STATIC_CAST(TUint, readLength)) || (decodedValue != encodedValue))
       
    78 		{
       
    79 		aConsole.Printf(_L("\nERROR!  Problem integer: %d\n"), encodedValue);
       
    80 		iASN1Action.ReportProgressL(KErrASN1EncodingError, aTestNumber, aTotalTests);
       
    81 		CleanupStack::PopAndDestroy(2); // buf, encoder
       
    82 		return(EFalse);
       
    83 		}
       
    84 	else
       
    85 		{
       
    86 		iASN1Action.ReportProgressL(KErrNone, aTestNumber, aTotalTests);
       
    87 		CleanupStack::PopAndDestroy(2); // buf, encoder
       
    88 		return(ETrue);
       
    89 		}
       
    90 	}
       
    91 
       
    92 TBool CTestInt::PerformTestsL(CConsoleBase& aConsole)
       
    93 	{
       
    94 	TInt nLow = Math::Random();
       
    95 	TInt64 nHigh((TInt)Math::Random());
       
    96 	TInt64 seed((nHigh << 32) + nLow);
       
    97 	CTestParameter* test;
       
    98 	TInt totalTests, currentTest=0;
       
    99 
       
   100 	if(!CountTests(totalTests)) return(EFalse);
       
   101 
       
   102 	for(TInt pos = 0; pos < iValues->Count(); pos++)
       
   103 		{
       
   104 		test = (*iValues)[pos];
       
   105 		switch(test->GetType())
       
   106 			{
       
   107 			case CTestParameter::EInt :
       
   108 				{
       
   109 				CIntTestParameter *rangeInt = REINTERPRET_CAST(CIntTestParameter*, test);
       
   110 
       
   111 				if(!PerformTest(aConsole, rangeInt->Value(), currentTest, totalTests))
       
   112 					{
       
   113 					return(EFalse);
       
   114 					}
       
   115 				currentTest++;
       
   116 				break;
       
   117 				}
       
   118 			case CTestParameter::EIntRange :
       
   119 				{
       
   120 				CIntRangeTestParameter *rangeInt = REINTERPRET_CAST(CIntRangeTestParameter*, test);
       
   121 
       
   122 				for(TInt test = rangeInt->Start(); test <= rangeInt->Finish(); test++)
       
   123 					{
       
   124 					if(!PerformTest(aConsole, test, currentTest, totalTests))
       
   125 						{
       
   126 						return(EFalse);
       
   127 						}
       
   128 					currentTest++;
       
   129 					}
       
   130 				break;
       
   131 				}
       
   132 			case CTestParameter::ERandom :
       
   133 				{
       
   134 				CRandomTestParameter *randomInt = REINTERPRET_CAST(CRandomTestParameter*, test);
       
   135 				TInt encodedValue;
       
   136 
       
   137 				for(TInt test = 0; test <= randomInt->Interations(); test++)
       
   138 					{
       
   139 					encodedValue = Math::Rand(seed) >> (8 * (test % 4));
       
   140 					if(!PerformTest(aConsole, encodedValue, currentTest, totalTests))
       
   141 						{
       
   142 						return(EFalse);
       
   143 						}
       
   144 					currentTest++;
       
   145 					}
       
   146 				break;
       
   147 				}
       
   148 			default:
       
   149 				{
       
   150 				return EFalse;
       
   151 				}
       
   152 			}
       
   153 		}
       
   154 	iASN1Action.ReportProgressL(KErrNone, totalTests, totalTests);
       
   155 	return(ETrue);
       
   156 	}