os/persistentdata/persistentstorage/sql/TEST/t_sqlood.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-2009 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
sl@0
    20
///////////////////////////////////////////////////////////////////////////////////////
sl@0
    21
sl@0
    22
static RFs TheFs;
sl@0
    23
RTest TheTest(_L("t_sqlood test"));
sl@0
    24
sl@0
    25
#if  defined __WINSCW__ || defined __WINS__
sl@0
    26
sl@0
    27
	//The C: drive may be too big and may be used concurently by other applications. 
sl@0
    28
	//The T: drive is more suitable for the test if running on the emulator
sl@0
    29
	const TInt KTestDrive = EDriveT;
sl@0
    30
	_LIT(KTestDir, "t:\\test\\");
sl@0
    31
	_LIT(KTestDatabase, "t:\\test\\t_sql_ood.db");
sl@0
    32
	
sl@0
    33
#elif defined __X86GCC__
sl@0
    34
sl@0
    35
	const TInt KTestDrive = EDriveG;
sl@0
    36
	_LIT(KTestDir, "g:\\test\\");
sl@0
    37
	_LIT(KTestDatabase, "g:\\test\\t_sql_ood.db");
sl@0
    38
	
sl@0
    39
#else
sl@0
    40
sl@0
    41
	const TInt KTestDrive = EDriveE;
sl@0
    42
	_LIT(KTestDir, "e:\\test\\");
sl@0
    43
	_LIT(KTestDatabase, "e:\\test\\t_sql_ood.db");
sl@0
    44
	
sl@0
    45
#endif
sl@0
    46
sl@0
    47
//One or more files with KLargeFileName name and ".<n>" extension, where n is 
sl@0
    48
//000, 001, 002, 003...
sl@0
    49
//will be created and they will occupy almost all available disk space.
sl@0
    50
//The idea is to perform after that one "delete"
sl@0
    51
//transaction, which must to fail, because there won't be enough available disk space to complete the transaction.
sl@0
    52
#if  defined __WINSCW__ || defined __WINS__
sl@0
    53
sl@0
    54
	_LIT(KLargeFileName, "t:\\test\\DeleteMe");
sl@0
    55
sl@0
    56
#elif defined __X86GCC__
sl@0
    57
sl@0
    58
	_LIT(KLargeFileName, "g:\\test\\DeleteMe");
sl@0
    59
sl@0
    60
#else
sl@0
    61
sl@0
    62
	_LIT(KLargeFileName, "e:\\test\\DeleteMe");
sl@0
    63
sl@0
    64
#endif
sl@0
    65
sl@0
    66
_LIT8(KDatabasePageSizeConfig, "page_size=1024");
sl@0
    67
sl@0
    68
const TInt KMaxTestRecordsCount = 350;
sl@0
    69
TInt TestRecordsCount = 0;
sl@0
    70
sl@0
    71
///////////////////////////////////////////////////////////////////////////////////////
sl@0
    72
sl@0
    73
//Assemblesd a file name from "aFileName" and "aFileNumber" parameters and places the resulting string in "aResultPath".
sl@0
    74
void AssembleLargeFileName(const TDesC& aFileName, TInt aFileNumber, TDes& aResultPath)
sl@0
    75
	{
sl@0
    76
	_LIT(KFormatStr, "%S.%03d");
sl@0
    77
	aResultPath.Format(KFormatStr, &aFileName, aFileNumber);
sl@0
    78
	}
sl@0
    79
sl@0
    80
//Deletes all created large data files.
sl@0
    81
void DeleteLargeDataFiles()
sl@0
    82
	{
sl@0
    83
	TInt err = KErrNone;
sl@0
    84
	TInt i = -1;
sl@0
    85
	while(err == KErrNone)
sl@0
    86
		{
sl@0
    87
		TBuf<KMaxFileName> filePath;
sl@0
    88
		::AssembleLargeFileName(KLargeFileName, ++i, filePath);
sl@0
    89
		err = TheFs.Delete(filePath);
sl@0
    90
		}
sl@0
    91
	}
sl@0
    92
sl@0
    93
//Deletes all created test files.
sl@0
    94
void DeleteTestFiles()
sl@0
    95
	{
sl@0
    96
	DeleteLargeDataFiles();
sl@0
    97
	(void)RSqlDatabase::Delete(KTestDatabase);
sl@0
    98
	}
