1# 2014-01-20 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# 12 13set testdir [file dirname $argv0] 14source $testdir/tester.tcl 15set testprefix corruptH 16 17# Do not use a codec for tests in this file, as the database file is 18# manipulated directly using tcl scripts (using the [hexio_write] command). 19# 20do_not_use_codec 21database_may_be_corrupt 22 23# Initialize the database. 24# 25do_execsql_test 1.1 { 26 PRAGMA page_size=1024; 27 28 CREATE TABLE t1(a INTEGER PRIMARY KEY, b); 29 INSERT INTO t1 VALUES(1, 'one'); 30 INSERT INTO t1 VALUES(2, 'two'); 31 32 CREATE TABLE t2(x); 33 INSERT INTO t2 VALUES(randomblob(200)); 34 INSERT INTO t2 SELECT randomblob(200) FROM t2; 35 INSERT INTO t2 SELECT randomblob(200) FROM t2; 36 INSERT INTO t2 SELECT randomblob(200) FROM t2; 37 INSERT INTO t2 SELECT randomblob(200) FROM t2; 38 INSERT INTO t2 SELECT randomblob(200) FROM t2; 39 INSERT INTO t2 SELECT randomblob(200) FROM t2; 40} {} 41 42# Corrupt the file so that the root page of t1 is also linked into t2 as 43# a leaf page. 44# 45do_test 1.2 { 46 db eval { SELECT name, rootpage FROM sqlite_master } { 47 set r($name) $rootpage 48 } 49 db close 50 hexio_write test.db [expr {($r(t2)-1)*1024 + 11}] [format %.2X $r(t1)] 51 sqlite3 db test.db 52} {} 53 54do_test 1.3 { 55 db eval { PRAGMA secure_delete=1 } 56 list [catch { 57 db eval { SELECT * FROM t1 WHERE a IN (1, 2) } { 58 db eval { DELETE FROM t2 } 59 } 60 } msg] $msg 61} {1 {database disk image is malformed}} 62 63#------------------------------------------------------------------------- 64reset_db 65 66# Initialize the database. 67# 68do_execsql_test 2.1 { 69 PRAGMA auto_vacuum=0; 70 PRAGMA page_size=1024; 71 72 CREATE TABLE t1(a INTEGER PRIMARY KEY, b); 73 INSERT INTO t1 VALUES(1, 'one'); 74 INSERT INTO t1 VALUES(2, 'two'); 75 76 CREATE TABLE t3(x); 77 78 CREATE TABLE t2(x PRIMARY KEY) WITHOUT ROWID; 79 INSERT INTO t2 VALUES(randomblob(100)); 80 81 DROP TABLE t3; 82} {} 83 84do_test 2.2 { 85 db eval { SELECT name, rootpage FROM sqlite_master } { 86 set r($name) $rootpage 87 } 88 db close 89 set fl [hexio_get_int [hexio_read test.db 32 4]] 90 91 hexio_write test.db [expr {($fl-1) * 1024 + 0}] 00000000 92 hexio_write test.db [expr {($fl-1) * 1024 + 4}] 00000001 93 hexio_write test.db [expr {($fl-1) * 1024 + 8}] [format %.8X $r(t1)] 94 hexio_write test.db 36 00000002 95 96 sqlite3 db test.db 97} {} 98 99 100# The corruption migration caused by the test case below does not 101# cause corruption to be detected in mmap mode. 102# 103# The trick here is that the root page of the tree scanned by the outer 104# query is also currently on the free-list. So while the first seek on 105# the table (for a==1) works, by the time the second is attempted The 106# "INSERT INTO t2..." statements have recycled the root page of t1 and 107# used it as an index leaf. Normally, BtreeMovetoUnpacked() detects 108# that the PgHdr object associated with said root page does not match 109# the cursor (as it is now marked with PgHdr.intKey==0) and returns 110# SQLITE_CORRUPT. 111# 112# However, in mmap mode, the outer query and the inner queries use 113# different PgHdr objects (same data, but different PgHdr container 114# objects). And so the corruption is not detected. Instead, the second 115# seek fails to find anything and only a single row is returned. 116# 117set res23 {1 {database disk image is malformed}} 118if {[permutation]=="mmap"} { 119 set res23 {0 one} 120} 121do_test 2.3 { 122 list [catch { 123 set res [list] 124 db eval { SELECT * FROM t1 WHERE a IN (1, 2) } { 125 db eval { 126 INSERT INTO t2 SELECT randomblob(100) FROM t2; 127 INSERT INTO t2 SELECT randomblob(100) FROM t2; 128 INSERT INTO t2 SELECT randomblob(100) FROM t2; 129 INSERT INTO t2 SELECT randomblob(100) FROM t2; 130 INSERT INTO t2 SELECT randomblob(100) FROM t2; 131 } 132 lappend res $b 133 } 134 set res 135 } msg] $msg 136} $res23 137 138#------------------------------------------------------------------------- 139reset_db 140 141# Initialize the database. 142# 143do_execsql_test 3.1 { 144 PRAGMA page_size=1024; 145 146 CREATE TABLE t1(a INTEGER PRIMARY KEY, b); 147 INSERT INTO t1 VALUES(1, 'one'); 148 INSERT INTO t1 VALUES(2, 'two'); 149 150 CREATE TABLE t2(c INTEGER PRAGMA KEY, d); 151 INSERT INTO t2 VALUES(1, randomblob(1100)); 152} {} 153 154do_test 3.2 { 155 db eval { SELECT name, rootpage FROM sqlite_master } { 156 set r($name) $rootpage 157 } 158 db close 159 160 hexio_write test.db [expr {($r(t2)-1) * 1024 + 1020}] 00000002 161 162 sqlite3 db test.db 163} {} 164 165do_test 3.3 { 166 list [catch { 167 db eval { SELECT * FROM t1 WHERE a IN (1, 2) } { 168 db eval { 169 DELETE FROM t2 WHERE c=1; 170 } 171 } 172 } msg] $msg 173} {1 {database disk image is malformed}} 174 175finish_test 176 177