os/security/cryptoservices/certificateandkeymgmt/tasn1/testoid.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 object identifier encoding/decoding
sl@0
    16
*
sl@0
    17
*/
sl@0
    18
sl@0
    19
sl@0
    20
#include "testoid.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 <e32cons.h>
sl@0
    26
#include <e32math.h>
sl@0
    27
sl@0
    28
const TInt KMinOIDTerms = 2;
sl@0
    29
const TInt KMaxOIDTerms = KNumberOfIDs;
sl@0
    30
const TInt KMaxTermSize = 999999;
sl@0
    31
const TInt KMaxTermDigits = 6;
sl@0
    32
const TInt KMaxTermBits = 20;       // Bits in KMaxTermsSize
sl@0
    33
const TInt KMaxBufSize = (KMaxTermDigits * (KMaxOIDTerms - 2)) + 2 + KMaxOIDTerms;
sl@0
    34
sl@0
    35
CTestOID* CTestOID::NewL(CASN1NormalTest &aASN1Action)
sl@0
    36
	{
sl@0
    37
	CTestOID* test = new (ELeave) CTestOID(aASN1Action);
sl@0
    38
	return test;
sl@0
    39
	}
sl@0
    40
sl@0
    41
CTestOID::CTestOID(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action)
sl@0
    42
	{
sl@0
    43
	};
sl@0
    44
sl@0
    45
sl@0
    46
void CTestOID::GetName(TDes& aBuf)
sl@0
    47
	{
sl@0
    48
	aBuf.Copy(_L("Test Object Identifier"));
sl@0
    49
	}
sl@0
    50
sl@0
    51
sl@0
    52
void CTestOID::FillParameterArray(void)
sl@0
    53
	{
sl@0
    54
	iParameters->Append(CTestParameter::EString);
sl@0
    55
	}
sl@0
    56
sl@0
    57
TBool CTestOID::PerformTest(CConsoleBase& aConsole, HBufC &aTest, const TInt &aTestNumber, const TInt &aTotalTests)
sl@0
    58
	{
sl@0
    59
	// Get data, place in encoder
sl@0
    60
	CASN1EncObjectIdentifier* encoder = CASN1EncObjectIdentifier::NewLC(aTest);
sl@0
    61
sl@0
    62
	// Prepare a buffer
sl@0
    63
	TInt totalLength = encoder->LengthDER();
sl@0
    64
	HBufC8* buf = HBufC8::NewMaxLC(totalLength);
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 + check lengths
sl@0
    72
	TASN1DecObjectIdentifier decoder;
sl@0
    73
	TInt readLength = 0;
sl@0
    74
	HBufC* decodedData = decoder.DecodeDERL(tBuf, readLength);
sl@0
    75
	CleanupStack::PushL(decodedData);
sl@0
    76
	
sl@0
    77
	if (writeLength != STATIC_CAST(TUint, readLength) || aTest != *decodedData)
sl@0
    78
		{
sl@0
    79
		aConsole.Write(_L("ERROR!\nData to encode: "));
sl@0
    80
		aConsole.Write(aTest);
sl@0
    81
		aConsole.Write(_L("\nDecoded data: "));
sl@0
    82
		aConsole.Write(*decodedData);
sl@0
    83
		aConsole.Write(_L("\n"));
sl@0
    84
		OutputEncodingL(aConsole, tBuf);
sl@0
    85
		iASN1Action.ReportProgressL(KErrASN1EncodingError, aTestNumber, aTotalTests);
sl@0
    86
		CleanupStack::PopAndDestroy(3); // decodedData, buf, encoder
sl@0
    87
		return(EFalse);
sl@0
    88
		}
sl@0
    89
	else
sl@0
    90
		{
sl@0
    91
		iASN1Action.ReportProgressL(KErrNone, aTestNumber, aTotalTests);
sl@0
    92
		CleanupStack::PopAndDestroy(3); // decodedData, buf, encoder
sl@0
    93
		return(ETrue);
sl@0
    94
		}
sl@0
    95
	}
