tzservices/tzserver/test/Common/src/DstStep.cpp
changeset 0 2e3d3ce01487
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 // Copyright (c) 2004-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 #include "DstStep.h"
       
    17 
       
    18 // Type definitions
       
    19 #define UNUSED_VAR(a) a = a
       
    20  
       
    21 const TInt KErrTestFileHandle = -7400;
       
    22 
       
    23 _LIT(KRunTestCount, "MaxRunLimit");
       
    24 _LIT(KLeakTestCount, "MaxOOMLimit");
       
    25 _LIT(KFirstLeakIter, "MinOOMLimit");
       
    26 _LIT(KFileTestCount, "MaxOOFLimit");
       
    27 _LIT(KCheckHandles, "CheckHandles");
       
    28 
       
    29 _LIT(KNormalTestStarted, "Normal Test Started");
       
    30 _LIT(KNormalTestComplete, "Normal Test Completed; Attempts: %d");
       
    31 _LIT(KOOMTestStarted, "Out-of-Memory test started");
       
    32 _LIT(KOOMTestTerminated, "Out-of-Memory test ended; Allocations made: %d");
       
    33 _LIT(KOOFTestStarted, "Out-of-File-Handle Test Started");
       
    34 _LIT(KOOFTestTerminated, "Out-of-File-Handle test ended; Handles used: %d");
       
    35 _LIT(KOOFTestAborted, "Out-of-File-Handle Test Aborted; No File Session supplied.");
       
    36 
       
    37 
       
    38 //
       
    39 // CDSTActiveWorker
       
    40 //
       
    41 CDSTActiveWorker::~CDSTActiveWorker()
       
    42 	{
       
    43 	Deque();
       
    44 	}
       
    45 
       
    46 CDSTActiveWorker::CDSTActiveWorker(const CDstStep& aTestStep) : 
       
    47 	CActive(CActive::EPriorityStandard), iTestStep(const_cast<CDstStep&>(aTestStep))
       
    48 	{}
       
    49 
       
    50 void CDSTActiveWorker::Start()
       
    51 	{
       
    52 	CompleteSelf();
       
    53 	}
       
    54 
       
    55 
       
    56 void CDSTActiveWorker::CompleteSelf()
       
    57 	{
       
    58 	SetActive();
       
    59 	TRequestStatus* status = &iStatus;
       
    60 	User::RequestComplete(status,KErrNone);
       
    61 	}
       
    62 
       
    63 CDSTActiveWorker* CDSTActiveWorker::NewL(const CDstStep& aTestStep)
       
    64 	{
       
    65 	CDSTActiveWorker* self = new(ELeave) CDSTActiveWorker(aTestStep);
       
    66 	CActiveScheduler::Add(self);
       
    67 	self->CompleteSelf();
       
    68 	return self;
       
    69 	}
       
    70 
       
    71 void CDSTActiveWorker::RunL()
       
    72 	{
       
    73 	iTestStep.RunTest();
       
    74 	}
       
    75 
       
    76 void CDSTActiveWorker::DoCancel()
       
    77 	{
       
    78 	// do nothing
       
    79 	}
       
    80 
       
    81 TInt CDSTActiveWorker::RunError(TInt aError)
       
    82 	{
       
    83 	iTestStep.TestCompleted(aError);
       
    84 	return KErrNone;
       
    85 	}
       
    86 
       
    87 //
       
    88 // CDstStep
       
    89 //
       
    90 CDstStep::CDstStep() : 
       
    91 	iState(ERunningNormalTest),
       
    92 	iTestCmd(ECreateUnit),
       
    93 	iMaxRunTestCount (1),				
       
    94 	iMaxLeakTestCount (0),
       
    95 	iFirstLeakIter (0),
       
    96 	iMaxFileTestCount (0),
       
    97 	iCheckHandles(EFalse)
       
    98 	{
       
    99 	}
       
   100 
       
   101 CDstStep::~CDstStep()
       
   102 	{
       
   103 	delete iActiveWorker;
       
   104 	delete iScheduler;
       
   105 	}
       
   106 
       
   107 void CDstStep::PrepareTestEnvironmentL()
       
   108 	{
       
   109 	// do nothing
       
   110 	// sub-classes derive from this to load any script settings they require
       
   111 	}
       
   112 
       
   113 
       
   114 TVerdict CDstStep::doTestStepPreambleL()
       
   115 	{
       
   116 	iScheduler = new CActiveScheduler();
       
   117 	CActiveScheduler::Install(iScheduler);
       
   118 
       
   119 	GetIntFromConfig(ConfigSection(), KRunTestCount, iMaxRunTestCount);
       
   120 	GetIntFromConfig(ConfigSection(), KLeakTestCount, iMaxLeakTestCount);
       
   121 	GetIntFromConfig(ConfigSection(), KFirstLeakIter, iFirstLeakIter);
       
   122 	GetIntFromConfig(ConfigSection(), KFileTestCount, iMaxFileTestCount);
       
   123 	GetBoolFromConfig(ConfigSection(), KCheckHandles, iCheckHandles);
       
   124 
       
   125 	// there must be at least a single normal run;
       
   126 	if (iMaxRunTestCount <= 0) iMaxRunTestCount = 1;
       
   127 	INFO_PRINTF1(KNormalTestStarted);
       
   128 
       
   129 	PrepareTestEnvironmentL();
       
   130 
       
   131 	SetTestStepResult(EPass);
       
   132 	return TestStepResult();
       
   133 	}
       
   134 
       
   135 TVerdict CDstStep::doTestStepL()
       
   136 	{
       
   137 	if(TestStepResult() == EPass)
       
   138 		{
       
   139 		// the activeWorker is responsible for starting the actual test
       
   140 		// from its RunL
       
   141 		iActiveWorker = CDSTActiveWorker::NewL(*this);
       
   142 		CActiveScheduler::Start();
       
   143 
       
   144 		delete iActiveWorker;
       
   145 		iActiveWorker = NULL;
       
   146 		}
       
   147 
       
   148 	return TestStepResult();
       
   149 	}
       
   150 
       
   151 
       
   152 TVerdict CDstStep::doTestStepPostambleL()
       
   153 	{
       
   154 	delete iScheduler;
       
   155 	iScheduler=NULL;
       
   156 	CActiveScheduler::Install(NULL);
       
   157 
       
   158 	return TestStepResult();
       
   159 	}
       
   160 
       
   161 
       
   162 void CDstStep::SetFileSession(RFs& aFs)
       
   163 	{
       
   164 	if (ERunningFileTest == iState)
       
   165 		{
       
   166 		iFs = &aFs;
       
   167 
       
   168 		// set handles to allow
       
   169 		iFs->SetErrorCondition(KErrTestFileHandle, iTestCount);
       
   170 		}
       
   171 	}
       
   172 
       
   173  
       
   174 void CDstStep::TestCompleted(TInt aCondition)
       
   175 	{
       
   176 
       
   177 	iTestCmd = EDestroyUnit;
       
   178 
       
   179 	iTestCondition = aCondition;
       
   180 	iFs = NULL;
       
   181 
       
   182 	// Puts the test clean-up into our own active object RunL.
       
   183 	// we don't want to delete the test unit in a call from its own RunL or
       
   184 	// that of its delegates
       
   185 	iActiveWorker->Start();
       
   186 
       
   187 	}
       
   188 
       
   189 
       
   190 TInt CDstStep::FinishTest()
       
   191 	{
       
   192 
       
   193 	TRAPD(error, DestroyTestUnit());
       
   194 	UNUSED_VAR(error); //Used to supress build warnings
       
   195 
       
   196 	++iTestCount;
       
   197 
       
   198 	switch (iState)
       
   199 		{
       
   200 	case ERunningNormalTest:
       
   201 		if (iTestCount == iMaxRunTestCount)
       
   202 			{
       
   203 			TInt endPHandleCount;
       
   204 			TInt endTHandleCount;
       
   205 			RThread().HandleCount(endPHandleCount, endTHandleCount);
       
   206 
       
   207 			_LIT(KThreadHandleLeaks, "This test leaked %d thread handles");
       
   208 			_LIT(KThreadHandlesLeakCheck, "Thread Handle Leak Check");
       
   209 
       
   210 			// we cannot test process handles because other threads may be
       
   211 			// running in the server
       
   212 
       
   213 			if (endTHandleCount != iStartTHandleCount)
       
   214 				{
       
   215 				// TODO: check if EFF_PRINTF2 is better
       
   216 				INFO_PRINTF2(KThreadHandleLeaks, endTHandleCount - iStartTHandleCount);
       
   217 				}
       
   218 
       
   219 			if (iCheckHandles)
       
   220 				{
       
   221 				LogCheck(KThreadHandlesLeakCheck(), 
       
   222 					(endTHandleCount == iStartTHandleCount) ? KErrNone : KErrBadHandle);
       
   223 				}
       
   224 
       
   225 			INFO_PRINTF2(KNormalTestComplete, iTestCount);
       
   226 
       
   227 			iTestCount = 0;
       
   228 			if (iMaxLeakTestCount)
       
   229 				{
       
   230 				iState = ERunningLeakTest;
       
   231 				INFO_PRINTF1(KOOMTestStarted);
       
   232 				}
       
   233 			else if (iMaxFileTestCount)
       
   234 				{
       
   235 				iState = ERunningFileTest;
       
   236 				INFO_PRINTF1(KOOFTestStarted);
       
   237 				}
       
   238 			else
       
   239 				{
       
   240 				iState = EIdle;
       
   241 				}
       
   242 			}
       
   243 		break;
       
   244 
       
   245 	case ERunningLeakTest:
       
   246 		__UHEAP_MARKEND;
       
   247 		User::Heap().Check();
       
   248 		__UHEAP_RESET;
       
   249 
       
   250 		if ((iTestCondition != KErrNoMemory) || (iTestCount == iMaxLeakTestCount ))
       
   251 			{
       
   252 
       
   253 			INFO_PRINTF2(KOOMTestTerminated, iTestCount);
       
   254 			iTestCount = 0;
       
   255 			// TODO: save memory leak test result
       
   256 
       
   257 			// out of memory test completed
       
   258 			if (iMaxFileTestCount)
       
   259 				{
       
   260 				iState = ERunningFileTest;
       
   261 				INFO_PRINTF1(KOOFTestStarted);
       
   262 				}
       
   263 			else
       
   264 				{
       
   265 				iState = EIdle;
       
   266 				}
       
   267 			}
       
   268 
       
   269 		break;
       
   270 
       
   271 	case ERunningFileTest:
       
   272 		if (!iFs)
       
   273 			{
       
   274 			// no file session object was provided, stop the file test
       
   275 			INFO_PRINTF1(KOOFTestAborted);
       
   276 			iTestCount = 0;
       
   277 			// TODO: save file handle leak test result
       
   278 
       
   279 			iState = EIdle;
       
   280 
       
   281 			// _LIT(KFileTestTerm, "Out Of File Handles test terminated: No file session provided");
       
   282 			// Utils().LogIt(KFileTestTerm);
       
   283 			}
       
   284 		else
       
   285 			{
       
   286 			// clean-up file session handles restriction
       
   287 			iFs->SetErrorCondition(KErrNone);
       
   288 
       
   289 			if ((iTestCondition != KErrTestFileHandle) || (iTestCount == iMaxFileTestCount))
       
   290 				{
       
   291 				// out of file handle test completed
       
   292 				INFO_PRINTF2(KOOFTestTerminated, iTestCount);
       
   293 				iTestCount = 0;
       
   294 
       
   295 				// TODO: save file handle leak test result
       
   296 				iState = EIdle;
       
   297 
       
   298 				}
       
   299 			}
       
   300 		break;
       
   301 
       
   302 	default:
       
   303 		break;
       
   304 		}
       
   305 
       
   306 	// prepare for the next test state
       
   307 	iTestCmd = ECreateUnit;
       
   308 
       
   309 	iActiveWorker->Start();
       
   310 	return iTestCondition;
       
   311 	}
       
   312 
       
   313 
       
   314 TInt CDstStep::StartTest()
       
   315 	{
       
   316 	RThread().HandleCount(iStartPHandleCount, iStartTHandleCount);
       
   317 	SetTestStepResult(EFail);
       
   318 	TInt results = KErrNone;
       
   319 	switch (iState)
       
   320 		{
       
   321 		case ERunningLeakTest:
       
   322 			__UHEAP_MARK;
       
   323 			__UHEAP_FAILNEXT(iTestCount);
       
   324 		case ERunningFileTest:
       
   325 		case ERunningNormalTest:
       
   326 			TRAPD(error, results = CreateAndRunTestUnitL());
       
   327 			if ((results != KRequestPending) || (error != KErrNone))
       
   328 				{
       
   329 				// TODO: if there is a leave; log a message to that effect
       
   330 				// Test is Synchronous, or leave condition
       
   331 				if (results == KErrNone)
       
   332 					{
       
   333 					results = error;
       
   334 					}
       
   335 				TestCompleted(results);
       
   336 				}
       
   337 			break;
       
   338 
       
   339 		default:
       
   340 			// stop the scheduler
       
   341 			// All test states covered
       
   342 
       
   343 			SetTestStepResult(((iTestCondition == KErrNone) && (iChecksFailed == 0)) ? EPass : EFail);
       
   344 			CActiveScheduler::Stop();
       
   345 			break;
       
   346 		}
       
   347 
       
   348 	return (results);
       
   349 	}
       
   350 
       
   351 
       
   352 TInt CDstStep::RunTest()
       
   353 	{
       
   354 	TInt results = KErrNone;
       
   355 
       
   356 	switch (iTestCmd)
       
   357 		{
       
   358 		case ECreateUnit:
       
   359 			results = StartTest();
       
   360 			break;
       
   361 
       
   362 		case EDestroyUnit:
       
   363 			results = FinishTest();
       
   364 			break;
       
   365 
       
   366 		default:
       
   367 			// stop the scheduler
       
   368 			// something bad has happened
       
   369 			// TODO: Panic
       
   370 			SetTestStepResult(EFail);
       
   371 			CActiveScheduler::Stop();
       
   372 			break;
       
   373 		}
       
   374 
       
   375 	return (results);
       
   376 	}
       
   377 
       
   378 
       
   379 void CDstStep::LogCheck(const TDesC& aDescription, TInt aCondition)
       
   380 	{
       
   381 
       
   382 	// set the result for the temporary benefit of any dependent test step
       
   383 	// it will be overwritten when the test completes
       
   384 	if (aCondition == KErrNone)
       
   385 		{
       
   386 		SetTestStepResult(EPass);
       
   387 		++iChecksPassed;
       
   388 		}
       
   389 	else
       
   390 		{
       
   391 		SetTestStepResult(EFail);
       
   392 		++iChecksFailed;
       
   393 		if (iState == ERunningNormalTest)
       
   394 			{
       
   395 			// only do the logging in normal test mode.
       
   396 			// we don't want to use any resources that interferes with stress testing
       
   397 			ERR_PRINTF3(_L("Check: %S: Error Code: %d"), &aDescription, aCondition);
       
   398 			}
       
   399 		}
       
   400 	}
       
   401 
       
   402 
       
   403 TTime CDstStep::ReadTimeParamStringL(const TDesC& aParamString)
       
   404 	{
       
   405 	// Format of buffer to construct a TTime is YYYYMMDD:HHMMSS (15 characters).
       
   406 	// TTime uses zero-based values for month and day - which is confusing for scripting.
       
   407 	// In our script, we use actual month and day numbers to make things simpler so we
       
   408 	// must modify the string here to take account of this.
       
   409 	TBuf<32> buf;
       
   410 	TInt m, d;
       
   411 
       
   412 	buf.Zero();
       
   413 	buf.Copy(aParamString.Left(4));		// The year
       
   414 	TLex lexMonth = aParamString.Mid(4, 2);
       
   415 	lexMonth.Val(m);
       
   416 	TLex lexDay = aParamString.Mid(6, 2);
       
   417 	lexDay.Val(d);
       
   418 	buf.AppendFormat(_L("%02d%02d"), m - 1, d - 1);	// The month and day
       
   419 	buf.Append(aParamString.Right(7));
       
   420 
       
   421 	return TTime(buf);
       
   422 	}