sl@0
    99
sl@0
   100
///////////////////////////////////////////////////////////////////////////////////////
sl@0
   101
///////////////////////////////////////////////////////////////////////////////////////
sl@0
   102
//Test macros and functions
sl@0
   103
void Check1(TInt aValue, TInt aLine)
sl@0
   104
	{
sl@0
   105
	if(!aValue)
sl@0
   106
		{
sl@0
   107
		DeleteTestFiles();
sl@0
   108
		RDebug::Print(_L("*** Line %d\r\n"), aLine);
sl@0
   109
		TheTest(EFalse, aLine);
sl@0
   110
		}
sl@0
   111
	}
sl@0
   112
void Check2(TInt aValue, TInt aExpected, TInt aLine)
sl@0
   113
	{
sl@0
   114
	if(aValue != aExpected)
sl@0
   115
		{
sl@0
   116
		DeleteTestFiles();
sl@0
   117
		RDebug::Print(_L("*** Line %d, Expected error: %d, got: %d\r\n"), aLine, aExpected, aValue);
sl@0
   118
		TheTest(EFalse, aLine);
sl@0
   119
		}
sl@0
   120
	}
sl@0
   121
#define TEST(arg) ::Check1((arg), __LINE__)
sl@0
   122
#define TEST2(aValue, aExpected) ::Check2(aValue, aExpected, __LINE__)
sl@0
   123
sl@0
   124
///////////////////////////////////////////////////////////////////////////////////////
sl@0
   125
sl@0
   126
//Creates file session instance and the test directory
sl@0
   127
void CreateTestEnv()
sl@0
   128
    {
sl@0
   129
	TInt err = TheFs.Connect();
sl@0
   130
	TEST2(err, KErrNone);
sl@0
   131
sl@0
   132
	err = TheFs.MkDir(KTestDir);
sl@0
   133
	if(err != KErrNone)
sl@0
   134
	    {
sl@0
   135
	    RDebug::Print(_L("*** CreateTestEnv(), RFs::MkDir(), err=%d\r\n"), err);
sl@0
   136
	    }
sl@0
   137
	TEST(err == KErrNone || err == KErrAlreadyExists);
sl@0
   138
	}
sl@0
   139
sl@0
   140
//Creates one or more large files with the total size near to the size of the available disk space.
sl@0
   141
//The idea is to cause an "out of disk space" condition.
sl@0
   142
void FillLargeDataFile(RFile& aFile, TInt aSize)
sl@0
   143
	{
sl@0
   144
	TInt err = KErrDiskFull;
sl@0
   145
	while(err == KErrDiskFull)
sl@0
   146
		{
sl@0
   147
		err = aFile.SetSize(aSize);
sl@0
   148
		aSize -= 100;
sl@0
   149
		if(aSize <= 0)
sl@0
   150
			{
sl@0
   151
			break;
sl@0
   152
			}
sl@0
   153
		}
sl@0
   154
	TEST(err == KErrNone || err == KErrDiskFull);
sl@0
   155
	}
sl@0
   156
sl@0
   157
//Gets and returns the available disk space of the tested drive.
sl@0
   158
TInt64 FreeDiskSpace()
sl@0
   159
	{
sl@0
   160
	TVolumeInfo volInfoBefore;
sl@0
   161
	TInt err = TheFs.Volume(volInfoBefore, KTestDrive);
sl@0
   162
	TEST2(err, KErrNone);
sl@0
   163
	return volInfoBefore.iFree;
sl@0
   164
	}
sl@0
   165
sl@0
   166
//Creates a large data file with aSize size (in bytes).
sl@0
   167
void DoCreateLargeFile(const TDesC& aPath, TInt aSize)
sl@0
   168
	{
sl@0
   169
	RFile file;
sl@0
   170
	TInt err = file.Replace(TheFs, aPath, EFileRead | EFileWrite);
sl@0
   171
	TEST2(err, KErrNone);
sl@0
   172
	FillLargeDataFile(file, aSize);
sl@0
   173
	err = file.Flush();
sl@0
   174
	TEST2(err, KErrNone);
sl@0
   175
	file.Close();
sl@0
   176
	}
sl@0
   177
sl@0
   178
//Creates enough number of large data files to fill the available disk space.
sl@0
   179
