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