os/security/cryptoservices/filebasedcertificateandkeystores/test/tkeystore/t_delete.cpp
First public contribution.
2 * Copyright (c) 2003-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.
20 #include "t_keystore_actions.h"
21 #include "t_keystore_defs.h"
24 /*static*/ CTestAction* CDeleteKeys::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut, const TTestActionSpec& aTestActionSpec)
26 CTestAction* self = CDeleteKeys::NewLC(aFs, aConsole, aOut, aTestActionSpec);
27 CleanupStack::Pop(self);
31 /*static*/ CTestAction* CDeleteKeys::NewLC(RFs& aFs, CConsoleBase& aConsole, Output& aOut, const TTestActionSpec& aTestActionSpec)
33 CDeleteKeys* self = new (ELeave) CDeleteKeys(aFs, aConsole, aOut);
34 CleanupStack::PushL(self);
35 self->ConstructL(aTestActionSpec);
39 CDeleteKeys::~CDeleteKeys()
44 void CDeleteKeys::PerformAction(TRequestStatus& aStatus)
46 if (aStatus != KErrNone)
51 CUnifiedKeyStore* keyStore = CSharedKeyStores::TheUnifiedKeyStores().operator[](iKeystore);
57 keyStore->List(iKeys, iFilter, aStatus);
59 if (*iLabel != KNullDesC)
60 iState = EDeleteKeyByLabel;
61 else if (iNonExistentKey)
62 iState = EDeleteNonExistentKey;
64 iState = EDeleteAllKeys;
68 case EDeleteKeyByLabel:
69 for ( ; iDeleteIndex < iKeys.Count() ; ++iDeleteIndex)
71 CCTKeyInfo* key = iKeys[iDeleteIndex];
72 if (key->Label() == *iLabel)
74 keyStore->DeleteKey(*key, aStatus);
80 if (iDeleteIndex == iKeys.Count())
82 TRequestStatus* status = &aStatus;
83 User::RequestComplete(status, KErrNotFound);
89 case EDeleteNonExistentKey:
91 // Skip test if we want to delete a non-existant key, but there are
92 // no keys present to get token handle from - this happens if
93 // previous tests fail
94 if (iKeys.Count() == 0)
96 _LIT(KSkippingTest, "!!!No keys present, skipping delete non-existant key test!!!\n");
97 iConsole.Write(KSkippingTest);
98 iOut.writeString(KSkippingTest);
99 iActionState = EPostrequisite;
100 TRequestStatus* status = &aStatus;
101 User::RequestComplete(status, KErrNone);
105 CCTKeyInfo* keyInfo = iKeys[0];
106 iDeleteHandle = keyInfo->Handle();
107 if (iNonExistentKey > 0)
109 // Modify the handle to a key that will not exist
110 iDeleteHandle.iObjectId = 0xfbadcafe;
114 keyStore->DeleteKey(iDeleteHandle, aStatus);
119 if (iDeleteIndex < iKeys.Count())
121 iState = EDeleteAllKeys;
122 keyStore->DeleteKey(*iKeys[iDeleteIndex], aStatus);
129 TRequestStatus* status = &aStatus;
130 User::RequestComplete(status, KErrNone);
136 iOut.writeString(_L("Delete: initial key count == "));
137 iOut.writeNum(iKeys.Count());
139 iOut.writeString(_L("Delete: delete count == "));
140 iOut.writeNum(iDeletedCount);
142 iOut.writeString(_L("Delete: status == "));
143 iOut.writeNum(aStatus.Int());
146 if (aStatus != KErrNone && iDeletedCount > 0)
148 // Attempt to delete key failed, adjust count
152 iResult = (aStatus == iExpectedResult) &&
153 (iExpectedDeleteCount == iDeletedCount || iExpectedDeleteCount == -1);
155 iActionState = EPostrequisite;
157 TRequestStatus* status = &aStatus;
158 User::RequestComplete(status, aStatus.Int());
167 void CDeleteKeys::PerformCancel()
169 CUnifiedKeyStore* keystore = CSharedKeyStores::TheUnifiedKeyStores().operator[](iKeystore);
174 void CDeleteKeys::Reset()
182 void CDeleteKeys::DoReportAction()
184 _LIT(KDeleting, "Deleting...");
185 iOut.writeString(KDeleting);
189 void CDeleteKeys::DoCheckResult(TInt aError)
194 if (aError == KErrNone)
196 if (iExpectedDeleteCount!=-1)
198 _LIT(KSuccessful, "%d Key(s) deleted successfully (expected to delete %d)\n");
199 buf.Format(KSuccessful, iDeletedCount, iExpectedDeleteCount);
201 iOut.writeString(buf);
207 if (aError!=iExpectedResult)
209 if (iExpectedDeleteCount!=-1)
211 _LIT(KFailed, "!!!Key delete failure %d!!!\n");
212 buf.Format(KFailed, aError);
214 iOut.writeString(buf);
219 _LIT(KFailed, "Key delete failed, but expected\n");
220 iConsole.Write(KFailed);
221 iOut.writeString(KFailed);
229 CDeleteKeys::CDeleteKeys(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
230 : CKeyStoreTestAction(aFs, aConsole, aOut),
231 iInitialKeyCount(-1),
236 iExpectedDeleteCount(-1)
239 void CDeleteKeys::ConstructL(const TTestActionSpec& aTestActionSpec)
241 CKeyStoreTestAction::ConstructL(aTestActionSpec);
242 iFilter.iKeyAlgorithm = iAlgorithm;
245 buf.Set(Input::ParseElement(aTestActionSpec.iActionBody, KNonExistentKeyStart));
246 if (buf.Length() != 0)
249 buf.Set(Input::ParseElement(aTestActionSpec.iActionBody, KDeleteCountStart));
250 if (buf.Length() != 0)
253 buf.Set(Input::ParseElement(aTestActionSpec.iActionBody, KOwnerStart));
254 if (buf.Length() != 0)
256 User::Leave(KErrNotSupported);
259 if (*iLabel != KNullDesC || iNonExistentKey)
261 iFilter.iPolicyFilter = TCTKeyAttributeFilter::EAllKeys;
265 iFilter.iPolicyFilter = TCTKeyAttributeFilter::EManageableKeys;
272 void CDeleteKeys::SetNonExistent(const TDesC8& aNonExistent)
274 TLex8 lexer(aNonExistent);
275 lexer.Val(iNonExistentKey);
278 void CDeleteKeys::SetDeleteCount(const TDesC8& aDeleteCount)
280 TLex8 lexer(aDeleteCount);
281 lexer.Val(iExpectedDeleteCount);