1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/sql/TEST/t_sqlsecurity2.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,369 @@
1.4 +// Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// t_sqlsecurity2 application has capabilities allowing read-only access to the test database
1.18 +//
1.19 +//
1.20 +
1.21 +#include <e32test.h>
1.22 +#include <bautils.h>
1.23 +#include <sqldb.h>
1.24 +
1.25 +///////////////////////////////////////////////////////////////////////////////////////
1.26 +//The test database has:
1.27 +// SCHEMA database policy: ECapabilityReadDeviceData, ECapabilityWriteUserData, ECapabilityReadUserData
1.28 +// WRITE database policy: ECapabilityWriteUserData
1.29 +// READ database policy: ECapabilityReadUserData
1.30 +//
1.31 +//Database tables:
1.32 +// TABLE A(F1 INTEGER, B1 BLOB)
1.33 +// TABLE B(F2 INTEGER, F3 TEXT, B2 BLOB)
1.34 +//
1.35 +//Database data:
1.36 +// TABLE A: {1, x'41414141414141414141'}, {2, x'42424242424242424242'}, {3, x'43434343434343434343'}, {4, x'44444444444444444444'}
1.37 +// TABLE B: {2, "ABC", x'45454545454545454545'}, {4, "DEF", x'46464646464646464646'}
1.38 +
1.39 +///////////////////////////////////////////////////////////////////////////////////////
1.40 +
1.41 +#define UNUSED_VAR(a) (a) = (a)
1.42 +
1.43 +RTest TheTest(_L("t_sqlsecurity2 test"));
1.44 +RSqlDatabase TheDb;
1.45 +
1.46 +_LIT(KTestDbName, "c:[21212125]t_ab.db");
1.47 +_LIT(KTestDbName2, "c:\\test\\t_sqlsecurity2_2.db");
1.48 +
1.49 +///////////////////////////////////////////////////////////////////////////////////////
1.50 +
1.51 +void DeleteTestDb()
1.52 + {
1.53 + TheDb.Close();
1.54 + (void)RSqlDatabase::Delete(KTestDbName2);
1.55 + }
1.56 +
1.57 +///////////////////////////////////////////////////////////////////////////////////////
1.58 +///////////////////////////////////////////////////////////////////////////////////////
1.59 +//Test macros and functions
1.60 +void Check1(TInt aValue, TInt aLine)
1.61 + {
1.62 + if(!aValue)
1.63 + {
1.64 + DeleteTestDb();
1.65 + RDebug::Print(_L("*** Line %d\r\n"), aLine);
1.66 + TheTest(EFalse, aLine);
1.67 + }
1.68 + }
1.69 +void Check2(TInt aValue, TInt aExpected, TInt aLine)
1.70 + {
1.71 + if(aValue != aExpected)
1.72 + {
1.73 + DeleteTestDb();
1.74 + RDebug::Print(_L("*** Line %d, Expected error: %d, got: %d\r\n"), aLine, aExpected, aValue);
1.75 + TheTest(EFalse, aLine);
1.76 + }
1.77 + }
1.78 +#define TEST(arg) ::Check1((arg), __LINE__)
1.79 +#define TEST2(aValue, aExpected) ::Check2(aValue, aExpected, __LINE__)
1.80 +
1.81 +///////////////////////////////////////////////////////////////////////////////////////
1.82 +
1.83 +/**
1.84 +@SYMTestCaseID SYSLIB-SQL-CT-1644
1.85 +@SYMTestCaseDesc Testing database operations on a secure database.
1.86 + The test application's capabilities allow read-only access to the test secure database.
1.87 + Verify that any other kind of a database operation will fail with KErrPermissionDenied error.
1.88 +@SYMTestPriority High
1.89 +@SYMTestActions Testing database operations on a secure database.
1.90 +@SYMTestExpectedResults Test must not fail
1.91 +@SYMREQ REQ5792
1.92 + REQ5793
1.93 +*/
1.94 +void ReadOnlyDatabaseTest()
1.95 + {
1.96 + TInt err = TheDb.Open(KTestDbName);
1.97 + TEST2(err, KErrNone);
1.98 +
1.99 + //Attempt to modify the database schema
1.100 + err = TheDb.Exec(_L("CREATE TABLE C(FFF TEXT)"));
1.101 + TEST2(err, KErrPermissionDenied);
1.102 + err = TheDb.Exec(_L("CREATE TEMP TABLE TBL100(COL1 INTEGER)"));
1.103 + TEST(err >= 0);
1.104 + err = TheDb.Exec(_L("CREATE INDEX IDX100 ON TBL100(COL1)"));
1.105 + TEST(err >= 0);
1.106 + err = TheDb.Exec(_L("DROP INDEX IDX100"));
1.107 + TEST(err >= 0);
1.108 + err = TheDb.Exec(_L("DROP TABLE TBL100"));
1.109 + TEST(err >= 0);
1.110 + //Attempt to update the user data
1.111 + err = TheDb.Exec(_L("UPDATE A SET F1 = 11 WHERE F1 = 1"));
1.112 + TEST2(err, KErrPermissionDenied);
1.113 + //Attempt to delete the user data
1.114 + err = TheDb.Exec(_L("DELETE FROM B WHERE F2 = 2"));
1.115 + TEST2(err, KErrPermissionDenied);
1.116 + //Attempt to insert new user data
1.117 + err = TheDb.Exec(_L("INSERT INTO B(F2, F3) VALUES(22, 'AAA')"));
1.118 + TEST2(err, KErrPermissionDenied);
1.119 + //Attempt to read the user data
1.120 + RSqlStatement stmt;
1.121 + err = stmt.Prepare(TheDb, _L("SELECT A.F1 FROM B,A WHERE A.F1 = B.F2"));
1.122 + TEST2(err, KErrNone);
1.123 + //ColumnCount() has no capabilities assigned
1.124 + TInt colCnt = stmt.ColumnCount();
1.125 + TEST2(colCnt, 1);
1.126 + //DeclaredColumnType() has no capabilities assigned
1.127 + TSqlColumnType colType;
1.128 + err = stmt.DeclaredColumnType(0, colType);
1.129 + TEST2(err, KErrNone);
1.130 + TEST2(colType, ESqlInt);
1.131 + err = stmt.Next();
1.132 + TEST2(err, KSqlAtRow);
1.133 + RDebug::Print(_L("Value=%d\r\n"), stmt.ColumnInt(0));
1.134 + err = stmt.Next();
1.135 + TEST2(err, KSqlAtRow);
1.136 + RDebug::Print(_L("Value=%d\r\n"), stmt.ColumnInt(0));
1.137 + stmt.Close();
1.138 + //Attempt to read the system data
1.139 + err = stmt.Prepare(TheDb, _L("SELECT * FROM SQLITE_MASTER"));
1.140 + TEST2(err, KErrNone);
1.141 + err = stmt.Next();
1.142 + TEST2(err, KSqlAtRow);
1.143 + TPtrC p;
1.144 + err = stmt.ColumnText(0, p);
1.145 + TEST2(err, KErrNone);
1.146 + RDebug::Print(_L("Value=%S\r\n"), &p);
1.147 + stmt.Close();
1.148 +
1.149 + //Attempt to execute PRAGMA statement directly
1.150 + err = TheDb.Exec(_L("PRAGMA encoding = \"UTF-8\""));
1.151 + TEST2(err, KErrPermissionDenied);
1.152 +
1.153 + TheDb.Close();
1.154 + }
1.155 +
1.156 +/**
1.157 +@SYMTestCaseID SYSLIB-SQL-UT-4009
1.158 +@SYMTestCaseDesc PlatSec warnings can occur even if an SQL database is successfully opened.
1.159 + This test application has a "ReadUserData" capability, and that should allow the
1.160 + test database ("c:[21212125]t_ab.db") to be opened successfully, because the "read"
1.161 + database policy consists of a "ReadUserData" capability only.
1.162 + No platsec warnings should be seen in the log file ("epocwind.out" file).
1.163 +@SYMTestPriority High
1.164 +@SYMTestActions PlatSec warnings can occur even if an SQL database is successfully opened.
1.165 +@SYMTestExpectedResults Test must not fail
1.166 +@SYMDEF DEF115811
1.167 +*/
1.168 +void DEF115811()
1.169 + {
1.170 + TInt err = TheDb.Open(KTestDbName);
1.171 + TEST2(err, KErrNone);
1.172 + TheDb.Close();
1.173 + }
1.174 +
1.175 +/**
1.176 +@SYMTestCaseID SYSLIB-SQL-UT-4095
1.177 +@SYMTestCaseDesc Testing incremental blob reads on a secure database.
1.178 + The test application's capabilities allow read-only access to the blobs.
1.179 + Verify that any attempt to write to a blob will fail with KErrPermissionDenied.
1.180 +@SYMTestPriority High
1.181 +@SYMTestActions Testing incremental blob reads on a secure database.
1.182 +@SYMTestExpectedResults Test must not fail
1.183 +@SYMREQ REQ5794
1.184 +*/
1.185 +void ReadOnlyBlobTestL()
1.186 + {
1.187 + TInt err = TheDb.Open(KTestDbName);
1.188 + TEST2(err, KErrNone);
1.189 +
1.190 + // Attempt to read the blobs in tables A and B
1.191 + RSqlBlobReadStream rdStrm;
1.192 + CleanupClosePushL(rdStrm);
1.193 + TBuf8<20> data;
1.194 + TRAP(err, rdStrm.OpenL(TheDb, _L("A"), _L("B1"), 1));
1.195 + TEST2(err, KErrNone);
1.196 + TRAP(err, rdStrm.ReadL(data, 3));
1.197 + TEST2(err, KErrNone);
1.198 + TEST(data.Compare(_L8("AAA")) == 0);
1.199 + rdStrm.Close();
1.200 + TRAP(err, rdStrm.OpenL(TheDb, _L("B"), _L("B2"), 2));
1.201 + TEST2(err, KErrNone);
1.202 + TRAP(err, rdStrm.ReadL(data, 10));
1.203 + TEST2(err, KErrNone);
1.204 + TEST(data.Compare(_L8("FFFFFFFFFF")) == 0);
1.205 + CleanupStack::PopAndDestroy(&rdStrm);
1.206 +
1.207 + HBufC8* wholeBuf = TSqlBlob::GetLC(TheDb, _L("A"), _L("B1"), 4);
1.208 + TEST(wholeBuf->Des().Compare(_L8("DDDDDDDDDD")) == 0);
1.209 + CleanupStack::PopAndDestroy(wholeBuf);
1.210 + wholeBuf = TSqlBlob::GetLC(TheDb, _L("B"), _L("B2"), 1);
1.211 + TEST(wholeBuf->Des().Compare(_L8("EEEEEEEEEE")) == 0);
1.212 + CleanupStack::PopAndDestroy(wholeBuf);
1.213 +
1.214 + HBufC8* buf = HBufC8::NewLC(10);
1.215 + TPtr8 bufPtr(buf->Des());
1.216 + err = TSqlBlob::Get(TheDb, _L("A"), _L("B1"), bufPtr, 2);
1.217 + TEST2(err, KErrNone);
1.218 + TEST(bufPtr.Compare(_L8("BBBBBBBBBB")) == 0);
1.219 + err = TSqlBlob::Get(TheDb, _L("B"), _L("B2"), bufPtr, 2);
1.220 + TEST2(err, KErrNone);
1.221 + TEST(bufPtr.Compare(_L8("FFFFFFFFFF")) == 0);
1.222 + CleanupStack::PopAndDestroy(buf);
1.223 +
1.224 + // Attempt to write to the blobs in tables A and B
1.225 + RSqlBlobWriteStream wrStrm;
1.226 + CleanupClosePushL(wrStrm);
1.227 + TRAP(err, wrStrm.OpenL(TheDb, _L("A"), _L("B1"), 1));
1.228 + TEST2(err, KErrPermissionDenied);
1.229 + wrStrm.Close();
1.230 + TRAP(err, wrStrm.OpenL(TheDb, _L("B"), _L("B2"), 1));
1.231 + TEST2(err, KErrPermissionDenied);
1.232 + CleanupStack::PopAndDestroy(&wrStrm);
1.233 +
1.234 + TRAP(err, TSqlBlob::SetL(TheDb, _L("A"), _L("B1"), _L8("VVVV"), 1));
1.235 + TEST2(err, KErrPermissionDenied);
1.236 + TRAP(err, TSqlBlob::SetL(TheDb, _L("B"), _L("B2"), _L8("VVVV"), 1));
1.237 + TEST2(err, KErrPermissionDenied);
1.238 +
1.239 + // SQLite and system tables
1.240 +
1.241 + // Attempt to read from and write to the SQLite master table - only reads should be permitted
1.242 + CleanupClosePushL(rdStrm);
1.243 + TRAP(err, rdStrm.OpenL(TheDb, _L("sqlite_master"), _L("tbl_name"), 1)); // TEXT column
1.244 + TEST2(err, KErrNone);
1.245 + TRAP(err, rdStrm.ReadL(data, 1));
1.246 + TEST2(err, KErrNone);
1.247 + CleanupStack::PopAndDestroy(&rdStrm);
1.248 +
1.249 + wholeBuf = TSqlBlob::GetLC(TheDb, _L("sqlite_master"), _L("tbl_name"), 1);
1.250 + TEST(wholeBuf->Length() > 0);
1.251 + CleanupStack::PopAndDestroy(wholeBuf);
1.252 +
1.253 + buf = HBufC8::NewLC(100);
1.254 + bufPtr.Set(buf->Des());
1.255 + err = TSqlBlob::Get(TheDb, _L("sqlite_master"), _L("tbl_name"), bufPtr, 1);
1.256 + TEST2(err, KErrNone);
1.257 + TEST(bufPtr.Length() > 0);
1.258 + CleanupStack::PopAndDestroy(buf);
1.259 +
1.260 + CleanupClosePushL(wrStrm);
1.261 + TRAP(err, wrStrm.OpenL(TheDb, _L("sqlite_master"), _L("tbl_name"), 1));
1.262 + TEST2(err, KErrPermissionDenied);
1.263 + CleanupStack::PopAndDestroy(&wrStrm);
1.264 +
1.265 + TRAP(err, TSqlBlob::SetL(TheDb, _L("sqlite_master"), _L("tbl_name"), _L8("VVVV"), 1));
1.266 + TEST2(err, KErrPermissionDenied);
1.267 +
1.268 + // Attempt to read from and write to the system tables - neither reads nor writes should be permitted
1.269 + CleanupClosePushL(rdStrm);
1.270 + TRAP(err, rdStrm.OpenL(TheDb, _L("symbian_security"), _L("PolicyData"), 1)); // BLOB column
1.271 + TEST2(err, KErrPermissionDenied);
1.272 + CleanupStack::PopAndDestroy(&rdStrm);
1.273 +
1.274 + TRAP(err, wholeBuf = TSqlBlob::GetLC(TheDb, _L("symbian_security"), _L("PolicyData"), 1));
1.275 + TEST2(err, KErrPermissionDenied);
1.276 +
1.277 + buf = HBufC8::NewLC(100);
1.278 + bufPtr.Set(buf->Des());
1.279 + err = TSqlBlob::Get(TheDb, _L("symbian_security"), _L("PolicyData"), bufPtr, 1);
1.280 + TEST2(err, KErrPermissionDenied);
1.281 + CleanupStack::PopAndDestroy(buf);
1.282 +
1.283 + CleanupClosePushL(wrStrm);
1.284 + TRAP(err, wrStrm.OpenL(TheDb, _L("symbian_security"), _L("PolicyData"), 1));
1.285 + TEST2(err, KErrPermissionDenied);
1.286 + CleanupStack::PopAndDestroy(&wrStrm);
1.287 +
1.288 + TRAP(err, TSqlBlob::SetL(TheDb, _L("symbian_security"), _L("PolicyData"), _L8("VVVV"), 1));
1.289 + TEST2(err, KErrPermissionDenied);
1.290 +
1.291 + TheDb.Close();
1.292 + }
1.293 +
1.294 +/**
1.295 +@SYMTestCaseID SYSLIB-SQL-UT-4078
1.296 +@SYMTestCaseDesc RSqlDatabase::Compact(), platsec test.
1.297 + The test verifies that RSqlDatabase::Compact() can be called
1.298 + on the main or on an attached database no matter what the client capabilities are.
1.299 +@SYMTestPriority Medium
1.300 +@SYMTestActions RSqlDatabase::Compact(), platsec test.
1.301 +@SYMTestExpectedResults Test must not fail
1.302 +@SYMREQ REQ10405
1.303 +*/
1.304 +void CompactTest()
1.305 + {
1.306 + TInt err = TheDb.Open(KTestDbName);
1.307 + TEST2(err, KErrNone);
1.308 +
1.309 + err = TheDb.Compact(RSqlDatabase::EMaxCompaction);
1.310 + TEST(err >= 0);
1.311 +
1.312 + TRequestStatus stat;
1.313 + TheDb.Compact(RSqlDatabase::EMaxCompaction, stat);
1.314 + User::WaitForRequest(stat);
1.315 + TEST(stat.Int() >= 0);
1.316 +
1.317 + TheDb.Close();
1.318 +
1.319 + err = TheDb.Create(KTestDbName2);
1.320 + TEST2(err, KErrNone);
1.321 + _LIT(KDbName, "Db");
1.322 + err = TheDb.Attach(KTestDbName, KDbName);
1.323 + TEST2(err, KErrNone);
1.324 +
1.325 + err = TheDb.Compact(RSqlDatabase::EMaxCompaction, KDbName);
1.326 + TEST(err >= 0);
1.327 +
1.328 + TheDb.Compact(RSqlDatabase::EMaxCompaction, stat, KDbName);
1.329 + User::WaitForRequest(stat);
1.330 + TEST(stat.Int() >= 0);
1.331 +
1.332 + err = TheDb.Detach(KDbName);
1.333 + TheDb.Close();
1.334 + (void)RSqlDatabase::Delete(KTestDbName2);
1.335 + }
1.336 +
1.337 +void DoTestsL()
1.338 + {
1.339 + TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1644 Read-only database access test "));
1.340 + ReadOnlyDatabaseTest();
1.341 +
1.342 + TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4009 DEF115811 - PlatSec warnings can occur even if an SQL database is successfully opened "));
1.343 + DEF115811();
1.344 +
1.345 + TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4095 - Read-only blob access test"));
1.346 + ReadOnlyBlobTestL();
1.347 +
1.348 + TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4078 - RSqlDatabase::Compact() test"));
1.349 + CompactTest();
1.350 + }
1.351 +
1.352 +TInt E32Main()
1.353 + {
1.354 + TheTest.Title();
1.355 +
1.356 + CTrapCleanup* tc = CTrapCleanup::New();
1.357 +
1.358 + __UHEAP_MARK;
1.359 +
1.360 + TRAPD(err, DoTestsL());
1.361 + TEST2(err, KErrNone);
1.362 +
1.363 + __UHEAP_MARKEND;
1.364 +
1.365 + TheTest.End();
1.366 + TheTest.Close();
1.367 +
1.368 + delete tc;
1.369 +
1.370 + User::Heap().Check();
1.371 + return KErrNone;
1.372 + }