os/persistentdata/persistentstorage/dbms/tdbms/t_dbplatsecdb.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
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 - database level.
sl@0
    15
// All tests assume that drive C is presented in the system and is not a ROM drive.
sl@0
    16
// 
sl@0
    17
//
sl@0
    18
sl@0
    19
#include "t_dbplatsecutl.h"
sl@0
    20
#include "t_dbplatsecdef.h"
sl@0
    21
#include "t_dbplatsecdb.h"
sl@0
    22
sl@0
    23
static TColDef const KColumns[]=
sl@0
    24
	{
sl@0
    25
	{_S("ID"), EDbColInt32, TDbCol::ENotNull | TDbCol::EAutoIncrement},
sl@0
    26
	{_S("DATA1"), EDbColInt32, TDbCol::ENotNull},
sl@0
    27
	{_S("DATA2"), EDbColInt32, TDbCol::ENotNull},
sl@0
    28
	{0}
sl@0
    29
	};
sl@0
    30
sl@0
    31
sl@0
    32
//Open database tests
sl@0
    33
static void OpenTestL()
sl@0
    34
	{
sl@0
    35
	RFs fileSess;
sl@0
    36
	CleanupClosePushL(fileSess);
sl@0
    37
	TEST2(fileSess.Connect(), KErrNone);
sl@0
    38
	
sl@0
    39
	TBuf<32> fmt;
sl@0
    40
	fmt.Copy(KSecure);
sl@0
    41
	fmt.Append(KSecureDbUid.Name());
sl@0
    42
	
sl@0
    43
	TheTest.Printf(_L("Open secure shared database\n"));
sl@0
    44
	TInt err = TheDb1.Open(TheDbs, KDb1Name, fmt);
sl@0
    45
	TheDb1.Close();
sl@0
    46
	TEST2(err, KErrNone);
sl@0
    47
	
sl@0
    48
sl@0
    49
	TheTest.Printf(_L("An attempt to open it as a secure shared database - no format string\n"));
sl@0
    50
	err = TheDb1.Open(TheDbs, KDb1Name);
sl@0
    51
	TheDb1.Close();
sl@0
    52
	TEST2(err, KErrNotFound);//Treated as a non-secure, not found database.
sl@0
    53
sl@0
    54
	TheTest.Printf(_L("An attempt to open it as a non-secure shared database\n"));
sl@0
    55
	_LIT(KNSDbName, "C:\\PRiVatE\\100012A5\\A.dB");
sl@0
    56
	err = TheDb1.Open(TheDbs, KNSDbName);
sl@0
    57
	TheDb1.Close();
sl@0
    58
	TEST2(err, KErrPermissionDenied);
sl@0
    59
    _LIT(KNSDbName2, "C:\\PRiVatE\\100012A5\\DBS_11335577_A.Db");
sl@0
    60
    if(PlatSec::ConfigSetting(PlatSec::EPlatSecEnforcement) && 
sl@0
    61
       PlatSec::IsCapabilityEnforced(ECapabilityAllFiles))
sl@0
    62
        {
sl@0
    63
	    err = TheDb1.Open(fileSess, KNSDbName2);
sl@0
    64
	    TheDb1.Close();
sl@0
    65
	    TEST2(err, KErrPermissionDenied);
sl@0
    66
sl@0
    67
		TheTest.Printf(_L("An attempt to create it as a non-secure shared database\n"));
sl@0
    68
		err = TheDb1.Create(fileSess, KNSDbName2);
sl@0
    69
		TheDb1.Close();
sl@0
    70
		TEST2(err, KErrPermissionDenied);
sl@0
    71
        }
sl@0
    72
sl@0
    73
	TheTest.Printf(_L("--An attempt to open a database, which security policy cannot be satisfied by the current test application\n"));	
sl@0
    74
	fmt.Copy(KSecure);
sl@0
    75
	fmt.Append(KSecureDbUid2.Name());
sl@0
    76
	err = TheDb1.Open(TheDbs, KProtDb3CName, fmt);
sl@0
    77
	TheDb1.Close();
sl@0
    78
	TEST2(err, KErrPermissionDenied);
sl@0
    79
	
sl@0
    80
	CleanupStack::PopAndDestroy(&fileSess);
sl@0
    81
	}
sl@0
    82
sl@0
    83
//RDbDatabase method calls test. All calls have to pass with KErrNone,
sl@0
    84
//because the test application has enough rights to do any R/W/S database operation.
sl@0
    85
