persistentstorage/sql/TEST/t_sqlsecurity3.cpp
changeset 0 08ec8eefde2f
child 10 fa9941cf3867
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // t_sqlsecurity3 application has capabilities allowing write-only access to the test database
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <e32test.h>
       
    19 #include <bautils.h>
       
    20 #include <sqldb.h>
       
    21 
       
    22 ///////////////////////////////////////////////////////////////////////////////////////
       
    23 //The test database has:
       
    24 //  SCHEMA database policy: ECapabilityReadDeviceData, ECapabilityWriteUserData, ECapabilityReadUserData
       
    25 //  WRITE database policy:  ECapabilityWriteUserData
       
    26 //  READ database policy:   ECapabilityReadUserData
       
    27 //
       
    28 //Database tables:
       
    29 //  TABLE A(F1 INTEGER, B1 BLOB)
       
    30 //  TABLE B(F2 INTEGER, F3 TEXT, B2 BLOB)
       
    31 //
       
    32 //Database data:
       
    33 //  TABLE A: {1, x'41414141414141414141'}, {2, x'42424242424242424242'}, {3, x'43434343434343434343'}, {4, x'44444444444444444444'}
       
    34 //  TABLE B: {2, "ABC", x'45454545454545454545'}, {4, "DEF", x'46464646464646464646'}
       
    35 
       
    36 ///////////////////////////////////////////////////////////////////////////////////////
       
    37 
       
    38 #define UNUSED_VAR(a) (a) = (a)
       
    39 
       
    40 RSqlDatabase TheDb;
       
    41 RTest TheTest(_L("t_sqlsecurity3 test"));
       
    42 
       
    43 _LIT(KTestDbName, "c:[21212125]t_ab.db");
       
    44 
       
    45 ///////////////////////////////////////////////////////////////////////////////////////
       
    46 //Restore original test database function
       
    47 void RestoreOriginalDb()
       
    48 	{
       
    49 	TheDb.Close();
       
    50 	TheDb.Open(KTestDbName);
       
    51 	
       
    52 	// Delete and restore the content of table A (unconditional DELETE, no READ operations)
       
    53 	TheDb.Exec(_L("DELETE FROM A"));
       
    54 	TheDb.Exec(_L("INSERT INTO A(F1,B1) VALUES(1,x'41414141414141414141');INSERT INTO A(F1,B1) VALUES(2,x'42424242424242424242');INSERT INTO A(F1,B1) VALUES(3,x'43434343434343434343');INSERT INTO A(F1,B1) VALUES(4,x'44444444444444444444');"));
       
    55 
       
    56 	// Delete and restore the content of table B (unconditional DELETE, no READ operations)
       
    57 	TheDb.Exec(_L("DELETE FROM B"));
       
    58 	TheDb.Exec(_L("INSERT INTO B(F2,F3,B2) VALUES(2, 'ABC',x'45454545454545454545');INSERT INTO B(F2,F3,B2) VALUES(4,'DEF',x'46464646464646464646');"));
       
    59 
       
    60 	TheDb.Close();	
       
    61 	}
       
    62 
       
    63 ///////////////////////////////////////////////////////////////////////////////////////
       
    64 //Test macros and functions
       
    65 void Check1(TInt aValue, TInt aLine)
       
    66 	{
       
    67 	if(!aValue)
       
    68 		{
       
    69 		RestoreOriginalDb();
       
    70 		RDebug::Print(_L("*** Line %d\r\n"), aLine);
       
    71 		TheTest(EFalse, aLine);
       
    72 		}
       
    73 	}
       
    74 void Check2(TInt aValue, TInt aExpected, TInt aLine)
       
    75 	{
       
    76 	if(aValue != aExpected)
       
    77 		{
       
    78 		RestoreOriginalDb();
       
    79 		RDebug::Print(_L("*** Line %d, Expected error: %d, got: %d\r\n"), aLine, aExpected, aValue);
       
    80 		TheTest(EFalse, aLine);
       
    81 		}
       
    82 	}
       
    83 #define TEST(arg) ::Check1((arg), __LINE__)
       
    84 #define TEST2(aValue, aExpected) ::Check2(aValue, aExpected, __LINE__)
       
    85 
       
    86 ///////////////////////////////////////////////////////////////////////////////////////
       
    87 
       
    88 /**
       
    89 @SYMTestCaseID			SYSLIB-SQL-CT-1645
       
    90 @SYMTestCaseDesc		Testing database operations on a secure database.
       
    91 						The test application's capabilities allow write-only access to the test secure database.
       
    92 						Verify that any other kind of a database operation will fail with KErrPermissionDenied error.
       
    93 @SYMTestPriority		High
       
    94 @SYMTestActions			Testing database operations on a secure database.
       
    95 @SYMTestExpectedResults Test must not fail
       
    96 @SYMREQ					REQ5792
       
    97                         REQ5793
       
    98 */	
       
    99 void WriteOnlyDatabaseTest()
       
   100 	{
       
   101 	TInt err = TheDb.Open(KTestDbName);
       
   102 	TEST2(err, KErrNone);
       
   103 	
       
   104 	//Attempt to modify the database schema
       
   105 	err = TheDb.Exec(_L("CREATE TABLE C(FFF TEXT)"));
       
   106 	TEST2(err, KErrPermissionDenied);
       
   107 	//Attempt to update the user data (but it includes a READ operation)
       
   108 	err = TheDb.Exec(_L("UPDATE A SET F1 = 11 WHERE F1 = 1"));
       
   109 	TEST2(err, KErrPermissionDenied);
       
   110 	//Attempt to update the user data (unconditional UPDATE, no READ operations)
       
   111 	err = TheDb.Exec(_L("UPDATE A SET F1 = 11"));
       
   112 	TEST(err >= 0);	
       
   113 	//Attempt to delete the user data (but it includes a READ operation)
       
   114 	err = TheDb.Exec(_L("DELETE FROM B WHERE F2 = 2"));
       
   115 	TEST2(err, KErrPermissionDenied);
       
   116 	//Attempt to delete the user data (unconditional DELETE, no READ operations)
       
   117 	err = TheDb.Exec(_L("DELETE FROM A"));
       
   118 	TEST(err >= 0);	
       
   119 	//Restore the deleted table A
       
   120 	err = TheDb.Exec(_L("INSERT INTO A(F1,B1) VALUES(1,x'41414141414141414141');INSERT INTO A(F1,B1) VALUES(2,x'42424242424242424242');INSERT INTO A(F1,B1) VALUES(3,x'43434343434343434343');INSERT INTO A(F1,B1) VALUES(4,x'44444444444444444444');"));
       
   121 	TEST(err >= 0);	
       
   122 	//Attempt to insert new user data
       
   123 	err = TheDb.Exec(_L("INSERT INTO B(F2, F3, B2) VALUES(22, 'AAA', x'47474747474747474747')"));
       
   124 	TEST2(err, 1);
       
   125 	//Attempt to change the isolation level.
       
   126 	err = TheDb.SetIsolationLevel(RSqlDatabase::ESerializable);	
       
   127 	TEST2(err, KErrNone);
       
   128 	err = TheDb.SetIsolationLevel(RSqlDatabase::EReadUncommitted);	
       
   129 	TEST2(err, KErrNone);
       
   130 	//Attempt to read the user data
       
   131 	RSqlStatement stmt;
       
   132 	err = stmt.Prepare(TheDb, _L("SELECT A.F1 FROM B,A WHERE A.F1 = B.F2"));
       
   133 	TEST2(err, KErrPermissionDenied);	
       
   134 	//Attempt to read the system data
       
   135 	err = stmt.Prepare(TheDb, _L("SELECT * FROM SQLITE_MASTER"));
       
   136 	TEST2(err, KErrNone);
       
   137 	err = stmt.Next();
       
   138 	TEST2(err, KSqlAtRow);
       
   139 	TPtrC p;
       
   140 	err = stmt.ColumnText(0, p);
       
   141 	TEST2(err, KErrNone);
       
   142 	RDebug::Print(_L("Value=%S\r\n"), &p);
       
   143 	stmt.Close();
       
   144 	
       
   145 	TheDb.Close();
       
   146 	}
       
   147 	
       
   148 /**
       
   149 @SYMTestCaseID			SYSLIB-SQL-UT-4096
       
   150 @SYMTestCaseDesc		Testing incremental blob writes on a secure database.
       
   151 						The test application's capabilities allow write-only access to the blobs.
       
   152 						Verify that any attempt to read a blob will fail with KErrPermissionDenied.
       
   153 @SYMTestPriority		High
       
   154 @SYMTestActions			Testing incremental blob writes on a secure database.
       
   155 @SYMTestExpectedResults Test must not fail
       
   156 @SYMREQ					REQ5794
       
   157 */	
       
   158 void WriteOnlyBlobTestL()
       
   159 	{
       
   160 	TInt err = TheDb.Open(KTestDbName);
       
   161 	TEST2(err, KErrNone);
       
   162 			
       
   163 	// Attempt to write the blobs in tables A and B
       
   164 	RSqlBlobWriteStream wrStrm;
       
   165 	CleanupClosePushL(wrStrm);
       
   166 	TRAP(err, wrStrm.OpenL(TheDb, _L("A"), _L("B1"), 2));
       
   167 	TEST2(err, KErrNone);
       
   168 	TRAP(err, wrStrm.WriteL(_L8("YYYYYYY")));
       
   169 	TEST2(err, KErrNone);
       
   170 	wrStrm.Close();
       
   171 	TRAP(err, wrStrm.OpenL(TheDb, _L("B"), _L("B2"), 1));
       
   172 	TEST2(err, KErrNone);
       
   173 	TRAP(err, wrStrm.WriteL(_L8("XXXXXXXXX")));
       
   174 	TEST2(err, KErrNone);
       
   175 	CleanupStack::PopAndDestroy(&wrStrm);	
       
   176 
       
   177 	TRAP(err, TSqlBlob::SetL(TheDb, _L("A"), _L("B1"), _L8("UUUUUUUU"), 4));
       
   178 	TEST2(err, KErrNone);
       
   179 	TRAP(err, TSqlBlob::SetL(TheDb, _L("B"), _L("B2"), _L8("SSS"), 2));
       
   180 	TEST2(err, KErrNone);
       
   181 	
       
   182 	// Attempt to read from the blobs in tables A and B
       
   183 	RSqlBlobReadStream rdStrm;
       
   184 	CleanupClosePushL(rdStrm);
       
   185 	TRAP(err, rdStrm.OpenL(TheDb, _L("A"), _L("B1"), 1));
       
   186 	TEST2(err, KErrPermissionDenied);
       
   187 	rdStrm.Close();
       
   188 	TRAP(err, rdStrm.OpenL(TheDb, _L("B"), _L("B2"), 1));
       
   189 	TEST2(err, KErrPermissionDenied);
       
   190 	CleanupStack::PopAndDestroy(&rdStrm);	
       
   191 
       
   192 	HBufC8* wholeBuf = NULL;
       
   193 	TRAP(err, wholeBuf = TSqlBlob::GetLC(TheDb, _L("A"), _L("B1"), 1));
       
   194 	TEST2(err, KErrPermissionDenied);
       
   195 	TRAP(err, wholeBuf = TSqlBlob::GetLC(TheDb, _L("B"), _L("B2"), 1));
       
   196 	TEST2(err, KErrPermissionDenied);
       
   197 
       
   198 	HBufC8* buf = HBufC8::NewLC(10);	
       
   199 	TPtr8 bufPtr(buf->Des());	  
       
   200 	err = TSqlBlob::Get(TheDb, _L("A"), _L("B1"), bufPtr, 2);
       
   201 	TEST2(err, KErrPermissionDenied); 
       
   202 	err = TSqlBlob::Get(TheDb, _L("B"), _L("B2"), bufPtr, 1);
       
   203 	TEST2(err, KErrPermissionDenied); 
       
   204 	CleanupStack::PopAndDestroy(buf); 
       
   205 	
       
   206 	// SQLite and system tables
       
   207 	
       
   208 	// Attempt to read from and write to the SQLite master table -
       
   209 	// reads should be permitted because write capability is enough for this, 
       
   210 	// writes should not be permitted because schema capability is required for this
       
   211 	CleanupClosePushL(rdStrm);
       
   212 	TRAP(err, rdStrm.OpenL(TheDb, _L("sqlite_master"), _L("tbl_name"), 1)); // TEXT column
       
   213 	TEST2(err, KErrNone);
       
   214 	TBuf8<20> data;
       
   215 	TRAP(err, rdStrm.ReadL(data, 1));
       
   216 	TEST2(err, KErrNone);
       
   217 	CleanupStack::PopAndDestroy(&rdStrm);	
       
   218 
       
   219 	wholeBuf = TSqlBlob::GetLC(TheDb, _L("sqlite_master"), _L("tbl_name"), 1);
       
   220 	TEST(wholeBuf->Length() > 0);	
       
   221 	CleanupStack::PopAndDestroy(wholeBuf); 	
       
   222 
       
   223 	buf = HBufC8::NewLC(100);
       
   224 	bufPtr.Set(buf->Des());	 	  
       
   225 	err = TSqlBlob::Get(TheDb, _L("sqlite_master"), _L("tbl_name"), bufPtr, 1);
       
   226 	TEST2(err, KErrNone); 
       
   227 	TEST(bufPtr.Length() > 0);	
       
   228 	CleanupStack::PopAndDestroy(buf); 
       
   229 	
       
   230 	CleanupClosePushL(wrStrm);
       
   231 	TRAP(err, wrStrm.OpenL(TheDb, _L("sqlite_master"), _L("tbl_name"), 1));
       
   232 	TEST2(err, KErrPermissionDenied);
       
   233 	CleanupStack::PopAndDestroy(&wrStrm);	
       
   234 
       
   235 	TRAP(err, TSqlBlob::SetL(TheDb, _L("sqlite_master"), _L("tbl_name"), _L8("VVVV"), 1));
       
   236 	TEST2(err, KErrPermissionDenied);
       
   237 
       
   238 	// Attempt to read from and write to the system tables - neither reads nor writes should be permitted
       
   239 	CleanupClosePushL(rdStrm);
       
   240 	TRAP(err, rdStrm.OpenL(TheDb, _L("symbian_security"), _L("PolicyData"), 1)); // BLOB column
       
   241 	TEST2(err, KErrPermissionDenied);
       
   242 	CleanupStack::PopAndDestroy(&rdStrm);	
       
   243 
       
   244 	TRAP(err, wholeBuf = TSqlBlob::GetLC(TheDb, _L("symbian_security"), _L("PolicyData"), 1));
       
   245 	TEST2(err, KErrPermissionDenied);
       
   246 
       
   247 	buf = HBufC8::NewLC(100);	
       
   248 	bufPtr.Set(buf->Des());	  
       
   249 	err = TSqlBlob::Get(TheDb, _L("symbian_security"), _L("PolicyData"), bufPtr, 1);
       
   250 	TEST2(err, KErrPermissionDenied); 
       
   251 	CleanupStack::PopAndDestroy(buf); 
       
   252 	
       
   253 	CleanupClosePushL(wrStrm);
       
   254 	TRAP(err, wrStrm.OpenL(TheDb, _L("symbian_security"), _L("PolicyData"), 1));
       
   255 	TEST2(err, KErrPermissionDenied);
       
   256 	CleanupStack::PopAndDestroy(&wrStrm);	
       
   257 
       
   258 	TRAP(err, TSqlBlob::SetL(TheDb, _L("symbian_security"), _L("PolicyData"), _L8("VVVV"), 1));
       
   259 	TEST2(err, KErrPermissionDenied);
       
   260 	
       
   261 	TheDb.Close();
       
   262 	}
       
   263 	
       
   264 void DoTestsL()
       
   265 	{
       
   266 	TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1645 Write-only database access test "));
       
   267 	WriteOnlyDatabaseTest();
       
   268 	
       
   269 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4096 Write-only blob access test"));
       
   270 	WriteOnlyBlobTestL();
       
   271 	
       
   272 	RestoreOriginalDb(); // the same db is used by the other t_security test exe's
       
   273 	}
       
   274 
       
   275 TInt E32Main()
       
   276 	{
       
   277 	TheTest.Title();
       
   278 	
       
   279 	CTrapCleanup* tc = CTrapCleanup::New();
       
   280 	
       
   281 	__UHEAP_MARK;
       
   282 		
       
   283 	TRAPD(err, DoTestsL());
       
   284 	TEST2(err, KErrNone);
       
   285 
       
   286 	__UHEAP_MARKEND;
       
   287 	
       
   288 	TheTest.End();
       
   289 	TheTest.Close();
       
   290 	
       
   291 	delete tc;
       
   292 
       
   293 	User::Heap().Check();
       
   294 	return KErrNone;
       
   295 	}