3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
13 set testdir [file dirname $argv0]
14 source $testdir/tester.tcl
15 set testprefix corruptK
17 if {[permutation]=="mmap"} {
22 # This module uses hard-coded offsets which do not work if the reserved_bytes
24 if {[nonzero_reserved_bytes]} {finish_test; return;}
25 database_may_be_corrupt
27 # Initialize the database.
30 PRAGMA page_size=1024;
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));
39 DELETE FROM t1 WHERE rowid=2; -- free the 100 byte slot
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
49 # Then use sqlite3_blob_write() to set the size of said free slot
50 # to 24 bytes (instead of the actual 100).
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().
58 hexio_write test.db [expr 1024 + 0x360] 21
59 hexio_write test.db [expr 1024 + 0x363] [format %x [expr 31*2 + 12]]
62 set fd [db incrblob t1 x 3]
63 fconfigure $fd -translation binary -encoding binary
65 puts -nonewline $fd "\x18"
69 INSERT INTO t1 VALUES(randomblob(20));
72 # This test no longer functions due to the deferred computation of
76 do_catchsql_test 1.4 {
77 INSERT INTO t1 VALUES(randomblob(90));
78 } {1 {database disk image is malformed}}
81 #-------------------------------------------------------------------------
84 PRAGMA page_size=1024;
88 INSERT INTO t1 VALUES(randomblob(20));
89 INSERT INTO t1 VALUES(randomblob(20)); -- free this one
90 INSERT INTO t1 VALUES(randomblob(20));
91 INSERT INTO t1 VALUES(randomblob(20)); -- and this one
92 INSERT INTO t1 VALUES(randomblob(20)); -- corrupt this one.
94 DELETE FROM t1 WHERE rowid IN(2, 4);
100 hexio_write test.db [expr 1024 + 0x388] 53
101 hexio_write test.db [expr 1024 + 0x38A] 03812C
104 set fd [db incrblob t1 x 5]
105 fconfigure $fd -translation binary -encoding binary
108 puts -nonewline $fd "\x5d"
112 do_catchsql_test 2.3 {
113 INSERT INTO t1 VALUES(randomblob(900));
114 } {1 {database disk image is malformed}}
116 #-------------------------------------------------------------------------
119 if {[permutation]!="inmemory_journal"} {
121 proc hex2blob {hex} {
124 foreach l [split $hex "\n"] {
125 if {[string is space $l]} continue
127 foreach b [split $l] {
128 if {[string is xdigit $b] && [string length $b]==2} {
129 lappend L [expr "0x$b"]
132 if {[llength $L]!=16} {
133 error "Badly formed hex (1)"
135 set bytes [concat $bytes $L]
138 binary format c* $bytes
142 db func hex2blob hex2blob
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);
152 sqlite3_db_config db DEFENSIVE 0
153 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 ^........tablese
214 3b0: 6e 73 6f 32 73 73 65 6e 73 6f 72 73 02 43 52 45 nso2ssensors.CRE
215 3c0: 41 54 45 20 54 41 42 4c 45 20 73 65 6e 73 6f 72 ATE TABLE sensor
216 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)
225 do_catchsql_test 3.3 {
226 PRAGMA integrity_check;
227 } {1 {database disk image is malformed}}
229 } ;# [permutation]!="inmemory_journal"