authenticationservices/authenticationserver/test/tauthdb/step_persist2.cpp
changeset 19 ece3df019add
child 56 c11c717470d0
equal deleted inserted replaced
17:cd501b96611d 19:ece3df019add
       
     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 #include <s32mem.h>
       
    20 #include "tauthdbstep.h"
       
    21 #include <f32file.h>
       
    22 
       
    23 using namespace AuthServer;
       
    24 
       
    25 const TIdentityId KIdentity1Id = 'I1ID';
       
    26 const TIdentityId KIdentity2Id = 'I2ID';
       
    27 const TIdentityId KIdentity3Id = 'I3ID';
       
    28 _LIT(KIdentity1Desc, "identity-1-desc");
       
    29 _LIT(KIdentity1DescB, "identity-1-descB");
       
    30 _LIT(KIdentity2Desc, "identity-2-desc");
       
    31 _LIT(KIdentity3Desc, "identity-3-desc");
       
    32 
       
    33 const TPluginId KPlugin1Id = 'PID1';
       
    34 const TPluginId KPlugin2Id = 'PID2';
       
    35 const TPluginId KPlugin3Id = 'PID3';
       
    36 const TPluginId KPlugin4Id = 'PID4';
       
    37 const TPluginId KPluginAId = 'PIDA';
       
    38 const TPluginId KPluginBId = 'PIDB';
       
    39 
       
    40 
       
    41 CTStepPersist2::CTStepPersist2()
       
    42 	{
       
    43 	SetTestStepName(KTStepCreateTestDb);
       
    44 	}
       
    45 
       
    46 
       
    47 TVerdict CTStepPersist2::doTestStepL()
       
    48 	{
       
    49 	__UHEAP_MARK;
       
    50 	
       
    51 	RemoveExistingDbL();
       
    52 		
       
    53 	TestOpenCloseL();
       
    54 	TestIdentitiesL();
       
    55 	TestPrefsL();
       
    56 	TestTrainedPluginsL();
       
    57 	TestTrainingStatusL();
       
    58 	TestRepairL();
       
    59 	TestCompressL();
       
    60 	
       
    61 	__UHEAP_MARKEND;
       
    62 	return EPass;
       
    63 	}
       
    64 
       
    65 
       
    66 void CTStepPersist2::TestOpenCloseL()
       
    67 /**
       
    68 	Test simply opening and closing a database,
       
    69 	without using any other operations.
       
    70  */
       
    71 	{
       
    72 	CAuthDb2* db = CAuthDb2::NewL(iFs);
       
    73 	delete db;
       
    74 	}
       
    75 
       
    76 
       
    77 void CTStepPersist2::TestIdentitiesL()
       
    78 	{
       
    79 	TInt r;
       
    80 	
       
    81 	__UHEAP_MARK;
       
    82 	RemoveExistingDbL();
       
    83 	CAuthDb2* db = CAuthDb2::NewLC(iFs);
       
    84 	
       
    85 	RArray<TIdentityId> ids;
       
    86 	
       
    87 	// get id list before any ids defined
       
    88 	ids.AppendL('SYMB');	// garbage; ensure removed
       
    89 	db->IdentitiesL(ids);
       
    90 	TESTL(ids.Count() == 0);
       
    91 	
       
    92 	// add an identity
       
    93 	db->AddIdentityL(KIdentity1Id, KIdentity1Desc);
       
    94 	db->IdentitiesL(ids);
       
    95 	TESTL(ids.Count() == 1);
       
    96 	TESTL(ids[0] == KIdentity1Id);
       
    97 	
       
    98 	// retrieve the identity's description
       
    99 	TestDescriptionL(db, KIdentity1Id, KIdentity1Desc);
       
   100 	
       
   101 	// change the identity's description
       
   102 	db->SetDescriptionL(KIdentity1Id, KIdentity1DescB);
       
   103 	TestDescriptionL(db, KIdentity1Id, KIdentity1DescB);
       
   104 
       
   105 	// change the identity's description to a string which is
       
   106 	// too long; ensure fails.
       
   107 	TBuf<KMaxDescLen + 1> dbuf;
       
   108 	dbuf.Fill('a', KMaxDescLen - 1);		// limit - 1
       
   109 	db->SetDescriptionL(KIdentity1Id, dbuf);
       
   110 	TestDescriptionL(db, KIdentity1Id, dbuf);
       
   111 	
       
   112 	dbuf.Fill('a', KMaxDescLen);			// limit
       
   113 	db->SetDescriptionL(KIdentity1Id, dbuf);
       
   114 	TestDescriptionL(db, KIdentity1Id, dbuf);
       
   115 	
       
   116 	db->SetDescriptionL(KIdentity1Id, KIdentity1DescB);
       
   117 	TestDescriptionL(db, KIdentity1Id, KIdentity1DescB);
       
   118 	dbuf.Fill('a', KMaxDescLen + 1);		// limit + 1
       
   119 	TRAP(r, db->SetDescriptionL(KIdentity1Id, dbuf));
       
   120 	TESTL(r == KErrAuthServDescTooLong);
       
   121 	TestDescriptionL(db, KIdentity1Id, KIdentity1DescB);	// keep old desc
       
   122 	
       
   123 	// add a second identity
       
   124 	db->AddIdentityL(KIdentity2Id, KIdentity2Desc);
       
   125 	db->IdentitiesL(ids);
       
   126 	TESTL(ids.Count() == 2);
       
   127 	TESTL(	(ids[0] == KIdentity1Id && ids[1] == KIdentity2Id)
       
   128 		||	(ids[0] == KIdentity2Id && ids[1] == KIdentity1Id) );
       
   129 	TestDescriptionL(db, KIdentity1Id, KIdentity1DescB);
       
   130 	TestDescriptionL(db, KIdentity2Id, KIdentity2Desc);
       
   131 	
       
   132 	// add an identity which already exists
       
   133 	TRAP(r, db->AddIdentityL(KIdentity2Id, KIdentity2Desc));
       
   134 	TESTL(r == KErrAuthServIdentityAlreadyExists);
       
   135 	
       
   136 	// retrieve description for an unregistered identity
       
   137 	TRAP(r, db->DescriptionL(KIdentity3Id));
       
   138 	TESTL(r == KErrAuthServIdentityNotFound);
       
   139 	
       
   140 	// set description for an unregistered identity
       
   141 	TRAP(r, db->SetDescriptionL(KIdentity3Id, KIdentity3Desc));
       
   142 	TESTL(r == KErrAuthServIdentityNotFound);
       
   143 	
       
   144 	// remove an unregistered identity
       
   145 	TRAP(r, db->RemoveIdentityL(KIdentity3Id));
       
   146 	TESTL(r == KErrAuthServIdentityNotFound);
       
   147 	
       
   148 	// remove an identity
       
   149 	db->RemoveIdentityL(KIdentity1Id);
       
   150 	db->IdentitiesL(ids);
       
   151 	TESTL(ids.Count() == 1);
       
   152 	TESTL(ids[0] == KIdentity2Id);
       
   153 	TestDescriptionL(db, KIdentity2Id, KIdentity2Desc);
       
   154 	
       
   155 	// remove the last identity
       
   156 	db->RemoveIdentityL(KIdentity2Id);
       
   157 	db->IdentitiesL(ids);
       
   158 	TESTL(ids.Count() == 0);
       
   159 	
       
   160 	// create an identity with a description to a string which is
       
   161 	// too long; ensure fails.
       
   162 	dbuf.Fill('a', KMaxDescLen - 1);		// limit - 1
       
   163 	db->AddIdentityL(KIdentity3Id, dbuf);
       
   164 	TestDescriptionL(db, KIdentity3Id, dbuf);
       
   165 	db->RemoveIdentityL(KIdentity3Id);
       
   166 	
       
   167 	dbuf.Fill('a', KMaxDescLen);			// limit
       
   168 	db->AddIdentityL(KIdentity3Id, dbuf);
       
   169 	TestDescriptionL(db, KIdentity3Id, dbuf);
       
   170 	db->RemoveIdentityL(KIdentity3Id);
       
   171 	
       
   172 	dbuf.Fill('a', KMaxDescLen + 1);		// limit + 1
       
   173 	TRAP(r, db->AddIdentityL(KIdentity3Id, dbuf));
       
   174 	TESTL(r == KErrAuthServDescTooLong);
       
   175 	db->IdentitiesL(ids);							// no identity created
       
   176 	TESTL(ids.Count() == 0);
       
   177 	
       
   178 	CleanupStack::PopAndDestroy(db);
       
   179 	__UHEAP_MARKEND;
       
   180 	}
       
   181 
       
   182 void CTStepPersist2::TestDescriptionL(
       
   183 	CAuthDb2* aDb, TIdentityId aIdentityId, const TDesC& aExpDesc)
       
   184 /**
       
   185 	Helper function for TestIdentitiesL.  Ensures the
       
   186 	supplied identity has the supplied description.
       
   187 	
       
   188 	@param	aDb				Database which contains the identity.
       
   189 	@param	aIdentity		Identity whose description should be
       
   190 							retrieved.
       
   191 	@param	aDesc			Expected description.
       
   192  */
       
   193 	{
       
   194 	HBufC* desc = aDb->DescriptionL(aIdentityId);
       
   195 	CleanupStack::PushL(desc);
       
   196 	TESTL(*desc == aExpDesc);
       
   197 	CleanupStack::PopAndDestroy(desc);
       
   198 	}
       
   199 
       
   200 
       
   201 void CTStepPersist2::TestPrefsL()
       
   202 /**
       
   203 	Tests adding, modifying, and removing
       
   204 	preferences from the database.
       
   205  */
       
   206 	{
       
   207 	TPluginId prefId;	
       
   208 	__UHEAP_MARK;
       
   209 	
       
   210 	RemoveExistingDbL();
       
   211 	CAuthDb2* db = CAuthDb2::NewLC(iFs);
       
   212 
       
   213 	// get a preferred plugin for a type with no preferred plugin
       
   214 	prefId = db->PreferredPluginL(EAuthBiometric);
       
   215 	TESTL(prefId == KUnknownPluginId);
       
   216 
       
   217 	// set a type's preferred plugin
       
   218 	db->SetPreferredPluginL(EAuthBiometric, KPlugin1Id);
       
   219 	prefId = db->PreferredPluginL(EAuthBiometric);
       
   220 	TESTL(prefId == KPlugin1Id);
       
   221 	
       
   222 	// change a type's preferred plugin
       
   223 	db->SetPreferredPluginL(EAuthBiometric, KPlugin2Id);
       
   224 	prefId = db->PreferredPluginL(EAuthBiometric);
       
   225 	TESTL(prefId == KPlugin2Id);
       
   226 	
       
   227 	// clear a type's preferred plugin
       
   228 	db->ClearPreferredPluginL(EAuthBiometric);
       
   229 	prefId = db->PreferredPluginL(EAuthBiometric);
       
   230 	TESTL(prefId == KUnknownPluginId);
       
   231 	
       
   232 	// set preferred plugin for more than one type
       
   233 	db->SetPreferredPluginL(EAuthBiometric, KPlugin3Id);
       
   234 	db->SetPreferredPluginL(EAuthKnowledge, KPlugin4Id);
       
   235 	prefId = db->PreferredPluginL(EAuthBiometric);
       
   236 	TESTL(prefId == KPlugin3Id);
       
   237 	prefId = db->PreferredPluginL(EAuthKnowledge);
       
   238 	TESTL(prefId == KPlugin4Id);
       
   239 	
       
   240 	CleanupStack::PopAndDestroy(db);
       
   241 	
       
   242 	__UHEAP_MARKEND;
       
   243 	}
       
   244 
       
   245 
       
   246 void CTStepPersist2::TestTrainedPluginsL()
       
   247 /**
       
   248 	Test adding and removing trained plugins.
       
   249  */
       
   250 	{
       
   251 	__UHEAP_MARK;
       
   252 	
       
   253 	TInt r;
       
   254 	RemoveExistingDbL();
       
   255 	CAuthDb2* db = CAuthDb2::NewLC(iFs);
       
   256 
       
   257 	const CTransientKeyInfo& tkiA = *iId1Keys[0];
       
   258 	const CTransientKeyInfo& tkiB = *iId1Keys[1];
       
   259 	const CTransientKeyInfo& tkiC = *iId1Keys[2];
       
   260 
       
   261 	// set key info for an unregistered identity
       
   262 	TRAP(r, db->SetTrainedPluginL(KIdentity1Id, KPluginAId, tkiA));
       
   263 	TESTL(r == KErrAuthServIdentityNotFound);
       
   264 
       
   265 	// set key info for a registered identity
       
   266 	db->AddIdentityL(KIdentity1Id, KIdentity1Desc);
       
   267 	db->SetTrainedPluginL(KIdentity1Id, KPluginAId, tkiA);
       
   268 	
       
   269 	// get key info for a trained identity
       
   270 	TestKeyPresentL(db, KIdentity1Id, KPluginAId, tkiA);
       
   271 
       
   272 	// change key info for a trained identity
       
   273 	db->SetTrainedPluginL(KIdentity1Id, KPluginAId, tkiB);
       
   274 	TestKeyPresentL(db, KIdentity1Id, KPluginAId, tkiB);
       
   275 
       
   276 	// ---- bad KeyInfoL args ----
       
   277 	
       
   278 	// get key info for registered identity but untrained plugin
       
   279 	TRAP(r, db->KeyInfoL(KIdentity1Id, KPluginBId))
       
   280 	TESTL(r == KErrAuthServTrainingNotFound);
       
   281 
       
   282 	// get key info for unregistered identity but trained plugin	
       
   283 	TRAP(r, db->KeyInfoL(KIdentity3Id, KPluginAId))
       
   284 	TESTL(r == KErrAuthServTrainingNotFound);
       
   285 	
       
   286 	// get key info for unregistered identity and untrained plugin
       
   287 	TRAP(r, db->KeyInfoL(KIdentity3Id, KPluginBId));
       
   288 	TESTL(r == KErrAuthServTrainingNotFound);
       
   289 	
       
   290 	// ---- bad RemoveTrainedPluginL args ----
       
   291 	
       
   292 	// clear key info for registered identity but untrained plugin
       
   293 	TRAP(r, db->KeyInfoL(KIdentity1Id, KPluginBId))
       
   294 	TESTL(r == KErrAuthServTrainingNotFound);
       
   295 
       
   296 	// clear key info for unregistered identity but trained plugin	
       
   297 	TRAP(r, db->RemoveTrainedPluginL(KIdentity3Id, KPluginAId))
       
   298 	TESTL(r == KErrAuthServTrainingNotFound);
       
   299 	
       
   300 	// clear key info for unregistered identity and untrained plugin
       
   301 	TRAP(r, db->RemoveTrainedPluginL(KIdentity3Id, KPluginBId));
       
   302 	TESTL(r == KErrAuthServTrainingNotFound);
       
   303 	
       
   304 	// -----
       
   305 
       
   306 	// clear a present trained plugin
       
   307 	db->RemoveTrainedPluginL(KIdentity1Id, KPluginAId);
       
   308 	TRAP(r, db->KeyInfoL(KIdentity3Id, KPluginAId))
       
   309 	TESTL(r == KErrAuthServTrainingNotFound);
       
   310 	
       
   311 	// register multiple trained plugins for a single identity	
       
   312 	db->SetTrainedPluginL(KIdentity1Id, KPluginAId, tkiA);
       
   313 	db->SetTrainedPluginL(KIdentity1Id, KPluginBId, tkiB);
       
   314 	TestKeyPresentL(db, KIdentity1Id, KPluginAId, tkiA);
       
   315 	TestKeyPresentL(db, KIdentity1Id, KPluginAId, tkiB);
       
   316 
       
   317 	// train a single plugin for multiple identities
       
   318 	db->AddIdentityL(KIdentity2Id, KIdentity2Desc);
       
   319 	db->SetTrainedPluginL(KIdentity2Id, KPluginAId, tkiC);
       
   320 	TestKeyPresentL(db, KIdentity1Id, KPluginAId, tkiA);
       
   321 	TestKeyPresentL(db, KIdentity1Id, KPluginBId, tkiB);
       
   322 	TestKeyPresentL(db, KIdentity2Id, KPluginAId, tkiC);
       
   323 	
       
   324 	// ---- atomic identity and key addition ----
       
   325 	
       
   326 	// add identity with trained plugin
       
   327 	db->AddIdentityWithTrainedPluginL(KIdentity3Id, KIdentity3Desc, tkiA);
       
   328 	TestKeyPresentL(db, KIdentity3Id, tkiA.PluginId(), tkiA);
       
   329 	
       
   330 	// fail to add identity with trained plugin - id already exists
       
   331 	TRAP(r, db->AddIdentityWithTrainedPluginL(KIdentity3Id, KIdentity3Desc, tkiA));
       
   332 	TESTL(r == KErrAuthServIdentityAlreadyExists);
       
   333 	
       
   334 	CleanupStack::PopAndDestroy(db);
       
   335 	
       
   336 	__UHEAP_MARKEND;
       
   337 	}
       
   338 
       
   339 
       
   340 void CTStepPersist2::TestKeyPresentL(
       
   341 	CAuthDb2* aAuthDb, TIdentityId aIdentityId, TPluginId aPluginId,
       
   342 	const CTransientKeyInfo& aTarget)
       
   343 /**
       
   344 	Helper function for TestTrainedPluginsL.
       
   345 	
       
   346 	Test the described transient key exists in the database,
       
   347 	and that is is equal to the supplied key.  Leaves if not
       
   348 	the case.
       
   349 
       
   350 	@param	aAuthDb			Authorisation database to retrieve
       
   351 							transient key info from.
       
   352 	@param	aIdentityId		Identity which should be trained for
       
   353 							the supplied plugin.
       
   354 	@param	aPluginId		Plugin for which the supplied identity
       
   355 							should be trained.
       
   356 	@param	aTarget			The retrieved transient key info should
       
   357 							be equal to this.
       
   358  */
       
   359 	{
       
   360 	CTransientKeyInfo* tkiActual = aAuthDb->KeyInfoL(aIdentityId, aPluginId);
       
   361 	CleanupStack::PushL(tkiActual);
       
   362 
       
   363 	// ensure key infos have same externalized size
       
   364 	TSizeStream ssTarget;
       
   365 	RWriteStream wsTarget(&ssTarget);
       
   366 	aTarget.ExternalizeL(wsTarget);
       
   367 	TInt targetSize = ssTarget.Size();
       
   368 
       
   369 	TSizeStream ssActual;
       
   370 	RWriteStream wsActual(&ssActual);
       
   371 	tkiActual->ExternalizeL(wsActual);
       
   372 	TESTL(targetSize == ssActual.Size());
       
   373 
       
   374 	HBufC8* targetBuf = HBufC8::NewLC(targetSize);
       
   375 	TPtr8 targetBufDes = targetBuf->Des();
       
   376 	RDesWriteStream dwsTarget(targetBufDes);
       
   377 	aTarget.ExternalizeL(dwsTarget);
       
   378 
       
   379 	HBufC8* actualBuf = HBufC8::NewLC(targetSize);
       
   380 	TPtr8 actualBufDes = actualBuf->Des();
       
   381 	RDesWriteStream dwsActual(actualBufDes);
       
   382 	tkiActual->ExternalizeL(dwsActual);
       
   383 
       
   384 	TESTL(targetBufDes == actualBufDes);
       
   385 
       
   386 	CleanupStack::PopAndDestroy(3, tkiActual);
       
   387 	}
       
   388 
       
   389 
       
   390 void CTStepPersist2::TestTrainingStatusL()
       
   391 /**
       
   392 	Test the plugins' training statuses accurately
       
   393 	reflect the identities which are registered
       
   394 	with them.
       
   395  */
       
   396 	{
       
   397 	__UHEAP_MARK;
       
   398 
       
   399 	RemoveExistingDbL();
       
   400 	
       
   401 	CAuthDb2* db = CAuthDb2::NewLC(iFs);
       
   402 
       
   403 	// if there are no users then a plugin should be marked
       
   404 	// as untrained, even though its training count is equal
       
   405 	// to the number of registered identities.
       
   406 	TESTL(db->PluginStatusL(KPluginAId) == EAuthUntrained);
       
   407 
       
   408 	// untrained when no users trained
       
   409 	db->AddIdentityL(KIdentity1Id, KIdentity1Desc);
       
   410 	TESTL(db->PluginStatusL(KPluginAId) == EAuthUntrained);
       
   411 	db->AddIdentityL(KIdentity2Id, KIdentity2Desc);
       
   412 	TESTL(db->PluginStatusL(KPluginAId) == EAuthUntrained);
       
   413 
       
   414 	// trained when some, but not all, users trained
       
   415 	const CTransientKeyInfo& tkiA = *iId1Keys[0];
       
   416 	db->SetTrainedPluginL(KIdentity1Id, KPluginAId, tkiA);
       
   417 	TESTL(db->PluginStatusL(KPluginAId) == EAuthTrained);
       
   418 	
       
   419 	// fully trained when all users trained
       
   420 	db->SetTrainedPluginL(KIdentity2Id, KPluginAId, tkiA);
       
   421 	TESTL(db->PluginStatusL(KPluginAId) == EAuthFullyTrained);
       
   422 	
       
   423 	// back to trained when identity untrained
       
   424 	db->RemoveTrainedPluginL(KIdentity1Id, KPluginAId);
       
   425 	TESTL(db->PluginStatusL(KPluginAId) == EAuthTrained);
       
   426 	
       
   427 	// back to untrained when last training removed
       
   428 	db->RemoveTrainedPluginL(KIdentity2Id, KPluginAId);
       
   429 	TESTL(db->PluginStatusL(KPluginAId) == EAuthUntrained);
       
   430 	
       
   431 	// restored to trained when identity trained
       
   432 	db->SetTrainedPluginL(KIdentity1Id, KPluginAId, tkiA);
       
   433 	TESTL(db->PluginStatusL(KPluginAId) == EAuthTrained);
       
   434 
       
   435 	// restored to fully trained when last identity trained
       
   436 	db->SetTrainedPluginL(KIdentity2Id, KPluginAId, tkiA);
       
   437 	TESTL(db->PluginStatusL(KPluginAId) == EAuthFullyTrained);
       
   438 	
       
   439 	// back to trained when new identity added
       
   440 	db->AddIdentityL(KIdentity3Id, KIdentity3Desc);
       
   441 	TESTL(db->PluginStatusL(KPluginAId) == EAuthTrained);
       
   442 
       
   443 	// upgraded to fully trained when untrained identity removed
       
   444 	db->RemoveIdentityL(KIdentity3Id);
       
   445 	TESTL(db->PluginStatusL(KPluginAId) == EAuthFullyTrained);
       
   446 	
       
   447 	// kept at fully trained when trained identity removed
       
   448 	db->RemoveIdentityL(KIdentity2Id);
       
   449 	TESTL(db->PluginStatusL(KPluginAId) == EAuthFullyTrained);
       
   450 	
       
   451 	// drop to untrained when last identity removed
       
   452 	db->RemoveIdentityL(KIdentity1Id);
       
   453 	TESTL(db->PluginStatusL(KPluginAId) == EAuthUntrained);
       
   454 	
       
   455 	CleanupStack::PopAndDestroy(db);
       
   456 	
       
   457 	__UHEAP_MARKEND;
       
   458 	}
       
   459 
       
   460 
       
   461 static void IdToTestDesc(TIdentityId aId, TDes& aDesc)
       
   462 /**
       
   463 	Helper function for TestRepairL generates a description
       
   464 	from the supplied identity.
       
   465 	
       
   466 	@param	aId				Identity Id.
       
   467 	@param	aDesc			Out paramater is populated with
       
   468 							description text.
       
   469  */
       
   470 	{
       
   471 	_LIT(KDescFmt, "desc_%08x");
       
   472 	aDesc.Format(KDescFmt, aId);
       
   473 	}
       
   474 
       
   475 
       
   476 void CTStepPersist2::TestRepairL()
       
   477 /**
       
   478 	Test CAuthDb repairs the database if possible.
       
   479  */
       
   480 	{
       
   481 	RemoveExistingDbL();
       
   482 	
       
   483 	CAuthDb2* db = CAuthDb2::NewLC(iFs);
       
   484 	
       
   485 	// create a set of identities
       
   486 	const TInt KTestIdCount = 8;
       
   487 	for (TInt i = 1; i <= KTestIdCount; ++i)
       
   488 		{
       
   489 		TBuf<13> buf;
       
   490 		IdToTestDesc(i, buf);
       
   491 		db->AddIdentityL(i, buf);
       
   492 		}
       
   493 	CleanupStack::PopAndDestroy(db);
       
   494 	
       
   495 	// damage the database by inserting a new entry but then
       
   496 	// rolling it back.
       
   497 	RDbNamedDatabase ndb;
       
   498 	TFileName dbName(KDbName);
       
   499 	dbName[0] = RFs::GetSystemDriveChar();
       
   500 	
       
   501 	User::LeaveIfError(ndb.Open(iFs, dbName));
       
   502 	CleanupClosePushL(ndb);
       
   503 	
       
   504 	User::LeaveIfError(ndb.Begin());
       
   505 	
       
   506 	RDbTable table;
       
   507 	CleanupClosePushL(table);
       
   508 	User::LeaveIfError(table.Open(ndb, KIdentitiesTableName));
       
   509 	
       
   510 	table.InsertL();
       
   511 	table.SetColL(KIdentitiesIdentityIdCol, KTestIdCount+1);
       
   512 	table.SetColL(KIdentitiesDescCol, _L("descb"));
       
   513 	table.PutL();
       
   514 	table.Close();
       
   515 	ndb.Rollback();
       
   516 	TESTL(ndb.IsDamaged());
       
   517 	
       
   518 	CleanupStack::PopAndDestroy(2, &ndb);	// table ndb
       
   519 	
       
   520 	// ensure db contains the same identities when it
       
   521 	// is reopened.
       
   522 	db = CAuthDb2::NewLC(iFs);
       
   523 	
       
   524 	RArray<TIdentityId> ids;
       
   525 	db->IdentitiesL(ids);
       
   526 	TInt idCount = ids.Count();
       
   527 	ids.Reset();
       
   528 	TESTL(idCount == KTestIdCount);
       
   529 	
       
   530 	for (TInt index = 1; index <= KTestIdCount; ++index)
       
   531 		{
       
   532 		TBuf<13> bufExp;
       
   533 		IdToTestDesc(index, bufExp);
       
   534 		TBuf<KMaxDescLen> actDesc;
       
   535 		HBufC* descAct = db->DescriptionL(index);
       
   536 		CleanupStack::PushL(descAct);
       
   537 		TESTL(bufExp == *descAct);
       
   538 		CleanupStack::PopAndDestroy(descAct);
       
   539 		}
       
   540 	CleanupStack::PopAndDestroy(db);
       
   541 	}
       
   542 
       
   543 
       
   544 void CTStepPersist2::TestCompressL()
       
   545 /**
       
   546 	Creates and deletes identities to create unused
       
   547 	space in database file, and tests compressed.
       
   548  */
       
   549 	{
       
   550 	RemoveExistingDbL();
       
   551 	CAuthDb2* db = CAuthDb2::NewLC(iFs);
       
   552 	
       
   553 	const TInt KIdentityCount = 32;
       
   554 	for (TInt i = 0; i < KIdentityCount; ++i)
       
   555 		{
       
   556 		_LIT(KCompDesc, "tc-test");
       
   557 		db->AddIdentityL(i, KCompDesc);
       
   558 		db->RemoveIdentityL(i);
       
   559 		}
       
   560 	CleanupStack::PopAndDestroy(db);
       
   561 	
       
   562 	TInt szPreComp = DbFileSizeL();
       
   563 	
       
   564 	db = CAuthDb2::NewLC(iFs);
       
   565 	db->CompactIfRequired();
       
   566 	CleanupStack::PopAndDestroy(db);
       
   567 	
       
   568 	TInt szPostComp = DbFileSizeL();
       
   569 	
       
   570 	TESTL(szPreComp > szPostComp);
       
   571 	}
       
   572 
       
   573 
       
   574 TInt CTStepPersist2::DbFileSizeL()
       
   575 /**
       
   576 	Helper function for TestCompressL returns
       
   577 	the size of the database file in bytes.
       
   578  */
       
   579 	{
       
   580 	RFile f;
       
   581 	TFileName dbName(KDbName);
       
   582 	dbName[0] = RFs::GetSystemDriveChar();
       
   583 
       
   584 	User::LeaveIfError(f.Open(iFs, dbName, EFileRead | EFileStream));
       
   585 	CleanupClosePushL(f);
       
   586 	
       
   587 	TInt sz;
       
   588 	User::LeaveIfError(f.Size(sz));
       
   589 	CleanupStack::PopAndDestroy(&f);
       
   590 	
       
   591 	return sz;
       
   592 	}
       
   593 
       
   594 
       
   595