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