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 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this file is testing the operation of the library in
13 # "PRAGMA journal_mode=WAL" mode.
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
18 source $testdir/lock_common.tcl
19 source $testdir/malloc_common.tcl
20 source $testdir/wal_common.tcl
24 ifcapable !wal {finish_test ; return }
26 set sqlite_sync_count 0
27 proc cond_incr_sync_count {adj} {
28 global sqlite_sync_count
29 if {$::tcl_platform(platform) == "windows"} {
30 incr sqlite_sync_count $adj
33 incr sqlite_sync_count $adj
38 proc set_tvfs_hdr {file args} {
40 # Set $nHdr to the number of bytes in the wal-index header:
42 set nInt [expr {$nHdr/4}]
44 if {[llength $args]>2} {
45 error {wrong # args: should be "set_tvfs_hdr fileName ?val1? ?val2?"}
48 set blob [tvfs shm $file]
49 if {$::tcl_platform(byteOrder)=="bigEndian"} {set fmt I} {set fmt i}
51 if {[llength $args]} {
52 set ia [lindex $args 0]
54 if {[llength $args]==2} {
55 set ib [lindex $args 1]
57 binary scan $blob a[expr $nHdr*2]a* dummy tail
58 set blob [binary format ${fmt}${nInt}${fmt}${nInt}a* $ia $ib $tail]
62 binary scan $blob ${fmt}${nInt} ints
66 proc incr_tvfs_hdr {file idx incrval} {
67 set ints [set_tvfs_hdr $file]
68 set v [lindex $ints $idx]
71 set_tvfs_hdr $file $ints
75 #-------------------------------------------------------------------------
78 # Set up a small database containing a single table. The database is not
79 # checkpointed during the test - all content resides in the log file.
81 # Two connections are established to the database file - a writer ([db])
82 # and a reader ([db2]). For each of the 8 integer fields in the wal-index
83 # header (6 fields and 2 checksum values), do the following:
85 # 1. Modify the database using the writer.
87 # 2. Attempt to read the database using the reader. Before the reader
88 # has a chance to snapshot the wal-index header, increment one
89 # of the integer fields (so that the reader ends up with a corrupted
92 # 3. Check that the reader recovers the wal-index and reads the correct
96 proc tvfs_cb {method filename args} {
97 set ::filename $filename
105 sqlite3 db test.db -vfs tvfs
106 sqlite3 db2 test.db -vfs tvfs
109 PRAGMA journal_mode = WAL;
113 INSERT INTO t1 VALUES(1);
114 INSERT INTO t1 VALUES(2);
115 INSERT INTO t1 VALUES(3);
116 INSERT INTO t1 VALUES(4);
117 SELECT count(a), sum(a) FROM t1;
121 execsql { SELECT count(a), sum(a) FROM t1 } db2
125 {0 1 lock exclusive} {1 2 lock exclusive} \
126 {4 1 lock exclusive} {4 1 unlock exclusive} \
127 {5 1 lock exclusive} {5 1 unlock exclusive} \
128 {6 1 lock exclusive} {6 1 unlock exclusive} \
129 {7 1 lock exclusive} {7 1 unlock exclusive} \
130 {1 2 unlock exclusive} {0 1 unlock exclusive} \
133 {4 1 lock shared} {4 1 unlock shared} \
136 {4 1 lock exclusive} {4 1 unlock exclusive} \
139 foreach {tn iInsert res wal_index_hdr_mod wal_locks} "
140 2 5 {5 15} 0 {$RECOVER $READ}
141 3 6 {6 21} 1 {$RECOVER $READ}
142 4 7 {7 28} 2 {$RECOVER $READ}
143 5 8 {8 36} 3 {$RECOVER $READ}
144 6 9 {9 45} 4 {$RECOVER $READ}
145 7 10 {10 55} 5 {$RECOVER $READ}
146 8 11 {11 66} 6 {$RECOVER $READ}
147 9 12 {12 78} 7 {$RECOVER $READ}
148 10 13 {13 91} 8 {$RECOVER $READ}
149 11 14 {14 105} 9 {$RECOVER $READ}
150 12 15 {15 120} -1 {$INITSLOT $READ}
153 do_test wal2-1.$tn.1 {
154 execsql { INSERT INTO t1 VALUES($iInsert) }
156 proc tvfs_cb {method args} {
157 lappend ::locks [lindex $args 2]
161 if {$::wal_index_hdr_mod >= 0} {
162 incr_tvfs_hdr $::filename $::wal_index_hdr_mod 1
164 execsql { SELECT count(a), sum(a) FROM t1 } db2
167 do_test wal2-1.$tn.2 {
174 forcedelete test.db test.db-wal test.db-journal
176 #-------------------------------------------------------------------------
177 # This test case is very similar to the previous one, except, after
178 # the reader reads the corrupt wal-index header, but before it has
179 # a chance to re-read it under the cover of the RECOVER lock, the
180 # wal-index header is replaced with a valid, but out-of-date, header.
182 # Because the header checksum looks Ok, the reader does not run recovery,
183 # it simply drops back to a READ lock and proceeds. But because the
184 # header is out-of-date, the reader reads the out-of-date snapshot.
186 # After this, the header is corrupted again and the reader is allowed
187 # to run recovery. This time, it sees an up-to-date snapshot of the
190 set WRITER [list 0 1 lock exclusive]
192 {0 1 lock exclusive} {0 1 unlock exclusive} \
193 {4 1 lock exclusive} {4 1 unlock exclusive} \
194 {4 1 lock shared} {4 1 unlock shared} \
201 proc tvfs_cb {method args} {
202 set ::filename [lindex $args 0]
206 sqlite3 db test.db -vfs tvfs
207 sqlite3 db2 test.db -vfs tvfs
210 PRAGMA journal_mode = WAL;
214 INSERT INTO t1 VALUES(1);
215 INSERT INTO t1 VALUES(2);
216 INSERT INTO t1 VALUES(3);
217 INSERT INTO t1 VALUES(4);
218 SELECT count(a), sum(a) FROM t1;
222 execsql { SELECT count(a), sum(a) FROM t1 } db2
225 foreach {tn iInsert res0 res1 wal_index_hdr_mod} {
231 7 10 {9 45} {10 55} 5
232 8 11 {10 55} {11 66} 6
233 9 12 {11 66} {12 78} 7
237 do_test wal2-2.$tn.1 {
238 set oldhdr [set_tvfs_hdr $::filename]
239 execsql { INSERT INTO t1 VALUES($iInsert) }
240 execsql { SELECT count(a), sum(a) FROM t1 }
243 do_test wal2-2.$tn.2 {
245 proc tvfs_cb {method args} {
246 set lock [lindex $args 2]
247 lappend ::locks $lock
248 if {$lock == $::WRITER} {
249 set_tvfs_hdr $::filename $::oldhdr
254 if {$::wal_index_hdr_mod >= 0} {
255 incr_tvfs_hdr $::filename $::wal_index_hdr_mod 1
257 execsql { SELECT count(a), sum(a) FROM t1 } db2
260 do_test wal2-2.$tn.3 {
264 do_test wal2-2.$tn.4 {
266 proc tvfs_cb {method args} {
267 set lock [lindex $args 2]
268 lappend ::locks $lock
272 if {$::wal_index_hdr_mod >= 0} {
273 incr_tvfs_hdr $::filename $::wal_index_hdr_mod 1
275 execsql { SELECT count(a), sum(a) FROM t1 } db2
281 forcedelete test.db test.db-wal test.db-journal
285 #-------------------------------------------------------------------------
286 # This test case - wal2-3.* - tests the response of the library to an
287 # SQLITE_BUSY when attempting to obtain a READ or RECOVER lock.
289 # wal2-3.0 - 2: SQLITE_BUSY when obtaining a READ lock
290 # wal2-3.3 - 6: SQLITE_BUSY when obtaining a RECOVER lock
293 proc tvfs_cb {method args} {
294 if {$method == "xShmLock"} {
295 if {[info exists ::locked]} { return SQLITE_BUSY }
301 if {$x>3} { unset -nocomplain ::locked }
307 sqlite3 db test.db -vfs tvfs
311 PRAGMA journal_mode = WAL;
313 INSERT INTO t1 VALUES(1);
314 INSERT INTO t1 VALUES(2);
315 INSERT INTO t1 VALUES(3);
316 INSERT INTO t1 VALUES(4);
323 execsql { SELECT count(a), sum(a) FROM t1 }
330 proc tvfs_cb {method args} {
331 if {$method == "xShmLock"} {
332 if {[info exists ::sabotage]} {
333 unset -nocomplain ::sabotage
334 incr_tvfs_hdr [lindex $args 0] 1 1
336 if {[info exists ::locked] && [lindex $args 2] == "RECOVER"} {
344 list [info exists ::sabotage] [info exists ::locked]
347 execsql { SELECT count(a), sum(a) FROM t1 }
350 list [info exists ::sabotage] [info exists ::locked]
354 forcedelete test.db test.db-wal test.db-journal
358 #-------------------------------------------------------------------------
359 # Test that a database connection using a VFS that does not support the
360 # xShmXXX interfaces cannot open a WAL database.
365 PRAGMA auto_vacuum = 0;
366 PRAGMA journal_mode = WAL;
367 CREATE TABLE data(x);
368 INSERT INTO data VALUES('need xShmOpen to see this');
369 PRAGMA wal_checkpoint;
371 # Three pages in the WAL file at this point: One copy of page 1 and two
372 # of the root page for table "data".
376 testvfs tvfs -noshm 1
377 sqlite3 db test.db -vfs tvfs
378 catchsql { SELECT * FROM data }
379 } {1 {unable to open database file}}
383 sqlite3 db test.db -vfs tvfs
384 catchsql { SELECT * FROM data }
385 } {0 {{need xShmOpen to see this}}}
389 #-------------------------------------------------------------------------
390 # Test that if a database connection is forced to run recovery before it
391 # can perform a checkpoint, it does not transition into RECOVER state.
393 # UPDATE: This has now changed. When running a checkpoint, if recovery is
394 # required the client grabs all exclusive locks (just as it would for a
395 # recovery performed as a pre-cursor to a normal database transaction).
397 set expected_locks [list]
398 lappend expected_locks {1 1 lock exclusive} ;# Lock checkpoint
399 lappend expected_locks {0 1 lock exclusive} ;# Lock writer
400 lappend expected_locks {2 1 lock exclusive} ;# Lock recovery
401 # lappend expected_locks {4 4 lock exclusive} ;# Lock all aReadMark[]
402 lappend expected_locks {4 1 lock exclusive} ;# Lock aReadMark[1]
403 lappend expected_locks {4 1 unlock exclusive} ;# Unlock aReadMark[1]
404 lappend expected_locks {5 1 lock exclusive}
405 lappend expected_locks {5 1 unlock exclusive}
406 lappend expected_locks {6 1 lock exclusive}
407 lappend expected_locks {6 1 unlock exclusive}
408 lappend expected_locks {7 1 lock exclusive}
409 lappend expected_locks {7 1 unlock exclusive}
410 lappend expected_locks {2 1 unlock exclusive} ;# Unlock recovery
411 # lappend expected_locks {4 4 unlock exclusive} ;# Unlock all aReadMark[]
412 lappend expected_locks {0 1 unlock exclusive} ;# Unlock writer
413 lappend expected_locks {3 1 lock exclusive} ;# Lock aReadMark[0]
414 lappend expected_locks {3 1 unlock exclusive} ;# Unlock aReadMark[0]
415 lappend expected_locks {1 1 unlock exclusive} ;# Unlock checkpoint
417 proc tvfs_cb {method args} {
418 set ::shm_file [lindex $args 0]
419 if {$method == "xShmLock"} { lappend ::locks [lindex $args 2] }
420 return $::tvfs_cb_return
422 set tvfs_cb_return SQLITE_OK
427 sqlite3 db test.db -vfs tvfs
429 PRAGMA journal_mode = WAL;
431 INSERT INTO x VALUES(1);
434 incr_tvfs_hdr $::shm_file 1 1
436 execsql { PRAGMA wal_checkpoint }
442 #-------------------------------------------------------------------------
443 # This block, test cases wal2-6.*, tests the operation of WAL with
444 # "PRAGMA locking_mode=EXCLUSIVE" set.
446 # wal2-6.1.*: Changing to WAL mode before setting locking_mode=exclusive.
448 # wal2-6.2.*: Changing to WAL mode after setting locking_mode=exclusive.
450 # wal2-6.3.*: Changing back to rollback mode from WAL mode after setting
451 # locking_mode=exclusive.
453 # wal2-6.4.*: Check that xShmLock calls are omitted in exclusive locking
458 # wal2-6.6.*: Check that if the xShmLock() to reaquire a WAL read-lock when
459 # exiting exclusive mode fails (i.e. SQLITE_IOERR), then the
460 # connection silently remains in exclusive mode.
463 forcedelete test.db test.db-wal test.db-journal
466 Pragma Journal_Mode = Wal;
470 execsql { PRAGMA lock_status }
471 } {main unlocked temp closed}
474 SELECT * FROM sqlite_master;
475 Pragma Locking_Mode = Exclusive;
479 CREATE TABLE t1(a, b);
480 INSERT INTO t1 VALUES(1, 2);
484 } {main exclusive temp closed}
487 PRAGMA locking_mode = normal;
490 } {normal main exclusive temp closed}
496 } {1 2 main shared temp closed}
499 INSERT INTO t1 VALUES(3, 4);
502 } {main shared temp closed}
506 forcedelete test.db test.db-wal test.db-journal
509 Pragma Locking_Mode = Exclusive;
510 Pragma Journal_Mode = Wal;
513 } {exclusive wal main exclusive temp closed}
517 CREATE TABLE t1(a, b);
518 INSERT INTO t1 VALUES(1, 2);
522 } {main exclusive temp closed}
526 execsql { SELECT * FROM sqlite_master }
527 execsql { PRAGMA LOCKING_MODE = EXCLUSIVE }
534 } {1 2 main shared temp closed}
537 INSERT INTO t1 VALUES(3, 4);
540 } {main exclusive temp closed}
543 PRAGMA locking_mode = NORMAL;
546 } {normal main exclusive temp closed}
549 BEGIN IMMEDIATE; COMMIT;
552 } {main shared temp closed}
555 PRAGMA locking_mode = EXCLUSIVE;
556 BEGIN IMMEDIATE; COMMIT;
557 PRAGMA locking_mode = NORMAL;
563 } {1 2 3 4 main shared temp closed}
566 INSERT INTO t1 VALUES(5, 6);
570 } {1 2 3 4 5 6 main shared temp closed}
574 forcedelete test.db test.db-wal test.db-journal
577 PRAGMA journal_mode = WAL;
578 PRAGMA locking_mode = exclusive;
581 INSERT INTO t1 VALUES('Chico');
582 INSERT INTO t1 VALUES('Harpo');
585 list [file exists test.db-wal] [file exists test.db-journal]
588 execsql { PRAGMA journal_mode = DELETE }
589 file exists test.db-wal
592 execsql { PRAGMA lock_status }
593 } {main exclusive temp closed}
597 INSERT INTO t1 VALUES('Groucho');
600 if {[atomic_batch_write test.db]==0} {
601 do_test wal2-6.3.4.1 {
602 list [file exists test.db-wal] [file exists test.db-journal]
606 execsql { PRAGMA lock_status }
607 } {main exclusive temp closed}
611 if {[atomic_batch_write test.db]==0} {
612 do_test wal2-6.3.6.1 {
613 list [file exists test.db-wal] [file exists test.db-journal]
617 execsql { PRAGMA lock_status }
618 } {main exclusive temp closed}
622 # This test - wal2-6.4.* - uses a single database connection and the
623 # [testvfs] instrumentation to test that xShmLock() is being called
624 # as expected when a WAL database is used with locking_mode=exclusive.
627 forcedelete test.db test.db-wal test.db-journal
628 proc tvfs_cb {method args} {
629 set ::shm_file [lindex $args 0]
630 if {$method == "xShmLock"} { lappend ::locks [lindex $args 2] }
635 sqlite3 db test.db -vfs tvfs
640 {0 1 lock exclusive} {1 2 lock exclusive}
641 {4 1 lock exclusive} {4 1 unlock exclusive}
642 {5 1 lock exclusive} {5 1 unlock exclusive}
643 {6 1 lock exclusive} {6 1 unlock exclusive}
644 {7 1 lock exclusive} {7 1 unlock exclusive}
645 {1 2 unlock exclusive} {0 1 unlock exclusive}
648 {3 1 lock shared} {3 1 unlock shared}
650 set READMARK0_WRITE {
652 {0 1 lock exclusive} {3 1 unlock shared}
653 {4 1 lock exclusive} {4 1 unlock exclusive} {4 1 lock shared}
654 {0 1 unlock exclusive} {4 1 unlock shared}
657 {4 1 lock exclusive} {4 1 unlock exclusive}
660 {4 1 lock shared} {4 1 unlock shared}
662 set READMARK1_WRITE {
664 {0 1 lock exclusive} {0 1 unlock exclusive}
668 foreach {tn sql res expected_locks} {
670 PRAGMA auto_vacuum = 0;
671 PRAGMA journal_mode = WAL;
674 INSERT INTO t1 VALUES('Leonard');
675 INSERT INTO t1 VALUES('Arthur');
683 # This test should do the READMARK1_SET locking to populate the
684 # aReadMark[1] slot with the current mxFrame value. Followed by
685 # READMARK1_READ to read the database.
694 # aReadMark[1] is already set to mxFrame. So just READMARK1_READ
695 # this time, not READMARK1_SET.
697 SELECT * FROM t1 ORDER BY x
703 PRAGMA locking_mode = exclusive
707 INSERT INTO t1 VALUES('Julius Henry');
709 } {Leonard Arthur {Julius Henry}} {
714 INSERT INTO t1 VALUES('Karl');
716 } {Leonard Arthur {Julius Henry} Karl} { }
719 PRAGMA locking_mode = normal
723 SELECT * FROM t1 ORDER BY x
724 } {Arthur {Julius Henry} Karl Leonard} $READMARK1_READ
726 10 { DELETE FROM t1 } {} $READMARK1_WRITE
737 foreach el [subst $expected_locks] { lappend L $el }
740 foreach sq [split $sql "\n"] {
741 set sq [string trim $sq]
742 if {[string match {#*} $sq]==0} {append S "$sq\n"}
746 do_test wal2-6.4.$tn.1 { execsql $S } $res
747 do_test wal2-6.4.$tn.2 { set ::locks } $L
756 PRAGMA auto_vacuum = 0;
757 PRAGMA journal_mode = wal;
758 PRAGMA locking_mode = exclusive;
759 CREATE TABLE t2(a, b);
760 PRAGMA wal_checkpoint;
761 INSERT INTO t2 VALUES('I', 'II');
764 } {wal exclusive 0 2 2 wal}
767 PRAGMA locking_mode = normal;
768 INSERT INTO t2 VALUES('III', 'IV');
769 PRAGMA locking_mode = exclusive;
772 } {normal exclusive I II III IV}
774 execsql { PRAGMA wal_checkpoint }
778 proc lock_control {method filename handle spec} {
779 foreach {start n op type} $spec break
780 if {$op == "lock"} { return SQLITE_IOERR }
785 T script lock_control
787 sqlite3 db test.db -vfs T
788 execsql { SELECT * FROM sqlite_master }
789 execsql { PRAGMA locking_mode = exclusive }
790 execsql { INSERT INTO t2 VALUES('V', 'VI') }
793 execsql { PRAGMA locking_mode = normal }
795 execsql { INSERT INTO t2 VALUES('VII', 'VIII') }
798 # At this point the connection should still be in exclusive-mode, even
799 # though it tried to exit exclusive-mode when committing the INSERT
800 # statement above. To exit exclusive mode, SQLite has to take a read-lock
801 # on the WAL file using xShmLock(). Since that call failed, it remains
804 sqlite3 db2 test.db -vfs T
805 catchsql { SELECT * FROM t2 } db2
806 } {1 {database is locked}}
810 execsql { INSERT INTO t2 VALUES('IX', 'X') }
813 # This time, we have successfully exited exclusive mode. So the second
814 # connection can read the database.
815 sqlite3 db2 test.db -vfs T
816 catchsql { SELECT * FROM t2 } db2
817 } {0 {I II III IV V VI VII VIII IX X}}
823 #-------------------------------------------------------------------------
824 # Test a theory about the checksum algorithm. Theory was false and this
825 # test did not provoke a bug.
827 forcedelete test.db test.db-wal test.db-journal
831 PRAGMA page_size = 4096;
832 PRAGMA journal_mode = WAL;
833 CREATE TABLE t1(a, b);
838 forcecopy test.db test2.db
839 forcecopy test.db-wal test2.db-wal
840 # The first 32 bytes of the WAL file contain the WAL header. Offset 48
841 # is the first byte of the checksum for the first frame in the WAL.
842 # The following three lines replaces the contents of that byte with
845 if {$newval == [hexio_read test2.db-wal 48 1]} { set newval 00 }
846 hexio_write test2.db-wal 48 $newval
850 execsql { PRAGMA wal_checkpoint } db2
851 execsql { SELECT * FROM sqlite_master } db2
855 forcedelete test.db test.db-wal test.db-journal
859 PRAGMA auto_vacuum=OFF;
860 PRAGMA page_size = 1024;
861 PRAGMA journal_mode = WAL;
863 INSERT INTO t1 VALUES(zeroblob(8188*1020));
865 PRAGMA wal_checkpoint;
868 SELECT rootpage>=8192 FROM sqlite_master WHERE tbl_name = 't2';
873 PRAGMA cache_size = 10;
876 INSERT INTO t3 VALUES(randomblob(900));
877 INSERT INTO t3 SELECT randomblob(900) FROM t3;
878 INSERT INTO t2 VALUES('hello');
879 INSERT INTO t3 SELECT randomblob(900) FROM t3;
880 INSERT INTO t3 SELECT randomblob(900) FROM t3;
881 INSERT INTO t3 SELECT randomblob(900) FROM t3;
882 INSERT INTO t3 SELECT randomblob(900) FROM t3;
883 INSERT INTO t3 SELECT randomblob(900) FROM t3;
884 INSERT INTO t3 SELECT randomblob(900) FROM t3;
888 INSERT INTO t2 VALUES('goodbye');
889 INSERT INTO t3 SELECT randomblob(900) FROM t3;
890 INSERT INTO t3 SELECT randomblob(900) FROM t3;
895 execsql { SELECT * FROM t2 }
900 #-------------------------------------------------------------------------
901 # Test that even if the checksums for both are valid, if the two copies
902 # of the wal-index header in the wal-index do not match, the client
903 # runs (or at least tries to run) database recovery.
906 proc get_name {method args} { set ::filename [lindex $args 0] ; tvfs filter {} }
911 forcedelete test.db test.db-wal test.db-journal
913 sqlite3 db test.db -vfs tvfs
915 PRAGMA journal_mode = WAL;
917 INSERT INTO x VALUES('Barton');
918 INSERT INTO x VALUES('Deakin');
921 # Set $wih(1) to the contents of the wal-index header after
922 # the frames associated with the first two rows in table 'x' have
923 # been inserted. Then insert one more row and set $wih(2)
924 # to the new value of the wal-index header.
926 # If the $wih(1) is written into the wal-index before running
927 # a read operation, the client will see only the first two rows. If
928 # $wih(2) is written into the wal-index, the client will see
929 # three rows. If an invalid header is written into the wal-index, then
930 # the client will run recovery and see three rows.
932 set wih(1) [set_tvfs_hdr $::filename]
933 execsql { INSERT INTO x VALUES('Watson') }
934 set wih(2) [set_tvfs_hdr $::filename]
936 sqlite3 db2 test.db -vfs tvfs
937 execsql { SELECT * FROM x } db2
938 } {Barton Deakin Watson}
940 foreach {tn hdr1 hdr2 res} [list \
941 3 $wih(1) $wih(1) {Barton Deakin} \
942 4 $wih(1) $wih(2) {Barton Deakin Watson} \
943 5 $wih(2) $wih(1) {Barton Deakin Watson} \
944 6 $wih(2) $wih(2) {Barton Deakin Watson} \
945 7 $wih(1) $wih(1) {Barton Deakin} \
946 8 {0 0 0 0 0 0 0 0 0 0 0 0} {0 0 0 0 0 0 0 0 0 0 0 0} {Barton Deakin Watson}
949 set_tvfs_hdr $::filename $hdr1 $hdr2
950 execsql { SELECT * FROM x } db2
957 #-------------------------------------------------------------------------
958 # This block of tests - wal2-10.* - focus on the libraries response to
959 # new versions of the wal or wal-index formats.
961 # wal2-10.1.*: Test that the library refuses to "recover" a new WAL
964 # wal2-10.2.*: Test that the library refuses to read or write a database
965 # if the wal-index version is newer than it understands.
967 # At time of writing, the only versions of the wal and wal-index formats
968 # that exist are versions 3007000 (corresponding to SQLite version 3.7.0,
969 # the first version of SQLite to feature wal mode).
971 do_test wal2-10.1.1 {
972 faultsim_delete_and_reopen
974 PRAGMA journal_mode = WAL;
975 CREATE TABLE t1(a, b);
976 PRAGMA wal_checkpoint;
977 INSERT INTO t1 VALUES(1, 2);
978 INSERT INTO t1 VALUES(3, 4);
980 faultsim_save_and_close
982 do_test wal2-10.1.2 {
983 faultsim_restore_and_reopen
984 execsql { SELECT * FROM t1 }
986 do_test wal2-10.1.3 {
987 faultsim_restore_and_reopen
988 set hdr [wal_set_walhdr test.db-wal]
991 do_test wal2-10.1.4 {
993 wal_set_walhdr test.db-wal $hdr
994 catchsql { SELECT * FROM t1 }
995 } {1 {unable to open database file}}
997 testvfs tvfs -default 1
998 do_test wal2-10.2.1 {
999 faultsim_restore_and_reopen
1000 execsql { SELECT * FROM t1 }
1002 do_test wal2-10.2.2 {
1003 set hdr [set_tvfs_hdr $::filename]
1006 do_test wal2-10.2.3 {
1008 wal_fix_walindex_cksum hdr
1009 set_tvfs_hdr $::filename $hdr
1010 catchsql { SELECT * FROM t1 }
1011 } {1 {unable to open database file}}
1015 #-------------------------------------------------------------------------
1016 # This block of tests - wal2-11.* - tests that it is not possible to put
1017 # the library into an infinite loop by presenting it with a corrupt
1018 # hash table (one that appears to contain a single chain of infinite
1021 # wal2-11.1.*: While reading the hash-table.
1023 # wal2-11.2.*: While writing the hash-table.
1025 testvfs tvfs -default 1
1027 faultsim_delete_and_reopen
1029 PRAGMA journal_mode = WAL;
1030 CREATE TABLE t1(a, b, c);
1031 INSERT INTO t1 VALUES(1, 2, 3);
1032 INSERT INTO t1 VALUES(4, 5, 6);
1033 INSERT INTO t1 VALUES(7, 8, 9);
1036 } {wal 1 2 3 4 5 6 7 8 9}
1038 do_test wal2-11.1.1 {
1040 execsql { SELECT name FROM sqlite_master } db2
1043 if {$::tcl_version>=8.5} {
1044 # Set all zeroed slots in the first hash table to invalid values.
1046 set blob [string range [tvfs shm $::filename] 0 16383]
1047 set I [string range [tvfs shm $::filename] 16384 end]
1051 lappend I [expr $p ? $p : 400]
1053 append blob [binary format t* $I]
1054 tvfs shm $::filename $blob
1056 catchsql { INSERT INTO t1 VALUES(10, 11, 12) }
1057 } {1 {database disk image is malformed}}
1059 # Fill up the hash table on the first page of shared memory with 0x55 bytes.
1061 set blob [string range [tvfs shm $::filename] 0 16383]
1062 append blob [string repeat [binary format c 55] 16384]
1063 tvfs shm $::filename $blob
1065 catchsql { SELECT * FROM t1 } db2
1066 } {1 {database disk image is malformed}}
1073 #-------------------------------------------------------------------------
1074 # If a connection is required to create a WAL or SHM file, it creates
1075 # the new files with the same file-system permissions as the database
1076 # file itself. Test this.
1078 if {$::tcl_platform(platform) == "unix"} {
1079 faultsim_delete_and_reopen
1080 # Changed on 2012-02-13: umask is deliberately ignored for -wal files.
1081 #set umask [exec /bin/sh -c umask]
1088 CREATE TABLE tx(y, z);
1089 PRAGMA journal_mode = WAL;
1092 list [file exists test.db-wal] [file exists test.db-shm]
1095 foreach {tn permissions} {
1101 set effective [format %.5o [expr $permissions & ~$umask]]
1102 do_test wal2-12.2.$tn.1 {
1103 file attributes test.db -permissions $permissions
1104 string map {o 0} [file attributes test.db -permissions]
1106 do_test wal2-12.2.$tn.2 {
1107 list [file exists test.db-wal] [file exists test.db-shm]
1109 do_test wal2-12.2.$tn.3 {
1111 execsql { INSERT INTO tx DEFAULT VALUES }
1112 list [file exists test.db-wal] [file exists test.db-shm]
1114 do_test wal2-12.2.$tn.4 {
1115 set x [list [file attr test.db-wal -perm] [file attr test.db-shm -perm]]
1117 } [list $effective $effective]
1118 do_test wal2-12.2.$tn.5 {
1120 list [file exists test.db-wal] [file exists test.db-shm]
1125 #-------------------------------------------------------------------------
1126 # Test the libraries response to discovering that one or more of the
1127 # database, wal or shm files cannot be opened, or can only be opened
1130 if {$::tcl_platform(platform) == "unix"} {
1133 foreach f {test.db test.db-wal test.db-shm} {
1134 if {[file exists $f]} {
1135 lappend L [file attr $f -perm]
1143 faultsim_delete_and_reopen
1145 PRAGMA journal_mode = WAL;
1146 CREATE TABLE t1(a, b);
1147 PRAGMA wal_checkpoint;
1148 INSERT INTO t1 VALUES('3.14', '2.72');
1150 do_test wal2-13.1.1 {
1151 list [file exists test.db-shm] [file exists test.db-wal]
1153 faultsim_save_and_close
1155 foreach {tn db_perm wal_perm shm_perm can_open can_read can_write} {
1156 2 00644 00644 00644 1 1 1
1157 3 00644 00400 00644 1 1 0
1158 4 00644 00644 00400 1 1 0
1159 5 00400 00644 00644 1 1 0
1161 7 00644 00000 00644 1 0 0
1162 8 00644 00644 00000 1 0 0
1163 9 00000 00644 00644 0 0 0
1166 do_test wal2-13.$tn.1 {
1167 file attr test.db -perm $db_perm
1168 file attr test.db-wal -perm $wal_perm
1169 file attr test.db-shm -perm $shm_perm
1171 set L [file attr test.db -perm]
1172 lappend L [file attr test.db-wal -perm]
1173 lappend L [file attr test.db-shm -perm]
1175 } [list $db_perm $wal_perm $shm_perm]
1177 # If $can_open is true, then it should be possible to open a database
1178 # handle. Otherwise, if $can_open is 0, attempting to open the db
1179 # handle throws an "unable to open database file" exception.
1182 set r(0) {1 {unable to open database file}}
1183 do_test wal2-13.$tn.2 {
1184 list [catch {sqlite3 db test.db ; set {} ok} msg] $msg
1189 # If $can_read is true, then the client should be able to read from
1190 # the database file. If $can_read is false, attempting to read should
1191 # throw the "unable to open database file" exception.
1193 set a(0) {1 {unable to open database file}}
1194 set a(1) {0 {3.14 2.72}}
1195 do_test wal2-13.$tn.3 {
1196 catchsql { SELECT * FROM t1 }
1199 # Now try to write to the db file. If the client can read but not
1200 # write, then it should throw the familiar "unable to open db file"
1201 # exception. If it can read but not write, the exception should
1202 # be "attempt to write a read only database".
1204 # If the client can read and write, the operation should succeed.
1206 set b(0,0) {1 {unable to open database file}}
1207 set b(1,0) {1 {attempt to write a readonly database}}
1209 do_test wal2-13.$tn.4 {
1210 catchsql { INSERT INTO t1 DEFAULT VALUES }
1211 } $b($can_read,$can_write)
1217 #-------------------------------------------------------------------------
1218 # Test that "PRAGMA checkpoint_fullsync" appears to be working.
1220 foreach {tn sql reslist} {
1221 1 { } {10 0 4 0 6 0}
1222 2 { PRAGMA checkpoint_fullfsync = 1 } {10 6 4 3 6 3}
1223 3 { PRAGMA checkpoint_fullfsync = 0 } {10 0 4 0 6 0}
1225 ifcapable default_ckptfullfsync {
1226 if {[string trim $sql]==""} continue
1228 faultsim_delete_and_reopen
1230 execsql {PRAGMA auto_vacuum = 0; PRAGMA synchronous = FULL;}
1232 do_execsql_test wal2-14.$tn.0 { PRAGMA page_size = 4096 } {}
1233 do_execsql_test wal2-14.$tn.1 { PRAGMA journal_mode = WAL } {wal}
1235 set sqlite_sync_count 0
1236 set sqlite_fullsync_count 0
1238 do_execsql_test wal2-14.$tn.2 {
1239 PRAGMA wal_autocheckpoint = 10;
1240 CREATE TABLE t1(a, b); -- 2 wal syncs
1241 INSERT INTO t1 VALUES(1, 2); -- 2 wal sync
1242 PRAGMA wal_checkpoint; -- 1 wal sync, 1 db sync
1244 INSERT INTO t1 VALUES(3, 4);
1245 INSERT INTO t1 VALUES(5, 6);
1246 COMMIT; -- 2 wal sync
1247 PRAGMA wal_checkpoint; -- 1 wal sync, 1 db sync
1250 do_test wal2-14.$tn.3 {
1251 cond_incr_sync_count 1
1252 list $sqlite_sync_count $sqlite_fullsync_count
1253 } [lrange $reslist 0 1]
1255 set sqlite_sync_count 0
1256 set sqlite_fullsync_count 0
1258 do_test wal2-14.$tn.4 {
1259 execsql { INSERT INTO t1 VALUES(7, zeroblob(12*4096)) }
1260 list $sqlite_sync_count $sqlite_fullsync_count
1261 } [lrange $reslist 2 3]
1263 set sqlite_sync_count 0
1264 set sqlite_fullsync_count 0
1266 do_test wal2-14.$tn.5 {
1267 execsql { PRAGMA wal_autocheckpoint = 1000 }
1268 execsql { INSERT INTO t1 VALUES(9, 10) }
1269 execsql { INSERT INTO t1 VALUES(11, 12) }
1270 execsql { INSERT INTO t1 VALUES(13, 14) }
1272 list $sqlite_sync_count $sqlite_fullsync_count
1273 } [lrange $reslist 4 5]
1278 # PRAGMA checkpoint_fullsync
1280 # PRAGMA synchronous
1282 foreach {tn settings restart_sync commit_sync ckpt_sync} {
1283 1 {0 0 off} {0 0} {0 0} {0 0}
1284 2 {0 0 normal} {1 0} {0 0} {2 0}
1285 3 {0 0 full} {2 0} {1 0} {2 0}
1287 4 {0 1 off} {0 0} {0 0} {0 0}
1288 5 {0 1 normal} {0 1} {0 0} {0 2}
1289 6 {0 1 full} {0 2} {0 1} {0 2}
1291 7 {1 0 off} {0 0} {0 0} {0 0}
1292 8 {1 0 normal} {0 1} {0 0} {0 2}
1293 9 {1 0 full} {1 1} {1 0} {0 2}
1295 10 {1 1 off} {0 0} {0 0} {0 0}
1296 11 {1 1 normal} {0 1} {0 0} {0 2}
1297 12 {1 1 full} {0 2} {0 1} {0 2}
1301 testvfs tvfs -default 1
1304 proc xSyncCb {method file fileid flags} {
1309 do_execsql_test 15.$tn.1 "
1310 PRAGMA page_size = 4096;
1312 PRAGMA wal_autocheckpoint = OFF;
1313 PRAGMA journal_mode = WAL;
1314 PRAGMA checkpoint_fullfsync = [lindex $settings 0];
1315 PRAGMA fullfsync = [lindex $settings 1];
1316 PRAGMA synchronous = [lindex $settings 2];
1322 execsql { INSERT INTO t1 VALUES('abc') }
1323 list $::sync(normal) $::sync(full)
1329 execsql { INSERT INTO t1 VALUES('abc') }
1330 list $::sync(normal) $::sync(full)
1336 execsql { INSERT INTO t1 VALUES('def') }
1337 list $::sync(normal) $::sync(full)
1343 execsql { PRAGMA wal_checkpoint }
1344 list $::sync(normal) $::sync(full)