os/persistentdata/persistentstorage/dbms/tdbms/t_dbplatsec5.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200 (2014-06-10)
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// DBMS security policy - testing new APIs.
sl@0
    15
// This test app has "None" capabilities,
sl@0
    16
// The UID policy file is 11335579.spd.
sl@0
    17
// The test uses C:TESTDB.DB secure shared database, which has tables A, B and C, each of them
sl@0
    18
// with at least one record.
sl@0
    19
// Please, ensure that t_dbenvcreate test is executed before t_dbplatsec<N>/t_dbplatsecperf tests!
sl@0
    20
// Please, ensure that t_dbenvdestroy test is executed after t_dbplatsec<N>/t_dbplatsecperf tests!
sl@0
    21
// 
sl@0
    22
//
sl@0
    23
sl@0
    24
#include <e32test.h>
sl@0
    25
#include <d32dbms.h>
sl@0
    26
#include "t_dbplatsecutl.h"
sl@0
    27
sl@0
    28
const TUid KSecureDbUid = {0x11335579};
sl@0
    29
_LIT(KSecure,	"SECURE");
sl@0
    30
_LIT(KDbName,	"C:TestDB.DB");
sl@0
    31
_LIT(KTblNameA,	"A");
sl@0
    32
_LIT(KTblNameB,	"B");
sl@0
    33
_LIT(KTblNameC,	"C");
sl@0
    34
sl@0
    35
static RTest 				TheTest(_L("t_dbplatsec5: DBMS platform security testing - 5"));
sl@0
    36
static RDbs 				TheDbs;
sl@0
    37
static RDbNamedDatabase 	TheDb;
sl@0
    38
static RDbTable 			TheTbl;
sl@0
    39
static RDbView 				TheView;
sl@0
    40
sl@0
    41
TDBSCUtils 	TheDbscUtils(TheTest, NULL);
sl@0
    42
sl@0
    43
/**
sl@0
    44
@SYMTestCaseID SYSLIB-DBMS-CT-0018
sl@0
    45
@SYMTestCaseDesc Open table test.
sl@0
    46
				 This test app has no capabilities and it is restricted to be able to
sl@0
    47
				 open tables B and C in read-only mode.
sl@0
    48
@SYMTestPriority High
sl@0
    49
@SYMTestActions  Open table test.
sl@0
    50
@SYMTestExpectedResults The test must not fail.
sl@0
    51
@SYMREQ REQ2429
sl@0
    52
                 DBMS shall provide an API to apply security policies to database tables.
sl@0
    53
*/
sl@0
    54
static void Test1L()
sl@0
    55
	{
sl@0
    56
	TheTest.Printf(_L("An attempt to open tables in update/insert mode\n"));
sl@0
    57
	//The test must fail, because the test app cannot satisfy table A, B, C, policy W.
sl@0
    58
	TInt err = TheTbl.Open(TheDb, KTblNameA);
sl@0
    59
	TEST2(err, KErrPermissionDenied);
sl@0
    60
	err = TheTbl.Open(TheDb, KTblNameB);
sl@0
    61
	TEST2(err, KErrPermissionDenied);
sl@0
    62
	err = TheTbl.Open(TheDb, KTblNameC);
sl@0
    63
	TEST2(err, KErrPermissionDenied);
sl@0
    64
sl@0
    65
	TheTest.Printf(_L("An attempt to open tables in read-only mode\n"));
sl@0
    66
	//The test must pass for table B & C, but the test app cannot satisfy table A, policy R.
sl@0
    67
	err = TheTbl.Open(TheDb, KTblNameA, RDbRowSet::EReadOnly);
sl@0
    68
	TEST2(err, KErrPermissionDenied);
sl@0
    69
	err = TheTbl.Open(TheDb, KTblNameB, RDbRowSet::EReadOnly);
sl@0
    70
	TEST2(err, KErrNone);
sl@0
    71
	TheTbl.Close();
sl@0
    72
	err = TheTbl.Open(TheDb, KTblNameC, RDbRowSet::EReadOnly);
sl@0
    73
	TEST2(err, KErrNone);
sl@0
    74
	TheTbl.Close();
sl@0
    75
sl@0
    76
	TheTest.Printf(_L("An attempt to read tables\n"));
sl@0
    77
	//The test must pass for table B & C, but the test app cannot satisfy table A, policy R.
sl@0
    78
	err = TheView.Prepare(TheDb, TDbQuery(_L("SELECT * FROM A")));
sl@0
    79
	TEST2(err, KErrPermissionDenied);
sl@0
    80
	err = TheView.Prepare(TheDb, TDbQuery(_L("SELECT * FROM B")));
sl@0
    81
	TEST2(err, KErrNone);
sl@0
    82
	TInt cnt = TheView.CountL();
sl@0
    83
	TEST(cnt > 0);
sl@0
    84
	TheView.Close();
sl@0
    85
	err = TheView.Prepare(TheDb, TDbQuery(_L("SELECT * FROM C")));
sl@0
    86
	TEST2(err, KErrNone);
sl@0
    87
	cnt = TheView.CountL();
sl@0
    88
	TEST(cnt > 0);
sl@0
    89
	TheView.Close();
sl@0
    90
	}
