os/security/cryptoservices/filebasedcertificateandkeystores/test/tcertapps/t_certapps_actions.cpp
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoservices/filebasedcertificateandkeystores/test/tcertapps/t_certapps_actions.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,455 @@
1.4 +/*
1.5 +* Copyright (c) 2005-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 +/**
1.23 + @file
1.24 +*/
1.25 +
1.26 +#include "t_certapps_actions.h"
1.27 +#include "t_certapps_defs.h"
1.28 +#include "t_input.h"
1.29 +#include "t_output.h"
1.30 +
1.31 +#include "certificateapps.h"
1.32 +
1.33 +/////////////////////////////////////////////////////////////////////////////////
1.34 +//CCertAppTestAction base class
1.35 +/////////////////////////////////////////////////////////////////////////////////
1.36 +
1.37 +// singleton instance of the manager
1.38 +CCertificateAppInfoManager* CCertAppTestAction::iAppManager = NULL;
1.39 +
1.40 +CCertAppTestAction::~CCertAppTestAction()
1.41 + {
1.42 + }
1.43 +
1.44 +
1.45 +CCertAppTestAction::CCertAppTestAction(RFs& aFs, CConsoleBase& aConsole,
1.46 + Output& aOut)
1.47 +: CTestAction(aConsole, aOut), iFs(aFs)
1.48 + {
1.49 + }
1.50 +
1.51 +void CCertAppTestAction::ConstructL(const TTestActionSpec& aTestActionSpec)
1.52 + {
1.53 + CTestAction::ConstructL(aTestActionSpec);
1.54 + TInt pos = 0, error = 0;
1.55 + SetExpectedResult(ParseTagString(aTestActionSpec.iActionResult, KReturn, pos, error));
1.56 + }
1.57 +
1.58 +void CCertAppTestAction::InitialiseL(TBool& /*aMemFailureFlag*/,
1.59 + TBool& /*aCancel*/, TInt& /*aHeapMark*/, TInt& /*aHeapMarkEnd*/)
1.60 + {
1.61 + }
1.62 +
1.63 +TPtrC8 CCertAppTestAction::ParseTagString(const TDesC8& aBuf, const TDesC8& aTagName, TInt& aPos, TInt& aError)
1.64 + {
1.65 + // wrapper around the Input::ParseElement function. Processes aTagName
1.66 + // and produces <aTagName> and </aTagName> used by ParseElement
1.67 + TBuf8<64> start(0);
1.68 + TBuf8<64> end(0);
1.69 +
1.70 + start.Append('<');
1.71 + start.Append(aTagName);
1.72 + start.Append('>');
1.73 +
1.74 + end.Append('<');
1.75 + end.Append('/');
1.76 + end.Append(aTagName);
1.77 + end.Append('>');
1.78 +
1.79 + return Input::ParseElement(aBuf, start, end, aPos, aError);
1.80 + }
1.81 +
1.82 +TInt32 CCertAppTestAction::ParseTagInt(const TDesC8& aBuf, const TDesC8& aTagName, TInt& aPos, TInt& aError)
1.83 + {
1.84 + TPtrC8 ptr(ParseTagString(aBuf, aTagName, aPos, aError));
1.85 +
1.86 + // Get an integer value out of it
1.87 + TInt32 retVal = 0;
1.88 + TLex8 lex(ptr);
1.89 + lex.Val(retVal);
1.90 + return retVal;
1.91 + }
1.92 +
1.93 +
1.94 +void CCertAppTestAction::SetExpectedResult(const TDesC8& aResult)
1.95 + {
1.96 + if (aResult == _L8("KErrNone") || aResult == KNullDesC8)
1.97 + {
1.98 + iExpectedResult = KErrNone;
1.99 + }
1.100 + else if (aResult == _L8("KErrAccessDenied"))
1.101 + {
1.102 + iExpectedResult = KErrAccessDenied;
1.103 + }
1.104 + else if (aResult == _L8("KErrNotReady"))
1.105 + {
1.106 + iExpectedResult = KErrNotReady;
1.107 + }
1.108 + else if (aResult == _L8("KErrAlreadyExists"))
1.109 + {
1.110 + iExpectedResult = KErrAlreadyExists;
1.111 + }
1.112 + else if (aResult == _L8("KErrInUse"))
1.113 + {
1.114 + iExpectedResult = KErrInUse;
1.115 + }
1.116 + else if (aResult == _L8("KErrNotFound"))
1.117 + {
1.118 + iExpectedResult = KErrNotFound;
1.119 + }
1.120 + else if (aResult == _L8("KErrBadName"))
1.121 + {
1.122 + iExpectedResult = KErrBadName;
1.123 + }
1.124 + else if (aResult == _L8("KErrArgument"))
1.125 + {
1.126 + iExpectedResult = KErrArgument;
1.127 + }
1.128 + else if (aResult == _L8("KErrNotReady"))
1.129 + {
1.130 + iExpectedResult = KErrNotReady;
1.131 + }
1.132 + else if (aResult == _L8("KErrCorrupt"))
1.133 + {
1.134 + iExpectedResult = KErrCorrupt;
1.135 + }
1.136 + else if (aResult == _L8("KErrPermissionDenied"))
1.137 + {
1.138 + iExpectedResult = KErrPermissionDenied;
1.139 + }
1.140 + else
1.141 + {
1.142 + iOut.write(_L("Unrecognised error code: "));
1.143 + iOut.writeString(aResult);
1.144 + iOut.writeNewLine();
1.145 + User::Leave(KErrArgument);
1.146 + }
1.147 + }
1.148 +
1.149 +void CCertAppTestAction::PerformAction(TRequestStatus& aStatus)
1.150 + {
1.151 + TInt err = KErrNone;
1.152 + TRAP(err, DoPerformActionL());
1.153 +
1.154 + if (err != KErrNoMemory)
1.155 + {
1.156 + iFinished = ETrue;
1.157 + }
1.158 +
1.159 + TRequestStatus* status = &aStatus;
1.160 + User::RequestComplete(status, err);
1.161 + }
1.162 +
1.163 +void CCertAppTestAction::DoPerformPrerequisite(TRequestStatus& aStatus)
1.164 + {
1.165 + TInt err = KErrNone;
1.166 +
1.167 + // For all tests (with the exception of InitManager which overrides this
1.168 + // method), we check to make sure that the AppManager is set
1.169 + if (iAppManager)
1.170 + {
1.171 + iActionState = EAction;
1.172 + }
1.173 + else
1.174 + {
1.175 + iFinished = ETrue;
1.176 + err = KErrNotFound;
1.177 + }
1.178 +
1.179 + TRequestStatus* status = &aStatus;
1.180 + User::RequestComplete(status, err);
1.181 + }
1.182 +
1.183 +void CCertAppTestAction::DoPerformPostrequisite(TRequestStatus& aStatus)
1.184 + {
1.185 + TRequestStatus* status = &aStatus;
1.186 + User::RequestComplete(status, KErrNone);
1.187 + }
1.188 +
1.189 +void CCertAppTestAction::PerformCancel()
1.190 + {
1.191 + }
1.192 +
1.193 +void CCertAppTestAction::Reset()
1.194 + {
1.195 + }
1.196 +
1.197 +void CCertAppTestAction::DoReportAction()
1.198 + {
1.199 + }
1.200 +
1.201 +void CCertAppTestAction::DoCheckResult(TInt aError)
1.202 + {
1.203 + iResult = (aError == iExpectedResult);
1.204 + }
1.205 +
1.206 +
1.207 +/////////////////////////////////////////////////////////////////////////////////
1.208 +//CInitManager - initialises the singleton manager
1.209 +/////////////////////////////////////////////////////////////////////////////////
1.210 +CInitManager::CInitManager(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
1.211 +: CCertAppTestAction(aFs, aConsole, aOut)
1.212 + {
1.213 + }
1.214 +
1.215 +void CInitManager::DoPerformPrerequisite(TRequestStatus& aStatus)
1.216 + {
1.217 + TInt err = KErrNone;
1.218 +
1.219 + // If it does not exist, then we are OK
1.220 + if (!iAppManager)
1.221 + {
1.222 + iActionState = EAction;
1.223 + }
1.224 + else
1.225 + {
1.226 + iFinished = ETrue;
1.227 + err = KErrAlreadyExists;
1.228 + }
1.229 +
1.230 + TRequestStatus* status = &aStatus;
1.231 + User::RequestComplete(status, err);
1.232 + }
1.233 +
1.234 +void CInitManager::DoPerformActionL()
1.235 + {
1.236 + iAppManager = CCertificateAppInfoManager::NewL();
1.237 + }
1.238 +
1.239 +/////////////////////////////////////////////////////////////////////////////////
1.240 +//CDestroyManager - destroys the singleton manager
1.241 +/////////////////////////////////////////////////////////////////////////////////
1.242 +CDestroyManager::CDestroyManager(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
1.243 +: CCertAppTestAction(aFs, aConsole, aOut)
1.244 + {
1.245 + }
1.246 +
1.247 +void CDestroyManager::DoPerformActionL()
1.248 + {
1.249 + delete iAppManager;
1.250 + iAppManager = NULL;
1.251 + }
1.252 +
1.253 +
1.254 +/////////////////////////////////////////////////////////////////////////////////
1.255 +//CClearAllApps - removes all the applications from the app list
1.256 +/////////////////////////////////////////////////////////////////////////////////
1.257 +CClearAllApps::CClearAllApps(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
1.258 +: CCertAppTestAction(aFs, aConsole, aOut)
1.259 + {
1.260 + }
1.261 +
1.262 +void CClearAllApps::DoPerformActionL()
1.263 + {
1.264 + for (;;)
1.265 + {
1.266 + // apps is owned by the app manager, don't try accessing it after we've
1.267 + // removed any of the applications though
1.268 +
1.269 + RArray<TCertificateAppInfo> apps;
1.270 + apps = iAppManager->Applications();
1.271 + if (apps.Count() == 0)
1.272 + {
1.273 + break;
1.274 + }
1.275 + iAppManager->RemoveL(apps[0].Id());
1.276 + }
1.277 + }
1.278 +
1.279 +/////////////////////////////////////////////////////////////////////////////////
1.280 +//CAddApp - adds applications
1.281 +/////////////////////////////////////////////////////////////////////////////////
1.282 +CAddApp::CAddApp(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
1.283 +: CCertAppTestAction(aFs, aConsole, aOut)
1.284 + {
1.285 + }
1.286 +
1.287 +CAddApp::~CAddApp()
1.288 + {
1.289 + iAppArray.Close();
1.290 + }
1.291 +
1.292 +void CAddApp::ConstructL(const TTestActionSpec& aTestActionSpec)
1.293 + {
1.294 + CCertAppTestAction::ConstructL(aTestActionSpec);
1.295 + TInt pos = 0;
1.296 + TInt err = KErrNone;
1.297 +
1.298 + // Parse the UID and name for the new app
1.299 + do
1.300 + {
1.301 + TUid uid(TUid::Uid(ParseTagInt(aTestActionSpec.iActionBody, KUid, pos, err)));
1.302 + if (!err)
1.303 + {
1.304 + TName name;
1.305 + name.Copy(ParseTagString(aTestActionSpec.iActionBody, KAppName, pos, err));
1.306 + iAppArray.Append(TCertificateAppInfo(uid, name));
1.307 + }
1.308 + }
1.309 + while (!err);
1.310 + }
1.311 +
1.312 +void CAddApp::DoPerformActionL()
1.313 + {
1.314 + for (TInt i = 0; i < iAppArray.Count(); ++i)
1.315 + {
1.316 + iAppManager->AddL(iAppArray[i]);
1.317 + }
1.318 + }
1.319 +
1.320 +/////////////////////////////////////////////////////////////////////////////////
1.321 +//CRemoveApp - removes an application
1.322 +/////////////////////////////////////////////////////////////////////////////////
1.323 +CRemoveApp::CRemoveApp(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
1.324 +: CCertAppTestAction(aFs, aConsole, aOut)
1.325 + {
1.326 + }
1.327 +
1.328 +void CRemoveApp::ConstructL(const TTestActionSpec& aTestActionSpec)
1.329 + {
1.330 + CCertAppTestAction::ConstructL(aTestActionSpec);
1.331 + TInt pos = 0;
1.332 + TInt err = KErrNone;
1.333 +
1.334 + // Parse the UID
1.335 + iUid = TUid::Uid(ParseTagInt(aTestActionSpec.iActionBody, KUid, pos, err));
1.336 + }
1.337 +
1.338 +void CRemoveApp::DoPerformActionL()
1.339 + {
1.340 + iAppManager->RemoveL(iUid);
1.341 + }
1.342 +
1.343 +/////////////////////////////////////////////////////////////////////////////////
1.344 +//CAppCount - Gets the number of applications
1.345 +/////////////////////////////////////////////////////////////////////////////////
1.346 +CAppCount::CAppCount(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
1.347 +: CCertAppTestAction(aFs, aConsole, aOut)
1.348 + {
1.349 + }
1.350 +
1.351 +void CAppCount::ConstructL(const TTestActionSpec& aTestActionSpec)
1.352 + {
1.353 + CCertAppTestAction::ConstructL(aTestActionSpec);
1.354 + TInt pos = 0;
1.355 + TInt err = KErrNone;
1.356 +
1.357 + // Parse the expected number of applications
1.358 + iCount = ParseTagInt(aTestActionSpec.iActionBody, KCount, pos, err);
1.359 + }
1.360 +
1.361 +void CAppCount::DoPerformActionL()
1.362 + {
1.363 + if (iCount != iAppManager->Applications().Count())
1.364 + User::Leave(KErrArgument);
1.365 + }
1.366 +
1.367 +/////////////////////////////////////////////////////////////////////////////////
1.368 +//CGetApp - Gets an application with a given ID
1.369 +/////////////////////////////////////////////////////////////////////////////////
1.370 +CGetApp::CGetApp(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
1.371 +: CCertAppTestAction(aFs, aConsole, aOut)
1.372 + {
1.373 + }
1.374 +
1.375 +void CGetApp::ConstructL(const TTestActionSpec& aTestActionSpec)
1.376 + {
1.377 + CCertAppTestAction::ConstructL(aTestActionSpec);
1.378 + TInt pos = 0;
1.379 + TInt err = KErrNone;
1.380 +
1.381 + // Parse the UID to retrieve and the name to expect
1.382 + iUid = TUid::Uid(ParseTagInt(aTestActionSpec.iActionBody, KUid, pos, err));
1.383 + iName.Copy(ParseTagString(aTestActionSpec.iActionBody, KAppName, pos, err));
1.384 + }
1.385 +
1.386 +void CGetApp::DoPerformActionL()
1.387 + {
1.388 + TCertificateAppInfo app;
1.389 + TInt index;
1.390 + app = iAppManager->ApplicationL(iUid, index);
1.391 +
1.392 + if (app.Name() != iName)
1.393 + User::Leave(KErrCorrupt);
1.394 + }
1.395 +
1.396 +/////////////////////////////////////////////////////////////////////////////////
1.397 +//CGetApplications - Gets the applications and compares with what is expected
1.398 +/////////////////////////////////////////////////////////////////////////////////
1.399 +CGetApplications::CGetApplications(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
1.400 +: CCertAppTestAction(aFs, aConsole, aOut)
1.401 + {
1.402 + }
1.403 +
1.404 +CGetApplications::~CGetApplications()
1.405 + {
1.406 + iAppArray.Close();
1.407 + }
1.408 +
1.409 +void CGetApplications::ConstructL(const TTestActionSpec& aTestActionSpec)
1.410 + {
1.411 + CCertAppTestAction::ConstructL(aTestActionSpec);
1.412 + TInt pos = 0;
1.413 + TInt err = KErrNone;
1.414 +
1.415 + // Parse the UID and name for the new app
1.416 + do
1.417 + {
1.418 + TUid uid(TUid::Uid(ParseTagInt(aTestActionSpec.iActionBody, KUid, pos, err)));
1.419 + if (!err)
1.420 + {
1.421 + TName name;
1.422 + name.Copy(ParseTagString(aTestActionSpec.iActionBody, KAppName, pos, err));
1.423 + iAppArray.Append(TCertificateAppInfo(uid, name));
1.424 + }
1.425 + }
1.426 + while (!err);
1.427 + }
1.428 +
1.429 +void CGetApplications::DoPerformActionL()
1.430 + {
1.431 + const RArray<TCertificateAppInfo>& recArray = iAppManager->Applications();
1.432 + TInt count = iAppArray.Count();
1.433 +
1.434 + if (count != recArray.Count())
1.435 + {
1.436 + User::Leave(KErrArgument);
1.437 + }
1.438 +
1.439 + for (TInt i = 0 ; i < count ; ++i)
1.440 + {
1.441 + TInt j;
1.442 + for (j = 0 ; j < count ; ++j)
1.443 + {
1.444 + if ((iAppArray[i].Id() == recArray[j].Id()) &&
1.445 + (iAppArray[i].Name() == recArray[j].Name()))
1.446 + {
1.447 + break;
1.448 + }
1.449 + }
1.450 +
1.451 + if (j == count)
1.452 + {
1.453 + // If we get to the end of recArray and there is no match then
1.454 + // the arrays definitely do not match
1.455 + User::Leave(KErrArgument);
1.456 + }
1.457 + }
1.458 + }