sl@0: // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: sl@0: #include "t_sqloom.h" sl@0: #include "SqlResourceTester.h" //TSqlResourceTester sl@0: sl@0: sl@0: TInt TheHandleCount1B = 0; sl@0: TInt TheHandleCount2B = 0; sl@0: TInt TheAllocatedCellsCountB = 0; sl@0: TOomTestType TheOomTestType[2] = {EClientSideTest, EServerSideTest}; sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////////////// sl@0: /////////////// OOM tests - utility functions ////////////////////////// sl@0: /////////////////////////////////////////////////////////////////////////////////////// sl@0: sl@0: void DeleteTestFiles() sl@0: { sl@0: (void)RSqlDatabase::Delete(KSecureAttachDb); sl@0: (void)RSqlDatabase::Delete(KAttachDb); sl@0: (void)RSqlDatabase::Delete(KSecureTestDb); sl@0: (void)RSqlDatabase::Delete(KTestDb); sl@0: (void)RSqlDatabase::Delete(KSecureDb2); sl@0: (void)RSqlDatabase::Delete(KTestDb2); sl@0: (void)RSqlDatabase::Delete(KPrivateTestDb); sl@0: } sl@0: sl@0: void Check(TInt64 aValue, TInt aLine) sl@0: { sl@0: if(!aValue) sl@0: { sl@0: DeleteTestFiles(); sl@0: TheTest(EFalse, aLine); sl@0: } sl@0: } sl@0: sl@0: void Check(TInt64 aValue, TInt64 aExpected, TInt aLine) sl@0: { sl@0: if(aValue != aExpected) sl@0: { sl@0: DeleteTestFiles(); sl@0: RDebug::Print(_L("*** Expected error: %ld, got: %ld\r\n"), aExpected, aValue); sl@0: TheTest(EFalse, aLine); sl@0: } sl@0: } sl@0: sl@0: void CreateTestDir() sl@0: { sl@0: RFs fs; sl@0: TInt err = fs.Connect(); sl@0: TEST2(err, KErrNone); sl@0: sl@0: err = fs.MkDir(KTestDir); sl@0: TEST(err == KErrNone || err == KErrAlreadyExists); sl@0: sl@0: err = fs.CreatePrivatePath(EDriveC); sl@0: TEST(err == KErrNone || err == KErrAlreadyExists); sl@0: sl@0: fs.Close(); sl@0: } sl@0: sl@0: void PrepareAttachFromHandle() sl@0: { sl@0: (void)RSqlDatabase::Delete(KAttachDb); sl@0: (void)RSqlDatabase::Delete(KPrivateTestDb); sl@0: sl@0: RSqlDatabase db; sl@0: TInt err = db.Create(KAttachDb); sl@0: db.Close(); sl@0: TEST2(err, KErrNone); sl@0: sl@0: RFs fs; sl@0: err = fs.Connect(); sl@0: TEST2(err, KErrNone); sl@0: sl@0: err = BaflUtils::CopyFile(fs, KAttachDb, KPrivateTestDb); sl@0: TEST2(err, KErrNone); sl@0: } sl@0: sl@0: void MarkHandles() sl@0: { sl@0: RThread().HandleCount(TheHandleCount1B, TheHandleCount2B); sl@0: } sl@0: sl@0: void CheckHandles() sl@0: { sl@0: TInt endHandleCount1E; sl@0: TInt endHandleCount2E; sl@0: sl@0: RThread().HandleCount(endHandleCount1E, endHandleCount2E); sl@0: sl@0: TEST(TheHandleCount1B == endHandleCount1E); sl@0: TEST(TheHandleCount2B == endHandleCount2E); sl@0: } sl@0: sl@0: void MarkAllocatedCells() sl@0: { sl@0: TheAllocatedCellsCountB = User::CountAllocCells(); sl@0: } sl@0: sl@0: void CheckAllocatedCells() sl@0: { sl@0: TInt allocatedCellsCountE = User::CountAllocCells(); sl@0: TEST(allocatedCellsCountE == TheAllocatedCellsCountB); sl@0: } sl@0: sl@0: void PrintEndOfOomTest(TOomTestType aOomTestType, TInt aFailingAllocationNo) sl@0: { sl@0: _LIT(KClientSide, "Client side"); sl@0: _LIT(KServerSide, "Server side"); sl@0: RDebug::Print(_L("=== %S OOM Test succeeded at heap failure rate of %d ===\r\n"), sl@0: aOomTestType == EClientSideTest ? &KClientSide() : &KServerSide(), sl@0: aFailingAllocationNo); sl@0: } sl@0: sl@0: //If aDelayed is true, then the SQL server will delay the heap failure simulation until the database is opened. sl@0: void SetDbHeapFailure(TOomTestType aOomTestType, TInt aFailingAllocationNo, TBool aDelayed) sl@0: { sl@0: const TInt KDelayedDbHeapFailureMask = 0x1000; sl@0: if(aOomTestType == EClientSideTest) sl@0: { sl@0: __UHEAP_SETFAIL(RHeap::EDeterministic, aFailingAllocationNo); sl@0: } sl@0: else sl@0: { sl@0: TSqlResourceTester::SetDbHeapFailure(RHeap::EDeterministic | (aDelayed ? KDelayedDbHeapFailureMask : 0), aFailingAllocationNo); sl@0: } sl@0: } sl@0: sl@0: void ResetDbHeapFailure(TOomTestType aOomTestType) sl@0: { sl@0: if(aOomTestType == EClientSideTest) sl@0: { sl@0: __UHEAP_SETFAIL(RHeap::ENone, 0); sl@0: } sl@0: else sl@0: { sl@0: TSqlResourceTester::SetDbHeapFailure(RHeap::ENone, 0); sl@0: } sl@0: } sl@0: sl@0: void SetHeapFailure(TOomTestType aOomTestType, TInt aFailingAllocationNo) sl@0: { sl@0: if(aOomTestType == EClientSideTest) sl@0: { sl@0: __UHEAP_SETFAIL(RHeap::EDeterministic, aFailingAllocationNo); sl@0: } sl@0: else sl@0: { sl@0: TSqlResourceTester::Mark(); sl@0: TSqlResourceTester::SetHeapFailure(RHeap::EDeterministic, aFailingAllocationNo); sl@0: } sl@0: } sl@0: sl@0: void ResetHeapFailure(TOomTestType aOomTestType) sl@0: { sl@0: if(aOomTestType == EClientSideTest) sl@0: { sl@0: __UHEAP_SETFAIL(RHeap::ENone, 0); sl@0: } sl@0: else sl@0: { sl@0: TSqlResourceTester::SetHeapFailure(RHeap::ENone, 0); sl@0: TSqlResourceTester::Check(); sl@0: } sl@0: } sl@0: sl@0: void CreateTestSecurityPolicy(RSqlSecurityPolicy& aSecurityPolicy) sl@0: { sl@0: TInt err = aSecurityPolicy.Create(KDefaultPolicy); sl@0: TEST2(err, KErrNone); sl@0: sl@0: err = aSecurityPolicy.SetDbPolicy(RSqlSecurityPolicy::ESchemaPolicy, KPolicy1); sl@0: TEST2(err, KErrNone); sl@0: err = aSecurityPolicy.SetDbPolicy(RSqlSecurityPolicy::EWritePolicy, KPolicy3); sl@0: TEST2(err, KErrNone); sl@0: sl@0: err = aSecurityPolicy.SetPolicy(RSqlSecurityPolicy::ETable, KTableName1, RSqlSecurityPolicy::EReadPolicy, KPolicy1); sl@0: TEST2(err, KErrNone); sl@0: err = aSecurityPolicy.SetPolicy(RSqlSecurityPolicy::ETable, KTableName1, RSqlSecurityPolicy::EWritePolicy, KPolicy2); sl@0: TEST2(err, KErrNone); sl@0: sl@0: err = aSecurityPolicy.SetPolicy(RSqlSecurityPolicy::ETable, KTableName2, RSqlSecurityPolicy::EReadPolicy, KPolicy1); sl@0: TEST2(err, KErrNone); sl@0: err = aSecurityPolicy.SetPolicy(RSqlSecurityPolicy::ETable, KTableName3, RSqlSecurityPolicy::EReadPolicy, KPolicy2); sl@0: TEST2(err, KErrNone); sl@0: sl@0: err = aSecurityPolicy.SetPolicy(RSqlSecurityPolicy::ETable, KTableName2, RSqlSecurityPolicy::EWritePolicy, KPolicy1); sl@0: TEST2(err, KErrNone); sl@0: sl@0: err = aSecurityPolicy.SetPolicy(RSqlSecurityPolicy::ETable, KTableName3, RSqlSecurityPolicy::EWritePolicy, KPolicy3); sl@0: TEST2(err, KErrNone); sl@0: } sl@0: sl@0: void CreateAttachDb() sl@0: { sl@0: RSqlDatabase db; sl@0: sl@0: TInt err = db.Create(KAttachDb); sl@0: TEST2(err, KErrNone); sl@0: err = db.Exec(_L("CREATE TABLE Aab(F1 INTEGER, F2 TEXT)")); sl@0: TEST(err >= 0); sl@0: db.Close(); sl@0: sl@0: RSqlSecurityPolicy securityPolicy; sl@0: CreateTestSecurityPolicy(securityPolicy); sl@0: sl@0: err = db.Create(KSecureAttachDb, securityPolicy); sl@0: TEST2(err, KErrNone); sl@0: err = db.Exec(_L("CREATE TABLE Aab(F1 INTEGER, F2 TEXT)")); sl@0: TEST(err >= 0); sl@0: db.Close(); sl@0: sl@0: securityPolicy.Close(); sl@0: }