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
#***********************************************************************
11
# This file implements regression tests for SQLite library.
13
# This file implements tests to make sure SQLite does not crash or
14
# segfault if it sees a corrupt database file.
16
# $Id: corrupt2.test,v 1.14 2008/08/02 20:09:37 drh Exp $
18
set testdir [file dirname $argv0]
19
source $testdir/tester.tcl
21
# The following tests - corrupt2-1.* - create some databases corrupted in
22
# specific ways and ensure that SQLite detects them as corrupt.
24
do_test corrupt2-1.1 {
27
PRAGMA page_size=1024;
28
CREATE TABLE abc(a, b, c);
32
do_test corrupt2-1.2 {
34
# Corrupt the 16 byte magic string at the start of the file
35
file delete -force corrupt.db
36
file delete -force corrupt.db-journal
37
copy_file test.db corrupt.db
38
set f [open corrupt.db RDWR]
43
sqlite3 db2 corrupt.db
45
SELECT * FROM sqlite_master;
47
} {1 {file is encrypted or is not a database}}
49
do_test corrupt2-1.3 {
52
# Corrupt the page-size (bytes 16 and 17 of page 1).
53
file delete -force corrupt.db
54
file delete -force corrupt.db-journal
55
copy_file test.db corrupt.db
56
set f [open corrupt.db RDWR]
57
fconfigure $f -encoding binary
59
puts -nonewline $f "\x00\xFF"
62
sqlite3 db2 corrupt.db
64
SELECT * FROM sqlite_master;
66
} {1 {file is encrypted or is not a database}}
68
do_test corrupt2-1.4 {
71
# Corrupt the free-block list on page 1.
72
file delete -force corrupt.db
73
file delete -force corrupt.db-journal
74
copy_file test.db corrupt.db
75
set f [open corrupt.db RDWR]
76
fconfigure $f -encoding binary
78
puts -nonewline $f "\xFF\xFF"
81
sqlite3 db2 corrupt.db
83
SELECT * FROM sqlite_master;
85
} {1 {database disk image is malformed}}
87
do_test corrupt2-1.5 {
90
# Corrupt the free-block list on page 1.
91
file delete -force corrupt.db
92
file delete -force corrupt.db-journal
93
copy_file test.db corrupt.db
94
set f [open corrupt.db RDWR]
95
fconfigure $f -encoding binary
97
puts -nonewline $f "\x00\xC8"
99
puts -nonewline $f "\x00\x00"
100
puts -nonewline $f "\x10\x00"
103
sqlite3 db2 corrupt.db
105
SELECT * FROM sqlite_master;
107
} {1 {database disk image is malformed}}
110
# Corrupt a database by having 2 indices of the same name:
111
do_test corrupt2-2.1 {
113
file delete -force corrupt.db
114
file delete -force corrupt.db-journal
115
copy_file test.db corrupt.db
117
sqlite3 db2 corrupt.db
119
CREATE INDEX a1 ON abc(a);
120
CREATE INDEX a2 ON abc(b);
121
PRAGMA writable_schema = 1;
123
SET name = 'a3', sql = 'CREATE INDEX a3' || substr(sql, 16, 10000)
124
WHERE type = 'index';
125
PRAGMA writable_schema = 0;
129
sqlite3 db2 corrupt.db
131
SELECT * FROM sqlite_master;
133
} {1 {malformed database schema (a3) - index a3 already exists}}
137
do_test corrupt2-3.1 {
138
file delete -force corrupt.db
139
file delete -force corrupt.db-journal
140
sqlite3 db2 corrupt.db
143
PRAGMA auto_vacuum = 1;
144
PRAGMA page_size = 1024;
145
CREATE TABLE t1(a, b, c);
146
CREATE TABLE t2(a, b, c);
147
INSERT INTO t2 VALUES(randomblob(100), randomblob(100), randomblob(100));
148
INSERT INTO t2 SELECT * FROM t2;
149
INSERT INTO t2 SELECT * FROM t2;
150
INSERT INTO t2 SELECT * FROM t2;
151
INSERT INTO t2 SELECT * FROM t2;
156
# 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
158
# the pointer-map after moving the content of page 4 to page 3 as part
159
# of the DROP TABLE operation below.
161
set fd [open corrupt.db r+]
162
fconfigure $fd -encoding binary -translation binary
163
seek $fd [expr 1024*3 + 12]
164
set zCelloffset [read $fd 2]
165
binary scan $zCelloffset S iCelloffset
166
seek $fd [expr 1024*3 + $iCelloffset]
167
puts -nonewline $fd "\00\00\00\00"
170
sqlite3 db2 corrupt.db
174
} {1 {database disk image is malformed}}
176
do_test corrupt2-4.1 {
180
} {1 {database disk image is malformed}}
182
do_test corrupt2-5.1 {
183
file delete -force corrupt.db
184
file delete -force corrupt.db-journal
185
sqlite3 db2 corrupt.db
188
PRAGMA auto_vacuum = 0;
189
PRAGMA page_size = 1024;
190
CREATE TABLE t1(a, b, c);
191
CREATE TABLE t2(a, b, c);
192
INSERT INTO t2 VALUES(randomblob(100), randomblob(100), randomblob(100));
193
INSERT INTO t2 SELECT * FROM t2;
194
INSERT INTO t2 SELECT * FROM t2;
195
INSERT INTO t2 SELECT * FROM t2;
196
INSERT INTO t2 SELECT * FROM t2;
197
INSERT INTO t1 SELECT * FROM t2;
202
# This block links a page from table t2 into the t1 table structure.
204
set fd [open corrupt.db r+]
205
fconfigure $fd -encoding binary -translation binary
206
seek $fd [expr 1024 + 12]
207
set zCelloffset [read $fd 2]
208
binary scan $zCelloffset S iCelloffset
209
seek $fd [expr 1024 + $iCelloffset]
210
set zChildPage [read $fd 4]
211
seek $fd [expr 2*1024 + 12]
212
set zCelloffset [read $fd 2]
213
binary scan $zCelloffset S iCelloffset
214
seek $fd [expr 2*1024 + $iCelloffset]
215
puts -nonewline $fd $zChildPage
218
sqlite3 db2 corrupt.db
219
db2 eval {SELECT rowid FROM t1} {
220
set result [db2 eval {pragma integrity_check}]
224
} {{*** in database main ***
225
Page 10: sqlite3BtreeInitPage() returns error code 11
226
On tree page 3 cell 1: Child page depth differs
227
On tree page 2 cell 0: 2nd reference to page 10
228
On tree page 2 cell 1: Child page depth differs
229
Page 4 is never used}}
233
proc corruption_test {args} {
237
file delete -force corrupt.db
238
file delete -force corrupt.db-journal
240
sqlite3 db corrupt.db
246
sqlite3 db corrupt.db
250
ifcapable autovacuum {
251
# The tests within this block - corrupt2-6.* - aim to test corruption
252
# detection within an incremental-vacuum. When an incremental-vacuum
253
# step is executed, the last non-free page of the database file is
254
# moved into a free space in the body of the file. After doing so,
255
# the page reference in the parent page must be updated to refer
256
# to the new location. These tests test the outcome of corrupting
257
# that page reference before performing the incremental vacuum.
260
# The last page in the database page is the second page
261
# in an overflow chain.
263
corruption_test -sqlprep {
264
PRAGMA auto_vacuum = incremental;
265
PRAGMA page_size = 1024;
266
CREATE TABLE t1(a, b);
267
INSERT INTO t1 VALUES(1, randomblob(2500));
268
INSERT INTO t1 VALUES(2, randomblob(2500));
269
DELETE FROM t1 WHERE a = 1;
271
hexio_write corrupt.db [expr 1024*5] 00000008
273
do_test corrupt2-6.1 {
274
catchsql { pragma incremental_vacuum = 1 }
275
} {1 {database disk image is malformed}}
278
# The last page in the database page is a non-root b-tree page.
280
corruption_test -sqlprep {
281
PRAGMA auto_vacuum = incremental;
282
PRAGMA page_size = 1024;
283
CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
284
INSERT INTO t1 VALUES(1, randomblob(2500));
285
INSERT INTO t1 VALUES(2, randomblob(50));
286
INSERT INTO t1 SELECT NULL, randomblob(50) FROM t1;
287
INSERT INTO t1 SELECT NULL, randomblob(50) FROM t1;
288
INSERT INTO t1 SELECT NULL, randomblob(50) FROM t1;
289
INSERT INTO t1 SELECT NULL, randomblob(50) FROM t1;
290
DELETE FROM t1 WHERE a = 1;
292
hexio_write corrupt.db [expr 1024*2 + 8] 00000009
294
do_test corrupt2-6.2 {
295
catchsql { pragma incremental_vacuum = 1 }
296
} {1 {database disk image is malformed}}
299
# Set up a pointer-map entry so that the last page of the database
300
# file appears to be a b-tree root page. This should be detected
303
corruption_test -sqlprep {
304
PRAGMA auto_vacuum = incremental;
305
PRAGMA page_size = 1024;
306
CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
307
INSERT INTO t1 VALUES(1, randomblob(2500));
308
INSERT INTO t1 VALUES(2, randomblob(2500));
309
INSERT INTO t1 VALUES(3, randomblob(2500));
310
DELETE FROM t1 WHERE a = 1;
312
set nPage [expr [file size corrupt.db] / 1024]
313
hexio_write corrupt.db [expr 1024 + ($nPage-3)*5] 010000000
315
do_test corrupt2-6.3 {
316
catchsql { pragma incremental_vacuum = 1 }
317
} {1 {database disk image is malformed}}
320
corruption_test -sqlprep {
321
PRAGMA auto_vacuum = 1;
322
PRAGMA page_size = 1024;
323
CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
324
INSERT INTO t1 VALUES(1, randomblob(2500));
325
DELETE FROM t1 WHERE a = 1;
327
set nAppend [expr 1024*207 - [file size corrupt.db]]
328
set fd [open corrupt.db r+]
330
puts -nonewline $fd [string repeat x $nAppend]
333
do_test corrupt2-6.4 {
338
} {1 {database disk image is malformed}}
344
PRAGMA auto_vacuum = 0;
345
PRAGMA page_size = 1024;
346
CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
347
CREATE INDEX i1 ON t1(b);
348
INSERT INTO t1 VALUES(1, randomblob(50));
349
INSERT INTO t1 SELECT NULL, randomblob(50) FROM t1;
350
INSERT INTO t1 SELECT NULL, randomblob(50) FROM t1;
351
INSERT INTO t1 SELECT NULL, randomblob(50) FROM t1;
352
INSERT INTO t1 SELECT NULL, randomblob(50) FROM t1;
353
INSERT INTO t1 SELECT NULL, randomblob(50) FROM t1;
354
INSERT INTO t1 SELECT NULL, randomblob(50) FROM t1;
357
corruption_test -sqlprep $sqlprep -corrupt {
358
# Set the page-flags of one of the leaf pages of the index B-Tree to
359
# 0x0D (interpreted by SQLite as "leaf page of a table B-Tree").
361
set fd [open corrupt.db r+]
362
fconfigure $fd -translation binary -encoding binary
363
seek $fd [expr 1024*2 + 8]
364
set zRightChild [read $fd 4]
365
binary scan $zRightChild I iRightChild
366
seek $fd [expr 1024*($iRightChild-1)]
367
puts -nonewline $fd "\x0D"
370
do_test corrupt2-7.1 {
371
catchsql { SELECT b FROM t1 ORDER BY b ASC }
372
} {1 {database disk image is malformed}}
375
corruption_test -sqlprep $sqlprep -corrupt {
376
# Mess up the page-header of one of the leaf pages of the index B-Tree.
377
# The corruption is detected as part of an OP_Prev opcode.
379
set fd [open corrupt.db r+]
380
fconfigure $fd -translation binary -encoding binary
381
seek $fd [expr 1024*2 + 12]
382
set zCellOffset [read $fd 2]
383
binary scan $zCellOffset S iCellOffset
384
seek $fd [expr 1024*2 + $iCellOffset]
385
set zChild [read $fd 4]
386
binary scan $zChild I iChild
387
seek $fd [expr 1024*($iChild-1)+3]
388
puts -nonewline $fd "\xFFFF"
391
do_test corrupt2-7.1 {
392
catchsql { SELECT b FROM t1 ORDER BY b DESC }
393
} {1 {database disk image is malformed}}
396
corruption_test -sqlprep $sqlprep -corrupt {
397
# Set the page-flags of one of the leaf pages of the table B-Tree to
398
# 0x0A (interpreted by SQLite as "leaf page of an index B-Tree").
400
set fd [open corrupt.db r+]
401
fconfigure $fd -translation binary -encoding binary
402
seek $fd [expr 1024*1 + 8]
403
set zRightChild [read $fd 4]
404
binary scan $zRightChild I iRightChild
405
seek $fd [expr 1024*($iRightChild-1)]
406
puts -nonewline $fd "\x0A"
409
do_test corrupt2-8.1 {
410
catchsql { SELECT * FROM t1 WHERE rowid=1000 }
411
} {1 {database disk image is malformed}}
414
corruption_test -sqlprep {
415
CREATE TABLE t1(a, b, c); CREATE TABLE t8(a, b, c); CREATE TABLE tE(a, b, c);
416
CREATE TABLE t2(a, b, c); CREATE TABLE t9(a, b, c); CREATE TABLE tF(a, b, c);
417
CREATE TABLE t3(a, b, c); CREATE TABLE tA(a, b, c); CREATE TABLE tG(a, b, c);
418
CREATE TABLE t4(a, b, c); CREATE TABLE tB(a, b, c); CREATE TABLE tH(a, b, c);
419
CREATE TABLE t5(a, b, c); CREATE TABLE tC(a, b, c); CREATE TABLE tI(a, b, c);
420
CREATE TABLE t6(a, b, c); CREATE TABLE tD(a, b, c); CREATE TABLE tJ(a, b, c);
421
CREATE TABLE x1(a, b, c); CREATE TABLE x8(a, b, c); CREATE TABLE xE(a, b, c);
422
CREATE TABLE x2(a, b, c); CREATE TABLE x9(a, b, c); CREATE TABLE xF(a, b, c);
423
CREATE TABLE x3(a, b, c); CREATE TABLE xA(a, b, c); CREATE TABLE xG(a, b, c);
424
CREATE TABLE x4(a, b, c); CREATE TABLE xB(a, b, c); CREATE TABLE xH(a, b, c);
425
CREATE TABLE x5(a, b, c); CREATE TABLE xC(a, b, c); CREATE TABLE xI(a, b, c);
426
CREATE TABLE x6(a, b, c); CREATE TABLE xD(a, b, c); CREATE TABLE xJ(a, b, c);
428
set fd [open corrupt.db r+]
429
fconfigure $fd -translation binary -encoding binary
431
set zRightChild [read $fd 4]
432
binary scan $zRightChild I iRightChild
433
seek $fd [expr 1024*($iRightChild-1)+3]
434
puts -nonewline $fd "\x00\x00"
437
do_test corrupt2-9.1 {
438
catchsql { SELECT sql FROM sqlite_master }
439
} {1 {database disk image is malformed}}