os/security/cryptomgmtlibs/securitytestfw/test/testhandler2/tScriptSetup.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 1998-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
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#include "tScriptSetup.h"
sl@0
    20
#include "tScriptTests.h"
sl@0
    21
sl@0
    22
#include "t_testactionspec.h"
sl@0
    23
#include "t_input.h"
sl@0
    24
#include "t_tefinput.h"
sl@0
    25
#include "t_certstoreactionmemfail.h"
sl@0
    26
#include "tcancel.h"
sl@0
    27
#include "t_sleep.h"
sl@0
    28
#include "t_message.h"
sl@0
    29
#include "t_output.h"
sl@0
    30
#include "tTestSpec.h"
sl@0
    31
sl@0
    32
#include <s32file.h>
sl@0
    33
sl@0
    34
#include "t_logger.h"
sl@0
    35
sl@0
    36
_LIT8(KStartMemFail, "startmemfailure");
sl@0
    37
_LIT8(KStopMemFail, "stopmemfailure");
sl@0
    38
_LIT8(KCancelStart, "startcancellation");
sl@0
    39
_LIT8(KCancelStop, "stopcancellation");
sl@0
    40
_LIT8(KMessage, "message");
sl@0
    41
_LIT8(KSleep, "sleep");
sl@0
    42
sl@0
    43
#include "t_policy.h"
sl@0
    44
_LIT8(KPolicyTest, "policytest");
sl@0
    45
sl@0
    46
EXPORT_C CScriptSetup* CScriptSetup::NewLC(CConsoleBase* aConsole)
sl@0
    47
	{
sl@0
    48
	CScriptSetup* self = new (ELeave) CScriptSetup(aConsole);
sl@0
    49
	CleanupStack::PushL(self);
sl@0
    50
	self->ConstructL();
sl@0
    51
	return self;
sl@0
    52
	}
sl@0
    53
sl@0
    54
CScriptSetup::CScriptSetup(CConsoleBase* aConsole) : CTestSetup(aConsole)
sl@0
    55
	{
sl@0
    56
	}
sl@0
    57
sl@0
    58
EXPORT_C CScriptSetup::~CScriptSetup()
sl@0
    59
	{
sl@0
    60
	delete iTestInput;
sl@0
    61
	delete iScriptPath;
sl@0
    62
	}
sl@0
    63
sl@0
    64
EXPORT_C void CScriptSetup::SetupTestsL(RFs& aFs,
sl@0
    65
										CTestSpec& aTestSpec, 
sl@0
    66
										TScriptTests theTestTypes[], 
sl@0
    67
										const CTestHandlerSettings& aCommandLineSettings)
