OLD | NEW |
1 # 2004 August 30 | 1 # 2004 August 30 |
2 # | 2 # |
3 # The author disclaims copyright to this source code. In place of | 3 # The author disclaims copyright to this source code. In place of |
4 # a legal notice, here is a blessing: | 4 # a legal notice, here is a blessing: |
5 # | 5 # |
6 # May you do good and not evil. | 6 # May you do good and not evil. |
7 # May you find forgiveness for yourself and forgive others. | 7 # May you find forgiveness for yourself and forgive others. |
8 # May you share freely, never taking more than you give. | 8 # May you share freely, never taking more than you give. |
9 # | 9 # |
10 #*********************************************************************** | 10 #*********************************************************************** |
11 # This file implements regression tests for SQLite library. | 11 # This file implements regression tests for SQLite library. |
12 # | 12 # |
13 # This file implements tests to make sure SQLite does not crash or | 13 # This file implements tests to make sure SQLite does not crash or |
14 # segfault if it sees a corrupt database file. | 14 # segfault if it sees a corrupt database file. |
15 # | 15 # |
16 # $Id: corrupt2.test,v 1.20 2009/04/06 17:50:03 danielk1977 Exp $ | 16 # $Id: corrupt2.test,v 1.20 2009/04/06 17:50:03 danielk1977 Exp $ |
17 | 17 |
18 set testdir [file dirname $argv0] | 18 set testdir [file dirname $argv0] |
19 source $testdir/tester.tcl | 19 source $testdir/tester.tcl |
20 | 20 |
| 21 # Do not use a codec for tests in this file, as the database file is |
| 22 # manipulated directly using tcl scripts (using the [hexio_write] command). |
| 23 # |
| 24 do_not_use_codec |
| 25 |
| 26 set presql "" |
| 27 catch { set presql "$::G(perm:presql);" } |
| 28 unset -nocomplain ::G(perm:presql) |
| 29 |
21 # The following tests - corrupt2-1.* - create some databases corrupted in | 30 # The following tests - corrupt2-1.* - create some databases corrupted in |
22 # specific ways and ensure that SQLite detects them as corrupt. | 31 # specific ways and ensure that SQLite detects them as corrupt. |
23 # | 32 # |
24 do_test corrupt2-1.1 { | 33 do_test corrupt2-1.1 { |
25 execsql { | 34 execsql { |
26 PRAGMA auto_vacuum=0; | 35 PRAGMA auto_vacuum=0; |
27 PRAGMA page_size=1024; | 36 PRAGMA page_size=1024; |
28 CREATE TABLE abc(a, b, c); | 37 CREATE TABLE abc(a, b, c); |
29 } | 38 } |
30 } {} | 39 } {} |
31 | 40 |
32 do_test corrupt2-1.2 { | 41 do_test corrupt2-1.2 { |
33 | 42 |
34 # Corrupt the 16 byte magic string at the start of the file | 43 # Corrupt the 16 byte magic string at the start of the file |
35 file delete -force corrupt.db | 44 file delete -force corrupt.db |
36 file delete -force corrupt.db-journal | 45 file delete -force corrupt.db-journal |
37 copy_file test.db corrupt.db | 46 copy_file test.db corrupt.db |
38 set f [open corrupt.db RDWR] | 47 set f [open corrupt.db RDWR] |
39 seek $f 8 start | 48 seek $f 8 start |
40 puts $f blah | 49 puts $f blah |
41 close $f | 50 close $f |
42 | 51 |
43 sqlite3 db2 corrupt.db | 52 sqlite3 db2 corrupt.db |
44 catchsql { | 53 catchsql " |
| 54 $::presql |
45 SELECT * FROM sqlite_master; | 55 SELECT * FROM sqlite_master; |
46 } db2 | 56 " db2 |
47 } {1 {file is encrypted or is not a database}} | 57 } {1 {file is encrypted or is not a database}} |
48 | 58 |
49 do_test corrupt2-1.3 { | 59 do_test corrupt2-1.3 { |
50 db2 close | 60 db2 close |
51 | 61 |
52 # Corrupt the page-size (bytes 16 and 17 of page 1). | 62 # Corrupt the page-size (bytes 16 and 17 of page 1). |
53 file delete -force corrupt.db | 63 file delete -force corrupt.db |
54 file delete -force corrupt.db-journal | 64 file delete -force corrupt.db-journal |
55 copy_file test.db corrupt.db | 65 copy_file test.db corrupt.db |
56 set f [open corrupt.db RDWR] | 66 set f [open corrupt.db RDWR] |
57 fconfigure $f -encoding binary | 67 fconfigure $f -encoding binary |
58 seek $f 16 start | 68 seek $f 16 start |
59 puts -nonewline $f "\x00\xFF" | 69 puts -nonewline $f "\x00\xFF" |
60 close $f | 70 close $f |
61 | 71 |
62 sqlite3 db2 corrupt.db | 72 sqlite3 db2 corrupt.db |
63 catchsql { | 73 catchsql " |
| 74 $::presql |
64 SELECT * FROM sqlite_master; | 75 SELECT * FROM sqlite_master; |
65 } db2 | 76 " db2 |
66 } {1 {file is encrypted or is not a database}} | 77 } {1 {file is encrypted or is not a database}} |
67 | 78 |
68 do_test corrupt2-1.4 { | 79 do_test corrupt2-1.4 { |
69 db2 close | 80 db2 close |
70 | 81 |
71 # Corrupt the free-block list on page 1. | 82 # Corrupt the free-block list on page 1. |
72 file delete -force corrupt.db | 83 file delete -force corrupt.db |
73 file delete -force corrupt.db-journal | 84 file delete -force corrupt.db-journal |
74 copy_file test.db corrupt.db | 85 copy_file test.db corrupt.db |
75 set f [open corrupt.db RDWR] | 86 set f [open corrupt.db RDWR] |
76 fconfigure $f -encoding binary | 87 fconfigure $f -encoding binary |
77 seek $f 101 start | 88 seek $f 101 start |
78 puts -nonewline $f "\xFF\xFF" | 89 puts -nonewline $f "\xFF\xFF" |
79 close $f | 90 close $f |
80 | 91 |
81 sqlite3 db2 corrupt.db | 92 sqlite3 db2 corrupt.db |
82 catchsql { | 93 catchsql " |
| 94 $::presql |
83 SELECT * FROM sqlite_master; | 95 SELECT * FROM sqlite_master; |
84 } db2 | 96 " db2 |
85 } {1 {database disk image is malformed}} | 97 } {1 {database disk image is malformed}} |
86 | 98 |
87 do_test corrupt2-1.5 { | 99 do_test corrupt2-1.5 { |
88 db2 close | 100 db2 close |
89 | 101 |
90 # Corrupt the free-block list on page 1. | 102 # Corrupt the free-block list on page 1. |
91 file delete -force corrupt.db | 103 file delete -force corrupt.db |
92 file delete -force corrupt.db-journal | 104 file delete -force corrupt.db-journal |
93 copy_file test.db corrupt.db | 105 copy_file test.db corrupt.db |
94 set f [open corrupt.db RDWR] | 106 set f [open corrupt.db RDWR] |
95 fconfigure $f -encoding binary | 107 fconfigure $f -encoding binary |
96 seek $f 101 start | 108 seek $f 101 start |
97 puts -nonewline $f "\x00\xC8" | 109 puts -nonewline $f "\x00\xC8" |
98 seek $f 200 start | 110 seek $f 200 start |
99 puts -nonewline $f "\x00\x00" | 111 puts -nonewline $f "\x00\x00" |
100 puts -nonewline $f "\x10\x00" | 112 puts -nonewline $f "\x10\x00" |
101 close $f | 113 close $f |
102 | 114 |
103 sqlite3 db2 corrupt.db | 115 sqlite3 db2 corrupt.db |
104 catchsql { | 116 catchsql " |
| 117 $::presql |
105 SELECT * FROM sqlite_master; | 118 SELECT * FROM sqlite_master; |
106 } db2 | 119 " db2 |
107 } {1 {database disk image is malformed}} | 120 } {1 {database disk image is malformed}} |
108 db2 close | 121 db2 close |
109 | 122 |
110 # Corrupt a database by having 2 indices of the same name: | 123 # Corrupt a database by having 2 indices of the same name: |
111 do_test corrupt2-2.1 { | 124 do_test corrupt2-2.1 { |
112 | 125 |
113 file delete -force corrupt.db | 126 file delete -force corrupt.db |
114 file delete -force corrupt.db-journal | 127 file delete -force corrupt.db-journal |
115 copy_file test.db corrupt.db | 128 copy_file test.db corrupt.db |
116 | 129 |
117 sqlite3 db2 corrupt.db | 130 sqlite3 db2 corrupt.db |
118 execsql { | 131 execsql " |
| 132 $::presql |
119 CREATE INDEX a1 ON abc(a); | 133 CREATE INDEX a1 ON abc(a); |
120 CREATE INDEX a2 ON abc(b); | 134 CREATE INDEX a2 ON abc(b); |
121 PRAGMA writable_schema = 1; | 135 PRAGMA writable_schema = 1; |
122 UPDATE sqlite_master | 136 UPDATE sqlite_master |
123 SET name = 'a3', sql = 'CREATE INDEX a3' || substr(sql, 16, 10000) | 137 SET name = 'a3', sql = 'CREATE INDEX a3' || substr(sql, 16, 10000) |
124 WHERE type = 'index'; | 138 WHERE type = 'index'; |
125 PRAGMA writable_schema = 0; | 139 PRAGMA writable_schema = 0; |
126 } db2 | 140 " db2 |
127 | 141 |
128 db2 close | 142 db2 close |
129 sqlite3 db2 corrupt.db | 143 sqlite3 db2 corrupt.db |
130 catchsql { | 144 catchsql " |
| 145 $::presql |
131 SELECT * FROM sqlite_master; | 146 SELECT * FROM sqlite_master; |
132 } db2 | 147 " db2 |
133 } {1 {malformed database schema (a3) - index a3 already exists}} | 148 } {1 {malformed database schema (a3) - index a3 already exists}} |
134 | 149 |
135 db2 close | 150 db2 close |
136 | 151 |
137 do_test corrupt2-3.1 { | 152 do_test corrupt2-3.1 { |
138 file delete -force corrupt.db | 153 file delete -force corrupt.db |
139 file delete -force corrupt.db-journal | 154 file delete -force corrupt.db-journal |
140 sqlite3 db2 corrupt.db | 155 sqlite3 db2 corrupt.db |
141 | 156 |
142 execsql { | 157 execsql " |
| 158 $::presql |
143 PRAGMA auto_vacuum = 1; | 159 PRAGMA auto_vacuum = 1; |
144 PRAGMA page_size = 1024; | 160 PRAGMA page_size = 1024; |
145 CREATE TABLE t1(a, b, c); | 161 CREATE TABLE t1(a, b, c); |
146 CREATE TABLE t2(a, b, c); | 162 CREATE TABLE t2(a, b, c); |
147 INSERT INTO t2 VALUES(randomblob(100), randomblob(100), randomblob(100)); | 163 INSERT INTO t2 VALUES(randomblob(100), randomblob(100), randomblob(100)); |
148 INSERT INTO t2 SELECT * FROM t2; | 164 INSERT INTO t2 SELECT * FROM t2; |
149 INSERT INTO t2 SELECT * FROM t2; | 165 INSERT INTO t2 SELECT * FROM t2; |
150 INSERT INTO t2 SELECT * FROM t2; | 166 INSERT INTO t2 SELECT * FROM t2; |
151 INSERT INTO t2 SELECT * FROM t2; | 167 INSERT INTO t2 SELECT * FROM t2; |
152 } db2 | 168 " db2 |
153 | 169 |
154 db2 close | 170 db2 close |
155 | 171 |
156 # On the root page of table t2 (page 4), set one of the child page-numbers | 172 # On the root page of table t2 (page 4), set one of the child page-numbers |
157 # to 0. This corruption will be detected when SQLite attempts to update | 173 # to 0. This corruption will be detected when SQLite attempts to update |
158 # the pointer-map after moving the content of page 4 to page 3 as part | 174 # the pointer-map after moving the content of page 4 to page 3 as part |
159 # of the DROP TABLE operation below. | 175 # of the DROP TABLE operation below. |
160 # | 176 # |
161 set fd [open corrupt.db r+] | 177 set fd [open corrupt.db r+] |
162 fconfigure $fd -encoding binary -translation binary | 178 fconfigure $fd -encoding binary -translation binary |
163 seek $fd [expr 1024*3 + 12] | 179 seek $fd [expr 1024*3 + 12] |
164 set zCelloffset [read $fd 2] | 180 set zCelloffset [read $fd 2] |
165 binary scan $zCelloffset S iCelloffset | 181 binary scan $zCelloffset S iCelloffset |
166 seek $fd [expr 1024*3 + $iCelloffset] | 182 seek $fd [expr 1024*3 + $iCelloffset] |
167 puts -nonewline $fd "\00\00\00\00" | 183 puts -nonewline $fd "\00\00\00\00" |
168 close $fd | 184 close $fd |
169 | 185 |
170 sqlite3 db2 corrupt.db | 186 sqlite3 db2 corrupt.db |
171 catchsql { | 187 catchsql " |
| 188 $::presql |
172 DROP TABLE t1; | 189 DROP TABLE t1; |
173 } db2 | 190 " db2 |
174 } {1 {database disk image is malformed}} | 191 } {1 {database disk image is malformed}} |
175 | 192 |
176 do_test corrupt2-4.1 { | 193 do_test corrupt2-4.1 { |
177 catchsql { | 194 catchsql { |
178 SELECT * FROM t2; | 195 SELECT * FROM t2; |
179 } db2 | 196 } db2 |
180 } {1 {database disk image is malformed}} | 197 } {1 {database disk image is malformed}} |
181 | 198 |
182 db2 close | 199 db2 close |
183 | 200 |
184 unset -nocomplain result | 201 unset -nocomplain result |
185 do_test corrupt2-5.1 { | 202 do_test corrupt2-5.1 { |
186 file delete -force corrupt.db | 203 file delete -force corrupt.db |
187 file delete -force corrupt.db-journal | 204 file delete -force corrupt.db-journal |
188 sqlite3 db2 corrupt.db | 205 sqlite3 db2 corrupt.db |
189 | 206 |
190 execsql { | 207 execsql " |
| 208 $::presql |
191 PRAGMA auto_vacuum = 0; | 209 PRAGMA auto_vacuum = 0; |
192 PRAGMA page_size = 1024; | 210 PRAGMA page_size = 1024; |
193 CREATE TABLE t1(a, b, c); | 211 CREATE TABLE t1(a, b, c); |
194 CREATE TABLE t2(a, b, c); | 212 CREATE TABLE t2(a, b, c); |
195 INSERT INTO t2 VALUES(randomblob(100), randomblob(100), randomblob(100)); | 213 INSERT INTO t2 VALUES(randomblob(100), randomblob(100), randomblob(100)); |
196 INSERT INTO t2 SELECT * FROM t2; | 214 INSERT INTO t2 SELECT * FROM t2; |
197 INSERT INTO t2 SELECT * FROM t2; | 215 INSERT INTO t2 SELECT * FROM t2; |
198 INSERT INTO t2 SELECT * FROM t2; | 216 INSERT INTO t2 SELECT * FROM t2; |
199 INSERT INTO t2 SELECT * FROM t2; | 217 INSERT INTO t2 SELECT * FROM t2; |
200 INSERT INTO t1 SELECT * FROM t2; | 218 INSERT INTO t1 SELECT * FROM t2; |
201 } db2 | 219 " db2 |
202 | 220 |
203 db2 close | 221 db2 close |
204 | 222 |
205 # This block links a page from table t2 into the t1 table structure. | 223 # This block links a page from table t2 into the t1 table structure. |
206 # | 224 # |
207 set fd [open corrupt.db r+] | 225 set fd [open corrupt.db r+] |
208 fconfigure $fd -encoding binary -translation binary | 226 fconfigure $fd -encoding binary -translation binary |
209 seek $fd [expr 1024 + 12] | 227 seek $fd [expr 1024 + 12] |
210 set zCelloffset [read $fd 2] | 228 set zCelloffset [read $fd 2] |
211 binary scan $zCelloffset S iCelloffset | 229 binary scan $zCelloffset S iCelloffset |
212 seek $fd [expr 1024 + $iCelloffset] | 230 seek $fd [expr 1024 + $iCelloffset] |
213 set zChildPage [read $fd 4] | 231 set zChildPage [read $fd 4] |
214 seek $fd [expr 2*1024 + 12] | 232 seek $fd [expr 2*1024 + 12] |
215 set zCelloffset [read $fd 2] | 233 set zCelloffset [read $fd 2] |
216 binary scan $zCelloffset S iCelloffset | 234 binary scan $zCelloffset S iCelloffset |
217 seek $fd [expr 2*1024 + $iCelloffset] | 235 seek $fd [expr 2*1024 + $iCelloffset] |
218 puts -nonewline $fd $zChildPage | 236 puts -nonewline $fd $zChildPage |
219 close $fd | 237 close $fd |
220 | 238 |
221 sqlite3 db2 corrupt.db | 239 sqlite3 db2 corrupt.db |
| 240 db2 eval $::presql |
222 db2 eval {SELECT rowid FROM t1} { | 241 db2 eval {SELECT rowid FROM t1} { |
223 set result [db2 eval {pragma integrity_check}] | 242 set result [db2 eval {pragma integrity_check}] |
224 break | 243 break |
225 } | 244 } |
226 set result | 245 set result |
227 } {{*** in database main *** | 246 } {{*** in database main *** |
228 On tree page 2 cell 0: 2nd reference to page 10 | 247 On tree page 2 cell 0: 2nd reference to page 10 |
229 On tree page 2 cell 1: Child page depth differs | 248 On tree page 2 cell 1: Child page depth differs |
230 Page 4 is never used}} | 249 Page 4 is never used}} |
231 | 250 |
232 db2 close | 251 db2 close |
233 | 252 |
234 proc corruption_test {args} { | 253 proc corruption_test {args} { |
235 set A(-corrupt) {} | 254 set A(-corrupt) {} |
236 set A(-sqlprep) {} | 255 set A(-sqlprep) {} |
237 set A(-tclprep) {} | 256 set A(-tclprep) {} |
238 array set A $args | 257 array set A $args |
239 | 258 |
240 catch {db close} | 259 catch {db close} |
241 file delete -force corrupt.db | 260 file delete -force corrupt.db |
242 file delete -force corrupt.db-journal | 261 file delete -force corrupt.db-journal |
243 | 262 |
244 sqlite3 db corrupt.db | 263 sqlite3 db corrupt.db |
| 264 db eval $::presql |
245 eval $A(-tclprep) | 265 eval $A(-tclprep) |
246 db eval $A(-sqlprep) | 266 db eval $A(-sqlprep) |
247 db close | 267 db close |
248 | 268 |
249 eval $A(-corrupt) | 269 eval $A(-corrupt) |
250 | 270 |
251 sqlite3 db corrupt.db | 271 sqlite3 db corrupt.db |
252 eval $A(-test) | 272 eval $A(-test) |
253 } | 273 } |
254 | 274 |
(...skipping 14 matching lines...) Expand all Loading... |
269 PRAGMA auto_vacuum = incremental; | 289 PRAGMA auto_vacuum = incremental; |
270 PRAGMA page_size = 1024; | 290 PRAGMA page_size = 1024; |
271 CREATE TABLE t1(a, b); | 291 CREATE TABLE t1(a, b); |
272 INSERT INTO t1 VALUES(1, randomblob(2500)); | 292 INSERT INTO t1 VALUES(1, randomblob(2500)); |
273 INSERT INTO t1 VALUES(2, randomblob(2500)); | 293 INSERT INTO t1 VALUES(2, randomblob(2500)); |
274 DELETE FROM t1 WHERE a = 1; | 294 DELETE FROM t1 WHERE a = 1; |
275 } -corrupt { | 295 } -corrupt { |
276 hexio_write corrupt.db [expr 1024*5] 00000008 | 296 hexio_write corrupt.db [expr 1024*5] 00000008 |
277 } -test { | 297 } -test { |
278 do_test corrupt2-6.1 { | 298 do_test corrupt2-6.1 { |
279 catchsql { pragma incremental_vacuum = 1 } | 299 catchsql " $::presql pragma incremental_vacuum = 1 " |
280 } {1 {database disk image is malformed}} | 300 } {1 {database disk image is malformed}} |
281 } | 301 } |
282 | 302 |
283 # The last page in the database page is a non-root b-tree page. | 303 # The last page in the database page is a non-root b-tree page. |
284 # | 304 # |
285 corruption_test -sqlprep { | 305 corruption_test -sqlprep { |
286 PRAGMA auto_vacuum = incremental; | 306 PRAGMA auto_vacuum = incremental; |
287 PRAGMA page_size = 1024; | 307 PRAGMA page_size = 1024; |
288 CREATE TABLE t1(a INTEGER PRIMARY KEY, b); | 308 CREATE TABLE t1(a INTEGER PRIMARY KEY, b); |
289 INSERT INTO t1 VALUES(1, randomblob(2500)); | 309 INSERT INTO t1 VALUES(1, randomblob(2500)); |
290 INSERT INTO t1 VALUES(2, randomblob(50)); | 310 INSERT INTO t1 VALUES(2, randomblob(50)); |
291 INSERT INTO t1 SELECT NULL, randomblob(50) FROM t1; | 311 INSERT INTO t1 SELECT NULL, randomblob(50) FROM t1; |
292 INSERT INTO t1 SELECT NULL, randomblob(50) FROM t1; | 312 INSERT INTO t1 SELECT NULL, randomblob(50) FROM t1; |
293 INSERT INTO t1 SELECT NULL, randomblob(50) FROM t1; | 313 INSERT INTO t1 SELECT NULL, randomblob(50) FROM t1; |
294 INSERT INTO t1 SELECT NULL, randomblob(50) FROM t1; | 314 INSERT INTO t1 SELECT NULL, randomblob(50) FROM t1; |
295 DELETE FROM t1 WHERE a = 1; | 315 DELETE FROM t1 WHERE a = 1; |
296 } -corrupt { | 316 } -corrupt { |
297 hexio_write corrupt.db [expr 1024*2 + 8] 00000009 | 317 hexio_write corrupt.db [expr 1024*2 + 8] 00000009 |
298 } -test { | 318 } -test { |
299 do_test corrupt2-6.2 { | 319 do_test corrupt2-6.2 { |
300 catchsql { pragma incremental_vacuum = 1 } | 320 catchsql " $::presql pragma incremental_vacuum = 1 " |
301 } {1 {database disk image is malformed}} | 321 } {1 {database disk image is malformed}} |
302 } | 322 } |
303 | 323 |
304 # Set up a pointer-map entry so that the last page of the database | 324 # Set up a pointer-map entry so that the last page of the database |
305 # file appears to be a b-tree root page. This should be detected | 325 # file appears to be a b-tree root page. This should be detected |
306 # as corruption. | 326 # as corruption. |
307 # | 327 # |
308 corruption_test -sqlprep { | 328 corruption_test -sqlprep { |
309 PRAGMA auto_vacuum = incremental; | 329 PRAGMA auto_vacuum = incremental; |
310 PRAGMA page_size = 1024; | 330 PRAGMA page_size = 1024; |
311 CREATE TABLE t1(a INTEGER PRIMARY KEY, b); | 331 CREATE TABLE t1(a INTEGER PRIMARY KEY, b); |
312 INSERT INTO t1 VALUES(1, randomblob(2500)); | 332 INSERT INTO t1 VALUES(1, randomblob(2500)); |
313 INSERT INTO t1 VALUES(2, randomblob(2500)); | 333 INSERT INTO t1 VALUES(2, randomblob(2500)); |
314 INSERT INTO t1 VALUES(3, randomblob(2500)); | 334 INSERT INTO t1 VALUES(3, randomblob(2500)); |
315 DELETE FROM t1 WHERE a = 1; | 335 DELETE FROM t1 WHERE a = 1; |
316 } -corrupt { | 336 } -corrupt { |
317 set nPage [expr [file size corrupt.db] / 1024] | 337 set nPage [expr [file size corrupt.db] / 1024] |
318 hexio_write corrupt.db [expr 1024 + ($nPage-3)*5] 010000000 | 338 hexio_write corrupt.db [expr 1024 + ($nPage-3)*5] 010000000 |
319 } -test { | 339 } -test { |
320 do_test corrupt2-6.3 { | 340 do_test corrupt2-6.3 { |
321 catchsql { pragma incremental_vacuum = 1 } | 341 catchsql " $::presql pragma incremental_vacuum = 1 " |
322 } {1 {database disk image is malformed}} | 342 } {1 {database disk image is malformed}} |
323 } | 343 } |
324 | 344 |
325 corruption_test -sqlprep { | 345 corruption_test -sqlprep { |
326 PRAGMA auto_vacuum = 1; | 346 PRAGMA auto_vacuum = 1; |
327 PRAGMA page_size = 1024; | 347 PRAGMA page_size = 1024; |
328 CREATE TABLE t1(a INTEGER PRIMARY KEY, b); | 348 CREATE TABLE t1(a INTEGER PRIMARY KEY, b); |
329 INSERT INTO t1 VALUES(1, randomblob(2500)); | 349 INSERT INTO t1 VALUES(1, randomblob(2500)); |
330 DELETE FROM t1 WHERE a = 1; | 350 DELETE FROM t1 WHERE a = 1; |
331 } -corrupt { | 351 } -corrupt { |
332 set nAppend [expr 1024*207 - [file size corrupt.db]] | 352 set nAppend [expr 1024*207 - [file size corrupt.db]] |
333 set fd [open corrupt.db r+] | 353 set fd [open corrupt.db r+] |
334 seek $fd 0 end | 354 seek $fd 0 end |
335 puts -nonewline $fd [string repeat x $nAppend] | 355 puts -nonewline $fd [string repeat x $nAppend] |
336 close $fd | 356 close $fd |
| 357 hexio_write corrupt.db 28 00000000 |
337 } -test { | 358 } -test { |
338 do_test corrupt2-6.4 { | 359 do_test corrupt2-6.4 { |
339 catchsql { | 360 catchsql " |
| 361 $::presql |
340 BEGIN EXCLUSIVE; | 362 BEGIN EXCLUSIVE; |
341 COMMIT; | 363 COMMIT; |
342 } | 364 " |
343 } {1 {database disk image is malformed}} | 365 } {1 {database disk image is malformed}} |
344 } | 366 } |
345 } | 367 } |
346 | 368 |
347 | 369 |
348 set sqlprep { | 370 set sqlprep { |
349 PRAGMA auto_vacuum = 0; | 371 PRAGMA auto_vacuum = 0; |
350 PRAGMA page_size = 1024; | 372 PRAGMA page_size = 1024; |
351 CREATE TABLE t1(a INTEGER PRIMARY KEY, b); | 373 CREATE TABLE t1(a INTEGER PRIMARY KEY, b); |
352 CREATE INDEX i1 ON t1(b); | 374 CREATE INDEX i1 ON t1(b); |
(...skipping 13 matching lines...) Expand all Loading... |
366 set fd [open corrupt.db r+] | 388 set fd [open corrupt.db r+] |
367 fconfigure $fd -translation binary -encoding binary | 389 fconfigure $fd -translation binary -encoding binary |
368 seek $fd [expr 1024*2 + 8] | 390 seek $fd [expr 1024*2 + 8] |
369 set zRightChild [read $fd 4] | 391 set zRightChild [read $fd 4] |
370 binary scan $zRightChild I iRightChild | 392 binary scan $zRightChild I iRightChild |
371 seek $fd [expr 1024*($iRightChild-1)] | 393 seek $fd [expr 1024*($iRightChild-1)] |
372 puts -nonewline $fd "\x0D" | 394 puts -nonewline $fd "\x0D" |
373 close $fd | 395 close $fd |
374 } -test { | 396 } -test { |
375 do_test corrupt2-7.1 { | 397 do_test corrupt2-7.1 { |
376 catchsql { SELECT b FROM t1 ORDER BY b ASC } | 398 catchsql " $::presql SELECT b FROM t1 ORDER BY b ASC " |
377 } {1 {database disk image is malformed}} | 399 } {1 {database disk image is malformed}} |
378 } | 400 } |
379 | 401 |
380 corruption_test -sqlprep $sqlprep -corrupt { | 402 corruption_test -sqlprep $sqlprep -corrupt { |
381 # Mess up the page-header of one of the leaf pages of the index B-Tree. | 403 # Mess up the page-header of one of the leaf pages of the index B-Tree. |
382 # The corruption is detected as part of an OP_Prev opcode. | 404 # The corruption is detected as part of an OP_Prev opcode. |
383 # | 405 # |
384 set fd [open corrupt.db r+] | 406 set fd [open corrupt.db r+] |
385 fconfigure $fd -translation binary -encoding binary | 407 fconfigure $fd -translation binary -encoding binary |
386 seek $fd [expr 1024*2 + 12] | 408 seek $fd [expr 1024*2 + 12] |
387 set zCellOffset [read $fd 2] | 409 set zCellOffset [read $fd 2] |
388 binary scan $zCellOffset S iCellOffset | 410 binary scan $zCellOffset S iCellOffset |
389 seek $fd [expr 1024*2 + $iCellOffset] | 411 seek $fd [expr 1024*2 + $iCellOffset] |
390 set zChild [read $fd 4] | 412 set zChild [read $fd 4] |
391 binary scan $zChild I iChild | 413 binary scan $zChild I iChild |
392 seek $fd [expr 1024*($iChild-1)+3] | 414 seek $fd [expr 1024*($iChild-1)+3] |
393 puts -nonewline $fd "\xFFFF" | 415 puts -nonewline $fd "\xFFFF" |
394 close $fd | 416 close $fd |
395 } -test { | 417 } -test { |
396 do_test corrupt2-7.1 { | 418 do_test corrupt2-7.1 { |
397 catchsql { SELECT b FROM t1 ORDER BY b DESC } | 419 catchsql " $::presql SELECT b FROM t1 ORDER BY b DESC " |
398 } {1 {database disk image is malformed}} | 420 } {1 {database disk image is malformed}} |
399 } | 421 } |
400 | 422 |
401 corruption_test -sqlprep $sqlprep -corrupt { | 423 corruption_test -sqlprep $sqlprep -corrupt { |
402 # Set the page-flags of one of the leaf pages of the table B-Tree to | 424 # Set the page-flags of one of the leaf pages of the table B-Tree to |
403 # 0x0A (interpreted by SQLite as "leaf page of an index B-Tree"). | 425 # 0x0A (interpreted by SQLite as "leaf page of an index B-Tree"). |
404 # | 426 # |
405 set fd [open corrupt.db r+] | 427 set fd [open corrupt.db r+] |
406 fconfigure $fd -translation binary -encoding binary | 428 fconfigure $fd -translation binary -encoding binary |
407 seek $fd [expr 1024*1 + 8] | 429 seek $fd [expr 1024*1 + 8] |
408 set zRightChild [read $fd 4] | 430 set zRightChild [read $fd 4] |
409 binary scan $zRightChild I iRightChild | 431 binary scan $zRightChild I iRightChild |
410 seek $fd [expr 1024*($iRightChild-1)] | 432 seek $fd [expr 1024*($iRightChild-1)] |
411 puts -nonewline $fd "\x0A" | 433 puts -nonewline $fd "\x0A" |
412 close $fd | 434 close $fd |
413 } -test { | 435 } -test { |
414 do_test corrupt2-8.1 { | 436 do_test corrupt2-8.1 { |
415 catchsql { SELECT * FROM t1 WHERE rowid=1000 } | 437 catchsql " $::presql SELECT * FROM t1 WHERE rowid=1000 " |
416 } {1 {database disk image is malformed}} | 438 } {1 {database disk image is malformed}} |
417 } | 439 } |
418 | 440 |
419 corruption_test -sqlprep { | 441 corruption_test -sqlprep { |
420 CREATE TABLE t1(a, b, c); CREATE TABLE t8(a, b, c); CREATE TABLE tE(a, b, c); | 442 CREATE TABLE t1(a, b, c); CREATE TABLE t8(a, b, c); CREATE TABLE tE(a, b, c); |
421 CREATE TABLE t2(a, b, c); CREATE TABLE t9(a, b, c); CREATE TABLE tF(a, b, c); | 443 CREATE TABLE t2(a, b, c); CREATE TABLE t9(a, b, c); CREATE TABLE tF(a, b, c); |
422 CREATE TABLE t3(a, b, c); CREATE TABLE tA(a, b, c); CREATE TABLE tG(a, b, c); | 444 CREATE TABLE t3(a, b, c); CREATE TABLE tA(a, b, c); CREATE TABLE tG(a, b, c); |
423 CREATE TABLE t4(a, b, c); CREATE TABLE tB(a, b, c); CREATE TABLE tH(a, b, c); | 445 CREATE TABLE t4(a, b, c); CREATE TABLE tB(a, b, c); CREATE TABLE tH(a, b, c); |
424 CREATE TABLE t5(a, b, c); CREATE TABLE tC(a, b, c); CREATE TABLE tI(a, b, c); | 446 CREATE TABLE t5(a, b, c); CREATE TABLE tC(a, b, c); CREATE TABLE tI(a, b, c); |
425 CREATE TABLE t6(a, b, c); CREATE TABLE tD(a, b, c); CREATE TABLE tJ(a, b, c); | 447 CREATE TABLE t6(a, b, c); CREATE TABLE tD(a, b, c); CREATE TABLE tJ(a, b, c); |
426 CREATE TABLE x1(a, b, c); CREATE TABLE x8(a, b, c); CREATE TABLE xE(a, b, c); | 448 CREATE TABLE x1(a, b, c); CREATE TABLE x8(a, b, c); CREATE TABLE xE(a, b, c); |
427 CREATE TABLE x2(a, b, c); CREATE TABLE x9(a, b, c); CREATE TABLE xF(a, b, c); | 449 CREATE TABLE x2(a, b, c); CREATE TABLE x9(a, b, c); CREATE TABLE xF(a, b, c); |
428 CREATE TABLE x3(a, b, c); CREATE TABLE xA(a, b, c); CREATE TABLE xG(a, b, c); | 450 CREATE TABLE x3(a, b, c); CREATE TABLE xA(a, b, c); CREATE TABLE xG(a, b, c); |
429 CREATE TABLE x4(a, b, c); CREATE TABLE xB(a, b, c); CREATE TABLE xH(a, b, c); | 451 CREATE TABLE x4(a, b, c); CREATE TABLE xB(a, b, c); CREATE TABLE xH(a, b, c); |
430 CREATE TABLE x5(a, b, c); CREATE TABLE xC(a, b, c); CREATE TABLE xI(a, b, c); | 452 CREATE TABLE x5(a, b, c); CREATE TABLE xC(a, b, c); CREATE TABLE xI(a, b, c); |
431 CREATE TABLE x6(a, b, c); CREATE TABLE xD(a, b, c); CREATE TABLE xJ(a, b, c); | 453 CREATE TABLE x6(a, b, c); CREATE TABLE xD(a, b, c); CREATE TABLE xJ(a, b, c); |
432 } -corrupt { | 454 } -corrupt { |
433 set fd [open corrupt.db r+] | 455 set fd [open corrupt.db r+] |
434 fconfigure $fd -translation binary -encoding binary | 456 fconfigure $fd -translation binary -encoding binary |
435 seek $fd 108 | 457 seek $fd 108 |
436 set zRightChild [read $fd 4] | 458 set zRightChild [read $fd 4] |
437 binary scan $zRightChild I iRightChild | 459 binary scan $zRightChild I iRightChild |
438 seek $fd [expr 1024*($iRightChild-1)+3] | 460 seek $fd [expr 1024*($iRightChild-1)+3] |
439 puts -nonewline $fd "\x00\x00" | 461 puts -nonewline $fd "\x00\x00" |
440 close $fd | 462 close $fd |
441 } -test { | 463 } -test { |
442 do_test corrupt2-9.1 { | 464 do_test corrupt2-9.1 { |
443 catchsql { SELECT sql FROM sqlite_master } | 465 catchsql " $::presql SELECT sql FROM sqlite_master " |
444 } {1 {database disk image is malformed}} | 466 } {1 {database disk image is malformed}} |
445 } | 467 } |
446 | 468 |
447 corruption_test -sqlprep { | 469 corruption_test -sqlprep { |
448 CREATE TABLE t1(a, b, c); | 470 CREATE TABLE t1(a, b, c); |
449 CREATE TABLE t2(a, b, c); | 471 CREATE TABLE t2(a, b, c); |
450 PRAGMA writable_schema = 1; | 472 PRAGMA writable_schema = 1; |
451 UPDATE sqlite_master SET rootpage = NULL WHERE name = 't2'; | 473 UPDATE sqlite_master SET rootpage = NULL WHERE name = 't2'; |
452 } -test { | 474 } -test { |
453 do_test corrupt2-10.1 { | 475 do_test corrupt2-10.1 { |
454 catchsql { SELECT * FROM t2 } | 476 catchsql " $::presql SELECT * FROM t2 " |
455 } {1 {malformed database schema (t2)}} | 477 } {1 {malformed database schema (t2)}} |
456 do_test corrupt2-10.2 { | 478 do_test corrupt2-10.2 { |
457 sqlite3_errcode db | 479 sqlite3_errcode db |
458 } {SQLITE_CORRUPT} | 480 } {SQLITE_CORRUPT} |
459 } | 481 } |
460 | 482 |
461 corruption_test -sqlprep { | 483 corruption_test -sqlprep { |
462 PRAGMA auto_vacuum = incremental; | 484 PRAGMA auto_vacuum = incremental; |
463 CREATE TABLE t1(a INTEGER PRIMARY KEY, b); | 485 CREATE TABLE t1(a INTEGER PRIMARY KEY, b); |
464 CREATE TABLE t2(a INTEGER PRIMARY KEY, b); | 486 CREATE TABLE t2(a INTEGER PRIMARY KEY, b); |
465 INSERT INTO t1 VALUES(1, randstr(100,100)); | 487 INSERT INTO t1 VALUES(1, randstr(100,100)); |
466 INSERT INTO t1 SELECT NULL, randstr(100,100) FROM t1; | 488 INSERT INTO t1 SELECT NULL, randstr(100,100) FROM t1; |
467 INSERT INTO t1 SELECT NULL, randstr(100,100) FROM t1; | 489 INSERT INTO t1 SELECT NULL, randstr(100,100) FROM t1; |
468 INSERT INTO t1 SELECT NULL, randstr(100,100) FROM t1; | 490 INSERT INTO t1 SELECT NULL, randstr(100,100) FROM t1; |
469 INSERT INTO t1 SELECT NULL, randstr(100,100) FROM t1; | 491 INSERT INTO t1 SELECT NULL, randstr(100,100) FROM t1; |
470 INSERT INTO t1 SELECT NULL, randstr(100,100) FROM t1; | 492 INSERT INTO t1 SELECT NULL, randstr(100,100) FROM t1; |
471 INSERT INTO t2 SELECT * FROM t1; | 493 INSERT INTO t2 SELECT * FROM t1; |
472 DELETE FROM t1; | 494 DELETE FROM t1; |
473 } -corrupt { | 495 } -corrupt { |
474 set offset [expr [file size corrupt.db] - 1024] | 496 set offset [expr [file size corrupt.db] - 1024] |
475 hexio_write corrupt.db $offset FF | 497 hexio_write corrupt.db $offset FF |
476 hexio_write corrupt.db 24 12345678 | 498 hexio_write corrupt.db 24 12345678 |
477 } -test { | 499 } -test { |
478 do_test corrupt2-11.1 { | 500 do_test corrupt2-11.1 { |
479 catchsql { PRAGMA incremental_vacuum } | 501 catchsql " $::presql PRAGMA incremental_vacuum " |
480 } {1 {database disk image is malformed}} | 502 } {1 {database disk image is malformed}} |
481 } | 503 } |
482 corruption_test -sqlprep { | 504 corruption_test -sqlprep { |
483 PRAGMA auto_vacuum = incremental; | 505 PRAGMA auto_vacuum = incremental; |
484 CREATE TABLE t1(a INTEGER PRIMARY KEY, b); | 506 CREATE TABLE t1(a INTEGER PRIMARY KEY, b); |
485 CREATE TABLE t2(a INTEGER PRIMARY KEY, b); | 507 CREATE TABLE t2(a INTEGER PRIMARY KEY, b); |
486 INSERT INTO t1 VALUES(1, randstr(100,100)); | 508 INSERT INTO t1 VALUES(1, randstr(100,100)); |
487 INSERT INTO t1 SELECT NULL, randstr(100,100) FROM t1; | 509 INSERT INTO t1 SELECT NULL, randstr(100,100) FROM t1; |
488 INSERT INTO t1 SELECT NULL, randstr(100,100) FROM t1; | 510 INSERT INTO t1 SELECT NULL, randstr(100,100) FROM t1; |
489 INSERT INTO t1 SELECT NULL, randstr(100,100) FROM t1; | 511 INSERT INTO t1 SELECT NULL, randstr(100,100) FROM t1; |
490 INSERT INTO t1 SELECT NULL, randstr(100,100) FROM t1; | 512 INSERT INTO t1 SELECT NULL, randstr(100,100) FROM t1; |
491 INSERT INTO t1 SELECT NULL, randstr(100,100) FROM t1; | 513 INSERT INTO t1 SELECT NULL, randstr(100,100) FROM t1; |
492 INSERT INTO t2 SELECT * FROM t1; | 514 INSERT INTO t2 SELECT * FROM t1; |
493 DELETE FROM t1; | 515 DELETE FROM t1; |
494 } -corrupt { | 516 } -corrupt { |
495 set pgno [expr [file size corrupt.db] / 1024] | 517 set pgno [expr [file size corrupt.db] / 1024] |
496 hexio_write corrupt.db [expr 1024+5*($pgno-3)] 03 | 518 hexio_write corrupt.db [expr 1024+5*($pgno-3)] 03 |
497 hexio_write corrupt.db 24 12345678 | 519 hexio_write corrupt.db 24 12345678 |
498 } -test { | 520 } -test { |
499 do_test corrupt2-12.1 { | 521 do_test corrupt2-12.1 { |
500 catchsql { PRAGMA incremental_vacuum } | 522 catchsql " $::presql PRAGMA incremental_vacuum " |
501 } {1 {database disk image is malformed}} | 523 } {1 {database disk image is malformed}} |
502 } | 524 } |
503 | 525 |
504 ifcapable autovacuum { | 526 ifcapable autovacuum { |
505 # It is not possible for the last page in a database file to be the | 527 # It is not possible for the last page in a database file to be the |
506 # pending-byte page (AKA the locking page). This test verifies that if | 528 # pending-byte page (AKA the locking page). This test verifies that if |
507 # an attempt is made to commit a transaction to such an auto-vacuum | 529 # an attempt is made to commit a transaction to such an auto-vacuum |
508 # database SQLITE_CORRUPT is returned. | 530 # database SQLITE_CORRUPT is returned. |
509 # | 531 # |
510 corruption_test -tclprep { | 532 corruption_test -tclprep { |
511 db eval { | 533 db eval { |
512 PRAGMA auto_vacuum = full; | 534 PRAGMA auto_vacuum = full; |
513 PRAGMA page_size = 1024; | 535 PRAGMA page_size = 1024; |
514 CREATE TABLE t1(a INTEGER PRIMARY KEY, b); | 536 CREATE TABLE t1(a INTEGER PRIMARY KEY, b); |
515 INSERT INTO t1 VALUES(NULL, randstr(50,50)); | 537 INSERT INTO t1 VALUES(NULL, randstr(50,50)); |
516 } | 538 } |
517 for {set ii 0} {$ii < 10} {incr ii} { | 539 for {set ii 0} {$ii < 10} {incr ii} { |
518 db eval { INSERT INTO t1 SELECT NULL, randstr(50,50) FROM t1 } | 540 db eval " $::presql INSERT INTO t1 SELECT NULL, randstr(50,50) FROM t1 " |
519 } | 541 } |
520 } -corrupt { | 542 } -corrupt { |
521 do_test corrupt2-13.1 { | 543 do_test corrupt2-13.1 { |
522 file size corrupt.db | 544 file size corrupt.db |
523 } $::sqlite_pending_byte | 545 } $::sqlite_pending_byte |
524 hexio_write corrupt.db [expr $::sqlite_pending_byte+1023] 00 | 546 hexio_write corrupt.db [expr $::sqlite_pending_byte+1023] 00 |
| 547 hexio_write corrupt.db 28 00000000 |
525 } -test { | 548 } -test { |
526 do_test corrupt2-13.2 { | 549 do_test corrupt2-13.2 { |
527 file size corrupt.db | 550 file size corrupt.db |
528 } [expr $::sqlite_pending_byte + 1024] | 551 } [expr $::sqlite_pending_byte + 1024] |
529 do_test corrupt2-13.3 { | 552 do_test corrupt2-13.3 { |
530 catchsql { DELETE FROM t1 WHERE rowid < 30; } | 553 catchsql { DELETE FROM t1 WHERE rowid < 30; } |
531 } {1 {database disk image is malformed}} | 554 } {1 {database disk image is malformed}} |
532 } | 555 } |
533 } | 556 } |
534 | 557 |
535 finish_test | 558 finish_test |
OLD | NEW |