os/persistentdata/persistentstorage/sql/TEST/t_sqlconfig.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#include <e32test.h>
sl@0
    17
#include <bautils.h>
sl@0
    18
#include <sqldb.h>
sl@0
    19
#include "SqlSrvConfig.h"
sl@0
    20
sl@0
    21
///////////////////////////////////////////////////////////////////////////////////////
sl@0
    22
sl@0
    23
RTest TheTest(_L("t_sqlconfig test"));
sl@0
    24
_LIT(KTestDir, "c:\\test\\");
sl@0
    25
_LIT(KTestDbName, "c:\\test\\t_sqlconfig_1.db");
sl@0
    26
_LIT(KTestDbName2, "c:\\test\\t_sqlconfig_2.db");
sl@0
    27
_LIT(KTestDbName3, "c:[1111C1C1]t_sqlconfig_3.db");
sl@0
    28
_LIT(KTestDbName4, "c:\\private\\1111C1C1\\t_sqlconfig_4.db");
sl@0
    29
_LIT(KSqlSrvConfigFile, "c:\\test\\t_sqlserver.cfg");
sl@0
    30
RFs TheFs;
sl@0
    31
RSqlDatabase TheDb;
sl@0
    32
RSqlDatabase TheDb2;
sl@0
    33
sl@0
    34
enum TConfigParamType {EPrmCacheSize, EPrmPageSize, EPrmDbEncoding};
sl@0
    35
sl@0
    36
//Default (build-time) configuration parameter values
sl@0
    37
const TInt KDefaultPageSize = 1024;
sl@0
    38
const TInt KDefaultCacheSize = (TSqlSrvConfigParams::KDefaultSoftHeapLimitKb * 1024) / KDefaultPageSize;
sl@0
    39
const TSqlSrvConfigParams::TDbEncoding KDefaultEncoding = TSqlSrvConfigParams::EEncUtf16;
sl@0
    40
sl@0
    41
///////////////////////////////////////////////////////////////////////////////////////
sl@0
    42
sl@0
    43
void DestroyTestEnv()
sl@0
    44
	{
sl@0
    45
	TheDb2.Close();
sl@0
    46
	TheDb.Close();
sl@0
    47
	(void)RSqlDatabase::Delete(KTestDbName4);
sl@0
    48
	(void)RSqlDatabase::Delete(KTestDbName3);
sl@0
    49
	(void)RSqlDatabase::Delete(KTestDbName2);
sl@0
    50
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
    51
	(void)TheFs.Delete(KSqlSrvConfigFile);
sl@0
    52
	TheFs.Close();
sl@0
    53
	}
sl@0
    54
sl@0
    55
///////////////////////////////////////////////////////////////////////////////////////
sl@0
    56
///////////////////////////////////////////////////////////////////////////////////////
sl@0
    57
//Test macros and functions
sl@0
    58
void Check(TInt aValue, TInt aLine)
sl@0
    59
	{
sl@0
    60
	if(!aValue)
sl@0
    61
		{
sl@0
    62
		DestroyTestEnv();
sl@0
    63
		TheTest(EFalse, aLine);
sl@0
    64
		}
sl@0
    65
	}
sl@0
    66
void Check(TInt aValue, TInt aExpected, TInt aLine)
sl@0
    67
	{
sl@0
    68
	if(aValue != aExpected)
sl@0
    69
		{
sl@0
    70
		DestroyTestEnv();
sl@0
    71
		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
sl@0
    72
		TheTest(EFalse, aLine);
sl@0
    73
		}
sl@0
    74
	}
sl@0
    75
#define TEST(arg) ::Check((arg), __LINE__)
sl@0
    76
#define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
sl@0
    77
sl@0
    78
///////////////////////////////////////////////////////////////////////////////////////
sl@0
    79
sl@0
    80
void SetupTestEnv()
sl@0
    81
    {
sl@0
    82
	TInt err = TheFs.Connect();
sl@0
    83
	TEST2(err, KErrNone);
sl@0
    84
sl@0
    85
	err = TheFs.MkDir(KTestDir);
sl@0
    86
	TEST(err == KErrNone || err == KErrAlreadyExists);
sl@0
    87
sl@0
    88
	err = TheFs.CreatePrivatePath(EDriveC);
sl@0
    89
	TEST(err == KErrNone || err == KErrAlreadyExists);
sl@0
    90
sl@0
    91
	(void)RSqlDatabase::Delete(KTestDbName3);
sl@0
    92
	(void)RSqlDatabase::Delete(KTestDbName2);
sl@0
    93
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
    94
	(void)TheFs.Delete(KSqlSrvConfigFile);//The test does not work if there is a test config file.
sl@0
    95
	}
sl@0
    96
sl@0
    97