sl@0
    91
sl@0
    92
/**
sl@0
    93
@SYMTestCaseID			SYSLIB-DBMS-CT-3407
sl@0
    94
@SYMTestCaseDesc		Test for defect DEF103023 - DBMS requires ReadDeviceData and WriteDeviceData capability to read from the db.
sl@0
    95
 						The current test application has no capabilities at all.
sl@0
    96
 						"C:TestDB.DB" database is a secure shared database with:
sl@0
    97
 							- no "READ" polycy (no restrictions apply to the database read operations);
sl@0
    98
 							- "WRITE" policy with "WriteUserData" capability defined;
sl@0
    99
 							- "SCHEMA" policy with "NetworkServices" capability defined;
sl@0
   100
 							- table C has no defined securoty policy, so the database security policy will be used;
sl@0
   101
 						The current test application should be able to:
sl@0
   102
 							- begin/commit/rollback a "read-only" transaction;
sl@0
   103
 						But should fail if:
sl@0
   104
 							- begin a transaction and try to modify the database within the transaction;
sl@0
   105
 						This test function asserts the test cases described above.
sl@0
   106
@SYMTestPriority		High
sl@0
   107
@SYMTestActions			Test for defect DEF103023 - DBMS requires ReadDeviceData and WriteDeviceData capability to read from the db.
sl@0
   108
@SYMTestExpectedResults Test must not fail
sl@0
   109
@SYMDEF					DEF103023
sl@0
   110
*/
sl@0
   111
