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.9 2008/11/13 18:29:51 shane 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 BEGIN; 31 CREATE TABLE t1(x,y); 32 INSERT INTO t1 VALUES(1,1); 33 INSERT OR IGNORE INTO t1 SELECT x*2,y FROM t1; 34 INSERT OR IGNORE INTO t1 SELECT x*3,y FROM t1; 35 INSERT OR IGNORE INTO t1 SELECT x*5,y FROM t1; 36 INSERT OR IGNORE INTO t1 SELECT x*7,y FROM t1; 37 INSERT OR IGNORE INTO t1 SELECT x*11,y FROM t1; 38 INSERT OR IGNORE INTO t1 SELECT x*13,y FROM t1; 39 CREATE INDEX t1i1 ON t1(x); 40 CREATE TABLE t2 AS SELECT x,2 as y FROM t1 WHERE rowid%5!=0; 41 COMMIT; 42 } 43} {} 44 45ifcapable {integrityck} { 46 integrity_check corruptC-1.2 47} 48 49# Generate random integer 50# 51proc random {range} { 52 return [expr {round(rand()*$range)}] 53} 54 55# Copy file $from into $to 56# 57proc copy_file {from to} { 58 file copy -force $from $to 59} 60 61# Setup for the tests. Make a backup copy of the good database in test.bu. 62# 63db close 64copy_file test.db test.bu 65sqlite3 db test.db 66set fsize [file size test.db] 67 68# Set a quasi-random random seed. 69if {[info exists SOAKTEST]} { 70 # If we are doing SOAK tests, we want a different 71 # random seed for each run. Ideally we would like 72 # to use [clock clicks] or something like that here. 73 set qseed [file mtime test.db] 74} else { 75 # If we are not doing soak tests, 76 # make it repeatable. 77 set qseed 0 78} 79expr srand($qseed) 80 81# 82# First test some specific corruption tests found from earlier runs 83# with specific seeds. 84# 85 86# test that a corrupt content offset size is handled (seed 5577) 87do_test corruptC-2.1 { 88 db close 89 copy_file test.bu test.db 90 91 # insert corrupt byte(s) 92 hexio_write test.db 2053 [format %02x 0x04] 93 94 sqlite3 db test.db 95 catchsql {PRAGMA integrity_check} 96} {0 {{*** in database main *** 97Corruption detected in header on page 3}}} 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 262 263# 264# now test for a series of quasi-random seeds 265for {set tn 0} {$tn<1024} {incr tn 1} { 266 267 # setup for test 268 db close 269 copy_file test.bu test.db 270 sqlite3 db test.db 271 272 # Seek to a random location in the file, and write a random single byte 273 # value. Then do various operations on the file to make sure that 274 # the database engine can handle the corruption gracefully. 275 # 276 set last 0 277 for {set i 1} {$i<=512 && !$last} {incr i 1} { 278 279 # insert random byte at random location 280 db close 281 set roffset [random $fsize] 282 set rbyte [format %02x [random 255]] 283 284 # You can uncomment the following to have it trace 285 # exactly how it's corrupting the file. This is 286 # useful for generating the "seed specific" tests 287 # above. 288 # set rline "$roffset $rbyte" 289 # puts stdout $rline 290 291 hexio_write test.db $roffset $rbyte 292 sqlite3 db test.db 293 294 # do a few random operations to make sure that if 295 # they error, they error gracefully instead of crashing. 296 do_test corruptC-3.$tn.($qseed).$i.1 { 297 catchsql {SELECT count(*) FROM sqlite_master} 298 set x {} 299 } {} 300 do_test corruptC-3.$tn.($qseed).$i.2 { 301 catchsql {SELECT count(*) FROM t1} 302 set x {} 303 } {} 304 do_test corruptC-3.$tn.($qseed).$i.3 { 305 catchsql {SELECT count(*) FROM t1 WHERE x>13} 306 set x {} 307 } {} 308 do_test corruptC-3.$tn.($qseed).$i.4 { 309 catchsql {SELECT count(*) FROM t2} 310 set x {} 311 } {} 312 do_test corruptC-3.$tn.($qseed).$i.5 { 313 catchsql {SELECT count(*) FROM t2 WHERE x<13} 314 set x {} 315 } {} 316 do_test corruptC-3.$tn.($qseed).$i.6 { 317 catchsql {BEGIN; UPDATE t1 SET y=1; ROLLBACK;} 318 set x {} 319 } {} 320 do_test corruptC-3.$tn.($qseed).$i.7 { 321 catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;} 322 set x {} 323 } {} 324 do_test corruptC-3.$tn.($qseed).$i.8 { 325 catchsql {BEGIN; DELETE FROM t1 WHERE x>13; ROLLBACK;} 326 set x {} 327 } {} 328 do_test corruptC-3.$tn.($qseed).$i.9 { 329 catchsql {BEGIN; DELETE FROM t2 WHERE x<13; ROLLBACK;} 330 set x {} 331 } {} 332 do_test corruptC-3.$tn.($qseed).$i.10 { 333 catchsql {BEGIN; CREATE TABLE t3 AS SELECT x,3 as y FROM t2 WHERE rowid%5!=0; ROLLBACK;} 334 set x {} 335 } {} 336 337 # check the integrity of the database. 338 # once the corruption is detected, we can stop. 339 ifcapable {integrityck} { 340 set res [ catchsql {PRAGMA integrity_check} ] 341 set ans [lindex $res 1] 342 if { [ string compare $ans "ok" ] != 0 } { 343 set last -1 344 } 345 } 346 # if we are not capable of doing an integrity check, 347 # stop after corrupting 5 bytes. 348 ifcapable {!integrityck} { 349 if { $i > 5 } { 350 set last -1 351 } 352 } 353 354 # Check that no page references were leaked. 355 # TBD: need to figure out why this doesn't work 356 # work with ROLLBACKs... 357 if {0} { 358 do_test corruptC-3.$tn.($qseed).$i.11 { 359 set bt [btree_from_db db] 360 db_enter db 361 array set stats [btree_pager_stats $bt] 362 db_leave db 363 set stats(ref) 364 } {0} 365 } 366 } 367 # end for i 368 369} 370# end for tn 371 372finish_test 373