persistentstorage/sql/TEST/t_sqlstartup.cpp
branchRCL_3
changeset 8 fa9941cf3867
child 9 667e88a979d7
equal deleted inserted replaced
6:5ffdb8f2067f 8:fa9941cf3867
       
     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 
       
    22 ///////////////////////////////////////////////////////////////////////////////////////
       
    23 
       
    24 RTest TheTest(_L("t_sqlstartup test"));
       
    25 
       
    26 static TInt TheProcessHandleCount = 0;
       
    27 static TInt TheThreadHandleCount = 0;
       
    28 static TInt TheAllocatedCellsCount = 0;
       
    29 
       
    30 #ifdef _DEBUG
       
    31 static const TInt KBurstRate = 20;
       
    32 #endif
       
    33 
       
    34 ///////////////////////////////////////////////////////////////////////////////////////
       
    35 
       
    36 void DeleteTestFiles()
       
    37 	{
       
    38 	}
       
    39 
       
    40 ///////////////////////////////////////////////////////////////////////////////////////
       
    41 ///////////////////////////////////////////////////////////////////////////////////////
       
    42 //Test macros and functions
       
    43 void Check(TInt aValue, TInt aLine)
       
    44 	{
       
    45 	if(!aValue)
       
    46 		{
       
    47 		DeleteTestFiles();
       
    48 		RDebug::Print(_L("*** Expresssion evaluated to false\r\n"));
       
    49 		TheTest(EFalse, aLine);
       
    50 		}
       
    51 	}
       
    52 void Check(TInt aValue, TInt aExpected, TInt aLine)
       
    53 	{
       
    54 	if(aValue != aExpected)
       
    55 		{
       
    56 		DeleteTestFiles();
       
    57 		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
       
    58 		TheTest(EFalse, aLine);
       
    59 		}
       
    60 	}
       
    61 #define TEST(arg) ::Check((arg), __LINE__)
       
    62 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
       
    63 
       
    64 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
    65 
       
    66 static void MarkHandles()
       
    67     {
       
    68     RThread().HandleCount(TheProcessHandleCount, TheThreadHandleCount);
       
    69     }
       
    70 
       
    71 static void MarkAllocatedCells()
       
    72     {
       
    73     TheAllocatedCellsCount = User::CountAllocCells();
       
    74     }
       
    75 
       
    76 static void CheckAllocatedCells()
       
    77     {
       
    78     TInt allocatedCellsCount = User::CountAllocCells();
       
    79     TEST2(allocatedCellsCount, TheAllocatedCellsCount);
       
    80     }
       
    81 
       
    82 static void CheckHandles()
       
    83     {
       
    84     TInt endProcessHandleCount;
       
    85     TInt endThreadHandleCount;
       
    86     
       
    87     RThread().HandleCount(endProcessHandleCount, endThreadHandleCount);
       
    88 
       
    89     TEST2(TheProcessHandleCount, endProcessHandleCount);
       
    90     TEST2(TheThreadHandleCount, endThreadHandleCount);
       
    91     }
       
    92 
       
    93 static void OomPreStep(TInt
       
    94 #ifdef _DEBUG        
       
    95     aFailingAllocationNo
       
    96 #endif
       
    97                       )
       
    98     {
       
    99     MarkHandles();
       
   100     MarkAllocatedCells();
       
   101     __UHEAP_MARK;
       
   102     __UHEAP_SETBURSTFAIL(RAllocator::EBurstFailNext, aFailingAllocationNo, KBurstRate);
       
   103     }
       
   104 
       
   105 static void OomPostStep()
       
   106     {
       
   107     __UHEAP_RESET;
       
   108     __UHEAP_MARKEND;
       
   109     CheckAllocatedCells();
       
   110     CheckHandles();
       
   111     }
       
   112 
       
   113 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
   114 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
   115 
       
   116 static void CreateAndDestroySqlServerL()
       
   117     {
       
   118     CSqlServer* server = CSqlServer::NewLC();
       
   119     CleanupStack::PopAndDestroy(server);
       
   120     }
       
   121 
       
   122 static CSqlServer* CreateSqlServerL()
       
   123     {
       
   124     CSqlServer* server = CSqlServer::NewLC();
       
   125     CleanupStack::Pop(server);
       
   126     return server;
       
   127     }
       
   128 
       
   129 /**
       
   130 @SYMTestCaseID          PDS-SQL-UT-4159
       
   131 @SYMTestCaseDesc        SQL server startup OOM test
       
   132 @SYMTestPriority        High
       
   133 @SYMTestActions         Runs the SQL server startup code in an OOM loop.
       
   134 @SYMTestExpectedResults Test must not fail
       
   135 @SYMDEF                 DEF144096
       
   136 */  
       
   137 void SqlServerStartupOomTest()
       
   138     {
       
   139     TInt err = KErrNoMemory;
       
   140     TInt failingAllocationNo = 0;
       
   141     TheTest.Printf(_L("Iteration:\r\n"));
       
   142     while(err == KErrNoMemory)
       
   143         {
       
   144         TheTest.Printf(_L(" %d"), ++failingAllocationNo);
       
   145         OomPreStep(failingAllocationNo);
       
   146         TRAP(err, CreateAndDestroySqlServerL());
       
   147         OomPostStep();
       
   148         }
       
   149     if(err != KErrNoMemory)
       
   150         {
       
   151         TEST2(err, KErrNone);   
       
   152         }
       
   153     TheTest.Printf(_L("\r\n===OOM test succeeded at heap failure rate of %d ===\r\n"), failingAllocationNo);
       
   154     }
       
   155 
       
   156 /**
       
   157 @SYMTestCaseID          PDS-SQL-UT-4160
       
   158 @SYMTestCaseDesc        CSqlServer::GetBackUpListL() OOM test
       
   159 @SYMTestPriority        High
       
   160 @SYMTestActions         Calls CSqlServer::GetBackUpListL() in an OOM loop.
       
   161 @SYMTestExpectedResults Test must not fail
       
   162 @SYMDEF                 DEF144096
       
   163 */  
       
   164 void GetBackupListOomTest()
       
   165     {
       
   166     CSqlServer* server = NULL;
       
   167     TRAPD(err, server = CreateSqlServerL());
       
   168     TEST2(err, KErrNone);
       
   169   
       
   170     TInt fileCnt = 0;
       
   171     err = KErrNoMemory;
       
   172     TInt failingAllocationNo = 0;
       
   173     TheTest.Printf(_L("Iteration:\r\n"));
       
   174     while(err == KErrNoMemory)
       
   175         {
       
   176         TheTest.Printf(_L(" %d"), ++failingAllocationNo);
       
   177         OomPreStep(failingAllocationNo);
       
   178         const TUid KDbUd = {0x98765432};
       
   179         RArray<TParse> files;
       
   180         TRAP(err, server->GetBackUpListL(KDbUd, files));
       
   181         fileCnt = files.Count(); 
       
   182         files.Close();
       
   183         OomPostStep();
       
   184         }
       
   185     
       
   186     delete server;
       
   187     
       
   188     if(err != KErrNoMemory)
       
   189         {
       
   190         TEST2(err, KErrNone);   
       
   191         }
       
   192     TheTest.Printf(_L("\r\n===OOM test succeeded at heap failure rate of %d ===\r\nFile count: %d\r\n"), failingAllocationNo, fileCnt);
       
   193     }
       
   194 
       
   195 /**
       
   196 @SYMTestCaseID          PDS-SQL-UT-4161
       
   197 @SYMTestCaseDesc        SQL server startup file I/O error simulation test
       
   198 @SYMTestPriority        High
       
   199 @SYMTestActions         Runs the SQL server startup code in a file I/O error simulation loop.
       
   200 @SYMTestExpectedResults Test must not fail
       
   201 @SYMDEF                 DEF144096
       
   202 */  
       
   203 void SqlServerStartupFileIoErrorTest()
       
   204     {
       
   205     RFs fs;
       
   206     TInt err = fs.Connect();
       
   207     TEST2(err, KErrNone);
       
   208     
       
   209     for(TInt fsError=KErrNotFound;fsError>=KErrBadName;--fsError)
       
   210         {
       
   211         TheTest.Printf(_L("===Simulated error: %d\r\nIteration: "), fsError);
       
   212         err = KErrNotFound;
       
   213         TInt cnt=1;
       
   214         while(err<KErrNone)
       
   215             {
       
   216             TheTest.Printf(_L("%d "), cnt);
       
   217             (void)fs.SetErrorCondition(fsError, cnt);
       
   218             TRAP(err, CreateAndDestroySqlServerL());
       
   219             (void)fs.SetErrorCondition(KErrNone);
       
   220             if(err != KErrNone)
       
   221                 {
       
   222                 ++cnt;
       
   223                 }
       
   224             }
       
   225         TEST2(err, KErrNone);
       
   226         TheTest.Printf(_L("\r\n===File I/O error simulation test succeeded on iteration %d===\r\n"), cnt);
       
   227         }
       
   228 
       
   229     fs.Close();
       
   230     }
       
   231 
       
   232 /**
       
   233 @SYMTestCaseID          PDS-SQL-UT-4162
       
   234 @SYMTestCaseDesc        CSqlServer::GetBackUpListL() file I/O error simulation test
       
   235 @SYMTestPriority        High
       
   236 @SYMTestActions         Calls CSqlServer::GetBackUpListL() in a file I/O error simulation loop.
       
   237 @SYMTestExpectedResults Test must not fail
       
   238 @SYMDEF                 DEF144096
       
   239 */  
       
   240 void GetBackupListFileIoErrorTest()
       
   241     {
       
   242     CSqlServer* server = NULL;
       
   243     TRAPD(err, server = CreateSqlServerL());
       
   244     TEST2(err, KErrNone);
       
   245 
       
   246     for(TInt fsError=KErrNotFound;fsError>=KErrBadName;--fsError)
       
   247         {
       
   248         TheTest.Printf(_L("===Simulated error: %d\r\nIteration: "), fsError);
       
   249         err = KErrNotFound;
       
   250         TInt fileCnt = 0;
       
   251         TInt cnt=1;
       
   252         while(err<KErrNone)
       
   253             {
       
   254             TheTest.Printf(_L("%d "), cnt);
       
   255             (void)server->Fs().SetErrorCondition(fsError, cnt);
       
   256             const TUid KDbUd = {0x98765432};
       
   257             RArray<TParse> files;
       
   258             TRAP(err, server->GetBackUpListL(KDbUd, files));
       
   259             fileCnt = files.Count(); 
       
   260             files.Close();
       
   261             (void)server->Fs().SetErrorCondition(KErrNone);
       
   262             if(err != KErrNone)
       
   263                 {
       
   264                 ++cnt;
       
   265                 }
       
   266             }
       
   267         TEST2(err, KErrNone);
       
   268         TheTest.Printf(_L("\r\n===File I/O error simulation test succeeded on iteration %d===\r\nFile count: %d\r\n"), cnt, fileCnt);
       
   269         }
       
   270         
       
   271     delete server;
       
   272     }
       
   273 
       
   274 /**
       
   275 @SYMTestCaseID          PDS-SQL-UT-4163
       
   276 @SYMTestCaseDesc        Test for DEF144196: SQL, server code coverage can be improved
       
   277 @SYMTestPriority        High
       
   278 @SYMTestActions         Tests the UTF conversion functions implemented in SqlSrvUtil.cpp.
       
   279 @SYMTestExpectedResults Test must not fail
       
   280 @SYMDEF                 DEF144196
       
   281 */  
       
   282 void UtfConversionTest()
       
   283     {
       
   284     /////////       UTF16ToUTF8()       ///////////////////////
       
   285     _LIT(KStr16, "abcd");
       
   286     _LIT8(KStr8, "abcd");
       
   287     TBuf8<KMaxFileName + 1> bufout;
       
   288     TBool rc = UTF16ToUTF8(KStr16, bufout);
       
   289     TEST(rc);
       
   290     TEST(bufout == KStr8);
       
   291     //Test where the input buffer contains non-convertible characters
       
   292     TBuf<2> name2;
       
   293     name2.SetLength(2);
       
   294     name2[0] = TChar(0xD800); 
       
   295     name2[1] = TChar(0xFC00); 
       
   296     rc = UTF16ToUTF8(name2, bufout);
       
   297     TEST(!rc);
       
   298     /////////       UTF16ToUTF8Z()       ///////////////////////
       
   299     _LIT8(KStr8Z, "abcd\x0");
       
   300     rc = UTF16ToUTF8Z(KStr16, bufout);
       
   301     TEST(rc);
       
   302     TEST(bufout == KStr8Z);
       
   303     //Test where the input buffer contains non-convertible characters
       
   304     rc = UTF16ToUTF8Z(name2, bufout);
       
   305     TEST(!rc);
       
   306     /////////       UTF16ZToUTF8Z()       ///////////////////////
       
   307     _LIT(KStr16Z, "abcd\x0");
       
   308     rc = UTF16ZToUTF8Z(KStr16Z, bufout);
       
   309     TEST(rc);
       
   310     TEST(bufout == KStr8Z);
       
   311     //Test where the input buffer contains non-convertible characters
       
   312     TBuf<3> name3;
       
   313     name3.SetLength(3);
       
   314     name3[0] = TChar(0xD800); 
       
   315     name3[1] = TChar(0xFC00); 
       
   316     name3[2] = TChar(0x0000); 
       
   317     rc = UTF16ZToUTF8Z(name3, bufout);
       
   318     TEST(!rc);
       
   319     }
       
   320 
       
   321 void DoTests()
       
   322 	{
       
   323     CActiveScheduler* scheduler = new CActiveScheduler;
       
   324     TEST(scheduler != NULL);
       
   325     CActiveScheduler::Install(scheduler);
       
   326 	
       
   327     TheTest.Start(_L(" @SYMTestCaseID:PDS-SQL-UT-4159 SQL server startup OOM test"));
       
   328     SqlServerStartupOomTest();
       
   329 
       
   330     TheTest.Next (_L(" @SYMTestCaseID:PDS-SQL-UT-4160 CSqlServer::GetBackUpListL() OOM test"));
       
   331     GetBackupListOomTest();
       
   332 
       
   333     TheTest.Next (_L(" @SYMTestCaseID:PDS-SQL-UT-4161 SQL server startup file I/O error simulation test"));
       
   334     SqlServerStartupFileIoErrorTest();
       
   335     
       
   336     TheTest.Next (_L(" @SYMTestCaseID:PDS-SQL-UT-4162 CSqlServer::GetBackUpListL() file I/O error simulation test"));
       
   337     GetBackupListFileIoErrorTest();
       
   338 
       
   339     TheTest.Next (_L(" @SYMTestCaseID:PDS-SQL-UT-4163 SQL server, UTF conversion test"));
       
   340     UtfConversionTest();
       
   341 
       
   342     delete scheduler;
       
   343 	}
       
   344 
       
   345 TInt E32Main()
       
   346 	{
       
   347 	TheTest.Title();
       
   348 	
       
   349 	CTrapCleanup* tc = CTrapCleanup::New();
       
   350 	
       
   351 	__UHEAP_MARK;
       
   352 	
       
   353 	DoTests();
       
   354 	DeleteTestFiles();
       
   355 
       
   356 	__UHEAP_MARKEND;
       
   357 	
       
   358 	TheTest.End();
       
   359 	TheTest.Close();
       
   360 	
       
   361 	delete tc;
       
   362 
       
   363 	User::Heap().Check();
       
   364 	return KErrNone;
       
   365 	}