xref: /sqlite-3.40.0/test/corruptE.test (revision a3fdec71)
1# 2010 February 18
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 specifcally
15# focuses on rowid order corruption.
16#
17# $Id: corruptE.test,v 1.14 2009/07/11 06:55:34 danielk1977 Exp $
18
19set testdir [file dirname $argv0]
20source $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#
25do_not_use_codec
26
27# These tests deal with corrupt database files
28#
29database_may_be_corrupt
30
31# Do not run the tests in this file if ENABLE_OVERSIZE_CELL_CHECK is on.
32#
33ifcapable oversize_cell_check {
34  finish_test
35  return
36}
37
38# Construct a compact, dense database for testing.
39#
40do_test corruptE-1.1 {
41  execsql {
42    PRAGMA auto_vacuum = 0;
43    PRAGMA legacy_file_format=1;
44    BEGIN;
45    CREATE TABLE t1(x,y);
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;
57    COMMIT;
58  }
59} {}
60
61ifcapable {integrityck} {
62  integrity_check corruptE-1.2
63}
64
65# Setup for the tests.  Make a backup copy of the good database in test.bu.
66#
67db close
68forcecopy test.db test.bu
69sqlite3 db test.db
70set fsize [file size test.db]
71
72
73do_test corruptE-2.1 {
74  db close
75  forcecopy test.bu test.db
76
77  # insert corrupt byte(s)
78  hexio_write test.db 2041 [format %02x 0x2e]
79
80  sqlite3 db test.db
81
82  set res [ catchsql {PRAGMA integrity_check} ]
83  set ans [lindex $res 1]
84
85  list [regexp {out of order.*previous was} $ans] \
86       [regexp {out of order.*max larger than parent max} $ans]
87} {1 1}
88
89do_test corruptE-2.2 {
90  db close
91  forcecopy test.bu test.db
92
93  # insert corrupt byte(s)
94  hexio_write test.db 2047 [format %02x 0x84]
95
96  sqlite3 db test.db
97
98  set res [ catchsql {PRAGMA integrity_check} ]
99  set ans [lindex $res 1]
100
101  list [regexp {out of order.*previous was} $ans] \
102       [regexp {out of order.*min less than parent min} $ans]
103} {1 1}
104
105do_test corruptE-2.3 {
106  db close
107  forcecopy test.bu test.db
108
109  # insert corrupt byte(s)
110  hexio_write test.db 7420 [format %02x 0xa8]
111  hexio_write test.db 10459 [format %02x 0x8d]
112
113  sqlite3 db test.db
114
115  set res [ catchsql {PRAGMA integrity_check} ]
116  set ans [lindex $res 1]
117
118  list [regexp {out of order.*max larger than parent min} $ans]
119} {1}
120
121do_test corruptE-2.4 {
122  db close
123  forcecopy test.bu test.db
124
125  # insert corrupt byte(s)
126  hexio_write test.db 10233 [format %02x 0xd0]
127
128  sqlite3 db test.db
129
130  set res [ catchsql {PRAGMA integrity_check} ]
131  set ans [lindex $res 1]
132
133  list [regexp {out of order.*min less than parent max} $ans]
134} {1}
135
136
137set tests [list {10233 0xd0} \
138                {941 0x42} \
139                {1028 0x53} \
140                {2041 0xd0} \
141                {2042 0x1f} \
142                {2047 0xaa} \
143                {2263 0x29} \
144                {2274 0x75} \
145                {3267 0xf2} \
146                {4104 0x2c} \
147                {5113 0x36} \
148                {10233 0x84} \
149                {10234 0x74} \
150                {10239 0x41} \
151                {10453 0x11} \
152                {11273 0x28} \
153                {11455 0x11} \
154                {11461 0xe6} \
155                {12281 0x99} \
156                {12296 0x9e} \
157                {12297 0xd7} \
158                {13303 0x53} ]
159
160set tc 1
161foreach test $tests {
162  do_test corruptE-3.$tc {
163    db close
164    forcecopy test.bu test.db
165
166    # insert corrupt byte(s)
167    hexio_write test.db [lindex $test 0] [format %02x [lindex $test 1]]
168
169    sqlite3 db test.db
170
171    set res [ catchsql {PRAGMA integrity_check} ]
172    set ans [lindex $res 1]
173
174    list [regexp {out of order} $ans]
175  } {1}
176  incr tc 1
177}
178
179finish_test
180