TInt DoGetConfigParamValueL(RSqlDatabase& aDb, TConfigParamType aPrmType, const TDesC& aLogicalDbName = KNullDesC)
sl@0
    98
	{
sl@0
    99
	TBuf<100> sql;
sl@0
   100
	sql.Copy(_L("PRAGMA "));
sl@0
   101
	if(aLogicalDbName.Length() > 0)
sl@0
   102
		{
sl@0
   103
		sql.Append(aLogicalDbName);
sl@0
   104
		sql.Append(_L("."));
sl@0
   105
		}
sl@0
   106
	
sl@0
   107
	TSqlScalarFullSelectQuery q(aDb);
sl@0
   108
	TInt res = 0;
sl@0
   109
	switch(aPrmType)
sl@0
   110
		{
sl@0
   111
		case EPrmCacheSize:
sl@0
   112
			sql.Append(_L("cache_size"));
sl@0
   113
			res = q.SelectIntL(sql);
sl@0
   114
			break;
sl@0
   115
		case EPrmPageSize:
sl@0
   116
			sql.Append(_L("page_size"));
sl@0
   117
			res = q.SelectIntL(sql);
sl@0
   118
			break;
sl@0
   119
		case EPrmDbEncoding:
sl@0
   120
			{
sl@0
   121
			sql.Append(_L("encoding"));
sl@0
   122
			TBuf<20> dbEncodingText;
sl@0
   123
			res = q.SelectTextL(sql, dbEncodingText);
sl@0
   124
			TEST2(res, KErrNone);
sl@0
   125
			if(dbEncodingText.FindF(_L("UTF-16")) >= 0)
sl@0
   126
				{
sl@0
   127
				res = TSqlSrvConfigParams::EEncUtf16;	
sl@0
   128
				}
sl@0
   129
			else if(dbEncodingText.FindF(_L("UTF-8")) >= 0)
sl@0
   130
				{
sl@0
   131
				res = TSqlSrvConfigParams::EEncUtf8;	
sl@0
   132
				}
sl@0
   133
			else
sl@0
   134
				{
sl@0
   135
				TEST2(0, 1);
sl@0
   136
				}
sl@0
   137
			}
sl@0
   138
			break;
sl@0
   139
		default:
sl@0
   140
			TEST2(0, 1);
sl@0
   141
			break;
sl@0
   142
		}
sl@0
   143
	return res;
sl@0
   144
	}
sl@0
   145
sl@0
   146
TInt GetConfigParamValue(RSqlDatabase& aDb, TConfigParamType aPrmType, const TDesC& aLogicalDbName = KNullDesC)
sl@0
   147
	{
sl@0
   148
	TInt res = 0;
sl@0
   149
	TRAPD(err, res = DoGetConfigParamValueL(aDb, aPrmType, aLogicalDbName));
sl@0
   150
	TEST2(err, KErrNone);
sl@0
   151
	return res;
sl@0
   152
	}
sl@0
   153
sl@0
   154
void AssertConfigPrmValues(RSqlDatabase& aDb, TInt aExpectedCacheSize, TInt aExpectedPageSize, TInt aExpectedDbEncoding, 
sl@0
   155
		                   const TDesC& aLogicalDbName = KNullDesC)	
sl@0
   156
	{
sl@0
   157
	TInt cacheSize = GetConfigParamValue(aDb, EPrmCacheSize, aLogicalDbName);
sl@0
   158
	TInt pageSize = GetConfigParamValue(aDb, EPrmPageSize ,aLogicalDbName);
sl@0
   159
	TInt dbEncoding = GetConfigParamValue(aDb, EPrmDbEncoding, aLogicalDbName);
sl@0
   160
	TEST2(cacheSize, aExpectedCacheSize);
sl@0
   161
	TEST2(pageSize, aExpectedPageSize);
sl@0
   162
	TEST2(dbEncoding, aExpectedDbEncoding);
sl@0
   163
	}
sl@0
   164
sl@0
   165
///////////////////////////////////////////////////////////////////////////////////////
sl@0
   166
sl@0
   167
/**
sl@0
   168
@SYMTestCaseID			SYSLIB-SQL-UT-3436
sl@0
   169
@SYMTestCaseDesc		Test for DEF105928 "SQL server ignores config string parameters".
sl@0
   170
						The test attempts to create/open a database using different cache size/page size/
sl@0
   171
						database encoding configurations and then checks that the configuration parameters
sl@0
   172
						have been set properly and have the expected values.
sl@0
   173
@SYMTestPriority		High
sl@0
   174
@SYMTestActions			Test for DEF105928 "SQL server ignores config string parameters".
sl@0
   175
@SYMTestExpectedResults The test must not fail
sl@0
   176
@SYMDEF					DEF105928
sl@0
   177
*/
sl@0
   178
