1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/dbms/tdbms/t_dbplatsec3.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,220 @@
1.4 +// Copyright (c) 2004-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 +// DBMS security policy - testing new APIs.
1.18 +// This test app has "PowerMgmt" (TABLE A: READ) capability, which allows it to
1.19 +// read data from table A.
1.20 +// The UID policy file is 11335579.spd.
1.21 +// The test uses C:TESTDB.DB secure shared database, which has tables A, B and C, each of them
1.22 +// with at least one record.
1.23 +// Please, ensure that t_dbenvcreate test is executed before t_dbplatsec<N>/t_dbplatsecperf tests!
1.24 +// Please, ensure that t_dbenvdestroy test is executed after t_dbplatsec<N>/t_dbplatsecperf tests!
1.25 +//
1.26 +//
1.27 +
1.28 +#include <e32test.h>
1.29 +#include <d32dbms.h>
1.30 +#include "t_dbplatsecutl.h"
1.31 +
1.32 +const TUid KSecureDbUid = {0x11335579};
1.33 +_LIT(KSecure, "SECURE");
1.34 +_LIT(KDbName, "C:TestDB.DB");
1.35 +_LIT(KTblNameA, "A");
1.36 +_LIT(KTblNameB, "B");
1.37 +_LIT(KTblNameC, "C");
1.38 +
1.39 +static RTest TheTest(_L("t_dbplatsec3: DBMS platform security testing - 3"));
1.40 +static RDbs TheDbs;
1.41 +static RDbNamedDatabase TheDb;
1.42 +static RDbTable TheTbl;
1.43 +static RDbView TheView;
1.44 +
1.45 +TDBSCUtils TheDbscUtils(TheTest, NULL);
1.46 +
1.47 +/**
1.48 +@SYMTestCaseID SYSLIB-DBMS-CT-0015
1.49 +@SYMTestCaseDesc OPen table test.
1.50 + This test app has "PowerMgmt" (TABLE A: READ) capability, which allows it to
1.51 + read data from table A. B and C tables can be read too, because they do
1.52 + not have read security policy. The attempts to open A, B and C tables in
1.53 + insert/update mode must fail.
1.54 +@SYMTestPriority High
1.55 +@SYMTestActions Open table test.
1.56 +@SYMTestExpectedResults The test must not fail.
1.57 +@SYMREQ REQ2429
1.58 + DBMS shall provide an API to apply security policies to database tables.
1.59 +*/
1.60 +static void TblOpenL()
1.61 + {
1.62 + TheTest.Printf(_L("An attempt to open table A\n"));
1.63 + //The test must fail, because the test app cannot satisfy table A, policy W.
1.64 + TInt err = TheTbl.Open(TheDb, KTblNameA);
1.65 + TEST2(err, KErrPermissionDenied);
1.66 + //The test must pass, because the test app can satisfy table A, policy R.
1.67 + err = TheTbl.Open(TheDb, KTblNameA, RDbRowSet::EReadOnly);
1.68 + TEST2(err, KErrNone);
1.69 + TheTbl.Close();
1.70 +
1.71 + TheTest.Printf(_L("An attempt to open table B\n"));
1.72 + //The test must fail, because the test app cannot satisfy table B, policy W.
1.73 + err = TheTbl.Open(TheDb, KTblNameB);
1.74 + TEST2(err, KErrPermissionDenied);
1.75 + //The test must pass, because table B has no R policy.
1.76 + err = TheTbl.Open(TheDb, KTblNameB, RDbRowSet::EReadOnly);
1.77 + TEST2(err, KErrNone);
1.78 + TheTbl.Close();
1.79 +
1.80 + TheTest.Printf(_L("An attempt to open table C\n"));
1.81 + //The test must fail, because the test app cannot satisfy table C, policy W.
1.82 + err = TheTbl.Open(TheDb, KTblNameC);
1.83 + TEST2(err, KErrPermissionDenied);
1.84 + //The test must pass, because table C has no R policy.
1.85 + err = TheTbl.Open(TheDb, KTblNameC, RDbRowSet::EReadOnly);
1.86 + TEST2(err, KErrNone);
1.87 + TheTbl.Close();
1.88 + }
1.89 +
1.90 +/**
1.91 +@SYMTestCaseID SYSLIB-DBMS-CT-0016
1.92 +@SYMTestCaseDesc R/W operations at a table level.
1.93 + This test app has "PowerMgmt" (TABLE A: READ) capability, which allows it to
1.94 + read data from table A. B and C tables can be read too, because they do
1.95 + not have read security policy.
1.96 +@SYMTestPriority High
1.97 +@SYMTestActions R/W table operations.
1.98 +@SYMTestExpectedResults The test must not fail.
1.99 +@SYMREQ REQ2429
1.100 + DBMS shall provide an API to apply security policies to database tables.
1.101 +*/
1.102 +static void TblRWL()
1.103 + {
1.104 + TheTest.Printf(_L("Table A - Write\n"));
1.105 + TInt err = TheTbl.Open(TheDb, KTblNameA, RDbRowSet::EReadOnly);
1.106 + TEST2(err, KErrNone);
1.107 + //The test must fail, because the test app cannot satisfy table A, policy W.
1.108 + TRAP(err, TheTbl.InsertL());
1.109 + TEST2(err, KErrPermissionDenied);
1.110 + err = TheDb.Execute(_L("UPDATE A SET DATA1 = 400 WHERE ID < 10"));
1.111 + TEST2(err, KErrPermissionDenied);
1.112 +
1.113 + TheTest.Printf(_L("Table A - Read\n"));
1.114 + //The test must pass, because the test app can satisfy table A, policy R.
1.115 + TBool res = EFalse;
1.116 + TRAP(err, res = TheTbl.FirstL());
1.117 + TEST2(err, KErrNone);
1.118 + TEST(res);
1.119 + TInt cnt = TheTbl.CountL();
1.120 + TEST(cnt > 0);
1.121 + err = TheView.Prepare(TheDb, TDbQuery(_L("SELECT * FROM A")));
1.122 + TEST2(err, KErrNone);
1.123 + cnt = TheView.CountL();
1.124 + TEST(cnt > 0);
1.125 + TheView.Close();
1.126 +
1.127 + TheTbl.Close();
1.128 +
1.129 + TheTest.Printf(_L("Table B - Write\n"));
1.130 + err = TheTbl.Open(TheDb, KTblNameB, RDbRowSet::EReadOnly);
1.131 + TEST2(err, KErrNone);
1.132 + //The test must fail, because the test app cannot satisfy table B, policy W.
1.133 + TRAP(err, TheTbl.InsertL());
1.134 + TEST2(err, KErrPermissionDenied);
1.135 + err = TheDb.Execute(_L("INSERT INTO B (DATA2) VALUES (45)"));
1.136 + TEST2(err, KErrPermissionDenied);
1.137 +
1.138 + TheTest.Printf(_L("Table B - Read\n"));
1.139 + //The test must pass, because table B has no R policy.
1.140 + TRAP(err, res = TheTbl.FirstL());
1.141 + TEST2(err, KErrNone);
1.142 + TEST(res);
1.143 + cnt = TheTbl.CountL();
1.144 + TEST(cnt > 0);
1.145 + err = TheView.Prepare(TheDb, TDbQuery(_L("SELECT * FROM B")));
1.146 + TEST2(err, KErrNone);
1.147 + cnt = TheView.CountL();
1.148 + TEST(cnt > 0);
1.149 + TheView.Close();
1.150 +
1.151 + TheTbl.Close();
1.152 +
1.153 + TheTest.Printf(_L("Table C - Write\n"));
1.154 + err = TheTbl.Open(TheDb, KTblNameC);
1.155 + //The test must fail, because the test app cannot satisfy table C, policy W.
1.156 + TEST2(err, KErrPermissionDenied);
1.157 + err = TheTbl.Open(TheDb, KTblNameC, RDbRowSet::EReadOnly);
1.158 + TEST2(err, KErrNone);
1.159 + TRAP(err, TheTbl.InsertL());
1.160 + TEST2(err, KErrPermissionDenied);
1.161 + err = TheDb.Execute(_L("UPDATE C SET DATA1 = 400 WHERE ID < 10"));
1.162 + TEST2(err, KErrPermissionDenied);
1.163 +
1.164 + TheTest.Printf(_L("Table C - Read\n"));
1.165 + //The test must pass, because table C has no R policy.
1.166 + TRAP(err, res = TheTbl.FirstL());
1.167 + TEST2(err, KErrNone);
1.168 + TEST(res);
1.169 + cnt = TheTbl.CountL();
1.170 + TEST(cnt > 0);
1.171 + err = TheView.Prepare(TheDb, TDbQuery(_L("SELECT * FROM C")));
1.172 + TEST2(err, KErrNone);
1.173 + cnt = TheView.CountL();
1.174 + TEST(cnt > 0);
1.175 + TheView.Close();
1.176 +
1.177 + TheTbl.Close();
1.178 + }
1.179 +
1.180 +static void DoRunL()
1.181 + {
1.182 + TheTest.Start(_L("An app with \"TABLE A:READ\" capabilities set"));
1.183 +
1.184 + TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0015 Open table tests "));
1.185 + ::TblOpenL();
1.186 +
1.187 + TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0016 Table R/W tests "));
1.188 + ::TblRWL();
1.189 + }
1.190 +
1.191 +TInt E32Main()
1.192 + {
1.193 + __UHEAP_MARK;
1.194 + CTrapCleanup* tc = CTrapCleanup::New();
1.195 + TEST(tc != NULL);
1.196 +
1.197 + TInt err = TheDbs.Connect();
1.198 + TEST2(err, KErrNone);
1.199 +
1.200 + TBuf<32> format;
1.201 + TheTest.Printf(_L("Open database\n"));
1.202 + format.Copy(KSecure);
1.203 + format.Append(KSecureDbUid.Name());
1.204 + err = TheDb.Open(TheDbs, KDbName, format);
1.205 + TEST2(err, KErrNone);
1.206 +
1.207 + TRAP(err, ::DoRunL());
1.208 + TEST2(err, KErrNone);
1.209 +
1.210 + TheView.Close();
1.211 + TheTbl.Close();
1.212 + TheDb.Close();
1.213 + TheDbs.Close();
1.214 +
1.215 + TheTest.End();
1.216 + TheTest.Close();
1.217 +
1.218 + delete tc;
1.219 +
1.220 + __UHEAP_MARKEND;
1.221 + User::Heap().Check();
1.222 + return KErrNone;
1.223 + }