1# 2017-03-03 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 corruptK 16 17if {[permutation]=="mmap"} { 18 finish_test 19 return 20} 21 22# This module uses hard-coded offsets which do not work if the reserved_bytes 23# value is nonzero. 24if {[nonzero_reserved_bytes]} {finish_test; return;} 25database_may_be_corrupt 26 27# Initialize the database. 28# 29do_execsql_test 1.1 { 30 PRAGMA page_size=1024; 31 PRAGMA auto_vacuum=0; 32 CREATE TABLE t1(x); 33 34 INSERT INTO t1 VALUES(randomblob(20)); 35 INSERT INTO t1 VALUES(randomblob(100)); -- make this into a free slot 36 INSERT INTO t1 VALUES(randomblob(27)); -- this one will be corrupt 37 INSERT INTO t1 VALUES(randomblob(800)); 38 39 DELETE FROM t1 WHERE rowid=2; -- free the 100 byte slot 40 PRAGMA page_count 41} {2} 42 43 44# Corrupt the database so that the blob stored immediately before 45# the free slot (rowid==3) has an overlarge length field. So that 46# we can use sqlite3_blob_write() to manipulate the size field of 47# the free slot. 48# 49# Then use sqlite3_blob_write() to set the size of said free slot 50# to 24 bytes (instead of the actual 100). 51# 52# Then use the new 24 byte slot. Leaving the in-memory version of 53# the page with zero free slots and a large nFree value. Then try 54# to allocate another slot to get to defragmentPage(). 55# 56do_test 1.2 { 57 db close 58 hexio_write test.db [expr 1024 + 0x360] 21 59 hexio_write test.db [expr 1024 + 0x363] [format %x [expr 31*2 + 12]] 60 sqlite3 db test.db 61 62 set fd [db incrblob t1 x 3] 63 fconfigure $fd -translation binary -encoding binary 64 seek $fd 30 65 puts -nonewline $fd "\x18" 66 close $fd 67} {} 68do_execsql_test 1.3 { 69 INSERT INTO t1 VALUES(randomblob(20)); 70} 71do_catchsql_test 1.4 { 72 INSERT INTO t1 VALUES(randomblob(90)); 73} {1 {database disk image is malformed}} 74 75#------------------------------------------------------------------------- 76reset_db 77do_execsql_test 2.1 { 78 PRAGMA page_size=1024; 79 PRAGMA auto_vacuum=0; 80 CREATE TABLE t1(x); 81 82 INSERT INTO t1 VALUES(randomblob(20)); 83 INSERT INTO t1 VALUES(randomblob(20)); -- free this one 84 INSERT INTO t1 VALUES(randomblob(20)); 85 INSERT INTO t1 VALUES(randomblob(20)); -- and this one 86 INSERT INTO t1 VALUES(randomblob(20)); -- corrupt this one. 87 88 DELETE FROM t1 WHERE rowid IN(2, 4); 89 PRAGMA page_count 90} {2} 91 92do_test 2.2 { 93 db close 94 hexio_write test.db [expr 1024 + 0x388] 53 95 hexio_write test.db [expr 1024 + 0x38A] 03812C 96 97 sqlite3 db test.db 98 set fd [db incrblob t1 x 5] 99 fconfigure $fd -translation binary -encoding binary 100 101 seek $fd 22 102 puts -nonewline $fd "\x5d" 103 close $fd 104} {} 105 106do_catchsql_test 2.3 { 107 INSERT INTO t1 VALUES(randomblob(900)); 108} {1 {database disk image is malformed}} 109 110#------------------------------------------------------------------------- 111 112proc hex2blob {hex} { 113 # Split on newlines: 114 set bytes [list] 115 foreach l [split $hex "\n"] { 116 if {[string is space $l]} continue 117 set L [list] 118 foreach b [split $l] { 119 if {[string is xdigit $b] && [string length $b]==2} { 120 lappend L [expr "0x$b"] 121 } 122 } 123 if {[llength $L]!=16} { 124 error "Badly formed hex (1)" 125 } 126 set bytes [concat $bytes $L] 127 } 128 129 binary format c* $bytes 130} 131 132reset_db 133db func hex2blob hex2blob 134 135do_execsql_test 3.1 { 136 PRAGMA page_size=1024; 137 CREATE TABLE t1(a, b, c); 138 CREATE TABLE t2(a, b, c); 139 CREATE TABLE t3(a, b, c); 140 CREATE TABLE t4(a, b, c); 141 CREATE TABLE t5(a, b, c); 142} 143 144do_execsql_test 3.2 { 145 UPDATE sqlite_dbpage SET data = hex2blob(' 146 000: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. 147 010: 04 00 01 01 20 40 20 20 00 00 3e d9 00 00 00 06 .... @ ..>..... 148 020: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 04 ................ 149 030: 0f 00 00 00 00 00 00 00 00 00 00 01 00 00 83 00 ................ 150 040: 00 00 00 00 00 00 00 00 00 00 00 00 00 38 00 00 .............8.. 151 050: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 3e d9 ..............>. 152 060: 00 2d e6 07 0d 00 00 00 01 03 a0 00 03 e0 00 00 .-.............. 153 070: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 154 080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 155 090: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 156 0a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 157 0b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 158 0c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 159 0d0: 00 00 00 00 00 c1 00 00 00 00 00 00 00 00 00 00 ................ 160 0e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 161 0f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 162 100: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 163 110: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 164 120: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 165 130: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 166 140: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 167 150: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 168 160: 00 83 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 169 170: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 170 180: 00 00 00 00 00 00 00 00 00 00 07 00 30 00 00 00 ............0... 171 190: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 172 1a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 173 1b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 174 1c0: 02 00 00 00 00 00 00 00 00 00 00 02 00 00 00 00 ................ 175 1d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 176 1e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 177 1f0: 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 178 200: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 179 210: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 180 220: 00 00 0e 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 181 230: 0c 00 00 00 00 00 00 60 00 00 00 06 00 00 c3 00 .......`........ 182 240: 00 06 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 183 250: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 184 260: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 185 270: 00 00 00 18 00 00 00 00 00 00 00 00 00 00 00 00 ................ 186 280: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 187 290: 04 00 0e 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 188 2a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 189 2b0: 00 00 00 00 83 00 8c 00 00 00 00 00 00 00 00 00 ................ 190 2c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 191 2d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 192 2e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 193 2f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 194 300: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 195 310: 00 78 00 00 00 00 00 00 00 00 00 00 00 00 70 00 .x............p. 196 320: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 197 330: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 198 340: 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 ................ 199 350: 00 00 00 00 00 68 00 00 00 00 00 00 00 00 00 00 .....h.......... 200 360: 00 00 00 00 00 03 00 00 00 00 00 00 00 00 00 00 ................ 201 370: 00 00 00 00 00 00 00 00 00 00 00 00 00 08 00 00 ................ 202 380: 00 00 00 00 70 00 00 00 00 00 00 00 00 00 00 00 ....p........... 203 390: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 204 3a0: 5e 01 07 17 1b 1b 01 81 13 74 61 62 6c 65 73 65 ^........tablese 205 3b0: 6e 73 6f 32 73 73 65 6e 73 6f 72 73 02 43 52 45 nso2ssensors.CRE 206 3c0: 41 54 45 20 54 41 42 4c 45 20 73 65 6e 73 6f 72 ATE TABLE sensor 207 3d0: 73 20 0a 20 20 24 20 20 20 20 20 20 20 20 20 20 s . $ 208 3e0: b8 6e 61 6d 65 21 74 65 78 74 2c 20 79 61 6c 20 .name!text, yal 209 3f0: 72 65 61 6c 2c 20 74 69 6d 65 20 74 65 78 74 29 real, time text) 210 ') WHERE pgno=1 211} 212 213db close 214sqlite3 db test.db 215 216do_catchsql_test 3.3 { 217 PRAGMA integrity_check; 218} {1 {database disk image is malformed}} 219 220 221 222finish_test 223