os/security/cryptoservices/filebasedcertificateandkeystores/test/tcertapps/t_certapps_actions.cpp
First public contribution.
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
23 #include "t_certapps_actions.h"
24 #include "t_certapps_defs.h"
28 #include "certificateapps.h"
30 /////////////////////////////////////////////////////////////////////////////////
31 //CCertAppTestAction base class
32 /////////////////////////////////////////////////////////////////////////////////
34 // singleton instance of the manager
35 CCertificateAppInfoManager* CCertAppTestAction::iAppManager = NULL;
37 CCertAppTestAction::~CCertAppTestAction()
42 CCertAppTestAction::CCertAppTestAction(RFs& aFs, CConsoleBase& aConsole,
44 : CTestAction(aConsole, aOut), iFs(aFs)
48 void CCertAppTestAction::ConstructL(const TTestActionSpec& aTestActionSpec)
50 CTestAction::ConstructL(aTestActionSpec);
51 TInt pos = 0, error = 0;
52 SetExpectedResult(ParseTagString(aTestActionSpec.iActionResult, KReturn, pos, error));
55 void CCertAppTestAction::InitialiseL(TBool& /*aMemFailureFlag*/,
56 TBool& /*aCancel*/, TInt& /*aHeapMark*/, TInt& /*aHeapMarkEnd*/)
60 TPtrC8 CCertAppTestAction::ParseTagString(const TDesC8& aBuf, const TDesC8& aTagName, TInt& aPos, TInt& aError)
62 // wrapper around the Input::ParseElement function. Processes aTagName
63 // and produces <aTagName> and </aTagName> used by ParseElement
68 start.Append(aTagName);
76 return Input::ParseElement(aBuf, start, end, aPos, aError);
79 TInt32 CCertAppTestAction::ParseTagInt(const TDesC8& aBuf, const TDesC8& aTagName, TInt& aPos, TInt& aError)
81 TPtrC8 ptr(ParseTagString(aBuf, aTagName, aPos, aError));
83 // Get an integer value out of it
91 void CCertAppTestAction::SetExpectedResult(const TDesC8& aResult)
93 if (aResult == _L8("KErrNone") || aResult == KNullDesC8)
95 iExpectedResult = KErrNone;
97 else if (aResult == _L8("KErrAccessDenied"))
99 iExpectedResult = KErrAccessDenied;
101 else if (aResult == _L8("KErrNotReady"))
103 iExpectedResult = KErrNotReady;
105 else if (aResult == _L8("KErrAlreadyExists"))
107 iExpectedResult = KErrAlreadyExists;
109 else if (aResult == _L8("KErrInUse"))
111 iExpectedResult = KErrInUse;
113 else if (aResult == _L8("KErrNotFound"))
115 iExpectedResult = KErrNotFound;
117 else if (aResult == _L8("KErrBadName"))
119 iExpectedResult = KErrBadName;
121 else if (aResult == _L8("KErrArgument"))
123 iExpectedResult = KErrArgument;
125 else if (aResult == _L8("KErrNotReady"))
127 iExpectedResult = KErrNotReady;
129 else if (aResult == _L8("KErrCorrupt"))
131 iExpectedResult = KErrCorrupt;
133 else if (aResult == _L8("KErrPermissionDenied"))
135 iExpectedResult = KErrPermissionDenied;
139 iOut.write(_L("Unrecognised error code: "));
140 iOut.writeString(aResult);
142 User::Leave(KErrArgument);
146 void CCertAppTestAction::PerformAction(TRequestStatus& aStatus)
149 TRAP(err, DoPerformActionL());
151 if (err != KErrNoMemory)
156 TRequestStatus* status = &aStatus;
157 User::RequestComplete(status, err);
160 void CCertAppTestAction::DoPerformPrerequisite(TRequestStatus& aStatus)
164 // For all tests (with the exception of InitManager which overrides this
165 // method), we check to make sure that the AppManager is set
168 iActionState = EAction;
176 TRequestStatus* status = &aStatus;
177 User::RequestComplete(status, err);
180 void CCertAppTestAction::DoPerformPostrequisite(TRequestStatus& aStatus)
182 TRequestStatus* status = &aStatus;
183 User::RequestComplete(status, KErrNone);
186 void CCertAppTestAction::PerformCancel()
190 void CCertAppTestAction::Reset()
194 void CCertAppTestAction::DoReportAction()
198 void CCertAppTestAction::DoCheckResult(TInt aError)
200 iResult = (aError == iExpectedResult);
204 /////////////////////////////////////////////////////////////////////////////////
205 //CInitManager - initialises the singleton manager
206 /////////////////////////////////////////////////////////////////////////////////
207 CInitManager::CInitManager(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
208 : CCertAppTestAction(aFs, aConsole, aOut)
212 void CInitManager::DoPerformPrerequisite(TRequestStatus& aStatus)
216 // If it does not exist, then we are OK
219 iActionState = EAction;
224 err = KErrAlreadyExists;
227 TRequestStatus* status = &aStatus;
228 User::RequestComplete(status, err);
231 void CInitManager::DoPerformActionL()
233 iAppManager = CCertificateAppInfoManager::NewL();
236 /////////////////////////////////////////////////////////////////////////////////
237 //CDestroyManager - destroys the singleton manager
238 /////////////////////////////////////////////////////////////////////////////////
239 CDestroyManager::CDestroyManager(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
240 : CCertAppTestAction(aFs, aConsole, aOut)
244 void CDestroyManager::DoPerformActionL()
251 /////////////////////////////////////////////////////////////////////////////////
252 //CClearAllApps - removes all the applications from the app list
253 /////////////////////////////////////////////////////////////////////////////////
254 CClearAllApps::CClearAllApps(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
255 : CCertAppTestAction(aFs, aConsole, aOut)
259 void CClearAllApps::DoPerformActionL()
263 // apps is owned by the app manager, don't try accessing it after we've
264 // removed any of the applications though
266 RArray<TCertificateAppInfo> apps;
267 apps = iAppManager->Applications();
268 if (apps.Count() == 0)
272 iAppManager->RemoveL(apps[0].Id());
276 /////////////////////////////////////////////////////////////////////////////////
277 //CAddApp - adds applications
278 /////////////////////////////////////////////////////////////////////////////////
279 CAddApp::CAddApp(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
280 : CCertAppTestAction(aFs, aConsole, aOut)
289 void CAddApp::ConstructL(const TTestActionSpec& aTestActionSpec)
291 CCertAppTestAction::ConstructL(aTestActionSpec);
295 // Parse the UID and name for the new app
298 TUid uid(TUid::Uid(ParseTagInt(aTestActionSpec.iActionBody, KUid, pos, err)));
302 name.Copy(ParseTagString(aTestActionSpec.iActionBody, KAppName, pos, err));
303 iAppArray.Append(TCertificateAppInfo(uid, name));
309 void CAddApp::DoPerformActionL()
311 for (TInt i = 0; i < iAppArray.Count(); ++i)
313 iAppManager->AddL(iAppArray[i]);
317 /////////////////////////////////////////////////////////////////////////////////
318 //CRemoveApp - removes an application
319 /////////////////////////////////////////////////////////////////////////////////
320 CRemoveApp::CRemoveApp(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
321 : CCertAppTestAction(aFs, aConsole, aOut)
325 void CRemoveApp::ConstructL(const TTestActionSpec& aTestActionSpec)
327 CCertAppTestAction::ConstructL(aTestActionSpec);
332 iUid = TUid::Uid(ParseTagInt(aTestActionSpec.iActionBody, KUid, pos, err));
335 void CRemoveApp::DoPerformActionL()
337 iAppManager->RemoveL(iUid);
340 /////////////////////////////////////////////////////////////////////////////////
341 //CAppCount - Gets the number of applications
342 /////////////////////////////////////////////////////////////////////////////////
343 CAppCount::CAppCount(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
344 : CCertAppTestAction(aFs, aConsole, aOut)
348 void CAppCount::ConstructL(const TTestActionSpec& aTestActionSpec)
350 CCertAppTestAction::ConstructL(aTestActionSpec);
354 // Parse the expected number of applications
355 iCount = ParseTagInt(aTestActionSpec.iActionBody, KCount, pos, err);
358 void CAppCount::DoPerformActionL()
360 if (iCount != iAppManager->Applications().Count())
361 User::Leave(KErrArgument);
364 /////////////////////////////////////////////////////////////////////////////////
365 //CGetApp - Gets an application with a given ID
366 /////////////////////////////////////////////////////////////////////////////////
367 CGetApp::CGetApp(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
368 : CCertAppTestAction(aFs, aConsole, aOut)
372 void CGetApp::ConstructL(const TTestActionSpec& aTestActionSpec)
374 CCertAppTestAction::ConstructL(aTestActionSpec);
378 // Parse the UID to retrieve and the name to expect
379 iUid = TUid::Uid(ParseTagInt(aTestActionSpec.iActionBody, KUid, pos, err));
380 iName.Copy(ParseTagString(aTestActionSpec.iActionBody, KAppName, pos, err));
383 void CGetApp::DoPerformActionL()
385 TCertificateAppInfo app;
387 app = iAppManager->ApplicationL(iUid, index);
389 if (app.Name() != iName)
390 User::Leave(KErrCorrupt);
393 /////////////////////////////////////////////////////////////////////////////////
394 //CGetApplications - Gets the applications and compares with what is expected
395 /////////////////////////////////////////////////////////////////////////////////
396 CGetApplications::CGetApplications(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
397 : CCertAppTestAction(aFs, aConsole, aOut)
401 CGetApplications::~CGetApplications()
406 void CGetApplications::ConstructL(const TTestActionSpec& aTestActionSpec)
408 CCertAppTestAction::ConstructL(aTestActionSpec);
412 // Parse the UID and name for the new app
415 TUid uid(TUid::Uid(ParseTagInt(aTestActionSpec.iActionBody, KUid, pos, err)));
419 name.Copy(ParseTagString(aTestActionSpec.iActionBody, KAppName, pos, err));
420 iAppArray.Append(TCertificateAppInfo(uid, name));
426 void CGetApplications::DoPerformActionL()
428 const RArray<TCertificateAppInfo>& recArray = iAppManager->Applications();
429 TInt count = iAppArray.Count();
431 if (count != recArray.Count())
433 User::Leave(KErrArgument);
436 for (TInt i = 0 ; i < count ; ++i)
439 for (j = 0 ; j < count ; ++j)
441 if ((iAppArray[i].Id() == recArray[j].Id()) &&
442 (iAppArray[i].Name() == recArray[j].Name()))
450 // If we get to the end of recArray and there is no match then
451 // the arrays definitely do not match
452 User::Leave(KErrArgument);