gitlab.com/CoiaPrant/sqlite3@v1.19.1/testdata/tcl/corrupt8.test (about)

     1  # 2008 July 9
     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  # This file implements tests to make sure SQLite does not crash or
    14  # segfault if it sees a corrupt database file.  It specifically focuses
    15  # on corrupt pointer map pages.
    16  #
    17  # $Id: corrupt8.test,v 1.2 2008/07/11 03:34:10 drh Exp $
    18  
    19  set testdir [file dirname $argv0]
    20  source $testdir/tester.tcl
    21  
    22  # Do not use a codec for tests in this file, as the database file is
    23  # manipulated directly using tcl scripts (using the [hexio_write] command).
    24  #
    25  do_not_use_codec
    26  
    27  # These tests deal with corrupt database files
    28  #
    29  database_may_be_corrupt
    30  
    31  # We must have the page_size pragma for these tests to work.
    32  #
    33  ifcapable !pager_pragmas||!autovacuum {
    34    finish_test
    35    return
    36  }
    37  
    38  # Create a database to work with.
    39  #
    40  do_test corrupt8-1.1 {
    41    execsql {
    42      PRAGMA auto_vacuum=1;
    43      PRAGMA page_size=1024;
    44      CREATE TABLE t1(x);
    45      INSERT INTO t1(x) VALUES(1);
    46      INSERT INTO t1(x) VALUES(2);
    47      INSERT INTO t1(x) SELECT x+2 FROM t1;
    48      INSERT INTO t1(x) SELECT x+4 FROM t1;
    49      INSERT INTO t1(x) SELECT x+8 FROM t1;
    50      INSERT INTO t1(x) SELECT x+16 FROM t1;
    51      INSERT INTO t1(x) SELECT x+32 FROM t1;
    52      INSERT INTO t1(x) SELECT x+64 FROM t1;
    53      INSERT INTO t1(x) SELECT x+128 FROM t1;
    54      INSERT INTO t1(x) SELECT x+256 FROM t1;
    55      CREATE TABLE t2(a,b);
    56      INSERT INTO t2 SELECT x, x*x FROM t1;
    57    }
    58    expr {[file size test.db]>1024*12}
    59  } {1}
    60  integrity_check corrupt8-1.2
    61  
    62  # Loop through each ptrmap entry.  Corrupt the entry and make sure the
    63  # corruption is detected by the integrity_check.
    64  #
    65  for {set i 1024} {$i<2048} {incr i 5} {
    66    set oldval [hexio_read test.db $i 1]
    67    if {$oldval==0} break
    68    hexio_write test.db $i 00
    69    do_test corrupt8-2.$i.0 {
    70      db close
    71      sqlite3 db test.db
    72      set x [db eval {PRAGMA integrity_check}]
    73      expr {$x!="ok"}
    74    } {1}
    75    for {set k 1} {$k<=5} {incr k} {
    76      if {$k==$oldval} continue
    77      hexio_write test.db $i 0$k
    78      do_test corrupt8-2.$i.$k {
    79        db close
    80        sqlite3 db test.db
    81        set x [db eval {PRAGMA integrity_check}]
    82        expr {$x!="ok"}
    83      } {1}
    84    }
    85    hexio_write test.db $i 06
    86    do_test corrupt8-2.$i.6 {
    87      db close
    88      sqlite3 db test.db
    89      set x [db eval {PRAGMA integrity_check}]
    90      expr {$x!="ok"}
    91    } {1}
    92    hexio_write test.db $i $oldval
    93    if {$oldval>2} {
    94      set i2 [expr {$i+1+$i%4}]
    95      set oldval [hexio_read test.db $i2 1]
    96      hexio_write test.db $i2 [format %02x [expr {($oldval+1)&0xff}]]
    97      do_test corrupt8-2.$i.7 {
    98        db close
    99        sqlite3 db test.db
   100        set x [db eval {PRAGMA integrity_check}]
   101        expr {$x!="ok"}
   102      } {1}
   103      hexio_write test.db $i2 $oldval
   104    }
   105  }
   106  
   107  
   108  finish_test