cryptomgmtlibs/securitytestfw/test/testhandler2/t_testrunner.cpp
changeset 0 2c201484c85f
child 8 35751d3474b7
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /*
       
     2 * Copyright (c) 2004-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 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "t_testrunner.h"
       
    20 #include "t_testaction.h"
       
    21 #include "t_output.h"
       
    22 
       
    23 // CTestRunner /////////////////////////////////////////////////////////////////
       
    24 
       
    25 EXPORT_C CTestRunner::CTestRunner(Output& aOut) :
       
    26     CActive(EPriorityNormal),
       
    27     iOut(aOut)
       
    28     {
       
    29 	CActiveScheduler::Add(this);
       
    30     }
       
    31 
       
    32 EXPORT_C CTestRunner::~CTestRunner()
       
    33     {
       
    34     Cancel();
       
    35     }
       
    36 
       
    37 EXPORT_C TInt CTestRunner::PerformPrerequisiteL(CTestAction* aAction)
       
    38     {
       
    39     TInt err = KErrNone;
       
    40     while (!aAction->Finished() && aAction->iActionState == CTestAction::EPrerequisite)
       
    41         {
       
    42         err = RunAsyncMethodL(&CTestAction::PerformPrerequisite, aAction, err);
       
    43         }
       
    44     return err;
       
    45     }
       
    46 
       
    47 EXPORT_C TInt CTestRunner::PerformActionL(CTestAction* aAction)
       
    48     {
       
    49     TInt err = KErrNone;
       
    50     while (!aAction->Finished() && aAction->iActionState == CTestAction::EAction)
       
    51         {
       
    52         err = RunAsyncMethodL(&CTestAction::PerformAction, aAction, err);
       
    53         }
       
    54     return err;
       
    55     }
       
    56 
       
    57 EXPORT_C TInt CTestRunner::PerformPostrequisiteL(CTestAction* aAction, TInt aInitialStatus)
       
    58     {
       
    59     TInt err = aInitialStatus;
       
    60     while (!aAction->Finished() && aAction->iActionState == CTestAction::EPostrequisite)
       
    61         {
       
    62         err = RunAsyncMethodL(&CTestAction::PerformPostrequisite, aAction, err);
       
    63         }
       
    64     return err;
       
    65     }
       
    66 
       
    67 TInt CTestRunner::RunAsyncMethodL(TTestMethod aMethod, CTestAction* aAction, TInt aInitialStatus)
       
    68     {
       
    69     iStatus = aInitialStatus;
       
    70     TRAPD(err, (aAction->*aMethod)(iStatus));
       
    71     if (err != KErrNone)
       
    72         {
       
    73         iStatus = err;
       
    74         if (err != KErrNoMemory)
       
    75         	{
       
    76         	aAction->iActionState = CTestAction::EPostrequisite;
       
    77         	}
       
    78         }
       
    79     else
       
    80         {
       
    81         SetActive();
       
    82 		RunSchedulerL();
       
    83         }
       
    84 
       
    85     return iStatus.Int();
       
    86     }
       
    87 
       
    88 void CTestRunner::RunSchedulerL()
       
    89 	{
       
    90 	iSchedulerRunning = ETrue;
       
    91 	CActiveScheduler::Start();
       
    92 	}
       
    93 
       
    94 TBool CTestRunner::StepScheduler()
       
    95 	{
       
    96     User::WaitForAnyRequest();
       
    97 	TInt err;
       
    98     if (!CActiveScheduler::Current()->RunIfReady(err, EPriorityNull)) 
       
    99         {
       
   100         User::Invariant(); 
       
   101         }
       
   102 	return !IsActive();
       
   103 	}
       
   104 
       
   105 EXPORT_C void CTestRunner::RunL()
       
   106     {
       
   107 	if (iSchedulerRunning)
       
   108 		{
       
   109 		iSchedulerRunning = EFalse;
       
   110 		CActiveScheduler::Stop();    
       
   111 		}
       
   112 	}
       
   113 
       
   114 EXPORT_C TInt CTestRunner::RunError(TInt /*aError*/)
       
   115     {
       
   116     return KErrGeneral; // RunL() can never leave
       
   117     }
       
   118 
       
   119 EXPORT_C void CTestRunner::DoCancel()
       
   120     {
       
   121     }
       
   122 
       
   123 // COOMTestRunnerBase ////////////////////////////////////////////////////////////////////
       
   124 
       
   125 /// Max OOM fail count, to prevent runaway tests
       
   126 const TInt KOOMFailLimit = 10000;
       
   127 
       
   128 EXPORT_C COOMTestRunnerBase::COOMTestRunnerBase(Output& aOut) :
       
   129     CTestRunner(aOut)
       
   130     {
       
   131     }
       
   132 
       
   133 EXPORT_C COOMTestRunnerBase::~COOMTestRunnerBase()
       
   134     {
       
   135     }
       
   136 
       
   137 EXPORT_C TInt COOMTestRunnerBase::PerformActionL(CTestAction* aAction)
       
   138     {
       
   139     iOut.writeString(_L("Running OOM test..."));
       
   140     iOut.writeNewLine();            
       
   141     iOut.writeString(_L("Fail point:  Heap used:  Action state:  Status:"));
       
   142     iOut.writeNewLine();            
       
   143 
       
   144 	StartOOMTestL();
       
   145 	
       
   146     TInt allocStart = AllocCount();
       
   147 
       
   148 	TInt failCount;
       
   149 	TInt err = KErrNone;
       
   150 	for (failCount = 1 ; failCount < KOOMFailLimit ; ++failCount)
       
   151         {
       
   152 		IncHeapFailPoint();
       
   153         
       
   154         err = KErrNone;
       
   155         TInt actionState = 0;
       
   156         while (!aAction->Finished() && aAction->iActionState == CTestAction::EAction && err != KErrNoMemory)
       
   157             {
       
   158             ++actionState;
       
   159             err = RunAsyncMethodL(&CTestAction::PerformAction, aAction, err);            
       
   160             }
       
   161 
       
   162         TInt allocEnd = AllocCount();
       
   163 		ResetHeapFail();
       
   164 
       
   165         TBuf<128> buffer;
       
   166         buffer.Format(_L("  %8d    %8d       %8d %8d"), failCount, allocEnd - allocStart, actionState, err);
       
   167         iOut.writeString(buffer);
       
   168         iOut.writeNewLine();
       
   169         
       
   170 		if (err != KErrNoMemory || aAction->Finished() || aAction->iActionState != CTestAction::EAction)
       
   171             {
       
   172 			// Test finished
       
   173 			break;
       
   174             }
       
   175 
       
   176         aAction->AfterOOMFailure();
       
   177         aAction->Reset();
       
   178         aAction->ResetState();
       
   179         }
       
   180 
       
   181 	EndOOMTestL();
       
   182 
       
   183 	if (failCount == KOOMFailLimit)
       
   184 		{
       
   185 		// Runaway OOM test
       
   186 		iOut.writeString(_L("OOM test failed to terminate"));
       
   187 		iOut.writeNewLine();
       
   188 		return KErrGeneral;
       
   189 		}
       
   190 
       
   191 	return err;
       
   192     }
       
   193 
       
   194 // COOMTestRunner ////////////////////////////////////////////////////////////////////////
       
   195 
       
   196 COOMTestRunner::COOMTestRunner(Output& aOut) :
       
   197     COOMTestRunnerBase(aOut)
       
   198     {
       
   199     }
       
   200 
       
   201 COOMTestRunner::~COOMTestRunner()
       
   202     {
       
   203     }
       
   204 
       
   205 void COOMTestRunner::StartOOMTestL()
       
   206 	{
       
   207 	iFailPoint = 0;
       
   208 	}
       
   209 
       
   210 void COOMTestRunner::IncHeapFailPoint()
       
   211 	{
       
   212 	++iFailPoint;
       
   213 	__UHEAP_SETFAIL(RHeap::EDeterministic, iFailPoint);
       
   214 	}
       
   215 
       
   216 void COOMTestRunner::ResetHeapFail()
       
   217 	{
       
   218 	__UHEAP_RESET;        		
       
   219 	}
       
   220 
       
   221 TInt COOMTestRunner::AllocCount()
       
   222 	{
       
   223 	return User::CountAllocCells();
       
   224 	}
       
   225 
       
   226 void COOMTestRunner::EndOOMTestL()
       
   227 	{
       
   228 	}
       
   229 
       
   230 // CCancelTestRunner /////////////////////////////////////////////////////////////////////
       
   231 
       
   232 /// Max cancel step, to prevent runaway tests
       
   233 const TInt KCancelStepLimit = 200;
       
   234 
       
   235 CCancelTestRunner::CCancelTestRunner(Output& aOut) :
       
   236     CTestRunner(aOut)
       
   237     {
       
   238     }
       
   239 
       
   240 CCancelTestRunner::~CCancelTestRunner()
       
   241     {
       
   242     }
       
   243 
       
   244 /**
       
   245  * Run the async PerformAction method for a specified number of steps and then
       
   246  * cancel it.  This does the equivalent of RunAsyncMethod, but calling
       
   247  * PerformAction and cancelling it.
       
   248  */
       
   249 TInt CCancelTestRunner::RunAndCancelPeformActionMethod(CTestAction* aAction, TInt aInitialStatus,
       
   250                                                        TInt aCancelStep, TInt& aStep)
       
   251     {
       
   252     iStatus = aInitialStatus;
       
   253     TRAPD(err, aAction->PerformAction(iStatus));
       
   254     if (err != KErrNone)
       
   255         {
       
   256         return err;
       
   257         }
       
   258 
       
   259     SetActive();
       
   260     
       
   261     // This is our equivalent of an active scheduler loop
       
   262 	while (IsActive())
       
   263 		{
       
   264 		StepScheduler();
       
   265 		
       
   266         // Check if we can cancel this step
       
   267         if (iStatus.Int() == KRequestPending)
       
   268 			{
       
   269             ++aStep;
       
   270 			// Check if this is the step we want to cancel
       
   271 			if (aStep == aCancelStep)
       
   272 				{
       
   273 				// Cancel request
       
   274 				aAction->PerformCancel();
       
   275 
       
   276 				// Check request completed immediately
       
   277 				if (iStatus.Int() == KRequestPending)
       
   278 					{
       
   279 					iOut.writeString(_L("Cancelled request not completed immediately!"));
       
   280 					iOut.writeNewLine();
       
   281 					iAbort = ETrue;
       
   282 					}
       
   283 				}
       
   284 			}
       
   285 		}
       
   286 
       
   287     return iAbort ? KErrGeneral : iStatus.Int();
       
   288     }
       
   289 
       
   290 /**
       
   291  * Run the test action for a specified number of steps and then cancel it.
       
   292  */
       
   293 TInt CCancelTestRunner::RunAndCancelTestAction(CTestAction* aAction, TInt aCancelStep)
       
   294     {
       
   295     TInt err = KErrNone;
       
   296 	TInt step = 0;
       
   297     TInt actionState = 0;
       
   298     while (!iAbort && !aAction->Finished() && aAction->iActionState == CTestAction::EAction && err != KErrCancel)
       
   299         {
       
   300         ++actionState;
       
   301 		err = RunAndCancelPeformActionMethod(aAction, err, aCancelStep, step);
       
   302         }
       
   303 
       
   304 	TBuf<128> buffer;
       
   305 	buffer.Format(_L(" %8d      %8d      %8d %8d"), aCancelStep, step, actionState, err);
       
   306 	iOut.writeString(buffer);
       
   307 	iOut.writeNewLine();
       
   308 
       
   309     return err;
       
   310     }
       
   311 
       
   312 TInt CCancelTestRunner::PerformActionL(CTestAction* aAction)
       
   313     {
       
   314     iOut.writeString(_L("Running cancellation test..."));
       
   315     iOut.writeNewLine();            
       
   316     iOut.writeString(_L("Fail step:  Total steps:  Action state:  Status:"));
       
   317     iOut.writeNewLine();            
       
   318 
       
   319     iAbort = EFalse;
       
   320     for (TInt step = 1 ; step <= KCancelStepLimit ; ++step) 
       
   321         {
       
   322         TInt err = RunAndCancelTestAction(aAction, step);
       
   323 
       
   324         if (iAbort || aAction->Finished() || aAction->iActionState != CTestAction::EAction)	
       
   325 			{
       
   326             return err;
       
   327 			}
       
   328 		
       
   329         aAction->Reset();
       
   330         }
       
   331     
       
   332     // Runaway cancel test
       
   333     iOut.writeString(_L("Cancel test failed to terminate"));
       
   334     iOut.writeNewLine();
       
   335     return KErrGeneral;
       
   336     }