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