void CfgFunctionalTest()
sl@0
   179
	{
sl@0
   180
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   181
	//Create a database. No config string. The default config parameters will be used.
sl@0
   182
	TInt err = TheDb.Create(KTestDbName);
sl@0
   183
	TEST2(err, KErrNone);
sl@0
   184
	AssertConfigPrmValues(TheDb, KDefaultCacheSize, KDefaultPageSize, KDefaultEncoding);
sl@0
   185
	TheDb.Close();
sl@0
   186
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   187
	//Create a database. Cache size set.
sl@0
   188
	_LIT8(KCfgStr1, "cache_size=32");
sl@0
   189
	err = TheDb.Create(KTestDbName, &KCfgStr1);
sl@0
   190
	TEST2(err, KErrNone);
sl@0
   191
	AssertConfigPrmValues(TheDb, 32, KDefaultPageSize, KDefaultEncoding);
sl@0
   192
	TheDb.Close();
sl@0
   193
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   194
	//Create a database. Page size set.
sl@0
   195
	_LIT8(KCfgStr2, "page_size=2048");
sl@0
   196
	err = TheDb.Create(KTestDbName, &KCfgStr2);
sl@0
   197
	TEST2(err, KErrNone);
sl@0
   198
	AssertConfigPrmValues(TheDb, (TSqlSrvConfigParams::KDefaultSoftHeapLimitKb * 1024) / 2048, 2048, KDefaultEncoding);
sl@0
   199
	TheDb.Close();
sl@0
   200
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   201
	//Create a database. Cache & Page size set.
sl@0
   202
	_LIT8(KCfgStr3, "cache_size=256;page_size=4096");
sl@0
   203
	err = TheDb.Create(KTestDbName, &KCfgStr3);
sl@0
   204
	TEST2(err, KErrNone);
sl@0
   205
	AssertConfigPrmValues(TheDb, 256, 4096, KDefaultEncoding);
sl@0
   206
	TheDb.Close();
sl@0
   207
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   208
	//Create a database. Cache size & Page size & db encoding set.
sl@0
   209
	_LIT8(KCfgStr4, "cache_size=512;page_size=512;encoding=UTF-8");
sl@0
   210
	err = TheDb.Create(KTestDbName, &KCfgStr4);
sl@0
   211
	TEST2(err, KErrNone);
sl@0
   212
	AssertConfigPrmValues(TheDb, 512, 512, TSqlSrvConfigParams::EEncUtf8);
sl@0
   213
	TheDb.Close();
sl@0
   214
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   215
	//Create a database. Cache size & Page size & db encoding set.
sl@0
   216
	_LIT8(KCfgStr5, "cache_size=16;page_size=1024;encoding=UTF-16");
sl@0
   217
	err = TheDb.Create(KTestDbName, &KCfgStr5);
sl@0
   218
	TEST2(err, KErrNone);
sl@0
   219
	AssertConfigPrmValues(TheDb, 16, 1024, TSqlSrvConfigParams::EEncUtf16);
sl@0
   220
	TheDb.Close();
sl@0
   221
	//Open the database. Cache size set. The rest of parameter values must be the same as for KCfgStr5.
sl@0
   222
	_LIT8(KCfgStr6, "cache_size=80");
sl@0
   223
	err = TheDb.Open(KTestDbName, &KCfgStr6);
sl@0
   224
	TEST2(err, KErrNone);
sl@0
   225
	AssertConfigPrmValues(TheDb, 80, 1024, TSqlSrvConfigParams::EEncUtf16);
sl@0
   226
	TheDb.Close();
sl@0
   227
	//Open the database. Attempt to set the page size (impossible when opening a database).
sl@0
   228
	_LIT8(KCfgStr7, "page_size=2048");
sl@0
   229
	err = TheDb.Open(KTestDbName, &KCfgStr7);
sl@0
   230
	TEST2(err, KErrNone);
sl@0
   231
	AssertConfigPrmValues(TheDb, KDefaultCacheSize, 1024, TSqlSrvConfigParams::EEncUtf16);
sl@0
   232
	TheDb.Close();
sl@0
   233
	//Open the database. Attempt to set the encoding (impossible when opening a database).
sl@0
   234
	_LIT8(KCfgStr8, "encoding=UTF-8");
sl@0
   235
	err = TheDb.Open(KTestDbName, &KCfgStr8);
sl@0
   236
	TEST2(err, KErrNone);
sl@0
   237
	AssertConfigPrmValues(TheDb, KDefaultCacheSize, 1024, TSqlSrvConfigParams::EEncUtf16);
sl@0
   238
	TheDb.Close();
sl@0
   239
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   240
	//Create a database. Empty config string - 1
sl@0
   241
	_LIT8(KCfgStr9, "");
sl@0
   242
	err = TheDb.Create(KTestDbName, &KCfgStr9);
sl@0
   243
	TEST2(err, KErrNone);
sl@0
   244
	AssertConfigPrmValues(TheDb, KDefaultCacheSize, KDefaultPageSize, KDefaultEncoding);
sl@0
   245
	TheDb.Close();
sl@0
   246
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   247
	//Create a database. Empty config string - 2
sl@0
   248
	_LIT8(KCfgStr10, "       ");
sl@0
   249
	err = TheDb.Create(KTestDbName, &KCfgStr10);
sl@0
   250
	TEST2(err, KErrNone);
sl@0
   251
	AssertConfigPrmValues(TheDb, KDefaultCacheSize, KDefaultPageSize, KDefaultEncoding);
sl@0
   252
	TheDb.Close();
sl@0
   253
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   254
	//Create a database. Empty config string - 3
sl@0
   255
	_LIT8(KCfgStr11, " ; ;   ;;   ");
sl@0
   256
	err = TheDb.Create(KTestDbName, &KCfgStr11);
sl@0
   257
	TEST2(err, KErrNone);
sl@0
   258
	AssertConfigPrmValues(TheDb, KDefaultCacheSize, KDefaultPageSize, KDefaultEncoding);
sl@0
   259
	TheDb.Close();
sl@0
   260
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   261
	}
sl@0
   262
sl@0
   263
/**
sl@0
   264
@SYMTestCaseID			SYSLIB-SQL-UT-3437
sl@0
   265
@SYMTestCaseDesc		Test for DEF105928 "SQL server ignores config string parameters".
sl@0
   266
						The test attempts to create/open a database using invalid configuration strings,
sl@0
   267
						too long configuration string, string with missing parameter values, string with
sl@0
   268
						invalid database encodings. The test expects the SQL server will report a failure
sl@0
   269
						and will refuse to execute the operation.
sl@0
   270
@SYMTestPriority		High
sl@0
   271
@SYMTestActions			Test for DEF105928 "SQL server ignores config string parameters".
sl@0
   272
@SYMTestExpectedResults The test must not fail
sl@0
   273
@SYMDEF					DEF105928
sl@0
   274
*/
sl@0
   275
