commsconfig/commsdatabaseshim/TE_commdb/src/step_059_xx.cpp
changeset 72 ae47d0499bee
equal deleted inserted replaced
68:5da8188e392b 72:ae47d0499bee
       
     1 //
       
     2 // Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 // All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of "Eclipse Public License v1.0"
       
     6 // which accompanies this distribution, and is available
       
     7 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 //
       
     9 // Initial Contributors:
       
    10 // Nokia Corporation - initial contribution.
       
    11 //
       
    12 // Contributors:
       
    13 //
       
    14 // Description:
       
    15 //
       
    16 //
       
    17 // 
       
    18  
       
    19 // EPOC includes
       
    20 #include <e32base.h>
       
    21 #include <commdb.h>
       
    22 #include <d32comm.h>
       
    23 #include <cdbstore.h>
       
    24 
       
    25 // Test system includes
       
    26 #include "Teststepcommdb.h"
       
    27 #include "comms-infras/commdb/protection/protectdb.h"
       
    28 #include "comms-infras/commdb/protection/protectcpdb.h"
       
    29 #include <cdbpreftable.h>
       
    30 
       
    31 #include "step_059_xx.h"
       
    32 
       
    33 #define ACCESS_TYPE_TABLE		_S("AccessTypeTable")
       
    34 #define TABLE_ACC_NAME          _S("TableName")
       
    35 #define TABLE_ACC_TYPE          _S("AccessType")
       
    36 
       
    37 /////////////////////
       
    38 //	Test step 059.01
       
    39 /////////////////////
       
    40 
       
    41 //TestProtectRecords
       
    42 CCommDbTest059_01::CCommDbTest059_01()
       
    43 {
       
    44 	// store the name of this test case
       
    45 	SetTestStepName(_L("step_059_01"));
       
    46 }
       
    47 
       
    48 CCommDbTest059_01::~CCommDbTest059_01()
       
    49 {
       
    50 	delete iTheDb;
       
    51 }
       
    52 
       
    53 TVerdict CCommDbTest059_01::doTestStepPreambleL()
       
    54 	{	
       
    55 	iTheDb = CCommsDatabase::NewL(EDatabaseTypeIAP);
       
    56         SetTestStepResult(EPass);	return TestStepResult();
       
    57 	}
       
    58 
       
    59 enum TVerdict CCommDbTest059_01::doTestStepL( void )
       
    60 	{
       
    61 	INFO_PRINTF1(_L("Combinational test (Only for Conn Pref)"));
       
    62 	SetTestStepResult(EPass);
       
    63 
       
    64 // Try a combination of operations on a table, as follows:
       
    65 // 1) protect record
       
    66 // 2) attempt update to protected record
       
    67 // 3) attempt update to unprotected record
       
    68 // 4) attempt delete of unprotected record
       
    69 // 5) protect table
       
    70 // 6) attempt record insertion
       
    71 // 7) attempt protected record deletion & update
       
    72 // 8) attempt unprotected record deletion and update
       
    73 // 9) unprotect table
       
    74 // 10) attempt record insertion
       
    75 // 11) attempt record deletion
       
    76 // 12) attempt update to protected record
       
    77 // 13) attempt update to unprotected record
       
    78 // 15) unprotect record
       
    79 // 15) attempt update to formerly unprotected record
       
    80 // 16) attempt update to unprotected record
       
    81 //
       
    82 	CCommsDbProtectConnectPrefTableView* table = (CCommsDbProtectConnectPrefTableView *)iTheDb->OpenConnectionPrefTableLC();
       
    83 			
       
    84 	TESTL(iTheDb->BeginTransaction()==KErrNone);
       
    85 
       
    86 	if(table->GotoFirstRecord()==KErrNone)
       
    87 		{
       
    88 		// Add another record to the table (so there should be 3)
       
    89 		InsertRecordL(table,KErrNone);
       
    90 
       
    91 		TESTL(table->GotoFirstRecord()==KErrNone);
       
    92 		TESTL(table->GotoNextRecord()==KErrNone);
       
    93 
       
    94 		// 1) Protect the second record
       
    95 		CheckRecordAccessL(table,RDbRowSet::EUpdatable); 
       
    96 		ProtectRecordL(table,KErrNone);
       
    97 		CheckRecordAccessL(table,RDbRowSet::EReadOnly); 
       
    98 		
       
    99 		// 2) Check record cannot be updated
       
   100 		UpdateRecordL(table,KErrAccessDenied);
       
   101 
       
   102 		// Go to the first record (unprotected)
       
   103 
       
   104 		TESTL(table->GotoFirstRecord()==KErrNone);
       
   105 		CheckRecordAccessL(table,RDbRowSet::EUpdatable); 
       
   106 
       
   107 		// 3) Check it can be updated
       
   108 		UpdateRecordL(table,KErrNone);
       
   109 
       
   110 		// 4) Check it can be deleted
       
   111 		DeleteRecordL(table,KErrNone);
       
   112 
       
   113 		//(which means that the new first record should be protected.....)
       
   114 		TESTL(table->GotoFirstRecord()==KErrNone);
       
   115 		CheckRecordAccessL(table,RDbRowSet::EReadOnly); 
       
   116 
       
   117 		// 5) Unprotect the first record which should be protected
       
   118 		CheckRecordAccessL(table,RDbRowSet::EReadOnly); 
       
   119 		UnprotectRecordL(table,KErrNone);
       
   120 		CheckRecordAccessL(table,RDbRowSet::EUpdatable); 
       
   121 		
       
   122 		// 6) Attempt update to formerly protected record (should succeed)
       
   123 		UpdateRecordL(table,KErrNone);
       
   124 		
       
   125 		// End the transaction, as a protect table is a separate one.
       
   126 		TESTL(iTheDb->CommitTransaction()==KErrNone);
       
   127 
       
   128 		// 7) Protect the table
       
   129 		TESTL(table->ProtectTable()==KErrNone);
       
   130 
       
   131 		TESTL(iTheDb->BeginTransaction()==KErrNone);
       
   132 
       
   133 		// 8) Check a record can't be inserted
       
   134 		InsertRecordL(table,KErrAccessDenied);
       
   135 
       
   136 		// 9) Check a record can't be deleted or updated
       
   137 		DeleteRecordL(table,KErrAccessDenied);
       
   138 		UpdateRecordL(table,KErrAccessDenied);
       
   139 		
       
   140 		// End the transaction, as unprotect table is a separate one.
       
   141 		TESTL(iTheDb->CommitTransaction()==KErrNone);
       
   142 
       
   143 		// 10) Unprotect the table
       
   144 		TESTL(table->UnprotectTable()==KErrNone);
       
   145 	
       
   146 		table->GotoFirstRecord();
       
   147 		TESTL(iTheDb->BeginTransaction()==KErrNone);
       
   148 		// 11) Check a record can be inserted
       
   149 		InsertRecordL(table,KErrNone);
       
   150 
       
   151 		// 12) Check an unprotected record can be deleted, but a protected one can't
       
   152 		// Again, yes it can so don't test this here.
       
   153 		DeleteRecordL(table,KErrNone);
       
   154 
       
   155 		// 13) Check the unprotected record can be updated
       
   156 		// Delete a record so there's only one left. Otherwise an update now will
       
   157 		// fail validation due to duplicate records.
       
   158 		TESTL(table->GotoFirstRecord()==KErrNone);
       
   159 		DeleteRecordL(table,KErrNone);
       
   160 		TESTL(table->GotoFirstRecord()==KErrNone);
       
   161 		UpdateRecordL(table,KErrNone);
       
   162 		
       
   163 		// 14) Finally check that last record can be deleted
       
   164 		TESTL(table->GotoFirstRecord()==KErrNone);
       
   165 		DeleteRecordL(table,KErrNone);
       
   166 		}
       
   167 	TESTL(iTheDb->CommitTransaction()==KErrNone);
       
   168 
       
   169 	SetTestStepResult(EPass);	INFO_PRINTF1(_L("\t\tSUCCESS"));
       
   170 
       
   171 	CleanupStack::PopAndDestroy(); 
       
   172 	return TestStepResult();
       
   173 	}
       
   174 
       
   175 
       
   176 
       
   177 /////////////////////
       
   178 //	Test step 060.01
       
   179 /////////////////////
       
   180 
       
   181 ///Panic Test
       
   182 CCommDbTest060_01::CCommDbTest060_01()
       
   183 {
       
   184 	// store the name of this test case
       
   185 	SetTestStepName(_L("step_060_01"));
       
   186 
       
   187 }
       
   188 
       
   189 CCommDbTest060_01::~CCommDbTest060_01()
       
   190 	{
       
   191 	delete iTheDb;
       
   192 	}
       
   193 
       
   194 TVerdict CCommDbTest060_01::doTestStepPreambleL()
       
   195 	{	
       
   196 	iTheDb = CCommsDatabase::NewL(EDatabaseTypeIAP);
       
   197         SetTestStepResult(EPass);	return TestStepResult();
       
   198 	}
       
   199 
       
   200 enum TVerdict CCommDbTest060_01::doTestStepL( void )
       
   201 	{
       
   202 	SetTestStepResult(EFail);
       
   203 	//turn-off JIT debugging...
       
   204 //	User::SetJustInTime(EFalse);
       
   205 
       
   206 	// Use the same table as step_055_01
       
   207 	CCommsDbProtectTableView* table = NULL;
       
   208 	table = (CCommsDbProtectTableView *)iTheDb->OpenTableLC(TPtrC(DIAL_OUT_ISP));
       
   209 
       
   210 	CheckTableIsNotProtectedL(table);
       
   211 
       
   212 	// Protection uses transaction, transaction within transaction is not allowed
       
   213 	// this test ensures that the ProtectTable code handles this correctly
       
   214 	if (table->GotoFirstRecord() == KErrNone)
       
   215 		{
       
   216 	    if (iTheDb->BeginTransaction() == KErrNone)
       
   217 			{
       
   218 			// Set protection from within a transaction
       
   219 			TESTL(table->ProtectTable()==KErrNone);
       
   220 	
       
   221 			CheckTableIsProtectedL(table);
       
   222 			SetTestStepResult(EPass);			INFO_PRINTF1(_L("\t\tSUCCESS"));
       
   223 				
       
   224 			iTheDb->RollbackTransaction();
       
   225 			}
       
   226 		}
       
   227 	CleanupStack::PopAndDestroy(table);
       
   228 	
       
   229 	return TestStepResult();
       
   230 	}
       
   231 
       
   232 ///////////////////////////////////////////
       
   233 //	Test step 060.02
       
   234 //This test case is for testing the fix for 
       
   235 //DEF035522: CommDb protection is easily circumvented using public APIs
       
   236 ////////////////////////////////////////////
       
   237 
       
   238 ///Panic Test
       
   239 CCommDbTest060_02::CCommDbTest060_02()
       
   240 {
       
   241 	// store the name of this test case
       
   242 	SetTestStepName(_L("step_060_02"));
       
   243 
       
   244 }
       
   245 
       
   246 CCommDbTest060_02::~CCommDbTest060_02()
       
   247 {
       
   248 
       
   249 }
       
   250 
       
   251 enum TVerdict CCommDbTest060_02::doTestStepL( void )
       
   252 	{
       
   253 
       
   254 	SetTestStepResult(EPass);	CCommsDatabase* db = CCommsDatabase::NewL();
       
   255 	CleanupStack::PushL(db);
       
   256 	//Protect ISP the table
       
   257 	INFO_PRINTF1(_L("Protect ISP table"));
       
   258 	TRAPD(protect, ProtectISPTableL(db));
       
   259 	if( protect != KErrNone)
       
   260 		{
       
   261 		INFO_PRINTF1(_L("Failed to protect ISP table"));
       
   262          SetTestStepResult(EFail);		return TestStepResult();
       
   263 		}
       
   264 	//try to remove the protection using public APIs
       
   265 	INFO_PRINTF1(_L("Try to remove the protection using public APIs"));
       
   266 	TRAPD(unprotect, RemoveProtectionL(db));
       
   267 	//Should return KErrNotFound
       
   268 	if (unprotect != KErrNotFound)
       
   269 		{
       
   270 		INFO_PRINTF1(_L("Protection settings broken using public APIs"));
       
   271          SetTestStepResult(EFail);		return TestStepResult();
       
   272 		}
       
   273 	INFO_PRINTF1(_L("Table is well protected!! "));
       
   274 	CleanupStack::PopAndDestroy(db);
       
   275 	return TestStepResult();
       
   276 	}
       
   277 
       
   278 void CCommDbTest060_02::ProtectISPTableL(CCommsDatabase* aDb)
       
   279 //
       
   280 //  Protect the DialOutISP table using the hidden APIs
       
   281 //
       
   282 	{
       
   283 
       
   284 	CCommsDbProtectTableView* view = (CCommsDbProtectTableView*) aDb->OpenTableLC(TPtrC(DIAL_OUT_ISP));
       
   285     if(view->GotoFirstRecord() == KErrNone)
       
   286 		{
       
   287 		User::LeaveIfError(view->ProtectTable());
       
   288 		}
       
   289 
       
   290 	CleanupStack::PopAndDestroy(view);
       
   291 	}
       
   292 
       
   293 
       
   294 void CCommDbTest060_02::RemoveProtectionL(CCommsDatabase* aDb)
       
   295 //
       
   296 //  Use the public CommDb APIs to modify the AccessTypeTable
       
   297 //
       
   298 	{
       
   299 	
       
   300 	aDb->ShowHiddenRecords();
       
   301 	//should fail here as its trying to open ACCESS_TYPE_TABLE
       
   302 	//leaves with KErrNotFound
       
   303 	CCommsDbTableView* view = aDb->OpenTableLC(TPtrC(ACCESS_TYPE_TABLE));
       
   304 	
       
   305 	//Shouldn't get here..
       
   306 	INFO_PRINTF1(_L("Contents of Access Type table:"));
       
   307 	while (view->GotoNextRecord() == KErrNone)
       
   308 		{
       
   309 		// display the current access settings
       
   310 		TBuf<KCommsDbSvrMaxFieldLength> name;
       
   311 		view->ReadTextL(TPtrC(TABLE_ACC_NAME), name);
       
   312 
       
   313 		TUint32 accessType;
       
   314 		view->ReadUintL(TPtrC(TABLE_ACC_TYPE), accessType);
       
   315 
       
   316 		RDebug::Print(_L("Table: '%S'\tAccess type: %d"), &name, accessType);
       
   317 
       
   318 		// now set the access type to 'updateable'
       
   319 		view->UpdateRecord();
       
   320 		view->WriteUintL(TPtrC(TABLE_ACC_TYPE), (TUint32)RDbRowSet::EUpdatable);
       
   321 		view->PutRecordChanges();
       
   322 
       
   323 		// display the new access settings
       
   324 		view->ReadUintL(TPtrC(TABLE_ACC_TYPE), accessType);
       
   325 		INFO_PRINTF3(_L("Table: '%S'\tNew access type: %d"), &name, accessType);
       
   326 		}
       
   327 
       
   328 	CleanupStack::PopAndDestroy(view);
       
   329 	}