os/security/cryptoservices/certificateandkeymgmt/tasn1/testint.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/tasn1/testint.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,157 @@
     1.4 +/*
     1.5 +* Copyright (c) 2001-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 TInt encoding/decoding
    1.19 +*
    1.20 +*/
    1.21 +
    1.22 +
    1.23 +#include "testint.h"
    1.24 +#include "tasn1normaltest.h"
    1.25 +#include <asn1enc.h>
    1.26 +#include <asn1dec.h>
    1.27 +
    1.28 +#include <e32math.h>
    1.29 +#include <e32cons.h>
    1.30 +
    1.31 +
    1.32 +CTestInt* CTestInt::NewL(CASN1NormalTest &aASN1Action)
    1.33 +	{
    1.34 +	CTestInt* test = new (ELeave) CTestInt(aASN1Action);
    1.35 +	return test;
    1.36 +	}
    1.37 +
    1.38 +CTestInt::CTestInt(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action)
    1.39 +	{
    1.40 +	};
    1.41 +
    1.42 +
    1.43 +void CTestInt::GetName(TDes& aBuf)
    1.44 +	{
    1.45 +	aBuf.Copy(_L("Test TInt"));
    1.46 +	}
    1.47 +
    1.48 +void CTestInt::FillParameterArray(void)
    1.49 +	{
    1.50 +	iParameters->Append(CTestParameter::EInt);
    1.51 +	}
    1.52 +
    1.53 +
    1.54 +
    1.55 +TBool CTestInt::PerformTest(CConsoleBase& aConsole, const TInt &aTest, const TInt &aTestNumber, const TInt &aTotalTests)
    1.56 +	{
    1.57 +	// Choose value to encode
    1.58 +	TInt encodedValue;
    1.59 +
    1.60 +	encodedValue = aTest;
    1.61 +
    1.62 +	// Get encoder
    1.63 +	CASN1EncInt* encoder = CASN1EncInt::NewLC(encodedValue);
    1.64 +	
    1.65 +	// Prepare a buffer
    1.66 +	TUint length = encoder->LengthDER();
    1.67 +	HBufC8* buf = HBufC8::NewMaxLC(length);
    1.68 +	TPtr8 tBuf = buf->Des();
    1.69 +
    1.70 +	// Write into the buffer
    1.71 +	TUint writeLength = 0;
    1.72 +	encoder->WriteDERL(tBuf, writeLength);
    1.73 +	
    1.74 +	// Read it out again
    1.75 +	TASN1DecInteger decoder;
    1.76 +	TInt readLength = 0;
    1.77 +	TInt decodedValue = decoder.DecodeDERShortL(tBuf, readLength);
    1.78 +	
    1.79 +	// Check lengths of reads + values
    1.80 +	if ((writeLength != STATIC_CAST(TUint, readLength)) || (decodedValue != encodedValue))
    1.81 +		{
    1.82 +		aConsole.Printf(_L("\nERROR!  Problem integer: %d\n"), encodedValue);
    1.83 +		iASN1Action.ReportProgressL(KErrASN1EncodingError, aTestNumber, aTotalTests);
    1.84 +		CleanupStack::PopAndDestroy(2); // buf, encoder
    1.85 +		return(EFalse);
    1.86 +		}
    1.87 +	else
    1.88 +		{
    1.89 +		iASN1Action.ReportProgressL(KErrNone, aTestNumber, aTotalTests);
    1.90 +		CleanupStack::PopAndDestroy(2); // buf, encoder
    1.91 +		return(ETrue);
    1.92 +		}
    1.93 +	}
    1.94 +
    1.95 +TBool CTestInt::PerformTestsL(CConsoleBase& aConsole)
    1.96 +	{
    1.97 +	TInt nLow = Math::Random();
    1.98 +	TInt64 nHigh((TInt)Math::Random());
    1.99 +	TInt64 seed((nHigh << 32) + nLow);
   1.100 +	CTestParameter* test;
   1.101 +	TInt totalTests, currentTest=0;
   1.102 +
   1.103 +	if(!CountTests(totalTests)) return(EFalse);
   1.104 +
   1.105 +	for(TInt pos = 0; pos < iValues->Count(); pos++)
   1.106 +		{
   1.107 +		test = (*iValues)[pos];
   1.108 +		switch(test->GetType())
   1.109 +			{
   1.110 +			case CTestParameter::EInt :
   1.111 +				{
   1.112 +				CIntTestParameter *rangeInt = REINTERPRET_CAST(CIntTestParameter*, test);
   1.113 +
   1.114 +				if(!PerformTest(aConsole, rangeInt->Value(), currentTest, totalTests))
   1.115 +					{
   1.116 +					return(EFalse);
   1.117 +					}
   1.118 +				currentTest++;
   1.119 +				break;
   1.120 +				}
   1.121 +			case CTestParameter::EIntRange :
   1.122 +				{
   1.123 +				CIntRangeTestParameter *rangeInt = REINTERPRET_CAST(CIntRangeTestParameter*, test);
   1.124 +
   1.125 +				for(TInt test = rangeInt->Start(); test <= rangeInt->Finish(); test++)
   1.126 +					{
   1.127 +					if(!PerformTest(aConsole, test, currentTest, totalTests))
   1.128 +						{
   1.129 +						return(EFalse);
   1.130 +						}
   1.131 +					currentTest++;
   1.132 +					}
   1.133 +				break;
   1.134 +				}
   1.135 +			case CTestParameter::ERandom :
   1.136 +				{
   1.137 +				CRandomTestParameter *randomInt = REINTERPRET_CAST(CRandomTestParameter*, test);
   1.138 +				TInt encodedValue;
   1.139 +
   1.140 +				for(TInt test = 0; test <= randomInt->Interations(); test++)
   1.141 +					{
   1.142 +					encodedValue = Math::Rand(seed) >> (8 * (test % 4));
   1.143 +					if(!PerformTest(aConsole, encodedValue, currentTest, totalTests))
   1.144 +						{
   1.145 +						return(EFalse);
   1.146 +						}
   1.147 +					currentTest++;
   1.148 +					}
   1.149 +				break;
   1.150 +				}
   1.151 +			default:
   1.152 +				{
   1.153 +				return EFalse;
   1.154 +				}
   1.155 +			}
   1.156 +		}
   1.157 +	iASN1Action.ReportProgressL(KErrNone, totalTests, totalTests);
   1.158 +	return(ETrue);
   1.159 +	}
   1.160 +