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.14 2009/07/11 06:55:34 danielk1977 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 ::G(issoak)]} { 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 *** 157Page 4: btreeInitPage() returns error code 11}}} 158 159# {0 {{*** in database main *** 160# Corruption detected in cell 710 on page 4 161# Multiple uses for byte 661 of page 4 162# Fragmented space is 249 byte reported as 21 on page 4}}} 163 164# test that a corrupt free cell size is handled (seed 169595) 165do_test corruptC-2.6 { 166 db close 167 copy_file test.bu test.db 168 169 # insert corrupt byte(s) 170 hexio_write test.db 619 [format %02x 0xe2] 171 hexio_write test.db 3150 [format %02x 0xa8] 172 173 sqlite3 db test.db 174 catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;} 175} {1 {database disk image is malformed}} 176 177# corruption (seed 178692) 178do_test corruptC-2.7 { 179 db close 180 copy_file test.bu test.db 181 182 # insert corrupt byte(s) 183 hexio_write test.db 3074 [format %02x 0xa0] 184 185 sqlite3 db test.db 186 catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;} 187} {1 {database disk image is malformed}} 188 189# corruption (seed 179069) 190do_test corruptC-2.8 { 191 db close 192 copy_file test.bu test.db 193 194 # insert corrupt byte(s) 195 hexio_write test.db 1393 [format %02x 0x7d] 196 hexio_write test.db 84 [format %02x 0x19] 197 hexio_write test.db 3287 [format %02x 0x3b] 198 hexio_write test.db 2564 [format %02x 0xed] 199 hexio_write test.db 2139 [format %02x 0x55] 200 201 sqlite3 db test.db 202 catchsql {BEGIN; DELETE FROM t1 WHERE x>13; ROLLBACK;} 203} {1 {database disk image is malformed}} 204 205# corruption (seed 170434) 206do_test corruptC-2.9 { 207 db close 208 copy_file test.bu test.db 209 210 # insert corrupt byte(s) 211 hexio_write test.db 2095 [format %02x 0xd6] 212 213 sqlite3 db test.db 214 catchsql {BEGIN; DELETE FROM t1 WHERE x>13; ROLLBACK;} 215} {1 {database disk image is malformed}} 216 217# corruption (seed 186504) 218do_test corruptC-2.10 { 219 db close 220 copy_file test.bu test.db 221 222 # insert corrupt byte(s) 223 hexio_write test.db 3130 [format %02x 0x02] 224 225 sqlite3 db test.db 226 catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;} 227} {1 {database disk image is malformed}} 228 229# corruption (seed 1589) 230do_test corruptC-2.11 { 231 db close 232 copy_file test.bu test.db 233 234 # insert corrupt byte(s) 235 hexio_write test.db 55 [format %02x 0xa7] 236 237 sqlite3 db test.db 238 catchsql {BEGIN; CREATE TABLE t3 AS SELECT x,3 as y FROM t2 WHERE rowid%5!=0; ROLLBACK;} 239} {1 {database disk image is malformed}} 240 241# corruption (seed 14166) 242do_test corruptC-2.12 { 243 db close 244 copy_file test.bu test.db 245 246 # insert corrupt byte(s) 247 hexio_write test.db 974 [format %02x 0x2e] 248 249 sqlite3 db test.db 250 catchsql {SELECT count(*) FROM sqlite_master;} 251} {1 {malformed database schema (t1i1) - corrupt database}} 252 253# corruption (seed 218803) 254do_test corruptC-2.13 { 255 db close 256 copy_file test.bu test.db 257 258 # insert corrupt byte(s) 259 hexio_write test.db 102 [format %02x 0x12] 260 261 sqlite3 db test.db 262 catchsql {BEGIN; CREATE TABLE t3 AS SELECT x,3 as y FROM t2 WHERE rowid%5!=0; ROLLBACK;} 263} {1 {database disk image is malformed}} 264 265do_test corruptC-2.14 { 266 db close 267 copy_file test.bu test.db 268 269 sqlite3 db test.db 270 set blob [string repeat abcdefghij 10000] 271 execsql { INSERT INTO t1 VALUES (1, $blob) } 272 273 sqlite3 db test.db 274 set filesize [file size test.db] 275 hexio_write test.db [expr $filesize-2048] 00000001 276 catchsql {DELETE FROM t1 WHERE rowid = (SELECT max(rowid) FROM t1)} 277} {1 {database disk image is malformed}} 278 279# At one point this particular corrupt database was causing a buffer 280# overread. Which caused a crash in a run of all.test once. 281# 282do_test corruptC-2.15 { 283 db close 284 copy_file test.bu test.db 285 hexio_write test.db 986 b9 286 sqlite3 db test.db 287 catchsql {SELECT count(*) FROM sqlite_master;} 288} {1 {malformed database schema (t1i1) - no such table: main.t1}} 289 290# 291# Now test for a series of quasi-random seeds. 292# We loop over the entire file size and touch 293# each byte at least once. 294for {set tn 0} {$tn<$fsize} {incr tn 1} { 295 296 # setup for test 297 db close 298 copy_file test.bu test.db 299 sqlite3 db test.db 300 301 # Seek to a random location in the file, and write a random single byte 302 # value. Then do various operations on the file to make sure that 303 # the database engine can handle the corruption gracefully. 304 # 305 set last 0 306 for {set i 1} {$i<=512 && !$last} {incr i 1} { 307 308 db close 309 if {$i==1} { 310 # on the first corrupt value, use location $tn 311 # this ensures that we touch each location in the 312 # file at least once. 313 set roffset $tn 314 } else { 315 # insert random byte at random location 316 set roffset [random $fsize] 317 } 318 set rbyte [format %02x [random 255]] 319 320 # You can uncomment the following to have it trace 321 # exactly how it's corrupting the file. This is 322 # useful for generating the "seed specific" tests 323 # above. 324 # set rline "$roffset $rbyte" 325 # puts stdout $rline 326 327 hexio_write test.db $roffset $rbyte 328 sqlite3 db test.db 329 330 # do a few random operations to make sure that if 331 # they error, they error gracefully instead of crashing. 332 do_test corruptC-3.$tn.($qseed).$i.1 { 333 catchsql {SELECT count(*) FROM sqlite_master} 334 set x {} 335 } {} 336 do_test corruptC-3.$tn.($qseed).$i.2 { 337 catchsql {SELECT count(*) FROM t1} 338 set x {} 339 } {} 340 do_test corruptC-3.$tn.($qseed).$i.3 { 341 catchsql {SELECT count(*) FROM t1 WHERE x>13} 342 set x {} 343 } {} 344 do_test corruptC-3.$tn.($qseed).$i.4 { 345 catchsql {SELECT count(*) FROM t2} 346 set x {} 347 } {} 348 do_test corruptC-3.$tn.($qseed).$i.5 { 349 catchsql {SELECT count(*) FROM t2 WHERE x<13} 350 set x {} 351 } {} 352 do_test corruptC-3.$tn.($qseed).$i.6 { 353 catchsql {BEGIN; UPDATE t1 SET y=1; ROLLBACK;} 354 set x {} 355 } {} 356 do_test corruptC-3.$tn.($qseed).$i.7 { 357 catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;} 358 set x {} 359 } {} 360 do_test corruptC-3.$tn.($qseed).$i.8 { 361 catchsql {BEGIN; DELETE FROM t1 WHERE x>13; ROLLBACK;} 362 set x {} 363 } {} 364 do_test corruptC-3.$tn.($qseed).$i.9 { 365 catchsql {BEGIN; DELETE FROM t2 WHERE x<13; ROLLBACK;} 366 set x {} 367 } {} 368 do_test corruptC-3.$tn.($qseed).$i.10 { 369 catchsql {BEGIN; CREATE TABLE t3 AS SELECT x,3 as y FROM t2 WHERE rowid%5!=0; ROLLBACK;} 370 set x {} 371 } {} 372 373 # check the integrity of the database. 374 # once the corruption is detected, we can stop. 375 ifcapable {integrityck} { 376 set res [ catchsql {PRAGMA integrity_check} ] 377 set ans [lindex $res 1] 378 if { [ string compare $ans "ok" ] != 0 } { 379 set last -1 380 } 381 } 382 # if we are not capable of doing an integrity check, 383 # stop after corrupting 5 bytes. 384 ifcapable {!integrityck} { 385 if { $i > 5 } { 386 set last -1 387 } 388 } 389 390 # Check that no page references were leaked. 391 # TBD: need to figure out why this doesn't work 392 # work with ROLLBACKs... 393 if {0} { 394 do_test corruptC-3.$tn.($qseed).$i.11 { 395 set bt [btree_from_db db] 396 db_enter db 397 array set stats [btree_pager_stats $bt] 398 db_leave db 399 set stats(ref) 400 } {0} 401 } 402 } 403 # end for i 404 405} 406# end for tn 407 408finish_test 409