sysstatemgmt/systemstarter/test/tamastarter/src/tamastarter_app.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 // Copyright (c) 2007-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 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @test
       
    19  @internalComponent - Internal Symbian test code 
       
    20 */
       
    21 
       
    22 #include <e32base.h>
       
    23 #include <e32debug.h>
       
    24 #include <f32file.h>
       
    25 #include <e32const.h>
       
    26 #include <bautils.h>
       
    27 #include <bacline.h>
       
    28 #include <apacmdln.h>
       
    29 #include <utf.h>
       
    30 #include <test/testexecutestepbase.h>
       
    31 #include <cinidata.h>
       
    32 #include <sysmonclisess.h>
       
    33 
       
    34 #include "tamastarter_constants.h"
       
    35 
       
    36 _LIT(KAmaStarterTestAppPanic, "AmaStarterTestApp");
       
    37 
       
    38 
       
    39 //Function to create the new file if it does not exists.
       
    40 void CreateResultFileL(const TDesC& aFileName)
       
    41 	{
       
    42 	RFs fs;
       
    43 	User::LeaveIfError(fs.Connect());	
       
    44 	CleanupClosePushL(fs);
       
    45 	
       
    46 	if (!BaflUtils::FileExists(fs, aFileName))
       
    47 		{
       
    48 		// Create the folder if it does not exist
       
    49 		TInt ret = fs.MkDirAll(KTestAppResultsPath);
       
    50 		if (ret != KErrAlreadyExists)
       
    51 			{
       
    52 			User::LeaveIfError(ret);
       
    53 			}
       
    54 		
       
    55 		RFile	resultFile;
       
    56 		ret = resultFile.Create(fs, aFileName, EFileShareExclusive); //CIniData isn't thread safe
       
    57 		resultFile.Close();
       
    58 		User::LeaveIfError(ret);
       
    59 		}
       
    60 	
       
    61 	CleanupStack::PopAndDestroy(&fs);
       
    62 	}
       
    63 
       
    64 
       
    65 
       
    66 // Set the counter value to '0' in case it does not exists.
       
    67 void InitCounterL(CIniData& aIniData, const TDesC& aSection, const TDesC& aKey)
       
    68 	{
       
    69 	TInt value = 0;
       
    70 	TBool readResult = aIniData.FindVar(aSection, aKey, value);
       
    71 	if (!readResult)
       
    72 		{
       
    73 		TBuf<2> descVal;
       
    74 		descVal.Num(value);
       
    75 		User::LeaveIfError(aIniData.AddValue(aSection, aKey, descVal));		
       
    76 		aIniData.WriteToFileL();
       
    77 		}
       
    78 	}
       
    79 
       
    80 // Get the counter value from the data (log) file.
       
    81 TInt GetCounter(CIniData& aIniData, const TDesC& aSection, const TDesC& aKey)
       
    82 	{
       
    83 	TInt value = 0;
       
    84 	aIniData.FindVar(aSection, aKey, value);
       
    85 	return value;
       
    86 	}
       
    87 
       
    88 
       
    89 // IncrementCounter increment the counter value by '1'
       
    90 void IncrementCounterL(CIniData& aIniData, const TDesC& aSection, const TDesC& aKey)
       
    91 	{
       
    92 	TInt value = 1 + GetCounter(aIniData, aSection, aKey);
       
    93 	TBuf<2> descVal;
       
    94 	descVal.Num(value);
       
    95 	User::LeaveIfError(aIniData.SetValue(aSection, aKey, descVal));
       
    96 	aIniData.WriteToFileL();
       
    97 	}
       
    98 
       
    99 
       
   100 //StartTestL processes the command line, and appropriately updates the various counters
       
   101 //(Sucessful, failure, panic etc) depending on the arguments provided during the launch.
       
   102 //The EXE name is used as section name where all the counters will be added/updated.
       
   103 //This way each EXE launched will update its own result in the section in the result
       
   104 //file. The result file name is expected to be provided as an command line argument for the
       
   105 //result of the launch of this test EXE to be logged.
       
   106 void StartTestL(void)
       
   107 	{
       
   108 	//Get the executable filename
       
   109 	RProcess testApp;
       
   110 	TParse parse;
       
   111 	parse.Set(testApp.FullName(), NULL, NULL);
       
   112 	
       
   113 	TTime starttime;
       
   114 	starttime.UniversalTime();
       
   115 	TFileName fileForLog(parse.Name());
       
   116 	RDebug::Print( _L("*** %S::StartTestL - entry at time %d:%d.%d"), &fileForLog, starttime.DateTime().Minute(), starttime.DateTime().Second(), starttime.DateTime().MicroSecond());
       
   117 
       
   118 	HBufC* commandLineDes = NULL;	
       
   119 	//EXE is launched as new process
       
   120 	if ( User::CommandLineLength() > 0 )
       
   121 		{
       
   122 		commandLineDes = HBufC::New(User::CommandLineLength());
       
   123 		TPtr cmd = commandLineDes->Des();
       
   124 		User::CommandLine(cmd);
       
   125 		}
       
   126 	//EXE is launched as APP
       
   127 	else
       
   128 		{
       
   129 		CApaCommandLine*	apaCmdLine = NULL;
       
   130 		CApaCommandLine::GetCommandLineFromProcessEnvironment(apaCmdLine);
       
   131 		CleanupStack::PushL(apaCmdLine);		
       
   132 		commandLineDes = CnvUtfConverter::ConvertToUnicodeFromUtf8L(apaCmdLine->TailEnd());
       
   133 		CleanupStack::PopAndDestroy(apaCmdLine);
       
   134 		}
       
   135 	
       
   136 	CleanupStack::PushL(commandLineDes);
       
   137 	TLex cmdLine = commandLineDes->Des();
       
   138 	
       
   139 	TInt	timeout = 0;
       
   140 	TInt	retriedCount = 0;
       
   141 	
       
   142 	TInt	err = KErrNone;
       
   143 	TBool	toLeave = EFalse;
       
   144 	
       
   145 	TInt	panic = 0;
       
   146 	TBool	toPanic = EFalse;
       
   147 	
       
   148 	TInt	rend = KErrNone;
       
   149 	TBool	toRend = EFalse;
       
   150 	
       
   151 	TInt	cancelMonLaunch = 0;
       
   152 	TBool	cancelMon = EFalse;
       
   153 	
       
   154 	TBuf<KMaxFileName>	fileName(KTestAppResultsPath);
       
   155 	TPtrC ptr;
       
   156 
       
   157 	for(ptr.Set(cmdLine.NextToken()) ; ptr.Length() ; ptr.Set(cmdLine.NextToken()))
       
   158 		{
       
   159 		//Result file name
       
   160 		if ( !ptr.CompareF(KLog) )
       
   161 			{
       
   162 			fileName.Append(cmdLine.NextToken());
       
   163 			fileName.Append(KTestLogFileExtension);
       
   164 			CreateResultFileL(fileName);
       
   165 			}
       
   166 		//Rendezvous with
       
   167 		else if ( !ptr.CompareF(KRendezvous) )
       
   168 			{
       
   169 			TLex	lex(cmdLine.NextToken());
       
   170 			lex.Val(rend);
       
   171 			toRend = ETrue;
       
   172 			}
       
   173 		//Timeout
       
   174 		else if ( !ptr.CompareF(KTimeout) )
       
   175 			{
       
   176 			TLex	lex(cmdLine.NextToken());
       
   177 			lex.Val(timeout);
       
   178 			}
       
   179 		//Retry count
       
   180 		else if ( !ptr.CompareF(KRetry) )
       
   181 			{
       
   182 			TLex	lex(cmdLine.NextToken());
       
   183 			lex.Val(retriedCount);
       
   184 			}
       
   185 		//Leave Error value
       
   186 		else if ( !ptr.CompareF(KLeaveErrorValue) )
       
   187 			{
       
   188 			TLex	lex(cmdLine.NextToken());
       
   189 			lex.Val(err);
       
   190 			toLeave = ETrue;
       
   191 			}
       
   192 		//Panic
       
   193 		else if ( !ptr.CompareF(KPanicValue) )
       
   194 			{
       
   195 			TLex	lex(cmdLine.NextToken());
       
   196 			lex.Val(panic);
       
   197 			toPanic = ETrue;
       
   198 			}
       
   199 		//Cancel monitoring after N launches/restarts
       
   200 		else if ( !ptr.CompareF(KCancelMonitoring) )
       
   201 			{
       
   202 			TLex	lex(cmdLine.NextToken());
       
   203 			lex.Val(cancelMonLaunch);
       
   204 			cancelMon = ETrue;
       
   205 			}	
       
   206 		}
       
   207 		
       
   208 	CIniData* appResults = CIniData::NewL(fileName);
       
   209 	CleanupStack::PushL(appResults);
       
   210 
       
   211 	// Set all the counters to 0
       
   212 	//  NOTE: This is done if the counter does not exist (otherwise, it keeps the old value)
       
   213 	InitCounterL(*appResults, parse.Name(), KLaunchCntr);
       
   214 	InitCounterL(*appResults, parse.Name(), KRetryCntr);
       
   215 	InitCounterL(*appResults, parse.Name(), KSuccessCntr);
       
   216 	InitCounterL(*appResults, parse.Name(), KPanicCntr);
       
   217 	InitCounterL(*appResults, parse.Name(), KLeaveCntr);
       
   218 	InitCounterL(*appResults, parse.Name(), KFailedCntr);
       
   219 
       
   220 	// Update the Launch counter	
       
   221 	IncrementCounterL(*appResults, parse.Name(), KLaunchCntr);
       
   222 
       
   223 	// Update the Retry counter	
       
   224 	if (retriedCount > 0)
       
   225 		{
       
   226 		TInt retryCntr = GetCounter(*appResults, parse.Name(), KRetryCntr);	
       
   227 
       
   228 		//Check if app is to be timedout in case retry counter is set > 0
       
   229 		if (retryCntr < retriedCount && timeout > 0)
       
   230 			{
       
   231 			IncrementCounterL(*appResults, parse.Name(), KRetryCntr);
       
   232 			User::After(TTimeIntervalMicroSeconds32(timeout*1000));
       
   233 			}
       
   234 		}		
       
   235 
       
   236 	// Update the Success counter (or Panic, or Leave)
       
   237 	if (toPanic == EFalse && toLeave == EFalse && rend == KErrNone)
       
   238 		{
       
   239 		IncrementCounterL(*appResults, parse.Name(), KSuccessCntr);
       
   240 		}
       
   241 	else
       
   242 		{
       
   243 		if (toPanic)
       
   244 			{
       
   245 			IncrementCounterL(*appResults, parse.Name(), KPanicCntr);
       
   246 			User::SetJustInTime(EFalse);
       
   247 			User::Panic(KAmaStarterTestAppPanic,panic);			
       
   248 			}
       
   249 		if (toLeave)
       
   250 			{
       
   251 			IncrementCounterL(*appResults, parse.Name(), KLeaveCntr);
       
   252 			User::Leave(err);
       
   253 			}
       
   254 		}
       
   255 
       
   256 	// Update the Failed counter	
       
   257 	if (toRend)
       
   258 		{
       
   259 		if (rend != KErrNone)
       
   260 			{
       
   261 			IncrementCounterL(*appResults, parse.Name(), KFailedCntr);
       
   262 			}
       
   263 		RProcess::Rendezvous(rend);
       
   264 		}
       
   265 	
       
   266 	CleanupStack::PopAndDestroy(2); //cmdLine, iAppResults
       
   267 	
       
   268 	//Check if this app/process was asked to cancel its monitoring, after it has been
       
   269 	//restarted/launched 'N' (cancelMonLaunch) times
       
   270 	if (cancelMon)
       
   271 		{
       
   272 		TInt launchCntr = GetCounter(*appResults, parse.Name(), KLaunchCntr);
       
   273 		if (launchCntr >= cancelMonLaunch)
       
   274 			{
       
   275 			//Wait for 25 secs to let process execution time get passed throttle time
       
   276 			User::After(TTimeIntervalMicroSeconds32(25000000));
       
   277 			RSysMonSession	sysMonSession;
       
   278 			sysMonSession.CancelMonitorSelfL();
       
   279 			}
       
   280 		}
       
   281 	
       
   282 	TTime endtime;
       
   283 	endtime.UniversalTime();
       
   284 	RDebug::Print( _L("*** %S::StartTestL - exit at time %d:%d.%d"), &fileForLog, endtime.DateTime().Minute(), endtime.DateTime().Second(), endtime.DateTime().MicroSecond());
       
   285 	}
       
   286 
       
   287 TInt E32Main()
       
   288 	{	
       
   289 	__UHEAP_MARK;
       
   290 
       
   291 	CTrapCleanup* theTrapCleanup = CTrapCleanup::New();
       
   292 
       
   293 	TRAPD(err, StartTestL());
       
   294 	if (err)
       
   295 		{
       
   296 		User::Panic(KAmaStarterTestAppPanic,err);
       
   297 		}
       
   298     delete theTrapCleanup;
       
   299 
       
   300 	__UHEAP_MARKEND;
       
   301 	
       
   302 	return KErrNone;
       
   303 	}