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 creates a base
15 # data base file, then tests that single byte corruptions in
16 # increasingly larger quantities are handled gracefully.
18 # $Id: corruptC.test,v 1.14 2009/07/11 06:55:34 danielk1977 Exp $
20 catch {file delete -force test.db test.db-journal test.bu}
22 set testdir [file dirname $argv0]
23 source $testdir/tester.tcl
25 # Do not use a codec for tests in this file, as the database file is
26 # manipulated directly using tcl scripts (using the [hexio_write] command).
30 # Construct a compact, dense database for testing.
32 do_test corruptC-1.1 {
34 PRAGMA auto_vacuum = 0;
35 PRAGMA legacy_file_format=1;
38 INSERT INTO t1 VALUES(1,1);
39 INSERT OR IGNORE INTO t1 SELECT x*2,y FROM t1;
40 INSERT OR IGNORE INTO t1 SELECT x*3,y FROM t1;
41 INSERT OR IGNORE INTO t1 SELECT x*5,y FROM t1;
42 INSERT OR IGNORE INTO t1 SELECT x*7,y FROM t1;
43 INSERT OR IGNORE INTO t1 SELECT x*11,y FROM t1;
44 INSERT OR IGNORE INTO t1 SELECT x*13,y FROM t1;
45 CREATE INDEX t1i1 ON t1(x);
46 CREATE TABLE t2 AS SELECT x,2 as y FROM t1 WHERE rowid%5!=0;
51 ifcapable {integrityck} {
52 integrity_check corruptC-1.2
55 # Generate random integer
58 return [expr {round(rand()*$range)}]
61 # Copy file $from into $to
63 proc copy_file {from to} {
64 file copy -force $from $to
67 # Setup for the tests. Make a backup copy of the good database in test.bu.
70 copy_file test.db test.bu
72 set fsize [file size test.db]
74 # Set a quasi-random random seed.
75 if {[info exists ::G(issoak)]} {
76 # If we are doing SOAK tests, we want a different
77 # random seed for each run. Ideally we would like
78 # to use [clock clicks] or something like that here.
79 set qseed [file mtime test.db]
81 # If we are not doing soak tests,
88 # First test some specific corruption tests found from earlier runs
89 # with specific seeds.
92 # test that a corrupt content offset size is handled (seed 5577)
93 do_test corruptC-2.1 {
95 copy_file test.bu test.db
97 # insert corrupt byte(s)
98 hexio_write test.db 2053 [format %02x 0x04]
101 catchsql {PRAGMA integrity_check}
102 } {1 {database disk image is malformed}}
104 # test that a corrupt content offset size is handled (seed 5649)
105 do_test corruptC-2.2 {
107 copy_file test.bu test.db
109 # insert corrupt byte(s)
110 hexio_write test.db 27 [format %02x 0x08]
111 hexio_write test.db 233 [format %02x 0x6a]
112 hexio_write test.db 328 [format %02x 0x67]
113 hexio_write test.db 750 [format %02x 0x1f]
114 hexio_write test.db 1132 [format %02x 0x52]
115 hexio_write test.db 1133 [format %02x 0x84]
116 hexio_write test.db 1220 [format %02x 0x01]
117 hexio_write test.db 3688 [format %02x 0xc1]
118 hexio_write test.db 3714 [format %02x 0x58]
119 hexio_write test.db 3746 [format %02x 0x9a]
122 catchsql {UPDATE t1 SET y=1}
123 } {1 {database disk image is malformed}}
125 # test that a corrupt free cell size is handled (seed 13329)
126 do_test corruptC-2.3 {
128 copy_file test.bu test.db
130 # insert corrupt byte(s)
131 hexio_write test.db 1094 [format %02x 0x76]
134 catchsql {UPDATE t1 SET y=1}
135 } {1 {database disk image is malformed}}
137 # test that a corrupt free cell size is handled (seed 169571)
138 do_test corruptC-2.4 {
140 copy_file test.bu test.db
142 # insert corrupt byte(s)
143 hexio_write test.db 3119 [format %02x 0xdf]
146 catchsql {UPDATE t2 SET y='abcdef-uvwxyz'}
147 } {1 {database disk image is malformed}}
149 # test that a corrupt free cell size is handled (seed 169571)
150 do_test corruptC-2.5 {
152 copy_file test.bu test.db
154 # insert corrupt byte(s)
155 hexio_write test.db 3119 [format %02x 0xdf]
156 hexio_write test.db 4073 [format %02x 0xbf]
159 catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;}
160 catchsql {PRAGMA integrity_check}
161 } {0 {{*** in database main ***
162 Page 4: btreeInitPage() returns error code 11}}}
164 # {0 {{*** in database main ***
165 # Corruption detected in cell 710 on page 4
166 # Multiple uses for byte 661 of page 4
167 # Fragmented space is 249 byte reported as 21 on page 4}}}
169 # test that a corrupt free cell size is handled (seed 169595)
170 do_test corruptC-2.6 {
172 copy_file test.bu test.db
174 # insert corrupt byte(s)
175 hexio_write test.db 619 [format %02x 0xe2]
176 hexio_write test.db 3150 [format %02x 0xa8]
179 catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;}
180 } {1 {database disk image is malformed}}
182 # corruption (seed 178692)
183 do_test corruptC-2.7 {
185 copy_file test.bu test.db
187 # insert corrupt byte(s)
188 hexio_write test.db 3074 [format %02x 0xa0]
191 catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;}
192 } {1 {database disk image is malformed}}
194 # corruption (seed 179069)
195 do_test corruptC-2.8 {
197 copy_file test.bu test.db
199 # insert corrupt byte(s)
200 hexio_write test.db 1393 [format %02x 0x7d]
201 hexio_write test.db 84 [format %02x 0x19]
202 hexio_write test.db 3287 [format %02x 0x3b]
203 hexio_write test.db 2564 [format %02x 0xed]
204 hexio_write test.db 2139 [format %02x 0x55]
207 catchsql {BEGIN; DELETE FROM t1 WHERE x>13; ROLLBACK;}
208 } {1 {database disk image is malformed}}
210 # corruption (seed 170434)
211 do_test corruptC-2.9 {
213 copy_file test.bu test.db
215 # insert corrupt byte(s)
216 hexio_write test.db 2095 [format %02x 0xd6]
219 catchsql {BEGIN; DELETE FROM t1 WHERE x>13; ROLLBACK;}
220 } {1 {database disk image is malformed}}
222 # corruption (seed 186504)
223 do_test corruptC-2.10 {
225 copy_file test.bu test.db
227 # insert corrupt byte(s)
228 hexio_write test.db 3130 [format %02x 0x02]
231 catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;}
232 } {1 {database disk image is malformed}}
234 # corruption (seed 1589)
235 do_test corruptC-2.11 {
237 copy_file test.bu test.db
239 # insert corrupt byte(s)
240 hexio_write test.db 55 [format %02x 0xa7]
243 catchsql {BEGIN; CREATE TABLE t3 AS SELECT x,3 as y FROM t2 WHERE rowid%5!=0; ROLLBACK;}
244 } {1 {database disk image is malformed}}
246 # corruption (seed 14166)
247 do_test corruptC-2.12 {
249 copy_file test.bu test.db
251 # insert corrupt byte(s)
252 hexio_write test.db 974 [format %02x 0x2e]
255 catchsql {SELECT count(*) FROM sqlite_master;}
256 } {1 {malformed database schema (t1i1) - corrupt database}}
258 # corruption (seed 218803)
259 do_test corruptC-2.13 {
261 copy_file test.bu test.db
263 # insert corrupt byte(s)
264 hexio_write test.db 102 [format %02x 0x12]
267 catchsql {BEGIN; CREATE TABLE t3 AS SELECT x,3 as y FROM t2 WHERE rowid%5!=0; ROLLBACK;}
268 } {1 {database disk image is malformed}}
270 do_test corruptC-2.14 {
272 copy_file test.bu test.db
275 set blob [string repeat abcdefghij 10000]
276 execsql { INSERT INTO t1 VALUES (1, $blob) }
279 set filesize [file size test.db]
280 hexio_write test.db [expr $filesize-2048] 00000001
281 catchsql {DELETE FROM t1 WHERE rowid = (SELECT max(rowid) FROM t1)}
282 } {1 {database disk image is malformed}}
284 # At one point this particular corrupt database was causing a buffer
285 # overread. Which caused a crash in a run of all.test once.
287 do_test corruptC-2.15 {
289 copy_file test.bu test.db
290 hexio_write test.db 986 b9
292 catchsql {SELECT count(*) FROM sqlite_master;}
293 } {1 {malformed database schema (t1i1) - no such table: main.t1}}
296 # Now test for a series of quasi-random seeds.
297 # We loop over the entire file size and touch
298 # each byte at least once.
299 for {set tn 0} {$tn<$fsize} {incr tn 1} {
303 copy_file test.bu test.db
306 # Seek to a random location in the file, and write a random single byte
307 # value. Then do various operations on the file to make sure that
308 # the database engine can handle the corruption gracefully.
311 for {set i 1} {$i<=512 && !$last} {incr i 1} {
315 # on the first corrupt value, use location $tn
316 # this ensures that we touch each location in the
317 # file at least once.
320 # insert random byte at random location
321 set roffset [random $fsize]
323 set rbyte [format %02x [random 255]]
325 # You can uncomment the following to have it trace
326 # exactly how it's corrupting the file. This is
327 # useful for generating the "seed specific" tests
329 # set rline "$roffset $rbyte"
332 hexio_write test.db $roffset $rbyte
335 # do a few random operations to make sure that if
336 # they error, they error gracefully instead of crashing.
337 do_test corruptC-3.$tn.($qseed).$i.1 {
338 catchsql {SELECT count(*) FROM sqlite_master}
341 do_test corruptC-3.$tn.($qseed).$i.2 {
342 catchsql {SELECT count(*) FROM t1}
345 do_test corruptC-3.$tn.($qseed).$i.3 {
346 catchsql {SELECT count(*) FROM t1 WHERE x>13}
349 do_test corruptC-3.$tn.($qseed).$i.4 {
350 catchsql {SELECT count(*) FROM t2}
353 do_test corruptC-3.$tn.($qseed).$i.5 {
354 catchsql {SELECT count(*) FROM t2 WHERE x<13}
357 do_test corruptC-3.$tn.($qseed).$i.6 {
358 catchsql {BEGIN; UPDATE t1 SET y=1; ROLLBACK;}
361 do_test corruptC-3.$tn.($qseed).$i.7 {
362 catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;}
365 do_test corruptC-3.$tn.($qseed).$i.8 {
366 catchsql {BEGIN; DELETE FROM t1 WHERE x>13; ROLLBACK;}
369 do_test corruptC-3.$tn.($qseed).$i.9 {
370 catchsql {BEGIN; DELETE FROM t2 WHERE x<13; ROLLBACK;}
373 do_test corruptC-3.$tn.($qseed).$i.10 {
374 catchsql {BEGIN; CREATE TABLE t3 AS SELECT x,3 as y FROM t2 WHERE rowid%5!=0; ROLLBACK;}
378 # check the integrity of the database.
379 # once the corruption is detected, we can stop.
380 ifcapable {integrityck} {
381 set res [ catchsql {PRAGMA integrity_check} ]
382 set ans [lindex $res 1]
383 if { [ string compare $ans "ok" ] != 0 } {
387 # if we are not capable of doing an integrity check,
388 # stop after corrupting 5 bytes.
389 ifcapable {!integrityck} {
395 # Check that no page references were leaked.
396 # TBD: need to figure out why this doesn't work
397 # work with ROLLBACKs...
399 do_test corruptC-3.$tn.($qseed).$i.11 {
400 set bt [btree_from_db db]
402 array set stats [btree_pager_stats $bt]