1# 2004 August 30 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 creates a base 15# data base file, then tests that single byte corruptions in 16# increasingly larger quantities are handled gracefully. 17# 18# $Id: corruptC.test,v 1.13 2009/06/06 19:21:13 drh Exp $ 19 20catch {file delete -force test.db test.db-journal test.bu} 21 22set testdir [file dirname $argv0] 23source $testdir/tester.tcl 24 25# Construct a compact, dense database for testing. 26# 27do_test corruptC-1.1 { 28 execsql { 29 PRAGMA auto_vacuum = 0; 30 PRAGMA legacy_file_format=1; 31 BEGIN; 32 CREATE TABLE t1(x,y); 33 INSERT INTO t1 VALUES(1,1); 34 INSERT OR IGNORE INTO t1 SELECT x*2,y FROM t1; 35 INSERT OR IGNORE INTO t1 SELECT x*3,y FROM t1; 36 INSERT OR IGNORE INTO t1 SELECT x*5,y FROM t1; 37 INSERT OR IGNORE INTO t1 SELECT x*7,y FROM t1; 38 INSERT OR IGNORE INTO t1 SELECT x*11,y FROM t1; 39 INSERT OR IGNORE INTO t1 SELECT x*13,y FROM t1; 40 CREATE INDEX t1i1 ON t1(x); 41 CREATE TABLE t2 AS SELECT x,2 as y FROM t1 WHERE rowid%5!=0; 42 COMMIT; 43 } 44} {} 45 46ifcapable {integrityck} { 47 integrity_check corruptC-1.2 48} 49 50# Generate random integer 51# 52proc random {range} { 53 return [expr {round(rand()*$range)}] 54} 55 56# Copy file $from into $to 57# 58proc copy_file {from to} { 59 file copy -force $from $to 60} 61 62# Setup for the tests. Make a backup copy of the good database in test.bu. 63# 64db close 65copy_file test.db test.bu 66sqlite3 db test.db 67set fsize [file size test.db] 68 69# Set a quasi-random random seed. 70if {[info exists SOAKTEST]} { 71 # If we are doing SOAK tests, we want a different 72 # random seed for each run. Ideally we would like 73 # to use [clock clicks] or something like that here. 74 set qseed [file mtime test.db] 75} else { 76 # If we are not doing soak tests, 77 # make it repeatable. 78 set qseed 0 79} 80expr srand($qseed) 81 82# 83# First test some specific corruption tests found from earlier runs 84# with specific seeds. 85# 86 87# test that a corrupt content offset size is handled (seed 5577) 88do_test corruptC-2.1 { 89 db close 90 copy_file test.bu test.db 91 92 # insert corrupt byte(s) 93 hexio_write test.db 2053 [format %02x 0x04] 94 95 sqlite3 db test.db 96 catchsql {PRAGMA integrity_check} 97} {1 {database disk image is malformed}} 98 99# test that a corrupt content offset size is handled (seed 5649) 100do_test corruptC-2.2 { 101 db close 102 copy_file test.bu test.db 103 104 # insert corrupt byte(s) 105 hexio_write test.db 27 [format %02x 0x08] 106 hexio_write test.db 233 [format %02x 0x6a] 107 hexio_write test.db 328 [format %02x 0x67] 108 hexio_write test.db 750 [format %02x 0x1f] 109 hexio_write test.db 1132 [format %02x 0x52] 110 hexio_write test.db 1133 [format %02x 0x84] 111 hexio_write test.db 1220 [format %02x 0x01] 112 hexio_write test.db 3688 [format %02x 0xc1] 113 hexio_write test.db 3714 [format %02x 0x58] 114 hexio_write test.db 3746 [format %02x 0x9a] 115 116 sqlite3 db test.db 117 catchsql {UPDATE t1 SET y=1} 118} {1 {database disk image is malformed}} 119 120# test that a corrupt free cell size is handled (seed 13329) 121do_test corruptC-2.3 { 122 db close 123 copy_file test.bu test.db 124 125 # insert corrupt byte(s) 126 hexio_write test.db 1094 [format %02x 0x76] 127 128 sqlite3 db test.db 129 catchsql {UPDATE t1 SET y=1} 130} {1 {database disk image is malformed}} 131 132# test that a corrupt free cell size is handled (seed 169571) 133do_test corruptC-2.4 { 134 db close 135 copy_file test.bu test.db 136 137 # insert corrupt byte(s) 138 hexio_write test.db 3119 [format %02x 0xdf] 139 140 sqlite3 db test.db 141 catchsql {UPDATE t2 SET y='abcdef-uvwxyz'} 142} {1 {database disk image is malformed}} 143 144# test that a corrupt free cell size is handled (seed 169571) 145do_test corruptC-2.5 { 146 db close 147 copy_file test.bu test.db 148 149 # insert corrupt byte(s) 150 hexio_write test.db 3119 [format %02x 0xdf] 151 hexio_write test.db 4073 [format %02x 0xbf] 152 153 sqlite3 db test.db 154 catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;} 155 catchsql {PRAGMA integrity_check} 156} {0 {{*** in database main *** 157Corruption detected in cell 710 on page 4 158Multiple uses for byte 661 of page 4 159Fragmented space is 249 byte reported as 21 on page 4}}} 160 161# test that a corrupt free cell size is handled (seed 169595) 162do_test corruptC-2.6 { 163 db close 164 copy_file test.bu test.db 165 166 # insert corrupt byte(s) 167 hexio_write test.db 619 [format %02x 0xe2] 168 hexio_write test.db 3150 [format %02x 0xa8] 169 170 sqlite3 db test.db 171 catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;} 172} {1 {database disk image is malformed}} 173 174# corruption (seed 178692) 175do_test corruptC-2.7 { 176 db close 177 copy_file test.bu test.db 178 179 # insert corrupt byte(s) 180 hexio_write test.db 3074 [format %02x 0xa0] 181 182 sqlite3 db test.db 183 catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;} 184} {1 {database disk image is malformed}} 185 186# corruption (seed 179069) 187do_test corruptC-2.8 { 188 db close 189 copy_file test.bu test.db 190 191 # insert corrupt byte(s) 192 hexio_write test.db 1393 [format %02x 0x7d] 193 hexio_write test.db 84 [format %02x 0x19] 194 hexio_write test.db 3287 [format %02x 0x3b] 195 hexio_write test.db 2564 [format %02x 0xed] 196 hexio_write test.db 2139 [format %02x 0x55] 197 198 sqlite3 db test.db 199 catchsql {BEGIN; DELETE FROM t1 WHERE x>13; ROLLBACK;} 200} {1 {database disk image is malformed}} 201 202# corruption (seed 170434) 203do_test corruptC-2.9 { 204 db close 205 copy_file test.bu test.db 206 207 # insert corrupt byte(s) 208 hexio_write test.db 2095 [format %02x 0xd6] 209 210 sqlite3 db test.db 211 catchsql {BEGIN; DELETE FROM t1 WHERE x>13; ROLLBACK;} 212} {1 {database disk image is malformed}} 213 214# corruption (seed 186504) 215do_test corruptC-2.10 { 216 db close 217 copy_file test.bu test.db 218 219 # insert corrupt byte(s) 220 hexio_write test.db 3130 [format %02x 0x02] 221 222 sqlite3 db test.db 223 catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;} 224} {1 {database disk image is malformed}} 225 226# corruption (seed 1589) 227do_test corruptC-2.11 { 228 db close 229 copy_file test.bu test.db 230 231 # insert corrupt byte(s) 232 hexio_write test.db 55 [format %02x 0xa7] 233 234 sqlite3 db test.db 235 catchsql {BEGIN; CREATE TABLE t3 AS SELECT x,3 as y FROM t2 WHERE rowid%5!=0; ROLLBACK;} 236} {1 {database disk image is malformed}} 237 238# corruption (seed 14166) 239do_test corruptC-2.12 { 240 db close 241 copy_file test.bu test.db 242 243 # insert corrupt byte(s) 244 hexio_write test.db 974 [format %02x 0x2e] 245 246 sqlite3 db test.db 247 catchsql {SELECT count(*) FROM sqlite_master;} 248} {1 {malformed database schema (t1i1) - corrupt database}} 249 250# corruption (seed 218803) 251do_test corruptC-2.13 { 252 db close 253 copy_file test.bu test.db 254 255 # insert corrupt byte(s) 256 hexio_write test.db 102 [format %02x 0x12] 257 258 sqlite3 db test.db 259 catchsql {BEGIN; CREATE TABLE t3 AS SELECT x,3 as y FROM t2 WHERE rowid%5!=0; ROLLBACK;} 260} {1 {database disk image is malformed}} 261 262do_test corruptC-2.14 { 263 db close 264 copy_file test.bu test.db 265 266 sqlite3 db test.db 267 set blob [string repeat abcdefghij 10000] 268 execsql { INSERT INTO t1 VALUES (1, $blob) } 269 270 sqlite3 db test.db 271 set filesize [file size test.db] 272 hexio_write test.db [expr $filesize-2048] 00000001 273 catchsql {DELETE FROM t1 WHERE rowid = (SELECT max(rowid) FROM t1)} 274} {1 {database disk image is malformed}} 275 276# 277# Now test for a series of quasi-random seeds. 278# We loop over the entire file size and touch 279# each byte at least once. 280for {set tn 0} {$tn<$fsize} {incr tn 1} { 281 282 # setup for test 283 db close 284 copy_file test.bu test.db 285 sqlite3 db test.db 286 287 # Seek to a random location in the file, and write a random single byte 288 # value. Then do various operations on the file to make sure that 289 # the database engine can handle the corruption gracefully. 290 # 291 set last 0 292 for {set i 1} {$i<=512 && !$last} {incr i 1} { 293 294 db close 295 if {$i==1} { 296 # on the first corrupt value, use location $tn 297 # this ensures that we touch each location in the 298 # file at least once. 299 set roffset $tn 300 } else { 301 # insert random byte at random location 302 set roffset [random $fsize] 303 } 304 set rbyte [format %02x [random 255]] 305 306 # You can uncomment the following to have it trace 307 # exactly how it's corrupting the file. This is 308 # useful for generating the "seed specific" tests 309 # above. 310 # set rline "$roffset $rbyte" 311 # puts stdout $rline 312 313 hexio_write test.db $roffset $rbyte 314 sqlite3 db test.db 315 316 # do a few random operations to make sure that if 317 # they error, they error gracefully instead of crashing. 318 do_test corruptC-3.$tn.($qseed).$i.1 { 319 catchsql {SELECT count(*) FROM sqlite_master} 320 set x {} 321 } {} 322 do_test corruptC-3.$tn.($qseed).$i.2 { 323 catchsql {SELECT count(*) FROM t1} 324 set x {} 325 } {} 326 do_test corruptC-3.$tn.($qseed).$i.3 { 327 catchsql {SELECT count(*) FROM t1 WHERE x>13} 328 set x {} 329 } {} 330 do_test corruptC-3.$tn.($qseed).$i.4 { 331 catchsql {SELECT count(*) FROM t2} 332 set x {} 333 } {} 334 do_test corruptC-3.$tn.($qseed).$i.5 { 335 catchsql {SELECT count(*) FROM t2 WHERE x<13} 336 set x {} 337 } {} 338 do_test corruptC-3.$tn.($qseed).$i.6 { 339 catchsql {BEGIN; UPDATE t1 SET y=1; ROLLBACK;} 340 set x {} 341 } {} 342 do_test corruptC-3.$tn.($qseed).$i.7 { 343 catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;} 344 set x {} 345 } {} 346 do_test corruptC-3.$tn.($qseed).$i.8 { 347 catchsql {BEGIN; DELETE FROM t1 WHERE x>13; ROLLBACK;} 348 set x {} 349 } {} 350 do_test corruptC-3.$tn.($qseed).$i.9 { 351 catchsql {BEGIN; DELETE FROM t2 WHERE x<13; ROLLBACK;} 352 set x {} 353 } {} 354 do_test corruptC-3.$tn.($qseed).$i.10 { 355 catchsql {BEGIN; CREATE TABLE t3 AS SELECT x,3 as y FROM t2 WHERE rowid%5!=0; ROLLBACK;} 356 set x {} 357 } {} 358 359 # check the integrity of the database. 360 # once the corruption is detected, we can stop. 361 ifcapable {integrityck} { 362 set res [ catchsql {PRAGMA integrity_check} ] 363 set ans [lindex $res 1] 364 if { [ string compare $ans "ok" ] != 0 } { 365 set last -1 366 } 367 } 368 # if we are not capable of doing an integrity check, 369 # stop after corrupting 5 bytes. 370 ifcapable {!integrityck} { 371 if { $i > 5 } { 372 set last -1 373 } 374 } 375 376 # Check that no page references were leaked. 377 # TBD: need to figure out why this doesn't work 378 # work with ROLLBACKs... 379 if {0} { 380 do_test corruptC-3.$tn.($qseed).$i.11 { 381 set bt [btree_from_db db] 382 db_enter db 383 array set stats [btree_pager_stats $bt] 384 db_leave db 385 set stats(ref) 386 } {0} 387 } 388 } 389 # end for i 390 391} 392# end for tn 393 394finish_test 395