os/security/cryptoservices/certificateandkeymgmt/tcertstore/t_swicertstoreactions.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 // t_metadataactions.cpp
    24 #include <e32std.h>
    25 #include <e32base.h>
    26 #include <s32file.h>
    27 #include "t_swicertstoreactions.h"
    28 #include "t_certstoredefs.h"
    29 #include "t_certstoreout.h"
    30 #include "t_input.h"
    31 #include <swicertstore.h>
    32 #include "testutilclient.h"
    33 
    34 TBool operator==(const TCapabilitySet& a, const TCapabilitySet& b)
    35 	{
    36 	return a.HasCapabilities(b) && b.HasCapabilities(a);
    37 	}
    38 
    39 // CInitSWICertStoreAction /////////////////////////////////////////////////////
    40 
    41 CTestAction* CInitSWICertStoreAction::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut,
    42 										   const TTestActionSpec& aTestActionSpec)
    43 	{
    44 	CInitSWICertStoreAction* self = new (ELeave) CInitSWICertStoreAction(aFs, aConsole, aOut);
    45 	CleanupStack::PushL(self);
    46 	self->ConstructL(aTestActionSpec);
    47 	CleanupStack::Pop(self);
    48 	return self;
    49 	}
    50 
    51 CInitSWICertStoreAction::CInitSWICertStoreAction(RFs& aFs, CConsoleBase& aConsole, Output& aOut) :
    52 	CCertStoreTestAction(aFs, aConsole, aOut), iFs(aFs)
    53 	{
    54 	}
    55 
    56 void CInitSWICertStoreAction::ConstructL(const TTestActionSpec& aTestActionSpec)
    57 	{
    58 	CCertStoreTestAction::ConstructL(aTestActionSpec);
    59 	
    60 	TInt err = KErrNone;
    61 	TInt pos = 0;
    62 	TPtrC8 fileName = Input::ParseElement(aTestActionSpec.iActionBody, KFileNameStart, 
    63 		KFileNameEnd, pos, err);
    64 	
    65 	if (!err)
    66 			{
    67 			HBufC* tempFName = HBufC::NewLC(fileName.Length());
    68 
    69 			tempFName->Des().Copy(fileName);
    70 
    71 			RTestUtilSession testutil;
    72 			User::LeaveIfError(testutil.Connect());
    73 			CleanupClosePushL(testutil);
    74 			
    75 			TDriveUnit sysDrive(RFs::GetSystemDrive());
    76 			TBuf<64> datfile (sysDrive.Name());
    77 			datfile.Append(_L("\\Resource\\SwiCertstore\\dat\\"));
    78 			testutil.MkDirAll(datfile);
    79 			
    80 			datfile.Copy(sysDrive.Name());
    81 			datfile.Append(_L("\\Resource\\SwiCertstore\\dat\\00000001"));
    82 			err = testutil.Copy(tempFName->Des(), datfile);
    83 			CleanupStack::PopAndDestroy(&testutil);
    84 			CleanupStack::PopAndDestroy(tempFName);
    85 			}
    86 
    87 	HBufC* result = HBufC::NewLC(aTestActionSpec.iActionResult.Length());
    88 	TPtr(result->Des()).Copy(aTestActionSpec.iActionResult);
    89 	Input::GetExpectedResultL(Input::ParseElement(*result, KReturnStart, KReturnEnd), iExpectedResult);
    90 	CleanupStack::PopAndDestroy(result);
    91 	}
    92 
    93 void CInitSWICertStoreAction::PerformAction(TRequestStatus& aStatus)
    94 	{
    95 	switch (iState)
    96 		{
    97 		case EInitCertStore:
    98 			{
    99 			iState = EFinished;
   100 			CSWICertStore* swiCertStore = CSWICertStore::NewL(iFs);
   101 			CleanupReleasePushL(*swiCertStore);
   102 			AddCertStoreL(swiCertStore);
   103 			CleanupStack::Pop(swiCertStore);
   104 			TRequestStatus* status = &aStatus;
   105 			User::RequestComplete(status, KErrNone);
   106 			}
   107 			break;
   108 
   109 		case EFinished:
   110 			{
   111 			TRequestStatus* status = &aStatus;
   112 			User::RequestComplete(status, KErrNone);
   113 			iResult = EFalse;
   114 			iFinished = ETrue;
   115 			}
   116 			break;
   117 		}
   118 	}
   119 
   120 void CInitSWICertStoreAction::PerformCancel()
   121 	{
   122 	}
   123 
   124 void CInitSWICertStoreAction::Reset()
   125 	{
   126 	iState = EInitCertStore;
   127 	}
   128 
   129 void CInitSWICertStoreAction::DoReportAction()
   130 	{
   131 	iOut.write(_L("Initialising SWI cert store...\n"));
   132 	}
   133 
   134 void CInitSWICertStoreAction::DoCheckResult(TInt aError)
   135 	{
   136 	iResult = (aError == iExpectedResult);
   137 	}
   138 
   139 // CSWICertStoreTestAction /////////////////////////////////////////////////////
   140 
   141 CSWICertStoreTestAction::CSWICertStoreTestAction(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
   142 	: CCertStoreTestAction(aFs, aConsole, aOut), iState(EGetCert)
   143 	{
   144 	}
   145 
   146 void CSWICertStoreTestAction::ConstructL(const TTestActionSpec& aTestActionSpec)
   147 	{
   148 	CCertStoreTestAction::ConstructL(aTestActionSpec);
   149 	iFilter = CCertAttributeFilter::NewL();
   150 	TInt err = KErrNone;
   151 	TInt pos = 0;
   152 	
   153 	TPtrC8 pLabel = Input::ParseElement(aTestActionSpec.iActionBody, KCertLabelStart, KCertLabelEnd, pos, err);
   154 	if (err != KErrNone)
   155 		{
   156 		User::Leave(err);
   157 		}
   158 	iCertificateLabel.Copy(pLabel);
   159 
   160 	HBufC* result = HBufC::NewLC(aTestActionSpec.iActionResult.Length());
   161 	TPtr(result->Des()).Copy(aTestActionSpec.iActionResult);
   162 	Input::GetExpectedResultL(Input::ParseElement(*result, KReturnStart, KReturnEnd), iExpectedResult);
   163 	CleanupStack::PopAndDestroy(result);
   164 
   165 	DoConstructL(aTestActionSpec);
   166 	}
   167 
   168 CSWICertStoreTestAction::~CSWICertStoreTestAction()
   169 	{
   170 	iCertInfos.Close();
   171 	delete iFilter;
   172 	}
   173 
   174 void CSWICertStoreTestAction::PerformAction(TRequestStatus& aStatus)
   175 	{
   176 	switch (iState)
   177 		{
   178 		case EGetCert:
   179 			iState = EPerformAction;
   180 			SWICertStore().List(iCertInfos, *iFilter, aStatus);
   181 			break;
   182 			
   183 		case EPerformAction:
   184 			{
   185 			TInt err = KErrNotFound;
   186 			iState = EFinished;
   187 			
   188 			CCTCertInfo* certInfo = FindCertificate();
   189 			if (certInfo)
   190 				{
   191 				err = KErrNone;
   192 				DoPerformActionL(*certInfo);
   193 				}
   194 			
   195 			TRequestStatus* status = &aStatus;
   196 			User::RequestComplete(status, err);
   197 			}	
   198 			break;
   199 			
   200 		case EFinished:
   201 			{
   202 			TRequestStatus* status = &aStatus;
   203 			User::RequestComplete(status, aStatus.Int());
   204 			iResult = EFalse; // Must be set in DoCheckResult
   205             iFinished = ETrue;
   206 			}
   207 			break;
   208 		}
   209 	}
   210 
   211 void CSWICertStoreTestAction::PerformCancel()
   212 	{
   213 	switch (iState)
   214 		{
   215 		case EGetCert:
   216 			SWICertStore().CancelList();
   217 			break;
   218 
   219 		case EPerformAction:
   220 			DoCancel();
   221 			break;
   222 
   223 		default:
   224 			// do nothing
   225 			break;
   226 		}
   227 	}
   228 
   229 void CSWICertStoreTestAction::Reset()
   230 	{
   231     iCertInfos.Close();
   232     iState = EGetCert;
   233 	}
   234 
   235 CCTCertInfo* CSWICertStoreTestAction::FindCertificate()
   236 	{
   237 	TInt count = iCertInfos.Count();
   238 	for (TInt i = 0; i < count; i++)
   239 		{
   240 		CCTCertInfo* info = iCertInfos[i];
   241 		if (info->Label() == iCertificateLabel)
   242 			{
   243 			return info;
   244 			}
   245 		}
   246 	return NULL;
   247 	}
   248 
   249 // CGetCapabilitiesAction //////////////////////////////////////////////////////
   250 
   251 CTestAction* CGetCapabilitiesAction::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut,
   252 										  const TTestActionSpec& aTestActionSpec)
   253 	{
   254 	CGetCapabilitiesAction* self = new (ELeave) CGetCapabilitiesAction(aFs, aConsole, aOut);
   255 	CleanupStack::PushL(self);
   256 	self->ConstructL(aTestActionSpec);
   257 	CleanupStack::Pop(self);
   258 	return self;
   259 	}
   260 
   261 CGetCapabilitiesAction::CGetCapabilitiesAction(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
   262 	: CSWICertStoreTestAction(aFs, aConsole, aOut)
   263 	{
   264 	}
   265 
   266 void CGetCapabilitiesAction::DoConstructL(const TTestActionSpec& aTestActionSpec)
   267 	{
   268 	if (iExpectedResult == KErrNone)
   269 		{
   270 		Input::ParseCapabilitySetL(aTestActionSpec.iActionResult, iExpectedCapapbilities);
   271 		}
   272 	}
   273 
   274 void CGetCapabilitiesAction::DoReportAction()
   275 	{
   276 	iOut.writeString(_L("Getting certificate capabilities, expecting: "));
   277 	iOut.writeCapabilitySetL(iExpectedCapapbilities);
   278 	iOut.writeNewLine();
   279 	}
   280 
   281 void CGetCapabilitiesAction::DoPerformActionL(CCTCertInfo& aCertInfo)
   282 	{
   283 	iCapabilities = SWICertStore().CertMetaInfoL(aCertInfo).iCapabilities;
   284 	}
   285 
   286 void CGetCapabilitiesAction::DoCheckResult(TInt aError)
   287 	{
   288 	iResult = (aError == iExpectedResult &&
   289 			   iCapabilities == iExpectedCapapbilities);
   290 
   291 	if (aError == KErrNone)
   292 		{
   293 		iOut.writeString(_L("Certificate has capabilities: "));
   294 		iOut.writeCapabilitySetL(iCapabilities);
   295 		}
   296 	}
   297 
   298 void CGetCapabilitiesAction::DoCancel()
   299 	{
   300 	}
   301 
   302 // CGetMandatoryAction //////////////////////////////////////////////////////
   303 
   304 CTestAction* CGetMandatoryAction::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut,
   305 									   const TTestActionSpec& aTestActionSpec)
   306 	{
   307 	CGetMandatoryAction* self = new (ELeave) CGetMandatoryAction(aFs, aConsole, aOut);
   308 	CleanupStack::PushL(self);
   309 	self->ConstructL(aTestActionSpec);
   310 	CleanupStack::Pop(self);
   311 	return self;
   312 	}
   313 
   314 CGetMandatoryAction::CGetMandatoryAction(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
   315 	: CSWICertStoreTestAction(aFs, aConsole, aOut)
   316 	{
   317 	}
   318 
   319 void CGetMandatoryAction::DoConstructL(const TTestActionSpec& aTestActionSpec)
   320 	{
   321 	if (iExpectedResult == KErrNone)
   322 		{
   323 		iExpectedValue = Input::ParseElementBoolL(aTestActionSpec.iActionResult, KMandatoryStart);
   324 		}
   325 	}
   326 
   327 void CGetMandatoryAction::DoReportAction()
   328 	{
   329 	iOut.writeString(_L("Getting certificate mandatory flag, expecting: "));
   330 	iOut.writeBoolL(iExpectedValue);
   331 	iOut.writeNewLine();
   332 	}
   333 
   334 void CGetMandatoryAction::DoPerformActionL(CCTCertInfo& aCertInfo)
   335 	{
   336 	iValue = SWICertStore().CertMetaInfoL(aCertInfo).iIsMandatory;
   337 	}
   338 
   339 void CGetMandatoryAction::DoCheckResult(TInt aError)
   340 	{
   341 	iResult = (aError == iExpectedResult &&
   342 			   iValue == iExpectedValue);
   343 
   344 	if (aError == KErrNone)
   345 		{
   346 		iOut.writeString(_L("Certificate mandatory flag: "));
   347 		iOut.writeBoolL(iValue);
   348 		}
   349 	}
   350 
   351 void CGetMandatoryAction::DoCancel()
   352 	{
   353 	}
   354 
   355 // CGetSystemUpgradeAction //////////////////////////////////////////////////////
   356 
   357 CTestAction* CGetSystemUpgradeAction::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut,
   358 									   const TTestActionSpec& aTestActionSpec)
   359 	{
   360 	CGetSystemUpgradeAction* self = new (ELeave) CGetSystemUpgradeAction(aFs, aConsole, aOut);
   361 	CleanupStack::PushL(self);
   362 	self->ConstructL(aTestActionSpec);
   363 	CleanupStack::Pop(self);
   364 	return self;
   365 	}
   366 
   367 CGetSystemUpgradeAction::CGetSystemUpgradeAction(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
   368 	: CSWICertStoreTestAction(aFs, aConsole, aOut)
   369 	{
   370 	}
   371 
   372 void CGetSystemUpgradeAction::DoConstructL(const TTestActionSpec& aTestActionSpec)
   373 	{
   374 	if (iExpectedResult == KErrNone)
   375 		{
   376 		iExpectedSystemUpgradeValue = Input::ParseElementBoolL(aTestActionSpec.iActionResult, KSystemUpgradeStart);
   377 		}
   378 	}
   379 
   380 void CGetSystemUpgradeAction::DoReportAction()
   381 	{
   382 	iOut.writeString(_L("Getting certificate systemupgrade flag, expecting: "));
   383 	iOut.writeBoolL(iExpectedSystemUpgradeValue);
   384 	iOut.writeNewLine();
   385 	}
   386 
   387 void CGetSystemUpgradeAction::DoPerformActionL(CCTCertInfo& aCertInfo)
   388 	{
   389 	iSystemUpgradeValue = SWICertStore().CertMetaInfoL(aCertInfo).iIsSystemUpgrade;
   390 	}
   391 
   392 void CGetSystemUpgradeAction::DoCheckResult(TInt aError)
   393 	{
   394 	iResult = (aError == iExpectedResult &&
   395 			   iSystemUpgradeValue == iExpectedSystemUpgradeValue);
   396 
   397 	if (aError == KErrNone)
   398 		{
   399 		iOut.writeString(_L("Certificate systemupgrade flag: "));
   400 		iOut.writeBoolL(iSystemUpgradeValue);
   401 		}
   402 	}
   403 
   404 void CGetSystemUpgradeAction::DoCancel()
   405 	{
   406 	}
   407 
   408 // CGetDeletableAction //////////////////////////////////////////////////////
   409 
   410 CTestAction* CGetDeletionAction::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut,
   411 									   const TTestActionSpec& aTestActionSpec)
   412 	{
   413 	CGetDeletionAction* self = new (ELeave) CGetDeletionAction(aFs, aConsole, aOut);
   414 	CleanupStack::PushL(self);
   415 	self->ConstructL(aTestActionSpec);
   416 	CleanupStack::Pop(self);
   417 	return self;
   418 	}
   419 
   420 CGetDeletionAction::CGetDeletionAction(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
   421 	: CSWICertStoreTestAction(aFs, aConsole, aOut)
   422 	{
   423 	}
   424 
   425 void CGetDeletionAction::DoConstructL(const TTestActionSpec& aTestActionSpec)
   426 	{
   427 	if (iExpectedResult == KErrNone)
   428 		{
   429 		iExpectedDeletableValue = Input::ParseElementBoolL(aTestActionSpec.iActionResult, KDeletionStart);
   430 		}
   431 	}
   432 
   433 void CGetDeletionAction::DoReportAction()
   434 	{
   435 	iOut.writeString(_L("Getting certificate deletable flag, expecting: "));
   436 	iOut.writeBoolL(iExpectedDeletableValue);
   437 	iOut.writeNewLine();
   438 	}
   439 
   440 void CGetDeletionAction::DoPerformActionL(CCTCertInfo& aCertInfo)
   441 	{
   442 	  iDeletableValue = aCertInfo.IsDeletable();
   443 	}
   444 
   445 void CGetDeletionAction::DoCheckResult(TInt aError)
   446 	{
   447 	iResult = (aError == iExpectedResult &&
   448 			   iDeletableValue == iExpectedDeletableValue);
   449 
   450 	if (aError == KErrNone)
   451 		{
   452 		iOut.writeString(_L("Certificate Deletable flag: "));
   453 		iOut.writeBoolL(iDeletableValue);
   454 		}
   455 	}
   456 
   457 void CGetDeletionAction::DoCancel()
   458 	{
   459 	}
   460 
   461 
   462 // CMultipleReadersAction /////////////////////////////////////////////////////
   463 
   464 
   465 CTestAction* CMultipleReadersAction::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut,
   466 										  const TTestActionSpec& aTestActionSpec)
   467        {
   468        CMultipleReadersAction* self = new (ELeave) CMultipleReadersAction(aFs,aConsole, aOut);
   469        CleanupStack::PushL(self);
   470        self->ConstructL(aTestActionSpec);
   471        CleanupStack::Pop(self);
   472        return self;
   473        }
   474 
   475 CMultipleReadersAction::CMultipleReadersAction(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
   476        : CSWICertStoreTestAction(aFs, aConsole, aOut)
   477        {
   478        }
   479 
   480 void CMultipleReadersAction::DoConstructL(const TTestActionSpec& /* aTestActionSpec*/)
   481        {
   482        }
   483 
   484 void CMultipleReadersAction::DoReportAction()
   485        {
   486        iOut.writeString(_L("Checking opening multiple readers works, expecting:"));
   487        iOut.writeBoolL(true);
   488        iOut.writeNewLine();
   489        }
   490 
   491 void CMultipleReadersAction::DoPerformActionL(CCTCertInfo& /*aCertInfo*/)
   492        {
   493        CSWICertStore* swiCertStore = 0;
   494 
   495        TRAPD(err, swiCertStore = CSWICertStore::NewL(iFs));
   496        
   497        if (err == KErrNone && swiCertStore != 0)
   498                {
   499                swiCertStore->Release();
   500                }
   501          iValue = err;
   502        }
   503 
   504 void CMultipleReadersAction::DoCheckResult(TInt aError)
   505        {
   506        iResult = (aError == KErrNone &&
   507                           iValue == KErrNone);
   508        }
   509 
   510 void CMultipleReadersAction::DoCancel()
   511        {
   512        }