| OLD | NEW |
| 1 # 2004 August 30 {} | 1 # 2004 August 30 {} |
| 2 # | 2 # |
| 3 # The author disclaims copyright to this source code. In place of | 3 # The author disclaims copyright to this source code. In place of |
| 4 # a legal notice, here is a blessing: | 4 # a legal notice, here is a blessing: |
| 5 # | 5 # |
| 6 # May you do good and not evil. | 6 # May you do good and not evil. |
| 7 # May you find forgiveness for yourself and forgive others. | 7 # May you find forgiveness for yourself and forgive others. |
| 8 # May you share freely, never taking more than you give. | 8 # May you share freely, never taking more than you give. |
| 9 # | 9 # |
| 10 #*********************************************************************** | 10 #*********************************************************************** |
| 11 # This file implements regression tests for SQLite library. | 11 # This file implements regression tests for SQLite library. |
| 12 # | 12 # |
| 13 # This file implements tests to make sure SQLite does not crash or | 13 # This file implements tests to make sure SQLite does not crash or |
| 14 # segfault if it sees a corrupt database file. | 14 # segfault if it sees a corrupt database file. |
| 15 # | 15 # |
| 16 # $Id: corrupt.test,v 1.12 2009/07/13 09:41:45 danielk1977 Exp $ | 16 # $Id: corrupt.test,v 1.12 2009/07/13 09:41:45 danielk1977 Exp $ |
| 17 | 17 |
| 18 catch {file delete -force test.db test.db-journal test.bu} | 18 catch {forcedelete test.db test.db-journal test.bu} |
| 19 | 19 |
| 20 set testdir [file dirname $argv0] | 20 set testdir [file dirname $argv0] |
| 21 source $testdir/tester.tcl | 21 source $testdir/tester.tcl |
| 22 | 22 |
| 23 # Do not use a codec for tests in this file, as the database file is | 23 # Do not use a codec for tests in this file, as the database file is |
| 24 # manipulated directly using tcl scripts (using the [hexio_write] command). | 24 # manipulated directly using tcl scripts (using the [hexio_write] command). |
| 25 # | 25 # |
| 26 do_not_use_codec | 26 do_not_use_codec |
| 27 | 27 |
| 28 # These tests deal with corrupt database files |
| 29 # |
| 30 database_may_be_corrupt |
| 31 |
| 28 # Construct a large database for testing. | 32 # Construct a large database for testing. |
| 29 # | 33 # |
| 30 do_test corrupt-1.1 { | 34 do_test corrupt-1.1 { |
| 31 execsql { | 35 execsql { |
| 32 BEGIN; | 36 BEGIN; |
| 33 CREATE TABLE t1(x); | 37 CREATE TABLE t1(x); |
| 34 INSERT INTO t1 VALUES(randstr(100,100)); | 38 INSERT INTO t1 VALUES(randstr(100,100)); |
| 35 INSERT INTO t1 VALUES(randstr(90,90)); | 39 INSERT INTO t1 VALUES(randstr(90,90)); |
| 36 INSERT INTO t1 VALUES(randstr(80,80)); | 40 INSERT INTO t1 VALUES(randstr(80,80)); |
| 37 INSERT INTO t1 SELECT x || randstr(5,5) FROM t1; | 41 INSERT INTO t1 SELECT x || randstr(5,5) FROM t1; |
| 38 INSERT INTO t1 SELECT x || randstr(6,6) FROM t1; | 42 INSERT INTO t1 SELECT x || randstr(6,6) FROM t1; |
| 39 INSERT INTO t1 SELECT x || randstr(7,7) FROM t1; | 43 INSERT INTO t1 SELECT x || randstr(7,7) FROM t1; |
| 40 INSERT INTO t1 SELECT x || randstr(8,8) FROM t1; | 44 INSERT INTO t1 SELECT x || randstr(8,8) FROM t1; |
| 41 INSERT INTO t1 VALUES(randstr(3000,3000)); | 45 INSERT INTO t1 VALUES(randstr(3000,3000)); |
| 42 INSERT INTO t1 SELECT x || randstr(9,9) FROM t1; | 46 INSERT INTO t1 SELECT x || randstr(9,9) FROM t1; |
| 43 INSERT INTO t1 SELECT x || randstr(10,10) FROM t1; | 47 INSERT INTO t1 SELECT x || randstr(10,10) FROM t1; |
| 44 INSERT INTO t1 SELECT x || randstr(11,11) FROM t1; | 48 INSERT INTO t1 SELECT x || randstr(11,11) FROM t1; |
| 45 INSERT INTO t1 SELECT x || randstr(12,12) FROM t1; | 49 INSERT INTO t1 SELECT x || randstr(12,12) FROM t1; |
| 46 CREATE INDEX t1i1 ON t1(x); | 50 CREATE INDEX t1i1 ON t1(x); |
| 47 CREATE TABLE t2 AS SELECT * FROM t1; | 51 CREATE TABLE t2 AS SELECT * FROM t1; |
| 48 DELETE FROM t2 WHERE rowid%5!=0; | 52 DELETE FROM t2 WHERE rowid%5!=0; |
| 49 COMMIT; | 53 COMMIT; |
| 50 } | 54 } |
| 51 } {} | 55 } {} |
| 52 integrity_check corrupt-1.2 | 56 integrity_check corrupt-1.2 |
| 53 | 57 |
| 54 # Copy file $from into $to | |
| 55 # | |
| 56 proc copy_file {from to} { | |
| 57 set f [open $from] | |
| 58 fconfigure $f -translation binary | |
| 59 set t [open $to w] | |
| 60 fconfigure $t -translation binary | |
| 61 puts -nonewline $t [read $f [file size $from]] | |
| 62 close $t | |
| 63 close $f | |
| 64 } | |
| 65 | |
| 66 # Setup for the tests. Make a backup copy of the good database in test.bu. | 58 # Setup for the tests. Make a backup copy of the good database in test.bu. |
| 67 # Create a string of garbage data that is 256 bytes long. | 59 # Create a string of garbage data that is 256 bytes long. |
| 68 # | 60 # |
| 69 copy_file test.db test.bu | 61 forcecopy test.db test.bu |
| 70 set fsize [file size test.db] | 62 set fsize [file size test.db] |
| 71 set junk "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" | 63 set junk "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 72 while {[string length $junk]<256} {append junk $junk} | 64 while {[string length $junk]<256} {append junk $junk} |
| 73 set junk [string range $junk 0 255] | 65 set junk [string range $junk 0 255] |
| 74 | 66 |
| 75 # Go through the database and write garbage data into each 256 segment | 67 # Go through the database and write garbage data into each 256 segment |
| 76 # of the file. Then do various operations on the file to make sure that | 68 # of the file. Then do various operations on the file to make sure that |
| 77 # the database engine can recover gracefully from the corruption. | 69 # the database engine can recover gracefully from the corruption. |
| 78 # | 70 # |
| 79 for {set i [expr {1*256}]} {$i<$fsize-256} {incr i 256} { | 71 for {set i [expr {1*256}]} {$i<$fsize-256} {incr i 256} { |
| 80 set tn [expr {$i/256}] | 72 set tn [expr {$i/256}] |
| 81 db close | 73 db close |
| 82 copy_file test.bu test.db | 74 forcecopy test.bu test.db |
| 83 set fd [open test.db r+] | 75 set fd [open test.db r+] |
| 84 fconfigure $fd -translation binary | 76 fconfigure $fd -translation binary |
| 85 seek $fd $i | 77 seek $fd $i |
| 86 puts -nonewline $fd $junk | 78 puts -nonewline $fd $junk |
| 87 close $fd | 79 close $fd |
| 88 do_test corrupt-2.$tn.1 { | 80 do_test corrupt-2.$tn.1 { |
| 89 sqlite3 db test.db | 81 sqlite3 db test.db |
| 90 catchsql {SELECT count(*) FROM sqlite_master} | 82 catchsql {SELECT count(*) FROM sqlite_master} |
| 91 set x {} | 83 set x {} |
| 92 } {} | 84 } {} |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 } {0} | 117 } {0} |
| 126 } | 118 } |
| 127 | 119 |
| 128 #------------------------------------------------------------------------ | 120 #------------------------------------------------------------------------ |
| 129 # For these tests, swap the rootpage entries of t1 (a table) and t1i1 (an | 121 # For these tests, swap the rootpage entries of t1 (a table) and t1i1 (an |
| 130 # index on t1) in sqlite_master. Then perform a few different queries | 122 # index on t1) in sqlite_master. Then perform a few different queries |
| 131 # and make sure this is detected as corruption. | 123 # and make sure this is detected as corruption. |
| 132 # | 124 # |
| 133 do_test corrupt-3.1 { | 125 do_test corrupt-3.1 { |
| 134 db close | 126 db close |
| 135 copy_file test.bu test.db | 127 forcecopy test.bu test.db |
| 136 sqlite3 db test.db | 128 sqlite3 db test.db |
| 137 list | 129 list |
| 138 } {} | 130 } {} |
| 139 do_test corrupt-3.2 { | 131 do_test corrupt-3.2 { |
| 140 set t1_r [execsql {SELECT rootpage FROM sqlite_master WHERE name = 't1i1'}] | 132 set t1_r [execsql {SELECT rootpage FROM sqlite_master WHERE name = 't1i1'}] |
| 141 set t1i1_r [execsql {SELECT rootpage FROM sqlite_master WHERE name = 't1'}] | 133 set t1i1_r [execsql {SELECT rootpage FROM sqlite_master WHERE name = 't1'}] |
| 142 set cookie [expr [execsql {PRAGMA schema_version}] + 1] | 134 set cookie [expr [execsql {PRAGMA schema_version}] + 1] |
| 143 execsql " | 135 execsql " |
| 144 PRAGMA writable_schema = 1; | 136 PRAGMA writable_schema = 1; |
| 145 UPDATE sqlite_master SET rootpage = $t1_r WHERE name = 't1'; | 137 UPDATE sqlite_master SET rootpage = $t1_r WHERE name = 't1'; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 174 do_test corrupt-3.6 { | 166 do_test corrupt-3.6 { |
| 175 db close | 167 db close |
| 176 sqlite3 db test.db | 168 sqlite3 db test.db |
| 177 catchsql { | 169 catchsql { |
| 178 SELECT * FROM t1 WHERE x = 'abcde'; | 170 SELECT * FROM t1 WHERE x = 'abcde'; |
| 179 } | 171 } |
| 180 } {1 {database disk image is malformed}} | 172 } {1 {database disk image is malformed}} |
| 181 | 173 |
| 182 do_test corrupt-4.1 { | 174 do_test corrupt-4.1 { |
| 183 db close | 175 db close |
| 184 file delete -force test.db test.db-journal | 176 forcedelete test.db test.db-journal |
| 185 sqlite3 db test.db | 177 sqlite3 db test.db |
| 186 execsql { | 178 execsql { |
| 187 PRAGMA page_size = 1024; | 179 PRAGMA page_size = 1024; |
| 188 CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT); | 180 CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT); |
| 189 } | 181 } |
| 190 for {set i 0} {$i < 10} {incr i} { | 182 for {set i 0} {$i < 10} {incr i} { |
| 191 set text [string repeat $i 220] | 183 set text [string repeat $i 220] |
| 192 execsql { INSERT INTO t1 VALUES($i, $text) } | 184 execsql { INSERT INTO t1 VALUES($i, $text) } |
| 193 } | 185 } |
| 194 execsql { CREATE INDEX i1 ON t1(b) } | 186 execsql { CREATE INDEX i1 ON t1(b) } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 206 # by a cell retrieved from the child page (a leaf) of the deleted cell. | 198 # by a cell retrieved from the child page (a leaf) of the deleted cell. |
| 207 # This will fail, as the block modified the database image so that the | 199 # This will fail, as the block modified the database image so that the |
| 208 # child page of the deleted cell is from a table (intkey) b-tree, not an | 200 # child page of the deleted cell is from a table (intkey) b-tree, not an |
| 209 # index b-tree as expected. At one point this was causing an assert() | 201 # index b-tree as expected. At one point this was causing an assert() |
| 210 # to fail. | 202 # to fail. |
| 211 catchsql { DELETE FROM t1 WHERE rowid = 3 } | 203 catchsql { DELETE FROM t1 WHERE rowid = 3 } |
| 212 } {1 {database disk image is malformed}} | 204 } {1 {database disk image is malformed}} |
| 213 | 205 |
| 214 do_test corrupt-5.1 { | 206 do_test corrupt-5.1 { |
| 215 db close | 207 db close |
| 216 file delete -force test.db test.db-journal | 208 forcedelete test.db test.db-journal |
| 217 sqlite3 db test.db | 209 sqlite3 db test.db |
| 218 | 210 |
| 219 execsql { PRAGMA page_size = 1024 } | 211 execsql { PRAGMA page_size = 1024 } |
| 220 set ct "CREATE TABLE t1(c0 " | 212 set ct "CREATE TABLE t1(c0 " |
| 221 set i 0 | 213 set i 0 |
| 222 while {[string length $ct] < 950} { append ct ", c[incr i]" } | 214 while {[string length $ct] < 950} { append ct ", c[incr i]" } |
| 223 append ct ")" | 215 append ct ")" |
| 224 execsql $ct | 216 execsql $ct |
| 225 } {} | 217 } {} |
| 226 | 218 |
| 227 do_test corrupt-5.2 { | 219 do_test corrupt-5.2 { |
| 228 db close | 220 db close |
| 229 hexio_write test.db 108 00000000 | 221 hexio_write test.db 108 00000000 |
| 230 sqlite3 db test.db | 222 sqlite3 db test.db |
| 231 catchsql { SELECT * FROM sqlite_master } | 223 catchsql { SELECT * FROM sqlite_master } |
| 232 } {1 {database disk image is malformed}} | 224 } {1 {database disk image is malformed}} |
| 233 | 225 |
| 234 # At one point, the specific corruption caused by this test case was | 226 # At one point, the specific corruption caused by this test case was |
| 235 # causing a buffer overwrite. Although a crash was never demonstrated, | 227 # causing a buffer overwrite. Although a crash was never demonstrated, |
| 236 # running this testcase under valgrind revealed the problem. | 228 # running this testcase under valgrind revealed the problem. |
| 237 do_test corrupt-6.1 { | 229 do_test corrupt-6.1 { |
| 238 db close | 230 db close |
| 239 file delete -force test.db test.db-journal | 231 forcedelete test.db test.db-journal |
| 240 sqlite3 db test.db | 232 sqlite3 db test.db |
| 241 execsql { | 233 execsql { |
| 242 PRAGMA page_size = 1024; CREATE TABLE t1(x); | 234 PRAGMA page_size = 1024; CREATE TABLE t1(x); |
| 243 } | 235 } |
| 244 | 236 |
| 245 # The root page of t1 is 1024 bytes in size. The header is 8 bytes, and | 237 # The root page of t1 is 1024 bytes in size. The header is 8 bytes, and |
| 246 # each of the cells inserted by the following INSERT statements consume | 238 # each of the cells inserted by the following INSERT statements consume |
| 247 # 16 bytes (including the 2 byte cell-offset array entry). So the page | 239 # 16 bytes (including the 2 byte cell-offset array entry). So the page |
| 248 # can contain up to 63 cells. | 240 # can contain up to 63 cells. |
| 249 for {set i 0} {$i < 63} {incr i} { | 241 for {set i 0} {$i < 63} {incr i} { |
| 250 execsql { INSERT INTO t1 VALUES( randomblob(10) ) } | 242 execsql { INSERT INTO t1 VALUES( randomblob(10) ) } |
| 251 } | 243 } |
| 252 | 244 |
| 253 # Free the cell stored right at the end of the page (at offset pgsz-14). | 245 # Free the cell stored right at the end of the page (at offset pgsz-14). |
| 254 execsql { DELETE FROM t1 WHERE rowid=1 } | 246 execsql { DELETE FROM t1 WHERE rowid=1 } |
| 255 set rootpage [db one {SELECT rootpage FROM sqlite_master WHERE name = 't1'}] | 247 set rootpage [db one {SELECT rootpage FROM sqlite_master WHERE name = 't1'}] |
| 256 db close | 248 db close |
| 257 | 249 |
| 258 set offset [expr ($rootpage * 1024)-14+2] | 250 set offset [expr ($rootpage * 1024)-14+2] |
| 259 hexio_write test.db $offset 00FF | 251 hexio_write test.db $offset 00FF |
| 260 sqlite3 db test.db | 252 sqlite3 db test.db |
| 261 | 253 |
| 262 catchsql { INSERT INTO t1 VALUES( randomblob(10) ) } | 254 catchsql { INSERT INTO t1 VALUES( randomblob(10) ) } |
| 263 } {1 {database disk image is malformed}} | 255 } {1 {database disk image is malformed}} |
| 264 | 256 |
| 265 ifcapable oversize_cell_check { | 257 ifcapable oversize_cell_check { |
| 266 db close | 258 db close |
| 267 file delete -force test.db test.db-journal | 259 forcedelete test.db test.db-journal |
| 268 sqlite3 db test.db | 260 sqlite3 db test.db |
| 269 execsql { | 261 execsql { |
| 270 PRAGMA page_size = 1024; CREATE TABLE t1(x); | 262 PRAGMA page_size = 1024; CREATE TABLE t1(x); |
| 271 } | 263 } |
| 272 | 264 |
| 273 do_test corrupt-7.1 { | 265 do_test corrupt-7.1 { |
| 274 for {set i 0} {$i < 39} {incr i} { | 266 for {set i 0} {$i < 39} {incr i} { |
| 275 execsql { | 267 execsql { |
| 276 INSERT INTO t1 VALUES(X'000100020003000400050006000700080009000A'); | 268 INSERT INTO t1 VALUES(X'000100020003000400050006000700080009000A'); |
| 277 } | 269 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 } | 302 } |
| 311 } {} | 303 } {} |
| 312 do_test corrupt-7.3 { | 304 do_test corrupt-7.3 { |
| 313 catchsql { | 305 catchsql { |
| 314 INSERT INTO t1 VALUES(X'000100020003000400050006000700080009000A'); | 306 INSERT INTO t1 VALUES(X'000100020003000400050006000700080009000A'); |
| 315 } | 307 } |
| 316 } {1 {database disk image is malformed}} | 308 } {1 {database disk image is malformed}} |
| 317 } | 309 } |
| 318 | 310 |
| 319 db close | 311 db close |
| 320 file delete -force test.db test.db-journal | 312 forcedelete test.db test.db-journal |
| 321 do_test corrupt-8.1 { | 313 do_test corrupt-8.1 { |
| 322 sqlite3 db test.db | 314 sqlite3 db test.db |
| 323 execsql { | 315 execsql { |
| 324 PRAGMA page_size = 1024; | 316 PRAGMA page_size = 1024; |
| 325 PRAGMA secure_delete = on; | 317 PRAGMA secure_delete = on; |
| 326 PRAGMA auto_vacuum = 0; | 318 PRAGMA auto_vacuum = 0; |
| 327 CREATE TABLE t1(x INTEGER PRIMARY KEY, y); | 319 CREATE TABLE t1(x INTEGER PRIMARY KEY, y); |
| 328 INSERT INTO t1 VALUES(5, randomblob(1900)); | 320 INSERT INTO t1 VALUES(5, randomblob(1900)); |
| 329 } | 321 } |
| 330 | 322 |
| 331 hexio_write test.db 2044 [hexio_render_int32 2] | 323 hexio_write test.db 2044 [hexio_render_int32 2] |
| 332 hexio_write test.db 24 [hexio_render_int32 45] | 324 hexio_write test.db 24 [hexio_render_int32 45] |
| 333 | 325 |
| 334 catchsql { INSERT OR REPLACE INTO t1 VALUES(5, randomblob(1900)) } | 326 catchsql { INSERT OR REPLACE INTO t1 VALUES(5, randomblob(1900)) } |
| 335 } {1 {database disk image is malformed}} | 327 } {1 {database disk image is malformed}} |
| 336 | 328 |
| 337 db close | 329 db close |
| 338 file delete -force test.db test.db-journal | 330 forcedelete test.db test.db-journal |
| 339 do_test corrupt-8.2 { | 331 do_test corrupt-8.2 { |
| 340 sqlite3 db test.db | 332 sqlite3 db test.db |
| 341 execsql { | 333 execsql { |
| 342 PRAGMA page_size = 1024; | 334 PRAGMA page_size = 1024; |
| 343 PRAGMA secure_delete = on; | 335 PRAGMA secure_delete = on; |
| 344 PRAGMA auto_vacuum = 0; | 336 PRAGMA auto_vacuum = 0; |
| 345 CREATE TABLE t1(x INTEGER PRIMARY KEY, y); | 337 CREATE TABLE t1(x INTEGER PRIMARY KEY, y); |
| 346 INSERT INTO t1 VALUES(5, randomblob(900)); | 338 INSERT INTO t1 VALUES(5, randomblob(900)); |
| 347 INSERT INTO t1 VALUES(6, randomblob(900)); | 339 INSERT INTO t1 VALUES(6, randomblob(900)); |
| 348 } | 340 } |
| 349 | 341 |
| 350 hexio_write test.db 2047 FF | 342 hexio_write test.db 2047 FF |
| 351 hexio_write test.db 24 [hexio_render_int32 45] | 343 hexio_write test.db 24 [hexio_render_int32 45] |
| 352 | 344 |
| 353 catchsql { INSERT INTO t1 VALUES(4, randomblob(1900)) } | 345 catchsql { INSERT INTO t1 VALUES(4, randomblob(1900)) } |
| 354 } {1 {database disk image is malformed}} | 346 } {1 {database disk image is malformed}} |
| 355 | 347 |
| 356 finish_test | 348 finish_test |
| OLD | NEW |