os/persistentdata/persistentstorage/sql/TEST/t_sqloom5.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2010 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 #include <e32test.h>
    17 #include <bautils.h>
    18 #include "SqlSrvMain.h"
    19 #include "SqlSrvStartup.h"
    20 #include "SqlSrvUtil.h"
    21 #include "SqlSrvDatabase.h"
    22 #include "SqlSrvFileData.h"
    23 
    24 ///////////////////////////////////////////////////////////////////////////////////////
    25 
    26 RTest TheTest(_L("t_sqloom5 test"));
    27 RFs TheFs;
    28 
    29 _LIT(KTestDir, "c:\\test\\");
    30 _LIT(KDbFile, "c:\\test\\t_sqloom5.db");
    31 _LIT(KDbFile2, "c:[10281E17]t_sqloom5.db");
    32 _LIT(KDbFile3, "c:[10281E17]t_sqloom5_2.db");
    33 _LIT(KDbFile4, "c:[10281E17]t_sqloom5_3.db");
    34 
    35 _LIT8(KConfig, "encoding=UTF-8");
    36 
    37 extern CSqlServer* TheServer;
    38 
    39 static TInt TheProcessHandleCount = 0;
    40 static TInt TheThreadHandleCount = 0;
    41 static TInt TheAllocatedCellsCount = 0;
    42 
    43 #ifdef _DEBUG
    44 static const TInt KBurstRate = 20;
    45 #endif
    46 
    47 enum TSqlDbEncoding
    48     {
    49     ESqlDbUtf16,
    50     ESqlDbUtf8
    51     };
    52 
    53 ///////////////////////////////////////////////////////////////////////////////////////
    54 
    55 void DestroyTestEnv()
    56 	{
    57     (void)TheFs.Delete(KDbFile4);
    58     (void)TheFs.Delete(KDbFile3);
    59     (void)TheFs.Delete(KDbFile2);
    60     (void)TheFs.Delete(KDbFile);
    61 	TheFs.Close();
    62 	}
    63 
    64 ///////////////////////////////////////////////////////////////////////////////////////
    65 ///////////////////////////////////////////////////////////////////////////////////////
    66 //Test macros and functions
    67 void Check(TInt aValue, TInt aLine)
    68 	{
    69 	if(!aValue)
    70 		{
    71 		DestroyTestEnv();
    72 		RDebug::Print(_L("*** Expresssion evaluated to false\r\n"));
    73 		TheTest(EFalse, aLine);
    74 		}
    75 	}
    76 void Check(TInt aValue, TInt aExpected, TInt aLine)
    77 	{
    78 	if(aValue != aExpected)
    79 		{
    80 		DestroyTestEnv();
    81 		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
    82 		TheTest(EFalse, aLine);
    83 		}
    84 	}
    85 #define TEST(arg) ::Check((arg), __LINE__)
    86 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
    87 
    88 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
    89 
    90 static void MarkHandles()
    91     {
    92     RThread().HandleCount(TheProcessHandleCount, TheThreadHandleCount);
    93     }
    94 
    95 static void MarkAllocatedCells()
    96     {
    97     TheAllocatedCellsCount = User::CountAllocCells();
    98     }
    99 
   100 static void CheckAllocatedCells()
   101     {
   102     TInt allocatedCellsCount = User::CountAllocCells();
   103     TEST2(allocatedCellsCount, TheAllocatedCellsCount);
   104     }
   105 
   106 static void CheckHandles()
   107     {
   108     TInt endProcessHandleCount;
   109     TInt endThreadHandleCount;
   110     
   111     RThread().HandleCount(endProcessHandleCount, endThreadHandleCount);
   112 
   113     TEST2(TheProcessHandleCount, endProcessHandleCount);
   114     TEST2(TheThreadHandleCount, endThreadHandleCount);
   115     }
   116 
   117 static void OomPreStep(TInt
   118                         #ifdef _DEBUG        
   119                             aFailingAllocationNo
   120                         #endif
   121                       )
   122     {
   123     MarkHandles();
   124     MarkAllocatedCells();
   125     __UHEAP_MARK;
   126     __UHEAP_SETBURSTFAIL(RAllocator::EBurstFailNext, aFailingAllocationNo, KBurstRate);
   127     }
   128 
   129 static void OomPostStep()
   130     {
   131     __UHEAP_RESET;
   132     __UHEAP_MARKEND;
   133     CheckAllocatedCells();
   134     CheckHandles();
   135     }
   136 
   137 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
   138 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
   139 
   140 void CreateTestEnv()
   141     {
   142     TInt err = TheFs.Connect();
   143     TEST2(err, KErrNone);
   144 
   145     err = TheFs.MkDir(KTestDir);
   146     TEST(err == KErrNone || err == KErrAlreadyExists);
   147 
   148     err = TheFs.CreatePrivatePath(EDriveC);
   149     TEST(err == KErrNone || err == KErrAlreadyExists);
   150     }
   151 
   152 static CSqlServer* CreateSqlServerL()
   153     {
   154     CSqlServer* server = CSqlServer::NewLC();
   155     CleanupStack::Pop(server);
   156     return server;
   157     }
   158 
   159 void CreateDatabaseOomTest(TSqlDbEncoding aEncoding)
   160     {
   161     (void)TheFs.Delete(KDbFile);
   162     TInt failingAllocationNo = 0;
   163     TheTest.Printf(_L("Iteration:\r\n"));
   164     
   165     TheServer = NULL; 
   166     TRAPD(err, TheServer = CreateSqlServerL());
   167     TEST2(err, KErrNone);
   168     
   169     err = KErrNoMemory;
   170     while(err == KErrNoMemory)
   171         {
   172         TheTest.Printf(_L(" %d"), ++failingAllocationNo);
   173         OomPreStep(failingAllocationNo);
   174         
   175         TSqlSrvFileData& fdata = TheServer->FileData();
   176         RMessage2 msg;
   177         TRAP(err, fdata.SetL(msg, KDbFile().Length(), 0, KDbFile, aEncoding == ESqlDbUtf8 ? &KConfig : NULL));
   178         if(err == KErrNone)
   179             {
   180             CSqlSrvDatabase* db = NULL;
   181             TRAP(err, db = CSqlSrvDatabase::CreateL(fdata));
   182             delete db;
   183             }
   184         OomPostStep();
   185         }
   186 
   187     delete TheServer;
   188     TheServer = NULL;
   189     
   190     TEST2(err, KErrNone);   
   191     TheTest.Printf(_L("\r\n===CSqlSrvDatabase::CreateL() OOM test succeeded at heap failure rate of %d ===\r\n"), failingAllocationNo);
   192     }
   193 
   194 /**
   195 @SYMTestCaseID          PDS-SQL-UT-4167
   196 @SYMTestCaseDesc        CSqlSrvDatabase::CreateL() OOM test.
   197 @SYMTestPriority        High
   198 @SYMTestActions         The test runs CSqlSrvDatabase::CreateL() in an OOM loop.
   199                         UTF16 encoded database is used.
   200 @SYMTestExpectedResults Test must not fail
   201 @SYMDEF                 DEF144577
   202 */  
   203 void Utf16CreateDatabaseOomTest()
   204     {
   205     CreateDatabaseOomTest(ESqlDbUtf16);
   206     }
   207 
   208 /**
   209 @SYMTestCaseID          PDS-SQL-UT-4182
   210 @SYMTestCaseDesc        CSqlSrvDatabase::CreateL() OOM test.
   211 @SYMTestPriority        High
   212 @SYMTestActions         The test runs CSqlSrvDatabase::CreateL() in an OOM loop.
   213                         UTF8 encoded database is used.
   214 @SYMTestExpectedResults Test must not fail
   215 @SYMDEF                 DEF145047
   216 */  
   217 void Utf8CreateDatabaseOomTest()
   218     {
   219     CreateDatabaseOomTest(ESqlDbUtf8);
   220     }
   221 
   222 void OpenDatabaseOomTest()
   223     {
   224     //The database is created by the previous test: CreateDatabaseOomTest().
   225     
   226     TInt failingAllocationNo = 0;
   227     TheTest.Printf(_L("Iteration:\r\n"));
   228     
   229     TheServer = NULL; 
   230     TRAPD(err, TheServer = CreateSqlServerL());
   231     TEST2(err, KErrNone);
   232     
   233     err = KErrNoMemory;
   234     while(err == KErrNoMemory)
   235         {
   236         TheTest.Printf(_L(" %d"), ++failingAllocationNo);
   237         OomPreStep(failingAllocationNo);
   238         
   239         TSqlSrvFileData& fdata = TheServer->FileData();
   240         RMessage2 msg;
   241         TRAP(err, fdata.SetL(msg, KDbFile().Length(), 0, KDbFile));
   242         if(err == KErrNone)
   243             {
   244             CSqlSrvDatabase* db = NULL;
   245             TRAP(err, db = CSqlSrvDatabase::OpenL(fdata));
   246             delete db;
   247             }
   248         
   249         OomPostStep();
   250         }
   251     
   252     delete TheServer;
   253     TheServer = NULL;
   254     
   255     TEST2(err, KErrNone);   
   256     TheTest.Printf(_L("\r\n===CSqlSrvDatabase::OpenL() [non-secure db] OOM test succeeded at heap failure rate of %d ===\r\n"), failingAllocationNo);
   257     }
   258 
   259 /**
   260 @SYMTestCaseID          PDS-SQL-UT-4168
   261 @SYMTestCaseDesc        CSqlSrvDatabase::OpenL() OOM test - non-secure database.
   262 @SYMTestPriority        High
   263 @SYMTestActions         The test runs CSqlSrvDatabase::OpenL() in an OOM loop.
   264                         UTF16 encoded database is used.
   265 @SYMTestExpectedResults Test must not fail
   266 @SYMDEF                 DEF144577
   267 */  
   268 void Utf16OpenDatabaseOomTest()
   269     {
   270     OpenDatabaseOomTest();
   271     }
   272 
   273 /**
   274 @SYMTestCaseID          PDS-SQL-UT-4183
   275 @SYMTestCaseDesc        CSqlSrvDatabase::OpenL() OOM test - non-secure database.
   276 @SYMTestPriority        High
   277 @SYMTestActions         The test runs CSqlSrvDatabase::OpenL() in an OOM loop.
   278                         UTF8 encoded database is used.
   279 @SYMTestExpectedResults Test must not fail
   280 @SYMDEF                 DEF145047
   281 */  
   282 void Utf8OpenDatabaseOomTest()
   283     {
   284     OpenDatabaseOomTest();
   285     }
   286 
   287 void CreateSecureDatabaseOomTest(TSqlDbEncoding aEncoding)
   288     {
   289     (void)TheFs.Delete(KDbFile2);
   290     TInt failingAllocationNo = 0;
   291     TheTest.Printf(_L("Iteration:\r\n"));
   292     
   293     TheServer = NULL; 
   294     TRAPD(err, TheServer = CreateSqlServerL());
   295     TEST2(err, KErrNone);
   296         
   297     err = KErrNoMemory;
   298     while(err == KErrNoMemory)
   299         {
   300         TheTest.Printf(_L(" %d"), ++failingAllocationNo);
   301         OomPreStep(failingAllocationNo);
   302         
   303         TSqlSrvFileData& fdata = TheServer->FileData();
   304         RMessage2 msg;
   305         TRAP(err, fdata.SetL(msg, KDbFile2().Length(), 0, KDbFile2, aEncoding == ESqlDbUtf8 ? &KConfig : NULL));
   306         if(err == KErrNone)
   307             {
   308             TSecurityPolicy defaultPolicy(TSecurityPolicy::EAlwaysPass);
   309             CSqlSecurityPolicy* policy = NULL;
   310             TRAP(err, policy = CSqlSecurityPolicy::NewL(defaultPolicy));
   311             if(err == KErrNone)
   312                 {
   313                 CSqlSrvDatabase* db = NULL;
   314                 TRAP(err, db = CSqlSrvDatabase::CreateSecureL(fdata, policy));
   315                 delete db;
   316                 }
   317             }
   318         OomPostStep();
   319         }
   320 
   321     delete TheServer;
   322     TheServer = NULL;
   323     
   324     TEST2(err, KErrNone);   
   325     TheTest.Printf(_L("\r\n===CSqlSrvDatabase::CreateSecureL() OOM test succeeded at heap failure rate of %d ===\r\n"), failingAllocationNo);
   326     }
   327 
   328 /**
   329 @SYMTestCaseID          PDS-SQL-UT-4169
   330 @SYMTestCaseDesc        CSqlSrvDatabase::CreateSecureL() OOM test.
   331 @SYMTestPriority        High
   332 @SYMTestActions         The test runs CSqlSrvDatabase::CreateSecureL() in an OOM loop.
   333                         UTF16 encoded database is used.
   334 @SYMTestExpectedResults Test must not fail
   335 @SYMDEF                 DEF144577
   336 */  
   337 void Utf16CreateSecureDatabaseOomTest()
   338     {
   339     CreateSecureDatabaseOomTest(ESqlDbUtf16);
   340     }
   341 
   342 /**
   343 @SYMTestCaseID          PDS-SQL-UT-4184
   344 @SYMTestCaseDesc        CSqlSrvDatabase::CreateSecureL() OOM test.
   345 @SYMTestPriority        High
   346 @SYMTestActions         The test runs CSqlSrvDatabase::CreateSecureL() in an OOM loop.
   347                         UTF8 encoded database is used.
   348 @SYMTestExpectedResults Test must not fail
   349 @SYMDEF                 DEF145047
   350 */  
   351 void Utf8CreateSecureDatabaseOomTest()
   352     {
   353     CreateSecureDatabaseOomTest(ESqlDbUtf8);
   354     }
   355 
   356 void OpenSecureDatabaseOomTest()
   357     {
   358     //The database is created by the previous test: CreateSecureDatabaseOomTest().
   359     
   360     TInt failingAllocationNo = 0;
   361     TheTest.Printf(_L("Iteration:\r\n"));
   362     
   363     TheServer = NULL; 
   364     TRAPD(err, TheServer = CreateSqlServerL());
   365     TEST2(err, KErrNone);
   366     
   367     err = KErrNoMemory;
   368     while(err == KErrNoMemory)
   369         {
   370         TheTest.Printf(_L(" %d"), ++failingAllocationNo);
   371         OomPreStep(failingAllocationNo);
   372         
   373         TSqlSrvFileData& fdata = TheServer->FileData();
   374         RMessage2 msg;
   375         TRAP(err, fdata.SetL(msg, KDbFile2().Length(), 0, KDbFile2));
   376         if(err == KErrNone)
   377             {
   378             CSqlSrvDatabase* db = NULL;
   379             TRAP(err, db = CSqlSrvDatabase::OpenL(fdata));
   380             delete db;
   381             }
   382         
   383         OomPostStep();
   384         }
   385     
   386     delete TheServer;
   387     TheServer = NULL;
   388     
   389     TEST2(err, KErrNone);   
   390     TheTest.Printf(_L("\r\n===CSqlSrvDatabase::OpenL() [secure db] OOM test succeeded at heap failure rate of %d ===\r\n"), failingAllocationNo);
   391     }
   392 
   393 /**
   394 @SYMTestCaseID          PDS-SQL-UT-4170
   395 @SYMTestCaseDesc        CSqlSrvDatabase::OpenL() OOM test - secure database.
   396 @SYMTestPriority        High
   397 @SYMTestActions         The test runs CSqlSrvDatabase::OpenL() in an OOM loop.
   398                         UTF16 encoded database is used.
   399 @SYMTestExpectedResults Test must not fail
   400 @SYMDEF                 DEF144577, PDEF44845
   401 */  
   402 void Utf16OpenSecureDatabaseOomTest()
   403     {
   404     OpenSecureDatabaseOomTest();
   405     }
   406 
   407 /**
   408 @SYMTestCaseID          PDS-SQL-UT-4185
   409 @SYMTestCaseDesc        CSqlSrvDatabase::OpenL() OOM test - secure database.
   410 @SYMTestPriority        High
   411 @SYMTestActions         The test runs CSqlSrvDatabase::OpenL() in an OOM loop.
   412                         UTF8 encoded database is used.
   413 @SYMTestExpectedResults Test must not fail
   414 @SYMDEF                 DEF145047
   415 */  
   416 void Utf8OpenSecureDatabaseOomTest()
   417     {
   418     OpenSecureDatabaseOomTest();
   419     }
   420 
   421 /////////////////////////////////////////////////////////////
   422 
   423 const TInt KDbConnCount = 7; 
   424 #ifdef _DEBUG
   425 const TInt KDbAttachedCount = 10;
   426 #endif
   427 TBuf<10> TheAttachedDbName;
   428 
   429 //Declares KDbConnCount "CSqlSrvDatabase*" variables.  
   430 #define TEST_DECLARE_DB_VARS() \
   431     CSqlSrvDatabase* db[KDbConnCount]; \
   432     Mem::FillZ(db, sizeof(db));
   433 
   434 //Declares all KDbConnCount "CSqlSrvDatabase*" objects.  
   435 #define TEST_DELETE_DB() \
   436     for(TInt i=0;i<KDbConnCount;++i) \
   437         { \
   438         delete db[i]; \
   439         }
   440 
   441 //Creates CSqlSrvDatabase object where the database file is dbFile (the second macro parameter).
   442 //The CSqlSrvDatabase pointer db[N - 1] (N is the first macro parameter) will be set to point to 
   443 //the created CSqlSrvDatabase object. 
   444 //N is the number of the database to be opened, between 1 and KDbConnCount.
   445 #define TEST_OPEN_DB(N, dbFile) \
   446     __ASSERT_DEBUG(N > 0 && N <= KDbConnCount, User::Invariant()); \
   447     TRAP(err, fdata.SetL(msg, dbFile.Length(), 0, dbFile)); \
   448     if(err != KErrNone) \
   449         { \
   450         goto Cleanup; \
   451         } \
   452     db[N - 1] = NULL; \
   453     TRAP(err, db[N - 1] = CSqlSrvDatabase::OpenL(fdata)); \
   454     if(err != KErrNone) \
   455         { \
   456         goto Cleanup; \
   457         }
   458 
   459 //Attaches the "dbFile" database to the database number specified by the first macro parameter.
   460 //The attached database name is "A<M>", where M is the third macro parameter.
   461 //N is the number of the database connection, between 1 and KDbConnCount.
   462 //M is the number of the database to be attached, between 1 and KDbAttachedCount.
   463 #define TEST_ATTACH_DB(N, dbFile, M) \
   464     __ASSERT_DEBUG(N > 0 && N <= KDbConnCount, User::Invariant()); \
   465     __ASSERT_DEBUG(M > 0 && M <= KDbAttachedCount, User::Invariant()); \
   466     TRAP(err, fdata.SetL(msg, dbFile.Length(), 0, dbFile)); \
   467     if(err != KErrNone) \
   468         { \
   469         goto Cleanup; \
   470         } \
   471     TheAttachedDbName.Copy(_L("A")); \
   472     TheAttachedDbName.AppendNum(M); \
   473     TRAP(err, db[N - 1]->AttachDbL(fdata, TheAttachedDbName)); \
   474     if(err != KErrNone) \
   475         { \
   476         goto Cleanup; \
   477         }
   478 
   479 //Detaches database "A<M>" (M is the second macro parameter) from the database identified
   480 //by the number N - the first macro parameter.
   481 //N is the number of the database connection, between 1 and KDbConnCount.
   482 //M is the number of the database to be detached, between 1 and KDbAttachedCount.
   483 #define TEST_DETACH_DB(N, M) \
   484     __ASSERT_DEBUG(N > 0 && N <= KDbConnCount, User::Invariant()); \
   485     __ASSERT_DEBUG(M > 0 && M <= KDbAttachedCount, User::Invariant()); \
   486     if(db[N - 1]) \
   487         { \
   488         TheAttachedDbName.Copy(_L("A")); \
   489         TheAttachedDbName.AppendNum(M); \
   490         TRAP_IGNORE(db[N - 1]->DetachDbL(TheAttachedDbName)); \
   491         }
   492 
   493 /////////////////////////////////////////////////////////////
   494 
   495 void CreateSecureTestDb(const TDesC& aDbFile, TSqlDbEncoding aEncoding)
   496     {
   497     (void)TheFs.Delete(aDbFile);
   498     
   499     TSecurityPolicy defaultPolicy(TSecurityPolicy::EAlwaysPass);
   500     CSqlSecurityPolicy* policy = NULL;
   501     TRAPD(err, policy = CSqlSecurityPolicy::NewL(defaultPolicy));
   502     TEST2(err, KErrNone);
   503     
   504     TSqlSrvFileData& fdata = TheServer->FileData();
   505     RMessage2 msg;
   506     TRAP(err, fdata.SetL(msg, aDbFile.Length(), 0, aDbFile, aEncoding == ESqlDbUtf8 ? &KConfig : NULL));
   507     
   508     CSqlSrvDatabase* db = NULL;
   509     TRAP(err, db = CSqlSrvDatabase::CreateSecureL(fdata, policy));
   510     delete db;
   511     TEST2(err, KErrNone);
   512     }
   513     
   514 void OpenAttachDatabaseOomTest(TSqlDbEncoding aEncoding)
   515     {
   516     //Part of the databases are created by the previous tests.
   517         
   518     TInt failingAllocationNo = 0;
   519     TheTest.Printf(_L("Iteration:\r\n"));
   520     
   521     TheServer = NULL; 
   522     TRAPD(err, TheServer = CreateSqlServerL());
   523     TEST2(err, KErrNone);
   524 
   525     CreateSecureTestDb(KDbFile3, aEncoding);
   526     CreateSecureTestDb(KDbFile4, aEncoding);
   527     
   528     //The following 2 declarations are used by the macros in the OOM loop
   529     RMessage2 msg;
   530     TSqlSrvFileData& fdata = TheServer->FileData();
   531     
   532     err = KErrNoMemory;
   533     while(err == KErrNoMemory)
   534         {
   535         TheTest.Printf(_L(" %d"), ++failingAllocationNo);
   536         OomPreStep(failingAllocationNo);
   537         //Declare KDbConnCount "CSqlSrvDatabase*" variables.
   538         TEST_DECLARE_DB_VARS();
   539         //Create CSqlSrvDatabase object, the database name is the second parameter of the macro. 
   540         //The related db[N - 1] variable will be set to point to the created object.
   541         TEST_OPEN_DB(1, KDbFile2());
   542         TEST_OPEN_DB(2, KDbFile2());
   543         TEST_OPEN_DB(3, KDbFile3());
   544         TEST_OPEN_DB(4, KDbFile3());
   545         TEST_OPEN_DB(5, KDbFile3());
   546         TEST_OPEN_DB(6, KDbFile());
   547         TEST_OPEN_DB(7, KDbFile());
   548         //Attach to the database with the number specified as first macro parameter, the database file, specified
   549         //as second macro parameter. The logical name of the attached database is "A<M>", where M is the third macro parameter.
   550         TEST_ATTACH_DB(1, KDbFile(), 1);
   551         TEST_ATTACH_DB(2, KDbFile(), 2);
   552         TEST_ATTACH_DB(2, KDbFile(), 3);
   553         TEST_ATTACH_DB(5, KDbFile4(), 4);
   554         TEST_ATTACH_DB(2, KDbFile4(), 5);
   555         TEST_ATTACH_DB(2, KDbFile4(), 6);
   556         TEST_ATTACH_DB(5, KDbFile4(), 7);
   557         TEST_ATTACH_DB(5, KDbFile4(), 8);
   558         TEST_ATTACH_DB(1, KDbFile4(), 9);
   559         TEST_ATTACH_DB(1, KDbFile(), 10);
   560 Cleanup:
   561         __UHEAP_SETBURSTFAIL(RAllocator::ENone, 0, 0);
   562         //Detach from the database with the number specified as first macro parameter, the database         
   563         //with name "A<M>", where M is the second macro parameter.
   564         TEST_DETACH_DB(1, 9);
   565         TEST_DETACH_DB(1, 1);
   566         TEST_DETACH_DB(1, 10);
   567         TEST_DETACH_DB(2, 2);
   568         TEST_DETACH_DB(2, 3);
   569         TEST_DETACH_DB(2, 5);
   570         TEST_DETACH_DB(2, 6);
   571         TEST_DETACH_DB(5, 4);
   572         TEST_DETACH_DB(5, 7);
   573         TEST_DETACH_DB(5, 8);
   574         //Delete all created CSqlSrvDatabase objects.
   575         TEST_DELETE_DB();        
   576         
   577         OomPostStep();
   578         }
   579     
   580     delete TheServer;
   581     TheServer = NULL;
   582     
   583     (void)TheFs.Delete(KDbFile4);
   584     (void)TheFs.Delete(KDbFile3);
   585     
   586     TEST2(err, KErrNone);   
   587     TheTest.Printf(_L("\r\n===CSqlSrvDatabase::OpenL() & CSqlSrvDatabase::AttachDbL() OOM test succeeded at heap failure rate of %d ===\r\n"), failingAllocationNo);
   588     }
   589 
   590 /**
   591 @SYMTestCaseID          PDS-SQL-UT-4171
   592 @SYMTestCaseDesc        CSqlSrvDatabase::OpenL() & CSqlSrvDatabase::AttachDbL() OOM test.
   593 @SYMTestPriority        High
   594 @SYMTestActions         The test runs CSqlSrvDatabase::OpenL() and CSqlSrvDatabase::AttachDbL() in an OOM test.
   595                         The test is a complex one - 7 (KDbConnCount constant) databases opened 
   596                         (secure and non-secure), 10 (KDbAttachedCount constant) databases
   597                         attached (secure and non-secure).
   598                         UTF16 encoded database is used.
   599 @SYMTestExpectedResults Test must not fail
   600 @SYMDEF                 DEF144577, DEF144603
   601 */  
   602 void Utf16OpenAttachDatabaseOomTest()
   603     {
   604     OpenAttachDatabaseOomTest(ESqlDbUtf16);
   605     }
   606 
   607 /**
   608 @SYMTestCaseID          PDS-SQL-UT-4186
   609 @SYMTestCaseDesc        CSqlSrvDatabase::OpenL() & CSqlSrvDatabase::AttachDbL() OOM test.
   610 @SYMTestPriority        High
   611 @SYMTestActions         The test runs CSqlSrvDatabase::OpenL() and CSqlSrvDatabase::AttachDbL() in an OOM test.
   612                         The test is a complex one - 7 (KDbConnCount constant) databases opened 
   613                         (secure and non-secure), 10 (KDbAttachedCount constant) databases
   614                         attached (secure and non-secure).
   615                         UTF8 encoded database is used.
   616 @SYMTestExpectedResults Test must not fail
   617 @SYMDEF                 DEF144603
   618 */  
   619 void Utf8OpenAttachDatabaseOomTest()
   620     {
   621     OpenAttachDatabaseOomTest(ESqlDbUtf8);
   622     }
   623 
   624 void OpenAttachDatabaseOomTest2(TSqlDbEncoding aEncoding)
   625     {
   626     TheServer = NULL; 
   627     TRAPD(err, TheServer = CreateSqlServerL());
   628     TEST2(err, KErrNone);
   629     
   630     CreateSecureTestDb(KDbFile3, aEncoding);
   631     CreateSecureTestDb(KDbFile4, aEncoding);
   632     
   633     TInt failingAllocationNo = 0;
   634     TheTest.Printf(_L("Iteration:\r\n"));
   635     
   636     RMessage2 msg;
   637     TSqlSrvFileData& fdata = TheServer->FileData();
   638     
   639     err = KErrNoMemory;
   640     while(err == KErrNoMemory)
   641         {
   642         TheTest.Printf(_L(" %d"), ++failingAllocationNo);
   643         OomPreStep(failingAllocationNo);
   644         
   645         TRAP(err, fdata.SetL(msg, KDbFile3().Length(), 0, KDbFile3));
   646         if(err == KErrNone)
   647             {
   648             CSqlSrvDatabase* db = NULL;
   649             TRAP(err, db = CSqlSrvDatabase::OpenL(fdata));
   650             if(err == KErrNone)
   651                 {
   652                 TRAP(err, fdata.SetL(msg, KDbFile4().Length(), 0, KDbFile4));
   653                 if(err == KErrNone)
   654                     {
   655                     TRAP(err, db->AttachDbL(fdata, _L("db2")));
   656                     if(err == KErrNone)
   657                         {
   658                         TRAP(err, db->DetachDbL(_L("db2")));
   659                         }
   660                     }
   661                 delete db;
   662                 }
   663             }
   664         OomPostStep();
   665         }
   666     
   667     (void)TheFs.Delete(KDbFile4);
   668     (void)TheFs.Delete(KDbFile3);
   669     
   670     delete TheServer;
   671     TheServer = NULL;
   672      
   673     TEST2(err, KErrNone);   
   674     TheTest.Printf(_L("\r\n===CSqlSrvDatabase::OpenL() & CSqlSrvDatabase::AttachDbL() OOM test 2 succeeded at heap failure rate of %d ===\r\n"), failingAllocationNo);
   675     }
   676 
   677 /**
   678 @SYMTestCaseID          PDS-SQL-UT-4172
   679 @SYMTestCaseDesc        CSqlSrvDatabase::OpenL() & CSqlSrvDatabase::AttachDbL() OOM test.
   680 @SYMTestPriority        High
   681 @SYMTestActions         The test runs CSqlSrvDatabase::OpenL() and CSqlSrvDatabase::AttachDbL() in an OOM test.
   682                         Two secure databases are created and then, in an OOM loop, the test executes this sequence of
   683                         commands: open first database, attach the second database, detach the attached database,
   684                         close the first database. 
   685                         UTF16 encoded database is used.
   686 @SYMTestExpectedResults Test must not fail
   687 @SYMDEF                 DEF144577, PDEF44845
   688 */  
   689 void Utf16OpenAttachDatabaseOomTest2()
   690     {
   691     OpenAttachDatabaseOomTest2(ESqlDbUtf16);
   692     }
   693 
   694 /**
   695 @SYMTestCaseID          PDS-SQL-UT-4187
   696 @SYMTestCaseDesc        CSqlSrvDatabase::OpenL() & CSqlSrvDatabase::AttachDbL() OOM test.
   697 @SYMTestPriority        High
   698 @SYMTestActions         The test runs CSqlSrvDatabase::OpenL() and CSqlSrvDatabase::AttachDbL() in an OOM test.
   699                         Two secure databases are created and then, in an OOM loop, the test executes this sequence of
   700                         commands: open first database, attach the second database, detach the attached database,
   701                         close the first database. 
   702                         UTF8 encoded database is used.
   703 @SYMTestExpectedResults Test must not fail
   704 @SYMDEF                 DEF145047
   705 */  
   706 void Utf8OpenAttachDatabaseOomTest2()
   707     {
   708     OpenAttachDatabaseOomTest2(ESqlDbUtf8);
   709     }
   710 
   711 void OpenCreateDatabaseOomTest(TSqlDbEncoding aEncoding)
   712     {
   713     TheServer = NULL; 
   714     TRAPD(err, TheServer = CreateSqlServerL());
   715     TEST2(err, KErrNone);
   716     
   717     (void)TheFs.Delete(KDbFile2);
   718     CreateSecureTestDb(KDbFile2, aEncoding);
   719     
   720     TheTest.Printf(_L("Iteration:\r\n"));
   721 
   722     //Open the database
   723     TInt failingAllocationNo = 0;
   724     err = KErrNoMemory;
   725     while(err == KErrNoMemory)
   726         {
   727         TheTest.Printf(_L(" %d"), ++failingAllocationNo);
   728         OomPreStep(failingAllocationNo);
   729         
   730         RMessage2 msg;
   731         TSqlSrvFileData& fdata = TheServer->FileData();
   732         TRAP(err, fdata.SetL(msg, KDbFile2().Length(), 0, KDbFile2));
   733         if(err == KErrNone)
   734             {
   735             CSqlSrvDatabase* db = NULL;
   736             TRAP(err, db = CSqlSrvDatabase::OpenL(fdata));
   737             if(err == KErrNone)
   738                 {
   739                 delete db;
   740                 }
   741             else
   742                 {
   743                 TEST(!db);
   744                 }
   745             }
   746         OomPostStep();
   747         }
   748     TEST2(err, KErrNone);   
   749     err = TheFs.Delete(KDbFile2);
   750     TEST2(err, KErrNone);   
   751     //Create the database
   752     TInt failingAllocationNo2 = 0;
   753     err = KErrNoMemory;
   754     while(err == KErrNoMemory)
   755         {
   756         TheTest.Printf(_L(" %d"), ++failingAllocationNo2);
   757         OomPreStep(failingAllocationNo2);
   758         RMessage2 msg;
   759         TSqlSrvFileData& fdata = TheServer->FileData();
   760         TRAP(err, fdata.SetL(msg, KDbFile2().Length(), 0, KDbFile2, aEncoding == ESqlDbUtf8 ? &KConfig : NULL));
   761         if(err == KErrNone)
   762             {
   763             TSecurityPolicy defaultPolicy(TSecurityPolicy::EAlwaysPass);
   764             CSqlSecurityPolicy* policy = NULL;
   765             TRAP(err, policy = CSqlSecurityPolicy::NewL(defaultPolicy));
   766             if(err == KErrNone)
   767                 {
   768                 CSqlSrvDatabase* db = NULL;
   769                 TRAP(err, db = CSqlSrvDatabase::CreateSecureL(fdata, policy));
   770                 if(err == KErrNone)
   771                     {
   772                     delete db;
   773                     }
   774                 else
   775                     {
   776                     TEST(!db);
   777                     }
   778                 }
   779             }
   780         OomPostStep();
   781         }
   782     
   783     (void)TheFs.Delete(KDbFile2);
   784     
   785     delete TheServer;
   786     TheServer = NULL;
   787      
   788     TEST2(err, KErrNone);   
   789     TheTest.Printf(_L("\r\n===CSqlSrvDatabase::OpenL() & CSqlSrvDatabase::CreateSecureL() OOM test succeeded at heap failure rate of %d ===\r\n"), 
   790                    failingAllocationNo + failingAllocationNo2);
   791     }
   792 
   793 /**
   794 @SYMTestCaseID          PDS-SQL-UT-4173
   795 @SYMTestCaseDesc        CSqlSrvDatabase::OpenL() & CSqlSrvDatabase::CreateSecureL() OOM test.
   796 @SYMTestPriority        High
   797 @SYMTestActions         The test runs CSqlSrvDatabase::OpenL() and CSqlSrvDatabase::CreateSecureL() in an OOM test.
   798                         The test creates a secure database then executes CSqlSrvDatabase::OpenL() in an OOM loop.
   799                         After that the database is deleted and the test executes CSqlSrvDatabase::CreateSecureL() in an OOM loop.
   800                         The purpose of the test is to check that the CSqlSrver maps are properly updated when
   801                         the database is closed.
   802                         UTF16 encoded database is used.
   803 @SYMTestExpectedResults Test must not fail
   804 @SYMDEF                 DEF144577, PDEF44845
   805 */  
   806 void Utf16OpenCreateDatabaseOomTest()
   807     {
   808     OpenCreateDatabaseOomTest(ESqlDbUtf16);
   809     }
   810 
   811 /**
   812 @SYMTestCaseID          PDS-SQL-UT-4188
   813 @SYMTestCaseDesc        CSqlSrvDatabase::OpenL() & CSqlSrvDatabase::CreateSecureL() OOM test.
   814 @SYMTestPriority        High
   815 @SYMTestActions         The test runs CSqlSrvDatabase::OpenL() and CSqlSrvDatabase::CreateSecureL() in an OOM test.
   816                         The test creates a secure database then executes CSqlSrvDatabase::OpenL() in an OOM loop.
   817                         After that the database is deleted and the test executes CSqlSrvDatabase::CreateSecureL() in an OOM loop.
   818                         The purpose of the test is to check that the CSqlSrver maps are properly updated when
   819                         the database is closed.
   820                         UTF8 encoded database is used.
   821 @SYMTestExpectedResults Test must not fail
   822 @SYMDEF                 DEF145047
   823 */  
   824 void Utf8OpenCreateDatabaseOomTest()
   825     {
   826     OpenCreateDatabaseOomTest(ESqlDbUtf8);
   827     }
   828 
   829 void DoTests()
   830 	{
   831 #ifndef _DEBUG
   832     TheTest.Start(_L("This test can be run only in debug mode!"));
   833 #else	
   834     CActiveScheduler* scheduler = new CActiveScheduler;
   835     TEST(scheduler != NULL);
   836     CActiveScheduler::Install(scheduler);
   837 	
   838     TheTest.Start(_L(" @SYMTestCaseID:PDS-SQL-UT-4167 CSqlSrvDatabase::CreateL() OOM unit test"));
   839     Utf16CreateDatabaseOomTest();
   840 
   841     TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4168 CSqlSrvDatabase::OpenL() OOM unit test - non-secure database"));
   842     Utf16OpenDatabaseOomTest();
   843 
   844     TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4169 CSqlSrvDatabase::CreateSecureL() OOM unit test"));
   845     Utf16CreateSecureDatabaseOomTest();
   846 
   847     TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4170 CSqlSrvDatabase::OpenL() OOM unit test - secure database"));
   848     Utf16OpenSecureDatabaseOomTest();
   849     
   850     TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4171 CSqlSrvDatabase::OpenL() & CSqlSrvDatabase::AttachDbL() OOM unit test"));
   851     Utf16OpenAttachDatabaseOomTest();
   852 
   853     TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4172 CSqlSrvDatabase::OpenL() & CSqlSrvDatabase::AttachDbL() OOM unit test - 2"));
   854     Utf16OpenAttachDatabaseOomTest2();
   855 
   856     TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4173 CSqlSrvDatabase::OpenL() & CSqlSrvDatabase::CreateL() OOM unit test"));
   857     Utf16OpenCreateDatabaseOomTest();
   858 
   859     TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4182 CSqlSrvDatabase::CreateL() OOM unit test, UTF8 database"));
   860     Utf8CreateDatabaseOomTest();
   861 
   862     TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4183 CSqlSrvDatabase::OpenL() OOM unit test - non-secure UTF8 database"));
   863     Utf8OpenDatabaseOomTest();
   864 
   865     TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4184 CSqlSrvDatabase::CreateSecureL() OOM unit test, UTF8 database"));
   866     Utf8CreateSecureDatabaseOomTest();
   867 
   868     TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4185 CSqlSrvDatabase::OpenL() OOM unit test - secure UTF8 database"));
   869     Utf8OpenSecureDatabaseOomTest();
   870 
   871     TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4186 CSqlSrvDatabase::OpenL() & CSqlSrvDatabase::AttachDbL() OOM unit test, UTF8 database"));
   872     Utf8OpenAttachDatabaseOomTest();
   873 
   874     TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4187 CSqlSrvDatabase::OpenL() & CSqlSrvDatabase::AttachDbL() OOM unit test 2, UTF8 database"));
   875     Utf8OpenAttachDatabaseOomTest2();
   876 
   877     TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4188 CSqlSrvDatabase::OpenL() & CSqlSrvDatabase::CreateL() OOM unit test, UTF8 database"));
   878     Utf8OpenCreateDatabaseOomTest();
   879     
   880     delete scheduler;
   881 #endif //_DEBUG    
   882 	}
   883 
   884 TInt E32Main()
   885 	{
   886 	TheTest.Title();
   887 	
   888 	CTrapCleanup* tc = CTrapCleanup::New();
   889 	TheTest(tc != NULL);
   890 	
   891 	__UHEAP_MARK;
   892 	
   893 	CreateTestEnv();
   894 	DoTests();
   895 	DestroyTestEnv();
   896 
   897 	__UHEAP_MARKEND;
   898 	
   899 	TheTest.End();
   900 	TheTest.Close();
   901 	
   902 	delete tc;
   903 
   904 	User::Heap().Check();
   905 	return KErrNone;
   906 	}