persistentstorage/sql/TEST/t_sqlbur.cpp
changeset 51 7d4490026038
parent 29 cce6680bbf1c
child 55 44f437012c90
equal deleted inserted replaced
40:b8bdbc8f59c7 51:7d4490026038
    23 
    23 
    24 ///////////////////////////////////////////////////////////////////////////////////////
    24 ///////////////////////////////////////////////////////////////////////////////////////
    25 
    25 
    26 RTest TheTest(_L("SQL Backup and Restore Test"));
    26 RTest TheTest(_L("SQL Backup and Restore Test"));
    27 
    27 
    28 _LIT(KPrivateDir, "C:\\private\\10281e17\\");
    28 _LIT(KPrivateDir, "\\private\\10281e17\\");
    29 
    29 
    30 const TUid KClientUid = {0x21212122}; // the data owner's UID
    30 //Don't forget to update DeleteBackupFiles() implementation if a new uid is added
       
    31 const TSecureId KClientUid = 0x21212122; // the data owner's UID
       
    32 const TSecureId KZeroFileSizeUid = 0xFFFF4321;
       
    33 const TSecureId KTestClientUid1 = 0xFFFF4322;
       
    34 const TSecureId KTestClientUid2 = 0xFFFF4323;
       
    35 const TSecureId KTestClientUid3 = 0xFFFF4324;
    31 
    36 
    32 _LIT(KBackupDir, "C:\\TEST\\");
    37 _LIT(KBackupDir, "C:\\TEST\\");
    33 _LIT(KBackupFile, "C:\\TEST\\Backup.bak");
    38 _LIT(KBackupFileTemplate, "C:\\TEST\\Backup.bak");
       
    39 _LIT(KBackupCopy, "C:\\TEST\\Backup2.bak");
    34 _LIT(KBackupFile2Z, "Z:\\TEST\\t_sqlbur_backup_ver0.bak");
    40 _LIT(KBackupFile2Z, "Z:\\TEST\\t_sqlbur_backup_ver0.bak");
    35 _LIT(KBackupFile2, "C:\\TEST\\t_sqlbur_backup_ver0.bak");
    41 _LIT(KBackupFile2, "C:\\TEST\\t_sqlbur_backup_ver0.bak");
    36 
    42 
       
    43 _LIT(KZeroSizeFile, "C:\\private\\10281e17\\[FFFF4321]t_sqlbur_zero.db");
       
    44 _LIT(KTestFile1, "C:\\private\\10281e17\\[FFFF4322]t_sqlbur_test1.db");
       
    45 _LIT(KTestFile1NameOnly, "[FFFF4322]t_sqlbur_test1.db");
       
    46 _LIT(KTestFile1Bak, "C:\\private\\10281e17\\bak[FFFF4322]t_sqlbur_test1.db.bak");
       
    47 _LIT(KTestDeleteMask1, "C:\\private\\10281e17\\[FFFF4322]*");
       
    48 _LIT(KTestDeleteMask2, "*bur_test1.db");
       
    49 _LIT(KTestFile2, "\\private\\10281e17\\[FFFF4323]t_sqlbur_test2.db");
       
    50 _LIT(KTestFile3, "c:\\private\\10281e17\\[FFFF4324]t_sqlbur_test3.db");
       
    51 _LIT(KTestFile4, "c:\\private\\10281e17\\[FFFF4324]t_sqlbur_test4.db");
       
    52 
       
    53 const TDriveNumber KTestDrive = EDriveC;
       
    54 
    37 const TUint KBufferSize = 2048; // used for reading backup files for validation
    55 const TUint KBufferSize = 2048; // used for reading backup files for validation
    38 
    56 
    39 static CActiveScheduler* TheScheduler = NULL;
    57 static CActiveScheduler* TheScheduler = NULL;
    40 static CSqlBurTestHarness* TheTestHarness = NULL;
    58 static CSqlSrvTestBurInterface* TheSqlSrvTestBurInterface = NULL;
    41 
    59 
    42 /////////////////////////////////////
    60 /////////////////////////////////////
    43 
    61 
    44 const TInt KMaxDbFileSize = 10 * 1024;//The max test db file size
       
    45 const TInt KTestDbFileCnt = 2;
       
    46 
       
    47 //Test db files
    62 //Test db files
    48 _LIT(KTestFileName1,"[21212122]AADB2.db");//Created outside this test app
       
    49 _LIT(KTestFileName2,"[21212122]BBDB2.db");//Created outside this test app
       
    50 _LIT(KTestDbFileName1,"C:[21212122]AADB2.db");
    63 _LIT(KTestDbFileName1,"C:[21212122]AADB2.db");
    51 _LIT(KTestDbFileName2,"C:[21212122]BBDB2.db");
    64 _LIT(KTestDbFileName2,"C:[21212122]BBDB2.db");
    52 
    65 
    53 const TPtrC KTestFileNames[KTestDbFileCnt] = {KTestFileName1(), KTestFileName2()};
    66 //Temp buffers for storing files content, to be compared with what is received after the restore operation. 10 files max.
    54 
    67 TInt TheFileCount = 0;
    55 static TInt TheDbFileSizes[KTestDbFileCnt];//An array where the real db file size will be stored
    68 const TInt KMaxDbFileSize = 10 * 1024;//The max test db file size - 10 Kb max.
    56 static TUint8 TheDbFileData[KTestDbFileCnt][KMaxDbFileSize];//An array where the original db file content will be stored
    69 static TUint8 TheDbFileData[10][KMaxDbFileSize];//An array where the original db file content will be stored
       
    70 static TInt TheDbFileSizes[10];//An array where the real db file size will be stored
    57 static TUint8 TheBuf[KMaxDbFileSize];
    71 static TUint8 TheBuf[KMaxDbFileSize];
    58 
    72 
    59 /////////////////////////////////////
    73 /////////////////////////////////////
    60 
    74 
    61 ///////////////////////////////////////////////////////////////////////////////////////
    75 ///////////////////////////////////////////////////////////////////////////////////////
    62 
    76 
       
    77 void DeleteBackupFiles()
       
    78 	{
       
    79 	TFileName fname;
       
    80 	
       
    81 	fname.Copy(KBackupFileTemplate);
       
    82 	fname.Append((static_cast <TUid> (KClientUid)).Name());
       
    83 	(void)TheSqlSrvTestBurInterface->Fs().Delete(fname);
       
    84 	
       
    85 	fname.Copy(KBackupFileTemplate);
       
    86 	fname.Append((static_cast <TUid> (KZeroFileSizeUid)).Name());
       
    87 	(void)TheSqlSrvTestBurInterface->Fs().Delete(fname);
       
    88 	
       
    89 	fname.Copy(KBackupFileTemplate);
       
    90 	fname.Append((static_cast <TUid> (KTestClientUid1)).Name());
       
    91 	(void)TheSqlSrvTestBurInterface->Fs().Delete(fname);
       
    92 	
       
    93 	fname.Copy(KBackupFileTemplate);
       
    94 	fname.Append((static_cast <TUid> (KTestClientUid2)).Name());
       
    95 	(void)TheSqlSrvTestBurInterface->Fs().Delete(fname);
       
    96 	
       
    97 	fname.Copy(KBackupFileTemplate);
       
    98 	fname.Append((static_cast <TUid> (KTestClientUid3)).Name());
       
    99 	(void)TheSqlSrvTestBurInterface->Fs().Delete(fname);
       
   100 	}
       
   101 
    63 void TestEnvDestroy()
   102 void TestEnvDestroy()
    64 	{
   103 	{
    65 	delete TheTestHarness;
   104 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KBackupCopy);
    66 	TheTestHarness = NULL;		
   105 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KTestFile1);
       
   106 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KTestFile1Bak);
       
   107 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KTestFile3);
       
   108 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KTestFile4);
       
   109 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KZeroSizeFile);
       
   110 
       
   111 	DeleteBackupFiles();
       
   112 	
       
   113 	delete TheSqlSrvTestBurInterface;
       
   114 	TheSqlSrvTestBurInterface = NULL;		
    67 	
   115 	
    68 	delete TheScheduler;
   116 	delete TheScheduler;
    69 	TheScheduler = NULL;
   117 	TheScheduler = NULL;
    70 	}
   118 	}
    71 
   119 
    94 #define TEST(arg) ::Check((arg), __LINE__)
   142 #define TEST(arg) ::Check((arg), __LINE__)
    95 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
   143 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
    96 
   144 
    97 ///////////////////////////////////////////////////////////////////////////////////////
   145 ///////////////////////////////////////////////////////////////////////////////////////
    98 
   146 
    99 //CSqlBurTestHarness - test implementation of the MSqlSrvBurInterface, implemented in the production code by the SQL server.
   147 static void DestroyFileList(RArray<HBufC*>& aFileList)
   100 CSqlBurTestHarness *CSqlBurTestHarness::New()
   148 	{
   101 	{
   149 	for(TInt i=0;i<aFileList.Count();++i)
   102 	CSqlBurTestHarness* self = new CSqlBurTestHarness;
   150 		{
       
   151 		delete aFileList[i];
       
   152 		}
       
   153 	aFileList.Close();
       
   154 	}
       
   155 
       
   156 //CSqlSrvTestBurInterface - test implementation of the MSqlSrvBurInterface, implemented in the production code by the SQL server.
       
   157 CSqlSrvTestBurInterface *CSqlSrvTestBurInterface::New()
       
   158 	{
       
   159 	CSqlSrvTestBurInterface* self = new CSqlSrvTestBurInterface;
   103 	TEST(self != NULL);
   160 	TEST(self != NULL);
   104 	self->Construct();
   161 	self->Construct();
   105 	return self;
   162 	return self;
   106 	}
   163 	}
   107 
   164 
   108 CSqlBurTestHarness::CSqlBurTestHarness()
   165 CSqlSrvTestBurInterface::CSqlSrvTestBurInterface()
   109 	{
   166 	{
   110 	}
   167 	}
   111 
   168 
   112 void CSqlBurTestHarness::Construct()
   169 void CSqlSrvTestBurInterface::Construct()
   113 	{
   170 	{
   114 	TInt err = iFs.Connect();
   171 	TInt err = iFs.Connect();
   115 	TEST2(err, KErrNone);
   172 	TEST2(err, KErrNone);
   116 	err = iFs.MkDir(KBackupDir);
   173 	err = iFs.MkDir(KBackupDir);
   117 	TEST(err == KErrNone || err == KErrAlreadyExists);
   174 	TEST(err == KErrNone || err == KErrAlreadyExists);
   118 	err = iFs.CreatePrivatePath(EDriveC);
   175 	err = iFs.CreatePrivatePath(KTestDrive);
   119 	TEST2(err, KErrNone);
   176 	TEST2(err, KErrNone);
   120 	}
   177 	}
   121 
   178 
   122 CSqlBurTestHarness::~CSqlBurTestHarness()
   179 CSqlSrvTestBurInterface::~CSqlSrvTestBurInterface()
   123 	{
   180 	{
   124 	(void)iFs.Delete(KBackupFile);
       
   125 	iFs.Close();
   181 	iFs.Close();
   126 	}
   182 	}
   127 
   183 
   128 //Called by the backup client ot get a list of database files to backup
   184 //Called by the backup client ot get a list of database files to backup
   129 //The array is owned by the caller
   185 //The array is owned by the caller
   130 //The SQL server would have the job to get a list of databases owned by
   186 //The SQL server would have the job to get a list of databases owned by
   131 //the given SID and to determine whether the backup flag is set
   187 //the given SID and to determine whether the backup flag is set
   132 //All databases that satisfy this requirement will be added to the array
   188 //All databases that satisfy this requirement will be added to the array.
   133 void CSqlBurTestHarness::GetBackUpListL(TSecureId aUid, RArray<TParse>& aFileList)
   189 void CSqlSrvTestBurInterface::GetBackUpListL(TSecureId aUid, TDriveNumber aDrive, RArray<HBufC*>& aFileList)
   134 	{
   190 	{
   135 	if(aUid.iId == 0)
   191 	if(aUid.iId == 0)
   136 		{//Simulates that there are no databases for backup
   192 		{//Simulates that there are no databases for backup
   137 		aFileList.Reset();
   193 		DestroyFileList(aFileList);
   138 		return;
   194 		return;
   139 		}
   195 		}
   140 	//TheTest.Printf(_L("Getting backup file list for SID=%x\r\n"),aUid);
   196 	
   141 	for(TInt i=0;i<KTestDbFileCnt;++i)
   197 	TDriveUnit driveUnit(aDrive);
   142 		{
   198 	TDriveName driveName = driveUnit.Name();
   143 		TParse parse;
   199 	TFileName path;
   144 		parse.Set(KTestFileNames[i], &KPrivateDir, NULL);
   200 	path.Copy(driveName);
   145 		aFileList.AppendL(parse);
   201 	path.Append(KPrivateDir);
   146 		}
   202 	//Include the aUid and the "*" mask
   147 	}
   203 	TUidName uidName = (static_cast <TUid> (aUid)).Name();
   148 
   204 	_LIT(KMatchAllDbFiles, "*");
   149 //Notification that a backup is starting
   205 	TBuf<KMaxUidName + sizeof(KMatchAllDbFiles)> fileNameMask(uidName);
   150 TBool CSqlBurTestHarness::StartBackupL(const RArray<TParse>& /*aFileList*/) 
   206 	fileNameMask.Append(KMatchAllDbFiles);
   151 	{
   207 	TParse parse;
   152 	//TheTest.Printf(_L("Start \"backup\". %d files in the list.\r\n"), aFileList.Count());
   208 	TInt err = parse.Set(path, &fileNameMask, NULL);
   153 	return ETrue;
   209 	User::LeaveIfError(err);
   154 	}
   210 	//Do the search
   155 
   211 	TPtrC fullPath(parse.FullName());
   156 //Notification that a backup has ended
   212 	CDir* fileNameCol = NULL;
   157 void CSqlBurTestHarness::EndBackup(const RArray<TParse>& /*aFileList*/)
   213 	err = TheSqlSrvTestBurInterface->Fs().GetDir(fullPath, KEntryAttNormal, ESortNone, fileNameCol);
   158 	{
   214 	if(err == KErrNotFound)
   159 	//TheTest.Printf(_L("End \"backup\". %d files in the list.\r\n"), aFileList.Count());
   215 		{
   160 	}
   216 		return;
   161 
   217 		}
   162 //Notification that a restore is starting
   218 	User::LeaveIfError(err);
   163 TBool CSqlBurTestHarness::StartRestoreL(TSecureId /*aUid*/) 
   219 	CleanupStack::PushL(fileNameCol);
   164 	{
   220 	TInt fileCount = fileNameCol->Count();
   165 	//TheTest.Printf(_L("Start \"restore\" for UID=%X\r\n"), aUid);
   221 	err = aFileList.Reserve(fileCount);
   166 	return ETrue;
   222 	User::LeaveIfError(err);
   167 	}
   223 	//Append the full database file paths to the file names list.
   168 
   224 	for(TInt i=0;i<fileCount;++i)
   169 //Notification that a restore has ended
   225 		{
   170 void CSqlBurTestHarness::EndRestore(TSecureId /*aUid*/) 
   226 		const ::TEntry& entry = (*fileNameCol)[i];
   171 	{
   227 		err = parse.Set(path, &entry.iName, NULL);
   172 	//TheTest.Printf(_L("End \"restore\" for UID=%X\r\n"), aUid);
   228 		User::LeaveIfError(err);
   173 	}
   229 		TPtrC fname(parse.FullName());
   174 	
   230 		HBufC* fnameBuf = fname.AllocL();
       
   231 		err = aFileList.Append(fnameBuf);
       
   232 		User::LeaveIfError(err);
       
   233 		}
       
   234 	CleanupStack::PopAndDestroy(fileNameCol);
       
   235 	}
       
   236 
   175 //Returns the file system resource handle to the caller.
   237 //Returns the file system resource handle to the caller.
   176 RFs& CSqlBurTestHarness::Fs()
   238 RFs& CSqlSrvTestBurInterface::Fs()
   177 	{
   239 	{
   178 	return iFs;
   240 	return iFs;
   179 	}
   241 	}
   180 
   242 
   181 ///////////////////////////////////////////////////////////////////////////////////////
   243 ///////////////////////////////////////////////////////////////////////////////////////
   193 	TheScheduler = new CActiveScheduler;
   255 	TheScheduler = new CActiveScheduler;
   194 	TEST(TheScheduler != NULL);
   256 	TEST(TheScheduler != NULL);
   195 	
   257 	
   196 	CActiveScheduler::Install(TheScheduler);
   258 	CActiveScheduler::Install(TheScheduler);
   197 
   259 
   198 	TheTestHarness = CSqlBurTestHarness::New();
   260 	TheSqlSrvTestBurInterface = CSqlSrvTestBurInterface::New();
   199 	TEST(TheTestHarness != NULL);
   261 	TEST(TheSqlSrvTestBurInterface != NULL);
       
   262 	}
       
   263 
       
   264 void PrepareSearchPath(TDriveNumber aDrive, TDes& aPath)
       
   265 	{
       
   266 	TDriveUnit driveUnit(aDrive);
       
   267 	TDriveName driveName = driveUnit.Name();
       
   268 	aPath.Copy(driveName);
       
   269 	aPath.Append(KPrivateDir);
       
   270 	}
       
   271 
       
   272 void PrepareSearchPattern(const TDesC& aPath, TSecureId aUid, TParse& aParse)
       
   273 	{
       
   274 	TUidName uidName = (static_cast <TUid> (aUid)).Name();
       
   275 	_LIT(KMatchAllDbFiles, "*");
       
   276 	TBuf<KMaxUidName + sizeof(KMatchAllDbFiles)> fileNameMask(uidName);
       
   277 	fileNameMask.Append(KMatchAllDbFiles);
       
   278 	TInt err = aParse.Set(aPath, &fileNameMask, NULL);
       
   279 	TEST2(err, KErrNone);
   200 	}
   280 	}
   201 
   281 
   202 //Reads the content of the db files and stores the content to a global memory buffer.
   282 //Reads the content of the db files and stores the content to a global memory buffer.
   203 //That buffer content will be used later for verification of the restore process.
   283 //That buffer content will be used later for verification of the restore process.
   204 void StoreDbContentToBuf(RFs& aFs)
   284 void StoreDbContentToBuf(RFs& aFs, TDriveNumber aDrive, TSecureId aUid)
   205 	{
   285 	{
   206 	for(TInt i=0;i<KTestDbFileCnt;++i)
   286 	TFileName path;
   207 		{
   287 	PrepareSearchPath(aDrive, path);
       
   288 	TParse parse;
       
   289 	PrepareSearchPattern(path, aUid, parse);
       
   290 
       
   291 	TheFileCount = -1;
       
   292 	Mem::FillZ(TheDbFileData, sizeof(TheDbFileData));
       
   293 	Mem::FillZ(TheDbFileSizes, sizeof(TheDbFileSizes));
       
   294 	
       
   295 	CDir* dir = NULL;
       
   296 	TInt err = aFs.GetDir(parse.FullName(), KEntryAttNormal, ESortByName, dir);
       
   297 	TEST2(err, KErrNone);
       
   298 	TheFileCount = dir->Count();
       
   299 	for(TInt i=0;i<TheFileCount;++i)
       
   300 		{
       
   301 		const ::TEntry& entry = (*dir)[i];
       
   302 		err = parse.Set(path, &entry.iName, NULL);
       
   303 		TEST2(err, KErrNone);
       
   304 		TPtrC fname(parse.FullName());
       
   305 	
   208 		RFile dbFile;
   306 		RFile dbFile;
   209 		TInt err = dbFile.Open(aFs, KTestFileNames[i], EFileRead);
   307 		err = dbFile.Open(aFs, fname, EFileRead);
   210 		TEST2(err, KErrNone);
   308 		TEST2(err, KErrNone);
   211 		
   309 		
   212 		TInt fileSize = 0;
   310 		TInt fileSize = 0;
   213 		err = dbFile.Size(fileSize);
   311 		err = dbFile.Size(fileSize);
   214 		TEST2(err, KErrNone);
   312 		TEST2(err, KErrNone);
   221 
   319 
   222 		TheDbFileSizes[i] = fileSize;
   320 		TheDbFileSizes[i] = fileSize;
   223 		
   321 		
   224 		dbFile.Close();
   322 		dbFile.Close();
   225 		}
   323 		}
       
   324 	delete dir;
   226 	}
   325 	}
   227 
   326 
   228 //At the moment when this function is called, the db files content is already restored.
   327 //At the moment when this function is called, the db files content is already restored.
   229 //The function will open the restored db files and compare their content against the content
   328 //The function will open the restored db files and compare their content against the content
   230 //of the original db files (kept in a global memory buffer).
   329 //of the original db files (kept in a global memory buffer).
   231 void CompareDbContentWithBuf(RFs& aFs)
   330 void CompareDbContentWithBuf(RFs& aFs, TDriveNumber aDrive, TSecureId aUid)
   232 	{
   331 	{
   233 	for(TInt i=0;i<KTestDbFileCnt;++i)
   332 	TFileName path;
       
   333 	PrepareSearchPath(aDrive, path);
       
   334 	TParse parse;
       
   335 	PrepareSearchPattern(path, aUid, parse);
       
   336 	
       
   337 	CDir* dir = NULL;
       
   338 	TInt err = aFs.GetDir(parse.FullName(), KEntryAttNormal, ESortByName, dir);
       
   339 	TEST2(err, KErrNone);
       
   340 	TEST2(TheFileCount, dir->Count());
       
   341 	for(TInt i=0;i<TheFileCount;++i)
   234 		{
   342 		{
   235 		TEST(TheDbFileSizes[i] > 0);
   343 		TEST(TheDbFileSizes[i] > 0);
   236 		
   344 		
       
   345 		const ::TEntry& entry = (*dir)[i];
       
   346 		err = parse.Set(path, &entry.iName, NULL);
       
   347 		TEST2(err, KErrNone);
       
   348 		TPtrC fname(parse.FullName());
       
   349 		
   237 		RFile dbFile;
   350 		RFile dbFile;
   238 		TInt err = dbFile.Open(aFs, KTestFileNames[i], EFileRead);
   351 		TInt err = dbFile.Open(aFs, fname, EFileRead);
   239 		TEST2(err, KErrNone);
   352 		TEST2(err, KErrNone);
   240 		
   353 		
   241 		TInt fileSize = 0;
   354 		TInt fileSize = 0;
   242 		err = dbFile.Size(fileSize);
   355 		err = dbFile.Size(fileSize);
   243 		TEST2(err, KErrNone);
   356 		TEST2(err, KErrNone);
   244 		TEST(fileSize > 0);
   357 		TEST(fileSize > 0);
   245 		TEST(TheDbFileSizes[i] == fileSize);
   358 		TEST2(TheDbFileSizes[i], fileSize);
   246 
   359 
   247 		TPtr8 bufptr(TheBuf, 0, KMaxDbFileSize);
   360 		TPtr8 bufptr(TheBuf, 0, KMaxDbFileSize);
   248 		err = dbFile.Read(bufptr, fileSize);
   361 		err = dbFile.Read(bufptr, fileSize);
   249 		TEST2(err, KErrNone);
   362 		TEST2(err, KErrNone);
   250 		TEST(fileSize == bufptr.Length());
   363 		TEST2(fileSize, bufptr.Length());
   251 
   364 
   252 		err = Mem::Compare(TheBuf, fileSize, TheDbFileData[i], TheDbFileSizes[i]);
   365 		err = Mem::Compare(TheBuf, fileSize, TheDbFileData[i], TheDbFileSizes[i]);
   253 		TEST2(err, 0);
   366 		TEST2(err, 0);
   254 
   367 
   255 		dbFile.Close();
   368 		dbFile.Close();
   256 		}
   369 		}
       
   370 	delete dir;
   257 	}
   371 	}
   258 
   372 
   259 ////////////////////////////////////////////////////////////////////////////////////////
   373 ////////////////////////////////////////////////////////////////////////////////////////
   260 
   374 
   261 //The backup client will return a series of data chunks representing
   375 //The backup client will return a series of data chunks representing
   262 //one of more databases for the uid of the data owner.
   376 //one of more databases for the uid of the data owner.
   263 //This data is stored in a file on the C drive for the purposes of the test
   377 //This data is stored in a file on the C drive for the purposes of the test
   264 TInt TestBackupL(CSqlBackupClient &aBackupClient, RFs& aFs, TInt aDataChunkSize = KBufferSize)
   378 TInt TestBackupL(CSqlBurCallback &aBackupClient, RFs& aFs, TSecureId aUid, TDriveNumber aDrive, TInt aDataChunkSize = KBufferSize)
   265 	{
   379 	{
       
   380 	TFileName backupFileName;
       
   381 	backupFileName.Copy(KBackupFileTemplate);
       
   382 	backupFileName.Append((static_cast <TUid> (aUid)).Name());
       
   383 	
   266 	RFile file;
   384 	RFile file;
   267 	CleanupClosePushL(file);
   385 	CleanupClosePushL(file);
   268 	TInt err = file.Replace(aFs, KBackupFile, EFileWrite | EFileStream | EFileShareExclusive);
   386 	TInt err = file.Replace(aFs, backupFileName, EFileWrite | EFileStream | EFileShareExclusive);
   269 	User::LeaveIfError(err);
   387 	User::LeaveIfError(err);
   270 	aBackupClient.InitialiseGetProxyBackupDataL(KClientUid, EDriveC);
   388 	aBackupClient.InitialiseGetProxyBackupDataL(aUid, aDrive);
   271 	
   389 	
   272 	TBuf8<KBufferSize> buf;
   390 	TBuf8<KBufferSize> buf;
   273 	TPtr8 ptr((TUint8*)buf.Ptr(), aDataChunkSize);
   391 	TPtr8 ptr((TUint8*)buf.Ptr(), aDataChunkSize);
   274 	TBool finishedFlag = EFalse;
   392 	TBool finishedFlag = EFalse;
   275 	TInt count = 0;
   393 	TInt count = 0;
   288 	
   406 	
   289 	if(count == 0)
   407 	if(count == 0)
   290 		{
   408 		{
   291 		User::Leave(KErrEof);
   409 		User::Leave(KErrEof);
   292 		}
   410 		}
   293 	if(!FileExists(aFs, KBackupFile))
   411 	if(!FileExists(aFs, backupFileName))
   294 		{
   412 		{
   295 		User::Leave(KErrNotFound);
   413 		User::Leave(KErrNotFound);
   296 		}
   414 		}
   297 	TheTest.Printf(_L("Backup complete. %d bytes processed.\r\n"), count);
   415 	TheTest.Printf(_L("Backup complete. %d bytes processed.\r\n"), count);
   298 	return count;
   416 	return count;
   299 	}
   417 	}
   300 
   418 
   301 //This sends the data in chunks form back to the BUR client
   419 //This sends the data in chunks form back to the BUR client
   302 //for nupacking and restoration of the original databases files
   420 //for nupacking and restoration of the original databases files
   303 TInt TestRestoreL(CSqlBackupClient &aRestoreClient, RFs& aFs, TInt aDataChunkSize = KBufferSize)
   421 TInt TestRestoreL(CSqlBurCallback &aRestoreClient, RFs& aFs, TSecureId aUid, TDriveNumber aDrive, TInt aDataChunkSize = KBufferSize)
   304 	{
   422 	{
       
   423 	TFileName backupFileName;
       
   424 	backupFileName.Copy(KBackupFileTemplate);
       
   425 	backupFileName.Append((static_cast <TUid> (aUid)).Name());
       
   426 	
   305 	RFile file;
   427 	RFile file;
   306 	CleanupClosePushL(file);
   428 	CleanupClosePushL(file);
   307 	TInt err = file.Open(aFs, KBackupFile, EFileRead | EFileShareExclusive);
   429 	TInt err = file.Open(aFs, backupFileName, EFileRead | EFileShareExclusive);
   308 	User::LeaveIfError(err);
   430 	User::LeaveIfError(err);
   309 	aRestoreClient.InitialiseRestoreProxyBaseDataL(KClientUid, EDriveC);
   431 	aRestoreClient.InitialiseRestoreProxyBaseDataL(aUid, aDrive);
   310 	
   432 	
   311 	TBuf8<KBufferSize> buf;
   433 	TBuf8<KBufferSize> buf;
   312 	TPtr8 ptr((TUint8*)buf.Ptr(), aDataChunkSize);
   434 	TPtr8 ptr((TUint8*)buf.Ptr(), aDataChunkSize);
   313 	TBool finishedFlag = EFalse;
   435 	TBool finishedFlag = EFalse;
   314 	
   436 	
   328 		} 
   450 		} 
   329 	while(fileSize > 0);
   451 	while(fileSize > 0);
   330 	
   452 	
   331 	CleanupStack::PopAndDestroy(&file);
   453 	CleanupStack::PopAndDestroy(&file);
   332 	
   454 	
   333 	aRestoreClient.RestoreComplete(EDriveC);
   455 	aRestoreClient.RestoreComplete(aDrive);
   334 	
   456 	
   335 	if(!finishedFlag)
   457 	if(!finishedFlag)
   336 		{
   458 		{
   337 		User::Leave(KErrEof);
   459 		User::Leave(KErrEof);
   338 		}
       
   339 	for(TInt i=0;i<KTestDbFileCnt;++i)
       
   340 		{
       
   341 		if(!FileExists(aFs, KTestFileNames[i]))
       
   342 			{
       
   343 			User::Leave(KErrNotFound);
       
   344 			}
       
   345 		}
   460 		}
   346 		
   461 		
   347 	TheTest.Printf(_L("Restore complete. %d bytes processed.\r\n"), count);
   462 	TheTest.Printf(_L("Restore complete. %d bytes processed.\r\n"), count);
   348 	return count;
   463 	return count;
   349 	}
   464 	}
   350 
   465 
   351 //Verifies the integrity of the backup file.
   466 //Verifies the integrity of the backup file.
   352 void TestArchiveIntegrityL(CSqlBackupClient &aBackupClient, RFs& aFs)
   467 void TestArchiveIntegrityL(CSqlBurCallback &aBackupClient, RFs& aFs, TSecureId aUid)
   353 	{
   468 	{
   354 	RFile bkpFile;
   469 	RFile bkpFile;
   355 	CleanupClosePushL(bkpFile);
   470 	CleanupClosePushL(bkpFile);
   356 	
   471 
   357 	TInt err = bkpFile.Open(aFs, KBackupFile, EFileRead | EFileShareExclusive);
   472 	TFileName backupFileName;
       
   473 	backupFileName.Copy(KBackupFileTemplate);
       
   474 	backupFileName.Append((static_cast <TUid> (aUid)).Name());
       
   475 	
       
   476 	TInt err = bkpFile.Open(aFs, backupFileName, EFileRead | EFileShareExclusive);
   358 	User::LeaveIfError(err);
   477 	User::LeaveIfError(err);
   359 	
   478 	
   360 	TBuf8<KBufferSize> buf;
   479 	TBuf8<KBufferSize> buf;
   361 	TPtr8 ptr((TUint8*)buf.Ptr(), buf.MaxLength());
   480 	TPtr8 ptr((TUint8*)buf.Ptr(), buf.MaxLength());
   362 	
   481 	
   471 			}
   590 			}
   472 
   591 
   473 		bkpFileSize -= fileSize;
   592 		bkpFileSize -= fileSize;
   474 		
   593 		
   475 		// checksum the file
   594 		// checksum the file
   476 		TUint32 dbChecksum = aBackupClient.CheckSumL(dbFile) & 0xFFFFFFFF;
   595 		TUint64 checkSum64 = 0;
   477 		
   596 		User::LeaveIfError(aBackupClient.CheckSum(dbFile, checkSum64));
   478 		if(checksum != dbChecksum)
   597 		TUint32 checksum32 = checkSum64 & 0xFFFFFFFF; 
       
   598 		
       
   599 		if(checksum != checksum32)
   479 			{
   600 			{
   480 			User::Leave(KErrCorrupt);
   601 			User::Leave(KErrCorrupt);
   481 			}
   602 			}
   482 			
   603 			
   483 		// all done with this file
   604 		// all done with this file
   501 @SYMTestExpectedResults Test must not fail
   622 @SYMTestExpectedResults Test must not fail
   502 @SYMDEF					DEF113598
   623 @SYMDEF					DEF113598
   503 */	
   624 */	
   504 void FunctionalTest()
   625 void FunctionalTest()
   505 	{
   626 	{
   506 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4002 Backup: functional test "));
   627 	CSqlBurCallback* backupClient = NULL;
   507 	
   628 	TRAPD(err, backupClient = CSqlBurCallback::NewL(*TheSqlSrvTestBurInterface));
   508 	CSqlBackupClient* backupClient = NULL;
       
   509 	TRAPD(err, backupClient = CSqlBackupClient::NewL(TheTestHarness));
       
   510 	TEST(backupClient != NULL);
   629 	TEST(backupClient != NULL);
   511 
   630 
   512 	////////////////////////////////////////
   631 	////////////////////////////////////////
   513 
       
   514 	const TDriveNumber KDrive = EDriveC;
       
   515 	
   632 	
   516 	//Virtual functions - with default implementation
   633 	//Virtual functions - with default implementation
   517 	
   634 	
   518 	(void)backupClient->GetExpectedDataSize(KDrive);
   635 	(void)backupClient->GetExpectedDataSize(KTestDrive);
   519 
   636 
   520 	(void)backupClient->GetDataChecksum(KDrive);
   637 	(void)backupClient->GetDataChecksum(KTestDrive);
   521 	
   638 	
   522 	TBool finished = EFalse;
   639 	TBool finished = EFalse;
   523 	TPtr8 ptr(0, 0, 0);
   640 	TPtr8 ptr(0, 0, 0);
   524 	TRAP(err, backupClient->GetSnapshotDataL(KDrive, ptr, finished));
   641 	TRAP(err, backupClient->GetSnapshotDataL(KTestDrive, ptr, finished));
   525 	TEST2(err, KErrNotSupported);
   642 	TEST2(err, KErrNotSupported);
   526 
   643 
   527 	TRAP(err, backupClient->InitialiseGetBackupDataL(KDrive));
   644 	TRAP(err, backupClient->InitialiseGetBackupDataL(KTestDrive));
   528 	TEST2(err, KErrNotSupported);
   645 	TEST2(err, KErrNotSupported);
   529 
   646 
   530 	TRAP(err, backupClient->InitialiseRestoreBaseDataL(KDrive));
   647 	TRAP(err, backupClient->InitialiseRestoreBaseDataL(KTestDrive));
   531 	TEST2(err, KErrNotSupported);
   648 	TEST2(err, KErrNotSupported);
   532 
   649 
   533 	TRAP(err, backupClient->InitialiseRestoreIncrementDataL(KDrive));
   650 	TRAP(err, backupClient->InitialiseRestoreIncrementDataL(KTestDrive));
   534 	TEST2(err, KErrNotSupported);
   651 	TEST2(err, KErrNotSupported);
   535 
   652 
   536 	TPtrC8 ptr2(KNullDesC8);
   653 	TPtrC8 ptr2(KNullDesC8);
   537 	TRAP(err, backupClient->RestoreIncrementDataSectionL(ptr2, finished));
   654 	TRAP(err, backupClient->RestoreIncrementDataSectionL(ptr2, finished));
   538 	TEST2(err, KErrNotSupported);
   655 	TEST2(err, KErrNotSupported);
   539 
   656 
   540 	TRAP(err, backupClient->AllSnapshotsSuppliedL());
   657 	TRAP(err, backupClient->AllSnapshotsSuppliedL());
   541 	TEST2(err, KErrNone);
   658 	TEST2(err, KErrNone);
   542 
   659 
   543 	TRAP(err, backupClient->ReceiveSnapshotDataL(KDrive, ptr2, finished));
   660 	TRAP(err, backupClient->ReceiveSnapshotDataL(KTestDrive, ptr2, finished));
   544 	TEST2(err, KErrNotSupported);
   661 	TEST2(err, KErrNotSupported);
   545 
   662 
   546 	backupClient->TerminateMultiStageOperation();
   663 	backupClient->TerminateMultiStageOperation();
   547 
   664 
   548 	////////////////////////////////////////
   665 	////////////////////////////////////////
   549 
   666 
   550 	TInt bytesStored = 0;
   667 	TInt bytesStored = 0;
   551 	TRAP(err, bytesStored = TestBackupL(*backupClient, TheTestHarness->Fs()));
   668 	TRAP(err, bytesStored = TestBackupL(*backupClient, TheSqlSrvTestBurInterface->Fs(), KClientUid, KTestDrive));
   552 	TEST2(err, KErrNone);
   669 	TEST2(err, KErrNone);
   553 
   670 
   554 	TheTest.Next(_L("Archive integrity test"));
   671 	TheTest.Next(_L("Archive integrity test"));
   555 	
   672 	
   556 	TRAP(err, TestArchiveIntegrityL(*backupClient, TheTestHarness->Fs()));
   673 	TRAP(err, TestArchiveIntegrityL(*backupClient, TheSqlSrvTestBurInterface->Fs(), KClientUid));
   557 	TEST2(err, KErrNone);
   674 	TEST2(err, KErrNone);
   558 
   675 
   559 	delete backupClient;
   676 	delete backupClient;
   560 
   677 
   561 	TheTest.Next(_L("Restore: functional test"));
   678 	TheTest.Next(_L("Restore: functional test"));
   562 
   679 
   563 	CSqlBackupClient* restoreClient = NULL;
   680 	CSqlBurCallback* restoreClient = NULL;
   564 	TRAP(err, restoreClient = CSqlBackupClient::NewL(TheTestHarness));
   681 	TRAP(err, restoreClient = CSqlBurCallback::NewL(*TheSqlSrvTestBurInterface));
   565 	TEST(restoreClient != NULL);
   682 	TEST(restoreClient != NULL);
   566 
   683 
   567 	TInt bytesRestored = 0;
   684 	TInt bytesRestored = 0;
   568 	TRAP(err, bytesRestored = TestRestoreL(*restoreClient, TheTestHarness->Fs()));
   685 	TRAP(err, bytesRestored = TestRestoreL(*restoreClient, TheSqlSrvTestBurInterface->Fs(), KClientUid, KTestDrive));
   569 	TEST2(err, KErrNone);
   686 	TEST2(err, KErrNone);
   570 	
   687 	
   571 	TEST(bytesRestored == bytesStored);
   688 	TEST(bytesRestored == bytesStored);
   572 
   689 
   573 	delete restoreClient;
   690 	delete restoreClient;
   574 
   691 
   575 	CompareDbContentWithBuf(TheTestHarness->Fs());
   692 	CompareDbContentWithBuf(TheSqlSrvTestBurInterface->Fs(), KTestDrive, KClientUid);
   576 	}
   693 	}
   577 	
   694 	
   578 TInt DoBackupL()
   695 TInt DoBackupL(TDriveNumber aDrive, TSecureId aUid)
   579 	{
   696 	{
   580 	CSqlBackupClient* backupClient = CSqlBackupClient::NewLC(TheTestHarness);
   697 	CSqlBurCallback* backupClient = CSqlBurCallback::NewL(*TheSqlSrvTestBurInterface);
   581 	TInt bytesStored = TestBackupL(*backupClient, TheTestHarness->Fs());
   698 	CleanupStack::PushL(backupClient);
       
   699 	TInt bytesStored = TestBackupL(*backupClient, TheSqlSrvTestBurInterface->Fs(), aUid, aDrive);
   582 	CleanupStack::PopAndDestroy(backupClient);
   700 	CleanupStack::PopAndDestroy(backupClient);
   583 	return bytesStored;
   701 	return bytesStored;
   584 	}
   702 	}
   585 
   703 
   586 TInt DoRestoreL()
   704 TInt DoRestoreL(TDriveNumber aDrive, TSecureId aUid)
   587 	{
   705 	{
   588 	CSqlBackupClient* restoreClient = CSqlBackupClient::NewLC(TheTestHarness);
   706 	CSqlBurCallback* restoreClient = CSqlBurCallback::NewL(*TheSqlSrvTestBurInterface);
   589 	TInt bytesRestored = TestRestoreL(*restoreClient, TheTestHarness->Fs());
   707 	CleanupStack::PushL(restoreClient);
       
   708 	TInt bytesRestored = TestRestoreL(*restoreClient, TheSqlSrvTestBurInterface->Fs(), aUid, aDrive);
   590 	CleanupStack::PopAndDestroy(restoreClient);
   709 	CleanupStack::PopAndDestroy(restoreClient);
   591 	return bytesRestored;
   710 	return bytesRestored;
   592 	}
   711 	}
   593 
   712 
   594 /**
   713 /**
   604 @SYMDEF					DEF113598
   723 @SYMDEF					DEF113598
   605 */	
   724 */	
   606 void OomTest()
   725 void OomTest()
   607 	{
   726 	{
   608 	///////////////////////////////////////////////////////////////////////////////
   727 	///////////////////////////////////////////////////////////////////////////////
   609 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4003 Backup: OOM test "));
       
   610 	TInt err = KErrNoMemory;
   728 	TInt err = KErrNoMemory;
   611 	TInt bytesStored = 0;
   729 	TInt bytesStored = 0;
   612 	TInt count = 0;
   730 	TInt count = 0;
   613 	
   731 	
   614 	for(count=1;err==KErrNoMemory;++count)
   732 	for(count=1;err==KErrNoMemory;++count)
   617 		TInt startThreadHandleCount;
   735 		TInt startThreadHandleCount;
   618 		RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
   736 		RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
   619 		
   737 		
   620 		User::__DbgMarkStart(RHeap::EUser);
   738 		User::__DbgMarkStart(RHeap::EUser);
   621 		User::__DbgSetAllocFail(RHeap::EUser,RHeap::EFailNext, count);
   739 		User::__DbgSetAllocFail(RHeap::EUser,RHeap::EFailNext, count);
   622 		TRAP(err, bytesStored = DoBackupL());
   740 		TRAP(err, bytesStored = DoBackupL(KTestDrive, KClientUid));
   623 		User::__DbgMarkEnd(RHeap::EUser, 0);
   741 		User::__DbgMarkEnd(RHeap::EUser, 0);
   624 		
   742 		
   625 		TInt endProcessHandleCount;
   743 		TInt endProcessHandleCount;
   626 		TInt endThreadHandleCount;
   744 		TInt endThreadHandleCount;
   627 		RThread().HandleCount(endProcessHandleCount, endThreadHandleCount);
   745 		RThread().HandleCount(endProcessHandleCount, endThreadHandleCount);
   643 		TInt startThreadHandleCount;
   761 		TInt startThreadHandleCount;
   644 		RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
   762 		RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
   645 		
   763 		
   646 		User::__DbgMarkStart(RHeap::EUser);
   764 		User::__DbgMarkStart(RHeap::EUser);
   647 		User::__DbgSetAllocFail(RHeap::EUser,RHeap::EFailNext, count);
   765 		User::__DbgSetAllocFail(RHeap::EUser,RHeap::EFailNext, count);
   648 		TRAP(err, bytesRestored = DoRestoreL());
   766 		TRAP(err, bytesRestored = DoRestoreL(KTestDrive, KClientUid));
   649 		User::__DbgMarkEnd(RHeap::EUser, 0);
   767 		User::__DbgMarkEnd(RHeap::EUser, 0);
   650 		
   768 		
   651 		TInt endProcessHandleCount;
   769 		TInt endProcessHandleCount;
   652 		TInt endThreadHandleCount;
   770 		TInt endThreadHandleCount;
   653 		RThread().HandleCount(endProcessHandleCount, endThreadHandleCount);
   771 		RThread().HandleCount(endProcessHandleCount, endThreadHandleCount);
   657 		}
   775 		}
   658 	TEST2(err, KErrNone);
   776 	TEST2(err, KErrNone);
   659 	User::__DbgSetAllocFail(RHeap::EUser, RAllocator::ENone, 0);
   777 	User::__DbgSetAllocFail(RHeap::EUser, RAllocator::ENone, 0);
   660 	TheTest.Printf(_L("OOM restore test succeeded at heap failure rate of %d\r\n"), count);
   778 	TheTest.Printf(_L("OOM restore test succeeded at heap failure rate of %d\r\n"), count);
   661 	
   779 	
   662 	TEST(bytesStored == bytesRestored);
   780 	TEST2(bytesStored, bytesRestored);
   663 
   781 
   664 	CompareDbContentWithBuf(TheTestHarness->Fs());
   782 	CompareDbContentWithBuf(TheSqlSrvTestBurInterface->Fs(), KTestDrive, KClientUid);
   665 	}
   783 	}
   666 
   784 
   667 /**
   785 /**
   668 @SYMTestCaseID			PDS-SQL-UT-4143
   786 @SYMTestCaseID			PDS-SQL-UT-4143
   669 @SYMTestCaseDesc		SQL Backup&Restore - data chunk size test.
   787 @SYMTestCaseDesc		SQL Backup&Restore - data chunk size test.
   676 @SYMTestPriority		Medium
   794 @SYMTestPriority		Medium
   677 @SYMREQ					REQ12104
   795 @SYMREQ					REQ12104
   678 */
   796 */
   679 void FunctionalTest2()
   797 void FunctionalTest2()
   680 	{
   798 	{
   681 	TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4143 Backup&Restore: functional test 2"));
       
   682 	
       
   683 	TTime now;
   799 	TTime now;
   684 	now.UniversalTime();
   800 	now.UniversalTime();
   685 	TInt64 seed = now.Int64();
   801 	TInt64 seed = now.Int64();
   686 
   802 
   687 	const TInt KArraySize = 10;
   803 	const TInt KArraySize = 10;
   698 		}
   814 		}
   699 	
   815 	
   700 	for(TInt i=0;i<KArraySize;++i)
   816 	for(TInt i=0;i<KArraySize;++i)
   701 		{
   817 		{
   702 		TheTest.Printf(_L(" === Iteration %d, chunk size %d\r\n"), i + 1, dataChunks[i]);
   818 		TheTest.Printf(_L(" === Iteration %d, chunk size %d\r\n"), i + 1, dataChunks[i]);
   703 		CSqlBackupClient* backupClient = NULL;
   819 		CSqlBurCallback* backupClient = NULL;
   704 		TRAPD(err, backupClient = CSqlBackupClient::NewL(TheTestHarness));
   820 		TRAPD(err, backupClient = CSqlBurCallback::NewL(*TheSqlSrvTestBurInterface));
   705 		TEST(backupClient != NULL);
   821 		TEST(backupClient != NULL);
   706 
   822 
   707 		TInt bytesStored = 0;
   823 		TInt bytesStored = 0;
   708 		TRAP(err, bytesStored = TestBackupL(*backupClient, TheTestHarness->Fs(), dataChunks[i]));
   824 		TRAP(err, bytesStored = TestBackupL(*backupClient, TheSqlSrvTestBurInterface->Fs(), KClientUid, KTestDrive, dataChunks[i]));
   709 		TEST2(err, KErrNone);
   825 		TEST2(err, KErrNone);
   710 
   826 
   711 		TRAP(err, TestArchiveIntegrityL(*backupClient, TheTestHarness->Fs()));
   827 		TRAP(err, TestArchiveIntegrityL(*backupClient, TheSqlSrvTestBurInterface->Fs(), KClientUid));
   712 		TEST2(err, KErrNone);
   828 		TEST2(err, KErrNone);
   713 
   829 
   714 		delete backupClient;
   830 		delete backupClient;
   715 
   831 
   716 		CSqlBackupClient* restoreClient = NULL;
   832 		CSqlBurCallback* restoreClient = NULL;
   717 		TRAP(err, restoreClient = CSqlBackupClient::NewL(TheTestHarness));
   833 		TRAP(err, restoreClient = CSqlBurCallback::NewL(*TheSqlSrvTestBurInterface));
   718 		TEST(restoreClient != NULL);
   834 		TEST(restoreClient != NULL);
   719 
   835 
   720 		TInt bytesRestored = 0;
   836 		TInt bytesRestored = 0;
   721 		TRAP(err, bytesRestored = TestRestoreL(*restoreClient, TheTestHarness->Fs(), dataChunks[i]));
   837 		TRAP(err, bytesRestored = TestRestoreL(*restoreClient, TheSqlSrvTestBurInterface->Fs(), KClientUid, KTestDrive, dataChunks[i]));
   722 		TEST2(err, KErrNone);
   838 		TEST2(err, KErrNone);
   723 		
   839 		
   724 		TEST(bytesRestored == bytesStored);
   840 		TEST(bytesRestored == bytesStored);
   725 
   841 
   726 		delete restoreClient;
   842 		delete restoreClient;
   727 
   843 
   728 		CompareDbContentWithBuf(TheTestHarness->Fs());
   844 		CompareDbContentWithBuf(TheSqlSrvTestBurInterface->Fs(), KTestDrive, KClientUid);
   729 		}
   845 		}
   730 	}
   846 	}
   731 
   847 
   732 /**
   848 /**
   733 @SYMTestCaseID			PDS-SQL-UT-4144
   849 @SYMTestCaseID			PDS-SQL-UT-4144
   741 @SYMTestPriority		Medium
   857 @SYMTestPriority		Medium
   742 @SYMREQ					REQ12104
   858 @SYMREQ					REQ12104
   743 */
   859 */
   744 void LegacyFileFormatTest()
   860 void LegacyFileFormatTest()
   745 	{
   861 	{
   746 	TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4144 Backup&Restore: legacy file format test"));
       
   747 
       
   748 	//KBackupFile2 is a database backup file with header version 0.
   862 	//KBackupFile2 is a database backup file with header version 0.
   749 	(void)TheTestHarness->Fs().Delete(KBackupFile2);
   863 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KBackupFile2);
   750 	TInt rc = BaflUtils::CopyFile(TheTestHarness->Fs(), KBackupFile2Z, KBackupFile2);
   864 	TInt rc = BaflUtils::CopyFile(TheSqlSrvTestBurInterface->Fs(), KBackupFile2Z, KBackupFile2);
   751 	TEST2(rc, KErrNone);
   865 	TEST2(rc, KErrNone);
   752 	(void)TheTestHarness->Fs().SetAtt(KBackupFile2, 0, KEntryAttReadOnly);
   866 	(void)TheSqlSrvTestBurInterface->Fs().SetAtt(KBackupFile2, 0, KEntryAttReadOnly);
   753 
   867 
   754 	//Restore the databases from KBackupFile2.
   868 	//Restore the databases from KBackupFile2.
   755 	CSqlBackupClient* restoreClient = NULL;
   869 	CSqlBurCallback* restoreClient = NULL;
   756 	TRAP(rc, restoreClient = CSqlBackupClient::NewL(TheTestHarness));
   870 	TRAP(rc, restoreClient = CSqlBurCallback::NewL(*TheSqlSrvTestBurInterface));
   757 	TEST(restoreClient != NULL);
   871 	TEST(restoreClient != NULL);
   758 
   872 
   759 	RFile file;
   873 	RFile file;
   760 	rc = file.Open(TheTestHarness->Fs(), KBackupFile2, EFileRead | EFileShareExclusive);
   874 	rc = file.Open(TheSqlSrvTestBurInterface->Fs(), KBackupFile2, EFileRead | EFileShareExclusive);
   761 	TEST2(rc, KErrNone);
   875 	TEST2(rc, KErrNone);
   762 	
   876 	
   763 	TRAP(rc, restoreClient->InitialiseRestoreProxyBaseDataL(KClientUid, EDriveC));
   877 	TRAP(rc, restoreClient->InitialiseRestoreProxyBaseDataL(KClientUid, KTestDrive));
   764 	TEST2(rc, KErrNone);
   878 	TEST2(rc, KErrNone);
   765 	
   879 	
   766 	TBuf8<KBufferSize> buf;
   880 	TBuf8<KBufferSize> buf;
   767 	TPtr8 ptr((TUint8*)buf.Ptr(), buf.MaxSize());
   881 	TPtr8 ptr((TUint8*)buf.Ptr(), buf.MaxSize());
   768 	TBool finishedFlag = EFalse;
   882 	TBool finishedFlag = EFalse;
   782 		} 
   896 		} 
   783 	while(fileSize > 0);
   897 	while(fileSize > 0);
   784 	
   898 	
   785 	file.Close();	
   899 	file.Close();	
   786 	
   900 	
   787 	restoreClient->RestoreComplete(EDriveC);
   901 	restoreClient->RestoreComplete(KTestDrive);
   788 	
   902 	
   789 	TEST(finishedFlag);
   903 	TEST(finishedFlag);
   790 		
   904 		
   791 	delete restoreClient;
   905 	delete restoreClient;
   792 
   906 
   823 		}
   937 		}
   824 	stmt.Close();
   938 	stmt.Close();
   825 	TEST2(rc, KSqlAtEnd);
   939 	TEST2(rc, KSqlAtEnd);
   826 	db.Close();
   940 	db.Close();
   827 
   941 
   828 	(void)TheTestHarness->Fs().Delete(KBackupFile2);
   942 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KBackupFile2);
   829 	}
   943 	}
   830 		
   944 		
   831 /**
   945 /**
   832 @SYMTestCaseID			PDS-SQL-UT-4192
   946 @SYMTestCaseID			PDS-SQL-UT-4192
   833 @SYMTestCaseDesc		SQL Backup&Restore - empty backup file list test.
   947 @SYMTestCaseDesc		SQL Backup&Restore - empty backup file list test.
   838 @SYMTestPriority		High
   952 @SYMTestPriority		High
   839 @SYMDEF					DEF145198
   953 @SYMDEF					DEF145198
   840 */
   954 */
   841 void EmptyBackupFileListTest()
   955 void EmptyBackupFileListTest()
   842 	{
   956 	{
   843 	TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4192 Backup&Restore: empty backup file list"));
   957 	CSqlBurCallback* backupClient = NULL;
   844 	
   958 	TRAPD(err, backupClient = CSqlBurCallback::NewL(*TheSqlSrvTestBurInterface));
   845 	CSqlBackupClient* backupClient = NULL;
       
   846 	TRAPD(err, backupClient = CSqlBackupClient::NewL(TheTestHarness));
       
   847 	TEST(backupClient != NULL);
   959 	TEST(backupClient != NULL);
   848 	
   960 	
   849 	TRAP(err, backupClient->InitialiseGetProxyBackupDataL(KNullUid, EDriveC));
   961 	TRAP(err, backupClient->InitialiseGetProxyBackupDataL(KNullUid, KTestDrive));
   850 	TEST2(err, KErrNone);
   962 	TEST2(err, KErrNone);
   851 
   963 
   852 	TBuf8<100> buf;
   964 	TBuf8<100> buf;
   853 	TPtr8 ptr((TUint8*)buf.Ptr(), 0, buf.MaxLength());
   965 	TPtr8 ptr((TUint8*)buf.Ptr(), 0, buf.MaxLength());
   854 	TBool finishedFlag = EFalse;
   966 	TBool finishedFlag = EFalse;
   870 @SYMTestPriority		High
   982 @SYMTestPriority		High
   871 @SYMDEF					DEF145198
   983 @SYMDEF					DEF145198
   872 */
   984 */
   873 void BackupRestoreFileIoErrTest()
   985 void BackupRestoreFileIoErrTest()
   874 	{
   986 	{
   875 	TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4193 Backup: File I/O error simulation test"));
       
   876 
       
   877 	//Make sure that the database content, just before the backup, will be copied to the test biffers.
   987 	//Make sure that the database content, just before the backup, will be copied to the test biffers.
   878 	//The buffers will be used during the restore testing for verification of the database content.
   988 	//The buffers will be used during the restore testing for verification of the database content.
   879 	StoreDbContentToBuf(TheTestHarness->Fs());
   989 	StoreDbContentToBuf(TheSqlSrvTestBurInterface->Fs(), KTestDrive, KClientUid);
   880 	
   990 	
   881 	for(TInt fsError=KErrNotFound;fsError>=KErrBadName;--fsError)
   991 	for(TInt fsError=KErrNotFound;fsError>=KErrBadName;--fsError)
   882 		{
   992 		{
   883 		TheTest.Printf(_L("===Backup&Restore, simulated file system error=%d\r\n"), fsError);
   993 		TheTest.Printf(_L("===Backup&Restore, simulated file system error=%d\r\n"), fsError);
   884 		
   994 		
   886 		TInt bytesStored = -1;
   996 		TInt bytesStored = -1;
   887 		TInt it_cnt1 = 0;
   997 		TInt it_cnt1 = 0;
   888 		for(;err<KErrNone;++it_cnt1)
   998 		for(;err<KErrNone;++it_cnt1)
   889 			{
   999 			{
   890 			__UHEAP_MARK;
  1000 			__UHEAP_MARK;
   891 			(void)TheTestHarness->Fs().SetErrorCondition(fsError, it_cnt1);
  1001 			(void)TheSqlSrvTestBurInterface->Fs().SetErrorCondition(fsError, it_cnt1);
   892 			TRAP(err, bytesStored = DoBackupL());
  1002 			TRAP(err, bytesStored = DoBackupL(KTestDrive, KClientUid));
   893 			(void)TheTestHarness->Fs().SetErrorCondition(KErrNone);
  1003 			(void)TheSqlSrvTestBurInterface->Fs().SetErrorCondition(KErrNone);
   894 			__UHEAP_MARKEND;
  1004 			__UHEAP_MARKEND;
   895 			}
  1005 			}
   896 		TEST2(err, KErrNone);
  1006 		TEST2(err, KErrNone);
   897 	
  1007 	
   898 		err = KErrGeneral;
  1008 		err = KErrGeneral;
   899 		TInt bytesRestored = -1;
  1009 		TInt bytesRestored = -1;
   900 		TInt it_cnt2 = 0;
  1010 		TInt it_cnt2 = 0;
   901 	    for(;err<KErrNone;++it_cnt2)
  1011 	    for(;err<KErrNone;++it_cnt2)
   902 			{
  1012 			{
   903 			__UHEAP_MARK;
  1013 			__UHEAP_MARK;
   904 			(void)TheTestHarness->Fs().SetErrorCondition(fsError, it_cnt2);
  1014 			(void)TheSqlSrvTestBurInterface->Fs().SetErrorCondition(fsError, it_cnt2);
   905 			TRAP(err, bytesRestored = DoRestoreL());
  1015 			TRAP(err, bytesRestored = DoRestoreL(KTestDrive, KClientUid));
   906 			(void)TheTestHarness->Fs().SetErrorCondition(KErrNone);
  1016 			(void)TheSqlSrvTestBurInterface->Fs().SetErrorCondition(KErrNone);
   907 			__UHEAP_MARKEND;
  1017 			__UHEAP_MARKEND;
   908 			}
  1018 			}
   909 		TEST2(err, KErrNone);
  1019 		TEST2(err, KErrNone);
   910 		
  1020 		
   911 		TEST2(bytesStored, bytesRestored);
  1021 		TEST2(bytesStored, bytesRestored);
   912 		CompareDbContentWithBuf(TheTestHarness->Fs());
  1022 		CompareDbContentWithBuf(TheSqlSrvTestBurInterface->Fs(), KTestDrive, KClientUid);
   913 		
  1023 		
   914 		TheTest.Printf(_L("Backup&Restore file I/O error simulation test succeeded at backup iteration %d and restore itreration %d\r\n"), it_cnt1 - 1, it_cnt2 - 1);
  1024 		TheTest.Printf(_L("Backup&Restore file I/O error simulation test succeeded at backup iteration %d and restore itreration %d\r\n"), it_cnt1 - 1, it_cnt2 - 1);
   915 		}
  1025 		}
   916 	}
  1026 	}
   917 
  1027 
       
  1028 /**
       
  1029 @SYMTestCaseID			PDS-SQL-UT-4225
       
  1030 @SYMTestCaseDesc		SQL Backup - zero size file backup.
       
  1031 						The test executes a backup on a file with zero size.
       
  1032 @SYMTestActions			SQL Backup - zero size file backup.
       
  1033 @SYMTestExpectedResults Test must not fail
       
  1034 @SYMTestPriority		High
       
  1035 */
       
  1036 void BackupZeroSizeFileTest()
       
  1037 	{
       
  1038 	CSqlBurCallback* backupClient = NULL;
       
  1039 	TRAPD(err, backupClient = CSqlBurCallback::NewL(*TheSqlSrvTestBurInterface));
       
  1040 	TEST2(err, KErrNone);
       
  1041 	TEST(backupClient != NULL);
       
  1042 	//
       
  1043 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KZeroSizeFile);
       
  1044 	RFile file;
       
  1045 	err = file.Replace(TheSqlSrvTestBurInterface->Fs(), KZeroSizeFile, EFileRead | EFileWrite);
       
  1046 	TEST2(err, KErrNone);
       
  1047 	file.Close();
       
  1048 	//
       
  1049 	TRAP(err, TestBackupL(*backupClient, TheSqlSrvTestBurInterface->Fs(), KZeroFileSizeUid, KTestDrive));
       
  1050 	TEST2(err, KErrNone);
       
  1051 	//Write something to the file
       
  1052 	err = file.Open(TheSqlSrvTestBurInterface->Fs(), KZeroSizeFile, EFileRead | EFileWrite);
       
  1053 	TEST2(err, KErrNone);
       
  1054 	_LIT8(KData, "123456787989");
       
  1055 	err = file.Write(KData);
       
  1056 	TEST2(err, KErrNone);
       
  1057 	err = file.Flush();
       
  1058 	TEST2(err, KErrNone);
       
  1059 	file.Close();
       
  1060 	//Do the restore. After the restore the file size should be 0.
       
  1061 	TRAP(err, TestRestoreL(*backupClient, TheSqlSrvTestBurInterface->Fs(), KZeroFileSizeUid, KTestDrive));
       
  1062 	TEST2(err, KErrNone);
       
  1063 	err = file.Open(TheSqlSrvTestBurInterface->Fs(), KZeroSizeFile, EFileRead | EFileWrite);
       
  1064 	TEST2(err, KErrNone);
       
  1065 	TInt size;
       
  1066 	err = file.Size(size);
       
  1067 	TEST2(err, KErrNone);
       
  1068 	file.Close();
       
  1069 	TEST2(size, 0);
       
  1070 	//Write something to the file
       
  1071 	err = file.Open(TheSqlSrvTestBurInterface->Fs(), KZeroSizeFile, EFileRead | EFileWrite);
       
  1072 	TEST2(err, KErrNone);
       
  1073 	err = file.Write(KData);
       
  1074 	TEST2(err, KErrNone);
       
  1075 	err = file.Flush();
       
  1076 	TEST2(err, KErrNone);
       
  1077 	file.Close();
       
  1078 	//Call RestoreBaseDataSectionL() with 0 data in the input buffer and finished flag: false.
       
  1079 	TRAP(err, backupClient->InitialiseRestoreProxyBaseDataL(KZeroFileSizeUid, KTestDrive));
       
  1080 	TEST2(err, KErrNone);
       
  1081 	TPtrC8 zeroBuf;
       
  1082 	TRAP(err, backupClient->RestoreBaseDataSectionL(zeroBuf, EFalse));
       
  1083 	TEST2(err, KErrNone);
       
  1084 	//No restore in this case, 
       
  1085 	err = file.Open(TheSqlSrvTestBurInterface->Fs(), KZeroSizeFile, EFileRead | EFileWrite);
       
  1086 	TEST2(err, KErrNone);
       
  1087 	err = file.Size(size);
       
  1088 	TEST2(err, KErrNone);
       
  1089 	file.Close();
       
  1090 	TEST(size > 0);
       
  1091 	//Call RestoreBaseDataSectionL() with 0 data in the input buffer and finished flag: true.
       
  1092 	TRAP(err, backupClient->InitialiseRestoreProxyBaseDataL(KZeroFileSizeUid, KTestDrive));
       
  1093 	TEST2(err, KErrNone);
       
  1094 	TRAP(err, backupClient->RestoreBaseDataSectionL(zeroBuf, ETrue));
       
  1095 	TEST2(err, KErrNone);
       
  1096 	//No restore in this case, 
       
  1097 	err = file.Open(TheSqlSrvTestBurInterface->Fs(), KZeroSizeFile, EFileRead | EFileWrite);
       
  1098 	TEST2(err, KErrNone);
       
  1099 	err = file.Size(size);
       
  1100 	TEST2(err, KErrNone);
       
  1101 	file.Close();
       
  1102 	TEST(size > 0);
       
  1103 	//
       
  1104 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KZeroSizeFile);
       
  1105 	delete backupClient;
       
  1106 	}
       
  1107 
       
  1108 /**
       
  1109 @SYMTestCaseID			PDS-SQL-UT-4226
       
  1110 @SYMTestCaseDesc		SQL Restore - corrupted archive 1.
       
  1111 						The test does a backup of a file with a non-zero size.
       
  1112 						The the test modifies the archive, simulating a corruption.
       
  1113 						The the test performs a restore from the archive. The corruption
       
  1114 						should be detected and reported by SQL B&R code.
       
  1115 @SYMTestActions			SQL Restore - corrupted archive 1.
       
  1116 @SYMTestExpectedResults Test must not fail
       
  1117 @SYMTestPriority		High
       
  1118 */
       
  1119 void CorruptedArchiveTest1()
       
  1120 	{
       
  1121 	CSqlBurCallback* backupClient = NULL;
       
  1122 	TRAPD(err, backupClient = CSqlBurCallback::NewL(*TheSqlSrvTestBurInterface));
       
  1123 	TEST2(err, KErrNone);
       
  1124 	TEST(backupClient != NULL);
       
  1125 	//Create the test file that will be sent for backup
       
  1126 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KTestFile1);
       
  1127 	RFile file;
       
  1128 	err = file.Replace(TheSqlSrvTestBurInterface->Fs(), KTestFile1, EFileRead | EFileWrite);
       
  1129 	TEST2(err, KErrNone);
       
  1130 	_LIT8(KData1, "123456787989");
       
  1131 	err = file.Write(KData1);
       
  1132 	TEST2(err, KErrNone);
       
  1133 	err = file.Flush();
       
  1134 	TEST2(err, KErrNone);
       
  1135 	file.Close();
       
  1136 	//Backup the file
       
  1137 	TRAP(err, TestBackupL(*backupClient, TheSqlSrvTestBurInterface->Fs(), KTestClientUid1, KTestDrive));
       
  1138 	TEST2(err, KErrNone);
       
  1139 	//Modify the file, which was sent for backup
       
  1140 	err = file.Open(TheSqlSrvTestBurInterface->Fs(), KTestFile1, EFileRead | EFileWrite);
       
  1141 	TEST2(err, KErrNone);
       
  1142 	_LIT8(KData2, "ABCDEFGH");
       
  1143 	err = file.Write(KData2);
       
  1144 	TEST2(err, KErrNone);
       
  1145 	err = file.SetSize(KData2().Length());
       
  1146 	TEST2(err, KErrNone);
       
  1147 	err = file.Flush();
       
  1148 	TEST2(err, KErrNone);
       
  1149 	file.Close();
       
  1150 	//Corrupt the archive
       
  1151 	TFileName backupFileName;
       
  1152 	backupFileName.Copy(KBackupFileTemplate);
       
  1153 	backupFileName.Append((static_cast <TUid> (KTestClientUid1)).Name());
       
  1154 	err = file.Open(TheSqlSrvTestBurInterface->Fs(), backupFileName, EFileRead | EFileWrite);
       
  1155 	TEST2(err, KErrNone);
       
  1156 	TInt pos = -3;
       
  1157 	err = file.Seek(ESeekEnd, pos);
       
  1158 	TEST2(err, KErrNone);
       
  1159 	_LIT8(KData3, "ERR");
       
  1160 	err = file.Write(KData3);
       
  1161 	TEST2(err, KErrNone);
       
  1162 	err = file.Flush();
       
  1163 	TEST2(err, KErrNone);
       
  1164 	file.Close();
       
  1165 	//Try to restore the archive
       
  1166 	TRAP(err, TestRestoreL(*backupClient, TheSqlSrvTestBurInterface->Fs(), KTestClientUid1, KTestDrive));
       
  1167 	TEST2(err, KErrCorrupt);
       
  1168 	//Check that the file really has not been restored
       
  1169 	err = file.Open(TheSqlSrvTestBurInterface->Fs(), KTestFile1, EFileRead | EFileWrite);
       
  1170 	TEST2(err, KErrNone);
       
  1171 	TBuf8<50> data;
       
  1172 	err = file.Read(data);
       
  1173 	TEST2(err, KErrNone);
       
  1174 	file.Close();
       
  1175 	TEST(data == KData2);
       
  1176 	//
       
  1177 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KTestFile1);
       
  1178 	delete backupClient;
       
  1179 	}
       
  1180 
       
  1181 /**
       
  1182 @SYMTestCaseID			PDS-SQL-UT-4227
       
  1183 @SYMTestCaseDesc		SQL Restore - corrupted archive 2.
       
  1184 						The test does a backup of a file with a non-zero size.
       
  1185 						Then the test modifies the archive, byte after byte each step,
       
  1186 						simulating a corruption.
       
  1187 						The the test performs a restore from the archive. The corruption
       
  1188 						should be detected and reported by SQL B&R code.
       
  1189 @SYMTestActions			SQL Restore - corrupted archive 2.
       
  1190 @SYMTestExpectedResults Test must not fail
       
  1191 @SYMTestPriority		High
       
  1192 */
       
  1193 void CorruptedArchiveTest2()
       
  1194 	{
       
  1195 	CSqlBurCallback* backupClient = NULL;
       
  1196 	TRAPD(err, backupClient = CSqlBurCallback::NewL(*TheSqlSrvTestBurInterface));
       
  1197 	TEST2(err, KErrNone);
       
  1198 	TEST(backupClient != NULL);
       
  1199 	//Create the test file that will be sent for backup
       
  1200 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KTestFile1);
       
  1201 	RFile file;
       
  1202 	err = file.Replace(TheSqlSrvTestBurInterface->Fs(), KTestFile1, EFileRead | EFileWrite);
       
  1203 	TEST2(err, KErrNone);
       
  1204 	_LIT8(KData1, "123456787989");
       
  1205 	err = file.Write(KData1);
       
  1206 	TEST2(err, KErrNone);
       
  1207 	err = file.Flush();
       
  1208 	TEST2(err, KErrNone);
       
  1209 	file.Close();
       
  1210 	//Backup the file
       
  1211 	TRAP(err, TestBackupL(*backupClient, TheSqlSrvTestBurInterface->Fs(), KTestClientUid1, KTestDrive));
       
  1212 	TEST2(err, KErrNone);
       
  1213 	//Modify the file, which was sent for backup
       
  1214 	err = file.Open(TheSqlSrvTestBurInterface->Fs(), KTestFile1, EFileRead | EFileWrite);
       
  1215 	TEST2(err, KErrNone);
       
  1216 	_LIT8(KData2, "ABCDEFGH");
       
  1217 	err = file.Write(KData2);
       
  1218 	TEST2(err, KErrNone);
       
  1219 	err = file.SetSize(KData2().Length());
       
  1220 	TEST2(err, KErrNone);
       
  1221 	err = file.Flush();
       
  1222 	TEST2(err, KErrNone);
       
  1223 	file.Close();
       
  1224 	//Make a copy of the modified file
       
  1225 	CFileMan* fm = NULL;
       
  1226 	TRAP(err, fm = CFileMan::NewL(TheSqlSrvTestBurInterface->Fs()));
       
  1227 	TEST2(err, KErrNone);
       
  1228 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KTestFile1Bak);
       
  1229 	err = fm->Copy(KTestFile1, KTestFile1Bak);
       
  1230 	TEST2(err, KErrNone);
       
  1231 	//Get the archive size
       
  1232 	TFileName backupFileName;
       
  1233 	backupFileName.Copy(KBackupFileTemplate);
       
  1234 	backupFileName.Append((static_cast <TUid> (KTestClientUid1)).Name());
       
  1235 	err = file.Open(TheSqlSrvTestBurInterface->Fs(), backupFileName, EFileRead | EFileWrite);
       
  1236 	TEST2(err, KErrNone);
       
  1237 	TInt size = 0;
       
  1238 	err = file.Size(size);
       
  1239 	TEST2(err, KErrNone);
       
  1240 	file.Close();
       
  1241 	//Save a copy of the archive
       
  1242 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KBackupCopy);
       
  1243 	err = fm->Copy(backupFileName, KBackupCopy);
       
  1244 	TEST2(err, KErrNone);
       
  1245 	//On each iteration step: corrupt the archive and try to do a restore from it.
       
  1246 	for(TInt i=0;i<size;++i)
       
  1247 		{
       
  1248 		//Change 1 byte in the archive
       
  1249 		err = file.Open(TheSqlSrvTestBurInterface->Fs(), backupFileName, EFileRead | EFileWrite);
       
  1250 		TEST2(err, KErrNone);
       
  1251 		TInt pos = i;
       
  1252 		err = file.Seek(ESeekStart, pos);
       
  1253 		TEST2(err, KErrNone);
       
  1254 		TBuf8<1> byte;
       
  1255 		err = file.Read(byte);
       
  1256 		TEST2(err, KErrNone);
       
  1257 		++byte[0];
       
  1258 		err = file.Seek(ESeekStart, pos);
       
  1259 		TEST2(err, KErrNone);
       
  1260 		err = file.Write(byte);
       
  1261 		TEST2(err, KErrNone);
       
  1262 		if(i == (size - 1) && (size & 0x01) == 0)
       
  1263 			{//Make the file size an odd number, just to test....  
       
  1264 			err = file.Write(byte);
       
  1265 			TEST2(err, KErrNone);
       
  1266 			}
       
  1267 		err = file.Flush();
       
  1268 		TEST2(err, KErrNone);
       
  1269 		file.Close();
       
  1270 		//Restore
       
  1271 		TRAP(err, TestRestoreL(*backupClient, TheSqlSrvTestBurInterface->Fs(), KTestClientUid1, KTestDrive));
       
  1272 		TheTest.Printf(_L("Iteration %d, err=%d\r\n"), i, err);
       
  1273 		if(err == KErrNone)
       
  1274 			{
       
  1275 			//Delete the restored file. The reason that the restore didn't fail is because only the file data is 
       
  1276 			//protected with checksum. The restore file header - not. The restore completed, the data was restored
       
  1277 			//to a file with different name. Or even to a file with the same name. Delete created file(s).
       
  1278 			(void)fm->Delete(KTestDeleteMask1);
       
  1279 			(void)fm->Delete(KTestDeleteMask2);
       
  1280 			}
       
  1281 		else
       
  1282 			{
       
  1283 			//The restore completed with an error. The file content is preserved.
       
  1284 			//Check that the file content is the same. 
       
  1285 			err = file.Open(TheSqlSrvTestBurInterface->Fs(), KTestFile1, EFileRead | EFileWrite);
       
  1286 			TEST2(err, KErrNone);
       
  1287 			TBuf8<50> data;
       
  1288 			err = file.Read(data);
       
  1289 			TEST2(err, KErrNone);
       
  1290 			file.Close();
       
  1291 			TEST(data == KData2);
       
  1292 			}
       
  1293 		//Restore the file from the backup copy
       
  1294 		(void)TheSqlSrvTestBurInterface->Fs().Delete(KTestFile1);
       
  1295 		err = fm->Copy(KTestFile1Bak, KTestFile1);
       
  1296 		TEST2(err, KErrNone);
       
  1297 		//Restore the archive from the good copy.
       
  1298 		(void)TheSqlSrvTestBurInterface->Fs().Delete(backupFileName);
       
  1299 		err = fm->Copy(KBackupCopy, backupFileName);
       
  1300 		TEST2(err, KErrNone);
       
  1301 		}
       
  1302 	//
       
  1303 	delete fm;
       
  1304 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KTestFile1);
       
  1305 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KTestFile1Bak);
       
  1306 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KBackupCopy);
       
  1307 	delete backupClient;
       
  1308 	}
       
  1309 
       
  1310 /**
       
  1311 @SYMTestCaseID			PDS-SQL-UT-4228
       
  1312 @SYMTestCaseDesc		SQL Backup&Restore on a drive different than KTestDrive (C: by default).
       
  1313 						The test creates one database on KTestDrive and another database 
       
  1314 						with the same name on a drive different than KTestDrive.
       
  1315 						Then the test backups the "not KTestDrive" drive and restores
       
  1316 						the data after that. The test verifies that the backup&restore
       
  1317 						really impacts only the other drive, not KTestDrive.
       
  1318 @SYMTestActions			SQL Backup&Restore on a drive different than KTestDrive (C: by default).
       
  1319 @SYMTestExpectedResults Test must not fail
       
  1320 @SYMTestPriority		High
       
  1321 */
       
  1322 void DbDriveTest()
       
  1323 	{
       
  1324 	CSqlBurCallback* backupClient = NULL;
       
  1325 	TRAPD(err, backupClient = CSqlBurCallback::NewL(*TheSqlSrvTestBurInterface));
       
  1326 	TEST2(err, KErrNone);
       
  1327 	TEST(backupClient != NULL);
       
  1328 	//
       
  1329 	TDriveUnit driveUnitDefault(KTestDrive);
       
  1330 	TDriveName driveNameDefault = driveUnitDefault.Name(); 
       
  1331 	//Find a r/w drive, different than KTestDrive
       
  1332 	TInt drive = EDriveA;
       
  1333 	for(;drive<=EDriveZ;++drive)
       
  1334 		{
       
  1335 		if(drive == KTestDrive)
       
  1336 			{
       
  1337 			continue;
       
  1338 			}
       
  1339 		TDriveInfo driveInfo;
       
  1340 		err = TheSqlSrvTestBurInterface->Fs().Drive(driveInfo, drive);
       
  1341 		if(err != KErrNone)
       
  1342 			{
       
  1343 			continue;
       
  1344 			}
       
  1345 		if(driveInfo.iDriveAtt & KDriveAttRom)
       
  1346 			{
       
  1347 			continue;
       
  1348 			}
       
  1349 		//
       
  1350 		TDriveUnit driveUnit(drive);
       
  1351 		TDriveName driveName = driveUnit.Name();
       
  1352 		//
       
  1353 		TVolumeInfo vinfo;
       
  1354 		err = TheSqlSrvTestBurInterface->Fs().Volume(vinfo, drive);
       
  1355 		if(err != KErrNone)
       
  1356 			{
       
  1357 			TheTest.Printf(_L("Drive %S, RFs::Volume() err=%d\r\n"), &driveName, err);
       
  1358 			continue;
       
  1359 			}
       
  1360 		//R/W drive found.
       
  1361 		TheTest.Printf(_L("Test drive: %S\r\n"), &driveName);
       
  1362 		TParse parse;
       
  1363 		err = parse.Set(KTestFile2, &driveName, 0);
       
  1364 		TEST2(err, KErrNone);
       
  1365 		//Create the test file that will be sent for backup
       
  1366 		TPtrC fname1(parse.FullName());
       
  1367 		TheTest.Printf(_L("Test file 1: %S\r\n"), &fname1);
       
  1368 		err = TheSqlSrvTestBurInterface->Fs().MkDirAll(parse.FullName());
       
  1369 		TEST(err == KErrNone || err == KErrAlreadyExists);
       
  1370 		(void)TheSqlSrvTestBurInterface->Fs().Delete(parse.FullName());
       
  1371 		RFile file;
       
  1372 		err = file.Replace(TheSqlSrvTestBurInterface->Fs(), parse.FullName(), EFileRead | EFileWrite);
       
  1373 		TEST2(err, KErrNone);
       
  1374 		_LIT8(KData1, "123456787989");
       
  1375 		err = file.Write(KData1);
       
  1376 		TEST2(err, KErrNone);
       
  1377 		err = file.Flush();
       
  1378 		TEST2(err, KErrNone);
       
  1379 		file.Close();
       
  1380 		//Create a test file with the same name on drive KTestDrive 
       
  1381 		err = parse.Set(KTestFile2, &driveNameDefault, 0);
       
  1382 		TEST2(err, KErrNone);
       
  1383 		TPtrC fname2(parse.FullName());
       
  1384 		TheTest.Printf(_L("Test file 2: %S\r\n"), &fname2);
       
  1385 		(void)TheSqlSrvTestBurInterface->Fs().Delete(parse.FullName());
       
  1386 		err = file.Replace(TheSqlSrvTestBurInterface->Fs(), parse.FullName(), EFileRead | EFileWrite);
       
  1387 		TEST2(err, KErrNone);
       
  1388 		_LIT8(KData2, "ABCDEFG");
       
  1389 		err = file.Write(KData2);
       
  1390 		TEST2(err, KErrNone);
       
  1391 		err = file.Flush();
       
  1392 		TEST2(err, KErrNone);
       
  1393 		file.Close();
       
  1394 		//Do the backup on "drive"
       
  1395 		TRAP(err, TestBackupL(*backupClient, TheSqlSrvTestBurInterface->Fs(), KTestClientUid2, (TDriveNumber)drive));
       
  1396 		TEST2(err, KErrNone);
       
  1397 		//Modify the file that was sent for backup
       
  1398 		err = parse.Set(KTestFile2, &driveName, 0);
       
  1399 		TEST2(err, KErrNone);
       
  1400 		err = file.Open(TheSqlSrvTestBurInterface->Fs(), parse.FullName(), EFileRead | EFileWrite);
       
  1401 		TEST2(err, KErrNone);
       
  1402 		_LIT8(KData3, "ABCDEFGHYYYYYY");
       
  1403 		err = file.Write(KData3);
       
  1404 		TEST2(err, KErrNone);
       
  1405 		err = file.Flush();
       
  1406 		TEST2(err, KErrNone);
       
  1407 		file.Close();
       
  1408 		//Do the restore on "drive"
       
  1409 		TRAP(err, TestRestoreL(*backupClient, TheSqlSrvTestBurInterface->Fs(), KTestClientUid2, (TDriveNumber)drive));
       
  1410 		TEST2(err, KErrNone);
       
  1411 		//Verify the content of the restored file
       
  1412 		err = parse.Set(KTestFile2, &driveName, 0);
       
  1413 		TEST2(err, KErrNone);
       
  1414 		err = file.Open(TheSqlSrvTestBurInterface->Fs(), parse.FullName(), EFileRead | EFileWrite);
       
  1415 		TEST2(err, KErrNone);
       
  1416 		TBuf8<50> data;
       
  1417 		err = file.Read(data);
       
  1418 		TEST2(err, KErrNone);
       
  1419 		file.Close();
       
  1420 		TEST(data == KData1);
       
  1421 		//Verify the content of the file on drive KTestDrive. It should be the same as before the backup&restore.
       
  1422 		err = parse.Set(KTestFile2, &driveNameDefault, 0);
       
  1423 		TEST2(err, KErrNone);
       
  1424 		err = file.Open(TheSqlSrvTestBurInterface->Fs(), parse.FullName(), EFileRead | EFileWrite);
       
  1425 		TEST2(err, KErrNone);
       
  1426 		err = file.Read(data);
       
  1427 		TEST2(err, KErrNone);
       
  1428 		file.Close();
       
  1429 		TEST(data == KData2);
       
  1430 		//Cleanup
       
  1431 		err = parse.Set(KTestFile2, &driveNameDefault, 0);
       
  1432 		TEST2(err, KErrNone);
       
  1433 		(void)TheSqlSrvTestBurInterface->Fs().Delete(parse.FullName());
       
  1434 		err = parse.Set(KTestFile2, &driveName, 0);
       
  1435 		TEST2(err, KErrNone);
       
  1436 		(void)TheSqlSrvTestBurInterface->Fs().Delete(parse.FullName());
       
  1437 		break;
       
  1438 		}
       
  1439 	delete backupClient;
       
  1440 	if(drive > EDriveZ)
       
  1441 		{
       
  1442 		TheTest.Printf(_L("No R/W drive has been found, different than %S\r\n"), &driveNameDefault);
       
  1443 		}
       
  1444 	}
       
  1445 
       
  1446 /**
       
  1447 @SYMTestCaseID			PDS-SQL-UT-4229
       
  1448 @SYMTestCaseDesc		SQL Backup&Restore with locked file.
       
  1449 						The test creates two test files on the same drive and with the same uid.
       
  1450 						Then the test backups the databases. After the backup the test simulates that
       
  1451 						the first file is "in use". Then the test performs a restore.
       
  1452 						The expected result: the locked file is not restored but the other file is restored.
       
  1453 @SYMTestActions			SQL Backup&Restore with locked file.
       
  1454 @SYMTestExpectedResults Test must not fail
       
  1455 @SYMTestPriority		High
       
  1456 */
       
  1457 void LockFileTest()
       
  1458 	{
       
  1459 	CSqlBurCallback* backupClient = NULL;
       
  1460 	TRAPD(err, backupClient = CSqlBurCallback::NewL(*TheSqlSrvTestBurInterface));
       
  1461 	TEST2(err, KErrNone);
       
  1462 	TEST(backupClient != NULL);
       
  1463 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KTestFile3);
       
  1464 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KTestFile4);
       
  1465 	//Create the files. File 1.
       
  1466 	RFile file;
       
  1467 	err = file.Replace(TheSqlSrvTestBurInterface->Fs(), KTestFile3, EFileRead | EFileWrite);
       
  1468 	TEST2(err, KErrNone);
       
  1469 	_LIT8(KData1, "123456787989");
       
  1470 	err = file.Write(KData1);
       
  1471 	TEST2(err, KErrNone);
       
  1472 	err = file.Flush();
       
  1473 	TEST2(err, KErrNone);
       
  1474 	file.Close();
       
  1475 	//...file 2
       
  1476 	err = file.Replace(TheSqlSrvTestBurInterface->Fs(), KTestFile4, EFileRead | EFileWrite);
       
  1477 	TEST2(err, KErrNone);
       
  1478 	_LIT8(KData2, "ABCDEF");
       
  1479 	err = file.Write(KData2);
       
  1480 	TEST2(err, KErrNone);
       
  1481 	err = file.Flush();
       
  1482 	TEST2(err, KErrNone);
       
  1483 	file.Close();
       
  1484 	//Backup
       
  1485 	TRAP(err, TestBackupL(*backupClient, TheSqlSrvTestBurInterface->Fs(), KTestClientUid3, KTestDrive));
       
  1486 	TEST2(err, KErrNone);
       
  1487 	//Modify the files. Keep the first file opened.
       
  1488 	RFile file1;
       
  1489 	err = file1.Open(TheSqlSrvTestBurInterface->Fs(), KTestFile3, EFileRead | EFileWrite);
       
  1490 	TEST2(err, KErrNone);
       
  1491 	_LIT8(KData3, "YYYYYQQQQQQQQQQQ");
       
  1492 	err = file1.Write(KData3);
       
  1493 	TEST2(err, KErrNone);
       
  1494 	err = file1.Flush();
       
  1495 	TEST2(err, KErrNone);
       
  1496 	//...file 2
       
  1497 	err = file.Open(TheSqlSrvTestBurInterface->Fs(), KTestFile4, EFileRead | EFileWrite);
       
  1498 	TEST2(err, KErrNone);
       
  1499 	_LIT8(KData4, "5545495444j32322332234223432");
       
  1500 	err = file.Write(KData4);
       
  1501 	TEST2(err, KErrNone);
       
  1502 	err = file.Flush();
       
  1503 	TEST2(err, KErrNone);
       
  1504 	file.Close();
       
  1505 	//Restore. The reported error should be KErrInUse.
       
  1506 	TRAP(err, TestRestoreL(*backupClient, TheSqlSrvTestBurInterface->Fs(), KTestClientUid3, KTestDrive));
       
  1507 	TEST2(err, KErrInUse);
       
  1508 	//Close file 1 and check the content. It should be the same as after the backup
       
  1509 	file1.Close();
       
  1510 	err = file.Open(TheSqlSrvTestBurInterface->Fs(), KTestFile3, EFileRead | EFileWrite);
       
  1511 	TEST2(err, KErrNone);
       
  1512 	TBuf8<50> data;
       
  1513 	err = file.Read(data);
       
  1514 	TEST2(err, KErrNone);
       
  1515 	file.Close();
       
  1516 	TEST(data == KData3);
       
  1517 	//File2:  check the content. It should be the same as before the backup
       
  1518 	err = file.Open(TheSqlSrvTestBurInterface->Fs(), KTestFile4, EFileRead | EFileWrite);
       
  1519 	TEST2(err, KErrNone);
       
  1520 	err = file.Read(data);
       
  1521 	TEST2(err, KErrNone);
       
  1522 	file.Close();
       
  1523 	TEST(data == KData2);
       
  1524 	//
       
  1525 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KTestFile4);
       
  1526 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KTestFile3);
       
  1527 	delete backupClient;
       
  1528 	}
       
  1529 
       
  1530 /**
       
  1531 @SYMTestCaseID			PDS-SQL-UT-4230
       
  1532 @SYMTestCaseDesc		SQL Backup&Restore with locked file - test 2.
       
  1533 						The test creates two test files on the same drive and with different uids.
       
  1534 						Then the test backups the databases. After the backup the test simulates that
       
  1535 						the first file is "in use". Then the test performs a restore.
       
  1536 						The expected result: the locked file is not restored but the other file is restored.
       
  1537 @SYMTestActions			SQL Backup&Restore with locked file - test 2.
       
  1538 @SYMTestExpectedResults Test must not fail
       
  1539 @SYMTestPriority		High
       
  1540 */
       
  1541 void LockFileTest2()
       
  1542 	{
       
  1543 	CSqlBurCallback* backupClient = NULL;
       
  1544 	TRAPD(err, backupClient = CSqlBurCallback::NewL(*TheSqlSrvTestBurInterface));
       
  1545 	TEST2(err, KErrNone);
       
  1546 	TEST(backupClient != NULL);
       
  1547 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KTestFile1);//KTestClientUid1 used
       
  1548 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KTestFile4);//KTestClientUid3 used
       
  1549 	//Create the files. File 1.
       
  1550 	RFile file;
       
  1551 	err = file.Replace(TheSqlSrvTestBurInterface->Fs(), KTestFile1, EFileRead | EFileWrite);
       
  1552 	TEST2(err, KErrNone);
       
  1553 	_LIT8(KData1, "123456787989");
       
  1554 	err = file.Write(KData1);
       
  1555 	TEST2(err, KErrNone);
       
  1556 	err = file.Flush();
       
  1557 	TEST2(err, KErrNone);
       
  1558 	file.Close();
       
  1559 	//...file 2
       
  1560 	err = file.Replace(TheSqlSrvTestBurInterface->Fs(), KTestFile4, EFileRead | EFileWrite);
       
  1561 	TEST2(err, KErrNone);
       
  1562 	_LIT8(KData2, "ABCDEF");
       
  1563 	err = file.Write(KData2);
       
  1564 	TEST2(err, KErrNone);
       
  1565 	err = file.Flush();
       
  1566 	TEST2(err, KErrNone);
       
  1567 	file.Close();
       
  1568 	//Backup
       
  1569 	TRAP(err, TestBackupL(*backupClient, TheSqlSrvTestBurInterface->Fs(), KTestClientUid1, KTestDrive));
       
  1570 	TEST2(err, KErrNone);
       
  1571 	TRAP(err, TestBackupL(*backupClient, TheSqlSrvTestBurInterface->Fs(), KTestClientUid3, KTestDrive));
       
  1572 	TEST2(err, KErrNone);
       
  1573 	//Modify the files. Keep the first file opened.
       
  1574 	RFile file1;
       
  1575 	err = file1.Open(TheSqlSrvTestBurInterface->Fs(), KTestFile1, EFileRead | EFileWrite);
       
  1576 	TEST2(err, KErrNone);
       
  1577 	_LIT8(KData3, "YYYYYQQQQQQQQQQQ");
       
  1578 	err = file1.Write(KData3);
       
  1579 	TEST2(err, KErrNone);
       
  1580 	err = file1.Flush();
       
  1581 	TEST2(err, KErrNone);
       
  1582 	//...file 2
       
  1583 	err = file.Open(TheSqlSrvTestBurInterface->Fs(), KTestFile4, EFileRead | EFileWrite);
       
  1584 	TEST2(err, KErrNone);
       
  1585 	_LIT8(KData4, "5545495444j32322332234223432");
       
  1586 	err = file.Write(KData4);
       
  1587 	TEST2(err, KErrNone);
       
  1588 	err = file.Flush();
       
  1589 	TEST2(err, KErrNone);
       
  1590 	file.Close();
       
  1591 	//Restore the first file. The reported error should be KErrInUse.
       
  1592 	TRAP(err, TestRestoreL(*backupClient, TheSqlSrvTestBurInterface->Fs(), KTestClientUid1, KTestDrive));
       
  1593 	TEST2(err, KErrInUse);
       
  1594 	//Restore the second file. The reported error should be KErrNone.
       
  1595 	TRAP(err, TestRestoreL(*backupClient, TheSqlSrvTestBurInterface->Fs(), KTestClientUid3, KTestDrive));
       
  1596 	TEST2(err, KErrNone);
       
  1597 	//Close file 1 and check the content. It should be the same as after the backup
       
  1598 	file1.Close();
       
  1599 	err = file.Open(TheSqlSrvTestBurInterface->Fs(), KTestFile1, EFileRead | EFileWrite);
       
  1600 	TEST2(err, KErrNone);
       
  1601 	TBuf8<50> data;
       
  1602 	err = file.Read(data);
       
  1603 	TEST2(err, KErrNone);
       
  1604 	file.Close();
       
  1605 	TEST(data == KData3);
       
  1606 	//File2:  check the content. It should be the same as before the backup
       
  1607 	err = file.Open(TheSqlSrvTestBurInterface->Fs(), KTestFile4, EFileRead | EFileWrite);
       
  1608 	TEST2(err, KErrNone);
       
  1609 	err = file.Read(data);
       
  1610 	TEST2(err, KErrNone);
       
  1611 	file.Close();
       
  1612 	TEST(data == KData2);
       
  1613 	//
       
  1614 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KTestFile4);
       
  1615 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KTestFile1);
       
  1616 	delete backupClient;
       
  1617 	}
       
  1618 
       
  1619 CDir* GetPrivateDirContent(TDriveNumber aDrive)
       
  1620 	{
       
  1621 	TDriveUnit driveUnit(aDrive);
       
  1622 	TDriveName driveName = driveUnit.Name();
       
  1623 	TFileName path;
       
  1624 	path.Copy(driveName);
       
  1625 	path.Append(KPrivateDir);
       
  1626 	_LIT(KMatchAllDbFiles, "*");
       
  1627 	path.Append(KMatchAllDbFiles);
       
  1628 	//Do the search
       
  1629 	CDir* fileNameCol = NULL;
       
  1630 	TInt err = TheSqlSrvTestBurInterface->Fs().GetDir(path, KEntryAttNormal, ESortByName, fileNameCol);
       
  1631 	TEST2(err, KErrNone);
       
  1632 	return fileNameCol;
       
  1633 	}
       
  1634 
       
  1635 /**
       
  1636 @SYMTestCaseID			PDS-SQL-UT-4231
       
  1637 @SYMTestCaseDesc		SQL Backup&Restore - directory content test.
       
  1638 						The test stores into an array information regarding all files in 
       
  1639 						SQL private datacage. Then the test backups one of the files and modifies
       
  1640 						the file after that. Then the test does a restore. Expected result - the only
       
  1641 						modifed file should be the file which was sent for backup.
       
  1642 @SYMTestActions			SQL Backup&Restore - directory content test.
       
  1643 @SYMTestExpectedResults Test must not fail
       
  1644 @SYMTestPriority		High
       
  1645 */
       
  1646 void DirectoryContentTest()
       
  1647 	{
       
  1648 	CSqlBurCallback* backupClient = NULL;
       
  1649 	TRAPD(err, backupClient = CSqlBurCallback::NewL(*TheSqlSrvTestBurInterface));
       
  1650 	TEST2(err, KErrNone);
       
  1651 	TEST(backupClient != NULL);
       
  1652 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KTestFile1);//KTestClientUid1 used
       
  1653 	//Create the file
       
  1654 	RFile file;
       
  1655 	err = file.Replace(TheSqlSrvTestBurInterface->Fs(), KTestFile1, EFileRead | EFileWrite);
       
  1656 	TEST2(err, KErrNone);
       
  1657 	_LIT8(KData1, "123456787989");
       
  1658 	err = file.Write(KData1);
       
  1659 	TEST2(err, KErrNone);
       
  1660 	err = file.Flush();
       
  1661 	TEST2(err, KErrNone);
       
  1662 	file.Close();
       
  1663 	//Store file entries into an array
       
  1664 	CDir* dirBeforeBackup = GetPrivateDirContent(KTestDrive);
       
  1665 	//Backup
       
  1666 	TRAP(err, TestBackupL(*backupClient, TheSqlSrvTestBurInterface->Fs(), KTestClientUid1, KTestDrive));
       
  1667 	TEST2(err, KErrNone);
       
  1668 	//Check dir content
       
  1669 	CDir* dirAfterBackup = GetPrivateDirContent(KTestDrive);
       
  1670 	TEST2(dirBeforeBackup->Count(), dirAfterBackup->Count());
       
  1671 	for(TInt i=0;i<dirBeforeBackup->Count();++i)
       
  1672 		{
       
  1673 		const TEntry& entry1 = (*dirBeforeBackup)[i];
       
  1674 		const TEntry& entry2 = (*dirAfterBackup)[i];
       
  1675 		TheTest.Printf(_L("Entry1=%S, Entry2=%S\r\n"), &entry1.iName, &entry2.iName);
       
  1676 		TBool rc = entry1.iAtt == entry2.iAtt;
       
  1677 		TEST(rc);
       
  1678 		rc = entry1.iSize == entry2.iSize;
       
  1679 		TEST(rc);
       
  1680 		rc = entry1.iModified == entry2.iModified;
       
  1681 		TEST(rc);
       
  1682 		rc = entry1.iType == entry2.iType;
       
  1683 		TEST(rc);
       
  1684 		rc = entry1.iName == entry2.iName;
       
  1685 		TEST(rc);
       
  1686 		}
       
  1687 	delete dirAfterBackup;
       
  1688 	//Modify the file
       
  1689 	err = file.Replace(TheSqlSrvTestBurInterface->Fs(), KTestFile1, EFileRead | EFileWrite);
       
  1690 	TEST2(err, KErrNone);
       
  1691 	_LIT8(KData2, "ABCDEF");
       
  1692 	err = file.Write(KData2);
       
  1693 	TEST2(err, KErrNone);
       
  1694 	err = file.Flush();
       
  1695 	TEST2(err, KErrNone);
       
  1696 	file.Close();
       
  1697 	//Restore
       
  1698 	User::After(2000000);//To force a change in the file time stamp (the restored file time stamp).
       
  1699 	TRAP(err, TestRestoreL(*backupClient, TheSqlSrvTestBurInterface->Fs(), KTestClientUid1, KTestDrive));
       
  1700 	TEST2(err, KErrNone);
       
  1701 	//Check dir content
       
  1702 	CDir* dirAfterRestore = GetPrivateDirContent(KTestDrive);
       
  1703 	TEST2(dirBeforeBackup->Count(), dirAfterRestore->Count());
       
  1704 	for(TInt i=0;i<dirBeforeBackup->Count();++i)
       
  1705 		{
       
  1706 		const TEntry& entry1 = (*dirBeforeBackup)[i];
       
  1707 		const TEntry& entry2 = (*dirAfterRestore)[i];
       
  1708 		TheTest.Printf(_L("Entry1=%S, Entry2=%S\r\n"), &entry1.iName, &entry2.iName);
       
  1709 		TBool rc = entry1.iAtt == entry2.iAtt;
       
  1710 		TEST(rc);
       
  1711 		rc = entry1.iSize == entry2.iSize;
       
  1712 		TEST(rc);
       
  1713 		if(entry1.iName.FindF(KTestFile1NameOnly) >= 0)
       
  1714 			{
       
  1715 			rc = entry1.iModified != entry2.iModified;
       
  1716 			}
       
  1717 		else
       
  1718 			{
       
  1719 			rc = entry1.iModified == entry2.iModified;
       
  1720 			}
       
  1721 		TEST(rc);
       
  1722 		rc = entry1.iType == entry2.iType;
       
  1723 		TEST(rc);
       
  1724 		rc = entry1.iName == entry2.iName;
       
  1725 		TEST(rc);
       
  1726 		}
       
  1727 	delete dirAfterRestore;
       
  1728 	//
       
  1729 	delete dirBeforeBackup;
       
  1730 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KTestFile1);
       
  1731 	delete backupClient;
       
  1732 	}
       
  1733 
       
  1734 /**
       
  1735 @SYMTestCaseID			PDS-SQL-UT-4232
       
  1736 @SYMTestCaseDesc		SQL Backup&Restore - large file test.
       
  1737 						Backup and restore with a file with size bigger than 1 Mb.
       
  1738 @SYMTestActions			SQL Backup&Restore - large file test.
       
  1739 @SYMTestExpectedResults Test must not fail
       
  1740 @SYMTestPriority		High
       
  1741 */
       
  1742 void LargeFileTest()
       
  1743 	{
       
  1744 	CSqlBurCallback* backupClient = NULL;
       
  1745 	TRAPD(err, backupClient = CSqlBurCallback::NewL(*TheSqlSrvTestBurInterface));
       
  1746 	TEST2(err, KErrNone);
       
  1747 	TEST(backupClient != NULL);
       
  1748 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KTestFile1);//KTestClientUid1 used
       
  1749 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KTestFile2);//KTestClientUid2 used
       
  1750 	//Allocate buffer for the data
       
  1751 	const TInt KDataBufSize = 100000;
       
  1752 	HBufC8* dataBuf = HBufC8::New(KDataBufSize);
       
  1753 	TEST(dataBuf != NULL);
       
  1754 	TPtr8 dataPtr = dataBuf->Des();
       
  1755 	//Create file 1
       
  1756 	const TInt KFileSize1 = 1201345;
       
  1757 	RFile file;
       
  1758 	err = file.Replace(TheSqlSrvTestBurInterface->Fs(), KTestFile1, EFileRead | EFileWrite);
       
  1759 	TEST2(err, KErrNone);
       
  1760 	dataPtr.SetLength(dataPtr.MaxLength());
       
  1761 	const TChar KChar1(0x5A);
       
  1762 	dataPtr.Fill(KChar1);
       
  1763 	TInt len = KFileSize1;
       
  1764 	while(len > 0)
       
  1765 		{
       
  1766 		TInt blockSize = Min(len, dataPtr.MaxLength()); 
       
  1767 		err = file.Write(dataPtr, blockSize);
       
  1768 		TEST2(err, KErrNone);
       
  1769 		len -= blockSize;
       
  1770 		}
       
  1771 	err = file.Flush();
       
  1772 	TEST2(err, KErrNone);
       
  1773 	file.Close();
       
  1774 	//Create file 2
       
  1775 	const TInt KFileSize2 = 1387651;
       
  1776 	err = file.Replace(TheSqlSrvTestBurInterface->Fs(), KTestFile2, EFileRead | EFileWrite);
       
  1777 	TEST2(err, KErrNone);
       
  1778 	dataPtr.SetLength(dataPtr.MaxLength());
       
  1779 	const TChar KChar2(0xD5);
       
  1780 	dataPtr.Fill(KChar2);
       
  1781 	len = KFileSize2;
       
  1782 	while(len > 0)
       
  1783 		{
       
  1784 		TInt blockSize = Min(len, dataPtr.MaxLength()); 
       
  1785 		err = file.Write(dataPtr, blockSize);
       
  1786 		TEST2(err, KErrNone);
       
  1787 		len -= blockSize;
       
  1788 		}
       
  1789 	err = file.Flush();
       
  1790 	TEST2(err, KErrNone);
       
  1791 	file.Close();
       
  1792 	//Backup
       
  1793 	TRAP(err, TestBackupL(*backupClient, TheSqlSrvTestBurInterface->Fs(), KTestClientUid1, KTestDrive));
       
  1794 	TEST2(err, KErrNone);
       
  1795 	TRAP(err, TestBackupL(*backupClient, TheSqlSrvTestBurInterface->Fs(), KTestClientUid2, KTestDrive));
       
  1796 	TEST2(err, KErrNone);
       
  1797 	//Delete the files
       
  1798 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KTestFile1);
       
  1799 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KTestFile2);
       
  1800 	//Restore
       
  1801 	TRAP(err, TestRestoreL(*backupClient, TheSqlSrvTestBurInterface->Fs(), KTestClientUid1, KTestDrive));
       
  1802 	TEST2(err, KErrNone);
       
  1803 	TRAP(err, TestRestoreL(*backupClient, TheSqlSrvTestBurInterface->Fs(), KTestClientUid2, KTestDrive));
       
  1804 	TEST2(err, KErrNone);
       
  1805 	//Check restored files content
       
  1806 	const TPtrC KFileNames[] = {KTestFile1(), KTestFile2()};
       
  1807 	const TInt KFileSizes[] =  {KFileSize1, KFileSize2};
       
  1808 	const TChar KSymbols[] =  {KChar1, KChar2};
       
  1809 	for(TInt i=0;i<(sizeof(KFileNames)/sizeof(KFileNames[i]));++i)
       
  1810 		{
       
  1811 		err = file.Open(TheSqlSrvTestBurInterface->Fs(), KFileNames[i], EFileRead);
       
  1812 		TEST2(err, KErrNone);
       
  1813 		len = 0;
       
  1814 		err = file.Size(len);
       
  1815 		TEST2(err, KErrNone);
       
  1816 		TEST2(len, KFileSizes[i]);
       
  1817 		while(len > 0)
       
  1818 			{
       
  1819 			TInt blockSize = Min(len, dataPtr.MaxLength());
       
  1820 			err = file.Read(dataPtr, blockSize);
       
  1821 			TEST2(err, KErrNone);
       
  1822 			len -= blockSize;
       
  1823 			for(TInt j=0;j<dataPtr.Length();++j)
       
  1824 				{
       
  1825 				TEST(dataPtr[j] == KSymbols[i]);
       
  1826 				}
       
  1827 			}
       
  1828 		file.Close();
       
  1829 		}
       
  1830 	//
       
  1831 	delete dataBuf;
       
  1832 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KTestFile2);
       
  1833 	(void)TheSqlSrvTestBurInterface->Fs().Delete(KTestFile1);
       
  1834 	delete backupClient;
       
  1835 	}
   918 
  1836 
   919 void DoMain()
  1837 void DoMain()
   920 	{
  1838 	{
   921 	TestEnvCreate();
  1839 	TestEnvCreate();
   922 
  1840 
   923 	TheTest.Start(_L("Store db content to memory buffer"));
  1841 	TheTest.Start(_L("Store db content to memory buffer"));
   924 	StoreDbContentToBuf(TheTestHarness->Fs());
  1842 	StoreDbContentToBuf(TheSqlSrvTestBurInterface->Fs(), KTestDrive, KClientUid);
   925 		
  1843 		
       
  1844 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4002 Backup: functional test "));
   926 	FunctionalTest();
  1845 	FunctionalTest();
   927 
  1846 
       
  1847 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4003 Backup: OOM test "));
   928 	OomTest();
  1848 	OomTest();
   929 
  1849 
       
  1850 	TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4143 Backup&Restore: functional test 2"));
   930 	FunctionalTest2();
  1851 	FunctionalTest2();
   931 
  1852 
       
  1853 	TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4144 Backup&Restore: legacy file format test"));
   932 	LegacyFileFormatTest();
  1854 	LegacyFileFormatTest();
   933 	
  1855 	
       
  1856 	TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4192 Backup&Restore: empty backup file list"));
   934 	EmptyBackupFileListTest();
  1857 	EmptyBackupFileListTest();
   935 	
  1858 	
       
  1859 	TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4193 Backup: File I/O error simulation test"));
   936 	BackupRestoreFileIoErrTest();
  1860 	BackupRestoreFileIoErrTest();
   937 
  1861 	
       
  1862 	TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4225 Zero size file - backup test"));	
       
  1863 	BackupZeroSizeFileTest();
       
  1864 	
       
  1865 	TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4226 Restore test - corrupted archive 1"));	
       
  1866 	CorruptedArchiveTest1();
       
  1867 
       
  1868 	TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4227 Restore test - corrupted archive 2"));	
       
  1869 	CorruptedArchiveTest2();
       
  1870 
       
  1871 	TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4228 Backup&Restore test on a drive different than KTestDrive (C: by default)"));
       
  1872 	DbDriveTest();
       
  1873 	
       
  1874 	TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4229 Backup&Restore test with locked file"));
       
  1875 	LockFileTest();
       
  1876 	
       
  1877 	TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4230 Backup&Restore test with locked file 2"));
       
  1878 	LockFileTest2();
       
  1879 
       
  1880 	TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4231 Backup&Restore - directory content test"));
       
  1881 	DirectoryContentTest();
       
  1882 
       
  1883 	TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4232 Backup&Restore - large file test"));
       
  1884 	LargeFileTest();
       
  1885 	
   938 	TestEnvDestroy();
  1886 	TestEnvDestroy();
   939 	}
  1887 	}
   940 
  1888 
   941 TInt E32Main()
  1889 TInt E32Main()
   942 	{
  1890 	{
   943 	TheTest.Title();
  1891 	TheTest.Title();
   944 	
  1892 	
   945 	CTrapCleanup* tc = CTrapCleanup::New();
  1893 	CTrapCleanup* tc = CTrapCleanup::New();
   946 	TEST(tc != NULL);
  1894 	TheTest(tc != NULL);
   947 	
  1895 	
   948 	__UHEAP_MARK;
  1896 	__UHEAP_MARK;
   949 	
  1897 	
   950 	DoMain();
  1898 	DoMain();
   951 	
  1899