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 #***********************************************************************
13 set testdir [file dirname $argv0]
14 source $testdir/tester.tcl
15 source $testdir/lock_common.tcl
16 source $testdir/malloc_common.tcl
18 if {[permutation] == "inmemory_journal"} {
23 set a_string_counter 1
25 global a_string_counter
27 string range [string repeat "${a_string_counter}." $n] 1 $n
29 db func a_string a_string
31 #-------------------------------------------------------------------------
32 # Test fault-injection while rolling back a hot-journal file.
34 do_test pagerfault-1-pre1 {
36 PRAGMA journal_mode = DELETE;
37 PRAGMA cache_size = 10;
38 CREATE TABLE t1(a UNIQUE, b UNIQUE);
39 INSERT INTO t1 VALUES(a_string(200), a_string(300));
40 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
41 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
43 INSERT INTO t1 SELECT a_string(201), a_string(301) FROM t1;
44 INSERT INTO t1 SELECT a_string(202), a_string(302) FROM t1;
45 INSERT INTO t1 SELECT a_string(203), a_string(303) FROM t1;
46 INSERT INTO t1 SELECT a_string(204), a_string(304) FROM t1;
48 faultsim_save_and_close
50 do_faultsim_test pagerfault-1 -prep {
51 faultsim_restore_and_reopen
53 execsql { SELECT count(*) FROM t1 }
55 faultsim_test_result {0 4}
56 faultsim_integrity_check
57 if {[db one { SELECT count(*) FROM t1 }] != 4} {
58 error "Database content appears incorrect"
62 #-------------------------------------------------------------------------
63 # Test fault-injection while rolling back a hot-journal file with a
64 # page-size different from the current value stored on page 1 of the
67 do_test pagerfault-2-pre1 {
71 proc xSyncCb {filename args} {
72 if {[string match *journal filename]==0} faultsim_save
74 faultsim_delete_and_reopen
76 PRAGMA page_size = 4096;
78 CREATE TABLE abc(a, b, c);
79 INSERT INTO abc VALUES('o', 't', 't');
80 INSERT INTO abc VALUES('f', 'f', 's');
81 INSERT INTO abc SELECT * FROM abc; -- 4
82 INSERT INTO abc SELECT * FROM abc; -- 8
83 INSERT INTO abc SELECT * FROM abc; -- 16
84 INSERT INTO abc SELECT * FROM abc; -- 32
85 INSERT INTO abc SELECT * FROM abc; -- 64
86 INSERT INTO abc SELECT * FROM abc; -- 128
87 INSERT INTO abc SELECT * FROM abc; -- 256
89 PRAGMA page_size = 1024;
95 do_faultsim_test pagerfault-2 -prep {
96 faultsim_restore_and_reopen
98 execsql { SELECT * FROM abc }
100 set answer [split [string repeat "ottffs" 128] ""]
101 faultsim_test_result [list 0 $answer]
102 faultsim_integrity_check
103 set res [db eval { SELECT * FROM abc }]
104 if {$res != $answer} { error "Database content appears incorrect ($res)" }
107 #-------------------------------------------------------------------------
108 # Test fault-injection while rolling back hot-journals that were created
109 # as part of a multi-file transaction.
111 do_test pagerfault-3-pre1 {
112 testvfs tstvfs -default 1
113 tstvfs filter xDelete
114 tstvfs script xDeleteCallback
116 proc xDeleteCallback {method file args} {
117 set file [file tail $file]
118 if { [string match *mj* $file] } { faultsim_save }
121 faultsim_delete_and_reopen
122 db func a_string a_string
125 ATTACH 'test.db2' AS aux;
126 PRAGMA journal_mode = DELETE;
127 PRAGMA main.cache_size = 10;
128 PRAGMA aux.cache_size = 10;
130 CREATE TABLE t1(a UNIQUE, b UNIQUE);
131 CREATE TABLE aux.t2(a UNIQUE, b UNIQUE);
132 INSERT INTO t1 VALUES(a_string(200), a_string(300));
133 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
134 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
135 INSERT INTO t2 SELECT * FROM t1;
138 INSERT INTO t1 SELECT a_string(201), a_string(301) FROM t1;
139 INSERT INTO t1 SELECT a_string(202), a_string(302) FROM t1;
140 INSERT INTO t1 SELECT a_string(203), a_string(303) FROM t1;
141 INSERT INTO t1 SELECT a_string(204), a_string(304) FROM t1;
142 REPLACE INTO t2 SELECT * FROM t1;
149 do_faultsim_test pagerfault-3 -prep {
150 faultsim_restore_and_reopen
153 ATTACH 'test.db2' AS aux;
154 SELECT count(*) FROM t2;
155 SELECT count(*) FROM t1;
158 faultsim_test_result {0 {4 4}} {1 {unable to open database: test.db2}}
159 faultsim_integrity_check
160 catchsql { ATTACH 'test.db2' AS aux }
161 if {[db one { SELECT count(*) FROM t1 }] != 4
162 || [db one { SELECT count(*) FROM t2 }] != 4
164 error "Database content appears incorrect"
168 #-------------------------------------------------------------------------
169 # Test fault-injection as part of a vanilla, no-transaction, INSERT
172 do_faultsim_test pagerfault-4 -prep {
173 faultsim_delete_and_reopen
177 INSERT INTO x VALUES('z');
181 faultsim_test_result {0 z}
182 faultsim_integrity_check
185 #-------------------------------------------------------------------------
186 # Test fault-injection as part of a commit when using journal_mode=PERSIST.
187 # Three different cases:
189 # pagerfault-5.1: With no journal_size_limit configured.
190 # pagerfault-5.2: With a journal_size_limit configured.
191 # pagerfault-5.4: Multi-file transaction. One connection has a
192 # journal_size_limit of 0, the other has no limit.
194 do_test pagerfault-5-pre1 {
195 faultsim_delete_and_reopen
196 db func a_string a_string
198 CREATE TABLE t1(a UNIQUE, b UNIQUE);
199 INSERT INTO t1 VALUES(a_string(200), a_string(300));
200 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
201 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
203 faultsim_save_and_close
205 do_faultsim_test pagerfault-5.1 -prep {
206 faultsim_restore_and_reopen
207 db func a_string a_string
208 execsql { PRAGMA journal_mode = PERSIST }
210 execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
212 faultsim_test_result {0 {}}
213 faultsim_integrity_check
215 do_faultsim_test pagerfault-5.2 -prep {
216 faultsim_restore_and_reopen
217 db func a_string a_string
219 PRAGMA journal_mode = PERSIST;
220 PRAGMA journal_size_limit = 2048;
223 execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
225 faultsim_test_result {0 {}}
226 faultsim_integrity_check
228 do_faultsim_test pagerfault-5.3 -faults oom-transient -prep {
229 faultsim_restore_and_reopen
230 db func a_string a_string
231 file delete -force test2.db test2.db-journal test2.db-wal
233 PRAGMA journal_mode = PERSIST;
234 ATTACH 'test2.db' AS aux;
235 PRAGMA aux.journal_mode = PERSIST;
236 PRAGMA aux.journal_size_limit = 0;
241 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
242 CREATE TABLE aux.t2 AS SELECT * FROM t1;
246 faultsim_test_result {0 {}}
249 catchsql { ROLLBACK }
251 faultsim_integrity_check
253 set rc [catch { set res [db one { PRAGMA aux.integrity_check }] }]
254 if {$rc!=0 || $res != "ok"} {error "integrity-check problem:$rc $res"}
257 #-------------------------------------------------------------------------
258 # Test fault-injection as part of a commit when using
259 # journal_mode=TRUNCATE.
261 do_test pagerfault-6-pre1 {
262 faultsim_delete_and_reopen
263 db func a_string a_string
265 CREATE TABLE t1(a UNIQUE, b UNIQUE);
266 INSERT INTO t1 VALUES(a_string(200), a_string(300));
268 faultsim_save_and_close
271 do_faultsim_test pagerfault-6.1 -prep {
272 faultsim_restore_and_reopen
273 db func a_string a_string
274 execsql { PRAGMA journal_mode = TRUNCATE }
276 execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
277 execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
279 faultsim_test_result {0 {}}
280 faultsim_integrity_check
283 # The unix vfs xAccess() method considers a file zero bytes in size to
284 # "not exist". This proc overrides that behaviour so that a zero length
285 # file is considered to exist.
287 proc xAccess {method filename op args} {
288 if {$op != "SQLITE_ACCESS_EXISTS"} { return "" }
289 return [file exists $filename]
291 do_faultsim_test pagerfault-6.2 -faults cantopen-* -prep {
292 shmfault filter xAccess
293 shmfault script xAccess
295 faultsim_restore_and_reopen
296 db func a_string a_string
297 execsql { PRAGMA journal_mode = TRUNCATE }
299 execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
300 execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
302 faultsim_test_result {0 {}}
303 faultsim_integrity_check
306 # The following was an attempt to get a bitvec malloc to fail. Didn't work.
308 # do_test pagerfault-6-pre1 {
309 # faultsim_delete_and_reopen
311 # CREATE TABLE t1(x, y, UNIQUE(x, y));
312 # INSERT INTO t1 VALUES(1, randomblob(1501));
313 # INSERT INTO t1 VALUES(2, randomblob(1502));
314 # INSERT INTO t1 VALUES(3, randomblob(1503));
315 # INSERT INTO t1 VALUES(4, randomblob(1504));
317 # SELECT x, randomblob(1500+oid+(SELECT max(oid) FROM t1)) FROM t1;
319 # SELECT x, randomblob(1500+oid+(SELECT max(oid) FROM t1)) FROM t1;
321 # SELECT x, randomblob(1500+oid+(SELECT max(oid) FROM t1)) FROM t1;
323 # SELECT x, randomblob(1500+oid+(SELECT max(oid) FROM t1)) FROM t1;
325 # faultsim_save_and_close
327 # do_faultsim_test pagerfault-6 -prep {
328 # faultsim_restore_and_reopen
332 # UPDATE t1 SET x=x+4 WHERE x=1;
334 # UPDATE t1 SET x=x+4 WHERE x=2;
336 # UPDATE t1 SET x=x+4 WHERE x=3;
338 # UPDATE t1 SET x=x+4 WHERE x=4;
341 # SELECT DISTINCT x FROM t1;
344 # faultsim_test_result {0 {5 6 7 8}}
345 # faultsim_integrity_check
349 # This is designed to provoke a special case in the pager code:
351 # If an error (specifically, a FULL or IOERR error) occurs while writing a
352 # dirty page to the file-system in order to free up memory, the pager enters
353 # the "error state". An IO error causes SQLite to roll back the current
354 # transaction (exiting the error state). A FULL error, however, may only
355 # rollback the current statement.
357 # This block tests that nothing goes wrong if a FULL error occurs while
358 # writing a dirty page out to free memory from within a statement that has
359 # opened a statement transaction.
361 do_test pagerfault-7-pre1 {
362 faultsim_delete_and_reopen
364 CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
366 INSERT INTO t2 VALUES(NULL, randomblob(1500));
367 INSERT INTO t2 VALUES(NULL, randomblob(1500));
368 INSERT INTO t2 SELECT NULL, randomblob(1500) FROM t2; -- 4
369 INSERT INTO t2 SELECT NULL, randomblob(1500) FROM t2; -- 8
370 INSERT INTO t2 SELECT NULL, randomblob(1500) FROM t2; -- 16
371 INSERT INTO t2 SELECT NULL, randomblob(1500) FROM t2; -- 32
372 INSERT INTO t2 SELECT NULL, randomblob(1500) FROM t2; -- 64
374 CREATE TABLE t1(a PRIMARY KEY, b);
375 INSERT INTO t1 SELECT * FROM t2;
378 faultsim_save_and_close
380 do_faultsim_test pagerfault-7 -prep {
381 faultsim_restore_and_reopen
383 PRAGMA cache_size = 10;
385 UPDATE t1 SET b = randomblob(1500);
388 execsql { UPDATE t1 SET a = 65, b = randomblob(1500) WHERE (a+1)>200 }
391 faultsim_test_result {0 {}}
392 faultsim_integrity_check
395 do_test pagerfault-8-pre1 {
396 faultsim_delete_and_reopen
398 PRAGMA auto_vacuum = 1;
399 CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
401 INSERT INTO t1 VALUES(NULL, randomblob(1500));
402 INSERT INTO t1 VALUES(NULL, randomblob(1500));
403 INSERT INTO t1 SELECT NULL, randomblob(1500) FROM t1; -- 4
404 INSERT INTO t1 SELECT NULL, randomblob(1500) FROM t1; -- 8
405 INSERT INTO t1 SELECT NULL, randomblob(1500) FROM t1; -- 16
406 INSERT INTO t1 SELECT NULL, randomblob(1500) FROM t1; -- 32
407 INSERT INTO t1 SELECT NULL, randomblob(1500) FROM t1; -- 64
410 faultsim_save_and_close
411 set filesize [file size test.db]
414 do_test pagerfault-8-pre2 {
415 faultsim_restore_and_reopen
416 execsql { DELETE FROM t1 WHERE a>32 }
417 expr {[file size test.db] < $filesize}
419 do_faultsim_test pagerfault-8 -prep {
420 faultsim_restore_and_reopen
423 DELETE FROM t1 WHERE a>32;
428 faultsim_test_result {0 {}}
429 faultsim_integrity_check
432 #-------------------------------------------------------------------------
433 # This test case is specially designed so that during a savepoint
434 # rollback, a new cache entry must be allocated (see comments surrounding
435 # the call to sqlite3PagerAcquire() from within pager_playback_one_page()
436 # for details). Test the effects of injecting an OOM at this point.
438 do_test pagerfault-9-pre1 {
439 faultsim_delete_and_reopen
441 PRAGMA auto_vacuum = incremental;
446 INSERT INTO t1 VALUES(randomblob(900));
447 INSERT INTO t1 VALUES(randomblob(900));
450 faultsim_save_and_close
452 do_faultsim_test pagerfault-9.1 -prep {
453 faultsim_restore_and_reopen
456 INSERT INTO t1 VALUES(randomblob(900));
457 INSERT INTO t1 VALUES(randomblob(900));
461 PRAGMA incremental_vacuum;
467 PRAGMA freelist_count
470 faultsim_test_result {0 2}
471 faultsim_integrity_check
473 set sl [db one { SELECT COALESCE(sum(length(x)), 'null') FROM t1 }]
474 if {$sl!="null" && $sl!=1800} {
475 error "Content looks no good... ($sl)"
479 #-------------------------------------------------------------------------
480 # Test fault injection with a temporary database file.
483 do_faultsim_test pagerfault-10$v -prep {
485 db func a_string a_string;
487 PRAGMA cache_size = 10;
489 CREATE TABLE xx(a, b, UNIQUE(a, b));
490 INSERT INTO xx VALUES(a_string(200), a_string(200));
491 INSERT INTO xx SELECT a_string(200), a_string(200) FROM xx;
492 INSERT INTO xx SELECT a_string(200), a_string(200) FROM xx;
493 INSERT INTO xx SELECT a_string(200), a_string(200) FROM xx;
494 INSERT INTO xx SELECT a_string(200), a_string(200) FROM xx;
498 execsql { UPDATE xx SET a = a_string(300) }
500 faultsim_test_result {0 {}}
501 if {$::v == "b"} { execsql { PRAGMA journal_mode = TRUNCATE } }
502 faultsim_integrity_check
503 faultsim_integrity_check
507 #-------------------------------------------------------------------------
508 # Test fault injection with transaction savepoints (savepoints created
509 # when a SAVEPOINT command is executed outside of any other savepoint
510 # or transaction context).
512 do_test pagerfault-9-pre1 {
513 faultsim_delete_and_reopen
514 db func a_string a_string;
516 PRAGMA auto_vacuum = on;
517 CREATE TABLE t1(x UNIQUE);
518 CREATE TABLE t2(y UNIQUE);
519 CREATE TABLE t3(z UNIQUE);
521 INSERT INTO t1 VALUES(a_string(202));
522 INSERT INTO t2 VALUES(a_string(203));
523 INSERT INTO t3 VALUES(a_string(204));
524 INSERT INTO t1 SELECT a_string(202) FROM t1;
525 INSERT INTO t1 SELECT a_string(203) FROM t1;
526 INSERT INTO t1 SELECT a_string(204) FROM t1;
527 INSERT INTO t1 SELECT a_string(205) FROM t1;
528 INSERT INTO t2 SELECT a_string(length(x)) FROM t1;
529 INSERT INTO t3 SELECT a_string(length(x)) FROM t1;
532 faultsim_save_and_close
534 do_faultsim_test pagerfault-11 -prep {
535 faultsim_restore_and_reopen
536 execsql { PRAGMA cache_size = 10 }
540 UPDATE t2 SET y = y||'2';
541 INSERT INTO t3 SELECT * FROM t2;
544 UPDATE t1 SET x = x||'3';
545 INSERT INTO t2 SELECT * FROM t1;
550 faultsim_test_result {0 {}}
551 faultsim_integrity_check
555 #-------------------------------------------------------------------------
556 # Test fault injection when writing to a database file that resides on
557 # a file-system with a sector-size larger than the database page-size.
559 do_test pagerfault-12-pre1 {
560 testvfs ss_layer -default 1
561 ss_layer sectorsize 4096
562 faultsim_delete_and_reopen
563 db func a_string a_string;
566 PRAGMA page_size = 1024;
567 PRAGMA journal_mode = PERSIST;
568 PRAGMA cache_size = 10;
570 CREATE TABLE t1(x, y UNIQUE);
571 INSERT INTO t1 VALUES(a_string(333), a_string(444));
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(333+rowid), a_string(444+rowid) FROM t1;
576 INSERT INTO t1 SELECT a_string(44), a_string(55) FROM t1 LIMIT 13;
579 faultsim_save_and_close
582 do_faultsim_test pagerfault-12a -prep {
583 faultsim_restore_and_reopen
584 execsql { PRAGMA cache_size = 10 }
585 db func a_string a_string;
588 UPDATE t1 SET x = a_string(length(x)), y = a_string(length(y));
591 faultsim_test_result {0 {}}
592 faultsim_integrity_check
595 do_test pagerfault-12-pre2 {
596 faultsim_restore_and_reopen
598 CREATE TABLE t2 AS SELECT * FROM t1 LIMIT 10;
600 faultsim_save_and_close
602 do_faultsim_test pagerfault-12b -prep {
603 faultsim_restore_and_reopen
604 db func a_string a_string;
605 execsql { SELECT * FROM t1 }
607 set sql(1) { UPDATE t2 SET x = a_string(280) }
608 set sql(2) { UPDATE t1 SET x = a_string(280) WHERE rowid = 5 }
610 db eval { SELECT rowid FROM t1 LIMIT 2 } { db eval $sql($rowid) }
613 faultsim_test_result {0 {}}
614 faultsim_integrity_check
621 #-------------------------------------------------------------------------
622 # Test fault injection when SQLite opens a database where the size of the
623 # database file is zero bytes but the accompanying journal file is larger
624 # than that. In this scenario SQLite should delete the journal file
625 # without rolling it back, even if it is in all other respects a valid
628 do_test pagerfault-13-pre1 {
629 faultsim_delete_and_reopen
630 db func a_string a_string;
632 PRAGMA journal_mode = PERSIST;
634 CREATE TABLE t1(x, y UNIQUE);
635 INSERT INTO t1 VALUES(a_string(333), a_string(444));
639 file delete -force test.db
642 do_faultsim_test pagerfault-13 -prep {
643 faultsim_restore_and_reopen
645 execsql { CREATE TABLE xx(a, b) }
647 faultsim_test_result {0 {}}
650 #---------------------------------------------------------------------------
651 # Test fault injection into a small backup operation.
653 do_test pagerfault-14-pre1 {
654 faultsim_delete_and_reopen
655 db func a_string a_string;
657 PRAGMA journal_mode = PERSIST;
658 ATTACH 'test.db2' AS two;
660 CREATE TABLE t1(x, y UNIQUE);
661 CREATE TABLE two.t2(x, y UNIQUE);
662 INSERT INTO t1 VALUES(a_string(333), a_string(444));
663 INSERT INTO t2 VALUES(a_string(333), a_string(444));
666 faultsim_save_and_close
669 do_faultsim_test pagerfault-14a -prep {
670 faultsim_restore_and_reopen
672 if {[catch {db backup test.db2} msg]} { error [regsub {.*: } $msg {}] }
674 faultsim_test_result {0 {}} {1 {}} {1 {SQL logic error or missing database}}
677 # If TEMP_STORE is 2 or greater, then the database [db2] will be created
678 # as an in-memory database. This test will not work in that case, as it
679 # is not possible to change the page-size of an in-memory database. Even
680 # using the backup API.
683 do_faultsim_test pagerfault-14b -prep {
685 faultsim_restore_and_reopen
687 db2 eval { PRAGMA page_size = 4096; CREATE TABLE xx(a) }
689 sqlite3_backup B db2 main db main
692 if {[string match SQLITE_IOERR_* $rc]} {set rc SQLITE_IOERR}
693 if {$rc != "SQLITE_OK"} { error [sqlite3_test_errstr $rc] }
696 faultsim_test_result {0 {}} {1 {sqlite3_backup_init() failed}}
700 do_faultsim_test pagerfault-14c -prep {
702 faultsim_restore_and_reopen
705 PRAGMA synchronous = off;
706 PRAGMA page_size = 4096;
710 sqlite3_backup B db2 main db main
713 if {[string match SQLITE_IOERR_* $rc]} {set rc SQLITE_IOERR}
714 if {$rc != "SQLITE_OK"} { error [sqlite3_test_errstr $rc] }
717 faultsim_test_result {0 {}} {1 {sqlite3_backup_init() failed}}
720 do_test pagerfault-15-pre1 {
721 faultsim_delete_and_reopen
722 db func a_string a_string;
725 CREATE TABLE t1(x, y UNIQUE);
726 INSERT INTO t1 VALUES(a_string(11), a_string(22));
727 INSERT INTO t1 VALUES(a_string(11), a_string(22));
730 faultsim_save_and_close
732 do_faultsim_test pagerfault-15 -prep {
733 faultsim_restore_and_reopen
734 db func a_string a_string;
736 db eval { SELECT * FROM t1 LIMIT 1 } {
738 BEGIN; INSERT INTO t1 VALUES(a_string(333), a_string(555)); COMMIT;
739 BEGIN; INSERT INTO t1 VALUES(a_string(333), a_string(555)); COMMIT;
743 faultsim_test_result {0 {}}
744 faultsim_integrity_check
748 do_test pagerfault-16-pre1 {
749 faultsim_delete_and_reopen
750 execsql { CREATE TABLE t1(x, y UNIQUE) }
751 faultsim_save_and_close
753 do_faultsim_test pagerfault-16 -prep {
754 faultsim_restore_and_reopen
757 PRAGMA locking_mode = exclusive;
758 PRAGMA journal_mode = wal;
759 INSERT INTO t1 VALUES(1, 2);
760 INSERT INTO t1 VALUES(3, 4);
761 PRAGMA journal_mode = delete;
762 INSERT INTO t1 VALUES(4, 5);
763 PRAGMA journal_mode = wal;
764 INSERT INTO t1 VALUES(6, 7);
765 PRAGMA journal_mode = persist;
766 INSERT INTO t1 VALUES(8, 9);
769 faultsim_test_result {0 {exclusive wal delete wal persist}}
770 faultsim_integrity_check
774 #-------------------------------------------------------------------------
775 # Test fault injection while changing into and out of WAL mode.
777 do_test pagerfault-17-pre1 {
778 faultsim_delete_and_reopen
780 CREATE TABLE t1(a PRIMARY KEY, b);
781 INSERT INTO t1 VALUES(1862, 'Botha');
782 INSERT INTO t1 VALUES(1870, 'Smuts');
783 INSERT INTO t1 VALUES(1866, 'Hertzog');
785 faultsim_save_and_close
787 do_faultsim_test pagerfault-17a -prep {
788 faultsim_restore_and_reopen
791 PRAGMA journal_mode = wal;
792 PRAGMA journal_mode = delete;
795 faultsim_test_result {0 {wal delete}}
796 faultsim_integrity_check
798 do_faultsim_test pagerfault-17b -prep {
799 faultsim_restore_and_reopen
800 execsql { PRAGMA synchronous = OFF }
803 PRAGMA journal_mode = wal;
804 INSERT INTO t1 VALUES(22, 'Clarke');
805 PRAGMA journal_mode = delete;
808 faultsim_test_result {0 {wal delete}}
809 faultsim_integrity_check
811 do_faultsim_test pagerfault-17c -prep {
812 faultsim_restore_and_reopen
814 PRAGMA locking_mode = exclusive;
815 PRAGMA journal_mode = wal;
818 execsql { PRAGMA journal_mode = delete }
820 faultsim_test_result {0 delete}
821 faultsim_integrity_check
823 do_faultsim_test pagerfault-17d -prep {
825 faultsim_restore_and_reopen
827 execsql { PRAGMA journal_mode = delete }
828 execsql { PRAGMA journal_mode = wal }
829 execsql { INSERT INTO t1 VALUES(99, 'Bradman') } db2
831 execsql { PRAGMA journal_mode = delete }
833 faultsim_test_result {1 {database is locked}}
834 faultsim_integrity_check
836 do_faultsim_test pagerfault-17e -prep {
838 faultsim_restore_and_reopen
840 execsql { PRAGMA journal_mode = delete }
841 execsql { PRAGMA journal_mode = wal }
842 set ::chan [launch_testfixture]
843 testfixture $::chan {
845 db eval { INSERT INTO t1 VALUES(101, 'Latham') }
847 catch { testfixture $::chan sqlite_abort }
848 catch { close $::chan }
850 execsql { PRAGMA journal_mode = delete }
852 faultsim_test_result {0 delete}
853 faultsim_integrity_check
856 #-------------------------------------------------------------------------
857 # Test fault-injection when changing from journal_mode=persist to
858 # journal_mode=delete (this involves deleting the journal file).
860 do_test pagerfault-18-pre1 {
861 faultsim_delete_and_reopen
864 INSERT INTO qq VALUES('Herbert');
865 INSERT INTO qq VALUES('Macalister');
866 INSERT INTO qq VALUES('Mackenzie');
867 INSERT INTO qq VALUES('Lilley');
868 INSERT INTO qq VALUES('Palmer');
870 faultsim_save_and_close
872 do_faultsim_test pagerfault-18 -prep {
873 faultsim_restore_and_reopen
875 PRAGMA journal_mode = PERSIST;
876 INSERT INTO qq VALUES('Beatty');
879 execsql { PRAGMA journal_mode = delete }
881 faultsim_test_result {0 delete}
882 faultsim_integrity_check
885 do_faultsim_test pagerfault-19a -prep {
887 db func a_string a_string
889 PRAGMA auto_vacuum = FULL;
891 CREATE TABLE t1(a, b);
892 INSERT INTO t1 VALUES(a_string(5000), a_string(6000));
897 CREATE TABLE t2(a, b);
898 INSERT INTO t2 SELECT * FROM t1;
902 faultsim_test_result {0 {}}
905 do_test pagerfault-19-pre1 {
906 faultsim_delete_and_reopen
908 PRAGMA auto_vacuum = FULL;
909 CREATE TABLE t1(x); INSERT INTO t1 VALUES(1);
910 CREATE TABLE t2(x); INSERT INTO t2 VALUES(2);
911 CREATE TABLE t3(x); INSERT INTO t3 VALUES(3);
912 CREATE TABLE t4(x); INSERT INTO t4 VALUES(4);
913 CREATE TABLE t5(x); INSERT INTO t5 VALUES(5);
914 CREATE TABLE t6(x); INSERT INTO t6 VALUES(6);
916 faultsim_save_and_close
918 do_faultsim_test pagerfault-19b -prep {
919 faultsim_restore_and_reopen
923 UPDATE t4 SET x = x+1;
924 UPDATE t6 SET x = x+1;
926 UPDATE t3 SET x = x+1;
936 faultsim_test_result {0 {3 5 7}}
939 #-------------------------------------------------------------------------
940 # This tests fault-injection in a special case in the auto-vacuum code.
942 do_test pagerfault-20-pre1 {
943 faultsim_delete_and_reopen
945 PRAGMA cache_size = 10;
946 PRAGMA auto_vacuum = FULL;
947 CREATE TABLE t0(a, b);
949 faultsim_save_and_close
951 do_faultsim_test pagerfault-20 -prep {
952 faultsim_restore_and_reopen
956 CREATE TABLE t1(a, b);
957 CREATE TABLE t2(a, b);
962 faultsim_test_result {0 {}}
965 do_test pagerfault-21-pre1 {
966 faultsim_delete_and_reopen
968 PRAGMA cache_size = 10;
969 CREATE TABLE t0(a PRIMARY KEY, b);
970 INSERT INTO t0 VALUES(1, 2);
972 faultsim_save_and_close
974 do_faultsim_test pagerfault-21 -prep {
975 faultsim_restore_and_reopen
977 db eval { SELECT * FROM t0 LIMIT 1 } {
978 db eval { INSERT INTO t0 SELECT a+1, b FROM t0 }
979 db eval { INSERT INTO t0 SELECT a+2, b FROM t0 }
982 faultsim_test_result {0 {}}
986 #-------------------------------------------------------------------------
987 # Test fault-injection and rollback when the nReserve header value
990 do_test pagerfault-21-pre1 {
991 faultsim_delete_and_reopen
993 PRAGMA page_size = 1024;
994 PRAGMA journal_mode = WAL;
995 PRAGMA journal_mode = DELETE;
998 hexio_write test.db 20 10
999 hexio_write test.db 105 03F0
1001 db func a_string a_string
1003 CREATE TABLE t0(a PRIMARY KEY, b UNIQUE);
1004 INSERT INTO t0 VALUES(a_string(222), a_string(333));
1005 INSERT INTO t0 VALUES(a_string(223), a_string(334));
1006 INSERT INTO t0 VALUES(a_string(224), a_string(335));
1007 INSERT INTO t0 VALUES(a_string(225), a_string(336));
1009 faultsim_save_and_close
1012 do_faultsim_test pagerfault-21 -prep {
1013 faultsim_restore_and_reopen
1015 execsql { INSERT INTO t0 SELECT a||'x', b||'x' FROM t0 }
1017 faultsim_test_result {0 {}}
1018 faultsim_integrity_check
1020 ifcapable crashtest {
1021 faultsim_delete_and_reopen
1023 PRAGMA page_size = 1024;
1024 PRAGMA journal_mode = WAL;
1025 PRAGMA journal_mode = DELETE;
1028 hexio_write test.db 20 10
1029 hexio_write test.db 105 03F0
1032 db func a_string a_string
1034 CREATE TABLE t0(a PRIMARY KEY, b UNIQUE);
1035 INSERT INTO t0 VALUES(a_string(222), a_string(333));
1036 INSERT INTO t0 VALUES(a_string(223), a_string(334));
1038 faultsim_save_and_close
1040 for {set iTest 1} {$iTest<50} {incr iTest} {
1041 do_test pagerfault-21.crash.$iTest.1 {
1042 crashsql -delay 1 -file test.db -seed $iTest {
1044 CREATE TABLE t1(a PRIMARY KEY, b UNIQUE);
1045 INSERT INTO t1 SELECT a, b FROM t0;
1048 } {1 {child process exited abnormally}}
1049 do_test pagerfault-22.$iTest.2 {
1051 execsql { PRAGMA integrity_check }
1058 #-------------------------------------------------------------------------
1059 # When a 3.7.0 client opens a write-transaction on a database file that
1060 # has been appended to or truncated by a pre-370 client, it updates
1061 # the db-size in the file header immediately. This test case provokes
1062 # errors during that operation.
1064 do_test pagerfault-22-pre1 {
1065 faultsim_delete_and_reopen
1066 db func a_string a_string
1068 PRAGMA page_size = 1024;
1069 PRAGMA auto_vacuum = 0;
1071 CREATE INDEX i1 ON t1(a);
1072 INSERT INTO t1 VALUES(a_string(3000));
1074 INSERT INTO t2 VALUES(1);
1077 sql36231 { INSERT INTO t1 VALUES(a_string(3000)) }
1078 faultsim_save_and_close
1080 do_faultsim_test pagerfault-22 -prep {
1081 faultsim_restore_and_reopen
1083 execsql { INSERT INTO t2 VALUES(2) }
1084 execsql { SELECT * FROM t2 }
1086 faultsim_test_result {0 {1 2}}
1087 faultsim_integrity_check
1090 #-------------------------------------------------------------------------
1091 # Provoke an OOM error during a commit of multi-file transaction. One of
1092 # the databases written during the transaction is an in-memory database.
1093 # This test causes rollback of the in-memory database after CommitPhaseOne()
1094 # has successfully returned. i.e. the series of calls for the aborted commit
1097 # PagerCommitPhaseOne(<in-memory-db>) -> SQLITE_OK
1098 # PagerCommitPhaseOne(<file-db>) -> SQLITE_IOERR
1099 # PagerRollback(<in-memory-db>)
1100 # PagerRollback(<file-db>)
1102 do_faultsim_test pagerfault-23 -prep {
1104 foreach f [glob -nocomplain test.db*] { file delete -force $f }
1106 ATTACH 'test.db2' AS aux;
1107 CREATE TABLE t1(a, b);
1108 CREATE TABLE aux.t2(a, b);
1113 INSERT INTO t1 VALUES(1,2);
1114 INSERT INTO t2 VALUES(3,4);
1118 faultsim_test_result {0 {}}
1119 faultsim_integrity_check
1122 do_faultsim_test pagerfault-24 -prep {
1123 faultsim_delete_and_reopen
1124 db eval { PRAGMA temp_store = file }
1125 execsql { CREATE TABLE x(a, b) }
1127 execsql { CREATE TEMP TABLE t1(a, b) }
1129 faultsim_test_result {0 {}} \
1130 {1 {unable to open a temporary database file for storing temporary tables}}
1131 set ic [db eval { PRAGMA temp.integrity_check }]
1132 if {$ic != "ok"} { error "Integrity check: $ic" }
1136 if {$n==0} { return "" }
1137 db eval { SELECT * FROM t1 WHERE oid = $n } {
1138 return [lockrows [expr {$n-1}]]
1143 do_test pagerfault-25-pre1 {
1144 faultsim_delete_and_reopen
1145 db func a_string a_string
1147 PRAGMA page_size = 1024;
1148 PRAGMA auto_vacuum = 0;
1150 INSERT INTO t1 VALUES(a_string(500));
1151 INSERT INTO t1 SELECT a_string(500) FROM t1;
1152 INSERT INTO t1 SELECT a_string(500) FROM t1;
1153 INSERT INTO t1 SELECT a_string(500) FROM t1;
1154 INSERT INTO t1 SELECT a_string(500) FROM t1;
1155 INSERT INTO t1 SELECT a_string(500) FROM t1;
1157 faultsim_save_and_close
1159 do_faultsim_test pagerfault-25 -prep {
1160 faultsim_restore_and_reopen
1161 db func a_string a_string
1162 set ::channel [db incrblob -readonly t1 a 1]
1164 PRAGMA cache_size = 10;
1166 INSERT INTO t1 VALUES(a_string(3000));
1167 INSERT INTO t1 VALUES(a_string(3000));
1172 catch { lockrows 30 }
1173 catch { db eval COMMIT }
1175 faultsim_test_result {0 {}}
1178 do_faultsim_test pagerfault-26 -prep {
1179 faultsim_delete_and_reopen
1181 PRAGMA page_size = 1024;
1182 PRAGMA journal_mode = truncate;
1183 PRAGMA auto_vacuum = full;
1184 PRAGMA locking_mode=exclusive;
1185 CREATE TABLE t1(a, b);
1186 INSERT INTO t1 VALUES(1, 2);
1187 PRAGMA page_size = 4096;
1194 faultsim_test_result {0 {}}
1196 set contents [db eval {SELECT * FROM t1}]
1197 if {$contents != "1 2"} { error "Bad database contents ($contents)" }
1199 set sz [file size test.db]
1200 if {$testrc!=0 && $sz!=1024*3 && $sz!=4096*3} {
1201 error "Expected file size to be 3072 or 12288 bytes - actual size $sz bytes"
1203 if {$testrc==0 && $sz!=4096*3} {
1204 error "Expected file size to be 12288 bytes - actual size $sz bytes"
1208 do_test pagerfault-27-pre {
1209 faultsim_delete_and_reopen
1210 db func a_string a_string
1212 PRAGMA page_size = 1024;
1213 CREATE TABLE t1(a, b);
1214 CREATE TABLE t2(a UNIQUE, b UNIQUE);
1215 INSERT INTO t2 VALUES( a_string(800), a_string(800) );
1216 INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
1217 INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
1218 INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
1219 INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
1220 INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
1221 INSERT INTO t2 SELECT a_string(800), a_string(800) FROM t2;
1222 INSERT INTO t1 VALUES (a_string(20000), a_string(20000));
1224 faultsim_save_and_close
1226 do_faultsim_test pagerfault-27 -faults ioerr-persistent -prep {
1227 faultsim_restore_and_reopen
1228 db func a_string a_string
1230 PRAGMA cache_size = 10;
1233 set ::channel [db incrblob t1 a 1]
1235 puts $::channel [string repeat abc 6000]
1238 catchsql { UPDATE t2 SET a = a_string(800), b = a_string(800) }
1239 catch { close $::channel }
1240 catchsql { ROLLBACK }
1241 faultsim_integrity_check