void CreateLargeFile()
sl@0
   180
	{
sl@0
   181
	TInt fileNo = 0;
sl@0
   182
	const TInt KLargeFileSize = 1000000000;
sl@0
   183
	TInt64 diskSpace = ::FreeDiskSpace();
sl@0
   184
	RDebug::Print(_L("CreateLargeFile: free space before = %ld\r\n"), diskSpace);
sl@0
   185
	TBuf<KMaxFileName> filePath;
sl@0
   186
	while(diskSpace > KLargeFileSize)
sl@0
   187
		{
sl@0
   188
		AssembleLargeFileName(KLargeFileName, fileNo++, filePath);
sl@0
   189
		DoCreateLargeFile(filePath, KLargeFileSize);
sl@0
   190
		diskSpace = ::FreeDiskSpace();
sl@0
   191
		RDebug::Print(_L("----CreateLargeFile, step %d, free space = %ld\r\n"), fileNo, diskSpace);
sl@0
   192
		}
sl@0
   193
	//Reserve almost all disk space, except a small amount - 200 bytes.
sl@0
   194
	if(diskSpace > 0)
sl@0
   195
		{
sl@0
   196
		::AssembleLargeFileName(KLargeFileName, fileNo++, filePath);
sl@0
   197
		const TInt64 KSpaceLeft = 200;
sl@0
   198
		TInt64 lastFileSize = diskSpace - KSpaceLeft;
sl@0
   199
        TInt lastFileSize32 = I64LOW(lastFileSize);
sl@0
   200
		RDebug::Print(_L("----file size32 = %d\r\n"), lastFileSize32);
sl@0
   201
		::DoCreateLargeFile(filePath, lastFileSize32);
sl@0
   202
		RDebug::Print(_L("----CreateLargeFile, last step (%d), file size = %ld\r\n"), fileNo, lastFileSize);
sl@0
   203
		}
sl@0
   204
	diskSpace = ::FreeDiskSpace();
sl@0
   205
	RDebug::Print(_L("CreateLargeFile: free space after = %ld\r\n"), diskSpace);
sl@0
   206
	}
sl@0
   207
sl@0
   208
sl@0
   209
// Number of bytes in the default journal header size.
sl@0
   210
const TInt KJournalHeaderSize = 0x200;
sl@0
   211
sl@0
   212
// Number of bytes added to each database page in the journal.
sl@0
   213
const TInt KJournalPageOverhead = 8; 
sl@0
   214
sl@0
   215
// The default amount of reserved space provided by the ReserveDriveSpace API
sl@0
   216
const TInt KReserveDriveSpaceAmount = 64*1024;
sl@0
   217
sl@0
   218
//Creates and fills with some records a test database
sl@0
   219
