| OLD | NEW |
| (Empty) |
| 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 | |
| 20 catch {file delete -force test.db test.db-journal test.bu} | |
| 21 | |
| 22 set testdir [file dirname $argv0] | |
| 23 source $testdir/tester.tcl | |
| 24 | |
| 25 # Construct a compact, dense database for testing. | |
| 26 # | |
| 27 do_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 | |
| 46 ifcapable {integrityck} { | |
| 47 integrity_check corruptC-1.2 | |
| 48 } | |
| 49 | |
| 50 # Generate random integer | |
| 51 # | |
| 52 proc random {range} { | |
| 53 return [expr {round(rand()*$range)}] | |
| 54 } | |
| 55 | |
| 56 # Copy file $from into $to | |
| 57 # | |
| 58 proc 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 # | |
| 64 db close | |
| 65 copy_file test.db test.bu | |
| 66 sqlite3 db test.db | |
| 67 set fsize [file size test.db] | |
| 68 | |
| 69 # Set a quasi-random random seed. | |
| 70 if {[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 } | |
| 80 expr 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) | |
| 88 do_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) | |
| 100 do_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) | |
| 121 do_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) | |
| 133 do_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) | |
| 145 do_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 *** | |
| 157 Page 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) | |
| 165 do_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) | |
| 178 do_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) | |
| 190 do_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) | |
| 206 do_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) | |
| 218 do_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) | |
| 230 do_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) | |
| 242 do_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) | |
| 254 do_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 | |
| 265 do_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 # | |
| 280 # Now test for a series of quasi-random seeds. | |
| 281 # We loop over the entire file size and touch | |
| 282 # each byte at least once. | |
| 283 for {set tn 0} {$tn<$fsize} {incr tn 1} { | |
| 284 | |
| 285 # setup for test | |
| 286 db close | |
| 287 copy_file test.bu test.db | |
| 288 sqlite3 db test.db | |
| 289 | |
| 290 # Seek to a random location in the file, and write a random single byte | |
| 291 # value. Then do various operations on the file to make sure that | |
| 292 # the database engine can handle the corruption gracefully. | |
| 293 # | |
| 294 set last 0 | |
| 295 for {set i 1} {$i<=512 && !$last} {incr i 1} { | |
| 296 | |
| 297 db close | |
| 298 if {$i==1} { | |
| 299 # on the first corrupt value, use location $tn | |
| 300 # this ensures that we touch each location in the | |
| 301 # file at least once. | |
| 302 set roffset $tn | |
| 303 } else { | |
| 304 # insert random byte at random location | |
| 305 set roffset [random $fsize] | |
| 306 } | |
| 307 set rbyte [format %02x [random 255]] | |
| 308 | |
| 309 # You can uncomment the following to have it trace | |
| 310 # exactly how it's corrupting the file. This is | |
| 311 # useful for generating the "seed specific" tests | |
| 312 # above. | |
| 313 # set rline "$roffset $rbyte" | |
| 314 # puts stdout $rline | |
| 315 | |
| 316 hexio_write test.db $roffset $rbyte | |
| 317 sqlite3 db test.db | |
| 318 | |
| 319 # do a few random operations to make sure that if | |
| 320 # they error, they error gracefully instead of crashing. | |
| 321 do_test corruptC-3.$tn.($qseed).$i.1 { | |
| 322 catchsql {SELECT count(*) FROM sqlite_master} | |
| 323 set x {} | |
| 324 } {} | |
| 325 do_test corruptC-3.$tn.($qseed).$i.2 { | |
| 326 catchsql {SELECT count(*) FROM t1} | |
| 327 set x {} | |
| 328 } {} | |
| 329 do_test corruptC-3.$tn.($qseed).$i.3 { | |
| 330 catchsql {SELECT count(*) FROM t1 WHERE x>13} | |
| 331 set x {} | |
| 332 } {} | |
| 333 do_test corruptC-3.$tn.($qseed).$i.4 { | |
| 334 catchsql {SELECT count(*) FROM t2} | |
| 335 set x {} | |
| 336 } {} | |
| 337 do_test corruptC-3.$tn.($qseed).$i.5 { | |
| 338 catchsql {SELECT count(*) FROM t2 WHERE x<13} | |
| 339 set x {} | |
| 340 } {} | |
| 341 do_test corruptC-3.$tn.($qseed).$i.6 { | |
| 342 catchsql {BEGIN; UPDATE t1 SET y=1; ROLLBACK;} | |
| 343 set x {} | |
| 344 } {} | |
| 345 do_test corruptC-3.$tn.($qseed).$i.7 { | |
| 346 catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;} | |
| 347 set x {} | |
| 348 } {} | |
| 349 do_test corruptC-3.$tn.($qseed).$i.8 { | |
| 350 catchsql {BEGIN; DELETE FROM t1 WHERE x>13; ROLLBACK;} | |
| 351 set x {} | |
| 352 } {} | |
| 353 do_test corruptC-3.$tn.($qseed).$i.9 { | |
| 354 catchsql {BEGIN; DELETE FROM t2 WHERE x<13; ROLLBACK;} | |
| 355 set x {} | |
| 356 } {} | |
| 357 do_test corruptC-3.$tn.($qseed).$i.10 { | |
| 358 catchsql {BEGIN; CREATE TABLE t3 AS SELECT x,3 as y FROM t2 WHERE rowid%5!
=0; ROLLBACK;} | |
| 359 set x {} | |
| 360 } {} | |
| 361 | |
| 362 # check the integrity of the database. | |
| 363 # once the corruption is detected, we can stop. | |
| 364 ifcapable {integrityck} { | |
| 365 set res [ catchsql {PRAGMA integrity_check} ] | |
| 366 set ans [lindex $res 1] | |
| 367 if { [ string compare $ans "ok" ] != 0 } { | |
| 368 set last -1 | |
| 369 } | |
| 370 } | |
| 371 # if we are not capable of doing an integrity check, | |
| 372 # stop after corrupting 5 bytes. | |
| 373 ifcapable {!integrityck} { | |
| 374 if { $i > 5 } { | |
| 375 set last -1 | |
| 376 } | |
| 377 } | |
| 378 | |
| 379 # Check that no page references were leaked. | |
| 380 # TBD: need to figure out why this doesn't work | |
| 381 # work with ROLLBACKs... | |
| 382 if {0} { | |
| 383 do_test corruptC-3.$tn.($qseed).$i.11 { | |
| 384 set bt [btree_from_db db] | |
| 385 db_enter db | |
| 386 array set stats [btree_pager_stats $bt] | |
| 387 db_leave db | |
| 388 set stats(ref) | |
| 389 } {0} | |
| 390 } | |
| 391 } | |
| 392 # end for i | |
| 393 | |
| 394 } | |
| 395 # end for tn | |
| 396 | |
| 397 finish_test | |
| OLD | NEW |