persistentstorage/sql/TEST/t_sqlconfigfile.cpp
changeset 0 08ec8eefde2f
child 12 6b6fd149daa2
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <e32test.h>
       
    17 #include <sqldb.h>
       
    18 //#include "SqlUtil.h"
       
    19 #include "SqlSrvConfig.h"
       
    20 #include "SqlResourceTester.h"
       
    21 
       
    22 /////////////////////////////////////////////////////////////////////////////////////////////////
       
    23 /// This test works only if the whole SQL component is built with SYSLIBS_TEST macro defined! ///
       
    24 /////////////////////////////////////////////////////////////////////////////////////////////////
       
    25 
       
    26 RTest TheTest(_L("t_sqlconfigfile test"));
       
    27 
       
    28 #ifdef SYSLIBS_TEST	
       
    29 
       
    30 RFs TheFs;
       
    31 RSqlDatabase TheDb;
       
    32 
       
    33 _LIT(KTestDir, "c:\\test\\");
       
    34 _LIT(KTestDbName, "c:\\test\\t_sqlconfigfile.db");
       
    35 _LIT(KSqlSrvConfigFile, "c:\\test\\t_sqlserver.cfg");
       
    36 _LIT(KSqlSrvName, "sqlsrv.exe");
       
    37 
       
    38 enum TConfigParamType {EPrmCacheSize, EPrmPageSize, EPrmDbEncoding};
       
    39 
       
    40 //Default configuration parameter values, defined in ../test/sqlserver.cfg file
       
    41 //(the same as the build-time configuration parameter values)
       
    42 const TInt KDefaultPageSize = 1024;
       
    43 const TInt KDefaultCacheSize = (TSqlSrvConfigParams::KDefaultSoftHeapLimitKb * 1024) / KDefaultPageSize;
       
    44 const TSqlSrvConfigParams::TDbEncoding KDefaultEncoding = TSqlSrvConfigParams::EEncUtf16;
       
    45 
       
    46 TInt KillProcess(const TDesC& aProcessName);
       
    47 
       
    48 ///////////////////////////////////////////////////////////////////////////////////////
       
    49 // Destroy functions
       
    50 
       
    51 TInt KillProcess(const TDesC& aProcessName)
       
    52 	{
       
    53 	TFullName name;
       
    54 	//RDebug::Print(_L("Find and kill \"%S\" process.\n"), &aProcessName);
       
    55 	TBuf<64> pattern(aProcessName);
       
    56 	TInt length = pattern.Length();
       
    57 	pattern += _L("*");
       
    58 	TFindProcess procFinder(pattern);
       
    59 
       
    60 	while (procFinder.Next(name) == KErrNone)
       
    61 		{
       
    62 		if (name.Length() > length)
       
    63 			{//If found name is a string containing aProcessName string.
       
    64 			TChar c(name[length]);
       
    65 			if (c.IsAlphaDigit() ||
       
    66 				c == TChar('_') ||
       
    67 				c == TChar('-'))
       
    68 				{
       
    69 				// If the found name is other valid application name
       
    70 				// starting with aProcessName string.
       
    71 				//RDebug::Print(_L(":: Process name: \"%S\".\n"), &name);
       
    72 				continue;
       
    73 				}
       
    74 			}
       
    75 		RProcess proc;
       
    76 		if (proc.Open(name) == KErrNone)
       
    77 			{
       
    78 			proc.Kill(0);
       
    79 			//RDebug::Print(_L("\"%S\" process killed.\n"), &name);
       
    80 			}
       
    81 		proc.Close();
       
    82 		}
       
    83 	return KErrNone;
       
    84 	}
       
    85 
       
    86 void DestroyTestEnv()
       
    87 	{
       
    88 	TheDb.Close();
       
    89 	(void)KillProcess(KSqlSrvName);
       
    90 	(void)TheFs.Delete(KTestDbName);
       
    91 	(void)TheFs.Delete(KSqlSrvConfigFile);
       
    92 	TheFs.Close();
       
    93 	}
       
    94 	
       
    95 ///////////////////////////////////////////////////////////////////////////////////////
       
    96 // Test macros and functions
       
    97 
       
    98 void Check(TInt aValue, TInt aLine)
       
    99 	{
       
   100 	if(!aValue)
       
   101 		{
       
   102 		DestroyTestEnv();
       
   103 		TheTest(EFalse, aLine);
       
   104 		}
       
   105 	}
       
   106 void Check(TInt aValue, TInt aExpected, TInt aLine)
       
   107 	{
       
   108 	if(aValue != aExpected)
       
   109 		{
       
   110 		DestroyTestEnv();
       
   111 		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
       
   112 		TheTest(EFalse, aLine);
       
   113 		}
       
   114 	}
       
   115 #define TEST(arg) ::Check((arg), __LINE__)
       
   116 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
       
   117 
       
   118 // OOM test functions
       
   119 
       
   120 static TInt TheHandleCount1;
       
   121 static TInt TheHandleCount2;
       
   122 static TInt TheAllocatedCellsCount;
       
   123 
       
   124 void MarkHandles()
       
   125 	{
       
   126 	RThread().HandleCount(TheHandleCount1, TheHandleCount2);
       
   127 	}
       
   128 
       
   129 void CheckHandles()
       
   130 	{
       
   131 	TInt endHandleCount1;
       
   132 	TInt endHandleCount2;
       
   133 
       
   134 	RThread().HandleCount(endHandleCount1, endHandleCount2);
       
   135 
       
   136 	TEST(TheHandleCount1 == endHandleCount1);
       
   137 	TEST(TheHandleCount2 == endHandleCount2);
       
   138 	}
       
   139 
       
   140 void MarkAllocatedCells()
       
   141 	{
       
   142 	TheAllocatedCellsCount = User::CountAllocCells();
       
   143 	}
       
   144 
       
   145 void CheckAllocatedCells()
       
   146 	{
       
   147 	TInt allocatedCellsCount = User::CountAllocCells();
       
   148 	TEST(allocatedCellsCount == TheAllocatedCellsCount);
       
   149 	}
       
   150 
       
   151 ///////////////////////////////////////////////////////////////////////////////////////
       
   152 // Set up functions
       
   153 
       
   154 TInt DoCreateSecurityPolicy(RSqlSecurityPolicy& securityPolicy)
       
   155 	{
       
   156 	const TSecurityPolicy KDefaultPolicy(TSecurityPolicy::EAlwaysPass);
       
   157 	if((KErrNone != securityPolicy.Create(KDefaultPolicy))
       
   158 	   ||
       
   159 	   (KErrNone != securityPolicy.SetDbPolicy(RSqlSecurityPolicy::ESchemaPolicy, KDefaultPolicy))
       
   160 	   ||
       
   161 	   (KErrNone != securityPolicy.SetDbPolicy(RSqlSecurityPolicy::EWritePolicy, KDefaultPolicy))
       
   162 	   ||
       
   163 	   (KErrNone != securityPolicy.SetDbPolicy(RSqlSecurityPolicy::EReadPolicy, KDefaultPolicy)))
       
   164 		{
       
   165 		return KErrGeneral;
       
   166 		}
       
   167 		
       
   168 	return KErrNone;
       
   169 	}
       
   170 	
       
   171 void SetupTestEnv()
       
   172     {
       
   173 	TInt err = TheFs.Connect();
       
   174 	TEST2(err, KErrNone);
       
   175 
       
   176 	err = TheFs.MkDir(KTestDir);
       
   177 	TEST(err == KErrNone || err == KErrAlreadyExists);
       
   178 
       
   179 	err = TheFs.CreatePrivatePath(EDriveC);
       
   180 	TEST(err == KErrNone || err == KErrAlreadyExists);
       
   181 
       
   182 	(void)TheFs.Delete(KTestDbName);
       
   183 	(void)TheFs.Delete(KSqlSrvConfigFile);
       
   184 	}
       
   185 
       
   186 ///////////////////////////////////////////////////////////////////////////////////////
       
   187 // Parameter check functions
       
   188 
       
   189 TInt DoGetConfigParamValueL(RSqlDatabase& aDb, TConfigParamType aPrmType)
       
   190 	{
       
   191 	TSqlScalarFullSelectQuery q(aDb);
       
   192 	TInt res = 0;
       
   193 	switch(aPrmType)
       
   194 		{
       
   195 		case EPrmCacheSize:
       
   196 			res = q.SelectIntL(_L8("PRAGMA cache_size"));
       
   197 			break;
       
   198 		case EPrmPageSize:
       
   199 			res = q.SelectIntL(_L8("PRAGMA page_size"));
       
   200 			break;
       
   201 		case EPrmDbEncoding:
       
   202 			{
       
   203 			TBuf<20> dbEncodingText;
       
   204 			res = q.SelectTextL(_L8("PRAGMA encoding"), dbEncodingText);
       
   205 			TEST2(res, KErrNone);
       
   206 			if(dbEncodingText.FindF(_L("UTF-16")) >= 0)
       
   207 				{
       
   208 				res = TSqlSrvConfigParams::EEncUtf16;	
       
   209 				}
       
   210 			else if(dbEncodingText.FindF(_L("UTF-8")) >= 0)
       
   211 				{
       
   212 				res = TSqlSrvConfigParams::EEncUtf8;	
       
   213 				}
       
   214 			else
       
   215 				{
       
   216 				TEST2(0, 1);
       
   217 				}
       
   218 			}
       
   219 			break;
       
   220 		default:
       
   221 			TEST2(0, 1);
       
   222 			break;
       
   223 		}
       
   224 	return res;
       
   225 	}
       
   226 
       
   227 TInt GetConfigParamValue(RSqlDatabase& aDb, TConfigParamType aPrmType)
       
   228 	{
       
   229 	TInt res = 0;
       
   230 	TRAPD(err, res = DoGetConfigParamValueL(aDb, aPrmType));
       
   231 	TEST2(err, KErrNone);
       
   232 	return res;
       
   233 	}
       
   234 
       
   235 void AssertConfigPrmValues(RSqlDatabase& aDb, TInt aExpectedCacheSize, TInt aExpectedPageSize, TInt aExpectedDbEncoding)
       
   236 	{
       
   237 	TInt cacheSize = GetConfigParamValue(aDb, EPrmCacheSize);
       
   238 	TInt pageSize = GetConfigParamValue(aDb, EPrmPageSize);
       
   239 	TInt dbEncoding = GetConfigParamValue(aDb, EPrmDbEncoding);
       
   240 	TEST2(cacheSize, aExpectedCacheSize);
       
   241 	TEST2(pageSize, aExpectedPageSize);
       
   242 	TEST2(dbEncoding, aExpectedDbEncoding);
       
   243 	}
       
   244 
       
   245 ///////////////////////////////////////////////////////////////////////////////////////
       
   246 // Config file replacement functions
       
   247 
       
   248 // File config strings are 16-bit.
       
   249 void ReplaceConfigFile(const TDesC16& aConfig)
       
   250 	{
       
   251 	(void)KillProcess(KSqlSrvName);
       
   252 	(void)TheFs.Delete(KSqlSrvConfigFile);
       
   253 	RFile file;
       
   254 	TInt err = file.Create(TheFs, KSqlSrvConfigFile, EFileRead | EFileWrite);
       
   255 	TEST2(err, KErrNone);
       
   256 	TPtrC8 p((const TUint8*)aConfig.Ptr(), aConfig.Length() * sizeof(TUint16));
       
   257 	err = file.Write(p);
       
   258 	file.Close();
       
   259 	TEST2(err, KErrNone);
       
   260 	}
       
   261 	
       
   262 ///////////////////////////////////////////////////////////////////////////////////////
       
   263 //
       
   264 
       
   265 /**
       
   266 @SYMTestCaseID			SYSLIB-SQL-UT-3603
       
   267 @SYMTestCaseDesc		Bad config file test
       
   268 						The test creates bad config files like:
       
   269 							- empty config file;
       
   270 							- "\n" config file;
       
   271 							- "\r\n" config file;
       
   272 							- config file with comment lines only;
       
   273 						Then the test restarts the SQL server and checks that the bad config file is detected and 
       
   274 						appropriate error code - returned to the caller (during "database create" operation).
       
   275 @SYMTestPriority		High
       
   276 @SYMTestActions			Bad config file test
       
   277 @SYMTestExpectedResults The test must not fail
       
   278 @SYMREQ					REQ8162
       
   279 */
       
   280 void BadCfgFileTest()
       
   281 	{
       
   282 	//Empty config file	
       
   283 	ReplaceConfigFile(_L(""));
       
   284 	TInt err = TheDb.Create(KTestDbName);
       
   285 	TEST2(err, KErrEof);//BC kept - an empty config file is treated as invalid
       
   286 	TheDb.Close();
       
   287 	(void)RSqlDatabase::Delete(KTestDbName);
       
   288 	//"\n" config file	
       
   289 	ReplaceConfigFile(_L("\n"));
       
   290 	err = TheDb.Create(KTestDbName);
       
   291 	TEST2(err, KErrEof);//BC compatible
       
   292 	TheDb.Close();
       
   293 	(void)RSqlDatabase::Delete(KTestDbName);
       
   294 	//"\r\n" config file	
       
   295 	ReplaceConfigFile(_L("\r\n"));
       
   296 	err = TheDb.Create(KTestDbName);
       
   297 	TEST2(err, KErrEof);//BC compatible
       
   298 	TheDb.Close();
       
   299 	(void)RSqlDatabase::Delete(KTestDbName);
       
   300 	//"        \r\n" config file	
       
   301 	ReplaceConfigFile(_L("        \r\n"));
       
   302 	err = TheDb.Create(KTestDbName);
       
   303 	TEST2(err, KErrEof);//BC compatible
       
   304 	TheDb.Close();
       
   305 	(void)RSqlDatabase::Delete(KTestDbName);
       
   306 	//"  # \r\n" config file
       
   307 	ReplaceConfigFile(_L("  # \r\n"));
       
   308 	err = TheDb.Create(KTestDbName);
       
   309 	TEST2(err, KErrEof);//BC compatible
       
   310 	TheDb.Close();
       
   311 	(void)RSqlDatabase::Delete(KTestDbName);
       
   312 	//"  # \r\na=b\r\n" config file
       
   313 	ReplaceConfigFile(_L("  # \r\na=b\r\n"));
       
   314 	err = TheDb.Create(KTestDbName);
       
   315 	TEST2(err, KErrNone);
       
   316 	AssertConfigPrmValues(TheDb, KDefaultCacheSize, KDefaultPageSize, KDefaultEncoding);
       
   317 	TheDb.Close();
       
   318 	(void)RSqlDatabase::Delete(KTestDbName);
       
   319 	//"  # \r\n   a=b   \r\n" config file
       
   320 	ReplaceConfigFile(_L("  # \r\n   a=b   \r\n"));
       
   321 	err = TheDb.Create(KTestDbName);
       
   322 	TEST2(err, KErrNone);
       
   323 	AssertConfigPrmValues(TheDb, KDefaultCacheSize, KDefaultPageSize, KDefaultEncoding);
       
   324 	TheDb.Close();
       
   325 	(void)RSqlDatabase::Delete(KTestDbName);
       
   326 	//"  # \r\n   a=b  " config file
       
   327 	ReplaceConfigFile(_L("  # \r\n   a=b  "));
       
   328 	err = TheDb.Create(KTestDbName);
       
   329 	TEST2(err, KErrNone);
       
   330 	AssertConfigPrmValues(TheDb, KDefaultCacheSize, KDefaultPageSize, KDefaultEncoding);
       
   331 	TheDb.Close();
       
   332 	(void)RSqlDatabase::Delete(KTestDbName);
       
   333 	(void)TheFs.Delete(KSqlSrvConfigFile);
       
   334 	}
       
   335 
       
   336 /**
       
   337 @SYMTestCaseID			SYSLIB-SQL-UT-3604
       
   338 @SYMTestCaseDesc		Config file with bad parameters test
       
   339 						The test creates config files with bad parameters like:
       
   340 							- negative cache size;
       
   341 							- non-numeric cache size value;
       
   342 							- empty cache size value;
       
   343 							- negative page size;
       
   344 							- non-numeric page size value;
       
   345 							- empty page size value;
       
   346 							- negative soft heap limit size;
       
   347 							- non-numeric soft heap limit value;
       
   348 							- empty soft heap limit value;
       
   349 							- too small soft heap limit value;
       
   350 							- too big soft heap limit value;
       
   351 							- negative free page threshold value;
       
   352 							- empty free page threshold value;
       
   353 							- non-numeric free page threshold value;
       
   354 						Then the test restarts the SQL server and checks that the bad config file is detected and 
       
   355 						appropriate error code - returned to the caller (during "database create" operation).
       
   356 @SYMTestPriority		High
       
   357 @SYMTestActions			Config file with bad parameters test
       
   358 @SYMTestExpectedResults The test must not fail
       
   359 @SYMREQ					REQ8162
       
   360                         REQ10271
       
   361 */
       
   362 void BadCfgFileParametersTest()
       
   363 	{
       
   364 	/////////////// cache_size  ////////////////
       
   365 	//"cache_size=-20;" config file
       
   366 	ReplaceConfigFile(_L("cache_size=-20;"));
       
   367 	TInt err = TheDb.Create(KTestDbName);
       
   368 	TEST2(err, KErrArgument);
       
   369 	TheDb.Close();
       
   370 	(void)RSqlDatabase::Delete(KTestDbName);
       
   371 	//"cache_size=456.90" config file
       
   372 	ReplaceConfigFile(_L("cache_size=456.90"));
       
   373 	err = TheDb.Create(KTestDbName);
       
   374 	TEST2(err, KErrNone);
       
   375 	TheDb.Close();
       
   376 	(void)RSqlDatabase::Delete(KTestDbName);
       
   377 	//"cache_size='dfjkhdfjk';" config file
       
   378 	ReplaceConfigFile(_L("cache_size='dfjkhdfjk';"));
       
   379 	err = TheDb.Create(KTestDbName);
       
   380 	TEST2(err, KErrArgument);
       
   381 	TheDb.Close();
       
   382 	(void)RSqlDatabase::Delete(KTestDbName);
       
   383 	//"cache_size=;" config file
       
   384 	ReplaceConfigFile(_L("cache_size=;"));
       
   385 	err = TheDb.Create(KTestDbName);
       
   386 	TEST2(err, KErrArgument);
       
   387 	TheDb.Close();
       
   388 	(void)RSqlDatabase::Delete(KTestDbName);
       
   389 	/////////////// page_size  ////////////////
       
   390 	//"page_size=-55" config file
       
   391 	ReplaceConfigFile(_L("page_size=-55"));
       
   392 	err = TheDb.Create(KTestDbName);
       
   393 	TEST2(err, KErrArgument);
       
   394 	TheDb.Close();
       
   395 	(void)RSqlDatabase::Delete(KTestDbName);
       
   396 	//"page_size=25.89" config file
       
   397 	ReplaceConfigFile(_L("page_size=25.89"));
       
   398 	err = TheDb.Create(KTestDbName);
       
   399 	TEST2(err, KErrNone);//BC compatible
       
   400 	TheDb.Close();
       
   401 	(void)RSqlDatabase::Delete(KTestDbName);
       
   402 	//"page_size=gffgrtj" config file
       
   403 	ReplaceConfigFile(_L("page_size=gffgrtj"));
       
   404 	err = TheDb.Create(KTestDbName);
       
   405 	TEST2(err, KErrArgument);
       
   406 	TheDb.Close();
       
   407 	(void)RSqlDatabase::Delete(KTestDbName);
       
   408 	//"page_size=" config file
       
   409 	ReplaceConfigFile(_L("page_size="));
       
   410 	err = TheDb.Create(KTestDbName);
       
   411 	TEST2(err, KErrArgument);
       
   412 	TheDb.Close();
       
   413 	(void)RSqlDatabase::Delete(KTestDbName);
       
   414 	////////   soft_heap_limit_kb    ///////////
       
   415 	//"soft_heap_limit_kb=-10" config file
       
   416 	ReplaceConfigFile(_L("soft_heap_limit_kb=-10"));
       
   417 	err = TheDb.Create(KTestDbName);
       
   418 	TEST2(err, KErrArgument);
       
   419 	TheDb.Close();
       
   420 	(void)RSqlDatabase::Delete(KTestDbName);
       
   421 	//"soft_heap_limit_kb=5" config file (bellow min limit - 8Kb when SYSLIBS_TEST macro is defined)
       
   422 	ReplaceConfigFile(_L("soft_heap_limit_kb=5"));
       
   423 	err = TheDb.Create(KTestDbName);
       
   424 	TEST2(err, KErrArgument);
       
   425 	TheDb.Close();
       
   426 	(void)RSqlDatabase::Delete(KTestDbName);
       
   427 	//"soft_heap_limit_kb=8" config file (the min limit - 8Kb when SYSLIBS_TEST macro is defined)
       
   428 	ReplaceConfigFile(_L("soft_heap_limit_kb=8"));
       
   429 	err = TheDb.Create(KTestDbName);
       
   430 	TEST2(err, KErrNone);
       
   431 	TheDb.Close();
       
   432 	(void)RSqlDatabase::Delete(KTestDbName);
       
   433 	//"soft_heap_limit_kb=2000000000" config file (above max limit)
       
   434 	ReplaceConfigFile(_L("soft_heap_limit_kb=2000000000"));
       
   435 	err = TheDb.Create(KTestDbName);
       
   436 	TEST2(err, KErrArgument);
       
   437 	TheDb.Close();
       
   438 	(void)RSqlDatabase::Delete(KTestDbName);
       
   439 	//"soft_heap_limit_kb=KMaxTInt/1024" config file (the max limit)
       
   440 	TBuf<32> configBuf;
       
   441 	configBuf.Copy(_L("soft_heap_limit_kb="));
       
   442 	TInt maxSoftHeapLimit = KMaxTInt / 1024;
       
   443 	configBuf.AppendNum(maxSoftHeapLimit);
       
   444 	ReplaceConfigFile(configBuf);
       
   445 	err = TheDb.Create(KTestDbName);
       
   446 	TEST2(err, KErrNone);
       
   447 	TheDb.Close();
       
   448 	(void)RSqlDatabase::Delete(KTestDbName);
       
   449 	//"soft_heap_limit_kb=sdfcvyua" config file
       
   450 	ReplaceConfigFile(_L("soft_heap_limit_kb=sdfcvyua"));
       
   451 	err = TheDb.Create(KTestDbName);
       
   452 	TEST2(err, KErrArgument);
       
   453 	TheDb.Close();
       
   454 	(void)RSqlDatabase::Delete(KTestDbName);
       
   455 	//"soft_heap_limit_kb=" config file
       
   456 	ReplaceConfigFile(_L("soft_heap_limit_kb="));
       
   457 	err = TheDb.Create(KTestDbName);
       
   458 	TEST2(err, KErrArgument);
       
   459 	TheDb.Close();
       
   460 	(void)RSqlDatabase::Delete(KTestDbName);
       
   461 	//"soft_heap_limit_kb=1023.456" config file
       
   462 	ReplaceConfigFile(_L("soft_heap_limit_kb=1023.456"));
       
   463 	err = TheDb.Create(KTestDbName);
       
   464 	TEST2(err, KErrNone);
       
   465 	TheDb.Close();
       
   466 	(void)RSqlDatabase::Delete(KTestDbName);
       
   467 	////////   free_space_threshold_kb    ///////////
       
   468 	//"free_space_threshold_kb=-10" config file
       
   469 	ReplaceConfigFile(_L("free_space_threshold_kb=-10"));
       
   470 	err = TheDb.Create(KTestDbName);
       
   471 	TEST2(err, KErrArgument);
       
   472 	TheDb.Close();
       
   473 	(void)RSqlDatabase::Delete(KTestDbName);
       
   474 	//"free_space_threshold_kb=0" config file
       
   475 	ReplaceConfigFile(_L("free_space_threshold_kb=0"));
       
   476 	err = TheDb.Create(KTestDbName);
       
   477 	TEST2(err, KErrNone);
       
   478 	TheDb.Close();
       
   479 	(void)RSqlDatabase::Delete(KTestDbName);
       
   480 	//"free_space_threshold_kb=" config file
       
   481 	ReplaceConfigFile(_L("free_space_threshold_kb="));
       
   482 	err = TheDb.Create(KTestDbName);
       
   483 	TEST2(err, KErrArgument);
       
   484 	TheDb.Close();
       
   485 	(void)RSqlDatabase::Delete(KTestDbName);
       
   486 	//"free_space_threshold_kb=34.56" config file
       
   487 	ReplaceConfigFile(_L("free_space_threshold_kb=34.56"));
       
   488 	err = TheDb.Create(KTestDbName);
       
   489 	TEST2(err, KErrNone);
       
   490 	TheDb.Close();
       
   491 	(void)RSqlDatabase::Delete(KTestDbName);
       
   492 	//"free_space_threshold_kb=gfghfg" config file
       
   493 	ReplaceConfigFile(_L("free_space_threshold_kb=gfghfg"));
       
   494 	err = TheDb.Create(KTestDbName);
       
   495 	TEST2(err, KErrArgument);
       
   496 	TheDb.Close();
       
   497 	(void)RSqlDatabase::Delete(KTestDbName);
       
   498 	/////////////////////////////////////////////
       
   499 	(void)TheFs.Delete(KSqlSrvConfigFile);
       
   500 	}
       
   501 
       
   502 /**
       
   503 @SYMTestCaseID			SYSLIB-SQL-UT-3605
       
   504 @SYMTestCaseDesc		Config parameters conflict test.
       
   505 						1) The test creates a database with cache size parameter value specified in both the config file and the
       
   506 							config string. The expectation is that the config string parameter will be used.
       
   507 						2) The test creates a database with page size parameter value specified in both the config file and the
       
   508 							config string. The expectation is that the config string parameter will be used.
       
   509 						3) The test creates a database with encoding parameter value specified in both the config file and the
       
   510 							config string. The expectation is that the config string parameter will be used.
       
   511 						4) The test creates a database with soft heap limit value specified in both the config file and the
       
   512 							config string. The expectation is that the database creation will fail (the soft heap limit
       
   513 							cannot be configured using a config string).
       
   514 						5) The test creates a database with free page threshold value specified in both the config file and the
       
   515 							config string. The expectation is that the database creation will succeeds. The free page threshold
       
   516 							value from the config file will be used.
       
   517 @SYMTestPriority		High
       
   518 @SYMTestActions			Config parameters conflict test
       
   519 @SYMTestExpectedResults The test must not fail
       
   520 @SYMREQ					REQ8162
       
   521                         REQ10271
       
   522 */
       
   523 void CfgFileConflictTest()
       
   524 	{
       
   525 	//"cache_size=200" config file
       
   526 	//"cache_size=100" client config string
       
   527 	ReplaceConfigFile(_L("cache_size=200"));
       
   528 	_LIT8(KConfigStr1, "cache_size=100");
       
   529 	TInt err = TheDb.Create(KTestDbName, &KConfigStr1);
       
   530 	TEST2(err, KErrNone);
       
   531 	AssertConfigPrmValues(TheDb, 100, KDefaultPageSize, KDefaultEncoding);
       
   532 	TheDb.Close();
       
   533 	(void)RSqlDatabase::Delete(KTestDbName);
       
   534 	//"page_size=512" config file
       
   535 	//"page_size=8192" client config string
       
   536 	ReplaceConfigFile(_L("page_size=512"));
       
   537 	_LIT8(KConfigStr2, "page_size=8192");
       
   538 	err = TheDb.Create(KTestDbName, &KConfigStr2);
       
   539 	TEST2(err, KErrNone);
       
   540 	AssertConfigPrmValues(TheDb, (TSqlSrvConfigParams::KDefaultSoftHeapLimitKb * 1024) / 8192, 8192, KDefaultEncoding);
       
   541 	TheDb.Close();
       
   542 	(void)RSqlDatabase::Delete(KTestDbName);
       
   543 	//"encoding=UTF-16" config file
       
   544 	//"encoding=UTF-8" client config string
       
   545 	ReplaceConfigFile(_L("encoding=UTF-16"));
       
   546 	_LIT8(KConfigStr3, "encoding=UTF-8");
       
   547 	err = TheDb.Create(KTestDbName, &KConfigStr3);
       
   548 	TEST2(err, KErrNone);
       
   549 	AssertConfigPrmValues(TheDb, KDefaultCacheSize, KDefaultPageSize, TSqlSrvConfigParams::EEncUtf8);
       
   550 	TheDb.Close();
       
   551 	(void)RSqlDatabase::Delete(KTestDbName);
       
   552 	//"soft_heap_limit_kb=900" config file
       
   553 	//"soft_heap_limit_kb=800" client config string
       
   554 	ReplaceConfigFile(_L("soft_heap_limit_kb=900"));
       
   555 	_LIT8(KConfigStr4, "soft_heap_limit_kb=800");
       
   556 	err = TheDb.Create(KTestDbName, &KConfigStr4);
       
   557 	TEST2(err, KErrArgument);
       
   558 	TheDb.Close();
       
   559 	(void)RSqlDatabase::Delete(KTestDbName);
       
   560 	//"free_space_threshold_kb=100" config file
       
   561 	//"free_space_threshold_kb=200" client config string
       
   562 	ReplaceConfigFile(_L("free_space_threshold_kb=100"));
       
   563 	_LIT8(KConfigStr5, "free_space_threshold_kb=200");
       
   564 	err = TheDb.Create(KTestDbName, &KConfigStr5);
       
   565 	TEST2(err, KErrArgument);
       
   566 	TheDb.Close();
       
   567 	(void)RSqlDatabase::Delete(KTestDbName);
       
   568 	(void)TheFs.Delete(KSqlSrvConfigFile);
       
   569 	}
       
   570 
       
   571 /**
       
   572 @SYMTestCaseID			SYSLIB-SQL-UT-3606
       
   573 @SYMTestCaseDesc		Soft Heap Limit - functional test.
       
   574 						The test attempts to create a database with the soft heap limit value specified in the config file
       
   575 						and different combinations of the page size and cache size parameters in both config file and client
       
   576 						config string. The expectation is that when the cache size parameter value is not specified explicitly
       
   577 						in the config file or in the config string, the cache size value will be calculated, using the soft
       
   578 						heap limit and the database page size.
       
   579 @SYMTestPriority		High
       
   580 @SYMTestActions			Soft Heap Limit - functional test.
       
   581 @SYMTestExpectedResults The test must not fail
       
   582 @SYMREQ					REQ8162
       
   583 */
       
   584 void SoftHeapLimitFunctionalTest1()
       
   585 	{
       
   586 	///////////////////// CREATE DATABASE /////////////////////////////////////////////////////////
       
   587 	//"soft_heap_limit_kb=512" config file. (512 is the min soft heap limit value)
       
   588 	//Expected result: the database cache size will be (512 * 1024)/page_size;
       
   589 	ReplaceConfigFile(_L("soft_heap_limit_kb=512"));
       
   590 	TInt err = TheDb.Create(KTestDbName);
       
   591 	TEST2(err, KErrNone);
       
   592 	AssertConfigPrmValues(TheDb, (512 * 1024) / KDefaultPageSize, KDefaultPageSize, KDefaultEncoding);
       
   593 	TheDb.Close();
       
   594 	(void)RSqlDatabase::Delete(KTestDbName);
       
   595 	//"soft_heap_limit_kb=KMaxTInt/1024" config file. (KMaxTInt / 1024 is the max soft heap limit value)
       
   596 	//Expected result: the database cache size will be KMaxTInt/page_size;
       
   597 	TBuf<32> configBuf;
       
   598 	configBuf.Copy(_L("soft_heap_limit_kb="));
       
   599 	TInt maxSoftHeapLimit = KMaxTInt / 1024;
       
   600 	configBuf.AppendNum(maxSoftHeapLimit);
       
   601 	ReplaceConfigFile(configBuf);
       
   602 	err = TheDb.Create(KTestDbName);
       
   603 	TEST2(err, KErrNone);
       
   604 	AssertConfigPrmValues(TheDb, KMaxTInt / KDefaultPageSize, KDefaultPageSize, KDefaultEncoding);
       
   605 	TheDb.Close();
       
   606 	(void)RSqlDatabase::Delete(KTestDbName);
       
   607 	//"soft_heap_limit_kb=512;page_size=2048" config file.
       
   608 	//Expected result: the database cache size will be (512 * 1024)/2048. The page size value from the config file is used.
       
   609 	ReplaceConfigFile(_L("soft_heap_limit_kb=512;page_size=2048"));
       
   610 	err = TheDb.Create(KTestDbName);
       
   611 	TEST2(err, KErrNone);
       
   612 	AssertConfigPrmValues(TheDb, (512 * 1024) / 2048, 2048, KDefaultEncoding);
       
   613 	TheDb.Close();
       
   614 	(void)RSqlDatabase::Delete(KTestDbName);
       
   615 	//"soft_heap_limit_kb=512" config file.
       
   616 	//"page_size=4096" client config string.
       
   617 	//Expected result: the database cache size will be (512 * 1024)/4096. The page size value from the client config string is used.
       
   618 	ReplaceConfigFile(_L("soft_heap_limit_kb=512;"));
       
   619 	_LIT8(KConfigStr1, "page_size=4096");
       
   620 	err = TheDb.Create(KTestDbName, &KConfigStr1);
       
   621 	TEST2(err, KErrNone);
       
   622 	AssertConfigPrmValues(TheDb, (512 * 1024) / 4096, 4096, KDefaultEncoding);
       
   623 	TheDb.Close();
       
   624 	(void)RSqlDatabase::Delete(KTestDbName);
       
   625 	//"soft_heap_limit_kb=512;page_size=8192" config file.
       
   626 	//"page_size=2048" client config string.
       
   627 	//Expected result: the database cache size will be (512 * 1024)/2048. The page size value from the client config string is used.
       
   628 	ReplaceConfigFile(_L("soft_heap_limit_kb=512;page_size=8192"));
       
   629 	_LIT8(KConfigStr2, "page_size=2048");
       
   630 	err = TheDb.Create(KTestDbName, &KConfigStr2);
       
   631 	TEST2(err, KErrNone);
       
   632 	AssertConfigPrmValues(TheDb, (512 * 1024) / 2048, 2048, KDefaultEncoding);
       
   633 	TheDb.Close();
       
   634 	(void)RSqlDatabase::Delete(KTestDbName);
       
   635 	//"soft_heap_limit_kb=512;page_size=2048;encoding=UTF-8" config file.
       
   636 	//"cache_size=100" client config string.
       
   637 	//Expected result: the database cache size will be 100. The soft heap limit is not used for the cache size calculation.
       
   638 	ReplaceConfigFile(_L("soft_heap_limit_kb=512;page_size=2048;encoding=UTF-8"));
       
   639 	_LIT8(KConfigStr3, "cache_size=100");
       
   640 	err = TheDb.Create(KTestDbName, &KConfigStr3);
       
   641 	TEST2(err, KErrNone);
       
   642 	AssertConfigPrmValues(TheDb, 100, 2048, TSqlSrvConfigParams::EEncUtf8);
       
   643 	TheDb.Close();
       
   644 	(void)RSqlDatabase::Delete(KTestDbName);
       
   645 	(void)TheFs.Delete(KSqlSrvConfigFile);
       
   646 	}
       
   647 
       
   648 /**
       
   649 @SYMTestCaseID			SYSLIB-SQL-UT-3607
       
   650 @SYMTestCaseDesc		Soft Heap Limit - functional test.
       
   651 						The test attempts to open a database with the soft heap limit value specified in the config file
       
   652 						and different combinations of the page size and cache size parameters in both config file and client
       
   653 						config string. The expectation is that when the cache size parameter value is not specified explicitly
       
   654 						in the config file or in the config string, the cache size value will be calculated, using the soft
       
   655 						heap limit and the database page size (read from the database, not from the config file or string).
       
   656 @SYMTestPriority		High
       
   657 @SYMTestActions			Soft Heap Limit - functional test.
       
   658 @SYMTestExpectedResults The test must not fail
       
   659 @SYMREQ					REQ8162
       
   660 */
       
   661 void SoftHeapLimitFunctionalTest2()
       
   662 	{
       
   663 	///////////////////// OPEN DATABASE /////////////////////////////////////////////////////////
       
   664 	//"soft_heap_limit_kb=512;page_size=2048" config file.
       
   665 	//Expected result: the database cache size will be (512 * 1024)/2048. The database page size value is used (not the built-time one).
       
   666 	ReplaceConfigFile(_L("soft_heap_limit_kb=512;page_size=2048"));
       
   667 	TInt err = TheDb.Create(KTestDbName);
       
   668 	TEST2(err, KErrNone);
       
   669 	AssertConfigPrmValues(TheDb, (512 * 1024) / 2048, 2048, KDefaultEncoding);
       
   670 	TheDb.Close();
       
   671 	ReplaceConfigFile(_L("soft_heap_limit_kb=1024;page_size=8192"));
       
   672 	err = TheDb.Open(KTestDbName);
       
   673 	TEST2(err, KErrNone);
       
   674 	AssertConfigPrmValues(TheDb, (1024 * 1024) / 2048, 2048, KDefaultEncoding);
       
   675 	TheDb.Close();
       
   676 	(void)RSqlDatabase::Delete(KTestDbName);
       
   677 	//"soft_heap_limit_kb=512" config file.
       
   678 	//"page_size=4096" client config string.
       
   679 	//Expected result: the database cache size will be (512 * 1024)/4096. The database page size value is used (not the built-time one).
       
   680 	ReplaceConfigFile(_L("soft_heap_limit_kb=512"));
       
   681 	_LIT8(KConfigStr1, "page_size=4096");
       
   682 	err = TheDb.Create(KTestDbName, &KConfigStr1);
       
   683 	TEST2(err, KErrNone);
       
   684 	AssertConfigPrmValues(TheDb, (512 * 1024) / 4096, 4096, KDefaultEncoding);
       
   685 	TheDb.Close();
       
   686 	ReplaceConfigFile(_L("soft_heap_limit_kb=1024;page_size=8192"));
       
   687 	err = TheDb.Open(KTestDbName);
       
   688 	TEST2(err, KErrNone);
       
   689 	AssertConfigPrmValues(TheDb, (1024 * 1024) / 4096, 4096, KDefaultEncoding);
       
   690 	TheDb.Close();
       
   691 	(void)RSqlDatabase::Delete(KTestDbName);
       
   692 	//"soft_heap_limit_kb=512" config file.
       
   693 	//"page_size=4096" client config string when openning the database.
       
   694 	//Expected result: the database cache size will be 512. The database page size value is used (the built-time one).
       
   695 	ReplaceConfigFile(_L("soft_heap_limit_kb=512"));
       
   696 	err = TheDb.Create(KTestDbName);
       
   697 	TEST2(err, KErrNone);
       
   698 	AssertConfigPrmValues(TheDb, 512, KDefaultPageSize, KDefaultEncoding);
       
   699 	TheDb.Close();
       
   700 	ReplaceConfigFile(_L("soft_heap_limit_kb=1024;page_size=512"));
       
   701 	_LIT8(KConfigStr2, "page_size=4096");
       
   702 	err = TheDb.Open(KTestDbName, &KConfigStr2);
       
   703 	TEST2(err, KErrNone);
       
   704 	AssertConfigPrmValues(TheDb, 1024, KDefaultPageSize, KDefaultEncoding);
       
   705 	TheDb.Close();
       
   706 	(void)RSqlDatabase::Delete(KTestDbName);
       
   707 	(void)TheFs.Delete(KSqlSrvConfigFile);
       
   708 	}
       
   709 	
       
   710 /**
       
   711 @SYMTestCaseID			SYSLIB-SQL-UT-3608
       
   712 @SYMTestCaseDesc		Soft Heap Limit - file I/O failure simulation test.
       
   713 						The test creates a database with very small soft heap limit value (8Kb).
       
   714 						Then the test attempts to insert a record in an explicit transaction while doing
       
   715 						file I/O failure simulation.
       
   716 @SYMTestPriority		High
       
   717 @SYMTestActions			Soft Heap Limit - file I/O failure simulation test.
       
   718 @SYMTestExpectedResults The test must not fail
       
   719 @SYMREQ					REQ8162
       
   720                         REQ10271
       
   721 */
       
   722 void FileIOFailureTest()
       
   723 	{
       
   724 	(void)RSqlDatabase::Delete(KTestDbName);
       
   725 	ReplaceConfigFile(_L("soft_heap_limit_kb=8;cache_size=16;page_size=512;free_space_threshold_kb=100"));
       
   726 	TInt err = TheDb.Create(KTestDbName);
       
   727 	TEST2(err, KErrNone);
       
   728 	err = TheDb.Exec(_L("CREATE TABLE A(Id INTEGER,Name TEXT)"));
       
   729 	TEST(err >= 0);
       
   730 	TheDb.Close();
       
   731 	err = -1;
       
   732 	const TInt KTestRecCnt = 100;
       
   733 	for(TInt cnt=1;err<KErrNone;++cnt)
       
   734 		{		
       
   735 		TheTest.Printf(_L("%d \r"), cnt);		
       
   736 		err = TheDb.Open(KTestDbName);
       
   737 		TEST2(err, KErrNone);
       
   738 		TInt recCntBegin = 0;
       
   739 		TSqlScalarFullSelectQuery q1(TheDb);
       
   740 		TRAP(err, recCntBegin = q1.SelectIntL(_L("SELECT COUNT (*) FROM A")));
       
   741 		TEST2(err, KErrNone);
       
   742 		(void)TheFs.SetErrorCondition(KErrGeneral, cnt);
       
   743 		err = TheDb.Exec(_L("BEGIN TRANSACTION"));
       
   744 		if(err == KErrNone)
       
   745 			{
       
   746 			for(TInt i=0;i<KTestRecCnt;++i)
       
   747 				{
       
   748 				TBuf<300> sql;
       
   749 				sql.Format(_L("INSERT INTO A(Id,Name) VALUES(%d, 'A1234567890B1234567890C1234567890D1234567890E1234567890F1234567890G1234567890H1234567890I1234567890J1234567890K1234567890L1234567890M1234567890N1234567890O1234567890P1234567890Q1234567890R1234567890')"), cnt);
       
   750 				err = TheDb.Exec(sql);
       
   751 				TEST(err == 1 || err < 0);
       
   752 				if(err < 0)
       
   753 					{
       
   754 					break;
       
   755 					}
       
   756 				}
       
   757 			if(err == 1)
       
   758 				{
       
   759 				err = TheDb.Exec(_L("COMMIT TRANSACTION"));
       
   760 				}
       
   761 			else if(TheDb.InTransaction()) //the transaction may have been rolled back automatically
       
   762 				{
       
   763 				err = TheDb.Exec(_L("ROLLBACK TRANSACTION"));
       
   764 				}
       
   765 			if(err == 0)
       
   766 				{
       
   767 				err = 1;	
       
   768 				}
       
   769 			}
       
   770 		(void)TheFs.SetErrorCondition(KErrNone);
       
   771 		if(err < 1)
       
   772 			{
       
   773 			TheDb.Close();//close the database to recover from the last error
       
   774 			TInt err2 = TheDb.Open(KTestDbName);
       
   775 			TEST2(err2, KErrNone);
       
   776 			}
       
   777 		TSqlScalarFullSelectQuery q2(TheDb);
       
   778 		TInt recCntEnd = 0;
       
   779 		TRAPD(err3, recCntEnd = q2.SelectIntL(_L("SELECT COUNT (*) FROM A")));
       
   780 		TheDb.Close();
       
   781 		TEST2(err3, KErrNone);
       
   782 		//check the database content - all bets are off in a case of an I/O error. 
       
   783 		//The new records may have actually been inserted.
       
   784 		TEST(recCntEnd == recCntBegin || recCntEnd == (recCntBegin + KTestRecCnt));
       
   785 		}
       
   786 	(void)TheFs.SetErrorCondition(KErrNone);
       
   787 	(void)RSqlDatabase::Delete(KTestDbName);
       
   788 	(void)TheFs.Delete(KSqlSrvConfigFile);
       
   789 	}
       
   790 	
       
   791 /**
       
   792 @SYMTestCaseID			SYSLIB-SQL-UT-3609
       
   793 @SYMTestCaseDesc		Soft Heap Limit - OOM test.
       
   794 						The test creates a database with very small soft heap limit value (8Kb).
       
   795 						The the test attempts to insert a record in an explicit transaction while doing
       
   796 						OOM simulation.
       
   797 @SYMTestPriority		High
       
   798 @SYMTestActions			Soft Heap Limit - OOM test.
       
   799 @SYMTestExpectedResults The test must not fail
       
   800 @SYMREQ					REQ8162
       
   801                         REQ10271
       
   802 */
       
   803 void OOMtest()
       
   804 	{
       
   805 	(void)RSqlDatabase::Delete(KTestDbName);
       
   806 	ReplaceConfigFile(_L("soft_heap_limit_kb=8;cache_size=16;page_size=512;free_space_threshold_kb=150"));
       
   807 	TInt err = TheDb.Create(KTestDbName);
       
   808 	TEST2(err, KErrNone);
       
   809 	err = TheDb.Exec(_L("CREATE TABLE A(Id INTEGER,Name TEXT)"));
       
   810 	TEST(err >= 0);
       
   811 	TheDb.Close();
       
   812 
       
   813 	const TInt KOomIterationCount = 1000;//Instead fo doing the OOM test while "err == KErrNoMemory", the test is
       
   814 										 //performed KOomIterationCount times, because the "soft heap limit" will
       
   815 										 //force the SQLite library to reuse some of the already allocated but not used pages.
       
   816 	TInt failingAllocationNo = 0;
       
   817 	while(failingAllocationNo < KOomIterationCount)
       
   818 		{
       
   819 		__UHEAP_MARK;
       
   820 
       
   821 		const TInt KDelayedDbHeapFailureMask = 0x1000;
       
   822 		TSqlResourceTester::SetDbHeapFailure(RHeap::EFailNext | KDelayedDbHeapFailureMask, ++failingAllocationNo);
       
   823 
       
   824 		err = TheDb.Open(KTestDbName);
       
   825 		TEST2(err, KErrNone);
       
   826 
       
   827 		err = TheDb.Exec(_L("BEGIN TRANSACTION"));
       
   828 		if(err == KErrNone)
       
   829 			{
       
   830 			const TInt KTestRecCnt = 4;
       
   831 			for(TInt i=0;i<KTestRecCnt;++i)
       
   832 				{
       
   833 				err = TheDb.Exec(_L("INSERT INTO A(Id,Name) VALUES(1, 'A1234567890B1234567890C1234567890D1234567890E1234567890F1234567890G1234567890H1234567890I1234567890J1234567890K1234567890L1234567890M1234567890N1234567890O1234567890P1234567890Q1234567890R1234567890')"));
       
   834 				if(err < 1)
       
   835 					{
       
   836 					break;
       
   837 					}
       
   838 				}
       
   839 			if(err == 1)
       
   840 				{
       
   841 				err = TheDb.Exec(_L("COMMIT TRANSACTION"));
       
   842 				}
       
   843 			else if(TheDb.InTransaction()) //the transaction may have been rolled back automatically
       
   844 				{
       
   845 				err = TheDb.Exec(_L("ROLLBACK TRANSACTION"));
       
   846 				}
       
   847 			}
       
   848 		
       
   849 		TSqlResourceTester::SetDbHeapFailure(RHeap::ENone, 0);
       
   850 	
       
   851 		TheDb.Close();	
       
   852 
       
   853 		TheTest.Printf(_L("%d/%d   \r"), failingAllocationNo, err);
       
   854 		
       
   855 		__UHEAP_MARKEND;
       
   856 		
       
   857 		TEST(err >= 0 || err == KErrNoMemory);
       
   858 		}
       
   859 	TEST(err >= 0);
       
   860 	(void)RSqlDatabase::Delete(KTestDbName);
       
   861 	(void)TheFs.Delete(KSqlSrvConfigFile);
       
   862 	}
       
   863 
       
   864 /**
       
   865 @SYMTestCaseID			SYSLIB-SQL-UT-4081
       
   866 @SYMTestCaseDesc		Background compaction, free page threshold - functional test.
       
   867 						The test creates a server config file, where the free page threshold is set to be 20 Kb.
       
   868 						Then the test creates a database. The test inserts 40 pages (40 Kb) into the database, closes and 
       
   869 						reopens the database. Then the test deletes some records from the database.
       
   870 						But the space in the free pages is not big enough to kick-off the background compaction.
       
   871 						The test checks that no compaction has occurred after the deletions.
       
   872 						The test deletes more records and the free page threshold is reached.
       
   873 						The test checks that after the last deletion the database really has been compacted.
       
   874 @SYMTestPriority		Medium
       
   875 @SYMTestActions			Background compaction, free page threshold - functional test.
       
   876 @SYMTestExpectedResults Test must not fail
       
   877 @SYMREQ					REQ10271
       
   878 */
       
   879 void FreePageThresholdTest()
       
   880 	{
       
   881 	const TInt KFreePageThresholdSrvCfgKb = 20;
       
   882 	TBuf<50> cfgBuf1;
       
   883 	cfgBuf1.Format(_L("free_space_threshold_kb=%d"), KFreePageThresholdSrvCfgKb);
       
   884 	ReplaceConfigFile(cfgBuf1);
       
   885 	
       
   886 	const TInt KPageSize = 1024;
       
   887 	TBuf8<50> cfgBuf2;
       
   888 	cfgBuf2.Format(_L8("page_size=%d;"), KPageSize);
       
   889 	//Create a database and insert some records. At the end the database size is bigger than the free pages threshold.
       
   890 	(void)RSqlDatabase::Delete(KTestDbName);
       
   891 	TInt err = TheDb.Create(KTestDbName, &cfgBuf2);
       
   892 	TEST2(err, KErrNone);
       
   893 	err = TheDb.Exec(_L("CREATE TABLE A(B BLOB)"));
       
   894 	TEST2(err, 1);
       
   895 	TBuf8<(KPageSize - 150) * 2> blob;
       
   896 	blob.SetLength((KPageSize - 150) * 2);
       
   897 	blob.Fill(TChar('A'));
       
   898 	for(TInt i=0;i<KFreePageThresholdSrvCfgKb*2;++i)
       
   899 		{
       
   900 		TBuf8<KPageSize * 2> sql;
       
   901 		sql.Format(_L8("INSERT INTO A VALUES(x'%S')"), &blob);
       
   902 		err = TheDb.Exec(sql);
       
   903 		TEST2(err, 1);
       
   904 		}
       
   905 	TheDb.Close();
       
   906 	//Reopen the database and delete some records. The free spave is not big enough to kick-off the background compaction.
       
   907 	err = TheDb.Open(KTestDbName);
       
   908 	TEST2(err, KErrNone);
       
   909 	for(TInt i=0;i<10;++i)
       
   910 		{
       
   911 		TBuf8<50> sql;
       
   912 		sql.Format(_L8("DELETE FROM A WHERE ROWID=%d"), i + 1);
       
   913 		err = TheDb.Exec(sql);
       
   914 		TEST2(err, 1);
       
   915 		}
       
   916 	User::After(1000000);
       
   917 	RSqlDatabase::TSize size;
       
   918 	err = TheDb.Size(size);
       
   919 	TEST2(err, KErrNone);
       
   920 	TEST(size.iFree > 0);
       
   921 	//Delete more records, the free page threshold is reached, the background compaction - kicked-off.
       
   922 	for(TInt i=10;i<20;++i)
       
   923 		{
       
   924 		TBuf8<50> sql;
       
   925 		sql.Format(_L8("DELETE FROM A WHERE ROWID=%d"), i + 1);
       
   926 		err = TheDb.Exec(sql);
       
   927 		TEST2(err, 1);
       
   928 		}
       
   929 	User::After(1000000);
       
   930 	err = TheDb.Size(size);
       
   931 	TEST2(err, KErrNone);
       
   932 	TEST2(size.iFree, 0);
       
   933 	//
       
   934 	TheDb.Close();
       
   935 	(void)RSqlDatabase::Delete(KTestDbName);
       
   936 	(void)TheFs.Delete(KSqlSrvConfigFile);
       
   937 	}
       
   938 
       
   939 /**
       
   940 @SYMTestCaseID			SYSLIB-SQL-UT-4075
       
   941 @SYMTestCaseDesc		Server configuration file, large string test.
       
   942 						The test creates a server config file, where all parameters are used
       
   943 						and checks the the parameter values are processed normally.
       
   944 @SYMTestPriority		Medium
       
   945 @SYMTestActions			Server configuration file, large string test.
       
   946 @SYMTestExpectedResults Test must not fail
       
   947 @SYMREQ					REQ10271
       
   948 */
       
   949 void LargeStringTest()
       
   950 	{
       
   951 	ReplaceConfigFile(_L("page_size=32768;cache_size=2048;encoding=UTF-16;soft_heap_limit_kb=2048;free_space_threshold_kb=100000000;compaction=background"));
       
   952 	TInt err = TheDb.Create(KTestDbName);
       
   953 	TEST2(err, KErrNone);
       
   954 	AssertConfigPrmValues(TheDb, (2048 * 1024) / 32768, 32768, TSqlSrvConfigParams::EEncUtf16);
       
   955 	TheDb.Close();
       
   956 	(void)RSqlDatabase::Delete(KTestDbName);
       
   957 	}
       
   958 
       
   959 void DoTests()
       
   960 	{
       
   961 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-3603 Bad config file "));
       
   962 	BadCfgFileTest();
       
   963  	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-3604 Config file - bad parameters "));
       
   964 	BadCfgFileParametersTest();
       
   965  	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-3605 Config file - conflict test "));
       
   966 	CfgFileConflictTest();
       
   967  	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-3606 Soft heap limit - functional test (\"create database\") "));
       
   968  	SoftHeapLimitFunctionalTest1();
       
   969  	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-3607 Soft heap limit - functional test (\"open database\") "));
       
   970  	SoftHeapLimitFunctionalTest2();
       
   971  	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-3608 Soft heap limit - file I/O failure "));
       
   972     FileIOFailureTest();
       
   973  	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-3609 Soft heap limit - OOM failure "));
       
   974 	OOMtest();
       
   975  	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4081 SQL server configuration file + free page threshold - functional test "));
       
   976  	FreePageThresholdTest();
       
   977  	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4075 SQL server configuration file + large string "));
       
   978  	LargeStringTest();
       
   979 	}
       
   980 	
       
   981 #endif	//SYSLIBS_TEST
       
   982 
       
   983 TInt E32Main()
       
   984 	{
       
   985 	TheTest.Title();
       
   986 	
       
   987 	CTrapCleanup* tc = CTrapCleanup::New();
       
   988 	TheTest(tc != NULL);
       
   989 	
       
   990 	__UHEAP_MARK;
       
   991 
       
   992 #ifdef SYSLIBS_TEST	
       
   993 	TheTest.Start(_L("t_sqlconfigfile tests"));
       
   994 
       
   995 	SetupTestEnv();
       
   996 	DoTests();
       
   997  	DestroyTestEnv();
       
   998 	
       
   999 	TheTest.End();
       
  1000 #else
       
  1001  	TheTest.Start(_L("This test works only if the whole SQL component is built with SYSLIBS_TEST macro defined!"));
       
  1002 	TheTest.End();
       
  1003 #endif	
       
  1004 	
       
  1005 	__UHEAP_MARKEND;
       
  1006 	
       
  1007 	TheTest.Close();
       
  1008 	
       
  1009 	delete tc;
       
  1010 	
       
  1011 	User::Heap().Check();
       
  1012 	return KErrNone;
       
  1013 	}