persistentstorage/dbms/tdbms/t_dbplatsec3.cpp
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 2004-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 // DBMS security policy - testing new APIs.
       
    15 // This test app has "PowerMgmt" (TABLE A: READ) capability, which allows it to
       
    16 // read data from table A.
       
    17 // The UID policy file is 11335579.spd.
       
    18 // The test uses C:TESTDB.DB secure shared database, which has tables A, B and C, each of them
       
    19 // with at least one record.
       
    20 // Please, ensure that t_dbenvcreate test is executed before t_dbplatsec<N>/t_dbplatsecperf tests!
       
    21 // Please, ensure that t_dbenvdestroy test is executed after t_dbplatsec<N>/t_dbplatsecperf tests!
       
    22 // 
       
    23 //
       
    24 
       
    25 #include <e32test.h>
       
    26 #include <d32dbms.h>
       
    27 #include "t_dbplatsecutl.h"
       
    28 
       
    29 const TUid KSecureDbUid = {0x11335579};
       
    30 _LIT(KSecure,	"SECURE");
       
    31 _LIT(KDbName,	"C:TestDB.DB");
       
    32 _LIT(KTblNameA,	"A");
       
    33 _LIT(KTblNameB,	"B");
       
    34 _LIT(KTblNameC,	"C");
       
    35 
       
    36 static RTest 				TheTest(_L("t_dbplatsec3: DBMS platform security testing - 3"));
       
    37 static RDbs 				TheDbs;
       
    38 static RDbNamedDatabase 	TheDb;
       
    39 static RDbTable 			TheTbl;
       
    40 static RDbView 				TheView;
       
    41 
       
    42 TDBSCUtils 	TheDbscUtils(TheTest, NULL);
       
    43 
       
    44 /**
       
    45 @SYMTestCaseID SYSLIB-DBMS-CT-0015
       
    46 @SYMTestCaseDesc OPen table test.
       
    47 				 This test app has "PowerMgmt" (TABLE A: READ) capability, which allows it to
       
    48 				 read data from table A. B and C tables can be read too, because they do
       
    49 				 not have read security policy. The attempts to open A, B and C tables in
       
    50 				 insert/update mode must fail.
       
    51 @SYMTestPriority High
       
    52 @SYMTestActions  Open table test.
       
    53 @SYMTestExpectedResults The test must not fail.
       
    54 @SYMREQ REQ2429
       
    55                  DBMS shall provide an API to apply security policies to database tables.
       
    56 */
       
    57 static void TblOpenL()
       
    58 	{
       
    59 	TheTest.Printf(_L("An attempt to open table A\n"));
       
    60 	//The test must fail, because the test app cannot satisfy table A, policy W.
       
    61 	TInt err = TheTbl.Open(TheDb, KTblNameA);
       
    62 	TEST2(err, KErrPermissionDenied);
       
    63 	//The test must pass, because the test app can satisfy table A, policy R.
       
    64 	err = TheTbl.Open(TheDb, KTblNameA, RDbRowSet::EReadOnly);
       
    65 	TEST2(err, KErrNone);
       
    66 	TheTbl.Close();
       
    67 
       
    68 	TheTest.Printf(_L("An attempt to open table B\n"));
       
    69 	//The test must fail, because the test app cannot satisfy table B, policy W.
       
    70 	err = TheTbl.Open(TheDb, KTblNameB);
       
    71 	TEST2(err, KErrPermissionDenied);
       
    72 	//The test must pass, because table B has no R policy.
       
    73 	err = TheTbl.Open(TheDb, KTblNameB, RDbRowSet::EReadOnly);
       
    74 	TEST2(err, KErrNone);
       
    75 	TheTbl.Close();
       
    76 
       
    77 	TheTest.Printf(_L("An attempt to open table C\n"));
       
    78 	//The test must fail, because the test app cannot satisfy table C, policy W.
       
    79 	err = TheTbl.Open(TheDb, KTblNameC);
       
    80 	TEST2(err, KErrPermissionDenied);
       
    81 	//The test must pass, because table C has no R policy.
       
    82 	err = TheTbl.Open(TheDb, KTblNameC, RDbRowSet::EReadOnly);
       
    83 	TEST2(err, KErrNone);
       
    84 	TheTbl.Close();
       
    85 	}
       
    86 
       
    87 /**
       
    88 @SYMTestCaseID SYSLIB-DBMS-CT-0016
       
    89 @SYMTestCaseDesc R/W operations at a table level.
       
    90 				 This test app has "PowerMgmt" (TABLE A: READ) capability, which allows it to
       
    91 				 read data from table A. B and C tables can be read too, because they do
       
    92 				 not have read security policy.
       
    93 @SYMTestPriority High
       
    94 @SYMTestActions  R/W table operations.
       
    95 @SYMTestExpectedResults The test must not fail.
       
    96 @SYMREQ REQ2429
       
    97                  DBMS shall provide an API to apply security policies to database tables.
       
    98 */
       
    99 static void TblRWL()
       
   100 	{
       
   101 	TheTest.Printf(_L("Table A - Write\n"));
       
   102 	TInt err = TheTbl.Open(TheDb, KTblNameA, RDbRowSet::EReadOnly);
       
   103 	TEST2(err, KErrNone);
       
   104 	//The test must fail, because the test app cannot satisfy table A, policy W.
       
   105 	TRAP(err, TheTbl.InsertL());
       
   106 	TEST2(err, KErrPermissionDenied);
       
   107 	err = TheDb.Execute(_L("UPDATE A SET DATA1 = 400 WHERE ID < 10"));
       
   108 	TEST2(err, KErrPermissionDenied);
       
   109 
       
   110 	TheTest.Printf(_L("Table A - Read\n"));
       
   111 	//The test must pass, because the test app can satisfy table A, policy R.
       
   112 	TBool res = EFalse;
       
   113 	TRAP(err, res = TheTbl.FirstL());
       
   114 	TEST2(err, KErrNone);
       
   115 	TEST(res);
       
   116 	TInt cnt = TheTbl.CountL();
       
   117 	TEST(cnt > 0);
       
   118 	err = TheView.Prepare(TheDb, TDbQuery(_L("SELECT * FROM A")));
       
   119 	TEST2(err, KErrNone);
       
   120 	cnt = TheView.CountL();
       
   121 	TEST(cnt > 0);
       
   122 	TheView.Close();
       
   123 
       
   124 	TheTbl.Close();
       
   125 
       
   126 	TheTest.Printf(_L("Table B - Write\n"));
       
   127 	err = TheTbl.Open(TheDb, KTblNameB, RDbRowSet::EReadOnly);
       
   128 	TEST2(err, KErrNone);
       
   129 	//The test must fail, because the test app cannot satisfy table B, policy W.
       
   130 	TRAP(err, TheTbl.InsertL());
       
   131 	TEST2(err, KErrPermissionDenied);
       
   132 	err = TheDb.Execute(_L("INSERT INTO B (DATA2) VALUES (45)"));
       
   133 	TEST2(err, KErrPermissionDenied);
       
   134 
       
   135 	TheTest.Printf(_L("Table B - Read\n"));
       
   136 	//The test must pass, because table B has no R policy.
       
   137 	TRAP(err, res = TheTbl.FirstL());
       
   138 	TEST2(err, KErrNone);
       
   139 	TEST(res);
       
   140 	cnt = TheTbl.CountL();
       
   141 	TEST(cnt > 0);
       
   142 	err = TheView.Prepare(TheDb, TDbQuery(_L("SELECT * FROM B")));
       
   143 	TEST2(err, KErrNone);
       
   144 	cnt = TheView.CountL();
       
   145 	TEST(cnt > 0);
       
   146 	TheView.Close();
       
   147 
       
   148 	TheTbl.Close();
       
   149 
       
   150 	TheTest.Printf(_L("Table C - Write\n"));
       
   151 	err = TheTbl.Open(TheDb, KTblNameC);
       
   152 	//The test must fail, because the test app cannot satisfy table C, policy W.
       
   153 	TEST2(err, KErrPermissionDenied);
       
   154 	err = TheTbl.Open(TheDb, KTblNameC, RDbRowSet::EReadOnly);
       
   155 	TEST2(err, KErrNone);
       
   156 	TRAP(err, TheTbl.InsertL());
       
   157 	TEST2(err, KErrPermissionDenied);
       
   158 	err = TheDb.Execute(_L("UPDATE C SET DATA1 = 400 WHERE ID < 10"));
       
   159 	TEST2(err, KErrPermissionDenied);
       
   160 
       
   161 	TheTest.Printf(_L("Table C - Read\n"));
       
   162 	//The test must pass, because table C has no R policy.
       
   163 	TRAP(err, res = TheTbl.FirstL());
       
   164 	TEST2(err, KErrNone);
       
   165 	TEST(res);
       
   166 	cnt = TheTbl.CountL();
       
   167 	TEST(cnt > 0);
       
   168 	err = TheView.Prepare(TheDb, TDbQuery(_L("SELECT * FROM C")));
       
   169 	TEST2(err, KErrNone);
       
   170 	cnt = TheView.CountL();
       
   171 	TEST(cnt > 0);
       
   172 	TheView.Close();
       
   173 
       
   174 	TheTbl.Close();
       
   175 	}
       
   176 
       
   177 static void DoRunL()
       
   178 	{
       
   179 	TheTest.Start(_L("An app with \"TABLE A:READ\" capabilities set"));
       
   180 
       
   181 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0015 Open table tests "));
       
   182 	::TblOpenL();
       
   183 
       
   184 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0016 Table R/W tests "));
       
   185 	::TblRWL();
       
   186 	}
       
   187 
       
   188 TInt E32Main()
       
   189     {
       
   190 	__UHEAP_MARK;
       
   191 	CTrapCleanup* tc = CTrapCleanup::New();
       
   192 	TEST(tc != NULL);
       
   193 
       
   194 	TInt err = TheDbs.Connect();
       
   195 	TEST2(err, KErrNone);
       
   196 
       
   197 	TBuf<32> format;
       
   198 	TheTest.Printf(_L("Open database\n"));
       
   199 	format.Copy(KSecure);
       
   200 	format.Append(KSecureDbUid.Name());
       
   201 	err = TheDb.Open(TheDbs, KDbName, format);
       
   202 	TEST2(err, KErrNone);
       
   203 
       
   204 	TRAP(err, ::DoRunL());
       
   205 	TEST2(err, KErrNone);
       
   206 
       
   207 	TheView.Close();
       
   208 	TheTbl.Close();
       
   209 	TheDb.Close();
       
   210 	TheDbs.Close();
       
   211 
       
   212 	TheTest.End();
       
   213 	TheTest.Close();
       
   214 
       
   215 	delete tc;
       
   216 
       
   217 	__UHEAP_MARKEND;
       
   218 	User::Heap().Check();
       
   219 	return KErrNone;
       
   220     }