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