1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptomgmtlibs/securitytestfw/test/testhandler2/t_testrunner.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,336 @@
1.4 +/*
1.5 +* Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +#include "t_testrunner.h"
1.23 +#include "t_testaction.h"
1.24 +#include "t_output.h"
1.25 +
1.26 +// CTestRunner /////////////////////////////////////////////////////////////////
1.27 +
1.28 +EXPORT_C CTestRunner::CTestRunner(Output& aOut) :
1.29 + CActive(EPriorityNormal),
1.30 + iOut(aOut)
1.31 + {
1.32 + CActiveScheduler::Add(this);
1.33 + }
1.34 +
1.35 +EXPORT_C CTestRunner::~CTestRunner()
1.36 + {
1.37 + Cancel();
1.38 + }
1.39 +
1.40 +EXPORT_C TInt CTestRunner::PerformPrerequisiteL(CTestAction* aAction)
1.41 + {
1.42 + TInt err = KErrNone;
1.43 + while (!aAction->Finished() && aAction->iActionState == CTestAction::EPrerequisite)
1.44 + {
1.45 + err = RunAsyncMethodL(&CTestAction::PerformPrerequisite, aAction, err);
1.46 + }
1.47 + return err;
1.48 + }
1.49 +
1.50 +EXPORT_C TInt CTestRunner::PerformActionL(CTestAction* aAction)
1.51 + {
1.52 + TInt err = KErrNone;
1.53 + while (!aAction->Finished() && aAction->iActionState == CTestAction::EAction)
1.54 + {
1.55 + err = RunAsyncMethodL(&CTestAction::PerformAction, aAction, err);
1.56 + }
1.57 + return err;
1.58 + }
1.59 +
1.60 +EXPORT_C TInt CTestRunner::PerformPostrequisiteL(CTestAction* aAction, TInt aInitialStatus)
1.61 + {
1.62 + TInt err = aInitialStatus;
1.63 + while (!aAction->Finished() && aAction->iActionState == CTestAction::EPostrequisite)
1.64 + {
1.65 + err = RunAsyncMethodL(&CTestAction::PerformPostrequisite, aAction, err);
1.66 + }
1.67 + return err;
1.68 + }
1.69 +
1.70 +TInt CTestRunner::RunAsyncMethodL(TTestMethod aMethod, CTestAction* aAction, TInt aInitialStatus)
1.71 + {
1.72 + iStatus = aInitialStatus;
1.73 + TRAPD(err, (aAction->*aMethod)(iStatus));
1.74 + if (err != KErrNone)
1.75 + {
1.76 + iStatus = err;
1.77 + if (err != KErrNoMemory)
1.78 + {
1.79 + aAction->iActionState = CTestAction::EPostrequisite;
1.80 + }
1.81 + }
1.82 + else
1.83 + {
1.84 + SetActive();
1.85 + RunSchedulerL();
1.86 + }
1.87 +
1.88 + return iStatus.Int();
1.89 + }
1.90 +
1.91 +void CTestRunner::RunSchedulerL()
1.92 + {
1.93 + iSchedulerRunning = ETrue;
1.94 + CActiveScheduler::Start();
1.95 + }
1.96 +
1.97 +TBool CTestRunner::StepScheduler()
1.98 + {
1.99 + User::WaitForAnyRequest();
1.100 + TInt err;
1.101 + if (!CActiveScheduler::Current()->RunIfReady(err, EPriorityNull))
1.102 + {
1.103 + User::Invariant();
1.104 + }
1.105 + return !IsActive();
1.106 + }
1.107 +
1.108 +EXPORT_C void CTestRunner::RunL()
1.109 + {
1.110 + if (iSchedulerRunning)
1.111 + {
1.112 + iSchedulerRunning = EFalse;
1.113 + CActiveScheduler::Stop();
1.114 + }
1.115 + }
1.116 +
1.117 +EXPORT_C TInt CTestRunner::RunError(TInt /*aError*/)
1.118 + {
1.119 + return KErrGeneral; // RunL() can never leave
1.120 + }
1.121 +
1.122 +EXPORT_C void CTestRunner::DoCancel()
1.123 + {
1.124 + }
1.125 +
1.126 +// COOMTestRunnerBase ////////////////////////////////////////////////////////////////////
1.127 +
1.128 +/// Max OOM fail count, to prevent runaway tests
1.129 +const TInt KOOMFailLimit = 10000;
1.130 +
1.131 +EXPORT_C COOMTestRunnerBase::COOMTestRunnerBase(Output& aOut) :
1.132 + CTestRunner(aOut)
1.133 + {
1.134 + }
1.135 +
1.136 +EXPORT_C COOMTestRunnerBase::~COOMTestRunnerBase()
1.137 + {
1.138 + }
1.139 +
1.140 +EXPORT_C TInt COOMTestRunnerBase::PerformActionL(CTestAction* aAction)
1.141 + {
1.142 + iOut.writeString(_L("Running OOM test..."));
1.143 + iOut.writeNewLine();
1.144 + iOut.writeString(_L("Fail point: Heap used: Action state: Status:"));
1.145 + iOut.writeNewLine();
1.146 +
1.147 + StartOOMTestL();
1.148 +
1.149 + TInt allocStart = AllocCount();
1.150 +
1.151 + TInt failCount;
1.152 + TInt err = KErrNone;
1.153 + for (failCount = 1 ; failCount < KOOMFailLimit ; ++failCount)
1.154 + {
1.155 + IncHeapFailPoint();
1.156 +
1.157 + err = KErrNone;
1.158 + TInt actionState = 0;
1.159 + while (!aAction->Finished() && aAction->iActionState == CTestAction::EAction && err != KErrNoMemory)
1.160 + {
1.161 + ++actionState;
1.162 + err = RunAsyncMethodL(&CTestAction::PerformAction, aAction, err);
1.163 + }
1.164 +
1.165 + TInt allocEnd = AllocCount();
1.166 + ResetHeapFail();
1.167 +
1.168 + TBuf<128> buffer;
1.169 + buffer.Format(_L(" %8d %8d %8d %8d"), failCount, allocEnd - allocStart, actionState, err);
1.170 + iOut.writeString(buffer);
1.171 + iOut.writeNewLine();
1.172 +
1.173 + if (err != KErrNoMemory || aAction->Finished() || aAction->iActionState != CTestAction::EAction)
1.174 + {
1.175 + // Test finished
1.176 + break;
1.177 + }
1.178 +
1.179 + aAction->AfterOOMFailure();
1.180 + aAction->Reset();
1.181 + aAction->ResetState();
1.182 + }
1.183 +
1.184 + EndOOMTestL();
1.185 +
1.186 + if (failCount == KOOMFailLimit)
1.187 + {
1.188 + // Runaway OOM test
1.189 + iOut.writeString(_L("OOM test failed to terminate"));
1.190 + iOut.writeNewLine();
1.191 + return KErrGeneral;
1.192 + }
1.193 +
1.194 + return err;
1.195 + }
1.196 +
1.197 +// COOMTestRunner ////////////////////////////////////////////////////////////////////////
1.198 +
1.199 +COOMTestRunner::COOMTestRunner(Output& aOut) :
1.200 + COOMTestRunnerBase(aOut)
1.201 + {
1.202 + }
1.203 +
1.204 +COOMTestRunner::~COOMTestRunner()
1.205 + {
1.206 + }
1.207 +
1.208 +void COOMTestRunner::StartOOMTestL()
1.209 + {
1.210 + iFailPoint = 0;
1.211 + }
1.212 +
1.213 +void COOMTestRunner::IncHeapFailPoint()
1.214 + {
1.215 + ++iFailPoint;
1.216 + __UHEAP_SETFAIL(RHeap::EDeterministic, iFailPoint);
1.217 + }
1.218 +
1.219 +void COOMTestRunner::ResetHeapFail()
1.220 + {
1.221 + __UHEAP_RESET;
1.222 + }
1.223 +
1.224 +TInt COOMTestRunner::AllocCount()
1.225 + {
1.226 + return User::CountAllocCells();
1.227 + }
1.228 +
1.229 +void COOMTestRunner::EndOOMTestL()
1.230 + {
1.231 + }
1.232 +
1.233 +// CCancelTestRunner /////////////////////////////////////////////////////////////////////
1.234 +
1.235 +/// Max cancel step, to prevent runaway tests
1.236 +const TInt KCancelStepLimit = 200;
1.237 +
1.238 +CCancelTestRunner::CCancelTestRunner(Output& aOut) :
1.239 + CTestRunner(aOut)
1.240 + {
1.241 + }
1.242 +
1.243 +CCancelTestRunner::~CCancelTestRunner()
1.244 + {
1.245 + }
1.246 +
1.247 +/**
1.248 + * Run the async PerformAction method for a specified number of steps and then
1.249 + * cancel it. This does the equivalent of RunAsyncMethod, but calling
1.250 + * PerformAction and cancelling it.
1.251 + */
1.252 +TInt CCancelTestRunner::RunAndCancelPeformActionMethod(CTestAction* aAction, TInt aInitialStatus,
1.253 + TInt aCancelStep, TInt& aStep)
1.254 + {
1.255 + iStatus = aInitialStatus;
1.256 + TRAPD(err, aAction->PerformAction(iStatus));
1.257 + if (err != KErrNone)
1.258 + {
1.259 + return err;
1.260 + }
1.261 +
1.262 + SetActive();
1.263 +
1.264 + // This is our equivalent of an active scheduler loop
1.265 + while (IsActive())
1.266 + {
1.267 + StepScheduler();
1.268 +
1.269 + // Check if we can cancel this step
1.270 + if (iStatus.Int() == KRequestPending)
1.271 + {
1.272 + ++aStep;
1.273 + // Check if this is the step we want to cancel
1.274 + if (aStep == aCancelStep)
1.275 + {
1.276 + // Cancel request
1.277 + aAction->PerformCancel();
1.278 +
1.279 + // Check request completed immediately
1.280 + if (iStatus.Int() == KRequestPending)
1.281 + {
1.282 + iOut.writeString(_L("Cancelled request not completed immediately!"));
1.283 + iOut.writeNewLine();
1.284 + iAbort = ETrue;
1.285 + }
1.286 + }
1.287 + }
1.288 + }
1.289 +
1.290 + return iAbort ? KErrGeneral : iStatus.Int();
1.291 + }
1.292 +
1.293 +/**
1.294 + * Run the test action for a specified number of steps and then cancel it.
1.295 + */
1.296 +TInt CCancelTestRunner::RunAndCancelTestAction(CTestAction* aAction, TInt aCancelStep)
1.297 + {
1.298 + TInt err = KErrNone;
1.299 + TInt step = 0;
1.300 + TInt actionState = 0;
1.301 + while (!iAbort && !aAction->Finished() && aAction->iActionState == CTestAction::EAction && err != KErrCancel)
1.302 + {
1.303 + ++actionState;
1.304 + err = RunAndCancelPeformActionMethod(aAction, err, aCancelStep, step);
1.305 + }
1.306 +
1.307 + TBuf<128> buffer;
1.308 + buffer.Format(_L(" %8d %8d %8d %8d"), aCancelStep, step, actionState, err);
1.309 + iOut.writeString(buffer);
1.310 + iOut.writeNewLine();
1.311 +
1.312 + return err;
1.313 + }
1.314 +
1.315 +TInt CCancelTestRunner::PerformActionL(CTestAction* aAction)
1.316 + {
1.317 + iOut.writeString(_L("Running cancellation test..."));
1.318 + iOut.writeNewLine();
1.319 + iOut.writeString(_L("Fail step: Total steps: Action state: Status:"));
1.320 + iOut.writeNewLine();
1.321 +
1.322 + iAbort = EFalse;
1.323 + for (TInt step = 1 ; step <= KCancelStepLimit ; ++step)
1.324 + {
1.325 + TInt err = RunAndCancelTestAction(aAction, step);
1.326 +
1.327 + if (iAbort || aAction->Finished() || aAction->iActionState != CTestAction::EAction)
1.328 + {
1.329 + return err;
1.330 + }
1.331 +
1.332 + aAction->Reset();
1.333 + }
1.334 +
1.335 + // Runaway cancel test
1.336 + iOut.writeString(_L("Cancel test failed to terminate"));
1.337 + iOut.writeNewLine();
1.338 + return KErrGeneral;
1.339 + }