void CfgNegativeTest()
sl@0
   276
	{
sl@0
   277
	//Create a database. Spelling problem in the parameter name. This is not reported as an error by the SQL server
sl@0
   278
	//(treated as unknown parameter)
sl@0
   279
	_LIT8(KCfgStr1, "casche_size = 32");
sl@0
   280
	TInt err = TheDb.Create(KTestDbName, &KCfgStr1);
sl@0
   281
	TEST2(err, KErrNone);
sl@0
   282
	TheDb.Close();
sl@0
   283
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   284
	//Create a database. Spelling problem in the parameter value. 
sl@0
   285
	_LIT8(KCfgStr2, "encoding = UTF-32");
sl@0
   286
	err = TheDb.Create(KTestDbName, &KCfgStr2);
sl@0
   287
	TEST2(err, KErrNone);//Invalid encoding: KErrNone, but the encoding will not be used
sl@0
   288
	AssertConfigPrmValues(TheDb, KDefaultCacheSize, KDefaultPageSize, KDefaultEncoding);
sl@0
   289
	TheDb.Close();
sl@0
   290
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   291
	//Create a database. Invalid config string.
sl@0
   292
	_LIT8(KCfgStr3, "dfgjkdfgkdfk; sfkgjhdfgjkdfk; dfgdfetrwer");
sl@0
   293
	err = TheDb.Create(KTestDbName, &KCfgStr3);
sl@0
   294
	TEST2(err, KErrArgument);
sl@0
   295
	TheDb.Close();
sl@0
   296
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   297
	//Create a database. Too long and invalid config string.
sl@0
   298
	_LIT8(KCfgStr4, "A123456789B123456789C123456789D123456789E123456789F123456789G123456789H123456789I123456789J123456789K123456789L123456789M123456789N123456789O123456789");
sl@0
   299
	err = TheDb.Create(KTestDbName, &KCfgStr4);
sl@0
   300
	TEST2(err, KErrArgument);
sl@0
   301
	TheDb.Close();
sl@0
   302
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   303
	//Create a database. No parameter value - 1.
sl@0
   304
	_LIT8(KCfgStr5, "page_size=");
sl@0
   305
	err = TheDb.Create(KTestDbName, &KCfgStr5);
sl@0
   306
	TEST2(err, KErrArgument);
sl@0
   307
	TheDb.Close();
sl@0
   308
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   309
	//Create a database. No parameter value - 2.
sl@0
   310
	_LIT8(KCfgStr6, "page_size=;");
sl@0
   311
	err = TheDb.Create(KTestDbName, &KCfgStr6);
sl@0
   312
	TEST2(err, KErrArgument);
sl@0
   313
	TheDb.Close();
sl@0
   314
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   315
	//Create a database. Non-number parameter value - 1.
sl@0
   316
	_LIT8(KCfgStr7, "page_size=aaa;");
sl@0
   317
	err = TheDb.Create(KTestDbName, &KCfgStr7);
sl@0
   318
	TEST2(err, KErrArgument);
sl@0
   319
	TheDb.Close();
sl@0
   320
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   321
	//Create a database. Non-number parameter value - 2.
sl@0
   322
	_LIT8(KCfgStr8, "cache_size=weyu34;");
sl@0
   323
	err = TheDb.Create(KTestDbName, &KCfgStr8);
sl@0
   324
	TEST2(err, KErrArgument);
sl@0
   325
	TheDb.Close();
sl@0
   326
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   327
	//Create a database. "soft_heap_limit_kb" in the config string.
sl@0
   328
	_LIT8(KCfgStr9, "soft_heap_limit_kb=512;");
sl@0
   329
	err = TheDb.Create(KTestDbName, &KCfgStr9);
sl@0
   330
	TEST2(err, KErrArgument);
sl@0
   331
	TheDb.Close();
sl@0
   332
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   333
	//Create a database. "free_space_threshold_kb" in the config string.
sl@0
   334
	_LIT8(KCfgStr10, "free_space_threshold_kb=256;");
sl@0
   335
	err = TheDb.Create(KTestDbName, &KCfgStr10);
sl@0
   336
	TEST2(err, KErrArgument);
sl@0
   337
	TheDb.Close();
sl@0
   338
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   339
	}
sl@0
   340
sl@0
   341
/**
sl@0
   342
@SYMTestCaseID			SYSLIB-SQL-UT-3438
sl@0
   343
@SYMTestCaseDesc		Test for DEF105928 "SQL server ignores config string parameters".
sl@0
   344
						The test attempts to create a database with configuration string containing:
sl@0
   345
						negative or zero page size and cache size, too small or too big page size,
sl@0
   346
						page size, not power of two. The test expects the SQL server will detect those 
sl@0
   347
						invalid page size and cache size values and will refuse to execute the operation.
sl@0
   348
@SYMTestPriority		High
sl@0
   349
@SYMTestActions			Test for DEF105928 "SQL server ignores config string parameters".
sl@0
   350
@SYMTestExpectedResults The test must not fail
sl@0
   351
@SYMDEF					DEF105928
sl@0
   352
*/
sl@0
   353
