networkingtestandutils/networkingintegrationtest/scheduleTest/script.cpp
changeset 0 af10295192d8
equal deleted inserted replaced
-1:000000000000 0:af10295192d8
       
     1 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // This module contains CScript class
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file Script.cpp
       
    20 */
       
    21 
       
    22 // system includes
       
    23 #include <e32base.h>
       
    24 #include <e32cons.h>
       
    25 #include "f32file.h"
       
    26 
       
    27 // test system includes
       
    28 #include "../inc/Log.h"
       
    29 #include "../inc/TestUtils.h"
       
    30 #include "../inc/TestStep.h"
       
    31 #include "../inc/TestSuite.h"
       
    32 #include "script.h"
       
    33 #include "parseline.h"
       
    34 
       
    35 /** 
       
    36 Script files can reference other script files
       
    37 MAX_DEPTH limits the number of references
       
    38 This is to catch accidental circular references in script files
       
    39 which would otherwise cause the system to continue until all
       
    40 memory had be used making more CScript objects 
       
    41 the maximum number of script files
       
    42 @internalComponent
       
    43 */
       
    44 #define MAX_DEPTH 100
       
    45 
       
    46 /** 
       
    47 Holds press any key string constant 
       
    48 @internalComponent
       
    49 */
       
    50 _LIT(KTxtPressAnyKey,"[press any key to continue]\n");
       
    51 
       
    52 /** 
       
    53 Holds string constant that displys error message 
       
    54 @internalComponent
       
    55 */
       
    56 _LIT(KTxtBreakOnError,"The test has failed, press X to terminate this test\n [press any other key to continue]\n");
       
    57 
       
    58 /** 
       
    59 Global data
       
    60 count of how deep in script files parser is
       
    61 this is to check against infinite recursion
       
    62 @internalComponent
       
    63 */
       
    64 GLDEF_D static TInt ScriptDepth = 0;			
       
    65 
       
    66 
       
    67 
       
    68 CScript::CScript()
       
    69 /**
       
    70 constructor
       
    71 */
       
    72 {}
       
    73 
       
    74 void CScript::ConstructL( void )
       
    75 /**
       
    76 second phase constructor
       
    77 */
       
    78 	{
       
    79 	iParse = CParseLine::NewL(this);
       
    80 	
       
    81 	iParseLineOwner = ETrue;
       
    82 	iPauseAtEnd = EFalse;
       
    83 	}
       
    84 
       
    85 CScript* CScript::NewL( void )
       
    86 /**
       
    87 NewL constructor
       
    88 */
       
    89 	{
       
    90 	CScript * self = new(ELeave) CScript;
       
    91 	CleanupStack::PushL(self);
       
    92 	self->ConstructL();
       
    93 	CleanupStack::Pop();
       
    94 	return self;
       
    95 	}
       
    96 
       
    97 void CScript::ConstructL( CParseLine * aParse )
       
    98 /**
       
    99 Standard Symbian format second phase constructor.
       
   100 */
       
   101 	{
       
   102 	iParse = aParse;
       
   103 
       
   104 	iPauseAtEnd = EFalse;
       
   105 
       
   106 	}
       
   107 
       
   108 CScript* CScript::NewL( CParseLine * aParse )
       
   109 /**
       
   110 Standard Symbian format constructor.
       
   111 
       
   112 @param aParse A parse line object to be used for decoding the script.
       
   113 @returns A pointer to the new CScript object.
       
   114 */
       
   115 	{
       
   116 	CScript * self = new(ELeave) CScript;
       
   117 	CleanupStack::PushL(self);
       
   118 	self->ConstructL(aParse);
       
   119 	CleanupStack::Pop();
       
   120 	return self;
       
   121 	}
       
   122 
       
   123 
       
   124 CScript::~CScript( )
       
   125 /**
       
   126 destructor deletes the script buffer.
       
   127 */
       
   128 	{
       
   129 	// delete scriptbuffer
       
   130 	delete ipScriptBuffer;
       
   131 	
       
   132 	if (iParseLineOwner) delete iParse;
       
   133 	}
       
   134 
       
   135 
       
   136 bool CScript::OpenScriptFile(const TFileName &aScriptFileName)
       
   137 /**
       
   138 Read in the test script file. 
       
   139 
       
   140 @param aScriptFileName The script file name. If no extension is supplied .script will
       
   141 be appended.
       
   142 @return ETrue the script was read ok, EFalse the script file could not be found.
       
   143 */
       
   144 	{
       
   145 	// get the full pathname default drive name and extension
       
   146 	_LIT(Kdefault,"C:\\xx.script"); 
       
   147 	TParse ScriptFileName;
       
   148 	TInt returnCode = ScriptFileName.Set( aScriptFileName, &Kdefault, NULL );
       
   149 	if (returnCode!=KErrNone)
       
   150 		{
       
   151 		pLogSystem->LogExtra(((TText8*)(__FILE__)), (__LINE__), ESevrErr,
       
   152 			_L("Failed to open script file: %S"),&ScriptFileName.FullName());
       
   153 		Pause();
       
   154 		return false;
       
   155 		}
       
   156 
       
   157 	TFileName scriptFileName = aScriptFileName;
       
   158 
       
   159 	if ( ScriptDepth++ > MAX_DEPTH )
       
   160 		{
       
   161 		// prevent the parser from recursing forever
       
   162 		pLogSystem->LogExtra(((TText8*)(__FILE__)), (__LINE__), ESevrErr,
       
   163 			_L("script paser aborting depth:%d"), ScriptDepth );
       
   164 		return false;	
       
   165 		}
       
   166 
       
   167 	// connect to the fileserver
       
   168 	returnCode=iTheFs.Connect();
       
   169 	if (returnCode!=KErrNone)
       
   170 		{
       
   171 		pLogSystem->LogExtra(((TText8*)(__FILE__)), (__LINE__), ESevrErr,
       
   172 			_L("Error trying to connect to the file server") );	
       
   173 		return false;
       
   174 		}
       
   175 
       
   176 	// open the script file 
       
   177 	RFile listfile;
       
   178 	returnCode=listfile.Open(iTheFs,ScriptFileName.FullName(),EFileRead|EFileShareAny);
       
   179 
       
   180 	// check if open fails 
       
   181 	if (returnCode!=KErrNone)
       
   182 		{
       
   183 		pLogSystem->LogExtra(((TText8*)(__FILE__)), (__LINE__), ESevrErr,
       
   184 			_L("Failed to open script file: %S"),&ScriptFileName.FullName());
       
   185 		Pause();
       
   186 		return false;
       
   187 		}
       
   188 
       
   189 	// display the file being processed
       
   190 	pLogSystem->LogExtra(((TText8*)(__FILE__)), (__LINE__), ESevrErr,
       
   191 		_L("reading script %S"),&ScriptFileName.FullName());
       
   192 
       
   193 	// get the script file size
       
   194 	TInt listfilesize;
       
   195 	returnCode=listfile.Size(listfilesize);
       
   196 	if (returnCode!=KErrNone)
       
   197 		{
       
   198 		pLogSystem->LogExtra(((TText8*)(__FILE__)), (__LINE__), ESevrErr,
       
   199 			_L("Failed read script file: %S size "),&scriptFileName);
       
   200 		return false;
       
   201 		}
       
   202 
       
   203 	// get a buffer to read the file into
       
   204 	ipScriptBuffer=HBufC8::New(listfilesize);
       
   205 	if (!ipScriptBuffer)
       
   206 		{
       
   207 		pLogSystem->LogExtra(((TText8*)(__FILE__)), (__LINE__), ESevrErr,
       
   208 			_L("Failed to allocate memory for script file %S "),&scriptFileName);
       
   209 		return false;
       
   210 		}
       
   211 
       
   212 	// get a pointer to the buffer
       
   213 	TPtr8 ptr=ipScriptBuffer->Des();
       
   214 
       
   215 	// read the file into the buffer
       
   216 	returnCode=listfile.Read(ptr);
       
   217 	if (returnCode!=KErrNone)
       
   218 		{
       
   219 		pLogSystem->LogExtra(((TText8*)(__FILE__)), (__LINE__), ESevrErr,
       
   220 			_L("Failed to read script file %S "),&scriptFileName);
       
   221 		return false;
       
   222 		}
       
   223 
       
   224 	// close the file
       
   225 	listfile.Close();
       
   226 
       
   227 	// close the file system
       
   228 	iTheFs.Close();
       
   229 
       
   230 	return true;
       
   231 	
       
   232 }
       
   233 
       
   234 enum TVerdict CScript::ExecuteScriptL()
       
   235 /**
       
   236 The script file has been read into pScriptBuffer.
       
   237 Now parse it for commands and excute them.
       
   238 
       
   239 @return TVerdict The current test result
       
   240 */
       
   241 	{
       
   242 	// use TLex to decode the script
       
   243 	TLex8 llex( *ipScriptBuffer);
       
   244 
       
   245 	// keep a count of the line number
       
   246 	TInt8 lineNo = 1;
       
   247 
       
   248 	// loop though processing the rest a line at a time
       
   249 	while(!llex.Eos())
       
   250 		{
       
   251 		// skip any spaces
       
   252 		while ( llex.Peek()==' ' )
       
   253 			llex.Inc();
       
   254 
       
   255 		// mark the start of the line
       
   256 		llex.Mark();
       
   257 		
       
   258 		// move to the next
       
   259 		while(!llex.Eos() && llex.Peek()!='\n')
       
   260 			llex.Inc();
       
   261 
       
   262 		// step over \n
       
   263 		if ( llex.Peek()=='\n' )
       
   264 			llex.Inc();
       
   265 				
       
   266 		// get the line 
       
   267 		TPtrC8 pline=llex.MarkedToken();
       
   268 		if (pline.Length()!=0)
       
   269 			{
       
   270 			// and then process
       
   271 			ProcessLineL( pline, lineNo );
       
   272 			}
       
   273 
       
   274 		// on to the next line
       
   275 		lineNo ++;
       
   276 		}
       
   277 
       
   278 	/* script processing complete, now return the script verdict */
       
   279 	/* Note: the script verdicts are just for the log */
       
   280 	/* if no tests failed then return pass for the script */
       
   281 	/* this covers scripts which do not test anything */
       
   282 	return (iFail == 0 ? EPass : EFail );
       
   283 	}
       
   284 
       
   285 void CScript::ProcessLineL(const TPtrC8 &narrowline, TInt8 lineNo)
       
   286 /**
       
   287 process a line from the script file
       
   288 
       
   289 @param narrowline The line of script file to be processed.
       
   290 @param lineNo The current line number.
       
   291 */
       
   292 	{
       
   293 	// call parse to process line
       
   294 	iParse->ProcessLineL(narrowline, lineNo);
       
   295 	}
       
   296 
       
   297 
       
   298 void CScript::DisplayResults(void)
       
   299 /**
       
   300 Display the accumulated  results
       
   301 */
       
   302 	{
       
   303 	pLogSystem->LogBlankLine();
       
   304 
       
   305 	pLogSystem->LogExtra(((TText8*)(__FILE__)), (__LINE__), ESevrErr,_L("Test Results Summary ") );
       
   306 	pLogSystem->LogExtra(((TText8*)(__FILE__)), (__LINE__), ESevrErr,_L("-------------------- ") );
       
   307 	pLogSystem->LogExtra(((TText8*)(__FILE__)), (__LINE__), ESevrErr,_L("Passed            :%d"),  iPass);
       
   308 	pLogSystem->LogExtra(((TText8*)(__FILE__)), (__LINE__), ESevrErr,_L("Failed            :%d"),  iFail);
       
   309 	pLogSystem->LogExtra(((TText8*)(__FILE__)), (__LINE__), ESevrErr,_L("Inconclusive      :%d"),  iInconclusive);
       
   310 	pLogSystem->LogExtra(((TText8*)(__FILE__)), (__LINE__), ESevrErr,_L("Test suite errors :%d"),  iTestSuiteError);
       
   311 	pLogSystem->LogExtra(((TText8*)(__FILE__)), (__LINE__), ESevrErr,_L("Aborted           :%d"),  iAbort);
       
   312 	pLogSystem->LogExtra(((TText8*)(__FILE__)), (__LINE__), ESevrErr,_L("Total             :%d"),  iTotal);
       
   313 
       
   314 	if( iPauseAtEnd )
       
   315 		{
       
   316 		// A pause at the end has been requested
       
   317 		Pause();
       
   318 		}
       
   319 
       
   320 	}
       
   321 
       
   322 void CScript::Pause( void )
       
   323 /**
       
   324 Implements the Pause command.
       
   325 */
       
   326 	{
       
   327 	if(!automatedMode)
       
   328 		{
       
   329 		console->Printf(KTxtPressAnyKey);
       
   330 		console->Getch(); // get and ignore character
       
   331 		}
       
   332 	}
       
   333 
       
   334 TBool CScript::BreakOnError( void )
       
   335 /**
       
   336 Implements the BreakOnError command.
       
   337 @return ETrue if the user enters "x" or EFalse
       
   338 */
       
   339 	{
       
   340 	if(automatedMode)
       
   341 		{
       
   342 		pLogSystem->LogExtra(((TText8*)(__FILE__)), (__LINE__), ESevrErr,_L("BREAK_ON_ERROR suppressed; terminating current test") );
       
   343 		return ETrue;
       
   344 		}
       
   345 	else
       
   346 		{
       
   347 		// display prompt
       
   348 		console->Printf(KTxtBreakOnError);
       
   349 
       
   350 		// get a character from the keyboard
       
   351 		TChar ch = console->Getch(); 
       
   352 
       
   353 		if ( ch == 'x' )
       
   354 			return ETrue;
       
   355 		else
       
   356 			return EFalse;
       
   357 		}
       
   358 	}
       
   359 
       
   360 void CScript::AddResult(enum TVerdict aTestVerdict )
       
   361 /**
       
   362 The end of the test has been reached.
       
   363 Add the result to the current totals.
       
   364 
       
   365 @param aTestVerdict The latest result.
       
   366 */
       
   367 	{
       
   368 	// another test complete, so increment total
       
   369 	iTotal++;
       
   370 
       
   371 	// add in the current result
       
   372 	switch (aTestVerdict) 
       
   373 		{
       
   374 	case EPass:
       
   375 		iPass++;
       
   376 		break;
       
   377 	case EFail:
       
   378 		iFail++;
       
   379 		break;
       
   380 	case EInconclusive:
       
   381 		iInconclusive++;
       
   382 		break;
       
   383 	case ETestSuiteError:
       
   384 		iTestSuiteError++;
       
   385 		break;
       
   386 	case EAbort:
       
   387 		iAbort++;
       
   388 		break;
       
   389 		}
       
   390 
       
   391 	// display the result
       
   392 	pLogSystem->LogResult(aTestVerdict, _L("Test Result for %S is %s "), 
       
   393 		&(iParse->iCurrentStepName), 
       
   394 		pLogSystem->TestResultText( aTestVerdict ) );
       
   395 	
       
   396 	// add a blank line
       
   397 	pLogSystem->LogBlankLine();
       
   398 
       
   399 	}
       
   400 
       
   401 void CScript::AddResult(CScript * subScript )
       
   402 /**
       
   403 The end of a sub script has been reached.
       
   404 Add the result to the totals
       
   405 
       
   406 @param subScript Pointer to the sript object containing the results
       
   407 */
       
   408 	{
       
   409 
       
   410 	iPass+= subScript->iPass;
       
   411 	iFail+= subScript->iFail;
       
   412 	iInconclusive += subScript->iInconclusive;
       
   413 	iTestSuiteError+= subScript->iTestSuiteError;
       
   414 	iAbort+= subScript->iAbort;
       
   415 	iTotal+=subScript->iTotal;
       
   416 	}