static void DbCallTestL()
sl@0
    86
	{
sl@0
    87
	TBuf<32> fmt;
sl@0
    88
	fmt.Copy(KSecure);
sl@0
    89
	fmt.Append(KSecureDbUid.Name());
sl@0
    90
	
sl@0
    91
	TheTest.Printf(_L("Open()\n"));
sl@0
    92
	TInt err = TheDb1.Open(TheDbs, KDb1Name, fmt);
sl@0
    93
	
sl@0
    94
	TheTest.Printf(_L("IsDamaged()\n"));
sl@0
    95
	TBool res = TheDb1.IsDamaged();
sl@0
    96
	TEST(!res);
sl@0
    97
	
sl@0
    98
	TheTest.Printf(_L("Recover()\n"));
sl@0
    99
	err = TheDb1.Recover();
sl@0
   100
	TEST2(err, KErrNone);
sl@0
   101
	
sl@0
   102
	TheTest.Printf(_L("UpdateStats()\n"));
sl@0
   103
	err = TheDb1.UpdateStats();
sl@0
   104
	TEST2(err, KErrNone);
sl@0
   105
	
sl@0
   106
	TheTest.Printf(_L("Size()\n"));
sl@0
   107
	RDbDatabase::TSize size = TheDb1.Size();
sl@0
   108
	
sl@0
   109
	TheTest.Printf(_L("Compact()\n"));
sl@0
   110
	err = TheDb1.Compact();
sl@0
   111
	TEST2(err, KErrNone);
sl@0
   112
sl@0
   113
	
sl@0
   114
	TheTest.Printf(_L("Begin()/Commit()\n"));
sl@0
   115
	err = TheDb1.Begin();
sl@0
   116
	TEST2(err, KErrNone);
sl@0
   117
	res = TheDb1.InTransaction();
sl@0
   118
	TEST(res);
sl@0
   119
	err = TheDb1.Commit();
sl@0
   120
	TEST2(err, KErrNone);
sl@0
   121
	
sl@0
   122
	TheTest.Printf(_L("Begin()/Rollback()\n"));
sl@0
   123
	err = TheDb1.Begin();
sl@0
   124
	TEST2(err, KErrNone);
sl@0
   125
	res = TheDb1.InTransaction();
sl@0
   126
	TEST(res);
sl@0
   127
	TheDb1.Rollback();
sl@0
   128
	
sl@0
   129
	TheTest.Printf(_L("CreateTable()\n"));
sl@0
   130
	CDbColSet* colset = TDBSCUtils::CreateColSetLC(KColumns);
sl@0
   131
	_LIT(KTblName1, "A1");
sl@0
   132
	err = TheDb1.CreateTable(KTblName1, *colset);
sl@0
   133
	TEST2(err, KErrNone);
sl@0
   134
	_LIT(KTblName2, "A2");
sl@0
   135
	err = TheDb1.CreateTable(KTblName2, *colset);
sl@0
   136
	TEST2(err, KErrNone);
sl@0
   137
	
sl@0
   138
	TheTest.Printf(_L("DropTable()\n"));
sl@0
   139
	err = TheDb1.DropTable(KTblName2);
sl@0
   140
	TEST2(err, KErrNone);
sl@0
   141
sl@0
   142
	TheTest.Printf(_L("AlterTable()\n"));
sl@0
   143
	err = TheDb1.AlterTable(KTblName1, *colset);
sl@0
   144
	TEST2(err, KErrNone);
sl@0
   145
	
sl@0
   146
	TheTest.Printf(_L("CreateIndex()\n"));
sl@0
   147
	CDbKey* key = TDBSCUtils::CreateKeyLC(KCol2);
sl@0
   148
	err = TheDb1.CreateIndex(KCol2, KTblName1, *key);
sl@0
   149
	TEST2(err, KErrNone);
sl@0
   150
	
sl@0
   151
	TheTest.Printf(_L("DropIndex()\n"));
sl@0
   152
	err = TheDb1.DropIndex(KCol2, KTblName1);
sl@0
   153
	TEST2(err, KErrNone);
sl@0
   154
	
sl@0
   155
	TheTest.Printf(_L("TableNamesL()\n"));
sl@0
   156
	_LIT(KTblName3, "DdD");
sl@0
   157
	_LIT(KTblName4, "FFf");
sl@0
   158
	_LIT(KTblName5, "Ggg");
sl@0
   159
	TDBSCUtils::CreateTableL(TheDb1, KTblName3, KColumns);
sl@0
   160
	TDBSCUtils::CreateTableL(TheDb1, KTblName4, KColumns);
sl@0
   161
	TDBSCUtils::CreateTableL(TheDb1, KTblName5, KColumns);
sl@0
   162
	CDbTableNames* tblNames = TheDb1.TableNamesL();
sl@0
   163
	TInt cnt = tblNames->Count();
sl@0
   164
	TEST(cnt == 4);
sl@0
   165
	TInt i;
sl@0
   166
	for(i=0;i<cnt;++i)
sl@0
   167
		{
sl@0
   168
		const TDesC& name = (*tblNames)[i];
sl@0
   169
		TheTest.Printf(_L("%02d, %S\n"), i + 1, &name);
sl@0
   170
		}
sl@0
   171
	delete tblNames;
sl@0
   172
		
sl@0
   173
	TheTest.Printf(_L("ColSetL()\n"));
sl@0
   174
	CDbColSet* cs1 = TheDb1.ColSetL(KTblName3);
sl@0
   175
	cnt = cs1->Count();
sl@0
   176
	for(i=0;i<cnt;++i)
sl@0
   177
		{
sl@0
   178
		const TDbCol& dbc = (*cs1)[i + 1];
sl@0
   179
		TheTest.Printf(_L("%02d, %S\n"), i + 1, &dbc.iName);
sl@0
   180
		}
sl@0
   181
	delete cs1;
sl@0
   182
	
sl@0
   183
	TheTest.Printf(_L("IndexNamesL()\n"));
sl@0
   184
	CDbKey* tblKey1 = TDBSCUtils::CreateKeyLC(KCol2);
sl@0
   185
	err = TheDb1.CreateIndex(KCol2, KTblName1, *tblKey1);
sl@0
   186
	TEST2(err, KErrNone);
sl@0
   187
	CleanupStack::PopAndDestroy(tblKey1);
sl@0
   188
	CDbIndexNames* dbiNames = TheDb1.IndexNamesL(KTblName1);
sl@0
   189
	cnt = dbiNames->Count();
sl@0
   190
	for(i=0;i<cnt;++i)
sl@0
   191
		{
sl@0
   192
		const TDesC& name = (*dbiNames)[i];
sl@0
   193
		TheTest.Printf(_L("%02d, %S\n"), i + 1, &name);
sl@0
   194
		}
sl@0
   195
	delete dbiNames;
sl@0
   196
	
sl@0
   197
	TheTest.Printf(_L("KeyL()\n"));
sl@0
   198
	CDbKey* tblKey2 = TheDb1.KeyL(KCol2, KTblName1);
sl@0
   199
	delete tblKey2;
sl@0
   200
		
sl@0
   201
	CleanupStack::PopAndDestroy(key);
sl@0
   202
	CleanupStack::PopAndDestroy(colset);
sl@0
   203
	TheDb1.Close();
sl@0
   204
	}
