authenticationservices/authenticationserver/test/tAuthSvr/src/step_RegIdentity.cpp
changeset 29 ece3df019add
equal deleted inserted replaced
19:cd501b96611d 29:ece3df019add
       
     1 /*
       
     2 * Copyright (c) 2006-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 * CTestStep derived implementation
       
    16 *
       
    17 */
       
    18 
       
    19  
       
    20 #include "tAuthSvrStep.h"
       
    21 #include <s32file.h>
       
    22 class CAuthActive2 : public CActive
       
    23     {
       
    24     public:
       
    25     CAuthActive2(RAuthMgrClient& aClient, CAuthExpression* aExpr,
       
    26 				 TInt aFresh, TBool aClientSpecific, TBool aStop = ETrue) : 
       
    27          CActive(EPriorityNormal),
       
    28          iClient(aClient),
       
    29 		 iResult(EFail),
       
    30          iRunCount(0),
       
    31          iAe(aExpr),
       
    32          iStop(aStop),
       
    33          iFresh(aFresh),
       
    34          iIdToStore(0), 
       
    35          iClientSpecific(aClientSpecific)
       
    36         {
       
    37         CActiveScheduler::Add(this);
       
    38         }
       
    39     void doAuth() 
       
    40         {
       
    41         SetActive();		
       
    42 		TRequestStatus* status = &iStatus;		    
       
    43 		User::RequestComplete(status, KErrNone);
       
    44 		iRunCount = 0;
       
    45         }
       
    46     void doDeAuth()
       
    47         {
       
    48         iClient.DeauthenticateL();
       
    49         }
       
    50 	void DoCancel() 
       
    51         {
       
    52         }
       
    53      void RunL() 
       
    54         {
       
    55         iErr = iStatus.Int();
       
    56 		switch (iRunCount)
       
    57 		  {
       
    58 		  case 0:
       
    59             iStatus = KRequestPending;
       
    60 			iId = 0;
       
    61 
       
    62 			iClient.AuthenticateL(*iAe, iFresh, iClientSpecific, EFalse, iId, iStatus);
       
    63 			SetActive();
       
    64 
       
    65 			break;
       
    66 		  case 1:
       
    67 			if (0 != iId)
       
    68 				{
       
    69 				iIdToStore = iId->Id();
       
    70 				delete iId;
       
    71 				}
       
    72 			iStatus = KRequestPending;
       
    73 			TRequestStatus* status;
       
    74 			status = &iStatus;				
       
    75 			User::RequestComplete(status, iErr);				
       
    76 			SetActive();
       
    77 			
       
    78 			break;
       
    79    		  case 2:
       
    80 			iResult = iStatus == KErrNone ? EPass : EFail;
       
    81             iErr = iStatus.Int();
       
    82 			if (iStop)
       
    83                 {
       
    84                 CActiveScheduler::Stop();
       
    85                 }
       
    86 
       
    87 			break;
       
    88      	  default:
       
    89 			iResult = EFail;
       
    90 			
       
    91 			if (iStop)
       
    92 				{
       
    93 			    CActiveScheduler::Stop();
       
    94 			    }
       
    95 		    }
       
    96 		  ++iRunCount;
       
    97           }
       
    98 	RAuthMgrClient& iClient;
       
    99 	TVerdict iResult;
       
   100 	TInt iRunCount;
       
   101 	CAuthExpression* iAe;
       
   102 	CIdentity* iId;
       
   103     TBool iStop;
       
   104     TInt iErr;
       
   105     TInt iFresh;
       
   106     TIdentityId iIdToStore;
       
   107     TBool iClientSpecific;
       
   108     };
       
   109 
       
   110 //====================================================================================================
       
   111 
       
   112 
       
   113 class CRegActive : public CActive
       
   114     {
       
   115     public:
       
   116     CRegActive(RAuthMgrClient& aClient, HBufC* aIdentityString, CIdentity*& aResult) : 
       
   117          CActive(EPriorityNormal),
       
   118          iFirstTime(true),
       
   119          iClient(aClient), 
       
   120          iResult(aResult),
       
   121          iIdentityString(aIdentityString),
       
   122          iErr(KErrNone)
       
   123         {
       
   124         CActiveScheduler::Add(this);
       
   125         }
       
   126     ~CRegActive()
       
   127     	{
       
   128     	delete iIdentityString; 
       
   129     	}
       
   130     void doReg()
       
   131         {
       
   132         SetActive();		
       
   133 		TRequestStatus* status = &iStatus;
       
   134 		User::RequestComplete(status, KErrNone);
       
   135 		iFirstTime = ETrue;
       
   136         }
       
   137         void DoCancel() 
       
   138         {
       
   139         }
       
   140      void RunL() 
       
   141         {
       
   142         iErr = iStatus.Int();
       
   143         if (iFirstTime)
       
   144             {
       
   145 			SetActive();
       
   146             iStatus = KRequestPending;
       
   147             iClient.RegisterIdentityL(iResult, *iIdentityString, iStatus);
       
   148             iFirstTime = false;
       
   149             }
       
   150         else
       
   151             {
       
   152             iErr = iStatus.Int();
       
   153             CActiveScheduler::Stop();
       
   154 			}
       
   155         }
       
   156         
       
   157     TBool iFirstTime;
       
   158     RAuthMgrClient& iClient;
       
   159     CIdentity*& iResult;
       
   160     HBufC* iIdentityString;
       
   161     TInt iErr;
       
   162     };
       
   163 
       
   164 //====================================================================================================
       
   165 
       
   166 CTRegIdentity::~CTRegIdentity()
       
   167 /**
       
   168   Destructor
       
   169  */
       
   170 	{}
       
   171 
       
   172 CTRegIdentity::CTRegIdentity(CTAuthSvrServer& aParent): iParent(aParent)
       
   173 /**
       
   174   Constructor
       
   175  */
       
   176 	{
       
   177 	
       
   178 	// Call base class method to set up the human readable name for logging
       
   179 	SetTestStepName(KTAuthSvrCheck);
       
   180 	}
       
   181 
       
   182 TVerdict CTRegIdentity::doTestStepPreambleL()
       
   183 /**
       
   184   @return - TVerdict code
       
   185   Override of base class virtual
       
   186  */
       
   187 	{
       
   188 	//Call the parent class preamble, setting up the file server, etc
       
   189 	CTStepActSch::doTestStepPreambleL();	
       
   190 	return TestStepResult();
       
   191 	}
       
   192 
       
   193 TVerdict CTRegIdentity::doTestStepL()
       
   194 /**
       
   195   @return - TVerdict code
       
   196   Override of base class pure virtual
       
   197   
       
   198  */
       
   199 	{	
       
   200 	SetTestStepResult(EPass);	
       
   201 __UHEAP_MARK;		// Check for memory leaks
       
   202 	SetPinPluginStateL();
       
   203 	
       
   204 	//-----------------------------------------------------------------------------------------------------	
       
   205 	InitAuthServerFromFileL();	// Set things like 'iSupportsDefaultData' and 'DefaultPlugin'
       
   206 	
       
   207 	// this method creates the dat file from where the test implementation
       
   208 	// of pin plugin notifier reads user input.
       
   209 	// as the pin plugin fails without this data it has been included 
       
   210 	// in the code as a default step for initializing the pin plugin
       
   211 	// data.
       
   212 	TPinValue aPinValue;
       
   213 	CreatePinPluginInputFileL(EPinPluginTraining,aPinValue);
       
   214 	
       
   215 	CActiveScheduler::Install(iActSchd);
       
   216 	//Connect to the AuthServer	
       
   217 	AuthServer::RAuthMgrClient authMgrClient1;	
       
   218 	TInt connectVal = authMgrClient1.Connect();
       
   219 	if (KErrNotFound == connectVal)
       
   220 		{
       
   221 		//Retry after a delay
       
   222 		TTimeIntervalMicroSeconds32 timeInterval = 2000;	//2 Milliseconds
       
   223 		User::After(timeInterval);
       
   224 		connectVal = authMgrClient1.Connect();
       
   225 		}
       
   226 	if (KErrNone != connectVal)
       
   227 		{
       
   228 		ERR_PRINTF2(_L("Unable to start a session or other connection error. Err = %d"), connectVal);
       
   229 		RemovePinPluginFileL();
       
   230 		User::LeaveIfError(connectVal);
       
   231 		}	
       
   232 	CleanupClosePushL(authMgrClient1);
       
   233 
       
   234 	//Examine the authserver and see what's there
       
   235 	ListPluginsL(authMgrClient1);
       
   236 	
       
   237 	TBool statusAll = EFalse;
       
   238 	statusAll = CheckPluginStatusAllL(authMgrClient1);
       
   239 	
       
   240 	//Check the username of the second identity and then attempt to set it.
       
   241 //	SetTestStepResult(checkAndSetUserNameL(authMgrClient1, 0));	
       
   242 	
       
   243 	// Get the identity string (if defined)
       
   244 	TPtrC identityString;
       
   245 	if (!GetStringFromConfig(ConfigSection(), _L("IdentityString"), identityString))
       
   246 		{
       
   247 		identityString.Set(_L("SOMENAME"));
       
   248 		}
       
   249 	
       
   250 	//Register an identity using an active object
       
   251 	CIdentity* identity1 = 0;
       
   252     CRegActive* active = new (ELeave) CRegActive(authMgrClient1, identityString.AllocL(), identity1); // Ownership transferred
       
   253     active->doReg();
       
   254 	CActiveScheduler::Start();
       
   255 	TInt err = active->iErr;
       
   256 	delete active;
       
   257 
       
   258 	SetTestStepError(err);
       
   259 
       
   260 	if(KErrAuthServPluginQuit == err)
       
   261 		{
       
   262 		INFO_PRINTF1(_L("Training was Quit."));
       
   263 		INFO_PRINTF1(_L("User entered the Quit code as trainingInput."));		
       
   264 		}
       
   265 	else if (KErrAuthServPluginCancelled == err)
       
   266 		{
       
   267 		INFO_PRINTF1(_L("Training was cancelled."));
       
   268 		INFO_PRINTF1(_L("User entered trainingInput same as identifyingInput or an existing PIN."));
       
   269 		INFO_PRINTF1(_L("Or the Cancel code."));		
       
   270 		}
       
   271     else if ((identity1 == 0) && (KErrAuthServPluginQuit != err))
       
   272         {
       
   273         ERR_PRINTF1(_L("An unexpected error occurred during the registration process."));
       
   274         SetTestStepResult(EFail);
       
   275         }
       
   276     else
       
   277   		{
       
   278   		if (0 != identity1)
       
   279   			{
       
   280   			INFO_PRINTF3(_L("Id = %x , KeyLength = %d\n"), identity1->Id(), identity1->Key().KeyData().Size());
       
   281   			}	
       
   282 		//Get a list of all the present identities
       
   283 		RIdentityIdArray ids;
       
   284 		authMgrClient1.IdentitiesL(ids);
       
   285 		CleanupClosePushL(ids);
       
   286 		TInt actualNumIds = ids.Count();
       
   287 				
       
   288 		//If specified in the ini file, check the number of identities now present
       
   289 		TInt numIdsValue = 0;
       
   290 		if (GetIntFromConfig(ConfigSection(),_L("NumIdentities"), numIdsValue) != EFalse) // the tag 'numIds' was present
       
   291 			{
       
   292 			if (actualNumIds != numIdsValue)
       
   293 				{
       
   294 				ERR_PRINTF3(_L("Error. ActualNumIds = %d, numIdsExpected = %d"), actualNumIds, numIdsValue);
       
   295 				SetTestStepResult(EFail);
       
   296 				}				
       
   297 			}
       
   298 		//List the present identities by their strings
       
   299 		for (TInt i = 0; i < actualNumIds; i++)
       
   300 			{
       
   301 			HBufC* idName = 0;			
       
   302 			idName = authMgrClient1.IdentityStringL(ids[i]);
       
   303 			INFO_PRINTF3(_L("Id %d has the name %S"), i, idName);
       
   304 			delete idName; 
       
   305 			}		
       
   306 		CleanupStack::PopAndDestroy(&ids);
       
   307 		delete identity1;				
       
   308 	
       
   309   		}//End check for when identity1 == 0, i.e 'RegisterIdentity() fails
       
   310 
       
   311 	//Examine the authserver and see what's there
       
   312 	ListPluginsL(authMgrClient1);
       
   313 	
       
   314 	statusAll = CheckPluginStatusAllL(authMgrClient1);
       
   315 	if (!statusAll)
       
   316 		{
       
   317 		SetTestStepResult(EFail);
       
   318 		}
       
   319 	CleanupStack::PopAndDestroy(&authMgrClient1);	// authClient1	
       
   320 	
       
   321 	RemovePinPluginFileL();
       
   322 	
       
   323 	//Garbage collect the last previously destroyed implementation 
       
   324 	// and close the REComSession if no longer in use
       
   325 	REComSession::FinalClose(); 	
       
   326 __UHEAP_MARKEND;
       
   327 	return TestStepResult();	
       
   328 	}
       
   329 
       
   330 void CTRegIdentity::ListPluginsL(AuthServer::RAuthMgrClient& aAuthMgrClient )
       
   331 	{
       
   332 	RPluginDescriptions pluginList1;
       
   333 	TCleanupItem cleanup(CleanupEComArray, &pluginList1);
       
   334 	CleanupStack::PushL(cleanup);					
       
   335 	aAuthMgrClient.PluginsL(pluginList1);	
       
   336 	TInt numTotalPlugins = pluginList1.Count();
       
   337 	TInt i;
       
   338 	for(i = 0; i < numTotalPlugins; i++)
       
   339 		{
       
   340 		TInt presentPluginIdVal = pluginList1[i]->Id();
       
   341 		TInt presentPluginStatus = pluginList1[i]->TrainingStatus();
       
   342 		INFO_PRINTF3(_L("PluginId=%x,status =%i"), presentPluginIdVal,presentPluginStatus);
       
   343 		}
       
   344 	CleanupStack::PopAndDestroy(&pluginList1); //infoArray, results in a call to CleanupEComArray	
       
   345 	
       
   346 	}
       
   347 
       
   348 void CTRegIdentity::SetPinPluginStateL()
       
   349 	{
       
   350 	TPtrC activeStateFromFile;
       
   351 	
       
   352 	_LIT(KPinPlugin,"pinplugin_inactive.txt");
       
   353 	
       
   354 	TFileName filename;
       
   355 	filename.Copy(KPinPlugin); // convert from 8 -> 16 bit descriptor
       
   356 		
       
   357 	TDriveUnit sysDrive = RFs::GetSystemDrive();
       
   358 	TDriveName sysDriveName (sysDrive.Name());
       
   359 	filename.Insert(0,sysDriveName);
       
   360 	filename.Insert(2,_L("\\"));		
       
   361 	
       
   362 	RFileWriteStream stream;
       
   363 	RFs fs;
       
   364 	CleanupClosePushL(fs);
       
   365 	User::LeaveIfError(fs.Connect());
       
   366 	fs.Delete(filename);
       
   367 		
       
   368 	if (GetStringFromConfig(ConfigSection(),_L("Pin200032E5Active"), activeStateFromFile)) 
       
   369 		{
       
   370 		if(activeStateFromFile == _L("false"))
       
   371 			{		
       
   372 			User::LeaveIfError(stream.Create(fs, filename, EFileWrite | EFileShareExclusive));
       
   373 			stream.Close();
       
   374 			}
       
   375 		}
       
   376 	CleanupStack::PopAndDestroy(); // fs
       
   377 	}
       
   378 
       
   379 void CTRegIdentity::RemovePinPluginFileL()
       
   380 	{
       
   381 	TPtrC activeStateFromFile;
       
   382 	_LIT(KPinPlugin,"pinplugin_inactive.txt");
       
   383 	TFileName filename;
       
   384 	filename.Copy(KPinPlugin); // convert from 8 -> 16 bit descriptor
       
   385 			
       
   386 	TDriveUnit sysDrive = RFs::GetSystemDrive();
       
   387 	TDriveName sysDriveName (sysDrive.Name());
       
   388 	filename.Insert(0,sysDriveName);
       
   389 	filename.Insert(2,_L("\\"));
       
   390 		
       
   391 	RFs fs;
       
   392 	CleanupClosePushL(fs);
       
   393 	User::LeaveIfError(fs.Connect());
       
   394 	fs.Delete(filename);
       
   395 	CleanupStack::PopAndDestroy(); // fs
       
   396 	}
       
   397 
       
   398 TVerdict CTRegIdentity::doTestStepPostambleL()
       
   399 /**
       
   400   @return - TVerdict code
       
   401   Override of base class virtual
       
   402  */
       
   403 	{
       
   404 	//Call the parent postamble, releasing the file handle, etc
       
   405 	CTStepActSch::doTestStepPostambleL();
       
   406 	return TestStepResult();
       
   407 	}