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 #***********************************************************************
12 # This test simulates an application crash immediately following a
13 # system call to truncate a file. Specifically, the system call that
14 # truncates the WAL file if "PRAGMA journal_size_limit" is configured.
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
20 ifcapable !wal {finish_test ; return }
21 set testprefix walcrash3
25 tvfs filter {xTruncate xWrite}
26 tvfs script tvfs_callback
27 proc tvfs_callback {args} {}
29 sqlite3 db test.db -vfs tvfs
31 PRAGMA page_size = 1024;
32 PRAGMA journal_mode = WAL;
33 PRAGMA wal_autocheckpoint = 128;
34 PRAGMA journal_size_limit = 16384;
36 CREATE TABLE t1(a BLOB, b BLOB, UNIQUE(a, b));
37 INSERT INTO t1 VALUES(randomblob(10), randomblob(1000));
40 proc tvfs_callback {method file arglist} {
42 foreach f [glob -nocomplain xx_test.*] { forcedelete $f }
43 foreach f [glob -nocomplain test.*] { forcecopy $f "xx_$f" }
46 if {$::state==0 && $method=="xTruncate" && [file tail $file]=="test.db-wal"} {
51 for {set i 2} {$i<1000} {incr i} {
53 # If the WAL file is truncated within the following, within the following
54 # xWrite call the [tvfs_callback] makes a copy of the database and WAL
55 # files set sets $::state to 2. So that the copied files are in the same
56 # state as the real database and WAL files would be if an application crash
57 # occurred immediately following the xTruncate().
60 do_execsql_test 1.$i.1 {
61 INSERT INTO t1 VALUES(randomblob(10), randomblob(1000));
64 # If a copy was made, open it and run the integrity-check.
67 sqlite3 db2 xx_test.db
68 do_test 1.$i.2 { execsql { PRAGMA integrity_check } db2 } "ok"
69 do_test 1.$i.3 { execsql { SELECT count(*) FROM t1 } db2 } [expr $i-1]
76 #--------------------------------------------------------------------------
84 PRAGMA page_size = 512;
85 PRAGMA journal_mode = WAL;
86 PRAGMA wal_autocheckpoint = 128;
87 CREATE TABLE t1(a PRIMARY KEY, b);
88 INSERT INTO t1 VALUES(randomblob(25), randomblob(200));
91 for {set i 0} {$i < 1500} {incr i} {
92 execsql { INSERT INTO t1 VALUES(randomblob(25), randomblob(200)) }
99 set nInitialErr [set_test_counter errors]
100 for {set i 2} {$i<10000 && [set_test_counter errors]==$nInitialErr} {incr i} {
105 crashsql -delay 2 -file test.db-wal -seed $i {
106 SELECT * FROM sqlite_master;
107 PRAGMA synchronous = full;
108 PRAGMA wal_checkpoint;
110 INSERT INTO t1 VALUES(randomblob(26), randomblob(200));
111 INSERT INTO t1 VALUES(randomblob(26), randomblob(200));
112 INSERT INTO t1 VALUES(randomblob(26), randomblob(200));
113 INSERT INTO t1 VALUES(randomblob(26), randomblob(200));
114 INSERT INTO t1 VALUES(randomblob(26), randomblob(200));
115 INSERT INTO t1 VALUES(randomblob(26), randomblob(200));
116 INSERT INTO t1 VALUES(randomblob(26), randomblob(200));
117 INSERT INTO t1 VALUES(randomblob(26), randomblob(200));
120 } {1 {child process exited abnormally}}
124 execsql { PRAGMA integrity_check }