void CfgInvalidCacheAndPageSizeTest()
sl@0
   354
	{
sl@0
   355
	//Create a database. Page size < 512.
sl@0
   356
	_LIT8(KCfgStr1, "page_size=128;");
sl@0
   357
	TInt err = TheDb.Create(KTestDbName, &KCfgStr1);
sl@0
   358
	TEST2(err, KErrNone);//Invalid page size: KErrNone expected, but the page size will not be set
sl@0
   359
	AssertConfigPrmValues(TheDb, KDefaultCacheSize, KDefaultPageSize, KDefaultEncoding);
sl@0
   360
	TheDb.Close();
sl@0
   361
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   362
	//Create a database. Page size > 32768.
sl@0
   363
	_LIT8(KCfgStr2, "page_size=65535;");
sl@0
   364
	err = TheDb.Create(KTestDbName, &KCfgStr2);
sl@0
   365
	TEST2(err, KErrNone);//Invalid page size: KErrNone expected, but the page size will not be set
sl@0
   366
	AssertConfigPrmValues(TheDb, KDefaultCacheSize, KDefaultPageSize, KDefaultEncoding);
sl@0
   367
	TheDb.Close();
sl@0
   368
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   369
	//Create a database. Page size is not power of two.
sl@0
   370
	_LIT8(KCfgStr3, "page_size=5000;");
sl@0
   371
	err = TheDb.Create(KTestDbName, &KCfgStr3);
sl@0
   372
	TEST2(err, KErrNone);//Invalid page size: KErrNone expected, but the page size will not be set
sl@0
   373
	AssertConfigPrmValues(TheDb, KDefaultCacheSize, KDefaultPageSize, KDefaultEncoding);
sl@0
   374
	TheDb.Close();
sl@0
   375
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   376
	//Create a database. Zero cache size.
sl@0
   377
	_LIT8(KCfgStr4, "cache_size=0");
sl@0
   378
	err = TheDb.Create(KTestDbName, &KCfgStr4);
sl@0
   379
	TEST2(err, KErrNone);
sl@0
   380
	AssertConfigPrmValues(TheDb, 0, KDefaultPageSize, KDefaultEncoding);
sl@0
   381
	TheDb.Close();
sl@0
   382
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   383
	//Create a database. Negative cache size.
sl@0
   384
	_LIT8(KCfgStr5, "cache_size=-32");
sl@0
   385
	err = TheDb.Create(KTestDbName, &KCfgStr5);
sl@0
   386
	TEST2(err, KErrArgument);
sl@0
   387
	TheDb.Close();
sl@0
   388
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   389
	//Create a database. Zero page size.
sl@0
   390
	_LIT8(KCfgStr6, "page_size=0");
sl@0
   391
	err = TheDb.Create(KTestDbName, &KCfgStr6);
sl@0
   392
	TEST2(err, KErrNone);//Invalid page size: KErrNone expected, but the page size will not be set
sl@0
   393
	AssertConfigPrmValues(TheDb, KDefaultCacheSize, KDefaultPageSize, KDefaultEncoding);
sl@0
   394
	TheDb.Close();
sl@0
   395
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   396
	//Create a database. Negative page size.
sl@0
   397
	_LIT8(KCfgStr7, "page_size=-1024");
sl@0
   398
	err = TheDb.Create(KTestDbName, &KCfgStr7);
sl@0
   399
	TEST2(err, KErrArgument);
sl@0
   400
	TheDb.Close();
sl@0
   401
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   402
	}
sl@0
   403
sl@0
   404
/**
sl@0
   405
@SYMTestCaseID			SYSLIB-SQL-UT-3439
sl@0
   406
@SYMTestCaseDesc		Test for DEF105928 "SQL server ignores config string parameters".
sl@0
   407
						The test creates two databases using configuration strings and checks that database 1
sl@0
   408
						configuration has no impact on database 2 configuration.
sl@0
   409
@SYMTestPriority		High
sl@0
   410
@SYMTestActions			Test for DEF105928 "SQL server ignores config string parameters".
sl@0
   411
@SYMTestExpectedResults The test must not fail
sl@0
   412
@SYMDEF					DEF105928
sl@0
   413
*/
sl@0
   414
void CfgCrossConnectionTest()
sl@0
   415
	{
sl@0
   416
	//Create database 1 with cache size = 40, page size = 2048.
sl@0
   417
	_LIT8(KCfgStr1, "cache_size = 40; page_size = 2048");
sl@0
   418
	TInt err = TheDb.Create(KTestDbName, &KCfgStr1);
sl@0
   419
	TEST2(err, KErrNone);
sl@0
   420
	AssertConfigPrmValues(TheDb, 40, 2048, KDefaultEncoding);
sl@0
   421
	//Without closing database 1, create database 2 without a configuration string.
sl@0
   422
	//Check that database 2 uses the default configuration parameters
sl@0
   423
	err = TheDb2.Create(KTestDbName2);
sl@0
   424
	TEST2(err, KErrNone);
sl@0
   425
	AssertConfigPrmValues(TheDb2, KDefaultCacheSize, KDefaultPageSize, KDefaultEncoding);
sl@0
   426
	//Close database 2. Check the configuration parameters of database 1
sl@0
   427
	TheDb2.Close();
sl@0
   428
	AssertConfigPrmValues(TheDb, 40, 2048, KDefaultEncoding);
sl@0
   429
	TheDb.Close();
sl@0
   430
	(void)RSqlDatabase::Delete(KTestDbName2);
sl@0
   431
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   432
	//Create 2 databases with different configuration parameters
sl@0
   433
	_LIT8(KCfgStr2_1, "cache_size = 50; page_size = 512; encoding = \"UTF-16\"");
sl@0
   434
	_LIT8(KCfgStr2_2, "cache_size = 80; page_size = 4096; encoding = \"UTF-8\"");
sl@0
   435
	err = TheDb.Create(KTestDbName, &KCfgStr2_1);
sl@0
   436
	TEST2(err, KErrNone);
sl@0
   437
	err = TheDb2.Create(KTestDbName2, &KCfgStr2_2);
sl@0
   438
	TEST2(err, KErrNone);
sl@0
   439
	AssertConfigPrmValues(TheDb, 50, 512, TSqlSrvConfigParams::EEncUtf16);
sl@0
   440
	AssertConfigPrmValues(TheDb2, 80, 4096, TSqlSrvConfigParams::EEncUtf8);
sl@0
   441
	TheDb2.Close();
sl@0
   442
	TheDb.Close();
sl@0
   443
	//Reopen the databases and check the configuration parameters
sl@0
   444
	err = TheDb.Open(KTestDbName);
sl@0
   445
	TEST2(err, KErrNone);
sl@0
   446
	err = TheDb2.Open(KTestDbName2);
sl@0
   447
	TEST2(err, KErrNone);
sl@0
   448
	AssertConfigPrmValues(TheDb, (TSqlSrvConfigParams::KDefaultSoftHeapLimitKb * 1024) / 512, 512, TSqlSrvConfigParams::EEncUtf16);
sl@0
   449
	AssertConfigPrmValues(TheDb2, (TSqlSrvConfigParams::KDefaultSoftHeapLimitKb * 1024) / 4096, 4096, TSqlSrvConfigParams::EEncUtf8);
sl@0
   450
	TheDb2.Close();
sl@0
   451
	TheDb.Close();
sl@0
   452
	(void)RSqlDatabase::Delete(KTestDbName2);
sl@0
   453
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   454
	}