sl@0
   205
sl@0
   206
static void SQLTestL()
sl@0
   207
	{
sl@0
   208
	TDBSCUtils::DeleteDatabase(TheDbs, KSecureDbUid, KDb1Name);
sl@0
   209
	TheDb1.Close();
sl@0
   210
	TheDb1 = TDBSCUtils::CreateDatabase(TheDbs, KSecureDbUid, KDb1Name);
sl@0
   211
	
sl@0
   212
	TheTest.Printf(_L("Create table\n"));
sl@0
   213
	TInt err = TheDb1.Execute(_L("crEAte tabLe a (id couNter)"));
sl@0
   214
	TEST2(err, KErrNone);
sl@0
   215
	
sl@0
   216
	TheTest.Printf(_L("Alter table\n"));
sl@0
   217
	err = TheDb1.Execute(_L("ALter TablE a aDd DaTA1 CHaR(20)"));
sl@0
   218
	TEST2(err, KErrNone);
sl@0
   219
	
sl@0
   220
	err = TheDb1.Execute(_L("alter table A add DATA2 INTEGER"));
sl@0
   221
	TEST2(err, KErrNone);
sl@0
   222
	
sl@0
   223
	err = TheDb1.Execute(_L("ALTEr tABLe A dROp DaTa1"));
sl@0
   224
	TEST2(err, KErrNone);
sl@0
   225
sl@0
   226
	TheTest.Printf(_L("Drop table\n"));
sl@0
   227
	err = TheDb1.Execute(_L("DRop tABle a"));
sl@0
   228
	TEST2(err, KErrNone);
sl@0
   229
sl@0
   230
	err = TheDb1.Execute(_L("crEAte tabLe a (iD coUNteR)"));
sl@0
   231
	TEST2(err, KErrNone);
sl@0
   232
	err = TheDb1.Execute(_L("altER taBLe A add daTA2 InteGER"));
sl@0
   233
	TEST2(err, KErrNone);
sl@0
   234
	err = TheDb1.Execute(_L("creaTE tAblE b (Id counTEr)"));
sl@0
   235
	TEST2(err, KErrNone);
sl@0
   236
	err = TheDb1.Execute(_L("alter TAbLe B aDd DAtA2 INTEgER"));
sl@0
   237
	TEST2(err, KErrNone);
sl@0
   238
sl@0
   239
	TheTest.Printf(_L("Create index\n"));
sl@0
   240
	err = TheDb1.Execute(_L("CREAte INdeX idX oN A(dATA2)"));
sl@0
   241
	TEST2(err, KErrNone);
sl@0
   242
sl@0
   243
	TheTest.Printf(_L("Drop index\n"));
sl@0
   244
	err = TheDb1.Execute(_L("DroP IndEX idx frOm a"));
sl@0
   245
	TEST2(err, KErrNone);
sl@0
   246
sl@0
   247
	TheTest.Printf(_L("Insert\n"));
sl@0
   248
	TInt cnt = TheDb1.Execute(_L("INSerT IntO A (DATa2) VALuES(45)"));
sl@0
   249
	TEST(cnt > 0);
sl@0
   250
sl@0
   251
	TheTest.Printf(_L("Update\n"));
sl@0
   252
	cnt = TheDb1.Execute(_L("UPDATe A SeT DAtA2=56 WheRE iD = 0"));
sl@0
   253
	TEST(cnt > 0);
sl@0
   254
sl@0
   255
	TheTest.Printf(_L("Delete\n"));
sl@0
   256
	cnt = TheDb1.Execute(_L("INSErt INtO B (DATA2) VALUES(100)"));
sl@0
   257
	TEST(cnt > 0);
sl@0
   258
	cnt = TheDb1.Execute(_L("INSERT INTo b (DATA2) VALUES(200)"));
sl@0
   259
	TEST(cnt > 0);
sl@0
   260
	cnt = TheDb1.Execute(_L("delete FROM B where ID = 1"));
sl@0
   261
	TEST(cnt > 0);
sl@0
   262
sl@0
   263
	TheDb1.Close();
sl@0
   264
	}
