phonebookengines_old/contactsmodel/tsrc/T_SEC_CNTDBASE.CPP
changeset 40 b46a585f6909
equal deleted inserted replaced
37:fd64c38c277d 40:b46a585f6909
       
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <e32test.h>
       
    17 #include <e32base.h>
       
    18  
       
    19 #include <e32std.h>
       
    20 #include <f32file.h>
       
    21 #include <s32file.h>
       
    22 #include <cntdb.h>
       
    23 #include <cntitem.h>
       
    24 #include <cntfield.h>
       
    25 #include <cntfldst.h>
       
    26 #include <coreappstest/testserver.h>
       
    27 #include "T_UTILS.H"
       
    28 
       
    29 _LIT(KTestName,"T_SEC_CNTDBASE");
       
    30 
       
    31 #include "testhelpers.h"
       
    32 
       
    33 LOCAL_C RFs fileServer;
       
    34 LOCAL_C TDriveList drives;
       
    35 LOCAL_C const TInt NoDrives = 2;
       
    36 LOCAL_C TInt driveIdx=0;
       
    37 LOCAL_C TInt NoDatabases=0;
       
    38 LOCAL_C TInt NoFileOnPreviousDrive=0;
       
    39 const TInt KNumTestContacts=20;
       
    40 LOCAL_C TFileName KNewCntTestFile(_L("Contacts.cbrchar"));
       
    41 
       
    42 
       
    43 LOCAL_C TDriveUnit driveArray[NoDrives];
       
    44 LOCAL_C CContactDatabase* db;
       
    45 
       
    46 
       
    47 _LIT(KNoFiles,"NoFiles");
       
    48 _LIT(KContactsFilename,"Contacts.cdb");
       
    49 
       
    50 // These defines and the Local method DrivesOfMediaTypeL should be moved to the 
       
    51 // Agenda Test Library (agntestlibrary)
       
    52 _LIT(KMediaNotPresent,	"MediaNotPresent");
       
    53 _LIT(KMediaUnknown,		"MediaUnknown");
       
    54 _LIT(KMediaFloppy,		"MediaFloppy");
       
    55 _LIT(KMediaHardDisk,	"MediaHardDisk");
       
    56 _LIT(KMediaCdRom,		"MediaCdRom");
       
    57 _LIT(KMediaRam,			"MediaRam");
       
    58 _LIT(KMediaFlash,		"MediaFlash");
       
    59 _LIT(KMediaRom,			"MediaRom");
       
    60 _LIT(KMediaRemote,		"MediaRemote");
       
    61 _LIT(KMediaNANDFlash,	"MediaNANDFlash");
       
    62 TPtrC MediaTypes[] = 
       
    63 			{ KMediaNotPresent(),KMediaUnknown(),KMediaFloppy(),KMediaHardDisk(),KMediaCdRom(),
       
    64 			KMediaRam(),KMediaFlash(),KMediaRom(),KMediaRemote(),KMediaNANDFlash()};
       
    65 _LIT(KCdbSecure,"c:T_Sec_Cntdbase.cdb");
       
    66 
       
    67 #ifdef __SYMBIAN_CNTMODEL_USE_SQLITE__
       
    68 _LIT(KCdbPrivate,"c:\\private\\10003A73\\SQLite__T_Sec_Cntdbase.cdb");
       
    69 #else
       
    70 _LIT(KCdbPrivate,"c:\\private\\10003A73\\T_Sec_Cntdbase.cdb");
       
    71 #endif
       
    72 
       
    73 _LIT(KCntNameFormat, "NAME #%d");
       
    74 _LIT(KCntNewNameFormat, "NEW NAME #%d");
       
    75 
       
    76 LOCAL_C CArrayFix<TInt>* DrivesOfMediaTypeL( TMediaType aMedia, RFs& aFs, TBool aStopAtFirst )
       
    77 	{
       
    78 	TDriveInfo info;
       
    79 	TInt error = KErrNone;
       
    80 	TDriveList driveList;
       
    81 	CArrayFix<TInt>* array = new (ELeave) CArrayFixFlat<TInt>(10);
       
    82 	CleanupStack::PushL( array );
       
    83 	error = aFs.DriveList(driveList);
       
    84 	User::LeaveIfError(error);
       
    85 	for(TInt drive=EDriveA;drive<=EDriveZ;drive++)
       
    86 		{
       
    87 		if( driveList[drive] )
       
    88 			{
       
    89 			error = aFs.Drive(info, drive);
       
    90 			if	(error != KErrNone)
       
    91 				{
       
    92 				continue;
       
    93 				}
       
    94 			if	( info.iType == aMedia )
       
    95 				{
       
    96 				array->AppendL( drive );
       
    97 				if( aStopAtFirst )
       
    98 					{ // return if only the first drive of the required type.
       
    99 					break;
       
   100 					}
       
   101 				}
       
   102 			}
       
   103 		}
       
   104 	CleanupStack::Pop( array );
       
   105 	return array;
       
   106 	}
       
   107 
       
   108 /**
       
   109 	MMCDriveL
       
   110 	
       
   111 	Determines a list of drives of type EMediaHardDisk.
       
   112 	The first drive in this list is returned and used
       
   113 	by the rest of the test.
       
   114 
       
   115         NOTE: if this test is running on ARMv5, an MMC card
       
   116         must be present otherwise the test fails.
       
   117 
       
   118 */
       
   119 LOCAL_C TDriveUnit MMCDriveL()
       
   120 	{
       
   121 	CArrayFix<TInt>* drives = DrivesOfMediaTypeL(EMediaHardDisk,fileServer,EFalse);
       
   122 	CleanupStack::PushL(drives);
       
   123 	TInt drivesTInt = drives->Count();
       
   124 	if( drivesTInt )
       
   125 		{
       
   126 		test.Printf(_L("Found the following drives of type %S\n"),&MediaTypes[EMediaHardDisk]);
       
   127 		for(TInt i=0;i<drivesTInt;i++)
       
   128 			{
       
   129 			TDriveUnit drv(drives->At(i));
       
   130 			TPtrC drvPtr(drv.Name());
       
   131 			test.Printf(_L("%S\n"),&drvPtr);
       
   132 			}
       
   133 		}
       
   134 	else
       
   135 		{
       
   136 		test.Printf( _L("No drives found of type %S\n"),&MediaTypes[EMediaHardDisk]);
       
   137 		User::LeaveIfError(KErrHardwareNotAvailable);
       
   138 		}
       
   139 	TDriveUnit mmcDrive(drives->At(0));
       
   140 	CleanupStack::PopAndDestroy(drives);
       
   141 	// Use the first drive in the list for the rest of the test.
       
   142 	return mmcDrive;
       
   143 	}
       
   144 
       
   145 /**
       
   146 	AddNewContactsL
       
   147 	Add KNumTestContacts new contacts with varying numbres of text fields
       
   148 
       
   149 	@param aIdList
       
   150 */
       
   151 LOCAL_C void AddNewContactsL(CArrayFix<TContactItemId>* aIdList)
       
   152 	{
       
   153 	CContactCard* card=CContactCard::NewL();
       
   154 	CleanupStack::PushL(card);
       
   155 	TContactItemId id=KNullContactId;
       
   156 	TContactItemId newId=KNullContactId;
       
   157 	for (TInt ii=0;ii<KNumTestContacts;ii++)
       
   158 		{
       
   159 		TBuf<16> name;
       
   160 		name.Format(KCntNameFormat, ii);
       
   161 		SetNameL(*card,KUidContactFieldFamilyName,KUidContactFieldVCardMapUnusedN,name,EFalse);
       
   162 		CContactItemField* field=CContactItemField::NewLC(KStorageTypeText);
       
   163 		card->AddFieldL(*field);
       
   164 		CleanupStack::Pop(); // field
       
   165 		newId=db->AddNewContactL(*card);
       
   166 		id=newId;
       
   167 		aIdList->AppendL(id);
       
   168 		}
       
   169 	CleanupStack::PopAndDestroy(); // card
       
   170 	}
       
   171 
       
   172 LOCAL_C TPtrC Name(CContactItem& aItem)
       
   173 	{
       
   174 	CContactItemFieldSet& fieldSet=aItem.CardFields();
       
   175 	const TInt pos=fieldSet.Find(KUidContactFieldFamilyName);
       
   176 	if (pos==KErrNotFound)
       
   177 		return _L("");
       
   178 	return fieldSet[pos].TextStorage()->Text();
       
   179 	}
       
   180 
       
   181 /**
       
   182 	EditContactsL
       
   183 	Check then edit contact names
       
   184 
       
   185 	@param aIdList
       
   186 */
       
   187 LOCAL_C void EditContactsL(CArrayFix<TContactItemId>* aIdList)
       
   188 	{
       
   189 	TInt ii=0;
       
   190 	test.Next(_L("Change all contact names"));
       
   191 #ifdef __SYMBIAN_CNTMODEL_USE_SQLITE__
       
   192 	for (;ii<aIdList->Count();++ii)
       
   193 #else		
       
   194 	for (;ii<KNumTestContacts;ii++)
       
   195 #endif
       
   196 		{
       
   197 		const TInt index=(*aIdList)[ii];
       
   198 		// exercise OpenContactLX api, Commit the change and Pop the lock record
       
   199 		CContactItem* item=db->OpenContactLX(index);
       
   200 		CleanupStack::PushL(item);
       
   201 		TBuf<16> name;
       
   202 		name.Format(KCntNameFormat, ii); // "NAME %d"
       
   203 		test(name==Name(*item));
       
   204 		name.Format(KCntNewNameFormat, index);	// "NEW NAME %d"
       
   205 		SetNameL(*item,KUidContactFieldFamilyName,KUidContactFieldVCardMapUnusedN,name,EFalse);
       
   206 		db->CommitContactL(*item);
       
   207 		CleanupStack::PopAndDestroy(); // item;
       
   208 		CleanupStack::Pop(); // lock record
       
   209 		}
       
   210 	test.Next(_L("Check new contact names"));
       
   211 #ifdef __SYMBIAN_CNTMODEL_USE_SQLITE__
       
   212 	for (;ii<aIdList->Count();++ii)
       
   213 #else
       
   214 	for (ii=0;ii<KNumTestContacts;ii++)
       
   215 #endif
       
   216 		{
       
   217 		const TInt index=(*aIdList)[ii];
       
   218 		CContactItem* item=db->ReadContactL(index);
       
   219 		TBuf<16> name;
       
   220 		name.Format(KCntNewNameFormat, index);
       
   221 		test(name==Name(*item));
       
   222 		delete item;
       
   223 		}
       
   224 	test.Next(_L("Regression test for Contact locking/unlocking (DEF067162)"));
       
   225 #ifdef __SYMBIAN_CNTMODEL_USE_SQLITE__
       
   226 	for (;ii<aIdList->Count();++ii)
       
   227 #else	
       
   228 	for (ii = 0; ii < KNumTestContacts; ii++)
       
   229 #endif
       
   230 		{
       
   231 		// open 2 different items
       
   232 		const TInt index1 = (*aIdList)[ii];
       
   233 		const TInt index2 = (*aIdList)[(ii + 1) % KNumTestContacts];
       
   234 		CContactItem* item1 = db->OpenContactLX(index1);
       
   235 		CleanupStack::PushL(item1);
       
   236 		CContactItem* item2 = db->OpenContactLX(index2);
       
   237 		delete item2;
       
   238 		CleanupStack::PopAndDestroy(); // destroy lock = close item2
       
   239 		CleanupStack::PopAndDestroy(item1);
       
   240 		CleanupStack::PopAndDestroy(); // destroy lock = close item1
       
   241 
       
   242 		// re-open 1st item - should not be locked
       
   243 		// OpenContactLX() and PopAndDestroy()
       
   244 		item1 = db->OpenContactLX(index1);
       
   245 		delete item1;
       
   246 		CleanupStack::PopAndDestroy(); // destroy lock = close item1
       
   247 
       
   248 		// re-open 2nd item - should not be locked
       
   249 		// OpenContactL() and CloseContactL()
       
   250 		item2 = db->OpenContactL(index2);
       
   251 		CleanupStack::PushL(item2);
       
   252 		db->CloseContactL(index2);				// explicitly close item 2
       
   253 		CleanupStack::PopAndDestroy(item2);
       
   254 		}
       
   255 
       
   256 
       
   257 	// try to read a non-existant contact
       
   258 	TRAPD(err,db->ReadContactL(KNullContactId));
       
   259 	TESTTRUEL(err!=KErrNone);
       
   260 #ifdef __SYMBIAN_CNTMODEL_USE_SQLITE__
       
   261 	if(aIdList->Count() > 0)
       
   262 		{
       
   263 #endif		
       
   264 	// try to edit a contact that hasn't been opened
       
   265 	CContactItem* item=db->ReadContactLC((*aIdList)[0]);
       
   266 	TRAP(err,db->CommitContactL(*item));
       
   267 	CleanupStack::PopAndDestroy(); // item
       
   268 	TESTTRUEL(err==KErrAccessDenied);
       
   269 #ifdef __SYMBIAN_CNTMODEL_USE_SQLITE__
       
   270 		}
       
   271 #endif			
       
   272 	// try to close a non-existant contact
       
   273 	TRAP(err,db->CloseContactL(KNullContactId));
       
   274 	TESTTRUEL(err==KErrNone); // test changed from 001 (using DBMS 043) - this test makes more sense anyway
       
   275 #ifdef __SYMBIAN_CNTMODEL_USE_SQLITE__
       
   276 	if(aIdList->Count() > 0)
       
   277 		{
       
   278 #endif			
       
   279 	// try to close a contact that hasn't been opened
       
   280 	TRAP(err,db->CloseContactL((*aIdList)[0]));
       
   281 	TESTTRUEL(err==KErrNone);
       
   282 #ifdef __SYMBIAN_CNTMODEL_USE_SQLITE__
       
   283 		}
       
   284 #endif			
       
   285 	}
       
   286 
       
   287 /**
       
   288 	Gets the list of installed drives and then stores the first 2
       
   289 	in the array drives for use within the test. The second entry
       
   290 	in this array should use method MMCDriveL as used in T_Sec_Tidy
       
   291 	and T_Sec_ListAgendaFile.
       
   292 */
       
   293 LOCAL_C void DrivesL()
       
   294 	{
       
   295 	driveArray[0] = EDriveC;
       
   296 	driveArray[1] = MMCDriveL();
       
   297 	
       
   298 	// Tell the user which drive has been chosen. 
       
   299 	TPtrC drv( driveArray[1].Name() );
       
   300 	test.Printf(_L("%S chosen for the test.\n"),&drv);
       
   301 	}
       
   302 
       
   303 /**
       
   304 	Ensure that the filename conforms to the correct form.
       
   305 	The analysis functionality is dependent on the flags:
       
   306 	SYMBIAN_SECURE_DBMS. Unlike Agenda, in contacts as GetDefaultNameL
       
   307 	existed before platsec there is legacy functionality that must
       
   308 	maintained. In not SYMBIAN_SECURE_DBMS the full path must be checked
       
   309 	for. The default filename exits on the current drive.
       
   310 	@param reference to the default file name.
       
   311 	@return ETrue if the default filename is correctly formed.
       
   312 */
       
   313 LOCAL_C TBool CheckDefaultContactsFilename( TFileName& aFilename )
       
   314 	{
       
   315 	_LIT(KDriveFailure, "Incorrect Drive Specified" );
       
   316 	_LIT(KPathFailure, "Incorrect No Path expected" );
       
   317 	_LIT(KFilenameFailure, "Incorrect Filename" );
       
   318 
       
   319 	TDriveUnit lDriveUnit;
       
   320 
       
   321 	// Get the current drive.
       
   322 	CContactDatabase::DatabaseDrive(lDriveUnit);
       
   323 
       
   324 	TParse filename;
       
   325 	filename.Set( aFilename, NULL, NULL);
       
   326 	
       
   327 	// Check that the drive letter exists. No drive is an error 
       
   328 	// in both modes.
       
   329 	if( filename.Drive() != lDriveUnit.Name())
       
   330 		{
       
   331 		test.Printf(KDriveFailure);
       
   332 		return EFalse;
       
   333 		}
       
   334 	if( filename.Path().Length())
       
   335 		{
       
   336 		test.Printf(KPathFailure);
       
   337 		return EFalse;
       
   338 		}
       
   339 	if( filename.NameAndExt().CompareF(KContactsFilename()))
       
   340 		{
       
   341 		test.Printf(KFilenameFailure);
       
   342 		return EFalse;
       
   343 		}
       
   344 	return ETrue;
       
   345 	}
       
   346 
       
   347 /** 
       
   348 	CheckNotDefaultContactsFiles
       
   349 	
       
   350 	Uses all the default database related API's to ensure that
       
   351 	they all return negative, now that the default db has been
       
   352 	removed.
       
   353 	@return ETrue if a default database cannot be found on the
       
   354 	default drive.
       
   355 */
       
   356 LOCAL_C TBool CheckNotDefaultContactsFiles()
       
   357 	{
       
   358 	// The default database on the current default drive.(3)
       
   359 	// The default database on the specific drive. (4)
       
   360 	TBool response = EFalse;
       
   361 	TRAPD(err, response = CContactDatabase::DefaultContactDatabaseExistsL());
       
   362 	if( !(err == KErrNone && !response ))
       
   363 		{
       
   364 		return EFalse;
       
   365 		}
       
   366 	
       
   367 	// Use the default database, as we have confirmed previously
       
   368 	// this shouldn't exits. This is dependent upon the Security mode.
       
   369 	TFileName filename;
       
   370 	filename.Copy(driveArray[driveIdx].Name());
       
   371 	filename.Append(KContactsFilename);
       
   372 	// (5)
       
   373 	TRAP(err, response = CContactDatabase::ContactDatabaseExistsL(filename));
       
   374 	if( !(err == KErrNone && !response ))
       
   375 		{
       
   376 		return EFalse;
       
   377 		}
       
   378 	
       
   379 	// None of the API return true, no default Contact DB files were detected. 
       
   380 	return ETrue;
       
   381 	}
       
   382 
       
   383 
       
   384 /**
       
   385 	CheckCntFilesL
       
   386 	
       
   387 	Ensure that cntFiles contains the list of correct files.
       
   388 	The analysis functionality is dependent on the flags:
       
   389 	_SECURE_DATA_ and _UNSECURE_DATA_
       
   390 	Determines the list of agn files, checks for the existance of aFilename
       
   391 	within the list of aNum names.
       
   392 
       
   393 	@param aNum number of expected files. 
       
   394 	@param aFilename a reference to an expected filename.
       
   395 	@return aFileExpectedToExist ETrue if the list contains the expected files.
       
   396 */
       
   397 LOCAL_C TBool CheckCntFilesL( TInt aNum,  const TDesC& aFilename , TBool aFileExpectedToExist = ETrue)
       
   398 	{
       
   399 	// Data bases on all drives.
       
   400 	CDesCArray* cntFiles = CContactDatabase::ListDatabasesL();
       
   401 	CleanupStack::PushL(cntFiles);
       
   402 	// Data bases on the specified drive.
       
   403 	CDesCArray* cntFilesFromDrive = CContactDatabase::ListDatabasesL(driveArray[driveIdx]);
       
   404 	CleanupStack::PushL(cntFilesFromDrive);
       
   405 	
       
   406 	TInt DriveZCount=0;
       
   407 	// Determine files on drive Z:
       
   408 	
       
   409 	if( !cntFiles->Count() && !cntFilesFromDrive->Count() && !aNum )
       
   410 		{
       
   411 		CleanupStack::PopAndDestroy( cntFilesFromDrive );
       
   412 		CleanupStack::PopAndDestroy( cntFiles );								
       
   413 		return !aFileExpectedToExist;
       
   414 		}
       
   415 	// Ensure that all drive contents add up.
       
   416 	if( cntFiles->Count() != NoFileOnPreviousDrive + DriveZCount + cntFilesFromDrive->Count() )
       
   417 		{
       
   418 		CleanupStack::PopAndDestroy(cntFilesFromDrive);
       
   419 		CleanupStack::PopAndDestroy(cntFiles);
       
   420 		return !aFileExpectedToExist;
       
   421 		}
       
   422 	TFileName filename;
       
   423 	// In un-secure mode if aFilename contains no path, then
       
   424 	// a backslash must be included between the drive and the
       
   425 	// filename. This because when asked to create filename 
       
   426 	// "c:filename" ListDatabasesL returns the name "c:\filename"
       
   427 	// hence a backslash is inserted for comparison purposes.
       
   428 	TParse file;
       
   429 	file.Set(aFilename,NULL,NULL);
       
   430 
       
   431 	filename.Copy(aFilename);	
       
   432 
       
   433 	// Check that aFilename is in the list.
       
   434 	TBool retValue = ETrue;
       
   435 	if (!aFileExpectedToExist)
       
   436 		{
       
   437 		// File is not expected to exist. So return EFalse if we find it.
       
   438 		for(TInt i=0;i<cntFilesFromDrive->Count();i++)
       
   439 			{
       
   440 			TFileName driveList_1 = (*cntFilesFromDrive)[i];
       
   441 			if(!driveList_1.CompareF(filename))
       
   442 				{
       
   443 				CleanupStack::PopAndDestroy(cntFilesFromDrive);
       
   444 				CleanupStack::PopAndDestroy(cntFiles);
       
   445 				return !retValue;
       
   446 				}
       
   447 			}
       
   448 		retValue = EFalse;
       
   449 		}
       
   450 	else
       
   451 		{
       
   452 		// File is expected to exist. We should find it in both lists.
       
   453 		// return EFalse if not found in this list.
       
   454 		TBool fileFound = EFalse;
       
   455 		// does file exist in list from drive
       
   456 		for(TInt j=0;j<cntFilesFromDrive->Count();j++)
       
   457 			{
       
   458 			TFileName driveList_2 = (*cntFilesFromDrive)[j];
       
   459 			if(!driveList_2.CompareF(filename))
       
   460 				{
       
   461 				fileFound = ETrue;
       
   462 				break;
       
   463 				}
       
   464 			}
       
   465 
       
   466 		if(!fileFound)
       
   467 			{
       
   468 			CleanupStack::PopAndDestroy(cntFilesFromDrive);
       
   469 			CleanupStack::PopAndDestroy(cntFiles);
       
   470 			return !retValue;
       
   471 			}
       
   472 		}
       
   473 
       
   474 	// We found the file in cntFilesFromDrive 
       
   475 	// Does file exist in list from all drives
       
   476 	for(TInt k=0;k<cntFiles->Count();k++)
       
   477 		{
       
   478 		TFileName driveList_3 = (*cntFiles)[k];
       
   479 		if(!driveList_3.CompareF(filename))
       
   480 			{
       
   481 			CleanupStack::PopAndDestroy(cntFilesFromDrive);
       
   482 			CleanupStack::PopAndDestroy(cntFiles);
       
   483 			return retValue;
       
   484 			}
       
   485 		}	
       
   486 	CleanupStack::PopAndDestroy(cntFilesFromDrive);
       
   487 	CleanupStack::PopAndDestroy(cntFiles);
       
   488 	// File was not found so return false.
       
   489 	return !retValue;
       
   490 	}
       
   491 
       
   492 /**
       
   493 	This API uses all the Contacts API's relating to identification
       
   494 	of the default file.
       
   495 */
       
   496 LOCAL_C TBool DefaultDatabaseL()
       
   497 	{
       
   498 		// The default database on the current default drive.
       
   499 	if(!CContactDatabase::DefaultContactDatabaseExistsL())
       
   500 		return EFalse;
       
   501 
       
   502 	// Use the default database, as we have confirmed previously
       
   503 	// this shouldn't exits. This is dependent upon the Security mode.
       
   504 	TFileName filename;
       
   505 	filename.Copy(driveArray[driveIdx].Name());
       
   506 	filename.Append(KContactsFilename);
       
   507 	if(!CContactDatabase::ContactDatabaseExistsL(filename))
       
   508 		return EFalse;
       
   509 	
       
   510 	// None of the API return true, no files were detected.
       
   511 	return ETrue;
       
   512 
       
   513 	}
       
   514 	
       
   515 /**
       
   516 	This function uses Five API's available to determine the existance
       
   517 	of database files. These are (1)ListDatabasesL(),(2)ListDatabasesL(TDriveUnit&)
       
   518 	(3)DefaultContactDatabaseExistsL(),(4)DefaultContactDatabaseExistsL(TDriveUnit&)
       
   519 	and (5)ContactDatabaseExistsL(const TDesC&). All five should confirm that there
       
   520 	are no contacts database files.
       
   521 */
       
   522 LOCAL_C TBool CheckNoDatabaseFilesL()
       
   523 	{
       
   524 
       
   525 	TFileName filename;
       
   526 	TBool retVal = EFalse;
       
   527 	filename.Copy(driveArray[driveIdx].Name());
       
   528 
       
   529 	filename.Append(KContactsFilename);
       
   530 
       
   531 	retVal = CheckCntFilesL(NoDatabases,  filename , EFalse);
       
   532 
       
   533 	if (retVal == EFalse)
       
   534 		return retVal;
       
   535 	else
       
   536 		return CheckNotDefaultContactsFiles();
       
   537 	}
       
   538 
       
   539 /**
       
   540 	Tidy
       
   541 	
       
   542 	This function removes any files that may exist as a result of a
       
   543 	previous run of the T_Sec_cntdbase test.
       
   544 	The configuration of this operation is dependent upon __SECURE_DATA_
       
   545 */
       
   546 
       
   547 LOCAL_C void Tidy()
       
   548 	{
       
   549 	TFileName addedFile;
       
   550 	CDesCArray* driveFiles = NULL;
       
   551 	for(TInt i =0;i<NoDrives; i++ )
       
   552 		{
       
   553 		addedFile.Copy(driveArray[i].Name());
       
   554 		addedFile.Append(KNewCntTestFile);
       
   555 		TRAPD( err, CContactDatabase::SetDatabaseDriveL(driveArray[i], EFalse));
       
   556 		TRAP( err, CContactDatabase::DeleteDefaultFileL());
       
   557 		TRAP(err, CContactDatabase::DeleteDatabaseL(addedFile));
       
   558 		}
       
   559 	
       
   560 	TRAPD(ret, driveFiles = CContactDatabase::ListDatabasesL());
       
   561 	for(TInt j =0;j<driveFiles->Count();j++)
       
   562 		{
       
   563 		CContactDatabase::DeleteDatabaseL((*driveFiles)[j]);
       
   564 		}	
       
   565 	if( ret == KErrNone )
       
   566 		{
       
   567 		delete driveFiles;
       
   568 		}
       
   569 	}
       
   570 
       
   571 // support CreateL and ReplaceL OOM tests
       
   572 LOCAL_C TInt CheckDatabaseIsAbsentOrOpenable(TBool aDeleteDatabase=EFalse)
       
   573 	{
       
   574 	// CreateL or ReplaceL failed, so database can either be absent or it must Open
       
   575 	TBool exists(EFalse);
       
   576 	
       
   577 	TRAPD(err, exists = CContactDatabase::DefaultContactDatabaseExistsL());
       
   578 
       
   579 	if (err)
       
   580 		{
       
   581 		test.Printf(_L("CheckDatabaseIsAbsentOrOpenable - DefaultContactDatabaseExistsL error %d"), err);
       
   582 		}
       
   583 	
       
   584 	if(exists)
       
   585 		{
       
   586 		CContactDatabase* dbaseOpen = NULL;
       
   587 				
       
   588 		TRAP(err, dbaseOpen = CContactDatabase::OpenL());
       
   589 		
       
   590 		if (err)
       
   591 			{
       
   592 			test.Printf(_L("CheckDatabaseIsAbsentOrOpenable - OpenL error %d"), err);
       
   593 			}
       
   594 	
       
   595 		if (dbaseOpen != NULL)
       
   596 			{
       
   597 			// database exists, so it should have opened
       
   598 			delete dbaseOpen;
       
   599 			}
       
   600 			
       
   601 		if (aDeleteDatabase && (err == KErrNone))
       
   602 			{
       
   603 			// for client side CreateL OOM test
       
   604 			CContactDatabase::DeleteDefaultFileL();
       
   605 			}
       
   606 		}
       
   607 
       
   608 	return err;
       
   609 	}
       
   610 
       
   611 
       
   612 // Client Side OOM Tests //	
       
   613 LOCAL_C TBool ReplaceLOOM_ClientSideL()
       
   614 	{
       
   615 	test.Next(_L("ReplaceL client side OOM test.\n"));
       
   616 
       
   617 	CContactDatabase* dBase = NULL;
       
   618 	TInt ret = KErrNoMemory;
       
   619 	TInt failAt = 0;
       
   620 	while(ret != KErrNone )
       
   621 		{
       
   622 		failAt++;
       
   623 		if( !(failAt % 5 ))
       
   624 			{
       
   625 			test.Printf(_L("."));
       
   626 			}
       
   627 
       
   628 		__UHEAP_SETFAIL(RHeap::EDeterministic, failAt);
       
   629 		__UHEAP_MARK;
       
   630 		TRAP(ret, dBase = CContactDatabase::ReplaceL());
       
   631 		if( ret == KErrNone )
       
   632 			{
       
   633 			delete dBase;
       
   634 			}
       
   635 		__UHEAP_MARKEND;
       
   636 		__UHEAP_RESET;
       
   637 
       
   638 		if (ret != KErrNone)
       
   639 			{
       
   640 			if(ret != KErrNoMemory)
       
   641 				{
       
   642 				test.Printf(_L("iteration %d failed - non-memory error in OOM test: %d\n"), failAt, ret);
       
   643 				TESTTRUEL(ret == KErrNoMemory);
       
   644 				}
       
   645 
       
   646 			TESTNOERRL(CheckDatabaseIsAbsentOrOpenable());
       
   647 			}
       
   648 		}
       
   649 
       
   650 	// Remove the default database, created by Replace.
       
   651 	CContactDatabase::DeleteDefaultFileL();
       
   652 	test.Printf(_L("\n"));
       
   653 	return ETrue;
       
   654 	}
       
   655 
       
   656 	
       
   657 LOCAL_C TBool ContactDatabaseExistsLOOM_ClientSideL()
       
   658 	{
       
   659 	test.Next(_L("ContactDatabaseExistsL client side OOM test.\n"));
       
   660 	_LIT(KContactDBase,"c:ContactDBase");
       
   661 
       
   662 	TBool dExists = EFalse;
       
   663 	// First create a database to be detected.
       
   664 	CContactDatabase* dBase = NULL;
       
   665 	TRAPD(err, dBase = CContactDatabase::CreateL(KContactDBase));
       
   666 
       
   667 	// need the file to be present to test if it exists
       
   668 	TESTTRUEL((err == KErrNone) || (err == KErrAlreadyExists));
       
   669 	if (err == KErrNone)
       
   670 		{
       
   671 		delete dBase;
       
   672 		}
       
   673 
       
   674 	TInt ret = KErrNoMemory;
       
   675 	TInt failAt = 0;
       
   676 	while(ret != KErrNone )
       
   677 		{
       
   678 		failAt++;
       
   679 		if( !(failAt % 5 ))
       
   680 			{
       
   681 			test.Printf(_L("."));
       
   682 			}
       
   683 
       
   684 		__UHEAP_SETFAIL(RHeap::EDeterministic, failAt);
       
   685 		__UHEAP_MARK;
       
   686 		TRAP(ret, dExists = CContactDatabase::ContactDatabaseExistsL(KContactDBase));
       
   687 		__UHEAP_MARKEND;
       
   688 		__UHEAP_RESET;
       
   689 
       
   690 		if	(ret != KErrNoMemory && ret != KErrNone)
       
   691 			{
       
   692 			test.Printf(_L("iteration %d failed - non-memory error in OOM test: %d\n"), failAt, ret);
       
   693 			TESTTRUEL((ret == KErrNoMemory || ret == KErrNone));
       
   694 			}	
       
   695 		}
       
   696 	if( ret == KErrNone && dExists )
       
   697 		{
       
   698 		// Remove the database.
       
   699 		CContactDatabase::DeleteDatabaseL(KContactDBase);
       
   700 		}
       
   701 	test.Printf(_L("\n"));
       
   702 	return ETrue;
       
   703 	}
       
   704 
       
   705 LOCAL_C TBool DefaultContactDatabaseExistsLOOM_ClientSideL()
       
   706 	{
       
   707 	test.Next(_L("DefaultContactDatabaseExistsL client side OOM test.\n"));
       
   708 
       
   709 	TBool dExists = EFalse;
       
   710 	// First create a default database.
       
   711 	CContactDatabase* dBase = NULL;
       
   712 	TRAPD( err, dBase = CContactDatabase::CreateL());
       
   713 	// need the file to be present to test if it exists
       
   714 	TESTTRUEL((err == KErrNone) || (err == KErrAlreadyExists));
       
   715 	if (err == KErrNone)
       
   716 		{
       
   717 		delete dBase;
       
   718 		}
       
   719 
       
   720 	TInt ret = KErrNoMemory;
       
   721 	TInt failAt = 0;
       
   722 	while(ret != KErrNone )
       
   723 		{
       
   724 		failAt++;
       
   725 		if( !(failAt % 5 ))
       
   726 			{
       
   727 			test.Printf(_L("."));
       
   728 			}
       
   729 
       
   730 		__UHEAP_SETFAIL(RHeap::EDeterministic, failAt);
       
   731 		__UHEAP_MARK;
       
   732 		TRAP(ret, dExists = CContactDatabase::DefaultContactDatabaseExistsL());
       
   733 		__UHEAP_MARKEND;
       
   734 		__UHEAP_RESET;
       
   735 
       
   736 		if	(ret != KErrNoMemory && ret != KErrNone)
       
   737 			{
       
   738 			test.Printf(_L("iteration %d failed - non-memory error in OOM test: %d\n"), failAt, ret);
       
   739 			TESTTRUEL((ret == KErrNoMemory || ret == KErrNone));
       
   740 			}
       
   741 		}
       
   742 	if( ret == KErrNone && dExists )
       
   743 		{
       
   744 		// Remove the default database
       
   745 		CContactDatabase::DeleteDefaultFileL();
       
   746 		}
       
   747 	test.Printf(_L("\n"));
       
   748 	return ETrue;
       
   749 	}
       
   750 
       
   751 LOCAL_C TBool GetDefaultNameLOOM_ClientSideL()
       
   752 	{
       
   753 	test.Next(_L("GetDefaultNameL client side OOM test.\n"));
       
   754 	TFileName filename;
       
   755 
       
   756 	TInt ret = KErrNoMemory;
       
   757 	TInt failAt = 0;
       
   758 	while(ret != KErrNone )
       
   759 		{
       
   760 		failAt++;
       
   761 		if( !(failAt % 5 ))
       
   762 			{
       
   763 			test.Printf(_L("."));
       
   764 			}
       
   765 
       
   766 		__UHEAP_SETFAIL(RHeap::EDeterministic, failAt);
       
   767 		__UHEAP_MARK;
       
   768 		TRAP(ret, CContactDatabase::GetDefaultNameL(filename));
       
   769 		__UHEAP_MARKEND;
       
   770 		__UHEAP_RESET;
       
   771 
       
   772 		if	(ret != KErrNoMemory && ret != KErrNone)
       
   773 			{
       
   774 			test.Printf(_L("iteration %d failed - non-memory error in OOM test: %d\n"), failAt, ret);
       
   775 			TESTTRUEL((ret == KErrNoMemory || ret == KErrNone));
       
   776 			}		
       
   777 		}
       
   778 	test.Printf(_L("\n"));
       
   779 	return ETrue;
       
   780 	}
       
   781 	
       
   782 LOCAL_C TBool DeleteDatabaseLOOM_ClientSideL()
       
   783 	{
       
   784 	test.Next(_L("DeleteDatabaseL client side OOM test.\n"));
       
   785 	_LIT(KTestFile,"C:TestFile");
       
   786 	CContactDatabase* dBase = NULL;
       
   787 	// Create a file to delete.
       
   788 	TRAPD(err, dBase = CContactDatabase::CreateL(KTestFile));
       
   789 	// need the file to be present to delete it
       
   790 	TESTTRUEL((err == KErrNone) || (err == KErrAlreadyExists));
       
   791 	if (err == KErrNone)
       
   792 		{
       
   793 		delete dBase;
       
   794 		}
       
   795 
       
   796 	TInt ret = KErrNoMemory;
       
   797 	TInt failAt = 0;
       
   798 	while(ret != KErrNone )
       
   799 		{
       
   800 		failAt++;
       
   801 		if( !(failAt % 5 ))
       
   802 			{
       
   803 			test.Printf(_L("."));
       
   804 			}
       
   805 
       
   806 		__UHEAP_SETFAIL(RHeap::EDeterministic, failAt);
       
   807 		__UHEAP_MARK;
       
   808 		TRAP(ret, CContactDatabase::DeleteDatabaseL(KTestFile));
       
   809 		__UHEAP_MARKEND;
       
   810 		__UHEAP_RESET;
       
   811 
       
   812 		if	(ret != KErrNoMemory && ret != KErrNone)
       
   813 			{
       
   814 			test.Printf(_L("iteration %d failed - non-memory error in OOM test: %d\n"), failAt, ret);
       
   815 			TESTTRUEL((ret == KErrNoMemory || ret == KErrNone));
       
   816 			}		
       
   817 		}
       
   818 	test.Printf(_L("\n"));
       
   819 	// Check to see that database has been removed.
       
   820 	TBool dExists = EFalse;
       
   821 	TRAP(ret, dExists = CContactDatabase::ContactDatabaseExistsL(KTestFile));
       
   822 	TESTNOERRL(ret);
       
   823 
       
   824 	return !dExists;
       
   825 	}
       
   826 
       
   827 LOCAL_C TBool DeleteDefaultFileLOOM_ClientSideL()
       
   828 	{
       
   829 	test.Next(_L("DeleteDefaultFileL client side OOM test.\n"));
       
   830 
       
   831 	CContactDatabase* dBase = NULL;
       
   832 	TRAPD( err, dBase = CContactDatabase::CreateL());
       
   833 	// need the file to be present to delete it
       
   834 	TESTTRUEL((err == KErrNone) || (err == KErrAlreadyExists));
       
   835 	if (err == KErrNone)
       
   836 		{
       
   837 		delete dBase;
       
   838 		}
       
   839 
       
   840 	TInt ret = KErrNoMemory;
       
   841 	TInt failAt = 0;
       
   842 	while(ret != KErrNone )
       
   843 		{
       
   844 		failAt++;
       
   845 		if( !(failAt % 5 ))
       
   846 			{
       
   847 			test.Printf(_L("."));
       
   848 			}
       
   849 
       
   850 		__UHEAP_SETFAIL(RHeap::EDeterministic, failAt);
       
   851 		__UHEAP_MARK;
       
   852 		TRAP(ret, CContactDatabase::DeleteDefaultFileL());
       
   853 		__UHEAP_MARKEND;
       
   854 		__UHEAP_RESET;
       
   855 
       
   856 		if	(ret != KErrNoMemory && ret != KErrNone)
       
   857 			{
       
   858 			test.Printf(_L("iteration %d failed - non-memory error in OOM test: %d\n"), failAt, ret);
       
   859 			TESTTRUEL((ret == KErrNoMemory || ret == KErrNone));
       
   860 			}
       
   861 		}
       
   862 	test.Printf(_L("\n"));
       
   863 	// Ensure that the default File has been removed.
       
   864 	TBool dExists = EFalse;
       
   865 	TRAP(ret, dExists = CContactDatabase::DefaultContactDatabaseExistsL());
       
   866 	TESTNOERRL(ret);
       
   867 
       
   868 	return !dExists;
       
   869 	}
       
   870 	
       
   871 LOCAL_C TBool CreateLOOM_ClientSideL()
       
   872 	{
       
   873 	test.Next(_L("CreateL client side OOM test.\n"));
       
   874 
       
   875 	TRAPD(err, CContactDatabase::DeleteDefaultFileL());
       
   876 	TESTTRUEL((err == KErrNone) || (err == KErrNotFound));
       
   877 	
       
   878 	CContactDatabase* dBase = NULL;
       
   879 	TInt ret = KErrNoMemory;
       
   880 	TInt failAt = 0;
       
   881 	while(ret != KErrNone )
       
   882 		{
       
   883 		failAt++;
       
   884 		if( !(failAt % 5 ))
       
   885 			{
       
   886 			test.Printf(_L("."));
       
   887 			}
       
   888 
       
   889 		__UHEAP_SETFAIL(RHeap::EDeterministic, failAt);
       
   890 		__UHEAP_MARK;
       
   891 			
       
   892 		TRAP(ret, dBase = CContactDatabase::CreateL());
       
   893 		if( ret == KErrNone ) 
       
   894 			{
       
   895 			delete dBase;
       
   896 			dBase=NULL;
       
   897 			}
       
   898 		__UHEAP_MARKEND;
       
   899 		__UHEAP_RESET;
       
   900 		if (ret != KErrNone)
       
   901 			{
       
   902 			if(ret != KErrNoMemory)
       
   903 				{
       
   904 				test.Printf(_L("iteration %d failed - non-memory error in OOM test: %d\n"), failAt, ret);
       
   905 				TESTTRUEL(ret == KErrNoMemory);
       
   906 				}
       
   907 
       
   908 			TESTNOERRL(CheckDatabaseIsAbsentOrOpenable(ETrue));
       
   909 			}
       
   910 		}
       
   911 
       
   912 	//cleanup
       
   913 	if(CContactDatabase::DefaultContactDatabaseExistsL())
       
   914 		{
       
   915 		CContactDatabase::DeleteDefaultFileL();
       
   916 		}
       
   917 
       
   918 	test.Printf(_L("\n"));
       
   919 	return ETrue;
       
   920 	}
       
   921 
       
   922 LOCAL_C TBool OpenLOOM_ClientSideL()
       
   923 	{
       
   924 	test.Next(_L("OpenL client side OOM test.\n"));
       
   925 
       
   926 	// Create a default database.
       
   927 	CContactDatabase* dBase = NULL;
       
   928 	TRAPD( err, dBase = CContactDatabase::CreateL());
       
   929 	// need the file to be present to open it
       
   930 	TESTTRUEL((err == KErrNone) || (err == KErrAlreadyExists));
       
   931 	if (err == KErrNone)
       
   932 		{
       
   933 		delete dBase;
       
   934 		}		
       
   935 	
       
   936 	TInt ret = KErrNoMemory;
       
   937 	TInt failAt = 0;
       
   938 	while(ret != KErrNone )
       
   939 		{
       
   940 		failAt++;
       
   941 		if( !(failAt % 5 ))
       
   942 			{
       
   943 			test.Printf(_L("."));
       
   944 			}
       
   945 
       
   946 		__UHEAP_SETFAIL(RHeap::EDeterministic, failAt);
       
   947 		__UHEAP_MARK;
       
   948 		TRAP(ret, dBase = CContactDatabase::OpenL());
       
   949 		if( ret == KErrNone ) 
       
   950 			{
       
   951 			delete dBase;
       
   952 			}
       
   953 		__UHEAP_MARKEND;
       
   954 		__UHEAP_RESET;
       
   955 
       
   956 		if	(ret != KErrNoMemory && ret != KErrNone)
       
   957 			{
       
   958 			test.Printf(_L("iteration %d failed - non-memory error in OOM test: %d\n"), failAt, ret);
       
   959 			TESTTRUEL((ret == KErrNoMemory || ret == KErrNone));
       
   960 			}
       
   961 			
       
   962 		}
       
   963 	test.Printf(_L("\n"));
       
   964 	// Remove the default file we just opened.
       
   965 	TRAP( ret, CContactDatabase::DeleteDefaultFileL());
       
   966 	
       
   967 	return ETrue;
       
   968 	}
       
   969 
       
   970 LOCAL_C TBool ListDatabasesLOOM_ClientSideL()
       
   971 	{
       
   972 	test.Next(_L("ListDatabasesL client side OOM test.\n"));
       
   973 
       
   974 	CDesCArray* driveFiles = NULL;
       
   975 	TInt ret = KErrNoMemory;
       
   976 	TInt failAt = 0;
       
   977 	while(ret != KErrNone )
       
   978 		{
       
   979 		failAt++;
       
   980 		if( !(failAt % 5 ))
       
   981 			{
       
   982 			test.Printf(_L("."));
       
   983 			}
       
   984 
       
   985 		__UHEAP_SETFAIL(RHeap::EDeterministic, failAt);
       
   986 		__UHEAP_MARK;
       
   987 		TRAP(ret, driveFiles = CContactDatabase::ListDatabasesL());
       
   988 		if( ret == KErrNone )
       
   989 			{
       
   990 			delete driveFiles;
       
   991 			}
       
   992 		__UHEAP_MARKEND;
       
   993 		__UHEAP_RESET;
       
   994 
       
   995 		if	(ret != KErrNoMemory && ret != KErrNone)
       
   996 			{
       
   997 			test.Printf(_L("iteration %d failed - non-memory error in OOM test: %d\n"), failAt, ret);
       
   998 			TESTTRUEL((ret == KErrNoMemory || ret == KErrNone));
       
   999 			}
       
  1000 		}
       
  1001 	test.Printf(_L("\n"));
       
  1002 	return ETrue;
       
  1003 	}
       
  1004 
       
  1005 #ifdef _DEBUG
       
  1006 LOCAL_C TBool CreateLOOM_ServerSideL()
       
  1007 	{
       
  1008 	test.Next(_L("CreateL Server side OOM test.\n"));
       
  1009 
       
  1010 	_LIT(KTemporaryFile,"C:TempFile");
       
  1011 
       
  1012 	CContactDatabase* dBase = NULL;
       
  1013 	CContactDatabase* newDataBase = NULL;
       
  1014 
       
  1015 	// First create an instance of CContactDatabase to be used to call
       
  1016 	// Server side heap methods.
       
  1017 	TRAPD( err, dBase = CContactDatabase::ReplaceL(KTemporaryFile));
       
  1018 	// need the file to be present for server connection
       
  1019 	TESTTRUEL((err == KErrNone) && (dBase != NULL));
       
  1020 	CleanupStack::PushL(dBase);
       
  1021 
       
  1022 	TRAP_IGNORE(CContactDatabase::DeleteDefaultFileL());
       
  1023 
       
  1024 	TInt allocCells(0);	
       
  1025  	TInt ret = KErrNoMemory;
       
  1026 	TInt failAt = 0;
       
  1027 	
       
  1028 	while(ret != KErrNone)
       
  1029 		{
       
  1030 		failAt++;
       
  1031 
       
  1032 		if( !(failAt % 5 ))
       
  1033 			{
       
  1034 			test.Printf(_L("%d,"), failAt);
       
  1035 			}
       
  1036 
       
  1037 		dBase->SetCntServerHeapFailure(RHeap::EDeterministic, failAt);
       
  1038 		allocCells = dBase->CntServerResourceCount();    //__UHEAP_MARK;
       
  1039 		
       
  1040 		TRAP(ret, newDataBase = CContactDatabase::CreateL());
       
  1041 		if( ret == KErrNone )
       
  1042 			{
       
  1043 			delete newDataBase;
       
  1044 			}
       
  1045 			
       
  1046 		dBase->SetCntServerHeapFailure(RHeap::ENone, 1);
       
  1047 
       
  1048 		if (ret != KErrNone)
       
  1049 			{
       
  1050 			if	(ret == KErrNoMemory)
       
  1051 				{
       
  1052 				TInt allocLatest = dBase->CntServerResourceCount();
       
  1053 				if (allocLatest != allocCells)
       
  1054 					{
       
  1055 				#ifdef __SYMBIAN_CNTMODEL_USE_SQLITE__	
       
  1056 					if(allocLatest != allocCells -1)
       
  1057 				#endif	
       
  1058 						{		
       
  1059 						test.Printf(_L("iteraton %d failed - initial alloc cells = %d, alloc Latest = %d\n"), 
       
  1060 							failAt, allocCells, allocLatest);
       
  1061 						TESTVALUEL(allocLatest, allocCells); //__UHEAP_MARKEND;											
       
  1062 						}
       
  1063 					}
       
  1064 				}
       
  1065 			else
       
  1066 				{
       
  1067 				test.Printf(_L("iteration %d failed - non-memory error in OOM test: %d\n"), failAt, ret);
       
  1068 				TESTTRUEL((ret == KErrNoMemory || ret == KErrNone));
       
  1069 				}
       
  1070 			
       
  1071 			TESTNOERRL(CheckDatabaseIsAbsentOrOpenable(ETrue));
       
  1072 			}
       
  1073 		}
       
  1074 
       
  1075 	CleanupStack::PopAndDestroy(dBase);
       
  1076 	// This test has created two files; a default and KTemporaryFile
       
  1077 	// both must be deleted.
       
  1078 	TRAP( ret, CContactDatabase::DeleteDefaultFileL());
       
  1079 	TRAP( ret, CContactDatabase::DeleteDatabaseL( KTemporaryFile ));
       
  1080 
       
  1081 	return ETrue;
       
  1082 	}
       
  1083 
       
  1084 LOCAL_C TBool DeleteDatabaseLOOM_ServerSideL()
       
  1085 	{
       
  1086 	test.Next(_L("DeleteDatabaseL Server side OOM test.\n"));
       
  1087 
       
  1088 	_LIT(KFileToDelete,"c:FileToDelete");
       
  1089 	CContactDatabase* dBase = NULL;
       
  1090 	CContactDatabase* dBaseForFile = NULL;
       
  1091 
       
  1092 	//
       
  1093 	// create db file for delete test
       
  1094 	TRAPD( err, dBaseForFile = CContactDatabase::CreateL(KFileToDelete));
       
  1095 	TESTTRUEL((err == KErrNone) || (err == KErrAlreadyExists));
       
  1096 	if (err == KErrNone)
       
  1097 		{
       
  1098 		// Close the file
       
  1099 		delete dBaseForFile;
       
  1100 		}
       
  1101 
       
  1102 	// Create Default dBase to be used by ServerSide OOM Calls.
       
  1103 	TRAP( err, dBase = CContactDatabase::ReplaceL());
       
  1104 	// need the file to be present for server connection
       
  1105 	TESTTRUEL(err == KErrNone);
       
  1106 	CleanupStack::PushL(dBase);
       
  1107 
       
  1108 
       
  1109 	TInt allocCells(0);
       
  1110  	TInt ret = KErrNoMemory;
       
  1111 	TInt failAt = 0;
       
  1112 	while(ret != KErrNone)
       
  1113 		{
       
  1114 		failAt++;
       
  1115 		if( !(failAt % 5 ))
       
  1116 			{
       
  1117 			test.Printf(_L("."));
       
  1118 			}
       
  1119 		dBase->SetCntServerHeapFailure(RHeap::EDeterministic, failAt);
       
  1120 		allocCells = dBase->CntServerResourceCount();    //__UHEAP_MARK;
       
  1121 
       
  1122 		TRAP(ret, CContactDatabase::DeleteDatabaseL(KFileToDelete));
       
  1123 
       
  1124 		//__SRVHEAP_RESET;
       
  1125 		dBase->SetCntServerHeapFailure(RHeap::ENone, 1);
       
  1126 
       
  1127 		if	(ret == KErrNoMemory)
       
  1128 			{
       
  1129 			TInt allocLatest = dBase->CntServerResourceCount();
       
  1130 
       
  1131 			if (allocLatest != allocCells)
       
  1132 				{
       
  1133 				test.Printf(_L("iteraton %d failed - initial alloc cells = %d, alloc Latest = %d\n"), 
       
  1134 					failAt, allocCells, allocLatest);
       
  1135 				TESTVALUEL(allocLatest, allocCells); //__UHEAP_MARKEND;
       
  1136 				}
       
  1137 			}
       
  1138 		else if ( ret != KErrNone )
       
  1139 			{
       
  1140 			test.Printf(_L("iteration %d failed - non-memory error in OOM test: %d\n"), failAt, ret);
       
  1141 			TESTTRUEL((ret == KErrNoMemory || ret == KErrNone));
       
  1142 			}
       
  1143 		}
       
  1144 	CleanupStack::PopAndDestroy(dBase);
       
  1145 	// Delete default file.
       
  1146 	TRAP(ret, CContactDatabase::DeleteDefaultFileL());
       
  1147 	
       
  1148 	return ETrue;
       
  1149 	}
       
  1150 
       
  1151 LOCAL_C TBool DefaultContactDatabaseExistsLOOM_ServerSideL()
       
  1152 	{
       
  1153 	test.Next(_L("DefaultContactDatabaseExistsL Server side OOM test.\n"));
       
  1154 
       
  1155 	CContactDatabase* dBase = NULL;
       
  1156 
       
  1157 	TRAPD( err, dBase = CContactDatabase::ReplaceL());
       
  1158 	// need the file to be present for server connection
       
  1159 	TESTTRUEL((err == KErrNone) && (dBase != NULL));
       
  1160 	CleanupStack::PushL(dBase);
       
  1161 
       
  1162 	TInt allocCells(0);
       
  1163  	TInt ret = KErrNoMemory;
       
  1164 	TInt failAt = 0;
       
  1165 	while(ret != KErrNone)
       
  1166 		{
       
  1167 		failAt++;
       
  1168 		if( !(failAt % 5 ))
       
  1169 			{
       
  1170 			test.Printf(_L("."));
       
  1171 			}
       
  1172 		dBase->SetCntServerHeapFailure(RHeap::EDeterministic, failAt);
       
  1173 		allocCells = dBase->CntServerResourceCount();    //__UHEAP_MARK;
       
  1174 
       
  1175 		TRAP(ret, CContactDatabase::DefaultContactDatabaseExistsL());
       
  1176 
       
  1177 		//__SRVHEAP_RESET;
       
  1178 		dBase->SetCntServerHeapFailure(RHeap::ENone, 1);
       
  1179 
       
  1180 		if	(ret == KErrNoMemory)
       
  1181 			{
       
  1182 			TInt allocLatest = dBase->CntServerResourceCount();
       
  1183 
       
  1184 			if (allocLatest != allocCells)
       
  1185 				{
       
  1186 				test.Printf(_L("iteraton %d failed - initial alloc cells = %d, alloc Latest = %d\n"), 
       
  1187 					failAt, allocCells, allocLatest);
       
  1188 				TESTVALUEL(allocLatest, allocCells); //__UHEAP_MARKEND; 
       
  1189 				}
       
  1190 
       
  1191 			}
       
  1192 		else if ( ret != KErrNone )
       
  1193 			{
       
  1194 			test.Printf(_L("iteration %d failed - non-memory error in OOM test: %d\n"), failAt, ret);
       
  1195 			TESTTRUEL((ret == KErrNoMemory || ret == KErrNone)); //Bad leaving code
       
  1196 			}
       
  1197 		}
       
  1198 	CleanupStack::PopAndDestroy(dBase);
       
  1199 	// Check to see that database has been removed.
       
  1200 	TRAP_IGNORE(CContactDatabase::DeleteDefaultFileL());
       
  1201 
       
  1202 	return ETrue;
       
  1203 	}
       
  1204 	
       
  1205 
       
  1206 LOCAL_C TBool OpenLOOM_ServerSideL()
       
  1207 	{
       
  1208 	test.Next(_L("OpenL Server side OOM test.\n"));
       
  1209 
       
  1210 	CContactDatabase* dBase = NULL;
       
  1211 	CContactDatabase* dBaseToOpen = NULL;
       
  1212 
       
  1213 	// Create the default contacts file.
       
  1214 	TRAPD(err, dBase = CContactDatabase::ReplaceL());
       
  1215 	// need the file to be present for server connection
       
  1216 	TESTTRUEL((err == KErrNone) && (dBase != NULL));
       
  1217 	CleanupStack::PushL(dBase);
       
  1218 
       
  1219 	TInt allocCells(0);
       
  1220  	TInt ret = KErrNoMemory;
       
  1221 	TInt failAt = 0;
       
  1222 	while(ret != KErrNone)
       
  1223 		{
       
  1224 		failAt++;
       
  1225 		if( !(failAt % 5 ))
       
  1226 			{
       
  1227 			test.Printf(_L("."));
       
  1228 			}
       
  1229 		dBase->SetCntServerHeapFailure(RHeap::EDeterministic, failAt);
       
  1230 		allocCells = dBase->CntServerResourceCount();    //__UHEAP_MARK;
       
  1231 
       
  1232 		TRAP(ret, dBaseToOpen = CContactDatabase::OpenL());
       
  1233 		if( ret == KErrNone )
       
  1234 			{
       
  1235 			delete dBaseToOpen;
       
  1236 			}
       
  1237 
       
  1238 		//__SRVHEAP_RESET;
       
  1239 		dBase->SetCntServerHeapFailure(RHeap::ENone, 1);
       
  1240 
       
  1241 		if	(ret == KErrNoMemory)
       
  1242 			{
       
  1243 			TInt allocLatest = dBase->CntServerResourceCount();
       
  1244 
       
  1245 			if (allocLatest != allocCells)
       
  1246 				{
       
  1247 			#ifdef __SYMBIAN_CNTMODEL_USE_SQLITE__	
       
  1248 				if(allocLatest != allocCells + 1)
       
  1249 			#endif	
       
  1250 					{
       
  1251 					test.Printf(_L("iteraton %d failed - initial alloc cells = %d, alloc Latest = %d\n"), 
       
  1252 							failAt, allocCells, allocLatest);
       
  1253 					TESTVALUEL(allocLatest, allocCells); //__UHEAP_MARKEND;	
       
  1254 					}
       
  1255 				}
       
  1256 			}
       
  1257 		else if ( ret != KErrNone )
       
  1258 			{
       
  1259 			test.Printf(_L("iteration %d failed - non-memory error in OOM test: %d\n"), failAt, ret);
       
  1260 			TESTTRUEL((ret == KErrNoMemory || ret == KErrNone));
       
  1261 			}
       
  1262 		
       
  1263 		}
       
  1264 	CleanupStack::PopAndDestroy(dBase);
       
  1265 	// Remove the default.
       
  1266 	TRAP(ret, CContactDatabase::DeleteDefaultFileL());
       
  1267 	
       
  1268 	return ETrue;
       
  1269 	}
       
  1270 
       
  1271 LOCAL_C TBool ContactDatabaseExistsLOOM_ServerSideL()
       
  1272 	{
       
  1273 	test.Next(_L("ContactDatabaseExistsL Server side OOM test.\n"));
       
  1274 
       
  1275 	_LIT(KCntFileToExist,"C:FileToDetect");
       
  1276 
       
  1277 	CContactDatabase* dBase = NULL;
       
  1278 	// Create the named contacts file.
       
  1279 	TRAPD(err, dBase = CContactDatabase::ReplaceL(KCntFileToExist));
       
  1280 	// need the file to be present for server connection
       
  1281 	TESTTRUEL((err == KErrNone) && (dBase != NULL));
       
  1282 	CleanupStack::PushL(dBase);
       
  1283 
       
  1284 	TInt allocCells(0);
       
  1285  	TInt ret = KErrNoMemory;
       
  1286 	TInt failAt = 0;
       
  1287 	while(ret != KErrNone)
       
  1288 		{
       
  1289 		failAt++;
       
  1290 		if( !(failAt % 5 ))
       
  1291 			{
       
  1292 			test.Printf(_L("."));
       
  1293 			}
       
  1294 		dBase->SetCntServerHeapFailure(RHeap::EDeterministic, failAt);
       
  1295 		allocCells = dBase->CntServerResourceCount();    //__UHEAP_MARK;
       
  1296 		TRAP(ret, CContactDatabase::ContactDatabaseExistsL(KCntFileToExist));
       
  1297 
       
  1298 		//__SRVHEAP_RESET;
       
  1299 		dBase->SetCntServerHeapFailure(RHeap::ENone, 1);
       
  1300 
       
  1301 		if	(ret == KErrNoMemory)
       
  1302 			{
       
  1303 			TInt allocLatest = dBase->CntServerResourceCount();
       
  1304 
       
  1305 			if (allocLatest != allocCells)
       
  1306 				{
       
  1307 				test.Printf(_L("iteraton %d failed - initial alloc cells = %d, alloc Latest = %d\n"), 
       
  1308 					failAt, allocCells, allocLatest);
       
  1309 				TESTVALUEL(allocLatest, allocCells); //__UHEAP_MARKEND; 
       
  1310 				}
       
  1311 			}
       
  1312 		else if ( ret != KErrNone )
       
  1313 			{
       
  1314 			test.Printf(_L("iteration %d failed - non-memory error in OOM test: %d\n"), failAt, ret);
       
  1315 			TESTTRUEL((ret == KErrNoMemory || ret == KErrNone));//Bad leaving code
       
  1316 			}
       
  1317 		}
       
  1318 	CleanupStack::PopAndDestroy(dBase);
       
  1319 	// Remove the default.
       
  1320 	TRAP(ret, CContactDatabase::DeleteDatabaseL(KCntFileToExist));
       
  1321 
       
  1322 	return ETrue;
       
  1323 	}
       
  1324 
       
  1325 LOCAL_C TBool ReplaceLOOM_ServerSideL()
       
  1326 	{
       
  1327 	test.Next(_L("ReplaceL Server side OOM test.\n"));
       
  1328 	_LIT(KCntFileToExist,"C:FileToDetect");
       
  1329 	CContactDatabase* dBase = NULL;
       
  1330 	CContactDatabase* dBaseReplace = NULL;
       
  1331 
       
  1332 	// Create the named contacts file.
       
  1333 	TRAPD(err, dBase = CContactDatabase::ReplaceL(KCntFileToExist));
       
  1334 	// need the file to be present for server connection
       
  1335 	TESTTRUEL((err == KErrNone) && (dBase != NULL));
       
  1336 	CleanupStack::PushL(dBase);
       
  1337 
       
  1338 	TInt allocCells(0);
       
  1339  	TInt ret = KErrNoMemory;
       
  1340 	TInt failAt = 0;
       
  1341 	while(ret != KErrNone)
       
  1342 		{
       
  1343 		failAt++;
       
  1344 		if( !(failAt % 5 ))
       
  1345 			{
       
  1346 			test.Printf(_L("."));
       
  1347 			}
       
  1348 		dBase->SetCntServerHeapFailure(RHeap::EDeterministic, failAt);
       
  1349 		allocCells = dBase->CntServerResourceCount();    //__UHEAP_MARK;
       
  1350 
       
  1351 		TRAP(ret, dBaseReplace = CContactDatabase::ReplaceL());
       
  1352 		if(ret == KErrNone)
       
  1353 			{
       
  1354 			delete dBaseReplace;
       
  1355 			}
       
  1356 
       
  1357 		//__SRVHEAP_RESET;
       
  1358 		dBase->SetCntServerHeapFailure(RHeap::ENone, 1);
       
  1359 
       
  1360 		if (ret != KErrNone)
       
  1361 			{
       
  1362 			if(ret != KErrNoMemory)
       
  1363 				{
       
  1364 				test.Printf(_L("iteration %d failed - non-memory error in OOM test: %d\n"), failAt, ret);
       
  1365 				TESTTRUEL((ret == KErrNoMemory || ret == KErrNone));
       
  1366 				}
       
  1367 			else
       
  1368 				{
       
  1369 				TInt allocLatest = dBase->CntServerResourceCount();
       
  1370 
       
  1371 				if (allocLatest != allocCells)
       
  1372 					{
       
  1373 				#ifdef __SYMBIAN_CNTMODEL_USE_SQLITE__	
       
  1374 					if(allocLatest != allocCells - 1)
       
  1375 				#endif	
       
  1376 						{
       
  1377 						test.Printf(_L("iteraton %d failed - initial alloc cells = %d, alloc Latest = %d\n"), 
       
  1378 									failAt, allocCells, allocLatest);
       
  1379 						TESTVALUEL(allocLatest, allocCells); //__UHEAP_MARKEND; 							
       
  1380 						}
       
  1381 					}
       
  1382 				}
       
  1383 
       
  1384 			TESTNOERRL(CheckDatabaseIsAbsentOrOpenable());
       
  1385 			}
       
  1386 
       
  1387 
       
  1388 		}
       
  1389 	CleanupStack::PopAndDestroy(dBase);
       
  1390 
       
  1391 	// Remove the default file associated with the dBase.
       
  1392 	TRAP(ret, CContactDatabase::DeleteDatabaseL(KCntFileToExist));
       
  1393 
       
  1394 	return ETrue;
       
  1395 	}
       
  1396 	
       
  1397 LOCAL_C TBool DeleteDefaultFileLOOM_ServerSideL()
       
  1398 	{
       
  1399 	test.Next(_L("DeleteDefaultFileL Server side OOM test.\n"));
       
  1400 	_LIT(KCntFileToExist,"C:FileToDetect");
       
  1401 
       
  1402 	CContactDatabase* dBase = NULL;
       
  1403 	CContactDatabase* dBaseDefaultFile = NULL;
       
  1404 	// ensure default contacts file is created
       
  1405 	TRAPD(err, dBaseDefaultFile = CContactDatabase::CreateL());
       
  1406 	// need file to be created for delete test
       
  1407 	TESTTRUEL( (err == KErrNone) || (err == KErrAlreadyExists))
       
  1408 	if (err == KErrNone)
       
  1409 		{
       
  1410 		delete dBaseDefaultFile;
       
  1411 		}
       
  1412 
       
  1413 	// Create the named contacts file.
       
  1414 	TRAP(err, dBase = CContactDatabase::ReplaceL(KCntFileToExist));
       
  1415 	// need the file to be present for server connection
       
  1416 	TESTTRUEL(err == KErrNone);
       
  1417 	CleanupStack::PushL(dBase);
       
  1418 
       
  1419 	TInt allocCells(0);
       
  1420  	TInt ret = KErrNoMemory;
       
  1421 	TInt failAt = 0;
       
  1422 	while(ret != KErrNone)
       
  1423 		{
       
  1424 		failAt++;
       
  1425 		if( !(failAt % 5 ))
       
  1426 			{
       
  1427 			test.Printf(_L("."));
       
  1428 			}
       
  1429 		dBase->SetCntServerHeapFailure(RHeap::EDeterministic, failAt);
       
  1430 		allocCells = dBase->CntServerResourceCount();    //__UHEAP_MARK;
       
  1431 
       
  1432 		TRAP(ret, CContactDatabase::DeleteDefaultFileL());
       
  1433 
       
  1434 		//__SRVHEAP_RESET;
       
  1435 		dBase->SetCntServerHeapFailure(RHeap::ENone, 1);			
       
  1436 
       
  1437 		if	(ret == KErrNoMemory)
       
  1438 			{
       
  1439 			TInt allocLatest = dBase->CntServerResourceCount();
       
  1440 
       
  1441 			if (allocLatest != allocCells)
       
  1442 				{
       
  1443 				test.Printf(_L("iteraton %d failed - initial alloc cells = %d, alloc Latest = %d\n"), 
       
  1444 					failAt, allocCells, allocLatest);
       
  1445 				TESTVALUEL(allocLatest, allocCells); //__UHEAP_MARKEND; 
       
  1446 				}
       
  1447 			}
       
  1448 		else if ( ret != KErrNone )
       
  1449 			{
       
  1450 			test.Printf(_L("iteration %d failed - non-memory error in OOM test: %d\n"), failAt, ret);
       
  1451 			TESTTRUEL((ret == KErrNoMemory || ret == KErrNone));
       
  1452 			}
       
  1453 		}
       
  1454 	CleanupStack::PopAndDestroy(dBase);
       
  1455 	
       
  1456 	// Remove the default file associated with the dBase.
       
  1457 	// Ensure that the default File has been removed.
       
  1458 	TBool dExists = EFalse;
       
  1459 	TRAP(ret, dExists = CContactDatabase::DefaultContactDatabaseExistsL());
       
  1460 	TESTNOERRL(ret);
       
  1461 	TRAP_IGNORE(CContactDatabase::DeleteDatabaseL(KCntFileToExist));
       
  1462 
       
  1463 	return !dExists;
       
  1464 	}
       
  1465 	
       
  1466 LOCAL_C TBool GetDefaultNameLOOM_ServerSideL()
       
  1467 	{
       
  1468 	test.Next(_L("GetDefaultNameL Server side OOM test.\n"));
       
  1469 
       
  1470 	CContactDatabase* dBase = NULL;
       
  1471 	// Create the default contacts file.
       
  1472 	TRAPD(err, dBase = CContactDatabase::ReplaceL());
       
  1473 	// need the file to be present
       
  1474 	TESTTRUEL((err == KErrNone) && (dBase != NULL));
       
  1475 	CleanupStack::PushL(dBase);
       
  1476 
       
  1477 
       
  1478 	TInt allocCells(0);
       
  1479  	TInt ret = KErrNoMemory;
       
  1480 	TInt failAt = 0;
       
  1481 
       
  1482 	TFileName filename;
       
  1483 	while(ret != KErrNone)
       
  1484 		{
       
  1485 		failAt++;
       
  1486 		if( !(failAt % 5 ))
       
  1487 			{
       
  1488 			test.Printf(_L("."));
       
  1489 			}
       
  1490 		dBase->SetCntServerHeapFailure(RHeap::EDeterministic, failAt);
       
  1491 		allocCells = dBase->CntServerResourceCount();    //__UHEAP_MARK;
       
  1492 
       
  1493 		TRAP(ret, CContactDatabase::GetDefaultNameL(filename));
       
  1494 
       
  1495 		//__SRVHEAP_RESET;
       
  1496 		dBase->SetCntServerHeapFailure(RHeap::ENone, 1);		
       
  1497 
       
  1498 		if	(ret == KErrNoMemory)
       
  1499 			{
       
  1500 			TInt allocLatest = dBase->CntServerResourceCount();
       
  1501 
       
  1502 			if (allocLatest != allocCells)
       
  1503 				{
       
  1504 				test.Printf(_L("iteraton %d failed - initial alloc cells = %d, alloc Latest = %d\n"), 
       
  1505 					failAt, allocCells, allocLatest);
       
  1506 				TESTVALUEL(allocLatest, allocCells); //__UHEAP_MARKEND;
       
  1507 				}
       
  1508 			}
       
  1509 		else if ( ret != KErrNone )
       
  1510 			{
       
  1511 			test.Printf(_L("iteration %d failed - non-memory error in OOM test: %d\n"), failAt, ret);
       
  1512 			TESTTRUEL((ret == KErrNoMemory || ret == KErrNone)); //Bad leaving code
       
  1513 			}
       
  1514 		}
       
  1515 	CleanupStack::PopAndDestroy(dBase);
       
  1516 	TRAP_IGNORE(CContactDatabase::DeleteDefaultFileL());
       
  1517 	return ETrue;
       
  1518 	}
       
  1519 #endif
       
  1520 
       
  1521 
       
  1522 LOCAL_C void OOMTestingL()
       
  1523 	{
       
  1524 	
       
  1525 	// Client side OOM Testing.
       
  1526 	TESTTRUE(ListDatabasesLOOM_ClientSideL());
       
  1527 	TESTTRUE(OpenLOOM_ClientSideL());
       
  1528 	TESTTRUE(DeleteDatabaseLOOM_ClientSideL());
       
  1529 	TESTTRUE(CreateLOOM_ClientSideL());
       
  1530 	TESTTRUE(GetDefaultNameLOOM_ClientSideL());
       
  1531 	TESTTRUE(DefaultContactDatabaseExistsLOOM_ClientSideL());
       
  1532 	TESTTRUE(ContactDatabaseExistsLOOM_ClientSideL());
       
  1533 	TESTTRUE(ReplaceLOOM_ClientSideL());
       
  1534 	TESTTRUE(DeleteDefaultFileLOOM_ClientSideL());
       
  1535 
       
  1536 #ifdef _DEBUG
       
  1537 	// Server side OOM testing. Only server side debug method are
       
  1538 	// available in debug mode.
       
  1539 
       
  1540 	TESTTRUE(CreateLOOM_ServerSideL());
       
  1541 	
       
  1542 	TESTTRUE(DeleteDatabaseLOOM_ServerSideL());
       
  1543 	TESTTRUE(DefaultContactDatabaseExistsLOOM_ServerSideL());
       
  1544 	TESTTRUE(OpenLOOM_ServerSideL());
       
  1545 	TESTTRUE(ContactDatabaseExistsLOOM_ServerSideL());
       
  1546 	TESTTRUE(ReplaceLOOM_ServerSideL());
       
  1547 	TESTTRUE(DeleteDefaultFileLOOM_ServerSideL());
       
  1548 	TESTTRUE(GetDefaultNameLOOM_ServerSideL());
       
  1549 #endif
       
  1550 
       
  1551 	}
       
  1552 
       
  1553 /**
       
  1554 	NegTestL
       
  1555 	
       
  1556 	Intended to identify the failing cases of the API and ensure that they behave
       
  1557 	carefully under these conditions.
       
  1558 	
       
  1559 	This section of the test needs more coverage.
       
  1560 */
       
  1561 LOCAL_C void NegTestL()
       
  1562 	{
       
  1563 	TInt err (KErrNone);
       
  1564 
       
  1565 	test.Next(_L("Test drive Setting and Getting\n"));
       
  1566 
       
  1567 		{
       
  1568 		TBool dbDriveSet;
       
  1569 		TDriveUnit driveUnit;
       
  1570 
       
  1571 		// Set the Current drive to C:
       
  1572 		TRAP( err, CContactDatabase::SetDatabaseDriveL(TDriveUnit(2), EFalse));
       
  1573 		TESTTRUEL( err == KErrNone );
       
  1574 
       
  1575 		// Read the current drive back, ensuring it has been changed correctly.
       
  1576 		dbDriveSet = CContactDatabase::DatabaseDrive(driveUnit);
       
  1577 		TESTTRUEL(dbDriveSet);
       
  1578 		TESTTRUEL(driveUnit == 2);
       
  1579 
       
  1580 		// Create and open an empty contact database
       
  1581 		TRAP(err, db = CContactDatabase::ReplaceL());
       
  1582 		TESTTRUEL( err == KErrNone && db != NULL);
       
  1583 		if( !err )
       
  1584 			{
       
  1585 			delete db;
       
  1586 			db = NULL;
       
  1587 			}
       
  1588 		err = KErrNone;
       
  1589 		TBool exists = EFalse;
       
  1590 		TRAP( err, exists = CContactDatabase::DefaultContactDatabaseExistsL());
       
  1591 		TESTTRUEL( exists && err == KErrNone );
       
  1592 
       
  1593 
       
  1594 		TRAP(err, CContactDatabase::DeleteDefaultFileL());
       
  1595 		TESTTRUEL( err == KErrNone);
       
  1596 
       
  1597 		TRAP( err, exists = CContactDatabase::DefaultContactDatabaseExistsL());
       
  1598 		TESTTRUEL( !exists && err == KErrNone );
       
  1599 
       
  1600 		// delete again should fail		
       
  1601 		TRAP(err, CContactDatabase::DeleteDefaultFileL());
       
  1602 		TESTTRUEL( err == KErrNotFound );
       
  1603 
       
  1604 		db = CContactDatabase::CreateL();
       
  1605 		TRAP( err, exists = CContactDatabase::DefaultContactDatabaseExistsL());
       
  1606 		TESTTRUEL( exists );
       
  1607 		delete db;
       
  1608 		db = NULL;
       
  1609 
       
  1610 		TRAP( err, CContactDatabase::SetDatabaseDriveL(TDriveUnit(5), EFalse));
       
  1611 		TESTTRUEL( err == KErrNone );
       
  1612 
       
  1613 		TRAP( err, dbDriveSet = CContactDatabase::DatabaseDrive(driveUnit));
       
  1614 		TESTTRUEL( err == KErrNone );
       
  1615 		TESTTRUEL( dbDriveSet );
       
  1616 		TESTTRUEL( driveUnit == 5 );
       
  1617 
       
  1618 		TRAP( err, CContactDatabase::SetDatabaseDriveL(TDriveUnit(2), EFalse));
       
  1619 		TESTTRUEL( err == KErrNone );
       
  1620 
       
  1621 		TRAP( err, dbDriveSet = CContactDatabase::DatabaseDrive(driveUnit));
       
  1622 		TESTTRUEL( dbDriveSet );
       
  1623 		TESTTRUEL( driveUnit == 2 );
       
  1624 		}
       
  1625 
       
  1626 	TRAP( err, db = CContactDatabase::OpenL());
       
  1627 	TESTTRUEL( err == KErrNone && db != NULL );
       
  1628 	delete db;
       
  1629 	db = NULL;
       
  1630 
       
  1631 	test.Next(_L("Test database opening\n"));
       
  1632 		{
       
  1633 		// open twice
       
  1634 		db = CContactDatabase::OpenL();
       
  1635 		TESTTRUEL(db != NULL);
       
  1636 		CContactDatabase* db2 = CContactDatabase::OpenL();
       
  1637 		TESTTRUEL(db2 != NULL);
       
  1638 		TESTTRUEL(db2 != db);
       
  1639 		delete db;
       
  1640 		db = NULL;
       
  1641 		delete db2;
       
  1642 		db2 =NULL;
       
  1643 
       
  1644 		// KNullDesC is used internally, but is not a valid parameter
       
  1645 		TRAP(err, db = CContactDatabase::OpenL(KNullDesC));
       
  1646 		TESTTRUEL(err == KErrBadName);
       
  1647 		TESTTRUEL(db == NULL);
       
  1648 		
       
  1649 		// KNullDesC is used internally, but is not a valid parameter
       
  1650 		TRAP(err, db = CContactDatabase::ReplaceL(KNullDesC));
       
  1651 		TESTTRUEL(err == KErrBadName);
       
  1652 		TESTTRUEL(db == NULL);
       
  1653 
       
  1654 		// check for Open and Create Failing
       
  1655 		CContactDatabase::DeleteDefaultFileL();
       
  1656 
       
  1657 		TRAP(err, db = CContactDatabase::OpenL());
       
  1658 		TESTTRUEL(err == KErrNotFound);
       
  1659 		TESTTRUEL(db == NULL);
       
  1660 
       
  1661 		TRAP(err, db = CContactDatabase::CreateL());
       
  1662 		TESTTRUEL(err == KErrNone);
       
  1663 		delete db;
       
  1664 		db = NULL;
       
  1665 
       
  1666 		TRAP(err, db = CContactDatabase::CreateL());
       
  1667 		TESTTRUEL(err == KErrAlreadyExists);
       
  1668 		TESTTRUEL(db == NULL);
       
  1669 
       
  1670 		// check for Replace replacing!
       
  1671 		TRAP(err, db = CContactDatabase::ReplaceL());
       
  1672 		TESTTRUEL(err == KErrNone);
       
  1673 		TESTTRUEL(db != NULL);
       
  1674 		delete db;
       
  1675 		db = NULL;
       
  1676 
       
  1677 		// wait for server to shutdown (6s)
       
  1678 		User::After(6000000);
       
  1679 
       
  1680 		// open twice
       
  1681 		db = CContactDatabase::OpenL();
       
  1682 		db2 = CContactDatabase::OpenL();
       
  1683 		delete db;
       
  1684 		db = NULL;
       
  1685 		delete db2;
       
  1686 		db2 =NULL;
       
  1687 		}
       
  1688 	}
       
  1689 
       
  1690 /**
       
  1691 	FunctionTestL
       
  1692 	
       
  1693 	Excersises: ListDatabasesL varients over 2 drives; C & F
       
  1694 	SetDatabaseDriveL, DatabaseDrive, GetDefaultNameL, 
       
  1695 	the varients of DefaultContactDatabaseExistsL, CreateL, 
       
  1696 	asynchronous OpenL, FindContactFile, DeleteDefaultFileL and 
       
  1697 	DeleteDatabaseL.
       
  1698 */
       
  1699 LOCAL_C void FunctionTestL()
       
  1700 	{
       
  1701 
       
  1702 	TFileName addedFile;
       
  1703 	test.Next(_L("FunctionTestL CContactDatabase::ListDatabasesL over multiple drives.\n"));
       
  1704 	// At this point there should only be 1 file which is the default.
       
  1705 	TFileName filename, defaultFilename;
       
  1706 
       
  1707 
       
  1708 	for(driveIdx =0;driveIdx<NoDrives; driveIdx++ )
       
  1709 		{
       
  1710 		addedFile.Copy(driveArray[driveIdx].Name());
       
  1711 		addedFile.Append(KNewCntTestFile);
       
  1712 		Tidy();
       
  1713 
       
  1714 		CDesCArray* cntFiles = NULL;
       
  1715 		TRAPD(err, cntFiles = CContactDatabase::ListDatabasesL());
       
  1716 		TESTTRUEL(err == KErrNone && cntFiles != NULL);
       
  1717   		NoDatabases = 0;
       
  1718   		if( !err )
       
  1719 	  		{
       
  1720 	  		NoDatabases = cntFiles->Count();
       
  1721 			delete cntFiles;				
       
  1722 	  		}
       
  1723 
       
  1724 
       
  1725   		// Check Set and Get default drive.
       
  1726 		TPtrC drvName(driveArray[driveIdx].Name());
       
  1727 		test.Printf(_L("Testing Drive %S\n"), &drvName);
       
  1728 		test.Next(_L("Setting Drive\n"));
       
  1729 		TRAP( err, CContactDatabase::SetDatabaseDriveL(driveArray[driveIdx], EFalse));
       
  1730 		if( err )
       
  1731 			{
       
  1732 			test.Printf(_L("CContactDatabase::SetDatabaseDriveL:Leave code:%d"),err);		
       
  1733 			}
       
  1734 		TDriveUnit myDriveUnit;
       
  1735 			{
       
  1736 			TESTTRUEL(CContactDatabase::DatabaseDrive(myDriveUnit));
       
  1737 			}
       
  1738 		TESTTRUEL(myDriveUnit == driveArray[driveIdx]);
       
  1739 
       
  1740 		// Steve. To see if Drive of default filename changes.
       
  1741 		TRAP( err, CContactDatabase::GetDefaultNameL( filename ));
       
  1742 		if( err )
       
  1743 			{
       
  1744 			test.Printf(_L("CContactDatabase::GetDefaultNameL:Leave code:%d"),err);
       
  1745 			}
       
  1746 		test.Next(_L("Checking default filename.\n"));
       
  1747 		TESTTRUEL(CheckDefaultContactsFilename(filename));
       
  1748 		defaultFilename = filename;
       
  1749 
       
  1750 		// Exercise all API's the look for Contacts files.
       
  1751 
       
  1752 
       
  1753 		TESTTRUEL(CheckCntFilesL( NoDatabases, KNoFiles, EFalse ));
       
  1754 		TESTTRUEL(CheckNoDatabaseFilesL());
       
  1755 
       
  1756 		// ReplaceL and OpenL //
       
  1757 
       
  1758 		test.Next(_L("Creating/Replacing default db: ReplaceL.\n"));
       
  1759 		TRAP( err, db = CContactDatabase::ReplaceL() );
       
  1760 		TESTTRUEL( err == KErrNone && db != NULL );
       
  1761 		if ((err != KErrNone) && (db == NULL))
       
  1762 			{
       
  1763 			test.Printf(_L("CContactDatabase::ReplaceL failed: leave code %d, db pointer %X"), err, db);
       
  1764 			}
       
  1765 		delete db;
       
  1766 		db = NULL;
       
  1767 		TESTTRUEL(CheckCntFilesL( NoDatabases + 1, defaultFilename ));
       
  1768 		TESTTRUEL(DefaultDatabaseL());
       
  1769 
       
  1770 
       
  1771 		__UHEAP_MARK;
       
  1772 		test.Next(_L("Opening Default: OpenL().\n"));
       
  1773 		TRAP( err, db = CContactDatabase::OpenL());
       
  1774 		if ((err != KErrNone) && (db == NULL))
       
  1775 			{
       
  1776 			test.Printf(_L("CContactDatabase::OpenL failed: leave code %d, db pointer %X"), err, db);
       
  1777 			}
       
  1778 		// these tests Leave, as the database needs to be open to continue
       
  1779 		TESTTRUEL( db != NULL );						// db should exist..
       
  1780 		TESTTRUEL( err == KErrNone);
       
  1781 
       
  1782 		// Add contacts to the db		
       
  1783 		CArrayFix<TContactItemId>* idList=new(ELeave) CArrayFixFlat<TContactItemId>(5);
       
  1784 		CleanupStack::PushL(idList);
       
  1785 		test.Next(_L("Add new contacts"));
       
  1786 		TRAP(err,AddNewContactsL(idList));
       
  1787 #ifdef __SYMBIAN_CNTMODEL_USE_SQLITE__
       
  1788 		TESTTRUEL( err == KErrNone || err == KErrDiskFull);
       
  1789 #else		
       
  1790 		TESTTRUEL( err == KErrNone );
       
  1791 #endif		
       
  1792 
       
  1793 		// Read and modify contacts.
       
  1794 		test.Next(_L("Read and modify contacts"));
       
  1795 		TRAP(err,EditContactsL(idList));
       
  1796 		TESTTRUEL( err == KErrNone );
       
  1797 
       
  1798 		CleanupStack::PopAndDestroy(idList);
       
  1799 
       
  1800 		delete db;
       
  1801 		db = NULL;
       
  1802 		__UHEAP_MARKEND;
       
  1803 
       
  1804 		//Now test opening another database, should succeed
       
  1805 		test.Next(_L("Creating addedFile: CreateL(addedFile).\n"));
       
  1806 		TRAP( err, db = CContactDatabase::CreateL(addedFile) );
       
  1807 		TESTTRUEL( err == KErrNone && db != NULL );
       
  1808 		// check that file with name filename exists.
       
  1809 		TESTTRUEL(CheckCntFilesL( NoDatabases + 2, addedFile ));
       
  1810 		TESTTRUEL(DefaultDatabaseL());
       
  1811 
       
  1812 		//Add test to check that the db is opened
       
  1813 		delete db;
       
  1814 		db = NULL;
       
  1815 
       
  1816 		// Ensure that RecreateSystemTemplateL does not leave
       
  1817 		test.Printf(_L("About to recreate template on:"));
       
  1818 		test.Printf(addedFile);
       
  1819 		
       
  1820 		test.Next(_L("Testing: RecreateSystemTemplateL( addedFile )"));
       
  1821 
       
  1822 		err = KErrNone;
       
  1823 		TRAP( err, CContactDatabase::RecreateSystemTemplateL( addedFile ));
       
  1824 #ifdef __SYMBIAN_CNTMODEL_USE_SQLITE__
       
  1825 		test.Printf(_L("Recieved %d"),err);
       
  1826 		TESTTRUEL( err == KErrNone || err == KErrDiskFull);
       
  1827 #else		
       
  1828 		TESTTRUEL( err == KErrNone );
       
  1829 #endif	
       
  1830 
       
  1831 		test.Next(_L("Opening file addedFile: OpenL( addedFile )\n"));
       
  1832 		TRAP( err, db = CContactDatabase::OpenL( addedFile ));
       
  1833 		TESTTRUEL( db != NULL );
       
  1834 		TESTTRUEL( err == KErrNone);
       
  1835 
       
  1836 		// Files existing now: Contacts.cdb & Contacts.cdrchar
       
  1837 		// File Deletion //
       
  1838 
       
  1839 		// So at this point we have a default db and another.
       
  1840 		// Time to start deleting them ! Start with DeleteDefaultFileL
       
  1841 		test.Next(_L("Deleting default file: DeleteDefaultFileL()\n"));
       
  1842 		TRAP( err, CContactDatabase::DeleteDefaultFileL());
       
  1843 		TESTTRUEL( err == KErrNone );
       
  1844 		// only filename should exist.
       
  1845 		TESTTRUEL(CheckCntFilesL( NoDatabases + 1, addedFile ));
       
  1846 		TESTTRUEL(CheckNotDefaultContactsFiles());
       
  1847 
       
  1848 		// Check that an open file is cannot be deleted.
       
  1849 		// There is one remaining database which we can now remove using DeleteDatabaseL
       
  1850 		test.Next(_L("Deleting database addedFile, should fail first time: DeleteDatabaseL(addedFile)\n"));
       
  1851 		TRAP(err, CContactDatabase::DeleteDatabaseL(addedFile));
       
  1852 		// Given that this file is currently open it DeleteDatabaseL should fail.
       
  1853 		TESTTRUEL(err == KErrInUse);
       
  1854 		delete db;
       
  1855 		db = NULL;
       
  1856 
       
  1857 		// We have just opened another we can now delete the one that was originally open.			
       
  1858 		test.Next(_L("Deleting database addedFile, should pass second time: DeleteDatabaseL(addedFile)\n"));
       
  1859 		TRAP(err, CContactDatabase::DeleteDatabaseL(addedFile));
       
  1860 		TESTTRUEL(err == KErrNone);
       
  1861 
       
  1862 		// Check that the file "filename" has been deleted and that 
       
  1863 		// the default file exists.
       
  1864 		TESTTRUEL(CheckCntFilesL( NoDatabases, KContactsFilename, EFalse )); 	// exercise ListDatabasesL
       
  1865 
       
  1866 		// NoFileOnPreviousDrive is used by CheckCntFilesL as once we start adding files
       
  1867 		// to the second drive, so ListDatabasesL() without a parameter returns the total on 
       
  1868 		// both drives.
       
  1869 		CDesCArray* driveFiles = CContactDatabase::ListDatabasesL(driveArray[driveIdx]);
       
  1870 		NoFileOnPreviousDrive = driveFiles->Count();
       
  1871 		delete driveFiles;
       
  1872 		}
       
  1873 	}
       
  1874 	
       
  1875 
       
  1876 LOCAL_C TBool EmptyCntDatabaseDeleteL()
       
  1877 	{
       
  1878 /**
       
  1879 @SYMTestCaseID PIM-T-SEC-CNTDBASE-DEF049719-0001
       
  1880 @SYMTestType UT
       
  1881 @SYMTestPriority High
       
  1882 @SYMDEF DEF049719
       
  1883 @SYMTestCaseDependencies CntModel CoreAppsTest
       
  1884 @SYMTestCaseDesc Check that a zero length CntDatabase file can be deleted using CContactDatabase::DeleteDatabaseL()
       
  1885 @SYMTestActions Create an empty file where the default contact database should be. 
       
  1886 Try to delete it using CContactDatabase::DeleteDefaultFileL().
       
  1887 Check that it has been deleted.
       
  1888 @SYMTestExpectedResults The empty contact database should be deleted.
       
  1889 */	
       
  1890 	test.Next(_L("@SYMTestCaseID:PIM-T-SEC-CNTDBASE-DEF049719-0001 Check zero length CntDatabase can be deleted in CContactDatabase::DeleteDatabaseL()"));
       
  1891 	TBool result(EFalse);
       
  1892 	// Create an empty file
       
  1893 	RFs fs;
       
  1894 	User::LeaveIfError(fs.Connect());
       
  1895 	RFile file;
       
  1896 	
       
  1897 	RPIMTestServer serv;
       
  1898         User::LeaveIfError(serv.Connect());
       
  1899 	serv.CreateEmptyFileL(KCdbPrivate);
       
  1900 	serv.Close();
       
  1901 	const TDesC& datab=KCdbSecure;
       
  1902 	
       
  1903 	file.Close();
       
  1904 	fs.Close();
       
  1905 
       
  1906 	// Delete the contact database
       
  1907 	TRAPD(err,CContactDatabase::DeleteDatabaseL(datab));
       
  1908 			
       
  1909 	result = (err == KErrNone);	
       
  1910 	if(result)
       
  1911 		{
       
  1912 		// try to open the file
       
  1913 		User::LeaveIfError(fs.Connect());
       
  1914 		User::LeaveIfError(serv.Connect());
       
  1915 		TInt ret(serv.CheckForFile(KCdbPrivate));
       
  1916 		serv.Close();
       
  1917 		
       
  1918 		file.Close();
       
  1919 		fs.Close();
       
  1920 		
       
  1921 		// if the file is not found then it has been deleted and test is successful
       
  1922 		result= (ret == KErrNotFound);
       
  1923 		}
       
  1924 	return result;
       
  1925 	}
       
  1926 
       
  1927 LOCAL_C TBool EmptyCntDatabaseOverwriteL()
       
  1928 	{
       
  1929 /**
       
  1930 @SYMTestCaseID PIM-T-SEC-CNTDBASE-DEF049719-0002
       
  1931 @SYMTestType UT
       
  1932 @SYMTestPriority High
       
  1933 @SYMDEF DEF049719
       
  1934 @SYMTestCaseDependencies CntModel CoreAppsTest
       
  1935 @SYMTestCaseDesc Check that a zero length CntDatabase file can't be overwritten using CContactDatabase::CreateL()
       
  1936 @SYMTestActions Create an empty file where the default contact database should be. 
       
  1937 Try to overwrite it using CContactDatabase::CreateL().
       
  1938 @SYMTestExpectedResults The empty contact database should not be overwritten.
       
  1939 */	
       
  1940 	test.Next(_L("@SYMTestCaseID:PIM-T-SEC-CNTDBASE-DEF049719-0002 Check zero length CntDatabase can't be overwritten in CContactDatabase::CreateL()"));
       
  1941 
       
  1942 	// Create an empty file
       
  1943 	RFs fs;
       
  1944 	User::LeaveIfError(fs.Connect());
       
  1945 	RFile file;
       
  1946 	
       
  1947 	RPIMTestServer serv;
       
  1948         User::LeaveIfError(serv.Connect());
       
  1949 	serv.CreateEmptyFileL(KCdbPrivate);
       
  1950 	serv.Close();
       
  1951 	const TDesC& datab=KCdbSecure;
       
  1952 	
       
  1953 	file.Close();
       
  1954 	fs.Close();
       
  1955 	
       
  1956 	CContactDatabase * tData=NULL;
       
  1957 	// create the contact database
       
  1958 	TRAPD(err,tData=CContactDatabase::CreateL(datab));
       
  1959 	CContactDatabase::DeleteDatabaseL(datab); //Delete the test file after use
       
  1960 	delete tData;
       
  1961 		
       
  1962 	return (err == KErrAlreadyExists);
       
  1963 	}
       
  1964 
       
  1965 LOCAL_C void ContactDatabaseZeroL()
       
  1966 	{
       
  1967 	TESTTRUEL(EmptyCntDatabaseDeleteL());
       
  1968 	TESTTRUEL(EmptyCntDatabaseOverwriteL());
       
  1969 	}
       
  1970 	
       
  1971 /**
       
  1972 @SYMTestCaseID PIM-T-SEC-CNTDBASE-0001
       
  1973 */
       
  1974 LOCAL_C void DoTestsL()
       
  1975     {
       
  1976 	test.Start(_L("@SYMTestCaseID:PIM-T-SEC-CNTDBASE-0001 T_Sec_CntDBase\n"));
       
  1977 
       
  1978 	User::LeaveIfError(fileServer.Connect());
       
  1979 
       
  1980 	// Before we do anything else check that there is an MMC drive mapped.
       
  1981 	DrivesL();
       
  1982 
       
  1983 	__UHEAP_MARK;
       
  1984 	// This test Switches drives and calls CreateL, ListDatabasesL(TDriveUnit)
       
  1985 	FunctionTestL();
       
  1986 	__UHEAP_MARKEND;
       
  1987 
       
  1988 	__UHEAP_MARK;
       
  1989 	// This test is similar to the previous, but doesn't use ListDatabasesL(TDriveUnit)
       
  1990 	// It also makes use of ReplaceL()
       
  1991 	NegTestL();
       
  1992 	__UHEAP_MARKEND;
       
  1993 
       
  1994 	__UHEAP_MARK;
       
  1995 	OOMTestingL();
       
  1996 	__UHEAP_MARKEND;
       
  1997 	
       
  1998 	__UHEAP_MARK;
       
  1999 	ContactDatabaseZeroL();
       
  2000 	__UHEAP_MARKEND;
       
  2001 	
       
  2002     }
       
  2003 
       
  2004 GLDEF_C TInt E32Main()
       
  2005 	{
       
  2006 	__UHEAP_MARK;
       
  2007 	// The contacts database uses Active Objects to it must
       
  2008 	// be provided with an Active Scheduler.
       
  2009 	CActiveScheduler* scheduler=new CActiveScheduler;
       
  2010 	if (scheduler)
       
  2011 		{
       
  2012 		CActiveScheduler::Install(scheduler);
       
  2013 		CTrapCleanup* cleanup=CTrapCleanup::New();
       
  2014 		if (cleanup)
       
  2015 			{
       
  2016 		    TRAPD(err,DoTestsL());
       
  2017 			// reset default drive location
       
  2018 		    TRAP_IGNORE(CContactDatabase::SetDatabaseDriveL(TDriveUnit(EDriveC), EFalse));
       
  2019 		    test(err == KErrNone);
       
  2020 			delete cleanup;
       
  2021 			}
       
  2022 		delete scheduler;
       
  2023 		}
       
  2024 
       
  2025 
       
  2026 	test(__NB_failures == 0);
       
  2027 
       
  2028 	test.End();
       
  2029 	test.Close();
       
  2030 
       
  2031 	__UHEAP_MARKEND;
       
  2032 	return KErrNone;
       
  2033     }