os/security/cryptoservices/filebasedcertificateandkeystores/test/tkeystore/t_authobjects.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 <e32uid.h>
    24 #include <mctauthobject.h>
    25 #include "t_keystore_actions.h"
    26 #include "t_keystore_defs.h"
    27 #include "t_input.h"
    28 
    29 /////////////////////////////////////////////////////////////////////////////////
    30 // CAuthObjectTest
    31 /////////////////////////////////////////////////////////////////////////////////
    32 
    33 template <class TTestImpl>
    34 CTestAction* CAuthObjectTest<TTestImpl>::NewL(RFs& aFs, 
    35 											  CConsoleBase& aConsole, 
    36 											  Output& aOut,
    37 											  const TTestActionSpec& aTestActionSpec)
    38 	{
    39 	CTestAction* self = CAuthObjectTest<TTestImpl>::NewLC(aFs, aConsole, aOut, aTestActionSpec);
    40 	CleanupStack::Pop(self);
    41 	return self;
    42 	}
    43 
    44 template <class TTestImpl>
    45 CTestAction* CAuthObjectTest<TTestImpl>::NewLC(RFs& aFs,
    46 											   CConsoleBase& aConsole, 
    47 											   Output& aOut,
    48 											   const TTestActionSpec& aTestActionSpec)
    49 	{
    50 	CAuthObjectTest<TTestImpl>* self = new(ELeave) CAuthObjectTest<TTestImpl>(aFs, aConsole, aOut);
    51 	CleanupStack::PushL(self);
    52 	self->ConstructL(aTestActionSpec);
    53 	return self;
    54 	}
    55 
    56 template <class TTestImpl>
    57 CAuthObjectTest<TTestImpl>::CAuthObjectTest(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
    58 	: CKeyStoreTestAction(aFs, aConsole, aOut), iState(EListKeys)
    59 	{
    60 	}
    61 
    62 template <class TTestImpl>
    63 void CAuthObjectTest<TTestImpl>::ConstructL(const TTestActionSpec& aTestActionSpec)
    64 	{
    65 	CKeyStoreTestAction::ConstructL(aTestActionSpec);
    66 
    67 	iImpl.ConstructL(aTestActionSpec);
    68 	}
    69 
    70 template <class TTestImpl>
    71 CAuthObjectTest<TTestImpl>::~CAuthObjectTest()
    72 	{
    73 	iKeys.Close();
    74 	}
    75 
    76 template <class TTestImpl>
    77 void CAuthObjectTest<TTestImpl>::PerformAction(TRequestStatus& aStatus)
    78 	{
    79     if (aStatus != KErrNone)
    80         {
    81         iState = EFinished;
    82         }
    83     
    84 	switch (iState)
    85 		{
    86 		case EListKeys:
    87 			{
    88 			CUnifiedKeyStore& keystore = *CSharedKeyStores::TheUnifiedKeyStores().operator[](iKeystore);
    89 
    90 			TCTKeyAttributeFilter filter;
    91 			keystore.List(iKeys, filter, aStatus);			
    92 			iState = EMain;
    93 			break;
    94 			}
    95 
    96 		case EMain:
    97 			{
    98 			iState = EFinished;
    99 			TRequestStatus* status = &aStatus;
   100 			
   101 			if (iKeys.Count() == 0)
   102 				{
   103 				User::RequestComplete(status, KErrNotFound);
   104 				return;
   105 				}
   106 
   107 			const CCTKeyInfo* key = NULL;
   108 			for (TInt index = 0 ; index < iKeys.Count() ; ++index)
   109 				{
   110 				const CCTKeyInfo* k = iKeys[index];
   111 				if (k->Label() == *iLabel)
   112 					{
   113 					key = k;
   114 					break;
   115 					}
   116 				}
   117 
   118 			if (!key)
   119 				{
   120 				iOut.writeString(_L("Key not found: "));
   121 				iOut.writeString(*iLabel);
   122 				iOut.writeNewLine();
   123 				User::RequestComplete(status, KErrNotFound);
   124 				return;
   125 				}
   126 
   127 			iAuth = key->Protector();
   128 			if (!iAuth)
   129 				{
   130 				User::RequestComplete(status, KErrNotFound);
   131 				return;
   132 				}
   133 
   134 			iImpl.DoTest(*iAuth, aStatus);
   135 			break;
   136 			}
   137 
   138 		case EFinished:
   139 			{
   140 			if (aStatus == KErrNone && !iImpl.CheckResult(iOut))
   141 				{
   142 				iOut.writeString(_L("CheckReult() returned EFalse"));
   143 				iOut.writeNewLine();
   144 				aStatus = KErrGeneral;
   145 				}
   146 
   147 			if (aStatus == iExpectedResult)
   148 				{
   149 				iOut.writeString(_L("Status: "));
   150 				iOut.writeNum(aStatus.Int());
   151 				iOut.writeString(_L(", expected"));
   152 				iOut.writeNewLine();
   153 				iResult = ETrue;
   154 				}			
   155 			else
   156 				{
   157 				iOut.writeString(_L("Status: "));
   158 				iOut.writeNum(aStatus.Int());
   159 				iOut.writeString(_L(", failure"));
   160 				iOut.writeNewLine();
   161 				iResult = EFalse;
   162 				}
   163 				
   164 			iActionState = EPostrequisite;
   165 
   166 			TRequestStatus* status = &aStatus;
   167 			User::RequestComplete(status, aStatus.Int());
   168 			break;
   169 			}
   170 		}
   171 	}
   172 
   173 template <class TTestImpl>
   174 void CAuthObjectTest<TTestImpl>::PerformCancel()
   175 	{
   176     switch (iState)
   177         {
   178         case EMain:
   179             {
   180             CUnifiedKeyStore& keystore = *CSharedKeyStores::TheUnifiedKeyStores().operator[](iKeystore);
   181             keystore.CancelList();
   182             }
   183             break;
   184 
   185         case EFinished:
   186             if (iAuth)
   187                 {
   188                 iImpl.DoCancel(*iAuth);
   189                 }
   190             break;
   191 
   192         default:
   193             break;
   194         }
   195     }
   196 
   197 template <class TTestImpl>
   198 void CAuthObjectTest<TTestImpl>::Reset()
   199 	{
   200 	iState = EListKeys;
   201 	iKeys.Close();
   202 	iImpl.Reset();
   203     iAuth = NULL;
   204 	}
   205 
   206 template <class TTestImpl>
   207 void CAuthObjectTest<TTestImpl>::DoReportAction()
   208 	{
   209 	iOut.writeString(_L("Running auth object test: "));
   210 	iOut.writeString(iImpl.Name());
   211 	iOut.writeNewLine();
   212 	}
   213 
   214 template <class TTestImpl>
   215 void CAuthObjectTest<TTestImpl>::DoCheckResult(TInt /*aError*/)
   216 	{
   217 	}
   218 
   219 ////////////////////////////////////////////////////////////////////////////////
   220 // TAuthObjectTest
   221 ////////////////////////////////////////////////////////////////////////////////
   222 
   223 void TAuthObjectTest::ConstructL(const TTestActionSpec& /*aTestActionSpec*/)
   224 	{
   225 	}
   226 
   227 void TAuthObjectTest::Reset()
   228 	{
   229 	}
   230 
   231 TBool TAuthObjectTest::CheckResult(Output& /*aOut*/)
   232 	{
   233 	return ETrue;
   234 	}
   235 
   236 /**
   237  * Test changing the passphrase.
   238  */
   239 const TDesC& TChangePassphrase::Name()
   240 	{
   241 	_LIT(KName, "Change passphrase");
   242 	return KName;
   243 	}
   244 
   245 void TChangePassphrase::DoTest(MCTAuthenticationObject& aAuth, TRequestStatus& aStatus)
   246 	{
   247 	aAuth.ChangeReferenceData(aStatus);
   248 	}
   249 
   250 void TChangePassphrase::DoCancel(MCTAuthenticationObject& aAuth)
   251     {
   252     aAuth.CancelChangeReferenceData();
   253     }
   254 
   255 template class CAuthObjectTest<TChangePassphrase>;
   256 
   257 /**
   258  * Test listing protected objects.
   259  */
   260 
   261 TListProtectedObjects::~TListProtectedObjects()
   262 	{
   263 	iExpectedKeys.ResetAndDestroy();
   264 	iObjects.Close();
   265 	}
   266 
   267 const TDesC& TListProtectedObjects::Name()
   268 	{
   269 	_LIT(KName, "List protected objects");
   270 	return KName;
   271 	}
   272 
   273 void TListProtectedObjects::ConstructL(const TTestActionSpec& aTestActionSpec)
   274 	{
   275 	TInt pos = 0;
   276 	while (AddExpectedKeyL(Input::ParseElement(aTestActionSpec.iActionBody, KFoundKeyStart, KFoundKeyEnd, pos)))
   277 		/* do nothing */;
   278 	}
   279 
   280 void TListProtectedObjects::Reset()
   281 	{
   282 	iObjects.Close();
   283 	}
   284 
   285 TBool TListProtectedObjects::AddExpectedKeyL(const TDesC8& aKeyLabel)
   286 	{
   287 	if (aKeyLabel.Length() == 0)
   288 		return EFalse;
   289 
   290 	HBufC* label = HBufC::NewMaxLC(aKeyLabel.Length());
   291 	TPtr ptr = label->Des();
   292 	ptr.Copy(aKeyLabel);
   293 	User::LeaveIfError(iExpectedKeys.Append(label));
   294 	CleanupStack::Pop(label);
   295 	return ETrue;
   296 	}
   297 
   298 void TListProtectedObjects::DoTest(MCTAuthenticationObject& aAuth, TRequestStatus& aStatus)
   299 	{
   300 	aAuth.ListProtectedObjects(iObjects, aStatus);
   301 	}
   302 
   303 TBool TListProtectedObjects::CheckResult(Output& aOut)
   304 	{
   305 	aOut.writeString(_L("Expected "));
   306 	aOut.writeNum(iExpectedKeys.Count());
   307 	aOut.writeString(_L(" keys, found "));
   308 	aOut.writeNum(iObjects.Count());
   309 	aOut.writeNewLine();
   310 	
   311 	if (iObjects.Count() != iExpectedKeys.Count())
   312 		{
   313 		return EFalse;
   314 		}
   315 
   316 	for (TInt index = 0 ; index < iObjects.Count() ; ++index)
   317 		{
   318 		MCTTokenObject* key = iObjects[index];
   319 		const TDesC& keyLabel = key->Label();
   320 		HBufC* expectedLabel = iExpectedKeys[index];
   321 		
   322 		if (keyLabel != *expectedLabel)
   323 			{
   324 			aOut.writeString(_L("Expected "));
   325 			aOut.writeString(*expectedLabel);
   326 			aOut.writeString(_L(" but found "));
   327 			aOut.writeString(keyLabel);
   328 			aOut.writeNewLine();
   329 			return EFalse;
   330 			}
   331 		}
   332 
   333 	return ETrue;
   334 	}
   335 	
   336 void TListProtectedObjects::DoCancel(MCTAuthenticationObject& aAuth)
   337     {
   338     aAuth.CancelListProtectedObjects();
   339     }
   340 
   341 template class CAuthObjectTest<TListProtectedObjects>;
   342 
   343 /**
   344  * Test unblocking the passphrase.
   345  */
   346 const TDesC& TUnblockPassphrase::Name()
   347 	{
   348 	_LIT(KName, "Unblock passphrase");
   349 	return KName;
   350 	}
   351 
   352 void TUnblockPassphrase::DoTest(MCTAuthenticationObject& aAuth, TRequestStatus& aStatus)
   353 	{
   354 	aAuth.Unblock(aStatus);
   355 	}
   356 	
   357 void TUnblockPassphrase::DoCancel(MCTAuthenticationObject& aAuth)
   358     {
   359     aAuth.CancelUnblock();
   360     }
   361 
   362 template class CAuthObjectTest<TUnblockPassphrase>;
   363 
   364 /**
   365  * Test auth object open method.
   366  */
   367 const TDesC& TAuthOpen::Name()
   368 	{
   369 	_LIT(KName, "Auth object open");
   370 	return KName;
   371 	}
   372 
   373 void TAuthOpen::DoTest(MCTAuthenticationObject& aAuth, TRequestStatus& aStatus)
   374 	{
   375 	aAuth.Open(aStatus);
   376 	}
   377 	
   378 void TAuthOpen::DoCancel(MCTAuthenticationObject& aAuth)
   379     {
   380     aAuth.CancelOpen();
   381     }
   382 
   383 template class CAuthObjectTest<TAuthOpen>;
   384 
   385 /**
   386  * Test auth object close method.
   387  */
   388 const TDesC& TAuthClose::Name()
   389 	{
   390 	_LIT(KName, "Auth object close");
   391 	return KName;
   392 	}
   393 
   394 void TAuthClose::DoTest(MCTAuthenticationObject& aAuth, TRequestStatus& aStatus)
   395 	{
   396 	aAuth.Close(aStatus);
   397 	}	
   398 
   399 void TAuthClose::DoCancel(MCTAuthenticationObject& aAuth)
   400     {
   401     aAuth.CancelClose();
   402     }
   403 
   404 template class CAuthObjectTest<TAuthClose>;
   405 
   406 /**
   407  * Test get time remaining.
   408  */
   409 const TDesC& TGetTimeRemaining::Name()
   410 	{
   411 	_LIT(KName, "Get time remaining");
   412 	return KName;
   413 	}
   414 
   415 void TGetTimeRemaining::ConstructL(const TTestActionSpec& aTestActionSpec)
   416 	{
   417 	TPtrC8 ptr = Input::ParseElement(aTestActionSpec.iActionBody, KTimeoutStart);
   418 	if (ptr.Length() == 0)
   419 		User::Leave(KErrNotFound);
   420 	TLex8 lex(ptr);
   421 	lex.Val(iExpectedTime);
   422 	}
   423 
   424 void TGetTimeRemaining::DoTest(MCTAuthenticationObject& aAuth, TRequestStatus& aStatus)
   425 	{
   426 	aAuth.TimeRemaining(iTimeRemaining, aStatus);	
   427 	}
   428 
   429 TBool TGetTimeRemaining::CheckResult(Output& aOut)
   430 	{
   431 	aOut.writeString(_L("Expected "));
   432 	aOut.writeNum(iExpectedTime);
   433 	aOut.writeString(_L(", got "));
   434 	aOut.writeNum(iTimeRemaining);
   435 	aOut.writeNewLine();
   436 
   437 	// Check time remaining is within 5 seconds of expected
   438 	TInt difference = iTimeRemaining - iExpectedTime;
   439 	if (difference < 0)
   440 		difference = -difference;
   441 	return difference < 6;
   442 	}
   443 	
   444 void TGetTimeRemaining::DoCancel(MCTAuthenticationObject& aAuth)
   445     {
   446     aAuth.CancelTimeRemaining();
   447     }
   448 
   449 template class CAuthObjectTest<TGetTimeRemaining>;
   450 
   451 /**
   452  * Test get timeout.
   453  */
   454 const TDesC& TAuthSetTimeout::Name()
   455 	{
   456 	_LIT(KName, "Set timeout");
   457 	return KName;
   458 	}
   459 
   460 void TAuthSetTimeout::ConstructL(const TTestActionSpec& aTestActionSpec)
   461 	{
   462 	TPtrC8 ptr = Input::ParseElement(aTestActionSpec.iActionBody, KTimeoutStart);
   463 	if (ptr.Length() == 0)
   464 		User::Leave(KErrNotFound);
   465 	TLex8 lex(ptr);
   466 	lex.Val(iNewTimeout);
   467 	}
   468 
   469 void TAuthSetTimeout::DoTest(MCTAuthenticationObject& aAuth, TRequestStatus& aStatus)
   470 	{
   471 	aAuth.SetTimeout(iNewTimeout, aStatus);
   472 	}	
   473 
   474 void TAuthSetTimeout::DoCancel(MCTAuthenticationObject& aAuth)
   475     {
   476     aAuth.CancelSetTimeout();
   477     }
   478 
   479 template class CAuthObjectTest<TAuthSetTimeout>;
   480 
   481 /**
   482  * Test set timeout.
   483  */
   484 const TDesC& TGetTimeout::Name()
   485 	{
   486 	_LIT(KName, "Get timeout");
   487 	return KName;
   488 	}
   489 
   490 void TGetTimeout::ConstructL(const TTestActionSpec& aTestActionSpec)
   491 	{
   492 	TPtrC8 ptr = Input::ParseElement(aTestActionSpec.iActionBody, KTimeoutStart);
   493 	if (ptr.Length() == 0)
   494 		User::Leave(KErrNotFound);
   495 	TLex8 lex(ptr);
   496 	lex.Val(iExpectedTimeout);
   497 	}
   498 
   499 void TGetTimeout::DoTest(MCTAuthenticationObject& aAuth, TRequestStatus& aStatus)
   500 	{
   501 	aAuth.Timeout(iTimeout, aStatus);
   502 	}	
   503 
   504 TBool TGetTimeout::CheckResult(Output& aOut)
   505 	{
   506 	aOut.writeString(_L("Expected "));
   507 	aOut.writeNum(iExpectedTimeout);
   508 	aOut.writeString(_L(", got "));
   509 	aOut.writeNum(iTimeout);
   510 	aOut.writeNewLine();
   511 
   512 	return iExpectedTimeout == iTimeout;
   513 	}
   514 
   515 void TGetTimeout::DoCancel(MCTAuthenticationObject& aAuth)
   516     {
   517     aAuth.CancelTimeout();
   518     }
   519 
   520 template class CAuthObjectTest<TGetTimeout>;