sl@0
   265
sl@0
   266
static void TwoDatabasesTestL()
sl@0
   267
    {
sl@0
   268
	TBuf<32> fmt;
sl@0
   269
	fmt.Copy(KSecure);
sl@0
   270
	fmt.Append(KSecureDbUid.Name());
sl@0
   271
	
sl@0
   272
    RDbNamedDatabase db1;
sl@0
   273
	TheTest.Printf(_L("Open secure shared database-1\n"));
sl@0
   274
	TInt err = db1.Open(TheDbs, KDb1Name, fmt);
sl@0
   275
	TEST2(err, KErrNone);
sl@0
   276
sl@0
   277
    RDbNamedDatabase db2;
sl@0
   278
	TheTest.Printf(_L("Open secure shared database-2\n"));
sl@0
   279
	err = db2.Open(TheDbs, KDb1Name, fmt);
sl@0
   280
	TEST2(err, KErrNone);
sl@0
   281
sl@0
   282
    db2.Close();
sl@0
   283
    db1.Close();
sl@0
   284
    }
sl@0
   285
sl@0
   286
/**
sl@0
   287
@SYMTestCaseID SYSLIB-DBMS-CT-0019
sl@0
   288
@SYMTestCaseDesc RDbNamedDatabase method calls test for a secure shared database.
sl@0
   289
                 Every method of RDbNamedDatabase class and its base class too, is called
sl@0
   290
				 and the result - asserted.
sl@0
   291
@SYMTestPriority High
sl@0
   292
@SYMTestActions  RDbNamedDatabase method calls test.
sl@0
   293
@SYMTestExpectedResults The test must not fail.
sl@0
   294
@SYMREQ          REQ2429
sl@0
   295
                 DBMS shall provide an API to apply security policies to database tables.
sl@0
   296
*/
sl@0
   297
void DoDbTestL()
sl@0
   298
	{
sl@0
   299
	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0019 "));	
sl@0
   300
	TDBSCUtils::DeleteDatabase(TheDbs, KSecureDbUid, KDb1Name);
sl@0
   301
	TheDb1 = TDBSCUtils::CreateDatabase(TheDbs, KSecureDbUid, KDb1Name);
sl@0
   302
	TheDb1.Close();
sl@0
   303
sl@0
   304
	TheTest.Next(_L("Open secure database test"));	
sl@0
   305
	::OpenTestL();
sl@0
   306
	
sl@0
   307
	TheTest.Next(_L("Secure Database calls test"));	
sl@0
   308
	::DbCallTestL();
sl@0
   309
sl@0
   310
	TheTest.Next(_L("SQL test"));	
sl@0
   311
	::SQLTestL();
sl@0
   312
sl@0
   313
	TheTest.Next(_L("Two databases test"));	
sl@0
   314
	::TwoDatabasesTestL();
sl@0
   315
	}