xref: /sqlite-3.40.0/test/wal2.test (revision 60176fa9)
1# 2010 May 5
2#
3# The author disclaims copyright to this source code.  In place of
4# a legal notice, here is a blessing:
5#
6#    May you do good and not evil.
7#    May you find forgiveness for yourself and forgive others.
8#    May you share freely, never taking more than you give.
9#
10#***********************************************************************
11# This file implements regression tests for SQLite library.  The
12# focus of this file is testing the operation of the library in
13# "PRAGMA journal_mode=WAL" mode.
14#
15
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18source $testdir/lock_common.tcl
19source $testdir/malloc_common.tcl
20source $testdir/wal_common.tcl
21
22ifcapable !wal {finish_test ; return }
23
24proc set_tvfs_hdr {file args} {
25
26  # Set $nHdr to the number of bytes in the wal-index header:
27  set nHdr 48
28  set nInt [expr {$nHdr/4}]
29
30  if {[llength $args]>2} {
31    error {wrong # args: should be "set_tvfs_hdr fileName ?val1? ?val2?"}
32  }
33
34  set blob [tvfs shm $file]
35
36  if {[llength $args]} {
37    set ia [lindex $args 0]
38    set ib $ia
39    if {[llength $args]==2} {
40      set ib [lindex $args 1]
41    }
42    binary scan $blob a[expr $nHdr*2]a* dummy tail
43    set blob [binary format i${nInt}i${nInt}a* $ia $ib $tail]
44    tvfs shm $file $blob
45  }
46
47  binary scan $blob i${nInt} ints
48  return $ints
49}
50
51proc incr_tvfs_hdr {file idx incrval} {
52  set ints [set_tvfs_hdr $file]
53  set v [lindex $ints $idx]
54  incr v $incrval
55  lset ints $idx $v
56  set_tvfs_hdr $file $ints
57}
58
59
60#-------------------------------------------------------------------------
61# Test case wal2-1.*:
62#
63# Set up a small database containing a single table. The database is not
64# checkpointed during the test - all content resides in the log file.
65#
66# Two connections are established to the database file - a writer ([db])
67# and a reader ([db2]). For each of the 8 integer fields in the wal-index
68# header (6 fields and 2 checksum values), do the following:
69#
70#   1. Modify the database using the writer.
71#
72#   2. Attempt to read the database using the reader. Before the reader
73#      has a chance to snapshot the wal-index header, increment one
74#      of the the integer fields (so that the reader ends up with a corrupted
75#      header).
76#
77#   3. Check that the reader recovers the wal-index and reads the correct
78#      database content.
79#
80do_test wal2-1.0 {
81  proc tvfs_cb {method filename args} {
82    set ::filename $filename
83    return SQLITE_OK
84  }
85
86  testvfs tvfs
87  tvfs script tvfs_cb
88  tvfs filter xShmOpen
89
90  sqlite3 db  test.db -vfs tvfs
91  sqlite3 db2 test.db -vfs tvfs
92
93  execsql {
94    PRAGMA journal_mode = WAL;
95    CREATE TABLE t1(a);
96  } db2
97  execsql {
98    INSERT INTO t1 VALUES(1);
99    INSERT INTO t1 VALUES(2);
100    INSERT INTO t1 VALUES(3);
101    INSERT INTO t1 VALUES(4);
102    SELECT count(a), sum(a) FROM t1;
103  }
104} {4 10}
105do_test wal2-1.1 {
106  execsql { SELECT count(a), sum(a) FROM t1 } db2
107} {4 10}
108
109set RECOVER [list                                      \
110  {0 1 lock exclusive}   {1 7 lock exclusive}          \
111  {1 7 unlock exclusive} {0 1 unlock exclusive}        \
112]
113set READ [list                                         \
114  {4 1 lock exclusive} {4 1 unlock exclusive}          \
115  {4 1 lock shared}    {4 1 unlock shared}             \
116]
117
118foreach {tn iInsert res wal_index_hdr_mod wal_locks} "
119         2    5   {5 15}    0             {$RECOVER $READ}
120         3    6   {6 21}    1             {$RECOVER $READ}
121         4    7   {7 28}    2             {$RECOVER $READ}
122         5    8   {8 36}    3             {$RECOVER $READ}
123         6    9   {9 45}    4             {$RECOVER $READ}
124         7   10   {10 55}   5             {$RECOVER $READ}
125         8   11   {11 66}   6             {$RECOVER $READ}
126         9   12   {12 78}   7             {$RECOVER $READ}
127        10   13   {13 91}   8             {$RECOVER $READ}
128        11   14   {14 105}  9             {$RECOVER $READ}
129        12   15   {15 120}  -1            {$READ}
130" {
131
132  do_test wal2-1.$tn.1 {
133    execsql { INSERT INTO t1 VALUES($iInsert) }
134    set ::locks [list]
135    proc tvfs_cb {method args} {
136      lappend ::locks [lindex $args 2]
137      return SQLITE_OK
138    }
139    tvfs filter xShmLock
140    if {$::wal_index_hdr_mod >= 0} {
141      incr_tvfs_hdr $::filename $::wal_index_hdr_mod 1
142    }
143    execsql { SELECT count(a), sum(a) FROM t1 } db2
144  } $res
145
146  do_test wal2-1.$tn.2 {
147    set ::locks
148  } $wal_locks
149}
150db close
151db2 close
152tvfs delete
153file delete -force test.db test.db-wal test.db-journal
154
155#-------------------------------------------------------------------------
156# This test case is very similar to the previous one, except, after
157# the reader reads the corrupt wal-index header, but before it has
158# a chance to re-read it under the cover of the RECOVER lock, the
159# wal-index header is replaced with a valid, but out-of-date, header.
160#
161# Because the header checksum looks Ok, the reader does not run recovery,
162# it simply drops back to a READ lock and proceeds. But because the
163# header is out-of-date, the reader reads the out-of-date snapshot.
164#
165# After this, the header is corrupted again and the reader is allowed
166# to run recovery. This time, it sees an up-to-date snapshot of the
167# database file.
168#
169set WRITER [list 0 1 lock exclusive]
170set LOCKS  [list \
171  {0 1 lock exclusive} {0 1 unlock exclusive} \
172  {4 1 lock exclusive} {4 1 unlock exclusive} \
173  {4 1 lock shared}    {4 1 unlock shared}    \
174]
175do_test wal2-2.0 {
176
177  testvfs tvfs
178  tvfs script tvfs_cb
179  tvfs filter xShmOpen
180  proc tvfs_cb {method args} {
181    set ::filename [lindex $args 0]
182    return SQLITE_OK
183  }
184
185  sqlite3 db  test.db -vfs tvfs
186  sqlite3 db2 test.db -vfs tvfs
187
188  execsql {
189    PRAGMA journal_mode = WAL;
190    CREATE TABLE t1(a);
191  } db2
192  execsql {
193    INSERT INTO t1 VALUES(1);
194    INSERT INTO t1 VALUES(2);
195    INSERT INTO t1 VALUES(3);
196    INSERT INTO t1 VALUES(4);
197    SELECT count(a), sum(a) FROM t1;
198  }
199} {4 10}
200do_test wal2-2.1 {
201  execsql { SELECT count(a), sum(a) FROM t1 } db2
202} {4 10}
203
204foreach {tn iInsert res0 res1 wal_index_hdr_mod} {
205         2    5   {4 10}   {5 15}    0
206         3    6   {5 15}   {6 21}    1
207         4    7   {6 21}   {7 28}    2
208         5    8   {7 28}   {8 36}    3
209         6    9   {8 36}   {9 45}    4
210         7   10   {9 45}   {10 55}   5
211         8   11   {10 55}  {11 66}   6
212         9   12   {11 66}  {12 78}   7
213} {
214  tvfs filter xShmLock
215
216  do_test wal2-2.$tn.1 {
217    set oldhdr [set_tvfs_hdr $::filename]
218    execsql { INSERT INTO t1 VALUES($iInsert) }
219    execsql { SELECT count(a), sum(a) FROM t1 }
220  } $res1
221
222  do_test wal2-2.$tn.2 {
223    set ::locks [list]
224    proc tvfs_cb {method args} {
225      set lock [lindex $args 2]
226      lappend ::locks $lock
227      if {$lock == $::WRITER} {
228        set_tvfs_hdr $::filename $::oldhdr
229      }
230      return SQLITE_OK
231    }
232
233    if {$::wal_index_hdr_mod >= 0} {
234      incr_tvfs_hdr $::filename $::wal_index_hdr_mod 1
235    }
236    execsql { SELECT count(a), sum(a) FROM t1 } db2
237  } $res0
238
239  do_test wal2-2.$tn.3 {
240    set ::locks
241  } $LOCKS
242
243  do_test wal2-2.$tn.4 {
244    set ::locks [list]
245    proc tvfs_cb {method args} {
246      set lock [lindex $args 2]
247      lappend ::locks $lock
248      return SQLITE_OK
249    }
250
251    if {$::wal_index_hdr_mod >= 0} {
252      incr_tvfs_hdr $::filename $::wal_index_hdr_mod 1
253    }
254    execsql { SELECT count(a), sum(a) FROM t1 } db2
255  } $res1
256}
257db close
258db2 close
259tvfs delete
260file delete -force test.db test.db-wal test.db-journal
261
262
263if 0 {
264#-------------------------------------------------------------------------
265# This test case - wal2-3.* - tests the response of the library to an
266# SQLITE_BUSY when attempting to obtain a READ or RECOVER lock.
267#
268#   wal2-3.0 - 2: SQLITE_BUSY when obtaining a READ lock
269#   wal2-3.3 - 6: SQLITE_BUSY when obtaining a RECOVER lock
270#
271do_test wal2-3.0 {
272  proc tvfs_cb {method args} {
273    if {$method == "xShmLock"} {
274      if {[info exists ::locked]} { return SQLITE_BUSY }
275    }
276    return SQLITE_OK
277  }
278
279  proc busyhandler x {
280    if {$x>3} { unset -nocomplain ::locked }
281    return 0
282  }
283
284  testvfs tvfs
285  tvfs script tvfs_cb
286  sqlite3 db test.db -vfs tvfs
287  db busy busyhandler
288
289  execsql {
290    PRAGMA journal_mode = WAL;
291    CREATE TABLE t1(a);
292    INSERT INTO t1 VALUES(1);
293    INSERT INTO t1 VALUES(2);
294    INSERT INTO t1 VALUES(3);
295    INSERT INTO t1 VALUES(4);
296  }
297
298  set ::locked 1
299  info exists ::locked
300} {1}
301do_test wal2-3.1 {
302  execsql { SELECT count(a), sum(a) FROM t1 }
303} {4 10}
304do_test wal2-3.2 {
305  info exists ::locked
306} {0}
307
308do_test wal2-3.3 {
309  proc tvfs_cb {method args} {
310    if {$method == "xShmLock"} {
311      if {[info exists ::sabotage]} {
312        unset -nocomplain ::sabotage
313        incr_tvfs_hdr [lindex $args 0] 1 1
314      }
315      if {[info exists ::locked] && [lindex $args 2] == "RECOVER"} {
316        return SQLITE_BUSY
317      }
318    }
319    return SQLITE_OK
320  }
321  set ::sabotage 1
322  set ::locked 1
323  list [info exists ::sabotage] [info exists ::locked]
324} {1 1}
325do_test wal2-3.4 {
326  execsql { SELECT count(a), sum(a) FROM t1 }
327} {4 10}
328do_test wal2-3.5 {
329  list [info exists ::sabotage] [info exists ::locked]
330} {0 0}
331db close
332tvfs delete
333file delete -force test.db test.db-wal test.db-journal
334
335}
336
337#-------------------------------------------------------------------------
338# Test that a database connection using a VFS that does not support the
339# xShmXXX interfaces cannot open a WAL database.
340#
341do_test wal2-4.1 {
342  sqlite3 db test.db
343  execsql {
344    PRAGMA journal_mode = WAL;
345    CREATE TABLE data(x);
346    INSERT INTO data VALUES('need xShmOpen to see this');
347    PRAGMA wal_checkpoint;
348  }
349} {wal}
350do_test wal2-4.2 {
351  db close
352  testvfs tvfs -noshm 1
353  sqlite3 db test.db -vfs tvfs
354  catchsql { SELECT * FROM data }
355} {1 {unable to open database file}}
356do_test wal2-4.3 {
357  db close
358  testvfs tvfs
359  sqlite3 db test.db -vfs tvfs
360  catchsql { SELECT * FROM data }
361} {0 {{need xShmOpen to see this}}}
362db close
363tvfs delete
364
365#-------------------------------------------------------------------------
366# Test that if a database connection is forced to run recovery before it
367# can perform a checkpoint, it does not transition into RECOVER state.
368#
369# UPDATE: This has now changed. When running a checkpoint, if recovery is
370# required the client grabs all exclusive locks (just as it would for a
371# recovery performed as a pre-cursor to a normal database transaction).
372#
373set expected_locks [list]
374lappend expected_locks {1 1 lock exclusive}   ;# Lock checkpoint
375lappend expected_locks {0 1 lock exclusive}   ;# Lock writer
376lappend expected_locks {2 6 lock exclusive}   ;# Lock recovery & all aReadMark[]
377lappend expected_locks {2 6 unlock exclusive} ;# Unlock recovery & aReadMark[]
378lappend expected_locks {0 1 unlock exclusive} ;# Unlock writer
379lappend expected_locks {3 1 lock exclusive}   ;# Lock aReadMark[0]
380lappend expected_locks {3 1 unlock exclusive} ;# Unlock aReadMark[0]
381lappend expected_locks {1 1 unlock exclusive} ;# Unlock checkpoint
382do_test wal2-5.1 {
383  proc tvfs_cb {method args} {
384    set ::shm_file [lindex $args 0]
385    if {$method == "xShmLock"} { lappend ::locks [lindex $args 2] }
386    return $::tvfs_cb_return
387  }
388  set tvfs_cb_return SQLITE_OK
389
390  testvfs tvfs
391  tvfs script tvfs_cb
392
393  sqlite3 db test.db -vfs tvfs
394  execsql {
395    PRAGMA journal_mode = WAL;
396    CREATE TABLE x(y);
397    INSERT INTO x VALUES(1);
398  }
399
400  incr_tvfs_hdr $::shm_file 1 1
401  set ::locks [list]
402  execsql { PRAGMA wal_checkpoint }
403  set ::locks
404} $expected_locks
405db close
406tvfs delete
407
408#-------------------------------------------------------------------------
409# This block, test cases wal2-6.*, tests the operation of WAL with
410# "PRAGMA locking_mode=EXCLUSIVE" set.
411#
412#   wal2-6.1.*: Changing to WAL mode before setting locking_mode=exclusive.
413#
414#   wal2-6.2.*: Changing to WAL mode after setting locking_mode=exclusive.
415#
416#   wal2-6.3.*: Changing back to rollback mode from WAL mode after setting
417#               locking_mode=exclusive.
418#
419#   wal2-6.4.*: Check that xShmLock calls are omitted in exclusive locking
420#               mode.
421#
422#   wal2-6.5.*:
423#
424#   wal2-6.6.*: Check that if the xShmLock() to reaquire a WAL read-lock when
425#               exiting exclusive mode fails (i.e. SQLITE_IOERR), then the
426#               connection silently remains in exclusive mode.
427#
428do_test wal2-6.1.1 {
429  file delete -force test.db test.db-wal test.db-journal
430  sqlite3 db test.db
431  execsql {
432    Pragma Journal_Mode = Wal;
433    Pragma Locking_Mode = Exclusive;
434  }
435} {wal exclusive}
436do_test wal2-6.1.2 {
437  execsql { PRAGMA lock_status }
438} {main unlocked temp closed}
439do_test wal2-6.1.3 {
440  execsql {
441    BEGIN;
442      CREATE TABLE t1(a, b);
443      INSERT INTO t1 VALUES(1, 2);
444    COMMIT;
445    PRAGMA lock_status;
446  }
447} {main exclusive temp closed}
448do_test wal2-6.1.4 {
449  execsql {
450    PRAGMA locking_mode = normal;
451    PRAGMA lock_status;
452  }
453} {normal main exclusive temp closed}
454do_test wal2-6.1.5 {
455  execsql {
456    SELECT * FROM t1;
457    PRAGMA lock_status;
458  }
459} {1 2 main exclusive temp closed}
460do_test wal2-6.1.6 {
461  execsql {
462    INSERT INTO t1 VALUES(3, 4);
463    PRAGMA lock_status;
464  }
465} {main shared temp closed}
466db close
467
468do_test wal2-6.2.1 {
469  file delete -force test.db test.db-wal test.db-journal
470  sqlite3 db test.db
471  execsql {
472    Pragma Locking_Mode = Exclusive;
473    Pragma Journal_Mode = Wal;
474    Pragma Lock_Status;
475  }
476} {exclusive wal main exclusive temp closed}
477do_test wal2-6.2.2 {
478  execsql {
479    BEGIN;
480      CREATE TABLE t1(a, b);
481      INSERT INTO t1 VALUES(1, 2);
482    COMMIT;
483    Pragma loCK_STATus;
484  }
485} {main exclusive temp closed}
486do_test wal2-6.2.3 {
487  db close
488  sqlite3 db test.db
489  execsql { PRAGMA LOCKING_MODE = EXCLUSIVE }
490} {exclusive}
491do_test wal2-6.2.4 {
492  execsql {
493    SELECT * FROM t1;
494    pragma lock_status;
495  }
496} {1 2 main shared temp closed}
497do_test wal2-6.2.5 {
498  execsql {
499    INSERT INTO t1 VALUES(3, 4);
500    pragma lock_status;
501  }
502} {main exclusive temp closed}
503do_test wal2-6.2.6 {
504  execsql {
505    PRAGMA locking_mode = NORMAL;
506    pragma lock_status;
507  }
508} {normal main exclusive temp closed}
509do_test wal2-6.2.7 {
510  execsql {
511    BEGIN IMMEDIATE; COMMIT;
512    pragma lock_status;
513  }
514} {main shared temp closed}
515do_test wal2-6.2.8 {
516  execsql {
517    PRAGMA locking_mode = EXCLUSIVE;
518    BEGIN IMMEDIATE; COMMIT;
519    PRAGMA locking_mode = NORMAL;
520  }
521  execsql {
522    SELECT * FROM t1;
523    pragma lock_status;
524  }
525} {1 2 3 4 main exclusive temp closed}
526do_test wal2-6.2.9 {
527  execsql {
528    INSERT INTO t1 VALUES(5, 6);
529    SELECT * FROM t1;
530    pragma lock_status;
531  }
532} {1 2 3 4 5 6 main shared temp closed}
533db close
534
535do_test wal2-6.3.1 {
536  file delete -force test.db test.db-wal test.db-journal
537  sqlite3 db test.db
538  execsql {
539    PRAGMA journal_mode = WAL;
540    PRAGMA locking_mode = exclusive;
541    BEGIN;
542      CREATE TABLE t1(x);
543      INSERT INTO t1 VALUES('Chico');
544      INSERT INTO t1 VALUES('Harpo');
545    COMMIT;
546  }
547  list [file exists test.db-wal] [file exists test.db-journal]
548} {1 0}
549do_test wal2-6.3.2 {
550  execsql { PRAGMA journal_mode = DELETE }
551  file exists test.db-wal
552} {0}
553do_test wal2-6.3.3 {
554  execsql { PRAGMA lock_status }
555} {main exclusive temp closed}
556do_test wal2-6.3.4 {
557  execsql {
558    BEGIN;
559      INSERT INTO t1 VALUES('Groucho');
560  }
561  list [file exists test.db-wal] [file exists test.db-journal]
562} {0 1}
563do_test wal2-6.3.5 {
564  execsql { PRAGMA lock_status }
565} {main exclusive temp closed}
566do_test wal2-6.3.6 {
567  execsql { COMMIT }
568  list [file exists test.db-wal] [file exists test.db-journal]
569} {0 1}
570do_test wal2-6.3.7 {
571  execsql { PRAGMA lock_status }
572} {main exclusive temp closed}
573db close
574
575
576# This test - wal2-6.4.* - uses a single database connection and the
577# [testvfs] instrumentation to test that xShmLock() is being called
578# as expected when a WAL database is used with locking_mode=exclusive.
579#
580do_test wal2-6.4.1 {
581  file delete -force test.db test.db-wal test.db-journal
582  proc tvfs_cb {method args} {
583    set ::shm_file [lindex $args 0]
584    if {$method == "xShmLock"} { lappend ::locks [lindex $args 2] }
585    return "SQLITE_OK"
586  }
587  testvfs tvfs
588  tvfs script tvfs_cb
589  sqlite3 db test.db -vfs tvfs
590} {}
591
592set RECOVERY {
593  {0 1 lock exclusive} {1 7 lock exclusive}
594  {1 7 unlock exclusive} {0 1 unlock exclusive}
595}
596set READMARK0_READ {
597  {3 1 lock shared} {3 1 unlock shared}
598}
599set READMARK0_WRITE {
600  {3 1 lock shared}
601  {0 1 lock exclusive} {3 1 unlock shared}
602  {4 1 lock exclusive} {4 1 unlock exclusive} {4 1 lock shared}
603  {0 1 unlock exclusive} {4 1 unlock shared}
604}
605set READMARK1_SET {
606  {4 1 lock exclusive} {4 1 unlock exclusive}
607}
608set READMARK1_READ {
609  {4 1 lock shared} {4 1 unlock shared}
610}
611
612foreach {tn sql res expected_locks} {
613  2 {
614    PRAGMA journal_mode = WAL;
615    BEGIN;
616      CREATE TABLE t1(x);
617      INSERT INTO t1 VALUES('Leonard');
618      INSERT INTO t1 VALUES('Arthur');
619    COMMIT;
620  } {wal} {
621    $RECOVERY
622    $READMARK0_WRITE
623  }
624
625  3 {
626    # This test should do the READMARK1_SET locking to populate the
627    # aReadMark[1] slot with the current mxFrame value. Followed by
628    # READMARK1_READ to read the database.
629    #
630    SELECT * FROM t1
631  } {Leonard Arthur} {
632    $READMARK1_SET
633    $READMARK1_READ
634  }
635
636  4 {
637    # aReadMark[1] is already set to mxFrame. So just READMARK1_READ
638    # this time, not READMARK1_SET.
639    #
640    SELECT * FROM t1 ORDER BY x
641  } {Arthur Leonard} {
642    $READMARK1_READ
643  }
644
645  5 {
646    PRAGMA locking_mode = exclusive
647  } {exclusive} { }
648
649  6 {
650    INSERT INTO t1 VALUES('Julius Henry');
651    SELECT * FROM t1;
652  } {Leonard Arthur {Julius Henry}} {
653    $READMARK1_READ
654  }
655
656  7 {
657    INSERT INTO t1 VALUES('Karl');
658    SELECT * FROM t1;
659  } {Leonard Arthur {Julius Henry} Karl} { }
660
661  8 {
662    PRAGMA locking_mode = normal
663  } {normal} { }
664
665  9 {
666    SELECT * FROM t1 ORDER BY x
667  } {Arthur {Julius Henry} Karl Leonard} { }
668
669  10 {
670    DELETE FROM t1
671  } {} {
672    $READMARK1_READ
673  }
674
675  11 {
676    SELECT * FROM t1
677  } {} {
678    $READMARK1_SET
679    $READMARK1_READ
680  }
681} {
682
683  set L [list]
684  foreach el [subst $expected_locks] { lappend L $el }
685
686  set S ""
687  foreach sq [split $sql "\n"] {
688    set sq [string trim $sq]
689    if {[string match {#*} $sq]==0} {append S "$sq\n"}
690  }
691
692  set ::locks [list]
693  do_test wal2-6.4.$tn.1 { execsql $S } $res
694  do_test wal2-6.4.$tn.2 { set ::locks  } $L
695}
696
697db close
698tvfs delete
699
700do_test wal2-6.5.1 {
701  sqlite3 db test.db
702  execsql {
703    PRAGMA journal_mode = wal;
704    PRAGMA locking_mode = exclusive;
705    CREATE TABLE t2(a, b);
706    PRAGMA wal_checkpoint;
707    INSERT INTO t2 VALUES('I', 'II');
708    PRAGMA journal_mode;
709  }
710} {wal exclusive wal}
711do_test wal2-6.5.2 {
712  execsql {
713    PRAGMA locking_mode = normal;
714    INSERT INTO t2 VALUES('III', 'IV');
715    PRAGMA locking_mode = exclusive;
716    SELECT * FROM t2;
717  }
718} {normal exclusive I II III IV}
719do_test wal2-6.5.3 {
720  execsql { PRAGMA wal_checkpoint }
721} {}
722db close
723
724proc lock_control {method filename handle spec} {
725  foreach {start n op type} $spec break
726  if {$op == "lock"} { return SQLITE_IOERR }
727  return SQLITE_OK
728}
729do_test wal2-6.6.1 {
730  testvfs T
731  T script lock_control
732  T filter {}
733  sqlite3 db test.db -vfs T
734  execsql { PRAGMA locking_mode = exclusive }
735  execsql { INSERT INTO t2 VALUES('V', 'VI') }
736} {}
737do_test wal2-6.6.2 {
738  execsql { PRAGMA locking_mode = normal }
739  T filter xShmLock
740  execsql { INSERT INTO t2 VALUES('VII', 'VIII') }
741} {}
742do_test wal2-6.6.3 {
743  # At this point the connection should still be in exclusive-mode, even
744  # though it tried to exit exclusive-mode when committing the INSERT
745  # statement above. To exit exclusive mode, SQLite has to take a read-lock
746  # on the WAL file using xShmLock(). Since that call failed, it remains
747  # in exclusive mode.
748  #
749  sqlite3 db2 test.db -vfs T
750  catchsql { SELECT * FROM t2 } db2
751} {1 {database is locked}}
752do_test wal2-6.6.2 {
753  db2 close
754  T filter {}
755  execsql { INSERT INTO t2 VALUES('IX', 'X') }
756} {}
757do_test wal2-6.6.3 {
758  # This time, we have successfully exited exclusive mode. So the second
759  # connection can read the database.
760  sqlite3 db2 test.db -vfs T
761  catchsql { SELECT * FROM t2 } db2
762} {0 {I II III IV V VI VII VIII IX X}}
763
764db close
765db2 close
766T delete
767
768#-------------------------------------------------------------------------
769# Test a theory about the checksum algorithm. Theory was false and this
770# test did not provoke a bug.
771#
772file delete -force test.db test.db-wal test.db-journal
773do_test wal2-7.1.1 {
774  sqlite3 db test.db
775  execsql {
776    PRAGMA page_size = 4096;
777    PRAGMA journal_mode = WAL;
778    CREATE TABLE t1(a, b);
779  }
780  file size test.db
781} {4096}
782do_test wal2-7.1.2 {
783  file copy -force test.db test2.db
784  file copy -force test.db-wal test2.db-wal
785  hexio_write test2.db-wal 48 FF
786} {1}
787do_test wal2-7.1.3 {
788  sqlite3 db2 test2.db
789  execsql { PRAGMA wal_checkpoint } db2
790  execsql { SELECT * FROM sqlite_master } db2
791} {}
792db close
793db2 close
794file delete -force test.db test.db-wal test.db-journal
795do_test wal2-8.1.2 {
796  sqlite3 db test.db
797  execsql {
798    PRAGMA auto_vacuum=OFF;
799    PRAGMA page_size = 1024;
800    PRAGMA journal_mode = WAL;
801    CREATE TABLE t1(x);
802    INSERT INTO t1 VALUES(zeroblob(8188*1020));
803    CREATE TABLE t2(y);
804  }
805  execsql {
806    PRAGMA wal_checkpoint;
807    SELECT rootpage>=8192 FROM sqlite_master WHERE tbl_name = 't2';
808  }
809} {1}
810do_test wal2-8.1.3 {
811  execsql {
812    PRAGMA cache_size = 10;
813    CREATE TABLE t3(z);
814    BEGIN;
815      INSERT INTO t3 VALUES(randomblob(900));
816      INSERT INTO t3 SELECT randomblob(900) FROM t3;
817      INSERT INTO t2 VALUES('hello');
818      INSERT INTO t3 SELECT randomblob(900) FROM t3;
819      INSERT INTO t3 SELECT randomblob(900) FROM t3;
820      INSERT INTO t3 SELECT randomblob(900) FROM t3;
821      INSERT INTO t3 SELECT randomblob(900) FROM t3;
822      INSERT INTO t3 SELECT randomblob(900) FROM t3;
823      INSERT INTO t3 SELECT randomblob(900) FROM t3;
824    ROLLBACK;
825  }
826  execsql {
827    INSERT INTO t2 VALUES('goodbye');
828    INSERT INTO t3 SELECT randomblob(900) FROM t3;
829    INSERT INTO t3 SELECT randomblob(900) FROM t3;
830  }
831} {}
832do_test wal2-8.1.4 {
833  sqlite3 db2 test.db
834  execsql { SELECT * FROM t2 }
835} {goodbye}
836db2 close
837db close
838
839#-------------------------------------------------------------------------
840# Test that even if the checksums for both are valid, if the two copies
841# of the wal-index header in the wal-index do not match, the client
842# runs (or at least tries to run) database recovery.
843#
844#
845proc get_name {method args} { set ::filename [lindex $args 0] ; tvfs filter {} }
846testvfs tvfs
847tvfs script get_name
848tvfs filter xShmOpen
849
850file delete -force test.db test.db-wal test.db-journal
851do_test wal2-9.1 {
852  sqlite3 db test.db -vfs tvfs
853  execsql {
854    PRAGMA journal_mode = WAL;
855    CREATE TABLE x(y);
856    INSERT INTO x VALUES('Barton');
857    INSERT INTO x VALUES('Deakin');
858  }
859
860  # Set $wih(1) to the contents of the wal-index header after
861  # the frames associated with the first two rows in table 'x' have
862  # been inserted. Then insert one more row and set $wih(2)
863  # to the new value of the wal-index header.
864  #
865  # If the $wih(1) is written into the wal-index before running
866  # a read operation, the client will see only the first two rows. If
867  # $wih(2) is written into the wal-index, the client will see
868  # three rows. If an invalid header is written into the wal-index, then
869  # the client will run recovery and see three rows.
870  #
871  set wih(1) [set_tvfs_hdr $::filename]
872  execsql { INSERT INTO x VALUES('Watson') }
873  set wih(2) [set_tvfs_hdr $::filename]
874
875  sqlite3 db2 test.db -vfs tvfs
876  execsql { SELECT * FROM x } db2
877} {Barton Deakin Watson}
878
879foreach {tn hdr1 hdr2 res} [list                                            \
880  3  $wih(1)                $wih(1)                {Barton Deakin}          \
881  4  $wih(1)                $wih(2)                {Barton Deakin Watson}   \
882  5  $wih(2)                $wih(1)                {Barton Deakin Watson}   \
883  6  $wih(2)                $wih(2)                {Barton Deakin Watson}   \
884  7  $wih(1)                $wih(1)                {Barton Deakin}          \
885  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}
886] {
887  do_test wal2-9.$tn {
888    set_tvfs_hdr $::filename $hdr1 $hdr2
889    execsql { SELECT * FROM x } db2
890  } $res
891}
892
893db2 close
894db close
895
896#-------------------------------------------------------------------------
897# This block of tests - wal2-10.* - focus on the libraries response to
898# new versions of the wal or wal-index formats.
899#
900#   wal2-10.1.*: Test that the library refuses to "recover" a new WAL
901#                format.
902#
903#   wal2-10.2.*: Test that the library refuses to read or write a database
904#                if the wal-index version is newer than it understands.
905#
906# At time of writing, the only versions of the wal and wal-index formats
907# that exist are versions 3007000 (corresponding to SQLite version 3.7.0,
908# the first version of SQLite to feature wal mode).
909#
910do_test wal2-10.1.1 {
911  faultsim_delete_and_reopen
912  execsql {
913    PRAGMA journal_mode = WAL;
914    CREATE TABLE t1(a, b);
915    PRAGMA wal_checkpoint;
916    INSERT INTO t1 VALUES(1, 2);
917    INSERT INTO t1 VALUES(3, 4);
918  }
919  faultsim_save_and_close
920} {}
921do_test wal2-10.1.2 {
922  faultsim_restore_and_reopen
923  execsql { SELECT * FROM t1 }
924} {1 2 3 4}
925do_test wal2-10.1.3 {
926  faultsim_restore_and_reopen
927  set hdr [wal_set_walhdr test.db-wal]
928  lindex $hdr 1
929} {3007000}
930do_test wal2-10.1.4 {
931  lset hdr 1 3007001
932  wal_set_walhdr test.db-wal $hdr
933  catchsql { SELECT * FROM t1 }
934} {1 {unable to open database file}}
935
936testvfs tvfs -default 1
937do_test wal2-10.2.1 {
938  faultsim_restore_and_reopen
939  execsql { SELECT * FROM t1 }
940} {1 2 3 4}
941do_test wal2-10.2.2 {
942  set hdr [set_tvfs_hdr $::filename]
943  lindex $hdr 0
944} {3007000}
945breakpoint
946do_test wal2-10.2.3 {
947  lset hdr 0 3007001
948  wal_fix_walindex_cksum hdr
949  set_tvfs_hdr $::filename $hdr
950  catchsql { SELECT * FROM t1 }
951} {1 {unable to open database file}}
952db close
953tvfs delete
954
955#-------------------------------------------------------------------------
956# This block of tests - wal2-11.* - tests that it is not possible to put
957# the library into an infinite loop by presenting it with a corrupt
958# hash table (one that appears to contain a single chain of infinite
959# length).
960#
961#   wal2-11.1.*: While reading the hash-table.
962#
963#   wal2-11.2.*: While writing the hash-table.
964#
965testvfs tvfs -default 1
966do_test wal2-11.0 {
967  faultsim_delete_and_reopen
968  execsql {
969    PRAGMA journal_mode = WAL;
970    CREATE TABLE t1(a, b, c);
971    INSERT INTO t1 VALUES(1, 2, 3);
972    INSERT INTO t1 VALUES(4, 5, 6);
973    INSERT INTO t1 VALUES(7, 8, 9);
974    SELECT * FROM t1;
975  }
976} {wal 1 2 3 4 5 6 7 8 9}
977
978do_test wal2-11.1.1 {
979  sqlite3 db2 test.db
980  execsql { SELECT name FROM sqlite_master } db2
981} {t1}
982
983# Set all zeroed slots in the first hash table to invalid values.
984#
985set blob [string range [tvfs shm $::filename] 0 16383]
986set I [string range [tvfs shm $::filename] 16384 end]
987binary scan $I t* L
988set I [list]
989foreach p $L {
990  lappend I [expr $p ? $p : 400]
991}
992append blob [binary format t* $I]
993tvfs shm $::filename $blob
994do_test wal2-11.2 {
995  catchsql { INSERT INTO t1 VALUES(10, 11, 12) }
996} {1 {database disk image is malformed}}
997
998# Fill up the hash table on the first page of shared memory with 0x55 bytes.
999#
1000set blob [string range [tvfs shm $::filename] 0 16383]
1001append blob [string repeat [binary format c 55] 16384]
1002tvfs shm $::filename $blob
1003do_test wal2-11.3 {
1004  catchsql { SELECT * FROM t1 } db2
1005} {1 {database disk image is malformed}}
1006
1007db close
1008db2 close
1009tvfs delete
1010
1011#-------------------------------------------------------------------------
1012# If a connection is required to create a WAL or SHM file, it creates
1013# the new files with the same file-system permissions as the database
1014# file itself. Test this.
1015#
1016if {$::tcl_platform(platform) == "unix"} {
1017  faultsim_delete_and_reopen
1018  set umask [exec /bin/sh -c umask]
1019
1020  do_test wal2-12.1 {
1021    sqlite3 db test.db
1022    execsql {
1023      CREATE TABLE tx(y, z);
1024      PRAGMA journal_mode = WAL;
1025    }
1026    db close
1027    list [file exists test.db-wal] [file exists test.db-shm]
1028  } {0 0}
1029
1030  foreach {tn permissions} {
1031   1 00644
1032   2 00666
1033   3 00600
1034   4 00755
1035  } {
1036    set effective [format %.5o [expr $permissions & ~$umask]]
1037    do_test wal2-12.2.$tn.1 {
1038      file attributes test.db -permissions $permissions
1039      file attributes test.db -permissions
1040    } $permissions
1041    do_test wal2-12.2.$tn.2 {
1042      list [file exists test.db-wal] [file exists test.db-shm]
1043    } {0 0}
1044    do_test wal2-12.2.$tn.3 {
1045      sqlite3 db test.db
1046      execsql { INSERT INTO tx DEFAULT VALUES }
1047      list [file exists test.db-wal] [file exists test.db-shm]
1048    } {1 1}
1049    do_test wal2-12.2.$tn.4 {
1050      list [file attr test.db-wal -perm] [file attr test.db-shm -perm]
1051    } [list $effective $effective]
1052    do_test wal2-12.2.$tn.5 {
1053      db close
1054      list [file exists test.db-wal] [file exists test.db-shm]
1055    } {0 0}
1056  }
1057}
1058
1059#-------------------------------------------------------------------------
1060# Test the libraries response to discovering that one or more of the
1061# database, wal or shm files cannot be opened, or can only be opened
1062# read-only.
1063#
1064if {$::tcl_platform(platform) == "unix"} {
1065  proc perm {} {
1066    set L [list]
1067    foreach f {test.db test.db-wal test.db-shm} {
1068      if {[file exists $f]} {
1069        lappend L [file attr $f -perm]
1070      } else {
1071        lappend L {}
1072      }
1073    }
1074    set L
1075  }
1076
1077  faultsim_delete_and_reopen
1078  execsql {
1079    PRAGMA journal_mode = WAL;
1080    CREATE TABLE t1(a, b);
1081    PRAGMA wal_checkpoint;
1082    INSERT INTO t1 VALUES('3.14', '2.72');
1083  }
1084  do_test wal2-13.1.1 {
1085    list [file exists test.db-shm] [file exists test.db-wal]
1086  } {1 1}
1087  faultsim_save_and_close
1088
1089  foreach {tn db_perm wal_perm shm_perm can_open can_read can_write} {
1090    2   00644   00644   00644   1   1   1
1091    3   00644   00400   00644   1   1   0
1092    4   00644   00644   00400   1   0   0
1093    5   00400   00644   00644   1   1   0
1094
1095    7   00644   00000   00644   1   0   0
1096    8   00644   00644   00000   1   0   0
1097    9   00000   00644   00644   0   0   0
1098  } {
1099    faultsim_restore
1100    do_test wal2-13.$tn.1 {
1101      file attr test.db     -perm $db_perm
1102      file attr test.db-wal -perm $wal_perm
1103      file attr test.db-shm -perm $shm_perm
1104
1105      set     L [file attr test.db -perm]
1106      lappend L [file attr test.db-wal -perm]
1107      lappend L [file attr test.db-shm -perm]
1108    } [list $db_perm $wal_perm $shm_perm]
1109
1110    # If $can_open is true, then it should be possible to open a database
1111    # handle. Otherwise, if $can_open is 0, attempting to open the db
1112    # handle throws an "unable to open database file" exception.
1113    #
1114    set r(1) {0 ok}
1115    set r(0) {1 {unable to open database file}}
1116    do_test wal2-13.$tn.2 {
1117      list [catch {sqlite3 db test.db ; set {} ok} msg] $msg
1118    } $r($can_open)
1119
1120    if {$can_open} {
1121
1122      # If $can_read is true, then the client should be able to read from
1123      # the database file. If $can_read is false, attempting to read should
1124      # throw the "unable to open database file" exception.
1125      #
1126      set a(0) {1 {unable to open database file}}
1127      set a(1) {0 {3.14 2.72}}
1128      do_test wal2-13.$tn.3 {
1129        catchsql { SELECT * FROM t1 }
1130      } $a($can_read)
1131
1132      # Now try to write to the db file. If the client can read but not
1133      # write, then it should throw the familiar "unable to open db file"
1134      # exception. If it can read but not write, the exception should
1135      # be "attempt to write a read only database".
1136      #
1137      # If the client can read and write, the operation should succeed.
1138      #
1139      set b(0,0) {1 {unable to open database file}}
1140      set b(1,0) {1 {attempt to write a readonly database}}
1141      set b(1,1) {0 {}}
1142      do_test wal2-13.$tn.4 {
1143        catchsql { INSERT INTO t1 DEFAULT VALUES }
1144      } $b($can_read,$can_write)
1145    }
1146    catch { db close }
1147  }
1148}
1149
1150finish_test
1151
1152