os/security/cryptoservices/certificateandkeymgmt/tasn1/testoctetstr.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 octet string encoding/decoding
sl@0
    16
*
sl@0
    17
*/
sl@0
    18
sl@0
    19
sl@0
    20
#include "testoctetstr.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
const TInt KMaxStringLength = 1000;
sl@0
    29
sl@0
    30
CTestOctetString* CTestOctetString::NewL(CASN1NormalTest &aASN1Action)
sl@0
    31
	{
sl@0
    32
	CTestOctetString* test = new (ELeave) CTestOctetString(aASN1Action);
sl@0
    33
	test->ConstructL();
sl@0
    34
	return test;
sl@0
    35
	}
sl@0
    36
sl@0
    37
CTestOctetString::CTestOctetString(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action)
sl@0
    38
	{
sl@0
    39
	};
sl@0
    40
sl@0
    41
void CTestOctetString::ConstructL()	
sl@0
    42
	{
sl@0
    43
	toEncodeBuf = HBufC8::NewMaxLC(KMaxStringLength);
sl@0
    44
	encodedBuf = HBufC8::NewMaxLC(KMaxStringLength + 10); // extra 10 for tag/length
sl@0
    45
	CleanupStack::Pop(2);
sl@0
    46
	};
sl@0
    47
sl@0
    48
void CTestOctetString::FillParameterArray(void)
sl@0
    49
	{
sl@0
    50
	iParameters->Append(CTestParameter::EString);
sl@0
    51
	}
sl@0
    52
sl@0
    53
sl@0
    54
CTestOctetString::~CTestOctetString()	
sl@0
    55
	{
sl@0
    56
	delete toEncodeBuf;
sl@0
    57
	delete encodedBuf;
sl@0
    58
	}
sl@0
    59
sl@0
    60
void CTestOctetString::GetName(TDes& aBuf)
sl@0
    61
	{
sl@0
    62
	aBuf.Copy(_L("Test Octet String"));
sl@0
    63
	}
sl@0
    64
sl@0
    65
sl@0
    66
TBool CTestOctetString::PerformTest(CConsoleBase& aConsole, const TPtr8 &toEncodePtr, const TInt &aTestNumber, const TInt &aTotalTests)
sl@0
    67
	{
sl@0
    68
	TPtr8 encodedPtr = encodedBuf->Des();
sl@0
    69
sl@0
    70
	// Make the encoder
sl@0
    71
	CASN1EncOctetString* encoder = CASN1EncOctetString::NewLC(toEncodePtr);
sl@0
    72
sl@0
    73
	// Do the encoding
sl@0
    74
	TUint writeLength = 0;
sl@0
    75
	encoder->WriteDERL(encodedPtr, writeLength);
sl@0
    76
	
sl@0
    77
	// Read it out again
sl@0
    78
	TASN1DecOctetString decoder;
sl@0
    79
	TInt readLength = 0;
sl@0
    80
	HBufC8* readBuf = decoder.DecodeDERL(encodedPtr, readLength);
sl@0
    81
	CleanupStack::PushL(readBuf);
sl@0
    82
	
sl@0
    83
	// Check lengths of reads + values
sl@0
    84
	if ((writeLength != STATIC_CAST(TUint, readLength)) || (*readBuf != toEncodePtr))
sl@0
    85
		{
sl@0
    86
		aConsole.Printf(_L("\nERROR!\n"));
sl@0
    87
		iASN1Action.ReportProgressL(KErrASN1EncodingError, aTestNumber, aTotalTests);
sl@0
    88
		CleanupStack::PopAndDestroy(2); // encoder, readBuf
sl@0
    89
		return(ETrue);
sl@0
    90
		}
sl@0
    91
	else
sl@0
    92
		{
sl@0
    93
		iASN1Action.ReportProgressL(KErrNone, aTestNumber, aTotalTests);
sl@0
    94
		CleanupStack::PopAndDestroy(2); // encoder, readBuf
sl@0
    95
		return(ETrue);
sl@0
    96
		}
sl@0
    97
	}
sl@0
    98
sl@0
    99
sl@0
   100
