authenticationservices/authenticationserver/test/tauthcliserv/step_client.cpp
changeset 102 deec7e509f66
parent 94 0e6c5a9328b5
child 108 ca9a0fc2f082
equal deleted inserted replaced
94:0e6c5a9328b5 102:deec7e509f66
     1 /*
       
     2 * Copyright (c) 2005-2010 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 #include <test/testexecutelog.h>
       
    20 #include "tauthcliservstep.h"
       
    21 #include "authserver/aspubsubdefs.h"
       
    22 #include <e32property.h>
       
    23 #include <authserver/authpatchdata.h>
       
    24 #include <u32hal.h>
       
    25 #include <e32svr.h>
       
    26  
       
    27 using namespace AuthServer;
       
    28 
       
    29 class CAuthActive : CActive
       
    30     {
       
    31 public:
       
    32     CAuthActive(RAuthClient& aClient, CIdentity*& aResult, CTStepClient* aStep,
       
    33 				TBool aStop = ETrue) : 
       
    34          CActive(EPriorityNormal),
       
    35          iFirstTime(true),
       
    36          iClient(aClient), 
       
    37          iResult(aResult), 
       
    38          iStep(aStep),
       
    39 		 iStop(aStop)
       
    40 	    {
       
    41         CActiveScheduler::Add(this);
       
    42         }
       
    43 	
       
    44     void doAuthenticate(TPluginId aPlugin, TTimeIntervalSeconds aTime )
       
    45         {
       
    46 		iPlugin = aPlugin;
       
    47         SetActive();		
       
    48 		TRequestStatus* status = &iStatus;		    
       
    49 		User::RequestComplete(status, KErrNone);
       
    50 		iFirstTime = ETrue;
       
    51 		iTime = aTime;
       
    52         }
       
    53 	
       
    54         void DoCancel() 
       
    55         {
       
    56         }
       
    57         void RunL() 
       
    58         {
       
    59 		static TBool client = ETrue;
       
    60 		static TBool withString = ETrue;
       
    61 		client = !client;
       
    62         if (iFirstTime)
       
    63             {
       
    64 			// inactive | plugin id
       
    65             iAe = AuthExpr(0x10274106) | AuthExpr(iPlugin);
       
    66 			User::LeaveIfNull(iAe);
       
    67 			SetActive();
       
    68             iStatus = KRequestPending;
       
    69             iClient.AuthenticateL(*iAe, iTime, client, withString,
       
    70 								  iResult, iStatus);
       
    71             iFirstTime = false;
       
    72             }
       
    73         else
       
    74             {
       
    75 			if (iStop)
       
    76 				{
       
    77 				CActiveScheduler::Stop();
       
    78 				}
       
    79             delete iAe;
       
    80 			}
       
    81 		iErr =  iStatus.Int();
       
    82 		}
       
    83     TBool iFirstTime;
       
    84     RAuthClient& iClient;
       
    85     CIdentity*& iResult;
       
    86     CTStepClient* iStep;
       
    87     CAuthExpression* iAe;
       
    88     TPluginId        iPlugin;
       
    89 	TTimeIntervalSeconds iTime;
       
    90 	TInt iErr;
       
    91 	TBool iStop;
       
    92     };
       
    93 
       
    94 
       
    95 
       
    96 const TPluginId KIdentity1Id = 0xE0494423;
       
    97 _LIT(KIdentity1Desc, "identity-1-desc");
       
    98 const TPluginId KIdentity2Id = 0xE0494443;
       
    99 _LIT(KIdentity2Desc, "identity-2-desc");
       
   100 const TPluginId KIdentity3Id = 0xE0494453;
       
   101 
       
   102 
       
   103 
       
   104 CTStepClient::CTStepClient()
       
   105 /**
       
   106 	Record this test step's name.
       
   107  */
       
   108 	{
       
   109  	SetTestStepName(KTStepClient);
       
   110 	}
       
   111 
       
   112 
       
   113 void CTStepClient::TestIdentitiesL()
       
   114 /**
       
   115 	Test can retrieve correct identity arrays and
       
   116 	descritptinos from auth db.
       
   117  */
       
   118 	{
       
   119 	__UHEAP_MARK;
       
   120 	RemoveExistingDbL();
       
   121 	
       
   122 	RArray<TIdentityId> ids;
       
   123 	
       
   124 
       
   125 	// one identities (first start adds one)
       
   126 	GetIdentitiesLC(ids);
       
   127 	TESTL(ids.Count() == 1);
       
   128 	CleanupStack::PopAndDestroy(&ids);
       
   129 
       
   130 	
       
   131 	// two identity
       
   132 	AddIdentityL(KIdentity1Id, KIdentity1Desc);
       
   133 	GetIdentitiesLC(ids);
       
   134 	TESTL(ids.Count() == 2);
       
   135 	TESTL(ids[0] == KIdentity1Id || ids[1] == KIdentity1Id);
       
   136 	CleanupStack::PopAndDestroy(&ids);
       
   137 	TestIdentityL(KIdentity1Id, KIdentity1Desc);
       
   138 	
       
   139 	
       
   140 	// three identities
       
   141 	AddIdentityL(KIdentity2Id, KIdentity2Desc);
       
   142 	GetIdentitiesLC(ids);
       
   143 	TESTL(ids.Count() == 3);
       
   144 	TESTL(	ids.Find(KIdentity1Id) >= 0 && 
       
   145 	        ids.Find(KIdentity2Id) >= 0);
       
   146 	CleanupStack::PopAndDestroy(&ids);
       
   147 	TestIdentityL(KIdentity1Id, KIdentity1Desc);
       
   148 	TestIdentityL(KIdentity2Id, KIdentity2Desc);
       
   149 
       
   150 	// test fail to get description for non-existent identity
       
   151 	RAuthClient ac;
       
   152 	User::LeaveIfError(ac.Connect());
       
   153 	CleanupClosePushL(ac);
       
   154 	
       
   155 	
       
   156 	TRAPD(r, ac.IdentityStringL(KIdentity3Id));
       
   157 	TESTL(r == KErrAuthServIdentityNotFound);
       
   158 
       
   159 	// check id and string retrieval
       
   160 	RIdAndStringArray idWithString;
       
   161 	ac.IdentitiesWithStringL(idWithString);
       
   162 	CleanupClosePushL(idWithString);
       
   163 	
       
   164     for (TInt i = 0 ; i < idWithString.Count() ; ++i)
       
   165 	    {
       
   166 	    TestIdentityL(idWithString[i]->Id(), *idWithString[i]->String());
       
   167 		INFO_PRINTF3(_L("0x%x - %S"), idWithString[i]->Id(), idWithString[i]->String()); 
       
   168 	    }
       
   169 
       
   170 	CleanupStack::PopAndDestroy(2,&ac);
       
   171 	
       
   172 	__UHEAP_MARKEND;
       
   173 	}
       
   174 
       
   175 
       
   176 void CTStepClient::GetIdentitiesLC(RArray<TIdentityId>& aIds)
       
   177 /**
       
   178 	Populate the supplied array with the currently available
       
   179 	identities, as returned by the server.
       
   180 	
       
   181 	@param	aIds			Array to populate.  Any existing entries
       
   182 							are removed when this function is called.
       
   183 							On success, the array is placed on the
       
   184 							cleanup stack.
       
   185  */
       
   186 	{
       
   187 	aIds.Reset();
       
   188 	CleanupClosePushL(aIds);
       
   189 	
       
   190 	RAuthClient ac;
       
   191 	User::LeaveIfError(ac.Connect());
       
   192 	CleanupClosePushL(ac);
       
   193 	ac.IdentitiesL(aIds);	// clears array on failure?
       
   194 	CleanupStack::PopAndDestroy(&ac);
       
   195 	}
       
   196 
       
   197 
       
   198 void CTStepClient::AddIdentityL(TIdentityId aIdentityId, const TDesC& aDesc)
       
   199 /**
       
   200 	Add the described identity directly to the database.
       
   201 	
       
   202 	This function does not use the authentication manager APIs.
       
   203  */
       
   204 	{
       
   205 	WaitForServerToReleaseDb();
       
   206 	CAuthDb2* db = CAuthDb2::NewL(iFs);
       
   207 	CleanupStack::PushL(db);
       
   208 	db->AddIdentityL(aIdentityId, aDesc);
       
   209 	CleanupStack::PopAndDestroy(db);
       
   210 	}
       
   211 
       
   212 
       
   213 void CTStepClient::TestIdentityL(TIdentityId aIdentityId, const TDesC& aExpDesc)
       
   214 /**
       
   215 	Test the supplied identity has the supplied description,
       
   216 	as read via the RAuthClient API.
       
   217 	
       
   218 	@param	aIdentityId		Identity to retrieve description for.
       
   219 	@param	aExpDesc		The expected description.
       
   220  */
       
   221 	{
       
   222 	RAuthClient ac;
       
   223 	CleanupClosePushL(ac);
       
   224 	User::LeaveIfError(ac.Connect());
       
   225 	HBufC* desc = ac.IdentityStringL(aIdentityId);
       
   226 	CleanupStack::PushL(desc);
       
   227 	TESTL(*desc == aExpDesc);
       
   228 	CleanupStack::PopAndDestroy(2, &ac);
       
   229 	}
       
   230 
       
   231 
       
   232 /**
       
   233 	TestClientPluginListsL poulates this with a description for
       
   234 	every plugin that can be seen with ECOM.
       
   235  */
       
   236 static RCPointerArray<CPluginDesc> allPlugins;
       
   237 
       
   238 /**
       
   239 	Total number of plugins on the device.  Should be equal to
       
   240 	allPlugins.Count().
       
   241  */
       
   242 static TInt totalCount;
       
   243 
       
   244 /**
       
   245 	Maximum number of plugins which can are expected.  This is
       
   246 	an arbitrary value which is used to size an array.  It can be
       
   247 	changed if required.
       
   248  */
       
   249 const TInt KMaxSeenCount = 17;
       
   250 
       
   251 /**
       
   252 	This array stores how many times each plugin from allPlugins
       
   253 	has been seen.  It is set by SetSeenCountsL.
       
   254  */
       
   255 static TFixedArray<TInt, KMaxSeenCount> seenCounts;
       
   256 
       
   257 
       
   258 void CTStepClient::GetDescriptionsFromEComL(RPointerArray<CPluginDesc>& aDescs)
       
   259 /**
       
   260 	Populate the supplied array with plugin descriptions generated
       
   261 	from the available ECOM plugins.  The array can then be used to
       
   262 	check the results from the server.
       
   263  */
       
   264 	{
       
   265 	WaitForServerToReleaseDb();
       
   266 	
       
   267 	CAuthDb2* db = CAuthDb2::NewL(iFs);
       
   268 	CleanupStack::PushL(db);
       
   269 	
       
   270 	// typedef RPointerArray<CImplementationInformation> RImplInfoPtrArray;
       
   271 	RCPointerArray<CImplementationInformation> implInfo;
       
   272 	REComSession::ListImplementationsL(KCAuthPluginInterfaceUid, implInfo);
       
   273 	CleanupClosePushL(implInfo);
       
   274 	
       
   275 	TInt implCount = implInfo.Count();
       
   276 	TInt err = 0;
       
   277 	for (TInt i = 0; i < implCount; ++i)
       
   278 		{
       
   279 		// avoid RVCT warning C2874W: pi may be used before being set
       
   280 		CAuthPluginInterface* pi = 0;
       
   281 
       
   282 		TEComResolverParams resolverParams;
       
   283 		TBufC8<16> pluginIdTxt;
       
   284 		  
       
   285 		pluginIdTxt.Des().Format(_L8("%x"), implInfo[i]->ImplementationUid().iUid);
       
   286 		pluginIdTxt.Des().UpperCase();	  
       
   287 		resolverParams.SetDataType(pluginIdTxt);
       
   288 		 
       
   289 		//To load plugins from sources other than ROM the patch 
       
   290 		// data KEnablePostMarketAuthenticationPlugins must be set to True.
       
   291 		TUint32 enablePostMarketPlugin = KEnablePostMarketAuthenticationPlugins;
       
   292 
       
   293 #ifdef __WINS__
       
   294 
       
   295     	// Default SymbianOS behavior is to only load auth plugins from ROM.
       
   296 		enablePostMarketPlugin = 0;
       
   297 
       
   298 		// For the emulator allow the constant to be patched via epoc.ini
       
   299 		UserSvr::HalFunction(EHalGroupEmulator, EEmulatorHalIntProperty,
       
   300 		(TAny*)"KEnablePostMarketAuthenticationPlugins", &enablePostMarketPlugin); // read emulator property (if present)
       
   301 
       
   302 #endif			  
       
   303 		TAny* plugin = 0; 
       
   304 		TUid Dtor_ID_Key = TUid::Null();
       
   305 		if(enablePostMarketPlugin == 0) 
       
   306 			{
       
   307 			TRAP(err, plugin = 
       
   308 			REComSession::CreateImplementationL(KCAuthPluginInterfaceUid,
       
   309 												Dtor_ID_Key,
       
   310 												resolverParams,
       
   311 												KRomOnlyResolverUid));
       
   312 			}
       
   313 			  
       
   314 		else
       
   315 		  	{
       
   316 		  	TRAP(err, plugin = 
       
   317 			REComSession::CreateImplementationL(KCAuthPluginInterfaceUid,
       
   318 												Dtor_ID_Key,
       
   319 												resolverParams));
       
   320 		  	}
       
   321 					  
       
   322 		if (err == KErrAuthServNoSuchPlugin)
       
   323 			continue;
       
   324 		User::LeaveIfError(err);
       
   325 		pi = reinterpret_cast<CAuthPluginInterface*>(plugin);	
       
   326 	
       
   327 		// get training status from db
       
   328 		TAuthTrainingStatus ts = db->PluginStatusL(pi->Id());
       
   329 		
       
   330 		CleanupStack::PushL(pi);
       
   331 		CPluginDesc* pd = CPluginDesc::NewL(
       
   332 			pi->Id(), pi->Name(), pi->Type(),
       
   333 			ts, pi->MinEntropy(),
       
   334 			pi->FalsePositiveRate(), pi->FalseNegativeRate() );
       
   335 		CleanupStack::PopAndDestroy(pi);
       
   336 		REComSession::DestroyedImplementation(Dtor_ID_Key);
       
   337 		
       
   338 		CleanupStack::PushL(pd);
       
   339 		aDescs.AppendL(pd);
       
   340 		CleanupStack::Pop(pd);
       
   341 		}
       
   342 	
       
   343 	CleanupStack::PopAndDestroy(2, db);
       
   344 	REComSession::FinalClose();
       
   345 	}
       
   346 
       
   347 
       
   348 static TBool AreDescsEqual(const CPluginDesc& aLeft, const CPluginDesc& aRight)
       
   349 /**
       
   350 	Predicate function determines whether the two supplied
       
   351 	plugin descriptions contain the same information.
       
   352 
       
   353 	@param	aLeft			Plugin description to compare to aRight.
       
   354 	@param	aRight			Plugin description to compare to aLeft.
       
   355 	@return					Zero if the two descriptions contains different
       
   356 							information, non-zero otherwise.
       
   357  */
       
   358 	{
       
   359 	return	aLeft.Id() == aRight.Id()
       
   360 		&&	*aLeft.Name() == *aRight.Name()
       
   361 		&&	aLeft.Type() == aRight.Type()
       
   362 		&&	aLeft.TrainingStatus() == aRight.TrainingStatus()
       
   363 		&&	aLeft.MinEntropy() == aRight.MinEntropy()
       
   364 		&&	aLeft.FalsePositiveRate() == aRight.FalsePositiveRate()
       
   365 		&&	aLeft.FalseNegativeRate() == aRight.FalseNegativeRate();
       
   366 	}
       
   367 
       
   368 
       
   369 inline TBool AreDescsEqual(const CPluginDesc* aLeftP, const CPluginDesc* aRightP)
       
   370 	{
       
   371 	return AreDescsEqual(*aLeftP, *aRightP);
       
   372 	}
       
   373 	
       
   374 
       
   375 void CTStepClient::SetSeenCountsL(const RCPointerArray<const CPluginDesc>& aPlugins)
       
   376 /**
       
   377 	Update seenCounts array so each entry contains the
       
   378 	number of times that plugin was seen in aPlugins.
       
   379  */
       
   380 	{
       
   381 	const TInt suppliedCount = aPlugins.Count();
       
   382 	const TInt totalCount = allPlugins.Count();
       
   383 	
       
   384 	// i = index into seenCounts
       
   385 	for (TInt i = 0; i < totalCount; ++i)
       
   386 		{
       
   387 		seenCounts[i] = 0;
       
   388 		
       
   389 		// j = index into aPlugins
       
   390 		TInt j = 0;
       
   391 		for (; j < suppliedCount; ++j)
       
   392 			{
       
   393 			if (AreDescsEqual(*allPlugins[i], *aPlugins[j]))
       
   394 				{
       
   395 				++seenCounts[i];
       
   396 				break;		// assume plugins in allPlugins are unique
       
   397 				}
       
   398 			}
       
   399 		
       
   400 		// if plugin not found in reference set then abort
       
   401 		TESTL(j < totalCount);
       
   402 		}
       
   403 	}
       
   404 
       
   405 
       
   406 void CTStepClient::TestClientPluginListsL()
       
   407 /**
       
   408 	Test the expected plugins are returned when the
       
   409 	client asks the server to list them.
       
   410  */
       
   411 	{	
       
   412 	__UHEAP_MARK;
       
   413 	
       
   414 	CleanupClosePushL(allPlugins);
       
   415 	GetDescriptionsFromEComL(allPlugins);
       
   416 	PrepareTrainedPluginsL();
       
   417 	
       
   418 	// re-read plugins now training statuses have been set
       
   419 	allPlugins.ResetAndDestroy();	
       
   420 	GetDescriptionsFromEComL(allPlugins);
       
   421 	CleanupClosePushL(allPlugins);
       
   422 	
       
   423 	totalCount = allPlugins.Count();
       
   424 	TESTL(totalCount <= KMaxSeenCount);
       
   425 	
       
   426 	RAuthClient ac;
       
   427 	User::LeaveIfError(ac.Connect());
       
   428 	CleanupClosePushL(ac);
       
   429 	
       
   430 	// ensure all plugins are returned by PluginsL
       
   431 	RCPointerArray<const CPluginDesc> fullPlugins;
       
   432 	ac.PluginsL(fullPlugins);
       
   433 	CleanupClosePushL(fullPlugins);
       
   434 	TESTL(fullPlugins.Count() == totalCount);
       
   435 	SetSeenCountsL(fullPlugins);
       
   436 	for (TInt k = 0; k < totalCount; ++k)
       
   437 		{
       
   438 		TESTL(seenCounts[k] == 1);
       
   439 		}
       
   440 	CleanupStack::PopAndDestroy(&fullPlugins);
       
   441 	
       
   442 	// ensure expected plugins are returned for each type
       
   443 	TestTypedPluginsL(ac, EAuthKnowledge);
       
   444 	TestTypedPluginsL(ac, EAuthBiometric);
       
   445 	TestTypedPluginsL(ac, EAuthToken);
       
   446 
       
   447 	TestActivePluginsL(ac);
       
   448 	
       
   449 	CleanupStack::PopAndDestroy(&ac);
       
   450 	
       
   451 	TestTrainedPluginsL();
       
   452 	
       
   453 	CleanupStack::PopAndDestroy(2, &allPlugins);
       
   454 	
       
   455 	__UHEAP_MARKEND;
       
   456 	}
       
   457 
       
   458 
       
   459 void CTStepClient::TestTypedPluginsL(RAuthClient& aClient,
       
   460 									 TAuthPluginType aPluginType)
       
   461 /**
       
   462 	Ensure the plugin descriptions returned by the server
       
   463 	match those directly seen by ECOM.
       
   464 	
       
   465 	@param	aPluginType		Type of plugin to retrieve.
       
   466  */
       
   467 	{
       
   468 	__UHEAP_MARK;
       
   469 	RCPointerArray<const CPluginDesc> typedPlugins;
       
   470 	aClient.PluginsOfTypeL(aPluginType, typedPlugins);
       
   471 	CleanupClosePushL(typedPlugins);
       
   472 	SetSeenCountsL(typedPlugins);
       
   473 	
       
   474 	for (TInt k = 0; k < totalCount; ++k)
       
   475 		{
       
   476 		TInt& recCount = seenCounts[k];
       
   477 		TESTL(recCount == (allPlugins[k]->Type() == aPluginType) ? 1 : 0);
       
   478 		}
       
   479 	
       
   480 	CleanupStack::PopAndDestroy(&typedPlugins);
       
   481 	__UHEAP_MARKEND;
       
   482 	}
       
   483 
       
   484 
       
   485 void CTStepClient::TestActivePluginsL(RAuthClient& aClient)
       
   486 /**
       
   487 	Ensure the plugin descriptions returned by the server
       
   488 	match those directly seen by ECOM.
       
   489  */
       
   490 	{
       
   491 	__UHEAP_MARK;
       
   492 	RCPointerArray<const CPluginDesc> activePlugins;
       
   493 	aClient.ActivePluginsL(activePlugins);
       
   494 	CleanupClosePushL(activePlugins);
       
   495 		
       
   496 	CleanupStack::PopAndDestroy(&activePlugins);
       
   497 	__UHEAP_MARKEND;
       
   498 	}
       
   499 
       
   500 void CTStepClient::TestTrainedPluginsL()
       
   501 /**
       
   502 	Test the expected plugins are returned when filtering
       
   503 	for training types.
       
   504 	
       
   505 	@param	aClient			Client with open connection to auth server.
       
   506  */
       
   507 	{
       
   508 	RCPointerArray<const CPluginDesc> pids;
       
   509 	CleanupClosePushL(pids);
       
   510 	
       
   511 	RAuthClient ac;
       
   512 	User::LeaveIfError(ac.Connect());
       
   513 	CleanupClosePushL(ac);
       
   514 	
       
   515 	const CPluginDesc* pdsc0 = allPlugins[0];
       
   516 	const CPluginDesc* pdsc1 = allPlugins[1];
       
   517 	const CPluginDesc* pdsc2 = allPlugins[2];
       
   518 	const CPluginDesc* pdsc3 = allPlugins[3];
       
   519 
       
   520 	ac.PluginsWithTrainingStatusL(EAuthUntrained, pids);
       
   521 	TInt filterCount = pids.Count();
       
   522 	TESTL(filterCount == totalCount - 4);
       
   523 	for (TInt i = 0; i < filterCount; ++i)
       
   524 	{
       
   525 		const CPluginDesc* pidF = pids[i];
       
   526 		TBool eq0 = AreDescsEqual(pidF, pdsc0);
       
   527 		TBool eq1 = AreDescsEqual(pidF, pdsc1);
       
   528 		TBool eq2 = AreDescsEqual(pidF, pdsc2);
       
   529 		TBool eq3 = AreDescsEqual(pidF, pdsc3);
       
   530 		TESTL(!eq0 && !eq1 && !eq2 && !eq3);
       
   531 		}
       
   532 
       
   533 	pids.ResetAndDestroy();
       
   534 	ac.PluginsWithTrainingStatusL(EAuthTrained, pids);
       
   535 	TESTL(pids.Count() == 2);
       
   536 	TESTL(	(AreDescsEqual(pids[0], pdsc0) && AreDescsEqual(pids[1], pdsc1))
       
   537 		||	(AreDescsEqual(pids[0], pdsc1) && AreDescsEqual(pids[1], pdsc0)) );
       
   538 	
       
   539 	pids.ResetAndDestroy();
       
   540 	ac.PluginsWithTrainingStatusL(EAuthFullyTrained, pids);
       
   541 	TESTL(pids.Count() == 2);
       
   542 	TESTL(	(AreDescsEqual(pids[0], pdsc2) && AreDescsEqual(pids[1], pdsc3))
       
   543 		||	(AreDescsEqual(pids[0], pdsc3) && AreDescsEqual(pids[1], pdsc2)) );
       
   544 	
       
   545 	CleanupStack::PopAndDestroy(2, &pids);
       
   546 	}
       
   547 
       
   548 
       
   549 void CTStepClient::PrepareTrainedPluginsL()
       
   550 /**
       
   551 	Helper function for TestTrainedPluginsL.
       
   552 	
       
   553 	Train some plugins so they can be filtered out later.
       
   554 	This function replaces the database.
       
   555  */
       
   556 	{
       
   557 	// There have to be at least four available plugins
       
   558 	// to run the training status test.
       
   559 	TESTL(allPlugins.Count() >= 4);
       
   560 	
       
   561 	RemoveExistingDbL();
       
   562 	CAuthDb2* db = CAuthDb2::NewLC(iFs);
       
   563 
       
   564 	db->AddIdentityL(KIdentity1Id, KIdentity1Desc);
       
   565 	db->AddIdentityL(KIdentity2Id, KIdentity2Desc);
       
   566 
       
   567 	const CTransientKeyInfo& tki = *iId1Keys[0];
       
   568 	
       
   569 	// partially train plugins zero and one
       
   570 	TPluginId pid0 = allPlugins[0]->Id();
       
   571 	TPluginId pid1 = allPlugins[1]->Id();
       
   572 	db->SetTrainedPluginL(KIdentity1Id, pid0, tki);
       
   573 	db->SetTrainedPluginL(KIdentity2Id, pid1, tki);
       
   574 
       
   575 	// fully train plugins two and three
       
   576 	TPluginId pid2 = allPlugins[2]->Id();
       
   577 	TPluginId pid3 = allPlugins[3]->Id();
       
   578 	db->SetTrainedPluginL(KIdentity1Id, pid2, tki);
       
   579 	db->SetTrainedPluginL(KIdentity2Id, pid2, tki);
       
   580 	db->SetTrainedPluginL(KIdentity1Id, pid3, tki);
       
   581 	db->SetTrainedPluginL(KIdentity2Id, pid3, tki);
       
   582 
       
   583 	CleanupStack::PopAndDestroy(db);
       
   584 	}
       
   585 
       
   586 TBool CTStepClient::TestMultiAuthL()
       
   587 	{
       
   588 	INFO_PRINTF1(_L("Testing only a single client can make an auth "));
       
   589 	INFO_PRINTF1(_L("request at any one time"));	
       
   590 	
       
   591 	RAuthClient ac1;
       
   592 	User::LeaveIfError(ac1.Connect());
       
   593 	CleanupClosePushL(ac1);
       
   594 
       
   595 	RAuthClient ac2;
       
   596 	User::LeaveIfError(ac2.Connect());
       
   597 	CleanupClosePushL(ac2);
       
   598 
       
   599     CIdentity* result1 = 0;
       
   600     CAuthActive active1(ac1, result1, this);
       
   601     active1.doAuthenticate(KTestPluginBlocking,0);
       
   602 
       
   603     CIdentity* result2 = 0;
       
   604     CAuthActive active2(ac2, result2, this, EFalse);
       
   605     active2.doAuthenticate(KTestPluginBlocking,0);
       
   606 	
       
   607 	CActiveScheduler::Start();
       
   608 
       
   609     TEST(active1.iErr == KErrNone);
       
   610 	TEST(active2.iErr == KErrServerBusy);
       
   611 	
       
   612     delete result1;
       
   613 	delete result2;
       
   614 	
       
   615 	CleanupStack::PopAndDestroy(2,&ac1);
       
   616 	return ETrue;
       
   617 	}
       
   618 
       
   619 
       
   620 TBool CTStepClient::TestAuthenticateL()
       
   621 /**
       
   622 	Send simple authentication request.
       
   623  */
       
   624 	{
       
   625 	// create a transient key
       
   626 	CProtectionKey* key = CProtectionKey::NewLC(8);
       
   627 	CTransientKeyInfo* tki = CTransientKeyInfo::NewLC(KTestPluginId22);
       
   628 	
       
   629 	CTransientKey* tk = tki->CreateTransientKeyL(KIdentifyData);
       
   630 	CleanupStack::PushL(tk);
       
   631 	
       
   632 	CEncryptedProtectionKey* epKey = tk->EncryptL(*key);
       
   633 	CleanupStack::PushL(epKey);
       
   634 	
       
   635 	tki->SetEncryptedProtectionKeyL(epKey);
       
   636 	CleanupStack::Pop(epKey);	// now owned by tki
       
   637 	
       
   638 	// create identity 22, which identified by test plugin
       
   639 	WaitForServerToReleaseDb();
       
   640 	CAuthDb2* db = CAuthDb2::NewL(iFs);
       
   641 	CleanupStack::PushL(db);	
       
   642 	db->AddIdentityL(22, _L("22-desc"));
       
   643 	db->SetTrainedPluginL(22, KTestPluginId22, *tki);
       
   644 
       
   645 	CleanupStack::PopAndDestroy(4, key);	// tki, tk, db
       
   646 	
       
   647 	RAuthClient ac;
       
   648 	User::LeaveIfError(ac.Connect());
       
   649 	CleanupClosePushL(ac);
       
   650 
       
   651 	RAuthMgrClient amc;
       
   652 	User::LeaveIfError(amc.Connect());
       
   653 	CleanupClosePushL(amc);
       
   654 
       
   655 	TInt eventId = KUnknownIdentity;
       
   656 	TLastAuth lastAuth;
       
   657 	TPckg<TLastAuth> lastAuthPckg(lastAuth);
       
   658 
       
   659 	TInt err = RProperty::Get(KAuthServerSecureId, KUidAuthServerLastAuth,
       
   660 							  lastAuthPckg);
       
   661 	RProperty::Get(KAuthServerSecureId, KUidAuthServerAuthChangeEvent, eventId);
       
   662 	TESTL(eventId == KUnknownIdentity);
       
   663 	TESTL(lastAuth.iId == KUnknownIdentity);
       
   664 	TESTL(lastAuth.iAuthTime == 0);
       
   665 	TESTL(lastAuth.iMaxCombinations == 0);
       
   666 	TESTL(lastAuth.iFalsePositiveRate == 0);
       
   667 	TESTL(lastAuth.iFalseNegativeRate == 0);
       
   668 	TESTL(lastAuth.iNumFactors == 0);
       
   669 	
       
   670 	// authenticate the client with the test plugin but using type
       
   671 	
       
   672 	amc.SetPreferredTypePluginL(EAuthKnowledge,
       
   673 								KTestPluginId22);
       
   674     CleanupStack::PopAndDestroy(&amc);
       
   675 	TESTL(KTestPluginId22 == ac.PreferredTypePluginL(EAuthKnowledge));
       
   676 
       
   677 	CAuthExpression* ae = AuthExpr(EAuthKnowledge);		// plugin type
       
   678 	User::LeaveIfNull(ae);
       
   679 	CleanupStack::PushL(ae);
       
   680 
       
   681     CIdentity* result = 0;
       
   682     result = ac.AuthenticateL(
       
   683     	*ae,		// aExpression
       
   684     	0,			// aTimeout
       
   685     	ETrue,     // aClientSpecificKey       
       
   686 		EFalse);	// aWithString
       
   687 	INFO_PRINTF3(_L("1. Id = 0x%x , KeyLength = %d\n"), result->Id(),
       
   688 				 result->Key().KeyData().Size());
       
   689 	TESTL(result->Id() == 22);
       
   690 	delete result;
       
   691 
       
   692 	RProperty::Get(KAuthServerSecureId, KUidAuthServerLastAuth, lastAuthPckg);
       
   693 	RProperty::Get(KAuthServerSecureId, KUidAuthServerAuthChangeEvent, eventId);
       
   694 	TESTL(eventId == eventId);
       
   695 	TESTL(lastAuth.iId == 22);
       
   696 	TTime now;
       
   697 	TTimeIntervalSeconds since;
       
   698 	now.UniversalTime();
       
   699 	now.SecondsFrom(lastAuth.iAuthTime, since);
       
   700 	TESTL(since < TTimeIntervalSeconds(5));
       
   701 	TESTL(lastAuth.iMaxCombinations == KEntropy);
       
   702 	TESTL(lastAuth.iFalsePositiveRate == KFalsePos);
       
   703 	TESTL(lastAuth.iFalseNegativeRate == KFalseNeg);
       
   704 	TESTL(lastAuth.iNumFactors == 1);
       
   705 	
       
   706 	
       
   707 	// authenticate again, to get cached value
       
   708     result = ac.AuthenticateL(
       
   709     	*ae,		// aExpression
       
   710     	55,			// aTimeout
       
   711     	ETrue,      // aClientSpecificKey
       
   712 		ETrue);	    // aWithString
       
   713 	TDesC p = result->String();
       
   714 	INFO_PRINTF4(_L("2. Id = 0x%x , KeyLength = %d, String = %S\n"), result->Id(),
       
   715 				 result->Key().KeyData().Size(), &p);
       
   716 	TESTL(result->Id() == 22);
       
   717 	delete result;
       
   718 
       
   719 	
       
   720 	// deauthenticate
       
   721 	TRAP(err, ac.DeauthenticateL());
       
   722 	TESTL(err == KErrNone);
       
   723 
       
   724 	RProperty::Get(KAuthServerSecureId, KUidAuthServerLastAuth, lastAuthPckg);
       
   725 	RProperty::Get(KAuthServerSecureId, KUidAuthServerAuthChangeEvent, eventId);
       
   726 	TESTL(eventId == KUnknownIdentity);
       
   727 	TESTL(lastAuth.iId == KUnknownIdentity);	
       
   728 	TESTL(lastAuth.iAuthTime == 0);
       
   729 	TESTL(lastAuth.iMaxCombinations == 0);
       
   730 	TESTL(lastAuth.iFalsePositiveRate == 0);
       
   731 	TESTL(lastAuth.iFalseNegativeRate == 0);
       
   732 	TESTL(lastAuth.iNumFactors == 0);
       
   733 		
       
   734     result = 0;
       
   735     CAuthActive active(ac, result, this);
       
   736     active.doAuthenticate(KTestPluginId22,0);		
       
   737 	CActiveScheduler::Start();
       
   738 	INFO_PRINTF3(_L("3. Id = 0x%x , KeyLength = %d"), result->Id(),
       
   739 				 result->Key().KeyData().Size());
       
   740     TESTL(result->Id() == 22);
       
   741 	delete result;
       
   742 
       
   743 	
       
   744 	// unknown
       
   745     result = 0;
       
   746     active.doAuthenticate(KTestPluginIdUnknown,0);		
       
   747 	CActiveScheduler::Start();
       
   748 	INFO_PRINTF2(_L("4. Id = 0x%x"),result->Id());
       
   749 	TESTL(result->Id() == KUnknownIdentity);
       
   750 	delete result;
       
   751 
       
   752 	
       
   753 	// failure (bad plugin)
       
   754     result = 0;
       
   755     active.doAuthenticate(0xD0DAD0DA,0);		
       
   756 	CActiveScheduler::Start();
       
   757 	INFO_PRINTF3(_L("5. CAuthActive:RunL err %d : res = 0x%x"),
       
   758 				 active.iErr, result);
       
   759 	TESTL(active.iErr == KErrAuthServNoSuchPlugin && result == 0);
       
   760 	
       
   761 	CleanupStack::PopAndDestroy(2, &ac);	// ae, ac
       
   762 	return ETrue;
       
   763 	}
       
   764 
       
   765 void CTStepClient::TestSetIdentityStrL()
       
   766     {
       
   767 	// assumes TestAuthenticateL has been called and id 22 is created
       
   768 	RAuthClient ac;
       
   769 	User::LeaveIfError(ac.Connect());
       
   770 	CleanupClosePushL(ac);
       
   771 
       
   772 	_LIT(KId22DescNew, "ID22NewDesc");
       
   773 
       
   774 	HBufC* desc = ac.IdentityStringL(22);
       
   775 	CleanupStack::PushL(desc);
       
   776 	TESTL(*desc == _L("22-desc")); 
       
   777 	CleanupStack::PopAndDestroy(desc); 
       
   778 	
       
   779 	ac.SetIdentityStringL(22, KId22DescNew);
       
   780 	
       
   781 	desc = ac.IdentityStringL(22);
       
   782 	CleanupStack::PushL(desc); 
       
   783 	TESTL(*desc == KId22DescNew); 
       
   784 	CleanupStack::PopAndDestroy(desc); 
       
   785 	
       
   786 	CleanupStack::PopAndDestroy(&ac);
       
   787 	}    
       
   788 
       
   789 						 
       
   790 
       
   791 TVerdict CTStepClient::doTestStepL()
       
   792 	{
       
   793 	if (TestStepResult() != EPass)
       
   794 		return TestStepResult();
       
   795 	SetTestStepResult(EPass);
       
   796 	__UHEAP_MARK;
       
   797 	
       
   798 	CActiveScheduler::Install(iActSchd);
       
   799 	
       
   800 	INFO_PRINTF1(_L("Test identities"));
       
   801 	TestIdentitiesL();
       
   802 	
       
   803 	INFO_PRINTF1(_L("Test ClientPluginLists"));
       
   804  	TestClientPluginListsL();
       
   805  	
       
   806  	INFO_PRINTF1(_L("Test Authenticate"));
       
   807 	TestAuthenticateL();
       
   808 	
       
   809 	INFO_PRINTF1(_L("Test MultiAuth"));
       
   810 	TestMultiAuthL();
       
   811 	
       
   812 	INFO_PRINTF1(_L("Test SetIdentityStr"));
       
   813 	TestSetIdentityStrL();
       
   814 	
       
   815 	__UHEAP_MARKEND;	
       
   816 	
       
   817 	return EPass;
       
   818 	}
       
   819 
       
   820