persistentstorage/sql/TEST/t_sqldbconfigfile.cpp
branchRCL_3
changeset 12 31a8f755b7fe
parent 11 667e88a979d7
child 16 6b6fd149daa2
equal deleted inserted replaced
11:667e88a979d7 12:31a8f755b7fe
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     1 // Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
    43 
    43 
    44 RFs TheFs;
    44 RFs TheFs;
    45 RSqlDatabase TheDb;
    45 RSqlDatabase TheDb;
    46 sqlite3 *TheDbHandle = NULL;
    46 sqlite3 *TheDbHandle = NULL;
    47 
    47 
    48 _LIT(KTestDir, "c:\\test\\");
       
    49 _LIT(KSqlSrvName, "sqlsrv.exe");
       
    50 
       
    51 enum TConfigParamType {EPrmCacheSize, EPrmPageSize, EPrmDbEncoding};
       
    52 
       
    53 TInt KillProcess(const TDesC& aProcessName);
       
    54 
       
    55 _LIT(KCfgDb1, "c:[1111C1C1]a.db"); // shared, secure db
    48 _LIT(KCfgDb1, "c:[1111C1C1]a.db"); // shared, secure db
    56 _LIT(KCfgDb2, "c:[1111C1C1]b.db"); // shared, secure db (no config file for it)
    49 _LIT(KCfgDb2, "c:[1111C1C1]b.db"); // shared, secure db (no config file for it)
    57 _LIT(KCfgDb3, "c:\\private\\1111C1C1\\c.db"); // private, secure db
    50 _LIT(KCfgDb3, "c:\\private\\1111C1C1\\c.db"); // private, secure db
    58 _LIT(KCfgDb4, "c:\\test\\d.db"); // public db
    51 _LIT(KCfgDb4, "c:\\test\\d.db"); // public db
    59 _LIT(KCfgDb5, "c:[1111C1C1]e.db"); // shared, secure db (config file created before db is created)
    52 _LIT(KCfgDb5, "c:[1111C1C1]e.db"); // shared, secure db (config file created before db is created)
    62 _LIT(KCfgDb3ConfigFileV01Path, "c:\\private\\10281e17\\cfgc.db.01"); // config file v01 for c.db (unsupported)
    55 _LIT(KCfgDb3ConfigFileV01Path, "c:\\private\\10281e17\\cfgc.db.01"); // config file v01 for c.db (unsupported)
    63 _LIT(KCfgDb4ConfigFileV01Path, "c:\\private\\10281e17\\cfgd.db.01"); // config file v01 for d.db (unsupported)
    56 _LIT(KCfgDb4ConfigFileV01Path, "c:\\private\\10281e17\\cfgd.db.01"); // config file v01 for d.db (unsupported)
    64 _LIT(KCfgDb5ConfigFileV01Path, "c:\\private\\10281e17\\cfg[1111C1C1]e.db.01"); // config file v01 for e.db
    57 _LIT(KCfgDb5ConfigFileV01Path, "c:\\private\\10281e17\\cfg[1111C1C1]e.db.01"); // config file v01 for e.db
    65 _LIT(KCfgDb1CorruptConfigFilePath, "c:\\private\\10281e17\\cfg[1111C1C1]a.db.invalidextension"); // invalid config file name syntax
    58 _LIT(KCfgDb1CorruptConfigFilePath, "c:\\private\\10281e17\\cfg[1111C1C1]a.db.invalidextension"); // invalid config file name syntax
    66 
    59 
    67 // config file v01 contents for db1 (also tests that upper and lower case statements are both supported)
       
    68 _LIT8(KCfgDb1ConfigFile01Stmts, "CREATE INDEX idx ON table1(i1);CREATE INDEX idx2 ON table1(i2);INSERT INTO table1 (i1,i2,i3) values(5,8,9);DELETE FROM table1;create index multiidx ON TABLE1(i2,i3)");
       
    69 // config file v01 contents for db3 (will not be processed as db3 is not a shared, secure db)
       
    70 _LIT8(KCfgDb3ConfigFile01Stmts, "CREATE INDEX idx ON table3(i1)");
       
    71 // config file v01 contents for db4 (will not be processed as db4 is not a shared, secure db)
       
    72 _LIT8(KCfgDb4ConfigFile01Stmts, "CREATE INDEX idx ON table4(i1)");
       
    73 // config file v01 contents for db5 (will eventually be processed after db5 itself is created)
       
    74 _LIT8(KCfgDb5ConfigFile01Stmts, "CREATE INDEX idx ON table5(i1);\r\n");
       
    75 // config file valid contents (used for v02 and others)
    60 // config file valid contents (used for v02 and others)
    76 _LIT8(KCfgConfigFileValidStmt, "CREATE INDEX newIdx ON table1(i3)  ;"); 
    61 _LIT8(KCfgConfigFileValidStmt, "CREATE INDEX newIdx ON table1(i3)  ;"); 
    77 // config file v03 empty contents
    62 // config file v03 empty contents
    78 _LIT8(KCfgDb1ConfigFileV03EmptyStmt, ""); 
    63 _LIT8(KCfgDb1ConfigFileV03EmptyStmt, ""); 
    79 // config file v04 unsupported contents
    64 // config file v04 unsupported contents
   102 //KLongDbName1 is "long" enough to allow "-journal" to be added at the end.
    87 //KLongDbName1 is "long" enough to allow "-journal" to be added at the end.
   103 _LIT(KLongDbName1,  "c:[1111C1C1]a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a1234.db");
    88 _LIT(KLongDbName1,  "c:[1111C1C1]a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a1234.db");
   104 _LIT(KLongCfgName1, "c:\\private\\10281e17\\cfg[1111C1C1]a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a1234.db.01");
    89 _LIT(KLongCfgName1, "c:\\private\\10281e17\\cfg[1111C1C1]a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a123456789a1234.db.01");
   105 
    90 
   106 _LIT(KSqlSrvPrivatePath, "\\private\\10281e17\\");
    91 _LIT(KSqlSrvPrivatePath, "\\private\\10281e17\\");
   107 _LIT(KGetSettingsSql, "SELECT * FROM symbian_settings WHERE Reserved==%d");
       
   108 _LIT(KResetCollationDllSql, "UPDATE symbian_settings SET CollationDllName='hjagafsff'");
    92 _LIT(KResetCollationDllSql, "UPDATE symbian_settings SET CollationDllName='hjagafsff'");
   109 _LIT(KGetCollationDllSql, "SELECT CollationDllName FROM symbian_settings");
       
   110 
    93 
   111 _LIT(KAttachDb1, "Db1");
    94 _LIT(KAttachDb1, "Db1");
   112 _LIT(KAttachDb2, "Db2");
    95 _LIT(KAttachDb2, "Db2");
   113 _LIT(KAttachDb5, "Db5");
    96 _LIT(KAttachDb5, "Db5");
   114 
    97 
   115 _LIT(KDb1CheckNumRecords, "SELECT * FROM table1");
       
   116 _LIT(KDb2CheckNumRecords, "SELECT * FROM table2");
       
   117 _LIT(KDb3CheckNumRecords, "SELECT * FROM table3");
       
   118 _LIT(KDb4CheckNumRecords, "SELECT * FROM table4");
       
   119 _LIT(KDb5CheckNumRecords, "SELECT * FROM table5");
       
   120 _LIT(KDbCheckNumIndices, "SELECT name FROM sqlite_master WHERE type = 'index'");
       
   121 
       
   122 ///////////////////////////////////////////////////////////////////////////////////////
    98 ///////////////////////////////////////////////////////////////////////////////////////
   123 // Destroy functions
    99 // Destroy functions
   124 
   100 
   125 TInt KillProcess(const TDesC& aProcessName)
   101 void KillSqlServer()
   126 	{
   102 	{
       
   103 	_LIT(KSqlSrvName, "sqlsrv.exe");
   127 	TFullName name;
   104 	TFullName name;
   128 	//RDebug::Print(_L("Find and kill \"%S\" process.\n"), &aProcessName);
   105 	//RDebug::Print(_L("Find and kill \"%S\" process.\n"), &aProcessName);
   129 	TBuf<64> pattern(aProcessName);
   106 	TBuf<64> pattern(KSqlSrvName);
   130 	TInt length = pattern.Length();
   107 	TInt length = pattern.Length();
   131 	pattern += _L("*");
   108 	pattern += _L("*");
   132 	TFindProcess procFinder(pattern);
   109 	TFindProcess procFinder(pattern);
   133 
   110 
   134 	while (procFinder.Next(name) == KErrNone)
   111 	while (procFinder.Next(name) == KErrNone)
   152 			proc.Kill(0);
   129 			proc.Kill(0);
   153 			//RDebug::Print(_L("\"%S\" process killed.\n"), &name);
   130 			//RDebug::Print(_L("\"%S\" process killed.\n"), &name);
   154 			}
   131 			}
   155 		proc.Close();
   132 		proc.Close();
   156 		}
   133 		}
   157 	return KErrNone;
       
   158 	}
   134 	}
   159 
   135 
   160 void DeleteCfgFilesAndDbs()
   136 void DeleteCfgFilesAndDbs()
   161 	{
   137 	{
   162  	(void)RSqlDatabase::Delete(KLongDbName1);
   138  	(void)RSqlDatabase::Delete(KLongDbName1);
   184 		{
   160 		{
   185 		sqlite3_close(TheDbHandle);	
   161 		sqlite3_close(TheDbHandle);	
   186 		TheDbHandle = NULL;
   162 		TheDbHandle = NULL;
   187 		}
   163 		}
   188 	TheDb.Close();
   164 	TheDb.Close();
   189 	(void)KillProcess(KSqlSrvName);
   165 	KillSqlServer();
   190 	DeleteCfgFilesAndDbs();
   166 	DeleteCfgFilesAndDbs();
   191 	TheFs.Close();
   167 	TheFs.Close();
   192 	}
   168 	}
   193 	
   169 	
   194 ///////////////////////////////////////////////////////////////////////////////////////
   170 ///////////////////////////////////////////////////////////////////////////////////////
   230 	TInt endHandleCount1;
   206 	TInt endHandleCount1;
   231 	TInt endHandleCount2;
   207 	TInt endHandleCount2;
   232 
   208 
   233 	RThread().HandleCount(endHandleCount1, endHandleCount2);
   209 	RThread().HandleCount(endHandleCount1, endHandleCount2);
   234 
   210 
   235 	TEST(TheHandleCount1 == endHandleCount1);
   211 	TEST2(TheHandleCount1, endHandleCount1);
   236 	TEST(TheHandleCount2 == endHandleCount2);
   212 	TEST2(TheHandleCount2, endHandleCount2);
   237 	}
   213 	}
   238 
   214 
   239 void MarkAllocatedCells()
   215 void MarkAllocatedCells()
   240 	{
   216 	{
   241 	TheAllocatedCellsCount = User::CountAllocCells();
   217 	TheAllocatedCellsCount = User::CountAllocCells();
   242 	}
   218 	}
   243 
   219 
   244 void CheckAllocatedCells()
   220 void CheckAllocatedCells()
   245 	{
   221 	{
   246 	TInt allocatedCellsCount = User::CountAllocCells();
   222 	TInt allocatedCellsCount = User::CountAllocCells();
   247 	TEST(allocatedCellsCount == TheAllocatedCellsCount);
   223 	TEST2(allocatedCellsCount, TheAllocatedCellsCount);
   248 	}
   224 	}
   249 
   225 
   250 ///////////////////////////////////////////////////////////////////////////////////////
   226 ///////////////////////////////////////////////////////////////////////////////////////
   251 // Set up functions
   227 // Set up functions
   252 
   228 
   253 TInt DoCreateSecurityPolicy(RSqlSecurityPolicy& securityPolicy)
   229 RSqlSecurityPolicy CreateSecurityPolicy()
   254 	{
   230 	{
   255 	const TSecurityPolicy KDefaultPolicy(TSecurityPolicy::EAlwaysPass);
   231 	const TSecurityPolicy KDefaultPolicy(TSecurityPolicy::EAlwaysPass);
   256 	if((KErrNone != securityPolicy.Create(KDefaultPolicy))
   232 	RSqlSecurityPolicy policy;
   257 	   ||
   233 	TInt err = policy.Create(KDefaultPolicy);
   258 	   (KErrNone != securityPolicy.SetDbPolicy(RSqlSecurityPolicy::ESchemaPolicy, KDefaultPolicy))
   234 	TEST2(err, KErrNone);
   259 	   ||
   235 	err = policy.SetDbPolicy(RSqlSecurityPolicy::ESchemaPolicy, KDefaultPolicy);
   260 	   (KErrNone != securityPolicy.SetDbPolicy(RSqlSecurityPolicy::EWritePolicy, KDefaultPolicy))
   236     TEST2(err, KErrNone);
   261 	   ||
   237     err = policy.SetDbPolicy(RSqlSecurityPolicy::EWritePolicy, KDefaultPolicy);
   262 	   (KErrNone != securityPolicy.SetDbPolicy(RSqlSecurityPolicy::EReadPolicy, KDefaultPolicy)))
   238     TEST2(err, KErrNone);
   263 		{
   239     err = policy.SetDbPolicy(RSqlSecurityPolicy::EReadPolicy, KDefaultPolicy);
   264 		return KErrGeneral;
   240     TEST2(err, KErrNone);
   265 		}
   241     return policy;
   266 		
   242 	}
   267 	return KErrNone;
   243 	
   268 	}
   244 void DoCreateCfgFile(const TDesC& aFileName, const TDesC8& aData)
   269 	
   245     {
       
   246     RFile file;
       
   247     TInt err = file.Create(TheFs, aFileName, EFileRead | EFileWrite);
       
   248     TEST2(err, KErrNone);
       
   249     err = file.Write(aData); 
       
   250     file.Close();   
       
   251     TEST2(err, KErrNone);
       
   252     }
       
   253 
       
   254 // Create v01 of the test config files
   270 void CreateCfgFiles()
   255 void CreateCfgFiles()
   271 	{
   256 	{
   272  	// Create v01 of the test config files
       
   273  	
       
   274  	RFile file;
       
   275 	TFileName fileName;
   257 	TFileName fileName;
   276 	TInt v1 = 1;
   258 	TInt v1 = 1;
   277 	fileName.Format(KCfgDb1ConfigFilePath(), v1);
   259 	fileName.Format(KCfgDb1ConfigFilePath, v1);
   278 	TInt err = file.Create(TheFs, fileName, EFileRead | EFileWrite);
   260 
   279 	TEST2(err, KErrNone);
   261 	// config file v01 contents for db1 (also tests that upper and lower case statements are both supported)
   280 	TPtrC8 pDb1((const TUint8*)KCfgDb1ConfigFile01Stmts().Ptr(), KCfgDb1ConfigFile01Stmts().Length());
   262 	_LIT8(KCfgDb1ConfigFile01Stmts, "CREATE INDEX idx ON table1(i1);CREATE INDEX idx2 ON table1(i2);INSERT INTO table1 (i1,i2,i3) values(5,8,9);DELETE FROM table1;create index multiidx ON TABLE1(i2,i3)");
   281 	err = file.Write(pDb1);	
   263     DoCreateCfgFile(fileName, KCfgDb1ConfigFile01Stmts);
   282 	file.Close();	
   264     
   283 	TEST2(err, KErrNone);
   265     // config file v01 contents for db3 (will not be processed as db3 is not a shared, secure db)
   284 	
   266     _LIT8(KCfgDb3ConfigFile01Stmts, "CREATE INDEX idx ON table3(i1)");
   285 	fileName.Copy(KCfgDb3ConfigFileV01Path);
   267     DoCreateCfgFile(KCfgDb3ConfigFileV01Path, KCfgDb3ConfigFile01Stmts);
   286 	err = file.Create(TheFs, fileName, EFileRead | EFileWrite);
   268     
   287 	TEST2(err, KErrNone);
   269     // config file v01 contents for db4 (will not be processed as db4 is not a shared, secure db)
   288 	TPtrC8 pDb3((const TUint8*)KCfgDb3ConfigFile01Stmts().Ptr(), KCfgDb3ConfigFile01Stmts().Length());
   270     _LIT8(KCfgDb4ConfigFile01Stmts, "CREATE INDEX idx ON table4(i1)");
   289 	err = file.Write(pDb3);	
   271     DoCreateCfgFile(KCfgDb4ConfigFileV01Path, KCfgDb4ConfigFile01Stmts);
   290 	file.Close();	
   272     
   291 	TEST2(err, KErrNone);	
   273     // create the config file for Db5 before the database has been created
   292 	
   274     // config file v01 contents for db5 (will eventually be processed after db5 itself is created)
   293 	fileName.Copy(KCfgDb4ConfigFileV01Path);
   275     _LIT8(KCfgDb5ConfigFile01Stmts, "CREATE INDEX idx ON table5(i1);\r\n");
   294 	err = file.Create(TheFs, fileName, EFileRead | EFileWrite);
   276     DoCreateCfgFile(KCfgDb5ConfigFileV01Path, KCfgDb5ConfigFile01Stmts);
   295 	TEST2(err, KErrNone);
   277 	}
   296 	TPtrC8 pDb4((const TUint8*)KCfgDb4ConfigFile01Stmts().Ptr(), KCfgDb4ConfigFile01Stmts().Length());
   278 
   297 	err = file.Write(pDb4);	
   279 void DoCreateCfgDb(const TDesC& aFileName, const TDesC& aCreateTblSql, const TDesC& aInsertSql, 
   298 	file.Close();	
   280                    RSqlSecurityPolicy* aPolicy = NULL)
   299 	TEST2(err, KErrNone);
   281     {
   300 	
   282     TInt err = aPolicy ? TheDb.Create(aFileName, *aPolicy) : TheDb.Create(aFileName);
   301 	fileName.Copy(KCfgDb5ConfigFileV01Path); // create the config file for Db5 before the database has been created
   283     TEST2(err, KErrNone);
   302 	err = file.Create(TheFs, fileName, EFileRead | EFileWrite);
   284     err = TheDb.Exec(aCreateTblSql);
   303 	TEST2(err, KErrNone);
   285     TEST(err >= 0);
   304 	TPtrC8 pDb5((const TUint8*)KCfgDb5ConfigFile01Stmts().Ptr(), KCfgDb5ConfigFile01Stmts().Length());
   286     err = TheDb.Exec(aInsertSql);
   305 	err = file.Write(pDb5);	
   287     TEST(err == 1);
   306 	file.Close();	
   288     TheDb.Close();
   307 	TEST2(err, KErrNone);	
   289     }
   308 	}
   290 
   309 
   291 // Create the test databases
   310 void CreateCfgDbs()
   292 void CreateCfgDbs()
   311 	{
   293 	{
   312 	// Create the test databases
   294     RSqlSecurityPolicy securityPolicy = CreateSecurityPolicy();
   313 	
   295     DoCreateCfgDb(KCfgDb1, _L("CREATE TABLE table1(i1 INTEGER, i2 INTEGER, i3 INTEGER)"),
   314  	TBuf<100> sql;
   296                            _L("INSERT INTO table1 (i1,i2,i3) values(1,2,3)"), &securityPolicy);
   315  		
   297     DoCreateCfgDb(KCfgDb2, _L("CREATE TABLE table2(i1 INTEGER, i2 INTEGER, i3 INTEGER)"),
   316  	RSqlSecurityPolicy securityPolicy;
   298                            _L("INSERT INTO table2 (i1,i2,i3) values(4,5,6)"), &securityPolicy);
   317  	TInt err = DoCreateSecurityPolicy(securityPolicy);
   299     securityPolicy.Close();
   318  	TEST2(err, KErrNone);
   300     DoCreateCfgDb(KCfgDb3, _L("CREATE TABLE table3(i1 INTEGER, i2 INTEGER)"),
   319  	
   301                            _L("INSERT INTO table3 (i1,i2) values(7,8)"));
   320  	err = TheDb.Create(KCfgDb1, securityPolicy);
   302     DoCreateCfgDb(KCfgDb4, _L("CREATE TABLE table4(i1 INTEGER, i2 INTEGER, i3 INTEGER)"),
   321  	TEST2(err, KErrNone);
   303                            _L("INSERT INTO table4 (i1,i2,i3) values(9,10,11)"));
   322  	sql.Copy(_L("CREATE TABLE table1(i1 INTEGER, i2 INTEGER, i3 INTEGER)"));
       
   323  	err = TheDb.Exec(sql);
       
   324  	TEST(err >= 0);
       
   325  	sql.Copy(_L("INSERT INTO table1 (i1,i2,i3) values(1,2,3)"));
       
   326  	err = TheDb.Exec(sql);
       
   327  	TEST(err == 1);
       
   328  	TheDb.Close();
       
   329  	
       
   330  	err = TheDb.Create(KCfgDb2, securityPolicy);
       
   331  	TEST2(err, KErrNone);
       
   332  	sql.Copy(_L("CREATE TABLE table2(i1 INTEGER, i2 INTEGER, i3 INTEGER)"));
       
   333  	err = TheDb.Exec(sql);
       
   334  	TEST(err >= 0);
       
   335  	sql.Copy(_L("INSERT INTO table2 (i1,i2,i3) values(4,5,6)"));
       
   336  	err = TheDb.Exec(sql);
       
   337  	TEST(err == 1);
       
   338  	TheDb.Close();
       
   339  	
       
   340  	securityPolicy.Close();
       
   341  
       
   342  	err = TheDb.Create(KCfgDb3);
       
   343  	TEST2(err, KErrNone);
       
   344  	sql.Copy(_L("CREATE TABLE table3(i1 INTEGER, i2 INTEGER)"));
       
   345  	err = TheDb.Exec(sql);
       
   346  	TEST(err >= 0);
       
   347  	sql.Copy(_L("INSERT INTO table3 (i1,i2) values(7,8)"));
       
   348  	err = TheDb.Exec(sql);
       
   349  	TEST(err == 1);
       
   350  	TheDb.Close();	
       
   351  
       
   352  	err = TheDb.Create(KCfgDb4);
       
   353  	TEST2(err, KErrNone);
       
   354  	sql.Copy(_L("CREATE TABLE table4(i1 INTEGER, i2 INTEGER, i3 INTEGER)"));
       
   355  	err = TheDb.Exec(sql);
       
   356  	TEST(err >= 0);
       
   357  	sql.Copy(_L("INSERT INTO table4 (i1,i2,i3) values(9,10,11)"));
       
   358  	err = TheDb.Exec(sql);
       
   359  	TEST(err == 1);
       
   360  	TheDb.Close();
       
   361  	}
   304  	}
   362 
   305 
   363 void CreateCfgFilesAndDbs()
   306 void CreateCfgFilesAndDbs()
   364 	{
   307 	{
   365 	CreateCfgFiles();
   308 	CreateCfgFiles();
   366 	CreateCfgDbs();
   309 	CreateCfgDbs();
   367 	
   310 	
   368 	// Must now kill the SQL Server so that the config files 
   311 	// Must now kill the SQL Server so that the config files 
   369 	// created above are found and processed when it restarts
   312 	// created above are found and processed when it restarts
   370 	(void)KillProcess(KSqlSrvName);
   313 	KillSqlServer();
   371  	}
   314  	}
   372  	
   315  	
       
   316 // Create the Db5 test database (a config file for it already exists)
   373  void CreateDb5()
   317  void CreateDb5()
   374 	{
   318 	{
   375 	// Create the Db5 test database (a config file for it already exists)
   319  	RSqlSecurityPolicy securityPolicy = CreateSecurityPolicy();
   376 	
   320     DoCreateCfgDb(KCfgDb5, _L("CREATE TABLE table5(i1 INTEGER, i2 INTEGER, i3 INTEGER)"),
   377  	TBuf<100> sql;
   321                            _L("INSERT INTO table5 (i1,i2,i3) values(1,2,3)"), &securityPolicy);
   378  		
       
   379  	RSqlSecurityPolicy securityPolicy;
       
   380  	TInt err = DoCreateSecurityPolicy(securityPolicy);
       
   381  	TEST2(err, KErrNone);
       
   382  	
       
   383  	err = TheDb.Create(KCfgDb5, securityPolicy);
       
   384  	TEST2(err, KErrNone);
       
   385  	sql.Copy(_L("CREATE TABLE table5(i1 INTEGER, i2 INTEGER, i3 INTEGER)"));
       
   386  	err = TheDb.Exec(sql);
       
   387  	TEST(err >= 0);
       
   388  	sql.Copy(_L("INSERT INTO table5 (i1,i2,i3) values(1,2,3)"));
       
   389  	err = TheDb.Exec(sql);
       
   390  	TEST(err == 1);
       
   391  	
       
   392  	TheDb.Close();
       
   393  	securityPolicy.Close();
   322  	securityPolicy.Close();
   394 	}
   323 	}
   395 
   324 
   396 void SetupTestEnv()
   325 void SetupTestEnv()
   397     {
   326     {
   398 	TInt err = TheFs.Connect();
   327 	TInt err = TheFs.Connect();
   399 	TEST2(err, KErrNone);
   328 	TEST2(err, KErrNone);
   400 
   329 
       
   330 	_LIT(KTestDir, "c:\\test\\");
   401 	err = TheFs.MkDir(KTestDir);
   331 	err = TheFs.MkDir(KTestDir);
   402 	TEST(err == KErrNone || err == KErrAlreadyExists);
   332 	TEST(err == KErrNone || err == KErrAlreadyExists);
   403 
   333 
   404 	err = TheFs.CreatePrivatePath(EDriveC);
   334 	err = TheFs.CreatePrivatePath(EDriveC);
   405 	TEST(err == KErrNone || err == KErrAlreadyExists);
   335 	TEST(err == KErrNone || err == KErrAlreadyExists);
   429 	}
   359 	}
   430 
   360 
   431 ///////////////////////////////////////////////////////////////////////////////////////
   361 ///////////////////////////////////////////////////////////////////////////////////////
   432 // Config file replacement functions
   362 // Config file replacement functions
   433 
   363 
   434 void UpgradeDbConfigFile(const TInt aCurrentVersion)
   364 void UpgradeDbConfigFile(TInt aCurrentVersion)
   435 	{
   365 	{
   436 	(void)KillProcess(KSqlSrvName);
   366     TInt newVersion = aCurrentVersion + 1;
   437 	TFileName oldFile;
   367     TEST(newVersion != 0 && newVersion != 1 && newVersion != 10 && newVersion != 11 && newVersion < 15);
   438 	oldFile.Format(KCfgDb1ConfigFilePath(), aCurrentVersion);
   368 	
   439 	TInt err = TheFs.Delete(oldFile);
   369     KillSqlServer();
   440 	TEST2(err, KErrNone);
   370 		
   441 	RFile file;
   371 	TFileName fname;
   442 	TFileName newFile;
   372 	fname.Format(KCfgDb1ConfigFilePath, aCurrentVersion);
   443 	TInt newVersion = aCurrentVersion+1;
   373 	TInt err = TheFs.Delete(fname);
   444 	newFile.Format(KCfgDb1ConfigFilePath(), newVersion);
   374 	TEST2(err, KErrNone);
   445 	err = file.Create(TheFs, newFile, EFileRead | EFileWrite);
   375 	
   446 	TEST2(err, KErrNone);
   376 	if(newVersion == 12)
   447 	
   377 	    {
   448 	switch(newVersion)
   378         // also delete version 10 of file
   449 		{
   379 	    fname.Format(KCfgDb1ConfigFilePath, 10);
   450 			case 2:
   380         err = TheFs.Delete(fname);
   451 				{
   381         TEST2(err, KErrNone);   
   452 				TPtrC8 p((const TUint8*)KCfgConfigFileValidStmt().Ptr(), KCfgConfigFileValidStmt().Length());
   382 	    }
   453 				err = file.Write(p);
   383 
   454 				break;
   384 	const TPtrC8 stmts[] = {KNullDesC8(), KNullDesC8(), 
   455 				}
   385 	        KCfgConfigFileValidStmt(), 
   456 			case 3:
   386 	        KCfgDb1ConfigFileV03EmptyStmt(),
   457 				{
   387 	        KCfgDb1ConfigFileV04UnsupportedStmt(),
   458 				TPtrC8 p((const TUint8*)KCfgDb1ConfigFileV03EmptyStmt().Ptr(), KCfgDb1ConfigFileV03EmptyStmt().Length());
   388 	        KCfgDb1ConfigFileV05OnlyWhitespaceStmt(),
   459 				err = file.Write(p);
   389 	        KCfgDb1ConfigFileV06InvalidSchemaStmt(),
   460 				break;
   390 	        KCfgDb1ConfigFileV07InvalidCommentedStmt(),
   461 				}
   391 	        KCfgDb1ConfigFileV08SeqStmt(),
   462 			case 4:
   392 	        KCfgDb1ConfigFileV09WhitespacePreAndPostStmt(),
   463 				{
   393 	        KNullDesC8(), KNullDesC8(),
   464 				TPtrC8 p((const TUint8*)KCfgDb1ConfigFileV04UnsupportedStmt().Ptr(), KCfgDb1ConfigFileV04UnsupportedStmt().Length());
   394 	        KCfgDb1ConfigFileV12InvalidPlusValidStmt(),
   465 				err = file.Write(p);
   395 	        KCfgDb1ConfigFileV13SQLCommentStmt(),
   466 				break;	
   396 	        KCfgDb1ConfigFileV14CCommentStmt()};
   467 				}
   397 
   468 			case 5:
   398     fname.Format(KCfgDb1ConfigFilePath, newVersion);
   469 				{
   399 	DoCreateCfgFile(fname, stmts[newVersion]);
   470 				TPtrC8 p((const TUint8*)KCfgDb1ConfigFileV05OnlyWhitespaceStmt().Ptr(), KCfgDb1ConfigFileV05OnlyWhitespaceStmt().Length());
       
   471 				err = file.Write(p);
       
   472 				break;
       
   473 				}	
       
   474 			case 6:
       
   475 				{
       
   476 				TPtrC8 p((const TUint8*)KCfgDb1ConfigFileV06InvalidSchemaStmt().Ptr(), KCfgDb1ConfigFileV06InvalidSchemaStmt().Length());
       
   477 				err = file.Write(p);
       
   478 				break;
       
   479 				}
       
   480 			case 7:
       
   481 				{
       
   482 				TPtrC8 p((const TUint8*)KCfgDb1ConfigFileV07InvalidCommentedStmt().Ptr(), KCfgDb1ConfigFileV07InvalidCommentedStmt().Length());
       
   483 				err = file.Write(p);	
       
   484 				break;
       
   485 				}
       
   486 			case 8:
       
   487 				{
       
   488 				TPtrC8 p((const TUint8*)KCfgDb1ConfigFileV08SeqStmt().Ptr(), KCfgDb1ConfigFileV08SeqStmt().Length());
       
   489 				err = file.Write(p);	
       
   490 				break;
       
   491 				}
       
   492 			case 9:
       
   493 				{
       
   494 				TPtrC8 p((const TUint8*)KCfgDb1ConfigFileV09WhitespacePreAndPostStmt().Ptr(), KCfgDb1ConfigFileV09WhitespacePreAndPostStmt().Length());
       
   495 				err = file.Write(p);	
       
   496 				break;
       
   497 				}
       
   498 			case 12:
       
   499 				{
       
   500 				// also delete version 10 of file
       
   501 				oldFile.Format(KCfgDb1ConfigFilePath(), 10);
       
   502 				err = TheFs.Delete(oldFile);
       
   503 				TEST2(err, KErrNone);	
       
   504 				
       
   505 				TPtrC8 p((const TUint8*)KCfgDb1ConfigFileV12InvalidPlusValidStmt().Ptr(), KCfgDb1ConfigFileV12InvalidPlusValidStmt().Length());
       
   506 				err = file.Write(p);
       
   507 				break;
       
   508 				}
       
   509 			case 13:
       
   510 				{
       
   511 				TPtrC8 p((const TUint8*)KCfgDb1ConfigFileV13SQLCommentStmt().Ptr(), KCfgDb1ConfigFileV13SQLCommentStmt().Length());
       
   512 				err = file.Write(p);	
       
   513 				break;
       
   514 				}
       
   515 			case 14:
       
   516 				{
       
   517 				TPtrC8 p((const TUint8*)KCfgDb1ConfigFileV14CCommentStmt().Ptr(), KCfgDb1ConfigFileV14CCommentStmt().Length());
       
   518 				err = file.Write(p);	
       
   519 				break;
       
   520 				}
       
   521 			default:
       
   522 				{
       
   523 				err = KErrArgument;
       
   524 				break;
       
   525 				}		
       
   526 		}
       
   527 	file.Close();
       
   528 	TEST2(err, KErrNone);
       
   529 	}	
   400 	}	
   530 	
   401 	
   531 void DowngradeDbConfigFile(const TInt aCurrentVersion)
   402 void DowngradeDbConfigFile(TInt aCurrentVersion)
   532 	{
   403 	{
   533 	(void)KillProcess(KSqlSrvName);
   404 	TEST(aCurrentVersion > 1);
   534 	TFileName oldFile;
   405 	
   535 	oldFile.Format(KCfgDb1ConfigFilePath(), aCurrentVersion);
   406 	KillSqlServer();
   536 	TInt err = TheFs.Delete(oldFile);
   407 	
   537 	TEST2(err, KErrNone);
   408 	TFileName fname;
   538 	RFile file;
   409 	fname.Format(KCfgDb1ConfigFilePath, aCurrentVersion);
   539 	TFileName newFile;
   410 	TInt err = TheFs.Delete(fname);
   540 	TInt previousVersion = aCurrentVersion-1;
   411 	TEST2(err, KErrNone);
   541 	TEST(previousVersion > 0);
   412 	
   542 	newFile.Format(KCfgDb1ConfigFilePath(), previousVersion);
   413     fname.Format(KCfgDb1ConfigFilePath, aCurrentVersion - 1);
   543 	err = file.Create(TheFs, newFile, EFileRead | EFileWrite);
   414     DoCreateCfgFile(fname, KCfgConfigFileValidStmt);
   544 	TEST2(err, KErrNone);
   415 	}
   545 	TPtrC8 p((const TUint8*)KCfgConfigFileValidStmt().Ptr(), KCfgConfigFileValidStmt().Length());
   416 	
   546 	err = file.Write(p);
   417 void CreateCorruptDbConfigFile(TInt aCurrentVersion)
   547 	file.Close();
   418 	{
   548 	TEST2(err, KErrNone);
   419 	KillSqlServer();
   549 	}
   420 	
   550 	
   421 	TFileName fname;
   551 void CreateCorruptDbConfigFile(const TInt aCurrentVersion)
   422 	fname.Format(KCfgDb1ConfigFilePath, aCurrentVersion);
   552 	{
   423 	TInt err = TheFs.Delete(fname);
   553 	(void)KillProcess(KSqlSrvName);
   424 	TEST2(err, KErrNone);
   554 	TFileName oldFile;
   425 
   555 	oldFile.Format(KCfgDb1ConfigFilePath(), aCurrentVersion);
   426     DoCreateCfgFile(KCfgDb1CorruptConfigFilePath, KCfgConfigFileValidStmt);
   556 	TInt err = TheFs.Delete(oldFile);
       
   557 	TEST2(err, KErrNone);
       
   558 	RFile file;
       
   559 	err = file.Create(TheFs, KCfgDb1CorruptConfigFilePath(), EFileRead | EFileWrite);
       
   560 	TEST2(err, KErrNone);
       
   561 	TPtrC8 p((const TUint8*)KCfgConfigFileValidStmt().Ptr(), KCfgConfigFileValidStmt().Length());
       
   562 	err = file.Write(p);
       
   563 	file.Close();
       
   564 	TEST2(err, KErrNone);
       
   565 	}	
   427 	}	
   566 	
   428 	
   567 void CreateTwoVersionsOfConfigFile()
   429 void CreateTwoVersionsOfConfigFile()
   568 	{
   430 	{
   569 	(void)KillProcess(KSqlSrvName);
   431 	KillSqlServer();
       
   432 	
   570 	TInt err = TheFs.Delete(KCfgDb1CorruptConfigFilePath);
   433 	TInt err = TheFs.Delete(KCfgDb1CorruptConfigFilePath);
   571 	TEST2(err, KErrNone);
   434 	TEST2(err, KErrNone);
   572 	RFile file;
   435 
   573 	TFileName newFile;
   436     TInt nextVersion = 10;
   574 	TInt nextVersion = 10;
   437     TFileName fname;
   575 	newFile.Format(KCfgDb1ConfigFilePath(), nextVersion);
   438     fname.Format(KCfgDb1ConfigFilePath, nextVersion);
   576 	err = file.Create(TheFs, newFile, EFileRead | EFileWrite);
   439     DoCreateCfgFile(fname, KCfgDb1ConfigFileV10ValidStmt);
   577 	TEST2(err, KErrNone);
   440 	
   578 	TPtrC8 p10((const TUint8*)KCfgDb1ConfigFileV10ValidStmt().Ptr(), KCfgDb1ConfigFileV10ValidStmt().Length());
       
   579 	err = file.Write(p10);
       
   580 	file.Close();
       
   581 	TEST2(err, KErrNone);
       
   582 	++nextVersion;
   441 	++nextVersion;
   583 	newFile.Format(KCfgDb1ConfigFilePath(), nextVersion);
   442     fname.Format(KCfgDb1ConfigFilePath, nextVersion);
   584 	err = file.Create(TheFs, newFile, EFileRead | EFileWrite);
   443     DoCreateCfgFile(fname, KCfgDb1ConfigFileV11ValidStmt);
   585 	TEST2(err, KErrNone);
       
   586 	TPtrC8 p11((const TUint8*)KCfgDb1ConfigFileV11ValidStmt().Ptr(), KCfgDb1ConfigFileV11ValidStmt().Length());
       
   587 	err = file.Write(p11);
       
   588 	file.Close();
       
   589 	TEST2(err, KErrNone);	
       
   590 	}
   444 	}
   591  
   445  
   592 ///////////////////////////////////////////////////////////////////////////////////////
   446 ///////////////////////////////////////////////////////////////////////////////////////
   593 // DoDbCfgTests() functions
   447 // DoDbCfgTests() functions
   594 
   448 
       
   449 TInt GetDbCfgVersion(const TDesC& aDbName)
       
   450     {
       
   451     // Note: We have to use SQLite directly to access the settings
       
   452     // table as the SQL Server denies permission to access this table
       
   453     // as it is in a shared, secure database
       
   454     
       
   455     TParse parse;
       
   456     TInt err = parse.Set(aDbName, &KSqlSrvPrivatePath, 0);
       
   457     TEST2(err, KErrNone);
       
   458     
       
   459     TBuf8<KMaxFileName + 1> dbFileName;
       
   460     dbFileName.Copy(parse.FullName());
       
   461     
       
   462     sqlite3 *dbHandle = NULL;
       
   463     TInt rc = sqlite3_open((const char*)dbFileName.PtrZ(), &dbHandle);
       
   464     TEST2(rc, SQLITE_OK);
       
   465     
       
   466     _LIT(KGetDbCfgVersionSql, "SELECT Reserved AS DbCfgVersion FROM symbian_settings");
       
   467     TBuf<100> queryBuf;
       
   468     queryBuf.Copy(KGetDbCfgVersionSql);
       
   469     
       
   470     sqlite3_stmt* stmtHandle = NULL;
       
   471     const void* stmtTailZ = NULL;
       
   472     rc = sqlite3_prepare16_v2(dbHandle, queryBuf.PtrZ(), -1, &stmtHandle, &stmtTailZ);
       
   473     TEST2(rc, SQLITE_OK);
       
   474     
       
   475     rc = sqlite3_step(stmtHandle);
       
   476     TEST2(rc, SQLITE_ROW);
       
   477     
       
   478     TInt version = sqlite3_column_int(stmtHandle, 0);
       
   479     
       
   480     rc = sqlite3_step(stmtHandle);
       
   481     TEST2(rc, SQLITE_DONE);
       
   482     
       
   483     sqlite3_finalize(stmtHandle);
       
   484     sqlite3_close(dbHandle);
       
   485     
       
   486     return version;
       
   487     }
       
   488 
   595 TBool GuessSystemSettingsTable(const TDesC& aDbName, TInt aVersion)
   489 TBool GuessSystemSettingsTable(const TDesC& aDbName, TInt aVersion)
   596 	{
   490 	{
   597 	// Note: We have to use SQLite directly to access the settings
   491 	TInt dbCfgVersion = GetDbCfgVersion(aDbName);
   598 	// table as the SQL Server denies permission to access this table
   492 	return dbCfgVersion == aVersion; 
   599 	// as it is in a shared, secure database
       
   600 	
       
   601 	TBool guessIsCorrect = EFalse;
       
   602 	
       
   603 	TParse parse;
       
   604 	parse.Set(aDbName, &KSqlSrvPrivatePath, 0);
       
   605 	
       
   606 	TBuf8<KMaxFileName + 1> dbFileName;
       
   607 	dbFileName.Copy(parse.FullName());
       
   608 	
       
   609 	TheDbHandle = NULL;
       
   610 	TInt rc = sqlite3_open((const char*)dbFileName.PtrZ(), &TheDbHandle);
       
   611 	TEST2(rc, SQLITE_OK);
       
   612 	
       
   613 	TBuf<100> queryBuf;
       
   614 	queryBuf.Format(KGetSettingsSql(), aVersion);	
       
   615 	
       
   616 	sqlite3_stmt* stmtHandle = NULL;
       
   617 	const void* stmtTailZ = NULL;
       
   618 	rc = sqlite3_prepare16_v2(TheDbHandle, queryBuf.PtrZ(), -1, &stmtHandle, &stmtTailZ);
       
   619 	TEST2(rc, SQLITE_OK);
       
   620 	
       
   621 	rc = sqlite3_step(stmtHandle);
       
   622 	if(SQLITE_ROW == rc)
       
   623 		{
       
   624 		guessIsCorrect = ETrue;
       
   625 		rc = sqlite3_step(stmtHandle);
       
   626 		TEST2(rc, SQLITE_DONE);
       
   627 		}
       
   628 	
       
   629 	sqlite3_finalize(stmtHandle);
       
   630 	sqlite3_close(TheDbHandle);
       
   631 	TheDbHandle = NULL;
       
   632 	
       
   633 	return guessIsCorrect;
       
   634 	}
   493 	}
   635 
   494 
   636 void CheckSystemSettingsTable(const TDesC& aDbName, TInt aVersion)
   495 void CheckSystemSettingsTable(const TDesC& aDbName, TInt aVersion)
   637 	{
   496 	{
   638 	// Note: We have to use SQLite directly to access the settings
   497     TInt dbCfgVersion = GetDbCfgVersion(aDbName);
   639 	// table as the SQL Server denies permission to access this table
   498     TEST2(dbCfgVersion, aVersion); 
   640 	// as it is in a shared, secure database
   499 	}
   641 	
   500 
   642 	TParse parse;
   501 void CheckRecordCount(const TDesC& aTable)
   643 	parse.Set(aDbName, &KSqlSrvPrivatePath, 0);
       
   644 	
       
   645 	TBuf8<KMaxFileName + 1> dbFileName;
       
   646 	dbFileName.Copy(parse.FullName());
       
   647 	
       
   648 	sqlite3 *dbHandle = NULL;
       
   649 	TInt rc = sqlite3_open((const char*)dbFileName.PtrZ(), &dbHandle);
       
   650 	TEST2(rc, SQLITE_OK);
       
   651 	
       
   652 	TBuf<100> queryBuf;
       
   653 	queryBuf.Format(KGetSettingsSql(), aVersion);	
       
   654 	
       
   655 	sqlite3_stmt* stmtHandle = NULL;
       
   656 	const void* stmtTailZ = NULL;
       
   657 	rc = sqlite3_prepare16_v2(dbHandle, queryBuf.PtrZ(), -1, &stmtHandle, &stmtTailZ);
       
   658 	TEST2(rc, SQLITE_OK);
       
   659 	
       
   660 	rc = sqlite3_step(stmtHandle);
       
   661 	TEST2(rc, SQLITE_ROW);
       
   662 	
       
   663 	rc = sqlite3_step(stmtHandle);
       
   664 	TEST2(rc, SQLITE_DONE);
       
   665 	
       
   666 	sqlite3_finalize(stmtHandle);
       
   667 	sqlite3_close(dbHandle);
       
   668 	}
       
   669 
       
   670 void CheckNumberRecordsL(const TDesC& aStmt)
       
   671 	{
   502 	{
   672 	// There should always be only 1 record in the table
   503 	// There should always be only 1 record in the table
   673 	// in each database as INSERT and DELETE statements are
   504 	// in each database as INSERT and DELETE statements are
   674 	// not supported in the config files
   505 	// not supported in the config files
   675 	
   506 	TBuf<50> sql;
   676 	// Prepare stmt
   507 	sql.Format(_L("SELECT COUNT(*) FROM %S"), &aTable);
   677 	RSqlStatement stmt;
   508 	TSqlScalarFullSelectQuery q(TheDb);
   678 	stmt.PrepareL(TheDb, aStmt);
   509 	TInt count = 0;
   679 	
   510     TRAPD(err, count = q.SelectIntL(sql));
   680 	// Get each row
   511     TEST2(err, KErrNone);
   681 	TUint numRows = 0;
   512     TEST2(count, 1);
   682 	TInt err = stmt.Next();
   513 	}
   683 	while(KSqlAtRow == err)
   514 	
   684 		{
   515 TInt ExpectedDb1IndexCount(TInt aExpectedStoredVersion)
   685 		numRows++;
   516 	{
   686 		err = stmt.Next();
   517 	TEST(aExpectedStoredVersion >= 0 && aExpectedStoredVersion < 15);
   687 		}
   518 	const TInt KIdxCnt[] = {
   688 
   519 	       -1, 
   689 	if (KSqlAtEnd != err)
   520 	        3,             // Only files 01 - 04, 09 and 11 will be successfully processed and so stored in the settings table
   690 		{
   521 	        4,             // 1 more index should be added to db1 based on config file 02
   691 		User::Leave(KErrCorrupt);
   522 	        4, 4, 4, 4, 4, // no more indices should be added to db1 based on config file 03 - 07 
   692 		}
   523 	        6,             // 2 more indices should be added to db1 based on config file 08 
       
   524 	        7,             // 1 more index should be added to db1 based on config file 09 
       
   525 	        -1, 
       
   526 	        9,             // 2 more indices should be added to db1 based on config file 11 
       
   527 	        10,            // 1 more index should be added to db1 based on config file 12
       
   528 	        11,            // 1 more index should be added to db1 based on config file 13
       
   529 	        12};           // 1 more index should be added to db1 based on config file 14
       
   530 	return KIdxCnt[aExpectedStoredVersion];
       
   531 	}
       
   532 
       
   533 void CheckIndexCount(TInt aExpectedIndexCount)
       
   534 	{
       
   535 	_LIT(KDbCheckNumIndices, "SELECT COUNT(*) FROM sqlite_master WHERE type = 'index'");
       
   536     TSqlScalarFullSelectQuery q(TheDb);
       
   537     TInt count = 0;
       
   538     TRAPD(err, count = q.SelectIntL(KDbCheckNumIndices));
       
   539     TEST2(err, KErrNone);
       
   540     TEST2(count, aExpectedIndexCount);
       
   541 	}
       
   542 
       
   543 void DoCfgOpenTest(TInt aExpectedStoredVersion, const TDesC& aDbFileName, const TDesC& aTblName, TInt aExpectedIndexCnt)
       
   544     {
       
   545     TInt err = TheDb.Open(aDbFileName);
       
   546     TEST2(err, KErrNone);
       
   547     CheckRecordCount(aTblName);
       
   548     CheckIndexCount(aExpectedIndexCnt);
       
   549     TheDb.Close();
       
   550     // Check that the ops in the specified config file have been applied.
       
   551     CheckSystemSettingsTable(aDbFileName, aExpectedStoredVersion);
       
   552     }
       
   553 
       
   554 void DoCfgOpenTests(TInt aExpectedStoredVersion)
       
   555 	{	
       
   556 	// Open a shared, secure database - config ops should be applied on it.
       
   557     // There should still be only 1 record and 3 indices in the table.
       
   558 	TheTest.Printf(_L("===CfgOpen: Open shared, secure database\r\n"));
       
   559 	DoCfgOpenTest(aExpectedStoredVersion, KCfgDb1, _L("table1"), ExpectedDb1IndexCount(aExpectedStoredVersion));
   693 		
   560 		
   694 	if (numRows != 1)
   561 	// Open again the same shared, secure database - no config should occur (it has already been done).
   695 		{
       
   696 		User::Leave(KErrArgument);
       
   697 		}
       
   698 
       
   699 	// Close stmt
       
   700 	stmt.Close();	
       
   701 	}
       
   702 	
       
   703 TInt NumberIndicesExpectedInDb1(const TInt aExpectedStoredVersion)
       
   704 	{
       
   705 	TInt numIndices = -1;
       
   706 	
       
   707 	switch(aExpectedStoredVersion)
       
   708 		{
       
   709      	// Only files 01 - 04, 09 and 11 will be successfully processed and so stored in the settings table
       
   710 		case 1:
       
   711 			{
       
   712 			numIndices = 3; // 3 indices should be added to db1 based on config file 01
       
   713 			break;
       
   714 			}
       
   715 		case 2:
       
   716 			{
       
   717 			numIndices = 4; // 1 more index should be added to db1 based on config file 02
       
   718 			break;
       
   719 			}
       
   720 		case 3:
       
   721 		case 4:
       
   722 		case 5:
       
   723 		case 6:
       
   724 		case 7:
       
   725 			{
       
   726 			numIndices = 4; // no more indices should be added to db1 based on config file 03 - 07
       
   727 			break;
       
   728 			}
       
   729 		case 8:
       
   730 			{
       
   731 			numIndices = 6; // 2 more indices should be added to db1 based on config file 08
       
   732 			break;
       
   733 			}
       
   734 		case 9:
       
   735 			{
       
   736 			numIndices = 7; // 1 more index should be added to db1 based on config file 09
       
   737 			break;
       
   738 			}
       
   739 		case 11:
       
   740 			{
       
   741 			numIndices = 9; // 2 more indices should be added to db1 based on config file 11
       
   742 			break;
       
   743 			}	
       
   744 		case 12:
       
   745 			{
       
   746 			numIndices = 10; // 1 more index should be added to db1 based on config file 12
       
   747 			break;
       
   748 			}
       
   749 		case 13:
       
   750 			{
       
   751 			numIndices = 11; // 1 more index should be added to db1 based on config file 13
       
   752 			break;
       
   753 			}	
       
   754 		case 14:
       
   755 			{
       
   756 			numIndices = 12; // 1 more index should be added to db1 based on config file 14
       
   757 			break;
       
   758 			}		
       
   759 		}
       
   760 		
       
   761 	return numIndices;	
       
   762 	}
       
   763 
       
   764 void CheckNumberIndicesL(const TInt aNumIndicesExpected)
       
   765 	{
       
   766 	// Prepare stmt
       
   767 	RSqlStatement stmt;
       
   768 	stmt.PrepareL(TheDb, KDbCheckNumIndices);
       
   769 	
       
   770 	// Get each entry of type 'index' in the 'sqlite_master' table
       
   771 	TUint numEntries = 0;
       
   772 	TInt err = stmt.Next();
       
   773 	while(KSqlAtRow == err)
       
   774 		{
       
   775 		numEntries++;
       
   776 		err = stmt.Next();
       
   777 		}
       
   778 
       
   779 	if (KSqlAtEnd != err)
       
   780 		{
       
   781 		User::Leave(KErrCorrupt);
       
   782 		}
       
   783 		
       
   784 	if (numEntries != aNumIndicesExpected)
       
   785 		{
       
   786 		User::Leave(KErrArgument);
       
   787 		}
       
   788 
       
   789 	// Close stmt
       
   790 	stmt.Close();	
       
   791 	}
       
   792 
       
   793 void DoCfgOpenTests(TInt aExpectedStoredVersion = 1)
       
   794 	{	
       
   795 	// Open a shared, secure database - config ops should be applied on it
       
   796 	TheTest.Printf(_L("===CfgOpen: Open shared, secure database\r\n"));
       
   797 	TInt err = TheDb.Open(KCfgDb1);
       
   798 	TEST2(err, KErrNone);
       
   799 	TRAP(err, CheckNumberRecordsL(KDb1CheckNumRecords)); // there should still be only 1 record in the table
       
   800 	TEST2(err, KErrNone);
       
   801 	TRAP(err, CheckNumberIndicesL(NumberIndicesExpectedInDb1(aExpectedStoredVersion))); // there should now be 3 indices in the table
       
   802 	TEST2(err, KErrNone);
       
   803 	TheDb.Close();
       
   804 	CheckSystemSettingsTable(KCfgDb1, aExpectedStoredVersion); // check that the ops in the specified config file have been applied
       
   805 		
       
   806 	// Open again the same shared, secure database - no config should occur (it has already been done)
       
   807 	TheTest.Printf(_L("===CfgOpen: Open shared, secure database again\r\n"));
   562 	TheTest.Printf(_L("===CfgOpen: Open shared, secure database again\r\n"));
   808 	err = TheDb.Open(KCfgDb1);
   563     DoCfgOpenTest(aExpectedStoredVersion, KCfgDb1, _L("table1"), ExpectedDb1IndexCount(aExpectedStoredVersion));
   809 	TEST2(err, KErrNone);
       
   810 	TRAP(err, CheckNumberRecordsL(KDb1CheckNumRecords)); 
       
   811 	TEST2(err, KErrNone);
       
   812 	TRAP(err, CheckNumberIndicesL(NumberIndicesExpectedInDb1(aExpectedStoredVersion)));
       
   813 	TEST2(err, KErrNone);
       
   814 	TheDb.Close();
       
   815 	CheckSystemSettingsTable(KCfgDb1, aExpectedStoredVersion);
       
   816 	
   564 	
   817 	// Open a shared, secure database - no config should occur (there is no config file for this database)
   565 	// Open a shared, secure database - no config should occur (there is no config file for this database)
   818 	TheTest.Printf(_L("===CfgOpen: Open shared, secure database (that has no config file)\r\n"));
   566 	TheTest.Printf(_L("===CfgOpen: Open shared, secure database (that has no config file)\r\n"));
   819 	err = TheDb.Open(KCfgDb2);
   567     DoCfgOpenTest(0, KCfgDb2, _L("table2"), 0);
   820 	TEST2(err, KErrNone);
       
   821 	TRAP(err, CheckNumberRecordsL(KDb2CheckNumRecords));
       
   822 	TEST2(err, KErrNone);
       
   823 	TRAP(err, CheckNumberIndicesL(0)); 
       
   824 	TEST2(err, KErrNone);
       
   825 	TheDb.Close();
       
   826 	CheckSystemSettingsTable(KCfgDb2, 0);
       
   827 	
   568 	
   828 	// Open a private, secure database - no config should occur
   569 	// Open a private, secure database - no config should occur
   829 	TheTest.Printf(_L("===CfgOpen: Open private, secure database\r\n"));
   570 	TheTest.Printf(_L("===CfgOpen: Open private, secure database\r\n"));
   830 	err = TheDb.Open(KCfgDb3);
   571     DoCfgOpenTest(0, KCfgDb3, _L("table3"), 0);
   831 	TEST2(err, KErrNone);
       
   832 	TRAP(err, CheckNumberRecordsL(KDb3CheckNumRecords));
       
   833 	TEST2(err, KErrNone);
       
   834 	TRAP(err, CheckNumberIndicesL(0)); 
       
   835 	TEST2(err, KErrNone);
       
   836 	TheDb.Close();
       
   837 	CheckSystemSettingsTable(KCfgDb3, 0);
       
   838 	
   572 	
   839 	// Open a public database - no config should occur
   573 	// Open a public database - no config should occur
   840 	TheTest.Printf(_L("===CfgOpen: Open public database\r\n"));
   574 	TheTest.Printf(_L("===CfgOpen: Open public database\r\n"));
   841 	err = TheDb.Open(KCfgDb4);
   575     DoCfgOpenTest(0, KCfgDb4, _L("table4"), 0);
   842 	TEST2(err, KErrNone);
       
   843 	TRAP(err, CheckNumberRecordsL(KDb4CheckNumRecords));
       
   844 	TEST2(err, KErrNone);
       
   845 	TRAP(err, CheckNumberIndicesL(0)); 
       
   846 	TEST2(err, KErrNone);
       
   847 	TheDb.Close();
       
   848 	CheckSystemSettingsTable(KCfgDb4, 0);
       
   849 	}
   576 	}
   850 	
   577 	
   851 void DoUpgradedCfgOpenTest()
   578 void DoUpgradedCfgOpenTest()
   852 	{
   579 	{
   853 	// Upgrade the config file for KCfgDb1, i.e. replace v1 file with v2 file
   580 	// Upgrade the config file for KCfgDb1, i.e. replace v1 file with v2 file
   913 void DoNewDbCfgOpenTest()
   640 void DoNewDbCfgOpenTest()
   914 	{
   641 	{
   915 	// Create Db5 - a config file already exists for this database
   642 	// Create Db5 - a config file already exists for this database
   916 	CreateDb5();
   643 	CreateDb5();
   917 	
   644 	
   918 	// Open the shared, secure database Db5 - config ops should be applied on it
   645 	// Open the shared, secure database Db5 - config ops should be applied on it.
       
   646 	// There should now be 1 index in the table.
   919 	TheTest.Printf(_L("===NewDbCfg: Open shared, secure database\r\n"));
   647 	TheTest.Printf(_L("===NewDbCfg: Open shared, secure database\r\n"));
   920 	TInt err = TheDb.Open(KCfgDb5);
   648     DoCfgOpenTest(1, KCfgDb5, _L("table5"), 1);
   921 	TEST2(err, KErrNone);
       
   922 	TRAP(err, CheckNumberRecordsL(KDb5CheckNumRecords)); // there should still be only 1 record in the table
       
   923 	TEST2(err, KErrNone);
       
   924 	TRAP(err, CheckNumberIndicesL(1)); // there should now be 1 index in the table
       
   925 	TEST2(err, KErrNone);
       
   926 	TheDb.Close();
       
   927 	CheckSystemSettingsTable(KCfgDb5, 1); // check that the ops in the specified config file have been applied
       
   928 		
   649 		
   929 	// Open again the same shared, secure database - no config should occur (it has already been done)
   650 	// Open again the same shared, secure database - no config should occur (it has already been done)
   930 	TheTest.Printf(_L("===NewDbCfg: Open shared, secure database again\r\n"));
   651 	TheTest.Printf(_L("===NewDbCfg: Open shared, secure database again\r\n"));
   931 	err = TheDb.Open(KCfgDb5);
   652     DoCfgOpenTest(1, KCfgDb5, _L("table5"), 1);
   932 	TEST2(err, KErrNone);
       
   933 	TRAP(err, CheckNumberRecordsL(KDb5CheckNumRecords)); 
       
   934 	TEST2(err, KErrNone);
       
   935 	TRAP(err, CheckNumberIndicesL(1));
       
   936 	TEST2(err, KErrNone);
       
   937 	TheDb.Close();
       
   938 	CheckSystemSettingsTable(KCfgDb5, 1);
       
   939 	}
   653 	}
   940 	
   654 	
   941 void DoCfgAttachTests(TInt aExpectedStoredVersion = 0)
   655 void DoCfgAttachTests(TInt aExpectedStoredVersion = 0)
   942 	{			
   656 	{			
   943 	// Open a private, secure database - no config should occur
   657 	// Open a private, secure database - no config should occur
  1034 	
   748 	
  1035 	// Open a private, secure database - no config should occur
   749 	// Open a private, secure database - no config should occur
  1036 	TheTest.Printf(_L("===NewDbCfgAttach: Open private, secure database\r\n"));
   750 	TheTest.Printf(_L("===NewDbCfgAttach: Open private, secure database\r\n"));
  1037 	TInt err = TheDb.Open(KCfgDb3);
   751 	TInt err = TheDb.Open(KCfgDb3);
  1038 	TEST2(err, KErrNone);
   752 	TEST2(err, KErrNone);
  1039 	TRAP(err, CheckNumberRecordsL(KDb3CheckNumRecords));
   753     CheckRecordCount(_L("table3"));
  1040 	TEST2(err, KErrNone);
   754     CheckIndexCount(0); 
  1041 	TRAP(err, CheckNumberIndicesL(0)); 
       
  1042 	TEST2(err, KErrNone);
       
  1043 	
   755 	
  1044 	// Attach a shared, secure database - the db5 config file should not be processed
   756 	// Attach a shared, secure database - the db5 config file should not be processed
  1045 	TheTest.Printf(_L("===NewDbCfgAttach: Attach shared, secure database\r\n"));
   757 	TheTest.Printf(_L("===NewDbCfgAttach: Attach shared, secure database\r\n"));
  1046 	err = TheDb.Attach(KCfgDb5, KAttachDb5);
   758 	err = TheDb.Attach(KCfgDb5, KAttachDb5);
  1047 	TEST2(err, KErrNone);
   759 	TEST2(err, KErrNone);
  1048 	TRAP(err, CheckNumberRecordsL(KDb5CheckNumRecords)); // there should still be only 1 record in the table
   760     CheckRecordCount(_L("table5"));
  1049 	TEST2(err, KErrNone);
   761     CheckIndexCount(0); // there should still be no indices in the table
  1050 	TRAP(err, CheckNumberIndicesL(0)); // there should still be no indices in the table
       
  1051 	TEST2(err, KErrNone);
       
  1052 	TheDb.Close();
   762 	TheDb.Close();
  1053 	CheckSystemSettingsTable(KCfgDb5, 1); // check that the config file has been processed for db1
   763 	CheckSystemSettingsTable(KCfgDb5, 1); // check that the config file has been processed for db1
  1054 	CheckSystemSettingsTable(KCfgDb3, 0);
   764 	CheckSystemSettingsTable(KCfgDb3, 0);
  1055 	}
   765 	}
  1056 	
   766 	
  1099 	
   809 	
  1100 	sqlite3 *dbHandle = NULL;
   810 	sqlite3 *dbHandle = NULL;
  1101 	TInt rc = sqlite3_open((const char*)dbFileName.PtrZ(), &dbHandle);
   811 	TInt rc = sqlite3_open((const char*)dbFileName.PtrZ(), &dbHandle);
  1102 	TEST2(rc, SQLITE_OK);
   812 	TEST2(rc, SQLITE_OK);
  1103 	
   813 	
  1104 	TBuf<100> queryBuf;
   814 	_LIT(KGetCollationDllSql, "SELECT CollationDllName FROM symbian_settings");
       
   815     TBuf<100> queryBuf;
  1105 	queryBuf.Append(KGetCollationDllSql());	
   816 	queryBuf.Append(KGetCollationDllSql());	
  1106 	
   817 	
  1107 	sqlite3_stmt* stmtHandle = NULL;
   818 	sqlite3_stmt* stmtHandle = NULL;
  1108 	const void* stmtTailZ = NULL;
   819 	const void* stmtTailZ = NULL;
  1109 	rc = sqlite3_prepare16_v2(dbHandle, queryBuf.PtrZ(), -1, &stmtHandle, &stmtTailZ);
   820 	rc = sqlite3_prepare16_v2(dbHandle, queryBuf.PtrZ(), -1, &stmtHandle, &stmtTailZ);
  1144 @SYMTestExpectedResults The test must not fail
   855 @SYMTestExpectedResults The test must not fail
  1145 @SYMCR 					LMAN-79SJ7L
   856 @SYMCR 					LMAN-79SJ7L
  1146 */
   857 */
  1147 void DoDbCfgTests()
   858 void DoDbCfgTests()
  1148 	{
   859 	{
  1149 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4010 DoDbCfgTests "));
       
  1150 		
       
  1151 	// Do 'open' tests for new db config file feature
   860 	// Do 'open' tests for new db config file feature
  1152 	DoCfgOpenTests();         // open the test databases
   861 	DoCfgOpenTests(1);         // open the test databases
  1153  	DoUpgradedCfgOpenTest();  // upgrade the config file for db1 and reopen the test databases
   862  	DoUpgradedCfgOpenTest();  // upgrade the config file for db1 and reopen the test databases
  1154  	DoBadCfgOpenTests();      // corrupt the config file for db1 (in a variety of ways) and reopen the test databases
   863  	DoBadCfgOpenTests();      // corrupt the config file for db1 (in a variety of ways) and reopen the test databases
  1155  	DoNewDbCfgOpenTest();		  // create a db for which a config file already exists and then open the db
   864  	DoNewDbCfgOpenTest();		  // create a db for which a config file already exists and then open the db
  1156  
   865  
  1157  	// Recreate the original dbs and config files
   866  	// Recreate the original dbs and config files
  1168  	DeleteCfgFilesAndDbs();
   877  	DeleteCfgFilesAndDbs();
  1169  	CreateCfgFilesAndDbs();
   878  	CreateCfgFilesAndDbs();
  1170  	
   879  	
  1171  	// Do the test that causes both reindexing and db configuration to occur when db1 is opened
   880  	// Do the test that causes both reindexing and db configuration to occur when db1 is opened
  1172 	ResetStoredCollationDll();
   881 	ResetStoredCollationDll();
  1173 	DoCfgOpenTests();
   882 	DoCfgOpenTests(1);
  1174 	CheckCollationDllUpdated();
   883 	CheckCollationDllUpdated();
  1175 	
   884 	
  1176 	// Recreate the original dbs and config files
   885 	// Recreate the original dbs and config files
  1177  	DeleteCfgFilesAndDbs();
   886  	DeleteCfgFilesAndDbs();
  1178  	CreateCfgFilesAndDbs();
   887  	CreateCfgFilesAndDbs();
  1198 @SYMTestExpectedResults The test must not fail
   907 @SYMTestExpectedResults The test must not fail
  1199 @SYMCR 					LMAN-79SJ7L
   908 @SYMCR 					LMAN-79SJ7L
  1200 */
   909 */
  1201 void DoDbCfgOOMTests()
   910 void DoDbCfgOOMTests()
  1202 	{
   911 	{
  1203 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4013 DoDbCfgOOMTests "));
       
  1204 
       
  1205 	// Recreate the original dbs and config files
   912 	// Recreate the original dbs and config files
  1206 	DeleteCfgFilesAndDbs();
   913 	DeleteCfgFilesAndDbs();
  1207 	CreateCfgFilesAndDbs();
   914 	CreateCfgFilesAndDbs();
  1208 	
   915 	
  1209 	// The server is stopped at the end of CreateCfgFilesAndDbs().
   916 	// The server is stopped at the end of CreateCfgFilesAndDbs().
  1275 @SYMTestExpectedResults The test must not fail
   982 @SYMTestExpectedResults The test must not fail
  1276 @SYMCR					LMAN-79SJ7L
   983 @SYMCR					LMAN-79SJ7L
  1277 */
   984 */
  1278 void DoDbCfgFileIOFailuresTests()
   985 void DoDbCfgFileIOFailuresTests()
  1279 	{
   986 	{
  1280 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4014 DoDbCfgFileIOFailuresTests "));
       
  1281 
       
  1282 	// Recreate the original dbs and config files
   987 	// Recreate the original dbs and config files
  1283 	DeleteCfgFilesAndDbs();
   988 	DeleteCfgFilesAndDbs();
  1284 	CreateCfgFilesAndDbs();
   989 	CreateCfgFilesAndDbs();
  1285 	
   990 	
  1286 	// The server is stopped at the end of CreateCfgFilesAndDbs().
   991 	// The server is stopped at the end of CreateCfgFilesAndDbs().
  1353 @SYMTestExpectedResults The test must not fail
  1058 @SYMTestExpectedResults The test must not fail
  1354 @SYMCR 					LMAN-79SJ7L
  1059 @SYMCR 					LMAN-79SJ7L
  1355 */
  1060 */
  1356 void DoDbCfgPerfTests()
  1061 void DoDbCfgPerfTests()
  1357 	{
  1062 	{
  1358 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4015 DoDbCfgPerfTests "));
       
  1359 
       
  1360 	// Recreate the original dbs
  1063 	// Recreate the original dbs
  1361 	DeleteCfgFilesAndDbs();
  1064 	DeleteCfgFilesAndDbs();
  1362 	CreateCfgDbs(); 
  1065 	CreateCfgDbs(); 
  1363 	(void)KillProcess(KSqlSrvName); // stop the server
  1066 	KillSqlServer();
  1364 
  1067 
  1365 	// Measure the start up time of the server when
  1068 	// Measure the start up time of the server when
  1366 	// there are no database configuration files to be cached.
  1069 	// there are no database configuration files to be cached.
  1367 	// Open Db4 (public db):
  1070 	// Open Db4 (public db):
  1368 	// No reindexing required
  1071 	// No reindexing required
  1369 	// Database configuration is not considered
  1072 	// Database configuration is not considered
  1370 	TUint32 start = User::FastCounter();
  1073 	TUint32 start = User::FastCounter();
  1371 	TInt err = TheDb.Open(KCfgDb4);
  1074 	TInt err = TheDb.Open(KCfgDb4);
  1372 	TUint32 end = User::FastCounter();
  1075 	TUint32 end = User::FastCounter();
  1373 	TEST2(err, KErrNone);
  1076 	TEST2(err, KErrNone);
  1374 	TRAP(err, CheckNumberRecordsL(KDb4CheckNumRecords));
  1077     CheckRecordCount(_L("table4"));
  1375 	TEST2(err, KErrNone);
  1078     CheckIndexCount(0);
  1376 	TRAP(err, CheckNumberIndicesL(0));
       
  1377 	TEST2(err, KErrNone);
       
  1378 	TheDb.Close();
  1079 	TheDb.Close();
  1379 	CheckSystemSettingsTable(KCfgDb4, 0);
  1080 	CheckSystemSettingsTable(KCfgDb4, 0);
  1380 	TInt ms = CalcTimeMs(start, end);
  1081 	TInt ms = CalcTimeMs(start, end);
  1381 	TheTest.Printf(_L("Execution time: Server startup (via Open call) - no database config files to cache: %d ms\r\n"), ms);
  1082 	TheTest.Printf(_L("Execution time: Server startup (via Open call) - no database config files to cache: %d ms\r\n"), ms);
  1382 	
  1083 	
  1387 	// Database configuration is considered but no config file is found
  1088 	// Database configuration is considered but no config file is found
  1388 	start = User::FastCounter();
  1089 	start = User::FastCounter();
  1389 	err = TheDb.Open(KCfgDb1);
  1090 	err = TheDb.Open(KCfgDb1);
  1390 	end = User::FastCounter();
  1091 	end = User::FastCounter();
  1391 	TEST2(err, KErrNone);
  1092 	TEST2(err, KErrNone);
  1392 	TRAP(err, CheckNumberRecordsL(KDb1CheckNumRecords));
  1093     CheckRecordCount(_L("table1"));
  1393 	TEST2(err, KErrNone);
  1094     CheckIndexCount(0);
  1394 	TRAP(err, CheckNumberIndicesL(0));
       
  1395 	TEST2(err, KErrNone);
       
  1396 	TheDb.Close();
  1095 	TheDb.Close();
  1397 	CheckSystemSettingsTable(KCfgDb1, 0);
  1096 	CheckSystemSettingsTable(KCfgDb1, 0);
  1398 	ms = CalcTimeMs(start, end);
  1097 	ms = CalcTimeMs(start, end);
  1399 	TheTest.Printf(_L("Execution time: Open shared, secure Db1 - no config file is found: %d ms\r\n"), ms);
  1098 	TheTest.Printf(_L("Execution time: Open shared, secure Db1 - no config file is found: %d ms\r\n"), ms);
  1400 	
  1099 	
  1414 	ms = CalcTimeMs(start, end);
  1113 	ms = CalcTimeMs(start, end);
  1415 	TheTest.Printf(_L("Execution time: Attach shared, secure Db1 - database config is not considered: %d ms\r\n"), ms);
  1114 	TheTest.Printf(_L("Execution time: Attach shared, secure Db1 - database config is not considered: %d ms\r\n"), ms);
  1416 	
  1115 	
  1417 	// Create the 4 version 01 config files now
  1116 	// Create the 4 version 01 config files now
  1418 	CreateCfgFiles(); 
  1117 	CreateCfgFiles(); 
  1419 	(void)KillProcess(KSqlSrvName); // stop the server so that the files are found when it is restarted
  1118 	KillSqlServer(); // stop the server so that the files are found when it is restarted
  1420 	
  1119 	
  1421 	// Measure the start up time of the server when
  1120 	// Measure the start up time of the server when
  1422 	// there are 4 database configuration files to be cached.
  1121 	// there are 4 database configuration files to be cached.
  1423 	// Open Db4 (public db):
  1122 	// Open Db4 (public db):
  1424 	// No reindexing required
  1123 	// No reindexing required
  1425 	// Database configuration is not considered
  1124 	// Database configuration is not considered
  1426 	start = User::FastCounter();
  1125 	start = User::FastCounter();
  1427 	err = TheDb.Open(KCfgDb4);
  1126 	err = TheDb.Open(KCfgDb4);
  1428 	end = User::FastCounter();
  1127 	end = User::FastCounter();
  1429 	TEST2(err, KErrNone);
  1128 	TEST2(err, KErrNone);
  1430 	TRAP(err, CheckNumberRecordsL(KDb4CheckNumRecords));
  1129     CheckRecordCount(_L("table4"));
  1431 	TEST2(err, KErrNone);
  1130     CheckIndexCount(0);
  1432 	TRAP(err, CheckNumberIndicesL(0));
       
  1433 	TEST2(err, KErrNone);
       
  1434 	TheDb.Close();
  1131 	TheDb.Close();
  1435 	CheckSystemSettingsTable(KCfgDb4, 0);
  1132 	CheckSystemSettingsTable(KCfgDb4, 0);
  1436 	ms = CalcTimeMs(start, end);
  1133 	ms = CalcTimeMs(start, end);
  1437 	TheTest.Printf(_L("Execution time: Server startup (via Open call) - 4 database config files to cache: %d ms\r\n"), ms);
  1134 	TheTest.Printf(_L("Execution time: Server startup (via Open call) - 4 database config files to cache: %d ms\r\n"), ms);
  1438 	
  1135 	
  1443 	// Database configuration is considered, a file is found and config is applied (3 indices are created)
  1140 	// Database configuration is considered, a file is found and config is applied (3 indices are created)
  1444 	start = User::FastCounter();
  1141 	start = User::FastCounter();
  1445 	err = TheDb.Open(KCfgDb1);
  1142 	err = TheDb.Open(KCfgDb1);
  1446 	end = User::FastCounter();
  1143 	end = User::FastCounter();
  1447 	TEST2(err, KErrNone);
  1144 	TEST2(err, KErrNone);
  1448 	TRAP(err, CheckNumberRecordsL(KDb1CheckNumRecords));
  1145     CheckRecordCount(_L("table1"));
  1449 	TEST2(err, KErrNone);
  1146     CheckIndexCount(3);
  1450 	TRAP(err, CheckNumberIndicesL(3));
       
  1451 	TEST2(err, KErrNone);
       
  1452 	TheDb.Close();
  1147 	TheDb.Close();
  1453 	CheckSystemSettingsTable(KCfgDb1, 1);
  1148 	CheckSystemSettingsTable(KCfgDb1, 1);
  1454 	ms = CalcTimeMs(start, end);
  1149 	ms = CalcTimeMs(start, end);
  1455 	TheTest.Printf(_L("Execution time: Open shared, secure Db1 - config file is found and applied: %d ms\r\n"), ms);
  1150 	TheTest.Printf(_L("Execution time: Open shared, secure Db1 - config file is found and applied: %d ms\r\n"), ms);
  1456 	
  1151 	
  1461 	// Database configuration is considered, a file is found but it has already been processed
  1156 	// Database configuration is considered, a file is found but it has already been processed
  1462 	start = User::FastCounter();
  1157 	start = User::FastCounter();
  1463 	err = TheDb.Open(KCfgDb1);
  1158 	err = TheDb.Open(KCfgDb1);
  1464 	end = User::FastCounter();
  1159 	end = User::FastCounter();
  1465 	TEST2(err, KErrNone);
  1160 	TEST2(err, KErrNone);
  1466 	TRAP(err, CheckNumberRecordsL(KDb1CheckNumRecords));
  1161     CheckRecordCount(_L("table1"));
  1467 	TEST2(err, KErrNone);
  1162     CheckIndexCount(3);
  1468 	TRAP(err, CheckNumberIndicesL(3));
       
  1469 	TEST2(err, KErrNone);
       
  1470 	TheDb.Close();
  1163 	TheDb.Close();
  1471 	CheckSystemSettingsTable(KCfgDb1, 1);
  1164 	CheckSystemSettingsTable(KCfgDb1, 1);
  1472 	ms = CalcTimeMs(start, end);
  1165 	ms = CalcTimeMs(start, end);
  1473 	TheTest.Printf(_L("Execution time: Open shared, secure Db1 - config file is found but already processed: %d ms\r\n"), ms);	
  1166 	TheTest.Printf(_L("Execution time: Open shared, secure Db1 - config file is found but already processed: %d ms\r\n"), ms);	
  1474 	
  1167 	
  1487 	CheckSystemSettingsTable(KCfgDb1, 1);
  1180 	CheckSystemSettingsTable(KCfgDb1, 1);
  1488 	ms = CalcTimeMs(start, end);
  1181 	ms = CalcTimeMs(start, end);
  1489 	TheTest.Printf(_L("Execution time: Attach shared, secure Db1 - database config is not considered: %d ms\r\n"), ms);
  1182 	TheTest.Printf(_L("Execution time: Attach shared, secure Db1 - database config is not considered: %d ms\r\n"), ms);
  1490 	}
  1183 	}
  1491 
  1184 
  1492 void TestStatementsL()
  1185 void TestStatements()
  1493 	{
  1186 	{
  1494 	_LIT(KDbName,					"attachDb");
  1187 	_LIT(KDbName, "attachDb");
  1495 	_LIT(KCreateIndex,				"CREATE INDEX idx ON tbl(ColA)");
       
  1496 	_LIT(KCreateIndexIfNotExists,   "CREATE INDEX IF NOT EXISTS idx ON tbl(ColA)");
       
  1497 	_LIT(KCreateUniqueIndex,		"CREATE UNIQUE INDEX idx ON tbl(ColA)");
       
  1498 	_LIT(KNonSupported,				"CREATE idx ON tbl(ColA)");
       
  1499 	TBuf<200> buf;
  1188 	TBuf<200> buf;
  1500 
  1189 
  1501 	// supported statements
  1190 	// supported statements
  1502 	
  1191 	TBool rc = IsStatementSupported(_L("CREATE INDEX idx ON tbl(ColA)"), KDbName, buf); 
  1503 	TBool rc = IsStatementSupported(KCreateIndex, KDbName, buf); 
       
  1504 	TEST(rc);
  1192 	TEST(rc);
  1505 	
  1193 	rc = IsStatementSupported(_L("CREATE INDEX IF NOT EXISTS idx ON tbl(ColA)"), KDbName, buf); 
  1506 	rc = IsStatementSupported(KCreateIndexIfNotExists, KDbName, buf); 
       
  1507 	TEST(rc);
  1194 	TEST(rc);
  1508 
  1195 
  1509 	// unsupported statements
  1196 	// unsupported statements
  1510 	rc = IsStatementSupported(KCreateUniqueIndex, KDbName, buf); 
  1197 	rc = IsStatementSupported(_L("CREATE UNIQUE INDEX idx ON tbl(ColA)"), KDbName, buf); 
  1511 	TEST(!rc);
  1198 	TEST(!rc);
  1512 
  1199 	rc = IsStatementSupported(_L("CREATE idx ON tbl(ColA)"), KDbName, buf); 
  1513 	rc = IsStatementSupported(KNonSupported, KDbName, buf); 
       
  1514 	TEST(!rc);
  1200 	TEST(!rc);
  1515 	}
  1201 	}
  1516 
  1202 
  1517 /**
  1203 /**
  1518 @SYMTestCaseID			SYSLIB-SQL-UT-4030
  1204 @SYMTestCaseID			SYSLIB-SQL-UT-4030
  1525 @SYMTestExpectedResults The test must not fail
  1211 @SYMTestExpectedResults The test must not fail
  1526 @SYMDEF					DEF118058
  1212 @SYMDEF					DEF118058
  1527 */
  1213 */
  1528 void DoIsStatementSupportedTests()
  1214 void DoIsStatementSupportedTests()
  1529 	{
  1215 	{
  1530 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4030 DoIsStatementSupportedTests "));
  1216 	TestStatements();
  1531 	TInt err = KErrNone;
       
  1532 	TRAP(err, TestStatementsL());
       
  1533 	TEST2(err, KErrNone);
       
  1534 	}
  1217 	}
  1535 
  1218 
  1536 void DoLongDbNameTest()
  1219 void DoLongDbNameTest()
  1537 	{
  1220 	{
  1538 	TheTest.Next(_L("'Long database name' tests"));
       
  1539 	//Create the database	
  1221 	//Create the database	
  1540  	RSqlSecurityPolicy securityPolicy;
  1222  	RSqlSecurityPolicy securityPolicy = CreateSecurityPolicy();
  1541  	TInt err = DoCreateSecurityPolicy(securityPolicy);
  1223  	
  1542  	TEST2(err, KErrNone);
  1224  	TInt err = TheDb.Create(KLongDbName1, securityPolicy);
  1543  	err = TheDb.Create(KLongDbName1, securityPolicy);
       
  1544  	TEST2(err, KErrNone);
  1225  	TEST2(err, KErrNone);
  1545  	err = TheDb.Exec(_L("CREATE TABLE table1(i1 INTEGER, i2 INTEGER, i3 INTEGER)"));
  1226  	err = TheDb.Exec(_L("CREATE TABLE table1(i1 INTEGER, i2 INTEGER, i3 INTEGER)"));
  1546  	TEST(err >= 0);
  1227  	TEST(err >= 0);
  1547  	err = TheDb.Exec(_L("INSERT INTO table1 (i1,i2,i3) values(1,2,3)"));
  1228  	err = TheDb.Exec(_L("INSERT INTO table1 (i1,i2,i3) values(1,2,3)"));
  1548  	TEST(err == 1);
  1229  	TEST(err == 1);
  1549  	TheDb.Close();
  1230  	TheDb.Close();
  1550  	//Kill the server (to reload config file names at the server startup)
  1231  	//Kill the server (to reload config file names at the server startup)
  1551 	(void)KillProcess(KSqlSrvName);
  1232  	KillSqlServer();
  1552 	///////////////////////////////////////////////////////////////////////
  1233 	///////////////////////////////////////////////////////////////////////
  1553 	TheTest.Printf(_L("Open a database with a long name\r\n"));
  1234 	TheTest.Printf(_L("Open a database with a long name\r\n"));
  1554 	//Create cfg file
  1235 	//Create cfg file
  1555  	RFile file;
  1236  	RFile file;
  1556 	TFileName fileName;
  1237 	TFileName fileName;
  1561 	file.Close();	
  1242 	file.Close();	
  1562 	TEST2(err, KErrNone);
  1243 	TEST2(err, KErrNone);
  1563 	//Open the database
  1244 	//Open the database
  1564 	err = TheDb.Open(KLongDbName1);
  1245 	err = TheDb.Open(KLongDbName1);
  1565 	TEST2(err, KErrNone);
  1246 	TEST2(err, KErrNone);
  1566 	TRAP(err, CheckNumberRecordsL(KDb1CheckNumRecords)); // there should still be only 1 record in the table
  1247     CheckRecordCount(_L("table1"));
  1567 	TEST2(err, KErrNone);
  1248     CheckIndexCount(1); // there should now be 1 index in the table
  1568 	TRAP(err, CheckNumberIndicesL(1)); // there should now be 1 index in the table
       
  1569 	TEST2(err, KErrNone);
       
  1570 	TheDb.Close();
  1249 	TheDb.Close();
  1571 	const TInt KVersion = 1;
  1250 	const TInt KVersion = 1;
  1572 	CheckSystemSettingsTable(KLongDbName1, KVersion); // check that the ops in the specified config file have been applied
  1251 	CheckSystemSettingsTable(KLongDbName1, KVersion); // check that the ops in the specified config file have been applied
  1573 	///////////////////////////////////////////////////////////////////////
  1252 	///////////////////////////////////////////////////////////////////////
  1574 	TheTest.Printf(_L("Attach a database with a long logical name\r\n"));
  1253 	TheTest.Printf(_L("Attach a database with a long logical name\r\n"));
  1581  	TEST(err >= 0);
  1260  	TEST(err >= 0);
  1582  	err = TheDb.Exec(_L("INSERT INTO table1 (i1,i2,i3) values(1,2,3)"));
  1261  	err = TheDb.Exec(_L("INSERT INTO table1 (i1,i2,i3) values(1,2,3)"));
  1583  	TEST(err == 1);
  1262  	TEST(err == 1);
  1584  	TheDb.Close();
  1263  	TheDb.Close();
  1585  	//Kill the server (to reload config file names at the server startup)
  1264  	//Kill the server (to reload config file names at the server startup)
  1586 	(void)KillProcess(KSqlSrvName);
  1265  	KillSqlServer();
  1587 	//Open the main database
  1266 	//Open the main database
  1588  	err = TheDb.Open(KCfgDb1);
  1267  	err = TheDb.Open(KCfgDb1);
  1589  	securityPolicy.Close();
  1268  	securityPolicy.Close();
  1590 	TEST2(err, KErrNone);
  1269 	TEST2(err, KErrNone);
  1591 	//Attach a database with a very long logical name
  1270 	//Attach a database with a very long logical name
  1600 	CheckSystemSettingsTable(KLongDbName1, KVersion); // check that the ops in the specified config file have been applied
  1279 	CheckSystemSettingsTable(KLongDbName1, KVersion); // check that the ops in the specified config file have been applied
  1601 	//Cleanup
  1280 	//Cleanup
  1602  	(void)RSqlDatabase::Delete(KLongDbName1);
  1281  	(void)RSqlDatabase::Delete(KLongDbName1);
  1603 	}
  1282 	}
  1604 
  1283 
       
  1284 void DoTests()
       
  1285     {
       
  1286     TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4010 Database config files test"));
       
  1287     DoDbCfgTests();
       
  1288     
       
  1289     TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4013 Database config files OOM test"));
       
  1290     DoDbCfgOOMTests();
       
  1291     
       
  1292     TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4014 Database config files file I/O error simulation test"));
       
  1293     DoDbCfgFileIOFailuresTests();
       
  1294     
       
  1295     TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4015 Database config files performance test"));
       
  1296     DoDbCfgPerfTests();
       
  1297 
       
  1298     TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4030 Database config files - supported SQl statements test"));
       
  1299     DoIsStatementSupportedTests();
       
  1300 
       
  1301     TheTest.Next(_L("'Long database name' tests"));
       
  1302     DoLongDbNameTest();
       
  1303     }
       
  1304 
  1605 #endif	//SYSLIBS_TEST
  1305 #endif	//SYSLIBS_TEST
  1606 
  1306 
  1607 TInt E32Main()
  1307 TInt E32Main()
  1608 	{
  1308 	{
  1609 	TheTest.Title();
  1309 	TheTest.Title();
  1612 	TheTest(tc != NULL);
  1312 	TheTest(tc != NULL);
  1613 	
  1313 	
  1614 	__UHEAP_MARK;
  1314 	__UHEAP_MARK;
  1615 
  1315 
  1616 #ifdef SYSLIBS_TEST	
  1316 #ifdef SYSLIBS_TEST	
  1617 	TheTest.Start(_L("t_sqldbconfigfile tests"));
  1317 	TheTest.Start(_L("Setting up the test environment"));
  1618 
       
  1619 	// Set up the test environment
       
  1620 	SetupTestEnv();
  1318 	SetupTestEnv();
  1621 	//Init sqlite library
       
  1622 	sqlite3SymbianLibInit();
  1319 	sqlite3SymbianLibInit();
  1623 
  1320 	DoTests();
  1624 	// Do tests for database config files
       
  1625 	DoDbCfgTests();
       
  1626 	
       
  1627 	// Do OOM tests for database config files
       
  1628 	DoDbCfgOOMTests();
       
  1629 	
       
  1630 	// Do file I/O failure tests for database config files
       
  1631 	DoDbCfgFileIOFailuresTests();
       
  1632 
       
  1633 	// Do performance tests for database config files
       
  1634 	DoDbCfgPerfTests();
       
  1635 
       
  1636 	// Test IsStatementSupportedLC function
       
  1637 	DoIsStatementSupportedTests();
       
  1638 
       
  1639 	//Test with a very long database file (and config file) name
       
  1640 	DoLongDbNameTest();
       
  1641  
       
  1642   	// Destroy the test environment
       
  1643  	DestroyTestEnv();
  1321  	DestroyTestEnv();
  1644 	sqlite3SymbianLibFinalize();
  1322 	sqlite3SymbianLibFinalize();
  1645 	CloseSTDLIB();
  1323 	CloseSTDLIB();
  1646 	
       
  1647 	TheTest.End();
  1324 	TheTest.End();
  1648 #else
  1325 #else
  1649  	TheTest.Start(_L("This test works only if the whole SQL component is built with SYSLIBS_TEST macro defined!"));
  1326  	TheTest.Start(_L("This test works only if the whole SQL component is built with SYSLIBS_TEST macro defined!"));
  1650 	TheTest.End();
  1327 	TheTest.End();
  1651 #endif	
  1328 #endif