sl@0
    96
sl@0
    97
sl@0
    98
TBool CTestOID::PerformTestsL(CConsoleBase& aConsole)
sl@0
    99
	{
sl@0
   100
	CTestParameter* test;
sl@0
   101
	TInt totalTests, currentTest=0;
sl@0
   102
sl@0
   103
	if(!CountTests(totalTests)) return(EFalse);
sl@0
   104
sl@0
   105
	for(TInt pos = 0; pos < iValues->Count(); pos++)
sl@0
   106
		{
sl@0
   107
		test = (*iValues)[pos];
sl@0
   108
		switch(test->GetType())
sl@0
   109
			{
sl@0
   110
			case CTestParameter::EString :
sl@0
   111
				{
sl@0
   112
				CStringTestParameter *testString = REINTERPRET_CAST(CStringTestParameter*, test);
sl@0
   113
				HBufC* value = HBufC::NewLC(KMaxBufSize);
sl@0
   114
				TPtr tBuf = value->Des();
sl@0
   115
sl@0
   116
				testString->GetValue(tBuf);
sl@0
   117
				if(PerformTest(aConsole, *value, currentTest, totalTests))
sl@0
   118
					{
sl@0
   119
					currentTest++;
sl@0
   120
					CleanupStack::PopAndDestroy();
sl@0
   121
					}
sl@0
   122
				else
sl@0
   123
					{
sl@0
   124
					CleanupStack::PopAndDestroy();
sl@0
   125
					return(EFalse);
sl@0
   126
					}
sl@0
   127
				break;
sl@0
   128
				}
sl@0
   129
			case CTestParameter::ERandom :
sl@0
   130
				{
sl@0
   131
				CRandomTestParameter *testRandom = REINTERPRET_CAST(CRandomTestParameter*, test);
sl@0
   132
				HBufC* value;
sl@0
   133
					
sl@0
   134
				for(TInt test = 0; test <= testRandom->Interations(); test++)
sl@0
   135
					{
sl@0
   136
					value = HBufC::NewLC(KMaxBufSize);
sl@0
   137
sl@0
   138
					TPtr tBuf = value->Des();
sl@0
   139
					tBuf.AppendNum(static_cast<TUint>(Math::Random() % 3), EDecimal);
sl@0
   140
					tBuf.Append('.');
sl@0
   141
					tBuf.AppendNum(static_cast<TUint>(Math::Random() % 40), EDecimal);
sl@0
   142
sl@0
   143
					TInt targetTerms = (test % (1 + KMaxOIDTerms - KMinOIDTerms)) + KMinOIDTerms;
sl@0
   144
					for (TInt terms = 2; terms < targetTerms; ++terms)
sl@0
   145
						{
sl@0
   146
						TInt term = Math::Random() % (KMaxTermSize >> (test % KMaxTermBits));
sl@0
   147
						tBuf.Append('.');
sl@0
   148
						tBuf.AppendNum(term, EDecimal);
sl@0
   149
						}
sl@0
   150
					if(PerformTest(aConsole, *value, currentTest, totalTests))
sl@0
   151
						{
sl@0
   152
						CleanupStack::PopAndDestroy();
sl@0
   153
						currentTest++;
sl@0
   154
						}
sl@0
   155
					else
sl@0
   156
						{
sl@0
   157
						CleanupStack::PopAndDestroy();
sl@0
   158
						return(EFalse);
sl@0
   159
						}
sl@0
   160
					}
sl@0
   161
				break;
sl@0
   162
				}
sl@0
   163
			default:
sl@0
   164
				{
sl@0
   165
				return EFalse;
sl@0
   166
				}
sl@0
   167
			}
sl@0
   168
		}
sl@0
   169
	iASN1Action.ReportProgressL(KErrNone, totalTests, totalTests);
sl@0
   170
	return(ETrue);
sl@0
   171
	}
sl@0
   172