authenticationservices/authenticationserver/test/tAuthSvr/src/step_resetidentity.cpp
changeset 29 ece3df019add
equal deleted inserted replaced
19:cd501b96611d 29:ece3df019add
       
     1 /*
       
     2 * Copyright (c) 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 <scs/cleanuputils.h>
       
    22 #include "authserver/authclient.h"
       
    23 #include "authserver/authmgrclient.h"
       
    24 
       
    25 using namespace AuthServer;
       
    26 
       
    27 _LIT(KResetType, "ResetType");
       
    28 _LIT(KResetTypeResetAll, "ResetAll");
       
    29 _LIT(KResetTypeResetByType, "ResetByType");
       
    30 _LIT(KResetTypeResetByList, "ResetByList");
       
    31 _LIT(KResetIdentityString, "IdentityString");
       
    32 _LIT(KResetRegDataSingle, "RegistrationData");
       
    33 _LIT(KResetPluginCount, "PluginCount");
       
    34 _LIT(KResetPluginId, "PluginId%d");
       
    35 _LIT(KResetRegData, "RegistrationData%d");
       
    36 _LIT(KResetPluginType, "PluginType");
       
    37 _LIT(KResetPluginTypeKnowledge, "EAuthKnowledge");
       
    38 _LIT(KResetPluginTypeToken, "EAuthToken");
       
    39 _LIT(KResetPluginTypeBiometric, "EAuthBiometric");
       
    40 
       
    41 
       
    42 CTResetIdentity::CTResetIdentity(CTAuthSvrServer& aParent): iParent(aParent)
       
    43 	{
       
    44 	// Call base class method to set up the human readable name for logging
       
    45 	SetTestStepName(KTResetIdentity);
       
    46 	}
       
    47 
       
    48 CTResetIdentity::~CTResetIdentity()
       
    49 	{}
       
    50 
       
    51 TVerdict CTResetIdentity::doTestStepPreambleL()
       
    52 	{
       
    53 	// Call the parent class preamble, setting up the file server, etc
       
    54 	CTStepActSch::doTestStepPreambleL();
       
    55 	
       
    56 	return TestStepResult();
       
    57 	}
       
    58 
       
    59 TVerdict CTResetIdentity::doTestStepL()
       
    60 	{	
       
    61 	INFO_PRINTF1(_L("In CTResetIdentity doTestStepL"));
       
    62 
       
    63 	// If any test step leaves or panics, the test step thread won't exit, further calls are made.
       
    64 	// but the html log is updated, so that subsequent failures could be traced here if necessary. 
       
    65 	if (TestStepResult() != EPass)
       
    66 		{
       
    67 		ERR_PRINTF1(_L("There was an error in a previous test step"));
       
    68 		//return TestStepResult();
       
    69 		}
       
    70 
       
    71 	SetTestStepResult(EPass);
       
    72 
       
    73 __UHEAP_MARK;		// Check for memory leaks
       
    74 
       
    75 	//-----------------------------------------------------------------------------------------------------
       
    76 	InitAuthServerFromFileL();	// Set things like 'iSupportsDefaultData' and 'DefaultPlugin'
       
    77 	CActiveScheduler::Install(iActSchd);
       
    78 
       
    79 	TRAPD(res2, doResetIdentityL());
       
    80 	if(KErrNone != res2)
       
    81 		{
       
    82 		ERR_PRINTF2(_L("doResetIdentityL() performed a Leave with code %d"), res2 );
       
    83 		//SetTestStepResult(EPass);
       
    84 		SetTestStepError(res2);
       
    85 		}
       
    86 	
       
    87 	//Garbage collect the last previously destroyed implementation 
       
    88 	// and close the REComSession if no longer in use
       
    89 	REComSession::FinalClose(); 
       
    90 __UHEAP_MARKEND;
       
    91 	return TestStepResult();
       
    92 	}
       
    93 
       
    94 TVerdict CTResetIdentity::doTestStepPostambleL()
       
    95 	{
       
    96 	// Call the parent postamble, releasing the file handle, etc
       
    97 	CTStepActSch::doTestStepPostambleL();
       
    98 	return TestStepResult();
       
    99 	}
       
   100 
       
   101 void CTResetIdentity::doResetIdentityL ()
       
   102 	{
       
   103 	INFO_PRINTF1(_L("In CTResetIdentity doResetIdentityL"));
       
   104 	// Read the identity string whose id which needs to be reset
       
   105 	TPtrC identityString;
       
   106 	if (!GetStringFromConfig(ConfigSection(), KResetIdentityString, identityString))
       
   107 		{
       
   108 		INFO_PRINTF1(_L("Identity string not found in the ini file"));
       
   109 		User::Leave(KErrNotFound);
       
   110 		}
       
   111 
       
   112 	TIdentityId identityId = KUnknownIdentity;
       
   113 	if (identityString != KNullDesC)
       
   114 		{
       
   115 		INFO_PRINTF1(_L("In doResetIdentityL Here1"));
       
   116 		// From the AuthDb get the id corresponding to the string
       
   117 		// Wait for authserver to release the db
       
   118 		WaitForServerToReleaseDb();
       
   119 		WaitForServerToReleaseDb();	// In debug mode AuthServer takes some time to terminate
       
   120 		INFO_PRINTF1(_L("In doResetIdentityL Here2"));
       
   121 		CAuthDb2* db = CAuthDb2::NewLC(iFs);
       
   122 		INFO_PRINTF1(_L("In doResetIdentityL Here3"));
       
   123 		RIdAndStringArray idList;
       
   124 		CleanupResetAndDestroyPushL(idList);
       
   125 		TRAPD(err, db->IdentitiesWithDescriptionL(idList));
       
   126 		INFO_PRINTF1(_L("In doResetIdentityL Here4"));
       
   127 		if (err != KErrNone)
       
   128 			{
       
   129 			INFO_PRINTF2(_L("CAuthDb::IdentitiesWithDescriptionL() failed with %d"), err);
       
   130 			User::Leave(err);
       
   131 			}
       
   132 		INFO_PRINTF2(_L("CAuthDb::IdentitiesWithDescriptionL() found %d identities"), idList.Count());
       
   133 		for (TInt index = 0; index < idList.Count(); ++index)
       
   134 			{
       
   135 			INFO_PRINTF2(_L("CAuthDb::IdentitiesWithDescriptionL() found identities %S"), idList[index]->String());
       
   136 			if (identityString.Compare(*idList[index]->String()) == 0)
       
   137 				{
       
   138 				identityId = idList[index]->Id();
       
   139 				break;
       
   140 				}
       
   141 			}
       
   142 		CleanupStack::PopAndDestroy(2, db); // idList
       
   143 		}
       
   144 	
       
   145 	// Get the reset type form the ini file and relevant details to do the reset
       
   146 	TPtrC resetType;
       
   147 	if (!GetStringFromConfig(ConfigSection(), KResetType, resetType))
       
   148 		{
       
   149 		INFO_PRINTF2(_L("%s was not specified in the ini file"), KResetType);
       
   150 		return;
       
   151 		}
       
   152 
       
   153 	AuthServer::RAuthMgrClient authMgrClient;
       
   154 	CleanupClosePushL(authMgrClient);
       
   155 	TInt err = authMgrClient.Connect();
       
   156 	if (err == KErrNotFound)
       
   157 		{
       
   158 		// Retry after a delay
       
   159 		TTimeIntervalMicroSeconds32 timeInterval = 2000;	// 2 Milliseconds
       
   160 		User::After(timeInterval);
       
   161 		err = authMgrClient.Connect();
       
   162 		}
       
   163 	if (err != KErrNone)
       
   164 		{
       
   165 		ERR_PRINTF2(_L("Unable to start a session or other connection error. Err = %d"), err);
       
   166 		User::Leave(err);
       
   167 		}
       
   168 
       
   169 	if (resetType == KResetTypeResetAll)
       
   170 		{
       
   171 		INFO_PRINTF1(_L("In CTResetIdentity doResetIdentityL KResetTypeResetAll"));
       
   172 		// Read the registration data
       
   173 		TPtrC regData;
       
   174 		if (!GetStringFromConfig(ConfigSection(), KResetRegDataSingle, regData))
       
   175 			{
       
   176 			INFO_PRINTF1(_L("Registration data was not specified in the ini file"));
       
   177 			User::Leave(KErrNotFound);
       
   178 			}
       
   179 		authMgrClient.ResetIdentityL(identityId, regData);
       
   180 		}
       
   181 	else if (resetType == KResetTypeResetByType)
       
   182 		{
       
   183 		INFO_PRINTF1(_L("In CTResetIdentity doResetIdentityL KResetTypeResetByType"));
       
   184 		// Get the plugin type
       
   185 		TPtrC pluginType;
       
   186 		if (!GetStringFromConfig(ConfigSection(), KResetPluginType, pluginType))
       
   187 			{
       
   188 			INFO_PRINTF1(_L("Plugin type was not specified in the ini file"));
       
   189 			User::Leave(KErrNotFound);
       
   190 			}
       
   191 		TAuthPluginType type;
       
   192 		if (pluginType == KResetPluginTypeKnowledge)
       
   193 			{
       
   194 			type = EAuthKnowledge;
       
   195 			}
       
   196 		else if (pluginType == KResetPluginTypeToken)
       
   197 			{
       
   198 			type = EAuthToken;
       
   199 			}
       
   200 		else if (pluginType == KResetPluginTypeBiometric)
       
   201 			{
       
   202 			type = EAuthBiometric;
       
   203 			}
       
   204 		else
       
   205 			{
       
   206 			INFO_PRINTF1(_L("Invalid plugin type was specified in the ini file"));
       
   207 			CleanupStack::PopAndDestroy(&authMgrClient);
       
   208 			return;
       
   209 			}
       
   210 		// Read the registration data
       
   211 		TPtrC regData;
       
   212 		if (!GetStringFromConfig(ConfigSection(), KResetRegDataSingle, regData))
       
   213 			{
       
   214 			INFO_PRINTF1(_L("Registration data was not specified in the ini file"));
       
   215 			User::Leave(KErrNotFound);
       
   216 			}
       
   217 		authMgrClient.ResetIdentityL(identityId, type, regData);
       
   218 		}
       
   219 	else if (resetType == KResetTypeResetByList)
       
   220 		{
       
   221 		// Read the reset information
       
   222 		INFO_PRINTF1(_L("In CTResetIdentity doResetIdentityL KResetTypeResetByList"));
       
   223 		// Get the plugin details
       
   224 		TInt pluginCount;
       
   225 		if (!GetIntFromConfig(ConfigSection(), KResetPluginCount, pluginCount))
       
   226 			{
       
   227 			INFO_PRINTF1(_L("PluginCount missing/invalid in the ini file"));
       
   228 			User::Leave(KErrNotFound);
       
   229 			}
       
   230 		RArray<TPluginId> pluginList;
       
   231 		CleanupClosePushL(pluginList);
       
   232 		RPointerArray<const HBufC> regDataList;
       
   233 		CleanupResetAndDestroyPushL(regDataList);
       
   234 		if (pluginCount > 0)
       
   235 			{
       
   236 			for (TInt index = 0; index < pluginCount; ++index)
       
   237 				{
       
   238 				HBufC* fmt = HBufC::NewLC(KResetPluginId().Length() + 10);
       
   239 				TPtr fmtPtr(fmt->Des());
       
   240 				fmtPtr.Format(KResetPluginId, index);
       
   241 				TInt pluginId;
       
   242 				if (!GetHexFromConfig(ConfigSection(), *fmt, pluginId))
       
   243 					{
       
   244 					INFO_PRINTF2(_L("%S missing in the ini file"), fmt);
       
   245 					User::Leave(KErrNotFound);
       
   246 					}
       
   247 				pluginList.AppendL(static_cast<TPluginId>(pluginId));
       
   248 				CleanupStack::PopAndDestroy(fmt);
       
   249 				fmt = HBufC::NewLC(KResetRegData().Length() + 10);
       
   250 				fmtPtr.Set(fmt->Des());
       
   251 				fmtPtr.Format(KResetRegData, index);
       
   252 				TPtrC regData;
       
   253 				if (!GetStringFromConfig(ConfigSection(), *fmt, regData))
       
   254 					{
       
   255 					INFO_PRINTF2(_L("%S missing in the ini file"), fmt);
       
   256 					User::Leave(KErrNotFound);
       
   257 					}
       
   258 				HBufC* reg = regData.AllocLC();
       
   259 				regDataList.AppendL(reg);	// Takes ownership
       
   260 				CleanupStack::Pop(reg);
       
   261 				CleanupStack::PopAndDestroy(fmt);
       
   262 				}
       
   263 			}
       
   264 		else
       
   265 			{
       
   266 			// If plugin count is negative, simulate a condition where the pluginList count != regDataList count
       
   267 			pluginList.AppendL(0);
       
   268 			}
       
   269 		authMgrClient.ResetIdentityL(identityId, pluginList, regDataList);
       
   270 		CleanupStack::PopAndDestroy(2, &pluginList); // regDataList
       
   271 		}
       
   272 	else
       
   273 		{
       
   274 		INFO_PRINTF1(_L("Invalid reset type was specified in the ini file."));
       
   275 		User::Leave(KErrNotFound);
       
   276 		}
       
   277 	
       
   278 	CleanupStack::PopAndDestroy(&authMgrClient);
       
   279 	}