blob: bb37758b17b48f0aadc2ba3a3e31e48e13ecdba8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
# 2008 July 9
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.
#
# This file implements tests to make sure SQLite does not crash or
# segfault if it sees a corrupt database file. It specifically focuses
# on corruption in the form of duplicate entries on the freelist.
#
# $Id: corrupt9.test,v 1.3 2009/06/04 02:47:04 shane Exp $
set testdir [file dirname $argv0]
source $testdir/tester.tcl
# Do not use a codec for tests in this file, as the database file is
# manipulated directly using tcl scripts (using the [hexio_write] command).
#
do_not_use_codec
# These tests deal with corrupt database files
#
database_may_be_corrupt
# We must have the page_size pragma for these tests to work.
#
ifcapable !pager_pragmas {
finish_test
return
}
# Return the offset to the first (trunk) page of the freelist. Return
# zero of the freelist is empty.
#
proc freelist_trunk_offset {filename} {
if {[hexio_read $filename 36 4]==0} {return 0}
set pgno [hexio_get_int [hexio_read $filename 32 4]]
return [expr {($pgno-1)*[hexio_get_int [hexio_read $filename 16 2]]}]
}
# This procedure looks at the first trunk page of the freelist and
# corrupts that page by overwriting up to N entries with duplicates
# of the first entry.
#
proc corrupt_freelist {filename N} {
set offset [freelist_trunk_offset $filename]
if {$offset==0} {error "Freelist is empty"}
set cnt [hexio_get_int [hexio_read $filename [expr {$offset+4}] 4]]
set pgno [hexio_read $filename [expr {$offset+8}] 4]
for {set i 12} {$N>0 && $i<8+4*$cnt} {incr i 4; incr N -1} {
hexio_write $filename [expr {$offset+$i}] $pgno
}
}
# Create a database to work with. Make sure there are plenty of
# entries on the freelist.
#
do_test corrupt9-1.1 {
execsql {
PRAGMA auto_vacuum=NONE;
PRAGMA page_size=1024;
CREATE TABLE t1(x);
INSERT INTO t1(x) VALUES(1);
INSERT INTO t1(x) VALUES(2);
INSERT INTO t1(x) SELECT x+2 FROM t1;
INSERT INTO t1(x) SELECT x+4 FROM t1;
INSERT INTO t1(x) SELECT x+8 FROM t1;
INSERT INTO t1(x) SELECT x+16 FROM t1;
INSERT INTO t1(x) SELECT x+32 FROM t1;
INSERT INTO t1(x) SELECT x+64 FROM t1;
INSERT INTO t1(x) SELECT x+128 FROM t1;
INSERT INTO t1(x) SELECT x+256 FROM t1;
CREATE TABLE t2(a,b);
INSERT INTO t2 SELECT x, x*x FROM t1;
CREATE INDEX i1 ON t1(x);
CREATE INDEX i2 ON t2(b,a);
DROP INDEX i2;
}
expr {[file size test.db]>1024*24}
} {1}
integrity_check corrupt9-1.2
# Corrupt the freelist by adding duplicate entries to the freelist.
# Make sure the corruption is detected.
#
db close
forcecopy test.db test.db-template
corrupt_freelist test.db 1
sqlite3 db test.db
do_test corrupt9-2.1 {
set x [db eval {PRAGMA integrity_check}]
expr {$x!="ok"}
} {1}
do_test corrupt9-2.2 {
catchsql {
CREATE INDEX i2 ON t2(b,a);
REINDEX;
}
} {1 {database disk image is malformed}}
db close
forcecopy test.db-template test.db
corrupt_freelist test.db 2
sqlite3 db test.db
do_test corrupt9-3.1 {
set x [db eval {PRAGMA integrity_check}]
expr {$x!="ok"}
} {1}
do_test corrupt9-3.2 {
catchsql {
CREATE INDEX i2 ON t2(b,a);
REINDEX;
}
} {1 {database disk image is malformed}}
db close
forcecopy test.db-template test.db
corrupt_freelist test.db 3
sqlite3 db test.db
do_test corrupt9-4.1 {
set x [db eval {PRAGMA integrity_check}]
expr {$x!="ok"}
} {1}
do_test corrupt9-4.2 {
catchsql {
CREATE INDEX i2 ON t2(b,a);
REINDEX;
}
} {1 {database disk image is malformed}}
finish_test
|