os/security/cryptoservices/certificateandkeymgmt/tcertstore/t_swicertstoreactions.cpp
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/tcertstore/t_swicertstoreactions.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,512 @@
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 +// t_metadataactions.cpp
1.27 +#include <e32std.h>
1.28 +#include <e32base.h>
1.29 +#include <s32file.h>
1.30 +#include "t_swicertstoreactions.h"
1.31 +#include "t_certstoredefs.h"
1.32 +#include "t_certstoreout.h"
1.33 +#include "t_input.h"
1.34 +#include <swicertstore.h>
1.35 +#include "testutilclient.h"
1.36 +
1.37 +TBool operator==(const TCapabilitySet& a, const TCapabilitySet& b)
1.38 + {
1.39 + return a.HasCapabilities(b) && b.HasCapabilities(a);
1.40 + }
1.41 +
1.42 +// CInitSWICertStoreAction /////////////////////////////////////////////////////
1.43 +
1.44 +CTestAction* CInitSWICertStoreAction::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut,
1.45 + const TTestActionSpec& aTestActionSpec)
1.46 + {
1.47 + CInitSWICertStoreAction* self = new (ELeave) CInitSWICertStoreAction(aFs, aConsole, aOut);
1.48 + CleanupStack::PushL(self);
1.49 + self->ConstructL(aTestActionSpec);
1.50 + CleanupStack::Pop(self);
1.51 + return self;
1.52 + }
1.53 +
1.54 +CInitSWICertStoreAction::CInitSWICertStoreAction(RFs& aFs, CConsoleBase& aConsole, Output& aOut) :
1.55 + CCertStoreTestAction(aFs, aConsole, aOut), iFs(aFs)
1.56 + {
1.57 + }
1.58 +
1.59 +void CInitSWICertStoreAction::ConstructL(const TTestActionSpec& aTestActionSpec)
1.60 + {
1.61 + CCertStoreTestAction::ConstructL(aTestActionSpec);
1.62 +
1.63 + TInt err = KErrNone;
1.64 + TInt pos = 0;
1.65 + TPtrC8 fileName = Input::ParseElement(aTestActionSpec.iActionBody, KFileNameStart,
1.66 + KFileNameEnd, pos, err);
1.67 +
1.68 + if (!err)
1.69 + {
1.70 + HBufC* tempFName = HBufC::NewLC(fileName.Length());
1.71 +
1.72 + tempFName->Des().Copy(fileName);
1.73 +
1.74 + RTestUtilSession testutil;
1.75 + User::LeaveIfError(testutil.Connect());
1.76 + CleanupClosePushL(testutil);
1.77 +
1.78 + TDriveUnit sysDrive(RFs::GetSystemDrive());
1.79 + TBuf<64> datfile (sysDrive.Name());
1.80 + datfile.Append(_L("\\Resource\\SwiCertstore\\dat\\"));
1.81 + testutil.MkDirAll(datfile);
1.82 +
1.83 + datfile.Copy(sysDrive.Name());
1.84 + datfile.Append(_L("\\Resource\\SwiCertstore\\dat\\00000001"));
1.85 + err = testutil.Copy(tempFName->Des(), datfile);
1.86 + CleanupStack::PopAndDestroy(&testutil);
1.87 + CleanupStack::PopAndDestroy(tempFName);
1.88 + }
1.89 +
1.90 + HBufC* result = HBufC::NewLC(aTestActionSpec.iActionResult.Length());
1.91 + TPtr(result->Des()).Copy(aTestActionSpec.iActionResult);
1.92 + Input::GetExpectedResultL(Input::ParseElement(*result, KReturnStart, KReturnEnd), iExpectedResult);
1.93 + CleanupStack::PopAndDestroy(result);
1.94 + }
1.95 +
1.96 +void CInitSWICertStoreAction::PerformAction(TRequestStatus& aStatus)
1.97 + {
1.98 + switch (iState)
1.99 + {
1.100 + case EInitCertStore:
1.101 + {
1.102 + iState = EFinished;
1.103 + CSWICertStore* swiCertStore = CSWICertStore::NewL(iFs);
1.104 + CleanupReleasePushL(*swiCertStore);
1.105 + AddCertStoreL(swiCertStore);
1.106 + CleanupStack::Pop(swiCertStore);
1.107 + TRequestStatus* status = &aStatus;
1.108 + User::RequestComplete(status, KErrNone);
1.109 + }
1.110 + break;
1.111 +
1.112 + case EFinished:
1.113 + {
1.114 + TRequestStatus* status = &aStatus;
1.115 + User::RequestComplete(status, KErrNone);
1.116 + iResult = EFalse;
1.117 + iFinished = ETrue;
1.118 + }
1.119 + break;
1.120 + }
1.121 + }
1.122 +
1.123 +void CInitSWICertStoreAction::PerformCancel()
1.124 + {
1.125 + }
1.126 +
1.127 +void CInitSWICertStoreAction::Reset()
1.128 + {
1.129 + iState = EInitCertStore;
1.130 + }
1.131 +
1.132 +void CInitSWICertStoreAction::DoReportAction()
1.133 + {
1.134 + iOut.write(_L("Initialising SWI cert store...\n"));
1.135 + }
1.136 +
1.137 +void CInitSWICertStoreAction::DoCheckResult(TInt aError)
1.138 + {
1.139 + iResult = (aError == iExpectedResult);
1.140 + }
1.141 +
1.142 +// CSWICertStoreTestAction /////////////////////////////////////////////////////
1.143 +
1.144 +CSWICertStoreTestAction::CSWICertStoreTestAction(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
1.145 + : CCertStoreTestAction(aFs, aConsole, aOut), iState(EGetCert)
1.146 + {
1.147 + }
1.148 +
1.149 +void CSWICertStoreTestAction::ConstructL(const TTestActionSpec& aTestActionSpec)
1.150 + {
1.151 + CCertStoreTestAction::ConstructL(aTestActionSpec);
1.152 + iFilter = CCertAttributeFilter::NewL();
1.153 + TInt err = KErrNone;
1.154 + TInt pos = 0;
1.155 +
1.156 + TPtrC8 pLabel = Input::ParseElement(aTestActionSpec.iActionBody, KCertLabelStart, KCertLabelEnd, pos, err);
1.157 + if (err != KErrNone)
1.158 + {
1.159 + User::Leave(err);
1.160 + }
1.161 + iCertificateLabel.Copy(pLabel);
1.162 +
1.163 + HBufC* result = HBufC::NewLC(aTestActionSpec.iActionResult.Length());
1.164 + TPtr(result->Des()).Copy(aTestActionSpec.iActionResult);
1.165 + Input::GetExpectedResultL(Input::ParseElement(*result, KReturnStart, KReturnEnd), iExpectedResult);
1.166 + CleanupStack::PopAndDestroy(result);
1.167 +
1.168 + DoConstructL(aTestActionSpec);
1.169 + }
1.170 +
1.171 +CSWICertStoreTestAction::~CSWICertStoreTestAction()
1.172 + {
1.173 + iCertInfos.Close();
1.174 + delete iFilter;
1.175 + }
1.176 +
1.177 +void CSWICertStoreTestAction::PerformAction(TRequestStatus& aStatus)
1.178 + {
1.179 + switch (iState)
1.180 + {
1.181 + case EGetCert:
1.182 + iState = EPerformAction;
1.183 + SWICertStore().List(iCertInfos, *iFilter, aStatus);
1.184 + break;
1.185 +
1.186 + case EPerformAction:
1.187 + {
1.188 + TInt err = KErrNotFound;
1.189 + iState = EFinished;
1.190 +
1.191 + CCTCertInfo* certInfo = FindCertificate();
1.192 + if (certInfo)
1.193 + {
1.194 + err = KErrNone;
1.195 + DoPerformActionL(*certInfo);
1.196 + }
1.197 +
1.198 + TRequestStatus* status = &aStatus;
1.199 + User::RequestComplete(status, err);
1.200 + }
1.201 + break;
1.202 +
1.203 + case EFinished:
1.204 + {
1.205 + TRequestStatus* status = &aStatus;
1.206 + User::RequestComplete(status, aStatus.Int());
1.207 + iResult = EFalse; // Must be set in DoCheckResult
1.208 + iFinished = ETrue;
1.209 + }
1.210 + break;
1.211 + }
1.212 + }
1.213 +
1.214 +void CSWICertStoreTestAction::PerformCancel()
1.215 + {
1.216 + switch (iState)
1.217 + {
1.218 + case EGetCert:
1.219 + SWICertStore().CancelList();
1.220 + break;
1.221 +
1.222 + case EPerformAction:
1.223 + DoCancel();
1.224 + break;
1.225 +
1.226 + default:
1.227 + // do nothing
1.228 + break;
1.229 + }
1.230 + }
1.231 +
1.232 +void CSWICertStoreTestAction::Reset()
1.233 + {
1.234 + iCertInfos.Close();
1.235 + iState = EGetCert;
1.236 + }
1.237 +
1.238 +CCTCertInfo* CSWICertStoreTestAction::FindCertificate()
1.239 + {
1.240 + TInt count = iCertInfos.Count();
1.241 + for (TInt i = 0; i < count; i++)
1.242 + {
1.243 + CCTCertInfo* info = iCertInfos[i];
1.244 + if (info->Label() == iCertificateLabel)
1.245 + {
1.246 + return info;
1.247 + }
1.248 + }
1.249 + return NULL;
1.250 + }
1.251 +
1.252 +// CGetCapabilitiesAction //////////////////////////////////////////////////////
1.253 +
1.254 +CTestAction* CGetCapabilitiesAction::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut,
1.255 + const TTestActionSpec& aTestActionSpec)
1.256 + {
1.257 + CGetCapabilitiesAction* self = new (ELeave) CGetCapabilitiesAction(aFs, aConsole, aOut);
1.258 + CleanupStack::PushL(self);
1.259 + self->ConstructL(aTestActionSpec);
1.260 + CleanupStack::Pop(self);
1.261 + return self;
1.262 + }
1.263 +
1.264 +CGetCapabilitiesAction::CGetCapabilitiesAction(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
1.265 + : CSWICertStoreTestAction(aFs, aConsole, aOut)
1.266 + {
1.267 + }
1.268 +
1.269 +void CGetCapabilitiesAction::DoConstructL(const TTestActionSpec& aTestActionSpec)
1.270 + {
1.271 + if (iExpectedResult == KErrNone)
1.272 + {
1.273 + Input::ParseCapabilitySetL(aTestActionSpec.iActionResult, iExpectedCapapbilities);
1.274 + }
1.275 + }
1.276 +
1.277 +void CGetCapabilitiesAction::DoReportAction()
1.278 + {
1.279 + iOut.writeString(_L("Getting certificate capabilities, expecting: "));
1.280 + iOut.writeCapabilitySetL(iExpectedCapapbilities);
1.281 + iOut.writeNewLine();
1.282 + }
1.283 +
1.284 +void CGetCapabilitiesAction::DoPerformActionL(CCTCertInfo& aCertInfo)
1.285 + {
1.286 + iCapabilities = SWICertStore().CertMetaInfoL(aCertInfo).iCapabilities;
1.287 + }
1.288 +
1.289 +void CGetCapabilitiesAction::DoCheckResult(TInt aError)
1.290 + {
1.291 + iResult = (aError == iExpectedResult &&
1.292 + iCapabilities == iExpectedCapapbilities);
1.293 +
1.294 + if (aError == KErrNone)
1.295 + {
1.296 + iOut.writeString(_L("Certificate has capabilities: "));
1.297 + iOut.writeCapabilitySetL(iCapabilities);
1.298 + }
1.299 + }
1.300 +
1.301 +void CGetCapabilitiesAction::DoCancel()
1.302 + {
1.303 + }
1.304 +
1.305 +// CGetMandatoryAction //////////////////////////////////////////////////////
1.306 +
1.307 +CTestAction* CGetMandatoryAction::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut,
1.308 + const TTestActionSpec& aTestActionSpec)
1.309 + {
1.310 + CGetMandatoryAction* self = new (ELeave) CGetMandatoryAction(aFs, aConsole, aOut);
1.311 + CleanupStack::PushL(self);
1.312 + self->ConstructL(aTestActionSpec);
1.313 + CleanupStack::Pop(self);
1.314 + return self;
1.315 + }
1.316 +
1.317 +CGetMandatoryAction::CGetMandatoryAction(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
1.318 + : CSWICertStoreTestAction(aFs, aConsole, aOut)
1.319 + {
1.320 + }
1.321 +
1.322 +void CGetMandatoryAction::DoConstructL(const TTestActionSpec& aTestActionSpec)
1.323 + {
1.324 + if (iExpectedResult == KErrNone)
1.325 + {
1.326 + iExpectedValue = Input::ParseElementBoolL(aTestActionSpec.iActionResult, KMandatoryStart);
1.327 + }
1.328 + }
1.329 +
1.330 +void CGetMandatoryAction::DoReportAction()
1.331 + {
1.332 + iOut.writeString(_L("Getting certificate mandatory flag, expecting: "));
1.333 + iOut.writeBoolL(iExpectedValue);
1.334 + iOut.writeNewLine();
1.335 + }
1.336 +
1.337 +void CGetMandatoryAction::DoPerformActionL(CCTCertInfo& aCertInfo)
1.338 + {
1.339 + iValue = SWICertStore().CertMetaInfoL(aCertInfo).iIsMandatory;
1.340 + }
1.341 +
1.342 +void CGetMandatoryAction::DoCheckResult(TInt aError)
1.343 + {
1.344 + iResult = (aError == iExpectedResult &&
1.345 + iValue == iExpectedValue);
1.346 +
1.347 + if (aError == KErrNone)
1.348 + {
1.349 + iOut.writeString(_L("Certificate mandatory flag: "));
1.350 + iOut.writeBoolL(iValue);
1.351 + }
1.352 + }
1.353 +
1.354 +void CGetMandatoryAction::DoCancel()
1.355 + {
1.356 + }
1.357 +
1.358 +// CGetSystemUpgradeAction //////////////////////////////////////////////////////
1.359 +
1.360 +CTestAction* CGetSystemUpgradeAction::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut,
1.361 + const TTestActionSpec& aTestActionSpec)
1.362 + {
1.363 + CGetSystemUpgradeAction* self = new (ELeave) CGetSystemUpgradeAction(aFs, aConsole, aOut);
1.364 + CleanupStack::PushL(self);
1.365 + self->ConstructL(aTestActionSpec);
1.366 + CleanupStack::Pop(self);
1.367 + return self;
1.368 + }
1.369 +
1.370 +CGetSystemUpgradeAction::CGetSystemUpgradeAction(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
1.371 + : CSWICertStoreTestAction(aFs, aConsole, aOut)
1.372 + {
1.373 + }
1.374 +
1.375 +void CGetSystemUpgradeAction::DoConstructL(const TTestActionSpec& aTestActionSpec)
1.376 + {
1.377 + if (iExpectedResult == KErrNone)
1.378 + {
1.379 + iExpectedSystemUpgradeValue = Input::ParseElementBoolL(aTestActionSpec.iActionResult, KSystemUpgradeStart);
1.380 + }
1.381 + }
1.382 +
1.383 +void CGetSystemUpgradeAction::DoReportAction()
1.384 + {
1.385 + iOut.writeString(_L("Getting certificate systemupgrade flag, expecting: "));
1.386 + iOut.writeBoolL(iExpectedSystemUpgradeValue);
1.387 + iOut.writeNewLine();
1.388 + }
1.389 +
1.390 +void CGetSystemUpgradeAction::DoPerformActionL(CCTCertInfo& aCertInfo)
1.391 + {
1.392 + iSystemUpgradeValue = SWICertStore().CertMetaInfoL(aCertInfo).iIsSystemUpgrade;
1.393 + }
1.394 +
1.395 +void CGetSystemUpgradeAction::DoCheckResult(TInt aError)
1.396 + {
1.397 + iResult = (aError == iExpectedResult &&
1.398 + iSystemUpgradeValue == iExpectedSystemUpgradeValue);
1.399 +
1.400 + if (aError == KErrNone)
1.401 + {
1.402 + iOut.writeString(_L("Certificate systemupgrade flag: "));
1.403 + iOut.writeBoolL(iSystemUpgradeValue);
1.404 + }
1.405 + }
1.406 +
1.407 +void CGetSystemUpgradeAction::DoCancel()
1.408 + {
1.409 + }
1.410 +
1.411 +// CGetDeletableAction //////////////////////////////////////////////////////
1.412 +
1.413 +CTestAction* CGetDeletionAction::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut,
1.414 + const TTestActionSpec& aTestActionSpec)
1.415 + {
1.416 + CGetDeletionAction* self = new (ELeave) CGetDeletionAction(aFs, aConsole, aOut);
1.417 + CleanupStack::PushL(self);
1.418 + self->ConstructL(aTestActionSpec);
1.419 + CleanupStack::Pop(self);
1.420 + return self;
1.421 + }
1.422 +
1.423 +CGetDeletionAction::CGetDeletionAction(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
1.424 + : CSWICertStoreTestAction(aFs, aConsole, aOut)
1.425 + {
1.426 + }
1.427 +
1.428 +void CGetDeletionAction::DoConstructL(const TTestActionSpec& aTestActionSpec)
1.429 + {
1.430 + if (iExpectedResult == KErrNone)
1.431 + {
1.432 + iExpectedDeletableValue = Input::ParseElementBoolL(aTestActionSpec.iActionResult, KDeletionStart);
1.433 + }
1.434 + }
1.435 +
1.436 +void CGetDeletionAction::DoReportAction()
1.437 + {
1.438 + iOut.writeString(_L("Getting certificate deletable flag, expecting: "));
1.439 + iOut.writeBoolL(iExpectedDeletableValue);
1.440 + iOut.writeNewLine();
1.441 + }
1.442 +
1.443 +void CGetDeletionAction::DoPerformActionL(CCTCertInfo& aCertInfo)
1.444 + {
1.445 + iDeletableValue = aCertInfo.IsDeletable();
1.446 + }
1.447 +
1.448 +void CGetDeletionAction::DoCheckResult(TInt aError)
1.449 + {
1.450 + iResult = (aError == iExpectedResult &&
1.451 + iDeletableValue == iExpectedDeletableValue);
1.452 +
1.453 + if (aError == KErrNone)
1.454 + {
1.455 + iOut.writeString(_L("Certificate Deletable flag: "));
1.456 + iOut.writeBoolL(iDeletableValue);
1.457 + }
1.458 + }
1.459 +
1.460 +void CGetDeletionAction::DoCancel()
1.461 + {
1.462 + }
1.463 +
1.464 +
1.465 +// CMultipleReadersAction /////////////////////////////////////////////////////
1.466 +
1.467 +
1.468 +CTestAction* CMultipleReadersAction::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut,
1.469 + const TTestActionSpec& aTestActionSpec)
1.470 + {
1.471 + CMultipleReadersAction* self = new (ELeave) CMultipleReadersAction(aFs,aConsole, aOut);
1.472 + CleanupStack::PushL(self);
1.473 + self->ConstructL(aTestActionSpec);
1.474 + CleanupStack::Pop(self);
1.475 + return self;
1.476 + }
1.477 +
1.478 +CMultipleReadersAction::CMultipleReadersAction(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
1.479 + : CSWICertStoreTestAction(aFs, aConsole, aOut)
1.480 + {
1.481 + }
1.482 +
1.483 +void CMultipleReadersAction::DoConstructL(const TTestActionSpec& /* aTestActionSpec*/)
1.484 + {
1.485 + }
1.486 +
1.487 +void CMultipleReadersAction::DoReportAction()
1.488 + {
1.489 + iOut.writeString(_L("Checking opening multiple readers works, expecting:"));
1.490 + iOut.writeBoolL(true);
1.491 + iOut.writeNewLine();
1.492 + }
1.493 +
1.494 +void CMultipleReadersAction::DoPerformActionL(CCTCertInfo& /*aCertInfo*/)
1.495 + {
1.496 + CSWICertStore* swiCertStore = 0;
1.497 +
1.498 + TRAPD(err, swiCertStore = CSWICertStore::NewL(iFs));
1.499 +
1.500 + if (err == KErrNone && swiCertStore != 0)
1.501 + {
1.502 + swiCertStore->Release();
1.503 + }
1.504 + iValue = err;
1.505 + }
1.506 +
1.507 +void CMultipleReadersAction::DoCheckResult(TInt aError)
1.508 + {
1.509 + iResult = (aError == KErrNone &&
1.510 + iValue == KErrNone);
1.511 + }
1.512 +
1.513 +void CMultipleReadersAction::DoCancel()
1.514 + {
1.515 + }