sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 1 | # 2010 February 18 |
| 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 specifcally |
| 15 | # focuses on rowid order corruption. |
| 16 | # |
| 17 | |
| 18 | set testdir [file dirname $argv0] |
| 19 | source $testdir/tester.tcl |
| 20 | |
| 21 | # This module uses hard-coded offsets which do not work if the reserved_bytes |
| 22 | # value is nonzero. |
| 23 | if {[nonzero_reserved_bytes]} {finish_test; return;} |
| 24 | |
| 25 | # These tests deal with corrupt database files |
| 26 | # |
| 27 | database_may_be_corrupt |
| 28 | |
| 29 | # Do not run the tests in this file if ENABLE_OVERSIZE_CELL_CHECK is on. |
| 30 | # |
| 31 | ifcapable oversize_cell_check { |
| 32 | finish_test |
| 33 | return |
| 34 | } |
| 35 | |
| 36 | # Construct a compact, dense database for testing. |
| 37 | # |
| 38 | do_test corruptE-1.1 { |
| 39 | sqlite3_db_config db LEGACY_FILE_FORMAT 1 |
| 40 | execsql { |
| 41 | PRAGMA auto_vacuum = 0; |
| 42 | BEGIN; |
| 43 | CREATE TABLE t1(x,y); |
| 44 | INSERT INTO t1 VALUES(1,1); |
| 45 | INSERT OR IGNORE INTO t1 SELECT x*2,y FROM t1; |
| 46 | INSERT OR IGNORE INTO t1 SELECT x*3,y FROM t1; |
| 47 | INSERT OR IGNORE INTO t1 SELECT x*5,y FROM t1; |
| 48 | INSERT OR IGNORE INTO t1 SELECT x*7,y FROM t1; |
| 49 | INSERT OR IGNORE INTO t1 SELECT x*11,y FROM t1; |
| 50 | INSERT OR IGNORE INTO t1 SELECT x*13,y FROM t1; |
| 51 | INSERT OR IGNORE INTO t1 SELECT x*17,y FROM t1; |
| 52 | INSERT OR IGNORE INTO t1 SELECT x*19,y FROM t1; |
| 53 | CREATE INDEX t1i1 ON t1(x); |
| 54 | CREATE TABLE t2 AS SELECT x,2 as y FROM t1 WHERE rowid%5!=0 ORDER BY rowid; |
| 55 | COMMIT; |
| 56 | } |
| 57 | } {} |
| 58 | |
| 59 | ifcapable {integrityck} { |
| 60 | integrity_check corruptE-1.2 |
| 61 | } |
| 62 | |
| 63 | # Setup for the tests. Make a backup copy of the good database in test.bu. |
| 64 | # |
| 65 | db close |
| 66 | forcecopy test.db test.bu |
| 67 | sqlite3 db test.db |
| 68 | set fsize [file size test.db] |
| 69 | |
| 70 | |
| 71 | do_test corruptE-2.1 { |
| 72 | db close |
| 73 | forcecopy test.bu test.db |
| 74 | |
| 75 | # insert corrupt byte(s) |
| 76 | hexio_write test.db 2041 [format %02x 0x2e] |
| 77 | |
| 78 | sqlite3 db test.db |
| 79 | |
| 80 | catchsql {PRAGMA integrity_check} |
| 81 | } {/ out of order/} |
| 82 | |
| 83 | do_test corruptE-2.2 { |
| 84 | db close |
| 85 | forcecopy test.bu test.db |
| 86 | |
| 87 | # insert corrupt byte(s) |
| 88 | hexio_write test.db 2047 [format %02x 0x84] |
| 89 | |
| 90 | sqlite3 db test.db |
| 91 | |
| 92 | catchsql {PRAGMA integrity_check} |
| 93 | } {/ Extends off end of page/} |
| 94 | |
| 95 | do_test corruptE-2.3 { |
| 96 | db close |
| 97 | forcecopy test.bu test.db |
| 98 | |
| 99 | # insert corrupt byte(s) |
| 100 | hexio_write test.db 7420 [format %02x 0xa8] |
| 101 | hexio_write test.db 10459 [format %02x 0x8d] |
| 102 | |
| 103 | sqlite3 db test.db |
| 104 | |
| 105 | catchsql {PRAGMA integrity_check} |
| 106 | } {/out of order/} |
| 107 | |
| 108 | do_test corruptE-2.4 { |
| 109 | db close |
| 110 | forcecopy test.bu test.db |
| 111 | |
| 112 | # insert corrupt byte(s) |
| 113 | hexio_write test.db 10233 [format %02x 0xd0] |
| 114 | |
| 115 | sqlite3 db test.db |
| 116 | |
| 117 | catchsql {PRAGMA integrity_check} |
| 118 | } {/out of order/} |
| 119 | |
| 120 | |
| 121 | set tests [list {10233 0xd0} \ |
| 122 | {941 0x42} \ |
| 123 | {2041 0xd0} \ |
| 124 | {2042 0x1f} \ |
| 125 | {2274 0x75} \ |
| 126 | {3267 0xf2} \ |
| 127 | {5113 0x36} \ |
| 128 | {10233 0x84} \ |
| 129 | {10234 0x74} \ |
| 130 | {10239 0x41} \ |
| 131 | {11273 0x28} \ |
| 132 | {11461 0xe6} \ |
| 133 | {12297 0xd7} \ |
| 134 | {13303 0x53} ] |
| 135 | |
| 136 | set tc 1 |
| 137 | foreach test $tests { |
| 138 | do_test corruptE-3.$tc { |
| 139 | db close |
| 140 | forcecopy test.bu test.db |
| 141 | |
| 142 | # insert corrupt byte(s) |
| 143 | hexio_write test.db [lindex $test 0] [format %02x [lindex $test 1]] |
| 144 | |
| 145 | sqlite3 db test.db |
| 146 | |
| 147 | catchsql {PRAGMA integrity_check} |
| 148 | } {/out of order/} |
| 149 | incr tc 1 |
| 150 | } |
| 151 | |
| 152 | finish_test |