cryptoservices/certificateandkeymgmt/tasn1/testbase.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 * Defines methods common to all test classes
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include <e32cons.h>
       
    21 #include "testbase.h"
       
    22 #include "t_input.h"
       
    23 
       
    24 _LIT(KParametersStart, "<parameters>");
       
    25 _LIT(KValuesStart, "<values>");
       
    26 _LIT(KRandomStart, "<random>");
       
    27 const TInt KMaxValuesSize = 1024;
       
    28 
       
    29 
       
    30 CTestBase::CTestBase(CASN1NormalTest &aASN1Action)
       
    31 : iASN1Action(aASN1Action)
       
    32 	{
       
    33 	}
       
    34 
       
    35 CTestBase::~CTestBase()
       
    36 	{
       
    37 	iParameters->Close();
       
    38 	delete iParameters;
       
    39 	iValues->ResetAndDestroy();
       
    40 	delete iValues;
       
    41 	};
       
    42 
       
    43 CTestAction::TScriptError CTestBase::ConstructL(const TTestActionSpec& aTestActionSpec)
       
    44 	{
       
    45 	CTestAction::TScriptError syntaxError;
       
    46 	iParameters = new (ELeave)RArray<CTestParameter::TType>;
       
    47 	iValues = new (ELeave)RPointerArray<CTestParameter>;
       
    48 	FillParameterArray();
       
    49 
       
    50 	HBufC* aBody = HBufC::NewLC(aTestActionSpec.iActionBody.Length());
       
    51 	
       
    52 	aBody->Des().Copy(aTestActionSpec.iActionBody);
       
    53 	TPtrC parameters(Input::ParseElement(*aBody, KParametersStart));
       
    54 
       
    55 	syntaxError = CheckValueParametersL(parameters);
       
    56 	if(syntaxError==CTestAction::ENone)
       
    57 		syntaxError = CheckRandomParametersL(parameters);
       
    58 
       
    59 	CleanupStack::PopAndDestroy();
       
    60 	return(syntaxError);
       
    61 	}
       
    62 
       
    63 
       
    64 CTestAction::TScriptError CTestBase::CheckValueParametersL(const TDesC& aParameters)
       
    65 	{
       
    66 	TInt dummyPos = 0, valuePos = 0, lastDummy = 0;
       
    67 	TBuf<KMaxValueSize> singleValue;
       
    68 	CTestParameter *testParameter=0;
       
    69 	TBuf<KMaxValuesSize> values;
       
    70 
       
    71 	// finds values to run test with
       
    72 	do
       
    73 		{
       
    74 		lastDummy = dummyPos;
       
    75 		values = Input::ParseElement(aParameters, KValuesStart, dummyPos);
       
    76 
       
    77 		// finds the next group of value
       
    78 		if(lastDummy != dummyPos)
       
    79 			{
       
    80 			// parses each value in group with its expeced type
       
    81 			for(TInt parameter = 0; parameter < iParameters->Count(); parameter++)
       
    82 				{
       
    83 				testParameter = NULL;
       
    84 				// checks if its last parameter
       
    85 				if(parameter < iParameters->Count() - 1)
       
    86 					{
       
    87 					// finds it position
       
    88 					valuePos = values.Find((_L(",")));
       
    89 					if(valuePos > 0)
       
    90 						{
       
    91 						// fills singleValue with value
       
    92 						if(valuePos > KMaxValueSize)
       
    93 							{
       
    94 							iSyntaxErrorDescription.Copy(_L("value too long"));
       
    95 							return(CTestAction::ESyntax);
       
    96 							}
       
    97 						singleValue.Copy(values.Left(valuePos));
       
    98 						values = values.Mid(valuePos + 1);
       
    99 						}
       
   100 					else
       
   101 						{
       
   102 						// couldnt find it there is a problem
       
   103 						iSyntaxErrorDescription.Copy(_L("Invalid number of parameters"));
       
   104 						return(CTestAction::ESyntax);
       
   105 						};
       
   106 					}
       
   107 				else
       
   108 					{
       
   109 					// its the last value fill singleValue with it
       
   110 					if(values.Length() > KMaxValueSize)
       
   111 						{
       
   112 						iSyntaxErrorDescription.Copy(_L("value too long"));
       
   113 						return(CTestAction::ESyntax);
       
   114 						}
       
   115 					singleValue.Copy(values);
       
   116 					};
       
   117 
       
   118 				// trims any white space
       
   119 				singleValue.Trim();
       
   120 				// checks what type its supposed to be
       
   121 				switch((*iParameters)[parameter])
       
   122 					{
       
   123 					case CTestParameter::EInt:
       
   124 						{
       
   125 							// its an int, check to see if its a range
       
   126 							if(singleValue.Find(_L("..")) == KErrNotFound)
       
   127 								testParameter = CIntTestParameter::NewL(singleValue);
       
   128 							else
       
   129 								testParameter = CIntRangeTestParameter::NewL(singleValue);
       
   130 							break;
       
   131 						}
       
   132 					case CTestParameter::EString:
       
   133 						{
       
   134 							// its a string
       
   135 							testParameter = CStringTestParameter::NewL(singleValue);
       
   136 							break;
       
   137 						}
       
   138 					case CTestParameter::EIntRange:
       
   139 					case CTestParameter::ERandom:
       
   140 						break;	//	Nothing to do
       
   141 					};
       
   142 				// if testparamer is found add it to the list
       
   143 				if(testParameter)
       
   144 					{	
       
   145 					if(testParameter->iValid)
       
   146 						iValues->Append(testParameter);
       
   147 					else
       
   148 						{
       
   149 						delete testParameter;
       
   150 						iSyntaxErrorDescription.Copy(_L("Invalid value "));
       
   151 						iSyntaxErrorDescription.Append(singleValue);
       
   152 						return(CTestAction::ESyntax);
       
   153 						}
       
   154 					}
       
   155 				};
       
   156 			}
       
   157 		}
       
   158 	while(lastDummy != dummyPos);
       
   159 
       
   160 	return(CTestAction::ENone);
       
   161 	};
       
   162 
       
   163 CTestAction::TScriptError CTestBase::CheckRandomParametersL(const TDesC& aParameters)
       
   164 	{
       
   165 	TInt dummyPos = 0, lastDummy = 0;
       
   166 	TBuf<KMaxValueSize> singleValue;
       
   167 	CTestParameter *testParameter=0;
       
   168 	TBuf<KMaxValuesSize> values;
       
   169 
       
   170 	// finds any random tests
       
   171 	do
       
   172 		{
       
   173 		lastDummy = dummyPos;
       
   174 		values = Input::ParseElement(aParameters, KRandomStart, dummyPos);
       
   175 
       
   176 		if(lastDummy != dummyPos)
       
   177 			{
       
   178 			if(values.Length() > KMaxValueSize)
       
   179 				{
       
   180 				iSyntaxErrorDescription.Copy(_L("value too long"));
       
   181 				return(CTestAction::ESyntax);
       
   182 				}
       
   183 			singleValue.Copy(values);
       
   184 			// found one create a random parameter with its interators
       
   185 			testParameter = CRandomTestParameter::NewL(singleValue);
       
   186 			// adds it to list
       
   187 			if(testParameter)
       
   188 				{	
       
   189 				if(testParameter->iValid)
       
   190 					iValues->Append(testParameter);
       
   191 				else
       
   192 					{
       
   193 					delete testParameter;
       
   194 					iSyntaxErrorDescription.Copy(_L("Invalid value "));
       
   195 					iSyntaxErrorDescription.Append(singleValue);
       
   196 					return(CTestAction::ESyntax);
       
   197 					}
       
   198 				}
       
   199 			}
       
   200 		}
       
   201 	while(lastDummy != dummyPos);
       
   202 
       
   203 	return(CTestAction::ENone);
       
   204 	};
       
   205 
       
   206 void CTestBase::OutputEncodingL(CConsoleBase& aConsole, TDesC8& aData)
       
   207 	{
       
   208 	aConsole.Printf(_L("Encoding: Length = "));
       
   209 	TBuf<20> bits;
       
   210 	bits.AppendNum(aData.Length());
       
   211 	bits.Append(_L(", data = "));
       
   212 	aConsole.Printf(bits);
       
   213 	
       
   214 	TInt size = aData.Length();
       
   215 	for (TInt i = 0; i < size; ++i)
       
   216 		{
       
   217 		TBuf<10> tbuf;
       
   218 		tbuf.AppendNumFixedWidth(aData[i], EHex, 2);
       
   219 		aConsole.Printf(tbuf);
       
   220 		}
       
   221 
       
   222 	aConsole.Printf(_L("\n"));
       
   223 	}
       
   224 
       
   225 TBool CTestBase::CountTests(TInt &totalTests)
       
   226 	{
       
   227 	CTestParameter* test;
       
   228 	TInt totalRandomTests = 0;
       
   229 	TInt totalParameters = 0;
       
   230 	TInt totalRangeTests = 0;
       
   231 
       
   232 	totalTests=0;
       
   233 
       
   234 	// counts number of tests to do for type
       
   235 	for(TInt pos = 0; pos < iValues->Count(); pos++)
       
   236 		{
       
   237 		test = (*iValues)[pos];
       
   238 		switch(test->GetType())
       
   239 			{
       
   240 			case CTestParameter::EInt :
       
   241 			case CTestParameter::EString :
       
   242 				{
       
   243 				totalTests++;
       
   244 				totalParameters++;
       
   245 				break;
       
   246 				}
       
   247 			case CTestParameter::EIntRange :
       
   248 				{
       
   249 				CIntRangeTestParameter *rangeInt = REINTERPRET_CAST(CIntRangeTestParameter*, test);
       
   250 				totalRangeTests += rangeInt->Range();
       
   251 				totalParameters++;
       
   252 				break;
       
   253 				}
       
   254 			case CTestParameter::ERandom :
       
   255 				{
       
   256 				CRandomTestParameter *randomInt = REINTERPRET_CAST(CRandomTestParameter*, test);
       
   257 				
       
   258 				totalRandomTests+= randomInt->Interations();
       
   259 				break;
       
   260 				}
       
   261 			};
       
   262 		};
       
   263 
       
   264 	// only count each test once, remember each test can only have one parameter field of 
       
   265 	// range or random
       
   266 	totalTests = totalRandomTests + totalRangeTests + (totalParameters / iParameters->Count());
       
   267 	// checks if tests correct number of parameters for type
       
   268 	return((totalParameters %  iParameters->Count()) == 0);
       
   269 	};