3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library.
13 # This file implements tests to make sure SQLite does not crash or
14 # segfault if it sees a corrupt database file. It specifcally
15 # focuses on rowid order corruption.
17 # $Id: corruptE.test,v 1.14 2009/07/11 06:55:34 danielk1977 Exp $
19 set testdir [file dirname $argv0]
20 source $testdir/tester.tcl
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).
27 # These tests deal with corrupt database files
29 database_may_be_corrupt
31 # Do not run the tests in this file if ENABLE_OVERSIZE_CELL_CHECK is on.
33 ifcapable oversize_cell_check {
38 # Construct a compact, dense database for testing.
40 do_test corruptE-1.1 {
42 PRAGMA auto_vacuum = 0;
43 PRAGMA legacy_file_format=1;
46 INSERT INTO t1 VALUES(1,1);
47 INSERT OR IGNORE INTO t1 SELECT x*2,y FROM t1;
48 INSERT OR IGNORE INTO t1 SELECT x*3,y FROM t1;
49 INSERT OR IGNORE INTO t1 SELECT x*5,y FROM t1;
50 INSERT OR IGNORE INTO t1 SELECT x*7,y FROM t1;
51 INSERT OR IGNORE INTO t1 SELECT x*11,y FROM t1;
52 INSERT OR IGNORE INTO t1 SELECT x*13,y FROM t1;
53 INSERT OR IGNORE INTO t1 SELECT x*17,y FROM t1;
54 INSERT OR IGNORE INTO t1 SELECT x*19,y FROM t1;
55 CREATE INDEX t1i1 ON t1(x);
56 CREATE TABLE t2 AS SELECT x,2 as y FROM t1 WHERE rowid%5!=0 ORDER BY rowid;
61 ifcapable {integrityck} {
62 integrity_check corruptE-1.2
65 # Setup for the tests. Make a backup copy of the good database in test.bu.
68 forcecopy test.db test.bu
70 set fsize [file size test.db]
73 do_test corruptE-2.1 {
75 forcecopy test.bu test.db
77 # insert corrupt byte(s)
78 hexio_write test.db 2041 [format %02x 0x2e]
82 set res [ catchsql {PRAGMA integrity_check} ]
83 set ans [lindex $res 1]
85 list [regexp {out of order.*previous was} $ans] \
86 [regexp {out of order.*max larger than parent max} $ans]
89 do_test corruptE-2.2 {
91 forcecopy test.bu test.db
93 # insert corrupt byte(s)
94 hexio_write test.db 2047 [format %02x 0x84]
98 set res [ catchsql {PRAGMA integrity_check} ]
99 set ans [lindex $res 1]
101 list [regexp {out of order.*previous was} $ans] \
102 [regexp {out of order.*min less than parent min} $ans]
105 do_test corruptE-2.3 {
107 forcecopy test.bu test.db
109 # insert corrupt byte(s)
110 hexio_write test.db 7420 [format %02x 0xa8]
111 hexio_write test.db 10459 [format %02x 0x8d]
115 set res [ catchsql {PRAGMA integrity_check} ]
116 set ans [lindex $res 1]
118 list [regexp {out of order.*max larger than parent min} $ans]
121 do_test corruptE-2.4 {
123 forcecopy test.bu test.db
125 # insert corrupt byte(s)
126 hexio_write test.db 10233 [format %02x 0xd0]
130 set res [ catchsql {PRAGMA integrity_check} ]
131 set ans [lindex $res 1]
133 list [regexp {out of order.*min less than parent max} $ans]
137 set tests [list {10233 0xd0} \
161 foreach test $tests {
162 do_test corruptE-3.$tc {
164 forcecopy test.bu test.db
166 # insert corrupt byte(s)
167 hexio_write test.db [lindex $test 0] [format %02x [lindex $test 1]]
171 set res [ catchsql {PRAGMA integrity_check} ]
172 set ans [lindex $res 1]
174 list [regexp {out of order} $ans]