sl@0
   455
sl@0
   456
/**
sl@0
   457
@SYMTestCaseID			SYSLIB-SQL-UT-3440
sl@0
   458
@SYMTestCaseDesc		Test for DEF105928 "SQL server ignores config string parameters".
sl@0
   459
						The test creates a secure database with a configuration string. The test must not fail.
sl@0
   460
@SYMTestPriority		High
sl@0
   461
@SYMTestActions			Test for DEF105928 "SQL server ignores config string parameters".
sl@0
   462
@SYMTestExpectedResults The test must not fail
sl@0
   463
@SYMDEF					DEF105928
sl@0
   464
*/
sl@0
   465
void CfgSecureDbTest()
sl@0
   466
	{
sl@0
   467
	//Create
sl@0
   468
	TSecurityPolicy policy(TSecurityPolicy::EAlwaysPass);
sl@0
   469
	RSqlSecurityPolicy dbPolicy;
sl@0
   470
	TInt err = dbPolicy.Create(policy);
sl@0
   471
	TEST2(err, KErrNone);
sl@0
   472
	_LIT8(KCfgStr1, "cache_size = 80; page_size = 4096; encoding = UTF-8");
sl@0
   473
	err = TheDb.Create(KTestDbName3, dbPolicy, &KCfgStr1);
sl@0
   474
	TEST2(err, KErrNone);
sl@0
   475
	//Since it is a secure database, PRAGMAs cannot be executed (in order to assert the parameter values)
sl@0
   476
	//AssertConfigPrmValues(TheDb, 80, 4096, TSqlSrvConfig::EEncUtf8);
sl@0
   477
	TheDb.Close();	
sl@0
   478
	dbPolicy.Close();
sl@0
   479
	//Open
sl@0
   480
	_LIT8(KCfgStr2, "cache_size = 100");
sl@0
   481
	err = TheDb.Open(KTestDbName3, &KCfgStr2);
sl@0
   482
	TEST2(err, KErrNone);
sl@0
   483
	TheDb.Close();	
sl@0
   484
	(void)RSqlDatabase::Delete(KTestDbName3);
sl@0
   485
	}
sl@0
   486
sl@0
   487
/**
sl@0
   488
@SYMTestCaseID			SYSLIB-SQL-UT-3441
sl@0
   489
@SYMTestCaseDesc		Test for DEF105928 "SQL server ignores config string parameters".
sl@0
   490
						The test creates a private database with a configuration string. The test must not fail.
sl@0
   491
@SYMTestPriority		High
sl@0
   492
@SYMTestActions			Test for DEF105928 "SQL server ignores config string parameters".
sl@0
   493
@SYMTestExpectedResults The test must not fail
sl@0
   494
@SYMDEF					DEF105928
sl@0
   495
*/
sl@0
   496
void CfgPrivateDbTest()
sl@0
   497
	{
sl@0
   498
	//Create
sl@0
   499
	_LIT8(KCfgStr1, "cache_size = 80; page_size = 4096; encoding = UTF-8");
sl@0
   500
	TInt err = TheDb.Create(KTestDbName4, &KCfgStr1);
sl@0
   501
	TEST2(err, KErrNone);
sl@0
   502
	AssertConfigPrmValues(TheDb, 80, 4096, TSqlSrvConfigParams::EEncUtf8);
sl@0
   503
	TheDb.Close();	
sl@0
   504
	//Open-1. The cache size can be changed.
sl@0
   505
	_LIT8(KCfgStr2, "cache_size = 100");
sl@0
   506
	err = TheDb.Open(KTestDbName4, &KCfgStr2);
sl@0
   507
	TEST2(err, KErrNone);
sl@0
   508
	AssertConfigPrmValues(TheDb, 100, 4096, TSqlSrvConfigParams::EEncUtf8);
sl@0
   509
	TheDb.Close();	
sl@0
   510
	//Open-2. The page size cannot be changed if the database does exist already.
sl@0
   511
	_LIT8(KCfgStr3, "page_size = 512");
sl@0
   512
	err = TheDb.Open(KTestDbName4, &KCfgStr3);
sl@0
   513
	TEST2(err, KErrNone);
sl@0
   514
	AssertConfigPrmValues(TheDb, (TSqlSrvConfigParams::KDefaultSoftHeapLimitKb * 1024) / 4096, 4096, TSqlSrvConfigParams::EEncUtf8);
sl@0
   515
	TheDb.Close();	
sl@0
   516
	(void)RSqlDatabase::Delete(KTestDbName4);
sl@0
   517
	}
sl@0
   518
sl@0
   519
/**
sl@0
   520
@SYMTestCaseID          SYSLIB-SQL-CT-3480
sl@0
   521
@SYMTestCaseDesc		Test for INC106788 - Cannot set SQLite page_size bigger than 4k (4096).
sl@0
   522
						The test attempts to create a database with page size 8K, 16K or 32K, which was not
sl@0
   523
						possible before (the default page size (1K) has been used).
sl@0
   524
@SYMTestPriority        High
sl@0
   525
@SYMTestActions			Test for INC106788 - Cannot set SQLite page_size bigger than 4k (4096).
sl@0
   526
@SYMTestExpectedResults The test should not fail or panic.
sl@0
   527
@SYMDEF INC106788
sl@0
   528
*/
sl@0
   529