void CreateAndFillTestDatabase(RSqlDatabase& aDb)
sl@0
   220
	{
sl@0
   221
	TInt err = aDb.Create(KTestDatabase, &KDatabasePageSizeConfig);
sl@0
   222
	TEST2(err, KErrNone);
sl@0
   223
	err = aDb.Exec(_L("CREATE TABLE A(Id INTEGER, Data TEXT)"));
sl@0
   224
	TEST(err >= 0);
sl@0
   225
sl@0
   226
	//
sl@0
   227
	// Find the page size of the database on this media
sl@0
   228
	//
sl@0
   229
	TBuf<200> sql;
sl@0
   230
	sql.Copy(_L("PRAGMA page_size"));
sl@0
   231
	TSqlScalarFullSelectQuery q(aDb);
sl@0
   232
	TInt pageSize = 0;
sl@0
   233
	TRAP(err, pageSize = q.SelectIntL(sql););
sl@0
   234
	//RDebug::Print(_L("Error %d Page Size %d"),err,pageSize);
sl@0
   235
	TEST2(err, KErrNone);
sl@0
   236
	TEST(pageSize > 0);
sl@0
   237
	//RDebug::Print(_L("Page Size %d"),pageSize);
sl@0
   238
	
sl@0
   239
	//
sl@0
   240
	// Find the sector size of this media
sl@0
   241
	//
sl@0
   242
	TDriveInfo driveInfo;
sl@0
   243
	err = TheFs.Drive(driveInfo, KTestDrive);
sl@0
   244
	TEST2(err, KErrNone);
sl@0
   245
	TVolumeIOParamInfo volumeInfo;
sl@0
   246
	err = TheFs.VolumeIOParam(KTestDrive, volumeInfo);
sl@0
   247
	TEST2(err, KErrNone);
sl@0
   248
	TInt sectorSize = volumeInfo.iBlockSize;
sl@0
   249
	//RDebug::Print(_L("Sector Size %d"),sectorSize);	
sl@0
   250
sl@0
   251
	TInt journalHeaderSize = Max(sectorSize, KJournalHeaderSize);
sl@0
   252
	//RDebug::Print(_L("Journal Header Size %d"),journalHeaderSize);
sl@0
   253
sl@0
   254
	//
sl@0
   255
	// Keep adding to database until it is a size such that all the data can still be deleted within the reserved disk space size. 
sl@0
   256
	// Do this piece-wise not in a transaction.
sl@0
   257
	//
sl@0
   258
	TInt i;
sl@0
   259
	for(i=0;i<KMaxTestRecordsCount;++i)
sl@0
   260
		{
sl@0
   261
		sql.Format(_L("INSERT INTO A(Id, Data) VALUES(%d, 'A0123456789B0123456789C0123456789D0123456789E0123456789F0123456789G0123456789H0123456789')"), i + 1);
sl@0
   262
		err = aDb.Exec(sql);
sl@0
   263
		TEST2(err, 1);
sl@0
   264
sl@0
   265
		TInt size = aDb.Size();
sl@0
   266
		TInt numberOfPages = size/pageSize;
sl@0
   267
		TInt predictedJournalSize = journalHeaderSize + numberOfPages * (pageSize + KJournalPageOverhead);
sl@0
   268
		//RDebug::Print(_L("Size %d, Pages %d, predictedJournalSize %d"),size, numberOfPages, predictedJournalSize);
sl@0
   269
		
sl@0
   270
		// Will another page take us over the limit ?
sl@0
   271
		if ((predictedJournalSize + (pageSize + KJournalPageOverhead)) >= (KReserveDriveSpaceAmount))
sl@0
   272
			{
sl@0
   273
			break;
sl@0
   274
			}
sl@0
   275
		}
sl@0
   276
	TestRecordsCount = i + 1;
sl@0
   277
	
sl@0
   278
	//RDebug::Print(_L("TestRecordsCount %d"),TestRecordsCount);
sl@0
   279
	
sl@0
   280
	}
sl@0
   281
sl@0
   282
//Tries to delete test database records
sl@0
   283
TInt DeleteTestRecords(RSqlDatabase& aDb)
sl@0
   284
	{
sl@0
   285
	TInt err = aDb.Exec(_L("BEGIN TRANSACTION"));
sl@0
   286
	TEST(err >= 0);
sl@0
   287
	for(TInt i=0;i<TestRecordsCount;++i)
sl@0
   288
		{
sl@0
   289
		TBuf<100> sql;
sl@0
   290
		sql.Format(_L("DELETE FROM A WHERE Id = %d"), i + 1);
sl@0
   291
		err = aDb.Exec(sql);// May fail with KErrDiskFull
sl@0
   292
		if(err < 0)
sl@0
   293
			{
sl@0
   294
			(void)aDb.Exec(_L("ROLLBACK TRANSACTION"));
sl@0
   295
			return err;
sl@0
   296
			}
sl@0
   297
		}
sl@0
   298
	err = aDb.Exec(_L("COMMIT TRANSACTION"));// May fail with KErrDiskFull
sl@0
   299
	return err;
sl@0
   300
	}
sl@0
   301
sl@0
   302
///////////////////////////////////////////////////////////////////////////////////////
sl@0
   303
sl@0
   304
//The function simply calls RSqlDatabase::ReserveDriveSpace(), RSqlDatabase::GetReserveAccess(),
sl@0
   305
//RSqlDatabase::ReleaseReserveAccess() methods and checks the return values.
sl@0
   306
//It might be usefull for debugging in case if something gets wrong.
sl@0
   307
