persistentstorage/sql/TEST/t_sqlsecurity4.cpp
changeset 0 08ec8eefde2f
child 8 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_sqlsecurity4 application has capabilities allowing read/write 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_sqlsecurity4 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-1646
       
    90 @SYMTestCaseDesc		Testing database operations on a secure database.
       
    91 						The test application's capabilities allow read/write 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 ReadWriteDatabaseTest()
       
   100 	{
       
   101 	RSqlDatabase db;
       
   102 	TInt err = TheDb.Open(KTestDbName);
       
   103 	TEST2(err, KErrNone);
       
   104 	
       
   105 	//Attempt to modify the database schema
       
   106 	err = TheDb.Exec(_L("CREATE TABLE C(FFF TEXT)"));
       
   107 	TEST2(err, KErrPermissionDenied);
       
   108 	//Attempt to update the user data (but it includes a READ operation)
       
   109 	err = TheDb.Exec(_L("UPDATE A SET F1 = 11 WHERE F1 = 1"));
       
   110 	TEST(err >= 0);	
       
   111 	//Attempt to update the user data (unconditional UPDATE, no READ operations)
       
   112 	err = TheDb.Exec(_L("UPDATE A SET F1 = 11"));
       
   113 	TEST(err >= 0);	
       
   114 	//Attempt to delete the user data (but it includes a READ operation)
       
   115 	err = TheDb.Exec(_L("DELETE FROM B WHERE F2 = 2"));
       
   116 	TEST(err >= 0);	
       
   117 	//Attempt to delete the user data (unconditional DELETE, no READ operations)
       
   118 	err = TheDb.Exec(_L("DELETE FROM A"));
       
   119 	TEST(err >= 0);	
       
   120 	//Restore the deleted table A
       
   121 	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');"));
       
   122 	TEST(err >= 0);	
       
   123 	//Restore the deleted record in table B
       
   124 	err = TheDb.Exec(_L("INSERT INTO B(F2, F3, B2) VALUES(2, 'ABC', x'45454545454545454545');"));
       
   125 	TEST2(err, 1);
       
   126 	//Attempt to insert new user data
       
   127 	err = TheDb.Exec(_L("INSERT INTO B(F2, F3, B2) VALUES(6, 'GHI', x'47474747474747474747');"));
       
   128 	TEST2(err, 1);
       
   129 	//Attempt to read the user data
       
   130 	RSqlStatement stmt;
       
   131 	err = stmt.Prepare(TheDb, _L("SELECT A.F1 FROM B,A WHERE A.F1 = B.F2"));
       
   132 	TEST2(err, KErrNone);
       
   133 	//ColumnCount() has no capabilities assigned
       
   134 	TInt colCnt = stmt.ColumnCount();
       
   135 	TEST2(colCnt, 1);
       
   136 	//DeclaredColumnType() has no capabilities assigned
       
   137 	TSqlColumnType colType;
       
   138 	err = stmt.DeclaredColumnType(0, colType);
       
   139 	TEST2(err, KErrNone);
       
   140 	TEST2(colType, ESqlInt);
       
   141 	err = stmt.Next();
       
   142 	TEST2(err, KSqlAtRow);
       
   143 	RDebug::Print(_L("Value=%d\r\n"), stmt.ColumnInt(0));
       
   144 	err = stmt.Next();
       
   145 	TEST2(err, KSqlAtRow);
       
   146 	RDebug::Print(_L("Value=%d\r\n"), stmt.ColumnInt(0));
       
   147 	stmt.Close();
       
   148 	//Attempt to read the system data
       
   149 	err = stmt.Prepare(TheDb, _L("SELECT * FROM SQLITE_MASTER"));
       
   150 	TEST2(err, KErrNone);
       
   151 	err = stmt.Next();
       
   152 	TEST2(err, KSqlAtRow);
       
   153 	TPtrC p;
       
   154 	err = stmt.ColumnText(0, p);
       
   155 	TEST2(err, KErrNone);
       
   156 	RDebug::Print(_L("Value=%S\r\n"), &p);
       
   157 	stmt.Close();
       
   158 	
       
   159 	TheDb.Close();
       
   160 	}
       
   161 	
       
   162 /**
       
   163 @SYMTestCaseID			SYSLIB-SQL-UT-4097
       
   164 @SYMTestCaseDesc		Testing incremental blob reads and writes on a secure database.
       
   165 						The test application's capabilities allow read and write access to the blobs.
       
   166 						Verify that both reads and writes are allowed.
       
   167 @SYMTestPriority		Medium
       
   168 @SYMTestActions			Testing incremental blob reads and writes on a secure database.
       
   169 @SYMTestExpectedResults Test must not fail
       
   170 @SYMREQ					REQ5794
       
   171 */	
       
   172 void ReadWriteBlobTestL()
       
   173 	{
       
   174 	// Current database data:
       
   175 	// TABLE A: {1, x'41414141414141414141'}, {2, x'42424242424242424242'}, {3, x'43434343434343434343'}, {4, x'44444444444444444444'}
       
   176 	// TABLE B: {4, "DEF", x'46464646464646464646'} <- ROWID = 2, {2, "ABC", x'45454545454545454545'} <- ROWID = 3, {6, "GHI", x'47474747474747474747'} <- ROWID = 4
       
   177 
       
   178 	RSqlDatabase db;
       
   179 	TInt err = TheDb.Open(KTestDbName);
       
   180 	TEST2(err, KErrNone);
       
   181 			
       
   182 	// Attempt to read the blobs in tables A and B
       
   183 	RSqlBlobReadStream rdStrm;
       
   184 	CleanupClosePushL(rdStrm);
       
   185 	TBuf8<20> data;
       
   186 	TRAP(err, rdStrm.OpenL(TheDb, _L("A"), _L("B1"), 1));
       
   187 	TEST2(err, KErrNone);
       
   188 	TRAP(err, rdStrm.ReadL(data, 3));
       
   189 	TEST2(err, KErrNone);
       
   190 	TEST(data.Compare(_L8("AAA")) == 0);
       
   191 	rdStrm.Close();
       
   192 	TRAP(err, rdStrm.OpenL(TheDb, _L("B"), _L("B2"), 3));
       
   193 	TEST2(err, KErrNone);
       
   194 	TRAP(err, rdStrm.ReadL(data, 10));
       
   195 	TEST2(err, KErrNone);
       
   196 	TEST(data.Compare(_L8("EEEEEEEEEE")) == 0);
       
   197 	CleanupStack::PopAndDestroy(&rdStrm);	
       
   198 
       
   199 	HBufC8* wholeBuf = TSqlBlob::GetLC(TheDb, _L("A"), _L("B1"), 4);
       
   200 	TEST(wholeBuf->Des().Compare(_L8("DDDDDDDDDD")) == 0);	
       
   201 	CleanupStack::PopAndDestroy(wholeBuf);
       
   202 	wholeBuf = TSqlBlob::GetLC(TheDb, _L("B"), _L("B2"), 2);
       
   203 	TEST(wholeBuf->Des().Compare(_L8("FFFFFFFFFF")) == 0);	
       
   204 	CleanupStack::PopAndDestroy(wholeBuf);
       
   205 
       
   206 	HBufC8* buf = HBufC8::NewLC(10);	
       
   207 	TPtr8 bufPtr(buf->Des());	  
       
   208 	err = TSqlBlob::Get(TheDb, _L("A"), _L("B1"), bufPtr, 2);
       
   209 	TEST2(err, KErrNone); 
       
   210 	TEST(bufPtr.Compare(_L8("BBBBBBBBBB")) == 0);	
       
   211 	err = TSqlBlob::Get(TheDb, _L("B"), _L("B2"), bufPtr, 4);
       
   212 	TEST2(err, KErrNone); 
       
   213 	TEST(bufPtr.Compare(_L8("GGGGGGGGGG")) == 0);
       
   214 	CleanupStack::PopAndDestroy(buf); 
       
   215 	
       
   216 	// Attempt to write the blobs in tables A and B
       
   217 	RSqlBlobWriteStream wrStrm;
       
   218 	CleanupClosePushL(wrStrm);
       
   219 	TRAP(err, wrStrm.OpenL(TheDb, _L("A"), _L("B1"), 1));
       
   220 	TEST2(err, KErrNone);
       
   221 	TRAP(err, wrStrm.WriteL(_L8("ZZZ")));
       
   222 	TEST2(err, KErrNone);
       
   223 	wrStrm.Close();
       
   224 	TRAP(err, wrStrm.OpenL(TheDb, _L("B"), _L("B2"), 2));
       
   225 	TEST2(err, KErrNone);
       
   226 	TRAP(err, wrStrm.WriteL(_L8("XXXXXXXXX")));
       
   227 	TEST2(err, KErrNone);
       
   228 	CleanupStack::PopAndDestroy(&wrStrm);	
       
   229 
       
   230 	TRAP(err, TSqlBlob::SetL(TheDb, _L("A"), _L("B1"), _L8("UUUUUUUU"), 4));
       
   231 	TEST2(err, KErrNone);
       
   232 	TRAP(err, TSqlBlob::SetL(TheDb, _L("B"), _L("B2"), _L8("TT"), 2));
       
   233 	TEST2(err, KErrNone);
       
   234 	
       
   235 	// SQLite and system tables
       
   236 	
       
   237 	// Attempt to read from and write to the SQLite master table -
       
   238 	// reads should be permitted because read capability is enough for this, 
       
   239 	// writes should not be permitted because schema capability is required for this
       
   240 	CleanupClosePushL(rdStrm);
       
   241 	TRAP(err, rdStrm.OpenL(TheDb, _L("sqlite_master"), _L("tbl_name"), 1)); // TEXT column
       
   242 	TEST2(err, KErrNone);
       
   243 	TRAP(err, rdStrm.ReadL(data, 1));
       
   244 	TEST2(err, KErrNone);
       
   245 	CleanupStack::PopAndDestroy(&rdStrm); 	
       
   246 
       
   247 	wholeBuf = TSqlBlob::GetLC(TheDb, _L("sqlite_master"), _L("tbl_name"), 1);
       
   248 	TEST(wholeBuf->Length() > 0);	
       
   249 	CleanupStack::PopAndDestroy(wholeBuf);  	
       
   250 
       
   251 	buf = HBufC8::NewLC(100);
       
   252 	bufPtr.Set(buf->Des());	 	  
       
   253 	err = TSqlBlob::Get(TheDb, _L("sqlite_master"), _L("tbl_name"), bufPtr, 1);
       
   254 	TEST2(err, KErrNone); 
       
   255 	TEST(bufPtr.Length() > 0);	
       
   256 	CleanupStack::PopAndDestroy(buf); 
       
   257 	
       
   258 	CleanupClosePushL(wrStrm);
       
   259 	TRAP(err, wrStrm.OpenL(TheDb, _L("sqlite_master"), _L("tbl_name"), 1));
       
   260 	TEST2(err, KErrPermissionDenied);
       
   261 	CleanupStack::PopAndDestroy(&wrStrm);	
       
   262 
       
   263 	TRAP(err, TSqlBlob::SetL(TheDb, _L("sqlite_master"), _L("tbl_name"), _L8("VVVV"), 1));
       
   264 	TEST2(err, KErrPermissionDenied);
       
   265 
       
   266 	// Attempt to read from and write to the system tables - neither reads nor writes should be permitted
       
   267 	CleanupClosePushL(rdStrm);
       
   268 	TRAP(err, rdStrm.OpenL(TheDb, _L("symbian_security"), _L("PolicyData"), 1)); // BLOB column
       
   269 	TEST2(err, KErrPermissionDenied);
       
   270 	CleanupStack::PopAndDestroy(&rdStrm);	
       
   271 
       
   272 	TRAP(err, wholeBuf = TSqlBlob::GetLC(TheDb, _L("symbian_security"), _L("PolicyData"), 1));
       
   273 	TEST2(err, KErrPermissionDenied);
       
   274 
       
   275 	buf = HBufC8::NewLC(100);	
       
   276 	bufPtr.Set(buf->Des());	  
       
   277 	err = TSqlBlob::Get(TheDb, _L("symbian_security"), _L("PolicyData"), bufPtr, 1);
       
   278 	TEST2(err, KErrPermissionDenied); 
       
   279 	CleanupStack::PopAndDestroy(buf); 
       
   280 	
       
   281 	CleanupClosePushL(wrStrm);
       
   282 	TRAP(err, wrStrm.OpenL(TheDb, _L("symbian_security"), _L("PolicyData"), 1));
       
   283 	TEST2(err, KErrPermissionDenied);
       
   284 	CleanupStack::PopAndDestroy(&wrStrm);	
       
   285 
       
   286 	TRAP(err, TSqlBlob::SetL(TheDb, _L("symbian_security"), _L("PolicyData"), _L8("VVVV"), 1));
       
   287 	TEST2(err, KErrPermissionDenied);
       
   288 	
       
   289 	TheDb.Close();
       
   290 	}
       
   291 	
       
   292 void DoTestsL()
       
   293 	{
       
   294 	TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1646 Read-write database access test "));
       
   295 	ReadWriteDatabaseTest();
       
   296 	
       
   297 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4097 Read-write blob access test"));
       
   298 	ReadWriteBlobTestL();
       
   299 	
       
   300 	RestoreOriginalDb(); // the same db is used by the other t_security test exe's
       
   301 	}
       
   302 
       
   303 TInt E32Main()
       
   304 	{
       
   305 	TheTest.Title();
       
   306 	
       
   307 	CTrapCleanup* tc = CTrapCleanup::New();
       
   308 	
       
   309 	__UHEAP_MARK;
       
   310 		
       
   311 	TRAPD(err, DoTestsL());
       
   312 	TEST2(err, KErrNone);
       
   313 
       
   314 	__UHEAP_MARKEND;
       
   315 	
       
   316 	TheTest.End();
       
   317 	TheTest.Close();
       
   318 	
       
   319 	delete tc;
       
   320 
       
   321 	User::Heap().Check();
       
   322 	return KErrNone;
       
   323 	}