diff options
Diffstat (limited to 'libraries/sqlite/unix/sqlite-3.5.1/test/crash.test')
-rw-r--r-- | libraries/sqlite/unix/sqlite-3.5.1/test/crash.test | 403 |
1 files changed, 403 insertions, 0 deletions
diff --git a/libraries/sqlite/unix/sqlite-3.5.1/test/crash.test b/libraries/sqlite/unix/sqlite-3.5.1/test/crash.test new file mode 100644 index 0000000..0d58f84 --- /dev/null +++ b/libraries/sqlite/unix/sqlite-3.5.1/test/crash.test | |||
@@ -0,0 +1,403 @@ | |||
1 | # 2001 September 15 | ||
2 | # | ||
3 | # The author disclaims copyright to this source code. In place of | ||
4 | # a legal notice, here is a blessing: | ||
5 | # | ||
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. | ||
9 | # | ||
10 | #*********************************************************************** | ||
11 | # This file implements regression tests for SQLite library. | ||
12 | # | ||
13 | # The focus of this file is testing the ability of the database to | ||
14 | # uses its rollback journal to recover intact (no database corruption) | ||
15 | # from a power failure during the middle of a COMMIT. The OS interface | ||
16 | # modules are overloaded using the modified I/O routines found in test6.c. | ||
17 | # These routines allow us to simulate the kind of file damage that | ||
18 | # occurs after a power failure. | ||
19 | # | ||
20 | # $Id: crash.test,v 1.25 2007/08/20 14:23:44 danielk1977 Exp $ | ||
21 | |||
22 | set testdir [file dirname $argv0] | ||
23 | source $testdir/tester.tcl | ||
24 | |||
25 | ifcapable !crashtest { | ||
26 | finish_test | ||
27 | return | ||
28 | } | ||
29 | |||
30 | set repeats 100 | ||
31 | #set repeats 10 | ||
32 | |||
33 | # The following procedure computes a "signature" for table "abc". If | ||
34 | # abc changes in any way, the signature should change. | ||
35 | proc signature {} { | ||
36 | return [db eval {SELECT count(*), md5sum(a), md5sum(b), md5sum(c) FROM abc}] | ||
37 | } | ||
38 | proc signature2 {} { | ||
39 | return [db eval {SELECT count(*), md5sum(a), md5sum(b), md5sum(c) FROM abc2}] | ||
40 | } | ||
41 | |||
42 | #-------------------------------------------------------------------------- | ||
43 | # Simple crash test: | ||
44 | # | ||
45 | # crash-1.1: Create a database with a table with two rows. | ||
46 | # crash-1.2: Run a 'DELETE FROM abc WHERE a = 1' that crashes during | ||
47 | # the first journal-sync. | ||
48 | # crash-1.3: Ensure the database is in the same state as after crash-1.1. | ||
49 | # crash-1.4: Run a 'DELETE FROM abc WHERE a = 1' that crashes during | ||
50 | # the first database-sync. | ||
51 | # crash-1.5: Ensure the database is in the same state as after crash-1.1. | ||
52 | # crash-1.6: Run a 'DELETE FROM abc WHERE a = 1' that crashes during | ||
53 | # the second journal-sync. | ||
54 | # crash-1.7: Ensure the database is in the same state as after crash-1.1. | ||
55 | # | ||
56 | # Tests 1.8 through 1.11 test for crashes on the third journal sync and | ||
57 | # second database sync. Neither of these is required in such a small test | ||
58 | # case, so these tests are just to verify that the test infrastructure | ||
59 | # operates as expected. | ||
60 | # | ||
61 | do_test crash-1.1 { | ||
62 | execsql { | ||
63 | CREATE TABLE abc(a, b, c); | ||
64 | INSERT INTO abc VALUES(1, 2, 3); | ||
65 | INSERT INTO abc VALUES(4, 5, 6); | ||
66 | } | ||
67 | set ::sig [signature] | ||
68 | expr 0 | ||
69 | } {0} | ||
70 | do_test crash-1.2 { | ||
71 | crashsql -delay 1 -file test.db-journal { | ||
72 | DELETE FROM abc WHERE a = 1; | ||
73 | } | ||
74 | } {1 {child process exited abnormally}} | ||
75 | do_test crash-1.3 { | ||
76 | signature | ||
77 | } $::sig | ||
78 | do_test crash-1.4 { | ||
79 | crashsql -delay 1 -file test.db { | ||
80 | DELETE FROM abc WHERE a = 1; | ||
81 | } | ||
82 | } {1 {child process exited abnormally}} | ||
83 | do_test crash-1.5 { | ||
84 | signature | ||
85 | } $::sig | ||
86 | do_test crash-1.6 { | ||
87 | crashsql -delay 2 -file test.db-journal { | ||
88 | DELETE FROM abc WHERE a = 1; | ||
89 | } | ||
90 | } {1 {child process exited abnormally}} | ||
91 | do_test crash-1.7 { | ||
92 | catchsql { | ||
93 | SELECT * FROM abc; | ||
94 | } | ||
95 | } {0 {1 2 3 4 5 6}} | ||
96 | |||
97 | do_test crash-1.8 { | ||
98 | crashsql -delay 3 -file test.db-journal { | ||
99 | DELETE FROM abc WHERE a = 1; | ||
100 | } | ||
101 | } {0 {}} | ||
102 | do_test crash-1.9 { | ||
103 | catchsql { | ||
104 | SELECT * FROM abc; | ||
105 | } | ||
106 | } {0 {4 5 6}} | ||
107 | do_test crash-1.10 { | ||
108 | crashsql -delay 2 -file test.db { | ||
109 | DELETE FROM abc WHERE a = 4; | ||
110 | } | ||
111 | } {0 {}} | ||
112 | do_test crash-1.11 { | ||
113 | catchsql { | ||
114 | SELECT * FROM abc; | ||
115 | } | ||
116 | } {0 {}} | ||
117 | |||
118 | #-------------------------------------------------------------------------- | ||
119 | # The following tests test recovery when both the database file and the the | ||
120 | # journal file contain corrupt data. This can happen after pages are | ||
121 | # written to the database file before a transaction is committed due to | ||
122 | # cache-pressure. | ||
123 | # | ||
124 | # crash-2.1: Insert 18 pages of data into the database. | ||
125 | # crash-2.2: Check the database file size looks ok. | ||
126 | # crash-2.3: Delete 15 or so pages (with a 10 page page-cache), then crash. | ||
127 | # crash-2.4: Ensure the database is in the same state as after crash-2.1. | ||
128 | # | ||
129 | # Test cases crash-2.5 and crash-2.6 check that the database is OK if the | ||
130 | # crash occurs during the main database file sync. But this isn't really | ||
131 | # different from the crash-1.* cases. | ||
132 | # | ||
133 | do_test crash-2.1 { | ||
134 | execsql { BEGIN } | ||
135 | for {set n 0} {$n < 1000} {incr n} { | ||
136 | execsql "INSERT INTO abc VALUES($n, [expr 2*$n], [expr 3*$n])" | ||
137 | } | ||
138 | execsql { COMMIT } | ||
139 | set ::sig [signature] | ||
140 | execsql { SELECT sum(a), sum(b), sum(c) from abc } | ||
141 | } {499500 999000 1498500} | ||
142 | do_test crash-2.2 { | ||
143 | expr ([file size test.db] / 1024)>16 | ||
144 | } {1} | ||
145 | do_test crash-2.3 { | ||
146 | crashsql -delay 2 -file test.db-journal { | ||
147 | DELETE FROM abc WHERE a < 800; | ||
148 | } | ||
149 | } {1 {child process exited abnormally}} | ||
150 | do_test crash-2.4 { | ||
151 | signature | ||
152 | } $sig | ||
153 | do_test crash-2.5 { | ||
154 | crashsql -delay 1 -file test.db { | ||
155 | DELETE FROM abc WHERE a<800; | ||
156 | } | ||
157 | } {1 {child process exited abnormally}} | ||
158 | do_test crash-2.6 { | ||
159 | signature | ||
160 | } $sig | ||
161 | |||
162 | #-------------------------------------------------------------------------- | ||
163 | # The crash-3.* test cases are essentially the same test as test case | ||
164 | # crash-2.*, but with a more complicated data set. | ||
165 | # | ||
166 | # The test is repeated a few times with different seeds for the random | ||
167 | # number generator in the crashing executable. Because there is no way to | ||
168 | # seed the random number generator directly, some SQL is added to the test | ||
169 | # case to 'use up' a different quantity random numbers before the test SQL | ||
170 | # is executed. | ||
171 | # | ||
172 | |||
173 | # Make sure the file is much bigger than the pager-cache (10 pages). This | ||
174 | # ensures that cache-spills happen regularly. | ||
175 | do_test crash-3.0 { | ||
176 | execsql { | ||
177 | INSERT INTO abc SELECT * FROM abc; | ||
178 | INSERT INTO abc SELECT * FROM abc; | ||
179 | INSERT INTO abc SELECT * FROM abc; | ||
180 | INSERT INTO abc SELECT * FROM abc; | ||
181 | INSERT INTO abc SELECT * FROM abc; | ||
182 | } | ||
183 | expr ([file size test.db] / 1024) > 450 | ||
184 | } {1} | ||
185 | for {set i 1} {$i < $repeats} {incr i} { | ||
186 | set sig [signature] | ||
187 | do_test crash-3.$i.1 { | ||
188 | crashsql -delay [expr $i%5 + 1] -file test.db-journal " | ||
189 | BEGIN; | ||
190 | SELECT random() FROM abc LIMIT $i; | ||
191 | INSERT INTO abc VALUES(randstr(10,10), 0, 0); | ||
192 | DELETE FROM abc WHERE random()%10!=0; | ||
193 | COMMIT; | ||
194 | " | ||
195 | } {1 {child process exited abnormally}} | ||
196 | do_test crash-3.$i.2 { | ||
197 | signature | ||
198 | } $sig | ||
199 | } | ||
200 | |||
201 | #-------------------------------------------------------------------------- | ||
202 | # The following test cases - crash-4.* - test the correct recovery of the | ||
203 | # database when a crash occurs during a multi-file transaction. | ||
204 | # | ||
205 | # crash-4.1.*: Test recovery when crash occurs during sync() of the | ||
206 | # main database journal file. | ||
207 | # crash-4.2.*: Test recovery when crash occurs during sync() of an | ||
208 | # attached database journal file. | ||
209 | # crash-4.3.*: Test recovery when crash occurs during sync() of the master | ||
210 | # journal file. | ||
211 | # | ||
212 | do_test crash-4.0 { | ||
213 | file delete -force test2.db | ||
214 | file delete -force test2.db-journal | ||
215 | execsql { | ||
216 | ATTACH 'test2.db' AS aux; | ||
217 | PRAGMA aux.default_cache_size = 10; | ||
218 | CREATE TABLE aux.abc2 AS SELECT 2*a as a, 2*b as b, 2*c as c FROM abc; | ||
219 | } | ||
220 | expr ([file size test2.db] / 1024) > 450 | ||
221 | } {1} | ||
222 | |||
223 | set fin 0 | ||
224 | for {set i 1} {$i<$repeats} {incr i} { | ||
225 | set sig [signature] | ||
226 | set sig2 [signature2] | ||
227 | do_test crash-4.1.$i.1 { | ||
228 | set c [crashsql -delay $i -file test.db-journal " | ||
229 | ATTACH 'test2.db' AS aux; | ||
230 | BEGIN; | ||
231 | SELECT randstr($i,$i) FROM abc LIMIT $i; | ||
232 | INSERT INTO abc VALUES(randstr(10,10), 0, 0); | ||
233 | DELETE FROM abc WHERE random()%10!=0; | ||
234 | INSERT INTO abc2 VALUES(randstr(10,10), 0, 0); | ||
235 | DELETE FROM abc2 WHERE random()%10!=0; | ||
236 | COMMIT; | ||
237 | "] | ||
238 | if { $c == {0 {}} } { | ||
239 | set ::fin 1 | ||
240 | set c {1 {child process exited abnormally}} | ||
241 | } | ||
242 | set c | ||
243 | } {1 {child process exited abnormally}} | ||
244 | if {$::fin} break | ||
245 | do_test crash-4.1.$i.2 { | ||
246 | signature | ||
247 | } $sig | ||
248 | do_test crash-4.1.$i.3 { | ||
249 | signature2 | ||
250 | } $sig2 | ||
251 | } | ||
252 | set i 0 | ||
253 | set fin 0 | ||
254 | while {[incr i]} { | ||
255 | set sig [signature] | ||
256 | set sig2 [signature2] | ||
257 | set ::fin 0 | ||
258 | do_test crash-4.2.$i.1 { | ||
259 | set c [crashsql -delay $i -file test2.db-journal " | ||
260 | ATTACH 'test2.db' AS aux; | ||
261 | BEGIN; | ||
262 | SELECT randstr($i,$i) FROM abc LIMIT $i; | ||
263 | INSERT INTO abc VALUES(randstr(10,10), 0, 0); | ||
264 | DELETE FROM abc WHERE random()%10!=0; | ||
265 | INSERT INTO abc2 VALUES(randstr(10,10), 0, 0); | ||
266 | DELETE FROM abc2 WHERE random()%10!=0; | ||
267 | COMMIT; | ||
268 | "] | ||
269 | if { $c == {0 {}} } { | ||
270 | set ::fin 1 | ||
271 | set c {1 {child process exited abnormally}} | ||
272 | } | ||
273 | set c | ||
274 | } {1 {child process exited abnormally}} | ||
275 | if { $::fin } break | ||
276 | do_test crash-4.2.$i.2 { | ||
277 | signature | ||
278 | } $sig | ||
279 | do_test crash-4.2.$i.3 { | ||
280 | signature2 | ||
281 | } $sig2 | ||
282 | } | ||
283 | for {set i 1} {$i < 5} {incr i} { | ||
284 | set sig [signature] | ||
285 | set sig2 [signature2] | ||
286 | do_test crash-4.3.$i.1 { | ||
287 | crashsql -delay 1 -file test.db-mj* " | ||
288 | ATTACH 'test2.db' AS aux; | ||
289 | BEGIN; | ||
290 | SELECT random() FROM abc LIMIT $i; | ||
291 | INSERT INTO abc VALUES(randstr(10,10), 0, 0); | ||
292 | DELETE FROM abc WHERE random()%10!=0; | ||
293 | INSERT INTO abc2 VALUES(randstr(10,10), 0, 0); | ||
294 | DELETE FROM abc2 WHERE random()%10!=0; | ||
295 | COMMIT; | ||
296 | " | ||
297 | } {1 {child process exited abnormally}} | ||
298 | do_test crash-4.3.$i.2 { | ||
299 | signature | ||
300 | } $sig | ||
301 | do_test crash-4.3.$i.3 { | ||
302 | signature2 | ||
303 | } $sig2 | ||
304 | } | ||
305 | |||
306 | #-------------------------------------------------------------------------- | ||
307 | # The following test cases - crash-5.* - exposes a bug that existed in the | ||
308 | # sqlite3pager_movepage() API used by auto-vacuum databases. | ||
309 | # database when a crash occurs during a multi-file transaction. See comments | ||
310 | # in test crash-5.3 for details. | ||
311 | # | ||
312 | db close | ||
313 | file delete -force test.db | ||
314 | sqlite3 db test.db | ||
315 | do_test crash-5.1 { | ||
316 | execsql { | ||
317 | CREATE TABLE abc(a, b, c); -- Root page 3 | ||
318 | INSERT INTO abc VALUES(randstr(1500,1500), 0, 0); -- Overflow page 4 | ||
319 | INSERT INTO abc SELECT * FROM abc; | ||
320 | INSERT INTO abc SELECT * FROM abc; | ||
321 | INSERT INTO abc SELECT * FROM abc; | ||
322 | } | ||
323 | } {} | ||
324 | do_test crash-5.2 { | ||
325 | expr [file size test.db] / 1024 | ||
326 | } [expr [string match [execsql {pragma auto_vacuum}] 1] ? 11 : 10] | ||
327 | set sig [signature] | ||
328 | do_test crash-5.3 { | ||
329 | # The SQL below is used to expose a bug that existed in | ||
330 | # sqlite3pager_movepage() during development of the auto-vacuum feature. It | ||
331 | # functions as follows: | ||
332 | # | ||
333 | # 1: Begin a transaction. | ||
334 | # 2: Put page 4 on the free-list (was the overflow page for the row deleted). | ||
335 | # 3: Write data to page 4 (it becomes the overflow page for the row inserted). | ||
336 | # The old page 4 data has been written to the journal file, but the | ||
337 | # journal file has not been sync()hronized. | ||
338 | # 4: Create a table, which calls sqlite3pager_movepage() to move page 4 | ||
339 | # to the end of the database (page 12) to make room for the new root-page. | ||
340 | # 5: Put pressure on the pager-cache. This results in page 4 being written | ||
341 | # to the database file to make space in the cache to load a new page. The | ||
342 | # bug was that page 4 was written to the database file before the journal | ||
343 | # is sync()hronized. | ||
344 | # 6: Commit. A crash occurs during the sync of the journal file. | ||
345 | # | ||
346 | # End result: Before the bug was fixed, data has been written to page 4 of the | ||
347 | # database file and the journal file does not contain trustworthy rollback | ||
348 | # data for this page. | ||
349 | # | ||
350 | crashsql -delay 1 -file test.db-journal { | ||
351 | BEGIN; -- 1 | ||
352 | DELETE FROM abc WHERE oid = 1; -- 2 | ||
353 | INSERT INTO abc VALUES(randstr(1500,1500), 0, 0); -- 3 | ||
354 | CREATE TABLE abc2(a, b, c); -- 4 | ||
355 | SELECT * FROM abc; -- 5 | ||
356 | COMMIT; -- 6 | ||
357 | } | ||
358 | } {1 {child process exited abnormally}} | ||
359 | integrity_check crash-5.4 | ||
360 | do_test crash-5.5 { | ||
361 | signature | ||
362 | } $sig | ||
363 | |||
364 | #-------------------------------------------------------------------------- | ||
365 | # The following test cases - crash-6.* - test that a DROP TABLE operation | ||
366 | # is correctly rolled back in the event of a crash while the database file | ||
367 | # is being written. This is mainly to test that all pages are written to the | ||
368 | # journal file before truncation in an auto-vacuum database. | ||
369 | # | ||
370 | do_test crash-6.1 { | ||
371 | crashsql -delay 1 -file test.db { | ||
372 | DROP TABLE abc; | ||
373 | } | ||
374 | } {1 {child process exited abnormally}} | ||
375 | do_test crash-6.2 { | ||
376 | signature | ||
377 | } $sig | ||
378 | |||
379 | #-------------------------------------------------------------------------- | ||
380 | # These test cases test the case where the master journal file name is | ||
381 | # corrupted slightly so that the corruption has to be detected by the | ||
382 | # checksum. | ||
383 | do_test crash-7.1 { | ||
384 | crashsql -delay 1 -file test.db { | ||
385 | ATTACH 'test2.db' AS aux; | ||
386 | BEGIN; | ||
387 | INSERT INTO abc VALUES(randstr(1500,1500), 0, 0); | ||
388 | INSERT INTO abc2 VALUES(randstr(1500,1500), 0, 0); | ||
389 | COMMIT; | ||
390 | } | ||
391 | |||
392 | # Change the checksum value for the master journal name. | ||
393 | set f [open test.db-journal a] | ||
394 | fconfigure $f -encoding binary | ||
395 | seek $f [expr [file size test.db-journal] - 12] | ||
396 | puts -nonewline $f "\00\00\00\00" | ||
397 | close $f | ||
398 | } {} | ||
399 | do_test crash-7.2 { | ||
400 | signature | ||
401 | } $sig | ||
402 | |||
403 | finish_test | ||