blob: 87ffda35b8ef80559f62eb94df0021d9b6ac61ca [file] [log] [blame]
danb0ac3e32010-06-16 10:55:42 +00001# 2010 June 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#
12
13set testdir [file dirname $argv0]
14source $testdir/tester.tcl
15source $testdir/lock_common.tcl
16source $testdir/malloc_common.tcl
17
18set a_string_counter 1
19proc a_string {n} {
20 global a_string_counter
21 incr a_string_counter
22 string range [string repeat "${a_string_counter}." $n] 1 $n
23}
24db func a_string a_string
25
dan273f3f02010-06-26 15:42:33 +000026if 1 {
27
danb0ac3e32010-06-16 10:55:42 +000028#-------------------------------------------------------------------------
29# Test fault-injection while rolling back a hot-journal file.
30#
31do_test pagerfault-1-pre1 {
32 execsql {
33 PRAGMA journal_mode = DELETE;
34 PRAGMA cache_size = 10;
35 CREATE TABLE t1(a UNIQUE, b UNIQUE);
36 INSERT INTO t1 VALUES(a_string(200), a_string(300));
37 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
38 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
39 BEGIN;
40 INSERT INTO t1 SELECT a_string(201), a_string(301) FROM t1;
41 INSERT INTO t1 SELECT a_string(202), a_string(302) FROM t1;
42 INSERT INTO t1 SELECT a_string(203), a_string(303) FROM t1;
43 INSERT INTO t1 SELECT a_string(204), a_string(304) FROM t1;
44 }
45 faultsim_save_and_close
46} {}
47do_faultsim_test pagerfault-1 -prep {
48 faultsim_restore_and_reopen
49} -body {
50 execsql { SELECT count(*) FROM t1 }
51} -test {
52 faultsim_test_result {0 4}
53 faultsim_integrity_check
54 if {[db one { SELECT count(*) FROM t1 }] != 4} {
55 error "Database content appears incorrect"
56 }
57}
58
59#-------------------------------------------------------------------------
dande4996e2010-06-19 11:30:41 +000060# Test fault-injection while rolling back a hot-journal file with a
61# page-size different from the current value stored on page 1 of the
62# database file.
63#
64do_test pagerfault-2-pre1 {
65 testvfs tv -default 1
66 tv filter xSync
67 tv script xSyncCb
68 proc xSyncCb {filename args} {
69 if {[string match *journal filename]==0} faultsim_save
70 }
71 faultsim_delete_and_reopen
72 execsql {
73 PRAGMA page_size = 4096;
74 BEGIN;
75 CREATE TABLE abc(a, b, c);
76 INSERT INTO abc VALUES('o', 't', 't');
77 INSERT INTO abc VALUES('f', 'f', 's');
78 INSERT INTO abc SELECT * FROM abc; -- 4
79 INSERT INTO abc SELECT * FROM abc; -- 8
80 INSERT INTO abc SELECT * FROM abc; -- 16
81 INSERT INTO abc SELECT * FROM abc; -- 32
82 INSERT INTO abc SELECT * FROM abc; -- 64
83 INSERT INTO abc SELECT * FROM abc; -- 128
84 INSERT INTO abc SELECT * FROM abc; -- 256
85 COMMIT;
86 PRAGMA page_size = 1024;
87 VACUUM;
88 }
89 db close
90 tv delete
91} {}
92do_faultsim_test pagerfault-2 -prep {
93 faultsim_restore_and_reopen
94} -body {
95 execsql { SELECT * FROM abc }
96} -test {
97 set answer [split [string repeat "ottffs" 128] ""]
98 faultsim_test_result [list 0 $answer]
99 faultsim_integrity_check
100 set res [db eval { SELECT * FROM abc }]
101 if {$res != $answer} { error "Database content appears incorrect ($res)" }
danec6ffc12010-06-24 19:16:06 +0000102}
dande4996e2010-06-19 11:30:41 +0000103
104#-------------------------------------------------------------------------
danb0ac3e32010-06-16 10:55:42 +0000105# Test fault-injection while rolling back hot-journals that were created
106# as part of a multi-file transaction.
107#
dande4996e2010-06-19 11:30:41 +0000108do_test pagerfault-3-pre1 {
danb0ac3e32010-06-16 10:55:42 +0000109 testvfs tstvfs -default 1
110 tstvfs filter xDelete
111 tstvfs script xDeleteCallback
112
113 proc xDeleteCallback {method file args} {
114 set file [file tail $file]
115 if { [string match *mj* $file] } { faultsim_save }
116 }
117
118 faultsim_delete_and_reopen
119 db func a_string a_string
120
121 execsql {
122 ATTACH 'test.db2' AS aux;
123 PRAGMA journal_mode = DELETE;
124 PRAGMA main.cache_size = 10;
125 PRAGMA aux.cache_size = 10;
126
127 CREATE TABLE t1(a UNIQUE, b UNIQUE);
128 CREATE TABLE aux.t2(a UNIQUE, b UNIQUE);
129 INSERT INTO t1 VALUES(a_string(200), a_string(300));
130 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
131 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
132 INSERT INTO t2 SELECT * FROM t1;
133
134 BEGIN;
135 INSERT INTO t1 SELECT a_string(201), a_string(301) FROM t1;
136 INSERT INTO t1 SELECT a_string(202), a_string(302) FROM t1;
137 INSERT INTO t1 SELECT a_string(203), a_string(303) FROM t1;
138 INSERT INTO t1 SELECT a_string(204), a_string(304) FROM t1;
139 REPLACE INTO t2 SELECT * FROM t1;
140 COMMIT;
141 }
142
143 db close
144 tstvfs delete
145} {}
danec6ffc12010-06-24 19:16:06 +0000146do_faultsim_test pagerfault-3 -prep {
danb0ac3e32010-06-16 10:55:42 +0000147 faultsim_restore_and_reopen
148} -body {
149 execsql {
150 ATTACH 'test.db2' AS aux;
151 SELECT count(*) FROM t2;
152 SELECT count(*) FROM t1;
153 }
154} -test {
155 faultsim_test_result {0 {4 4}} {1 {unable to open database: test.db2}}
156 faultsim_integrity_check
danb0ac3e32010-06-16 10:55:42 +0000157 catchsql { ATTACH 'test.db2' AS aux }
158 if {[db one { SELECT count(*) FROM t1 }] != 4
159 || [db one { SELECT count(*) FROM t2 }] != 4
160 } {
161 error "Database content appears incorrect"
162 }
163}
164
dande4996e2010-06-19 11:30:41 +0000165#-------------------------------------------------------------------------
166# Test fault-injection as part of a vanilla, no-transaction, INSERT
167# statement.
168#
169do_faultsim_test pagerfault-4 -prep {
170 faultsim_delete_and_reopen
171} -body {
172 execsql {
173 CREATE TABLE x(y);
174 INSERT INTO x VALUES('z');
175 SELECT * FROM x;
176 }
177} -test {
178 faultsim_test_result {0 z}
179 faultsim_integrity_check
180}
181
182#-------------------------------------------------------------------------
183# Test fault-injection as part of a commit when using journal_mode=PERSIST.
dan146ed782010-06-19 17:26:37 +0000184# Three different cases:
185#
186# pagerfault-5.1: With no journal_size_limit configured.
187# pagerfault-5.2: With a journal_size_limit configured.
188# pagerfault-5.4: Multi-file transaction. One connection has a
189# journal_size_limit of 0, the other has no limit.
dande4996e2010-06-19 11:30:41 +0000190#
191do_test pagerfault-5-pre1 {
192 faultsim_delete_and_reopen
193 db func a_string a_string
194 execsql {
195 CREATE TABLE t1(a UNIQUE, b UNIQUE);
196 INSERT INTO t1 VALUES(a_string(200), a_string(300));
197 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
198 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
199 }
200 faultsim_save_and_close
201} {}
202do_faultsim_test pagerfault-5.1 -prep {
203 faultsim_restore_and_reopen
204 db func a_string a_string
205 execsql { PRAGMA journal_mode = PERSIST }
206} -body {
207 execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
208} -test {
209 faultsim_test_result {0 {}}
210 faultsim_integrity_check
211}
212do_faultsim_test pagerfault-5.2 -prep {
213 faultsim_restore_and_reopen
214 db func a_string a_string
215 execsql {
216 PRAGMA journal_mode = PERSIST;
217 PRAGMA journal_size_limit = 2048;
218 }
219} -body {
220 execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
221} -test {
222 faultsim_test_result {0 {}}
223 faultsim_integrity_check
224}
danec6ffc12010-06-24 19:16:06 +0000225do_faultsim_test pagerfault-5.3 -faults oom-transient -prep {
dande4996e2010-06-19 11:30:41 +0000226 faultsim_restore_and_reopen
227 db func a_string a_string
228 file delete -force test2.db test2.db-journal test2.db-wal
229 execsql {
230 PRAGMA journal_mode = PERSIST;
231 ATTACH 'test2.db' AS aux;
232 PRAGMA aux.journal_mode = PERSIST;
233 PRAGMA aux.journal_size_limit = 0;
234 }
235} -body {
236 execsql {
237 BEGIN;
238 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
239 CREATE TABLE aux.t2 AS SELECT * FROM t1;
240 COMMIT;
241 }
242} -test {
243 faultsim_test_result {0 {}}
danec6ffc12010-06-24 19:16:06 +0000244
danf43d7fc2010-07-03 10:00:00 +0000245 catchsql { COMMIT }
246 catchsql { ROLLBACK }
247
248 faultsim_integrity_check
danec6ffc12010-06-24 19:16:06 +0000249 set res ""
250 set rc [catch { set res [db one { PRAGMA aux.integrity_check }] }]
251 if {$rc!=0 || $res != "ok"} {error "integrity-check problem:$rc $res"}
dande4996e2010-06-19 11:30:41 +0000252}
253
dan153eda02010-06-21 07:45:47 +0000254#-------------------------------------------------------------------------
255# Test fault-injection as part of a commit when using
256# journal_mode=TRUNCATE.
257#
258do_test pagerfault-6-pre1 {
259 faultsim_delete_and_reopen
260 db func a_string a_string
261 execsql {
262 CREATE TABLE t1(a UNIQUE, b UNIQUE);
263 INSERT INTO t1 VALUES(a_string(200), a_string(300));
264 }
265 faultsim_save_and_close
266} {}
danf9b44192010-06-25 19:09:48 +0000267
dan153eda02010-06-21 07:45:47 +0000268do_faultsim_test pagerfault-6.1 -prep {
269 faultsim_restore_and_reopen
270 db func a_string a_string
271 execsql { PRAGMA journal_mode = TRUNCATE }
272} -body {
273 execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
danf9b44192010-06-25 19:09:48 +0000274 execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
275} -test {
276 faultsim_test_result {0 {}}
277 faultsim_integrity_check
278}
279
280# The unix vfs xAccess() method considers a file zero bytes in size to
281# "not exist". This proc overrides that behaviour so that a zero length
282# file is considered to exist.
283#
284proc xAccess {method filename op args} {
285 if {$op != "SQLITE_ACCESS_EXISTS"} { return "" }
286 return [file exists $filename]
287}
288do_faultsim_test pagerfault-6.2 -faults cantopen-* -prep {
289 shmfault filter xAccess
290 shmfault script xAccess
291
292 faultsim_restore_and_reopen
293 db func a_string a_string
294 execsql { PRAGMA journal_mode = TRUNCATE }
295} -body {
296 execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
297 execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
dan153eda02010-06-21 07:45:47 +0000298} -test {
299 faultsim_test_result {0 {}}
300 faultsim_integrity_check
301}
302
dan146ed782010-06-19 17:26:37 +0000303# The following was an attempt to get a bitvec malloc to fail. Didn't work.
304#
305# do_test pagerfault-6-pre1 {
306# faultsim_delete_and_reopen
307# execsql {
308# CREATE TABLE t1(x, y, UNIQUE(x, y));
309# INSERT INTO t1 VALUES(1, randomblob(1501));
310# INSERT INTO t1 VALUES(2, randomblob(1502));
311# INSERT INTO t1 VALUES(3, randomblob(1503));
312# INSERT INTO t1 VALUES(4, randomblob(1504));
313# INSERT INTO t1
314# SELECT x, randomblob(1500+oid+(SELECT max(oid) FROM t1)) FROM t1;
315# INSERT INTO t1
316# SELECT x, randomblob(1500+oid+(SELECT max(oid) FROM t1)) FROM t1;
317# INSERT INTO t1
318# SELECT x, randomblob(1500+oid+(SELECT max(oid) FROM t1)) FROM t1;
319# INSERT INTO t1
320# SELECT x, randomblob(1500+oid+(SELECT max(oid) FROM t1)) FROM t1;
321# }
322# faultsim_save_and_close
323# } {}
324# do_faultsim_test pagerfault-6 -prep {
325# faultsim_restore_and_reopen
326# } -body {
327# execsql {
328# BEGIN;
329# UPDATE t1 SET x=x+4 WHERE x=1;
330# SAVEPOINT one;
331# UPDATE t1 SET x=x+4 WHERE x=2;
332# SAVEPOINT three;
333# UPDATE t1 SET x=x+4 WHERE x=3;
334# SAVEPOINT four;
335# UPDATE t1 SET x=x+4 WHERE x=4;
336# RELEASE three;
337# COMMIT;
338# SELECT DISTINCT x FROM t1;
339# }
340# } -test {
341# faultsim_test_result {0 {5 6 7 8}}
342# faultsim_integrity_check
343# }
dan346e4262010-06-23 19:27:36 +0000344#
dandca321a2010-06-24 10:50:17 +0000345
346# This is designed to provoke a special case in the pager code:
347#
348# If an error (specifically, a FULL or IOERR error) occurs while writing a
349# dirty page to the file-system in order to free up memory, the pager enters
350# the "error state". An IO error causes SQLite to roll back the current
351# transaction (exiting the error state). A FULL error, however, may only
352# rollback the current statement.
353#
354# This block tests that nothing goes wrong if a FULL error occurs while
355# writing a dirty page out to free memory from within a statement that has
356# opened a statement transaction.
357#
dan346e4262010-06-23 19:27:36 +0000358do_test pagerfault-7-pre1 {
359 faultsim_delete_and_reopen
360 execsql {
361 CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
362 BEGIN;
363 INSERT INTO t2 VALUES(NULL, randomblob(1500));
364 INSERT INTO t2 VALUES(NULL, randomblob(1500));
365 INSERT INTO t2 SELECT NULL, randomblob(1500) FROM t2; -- 4
366 INSERT INTO t2 SELECT NULL, randomblob(1500) FROM t2; -- 8
367 INSERT INTO t2 SELECT NULL, randomblob(1500) FROM t2; -- 16
368 INSERT INTO t2 SELECT NULL, randomblob(1500) FROM t2; -- 32
369 INSERT INTO t2 SELECT NULL, randomblob(1500) FROM t2; -- 64
370 COMMIT;
371 CREATE TABLE t1(a PRIMARY KEY, b);
372 INSERT INTO t1 SELECT * FROM t2;
373 DROP TABLE t2;
374 }
375 faultsim_save_and_close
376} {}
danec6ffc12010-06-24 19:16:06 +0000377do_faultsim_test pagerfault-7 -prep {
dan346e4262010-06-23 19:27:36 +0000378 faultsim_restore_and_reopen
379 execsql {
380 PRAGMA cache_size = 10;
381 BEGIN;
382 UPDATE t1 SET b = randomblob(1500);
383 }
384} -body {
385 execsql { UPDATE t1 SET a = 65, b = randomblob(1500) WHERE (a+1)>200 }
386 execsql COMMIT
387} -test {
388 faultsim_test_result {0 {}}
389 faultsim_integrity_check
390}
dan146ed782010-06-19 17:26:37 +0000391
dandca321a2010-06-24 10:50:17 +0000392do_test pagerfault-8-pre1 {
393 faultsim_delete_and_reopen
394 execsql {
395 PRAGMA auto_vacuum = 1;
396 CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
397 BEGIN;
398 INSERT INTO t1 VALUES(NULL, randomblob(1500));
399 INSERT INTO t1 VALUES(NULL, randomblob(1500));
400 INSERT INTO t1 SELECT NULL, randomblob(1500) FROM t1; -- 4
401 INSERT INTO t1 SELECT NULL, randomblob(1500) FROM t1; -- 8
402 INSERT INTO t1 SELECT NULL, randomblob(1500) FROM t1; -- 16
403 INSERT INTO t1 SELECT NULL, randomblob(1500) FROM t1; -- 32
404 INSERT INTO t1 SELECT NULL, randomblob(1500) FROM t1; -- 64
405 COMMIT;
406 }
407 faultsim_save_and_close
408 set filesize [file size test.db]
409 set {} {}
410} {}
411do_test pagerfault-8-pre2 {
412 faultsim_restore_and_reopen
413 execsql { DELETE FROM t1 WHERE a>32 }
414 expr {[file size test.db] < $filesize}
415} {1}
dandca321a2010-06-24 10:50:17 +0000416do_faultsim_test pagerfault-8 -prep {
417 faultsim_restore_and_reopen
418 execsql {
419 BEGIN;
420 DELETE FROM t1 WHERE a>32;
421 }
422} -body {
423 execsql COMMIT
424} -test {
425 faultsim_test_result {0 {}}
426 faultsim_integrity_check
427}
428
dan273f3f02010-06-26 15:42:33 +0000429#-------------------------------------------------------------------------
430# This test case is specially designed so that during a savepoint
431# rollback, a new cache entry must be allocated (see comments surrounding
432# the call to sqlite3PagerAcquire() from within pager_playback_one_page()
433# for details). Test the effects of injecting an OOM at this point.
434#
dan0a6052e2010-06-24 13:24:26 +0000435do_test pagerfault-9-pre1 {
436 faultsim_delete_and_reopen
437 execsql {
438 PRAGMA auto_vacuum = incremental;
439 CREATE TABLE t1(x);
440 CREATE TABLE t2(y);
441 CREATE TABLE t3(z);
442
443 INSERT INTO t1 VALUES(randomblob(900));
444 INSERT INTO t1 VALUES(randomblob(900));
445 DELETE FROM t1;
446 }
447 faultsim_save_and_close
448} {}
dan273f3f02010-06-26 15:42:33 +0000449do_faultsim_test pagerfault-9.1 -prep {
dan0a6052e2010-06-24 13:24:26 +0000450 faultsim_restore_and_reopen
451 execsql {
452 BEGIN;
453 INSERT INTO t1 VALUES(randomblob(900));
454 INSERT INTO t1 VALUES(randomblob(900));
455 DROP TABLE t3;
456 DROP TABLE t2;
457 SAVEPOINT abc;
458 PRAGMA incremental_vacuum;
459 }
460} -body {
461 execsql {
462 ROLLBACK TO abc;
463 COMMIT;
464 PRAGMA freelist_count
465 }
466} -test {
467 faultsim_test_result {0 2}
468 faultsim_integrity_check
469
470 set sl [db one { SELECT COALESCE(sum(length(x)), 'null') FROM t1 }]
471 if {$sl!="null" && $sl!=1800} {
472 error "Content looks no good... ($sl)"
473 }
474}
475
dan273f3f02010-06-26 15:42:33 +0000476#-------------------------------------------------------------------------
477# Test fault injection with a temporary database file.
478#
danc8ce3972010-06-29 10:30:23 +0000479foreach v {a b} {
480 do_faultsim_test pagerfault-10$v -prep {
481 sqlite3 db ""
482 db func a_string a_string;
483 execsql {
484 PRAGMA cache_size = 10;
485 BEGIN;
486 CREATE TABLE xx(a, b, UNIQUE(a, b));
487 INSERT INTO xx VALUES(a_string(200), a_string(200));
488 INSERT INTO xx SELECT a_string(200), a_string(200) FROM xx;
489 INSERT INTO xx SELECT a_string(200), a_string(200) FROM xx;
490 INSERT INTO xx SELECT a_string(200), a_string(200) FROM xx;
491 INSERT INTO xx SELECT a_string(200), a_string(200) FROM xx;
492 COMMIT;
493 }
494 } -body {
495 execsql { UPDATE xx SET a = a_string(300) }
496 } -test {
497 faultsim_test_result {0 {}}
498 if {$::v == "b"} { execsql { PRAGMA journal_mode = TRUNCATE } }
499 faultsim_integrity_check
500 faultsim_integrity_check
dan273f3f02010-06-26 15:42:33 +0000501 }
dan273f3f02010-06-26 15:42:33 +0000502}
503
dan273f3f02010-06-26 15:42:33 +0000504#-------------------------------------------------------------------------
505# Test fault injection with transaction savepoints (savepoints created
506# when a SAVEPOINT command is executed outside of any other savepoint
507# or transaction context).
508#
509do_test pagerfault-9-pre1 {
510 faultsim_delete_and_reopen
511 db func a_string a_string;
512 execsql {
513 PRAGMA auto_vacuum = on;
514 CREATE TABLE t1(x UNIQUE);
515 CREATE TABLE t2(y UNIQUE);
516 CREATE TABLE t3(z UNIQUE);
517 BEGIN;
518 INSERT INTO t1 VALUES(a_string(202));
519 INSERT INTO t2 VALUES(a_string(203));
520 INSERT INTO t3 VALUES(a_string(204));
521 INSERT INTO t1 SELECT a_string(202) FROM t1;
522 INSERT INTO t1 SELECT a_string(203) FROM t1;
523 INSERT INTO t1 SELECT a_string(204) FROM t1;
524 INSERT INTO t1 SELECT a_string(205) FROM t1;
525 INSERT INTO t2 SELECT a_string(length(x)) FROM t1;
526 INSERT INTO t3 SELECT a_string(length(x)) FROM t1;
527 COMMIT;
528 }
529 faultsim_save_and_close
530} {}
531do_faultsim_test pagerfault-11 -prep {
532 faultsim_restore_and_reopen
533 execsql { PRAGMA cache_size = 10 }
534} -body {
535 execsql {
536 SAVEPOINT trans;
537 UPDATE t2 SET y = y||'2';
538 INSERT INTO t3 SELECT * FROM t2;
539 DELETE FROM t1;
540 ROLLBACK TO trans;
541 UPDATE t1 SET x = x||'3';
542 INSERT INTO t2 SELECT * FROM t1;
543 DELETE FROM t3;
544 RELEASE trans;
545 }
546} -test {
547 faultsim_test_result {0 {}}
548 faultsim_integrity_check
549}
550
danc396d4a2010-07-02 11:27:43 +0000551}
552
553
danc8ce3972010-06-29 10:30:23 +0000554#-------------------------------------------------------------------------
555# Test fault injection when writing to a database file that resides on
556# a file-system with a sector-size larger than the database page-size.
557#
dand3533312010-06-28 19:04:02 +0000558do_test pagerfault-12-pre1 {
559 testvfs ss_layer -default 1
560 ss_layer sectorsize 4096
561 faultsim_delete_and_reopen
562 db func a_string a_string;
563
564 execsql {
565 PRAGMA page_size = 1024;
566 PRAGMA journal_mode = PERSIST;
567 PRAGMA cache_size = 10;
568 BEGIN;
569 CREATE TABLE t1(x, y UNIQUE);
570 INSERT INTO t1 VALUES(a_string(333), a_string(444));
571 INSERT INTO t1 SELECT a_string(333+rowid), a_string(444+rowid) FROM t1;
572 INSERT INTO t1 SELECT a_string(333+rowid), a_string(444+rowid) FROM t1;
573 INSERT INTO t1 SELECT a_string(333+rowid), a_string(444+rowid) FROM t1;
574 INSERT INTO t1 SELECT a_string(333+rowid), a_string(444+rowid) FROM t1;
575 INSERT INTO t1 SELECT a_string(44), a_string(55) FROM t1 LIMIT 13;
576 COMMIT;
577 }
578 faultsim_save_and_close
579} {}
danc396d4a2010-07-02 11:27:43 +0000580
581do_faultsim_test pagerfault-12a -prep {
dand3533312010-06-28 19:04:02 +0000582 faultsim_restore_and_reopen
583 execsql { PRAGMA cache_size = 10 }
584 db func a_string a_string;
585} -body {
586 execsql {
587 UPDATE t1 SET x = a_string(length(x)), y = a_string(length(y));
588 }
589} -test {
590 faultsim_test_result {0 {}}
591 faultsim_integrity_check
592}
593
danc396d4a2010-07-02 11:27:43 +0000594do_test pagerfault-12-pre2 {
595 faultsim_restore_and_reopen
596 execsql {
597 CREATE TABLE t2 AS SELECT * FROM t1 LIMIT 10;
598 }
599 faultsim_save_and_close
600} {}
601do_faultsim_test pagerfault-12b -prep {
602 faultsim_restore_and_reopen
603 db func a_string a_string;
604 execsql { SELECT * FROM t1 }
605} -body {
606 set sql(1) { UPDATE t2 SET x = a_string(280) }
607 set sql(2) { UPDATE t1 SET x = a_string(280) WHERE rowid = 5 }
608
609 db eval { SELECT rowid FROM t1 LIMIT 2 } { db eval $sql($rowid) }
610
611} -test {
612 faultsim_test_result {0 {}}
613 faultsim_integrity_check
614}
615
616catch { db close }
617ss_layer delete
618
dand3533312010-06-28 19:04:02 +0000619
danc8ce3972010-06-29 10:30:23 +0000620#-------------------------------------------------------------------------
danba3cbf32010-06-30 04:29:03 +0000621# Test fault injection when SQLite opens a database where the size of the
622# database file is zero bytes but the accompanying journal file is larger
623# than that. In this scenario SQLite should delete the journal file
624# without rolling it back, even if it is in all other respects a valid
625# hot-journal file.
danc8ce3972010-06-29 10:30:23 +0000626#
627do_test pagerfault-13-pre1 {
628 faultsim_delete_and_reopen
629 db func a_string a_string;
630 execsql {
631 PRAGMA journal_mode = PERSIST;
632 BEGIN;
633 CREATE TABLE t1(x, y UNIQUE);
634 INSERT INTO t1 VALUES(a_string(333), a_string(444));
635 COMMIT;
636 }
637 db close
638 file delete -force test.db
639 faultsim_save
640} {}
641do_faultsim_test pagerfault-13 -prep {
642 faultsim_restore_and_reopen
643} -body {
644 execsql { CREATE TABLE xx(a, b) }
645} -test {
646 faultsim_test_result {0 {}}
647}
648
danba3cbf32010-06-30 04:29:03 +0000649#---------------------------------------------------------------------------
650# Test fault injection into a small backup operation.
651#
652do_test pagerfault-14-pre1 {
653 faultsim_delete_and_reopen
654 db func a_string a_string;
655 execsql {
656 PRAGMA journal_mode = PERSIST;
657 ATTACH 'test.db2' AS two;
658 BEGIN;
659 CREATE TABLE t1(x, y UNIQUE);
660 CREATE TABLE two.t2(x, y UNIQUE);
661 INSERT INTO t1 VALUES(a_string(333), a_string(444));
662 INSERT INTO t2 VALUES(a_string(333), a_string(444));
663 COMMIT;
664 }
665 faultsim_save_and_close
666} {}
dan6b63ab42010-06-30 10:36:18 +0000667
668do_faultsim_test pagerfault-14a -prep {
danba3cbf32010-06-30 04:29:03 +0000669 faultsim_restore_and_reopen
670} -body {
671 if {[catch {db backup test.db2} msg]} { error [regsub {.*: } $msg {}] }
672} -test {
673 faultsim_test_result {0 {}} {1 {}} {1 {SQL logic error or missing database}}
danc8ce3972010-06-29 10:30:23 +0000674}
dan6b63ab42010-06-30 10:36:18 +0000675do_faultsim_test pagerfault-14b -prep {
676 faultsim_restore_and_reopen
677 sqlite3 db2 ""
678 db2 eval { PRAGMA page_size = 4096; CREATE TABLE xx(a) }
679} -body {
680 sqlite3_backup B db2 main db main
681 B step 200
682 set rc [B finish]
683 if {[string match SQLITE_IOERR_* $rc]} {set rc SQLITE_IOERR}
684 if {$rc != "SQLITE_OK"} { error [sqlite3_test_errstr $rc] }
685 set {} {}
686} -test {
687 faultsim_test_result {0 {}}
688}
dand0b0d4d2010-07-01 19:01:56 +0000689do_faultsim_test pagerfault-14c -prep {
690 faultsim_restore_and_reopen
691 sqlite3 db2 test.db2
692 db2 eval {
693 PRAGMA synchronous = off;
694 PRAGMA page_size = 4096;
695 CREATE TABLE xx(a);
696 }
697} -body {
698 sqlite3_backup B db2 main db main
699 B step 200
700 set rc [B finish]
701 if {[string match SQLITE_IOERR_* $rc]} {set rc SQLITE_IOERR}
702 if {$rc != "SQLITE_OK"} { error [sqlite3_test_errstr $rc] }
703 set {} {}
704} -test {
705 faultsim_test_result {0 {}}
706}
danc8ce3972010-06-29 10:30:23 +0000707
danba3cbf32010-06-30 04:29:03 +0000708do_test pagerfault-15-pre1 {
709 faultsim_delete_and_reopen
710 db func a_string a_string;
711 execsql {
712 BEGIN;
713 CREATE TABLE t1(x, y UNIQUE);
714 INSERT INTO t1 VALUES(a_string(11), a_string(22));
715 INSERT INTO t1 VALUES(a_string(11), a_string(22));
716 COMMIT;
717 }
718 faultsim_save_and_close
719} {}
720do_faultsim_test pagerfault-15 -prep {
721 faultsim_restore_and_reopen
722 db func a_string a_string;
723} -body {
724 db eval { SELECT * FROM t1 LIMIT 1 } {
725 execsql {
726 BEGIN; INSERT INTO t1 VALUES(a_string(333), a_string(555)); COMMIT;
727 BEGIN; INSERT INTO t1 VALUES(a_string(333), a_string(555)); COMMIT;
728 }
729 }
730} -test {
731 faultsim_test_result {0 {}}
732 faultsim_integrity_check
733}
734
danba3cbf32010-06-30 04:29:03 +0000735
736do_test pagerfault-16-pre1 {
737 faultsim_delete_and_reopen
738 execsql { CREATE TABLE t1(x, y UNIQUE) }
739 faultsim_save_and_close
740} {}
741do_faultsim_test pagerfault-16 -prep {
742 faultsim_restore_and_reopen
743} -body {
744 execsql {
745 PRAGMA locking_mode = exclusive;
746 PRAGMA journal_mode = wal;
747 INSERT INTO t1 VALUES(1, 2);
748 INSERT INTO t1 VALUES(3, 4);
749 PRAGMA journal_mode = delete;
750 INSERT INTO t1 VALUES(4, 5);
751 PRAGMA journal_mode = wal;
752 INSERT INTO t1 VALUES(6, 7);
753 PRAGMA journal_mode = persist;
754 INSERT INTO t1 VALUES(8, 9);
755 }
756} -test {
757 faultsim_test_result {0 {exclusive wal delete wal persist}}
758 faultsim_integrity_check
759}
760
761
dan89ccf442010-07-01 15:09:47 +0000762#-------------------------------------------------------------------------
763# Test fault injection while changing into and out of WAL mode.
764#
765do_test pagerfault-17-pre1 {
766 faultsim_delete_and_reopen
767 execsql {
768 CREATE TABLE t1(a PRIMARY KEY, b);
769 INSERT INTO t1 VALUES(1862, 'Botha');
770 INSERT INTO t1 VALUES(1870, 'Smuts');
771 INSERT INTO t1 VALUES(1866, 'Hertzog');
772 }
773 faultsim_save_and_close
774} {}
775do_faultsim_test pagerfault-17a -prep {
776 faultsim_restore_and_reopen
777} -body {
778 execsql {
779 PRAGMA journal_mode = wal;
780 PRAGMA journal_mode = delete;
781 }
782} -test {
783 faultsim_test_result {0 {wal delete}}
784 faultsim_integrity_check
785}
786do_faultsim_test pagerfault-17b -prep {
787 faultsim_restore_and_reopen
788 execsql { PRAGMA synchronous = OFF }
789} -body {
790 execsql {
791 PRAGMA journal_mode = wal;
792 INSERT INTO t1 VALUES(22, 'Clarke');
793 PRAGMA journal_mode = delete;
794 }
795} -test {
796 faultsim_test_result {0 {wal delete}}
797 faultsim_integrity_check
798}
799do_faultsim_test pagerfault-17c -prep {
800 faultsim_restore_and_reopen
801 execsql {
802 PRAGMA locking_mode = exclusive;
803 PRAGMA journal_mode = wal;
804 }
805} -body {
806 execsql { PRAGMA journal_mode = delete }
807} -test {
808 faultsim_test_result {0 delete}
809 faultsim_integrity_check
810}
811do_faultsim_test pagerfault-17d -prep {
812 faultsim_restore_and_reopen
813 sqlite3 db2 test.db
814 execsql { PRAGMA journal_mode = delete }
815 execsql { PRAGMA journal_mode = wal }
816 execsql { INSERT INTO t1 VALUES(99, 'Bradman') } db2
817} -body {
818 execsql { PRAGMA journal_mode = delete }
819} -test {
820 faultsim_test_result {1 {database is locked}}
821 faultsim_integrity_check
822}
823do_faultsim_test pagerfault-17e -prep {
824 faultsim_restore_and_reopen
825 sqlite3 db2 test.db
826 execsql { PRAGMA journal_mode = delete }
827 execsql { PRAGMA journal_mode = wal }
828 set ::chan [launch_testfixture]
829 testfixture $::chan {
830 sqlite3 db test.db
831 db eval { INSERT INTO t1 VALUES(101, 'Latham') }
832 }
833 catch { testfixture $::chan sqlite_abort }
834 catch { close $::chan }
835} -body {
836 execsql { PRAGMA journal_mode = delete }
837} -test {
838 faultsim_test_result {0 delete}
839 faultsim_integrity_check
840}
841
842#-------------------------------------------------------------------------
843# Test fault-injection when changing from journal_mode=persist to
844# journal_mode=delete (this involves deleting the journal file).
845#
846do_test pagerfault-18-pre1 {
847 faultsim_delete_and_reopen
848 execsql {
849 CREATE TABLE qq(x);
850 INSERT INTO qq VALUES('Herbert');
851 INSERT INTO qq VALUES('Macalister');
852 INSERT INTO qq VALUES('Mackenzie');
853 INSERT INTO qq VALUES('Lilley');
854 INSERT INTO qq VALUES('Palmer');
855 }
856 faultsim_save_and_close
857} {}
858do_faultsim_test pagerfault-18 -prep {
859 faultsim_restore_and_reopen
860 execsql {
861 PRAGMA journal_mode = PERSIST;
862 INSERT INTO qq VALUES('Beatty');
863 }
864} -body {
865 execsql { PRAGMA journal_mode = delete }
866} -test {
867 faultsim_test_result {0 delete}
868 faultsim_integrity_check
869}
870
dan89ccf442010-07-01 15:09:47 +0000871do_faultsim_test pagerfault-19a -prep {
872 sqlite3 db :memory:
873 db func a_string a_string
874 execsql {
875 PRAGMA auto_vacuum = FULL;
876 BEGIN;
877 CREATE TABLE t1(a, b);
878 INSERT INTO t1 VALUES(a_string(5000), a_string(6000));
879 COMMIT;
880 }
881} -body {
882 execsql {
883 CREATE TABLE t2(a, b);
884 INSERT INTO t2 SELECT * FROM t1;
885 DELETE FROM t1;
886 }
887} -test {
888 faultsim_test_result {0 {}}
889}
890
891do_test pagerfault-19-pre1 {
892 faultsim_delete_and_reopen
893 execsql {
894 PRAGMA auto_vacuum = FULL;
895 CREATE TABLE t1(x); INSERT INTO t1 VALUES(1);
896 CREATE TABLE t2(x); INSERT INTO t2 VALUES(2);
897 CREATE TABLE t3(x); INSERT INTO t3 VALUES(3);
898 CREATE TABLE t4(x); INSERT INTO t4 VALUES(4);
899 CREATE TABLE t5(x); INSERT INTO t5 VALUES(5);
900 CREATE TABLE t6(x); INSERT INTO t6 VALUES(6);
901 }
902 faultsim_save_and_close
903} {}
904do_faultsim_test pagerfault-19b -prep {
905 faultsim_restore_and_reopen
906} -body {
907 execsql {
908 BEGIN;
909 UPDATE t4 SET x = x+1;
910 UPDATE t6 SET x = x+1;
911 SAVEPOINT one;
912 UPDATE t3 SET x = x+1;
913 SAVEPOINT two;
914 DROP TABLE t2;
915 ROLLBACK TO one;
916 COMMIT;
917 SELECT * FROM t3;
918 SELECT * FROM t4;
919 SELECT * FROM t6;
920 }
921} -test {
922 faultsim_test_result {0 {3 5 7}}
923}
924
dand0b0d4d2010-07-01 19:01:56 +0000925#-------------------------------------------------------------------------
926# This tests fault-injection in a special case in the auto-vacuum code.
927#
dan89ccf442010-07-01 15:09:47 +0000928do_test pagerfault-20-pre1 {
929 faultsim_delete_and_reopen
dan89ccf442010-07-01 15:09:47 +0000930 execsql {
dand0b0d4d2010-07-01 19:01:56 +0000931 PRAGMA cache_size = 10;
932 PRAGMA auto_vacuum = FULL;
933 CREATE TABLE t0(a, b);
dan89ccf442010-07-01 15:09:47 +0000934 }
935 faultsim_save_and_close
936} {}
937do_faultsim_test pagerfault-20 -prep {
938 faultsim_restore_and_reopen
dan89ccf442010-07-01 15:09:47 +0000939} -body {
940 execsql {
941 BEGIN;
dand0b0d4d2010-07-01 19:01:56 +0000942 CREATE TABLE t1(a, b);
943 CREATE TABLE t2(a, b);
944 DROP TABLE t1;
dan89ccf442010-07-01 15:09:47 +0000945 COMMIT;
946 }
947} -test {
948 faultsim_test_result {0 {}}
dan89ccf442010-07-01 15:09:47 +0000949}
dan273f3f02010-06-26 15:42:33 +0000950
dand0b0d4d2010-07-01 19:01:56 +0000951do_test pagerfault-21-pre1 {
952 faultsim_delete_and_reopen
953 execsql {
954 PRAGMA cache_size = 10;
955 CREATE TABLE t0(a PRIMARY KEY, b);
956 INSERT INTO t0 VALUES(1, 2);
957 }
958 faultsim_save_and_close
959} {}
960do_faultsim_test pagerfault-21 -prep {
961 faultsim_restore_and_reopen
962} -body {
963 db eval { SELECT * FROM t0 LIMIT 1 } {
964 db eval { INSERT INTO t0 SELECT a+1, b FROM t0 }
965 db eval { INSERT INTO t0 SELECT a+2, b FROM t0 }
966 }
967} -test {
968 faultsim_test_result {0 {}}
969}
970
dand0b0d4d2010-07-01 19:01:56 +0000971
danb0ac3e32010-06-16 10:55:42 +0000972finish_test