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. It creates a base | 14 # segfault if it sees a corrupt database file. It creates a base |
15 # data base file, then tests that single byte corruptions in | 15 # data base file, then tests that single byte corruptions in |
16 # increasingly larger quantities are handled gracefully. | 16 # increasingly larger quantities are handled gracefully. |
17 # | 17 # |
18 # $Id: corruptC.test,v 1.14 2009/07/11 06:55:34 danielk1977 Exp $ | 18 # $Id: corruptC.test,v 1.14 2009/07/11 06:55:34 danielk1977 Exp $ |
19 | 19 |
20 catch {file delete -force test.db test.db-journal test.bu} | 20 catch {forcedelete test.db test.db-journal test.bu} |
21 | 21 |
22 set testdir [file dirname $argv0] | 22 set testdir [file dirname $argv0] |
23 source $testdir/tester.tcl | 23 source $testdir/tester.tcl |
24 | 24 |
25 # Do not use a codec for tests in this file, as the database file is | 25 # Do not use a codec for tests in this file, as the database file is |
26 # manipulated directly using tcl scripts (using the [hexio_write] command). | 26 # manipulated directly using tcl scripts (using the [hexio_write] command). |
27 # | 27 # |
28 do_not_use_codec | 28 do_not_use_codec |
29 | 29 |
| 30 # These tests deal with corrupt database files |
| 31 # |
| 32 database_may_be_corrupt |
| 33 |
30 # Construct a compact, dense database for testing. | 34 # Construct a compact, dense database for testing. |
31 # | 35 # |
32 do_test corruptC-1.1 { | 36 do_test corruptC-1.1 { |
33 execsql { | 37 execsql { |
34 PRAGMA auto_vacuum = 0; | 38 PRAGMA auto_vacuum = 0; |
35 PRAGMA legacy_file_format=1; | 39 PRAGMA legacy_file_format=1; |
36 BEGIN; | 40 BEGIN; |
37 CREATE TABLE t1(x,y); | 41 CREATE TABLE t1(x,y); |
38 INSERT INTO t1 VALUES(1,1); | 42 INSERT INTO t1 VALUES(1,1); |
39 INSERT OR IGNORE INTO t1 SELECT x*2,y FROM t1; | 43 INSERT OR IGNORE INTO t1 SELECT x*2,y FROM t1; |
(...skipping 11 matching lines...) Expand all Loading... |
51 ifcapable {integrityck} { | 55 ifcapable {integrityck} { |
52 integrity_check corruptC-1.2 | 56 integrity_check corruptC-1.2 |
53 } | 57 } |
54 | 58 |
55 # Generate random integer | 59 # Generate random integer |
56 # | 60 # |
57 proc random {range} { | 61 proc random {range} { |
58 return [expr {round(rand()*$range)}] | 62 return [expr {round(rand()*$range)}] |
59 } | 63 } |
60 | 64 |
61 # Copy file $from into $to | |
62 # | |
63 proc copy_file {from to} { | |
64 file copy -force $from $to | |
65 } | |
66 | |
67 # Setup for the tests. Make a backup copy of the good database in test.bu. | 65 # Setup for the tests. Make a backup copy of the good database in test.bu. |
68 # | 66 # |
69 db close | 67 db close |
70 copy_file test.db test.bu | 68 forcecopy test.db test.bu |
71 sqlite3 db test.db | 69 sqlite3 db test.db |
72 set fsize [file size test.db] | 70 set fsize [file size test.db] |
73 | 71 |
74 # Set a quasi-random random seed. | 72 # Set a quasi-random random seed. |
75 if {[info exists ::G(issoak)]} { | 73 if {[info exists ::G(issoak)]} { |
76 # If we are doing SOAK tests, we want a different | 74 # If we are doing SOAK tests, we want a different |
77 # random seed for each run. Ideally we would like | 75 # random seed for each run. Ideally we would like |
78 # to use [clock clicks] or something like that here. | 76 # to use [clock clicks] or something like that here. |
79 set qseed [file mtime test.db] | 77 set qseed [file mtime test.db] |
80 } else { | 78 } else { |
81 # If we are not doing soak tests, | 79 # If we are not doing soak tests, |
82 # make it repeatable. | 80 # make it repeatable. |
83 set qseed 0 | 81 set qseed 0 |
84 } | 82 } |
85 expr srand($qseed) | 83 expr srand($qseed) |
86 | 84 |
87 # | 85 # |
88 # First test some specific corruption tests found from earlier runs | 86 # First test some specific corruption tests found from earlier runs |
89 # with specific seeds. | 87 # with specific seeds. |
90 # | 88 # |
91 | 89 |
92 # test that a corrupt content offset size is handled (seed 5577) | 90 # test that a corrupt content offset size is handled (seed 5577) |
93 do_test corruptC-2.1 { | 91 do_test corruptC-2.1 { |
94 db close | 92 db close |
95 copy_file test.bu test.db | 93 forcecopy test.bu test.db |
96 | 94 |
97 # insert corrupt byte(s) | 95 # insert corrupt byte(s) |
98 hexio_write test.db 2053 [format %02x 0x04] | 96 hexio_write test.db 2053 [format %02x 0x04] |
99 | 97 |
100 sqlite3 db test.db | 98 sqlite3 db test.db |
101 catchsql {PRAGMA integrity_check} | 99 catchsql {PRAGMA integrity_check} |
102 } {1 {database disk image is malformed}} | 100 } {1 {database disk image is malformed}} |
103 | 101 |
104 # test that a corrupt content offset size is handled (seed 5649) | 102 # test that a corrupt content offset size is handled (seed 5649) |
105 do_test corruptC-2.2 { | 103 do_test corruptC-2.2 { |
106 db close | 104 db close |
107 copy_file test.bu test.db | 105 forcecopy test.bu test.db |
108 | 106 |
109 # insert corrupt byte(s) | 107 # insert corrupt byte(s) |
110 hexio_write test.db 27 [format %02x 0x08] | 108 hexio_write test.db 27 [format %02x 0x08] |
111 hexio_write test.db 233 [format %02x 0x6a] | 109 hexio_write test.db 233 [format %02x 0x6a] |
112 hexio_write test.db 328 [format %02x 0x67] | 110 hexio_write test.db 328 [format %02x 0x67] |
113 hexio_write test.db 750 [format %02x 0x1f] | 111 hexio_write test.db 750 [format %02x 0x1f] |
114 hexio_write test.db 1132 [format %02x 0x52] | 112 hexio_write test.db 1132 [format %02x 0x52] |
115 hexio_write test.db 1133 [format %02x 0x84] | 113 hexio_write test.db 1133 [format %02x 0x84] |
116 hexio_write test.db 1220 [format %02x 0x01] | 114 hexio_write test.db 1220 [format %02x 0x01] |
117 hexio_write test.db 3688 [format %02x 0xc1] | 115 hexio_write test.db 3688 [format %02x 0xc1] |
118 hexio_write test.db 3714 [format %02x 0x58] | 116 hexio_write test.db 3714 [format %02x 0x58] |
119 hexio_write test.db 3746 [format %02x 0x9a] | 117 hexio_write test.db 3746 [format %02x 0x9a] |
120 | 118 |
121 sqlite3 db test.db | 119 sqlite3 db test.db |
122 catchsql {UPDATE t1 SET y=1} | 120 catchsql {UPDATE t1 SET y=1} |
123 } {1 {database disk image is malformed}} | 121 } {1 {database disk image is malformed}} |
124 | 122 |
125 # test that a corrupt free cell size is handled (seed 13329) | 123 # test that a corrupt free cell size is handled (seed 13329) |
126 do_test corruptC-2.3 { | 124 do_test corruptC-2.3 { |
127 db close | 125 db close |
128 copy_file test.bu test.db | 126 forcecopy test.bu test.db |
129 | 127 |
130 # insert corrupt byte(s) | 128 # insert corrupt byte(s) |
131 hexio_write test.db 1094 [format %02x 0x76] | 129 hexio_write test.db 1094 [format %02x 0x76] |
132 | 130 |
133 sqlite3 db test.db | 131 sqlite3 db test.db |
134 catchsql {UPDATE t1 SET y=1} | 132 catchsql {UPDATE t1 SET y=1} |
135 } {1 {database disk image is malformed}} | 133 } {1 {database disk image is malformed}} |
136 | 134 |
137 # test that a corrupt free cell size is handled (seed 169571) | 135 # test that a corrupt free cell size is handled (seed 169571) |
138 do_test corruptC-2.4 { | 136 do_test corruptC-2.4 { |
139 db close | 137 db close |
140 copy_file test.bu test.db | 138 forcecopy test.bu test.db |
141 | 139 |
142 # insert corrupt byte(s) | 140 # insert corrupt byte(s) |
143 hexio_write test.db 3119 [format %02x 0xdf] | 141 hexio_write test.db 3119 [format %02x 0xdf] |
144 | 142 |
145 sqlite3 db test.db | 143 sqlite3 db test.db |
146 catchsql {UPDATE t2 SET y='abcdef-uvwxyz'} | 144 catchsql {UPDATE t2 SET y='abcdef-uvwxyz'} |
147 } {1 {database disk image is malformed}} | 145 } {1 {database disk image is malformed}} |
148 | 146 |
149 # test that a corrupt free cell size is handled (seed 169571) | 147 # test that a corrupt free cell size is handled (seed 169571) |
150 do_test corruptC-2.5 { | 148 do_test corruptC-2.5 { |
151 db close | 149 db close |
152 copy_file test.bu test.db | 150 forcecopy test.bu test.db |
153 | 151 |
154 # insert corrupt byte(s) | 152 # insert corrupt byte(s) |
155 hexio_write test.db 3119 [format %02x 0xdf] | 153 hexio_write test.db 3119 [format %02x 0xdf] |
156 hexio_write test.db 4073 [format %02x 0xbf] | 154 hexio_write test.db 4073 [format %02x 0xbf] |
157 | 155 |
158 sqlite3 db test.db | 156 sqlite3 db test.db |
159 catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;} | 157 catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;} |
160 catchsql {PRAGMA integrity_check} | 158 catchsql {PRAGMA integrity_check} |
161 } {0 {{*** in database main *** | 159 } {0 {{*** in database main *** |
162 Page 4: btreeInitPage() returns error code 11}}} | 160 Page 4: btreeInitPage() returns error code 11}}} |
163 | 161 |
164 # {0 {{*** in database main *** | 162 # {0 {{*** in database main *** |
165 # Corruption detected in cell 710 on page 4 | 163 # Corruption detected in cell 710 on page 4 |
166 # Multiple uses for byte 661 of page 4 | 164 # Multiple uses for byte 661 of page 4 |
167 # Fragmented space is 249 byte reported as 21 on page 4}}} | 165 # Fragmented space is 249 byte reported as 21 on page 4}}} |
168 | 166 |
169 # test that a corrupt free cell size is handled (seed 169595) | 167 # test that a corrupt free cell size is handled (seed 169595) |
170 do_test corruptC-2.6 { | 168 do_test corruptC-2.6 { |
171 db close | 169 db close |
172 copy_file test.bu test.db | 170 forcecopy test.bu test.db |
173 | 171 |
174 # insert corrupt byte(s) | 172 # insert corrupt byte(s) |
175 hexio_write test.db 619 [format %02x 0xe2] | 173 hexio_write test.db 619 [format %02x 0xe2] |
176 hexio_write test.db 3150 [format %02x 0xa8] | 174 hexio_write test.db 3150 [format %02x 0xa8] |
177 | 175 |
178 sqlite3 db test.db | 176 sqlite3 db test.db |
179 catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;} | 177 catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;} |
180 } {1 {database disk image is malformed}} | 178 } {1 {database disk image is malformed}} |
181 | 179 |
182 # corruption (seed 178692) | 180 # corruption (seed 178692) |
183 do_test corruptC-2.7 { | 181 do_test corruptC-2.7 { |
184 db close | 182 db close |
185 copy_file test.bu test.db | 183 forcecopy test.bu test.db |
186 | 184 |
187 # insert corrupt byte(s) | 185 # insert corrupt byte(s) |
188 hexio_write test.db 3074 [format %02x 0xa0] | 186 hexio_write test.db 3074 [format %02x 0xa0] |
189 | 187 |
190 sqlite3 db test.db | 188 sqlite3 db test.db |
191 catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;} | 189 catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;} |
192 } {1 {database disk image is malformed}} | 190 } {1 {database disk image is malformed}} |
193 | 191 |
194 # corruption (seed 179069) | 192 # corruption (seed 179069) |
195 do_test corruptC-2.8 { | 193 do_test corruptC-2.8 { |
196 db close | 194 db close |
197 copy_file test.bu test.db | 195 forcecopy test.bu test.db |
198 | 196 |
199 # insert corrupt byte(s) | 197 # insert corrupt byte(s) |
200 hexio_write test.db 1393 [format %02x 0x7d] | 198 hexio_write test.db 1393 [format %02x 0x7d] |
201 hexio_write test.db 84 [format %02x 0x19] | 199 hexio_write test.db 84 [format %02x 0x19] |
202 hexio_write test.db 3287 [format %02x 0x3b] | 200 hexio_write test.db 3287 [format %02x 0x3b] |
203 hexio_write test.db 2564 [format %02x 0xed] | 201 hexio_write test.db 2564 [format %02x 0xed] |
204 hexio_write test.db 2139 [format %02x 0x55] | 202 hexio_write test.db 2139 [format %02x 0x55] |
205 | 203 |
206 sqlite3 db test.db | 204 sqlite3 db test.db |
207 catchsql {BEGIN; DELETE FROM t1 WHERE x>13; ROLLBACK;} | 205 catchsql {BEGIN; DELETE FROM t1 WHERE x>13; ROLLBACK;} |
208 } {1 {database disk image is malformed}} | 206 } {1 {database disk image is malformed}} |
209 | 207 |
210 # corruption (seed 170434) | 208 # corruption (seed 170434) |
| 209 # |
| 210 # UPDATE: Prior to 3.8.2, this used to return SQLITE_CORRUPT. It no longer |
| 211 # does. That is Ok, the point of these tests is to verify that no buffer |
| 212 # overruns or overreads can be caused by corrupt databases. |
211 do_test corruptC-2.9 { | 213 do_test corruptC-2.9 { |
212 db close | 214 db close |
213 copy_file test.bu test.db | 215 forcecopy test.bu test.db |
214 | 216 |
215 # insert corrupt byte(s) | 217 # insert corrupt byte(s) |
216 hexio_write test.db 2095 [format %02x 0xd6] | 218 hexio_write test.db 2095 [format %02x 0xd6] |
217 | 219 |
218 sqlite3 db test.db | 220 sqlite3 db test.db |
219 catchsql {BEGIN; DELETE FROM t1 WHERE x>13; ROLLBACK;} | 221 catchsql {BEGIN; DELETE FROM t1 WHERE x>13; ROLLBACK;} |
220 } {1 {database disk image is malformed}} | 222 } {0 {}} |
221 | 223 |
222 # corruption (seed 186504) | 224 # corruption (seed 186504) |
223 do_test corruptC-2.10 { | 225 do_test corruptC-2.10 { |
224 db close | 226 db close |
225 copy_file test.bu test.db | 227 forcecopy test.bu test.db |
226 | 228 |
227 # insert corrupt byte(s) | 229 # insert corrupt byte(s) |
228 hexio_write test.db 3130 [format %02x 0x02] | 230 hexio_write test.db 3130 [format %02x 0x02] |
229 | 231 |
230 sqlite3 db test.db | 232 sqlite3 db test.db |
231 catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;} | 233 catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;} |
232 } {1 {database disk image is malformed}} | 234 } {1 {database disk image is malformed}} |
233 | 235 |
234 # corruption (seed 1589) | 236 # corruption (seed 1589) |
235 do_test corruptC-2.11 { | 237 do_test corruptC-2.11 { |
236 db close | 238 db close |
237 copy_file test.bu test.db | 239 forcecopy test.bu test.db |
238 | 240 |
239 # insert corrupt byte(s) | 241 # insert corrupt byte(s) |
240 hexio_write test.db 55 [format %02x 0xa7] | 242 hexio_write test.db 55 [format %02x 0xa7] |
241 | 243 |
242 sqlite3 db test.db | 244 sqlite3 db test.db |
243 catchsql {BEGIN; CREATE TABLE t3 AS SELECT x,3 as y FROM t2 WHERE rowid%5!=0;
ROLLBACK;} | 245 catchsql {BEGIN; CREATE TABLE t3 AS SELECT x,3 as y FROM t2 WHERE rowid%5!=0;
ROLLBACK;} |
244 } {1 {database disk image is malformed}} | 246 } {1 {database disk image is malformed}} |
245 | 247 |
246 # corruption (seed 14166) | 248 # corruption (seed 14166) |
247 do_test corruptC-2.12 { | 249 do_test corruptC-2.12 { |
248 db close | 250 db close |
249 copy_file test.bu test.db | 251 forcecopy test.bu test.db |
250 | 252 |
251 # insert corrupt byte(s) | 253 # insert corrupt byte(s) |
252 hexio_write test.db 974 [format %02x 0x2e] | 254 hexio_write test.db 974 [format %02x 0x2e] |
253 | 255 |
254 sqlite3 db test.db | 256 sqlite3 db test.db |
255 catchsql {SELECT count(*) FROM sqlite_master;} | 257 catchsql {SELECT count(*) FROM sqlite_master;} |
256 } {1 {malformed database schema (t1i1) - corrupt database}} | 258 } {1 {malformed database schema (t1i1) - corrupt database}} |
257 | 259 |
258 # corruption (seed 218803) | 260 # corruption (seed 218803) |
259 do_test corruptC-2.13 { | 261 do_test corruptC-2.13 { |
260 db close | 262 db close |
261 copy_file test.bu test.db | 263 forcecopy test.bu test.db |
262 | 264 |
263 # insert corrupt byte(s) | 265 # insert corrupt byte(s) |
264 hexio_write test.db 102 [format %02x 0x12] | 266 hexio_write test.db 102 [format %02x 0x12] |
265 | 267 |
266 sqlite3 db test.db | 268 sqlite3 db test.db |
267 catchsql {BEGIN; CREATE TABLE t3 AS SELECT x,3 as y FROM t2 WHERE rowid%5!=0;
ROLLBACK;} | 269 catchsql {BEGIN; CREATE TABLE t3 AS SELECT x,3 as y FROM t2 WHERE rowid%5!=0;
ROLLBACK;} |
268 } {1 {database disk image is malformed}} | 270 } {1 {database disk image is malformed}} |
269 | 271 |
270 do_test corruptC-2.14 { | 272 do_test corruptC-2.14 { |
271 db close | 273 db close |
272 copy_file test.bu test.db | 274 forcecopy test.bu test.db |
273 | 275 |
274 sqlite3 db test.db | 276 sqlite3 db test.db |
275 set blob [string repeat abcdefghij 10000] | 277 set blob [string repeat abcdefghij 10000] |
276 execsql { INSERT INTO t1 VALUES (1, $blob) } | 278 execsql { INSERT INTO t1 VALUES (1, $blob) } |
277 | 279 |
278 sqlite3 db test.db | 280 sqlite3 db test.db |
279 set filesize [file size test.db] | 281 set filesize [file size test.db] |
280 hexio_write test.db [expr $filesize-2048] 00000001 | 282 hexio_write test.db [expr $filesize-2048] 00000001 |
281 catchsql {DELETE FROM t1 WHERE rowid = (SELECT max(rowid) FROM t1)} | 283 catchsql {DELETE FROM t1 WHERE rowid = (SELECT max(rowid) FROM t1)} |
282 } {1 {database disk image is malformed}} | 284 } {1 {database disk image is malformed}} |
283 | 285 |
284 # At one point this particular corrupt database was causing a buffer | 286 # At one point this particular corrupt database was causing a buffer |
285 # overread. Which caused a crash in a run of all.test once. | 287 # overread. Which caused a crash in a run of all.test once. |
286 # | 288 # |
287 do_test corruptC-2.15 { | 289 do_test corruptC-2.15 { |
288 db close | 290 db close |
289 copy_file test.bu test.db | 291 forcecopy test.bu test.db |
290 hexio_write test.db 986 b9 | 292 hexio_write test.db 986 b9 |
291 sqlite3 db test.db | 293 sqlite3 db test.db |
292 catchsql {SELECT count(*) FROM sqlite_master;} | 294 catchsql {SELECT count(*) FROM sqlite_master;} |
293 } {1 {malformed database schema (t1i1) - no such table: main.t1}} | 295 } {1 {malformed database schema (t1i1) - no such table: main.t1}} |
294 | 296 |
295 # | 297 # |
296 # Now test for a series of quasi-random seeds. | 298 # Now test for a series of quasi-random seeds. |
297 # We loop over the entire file size and touch | 299 # We loop over the entire file size and touch |
298 # each byte at least once. | 300 # each byte at least once. |
299 for {set tn 0} {$tn<$fsize} {incr tn 1} { | 301 for {set tn 0} {$tn<$fsize} {incr tn 1} { |
300 | 302 |
301 # setup for test | 303 # setup for test |
302 db close | 304 db close |
303 copy_file test.bu test.db | 305 forcecopy test.bu test.db |
304 sqlite3 db test.db | 306 sqlite3 db test.db |
305 | 307 |
306 # Seek to a random location in the file, and write a random single byte | 308 # Seek to a random location in the file, and write a random single byte |
307 # value. Then do various operations on the file to make sure that | 309 # value. Then do various operations on the file to make sure that |
308 # the database engine can handle the corruption gracefully. | 310 # the database engine can handle the corruption gracefully. |
309 # | 311 # |
310 set last 0 | 312 set last 0 |
311 for {set i 1} {$i<=512 && !$last} {incr i 1} { | 313 for {set i 1} {$i<=512 && !$last} {incr i 1} { |
312 | 314 |
313 db close | 315 db close |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 set stats(ref) | 406 set stats(ref) |
405 } {0} | 407 } {0} |
406 } | 408 } |
407 } | 409 } |
408 # end for i | 410 # end for i |
409 | 411 |
410 } | 412 } |
411 # end for tn | 413 # end for tn |
412 | 414 |
413 finish_test | 415 finish_test |
OLD | NEW |