persistentstorage/sql/TEST/t_sqlcorrupt.cpp
branchRCL_3
changeset 15 fcc16690f446
equal deleted inserted replaced
14:04ec7606545c 15:fcc16690f446
       
     1 // Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // 
       
    15 //
       
    16 #include <e32test.h>
       
    17 #include <bautils.h>
       
    18 #include <sqldb.h>
       
    19 
       
    20 ///////////////////////////////////////////////////////////////////////////////////////
       
    21 
       
    22 RSqlDatabase TheDb;
       
    23 RTest TheTest(_L("t_sqlcorrupt test"));
       
    24 
       
    25 _LIT(KTestDir, "c:\\test\\");
       
    26 
       
    27 _LIT(KDbName, "c:[08770000]t_sqlcorrupt.db");
       
    28 _LIT(KFullDbName, "c:\\private\\10281E17\\[08770000]t_sqlcorrupt.db");
       
    29 
       
    30 _LIT(KDbName2, "c:[08770000]t_sqlcorrupt2.db");
       
    31 _LIT(KFullDbName2, "c:\\private\\10281E17\\[08770000]t_sqlcorrupt2.db");
       
    32 
       
    33 RFs TheFs;
       
    34 
       
    35 ///////////////////////////////////////////////////////////////////////////////////////
       
    36 
       
    37 void DestroyTestEnv()
       
    38 	{
       
    39 	TheDb.Close();
       
    40 	(void)RSqlDatabase::Delete(KDbName2);
       
    41 	(void)RSqlDatabase::Delete(KDbName);
       
    42 	TheFs.Close();
       
    43 	}
       
    44 
       
    45 ///////////////////////////////////////////////////////////////////////////////////////
       
    46 //Test macros and functions
       
    47 void Check1(TInt aValue, TInt aLine)
       
    48 	{
       
    49 	if(!aValue)
       
    50 		{
       
    51 		DestroyTestEnv();
       
    52 		RDebug::Print(_L("*** Boolean expression evaluated to false. Line %d\r\n"), aLine);
       
    53 		TheTest(EFalse, aLine);
       
    54 		}
       
    55 	}
       
    56 void Check2(TInt aValue, TInt aExpected, TInt aLine)
       
    57 	{
       
    58 	if(aValue != aExpected)
       
    59 		{
       
    60 		DestroyTestEnv();
       
    61 		RDebug::Print(_L("*** Line %d, Expected error: %d, got: %d\r\n"), aLine, aExpected, aValue);
       
    62 		TheTest(EFalse, aLine);
       
    63 		}
       
    64 	}
       
    65 #define TEST(arg) ::Check1((arg), __LINE__)
       
    66 #define TEST2(aValue, aExpected) ::Check2(aValue, aExpected, __LINE__)
       
    67 
       
    68 ///////////////////////////////////////////////////////////////////////////////////////
       
    69 
       
    70 enum TDbEncoding
       
    71 	{
       
    72 	EDbEncUtf16,
       
    73 	EDbEncUtf8,
       
    74 	};
       
    75 
       
    76 void DoCorruptedSecureDbTest(TDbEncoding aEncoding)
       
    77 	{
       
    78 	(void)RSqlDatabase::Delete(KDbName);
       
    79 	
       
    80 	RSqlSecurityPolicy policy;
       
    81 	TInt err = policy.Create(TSecurityPolicy::EAlwaysPass);
       
    82 	TEST2(err, KErrNone);
       
    83 	
       
    84 	err = policy.SetDbPolicy(RSqlSecurityPolicy::ESchemaPolicy, TSecurityPolicy::EAlwaysPass);
       
    85 	TEST2(err, KErrNone);
       
    86 	err = policy.SetDbPolicy(RSqlSecurityPolicy::EWritePolicy, TSecurityPolicy::EAlwaysPass);
       
    87 	TEST2(err, KErrNone);
       
    88 	err = policy.SetDbPolicy(RSqlSecurityPolicy::EReadPolicy, TSecurityPolicy::EAlwaysPass);
       
    89 	TEST2(err, KErrNone);
       
    90 	
       
    91 	err = policy.SetPolicy(RSqlSecurityPolicy::ETable, _L("A"), RSqlSecurityPolicy::EWritePolicy, TSecurityPolicy::EAlwaysPass);
       
    92 	TEST2(err, KErrNone);
       
    93 	err = policy.SetPolicy(RSqlSecurityPolicy::ETable, _L("A"), RSqlSecurityPolicy::EReadPolicy, TSecurityPolicy::EAlwaysPass);
       
    94 	TEST2(err, KErrNone);
       
    95 	
       
    96 	if(aEncoding == EDbEncUtf16)
       
    97 		{
       
    98 		err = TheDb.Create(KDbName, policy);
       
    99 		}
       
   100 	else
       
   101 		{
       
   102 		_LIT8(KConfig, "encoding = \"UTF-8\"");
       
   103 		err = TheDb.Create(KDbName, policy, &KConfig);
       
   104 		}
       
   105 	TEST2(err, KErrNone);
       
   106 	err = TheDb.Exec(_L("CREATE TABLE A(I INTEGER); INSERT INTO A VALUES(10)"));
       
   107 	TEST(err >= 0);
       
   108 	TheDb.Close();
       
   109 	policy.Close();
       
   110 
       
   111 	CFileMan* fm = NULL;
       
   112 	TRAP(err, fm = CFileMan::NewL(TheFs));
       
   113 	TEST2(err, KErrNone);
       
   114 	
       
   115 	//Make a copy of the database
       
   116 	err = fm->Copy(KFullDbName, KFullDbName2);
       
   117 	TEST2(err, KErrNone);
       
   118 	//Get the database file size and calculate the iterations count.
       
   119 	TEntry entry;
       
   120 	err = TheFs.Entry(KFullDbName, entry);
       
   121 	TEST2(err, KErrNone);
       
   122 	const TInt KCorruptBlockLen = 19;
       
   123 	const TInt KIterationCnt = entry.iSize / KCorruptBlockLen;
       
   124 	//
       
   125 	TBuf8<KCorruptBlockLen> invalidData;
       
   126 	invalidData.SetLength(KCorruptBlockLen);
       
   127 	invalidData.Fill(TChar(0xCC));
       
   128 	//
       
   129 	for(TInt i=0;i<KIterationCnt;++i)
       
   130 		{
       
   131 		TheTest.Printf(_L("% 4d\r"), i + 1);
       
   132 		//Corrupt the database
       
   133 		err = fm->Copy(KFullDbName2, KFullDbName);
       
   134 		TEST2(err, KErrNone);
       
   135 		RFile file;
       
   136 		err = file.Open(TheFs, KFullDbName, EFileRead | EFileWrite);
       
   137 		TEST2(err, KErrNone);
       
   138 		err = file.Write(i * KCorruptBlockLen, invalidData);
       
   139 		TEST2(err, KErrNone);
       
   140 		file.Close();
       
   141 		//Try to open the database and read the record
       
   142 		TBool testPassed = EFalse;
       
   143 		err = TheDb.Open(KDbName);
       
   144 		if(err == KErrNone)
       
   145 			{
       
   146 			RSqlStatement stmt;
       
   147 			err = stmt.Prepare(TheDb, _L("SELECT I FROM A"));
       
   148 			if(err == KErrNone)
       
   149 				{
       
   150 				err = stmt.Next();
       
   151 				if(err == KSqlAtRow)
       
   152 					{
       
   153 					TInt val = stmt.ColumnInt(0);
       
   154 					if(val == 10)
       
   155 						{
       
   156 						testPassed = ETrue;
       
   157 						err = KErrNone;
       
   158 						}
       
   159 					else
       
   160 						{
       
   161 						err = KErrGeneral;
       
   162 						}
       
   163 					}
       
   164 				stmt.Close();
       
   165 				}
       
   166 			}
       
   167 
       
   168 		TheDb.Close();
       
   169 		(void)RSqlDatabase::Delete(KDbName);
       
   170 		TheTest.Printf(_L("Iteration % 4d, err=%d\r\n"), i + 1, err);
       
   171 		if(!testPassed)
       
   172 			{
       
   173 			TEST(err != KErrNone);
       
   174 			}
       
   175 		}//end of - for(TInt i=0;i<KIterationCnt;++i)
       
   176 
       
   177 	delete fm;
       
   178 	TheTest.Printf(_L("\r\n"));
       
   179 	}
       
   180 
       
   181 /**
       
   182 @SYMTestCaseID          PDS-SQL-CT-4202
       
   183 @SYMTestCaseDesc        Invalid UTF16 encoded secure database test.
       
   184 @SYMTestPriority        High
       
   185 @SYMTestActions         The test creates 16-bit encoded secure database with one table and one record.
       
   186 						Then the test simulates a database corruption by writing 19 bytes with random values
       
   187 						from "pos" to "pos + 19", where "pos" is a valid db file position, incremented by 19
       
   188 						at the end of each test iteration.
       
   189 @SYMTestExpectedResults Test must not fail
       
   190 */  
       
   191 void CorruptedSecureDbTest16()
       
   192 	{
       
   193 	DoCorruptedSecureDbTest(EDbEncUtf16);
       
   194 	}
       
   195 
       
   196 /**
       
   197 @SYMTestCaseID          PDS-SQL-CT-4202
       
   198 @SYMTestCaseDesc        Invalid UTF8 encoded secure database test.
       
   199 @SYMTestPriority        High
       
   200 @SYMTestActions         The test creates 8-bit encoded secure database with one table and one record.
       
   201 						Then the test simulates a database corruption by writing 19 bytes with random values
       
   202 						from "pos" to "pos + 19", where "pos" is a valid db file position, incremented by 19
       
   203 						at the end of each test iteration.
       
   204 @SYMTestExpectedResults Test must not fail
       
   205 */  
       
   206 void CorruptedSecureDbTest8()
       
   207 	{
       
   208 	DoCorruptedSecureDbTest(EDbEncUtf8);
       
   209 	}
       
   210 
       
   211 void CreateTestEnv()
       
   212     {
       
   213     TInt err = TheFs.Connect();
       
   214     TEST2(err, KErrNone);
       
   215 
       
   216     err = TheFs.MkDir(KTestDir);
       
   217     TEST(err == KErrNone || err == KErrAlreadyExists);
       
   218 
       
   219     err = TheFs.CreatePrivatePath(EDriveC);
       
   220     TEST(err == KErrNone || err == KErrAlreadyExists);
       
   221     }
       
   222 
       
   223 void DoTestsL()
       
   224 	{
       
   225 	TheTest.Start(_L("@SYMTestCaseID:PDS-SQL-CT-4202 Corrupted UTF16 encoded secure database test"));
       
   226 	CorruptedSecureDbTest16();
       
   227 	
       
   228 	TheTest.Next(_L("@SYMTestCaseID:PDS-SQL-CT-4203 Corrupted UTF8 encoded secure database test"));
       
   229 	CorruptedSecureDbTest8();
       
   230 	}
       
   231 
       
   232 TInt E32Main()
       
   233 	{
       
   234 	TheTest.Title();
       
   235 	
       
   236 	CTrapCleanup* tc = CTrapCleanup::New();
       
   237 	TheTest(tc != NULL);
       
   238 	
       
   239 	__UHEAP_MARK;
       
   240 		
       
   241 	CreateTestEnv();
       
   242 	TRAPD(err, DoTestsL());
       
   243 	DestroyTestEnv();
       
   244 	TEST2(err, KErrNone);
       
   245 
       
   246 	__UHEAP_MARKEND;
       
   247 	
       
   248 	TheTest.End();
       
   249 	TheTest.Close();
       
   250 	
       
   251 	delete tc;
       
   252 
       
   253 	User::Heap().Check();
       
   254 	return KErrNone;
       
   255 	}