TBool CTestOctetString::PerformTestsL(CConsoleBase& aConsole)
sl@0
   101
	{
sl@0
   102
	TInt nLow = Math::Random();
sl@0
   103
	TInt64 nHigh((TInt)Math::Random());	
sl@0
   104
	TInt64 seed((nHigh << 32) + nLow);
sl@0
   105
	CTestParameter* test;
sl@0
   106
	TInt totalTests, currentTest=0;
sl@0
   107
sl@0
   108
	if(!CountTests(totalTests)) return(EFalse);
sl@0
   109
sl@0
   110
	for(TInt pos = 0; pos < iValues->Count(); pos++)
sl@0
   111
		{
sl@0
   112
		test = (*iValues)[pos];
sl@0
   113
		switch(test->GetType())
sl@0
   114
			{
sl@0
   115
			case CTestParameter::EString :
sl@0
   116
				{
sl@0
   117
				CStringTestParameter *testString = REINTERPRET_CAST(CStringTestParameter*, test);
sl@0
   118
				HBufC* value = HBufC::NewLC(KMaxStringLength);
sl@0
   119
				TPtr tBuf = value->Des();
sl@0
   120
sl@0
   121
				testString->GetValue(tBuf);
sl@0
   122
sl@0
   123
				if((tBuf.Length() % 2) != 0)
sl@0
   124
					User::Leave(KErrArgument);
sl@0
   125
sl@0
   126
				TPtr8 toEncodePtr(toEncodeBuf->Des());
sl@0
   127
				TUint theOctet;
sl@0
   128
				TInt octetPos=0;
sl@0
   129
sl@0
   130
				for(TInt octet = 0; octet < tBuf.Length(); octet+=2)
sl@0
   131
					{
sl@0
   132
					TLex lex(tBuf.Mid(octet, 2));
sl@0
   133
					
sl@0
   134
					if(lex.Val(theOctet, EHex)!=KErrNone)
sl@0
   135
						User::Leave(KErrArgument);
sl@0
   136
					toEncodePtr[octetPos++] = STATIC_CAST(TUint8, theOctet);
sl@0
   137
					};
sl@0
   138
				if(PerformTest(aConsole, toEncodePtr, currentTest, totalTests))
sl@0
   139
					{
sl@0
   140
					CleanupStack::PopAndDestroy();
sl@0
   141
					currentTest++;
sl@0
   142
					}
sl@0
   143
				else
sl@0
   144
					{
sl@0
   145
					CleanupStack::PopAndDestroy();
sl@0
   146
					return(EFalse);
sl@0
   147
					}
sl@0
   148
				break;
sl@0
   149
				}
sl@0
   150
			case CTestParameter::ERandom :
sl@0
   151
				{
sl@0
   152
				CRandomTestParameter *randomInt = REINTERPRET_CAST(CRandomTestParameter*, test);
sl@0
   153
sl@0
   154
				for (TInt count = 0; count <= randomInt->Interations(); ++count)
sl@0
   155
					{
sl@0
   156
					// Descriptor for part of buffer we'll use; fill with noise
sl@0
   157
					TInt toEncodeLength = count % KMaxStringLength;
sl@0
   158
					TPtr8 toEncodePtr(toEncodeBuf->Des());
sl@0
   159
					for (TInt i = 0; i < toEncodeLength; ++i)
sl@0
   160
						{
sl@0
   161
						toEncodePtr[i] = STATIC_CAST(TUint8, Math::Rand(seed));
sl@0
   162
						}
sl@0
   163
					if(!PerformTest(aConsole, toEncodePtr, currentTest, totalTests))
sl@0
   164
						return(EFalse);
sl@0
   165
					currentTest++;
sl@0
   166
					}
sl@0
   167
sl@0
   168
				break;
sl@0
   169
				}
sl@0
   170
			default:
sl@0
   171
				{
sl@0
   172
				return EFalse;
sl@0
   173
				}
sl@0
   174
			}
sl@0
   175
		}
sl@0
   176
	iASN1Action.ReportProgressL(KErrNone, totalTests, totalTests);
sl@0
   177
	return(ETrue);
sl@0
   178
	};
sl@0
   179