persistentstorage/sql/TEST/t_sqlfilesrvcrash1.cpp
branchRCL_3
changeset 8 fa9941cf3867
equal deleted inserted replaced
6:5ffdb8f2067f 8:fa9941cf3867
       
     1 // Copyright (c) 2009 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 /**
       
    17 Instructions:
       
    18 
       
    19 This is a manual test created to verify DEFXXXXX, 2 executables needs to be run to do this test
       
    20 1) t_sqlfilesrvcrash1.exe - Generate a corrupted journal file, this will cause the device to reset. 
       
    21 2) t_sqlfilesrvcrash2.exe - After the reboot, tests if SQL can handle the courrpted journal file.
       
    22 
       
    23 This test requires a non-rugged drive to store the database file and therefore will only work in hardware mode
       
    24 */
       
    25 
       
    26 #include <e32test.h>
       
    27 #include <f32file.h>
       
    28 #include <Sqldb.h>
       
    29 
       
    30 RTest TheTest(_L("t_sqlfilesrvcrash1 test"));
       
    31 
       
    32 #if !defined __WINS__ && !defined __WINSCW__
       
    33 
       
    34 RFs TheFs;
       
    35 RSqlDatabase TheDb;
       
    36 
       
    37 const TInt KControlIoRuggedOn=2;
       
    38 const TInt KControlIoRuggedOff=3;
       
    39 const TInt KControlIoIsRugged=4;
       
    40 
       
    41 const TChar KDrvLetter = ('E');
       
    42 _LIT(KTestDir, "E:\\test\\");
       
    43 _LIT(KDbName, "E:\\test\\t_sqlfilesrvcrash.db");
       
    44 _LIT8(KConfigStr, "page_size=32768");
       
    45 _LIT(KFileSrvName, "efile.exe");
       
    46 
       
    47 ///////////////////////////////////////////////////////////////////////////////////////
       
    48 //Deletes all created test files.
       
    49 void DeleteTestFiles()
       
    50     {
       
    51     TheDb.Close();
       
    52     (void)RSqlDatabase::Delete(KDbName);
       
    53     }
       
    54 
       
    55 ///////////////////////////////////////////////////////////////////////////////////////
       
    56 //Test macros and functions
       
    57 void Check(TInt aValue, TInt aLine)
       
    58     {
       
    59     if(!aValue)
       
    60         {
       
    61         DeleteTestFiles();
       
    62         RDebug::Print(_L("*** Line %d\r\n"), aLine);
       
    63         TheTest(EFalse, aLine);
       
    64         }
       
    65     }
       
    66 void Check(TInt aValue, TInt aExpected, TInt aLine)
       
    67     {
       
    68     if(aValue != aExpected)
       
    69         {
       
    70         DeleteTestFiles();
       
    71         RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
       
    72         TheTest(EFalse, aLine);
       
    73         }
       
    74     }
       
    75 
       
    76 #define TEST(arg) ::Check((arg), __LINE__)
       
    77 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
       
    78 
       
    79 ///////////////////////////////////////////////////////////////////////////////////////
       
    80 //Set the drive to Rugged or non-rugged depending on the input parameter
       
    81 void SetDrive(TBool aSetToRugged)
       
    82     {
       
    83     TUint8 isRugged;
       
    84     TPtr8 pRugged(&isRugged,1,1);
       
    85 
       
    86     TInt drvNum;
       
    87     TInt err = TheFs.CharToDrive(KDrvLetter, drvNum);
       
    88     TEST2(err, KErrNone);
       
    89     err=TheFs.ControlIo(drvNum,KControlIoIsRugged,pRugged);
       
    90     TEST2(err, KErrNone);
       
    91     
       
    92     if(!aSetToRugged)
       
    93         {
       
    94         if(isRugged)
       
    95             {
       
    96             err=TheFs.ControlIo(drvNum,KControlIoRuggedOff);
       
    97             TEST2(err, KErrNone);
       
    98             }
       
    99         }
       
   100     else
       
   101         {
       
   102         if(!isRugged)
       
   103             {
       
   104             err=TheFs.ControlIo(drvNum,KControlIoRuggedOn);
       
   105             TEST2(err, KErrNone);
       
   106             }
       
   107         }
       
   108     }
       
   109 
       
   110 //Creates file session instance and the test directory
       
   111 void CreateTestEnv()
       
   112     {
       
   113     TInt err = TheFs.Connect();
       
   114     TEST2(err, KErrNone);
       
   115     
       
   116     //Set the drive to non-rugged
       
   117     TBool setDriveToRugged = EFalse;
       
   118     SetDrive(setDriveToRugged);
       
   119     
       
   120     err = TheFs.MkDir(KTestDir);
       
   121     TEST(err == KErrNone || err == KErrAlreadyExists);
       
   122     }
       
   123 
       
   124 TInt KillProcess(const TDesC& aProcessName)
       
   125     {
       
   126     TFullName name;
       
   127     //RDebug::Print(_L("Find and kill \"%S\" process.\n"), &aProcessName);
       
   128     TBuf<64> pattern(aProcessName);
       
   129     TInt length = pattern.Length();
       
   130     pattern += _L("*");
       
   131     TFindProcess procFinder(pattern);
       
   132 
       
   133     while (procFinder.Next(name) == KErrNone)
       
   134         {
       
   135         if (name.Length() > length)
       
   136             {//If found name is a string containing aProcessName string.
       
   137             TChar c(name[length]);
       
   138             if (c.IsAlphaDigit() ||
       
   139                 c == TChar('_') ||
       
   140                 c == TChar('-'))
       
   141                 {
       
   142                 //RDebug::Print(_L(":: Process name: \"%S\".\n"), &name);
       
   143                 continue;
       
   144                 }
       
   145             }
       
   146         RProcess proc;
       
   147         if (proc.Open(name) == KErrNone)
       
   148             {
       
   149             RDebug::Print(_L("About to kill process \"%S\", This will force a reboot\n"), &name);
       
   150             proc.Kill(0);
       
   151             
       
   152             }
       
   153         proc.Close();
       
   154         }
       
   155     return KErrNone;
       
   156     }
       
   157 
       
   158 /**
       
   159 @SYMTestCaseID          PDS-SQL-CT-4164
       
   160 @SYMTestCaseDesc        Tests for DEF144027: SQL Open returns error if the reported and actual file size are different
       
   161                         This function creates a corrupted jorunal file on a non-rugged FAT, where the actual and 
       
   162                         reported file size is different. This is done by killing the file server forcing the device to 
       
   163                         reset. After the reset t_sqlfilesrvcrash2.exe is used to verify SQL can handle this corrupted
       
   164                         journal file properly.
       
   165 @SYMTestActions         DEF144027: SQL Open returns error if the reported and actual file size are different
       
   166 @SYMTestExpectedResults Test must not fail
       
   167 @SYMTestPriority        Medium
       
   168 @SYMDEF                 DEF144027
       
   169                         DEF144238
       
   170 */
       
   171 void DEF144027()
       
   172 	{
       
   173     TInt err = TheDb.Create(KDbName, &KConfigStr);
       
   174     TEST2(err, KErrNone);
       
   175     
       
   176     err = TheDb.Exec(_L("CREATE TABLE t1(NUM INTEGER)"));
       
   177     TEST2(err, 1);
       
   178     err = TheDb.Exec(_L("CREATE TABLE t2(NUM INTEGER)"));
       
   179     TEST2(err, 1);
       
   180     
       
   181     err = TheDb.Exec(_L("INSERT INTO t1(NUM) VALUES (1)"));
       
   182     TEST2(err, 1);
       
   183     
       
   184     err = TheDb.Exec(_L("INSERT INTO t2(NUM) VALUES (1)"));
       
   185     TEST2(err, 1);
       
   186     
       
   187     TheDb.Close();
       
   188     
       
   189     err = TheDb.Open(KDbName);
       
   190     TEST2(err, KErrNone);
       
   191     
       
   192     err = TheDb.Exec(_L("BEGIN"));
       
   193     TEST(err >= 0);
       
   194     
       
   195     err = TheDb.Exec(_L("CREATE TABLE t3(NUM INTEGER)"));
       
   196     TEST2(err, KErrNone);
       
   197     
       
   198     err = TheDb.Exec(_L("INSERT INTO t1(NUM) VALUES (1)"));
       
   199     TEST2(err, 1);
       
   200     
       
   201     err = TheDb.Exec(_L("INSERT INTO t2(NUM) VALUES (1)"));
       
   202     TEST2(err, 1);
       
   203     
       
   204     err = TheDb.Exec(_L("INSERT INTO t3(NUM) VALUES (1)"));
       
   205     TEST2(err, 1);
       
   206     
       
   207    
       
   208     //Here the transaction is committed but the database is not close
       
   209     //this makes sure that the journal is truncated but not deleted 
       
   210     err = TheDb.Exec(_L("COMMIT"));
       
   211     TEST(err >= 0);
       
   212     
       
   213     //Reset the drive to rugged
       
   214     TBool setDriveToRugged = ETrue;
       
   215     SetDrive(setDriveToRugged);
       
   216     
       
   217     //When the file server is killed, the file size meta data will not be updated
       
   218     KillProcess(KFileSrvName);  
       
   219 	}
       
   220 
       
   221 void DoTests()
       
   222     {    
       
   223     TheTest.Start(_L(" @SYMTestCaseID:PDS-SQL-CT-4164 DEF144027: SQL Open returns error if the reported and actual file size are different"));
       
   224     DEF144027();    
       
   225     }
       
   226 #endif //#if !defined __WINS__ && !defined __WINSCW__
       
   227 
       
   228 TInt E32Main()
       
   229     {
       
   230     TheTest.Title();
       
   231     
       
   232     CTrapCleanup* tc = CTrapCleanup::New();
       
   233     
       
   234     __UHEAP_MARK;
       
   235     
       
   236 #if !defined __WINS__ && !defined __WINSCW__
       
   237     CreateTestEnv();
       
   238     DeleteTestFiles();
       
   239     DoTests();
       
   240     TheFs.Close();
       
   241     TheTest.End();
       
   242 #else
       
   243     TheTest.Start(_L("This test works only works on hardware!"));
       
   244     TheTest.End();
       
   245 #endif  
       
   246  
       
   247     __UHEAP_MARKEND;
       
   248     
       
   249     TheTest.Close();
       
   250     
       
   251     delete tc;
       
   252 
       
   253     User::Heap().Check();
       
   254     return KErrNone;
       
   255     }