sl@0
    68
	{
sl@0
    69
	TInt pos = 0;
sl@0
    70
	TInt err = KErrNone;
sl@0
    71
	TBuf8<256> testCaseID;
sl@0
    72
	TBuf8<256> prevtestCaseID;
sl@0
    73
	TBuf8<512> scriptResult;
sl@0
    74
	TBuf8<512> actionType;
sl@0
    75
	TBool runtest = EFalse;
sl@0
    76
	TBool iniFile = EFalse;
sl@0
    77
	TBool startTest = EFalse;
sl@0
    78
	TBool endTest = EFalse;
sl@0
    79
	
sl@0
    80
	for (TInt i = 1 ; ; ++i)
sl@0
    81
		{
sl@0
    82
		TPtrC8 runTestStep;
sl@0
    83
		TPtrC8 testDescription;
sl@0
    84
		TPtrC8 actionData;
sl@0
    85
		
sl@0
    86
		if (iTefScript)
sl@0
    87
			{
sl@0
    88
			
sl@0
    89
			if(endTest)
sl@0
    90
				{
sl@0
    91
				startTest = EFalse;
sl@0
    92
				runtest = EFalse;
sl@0
    93
				testCaseID.Copy(Tefinput::ParseTestCaseID(*iTestInput, KStartTestStep, prevtestCaseID, pos, err, runtest, startTest));
sl@0
    94
				prevtestCaseID.Copy(testCaseID);
sl@0
    95
				}
sl@0
    96
			if(runtest)
sl@0
    97
				{
sl@0
    98
				if(endTest && startTest)
sl@0
    99
					{
sl@0
   100
					startTest = ETrue;
sl@0
   101
					}
sl@0
   102
				else
sl@0
   103
					{
sl@0
   104
					startTest = EFalse;
sl@0
   105
					}
sl@0
   106
				endTest = EFalse;
sl@0
   107
				testCaseID.Copy(prevtestCaseID);
sl@0
   108
				}
sl@0
   109
			else
sl@0
   110
				{
sl@0
   111
				testCaseID.Copy(Tefinput::ParseTestCaseID(*iTestInput, KStartTestStep, prevtestCaseID, pos, err, runtest, startTest));
sl@0
   112
				prevtestCaseID.Copy(testCaseID);
sl@0
   113
				}
sl@0
   114
			runTestStep.Set(Tefinput::ParseRunTestStep(*iTestInput, KRunTestStep, pos, err));
sl@0
   115
			endTest = Tefinput::EndTestCase(*iTestInput, runTestStep, KEndTestStep, pos);
sl@0
   116
			}
sl@0
   117
		else
sl@0
   118
			{
sl@0
   119
			actionData.Set(Input::ParseElement(*iTestInput, KActionStart, KActionEnd, pos, err));
sl@0
   120
			}
sl@0
   121
		if (err != KErrNone)
sl@0
   122
		  	{
sl@0
   123
			break;
sl@0
   124
			} 
sl@0
   125
			 
sl@0
   126
		TInt relativePos = 0;
sl@0
   127
		TInt scriptGroupings= DEFAULTGROUPING;
sl@0
   128
		TTestActionSpec actionSpec;
sl@0
   129
		//Parse the file to create action name, type , body and result
sl@0
   130
		if (iTefScript)
sl@0
   131
			{
sl@0
   132
			User::LeaveIfError(actionSpec.TEFInit(aFs, runTestStep, testCaseID, prevtestCaseID, iTefScript, runtest, iniFile, scriptResult, actionType, *iScriptPath, *iConsole, *iLogFile));
sl@0
   133
			}
sl@0
   134
		else
sl@0
   135
			{
sl@0
   136
			User::LeaveIfError(actionSpec.Init(actionData, relativePos, *iConsole, *iLogFile, scriptGroupings));
sl@0
   137
			}
sl@0
   138
		
sl@0
   139
		//Start off checking Exhaustive and Smoke flags, then calls groupings
sl@0
   140
		if (!CheckAllFlags(aCommandLineSettings, scriptGroupings))
sl@0
   141
			//The current test should not be run
sl@0
   142
			continue;
sl@0
   143
	
sl@0
   144
		
sl@0
   145
		CTestAction* newAction = 0;
sl@0
   146
		TRAP(err, newAction = CreateActionL(aFs, actionSpec, theTestTypes));
sl@0
   147
		
sl@0
   148
	
sl@0
   149
		
sl@0
   150
sl@0
   151
		if (err != KErrNone)
sl@0
   152
			{
sl@0
   153
			iLogFile->write(_L("CScriptSetup::CreateActionL failed: "));
sl@0
   154
			iLogFile->writeError(err);		
sl@0
   155
			iLogFile->writeNewLine();
sl@0
   156
			
sl@0
   157
			iLogFile->write(_L("Action name: "));
sl@0
   158
			iLogFile->writeString(actionSpec.iActionName);				
sl@0
   159
			iLogFile->writeNewLine();
sl@0
   160
sl@0
   161
			iLogFile->write(_L("Action type: "));
sl@0
   162
			iLogFile->writeString(actionSpec.iActionType);				
sl@0
   163
			iLogFile->writeNewLine();
sl@0
   164
			
sl@0
   165
			iLogFile->write(_L("1 tests failed out of 1\r\n"));
sl@0
   166
			
sl@0
   167
			User::Leave(err);
sl@0
   168
			}
sl@0
   169
		
sl@0
   170
		if(newAction->ScriptError() != CTestAction::ENone)
sl@0
   171
			{
sl@0
   172
			TBuf<KMaxErrorSize> scriptError;
sl@0
   173
			newAction->ScriptError(scriptError);
sl@0
   174
			
sl@0
   175
			iLogFile->write(_L("Script error \"%S\" in test %s\r\n"), &scriptError, i);
sl@0
   176
				
sl@0
   177
			delete newAction;
sl@0
   178
			User::Leave(KErrArgument);
sl@0
   179
			}
sl@0
   180
		
sl@0
   181
		CleanupStack::PushL(newAction);
sl@0
   182
		User::LeaveIfError(aTestSpec.AddNextTest(newAction));
sl@0
   183
		CleanupStack::Pop(newAction);
sl@0
   184
	
sl@0
   185
		if(iniFile)
sl@0
   186
			{
sl@0
   187
			CleanupStack::PopAndDestroy(1);
sl@0
   188
			}
sl@0
   189
		}
sl@0
   190
	}
