AryaWu/sqlite
0
1# 2017-03-032#3# The author disclaims copyright to this source code. In place of4# 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.tcl15set testprefix corruptK16 17if {[permutation]=="mmap"} {18 finish_test19 return20}21 22# This module uses hard-coded offsets which do not work if the reserved_bytes23# value is nonzero.24if {[nonzero_reserved_bytes]} {finish_test; return;}25database_may_be_corrupt26 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 slot36 INSERT INTO t1 VALUES(randomblob(27)); -- this one will be corrupt37 INSERT INTO t1 VALUES(randomblob(800));38 39 DELETE FROM t1 WHERE rowid=2; -- free the 100 byte slot40 PRAGMA page_count41} {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 that46# we can use sqlite3_blob_write() to manipulate the size field of47# the free slot.48#49# Then use sqlite3_blob_write() to set the size of said free slot50# to 24 bytes (instead of the actual 100).51#52# Then use the new 24 byte slot. Leaving the in-memory version of53# the page with zero free slots and a large nFree value. Then try54# to allocate another slot to get to defragmentPage().55#56do_test 1.2 {57 db close58 hexio_write test.db [expr 1024 + 0x360] 2159 hexio_write test.db [expr 1024 + 0x363] [format %x [expr 31*2 + 12]]60 sqlite3 db test.db61 62 set fd [db incrblob t1 x 3]63 fconfigure $fd -translation binary64 seek $fd 3065 puts -nonewline $fd "\x18"66 close $fd67} {}68do_execsql_test 1.3 {69 INSERT INTO t1 VALUES(randomblob(20));70}71 72# This test no longer functions due to the deferred computation of73# MemPage.nFree.74#75if 0 {76do_catchsql_test 1.4 {77 INSERT INTO t1 VALUES(randomblob(90));78} {1 {database disk image is malformed}}79}80 81#-------------------------------------------------------------------------82reset_db83do_execsql_test 2.1 {84 PRAGMA page_size=1024;85 PRAGMA auto_vacuum=0;86 CREATE TABLE t1(x);87 88 INSERT INTO t1 VALUES(randomblob(20));89 INSERT INTO t1 VALUES(randomblob(20)); -- free this one90 INSERT INTO t1 VALUES(randomblob(20));91 INSERT INTO t1 VALUES(randomblob(20)); -- and this one92 INSERT INTO t1 VALUES(randomblob(20)); -- corrupt this one.93 94 DELETE FROM t1 WHERE rowid IN(2, 4);95 PRAGMA page_count96} {2}97 98do_test 2.2 {99 db close100 hexio_write test.db [expr 1024 + 0x388] 53101 hexio_write test.db [expr 1024 + 0x38A] 03812C102 103 sqlite3 db test.db104 set fd [db incrblob t1 x 5]105 fconfigure $fd -translation binary106 107 seek $fd 22108 puts -nonewline $fd "\x5d"109 close $fd110} {}111 112do_catchsql_test 2.3 {113 INSERT INTO t1 VALUES(randomblob(900));114} {1 {database disk image is malformed}}115 116#-------------------------------------------------------------------------117 118ifcapable vtab {119if {[permutation]!="inmemory_journal"} {120 121 proc hex2blob {hex} {122 # Split on newlines:123 set bytes [list]124 foreach l [split $hex "\n"] {125 if {[string is space $l]} continue126 set L [list]127 foreach b [split $l] {128 if {[string is xdigit $b] && [string length $b]==2} { 129 lappend L [expr "0x$b"]130 }131 }132 if {[llength $L]!=16} {133 error "Badly formed hex (1)"134 }135 set bytes [concat $bytes $L]136 }137 138 binary format c* $bytes139 }140 141 reset_db142 db func hex2blob hex2blob143 144 do_execsql_test 3.1 {145 PRAGMA page_size=1024;146 CREATE TABLE t1(a, b, c);147 CREATE TABLE t2(a, b, c);148 CREATE TABLE t3(a, b, c);149 CREATE TABLE t4(a, b, c);150 CREATE TABLE t5(a, b, c);151 }152 sqlite3_db_config db DEFENSIVE 0153 do_execsql_test 3.2 {154 UPDATE sqlite_dbpage SET data = hex2blob('155 000: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3.156 010: 04 00 01 01 20 40 20 20 00 00 3e d9 00 00 00 06 .... @ ..>.....157 020: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 04 ................158 030: 0f 00 00 00 00 00 00 00 00 00 00 01 00 00 83 00 ................159 040: 00 00 00 00 00 00 00 00 00 00 00 00 00 38 00 00 .............8..160 050: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 3e d9 ..............>.161 060: 00 2d e6 07 0d 00 00 00 01 03 a0 00 03 e0 00 00 .-..............162 070: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................163 080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................164 090: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................165 0a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................166 0b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................167 0c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................168 0d0: 00 00 00 00 00 c1 00 00 00 00 00 00 00 00 00 00 ................169 0e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................170 0f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................171 100: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................172 110: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................173 120: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................174 130: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................175 140: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................176 150: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................177 160: 00 83 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................178 170: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................179 180: 00 00 00 00 00 00 00 00 00 00 07 00 30 00 00 00 ............0...180 190: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................181 1a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................182 1b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................183 1c0: 02 00 00 00 00 00 00 00 00 00 00 02 00 00 00 00 ................184 1d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................185 1e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................186 1f0: 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................187 200: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................188 210: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................189 220: 00 00 0e 00 00 00 00 00 00 00 00 00 00 00 00 00 ................190 230: 0c 00 00 00 00 00 00 60 00 00 00 06 00 00 c3 00 .......`........191 240: 00 06 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................192 250: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................193 260: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................194 270: 00 00 00 18 00 00 00 00 00 00 00 00 00 00 00 00 ................195 280: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................196 290: 04 00 0e 00 00 00 00 00 00 00 00 00 00 00 00 00 ................197 2a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................198 2b0: 00 00 00 00 83 00 8c 00 00 00 00 00 00 00 00 00 ................199 2c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................200 2d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................201 2e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................202 2f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................203 300: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................204 310: 00 78 00 00 00 00 00 00 00 00 00 00 00 00 70 00 .x............p.205 320: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................206 330: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................207 340: 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 ................208 350: 00 00 00 00 00 68 00 00 00 00 00 00 00 00 00 00 .....h..........209 360: 00 00 00 00 00 03 00 00 00 00 00 00 00 00 00 00 ................210 370: 00 00 00 00 00 00 00 00 00 00 00 00 00 08 00 00 ................211 380: 00 00 00 00 70 00 00 00 00 00 00 00 00 00 00 00 ....p...........212 390: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................213 3a0: 5e 01 07 17 1b 1b 01 81 13 74 61 62 6c 65 73 65 ^........tablese214 3b0: 6e 73 6f 32 73 73 65 6e 73 6f 72 73 02 43 52 45 nso2ssensors.CRE215 3c0: 41 54 45 20 54 41 42 4c 45 20 73 65 6e 73 6f 72 ATE TABLE sensor216 3d0: 73 20 0a 20 20 24 20 20 20 20 20 20 20 20 20 20 s . $ 217 3e0: b8 6e 61 6d 65 21 74 65 78 74 2c 20 79 61 6c 20 .name!text, yal 218 3f0: 72 65 61 6c 2c 20 74 69 6d 65 20 74 65 78 74 29 real, time text)219 ') WHERE pgno=1220 }221 222 db close223 sqlite3 db test.db224 225 do_catchsql_test 3.3 {226 PRAGMA integrity_check;227 } {1 {database disk image is malformed}}228 229} ;# [permutation]!="inmemory_journal"230} ;# ifcapable vtab231 232 233 234finish_test235 