void INC106788()
sl@0
   530
	{
sl@0
   531
	//Create a database with page size = 8192.
sl@0
   532
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   533
	_LIT8(KCfgStr1, "page_size = 8192");
sl@0
   534
	TInt err = TheDb.Create(KTestDbName, &KCfgStr1);
sl@0
   535
	TEST2(err, KErrNone);
sl@0
   536
	AssertConfigPrmValues(TheDb, (TSqlSrvConfigParams::KDefaultSoftHeapLimitKb * 1024) / 8192, 8192, KDefaultEncoding);
sl@0
   537
	TheDb.Close();
sl@0
   538
sl@0
   539
	//Create a database with page size = 16384.
sl@0
   540
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   541
	_LIT8(KCfgStr2, "page_size = 16384");
sl@0
   542
	err = TheDb.Create(KTestDbName, &KCfgStr2);
sl@0
   543
	TEST2(err, KErrNone);
sl@0
   544
	AssertConfigPrmValues(TheDb, (TSqlSrvConfigParams::KDefaultSoftHeapLimitKb * 1024) / 16384, 16384, KDefaultEncoding);
sl@0
   545
	TheDb.Close();
sl@0
   546
	
sl@0
   547
	//Create a database with page size = 32768.
sl@0
   548
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   549
	_LIT8(KCfgStr3, "page_size = 32768");
sl@0
   550
	err = TheDb.Create(KTestDbName, &KCfgStr3);
sl@0
   551
	TEST2(err, KErrNone);
sl@0
   552
	AssertConfigPrmValues(TheDb, (TSqlSrvConfigParams::KDefaultSoftHeapLimitKb * 1024) / 32768, 32768, KDefaultEncoding);
sl@0
   553
	TheDb.Close();
sl@0
   554
	
sl@0
   555
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   556
	}
sl@0
   557
sl@0
   558
/**
sl@0
   559
@SYMTestCaseID			SYSLIB-SQL-UT-4049
sl@0
   560
@SYMTestCaseDesc		Database configuration string - injection test.
sl@0
   561
						The test attempts to create a database using a configuration string
sl@0
   562
						and passing a DELETE SQL statement as a compaction mode name.
sl@0
   563
						The database should be created successfully, the invalid compaction mode - ignored,
sl@0
   564
						the database system tables content shoud not be corrupted by the call.
sl@0
   565
@SYMTestPriority		High
sl@0
   566
@SYMTestActions			Database configuration string - injection test.
sl@0
   567
@SYMTestExpectedResults Test must not fail
sl@0
   568
@SYMREQ					REQ10405
sl@0
   569
*/
sl@0
   570
void InjectionTest()
sl@0
   571
	{
sl@0
   572
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   573
	TInt err = TheDb.Create(KTestDbName);
sl@0
   574
	TEST2(err, KErrNone);
sl@0
   575
	err = TheDb.Exec(_L("CREATE TABLE A(I INTEGER); INSERT INTO A(I) VALUES(1);"));
sl@0
   576
	TEST(err > 0);
sl@0
   577
	TheDb.Close();
sl@0
   578
	
sl@0
   579
	_LIT8(KConfig1, "cache_size=128;DELETE FROM A");
sl@0
   580
	err = TheDb.Open(KTestDbName, &KConfig1);
sl@0
   581
	TEST2(err, KErrArgument);
sl@0
   582
	err = TheDb.Open(KTestDbName);
sl@0
   583
	TEST2(err, KErrNone);
sl@0
   584
	TSqlScalarFullSelectQuery query(TheDb);
sl@0
   585
	TInt recCount = 0;
sl@0
   586
	TRAP(err, recCount = query.SelectIntL(_L("SELECT COUNT(*) FROM A")));
sl@0
   587
	TEST2(err, KErrNone);
sl@0
   588
	TEST2(recCount, 1);
sl@0
   589
	TheDb.Close();
sl@0
   590
sl@0
   591
	_LIT8(KConfig2, "cache_size=256;compaction=DELETE FROM A;");
sl@0
   592
	err = TheDb.Open(KTestDbName, &KConfig2);
sl@0
   593
	TEST2(err, KErrNone);
sl@0
   594
	query.SetDatabase(TheDb);
sl@0
   595
	recCount = 0;
sl@0
   596
	TRAP(err, recCount = query.SelectIntL(_L("SELECT COUNT(*) FROM A")));
sl@0
   597
	TEST2(err, KErrNone);
sl@0
   598
	TEST2(recCount, 1);
sl@0
   599
	TheDb.Close();
sl@0
   600
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   601
sl@0
   602
	_LIT8(KConfig3, "cache_size=256;compaction=DELETE FROM symbian_settings;");
sl@0
   603
	err = TheDb.Create(KTestDbName, &KConfig3);
sl@0
   604
	TEST2(err, KErrNone);
sl@0
   605
	query.SetDatabase(TheDb);
sl@0
   606
	recCount = 0;
sl@0
   607
	TRAP(err, recCount = query.SelectIntL(_L("SELECT COUNT(*) FROM symbian_settings")));
sl@0
   608
	TEST2(err, KErrNone);
sl@0
   609
	TEST2(recCount, 1);
sl@0
   610
	TheDb.Close();
sl@0
   611
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   612
	}
sl@0
   613
	
sl@0
   614
/**
sl@0
   615
@SYMTestCaseID          PDS-SQL-UT-4152
sl@0
   616
@SYMTestCaseDesc		Test for DEF142305 - RSqlDatabase::Attach does not use cache_size configuration parameters.
sl@0
   617
						The test verifies that when a database is attached, the attached database cache size will be set
sl@0
   618
						based on the page size, soft heap limit and default cache size.
sl@0
   619
@SYMTestPriority        High
sl@0
   620
@SYMTestActions			Test for DEF142305 - RSqlDatabase::Attach does not use cache_size configuration parameters.
sl@0
   621
@SYMTestExpectedResults The test must not fail.
sl@0
   622
@SYMDEF INC106788
sl@0
   623
*/
sl@0
   624
