persistentstorage/dbms/tdbms/t_dbplatsec1.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 "NetworkServices" (SCHEMA) capability, which allows it to
       
    16 // modify the DBMS structure but not to write any data in the tables.
       
    17 // The UID policy file is 11335579.spd.
       
    18 // Please, ensure that t_dbenvcreate test is executed before t_dbplatsec<N>/t_dbplatsecperf tests!
       
    19 // Please, ensure that t_dbenvdestroy test is executed after t_dbplatsec<N>/t_dbplatsecperf tests!
       
    20 // 
       
    21 //
       
    22 
       
    23 #include <e32test.h>
       
    24 #include <d32dbms.h>
       
    25 #include "t_dbplatsecutl.h"
       
    26 
       
    27 const TUid KSecureDbUid = {0x11335579};
       
    28 const TUid KProtSecureDbUid = {0x11335578};
       
    29 _LIT(KSecure,	"SECURE");
       
    30 _LIT(KDbName,	"C:TestDB2.dB");
       
    31 _LIT(KTblNameA,	"A");
       
    32 _LIT(KTblNameB,	"B");
       
    33 _LIT(KTblNameC,	"C");
       
    34 
       
    35 static RTest 				TheTest(_L("t_dbplatsec1: DBMS platform security testing - 1"));
       
    36 static RDbs 				TheDbs;
       
    37 static RDbNamedDatabase 	TheDb;
       
    38 static RDbTable 			TheTbl;
       
    39 static RDbView 				TheView;
       
    40 
       
    41 static void CleanupTest()
       
    42 	{
       
    43 	TheView.Close();
       
    44 	TheTbl.Close();
       
    45 	TheDb.Close();
       
    46 	TDBSCUtils::DeleteDatabase(TheDbs, KSecureDbUid, KDbName);
       
    47 	TheDbs.Close();
       
    48 	}
       
    49 
       
    50 TDBSCUtils 	TheDbscUtils(TheTest, &CleanupTest);
       
    51 
       
    52 static TColDef const KColumns[]=
       
    53 	{
       
    54 	{_S("ID"), EDbColInt32, TDbCol::ENotNull | TDbCol::EAutoIncrement},
       
    55 	{_S("DATA1"), EDbColInt32, TDbCol::ENotNull},
       
    56 	{_S("DATA2"), EDbColInt32, TDbCol::ENotNull},
       
    57 	{0}
       
    58 	};
       
    59 
       
    60 /**
       
    61 @SYMTestCaseID SYSLIB-DBMS-CT-0008
       
    62 @SYMTestCaseDesc Database tests. Some of the calls must fail because the caller has no enough rights
       
    63                  for the requested operation.
       
    64 @SYMTestPriority High
       
    65 @SYMTestActions  RDbNamedDatabase::Open()/RDbNamedDatabase::DatabaseNamesL()/RDbNamedDatabase::Create().
       
    66 @SYMTestExpectedResults The test must not fail.
       
    67 @SYMREQ REQ2429
       
    68                  DBMS shall provide an API to apply security policies to database tables.
       
    69 */
       
    70 static void DbTestL()
       
    71 	{
       
    72 	TBuf<32> format;
       
    73 
       
    74 	TDBSCUtils::DeleteDatabase(TheDbs, KSecureDbUid, KDbName);
       
    75 	TheTest.Printf(_L("Create database\n"));
       
    76 	//The test must pass, because the test app has "SCHEMA" capability
       
    77 	format.Copy(KSecure);
       
    78 	format.Append(KSecureDbUid.Name());
       
    79 	TInt err = TheDb.Create(TheDbs, KDbName, format);
       
    80 	TEST2(err, KErrNone);
       
    81 
       
    82 	//The test must pass, because "DatabaseNamesL" is a DBMS operation available for everyone.
       
    83 	TheTest.Printf(_L("Database list\n"));
       
    84 	CDbDatabaseNames* dbNames = TheDbs.DatabaseNamesL(EDriveC, KSecureDbUid);
       
    85 	TEST(dbNames->Count() > 0);
       
    86 	TBool casePreserved = EFalse;
       
    87 	for(TInt i=0;i<dbNames->Count();++i)
       
    88 		{
       
    89 		const TDesC& dbName = (*dbNames)[i];
       
    90 		RDebug::Print(_L("--Database: %S\n"), &dbName);
       
    91 		TBuf<128> dbName2;
       
    92 		dbName2.Append(TChar('A' + EDriveC));
       
    93 		dbName2.Append(TChar(':'));
       
    94 		dbName2.Append(dbName);
       
    95 		if(dbName2 == KDbName())
       
    96 			{
       
    97 			casePreserved = ETrue;
       
    98 			}
       
    99 		}
       
   100 	//if casePreserved is non-zero that means the DBMS server does not change the database names to
       
   101 	//upper or lower case - that's what we want to check
       
   102 	TEST(casePreserved);
       
   103 	delete dbNames;
       
   104 
       
   105 	TheDb.Close();
       
   106 
       
   107 	TheTest.Printf(_L("An attempt to create database - existing, but protected UID\n"));
       
   108 	//The test must fail, because the test app does not have capabilities to satisfy
       
   109 	//KProtSecureDbUid "SCHEMA" policy.
       
   110 	format.Copy(KSecure);
       
   111 	format.Append(KProtSecureDbUid.Name());
       
   112 	err = TheDb.Create(TheDbs, KDbName, format);
       
   113 	TEST2(err, KErrPermissionDenied);
       
   114 
       
   115 	//The test must pass, because the test app has "SCHEMA" capability (it must have capabilities,
       
   116 	//satisfying at least one of the UID's R/W/S policies)
       
   117 	TheTest.Printf(_L("Open database\n"));
       
   118 	format.Copy(KSecure);
       
   119 	format.Append(KSecureDbUid.Name());
       
   120 	err = TheDb.Open(TheDbs, KDbName, format);
       
   121 	TEST2(err, KErrNone);
       
   122 	}
       
   123 
       
   124 /**
       
   125 @SYMTestCaseID SYSLIB-DBMS-CT-0009
       
   126 @SYMTestCaseDesc Opening table test. The caller has a set of capabilities which satisfy database's
       
   127                  schema security policy only. The test checks that the capapbility checking
       
   128 				 on the DBMS server side works properly. Some of the initiated open table
       
   129 				 operations won't executed and the returned error will be KErrPermisssionDenied.
       
   130 @SYMTestPriority High
       
   131 @SYMTestActions  Attempts to execute RDbTable::Open() on different tables from the test database.
       
   132 @SYMTestExpectedResults The test must not fail.
       
   133 @SYMREQ REQ2429
       
   134                  DBMS shall provide an API to apply security policies to database tables.
       
   135 */
       
   136 static void TblOpenL()
       
   137 	{
       
   138 	TheTest.Printf(_L("Create tables\n"));
       
   139 	//The test must pass, because the test app has "SCHEMA" capability
       
   140 	CDbColSet* colset = TDBSCUtils::CreateColSetLC(KColumns);
       
   141 	TInt err = TheDb.CreateTable(KTblNameA, *colset);//R: PowerMgmt, W: WriteUserData
       
   142 	TEST2(err, KErrNone);
       
   143 	err = TheDb.CreateTable(KTblNameB, *colset);//R: None, W: WriteUserData WriteDeviceData
       
   144 	TEST2(err, KErrNone);
       
   145 	err = TheDb.CreateTable(KTblNameC, *colset);//R: None, W: WriteUserData
       
   146 	TEST2(err, KErrNone);
       
   147 	CleanupStack::PopAndDestroy(colset);
       
   148 
       
   149 	TheTest.Printf(_L("An attempt to open table A\n"));
       
   150 	//The test must fail, because the test app has no capabilities to satisfy
       
   151 	//R/W policies of table A
       
   152 	err = TheTbl.Open(TheDb, KTblNameA);
       
   153 	TEST2(err, KErrPermissionDenied);
       
   154 	err = TheTbl.Open(TheDb, KTblNameA, RDbRowSet::EUpdatable);
       
   155 	TEST2(err, KErrPermissionDenied);
       
   156 	err = TheTbl.Open(TheDb, KTblNameA, RDbRowSet::EReadOnly);
       
   157 	TEST2(err, KErrPermissionDenied);
       
   158 	err = TheTbl.Open(TheDb, KTblNameA, RDbRowSet::EInsertOnly);
       
   159 	TEST2(err, KErrPermissionDenied);
       
   160 
       
   161 	TheTest.Printf(_L("An attempt to open table B\n"));
       
   162 	//Open table B in insert/update mode - the test must fail, because the test app has no
       
   163 	//capabilities to satisfy table B, policy W.
       
   164 	//Open table B in read-only mode - the test must pass, because table B has no R policy.
       
   165 	err = TheTbl.Open(TheDb, KTblNameB);
       
   166 	TEST2(err, KErrPermissionDenied);
       
   167 	err = TheTbl.Open(TheDb, KTblNameB, RDbRowSet::EUpdatable);
       
   168 	TEST2(err, KErrPermissionDenied);
       
   169 	err = TheTbl.Open(TheDb, KTblNameB, RDbRowSet::EInsertOnly);
       
   170 	TEST2(err, KErrPermissionDenied);
       
   171 	err = TheTbl.Open(TheDb, KTblNameB, RDbRowSet::EReadOnly);
       
   172 	TEST2(err, KErrNone);
       
   173 	TheTbl.Close();
       
   174 
       
   175 	TheTest.Printf(_L("An attempt to open table C\n"));
       
   176 	//Open table C in insert/update mode - the test must fail, because the test app has no
       
   177 	//capabilities to satisfy table C, policy W.
       
   178 	//Open table C in read-only mode - the test must pass, because table C has no R policy.
       
   179 	err = TheTbl.Open(TheDb, KTblNameC);
       
   180 	TEST2(err, KErrPermissionDenied);
       
   181 	err = TheTbl.Open(TheDb, KTblNameC, RDbRowSet::EUpdatable);
       
   182 	TEST2(err, KErrPermissionDenied);
       
   183 	err = TheTbl.Open(TheDb, KTblNameC, RDbRowSet::EInsertOnly);
       
   184 	TEST2(err, KErrPermissionDenied);
       
   185 	err = TheTbl.Open(TheDb, KTblNameC, RDbRowSet::EReadOnly);
       
   186 	TEST2(err, KErrNone);
       
   187 	TheTbl.Close();
       
   188 	}
       
   189 
       
   190 /**
       
   191 @SYMTestCaseID SYSLIB-DBMS-CT-0010
       
   192 @SYMTestCaseDesc Table R/w operations. The caller has a set of capabilities which satisfy database's
       
   193                  schema security policy only. The test checks that the capapbility checking
       
   194 				 on the DBMS server side works properly. Some of the R/W table operations won't be
       
   195 				 executed and the returned error will be KErrPermisssionDenied.
       
   196 @SYMTestPriority High
       
   197 @SYMTestActions  Attempts to execute RDbTable::Insert()/RDbTable::Update()/RDbTable::FirstL()
       
   198                  on different tables from the test database.
       
   199 @SYMTestExpectedResults The test must not fail.
       
   200 @SYMREQ REQ2429
       
   201                  DBMS shall provide an API to apply security policies to database tables.
       
   202 */
       
   203 static void TblRWL()
       
   204 	{
       
   205 	TheTest.Printf(_L("An attempt to write in table B\n"));
       
   206 	TInt err = TheTbl.Open(TheDb, KTblNameB, RDbRowSet::EReadOnly);
       
   207 	TEST2(err, KErrNone);
       
   208 	//"Write table B" test must fail, because the test app has no capabilities
       
   209 	//to satisfy table B, policy W.
       
   210 	TRAP(err, TheTbl.InsertL());
       
   211 	TEST2(err, KErrPermissionDenied);
       
   212 	TRAP(err, TheTbl.UpdateL());
       
   213 	TEST2(err, KErrPermissionDenied);
       
   214 
       
   215 	TheTest.Printf(_L("An attempt to read from table B\n"));
       
   216 	//"Read table B" test must pass, because table B has no R policy
       
   217 	TBool res = TheTbl.FirstL();
       
   218 	TEST(!res);
       
   219 
       
   220 	TheTbl.Close();
       
   221 
       
   222 	TheTest.Printf(_L("An attempt to write in table C\n"));
       
   223 	//"Write table C" test must fail, because the test app has no capabilities
       
   224 	//to satisfy table C, policy W.
       
   225 	err = TheTbl.Open(TheDb, KTblNameC, RDbRowSet::EReadOnly);
       
   226 	TEST2(err, KErrNone);
       
   227 	TRAP(err, TheTbl.InsertL());
       
   228 	TEST2(err, KErrPermissionDenied);
       
   229 	TRAP(err, TheTbl.UpdateL());
       
   230 	TEST2(err, KErrPermissionDenied);
       
   231 
       
   232 	TheTest.Printf(_L("An attempt to read from table C\n"));
       
   233 	//"Read table C" test must pass, because table C has no R policy
       
   234 	res = TheTbl.FirstL();
       
   235 	TEST(!res);
       
   236 
       
   237 	TheTbl.Close();
       
   238 	}
       
   239 
       
   240 /**
       
   241 @SYMTestCaseID SYSLIB-DBMS-CT-0011
       
   242 @SYMTestCaseDesc SQL tests. The caller has a set of capabilities which satisfy database's
       
   243                  schema security policy only. The test checks that the capapbility checking
       
   244 				 on the DBMS server side works properly. Some of the SQL statements won't be
       
   245 				 executed and the returned error will be KErrPermisssionDenied.
       
   246 @SYMTestPriority High
       
   247 @SYMTestActions  Attempts to execute various INSERT/UPDATE/SELECT SQL statements.
       
   248 @SYMTestExpectedResults The test must not fail.
       
   249 @SYMREQ REQ2429
       
   250                  DBMS shall provide an API to apply security policies to database tables.
       
   251 */
       
   252 static void TblSqlL()
       
   253 	{
       
   254 	TheTest.Printf(_L("SELECT SQL\n"));
       
   255 	//The test must fail, because the test app cannot satisfy table A, policy R.
       
   256 	TInt err = TheView.Prepare(TheDb, TDbQuery(_L("SELECT * FROM A")));
       
   257 	TEST2(err, KErrPermissionDenied);
       
   258 	//The test must pass, because table B has no R policy.
       
   259 	err = TheView.Prepare(TheDb, TDbQuery(_L("SELECT * FROM B")));
       
   260 	TEST2(err, KErrNone);
       
   261 	TheView.Close();
       
   262 	//The test must pass, because table C has no R policy.
       
   263 	err = TheView.Prepare(TheDb, TDbQuery(_L("SELECT * FROM C")));
       
   264 	TEST2(err, KErrNone);
       
   265 	TheView.Close();
       
   266 
       
   267 	TheTest.Printf(_L("INSERT/UPDATE SQL\n"));
       
   268 	//The test must fail, because the test app cannot satisfy table A, policy W.
       
   269 	err = TheDb.Execute(_L("INSERT INTO A (DATA2) VALUES(45)"));
       
   270 	TEST2(err, KErrPermissionDenied);
       
   271 	//The test must fail, because the test app cannot satisfy table B, policy W.
       
   272 	err = TheDb.Execute(_L("INSERT INTO B (DATA2) VALUES(45)"));
       
   273 	TEST2(err, KErrPermissionDenied);
       
   274 	//The test must fail, because the test app cannot satisfy table C, policy W.
       
   275 	err = TheDb.Execute(_L("INSERT INTO C (DATA2) VALUES(45)"));
       
   276 	TEST2(err, KErrPermissionDenied);
       
   277 
       
   278 	//The test must fail, because the test app cannot satisfy table A, policy W.
       
   279 	err = TheDb.Execute(_L("UPDATE A SET DATA2=56 WHERE ID = 0"));
       
   280 	TEST2(err, KErrPermissionDenied);
       
   281 	//The test must fail, because the test app cannot satisfy table B, policy W.
       
   282 	err = TheDb.Execute(_L("UPDATE B SET DATA2=56 WHERE ID = 0"));
       
   283 	TEST2(err, KErrPermissionDenied);
       
   284 	//The test must fail, because the test app cannot satisfy table C, policy W.
       
   285 	err = TheDb.Execute(_L("UPDATE C SET DATA2=56 WHERE ID = 0"));
       
   286 	TEST2(err, KErrPermissionDenied);
       
   287 	}
       
   288 
       
   289 static void DoRunL()
       
   290 	{
       
   291 	TheTest.Start(_L("An app with \"SCHEMA\" capabilities set"));
       
   292 
       
   293 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0008 Database test "));
       
   294 	::DbTestL();
       
   295 
       
   296 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0009 Open table test "));
       
   297 	::TblOpenL();
       
   298 
       
   299 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0010 Read/Write table test "));
       
   300 	::TblRWL();
       
   301 
       
   302 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0011 SQL table test "));
       
   303 	::TblSqlL();
       
   304 	}
       
   305 
       
   306 TInt E32Main()
       
   307     {
       
   308 	__UHEAP_MARK;
       
   309 	CTrapCleanup* tc = CTrapCleanup::New();
       
   310 	TEST(tc != NULL);
       
   311 
       
   312 	TInt err = TheDbs.Connect();
       
   313 	TEST2(err, KErrNone);
       
   314 
       
   315 	TRAP(err, ::DoRunL());
       
   316 	TEST2(err, KErrNone);
       
   317 
       
   318 	::CleanupTest();
       
   319 
       
   320 	TheTest.End();
       
   321 	TheTest.Close();
       
   322 
       
   323 	delete tc;
       
   324 
       
   325 	__UHEAP_MARKEND;
       
   326 	User::Heap().Check();
       
   327 	return KErrNone;
       
   328     }