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: sl@0: /////////////////////////////////////////////////////////////////////////////////////// sl@0: /////////////// RSqlDatabase OOM tests //////////////////////////////// sl@0: /////////////////////////////////////////////////////////////////////////////////////// sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-SQL-CT-1615, SYSLIB-SQL-CT-1639 sl@0: @SYMTestCaseDesc RSqlDatabase::Create() OOM test - secure and non-secure databases. sl@0: Precondition: the database does not exist. sl@0: The test calls RSqlDatabase::Create() while simulating OOM failures and checks sl@0: that there are no memory and resource leaks. sl@0: Note: It's possible for a database to be created even after memory allocation sl@0: has failed. This is because SQLITE reuses some pages of the page cache which sl@0: have been allocated but are curently not in use. This means it is necessary sl@0: to delete the database and continue checking for memory and resource leaks sl@0: even after a database has been created successfully. sl@0: @SYMTestPriority High sl@0: @SYMTestActions RSqlDatabase::Create() OOM test sl@0: @SYMTestExpectedResults Test must not fail sl@0: @SYMREQ REQ5792 sl@0: REQ5793 sl@0: REQ10271 sl@0: REQ10273 sl@0: REQ10274 sl@0: */ sl@0: void DoCreateDatabaseOomTest(const TDesC& aDbFileName, TDbType aDbType, TInt aExpectedError, const TDesC8* aConfigStr = NULL) sl@0: { sl@0: TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1639 RSqlDatabase::Create() - OOM test")); sl@0: RSqlSecurityPolicy securityPolicy; sl@0: CreateTestSecurityPolicy(securityPolicy); sl@0: enum TMethodType {ENonLeavingMethod, ELeavingMethod}; sl@0: const TMethodType KMethodType[] = {ENonLeavingMethod, ELeavingMethod}; sl@0: for(TInt j=0;j= 0); sl@0: err = KErrNone; sl@0: } sl@0: else if(aTestFunctionPtrL == &DeleteDbL && err == KErrNone) sl@0: { sl@0: err = aDbType == ESecureDb ? db.Create(aDbFileName, securityPolicy) : db.Create(aDbFileName); sl@0: TEST2(err, KErrNone); sl@0: } sl@0: db.Close(); sl@0: sl@0: __UHEAP_MARKEND; sl@0: sl@0: CheckAllocatedCells(); sl@0: CheckHandles(); sl@0: sl@0: if(err == KErrNoMemory && allocationNo == maxAllocationNo) sl@0: { sl@0: maxAllocationNo += 10; sl@0: } sl@0: } sl@0: TEST2(err, KErrNone); sl@0: PrintEndOfOomTest(TheOomTestType[i], failingAllocationNo + 1); sl@0: } sl@0: //Delete the database file sl@0: RSqlDatabase::Delete(aDbFileName); sl@0: securityPolicy.Close(); sl@0: } sl@0: sl@0: //An attempt to open a non-secure database somehow happened to be in the server's private data cage. sl@0: void DoDbOomTest2() sl@0: { sl@0: for(TInt i=0;i<(TInt)(sizeof(TheOomTestType)/sizeof(TheOomTestType[0]));++i) sl@0: { sl@0: TInt err = KErrNone; sl@0: TInt failingAllocationNo = 0; sl@0: TInt allocationNo = 0; sl@0: TInt maxAllocationNo = TheOomTestType[i] == EServerSideTest ? KDoDbOomTest2AllocLimitServer : KDoDbOomTest2AllocLimitClient; sl@0: while(allocationNo < maxAllocationNo) sl@0: { sl@0: MarkHandles(); sl@0: MarkAllocatedCells(); sl@0: sl@0: __UHEAP_MARK; sl@0: sl@0: SetDbHeapFailure(TheOomTestType[i], ++allocationNo); sl@0: sl@0: RSqlDatabase db; sl@0: err = db.Open(KSecureAttachDb2); sl@0: db.Close(); sl@0: if(err != KErrNoMemory) sl@0: { sl@0: TEST2(err, KSqlErrGeneral); sl@0: } sl@0: else sl@0: { sl@0: failingAllocationNo = allocationNo; sl@0: } sl@0: sl@0: ResetDbHeapFailure(TheOomTestType[i]); sl@0: sl@0: __UHEAP_MARKEND; sl@0: sl@0: CheckAllocatedCells(); sl@0: CheckHandles(); sl@0: sl@0: if(err == KErrNoMemory && allocationNo == maxAllocationNo) sl@0: { sl@0: maxAllocationNo += 10; sl@0: } sl@0: } sl@0: TEST2(err, KSqlErrGeneral); sl@0: PrintEndOfOomTest(TheOomTestType[i], failingAllocationNo + 1); sl@0: } sl@0: } sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////////////// sl@0: /////////////////////////////////////////////////////////////////////////////////////// sl@0: sl@0: //RSqlDatabase OOM tests sl@0: void DbOomTestsL(TDbType aDbType) sl@0: { sl@0: TPtrC dbFileName(KTestDb); sl@0: if(aDbType == ESecureDb) sl@0: { sl@0: dbFileName.Set(KSecureTestDb()); sl@0: } sl@0: sl@0: CreateAttachDb(); sl@0: sl@0: TheTest.Printf(_L("===RSqlDatabase::Create()\r\n")); sl@0: DoCreateDatabaseOomTest(dbFileName, aDbType, KErrNone); sl@0: sl@0: TheTest.Printf(_L("===RSqlDatabase::Create() + config string\r\n")); sl@0: _LIT8(KConfigStr, "page_size=2048"); sl@0: DoCreateDatabaseOomTest(dbFileName, aDbType, KErrNone, &KConfigStr); sl@0: sl@0: TheTest.Printf(_L("===RSqlDatabase::Create() + config string + manual compaction\r\n")); sl@0: _LIT8(KConfigStr2, "compaction=manual"); sl@0: DoCreateDatabaseOomTest(dbFileName, aDbType, KErrNone, &KConfigStr2); sl@0: sl@0: TheTest.Printf(_L("===RSqlDatabase::Create() + config string + background compaction\r\n")); sl@0: _LIT8(KConfigStr3, "compaction=background"); sl@0: DoCreateDatabaseOomTest(dbFileName, aDbType, KErrNone, &KConfigStr3); sl@0: sl@0: TheTest.Printf(_L("===RSqlDatabase::Create() + config string + auto compaction\r\n")); sl@0: _LIT8(KConfigStr4, "compaction=auto"); sl@0: DoCreateDatabaseOomTest(dbFileName, aDbType, KErrNone, &KConfigStr4); sl@0: sl@0: if(aDbType == ENonSecureDb) sl@0: {//Private database is not a database taht will be created in the SQL server private data cage. sl@0: (void)RSqlDatabase::Delete(KPrivateTestDb); sl@0: TheTest.Printf(_L("===RSqlDatabase::Create() private database + config string + manual compaction\r\n")); sl@0: DoCreateDatabaseOomTest(KPrivateTestDb, ENonSecureDb, KErrNone, &KConfigStr2); sl@0: sl@0: (void)RSqlDatabase::Delete(KPrivateTestDb); sl@0: TheTest.Printf(_L("===RSqlDatabase::Create() private database + config string + background compaction\r\n")); sl@0: DoCreateDatabaseOomTest(KPrivateTestDb, ENonSecureDb, KErrNone, &KConfigStr3); sl@0: sl@0: (void)RSqlDatabase::Delete(KPrivateTestDb); sl@0: TheTest.Printf(_L("===RSqlDatabase::Create() private database + config string + auto compaction\r\n")); sl@0: DoCreateDatabaseOomTest(KPrivateTestDb, ENonSecureDb, KErrNone, &KConfigStr4); sl@0: } sl@0: sl@0: TheTest.Printf(_L("===RSqlDatabase::Open()\r\n")); sl@0: DoDbOomTest(&OpenDatabaseL, dbFileName, ENotOpenDb, aDbType); sl@0: sl@0: TheTest.Printf(_L("===RSqlDatabase::OpenL()\r\n")); sl@0: DoDbOomTest(&OpenDatabase2L, dbFileName, ENotOpenDb, aDbType); sl@0: sl@0: TheTest.Printf(_L("===RSqlDatabase::Open() + config string\r\n")); sl@0: DoDbOomTest(&OpenDatabase3L, dbFileName, ENotOpenDb, aDbType); sl@0: sl@0: if(aDbType == ENonSecureDb) sl@0: {//Private database cannot be opened as a secure database sl@0: TheTest.Printf(_L("===RSqlDatabase::Open() - from handle\r\n")); sl@0: DoDbOomTest(&OpenDatabaseFromHandleL, KPrivateTestDb, ENotOpenDb, aDbType); sl@0: sl@0: TheTest.Printf(_L("===RSqlDatabase::Open() - from handle + config string\r\n")); sl@0: DoDbOomTest(&OpenDatabaseFromHandle2L, KPrivateTestDb, ENotOpenDb, aDbType); sl@0: } sl@0: sl@0: TheTest.Printf(_L("===RSqlDatabase::Exec(), 8-bit SQL\r\n")); sl@0: DoDbOomTest(&ExecStatement8L, dbFileName, EOpenDb, aDbType); sl@0: sl@0: TheTest.Printf(_L("===RSqlDatabase::Exec(), 16-bit SQL\r\n")); sl@0: DoDbOomTest(&ExecStatement16L, dbFileName, EOpenDb, aDbType); sl@0: sl@0: TheTest.Printf(_L("===RSqlDatabase::SetIsolationLevel()\r\n")); sl@0: DoDbOomTest(&SetIsolationLevelL, dbFileName, EOpenDb, aDbType); sl@0: sl@0: TheTest.Printf(_L("===RSqlDatabase::Size()\r\n")); sl@0: DoDbOomTest(&DbSizeL, dbFileName, EOpenDb, aDbType); sl@0: sl@0: TheTest.Printf(_L("===RSqlDatabase::Size(TSize&)\r\n")); sl@0: DoDbOomTest(&DbSize2L, dbFileName, EOpenDb, aDbType); sl@0: sl@0: TheTest.Printf(_L("===RSqlDatabase::Size(TSize&) - attached database\r\n")); sl@0: DoDbOomTest(&DbAttachSize2L, dbFileName, EOpenDb, aDbType); sl@0: sl@0: TheTest.Printf(_L("===RSqlDatabase::Delete()\r\n")); sl@0: DoDbOomTest(&DeleteDbL, dbFileName, ENotOpenDb, aDbType); sl@0: sl@0: TheTest.Printf(_L("===RSqlDatabase::Attach()\r\n")); sl@0: DoDbOomTest(&AttachDatabaseL, dbFileName, EOpenDb, aDbType); sl@0: sl@0: //Ensure that the private database to be attached exists sl@0: PrepareAttachFromHandle(); sl@0: TheTest.Printf(_L("===RSqlDatabase::Attach() - from handle\r\n")); sl@0: DoDbOomTest(&AttachDatabase2L, dbFileName, EOpenDb, aDbType); sl@0: sl@0: TheTest.Printf(_L("===RSqlDatabase::Copy()\r\n")); sl@0: DoDbOomTest(&CopyDatabaseL, dbFileName, ENotOpenDb, aDbType); sl@0: sl@0: if(aDbType == ESecureDb) sl@0: { sl@0: TheTest.Printf(_L("===RSqlDatabase::GetSecurityPolicy()\r\n")); sl@0: DoDbOomTest(&GetSecurityPolicyL, dbFileName, EOpenDb, aDbType); sl@0: sl@0: TheTest.Printf(_L("===RSqlDatabase::GetSecurityPolicyL()\r\n")); sl@0: DoDbOomTest(&GetSecurityPolicy2L, dbFileName, EOpenDb, aDbType); sl@0: } sl@0: sl@0: TheTest.Printf(_L("===RSqlDatabase::ReserveDriveSpace()\r\n")); sl@0: DoDbOomTest(&ReserveDriveSpaceL, dbFileName, EOpenDb, aDbType); sl@0: sl@0: TheTest.Printf(_L("===RSqlDatabase::GetReserveAccess()\r\n")); sl@0: DoDbOomTest(&GetReserveAccessL, dbFileName, EOpenDb, aDbType); sl@0: sl@0: TheTest.Printf(_L("===RSqlDatabase::LastInsertedRowId()\r\n")); sl@0: DoDbOomTest(&DbLastInsertedRowIdL, dbFileName, EOpenDb, aDbType); sl@0: sl@0: TheTest.Printf(_L("===RSqlDatabase::Open(), non-secure database in server data cage\r\n")); sl@0: DoDbOomTest2(); sl@0: }