sl@0
   191
sl@0
   192
EXPORT_C TBool CScriptSetup::InitialiseL(RFs &aFs, const TDesC& aDefaultScript, const TDesC& aDefaultLog, TBool aUseCommandLine)
sl@0
   193
	{
sl@0
   194
	// gets the script file argument
sl@0
   195
	iScriptPath = NULL;
sl@0
   196
	iTefScript = EFalse;
sl@0
   197
sl@0
   198
	if (aUseCommandLine)
sl@0
   199
		{
sl@0
   200
		iScriptPath = GetArgument();
sl@0
   201
		if(iScriptPath->Length()==0)
sl@0
   202
			{
sl@0
   203
			delete iScriptPath;  
sl@0
   204
			iScriptPath = NULL;
sl@0
   205
			}
sl@0
   206
		else
sl@0
   207
			{
sl@0
   208
			TPtr scriptFileNameDes = iScriptPath->Des();
sl@0
   209
			if (scriptFileNameDes.Right(8) == _L(".script1") || scriptFileNameDes.Right(7) == _L(".script"))
sl@0
   210
				{
sl@0
   211
				iTefScript = ETrue;
sl@0
   212
				}
sl@0
   213
			}
sl@0
   214
		}
sl@0
   215
sl@0
   216
	if(iScriptPath == NULL)
sl@0
   217
		{
sl@0
   218
		if(aDefaultScript.Length() == 0)
sl@0
   219
			{
sl@0
   220
			PRINTANDLOG(_L("No script file specified on command line and no default given"));
sl@0
   221
			return(EFalse);
sl@0
   222
			}
sl@0
   223
		else
sl@0
   224
			{
sl@0
   225
			iScriptPath = aDefaultScript.AllocLC();
sl@0
   226
			}
sl@0
   227
		};
sl@0
   228
sl@0
   229
	PRINTANDLOG1(_L("Script file: %S"), iScriptPath);
sl@0
   230
sl@0
   231
	// open the script file
sl@0
   232
	RFile scriptFile;
sl@0
   233
	TInt err = scriptFile.Open(aFs, *iScriptPath, EFileStream | EFileRead | EFileShareReadersOnly);
sl@0
   234
	if (err != KErrNone)
sl@0
   235
		{
sl@0
   236
		PRINTANDLOG1(_L("Error opening script file: %d"), err);
sl@0
   237
		delete iScriptPath;
sl@0
   238
		iScriptPath = NULL;
sl@0
   239
		return(EFalse);
sl@0
   240
		}
sl@0
   241
	CleanupClosePushL(scriptFile);
sl@0
   242
	
sl@0
   243
	TRAP(err, OpenLogFileL(aFs, 1, aDefaultLog, aUseCommandLine));	
sl@0
   244
	if (err != KErrNone)
sl@0
   245
		{
sl@0
   246
		PRINTANDLOG1(_L("Error opening log file: %d"), err);	
sl@0
   247
		delete iScriptPath;
sl@0
   248
		iScriptPath = NULL;
sl@0
   249
		CleanupStack::PopAndDestroy(1);// scripFile
sl@0
   250
		return(EFalse);
sl@0
   251
		}
sl@0
   252
		
sl@0
   253
	// gets size of script file
sl@0
   254
	TInt size;
sl@0
   255
	scriptFile.Size(size);
sl@0
   256
	CleanupStack::PopAndDestroy(); // scriptFile
sl@0
   257
		
sl@0
   258
	// reads script into iTestInput
sl@0
   259
	iTestInput = HBufC8::NewL(size);
sl@0
   260
	TPtr8 pInput(iTestInput->Des()); 
sl@0
   261
	pInput.SetLength(size); 
sl@0
   262
sl@0
   263
	RFileReadStream stream;
sl@0
   264
	User::LeaveIfError(stream.Open(aFs, *iScriptPath, EFileStream | EFileRead | EFileShareReadersOnly));
sl@0
   265
	CleanupClosePushL(stream);
sl@0
   266
	stream.ReadL(pInput, size);
sl@0
   267
	CleanupStack::PopAndDestroy(1); // stream
sl@0
   268
	return(ETrue);
sl@0
   269
	}