void SimpleCallsTest()
sl@0
   308
	{
sl@0
   309
	RSqlDatabase db, db2;
sl@0
   310
	TInt err = db.Create(KTestDatabase, &KDatabasePageSizeConfig);
sl@0
   311
	TEST2(err, KErrNone);
sl@0
   312
sl@0
   313
	err = db2.Open(KTestDatabase);
sl@0
   314
	TEST2(err, KErrNone);
sl@0
   315
	
sl@0
   316
	//An attempt to get an access to the reserved space (which is not reserved yet).
sl@0
   317
	err = db.GetReserveAccess();
sl@0
   318
	TEST2(err, KErrNotFound);
sl@0
   319
sl@0
   320
	//Reserve disk space
sl@0
   321
	err = db.ReserveDriveSpace(0);
sl@0
   322
	TEST2(err, KErrNone);
sl@0
   323
sl@0
   324
	//An attempt to re-reserve it
sl@0
   325
   	err = db.ReserveDriveSpace(0);
sl@0
   326
	TEST2(err, KErrAlreadyExists);
sl@0
   327
sl@0
   328
	//Get an access to the reserved disk space
sl@0
   329
	err = db.GetReserveAccess();
sl@0
   330
	TEST2(err, KErrNone);
sl@0
   331
sl@0
   332
	//Reserve disk space from the second connection
sl@0
   333
	err = db2.ReserveDriveSpace(0);
sl@0
   334
	TEST2(err, KErrNone);
sl@0
   335
sl@0
   336
	//An attempt to get an access to the reserved space twice.
sl@0
   337
	err = db.GetReserveAccess();
sl@0
   338
	TEST2(err, KErrInUse);
sl@0
   339
sl@0
   340
	//An attempt to get an access to the reserved space from the second connection.
sl@0
   341
	err = db2.GetReserveAccess();
sl@0
   342
	TEST2(err, KErrNone);
sl@0
   343
sl@0
   344
	db.ReleaseReserveAccess();
sl@0
   345
sl@0
   346
	//An attempt to release the reserved space twice.
sl@0
   347
	db.ReleaseReserveAccess();
sl@0
   348
sl@0
   349
	//Free the reserved disk space
sl@0
   350
	db.FreeReservedSpace();
sl@0
   351
sl@0
   352
	//Free the reserved disk space twice.
sl@0
   353
	db.FreeReservedSpace();
sl@0
   354
sl@0
   355
	//Free the reserved disk space from the second connection.
sl@0
   356
	db2.FreeReservedSpace();
sl@0
   357
sl@0
   358
	db2.Close();
sl@0
   359
	db.Close();
sl@0
   360
	(void)RSqlDatabase::Delete(KTestDatabase);
sl@0
   361
	}
sl@0
   362
sl@0
   363
/**
sl@0
   364
@SYMTestCaseID			SYSLIB-SQL-CT-1649
sl@0
   365
@SYMTestCaseDesc		SQL database "out of disk space" tests.
sl@0
   366
						The test creates and fills with some records a test database and then reserves a disk space.
sl@0
   367
						The second step: the test fills almost all available disk space creting large data files.
sl@0
   368
						The third step: the test attempts to delete all records from the test database and fails with
sl@0
   369
						KErrDiskFull error.
sl@0
   370
						The fourth step: the test gets an access to the reserved disk space and attempts to delete
sl@0
   371
						records again. This time the test should not fail.
sl@0
   372
@SYMTestPriority		High
sl@0
   373
@SYMTestActions			SQL database "out of disk space" tests.
sl@0
   374
@SYMTestExpectedResults Test must not fail
sl@0
   375
@SYMREQ					REQ5792
sl@0
   376
                        REQ5793
sl@0
   377
*/
sl@0
   378