void DEF142305()
sl@0
   625
	{
sl@0
   626
	_LIT8(KConfig, "cache_size=500");
sl@0
   627
	
sl@0
   628
	//Create main db
sl@0
   629
	TInt err = TheDb.Create(KTestDbName, &KConfig);
sl@0
   630
	TEST2(err, KErrNone);
sl@0
   631
	AssertConfigPrmValues(TheDb, 500, KDefaultPageSize, KDefaultEncoding);
sl@0
   632
	TheDb.Close();
sl@0
   633
	
sl@0
   634
	//Create non-secure db that will be attached 
sl@0
   635
	err = TheDb.Create(KTestDbName2, &KConfig);
sl@0
   636
	TEST2(err, KErrNone);
sl@0
   637
	AssertConfigPrmValues(TheDb, 500, KDefaultPageSize, KDefaultEncoding);
sl@0
   638
	TheDb.Close();
sl@0
   639
sl@0
   640
	//Create private db that will be attached 
sl@0
   641
	_LIT8(KConfig2, "page_size=2048");
sl@0
   642
	err = TheDb.Create(KTestDbName4, &KConfig2);
sl@0
   643
	TEST2(err, KErrNone);
sl@0
   644
	AssertConfigPrmValues(TheDb, (TSqlSrvConfigParams::KDefaultSoftHeapLimitKb * 1024) / 2048, 2048, KDefaultEncoding);//2048? - see KConfig2 string
sl@0
   645
	TheDb.Close();
sl@0
   646
sl@0
   647
	_LIT(KAttachDbName, "Attached");
sl@0
   648
	
sl@0
   649
	//Attach non-secure db
sl@0
   650
	err = TheDb.Open(KTestDbName, &KConfig);
sl@0
   651
	TEST2(err, KErrNone);
sl@0
   652
	AssertConfigPrmValues(TheDb, 500, KDefaultPageSize, KDefaultEncoding);
sl@0
   653
	err = TheDb.Attach(KTestDbName2, KAttachDbName);
sl@0
   654
	TEST2(err, KErrNone);
sl@0
   655
	AssertConfigPrmValues(TheDb, KDefaultCacheSize, KDefaultPageSize, KDefaultEncoding, KAttachDbName);
sl@0
   656
	err = TheDb.Detach(KAttachDbName);
sl@0
   657
	TEST2(err, KErrNone);
sl@0
   658
	TheDb.Close();
sl@0
   659
	
sl@0
   660
	//Attach private db
sl@0
   661
	err = TheDb.Open(KTestDbName, &KConfig);
sl@0
   662
	TEST2(err, KErrNone);
sl@0
   663
	AssertConfigPrmValues(TheDb, 500, KDefaultPageSize, KDefaultEncoding);
sl@0
   664
	err = TheDb.Attach(KTestDbName4, KAttachDbName);
sl@0
   665
	TEST2(err, KErrNone);
sl@0
   666
	AssertConfigPrmValues(TheDb, (TSqlSrvConfigParams::KDefaultSoftHeapLimitKb * 1024) / 2048, 2048, KDefaultEncoding, KAttachDbName);//2048? - see KConfig2 string
sl@0
   667
	err = TheDb.Detach(KAttachDbName);
sl@0
   668
	TEST2(err, KErrNone);
sl@0
   669
	TheDb.Close();
sl@0
   670
	
sl@0
   671
	(void)RSqlDatabase::Delete(KTestDbName4);
sl@0
   672
	(void)RSqlDatabase::Delete(KTestDbName2);
sl@0
   673
	(void)RSqlDatabase::Delete(KTestDbName);
sl@0
   674
	}
sl@0
   675
sl@0
   676
void DoTests()
sl@0
   677
	{
sl@0
   678
	TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-3436 Sql config string - functional test "));
sl@0
   679
	CfgFunctionalTest();
sl@0
   680
	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-3437 Sql config string - negative test "));
sl@0
   681
	CfgNegativeTest();
sl@0
   682
	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-3438 Sql config string - invalid cache size and page size test "));
sl@0
   683
	CfgInvalidCacheAndPageSizeTest();
sl@0
   684
	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-3439 Sql config string - cross connection test "));
sl@0
   685
	CfgCrossConnectionTest();
sl@0
   686
	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-3440 Sql config string - secure database test "));
sl@0
   687
	CfgSecureDbTest();
sl@0
   688
	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-3441 Sql config string - private database test "));
sl@0
   689
	CfgPrivateDbTest();
sl@0
   690
	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-3480 INC106788 - Cannot set SQLite page_size bigger than 4k (4096) "));
sl@0
   691
	INC106788();
sl@0
   692
	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4049 Config string - injection tests"));
sl@0
   693
	InjectionTest();
sl@0
   694
	TheTest.Next(_L("@SYMTestCaseID:PDS-SQL-UT-4152: DEF142305 RSqlDatabase::Attach does not use cache_size configuration parameters"));
sl@0
   695
	DEF142305();
sl@0
   696
	}
sl@0
   697
sl@0
   698
TInt E32Main()
sl@0
   699
	{
sl@0
   700
	TheTest.Title();
sl@0
   701
	
sl@0
   702
	CTrapCleanup* tc = CTrapCleanup::New();
sl@0
   703
	TheTest(tc != NULL);
sl@0
   704
	
sl@0
   705
	__UHEAP_MARK;
sl@0
   706
	
sl@0
   707
	SetupTestEnv();
sl@0
   708
	DoTests();
sl@0
   709
	DestroyTestEnv();
sl@0
   710
	
sl@0
   711
	__UHEAP_MARKEND;
sl@0
   712
	
sl@0
   713
	TheTest.End();
sl@0
   714
	TheTest.Close();
sl@0
   715
	
sl@0
   716
	delete tc;
sl@0
   717
	
sl@0
   718
	User::Heap().Check();
sl@0
   719
	return KErrNone;
sl@0
   720
	}