sl@0
   270
sl@0
   271
EXPORT_C CTestAction* CScriptSetup::CreateActionL(RFs& aFs,
sl@0
   272
												  const TTestActionSpec& aTestActionSpec,
sl@0
   273
												  const TScriptTests theTestTypes[]) const
sl@0
   274
	{
sl@0
   275
	// Instantiate built in test actions
sl@0
   276
	if (aTestActionSpec.iActionType == KStartMemFail)
sl@0
   277
		{
sl@0
   278
		return CMemFailStart::NewL(*iTestConsole, *iLogFile, aTestActionSpec);
sl@0
   279
		}
sl@0
   280
	else if (aTestActionSpec.iActionType == KStopMemFail)
sl@0
   281
		{
sl@0
   282
		return CMemFailStop::NewL(*iTestConsole, *iLogFile, aTestActionSpec);
sl@0
   283
		}
sl@0
   284
	else if (aTestActionSpec.iActionType == KCancelStart)
sl@0
   285
		{
sl@0
   286
		return CCancelStart::NewL(*iTestConsole, *iLogFile, aTestActionSpec);
sl@0
   287
		}
sl@0
   288
	else if (aTestActionSpec.iActionType == KCancelStop)
sl@0
   289
		{
sl@0
   290
		return CCancelStop::NewL(*iTestConsole, *iLogFile, aTestActionSpec);
sl@0
   291
		}
sl@0
   292
	else if (aTestActionSpec.iActionType == KMessage)
sl@0
   293
		{
sl@0
   294
		return CTestActionMessage::NewL(*iTestConsole, *iLogFile, aTestActionSpec);
sl@0
   295
		}
sl@0
   296
	else if (aTestActionSpec.iActionType == KSleep)
sl@0
   297
		{
sl@0
   298
		return CSleep::NewL(*iTestConsole, *iLogFile, aTestActionSpec);
sl@0
   299
		}
sl@0
   300
	else if (aTestActionSpec.iActionType == KPolicyTest)
sl@0
   301
		{
sl@0
   302
		return CPolicyTest::NewL(*iTestConsole, *iLogFile, aTestActionSpec);
sl@0
   303
		}
sl@0
   304
sl@0
   305
	// Look through the array of tests in order to find the correct class to return
sl@0
   306
	TInt loop=0;
sl@0
   307
	while (theTestTypes[loop].action!=NULL)
sl@0
   308
		{
sl@0
   309
		if (aTestActionSpec.iActionType == theTestTypes[loop].name)
sl@0
   310
			{
sl@0
   311
			return theTestTypes[loop].action(aFs, *iTestConsole, *iLogFile, aTestActionSpec);
sl@0
   312
			}
sl@0
   313
		loop++;
sl@0
   314
		}
sl@0
   315
sl@0
   316
	User::Leave(KErrNotSupported);
sl@0
   317
	return NULL;
sl@0
   318
	}