authenticationservices/authenticationserver/test/tAuthSvr/src/step_resultavailability.cpp
changeset 29 ece3df019add
equal deleted inserted replaced
19:cd501b96611d 29:ece3df019add
       
     1 /*
       
     2 * Copyright (c) 2008-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 #include "tAuthSvrStep.h"
       
    20 #include <s32file.h>
       
    21 using namespace AuthServer;
       
    22 class CAuthActive3 : public CActive
       
    23     {
       
    24     public:
       
    25     CAuthActive3(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 CRegActive2 : public CActive
       
   114     {
       
   115     public:
       
   116     CRegActive2(RAuthMgrClient& aClient, CIdentity*& aResult) : 
       
   117          CActive(EPriorityNormal),
       
   118          iFirstTime(true),
       
   119          iClient(aClient), 
       
   120          iResult(aResult),
       
   121          iErr(KErrNone)
       
   122         {
       
   123         CActiveScheduler::Add(this);
       
   124         }
       
   125     void doReg()
       
   126         {
       
   127         SetActive();		
       
   128 		TRequestStatus* status = &iStatus;
       
   129 		User::RequestComplete(status, KErrNone);
       
   130 		iFirstTime = ETrue;
       
   131         }
       
   132         void DoCancel() 
       
   133         {
       
   134         }
       
   135      void RunL() 
       
   136         {
       
   137         iErr = iStatus.Int();
       
   138         if (iFirstTime)
       
   139             {
       
   140 			SetActive();
       
   141             iStatus = KRequestPending;
       
   142             iClient.RegisterIdentityL(iResult, _L("SOMENAME"), iStatus);
       
   143             iFirstTime = false;
       
   144             }
       
   145         else
       
   146             {
       
   147             iErr = iStatus.Int();
       
   148             CActiveScheduler::Stop();
       
   149 			}
       
   150         }
       
   151         
       
   152     TBool iFirstTime;
       
   153     RAuthMgrClient& iClient;
       
   154     CIdentity*& iResult;
       
   155     TInt iErr;
       
   156     };
       
   157 //====================================================================================================     
       
   158 CResultAvailability::~CResultAvailability()
       
   159 /**
       
   160   Destructor
       
   161  */
       
   162 	{}
       
   163 
       
   164 CResultAvailability::CResultAvailability(CTAuthSvrServer& aParent): iParent(aParent)
       
   165 /**
       
   166   Constructor
       
   167  */
       
   168 	{
       
   169 	
       
   170 	SetTestStepName(KTResultAvailability);
       
   171 	}
       
   172 
       
   173 TVerdict CResultAvailability::doTestStepPreambleL()
       
   174 /**
       
   175   @return - TVerdict code
       
   176 
       
   177  */
       
   178 	{
       
   179 
       
   180 	CTStepActSch::doTestStepPreambleL();	
       
   181 	return TestStepResult();
       
   182 	}
       
   183 
       
   184 
       
   185 TVerdict CResultAvailability::doTestStepL()
       
   186 	{
       
   187 		SetTestStepResult(EFail);	
       
   188 		__UHEAP_MARK;		// Check for memory leaks
       
   189 
       
   190 		//-----------------------------------------------------------------------------------------------------	
       
   191 		//Get the Identity published by KUidAuthServerLastAuth property .
       
   192 		
       
   193 		TLastAuth lastAuth;
       
   194 		TPckg<TLastAuth> authPkg(lastAuth);
       
   195 		RProperty::Get(KAuthServerSecureId, KUidAuthServerLastAuth, authPkg);
       
   196 		
       
   197 		//Get the Last Authenticated Id from the authsvrpolicy.ini file
       
   198 		
       
   199 		TPtrC userIdString;
       
   200 		GetStringFromConfig(ConfigSection(),_L("LastAuthId"), userIdString);
       
   201 		
       
   202 		TLex lex = TLex(userIdString);
       
   203 		TUint userId(0);
       
   204 		
       
   205 		lex.Val(userId);
       
   206 		INFO_PRINTF3(_L("The UserId published by KUidAuthServerLastAuth property is %x and the UserId from AuthSvrPolicy.ini is %x"), lastAuth.iId, userId );
       
   207 		if(lastAuth.iId == userId )
       
   208 			{
       
   209 			SetTestStepResult(EPass);	
       
   210 
       
   211 			}
       
   212 
       
   213 		 __UHEAP_MARKEND;
       
   214 		 return TestStepResult();
       
   215 	}
       
   216 TVerdict CResultAvailability::doTestStepPostambleL()
       
   217 /**
       
   218   @return - TVerdict code
       
   219 
       
   220  */
       
   221 	{
       
   222 	
       
   223 	CTStepActSch::doTestStepPostambleL();
       
   224 	return TestStepResult();
       
   225 	}
       
   226 //====================================================================================================
       
   227 CResultChangeNotify::~CResultChangeNotify()
       
   228 /**
       
   229   Destructor
       
   230  */
       
   231 	{}
       
   232 
       
   233 CResultChangeNotify::CResultChangeNotify(CTAuthSvrServer& aParent): iParent(aParent)
       
   234 /**
       
   235   Constructor
       
   236  */
       
   237 	{
       
   238 	
       
   239 	SetTestStepName(KTResultChangeNotify);
       
   240 	}
       
   241 
       
   242 TVerdict CResultChangeNotify::doTestStepPreambleL()
       
   243 /**
       
   244   @return - TVerdict code
       
   245 
       
   246  */
       
   247 	{
       
   248 
       
   249 	CTStepActSch::doTestStepPreambleL();	
       
   250 	return TestStepResult();
       
   251 	}
       
   252 
       
   253 TVerdict CResultChangeNotify::doTestStepL()
       
   254 	{
       
   255 	SetTestStepResult(EPass);	
       
   256 	__UHEAP_MARK;		// Check for memory leaks
       
   257 	SetPinPluginStateL();
       
   258 
       
   259 	//-----------------------------------------------------------------------------------------------------	
       
   260 	InitAuthServerFromFileL();	// Set things like 'iSupportsDefaultData' and 'DefaultPlugin'
       
   261 	
       
   262 	// this method creates the dat file from where the test implementation
       
   263 	// of pin plugin notifier reads user input.
       
   264 	// as the pin plugin fails without this data it has been included 
       
   265 	// in the code as a default step for initializing the pin plugin
       
   266 	// data.
       
   267 	TPinValue aPinValue;
       
   268 	CreatePinPluginInputFileL(EPinPluginTraining,aPinValue);
       
   269 	CActiveScheduler::Install(iActSchd);
       
   270 	
       
   271 	// KUidAuthServerAuthChangeEvent Property retrieves the last authenticated identity
       
   272 	TInt AuthIdChange = 0;
       
   273 	RProperty::Get(KAuthServerSecureId, KUidAuthServerAuthChangeEvent, AuthIdChange);
       
   274 	INFO_PRINTF2(_L("Last Authenticated Identity is %x" ), AuthIdChange );
       
   275 	
       
   276 	//Connect to the AuthServer	
       
   277 	AuthServer::RAuthMgrClient authMgrClient;	
       
   278 	TInt connectVal = authMgrClient.Connect();
       
   279 	if (KErrNotFound == connectVal)
       
   280 		{
       
   281 			//Retry after a delay
       
   282 			TTimeIntervalMicroSeconds32 timeInterval = 2000;	//2 Milliseconds
       
   283 			User::After(timeInterval);
       
   284 			connectVal = authMgrClient.Connect();
       
   285 		}
       
   286 	if (KErrNone != connectVal)
       
   287 		{
       
   288 		ERR_PRINTF2(_L("Unable to start a session or other connection error. Err = %d"), connectVal);
       
   289 		User::LeaveIfError(connectVal);		
       
   290 		}	
       
   291 		
       
   292 	CleanupClosePushL(authMgrClient);
       
   293 
       
   294 	
       
   295 	//Register a new Identity
       
   296 	INFO_PRINTF1(_L("Registering a new Identity"));
       
   297 	CIdentity* identity = 0;
       
   298 	CRegActive2 regactive(authMgrClient, identity);
       
   299 	regactive.doReg();
       
   300 	CActiveScheduler::Start();
       
   301 	
       
   302 	if (0 != identity)
       
   303 	  	{
       
   304 	  		INFO_PRINTF1(_L("Registration Successful"));
       
   305 	  		INFO_PRINTF3(_L("Id = %x , KeyLength = %d\n"), identity->Id(), identity->Key().KeyData().Size());
       
   306 	  	}
       
   307 			
       
   308 	
       
   309 
       
   310 	//Authenticate the new Id.
       
   311 	
       
   312 	TPtrC exprString;	
       
   313 	TInt deAuth;
       
   314 	if (GetStringFromConfig(ConfigSection(), _L("plugin1Id"), exprString) != EFalse) // the tag 'pluginId1' was present
       
   315 		{
       
   316 				 	
       
   317 			TInt freshnessVal = 0;	//Default value
       
   318 			//Retrieve the value of 'freshness' from the ini file, but if it's absent use the default value
       
   319 			 if(GetIntFromConfig(ConfigSection(), _L("freshness"), freshnessVal) == EFalse)
       
   320 				 {
       
   321 				 	freshnessVal = 10;
       
   322 				 }
       
   323 				 		
       
   324 			TBool clientSpecificKeyVal = EFalse;	 	
       
   325 			//Retrieve the value of 'clientSpecificKey' from the ini file, but if it's absent use the default value
       
   326 			if(GetBoolFromConfig(ConfigSection(), _L("clientSpecificKey"), clientSpecificKeyVal) == EFalse)
       
   327 				 {
       
   328 				 	clientSpecificKeyVal = EFalse;
       
   329 				 }	 	
       
   330 				
       
   331 			if(GetIntFromConfig(ConfigSection(), _L("deauth"), deAuth) == EFalse)
       
   332 				{
       
   333 					deAuth = 0;
       
   334 				}
       
   335 			
       
   336 			TBool stop;
       
   337 			if(GetBoolFromConfig(ConfigSection(), _L("stop"), stop ) == EFalse)
       
   338 							{
       
   339 								stop = ETrue;
       
   340 							}
       
   341 			
       
   342 			//Create a CResultPropertyWatch object which subscribes to the KUidAuthServerAuthChangeEvent Property
       
   343 					
       
   344 			CResultPropertyWatch* watch = CResultPropertyWatch::NewLC();
       
   345 					
       
   346 				
       
   347 			//Train the plugin again with this identity.
       
   348 			CAuthExpression* expr = CreateAuthExprLC(exprString);
       
   349 		
       
   350 			CAuthActive3 authactive(authMgrClient, expr, freshnessVal, clientSpecificKeyVal,stop);
       
   351 	
       
   352 			INFO_PRINTF1(_L("Authenticating the new Identity"));
       
   353 			authactive.doAuth();		
       
   354 			CActiveScheduler::Start();
       
   355 		
       
   356 			//Check if Authentication successful.
       
   357 			if(authactive.iIdToStore)
       
   358 				{
       
   359 					INFO_PRINTF2(_L(" Successful Authentication Id = %x"), authactive.iIdToStore);
       
   360 				}
       
   361 			else
       
   362 				{
       
   363 					INFO_PRINTF1(_L(" Authentication Failed ."));
       
   364 				}
       
   365 			
       
   366 			if(watch->iNotified)
       
   367 				{
       
   368 				TInt AuthIdChange;
       
   369 					
       
   370 				//Get the published value
       
   371 				RProperty::Get(KAuthServerSecureId, KUidAuthServerAuthChangeEvent, AuthIdChange);	
       
   372 				INFO_PRINTF2(_L("Successfully received Notification. New User Identity is %x " ), AuthIdChange );
       
   373 				}
       
   374 			else
       
   375 				{
       
   376 				INFO_PRINTF1(_L("No Notification received "));
       
   377 				}
       
   378 			CleanupStack::PopAndDestroy(2,watch);
       
   379 			//Authenticate the same User again to verify that KUidAuthServerAuthChangeEvent 
       
   380 			//does not get published again
       
   381 			
       
   382 			//Create a CResultPropertyWatch object which subscribes to the KUidAuthServerAuthChangeEvent Property
       
   383 			
       
   384 			CResultPropertyWatch* watch2 = CResultPropertyWatch::NewLC();	
       
   385 			
       
   386 			CAuthExpression* expr2 = CreateAuthExprLC(exprString);
       
   387 			CAuthActive3 authactive2(authMgrClient, expr, freshnessVal, clientSpecificKeyVal);
       
   388 			authactive2.doAuth();		
       
   389 			CActiveScheduler::Start();
       
   390 		
       
   391 			//Check if Authentication successful.
       
   392 			if(authactive.iIdToStore)
       
   393 				{
       
   394 					INFO_PRINTF2(_L(" Successful Authentication Id = %x"), authactive.iIdToStore);
       
   395 				}
       
   396 			else
       
   397 				{
       
   398 					INFO_PRINTF1(_L(" Authentication Failed ."));
       
   399 					
       
   400 				}
       
   401 						
       
   402 			if(watch2->iNotified)
       
   403 				{
       
   404 					TInt AuthIdChange;	
       
   405 					//Get the published value
       
   406 					RProperty::Get(KAuthServerSecureId, KUidAuthServerAuthChangeEvent, AuthIdChange);	
       
   407 					INFO_PRINTF2(_L("Successfully received Notification. New User Identity is %x " ), AuthIdChange );
       
   408 					SetTestStepResult(EFail);
       
   409 				}
       
   410 			else
       
   411 				{
       
   412 					INFO_PRINTF1(_L("No Notification received "));
       
   413 				}
       
   414 			CleanupStack::PopAndDestroy(2,watch2);
       
   415 			// If deAuth flag is set ,subscribe to the KUidAuthServerAuthChangeEvent property
       
   416 			// and deauthenticate 
       
   417 			if(deAuth)
       
   418 			{
       
   419 								
       
   420 				INFO_PRINTF1(_L(" Trying to DeAuthenticate "));
       
   421 				//Creating a subscriber active object
       
   422 				CResultPropertyWatch* watch3 = CResultPropertyWatch::NewLC();
       
   423 		
       
   424 				TInt err;
       
   425 				TRAP(err,authactive.doDeAuth());
       
   426 				CActiveScheduler::Start();
       
   427 		
       
   428 				if(err != KErrNone)
       
   429 					{
       
   430 					INFO_PRINTF1(_L(" DeAuthentication Failed "));
       
   431 					SetTestStepResult(EFail);
       
   432 
       
   433 					}
       
   434 				else
       
   435 					{
       
   436 					INFO_PRINTF1(_L(" DeAuthentication Succeeded "));
       
   437 					}
       
   438 				
       
   439 				if(watch3->iNotified)
       
   440 					{
       
   441 					TInt AuthIdChange;	
       
   442 					//Get the published value
       
   443 					RProperty::Get(KAuthServerSecureId, KUidAuthServerAuthChangeEvent, AuthIdChange);	
       
   444 					INFO_PRINTF2(_L("Successfully received Notification. New User Identity is %x " ), AuthIdChange );
       
   445 					}
       
   446 				else
       
   447 					{
       
   448 					INFO_PRINTF1(_L(" No Notification of the change in the Authenticated Identity "));
       
   449 					SetTestStepResult(EFail);
       
   450 					}
       
   451 				CleanupStack::PopAndDestroy(watch3);
       
   452 				INFO_PRINTF1(_L(" Checking the Id published by KUidAuthServerLastAuth upon DeAuthentication"));
       
   453 				TLastAuth lastAuth;
       
   454 				TPckg<TLastAuth> authPkg(lastAuth);
       
   455 				RProperty::Get(KAuthServerSecureId, KUidAuthServerLastAuth, authPkg);
       
   456 		
       
   457 				INFO_PRINTF2(_L("Id published by KUidAuthServerLastAuth = %x"), lastAuth.iId);
       
   458 		
       
   459 				if(lastAuth.iId != 0)
       
   460 					{
       
   461 					SetTestStepResult(EFail);
       
   462 					}
       
   463 			
       
   464 				}
       
   465 			}
       
   466 			
       
   467 		RemovePinPluginFileL();	
       
   468 		delete identity;
       
   469 		CleanupStack::PopAndDestroy(&authMgrClient);
       
   470 		 __UHEAP_MARKEND;
       
   471 		 return TestStepResult();
       
   472 	}
       
   473 
       
   474 	
       
   475 void CResultChangeNotify::SetPinPluginStateL()
       
   476 	{
       
   477 	TPtrC activeStateFromFile;
       
   478 	
       
   479 	_LIT(KPinPlugin,"pinplugin_inactive.txt");
       
   480 	
       
   481 	TFileName filename;
       
   482 	filename.Copy(KPinPlugin); // convert from 8 -> 16 bit descriptor
       
   483 		
       
   484 	TDriveUnit sysDrive = RFs::GetSystemDrive();
       
   485 	TDriveName sysDriveName (sysDrive.Name());
       
   486 	filename.Insert(0,sysDriveName);
       
   487 	filename.Insert(2,_L("\\"));		
       
   488 	
       
   489 	RFileWriteStream stream;
       
   490 	RFs fs;
       
   491 	CleanupClosePushL(fs);
       
   492 	User::LeaveIfError(fs.Connect());
       
   493 	fs.Delete(filename);
       
   494 		
       
   495 	if (GetStringFromConfig(ConfigSection(),_L("Pin200032E5Active"), activeStateFromFile)) 
       
   496 		{
       
   497 		if(activeStateFromFile == _L("false"))
       
   498 			{		
       
   499 			User::LeaveIfError(stream.Create(fs, filename, EFileWrite | EFileShareExclusive));
       
   500 			stream.Close();
       
   501 			}
       
   502 		}
       
   503 	CleanupStack::PopAndDestroy(); // fs
       
   504 	}
       
   505 
       
   506 void CResultChangeNotify::RemovePinPluginFileL()
       
   507 	{
       
   508 	TPtrC activeStateFromFile;
       
   509 	_LIT(KPinPlugin,"pinplugin_inactive.txt");
       
   510 	TFileName filename;
       
   511 	filename.Copy(KPinPlugin); // convert from 8 -> 16 bit descriptor
       
   512 			
       
   513 	TDriveUnit sysDrive = RFs::GetSystemDrive();
       
   514 	TDriveName sysDriveName (sysDrive.Name());
       
   515 	filename.Insert(0,sysDriveName);
       
   516 	filename.Insert(2,_L("\\"));
       
   517 		
       
   518 	RFs fs;
       
   519 	CleanupClosePushL(fs);
       
   520 	User::LeaveIfError(fs.Connect());
       
   521 	fs.Delete(filename);
       
   522 	CleanupStack::PopAndDestroy(); // fs
       
   523 	}
       
   524 
       
   525 
       
   526 TVerdict CResultChangeNotify::doTestStepPostambleL()
       
   527 /**
       
   528   @return - TVerdict code
       
   529 
       
   530  */
       
   531 	{
       
   532 	
       
   533 	CTStepActSch::doTestStepPostambleL();
       
   534 	return TestStepResult();
       
   535 	}
       
   536 
       
   537 //====================================================================================================     
       
   538 
       
   539 
       
   540 CResultPropertyWatch* CResultPropertyWatch::NewLC()
       
   541 {
       
   542 	CResultPropertyWatch* me=new(ELeave) CResultPropertyWatch;
       
   543 	CleanupStack::PushL(me);
       
   544 	me->ConstructL();
       
   545 	return me;
       
   546 }
       
   547 
       
   548 CResultPropertyWatch::CResultPropertyWatch() :CActive(EPriority)
       
   549 	{
       
   550 		iNotified = EFalse;
       
   551 	}
       
   552 
       
   553 
       
   554 void CResultPropertyWatch::ConstructL()
       
   555 {
       
   556 	User::LeaveIfError(iProperty.Attach(KAuthServerSecureId,
       
   557 			 KUidAuthServerAuthChangeEvent));
       
   558 	CActiveScheduler::Add(this);
       
   559 	// Subscribe to the KUidAuthServerAuthChangeEvent property
       
   560 	iProperty.Subscribe(iStatus);
       
   561 	SetActive();
       
   562 	
       
   563 	}
       
   564 CResultPropertyWatch::~CResultPropertyWatch()
       
   565 {
       
   566 	Cancel();
       
   567 	iProperty.Close();
       
   568 
       
   569 }
       
   570 void CResultPropertyWatch::DoCancel()
       
   571 {
       
   572 	iProperty.Cancel();
       
   573 }
       
   574 void CResultPropertyWatch::RunL()
       
   575 {	
       
   576 	TInt AuthIdChange;
       
   577 	
       
   578 	//Get the published value
       
   579 	iProperty.Get(KAuthServerSecureId, KUidAuthServerAuthChangeEvent, AuthIdChange);
       
   580 	iNotified = ETrue;
       
   581 	RDebug::Print(_L("Received Notification. New User Identity is %x " ), AuthIdChange );
       
   582 	CActiveScheduler::Stop();
       
   583 		
       
   584 }
       
   585 
       
   586