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