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# $Id: corruptE.test,v 1.14 2009/07/11 06:55:34 danielk1977 Exp $ 18 19catch {forcedelete test.db test.db-journal test.bu} 20 21set testdir [file dirname $argv0] 22source $testdir/tester.tcl 23 24# Do not use a codec for tests in this file, as the database file is 25# manipulated directly using tcl scripts (using the [hexio_write] command). 26# 27do_not_use_codec 28 29# Do not run the tests in this file if ENABLE_OVERSIZE_CELL_CHECK is on. 30# 31ifcapable oversize_cell_check { 32 finish_test 33 return 34} 35 36# Construct a compact, dense database for testing. 37# 38do_test corruptE-1.1 { 39 execsql { 40 PRAGMA auto_vacuum = 0; 41 PRAGMA legacy_file_format=1; 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; 55 COMMIT; 56 } 57} {} 58 59ifcapable {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# 65db close 66forcecopy test.db test.bu 67sqlite3 db test.db 68set fsize [file size test.db] 69 70 71do_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 set res [ catchsql {PRAGMA integrity_check} ] 81 set ans [lindex $res 1] 82 83 list [regexp {out of order.*previous was} $ans] \ 84 [regexp {out of order.*max larger than parent max} $ans] 85} {1 1} 86 87do_test corruptE-2.2 { 88 db close 89 forcecopy test.bu test.db 90 91 # insert corrupt byte(s) 92 hexio_write test.db 2047 [format %02x 0x84] 93 94 sqlite3 db test.db 95 96 set res [ catchsql {PRAGMA integrity_check} ] 97 set ans [lindex $res 1] 98 99 list [regexp {out of order.*previous was} $ans] \ 100 [regexp {out of order.*min less than parent min} $ans] 101} {1 1} 102 103do_test corruptE-2.3 { 104 db close 105 forcecopy test.bu test.db 106 107 # insert corrupt byte(s) 108 hexio_write test.db 7420 [format %02x 0xa8] 109 hexio_write test.db 10459 [format %02x 0x8d] 110 111 sqlite3 db test.db 112 113 set res [ catchsql {PRAGMA integrity_check} ] 114 set ans [lindex $res 1] 115 116 list [regexp {out of order.*max larger than parent min} $ans] 117} {1} 118 119do_test corruptE-2.4 { 120 db close 121 forcecopy test.bu test.db 122 123 # insert corrupt byte(s) 124 hexio_write test.db 10233 [format %02x 0xd0] 125 126 sqlite3 db test.db 127 128 set res [ catchsql {PRAGMA integrity_check} ] 129 set ans [lindex $res 1] 130 131 list [regexp {out of order.*min less than parent max} $ans] 132} {1} 133 134 135set tests [list {10233 0xd0} \ 136 {941 0x42} \ 137 {1028 0x53} \ 138 {2041 0xd0} \ 139 {2042 0x1f} \ 140 {2047 0xaa} \ 141 {2263 0x29} \ 142 {2274 0x75} \ 143 {3267 0xf2} \ 144 {4104 0x2c} \ 145 {5113 0x36} \ 146 {10233 0x84} \ 147 {10234 0x74} \ 148 {10239 0x41} \ 149 {10453 0x11} \ 150 {11273 0x28} \ 151 {11455 0x11} \ 152 {11461 0xe6} \ 153 {12281 0x99} \ 154 {12296 0x9e} \ 155 {12297 0xd7} \ 156 {13303 0x53} ] 157 158set tc 1 159foreach test $tests { 160 do_test corruptE-3.$tc { 161 db close 162 forcecopy test.bu test.db 163 164 # insert corrupt byte(s) 165 hexio_write test.db [lindex $test 0] [format %02x [lindex $test 1]] 166 167 sqlite3 db test.db 168 169 set res [ catchsql {PRAGMA integrity_check} ] 170 set ans [lindex $res 1] 171 172 list [regexp {out of order} $ans] 173 } {1} 174 incr tc 1 175} 176 177finish_test 178