void DeleteTransactionTest()
sl@0
   379
	{
sl@0
   380
	TVolumeIOParamInfo volIoPrm;
sl@0
   381
	TInt err = TheFs.VolumeIOParam(KTestDrive, volIoPrm);
sl@0
   382
    TEST2(err, KErrNone);
sl@0
   383
    RDebug::Print(_L("--Drive %d. BlockSize=%d, ClusterSize=%d, RecReadBufSize=%d, RecWriteBufSize=%d\r\n"), KTestDrive, volIoPrm.iBlockSize, volIoPrm.iClusterSize, volIoPrm.iRecReadBufSize, volIoPrm.iRecWriteBufSize);
sl@0
   384
	/////////////////////////////////////////////////////////
sl@0
   385
    RDebug::Print(_L("--Create and fill database \"%S\".\r\n"), &KTestDatabase);
sl@0
   386
	RSqlDatabase db;
sl@0
   387
	CreateAndFillTestDatabase(db);
sl@0
   388
	db.Close();//When the database gets closed, the persisted journal file will be deleted.
sl@0
   389
    RDebug::Print(_L("--Close and reopen database \"%S\" (in order to get the persisted journal file deleted).\r\n"), &KTestDatabase);
sl@0
   390
    err = db.Open(KTestDatabase);
sl@0
   391
    TEST2(err, KErrNone);
sl@0
   392
    RDebug::Print(_L("--Reserve disk space for database \"%S\".\r\n"), &KTestDatabase);
sl@0
   393
    err = db.ReserveDriveSpace(0);
sl@0
   394
	TEST2(err, KErrNone);
sl@0
   395
    RDebug::Print(_L("--Simulate an \"out of disk space\" situation with creating a very large data file, which occupies almost the all the available disk space.\r\n"));
sl@0
   396
	CreateLargeFile();
sl@0
   397
	RDebug::Print(_L("--Attempt to delete test data records. The transaction must fail, because of \"out of disk space\".\r\n"));
sl@0
   398
	err = DeleteTestRecords(db);
sl@0
   399
	TEST2(err, KErrDiskFull);
sl@0
   400
    RDebug::Print(_L("--Get an access to the reserved disk space.\r\n"));
sl@0
   401
	err = db.GetReserveAccess();
sl@0
   402
	TEST2(err, KErrNone);
sl@0
   403
    TInt64 diskSpace = ::FreeDiskSpace();
sl@0
   404
    RDebug::Print(_L("After GetReserveAccess(), free disk space = %ld. Try again \"Delete records\" transaction. The transaction must not fail.\r\n"), diskSpace);
sl@0
   405
	err = DeleteTestRecords(db);
sl@0
   406
	RDebug::Print(_L("--DeleteTestRecords() returned %d error.\r\n"), err);
sl@0
   407
	TEST(err >= 0);
sl@0
   408
	//Releases the access to the reserved disk space
sl@0
   409
	db.ReleaseReserveAccess();
sl@0
   410
	//Frees the reserved disk space
sl@0
   411
	db.FreeReservedSpace();
sl@0
   412
    //Free the resources, used in the test
sl@0
   413
	DeleteLargeDataFiles();
sl@0
   414
	//Verify that the records have been deleted
sl@0
   415
	RSqlStatement stmt;
sl@0
   416
	err = stmt.Prepare(db, _L("SELECT COUNT(*) FROM A"));
sl@0
   417
	TEST2(err, KErrNone);
sl@0
   418
	err = stmt.Next();
sl@0
   419
	TEST2(err, KSqlAtRow);
sl@0
   420
	TInt recCount = stmt.ColumnInt(0);
sl@0
   421
	TEST2(recCount, 0);
sl@0
   422
	stmt.Close();
sl@0
   423
	db.Close();
sl@0
   424
	(void)RSqlDatabase::Delete(KTestDatabase);
sl@0
   425
	}
sl@0
   426
sl@0
   427
//OOD API tests with more than one connection to the same SQL database.
sl@0
   428
//The test calls ReserveDriveSpace/GetReserveAccess/ReleaseReserveAccess in a different
sl@0
   429
//combinations on four RSqlDatabase objects, connected to the same database .
sl@0
   430
//The test should not fail or panic.
sl@0
   431
