github.com/jdgcs/sqlite3@v1.12.1-0.20210908114423-bc5f96e4dd51/testdata/tcl/crash2.test (about)

     1  # 2001 September 15
     2  #
     3  # The author disclaims copyright to this source code.  In place of
     4  # a legal notice, here is a blessing:
     5  #
     6  #    May you do good and not evil.
     7  #    May you find forgiveness for yourself and forgive others.
     8  #    May you share freely, never taking more than you give.
     9  #
    10  #***********************************************************************
    11  # This file implements regression tests for SQLite library.
    12  #
    13  # The focus of this file is testing the ability of the database to
    14  # uses its rollback journal to recover intact (no database corruption)
    15  # from a power failure during the middle of a COMMIT. Even more
    16  # specifically, the tests in this file verify this functionality
    17  # for storage mediums with various sector sizes.
    18  #
    19  # $Id: crash2.test,v 1.6 2008/08/25 07:12:29 danielk1977 Exp $
    20  
    21  set testdir [file dirname $argv0]
    22  source $testdir/tester.tcl
    23  
    24  ifcapable !crashtest {
    25    finish_test
    26    return
    27  }
    28  
    29  db close
    30  
    31  # This test is designed to check that the crash-test infrastructure
    32  # can create files that do not consist of an integer number of
    33  # simulated disk blocks (i.e. 3KB file using 2KB disk blocks).
    34  #
    35  do_test crash2-1.1 {
    36    crashsql -delay 500 -file test.db -blocksize 2048 {
    37      PRAGMA auto_vacuum=OFF;
    38      PRAGMA page_size=1024;
    39      BEGIN;
    40      CREATE TABLE abc AS SELECT 1 AS a, 2 AS b, 3 AS c;
    41      CREATE TABLE def AS SELECT 1 AS d, 2 AS e, 3 AS f;
    42      COMMIT;
    43    }
    44    file size test.db
    45  } {3072}
    46  
    47  for {set ii 0} {$ii < 5} {incr ii} {
    48  
    49    # Simple test using the database created above: Create a new
    50    # table so that page 1 and page 4 are modified. Using a
    51    # block-size of 2048 and page-size of 1024, this means
    52    # pages 2 and 3 must also be saved in the journal to avoid
    53    # risking corruption.
    54    #
    55    # The loop is so that this test can be run with a couple
    56    # of different seeds for the random number generator.
    57    #
    58    do_test crash2-1.2.$ii {
    59      crashsql -file test.db -blocksize 2048 [subst {
    60        [string repeat {SELECT random();} $ii]
    61        CREATE TABLE hij(h, i, j);
    62      }]
    63      sqlite3 db test.db
    64      db eval {PRAGMA integrity_check}
    65    } {ok}
    66  }
    67  
    68  proc signature {} {
    69    return [db eval {SELECT count(*), md5sum(a), md5sum(b), md5sum(c) FROM abc}]
    70  }
    71  
    72  # Test case for crashing during journal sync with simulated
    73  # sector-size values from 1024 to 8192.
    74  #
    75  do_test crash2-2.0 {
    76    execsql BEGIN
    77    for {set n 0} {$n < 1000} {incr n} {
    78      execsql "INSERT INTO abc VALUES($n, [expr 2*$n], [expr 3*$n])"
    79    }
    80    execsql {
    81      INSERT INTO abc SELECT * FROM abc;
    82      INSERT INTO abc SELECT * FROM abc;
    83      INSERT INTO abc SELECT * FROM abc;
    84      INSERT INTO abc SELECT * FROM abc;
    85      INSERT INTO abc SELECT * FROM abc;
    86    }
    87    execsql COMMIT
    88    expr ([file size test.db] / 1024) > 450
    89  } {1}
    90  for {set i 1} {$i < 30} {incr i} {
    91    set sig [signature]
    92    set sector [expr 1024 * 1<<($i%4)]
    93    db close
    94    do_test crash2-2.$i.1 {
    95       crashsql -blocksize $sector -delay [expr $i%5 + 1] -file test.db-journal "
    96         PRAGMA temp_store = memory;
    97         BEGIN;
    98         SELECT random() FROM abc LIMIT $i;
    99         INSERT INTO abc SELECT randstr(10,10), 0, 0 FROM abc WHERE random()%2==0;
   100         DELETE FROM abc WHERE random()%2!=0;
   101         COMMIT;
   102       "
   103    } {1 {child process exited abnormally}}
   104    do_test crash2-2.$i.2 {
   105      sqlite3 db test.db
   106      signature
   107    } $sig
   108  } 
   109  
   110  
   111  # Test case for crashing during database sync with simulated
   112  # sector-size values from 1024 to 8192.
   113  #
   114  for {set i 1} {$i < 10} {incr i} {
   115    set sig [signature]
   116    set sector [expr 1024 * 1<<($i%4)]
   117    db close
   118    do_test crash2-3.$i.1 {
   119       crashsql -blocksize $sector -file test.db "
   120         BEGIN;
   121         SELECT random() FROM abc LIMIT $i;
   122         INSERT INTO abc SELECT randstr(10,10), 0, 0 FROM abc WHERE random()%2==0;
   123         DELETE FROM abc WHERE random()%2!=0;
   124         COMMIT;
   125       "
   126    } {1 {child process exited abnormally}}
   127    do_test crash2-3.$i.2 {
   128      sqlite3 db test.db
   129      signature
   130    } $sig
   131  } 
   132  
   133  finish_test