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));
71 do_catchsql_test 1.4 {
72 INSERT INTO t1 VALUES(randomblob(90));
73 } {1 {database disk image is malformed}}
75 #-------------------------------------------------------------------------
78 PRAGMA page_size=1024;
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.
88 DELETE FROM t1 WHERE rowid IN(2, 4);
94 hexio_write test.db [expr 1024 + 0x388] 53
95 hexio_write test.db [expr 1024 + 0x38A] 03812C
98 set fd [db incrblob t1 x 5]
99 fconfigure $fd -translation binary -encoding binary
102 puts -nonewline $fd "\x5d"
106 do_catchsql_test 2.3 {
107 INSERT INTO t1 VALUES(randomblob(900));
108 } {1 {database disk image is malformed}}
110 #-------------------------------------------------------------------------
113 if {[permutation]!="inmemory_journal"} {
115 proc hex2blob {hex} {
118 foreach l [split $hex "\n"] {
119 if {[string is space $l]} continue
121 foreach b [split $l] {
122 if {[string is xdigit $b] && [string length $b]==2} {
123 lappend L [expr "0x$b"]
126 if {[llength $L]!=16} {
127 error "Badly formed hex (1)"
129 set bytes [concat $bytes $L]
132 binary format c* $bytes
136 db func hex2blob hex2blob
138 do_execsql_test 3.1 {
139 PRAGMA page_size=1024;
140 CREATE TABLE t1(a, b, c);
141 CREATE TABLE t2(a, b, c);
142 CREATE TABLE t3(a, b, c);
143 CREATE TABLE t4(a, b, c);
144 CREATE TABLE t5(a, b, c);
147 do_execsql_test 3.2 {
148 UPDATE sqlite_dbpage SET data = hex2blob('
149 000: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3.
150 010: 04 00 01 01 20 40 20 20 00 00 3e d9 00 00 00 06 .... @ ..>.....
151 020: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 04 ................
152 030: 0f 00 00 00 00 00 00 00 00 00 00 01 00 00 83 00 ................
153 040: 00 00 00 00 00 00 00 00 00 00 00 00 00 38 00 00 .............8..
154 050: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 3e d9 ..............>.
155 060: 00 2d e6 07 0d 00 00 00 01 03 a0 00 03 e0 00 00 .-..............
156 070: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
157 080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
158 090: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
159 0a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
160 0b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
161 0c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
162 0d0: 00 00 00 00 00 c1 00 00 00 00 00 00 00 00 00 00 ................
163 0e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
164 0f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
165 100: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
166 110: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
167 120: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
168 130: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
169 140: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
170 150: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
171 160: 00 83 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
172 170: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
173 180: 00 00 00 00 00 00 00 00 00 00 07 00 30 00 00 00 ............0...
174 190: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
175 1a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
176 1b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
177 1c0: 02 00 00 00 00 00 00 00 00 00 00 02 00 00 00 00 ................
178 1d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
179 1e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
180 1f0: 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
181 200: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
182 210: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
183 220: 00 00 0e 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
184 230: 0c 00 00 00 00 00 00 60 00 00 00 06 00 00 c3 00 .......`........
185 240: 00 06 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
186 250: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
187 260: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
188 270: 00 00 00 18 00 00 00 00 00 00 00 00 00 00 00 00 ................
189 280: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
190 290: 04 00 0e 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
191 2a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
192 2b0: 00 00 00 00 83 00 8c 00 00 00 00 00 00 00 00 00 ................
193 2c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
194 2d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
195 2e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
196 2f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
197 300: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
198 310: 00 78 00 00 00 00 00 00 00 00 00 00 00 00 70 00 .x............p.
199 320: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
200 330: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
201 340: 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 ................
202 350: 00 00 00 00 00 68 00 00 00 00 00 00 00 00 00 00 .....h..........
203 360: 00 00 00 00 00 03 00 00 00 00 00 00 00 00 00 00 ................
204 370: 00 00 00 00 00 00 00 00 00 00 00 00 00 08 00 00 ................
205 380: 00 00 00 00 70 00 00 00 00 00 00 00 00 00 00 00 ....p...........
206 390: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
207 3a0: 5e 01 07 17 1b 1b 01 81 13 74 61 62 6c 65 73 65 ^........tablese
208 3b0: 6e 73 6f 32 73 73 65 6e 73 6f 72 73 02 43 52 45 nso2ssensors.CRE
209 3c0: 41 54 45 20 54 41 42 4c 45 20 73 65 6e 73 6f 72 ATE TABLE sensor
210 3d0: 73 20 0a 20 20 24 20 20 20 20 20 20 20 20 20 20 s . $
211 3e0: b8 6e 61 6d 65 21 74 65 78 74 2c 20 79 61 6c 20 .name!text, yal
212 3f0: 72 65 61 6c 2c 20 74 69 6d 65 20 74 65 78 74 29 real, time text)
219 do_catchsql_test 3.3 {
220 PRAGMA integrity_check;
221 } {1 {database disk image is malformed}}
223 } ;# [permutation]!="inmemory_journal"