void MultiDbTest()
sl@0
   432
    {
sl@0
   433
    RSqlDatabase db1;
sl@0
   434
	CreateAndFillTestDatabase(db1);
sl@0
   435
sl@0
   436
    RSqlDatabase db2;
sl@0
   437
    TInt err = db2.Open(KTestDatabase);
sl@0
   438
    TEST2(err, KErrNone);
sl@0
   439
sl@0
   440
    //Play with "ReserveDriveSpace" on both sessions
sl@0
   441
    err = db1.ReserveDriveSpace(0);
sl@0
   442
    TEST2(err, KErrNone);
sl@0
   443
    err = db2.ReserveDriveSpace(0);
sl@0
   444
    TEST2(err, KErrNone);
sl@0
   445
    db2.FreeReservedSpace();
sl@0
   446
    err = db2.ReserveDriveSpace(0);
sl@0
   447
    TEST2(err, KErrNone);
sl@0
   448
sl@0
   449
    //Get an access to the reserved space through db2
sl@0
   450
	err = db2.GetReserveAccess();
sl@0
   451
    TEST2(err, KErrNone);
sl@0
   452
    //Free/re-reserve disk space for db1.
sl@0
   453
    db1.FreeReservedSpace();
sl@0
   454
    err = db1.ReserveDriveSpace(0);
sl@0
   455
    TEST2(err, KErrNone);
sl@0
   456
sl@0
   457
    RSqlDatabase db4;
sl@0
   458
    err = db4.Open(KTestDatabase);
sl@0
   459
    TEST2(err, KErrNone);
sl@0
   460
sl@0
   461
    //Try to reserve space for db4.
sl@0
   462
    err = db4.ReserveDriveSpace(0);
sl@0
   463
    TEST2(err, KErrNone);
sl@0
   464
sl@0
   465
    RSqlDatabase db3;
sl@0
   466
    err = db3.Open(KTestDatabase);
sl@0
   467
    TEST2(err, KErrNone);
sl@0
   468
sl@0
   469
    //Try to reserve space for session db3.
sl@0
   470
    err = db3.ReserveDriveSpace(0);
sl@0
   471
    TEST2(err, KErrNone);
sl@0
   472
sl@0
   473
    //Release and free db2 access to the reserved space.
sl@0
   474
    db2.ReleaseReserveAccess();
sl@0
   475
    db2.FreeReservedSpace();
sl@0
   476
sl@0
   477
    db3.FreeReservedSpace();
sl@0
   478
    db3.Close();
sl@0
   479
sl@0
   480
    db4.FreeReservedSpace();
sl@0
   481
    db4.Close();
sl@0
   482
sl@0
   483
    //Get an access to the reserved space through db2.
sl@0
   484
    //But it was freed, so the call will fail.
sl@0
   485
	err = db2.GetReserveAccess();
sl@0
   486
    TEST2(err, KErrNotFound);
sl@0
   487
sl@0
   488
    //Free/re-reserve disk space for db1.
sl@0
   489
    db1.FreeReservedSpace();
sl@0
   490
    err = db1.ReserveDriveSpace(0);
sl@0
   491
    TEST2(err, KErrNone);
sl@0
   492
sl@0
   493
    //Get/release the access to the reserved space for db1.
sl@0
   494
	err = db1.GetReserveAccess();
sl@0
   495
    TEST2(err, KErrNone);
sl@0
   496
    db1.ReleaseReserveAccess();
sl@0
   497
sl@0
   498
    //Get an access to the reserved space for db2.
sl@0
   499
    //The call will fail because there is no reserved disk space for db2.
sl@0
   500
	err = db2.GetReserveAccess();
sl@0
   501
    TEST2(err, KErrNotFound);
sl@0
   502
sl@0
   503
    //Free the reserved space - db1
sl@0
   504
    db1.FreeReservedSpace();
sl@0
   505
sl@0
   506
	db2.Close();
sl@0
   507
	db1.Close();
sl@0
   508
sl@0
   509
	(void)RSqlDatabase::Delete(KTestDatabase);
sl@0
   510
    }
sl@0
   511
sl@0
   512
void DoTests()
sl@0
   513
	{
sl@0
   514
	TheTest.Start(_L(" \"Simple calls\" OOD test "));
sl@0
   515
	SimpleCallsTest();
sl@0
   516
sl@0
   517
	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1649 \"Delete transaction\" OOD test "));
sl@0
   518
	DeleteTransactionTest();
sl@0
   519
sl@0
   520
	TheTest.Next(_L(" Multi db OOD test "));
sl@0
   521
	MultiDbTest();
sl@0
   522
	}
sl@0
   523
sl@0
   524
TInt E32Main()
sl@0
   525
	{
sl@0
   526
	TheTest.Title();
sl@0
   527
sl@0
   528
	CTrapCleanup* tc = CTrapCleanup::New();
sl@0
   529
sl@0
   530
	__UHEAP_MARK;
sl@0
   531
sl@0
   532
	CreateTestEnv();
sl@0
   533
	DeleteTestFiles();
sl@0
   534
	DoTests();
sl@0
   535
	DeleteTestFiles();
sl@0
   536
	TheFs.Close();
sl@0
   537
sl@0
   538
	__UHEAP_MARKEND;
sl@0
   539
sl@0
   540
	TheTest.End();
sl@0
   541
	TheTest.Close();
sl@0
   542
sl@0
   543
	delete tc;
sl@0
   544
sl@0
   545
	User::Heap().Check();
sl@0
   546
	return KErrNone;
sl@0
   547
	}