os/persistentdata/persistentstorage/sql/TEST/t_sqlcompact4.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sql/TEST/t_sqlcompact4.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,1172 @@
     1.4 +// Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include <e32test.h>
    1.20 +#include <bautils.h>
    1.21 +#include <sqldb.h>
    1.22 +#include <stdlib.h>
    1.23 +#include "sqlite3.h"
    1.24 +#include "SqliteSymbian.h"
    1.25 +#include "SqlResourceTester.h"
    1.26 +
    1.27 +///////////////////////////////////////////////////////////////////////////////////////
    1.28 +
    1.29 +RTest TheTest(_L("t_sqlcompact4 test"));
    1.30 +TParse TheParse;
    1.31 +TDriveName TheDrive;
    1.32 +
    1.33 +RSqlDatabase TheDb;
    1.34 +sqlite3* TheSqliteDb = NULL;
    1.35 +TBuf<256> TheCmd;
    1.36 +
    1.37 +const TInt KTextLen = 1000;
    1.38 +const TInt KRecLen = 2000;
    1.39 +
    1.40 +TBuf<KTextLen> TheText;
    1.41 +TBuf8<KRecLen> TheSqlQuery;
    1.42 +TBuf8<KRecLen> TheSqlFmt;
    1.43 +TBuf<KTextLen + 50> TheSqlTexLen;
    1.44 +
    1.45 +_LIT(KDefaultDriveName, "c:");
    1.46 +_LIT(KTestDir, "c:\\test\\");
    1.47 +_LIT(KTestDbTemplate8, "c:\\test\\t_sqlcompact4_tmpl8.dat");
    1.48 +_LIT(KTestDbTemplate16, "c:\\test\\t_sqlcompact4_tmpl16.dat");
    1.49 +_LIT(KDbName, "c:\\test\\t_sqlcompact4_1.db");
    1.50 +_LIT(KDbName2, "c:\\test\\t_sqlcompact4_2.db");
    1.51 +_LIT(KRoDbName, "z:\\test\\testdb1.db");//Created outside the test app
    1.52 +TFileName TheTestDbName;
    1.53 +
    1.54 +const TInt KMaxThreadCount = 100;
    1.55 +TInt32 TheTestThreadCount = 8;
    1.56 +
    1.57 +const TInt KTestDbPageSize = 1024;
    1.58 +
    1.59 +TInt TheOriginalDbSize8 = -1;
    1.60 +TInt TheCompactedDbSize8 = -1;
    1.61 +
    1.62 +TInt TheOriginalDbSize16 = -1;
    1.63 +TInt TheCompactedDbSize16 = -1;
    1.64 +
    1.65 +//In order to be able to compile the test, the following variables are defined (used inside the OS porting layer, when _SQLPROFILER macro is defined)
    1.66 +#ifdef _SQLPROFILER
    1.67 +TInt TheSqlSrvProfilerFileRead = 0;
    1.68 +TInt TheSqlSrvProfilerFileWrite = 0;
    1.69 +TInt TheSqlSrvProfilerFileSync = 0;
    1.70 +TInt TheSqlSrvProfilerFileSetSize = 0;
    1.71 +#endif
    1.72 +
    1.73 +///////////////////////////////////////////////////////////////////////////////////////
    1.74 +
    1.75 +void DestroyTestEnv()
    1.76 +	{
    1.77 +	if(TheSqliteDb)
    1.78 +		{
    1.79 +		sqlite3_close(TheSqliteDb);
    1.80 +		TheSqliteDb = NULL;
    1.81 +		}
    1.82 +	TheDb.Close();
    1.83 +	(void)RSqlDatabase::Delete(KDbName2);
    1.84 +	(void)RSqlDatabase::Delete(TheTestDbName);
    1.85 +	(void)RSqlDatabase::Delete(KTestDbTemplate16);
    1.86 +	(void)RSqlDatabase::Delete(KTestDbTemplate8);
    1.87 +	sqlite3SymbianLibFinalize();
    1.88 +	CloseSTDLIB();
    1.89 +	}
    1.90 +
    1.91 +///////////////////////////////////////////////////////////////////////////////////////
    1.92 +///////////////////////////////////////////////////////////////////////////////////////
    1.93 +//Test macros and functions
    1.94 +void Check(TInt aValue, TInt aLine)
    1.95 +	{
    1.96 +	if(!aValue)
    1.97 +		{
    1.98 +		DestroyTestEnv();
    1.99 +		RDebug::Print(_L("*** Test failure. Boolean expression evaluates to false.\r\n"));
   1.100 +		TheTest(EFalse, aLine);
   1.101 +		}
   1.102 +	}
   1.103 +void Check(TInt aValue, TInt aExpected, TInt aLine)
   1.104 +	{
   1.105 +	if(aValue != aExpected)
   1.106 +		{
   1.107 +		DestroyTestEnv();
   1.108 +		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
   1.109 +		TheTest(EFalse, aLine);
   1.110 +		}
   1.111 +	}
   1.112 +#define TEST(arg) ::Check((arg), __LINE__)
   1.113 +#define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
   1.114 +
   1.115 +///////////////////////////////////////////////////////////////////////////////////////
   1.116 +
   1.117 +void CreateTestEnv()
   1.118 +    {
   1.119 +    RFs fs;
   1.120 +	TInt err = fs.Connect();
   1.121 +	TEST2(err, KErrNone);
   1.122 +
   1.123 +	err = fs.MkDir(KTestDir);
   1.124 +	TEST(err == KErrNone || err == KErrAlreadyExists);
   1.125 +
   1.126 +	TheParse.Set(TheDrive, &KTestDir, 0);
   1.127 +
   1.128 +	err = fs.MkDir(TheParse.DriveAndPath());
   1.129 +	TEST(err == KErrNone || err == KErrAlreadyExists);
   1.130 +
   1.131 +	fs.Close();
   1.132 +
   1.133 +	sqlite3SymbianLibInit();
   1.134 +	}
   1.135 +
   1.136 +///////////////////////////////////////////////////////////////////////////////////////
   1.137 +
   1.138 +void CreateTestDatabase8()
   1.139 +	{
   1.140 +	TheTest.Printf(_L("Create UTF8 test database: %S\r\n"), &KTestDbTemplate8);
   1.141 +	(void)RSqlDatabase::Delete(KTestDbTemplate8);
   1.142 +	TBuf8<KMaxFileName> fname;
   1.143 +	fname.Copy(KTestDbTemplate8);
   1.144 +	TheSqliteDb = NULL;
   1.145 +	TInt rc = sqlite3_open((const char*)fname.PtrZ(), &TheSqliteDb);
   1.146 +	TEST2(rc, SQLITE_OK);
   1.147 +	TBuf8<100> sql;
   1.148 +	_LIT8(KSql, "PRAGMA page_size=%d\x0");
   1.149 +	sql.Format(KSql, KTestDbPageSize);
   1.150 +	rc = sqlite3_exec(TheSqliteDb, (const char*)sql.Ptr(), 0, 0, 0);
   1.151 +	TEST2(rc, SQLITE_OK);
   1.152 +	rc = sqlite3_exec(TheSqliteDb, "CREATE TABLE A(Id INTEGER,Data BLOB)", 0, 0, 0);
   1.153 +	TEST2(rc, SQLITE_OK);
   1.154 +	//Insert records
   1.155 +	rc = sqlite3_exec(TheSqliteDb, "BEGIN", 0, 0, 0);
   1.156 +	TEST2(rc, SQLITE_OK);
   1.157 +	TheSqlQuery.Copy(_L8("INSERT INTO A VALUES(%d,x'"));
   1.158 +	for(TInt j=0;j<(KRecLen-50);++j)
   1.159 +		{
   1.160 +		TheSqlQuery.Append(_L8("A"));
   1.161 +		}
   1.162 +	TheSqlQuery.Append(_L8("')"));
   1.163 +	const TInt KRecCount = 100;	
   1.164 +	for(TInt i=0;i<KRecCount;++i)
   1.165 +		{
   1.166 +		TheSqlFmt.Format(TheSqlQuery, i + 1);
   1.167 +		rc = sqlite3_exec(TheSqliteDb, (const char*)TheSqlFmt.PtrZ(), 0, 0, 0);
   1.168 +		TEST2(rc, SQLITE_OK);
   1.169 +		}
   1.170 +	rc = sqlite3_exec(TheSqliteDb, "COMMIT", 0, 0, 0);
   1.171 +	TEST2(rc, SQLITE_OK);
   1.172 +	//Free some space
   1.173 +	rc = sqlite3_exec(TheSqliteDb, "DELETE FROM A WHERE Id > 10", 0, 0, 0);
   1.174 +	TEST2(rc, SQLITE_OK);
   1.175 +	sqlite3_close(TheSqliteDb);
   1.176 +	TheSqliteDb = NULL;
   1.177 +	}
   1.178 +
   1.179 +void CreateTestDatabase16()
   1.180 +	{
   1.181 +	TheTest.Printf(_L("Create UTF16 test database: %S\r\n"), &KTestDbTemplate16);
   1.182 +	(void)RSqlDatabase::Delete(KTestDbTemplate16);
   1.183 +	TBuf<KMaxFileName> fname;
   1.184 +	fname.Copy(KTestDbTemplate16);
   1.185 +	TheSqliteDb = NULL;
   1.186 +	TInt rc = sqlite3_open16(fname.PtrZ(), &TheSqliteDb);
   1.187 +	TEST2(rc, SQLITE_OK);
   1.188 +	TBuf8<100> sql;
   1.189 +	_LIT8(KSql, "PRAGMA page_size=%d\x0");
   1.190 +	sql.Format(KSql, KTestDbPageSize);
   1.191 +	rc = sqlite3_exec(TheSqliteDb, (const char*)sql.Ptr(), 0, 0, 0);
   1.192 +	TEST2(rc, SQLITE_OK);
   1.193 +	rc = sqlite3_exec(TheSqliteDb, "CREATE TABLE A(Id INTEGER,Data BLOB)", 0, 0, 0);
   1.194 +	TEST2(rc, SQLITE_OK);
   1.195 +	//Insert records
   1.196 +	rc = sqlite3_exec(TheSqliteDb, "BEGIN", 0, 0, 0);
   1.197 +	TEST2(rc, SQLITE_OK);
   1.198 +	TheSqlQuery.Copy(_L8("INSERT INTO A VALUES(%d,x'"));
   1.199 +	for(TInt j=0;j<(KRecLen-50);++j)
   1.200 +		{
   1.201 +		TheSqlQuery.Append(_L8("A"));
   1.202 +		}
   1.203 +	TheSqlQuery.Append(_L8("')"));
   1.204 +	const TInt KRecCount = 100;	
   1.205 +	for(TInt i=0;i<KRecCount;++i)
   1.206 +		{
   1.207 +		TheSqlFmt.Format(TheSqlQuery, i + 1);
   1.208 +		rc = sqlite3_exec(TheSqliteDb, (const char*)TheSqlFmt.PtrZ(), 0, 0, 0);
   1.209 +		TEST2(rc, SQLITE_OK);
   1.210 +		}
   1.211 +	rc = sqlite3_exec(TheSqliteDb, "COMMIT", 0, 0, 0);
   1.212 +	TEST2(rc, SQLITE_OK);
   1.213 +	//Free some space
   1.214 +	rc = sqlite3_exec(TheSqliteDb, "DELETE FROM A WHERE Id > 10", 0, 0, 0);
   1.215 +	TEST2(rc, SQLITE_OK);
   1.216 +	sqlite3_close(TheSqliteDb);
   1.217 +	TheSqliteDb = NULL;
   1.218 +	}
   1.219 +	
   1.220 +void CreateDatabase8(const TDesC& aTargetDbName)
   1.221 +	{
   1.222 +	RFs fs;
   1.223 +	TInt err = fs.Connect();
   1.224 +	TEST2(err, KErrNone);		
   1.225 +	CFileMan* fm = NULL;
   1.226 +	TRAP(err, fm = CFileMan::NewL(fs));
   1.227 +	TEST2(err, KErrNone);
   1.228 +	err = fm->Copy(KTestDbTemplate8, aTargetDbName);
   1.229 +	delete fm;
   1.230 +	fs.Close();
   1.231 +	TEST2(err, KErrNone);
   1.232 +	}
   1.233 +
   1.234 +void CreateDatabase16(const TDesC& aTargetDbName)
   1.235 +	{
   1.236 +	RFs fs;
   1.237 +	TInt err = fs.Connect();
   1.238 +	TEST2(err, KErrNone);		
   1.239 +	CFileMan* fm = NULL;
   1.240 +	TRAP(err, fm = CFileMan::NewL(fs));
   1.241 +	TEST2(err, KErrNone);
   1.242 +	err = fm->Copy(KTestDbTemplate16, aTargetDbName);
   1.243 +	delete fm;
   1.244 +	fs.Close();
   1.245 +	TEST2(err, KErrNone);
   1.246 +	}
   1.247 +
   1.248 +void CalculateMaxCompaction8()
   1.249 +	{
   1.250 +	TheTest.Printf(_L("UTF8 test database - calculate max compaction\r\n"));
   1.251 +	(void)RSqlDatabase::Delete(TheTestDbName);
   1.252 +	CreateDatabase8(TheTestDbName);
   1.253 +	TInt err = TheDb.Open(TheTestDbName);
   1.254 +	TEST2(err, KErrNone);
   1.255 +	RSqlDatabase::TSize size1;
   1.256 +	err = TheDb.Size(size1);
   1.257 +	TEST2(err, KErrNone);
   1.258 +	TheTest.Printf(_L("UTF8.Database before compaction: size %ld, free space %ld\r\n"), size1.iSize, size1.iFree);
   1.259 +	err = TheDb.Compact(RSqlDatabase::EMaxCompaction);
   1.260 +	TEST2(err, size1.iFree);
   1.261 +	RSqlDatabase::TSize size2;
   1.262 +	err = TheDb.Size(size2);
   1.263 +	TEST2(err, KErrNone);
   1.264 +	TheTest.Printf(_L("UTF8.Database after compaction: size %ld, free space %ld\r\n"), size2.iSize, size2.iFree);
   1.265 +	TheDb.Close();
   1.266 +	(void)RSqlDatabase::Delete(TheTestDbName);
   1.267 +	TheOriginalDbSize8 = size1.iSize;
   1.268 +	TheCompactedDbSize8 = size2.iSize;
   1.269 +	TEST(TheOriginalDbSize8 > 0);
   1.270 +	TEST(TheCompactedDbSize8 > 0 && TheCompactedDbSize8 < TheOriginalDbSize8);
   1.271 +	}
   1.272 +
   1.273 +void CalculateMaxCompaction16()
   1.274 +	{
   1.275 +	TheTest.Printf(_L("UTF16 test database - calculate max compaction\r\n"));
   1.276 +	(void)RSqlDatabase::Delete(TheTestDbName);
   1.277 +	CreateDatabase16(TheTestDbName);
   1.278 +	TInt err = TheDb.Open(TheTestDbName);
   1.279 +	TEST2(err, KErrNone);
   1.280 +	RSqlDatabase::TSize size1;
   1.281 +	err = TheDb.Size(size1);
   1.282 +	TEST2(err, KErrNone);
   1.283 +	TheTest.Printf(_L("UTF16.Database before compaction: size %ld, free space %ld\r\n"), size1.iSize, size1.iFree);
   1.284 +	err = TheDb.Compact(RSqlDatabase::EMaxCompaction);
   1.285 +	TEST2(err, size1.iFree);
   1.286 +	RSqlDatabase::TSize size2;
   1.287 +	err = TheDb.Size(size2);
   1.288 +	TEST2(err, KErrNone);
   1.289 +	TheTest.Printf(_L("UTF16.Database after compaction: size %ld, free space %ld\r\n"), size2.iSize, size2.iFree);
   1.290 +	TheDb.Close();
   1.291 +	(void)RSqlDatabase::Delete(TheTestDbName);
   1.292 +	TheOriginalDbSize16 = size1.iSize;
   1.293 +	TheCompactedDbSize16 = size2.iSize;
   1.294 +	TEST(TheOriginalDbSize16 > 0);
   1.295 +	TEST(TheCompactedDbSize16 > 0 && TheCompactedDbSize16 < TheOriginalDbSize16);
   1.296 +	}
   1.297 +
   1.298 +///////////////////////////////////////////////////////////////////////////////////////
   1.299 +
   1.300 +
   1.301 +enum TCompactionType {ESyncCompaction, EAsyncCompaction, EMaxCompactionType};
   1.302 +
   1.303 +TInt DoCompact(TCompactionType aType, TInt aSize, const TDesC& aAttachDbName = KNullDesC)
   1.304 +	{
   1.305 +	TInt err = KErrGeneral;
   1.306 +	switch(aType)
   1.307 +		{
   1.308 +		case ESyncCompaction:
   1.309 +			err = TheDb.Compact(aSize, aAttachDbName);
   1.310 +			break;
   1.311 +		case EAsyncCompaction:
   1.312 +			{
   1.313 +			TRequestStatus stat;				
   1.314 +			TheDb.Compact(aSize, stat, aAttachDbName);
   1.315 +			User::WaitForRequest(stat);
   1.316 +			TEST(stat != KRequestPending);
   1.317 +			err = stat.Int();
   1.318 +			break;
   1.319 +			}
   1.320 +		default:
   1.321 +			TEST(0);
   1.322 +			break;	
   1.323 +		}
   1.324 +	return err;
   1.325 +	}
   1.326 +
   1.327 +TInt DoManualCompaction(TCompactionType aType, const TDesC& aMainDb, TInt aSize, TBool aRoFlag = EFalse)
   1.328 +	{
   1.329 +	if(!aRoFlag)
   1.330 +		{
   1.331 +		(void)RSqlDatabase::Delete(aMainDb);
   1.332 +		CreateDatabase8(aMainDb);
   1.333 +		}
   1.334 +
   1.335 +	TInt err = TheDb.Open(aMainDb);
   1.336 +	TEST2(err, KErrNone);
   1.337 +	
   1.338 +	err = DoCompact(aType, aSize);
   1.339 +		
   1.340 +	TheDb.Close();
   1.341 +	if(!aRoFlag)
   1.342 +		{
   1.343 +		(void)RSqlDatabase::Delete(aMainDb);
   1.344 +		}
   1.345 +	return err;
   1.346 +	}
   1.347 +
   1.348 +TInt DoManualCompaction(TCompactionType aType, TInt aSize, const TDesC& aAttachDbName)
   1.349 +	{
   1.350 +	return DoCompact(aType, aSize, aAttachDbName);
   1.351 +	}
   1.352 +
   1.353 +void DoManualCompaction(TCompactionType aType, TInt aSize, TInt aCompactedSize)
   1.354 +	{
   1.355 +	(void)RSqlDatabase::Delete(TheTestDbName);
   1.356 +	CreateDatabase8(TheTestDbName);
   1.357 +
   1.358 +	TInt err = TheDb.Open(TheTestDbName);
   1.359 +	TEST2(err, KErrNone);
   1.360 +	
   1.361 +	err = DoCompact(aType, aSize);
   1.362 +	TEST(err >= 0);
   1.363 +	
   1.364 +	RSqlDatabase::TSize size;
   1.365 +	err = TheDb.Size(size);
   1.366 +	TEST2(err, KErrNone);
   1.367 +	TEST2(size.iSize, aCompactedSize);
   1.368 +		
   1.369 +	TheDb.Close();
   1.370 +	(void)RSqlDatabase::Delete(TheTestDbName);
   1.371 +	}
   1.372 +
   1.373 +void DoManualCompaction(TCompactionType aType, TInt aSize, TInt aCompactedSize, const TDesC& aAttachDbName)
   1.374 +	{
   1.375 +	TInt err = DoCompact(aType, aSize, aAttachDbName);
   1.376 +	TEST(err >= 0);
   1.377 +	
   1.378 +	RSqlDatabase::TSize size;
   1.379 +	err = TheDb.Size(size, aAttachDbName);
   1.380 +	TEST2(err, KErrNone);
   1.381 +	TEST2(size.iSize, aCompactedSize);
   1.382 +	}
   1.383 +
   1.384 +/**
   1.385 +@SYMTestCaseID			SYSLIB-SQL-UT-4064
   1.386 +@SYMTestCaseDesc		Manual compaction - negative tests.
   1.387 +						The test creates a database with a manual compaction mode.
   1.388 +						Then the test executes the following negative tests using both synchronous and
   1.389 +						asynchronous Compact() methods:
   1.390 +							- RSqlDatabase::Compact() called with aSize parameter value = KMinTInt;
   1.391 +							- RSqlDatabase::Compact() called with negative aSize parameter value;
   1.392 +							- RSqlDatabase::Compact() called on a read-only database;
   1.393 +							- RSqlDatabase::Compact() called on an attached read-only database;
   1.394 +							- RSqlDatabase::Compact() called on a nonexisting attached database with very long name;
   1.395 +							- RSqlDatabase::Compact() called with aSize = 0;
   1.396 +							- RSqlDatabase::Compact() called on a read-only database where the version number of symbian_settings table is 3;
   1.397 +@SYMTestPriority		Medium
   1.398 +@SYMTestActions			Manual compaction - negative tests.
   1.399 +@SYMTestExpectedResults Test must not fail
   1.400 +@SYMREQ					REQ10273
   1.401 +                        REQ10274
   1.402 +                        REQ10402
   1.403 +*/
   1.404 +void ManualCompactionNegativeTest()
   1.405 +	{
   1.406 +	for(TInt i=0;i<EMaxCompactionType;++i)
   1.407 +		{
   1.408 +		//Specifying KMaxTInt as aSize argument value.
   1.409 +		TInt err = DoManualCompaction((TCompactionType)i, TheTestDbName, KMaxTInt);
   1.410 +		TEST(err > 0);
   1.411 +		//Specifying KMinTInt as aSize argument value.
   1.412 +		err = DoManualCompaction((TCompactionType)i, TheTestDbName, KMinTInt);
   1.413 +		TEST2(err, KErrArgument);
   1.414 +		//Specifying another negative value as aSize argument value.
   1.415 +		err = DoManualCompaction((TCompactionType)i, TheTestDbName, -357);
   1.416 +		TEST2(err, KErrArgument);
   1.417 +		//Specifying zero as aSize argument value.
   1.418 +		err = DoManualCompaction((TCompactionType)i, TheTestDbName, 0);
   1.419 +		TEST2(err, 0);
   1.420 +		//Read-only database - old format (version 3 of symbian_settings table)
   1.421 +		err = DoManualCompaction((TCompactionType)i, KRoDbName, RSqlDatabase::EMaxCompaction, ETrue);
   1.422 +		TEST2(err, KSqlErrReadOnly);
   1.423 +		//
   1.424 +		(void)RSqlDatabase::Delete(TheTestDbName);
   1.425 +		CreateDatabase16(TheTestDbName);
   1.426 +		err = TheDb.Open(TheTestDbName);
   1.427 +		TEST2(err, KErrNone);
   1.428 +		_LIT(KAttachDbName, "Db");
   1.429 +		//Attached read-only database
   1.430 +		err = TheDb.Attach(KRoDbName, KAttachDbName);
   1.431 +		TEST2(err, KErrNone);
   1.432 +		err = DoManualCompaction((TCompactionType)i, RSqlDatabase::EMaxCompaction, KAttachDbName);
   1.433 +		TEST2(err, KSqlErrReadOnly);
   1.434 +		err = TheDb.Detach(KAttachDbName);
   1.435 +		TEST2(err, KErrNone);
   1.436 +		//Nonexisting attached database 
   1.437 +		err = DoManualCompaction((TCompactionType)i, RSqlDatabase::EMaxCompaction, _L("aaa"));
   1.438 +		TEST2(err, KSqlErrGeneral);
   1.439 +        //Very long name of a  nonexisting attached database 
   1.440 +        TBuf<KMaxFileName + 10> fname;
   1.441 +        fname.SetLength(fname.MaxLength());
   1.442 +        fname.Fill(0xDD);
   1.443 +        err = DoManualCompaction((TCompactionType)i, RSqlDatabase::EMaxCompaction, fname);
   1.444 +        TEST2(err, KErrBadName);
   1.445 +        //Invalid attached database name 
   1.446 +        fname.Copy(_L("c:\\|aaa|.db"));
   1.447 +        err = DoManualCompaction((TCompactionType)i, RSqlDatabase::EMaxCompaction, fname);
   1.448 +        TEST2(err, KSqlErrGeneral);
   1.449 +        //
   1.450 +		TheDb.Close();
   1.451 +		(void)RSqlDatabase::Delete(TheTestDbName);
   1.452 +		}
   1.453 +	}
   1.454 +
   1.455 +/**
   1.456 +@SYMTestCaseID			SYSLIB-SQL-UT-4065
   1.457 +@SYMTestCaseDesc		Manual compaction - functional tests.
   1.458 +						The test creates a database with a manual compaction mode.
   1.459 +						Then the test executes the following functional tests using both synchronous and
   1.460 +						asynchronous Compact() methods:
   1.461 +							- RSqlDatabase::Compact() called with aSize parameter value = RSqlDatabase::EMaxCompaction;
   1.462 +							- RSqlDatabase::Compact() called with aSize parameter value = 0. No pages should be removed;
   1.463 +							- RSqlDatabase::Compact() called with aSize parameter value = 1. 1 page should be removed;
   1.464 +							- RSqlDatabase::Compact() called with aSize parameter value = "db page size - 1". 1 page should be removed;
   1.465 +							- RSqlDatabase::Compact() called with aSize parameter value = "db page size * <cnt>". <cnt> pages should be removed;
   1.466 +							- RSqlDatabase::Compact() called with aSize parameter value > the free db space. All free pages should be removed;
   1.467 +						The same functional tests are repeated with an attached database.
   1.468 +@SYMTestPriority		Medium
   1.469 +@SYMTestActions			Manual compaction - functional tests.
   1.470 +@SYMTestExpectedResults Test must not fail
   1.471 +@SYMREQ					REQ10273
   1.472 +                        REQ10274
   1.473 +                        REQ10402
   1.474 +*/
   1.475 +void ManualCompactionTest()
   1.476 +	{
   1.477 +	for(TInt i=0;i<EMaxCompactionType;++i)
   1.478 +		{
   1.479 +		//Calling Compact() with aSize = RSqlDatabase::EMaxCompaction
   1.480 +		DoManualCompaction((TCompactionType)i, RSqlDatabase::EMaxCompaction, TheCompactedDbSize8);
   1.481 +		//Calling Compact() with aSize = 0. 0 pages expected to be removed
   1.482 +		DoManualCompaction((TCompactionType)i, 0, TheOriginalDbSize8);
   1.483 +		//Calling Compact() with aSize = 1. 1 page expected to be removed
   1.484 +		DoManualCompaction((TCompactionType)i, 1, TheOriginalDbSize8 - KTestDbPageSize);
   1.485 +		//Calling Compact() with aSize = KTestDbPageSize - 1. 1 page expected to be removed
   1.486 +		DoManualCompaction((TCompactionType)i, KTestDbPageSize - 1, TheOriginalDbSize8 - KTestDbPageSize);
   1.487 +		//Calling Compact() with aSize = KTestDbPageSize. 1 page expected to be removed
   1.488 +		DoManualCompaction((TCompactionType)i, KTestDbPageSize, TheOriginalDbSize8 - KTestDbPageSize);
   1.489 +		const TInt KPagesCnt1 = 17;
   1.490 +		//Calling Compact() with aSize = KTestDbPageSize * KPagesCnt1. KPagesCnt1 pages expected to be removed
   1.491 +		DoManualCompaction((TCompactionType)i, KTestDbPageSize * KPagesCnt1, TheOriginalDbSize8 - KTestDbPageSize * KPagesCnt1);
   1.492 +		//Calling Compact() with aSize > TheOriginalDbSize8. All free pages expected to be removed
   1.493 +		DoManualCompaction((TCompactionType)i, TheOriginalDbSize8 + 2000, TheCompactedDbSize8);
   1.494 +		//Attached database
   1.495 +		(void)RSqlDatabase::Delete(KDbName2);
   1.496 +		TInt err = TheDb.Create(KDbName2);
   1.497 +		TEST2(err, KErrNone);
   1.498 +		(void)RSqlDatabase::Delete(TheTestDbName);
   1.499 +		CreateDatabase16(TheTestDbName);
   1.500 +		_LIT(KAttachDbName, "Db");
   1.501 +		err = TheDb.Attach(TheTestDbName, KAttachDbName);
   1.502 +		TEST2(err, KErrNone);
   1.503 +		TInt newDatabaseSize = TheOriginalDbSize16;
   1.504 +		//Calling Compact() with aSize = 0. 0 pages expected to be removed
   1.505 +		DoManualCompaction((TCompactionType)i, 0, newDatabaseSize, KAttachDbName);
   1.506 +		//Calling Compact() with aSize = 1. 1 page expected to be removed
   1.507 +		DoManualCompaction((TCompactionType)i, 1, TheOriginalDbSize16 - KTestDbPageSize, KAttachDbName);
   1.508 +		newDatabaseSize -= KTestDbPageSize;
   1.509 +		//Calling Compact() with aSize = KTestDbPageSize - 1. 1 page expected to be removed
   1.510 +		DoManualCompaction((TCompactionType)i, KTestDbPageSize - 1, newDatabaseSize - KTestDbPageSize, KAttachDbName);
   1.511 +		newDatabaseSize -= KTestDbPageSize;
   1.512 +		//Calling Compact() with aSize = KTestDbPageSize. 1 page expected to be removed
   1.513 +		DoManualCompaction((TCompactionType)i, KTestDbPageSize, newDatabaseSize - KTestDbPageSize, KAttachDbName);
   1.514 +		newDatabaseSize -= KTestDbPageSize;
   1.515 +		//Calling Compact() with aSize = KTestDbPageSize * KPagesCnt1. KPagesCnt1 pages expected to be removed
   1.516 +		DoManualCompaction((TCompactionType)i, KTestDbPageSize * KPagesCnt1, newDatabaseSize - KTestDbPageSize * KPagesCnt1, KAttachDbName);
   1.517 +		newDatabaseSize -= KTestDbPageSize * KPagesCnt1;
   1.518 +		//Calling Compact() with aSize > newDatabaseSize. All free pages expected to be removed
   1.519 +		DoManualCompaction((TCompactionType)i, newDatabaseSize + 2000, TheCompactedDbSize16, KAttachDbName);
   1.520 +		//
   1.521 +		err = TheDb.Detach(KAttachDbName);
   1.522 +		TEST2(err, KErrNone);
   1.523 +		TheDb.Close();
   1.524 +		(void)RSqlDatabase::Delete(KDbName2);
   1.525 +		}
   1.526 +	}
   1.527 +
   1.528 +
   1.529 +enum TSizeTestType {EManualSizeTest, EAutoSizeTest};
   1.530 +
   1.531 +void DoCompactionDbSizeTest(TSizeTestType aType)
   1.532 +	{
   1.533 +	(void)RSqlDatabase::Delete(TheTestDbName);
   1.534 +	_LIT8(KConfig1, "compaction=manual");
   1.535 +	_LIT8(KConfig2, "compaction=auto");
   1.536 +	TInt err = TheDb.Create(TheTestDbName, aType == EManualSizeTest ? &KConfig1 : &KConfig2);
   1.537 +	TEST2(err, KErrNone);
   1.538 +	err = TheDb.Exec(_L("CREATE TABLE A(T TEXT)"));
   1.539 +	TEST2(err, 1);
   1.540 +	//
   1.541 +	RSqlDatabase::TSize size;
   1.542 +	err = TheDb.Size(size);
   1.543 +	TEST2(err, KErrNone);
   1.544 +	TEST2(size.iFree, 0);
   1.545 +	//
   1.546 +	const TInt KRecCnt = 50;
   1.547 +	for(TInt i=0;i<KRecCnt;++i)
   1.548 +		{
   1.549 +		err = TheDb.Exec(_L("INSERT INTO A VALUES('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaaa')"));
   1.550 +		TEST2(err, 1);
   1.551 +		}
   1.552 +	//
   1.553 +	err = TheDb.Size(size);
   1.554 +	TEST2(err, KErrNone);
   1.555 +	TEST2(size.iFree, 0);
   1.556 +	//
   1.557 +	err = TheDb.Exec(_L("DELETE FROM A WHERE 1"));
   1.558 +	TEST2(err, KRecCnt);
   1.559 +	//
   1.560 +	err = TheDb.Size(size);
   1.561 +	TEST2(err, KErrNone);
   1.562 +	if(aType == EManualSizeTest)
   1.563 +		{
   1.564 +		TEST(size.iFree > 0);
   1.565 +		}
   1.566 +	else
   1.567 +		{
   1.568 +		TEST2(size.iFree, 0);
   1.569 +		}
   1.570 +	//
   1.571 +	TheDb.Close();
   1.572 +	(void)RSqlDatabase::Delete(TheTestDbName);
   1.573 +	}
   1.574 +
   1.575 +/**
   1.576 +@SYMTestCaseID			SYSLIB-SQL-UT-4066
   1.577 +@SYMTestCaseDesc		RSqlDatabase::Size(TSize&) called on a database with manual compaction mode.
   1.578 +						The test creates a database with a manual compaction mode.
   1.579 +						Then the test inserts some records and deletes the records making some free database pages.
   1.580 +						The test calls RSqlDatabase::Size(TSize&) before and after the delete operation and verifies
   1.581 +						that the database file size stays unchanged.
   1.582 +@SYMTestPriority		Medium
   1.583 +@SYMTestActions			RSqlDatabase::Size(TSize&) called on a database with manual compaction mode.
   1.584 +@SYMTestExpectedResults Test must not fail
   1.585 +@SYMREQ					REQ10407
   1.586 +*/
   1.587 +void ManualCompactionSizeTest()
   1.588 +	{
   1.589 +	DoCompactionDbSizeTest(EManualSizeTest);
   1.590 +	}
   1.591 +
   1.592 +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   1.593 +//////////////////////////////////         OOM testing        ////////////////////////////////////////////////////////////
   1.594 +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   1.595 +
   1.596 +void PrintEndOfOomTest(TInt aFailingAllocationNo)
   1.597 +	{
   1.598 +	TheTest.Printf(_L("=== OOM Test succeeded at heap failure rate of %d ===\r\n"), aFailingAllocationNo);
   1.599 +	}
   1.600 +
   1.601 +void SetDbHeapFailure(TInt aFailingAllocationNo)
   1.602 +	{
   1.603 +	const TInt KDelayedDbHeapFailureMask = 0x1000;
   1.604 +	TSqlResourceTester::SetDbHeapFailure(RHeap::EDeterministic | KDelayedDbHeapFailureMask, aFailingAllocationNo);
   1.605 +	}
   1.606 +	
   1.607 +void ResetDbHeapFailure()
   1.608 +	{
   1.609 +	TSqlResourceTester::SetDbHeapFailure(RHeap::ENone, 0);
   1.610 +	}
   1.611 +
   1.612 +static TInt TheHandleCount1B;
   1.613 +static TInt TheHandleCount2B;
   1.614 +static TInt TheAllocatedCellsCountB;
   1.615 +
   1.616 +void MarkHandles()
   1.617 +	{
   1.618 +	RThread().HandleCount(TheHandleCount1B, TheHandleCount2B);
   1.619 +	}
   1.620 +
   1.621 +void CheckHandles()
   1.622 +	{
   1.623 +	TInt endHandleCount1E;
   1.624 +	TInt endHandleCount2E;
   1.625 +
   1.626 +	RThread().HandleCount(endHandleCount1E, endHandleCount2E);
   1.627 +
   1.628 +	TEST(TheHandleCount1B == endHandleCount1E);
   1.629 +	TEST(TheHandleCount2B == endHandleCount2E);
   1.630 +	}
   1.631 +
   1.632 +void MarkAllocatedCells()
   1.633 +	{
   1.634 +	TheAllocatedCellsCountB = User::CountAllocCells();
   1.635 +	}
   1.636 +
   1.637 +void CheckAllocatedCells()
   1.638 +	{
   1.639 +	TInt allocatedCellsCountE = User::CountAllocCells();
   1.640 +	TEST(allocatedCellsCountE == TheAllocatedCellsCountB);
   1.641 +	}
   1.642 +
   1.643 +typedef void (*TDbFuncPtrL)(const TDesC& aDbName);
   1.644 +
   1.645 +void DoManualCompactionOomTest(TDbFuncPtrL aTestFunctionPtrL, const TDesC& aDbFileName, const TDesC& aAttachDbFileName, const TDesC& aDbName)
   1.646 +	{
   1.647 +	const TInt KDoDbOomTestAllocLimitServer = 1000;
   1.648 +	TInt failingAllocation = 0;
   1.649 +	TInt allocation = 0;
   1.650 +	TInt err = KErrNoMemory;
   1.651 +	while(allocation < KDoDbOomTestAllocLimitServer)
   1.652 +		{
   1.653 +		MarkHandles();
   1.654 +		MarkAllocatedCells();
   1.655 +		
   1.656 +		__UHEAP_MARK;
   1.657 +
   1.658 +		SetDbHeapFailure(++allocation);
   1.659 +
   1.660 +		err = TheDb.Open(aDbFileName);
   1.661 +		TEST2(err, KErrNone);
   1.662 +		if(aAttachDbFileName != KNullDesC)
   1.663 +			{
   1.664 +			TEST(aDbName != KNullDesC);
   1.665 +			err = TheDb.Attach(aAttachDbFileName, aDbName);
   1.666 +			TEST(err == KErrNone || err == KErrNoMemory);
   1.667 +			}
   1.668 +		if(err == KErrNone)
   1.669 +			{
   1.670 +			TRAP(err, (*aTestFunctionPtrL)(aDbName));
   1.671 +			if(err != KErrNoMemory)
   1.672 +				{			
   1.673 +				TEST2(err, KErrNone);
   1.674 +				}
   1.675 +			else
   1.676 +				{
   1.677 +				failingAllocation = allocation;	
   1.678 +				}
   1.679 +			}
   1.680 +			
   1.681 +		ResetDbHeapFailure();
   1.682 +
   1.683 +		if(aAttachDbFileName != KNullDesC)
   1.684 +			{
   1.685 +			(void)TheDb.Detach(aDbName);
   1.686 +			}
   1.687 +		TheDb.Close();
   1.688 +
   1.689 +		__UHEAP_MARKEND;
   1.690 +
   1.691 +		CheckAllocatedCells();	    	
   1.692 +		CheckHandles();	
   1.693 +		}
   1.694 +	TEST2(err, KErrNone);
   1.695 +	PrintEndOfOomTest(failingAllocation + 1);
   1.696 +	}
   1.697 +
   1.698 +void OomTest1L(const TDesC&)
   1.699 +	{
   1.700 +	User::LeaveIfError(TheDb.Compact(RSqlDatabase::EMaxCompaction));
   1.701 +	}
   1.702 +
   1.703 +void OomTest2L(const TDesC& aDbName)
   1.704 +	{
   1.705 +	TEST(aDbName != KNullDesC);
   1.706 +	User::LeaveIfError(TheDb.Compact(RSqlDatabase::EMaxCompaction, aDbName));
   1.707 +	}
   1.708 +
   1.709 +/**
   1.710 +@SYMTestCaseID			SYSLIB-SQL-UT-4068
   1.711 +@SYMTestCaseDesc		RSqlDatabase::Compact() - OOM test.
   1.712 +						The test creates a database with a manual compaction mode.
   1.713 +						Then the test calls Compact() in an OOM loop.
   1.714 +						The same OOM test is repeated for Compact() called an attached database.
   1.715 +@SYMTestPriority		Medium
   1.716 +@SYMTestActions			RSqlDatabase::Compact() - OOM test.
   1.717 +@SYMTestExpectedResults Test must not fail
   1.718 +@SYMREQ					REQ10405
   1.719 +*/
   1.720 +void ManualCompactionOomTest()
   1.721 +	{
   1.722 +	TheTest.Printf(_L("Main database - manual compaction - OOM test\r\n"));
   1.723 +	(void)RSqlDatabase::Delete(TheTestDbName);
   1.724 +	CreateDatabase8(TheTestDbName);
   1.725 +	DoManualCompactionOomTest(&OomTest1L, TheTestDbName, KNullDesC, KNullDesC);
   1.726 +	TInt err = TheDb.Open(TheTestDbName);
   1.727 +	TEST2(err, KErrNone);
   1.728 +	RSqlDatabase::TSize size;
   1.729 +	err = TheDb.Size(size);
   1.730 +	TEST2(err, KErrNone);
   1.731 +	TEST2(size.iSize, TheCompactedDbSize8);
   1.732 +	TheDb.Close();
   1.733 +
   1.734 +	TheTest.Printf(_L("Attached database - manual compaction - OOM test\r\n"));
   1.735 +	(void)RSqlDatabase::Delete(KDbName2);
   1.736 +	err = TheDb.Create(KDbName2);
   1.737 +	TEST2(err, KErrNone);
   1.738 +	TheDb.Close();
   1.739 +	(void)RSqlDatabase::Delete(TheTestDbName);
   1.740 +	CreateDatabase16(TheTestDbName);
   1.741 +	DoManualCompactionOomTest(&OomTest2L, KDbName2, TheTestDbName, _L("Db"));
   1.742 +	err = TheDb.Open(TheTestDbName);
   1.743 +	TEST2(err, KErrNone);
   1.744 +	err = TheDb.Size(size);
   1.745 +	TEST2(err, KErrNone);
   1.746 +	TEST2(size.iSize, TheCompactedDbSize16);
   1.747 +	TheDb.Close();
   1.748 +
   1.749 +	(void)RSqlDatabase::Delete(KDbName2);
   1.750 +	(void)RSqlDatabase::Delete(TheTestDbName);
   1.751 +	}
   1.752 +
   1.753 +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   1.754 +
   1.755 +/**
   1.756 +@SYMTestCaseID			SYSLIB-SQL-UT-4067
   1.757 +@SYMTestCaseDesc		RSqlDatabase::Size(TSize&) called on a database with auto compaction mode.
   1.758 +						The test creates a database with an auto compaction mode.
   1.759 +						Then the test inserts some records and deletes the records.
   1.760 +						The test calls RSqlDatabase::Size(TSize&) after the delete operation and verifies
   1.761 +						that the database file does not contain any free pages.
   1.762 +@SYMTestPriority		Medium
   1.763 +@SYMTestActions			RSqlDatabase::Size(TSize&) called on a database with auto compaction mode.
   1.764 +@SYMTestExpectedResults Test must not fail
   1.765 +@SYMREQ					REQ10407
   1.766 +                        REQ10400
   1.767 +*/
   1.768 +void AutoCompactionSizeTest()
   1.769 +	{
   1.770 +	DoCompactionDbSizeTest(EAutoSizeTest);
   1.771 +	}
   1.772 +
   1.773 +/**
   1.774 +@SYMTestCaseID			SYSLIB-SQL-UT-4069
   1.775 +@SYMTestCaseDesc		Background compaction functional test.
   1.776 +						The test executes a 10 iterations loop with a "sleep" time 1000000 us at the beginning.
   1.777 +						The "sleep" time is divided by 2 on each iteration.
   1.778 +						In each iteration the test creates a database with free pages count big enough to kick-off the
   1.779 +						background compaction. Then the test executes enough Exec()s in order to kick-off the background compaction.
   1.780 +						Then the test "sleeps" the calculated "sleep" time and checks after that the database size and free pages
   1.781 +						count and prints them out. After the last iteration the same test is repeated with no "sleep" time.
   1.782 +						The test verifies how the client connection activity affects the possibility of the server to run the
   1.783 +						background compaction. 
   1.784 +@SYMTestPriority		Medium
   1.785 +@SYMTestActions			Background compaction functional test.
   1.786 +@SYMTestExpectedResults Test must not fail
   1.787 +@SYMREQ					REQ10271
   1.788 +                        REQ10407
   1.789 +*/
   1.790 +void BackgroundCompactionTest()
   1.791 +	{
   1.792 +	TInt interval = 1000000;//us
   1.793 +	const TInt KIterationCnt = 10;
   1.794 +	TheTest.Printf(_L("===Sleep after Exec()\r\n"));
   1.795 +	for(TInt i=0;i<KIterationCnt;++i)
   1.796 +		{
   1.797 +		(void)RSqlDatabase::Delete(TheTestDbName);
   1.798 +		CreateDatabase8(TheTestDbName);
   1.799 +		TInt err = TheDb.Open(TheTestDbName);
   1.800 +		TEST2(err, KErrNone);
   1.801 +		RSqlDatabase::TSize size1;
   1.802 +		err = TheDb.Size(size1);
   1.803 +		TEST2(err, KErrNone);
   1.804 +		TheTest.Printf(_L("---------------------------------\r\n"));
   1.805 +		TheTest.Printf(_L("===Sleep time %d ms. Database before background compaction: size %ld, free space %ld\r\n"), interval / 1000, size1.iSize, size1.iFree);
   1.806 +		//Simulate Exec() activities
   1.807 +		for(TInt j=0;j<100;++j)
   1.808 +			{
   1.809 +			err = TheDb.Exec(_L8("SELECT Id FROM A LIMIT 1"));
   1.810 +			TEST(err >= 0);
   1.811 +			}
   1.812 +		User::After(interval);
   1.813 +		RSqlDatabase::TSize size2;
   1.814 +		err = TheDb.Size(size2);
   1.815 +		TEST2(err, KErrNone);
   1.816 +		TheTest.Printf(_L("===Database after background compaction: size %ld, free space %ld\r\n"), size2.iSize, size2.iFree);
   1.817 +		TEST(size2.iSize <= size1.iSize);
   1.818 +		TEST(size2.iFree <= size1.iFree);
   1.819 +		interval /= 2;
   1.820 +		TheDb.Close();
   1.821 +		}
   1.822 +	TheTest.Printf(_L("===No sleep\r\n"));
   1.823 +	(void)RSqlDatabase::Delete(TheTestDbName);
   1.824 +	CreateDatabase8(TheTestDbName);
   1.825 +	TInt err = TheDb.Open(TheTestDbName);
   1.826 +	TEST2(err, KErrNone);
   1.827 +	RSqlDatabase::TSize size1;
   1.828 +	err = TheDb.Size(size1);
   1.829 +	TEST2(err, KErrNone);
   1.830 +	TheTest.Printf(_L("===Database before background compaction: size %ld, free space %ld\r\n"), size1.iSize, size1.iFree);
   1.831 +	//Simulate Exec() activities
   1.832 +	for(TInt j=0;j<100;++j)
   1.833 +		{
   1.834 +		err = TheDb.Exec(_L8("SELECT Id FROM A LIMIT 1"));
   1.835 +		TEST(err >= 0);
   1.836 +		}
   1.837 +	RSqlDatabase::TSize size2;
   1.838 +	err = TheDb.Size(size2);
   1.839 +	TEST2(err, KErrNone);
   1.840 +	TheTest.Printf(_L("===Database after background compaction: size %ld, free space %ld\r\n"), size2.iSize, size2.iFree);
   1.841 +	TEST(size2.iSize <= size1.iSize);
   1.842 +	TEST(size2.iFree <= size1.iFree);
   1.843 +	TheDb.Close();
   1.844 +	(void)RSqlDatabase::Delete(TheTestDbName);
   1.845 +	}
   1.846 +
   1.847 +struct TThreadData
   1.848 +	{
   1.849 +	TThreadData(const TDesC& aFileName, TInt aSleepInterval) :
   1.850 +		iDbName(aFileName),
   1.851 +		iSleepInterval(aSleepInterval)
   1.852 +		{
   1.853 +		TInt err = iCritSection.CreateLocal();
   1.854 +		TEST2(err, KErrNone);
   1.855 +		iCritSection.Wait();
   1.856 +		Mem::FillZ(&iSize1, sizeof(iSize1));
   1.857 +		Mem::FillZ(&iSize2, sizeof(iSize2));
   1.858 +		}
   1.859 +	TFileName iDbName;	
   1.860 +	RCriticalSection iCritSection;
   1.861 +	RSqlDatabase::TSize iSize1;
   1.862 +	RSqlDatabase::TSize iSize2;
   1.863 +	TInt	iSleepInterval;
   1.864 +	};
   1.865 +
   1.866 +TInt ThreadFunc(void* aPrm)
   1.867 +	{
   1.868 +	TEST(aPrm != NULL);
   1.869 +
   1.870 +	__UHEAP_MARK;
   1.871 +	CTrapCleanup* tc = CTrapCleanup::New();
   1.872 +	TheTest(tc != NULL);
   1.873 +
   1.874 +	//Wait for a signal from the main thread
   1.875 +	TThreadData* thrdat = (TThreadData*)aPrm;
   1.876 +	thrdat->iCritSection.Wait();
   1.877 +
   1.878 +	RSqlDatabase db;
   1.879 +	TInt err = db.Open(thrdat->iDbName);
   1.880 +	TEST2(err, KErrNone);
   1.881 +	err = db.Size(thrdat->iSize1);
   1.882 +	TEST2(err, KErrNone);
   1.883 +	//Simulate Exec() activities
   1.884 +	for(TInt j=0;j<100;++j)
   1.885 +		{
   1.886 +		err = db.Exec(_L8("SELECT Id FROM A LIMIT 1"));
   1.887 +		TEST(err >= 0);
   1.888 +		if((j % 10) == 0 && thrdat->iSleepInterval > 0)
   1.889 +			{
   1.890 +			User::After(thrdat->iSleepInterval);
   1.891 +			}
   1.892 +		}
   1.893 +	err = db.Size(thrdat->iSize2);
   1.894 +	TEST2(err, KErrNone);
   1.895 +	db.Close();
   1.896 +
   1.897 +	delete tc;	
   1.898 +	__UHEAP_MARKEND;
   1.899 +	return KErrNone;
   1.900 +	}
   1.901 +
   1.902 +/**
   1.903 +@SYMTestCaseID			SYSLIB-SQL-UT-4070
   1.904 +@SYMTestCaseDesc		Background compaction load test.
   1.905 +						The test runs 8 threads. Each thread connects to a different database.
   1.906 +						Each database has space in the free pages above the "free pages" threshold - 
   1.907 +						the background compaction will be scheduled at the moment when the database is opened.
   1.908 +						Every thread executes some operations on the opened database - that will delay the background compaction.
   1.909 +						After every 10 operations the thread sleeps for a specified interval of a time.
   1.910 +						After all threads complete, the test checks the database size and free pages count and
   1.911 +						prints them out.
   1.912 +						The test verifies the ability of the SQL server to run the background compaction under a load.
   1.913 +@SYMTestPriority		Medium
   1.914 +@SYMTestActions			Background compaction load test.
   1.915 +@SYMTestExpectedResults Test must not fail
   1.916 +@SYMREQ					REQ10271
   1.917 +                        REQ10407
   1.918 +*/
   1.919 +void BackgroundCompactionLoadTest()
   1.920 +	{
   1.921 +	RThread threads[KMaxThreadCount];
   1.922 +	TThreadData* thrdata[KMaxThreadCount] = {NULL};
   1.923 +	TRequestStatus thrstat[KMaxThreadCount];
   1.924 +	
   1.925 +	const TInt KSleepInterval[] = {0, 50000, 100000, 300000, 500000, 800000};//us
   1.926 +	const TInt KTestCnt = sizeof(KSleepInterval) / sizeof(KSleepInterval[0]);
   1.927 +	
   1.928 +	for(TInt k=0;k<KTestCnt;++k)
   1.929 +		{
   1.930 +		TheTest.Printf(_L("=================================================\r\n"));
   1.931 +		TheTest.Printf(_L("===Sleep interval %d ms\r\n"), KSleepInterval[k] / 1000);
   1.932 +		TheTest.Printf(_L("=================================================\r\n"));
   1.933 +		//Create test databases and threads
   1.934 +		for(TInt i=0;i<TheTestThreadCount;++i)
   1.935 +			{
   1.936 +			//Database
   1.937 +			TBuf<16> fname;
   1.938 +			fname.Copy(_L("\\test\\a"));
   1.939 +			fname.AppendNum(i + 1);
   1.940 +			fname.Append(_L(".db"));
   1.941 +			TheParse.Set(TheDrive, &fname, 0);
   1.942 +			(void)RSqlDatabase::Delete(TheParse.FullName());
   1.943 +			CreateDatabase8(TheParse.FullName());
   1.944 +			//Thread data
   1.945 +			thrdata[i] = new TThreadData(TheParse.FullName(), KSleepInterval[k]);
   1.946 +			TEST(thrdata[i] != NULL);
   1.947 +			//Thread
   1.948 +			TBuf<16> thrname;
   1.949 +			thrname.Copy(_L("Thread"));
   1.950 +			thrname.AppendNum(i + 1);
   1.951 +			TInt err = threads[i].Create(thrname, &ThreadFunc, 0x2000, 0x1000, 0x10000, thrdata[i], EOwnerProcess);
   1.952 +			TEST2(err, KErrNone);
   1.953 +			threads[i].Logon(thrstat[i]);
   1.954 +			TEST2(thrstat[i].Int(), KRequestPending);
   1.955 +			threads[i].Resume();
   1.956 +			}
   1.957 +		//Enable the threads
   1.958 +		for(TInt i=0;i<TheTestThreadCount;++i)
   1.959 +			{
   1.960 +			thrdata[i]->iCritSection.Signal();	
   1.961 +			}
   1.962 +		//Wait for cmpletion
   1.963 +		for(TInt i=0;i<TheTestThreadCount;++i)
   1.964 +			{
   1.965 +			User::WaitForRequest(thrstat[i]);
   1.966 +			}
   1.967 +		//Report
   1.968 +		for(TInt i=0;i<TheTestThreadCount;++i)
   1.969 +			{
   1.970 +			TheTest.Printf(_L("===Thread %d, database %S\r\n"), i + 1, &(thrdata[i]->iDbName));
   1.971 +			TheTest.Printf(_L("===Before background compaction: size %6ld, free space %6ld\r\n"), thrdata[i]->iSize1.iSize, thrdata[i]->iSize1.iFree);
   1.972 +			TheTest.Printf(_L("===After background compaction:  size %6ld, free space %6ld\r\n"), thrdata[i]->iSize2.iSize, thrdata[i]->iSize2.iFree);
   1.973 +			TEST(thrdata[i]->iSize2.iSize <= thrdata[i]->iSize1.iSize);
   1.974 +			TEST(thrdata[i]->iSize2.iFree <= thrdata[i]->iSize1.iFree);
   1.975 +			}
   1.976 +		//Destroy
   1.977 +		for(TInt i=0;i<TheTestThreadCount;++i)
   1.978 +			{
   1.979 +			(void)RSqlDatabase::Delete(thrdata[i]->iDbName);
   1.980 +			thrdata[i]->iCritSection.Close();
   1.981 +			delete thrdata[i];
   1.982 +			thrdata[i] = NULL;
   1.983 +			CLOSE_AND_WAIT(threads[i]);
   1.984 +			}
   1.985 +		}
   1.986 +	}
   1.987 +
   1.988 +/**
   1.989 +@SYMTestCaseID			SYSLIB-SQL-UT-4071
   1.990 +@SYMTestCaseDesc		Background compaction in a DDL transaction test.
   1.991 +						The test creates a database, begins a transaction that modifies the database structure  
   1.992 +						and executes enough operations in order free enough space to kick-off the background compaction. 
   1.993 +						The test should not report any failures caused by the fact that the main database connection is
   1.994 +						in a DML transaction and at the same time the background connection may try to execute
   1.995 +						a "PRAGMA freelist_count" statement.
   1.996 +@SYMTestPriority		Medium
   1.997 +@SYMTestActions			Background compaction in a DDL transaction test.
   1.998 +@SYMTestExpectedResults Test must not fail
   1.999 +@SYMREQ					REQ10271
  1.1000 +*/
  1.1001 +void BackgroundCompactionInDDLTransactionTest()
  1.1002 +	{
  1.1003 +	const TInt KOperationCount = 100;
  1.1004 +	(void)RSqlDatabase::Delete(KDbName);
  1.1005 +	TInt err = TheDb.Create(KDbName);
  1.1006 +	TEST2(err, KErrNone);
  1.1007 +	err = TheDb.Exec(_L("BEGIN"));
  1.1008 +	TEST(err >= 0);
  1.1009 +	err = TheDb.Exec(_L("CREATE TABLE A(I INTEGER, T TEXT)"));
  1.1010 +	TEST2(err, 1);
  1.1011 +	TheText.SetLength(KTextLen);
  1.1012 +	TheText.Fill(TChar('A'));
  1.1013 +	for(TInt i=0;i<=KOperationCount;++i)	
  1.1014 +		{
  1.1015 +		TheSqlTexLen.Format(_L("INSERT INTO A VALUES(%d, '%S')"), i + 1, &TheText);
  1.1016 +		err = TheDb.Exec(TheSqlTexLen);
  1.1017 +		TEST2(err, 1);
  1.1018 +		}
  1.1019 +	err = TheDb.Exec(_L("COMMIT"));
  1.1020 +	TEST(err >= 0);
  1.1021 +	TheDb.Close();
  1.1022 +	(void)RSqlDatabase::Delete(KDbName);
  1.1023 +	}
  1.1024 +
  1.1025 +/**
  1.1026 +@SYMTestCaseID			PDS-SQL-CT-4209
  1.1027 +@SYMTestCaseDesc		Corrupted database background compaction test.
  1.1028 +						The test creates a database, inserts records, then deletes part of the records.
  1.1029 +						The free pages count should be big enough to kick off the background compaction.
  1.1030 +						But the database is closed immediatelly and then the db file is corrupted in a such
  1.1031 +						way that during the "database open" operation the corruption is not detected.
  1.1032 +						But the corruption is detected during the background compaction. The SQL server
  1.1033 +						should detect during the compaction that the databas eis corrupted and should
  1.1034 +						stop compacting the database (and draining the battery). Unfortunatelly, this 
  1.1035 +						cannot be tested automatically, so a breakpoint should be set at the User::After()
  1.1036 +						call, and then the SQL server side should be debugged in order to berify that the
  1.1037 +						background compaction is really stopped for that database.  
  1.1038 +@SYMTestPriority		High
  1.1039 +@SYMTestActions			Corrupted database background compaction test.
  1.1040 +@SYMTestExpectedResults Test must not fail
  1.1041 +@SYMDEF					ou1cimx1#406830
  1.1042 +*/
  1.1043 +void CorruptedDbBckgCompactionTest()
  1.1044 +	{
  1.1045 +	//Step 1: Create a database with some records
  1.1046 +	const TInt KOperationCount = 100;
  1.1047 +	(void)RSqlDatabase::Delete(KDbName);
  1.1048 +	TInt err = TheDb.Create(KDbName);
  1.1049 +	TEST2(err, KErrNone);
  1.1050 +	err = TheDb.Exec(_L("BEGIN"));
  1.1051 +	TEST(err >= 0);
  1.1052 +	err = TheDb.Exec(_L("CREATE TABLE A(I INTEGER, T TEXT)"));
  1.1053 +	TEST2(err, 1);
  1.1054 +	TheText.SetLength(KTextLen);
  1.1055 +	TheText.Fill(TChar('A'));
  1.1056 +	for(TInt i=0;i<=KOperationCount;++i)	
  1.1057 +		{
  1.1058 +		TheSqlTexLen.Format(_L("INSERT INTO A VALUES(%d, '%S')"), i + 1, &TheText);
  1.1059 +		err = TheDb.Exec(TheSqlTexLen);
  1.1060 +		TEST2(err, 1);
  1.1061 +		}
  1.1062 +	err = TheDb.Exec(_L("COMMIT"));
  1.1063 +	TEST(err >= 0);
  1.1064 +	//Step 2: Delete some records to free some space
  1.1065 +	err = TheDb.Exec(_L("DELETE FROM A WHERE (I % 2) = 0"));
  1.1066 +	TEST(err > 0);
  1.1067 +	//Step 3: Close the database
  1.1068 +	TheDb.Close();
  1.1069 +	//Step 4: Corrupt the database
  1.1070 +    RFs fs;
  1.1071 +	err = fs.Connect();
  1.1072 +	TEST2(err, KErrNone);
  1.1073 +	RFile file;
  1.1074 +	err = file.Open(fs, KDbName, EFileRead | EFileWrite); 
  1.1075 +	TEST2(err, KErrNone);
  1.1076 +	TInt pos = 5000;
  1.1077 +	err = file.Seek(ESeekStart, pos);
  1.1078 +	TEST2(err, KErrNone);
  1.1079 +	TheSqlQuery.SetLength(1000);
  1.1080 +	for(TInt i=0;i<30;++i)
  1.1081 +		{
  1.1082 +		err = file.Write(TheSqlQuery);
  1.1083 +		TEST2(err, KErrNone);
  1.1084 +		}
  1.1085 +	file.Close();
  1.1086 +	//Step 5: Check the background compaction. Wait 10 seconds allowing the SQL server to try to compact the
  1.1087 +	//        database. The SQL server should detect that the SQL database is corrupted and should stop trying to
  1.1088 +	//        compact the database.
  1.1089 +	err = TheDb.Open(KDbName);
  1.1090 +	TEST2(err, KErrNone);
  1.1091 +	User::After(10000000);
  1.1092 +	//
  1.1093 +	TheDb.Close();
  1.1094 +	(void)RSqlDatabase::Delete(KDbName);
  1.1095 +	}
  1.1096 +
  1.1097 +void DoTestsL()
  1.1098 +	{
  1.1099 +	CreateTestDatabase8();
  1.1100 +	CalculateMaxCompaction8();
  1.1101 +	CreateTestDatabase16();
  1.1102 +	CalculateMaxCompaction16();
  1.1103 +
  1.1104 +	TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4064 Manual Compact() - negative tests"));	
  1.1105 +	ManualCompactionNegativeTest();
  1.1106 +
  1.1107 +	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4065 Manual Compact() tests"));	
  1.1108 +	ManualCompactionTest();
  1.1109 +
  1.1110 +	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4066 Manual compaction db size test"));	
  1.1111 +	ManualCompactionSizeTest();
  1.1112 +
  1.1113 +	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4068 Manual compaction - OOM test"));	
  1.1114 +	ManualCompactionOomTest();
  1.1115 +
  1.1116 +	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4067 Auto compaction db size test"));	
  1.1117 +	AutoCompactionSizeTest();
  1.1118 +
  1.1119 +	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4069 Background compaction test"));	
  1.1120 +	BackgroundCompactionTest();
  1.1121 +
  1.1122 +	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4070 Background compaction - load test"));	
  1.1123 +	BackgroundCompactionLoadTest();
  1.1124 +
  1.1125 +	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4071 Background compaction activated inside a DDL transaction - test"));	
  1.1126 +	BackgroundCompactionInDDLTransactionTest();
  1.1127 +	
  1.1128 +	TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-CT-4209 Corrupted database background compaction test"));	
  1.1129 +	CorruptedDbBckgCompactionTest();
  1.1130 +	}
  1.1131 +
  1.1132 +TInt E32Main()
  1.1133 +	{
  1.1134 +	TheTest.Title();
  1.1135 +	
  1.1136 +	CTrapCleanup* tc = CTrapCleanup::New();
  1.1137 +	TheTest(tc != NULL);
  1.1138 +	
  1.1139 +	TheTest.Printf(_L("Usage:\r\n  t_sqlcompact4 [[drive:] [test thread count]]\r\n"));
  1.1140 +	TheDrive.Copy(KDefaultDriveName);
  1.1141 +	User::CommandLine(TheCmd);
  1.1142 +	TheCmd.TrimAll();
  1.1143 +	if(TheCmd.Length() > 0)
  1.1144 +		{
  1.1145 +		TInt pos = TheCmd.Locate(TChar(' '));
  1.1146 +		TheTest(pos > 0);
  1.1147 +		TPtrC prm1(TheCmd.Left(pos));
  1.1148 +		TPtrC prm2(TheCmd.Mid(pos + 1));
  1.1149 +		
  1.1150 +		TheDrive.Copy(prm1);
  1.1151 +		
  1.1152 +		TLex lex(prm2);
  1.1153 +		lex.Val(TheTestThreadCount);
  1.1154 +		}
  1.1155 +	TheParse.Set(TheDrive, &KDbName, 0);
  1.1156 +	TheTestDbName.Copy(TheParse.FullName());
  1.1157 +	TheTest.Printf(_L("Test database: %S\r\n"), &TheTestDbName);
  1.1158 +	
  1.1159 +	__UHEAP_MARK;
  1.1160 +	
  1.1161 +	CreateTestEnv();
  1.1162 +	TRAPD(err, DoTestsL());
  1.1163 +	DestroyTestEnv();
  1.1164 +	TEST2(err, KErrNone);
  1.1165 +	
  1.1166 +	__UHEAP_MARKEND;
  1.1167 +	
  1.1168 +	TheTest.End();
  1.1169 +	TheTest.Close();
  1.1170 +	
  1.1171 +	delete tc;
  1.1172 +	
  1.1173 +	User::Heap().Check();
  1.1174 +	return KErrNone;
  1.1175 +	}