persistentstorage/sql/TEST/t_sqlfserr.cpp
branchRCL_3
changeset 8 fa9941cf3867
parent 0 08ec8eefde2f
child 11 211563e4b919
equal deleted inserted replaced
6:5ffdb8f2067f 8:fa9941cf3867
    25 ///////////////////////////////////////////////////////////////////////////////////////
    25 ///////////////////////////////////////////////////////////////////////////////////////
    26 
    26 
    27 RTest TheTest(_L("t_sqlfserr test"));
    27 RTest TheTest(_L("t_sqlfserr test"));
    28 _LIT(KTestDir, "c:\\test\\");
    28 _LIT(KTestDir, "c:\\test\\");
    29 _LIT(KTestDbName, "c:\\test\\t_fserr.db");
    29 _LIT(KTestDbName, "c:\\test\\t_fserr.db");
       
    30 _LIT(KPrivateTestDbName, "c:\\private\\212A2C27\\t_fserr2.db");
       
    31 _LIT(KSecureTestDbName, "c:[212A2C27]t_fserr3.db");
       
    32 
    30 TFileName TheRmvMediaDbFileName;//The name of the file used for tests on a removable media
    33 TFileName TheRmvMediaDbFileName;//The name of the file used for tests on a removable media
    31 RFs TheFs;
    34 RFs TheFs;
    32 RSqlDatabase TheDb;
    35 RSqlDatabase TheDb;
    33 
    36 
    34 //The next constants are used in the "blob write" test
    37 //The next constants are used in the "blob write" test
    53 	}
    56 	}
    54 
    57 
    55 void DestroyTestEnv()
    58 void DestroyTestEnv()
    56 	{
    59 	{
    57 	TheDb.Close();
    60 	TheDb.Close();
       
    61     (void)RSqlDatabase::Delete(KSecureTestDbName);
       
    62     (void)RSqlDatabase::Delete(KPrivateTestDbName);
    58 	(void)RSqlDatabase::Delete(KTestDbName);
    63 	(void)RSqlDatabase::Delete(KTestDbName);
    59 	(void)RSqlDatabase::Delete(TheRmvMediaDbFileName);
    64 	(void)RSqlDatabase::Delete(TheRmvMediaDbFileName);
    60 	TheFs.Close();
    65 	TheFs.Close();
    61 	sqlite3SymbianLibFinalize();
    66 	sqlite3SymbianLibFinalize();
    62 	CloseSTDLIB();
    67 	CloseSTDLIB();
   115 	TEST(err == KErrNone || err == KErrAlreadyExists);
   120 	TEST(err == KErrNone || err == KErrAlreadyExists);
   116 
   121 
   117 	sqlite3SymbianLibInit();
   122 	sqlite3SymbianLibInit();
   118 	}
   123 	}
   119 
   124 
   120 TBool CheckRecord(TInt aId, const TDesC& aExpectedName, TBool aOpenDb = ETrue)
   125 TBool CheckRecord(const TDesC& aDbName, TInt aId, const TDesC& aExpectedName, TBool aOpenDb = ETrue)
   121 	{
   126 	{
   122 	if(aOpenDb)
   127 	if(aOpenDb)
   123 		{
   128 		{
   124 		TEST2(TheDb.Open(KTestDbName), KErrNone);
   129 		TEST2(TheDb.Open(aDbName), KErrNone);
   125 		}
   130 		}
   126 	TBuf<64> sql;
   131 	TBuf<64> sql;
   127 	sql.Copy(_L("SELECT Name FROM A WHERE Id="));
   132 	sql.Copy(_L("SELECT Name FROM A WHERE Id="));
   128 	sql.AppendNum(aId);
   133 	sql.AppendNum(aId);
   129 	TSqlScalarFullSelectQuery q(TheDb);
   134 	TSqlScalarFullSelectQuery q(TheDb);
   177 			if(err < 1)
   182 			if(err < 1)
   178 				{
   183 				{
   179 				TheDb.Close();//close the database to recover from the last error
   184 				TheDb.Close();//close the database to recover from the last error
   180 				//check the database content - all bets are off in a case of an I/O error. 
   185 				//check the database content - all bets are off in a case of an I/O error. 
   181 				//The existing record might have been updated.
   186 				//The existing record might have been updated.
   182 				TEST(CheckRecord(1, _L("Name")) || CheckRecord(1, _L("Name2")));
   187 				TEST(CheckRecord(KTestDbName, 1, _L("Name")) || CheckRecord(KTestDbName, 1, _L("Name2")));
   183 				}
   188 				}
   184 			else
   189 			else
   185 				{
   190 				{
   186 				TEST2(err, 1);
   191 				TEST2(err, 1);
   187 				//check the database content has been modified by the operation. 
   192 				//check the database content has been modified by the operation. 
   188 				TEST(CheckRecord(1, _L("Name2"), EFalse));
   193 				TEST(CheckRecord(KTestDbName, 1, _L("Name2"), EFalse));
   189 				TheDb.Close();
   194 				TheDb.Close();
   190 				}
   195 				}
   191 			}
   196 			}
   192 		}
   197 		}
   193 	(void)TheFs.SetErrorCondition(KErrNone);
   198 	(void)TheFs.SetErrorCondition(KErrNone);
   194 	TEST2(err, 1);
   199 	TEST2(err, 1);
   195 	//check the database content (transaction durability). 
   200 	//check the database content (transaction durability). 
   196 	TEST(CheckRecord(1, _L("Name2")));
   201 	TEST(CheckRecord(KTestDbName, 1, _L("Name2")));
   197 	err = RSqlDatabase::Delete(KTestDbName);
   202 	err = RSqlDatabase::Delete(KTestDbName);
   198 	TEST2(err, KErrNone);
   203 	TEST2(err, KErrNone);
   199 	TheTest.Printf(_L("\r\n"));
   204 	TheTest.Printf(_L("\r\n"));
   200 	}
   205 	}
   201 
   206 
   243 			if(err < 1)
   248 			if(err < 1)
   244 				{
   249 				{
   245 				TheDb.Close();//close the database to recover from the last error
   250 				TheDb.Close();//close the database to recover from the last error
   246 				//check the database content - all bets are off in a case of an I/O error. 
   251 				//check the database content - all bets are off in a case of an I/O error. 
   247 				//The existing record might have been updated.
   252 				//The existing record might have been updated.
   248 				TEST(CheckRecord(1, _L("Name")) || CheckRecord(1, _L("Name2")));
   253 				TEST(CheckRecord(KTestDbName, 1, _L("Name")) || CheckRecord(KTestDbName, 1, _L("Name2")));
   249 				}
   254 				}
   250 			else
   255 			else
   251 				{
   256 				{
   252 				TEST2(err, 1);
   257 				TEST2(err, 1);
   253 				//check the database content has been modified by the operation. 
   258 				//check the database content has been modified by the operation. 
   254 				TEST(CheckRecord(1, _L("Name2"), EFalse));
   259 				TEST(CheckRecord(KTestDbName, 1, _L("Name2"), EFalse));
   255 				TheDb.Close();
   260 				TheDb.Close();
   256 				}
   261 				}
   257 			}
   262 			}
   258 		}
   263 		}
   259 	(void)TheFs.SetErrorCondition(KErrNone);
   264 	(void)TheFs.SetErrorCondition(KErrNone);
   260 	TEST2(err, 1);
   265 	TEST2(err, 1);
   261 	//check the database content has been modified by the operation. 
   266 	//check the database content has been modified by the operation. 
   262 	TEST(CheckRecord(1, _L("Name2")));
   267 	TEST(CheckRecord(KTestDbName, 1, _L("Name2")));
   263 	err = RSqlDatabase::Delete(KTestDbName);
   268 	err = RSqlDatabase::Delete(KTestDbName);
   264 	TEST2(err, KErrNone);
   269 	TEST2(err, KErrNone);
   265 	TheTest.Printf(_L("\r\n"));
   270 	TheTest.Printf(_L("\r\n"));
   266 	}
   271 	}
       
   272 
       
   273 void CreateTestSecurityPolicy(RSqlSecurityPolicy& aSecurityPolicy)
       
   274     {
       
   275     TSecurityPolicy alwaysPassPolicy(TSecurityPolicy::EAlwaysPass);
       
   276     TInt err = aSecurityPolicy.Create(alwaysPassPolicy);
       
   277     TEST2(err, KErrNone);
       
   278 
       
   279     err = aSecurityPolicy.SetDbPolicy(RSqlSecurityPolicy::ESchemaPolicy, alwaysPassPolicy);
       
   280     TEST2(err, KErrNone);
       
   281     err = aSecurityPolicy.SetDbPolicy(RSqlSecurityPolicy::EWritePolicy, alwaysPassPolicy);
       
   282     TEST2(err, KErrNone);
       
   283     err = aSecurityPolicy.SetDbPolicy(RSqlSecurityPolicy::EReadPolicy, alwaysPassPolicy);
       
   284     TEST2(err, KErrNone);
       
   285     }
   267 
   286 
   268 /**
   287 /**
   269 @SYMTestCaseID			SYSLIB-SQL-UT-3421
   288 @SYMTestCaseID			SYSLIB-SQL-UT-3421
   270 @SYMTestCaseDesc		Test for DEF103859 "SQLITE panic, _DEBUG mode, persistent file I/O error simulation".
   289 @SYMTestCaseDesc		Test for DEF103859 "SQLITE panic, _DEBUG mode, persistent file I/O error simulation".
   271 						The test creates a test database with one table, inserts one record.
   290 						The test creates a test database with one table, inserts one record.
   277 @SYMTestExpectedResults The test must not fail
   296 @SYMTestExpectedResults The test must not fail
   278 @SYMDEF					DEF103859
   297 @SYMDEF					DEF103859
   279 */
   298 */
   280 void OpenDatabaseTest()
   299 void OpenDatabaseTest()
   281 	{
   300 	{
   282 	(void)RSqlDatabase::Delete(KTestDbName);
   301     TPtrC dbName[] = {KTestDbName(), KPrivateTestDbName(), KSecureTestDbName()};
   283 	TInt err = TheDb.Create(KTestDbName);
   302     const TInt KDbNameCnt = sizeof(dbName) / sizeof(dbName[0]);
   284 	TEST2(err, KErrNone);
   303     for(TInt k=0;k<KDbNameCnt;++k)
   285 	err = TheDb.Exec(_L("CREATE TABLE A(Id INTEGER,Name TEXT)"));
   304         {	
   286 	TEST(err >= 0);
   305         TheTest.Printf(_L("Database: \"%S\"\r\n"), &dbName[k]);       
   287 	err = TheDb.Exec(_L("INSERT INTO A(Id,Name) VALUES(1,'Name')"));
   306         (void)RSqlDatabase::Delete(dbName[k]);
   288 	TEST2(err, 1);
   307         TInt err = KErrGeneral;
   289 	TheDb.Close();
   308         if(k == (KDbNameCnt - 1))
   290 
   309             {
   291 	err = KErrNotFound;
   310             RSqlSecurityPolicy policy;
   292 	for(TInt cnt=1;err<KErrNone;++cnt)
   311             CreateTestSecurityPolicy(policy);
   293 		{		
   312             err = TheDb.Create(dbName[k], policy);
   294 		TheTest.Printf(_L("%d \r"), cnt);		
   313             policy.Close();
   295 		for (TInt fsError=KErrNotFound;fsError>=KErrDied;--fsError)
   314             }
   296 			{
   315         else
   297 			(void)TheFs.SetErrorCondition(fsError, cnt);
   316             {
   298 			err = TheDb.Open(KTestDbName);
   317             err = TheDb.Create(dbName[k]);
   299 			(void)TheFs.SetErrorCondition(KErrNone);
   318             }
   300 			if(err != KErrNone)
   319         TEST2(err, KErrNone);
   301 				{
   320         err = TheDb.Exec(_L("CREATE TABLE A(Id INTEGER,Name TEXT)"));
   302 				TheDb.Close();//close the database to recover from the last error
   321         TEST(err >= 0);
   303 				//check the database content is still the same as before the "open" call
   322         err = TheDb.Exec(_L("INSERT INTO A(Id,Name) VALUES(1,'Name')"));
   304 				TEST(CheckRecord(1, _L("Name")));
   323         TEST2(err, 1);
   305 				}
   324         TheDb.Close();
   306 			else
   325     
   307 				{
   326         err = KErrNotFound;
   308 				TEST2(err, KErrNone);
   327         for(TInt cnt=1;err<KErrNone;++cnt)
   309 				//check the database content is still the same as before the operation, without closing the database
   328             {		
   310 				TEST(CheckRecord(1, _L("Name"), EFalse));
   329             TheTest.Printf(_L("%d \r"), cnt);		
   311 				TheDb.Close();
   330             for (TInt fsError=KErrNotFound;fsError>=KErrDied;--fsError)
   312 				}
   331                 {
   313 			}
   332                 (void)TheFs.SetErrorCondition(fsError, cnt);
   314 		}
   333                 err = TheDb.Open(dbName[k]);
   315 	(void)TheFs.SetErrorCondition(KErrNone);
   334                 (void)TheFs.SetErrorCondition(KErrNone);
   316 	TEST2(err, KErrNone);
   335                 if(err != KErrNone)
   317 	//check the database content is the same as before the operation, after reopening the database.
   336                     {
   318 	TEST(CheckRecord(1, _L("Name")));
   337                     TheDb.Close();//close the database to recover from the last error
   319 	err = RSqlDatabase::Delete(KTestDbName);
   338                     //check the database content is still the same as before the "open" call
   320 	TEST2(err, KErrNone);
   339                     TEST(CheckRecord(dbName[k], 1, _L("Name")));
   321 	TheTest.Printf(_L("\r\n"));
   340                     }
       
   341                 else
       
   342                     {
       
   343                     TEST2(err, KErrNone);
       
   344                     //check the database content is still the same as before the operation, without closing the database
       
   345                     TEST(CheckRecord(dbName[k], 1, _L("Name"), EFalse));
       
   346                     TheDb.Close();
       
   347                     }
       
   348                 }
       
   349             }
       
   350         (void)TheFs.SetErrorCondition(KErrNone);
       
   351         TEST2(err, KErrNone);
       
   352         //check the database content is the same as before the operation, after reopening the database.
       
   353         TEST(CheckRecord(dbName[k], 1, _L("Name")));
       
   354         err = RSqlDatabase::Delete(dbName[k]);
       
   355         TEST2(err, KErrNone);
       
   356         TheTest.Printf(_L("\r\n"));
       
   357         }
   322 	}
   358 	}
   323 
   359 
   324 /**
   360 /**
   325 @SYMTestCaseID			SYSLIB-SQL-UT-3434
   361 @SYMTestCaseID			SYSLIB-SQL-UT-3434
   326 @SYMTestCaseDesc		Test for DEF104820 "SQL, RSqlDatabase::Create() does not delete the file if fails".
   362 @SYMTestCaseDesc		Test for DEF104820 "SQL, RSqlDatabase::Create() does not delete the file if fails".
   333 @SYMTestExpectedResults The test must not fail
   369 @SYMTestExpectedResults The test must not fail
   334 @SYMDEF					DEF103859
   370 @SYMDEF					DEF103859
   335 */
   371 */
   336 void CreateDatabaseTest()
   372 void CreateDatabaseTest()
   337 	{
   373 	{
   338 	TInt err = -1;
   374 	RSqlSecurityPolicy policy;
   339 	for(TInt cnt=1;err<KErrNone;++cnt)
   375 	CreateTestSecurityPolicy(policy);
   340 		{		
   376 	
   341 		TheTest.Printf(_L("%d \r"), cnt);		
   377 	TPtrC dbName[] = {KTestDbName(), KPrivateTestDbName(), KSecureTestDbName()};
   342 		for (TInt fsError=KErrNotFound;fsError>=KErrDied;--fsError)
   378 	const TInt KDbNameCnt = sizeof(dbName) / sizeof(dbName[0]);
   343 			{
   379 	for(TInt k=0;k<KDbNameCnt;++k)
   344 			//Ideally, the database should be deleted by the SQL server, if RSqlDatabase::Create() fails.
   380 	    {
   345 			//But SetErrorCondition() makes the error persistent, so the SQL server will fail to delete the file.
   381         TheTest.Printf(_L("Database: \"%S\"\r\n"), &dbName[k]);       
   346 			//This is the reason, RSqlDatabase::Delete()to be used, before simulating file I/O error.
   382         TInt err = -1;
   347 			(void)RSqlDatabase::Delete(KTestDbName);
   383         for(TInt cnt=1;err<KErrNone;++cnt)
   348 			(void)TheFs.SetErrorCondition(fsError, cnt);
   384             {		
   349 			err = TheDb.Create(KTestDbName);
   385             TheTest.Printf(_L("%d \r"), cnt);		
   350 			(void)TheFs.SetErrorCondition(KErrNone);
   386             for (TInt fsError=KErrNotFound;fsError>=KErrDied;--fsError)
   351 			TheDb.Close();
   387                 {
   352 			//If err != KErrNone, the database file should have been already deleted by the server and here is 
   388                 //Ideally, the database should be deleted by the SQL server, if RSqlDatabase::Create() fails.
   353 			//the place to check that. But since the file I/O failure simulation makes the file I/O error 
   389                 //But SetErrorCondition() makes the error persistent, so the SQL server will fail to delete the file.
   354 			//persistent, the file cannot be deleted by the server, because the "file delete" operation also fails.
   390                 //This is the reason, RSqlDatabase::Delete()to be used, before simulating file I/O error.
   355 			}
   391                 (void)RSqlDatabase::Delete(dbName[k]);
   356 		}
   392                 (void)TheFs.SetErrorCondition(fsError, cnt);
   357 	(void)TheFs.SetErrorCondition(KErrNone);
   393                 err = (k == (KDbNameCnt - 1)) ? TheDb.Create(dbName[k], policy) : TheDb.Create(dbName[k]);
   358 	TheDb.Close();
   394                 (void)TheFs.SetErrorCondition(KErrNone);
   359 	TEST2(err, KErrNone);
   395                 TheDb.Close();
   360 	TEST(FileExists(KTestDbName));
   396                 //If err != KErrNone, the database file should have been already deleted by the server and here is 
   361 	err = RSqlDatabase::Delete(KTestDbName);
   397                 //the place to check that. But since the file I/O failure simulation makes the file I/O error 
   362 	TEST2(err, KErrNone);
   398                 //persistent, the file cannot be deleted by the server, because the "file delete" operation also fails.
   363 	TheTest.Printf(_L("\r\n"));
   399                 }
       
   400             }
       
   401         (void)TheFs.SetErrorCondition(KErrNone);
       
   402         TheDb.Close();
       
   403         TEST2(err, KErrNone);
       
   404         if( k != (KDbNameCnt - 1))
       
   405             {
       
   406             TEST(FileExists(dbName[k]));
       
   407             }
       
   408         err = RSqlDatabase::Delete(dbName[k]);
       
   409         TEST2(err, KErrNone);
       
   410         TheTest.Printf(_L("\r\n"));
       
   411 	    }
       
   412 	policy.Close();
   364 	}
   413 	}
   365 
   414 
   366 /**
   415 /**
   367 @SYMTestCaseID			SYSLIB-SQL-UT-3462
   416 @SYMTestCaseID			SYSLIB-SQL-UT-3462
   368 @SYMTestCaseDesc		Test for DEF105434 "SQL, persistent file I/O simulation, COMMIT problem".
   417 @SYMTestCaseDesc		Test for DEF105434 "SQL, persistent file I/O simulation, COMMIT problem".
   413 			}
   462 			}
   414 		(void)TheFs.SetErrorCondition(KErrNone);
   463 		(void)TheFs.SetErrorCondition(KErrNone);
   415 		stmt.Close();
   464 		stmt.Close();
   416 		TheDb.Close();
   465 		TheDb.Close();
   417 		//check the database content is the same as before the operation
   466 		//check the database content is the same as before the operation
   418 		TEST(CheckRecord(1, _L("Name")));
   467 		TEST(CheckRecord(KTestDbName, 1, _L("Name")));
   419 		}
   468 		}
   420 	(void)TheFs.SetErrorCondition(KErrNone);
   469 	(void)TheFs.SetErrorCondition(KErrNone);
   421 	TEST(err >= 0);
   470 	TEST(err >= 0);
   422 	TheDb.Close();
   471 	TheDb.Close();
   423 	//check the database content is the same as before the operation, after reopening the database.
   472 	//check the database content is the same as before the operation, after reopening the database.
   424 	TEST(CheckRecord(1, _L("Name")));
   473 	TEST(CheckRecord(KTestDbName, 1, _L("Name")));
   425 	err = RSqlDatabase::Delete(KTestDbName);
   474 	err = RSqlDatabase::Delete(KTestDbName);
   426 	TEST2(err, KErrNone);
   475 	TEST2(err, KErrNone);
   427 	TheTest.Printf(_L("\r\n"));
   476 	TheTest.Printf(_L("\r\n"));
   428 	}
   477 	}
   429 
   478 
   475 		stmt.Close();
   524 		stmt.Close();
   476 		if(err < 1)
   525 		if(err < 1)
   477 			{
   526 			{
   478 			TheDb.Close();//close the database to recover from the last error
   527 			TheDb.Close();//close the database to recover from the last error
   479 			//check that the database contains the "name" record that has been inserted before the file I/O failure test.
   528 			//check that the database contains the "name" record that has been inserted before the file I/O failure test.
   480 			TEST(CheckRecord(1, _L("Name")));
   529 			TEST(CheckRecord(KTestDbName, 1, _L("Name")));
   481 			}
   530 			}
   482 		else
   531 		else
   483 			{
   532 			{
   484 			TEST2(err, 1);
   533 			TEST2(err, 1);
   485 			//check the database content has been modified by the operation, without closing the database.
   534 			//check the database content has been modified by the operation, without closing the database.
   486 			TEST(CheckRecord(1, _L("Name"), EFalse));
   535 			TEST(CheckRecord(KTestDbName, 1, _L("Name"), EFalse));
   487 			TEST(CheckRecord(2, _L("Name2"), EFalse));
   536 			TEST(CheckRecord(KTestDbName, 2, _L("Name2"), EFalse));
   488 			TheDb.Close();
   537 			TheDb.Close();
   489 			}
   538 			}
   490 		}
   539 		}
   491 	(void)TheFs.SetErrorCondition(KErrNone);
   540 	(void)TheFs.SetErrorCondition(KErrNone);
   492 	TEST2(err, 1);
   541 	TEST2(err, 1);
   493 	//check the database content (transaction durability).
   542 	//check the database content (transaction durability).
   494 	TEST(CheckRecord(1, _L("Name")));
   543 	TEST(CheckRecord(KTestDbName, 1, _L("Name")));
   495 	TEST(CheckRecord(2, _L("Name2")));
   544 	TEST(CheckRecord(KTestDbName, 2, _L("Name2")));
   496 	(void)RSqlDatabase::Delete(KTestDbName);
   545 	(void)RSqlDatabase::Delete(KTestDbName);
   497 	TheTest.Printf(_L("\r\n"));
   546 	TheTest.Printf(_L("\r\n"));
   498 	}
   547 	}
   499 
   548 
   500 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   549 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////