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 file contains tests for using WAL databases in read-only mode.
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
17 source $testdir/lock_common.tcl
18 set ::testprefix walro
20 # These tests are only going to work on unix.
22 if {$::tcl_platform(platform) != "unix"} {
27 # And only if the build is WAL-capable.
34 do_multiclient_test tn {
36 # Close all connections and delete the database.
44 # Do not run tests with the connections in the same process.
48 foreach c {code1 code2 code3} {
58 code2 { sqlite3 db2 test.db }
60 PRAGMA auto_vacuum = 0;
61 PRAGMA journal_mode = WAL;
62 CREATE TABLE t1(x, y);
63 INSERT INTO t1 VALUES('a', 'b');
65 file exists test.db-shm
69 file attributes test.db-shm -permissions r--r--r--
70 code1 { sqlite3 db file:test.db?readonly_shm=1 }
73 do_test 1.1.3 { sql1 "SELECT * FROM t1" } {a b}
74 do_test 1.1.4 { sql2 "INSERT INTO t1 VALUES('c', 'd')" } {}
75 do_test 1.1.5 { sql1 "SELECT * FROM t1" } {a b c d}
77 # Check that the read-only connection cannot write or checkpoint the db.
80 csql1 "INSERT INTO t1 VALUES('e', 'f')"
81 } {1 {attempt to write a readonly database}}
83 csql1 "PRAGMA wal_checkpoint"
84 } {1 {attempt to write a readonly database}}
86 do_test 1.1.9 { sql2 "INSERT INTO t1 VALUES('e', 'f')" } {}
87 do_test 1.1.10 { sql1 "SELECT * FROM t1" } {a b c d e f}
91 INSERT INTO t1 VALUES('g', 'h');
92 PRAGMA wal_checkpoint;
96 do_test 1.1.12 { sql1 "SELECT * FROM t1" } {a b c d e f g h}
97 do_test 1.1.13 { sql2 "INSERT INTO t1 VALUES('i', 'j')" } {}
102 list [file exists test.db-wal] [file exists test.db-shm]
105 code1 { sqlite3 db file:test.db?readonly_shm=1 }
106 sql1 { SELECT * FROM t1 }
107 } {a b c d e f g h i j}
111 file attributes test.db-shm -permissions rw-r--r--
112 hexio_write test.db-shm 0 01020304
113 file attributes test.db-shm -permissions r--r--r--
114 code1 { sqlite3 db file:test.db?readonly_shm=1 }
115 csql1 { SELECT * FROM t1 }
116 } {1 {attempt to write a readonly database}}
118 code1 { sqlite3_extended_errcode db }
119 } {SQLITE_READONLY_RECOVERY}
122 file attributes test.db-shm -permissions rw-r--r--
123 code2 { sqlite3 db2 test.db }
124 sql2 "SELECT * FROM t1"
125 } {a b c d e f g h i j}
126 file attributes test.db-shm -permissions r--r--r--
127 do_test 1.2.6 { sql1 "SELECT * FROM t1" } {a b c d e f g h i j}
131 PRAGMA wal_checkpoint;
132 INSERT INTO t1 VALUES('k', 'l');
136 do_test 1.2.8 { sql1 "SELECT * FROM t1" } {a b c d e f g h i j k l}
138 # Now check that if the readonly_shm option is not supplied, or if it
139 # is set to zero, it is not possible to connect to the database without
140 # read-write access to the shm.
143 code1 { sqlite3 db test.db }
144 csql1 { SELECT * FROM t1 }
145 } {1 {unable to open database file}}
147 # Also test that if the -shm file can be opened for read/write access,
148 # it is not if readonly_shm=1 is present in the URI.
152 file exists test.db-shm
155 code1 { sqlite3 db file:test.db?readonly_shm=1 }
156 csql1 { SELECT * FROM sqlite_master }
157 } {1 {unable to open database file}}
160 close [open test.db-shm w]
161 file attributes test.db-shm -permissions r--r--r--
162 code1 { sqlite3 db file:test.db?readonly_shm=1 }
163 csql1 { SELECT * FROM t1 }
164 } {1 {attempt to write a readonly database}}
166 code1 { sqlite3_extended_errcode db }
167 } {SQLITE_READONLY_RECOVERY}
169 #-----------------------------------------------------------------------
170 # Test cases 1.4.* check that checkpoints and log wraps don't prevent
171 # read-only connections from reading the database.
174 forcedelete test.db-shm
175 file exists test.db-shm
178 # Open one read-only and one read-write connection. Write some data
179 # and then run a checkpoint using the read-write connection. Then
180 # check the read-only connection can still read.
182 code1 { sqlite3 db file:test.db?readonly_shm=1 }
183 code2 { sqlite3 db2 test.db }
185 INSERT INTO t1 VALUES(1, 2);
186 INSERT INTO t1 VALUES(3, 4);
187 INSERT INTO t1 VALUES(5, 6);
188 PRAGMA wal_checkpoint;
192 csql1 { SELECT * FROM t1 }
193 } {0 {a b c d e f g h i j k l 1 2 3 4 5 6}}
195 # Using the read-write connection, open a transaction and write lots
196 # of data - causing a cache spill and a log wrap. Then check that the
197 # read-only connection can still read the database.
200 PRAGMA cache_size = 10;
202 CREATE TABLE t2(x, y);
203 INSERT INTO t2 VALUES('abc', 'xyz');
204 INSERT INTO t2 SELECT x||y, y||x FROM t2;
205 INSERT INTO t2 SELECT x||y, y||x FROM t2;
206 INSERT INTO t2 SELECT x||y, y||x FROM t2;
207 INSERT INTO t2 SELECT x||y, y||x FROM t2;
208 INSERT INTO t2 SELECT x||y, y||x FROM t2;
209 INSERT INTO t2 SELECT x||y, y||x FROM t2;
210 INSERT INTO t2 SELECT x||y, y||x FROM t2;
211 INSERT INTO t2 SELECT x||y, y||x FROM t2;
212 INSERT INTO t2 SELECT x||y, y||x FROM t2;
214 file size test.db-wal
217 csql1 { SELECT * FROM t1 }
218 } {0 {a b c d e f g h i j k l 1 2 3 4 5 6}}
221 csql1 { SELECT count(*) FROM t2 }
231 #-----------------------------------------------------------------------
232 # Test cases 2.* check that a read-only connection may read the
233 # database file while a checkpoint operation is ongoing.
235 do_multiclient_test tn {
237 # Close all connections and delete the database.
245 # Do not run tests with the connections in the same process.
249 foreach c {code1 code2 code3} {
256 proc tv_hook {x file args} {
257 if {[file tail $file]=="test.db-wal"} {
259 code2 { sqlite3 db2 file:test.db?readonly_shm=1 }
260 csql2 { SELECT count(*) FROM t2 }
269 testvfs tv -default 1 -fullshm 1
272 code1 { sqlite3 db test.db }
274 PRAGMA auto_vacuum = 0;
275 PRAGMA journal_mode = WAL;
277 CREATE TABLE t2(x, y);
278 INSERT INTO t2 VALUES('abc', 'xyz');
279 INSERT INTO t2 SELECT x||y, y||x FROM t2;
280 INSERT INTO t2 SELECT x||y, y||x FROM t2;
286 set res [csql1 { PRAGMA wal_checkpoint }]
287 do_test 2.1.4 { set res } {0 {0 2 2}}