os/security/cryptoservices/filebasedcertificateandkeystores/test/tcertapps/t_certapps_actions.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2005-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 /**
    20  @file
    21 */
    22 
    23 #include "t_certapps_actions.h"
    24 #include "t_certapps_defs.h"
    25 #include "t_input.h"
    26 #include "t_output.h"
    27 
    28 #include "certificateapps.h"
    29 
    30 /////////////////////////////////////////////////////////////////////////////////
    31 //CCertAppTestAction base class
    32 /////////////////////////////////////////////////////////////////////////////////
    33 
    34 // singleton instance of the manager
    35 CCertificateAppInfoManager* CCertAppTestAction::iAppManager = NULL;
    36 
    37 CCertAppTestAction::~CCertAppTestAction()
    38 	{
    39 	}
    40 
    41 
    42 CCertAppTestAction::CCertAppTestAction(RFs& aFs, CConsoleBase& aConsole, 
    43 										 Output& aOut)
    44 :	CTestAction(aConsole, aOut), iFs(aFs)
    45 	{
    46 	}
    47 
    48 void CCertAppTestAction::ConstructL(const TTestActionSpec& aTestActionSpec)
    49 	{
    50 	CTestAction::ConstructL(aTestActionSpec);
    51 	TInt pos = 0, error = 0;
    52 	SetExpectedResult(ParseTagString(aTestActionSpec.iActionResult, KReturn, pos, error));
    53 	}
    54 
    55 void CCertAppTestAction::InitialiseL(TBool& /*aMemFailureFlag*/,
    56 									 TBool& /*aCancel*/, TInt& /*aHeapMark*/, TInt& /*aHeapMarkEnd*/)
    57 	{
    58 	}
    59 
    60 TPtrC8 CCertAppTestAction::ParseTagString(const TDesC8& aBuf, const TDesC8& aTagName, TInt& aPos, TInt& aError)
    61 	{
    62 	// wrapper around the Input::ParseElement function. Processes aTagName
    63 	// and produces <aTagName> and </aTagName> used by ParseElement
    64 	TBuf8<64> start(0);
    65 	TBuf8<64> end(0);
    66 
    67 	start.Append('<');
    68 	start.Append(aTagName);
    69 	start.Append('>');
    70 
    71 	end.Append('<');
    72 	end.Append('/');
    73 	end.Append(aTagName);
    74 	end.Append('>');
    75 
    76 	return Input::ParseElement(aBuf, start, end, aPos, aError);
    77 	}
    78 
    79 TInt32 CCertAppTestAction::ParseTagInt(const TDesC8& aBuf, const TDesC8& aTagName, TInt& aPos, TInt& aError)
    80 	{
    81 	TPtrC8 ptr(ParseTagString(aBuf, aTagName, aPos, aError));
    82 
    83 	// Get an integer value out of it
    84 	TInt32 retVal = 0;
    85 	TLex8 lex(ptr);
    86 	lex.Val(retVal);
    87 	return retVal;
    88 	}
    89 
    90 
    91 void CCertAppTestAction::SetExpectedResult(const TDesC8& aResult)
    92 	{
    93 	if (aResult == _L8("KErrNone") || aResult == KNullDesC8)
    94 		{
    95 		iExpectedResult = KErrNone;
    96 		}
    97 	else if (aResult == _L8("KErrAccessDenied"))
    98 		{
    99 		iExpectedResult = KErrAccessDenied;
   100 		}
   101 	else if (aResult == _L8("KErrNotReady"))
   102 		{
   103 		iExpectedResult = KErrNotReady;
   104 		}
   105 	else if (aResult == _L8("KErrAlreadyExists"))
   106 		{
   107 		iExpectedResult = KErrAlreadyExists;
   108 		}
   109 	else if (aResult == _L8("KErrInUse"))
   110 		{
   111 		iExpectedResult = KErrInUse;
   112 		}
   113 	else if (aResult == _L8("KErrNotFound"))
   114 		{
   115 		iExpectedResult = KErrNotFound;
   116 		}
   117 	else if (aResult == _L8("KErrBadName"))
   118 		{
   119 		iExpectedResult = KErrBadName;
   120 		}
   121 	else if (aResult == _L8("KErrArgument"))
   122 		{
   123 		iExpectedResult = KErrArgument;
   124 		}
   125 	else if (aResult == _L8("KErrNotReady"))
   126 		{
   127 		iExpectedResult = KErrNotReady;
   128 		}
   129 	else if (aResult == _L8("KErrCorrupt"))
   130 		{
   131 		iExpectedResult = KErrCorrupt;
   132 		}
   133 	else if (aResult == _L8("KErrPermissionDenied"))
   134 		{
   135 		iExpectedResult = KErrPermissionDenied;
   136 		}
   137 	else
   138 		{
   139 		iOut.write(_L("Unrecognised error code: "));
   140 		iOut.writeString(aResult);
   141 		iOut.writeNewLine();
   142 		User::Leave(KErrArgument);
   143 		}
   144 	}
   145 
   146 void CCertAppTestAction::PerformAction(TRequestStatus& aStatus)
   147 	{
   148 	TInt err = KErrNone;
   149 	TRAP(err, DoPerformActionL());
   150 
   151 	if (err != KErrNoMemory)
   152 		{
   153 		iFinished = ETrue;
   154 		}
   155 	
   156 	TRequestStatus* status = &aStatus;
   157 	User::RequestComplete(status, err);
   158 	}
   159 
   160 void CCertAppTestAction::DoPerformPrerequisite(TRequestStatus& aStatus)
   161 	{
   162 	TInt err = KErrNone;
   163 
   164 	// For all tests (with the exception of InitManager which overrides this
   165 	// method), we check to make sure that the AppManager is set
   166 	if (iAppManager)
   167 		{
   168 		iActionState = EAction;
   169 		}
   170 	else
   171 		{
   172 		iFinished = ETrue;
   173 		err = KErrNotFound;
   174 		}
   175 
   176 	TRequestStatus* status = &aStatus;
   177 	User::RequestComplete(status, err);
   178 	}
   179 
   180 void CCertAppTestAction::DoPerformPostrequisite(TRequestStatus& aStatus)
   181 	{
   182 	TRequestStatus* status = &aStatus;
   183 	User::RequestComplete(status, KErrNone);
   184 	}
   185 
   186 void CCertAppTestAction::PerformCancel()
   187 	{
   188 	}
   189 
   190 void CCertAppTestAction::Reset()
   191 	{
   192 	}
   193 
   194 void CCertAppTestAction::DoReportAction()
   195 	{
   196 	}
   197 
   198 void CCertAppTestAction::DoCheckResult(TInt aError)
   199 	{
   200 	iResult = (aError == iExpectedResult);
   201 	}
   202 
   203 
   204 /////////////////////////////////////////////////////////////////////////////////
   205 //CInitManager - initialises the singleton manager
   206 /////////////////////////////////////////////////////////////////////////////////
   207 CInitManager::CInitManager(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
   208 :	CCertAppTestAction(aFs, aConsole, aOut)
   209 	{
   210 	}
   211 
   212 void CInitManager::DoPerformPrerequisite(TRequestStatus& aStatus)
   213 	{
   214 	TInt err = KErrNone;
   215 
   216 	// If it does not exist, then we are OK
   217 	if (!iAppManager)
   218 		{
   219 		iActionState = EAction;
   220 		}
   221 	else
   222 		{
   223 		iFinished = ETrue;
   224 		err = KErrAlreadyExists;
   225 		}
   226 
   227 	TRequestStatus* status = &aStatus;
   228 	User::RequestComplete(status, err);
   229 	}
   230 
   231 void CInitManager::DoPerformActionL()
   232 	{
   233 	iAppManager = CCertificateAppInfoManager::NewL();
   234 	}
   235 
   236 /////////////////////////////////////////////////////////////////////////////////
   237 //CDestroyManager - destroys the singleton manager
   238 /////////////////////////////////////////////////////////////////////////////////
   239 CDestroyManager::CDestroyManager(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
   240 :	CCertAppTestAction(aFs, aConsole, aOut)
   241 	{
   242 	}
   243 
   244 void CDestroyManager::DoPerformActionL()
   245 	{
   246 	delete iAppManager;
   247 	iAppManager = NULL;
   248 	}
   249 
   250 
   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)
   256 	{
   257 	}
   258 
   259 void CClearAllApps::DoPerformActionL()
   260 	{
   261 	for (;;)
   262 		{
   263 		// apps is owned by the app manager, don't try accessing it after we've
   264 		// removed any of the applications though
   265 		
   266 		RArray<TCertificateAppInfo> apps;
   267 		apps = iAppManager->Applications();
   268 		if (apps.Count() == 0)
   269 			{
   270 			break;
   271 			}
   272 		iAppManager->RemoveL(apps[0].Id());
   273 		}
   274 	}
   275 
   276 /////////////////////////////////////////////////////////////////////////////////
   277 //CAddApp - adds applications
   278 /////////////////////////////////////////////////////////////////////////////////
   279 CAddApp::CAddApp(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
   280 :	CCertAppTestAction(aFs, aConsole, aOut)
   281 	{
   282 	}
   283 
   284 CAddApp::~CAddApp()
   285 	{
   286 	iAppArray.Close();
   287 	}
   288 
   289 void CAddApp::ConstructL(const TTestActionSpec& aTestActionSpec)
   290 	{
   291 	CCertAppTestAction::ConstructL(aTestActionSpec);
   292 	TInt pos = 0;
   293 	TInt err = KErrNone;
   294 
   295 	// Parse the UID and name for the new app
   296 	do
   297 		{
   298 		TUid uid(TUid::Uid(ParseTagInt(aTestActionSpec.iActionBody, KUid, pos, err)));
   299 		if (!err)
   300 			{
   301 			TName name;
   302 			name.Copy(ParseTagString(aTestActionSpec.iActionBody, KAppName, pos, err));
   303 			iAppArray.Append(TCertificateAppInfo(uid, name));
   304 			}
   305 		}
   306 	while (!err);
   307 	}
   308 
   309 void CAddApp::DoPerformActionL()
   310 	{
   311 	for (TInt i = 0; i < iAppArray.Count(); ++i)
   312 		{
   313 		iAppManager->AddL(iAppArray[i]);
   314 		}
   315 	}
   316 
   317 /////////////////////////////////////////////////////////////////////////////////
   318 //CRemoveApp - removes an application
   319 /////////////////////////////////////////////////////////////////////////////////
   320 CRemoveApp::CRemoveApp(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
   321 :	CCertAppTestAction(aFs, aConsole, aOut)
   322 	{
   323 	}
   324 	
   325 void CRemoveApp::ConstructL(const TTestActionSpec& aTestActionSpec)
   326 	{
   327 	CCertAppTestAction::ConstructL(aTestActionSpec);
   328 	TInt pos = 0;
   329 	TInt err = KErrNone;
   330 
   331 	// Parse the UID
   332 	iUid = TUid::Uid(ParseTagInt(aTestActionSpec.iActionBody, KUid, pos, err));
   333 	}
   334 
   335 void CRemoveApp::DoPerformActionL()
   336 	{
   337 	iAppManager->RemoveL(iUid);
   338 	}
   339 
   340 /////////////////////////////////////////////////////////////////////////////////
   341 //CAppCount - Gets the number of applications
   342 /////////////////////////////////////////////////////////////////////////////////
   343 CAppCount::CAppCount(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
   344 :	CCertAppTestAction(aFs, aConsole, aOut)
   345 	{
   346 	}
   347 	
   348 void CAppCount::ConstructL(const TTestActionSpec& aTestActionSpec)
   349 	{
   350 	CCertAppTestAction::ConstructL(aTestActionSpec);
   351 	TInt pos = 0;
   352 	TInt err = KErrNone;
   353 
   354 	// Parse the expected number of applications
   355 	iCount = ParseTagInt(aTestActionSpec.iActionBody, KCount, pos, err);
   356 	}
   357 
   358 void CAppCount::DoPerformActionL()
   359 	{
   360 	if (iCount != iAppManager->Applications().Count())
   361 		User::Leave(KErrArgument);
   362 	}
   363 
   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)
   369 	{
   370 	}
   371 	
   372 void CGetApp::ConstructL(const TTestActionSpec& aTestActionSpec)
   373 	{
   374 	CCertAppTestAction::ConstructL(aTestActionSpec);
   375 	TInt pos = 0;
   376 	TInt err = KErrNone;
   377 
   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));
   381 	}
   382 
   383 void CGetApp::DoPerformActionL()
   384 	{
   385 	TCertificateAppInfo app;
   386 	TInt index;
   387 	app = iAppManager->ApplicationL(iUid, index);
   388 
   389 	if (app.Name() != iName)
   390 		User::Leave(KErrCorrupt);
   391 	}
   392 
   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)
   398 	{
   399 	}
   400 
   401 CGetApplications::~CGetApplications()
   402 	{
   403 	iAppArray.Close();
   404 	}
   405 
   406 void CGetApplications::ConstructL(const TTestActionSpec& aTestActionSpec)
   407 	{
   408 	CCertAppTestAction::ConstructL(aTestActionSpec);
   409 	TInt pos = 0;
   410 	TInt err = KErrNone;
   411 
   412 	// Parse the UID and name for the new app
   413 	do
   414 		{
   415 		TUid uid(TUid::Uid(ParseTagInt(aTestActionSpec.iActionBody, KUid, pos, err)));
   416 		if (!err)
   417 			{
   418 			TName name;
   419 			name.Copy(ParseTagString(aTestActionSpec.iActionBody, KAppName, pos, err));
   420 			iAppArray.Append(TCertificateAppInfo(uid, name));
   421 			}
   422 		}
   423 	while (!err);
   424 	}
   425 
   426 void CGetApplications::DoPerformActionL()
   427 	{
   428 	const RArray<TCertificateAppInfo>& recArray = iAppManager->Applications();
   429 	TInt count = iAppArray.Count();
   430 
   431 	if (count != recArray.Count())
   432 		{
   433 		User::Leave(KErrArgument);
   434 		}
   435 
   436 	for (TInt i = 0 ; i < count ; ++i)
   437 		{
   438 		TInt j;
   439 		for (j = 0 ; j < count ; ++j)
   440 			{
   441 			if ((iAppArray[i].Id() == recArray[j].Id()) &&
   442 				(iAppArray[i].Name() == recArray[j].Name()))
   443 				{
   444 				break;
   445 				}
   446 			}
   447 
   448 		if (j == count)
   449 			{
   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);
   453 			}
   454 		}
   455 	}