diff -r 000000000000 -r 08ec8eefde2f persistentstorage/sql/TEST/t_sqloom.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/persistentstorage/sql/TEST/t_sqloom.cpp Fri Jan 22 11:06:30 2010 +0200 @@ -0,0 +1,565 @@ +// Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). +// All rights reserved. +// This component and the accompanying materials are made available +// under the terms of "Eclipse Public License v1.0" +// which accompanies this distribution, and is available +// at the URL "http://www.eclipse.org/legal/epl-v10.html". +// +// Initial Contributors: +// Nokia Corporation - initial contribution. +// +// Contributors: +// +// Description: +// + +#include "t_sqloom.h" + +/////////////////////////////////////////////////////////////////////////////////////// +/////////////// RSqlDatabase OOM tests //////////////////////////////// +/////////////////////////////////////////////////////////////////////////////////////// + +/** +@SYMTestCaseID SYSLIB-SQL-CT-1615, SYSLIB-SQL-CT-1639 +@SYMTestCaseDesc RSqlDatabase::Create() OOM test - secure and non-secure databases. + Precondition: the database does not exist. + The test calls RSqlDatabase::Create() while simulating OOM failures and checks + that there are no memory and resource leaks. + Note: It's possible for a database to be created even after memory allocation + has failed. This is because SQLITE reuses some pages of the page cache which + have been allocated but are curently not in use. This means it is necessary + to delete the database and continue checking for memory and resource leaks + even after a database has been created successfully. +@SYMTestPriority High +@SYMTestActions RSqlDatabase::Create() OOM test +@SYMTestExpectedResults Test must not fail +@SYMREQ REQ5792 + REQ5793 + REQ10271 + REQ10273 + REQ10274 +*/ +void DoCreateDatabaseOomTest(const TDesC& aDbFileName, TDbType aDbType, TInt aExpectedError, const TDesC8* aConfigStr = NULL) + { + TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1639 RSqlDatabase::Create() - OOM test")); + RSqlSecurityPolicy securityPolicy; + CreateTestSecurityPolicy(securityPolicy); + enum TMethodType {ENonLeavingMethod, ELeavingMethod}; + const TMethodType KMethodType[] = {ENonLeavingMethod, ELeavingMethod}; + for(TInt j=0;j= 0); + err = KErrNone; + } + else if(aTestFunctionPtrL == &DeleteDbL && err == KErrNone) + { + err = aDbType == ESecureDb ? db.Create(aDbFileName, securityPolicy) : db.Create(aDbFileName); + TEST2(err, KErrNone); + } + db.Close(); + + __UHEAP_MARKEND; + + CheckAllocatedCells(); + CheckHandles(); + + if(err == KErrNoMemory && allocationNo == maxAllocationNo) + { + maxAllocationNo += 10; + } + } + TEST2(err, KErrNone); + PrintEndOfOomTest(TheOomTestType[i], failingAllocationNo + 1); + } + //Delete the database file + RSqlDatabase::Delete(aDbFileName); + securityPolicy.Close(); + } + +//An attempt to open a non-secure database somehow happened to be in the server's private data cage. +void DoDbOomTest2() + { + for(TInt i=0;i<(TInt)(sizeof(TheOomTestType)/sizeof(TheOomTestType[0]));++i) + { + TInt err = KErrNone; + TInt failingAllocationNo = 0; + TInt allocationNo = 0; + TInt maxAllocationNo = TheOomTestType[i] == EServerSideTest ? KDoDbOomTest2AllocLimitServer : KDoDbOomTest2AllocLimitClient; + while(allocationNo < maxAllocationNo) + { + MarkHandles(); + MarkAllocatedCells(); + + __UHEAP_MARK; + + SetDbHeapFailure(TheOomTestType[i], ++allocationNo); + + RSqlDatabase db; + err = db.Open(KSecureAttachDb2); + db.Close(); + if(err != KErrNoMemory) + { + TEST2(err, KSqlErrGeneral); + } + else + { + failingAllocationNo = allocationNo; + } + + ResetDbHeapFailure(TheOomTestType[i]); + + __UHEAP_MARKEND; + + CheckAllocatedCells(); + CheckHandles(); + + if(err == KErrNoMemory && allocationNo == maxAllocationNo) + { + maxAllocationNo += 10; + } + } + TEST2(err, KSqlErrGeneral); + PrintEndOfOomTest(TheOomTestType[i], failingAllocationNo + 1); + } + } + +/////////////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////////////// + +//RSqlDatabase OOM tests +void DbOomTestsL(TDbType aDbType) + { + TPtrC dbFileName(KTestDb); + if(aDbType == ESecureDb) + { + dbFileName.Set(KSecureTestDb()); + } + + CreateAttachDb(); + + TheTest.Printf(_L("===RSqlDatabase::Create()\r\n")); + DoCreateDatabaseOomTest(dbFileName, aDbType, KErrNone); + + TheTest.Printf(_L("===RSqlDatabase::Create() + config string\r\n")); + _LIT8(KConfigStr, "page_size=2048"); + DoCreateDatabaseOomTest(dbFileName, aDbType, KErrNone, &KConfigStr); + + TheTest.Printf(_L("===RSqlDatabase::Create() + config string + manual compaction\r\n")); + _LIT8(KConfigStr2, "compaction=manual"); + DoCreateDatabaseOomTest(dbFileName, aDbType, KErrNone, &KConfigStr2); + + TheTest.Printf(_L("===RSqlDatabase::Create() + config string + background compaction\r\n")); + _LIT8(KConfigStr3, "compaction=background"); + DoCreateDatabaseOomTest(dbFileName, aDbType, KErrNone, &KConfigStr3); + + TheTest.Printf(_L("===RSqlDatabase::Create() + config string + auto compaction\r\n")); + _LIT8(KConfigStr4, "compaction=auto"); + DoCreateDatabaseOomTest(dbFileName, aDbType, KErrNone, &KConfigStr4); + + if(aDbType == ENonSecureDb) + {//Private database is not a database taht will be created in the SQL server private data cage. + (void)RSqlDatabase::Delete(KPrivateTestDb); + TheTest.Printf(_L("===RSqlDatabase::Create() private database + config string + manual compaction\r\n")); + DoCreateDatabaseOomTest(KPrivateTestDb, ENonSecureDb, KErrNone, &KConfigStr2); + + (void)RSqlDatabase::Delete(KPrivateTestDb); + TheTest.Printf(_L("===RSqlDatabase::Create() private database + config string + background compaction\r\n")); + DoCreateDatabaseOomTest(KPrivateTestDb, ENonSecureDb, KErrNone, &KConfigStr3); + + (void)RSqlDatabase::Delete(KPrivateTestDb); + TheTest.Printf(_L("===RSqlDatabase::Create() private database + config string + auto compaction\r\n")); + DoCreateDatabaseOomTest(KPrivateTestDb, ENonSecureDb, KErrNone, &KConfigStr4); + } + + TheTest.Printf(_L("===RSqlDatabase::Open()\r\n")); + DoDbOomTest(&OpenDatabaseL, dbFileName, ENotOpenDb, aDbType); + + TheTest.Printf(_L("===RSqlDatabase::OpenL()\r\n")); + DoDbOomTest(&OpenDatabase2L, dbFileName, ENotOpenDb, aDbType); + + TheTest.Printf(_L("===RSqlDatabase::Open() + config string\r\n")); + DoDbOomTest(&OpenDatabase3L, dbFileName, ENotOpenDb, aDbType); + + if(aDbType == ENonSecureDb) + {//Private database cannot be opened as a secure database + TheTest.Printf(_L("===RSqlDatabase::Open() - from handle\r\n")); + DoDbOomTest(&OpenDatabaseFromHandleL, KPrivateTestDb, ENotOpenDb, aDbType); + + TheTest.Printf(_L("===RSqlDatabase::Open() - from handle + config string\r\n")); + DoDbOomTest(&OpenDatabaseFromHandle2L, KPrivateTestDb, ENotOpenDb, aDbType); + } + + TheTest.Printf(_L("===RSqlDatabase::Exec(), 8-bit SQL\r\n")); + DoDbOomTest(&ExecStatement8L, dbFileName, EOpenDb, aDbType); + + TheTest.Printf(_L("===RSqlDatabase::Exec(), 16-bit SQL\r\n")); + DoDbOomTest(&ExecStatement16L, dbFileName, EOpenDb, aDbType); + + TheTest.Printf(_L("===RSqlDatabase::SetIsolationLevel()\r\n")); + DoDbOomTest(&SetIsolationLevelL, dbFileName, EOpenDb, aDbType); + + TheTest.Printf(_L("===RSqlDatabase::Size()\r\n")); + DoDbOomTest(&DbSizeL, dbFileName, EOpenDb, aDbType); + + TheTest.Printf(_L("===RSqlDatabase::Size(TSize&)\r\n")); + DoDbOomTest(&DbSize2L, dbFileName, EOpenDb, aDbType); + + TheTest.Printf(_L("===RSqlDatabase::Size(TSize&) - attached database\r\n")); + DoDbOomTest(&DbAttachSize2L, dbFileName, EOpenDb, aDbType); + + TheTest.Printf(_L("===RSqlDatabase::Delete()\r\n")); + DoDbOomTest(&DeleteDbL, dbFileName, ENotOpenDb, aDbType); + + TheTest.Printf(_L("===RSqlDatabase::Attach()\r\n")); + DoDbOomTest(&AttachDatabaseL, dbFileName, EOpenDb, aDbType); + + //Ensure that the private database to be attached exists + PrepareAttachFromHandle(); + TheTest.Printf(_L("===RSqlDatabase::Attach() - from handle\r\n")); + DoDbOomTest(&AttachDatabase2L, dbFileName, EOpenDb, aDbType); + + TheTest.Printf(_L("===RSqlDatabase::Copy()\r\n")); + DoDbOomTest(&CopyDatabaseL, dbFileName, ENotOpenDb, aDbType); + + if(aDbType == ESecureDb) + { + TheTest.Printf(_L("===RSqlDatabase::GetSecurityPolicy()\r\n")); + DoDbOomTest(&GetSecurityPolicyL, dbFileName, EOpenDb, aDbType); + + TheTest.Printf(_L("===RSqlDatabase::GetSecurityPolicyL()\r\n")); + DoDbOomTest(&GetSecurityPolicy2L, dbFileName, EOpenDb, aDbType); + } + + TheTest.Printf(_L("===RSqlDatabase::ReserveDriveSpace()\r\n")); + DoDbOomTest(&ReserveDriveSpaceL, dbFileName, EOpenDb, aDbType); + + TheTest.Printf(_L("===RSqlDatabase::GetReserveAccess()\r\n")); + DoDbOomTest(&GetReserveAccessL, dbFileName, EOpenDb, aDbType); + + TheTest.Printf(_L("===RSqlDatabase::LastInsertedRowId()\r\n")); + DoDbOomTest(&DbLastInsertedRowIdL, dbFileName, EOpenDb, aDbType); + + TheTest.Printf(_L("===RSqlDatabase::Open(), non-secure database in server data cage\r\n")); + DoDbOomTest2(); + }