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