void DEF103023L()
sl@0
   112
	{
sl@0
   113
	TheTest.Printf(_L("Begin a transaction. Read-only operations tested\n"));
sl@0
   114
	TInt err = TheDb.Begin();
sl@0
   115
	TEST2(err, KErrNone);
sl@0
   116
	TheTest.Printf(_L("Perform some read-only operations inside the transaction\n"));
sl@0
   117
	err = TheView.Prepare(TheDb, _L("SELECT * FROM C"));
sl@0
   118
	TEST2(err, KErrNone);
sl@0
   119
	err = TheView.EvaluateAll();
sl@0
   120
	TEST2(err, KErrNone);
sl@0
   121
	TInt cnt = TheView.CountL();
sl@0
   122
	TEST(cnt > 0);
sl@0
   123
	TBool rc = TheView.FirstL();
sl@0
   124
	TEST(rc);
sl@0
   125
	TheView.GetL();
sl@0
   126
	TInt val = TheView.ColInt32(1);
sl@0
   127
	rc = TheView.LastL();
sl@0
   128
	TEST(rc);
sl@0
   129
	rc = TheView.NextL();
sl@0
   130
	TEST(!rc);
sl@0
   131
	rc = TheView.PreviousL();
sl@0
   132
	TEST(rc);
sl@0
   133
	TheView.BeginningL();
sl@0
   134
	TheView.EndL();
sl@0
   135
	TheView.Close();
sl@0
   136
	TheTest.Printf(_L("Commit a transaction\n"));
sl@0
   137
	err = TheDb.Commit();
sl@0
   138
	TEST2(err, KErrNone);
sl@0
   139
	//
sl@0
   140
	TheTest.Printf(_L("Begin a transaction. Read-only operations tested\n"));
sl@0
   141
	err = TheDb.Begin();
sl@0
   142
	TEST2(err, KErrNone);
sl@0
   143
	err = TheView.Prepare(TheDb, _L("SELECT * FROM C"));
sl@0
   144
	TEST2(err, KErrNone);
sl@0
   145
	err = TheView.EvaluateAll();
sl@0
   146
	TEST2(err, KErrNone);
sl@0
   147
	cnt = TheView.CountL();
sl@0
   148
	TEST(cnt > 0);
sl@0
   149
	TheView.Close();
sl@0
   150
	TheTest.Printf(_L("Rollback a transaction\n"));
sl@0
   151
	TheDb.Rollback();
sl@0
   152
	//
sl@0
   153
	TheTest.Printf(_L("Begin a transaction. Tested operations violate the database security\n"));
sl@0
   154
	err = TheDb.Begin();
sl@0
   155
	TEST2(err, KErrNone);
sl@0
   156
	err = TheView.Prepare(TheDb, _L("SELECT * FROM C"));
sl@0
   157
	TEST2(err, KErrNone);
sl@0
   158
	err = TheView.EvaluateAll();
sl@0
   159
	TEST2(err, KErrNone);
sl@0
   160
	rc = TheView.FirstL();
sl@0
   161
	TEST(rc);
sl@0
   162
	TheView.GetL();
sl@0
   163
	TheTest.Printf(_L("An attempt to update a record within the transaction\n"));
sl@0
   164
	TRAP(err, TheView.UpdateL());
sl@0
   165
	TEST2(err, KErrPermissionDenied);
sl@0
   166
	TheTest.Printf(_L("An attempt to delete a record within the transaction\n"));
sl@0
   167
	TRAP(err, TheView.DeleteL());
sl@0
   168
	TEST2(err, KErrPermissionDenied);
sl@0
   169
	TheTest.Printf(_L("An attempt to insert a record within the transaction\n"));
sl@0
   170
	TRAP(err, TheView.InsertL());
sl@0
   171
	TEST2(err, KErrPermissionDenied);
sl@0
   172
	TheView.Close();
sl@0
   173
	TheTest.Printf(_L("An attempt to modify the database schema within the transaction\n"));
sl@0
   174
	err = TheDb.Execute(_L("CREATE TABLE C2(Id INTEGER, Z INTEGER)"));
sl@0
   175
	TEST2(err, KErrPermissionDenied);
sl@0
   176
	TheTest.Printf(_L("An attempt to execute an INSERT statement within the transaction\n"));
sl@0
   177
	err = TheDb.Execute(_L("INSERT INTO C VALUES(100)"));
sl@0
   178
	TEST2(err, KErrPermissionDenied);
sl@0
   179
	TheTest.Printf(_L("An attempt to modify the database within the transaction using RDbUpdate\n"));
sl@0
   180
	RDbUpdate update;
sl@0
   181
	err = update.Execute(TheDb, _L("INSERT INTO C VALUES(200)"));
sl@0
   182
	TEST2(err, KErrPermissionDenied);
sl@0
   183
	update.Close();
sl@0
   184
	TheTest.Printf(_L("Rollback a transaction\n"));
sl@0
   185
	TheDb.Rollback();
sl@0
   186
	}
sl@0
   187
sl@0
   188
void DoTestL()
sl@0
   189
	{
sl@0
   190
	TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0018 An app with \"None\" capabilities set "));
sl@0
   191
	Test1L();
sl@0
   192
sl@0
   193
	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-3407 DEF103023-DBMS requires ReadDeviceData and WriteDeviceData capability to read from the db "));
sl@0
   194
	DEF103023L();
sl@0
   195
	}
sl@0
   196
sl@0
   197
TInt E32Main()
sl@0
   198
    {
sl@0
   199
	__UHEAP_MARK;
sl@0
   200
	CTrapCleanup* tc = CTrapCleanup::New();
sl@0
   201
	TEST(tc != NULL);
sl@0
   202
sl@0
   203
	TInt err = TheDbs.Connect();
sl@0
   204
	TEST2(err, KErrNone);
sl@0
   205
sl@0
   206
	TBuf<32> format;
sl@0
   207
	TheTest.Printf(_L("Open database\n"));
sl@0
   208
	format.Copy(KSecure);
sl@0
   209
	format.Append(KSecureDbUid.Name());
sl@0
   210
	err = TheDb.Open(TheDbs, KDbName, format);
sl@0
   211
	TEST2(err, KErrNone);
sl@0
   212
sl@0
   213
	TRAP(err, DoTestL());
sl@0
   214
	TEST2(err, KErrNone);
sl@0
   215
sl@0
   216
	TheView.Close();
sl@0
   217
	TheTbl.Close();
sl@0
   218
	TheDb.Close();
sl@0
   219
	TheDbs.Close();
sl@0
   220
sl@0
   221
	TheTest.End();
sl@0
   222
	TheTest.Close();
sl@0
   223
sl@0
   224
	delete tc;
sl@0
   225
sl@0
   226
	__UHEAP_MARKEND;
sl@0
   227
	User::Heap().Check();
sl@0
   228
	return KErrNone;
sl@0
   229
    }