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.
16 # $Id: corrupt.test,v 1.12 2009/07/13 09:41:45 danielk1977 Exp $
18 catch {file delete -force test.db test.db-journal test.bu}
20 set testdir [file dirname $argv0]
21 source $testdir/tester.tcl
23 # Do not use a codec for tests in this file, as the database file is
24 # manipulated directly using tcl scripts (using the [hexio_write] command).
28 # Construct a large database for testing.
34 INSERT INTO t1 VALUES(randstr(100,100));
35 INSERT INTO t1 VALUES(randstr(90,90));
36 INSERT INTO t1 VALUES(randstr(80,80));
37 INSERT INTO t1 SELECT x || randstr(5,5) FROM t1;
38 INSERT INTO t1 SELECT x || randstr(6,6) FROM t1;
39 INSERT INTO t1 SELECT x || randstr(7,7) FROM t1;
40 INSERT INTO t1 SELECT x || randstr(8,8) FROM t1;
41 INSERT INTO t1 VALUES(randstr(3000,3000));
42 INSERT INTO t1 SELECT x || randstr(9,9) FROM t1;
43 INSERT INTO t1 SELECT x || randstr(10,10) FROM t1;
44 INSERT INTO t1 SELECT x || randstr(11,11) FROM t1;
45 INSERT INTO t1 SELECT x || randstr(12,12) FROM t1;
46 CREATE INDEX t1i1 ON t1(x);
47 CREATE TABLE t2 AS SELECT * FROM t1;
48 DELETE FROM t2 WHERE rowid%5!=0;
52 integrity_check corrupt-1.2
54 # Copy file $from into $to
56 proc copy_file {from to} {
58 fconfigure $f -translation binary
60 fconfigure $t -translation binary
61 puts -nonewline $t [read $f [file size $from]]
66 # Setup for the tests. Make a backup copy of the good database in test.bu.
67 # Create a string of garbage data that is 256 bytes long.
69 copy_file test.db test.bu
70 set fsize [file size test.db]
71 set junk "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
72 while {[string length $junk]<256} {append junk $junk}
73 set junk [string range $junk 0 255]
75 # Go through the database and write garbage data into each 256 segment
76 # of the file. Then do various operations on the file to make sure that
77 # the database engine can recover gracefully from the corruption.
79 for {set i [expr {1*256}]} {$i<$fsize-256} {incr i 256} {
80 set tn [expr {$i/256}]
82 copy_file test.bu test.db
83 set fd [open test.db r+]
84 fconfigure $fd -translation binary
86 puts -nonewline $fd $junk
88 do_test corrupt-2.$tn.1 {
90 catchsql {SELECT count(*) FROM sqlite_master}
93 do_test corrupt-2.$tn.2 {
94 catchsql {SELECT count(*) FROM t1}
97 do_test corrupt-2.$tn.3 {
98 catchsql {SELECT count(*) FROM t1 WHERE x>'abcdef'}
101 do_test corrupt-2.$tn.4 {
102 catchsql {SELECT count(*) FROM t2}
105 do_test corrupt-2.$tn.5 {
106 catchsql {CREATE TABLE t3 AS SELECT * FROM t1}
109 do_test corrupt-2.$tn.6 {
110 catchsql {DROP TABLE t1}
113 do_test corrupt-2.$tn.7 {
114 catchsql {PRAGMA integrity_check}
118 # Check that no page references were leaked.
119 do_test corrupt-2.$tn.8 {
120 set bt [btree_from_db db]
122 array set stats [btree_pager_stats $bt]
128 #------------------------------------------------------------------------
129 # For these tests, swap the rootpage entries of t1 (a table) and t1i1 (an
130 # index on t1) in sqlite_master. Then perform a few different queries
131 # and make sure this is detected as corruption.
133 do_test corrupt-3.1 {
135 copy_file test.bu test.db
139 do_test corrupt-3.2 {
140 set t1_r [execsql {SELECT rootpage FROM sqlite_master WHERE name = 't1i1'}]
141 set t1i1_r [execsql {SELECT rootpage FROM sqlite_master WHERE name = 't1'}]
142 set cookie [expr [execsql {PRAGMA schema_version}] + 1]
144 PRAGMA writable_schema = 1;
145 UPDATE sqlite_master SET rootpage = $t1_r WHERE name = 't1';
146 UPDATE sqlite_master SET rootpage = $t1i1_r WHERE name = 't1i1';
147 PRAGMA writable_schema = 0;
148 PRAGMA schema_version = $cookie;
152 # This one tests the case caught by code in checkin [2313].
153 do_test corrupt-3.3 {
157 INSERT INTO t1 VALUES('abc');
159 } {1 {database disk image is malformed}}
160 do_test corrupt-3.4 {
166 } {1 {database disk image is malformed}}
167 do_test corrupt-3.5 {
171 SELECT * FROM t1 WHERE oid = 10;
173 } {1 {database disk image is malformed}}
174 do_test corrupt-3.6 {
178 SELECT * FROM t1 WHERE x = 'abcde';
180 } {1 {database disk image is malformed}}
182 do_test corrupt-4.1 {
184 file delete -force test.db test.db-journal
187 PRAGMA page_size = 1024;
188 CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT);
190 for {set i 0} {$i < 10} {incr i} {
191 set text [string repeat $i 220]
192 execsql { INSERT INTO t1 VALUES($i, $text) }
194 execsql { CREATE INDEX i1 ON t1(b) }
196 do_test corrupt-4.2 {
197 set iRoot [db one {SELECT rootpage FROM sqlite_master WHERE name = 'i1'}]
198 set iOffset [hexio_get_int [hexio_read test.db [expr 12+($iRoot-1)*1024] 2]]
199 set data [hexio_render_int32 [expr $iRoot - 1]]
200 hexio_write test.db [expr ($iRoot-1)*1024 + $iOffset] $data
204 # The following DELETE statement attempts to delete a cell stored on the
205 # root page of index i1. After this cell is deleted it must be replaced
206 # by a cell retrieved from the child page (a leaf) of the deleted cell.
207 # This will fail, as the block modified the database image so that the
208 # child page of the deleted cell is from a table (intkey) b-tree, not an
209 # index b-tree as expected. At one point this was causing an assert()
211 catchsql { DELETE FROM t1 WHERE rowid = 3 }
212 } {1 {database disk image is malformed}}
214 do_test corrupt-5.1 {
216 file delete -force test.db test.db-journal
219 execsql { PRAGMA page_size = 1024 }
220 set ct "CREATE TABLE t1(c0 "
222 while {[string length $ct] < 950} { append ct ", c[incr i]" }
227 do_test corrupt-5.2 {
229 hexio_write test.db 108 00000000
231 catchsql { SELECT * FROM sqlite_master }
232 } {1 {database disk image is malformed}}
234 # At one point, the specific corruption caused by this test case was
235 # causing a buffer overwrite. Although a crash was never demonstrated,
236 # running this testcase under valgrind revealed the problem.
237 do_test corrupt-6.1 {
239 file delete -force test.db test.db-journal
242 PRAGMA page_size = 1024; CREATE TABLE t1(x);
245 # The root page of t1 is 1024 bytes in size. The header is 8 bytes, and
246 # each of the cells inserted by the following INSERT statements consume
247 # 16 bytes (including the 2 byte cell-offset array entry). So the page
248 # can contain up to 63 cells.
249 for {set i 0} {$i < 63} {incr i} {
250 execsql { INSERT INTO t1 VALUES( randomblob(10) ) }
253 # Free the cell stored right at the end of the page (at offset pgsz-14).
254 execsql { DELETE FROM t1 WHERE rowid=1 }
255 set rootpage [db one {SELECT rootpage FROM sqlite_master WHERE name = 't1'}]
258 set offset [expr ($rootpage * 1024)-14+2]
259 hexio_write test.db $offset 00FF
262 catchsql { INSERT INTO t1 VALUES( randomblob(10) ) }
263 } {1 {database disk image is malformed}}
265 ifcapable oversize_cell_check {
267 file delete -force test.db test.db-journal
270 PRAGMA page_size = 1024; CREATE TABLE t1(x);
273 do_test corrupt-7.1 {
274 for {set i 0} {$i < 39} {incr i} {
276 INSERT INTO t1 VALUES(X'000100020003000400050006000700080009000A');
282 # Corrupt the root page of table t1 so that the first offset in the
283 # cell-offset array points to the data for the SQL blob associated with
284 # record (rowid=10). The root page still passes the checks in btreeInitPage(),
285 # because the start of said blob looks like the start of a legitimate
288 # Test case cc-2 overwrites the blob so that it no longer looks like a
289 # real cell. But, by the time it is overwritten, btreeInitPage() has already
290 # initialized the root page, so no corruption is detected.
292 # Test case cc-3 inserts an extra record into t1, forcing balance-deeper
293 # to run. After copying the contents of the root page to the new child,
294 # btreeInitPage() is called on the child. This time, it detects corruption
295 # (because the start of the blob associated with the (rowid=10) record
296 # no longer looks like a real cell). At one point the code assumed that
297 # detecting corruption was not possible at that point, and an assert() failed.
299 set fd [open test.db r+]
300 fconfigure $fd -translation binary -encoding binary
301 seek $fd [expr 1024+8]
302 puts -nonewline $fd "\x03\x14"
306 do_test corrupt-7.2 {
308 UPDATE t1 SET x = X'870400020003000400050006000700080009000A'
312 do_test corrupt-7.3 {
314 INSERT INTO t1 VALUES(X'000100020003000400050006000700080009000A');
316 } {1 {database disk image is malformed}}
320 file delete -force test.db test.db-journal
321 do_test corrupt-8.1 {
324 PRAGMA page_size = 1024;
325 PRAGMA secure_delete = on;
326 PRAGMA auto_vacuum = 0;
327 CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
328 INSERT INTO t1 VALUES(5, randomblob(1900));
331 hexio_write test.db 2044 [hexio_render_int32 2]
332 hexio_write test.db 24 [hexio_render_int32 45]
334 catchsql { INSERT OR REPLACE INTO t1 VALUES(5, randomblob(1900)) }
335 } {1 {database disk image is malformed}}
338 file delete -force test.db test.db-journal
339 do_test corrupt-8.2 {
342 PRAGMA page_size = 1024;
343 PRAGMA secure_delete = on;
344 PRAGMA auto_vacuum = 0;
345 CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
346 INSERT INTO t1 VALUES(5, randomblob(900));
347 INSERT INTO t1 VALUES(6, randomblob(900));
350 hexio_write test.db 2047 FF
351 hexio_write test.db 24 [hexio_render_int32 45]
353 catchsql { INSERT INTO t1 VALUES(4, randomblob(1900)) }
354 } {1 {database disk image is malformed}}