os/security/cryptoservices/filebasedcertificateandkeystores/test/tkeystore/t_authobjects.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.
24 #include <mctauthobject.h>
25 #include "t_keystore_actions.h"
26 #include "t_keystore_defs.h"
29 /////////////////////////////////////////////////////////////////////////////////
31 /////////////////////////////////////////////////////////////////////////////////
33 template <class TTestImpl>
34 CTestAction* CAuthObjectTest<TTestImpl>::NewL(RFs& aFs,
35 CConsoleBase& aConsole,
37 const TTestActionSpec& aTestActionSpec)
39 CTestAction* self = CAuthObjectTest<TTestImpl>::NewLC(aFs, aConsole, aOut, aTestActionSpec);
40 CleanupStack::Pop(self);
44 template <class TTestImpl>
45 CTestAction* CAuthObjectTest<TTestImpl>::NewLC(RFs& aFs,
46 CConsoleBase& aConsole,
48 const TTestActionSpec& aTestActionSpec)
50 CAuthObjectTest<TTestImpl>* self = new(ELeave) CAuthObjectTest<TTestImpl>(aFs, aConsole, aOut);
51 CleanupStack::PushL(self);
52 self->ConstructL(aTestActionSpec);
56 template <class TTestImpl>
57 CAuthObjectTest<TTestImpl>::CAuthObjectTest(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
58 : CKeyStoreTestAction(aFs, aConsole, aOut), iState(EListKeys)
62 template <class TTestImpl>
63 void CAuthObjectTest<TTestImpl>::ConstructL(const TTestActionSpec& aTestActionSpec)
65 CKeyStoreTestAction::ConstructL(aTestActionSpec);
67 iImpl.ConstructL(aTestActionSpec);
70 template <class TTestImpl>
71 CAuthObjectTest<TTestImpl>::~CAuthObjectTest()
76 template <class TTestImpl>
77 void CAuthObjectTest<TTestImpl>::PerformAction(TRequestStatus& aStatus)
79 if (aStatus != KErrNone)
88 CUnifiedKeyStore& keystore = *CSharedKeyStores::TheUnifiedKeyStores().operator[](iKeystore);
90 TCTKeyAttributeFilter filter;
91 keystore.List(iKeys, filter, aStatus);
99 TRequestStatus* status = &aStatus;
101 if (iKeys.Count() == 0)
103 User::RequestComplete(status, KErrNotFound);
107 const CCTKeyInfo* key = NULL;
108 for (TInt index = 0 ; index < iKeys.Count() ; ++index)
110 const CCTKeyInfo* k = iKeys[index];
111 if (k->Label() == *iLabel)
120 iOut.writeString(_L("Key not found: "));
121 iOut.writeString(*iLabel);
123 User::RequestComplete(status, KErrNotFound);
127 iAuth = key->Protector();
130 User::RequestComplete(status, KErrNotFound);
134 iImpl.DoTest(*iAuth, aStatus);
140 if (aStatus == KErrNone && !iImpl.CheckResult(iOut))
142 iOut.writeString(_L("CheckReult() returned EFalse"));
144 aStatus = KErrGeneral;
147 if (aStatus == iExpectedResult)
149 iOut.writeString(_L("Status: "));
150 iOut.writeNum(aStatus.Int());
151 iOut.writeString(_L(", expected"));
157 iOut.writeString(_L("Status: "));
158 iOut.writeNum(aStatus.Int());
159 iOut.writeString(_L(", failure"));
164 iActionState = EPostrequisite;
166 TRequestStatus* status = &aStatus;
167 User::RequestComplete(status, aStatus.Int());
173 template <class TTestImpl>
174 void CAuthObjectTest<TTestImpl>::PerformCancel()
180 CUnifiedKeyStore& keystore = *CSharedKeyStores::TheUnifiedKeyStores().operator[](iKeystore);
181 keystore.CancelList();
188 iImpl.DoCancel(*iAuth);
197 template <class TTestImpl>
198 void CAuthObjectTest<TTestImpl>::Reset()
206 template <class TTestImpl>
207 void CAuthObjectTest<TTestImpl>::DoReportAction()
209 iOut.writeString(_L("Running auth object test: "));
210 iOut.writeString(iImpl.Name());
214 template <class TTestImpl>
215 void CAuthObjectTest<TTestImpl>::DoCheckResult(TInt /*aError*/)
219 ////////////////////////////////////////////////////////////////////////////////
221 ////////////////////////////////////////////////////////////////////////////////
223 void TAuthObjectTest::ConstructL(const TTestActionSpec& /*aTestActionSpec*/)
227 void TAuthObjectTest::Reset()
231 TBool TAuthObjectTest::CheckResult(Output& /*aOut*/)
237 * Test changing the passphrase.
239 const TDesC& TChangePassphrase::Name()
241 _LIT(KName, "Change passphrase");
245 void TChangePassphrase::DoTest(MCTAuthenticationObject& aAuth, TRequestStatus& aStatus)
247 aAuth.ChangeReferenceData(aStatus);
250 void TChangePassphrase::DoCancel(MCTAuthenticationObject& aAuth)
252 aAuth.CancelChangeReferenceData();
255 template class CAuthObjectTest<TChangePassphrase>;
258 * Test listing protected objects.
261 TListProtectedObjects::~TListProtectedObjects()
263 iExpectedKeys.ResetAndDestroy();
267 const TDesC& TListProtectedObjects::Name()
269 _LIT(KName, "List protected objects");
273 void TListProtectedObjects::ConstructL(const TTestActionSpec& aTestActionSpec)
276 while (AddExpectedKeyL(Input::ParseElement(aTestActionSpec.iActionBody, KFoundKeyStart, KFoundKeyEnd, pos)))
280 void TListProtectedObjects::Reset()
285 TBool TListProtectedObjects::AddExpectedKeyL(const TDesC8& aKeyLabel)
287 if (aKeyLabel.Length() == 0)
290 HBufC* label = HBufC::NewMaxLC(aKeyLabel.Length());
291 TPtr ptr = label->Des();
293 User::LeaveIfError(iExpectedKeys.Append(label));
294 CleanupStack::Pop(label);
298 void TListProtectedObjects::DoTest(MCTAuthenticationObject& aAuth, TRequestStatus& aStatus)
300 aAuth.ListProtectedObjects(iObjects, aStatus);
303 TBool TListProtectedObjects::CheckResult(Output& aOut)
305 aOut.writeString(_L("Expected "));
306 aOut.writeNum(iExpectedKeys.Count());
307 aOut.writeString(_L(" keys, found "));
308 aOut.writeNum(iObjects.Count());
311 if (iObjects.Count() != iExpectedKeys.Count())
316 for (TInt index = 0 ; index < iObjects.Count() ; ++index)
318 MCTTokenObject* key = iObjects[index];
319 const TDesC& keyLabel = key->Label();
320 HBufC* expectedLabel = iExpectedKeys[index];
322 if (keyLabel != *expectedLabel)
324 aOut.writeString(_L("Expected "));
325 aOut.writeString(*expectedLabel);
326 aOut.writeString(_L(" but found "));
327 aOut.writeString(keyLabel);
336 void TListProtectedObjects::DoCancel(MCTAuthenticationObject& aAuth)
338 aAuth.CancelListProtectedObjects();
341 template class CAuthObjectTest<TListProtectedObjects>;
344 * Test unblocking the passphrase.
346 const TDesC& TUnblockPassphrase::Name()
348 _LIT(KName, "Unblock passphrase");
352 void TUnblockPassphrase::DoTest(MCTAuthenticationObject& aAuth, TRequestStatus& aStatus)
354 aAuth.Unblock(aStatus);
357 void TUnblockPassphrase::DoCancel(MCTAuthenticationObject& aAuth)
359 aAuth.CancelUnblock();
362 template class CAuthObjectTest<TUnblockPassphrase>;
365 * Test auth object open method.
367 const TDesC& TAuthOpen::Name()
369 _LIT(KName, "Auth object open");
373 void TAuthOpen::DoTest(MCTAuthenticationObject& aAuth, TRequestStatus& aStatus)
378 void TAuthOpen::DoCancel(MCTAuthenticationObject& aAuth)
383 template class CAuthObjectTest<TAuthOpen>;
386 * Test auth object close method.
388 const TDesC& TAuthClose::Name()
390 _LIT(KName, "Auth object close");
394 void TAuthClose::DoTest(MCTAuthenticationObject& aAuth, TRequestStatus& aStatus)
396 aAuth.Close(aStatus);
399 void TAuthClose::DoCancel(MCTAuthenticationObject& aAuth)
404 template class CAuthObjectTest<TAuthClose>;
407 * Test get time remaining.
409 const TDesC& TGetTimeRemaining::Name()
411 _LIT(KName, "Get time remaining");
415 void TGetTimeRemaining::ConstructL(const TTestActionSpec& aTestActionSpec)
417 TPtrC8 ptr = Input::ParseElement(aTestActionSpec.iActionBody, KTimeoutStart);
418 if (ptr.Length() == 0)
419 User::Leave(KErrNotFound);
421 lex.Val(iExpectedTime);
424 void TGetTimeRemaining::DoTest(MCTAuthenticationObject& aAuth, TRequestStatus& aStatus)
426 aAuth.TimeRemaining(iTimeRemaining, aStatus);
429 TBool TGetTimeRemaining::CheckResult(Output& aOut)
431 aOut.writeString(_L("Expected "));
432 aOut.writeNum(iExpectedTime);
433 aOut.writeString(_L(", got "));
434 aOut.writeNum(iTimeRemaining);
437 // Check time remaining is within 5 seconds of expected
438 TInt difference = iTimeRemaining - iExpectedTime;
440 difference = -difference;
441 return difference < 6;
444 void TGetTimeRemaining::DoCancel(MCTAuthenticationObject& aAuth)
446 aAuth.CancelTimeRemaining();
449 template class CAuthObjectTest<TGetTimeRemaining>;
454 const TDesC& TAuthSetTimeout::Name()
456 _LIT(KName, "Set timeout");
460 void TAuthSetTimeout::ConstructL(const TTestActionSpec& aTestActionSpec)
462 TPtrC8 ptr = Input::ParseElement(aTestActionSpec.iActionBody, KTimeoutStart);
463 if (ptr.Length() == 0)
464 User::Leave(KErrNotFound);
466 lex.Val(iNewTimeout);
469 void TAuthSetTimeout::DoTest(MCTAuthenticationObject& aAuth, TRequestStatus& aStatus)
471 aAuth.SetTimeout(iNewTimeout, aStatus);
474 void TAuthSetTimeout::DoCancel(MCTAuthenticationObject& aAuth)
476 aAuth.CancelSetTimeout();
479 template class CAuthObjectTest<TAuthSetTimeout>;
484 const TDesC& TGetTimeout::Name()
486 _LIT(KName, "Get timeout");
490 void TGetTimeout::ConstructL(const TTestActionSpec& aTestActionSpec)
492 TPtrC8 ptr = Input::ParseElement(aTestActionSpec.iActionBody, KTimeoutStart);
493 if (ptr.Length() == 0)
494 User::Leave(KErrNotFound);
496 lex.Val(iExpectedTimeout);
499 void TGetTimeout::DoTest(MCTAuthenticationObject& aAuth, TRequestStatus& aStatus)
501 aAuth.Timeout(iTimeout, aStatus);
504 TBool TGetTimeout::CheckResult(Output& aOut)
506 aOut.writeString(_L("Expected "));
507 aOut.writeNum(iExpectedTimeout);
508 aOut.writeString(_L(", got "));
509 aOut.writeNum(iTimeout);
512 return iExpectedTimeout == iTimeout;
515 void TGetTimeout::DoCancel(MCTAuthenticationObject& aAuth)
517 aAuth.CancelTimeout();
520 template class CAuthObjectTest<TGetTimeout>;