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]
106 code1 { sqlite3 db file:test.db?readonly_shm=1 }
107 list [catch { sql1 { SELECT * FROM t1 } } msg] $msg
108 } {0 {a b c d e f g h i j}}
112 file attributes test.db-shm -permissions rw-r--r--
113 hexio_write test.db-shm 0 01020304
114 file attributes test.db-shm -permissions r--r--r--
115 code1 { sqlite3 db file:test.db?readonly_shm=1 }
116 csql1 { SELECT * FROM t1 }
117 } {0 {a b c d e f g h i j}}
119 code1 { sqlite3_extended_errcode db }
123 file attributes test.db-shm -permissions rw-r--r--
124 code2 { sqlite3 db2 test.db }
125 sql2 "SELECT * FROM t1"
126 } {a b c d e f g h i j}
127 file attributes test.db-shm -permissions r--r--r--
128 do_test 1.2.6 { sql1 "SELECT * FROM t1" } {a b c d e f g h i j}
132 PRAGMA wal_checkpoint;
133 INSERT INTO t1 VALUES('k', 'l');
137 do_test 1.2.8 { sql1 "SELECT * FROM t1" } {a b c d e f g h i j k l}
139 # Now check that if the readonly_shm option is not supplied, or if it
140 # is set to zero, it is not possible to connect to the database without
141 # read-write access to the shm.
143 # UPDATE: os_unix.c now opens the *-shm file in readonly mode
148 code1 { sqlite3 db test.db }
149 csql1 { SELECT * FROM t1 }
150 } {0 {a b c d e f g h i j k l}}
152 # Also test that if the -shm file can be opened for read/write access,
153 # it is not if readonly_shm=1 is present in the URI.
157 file exists test.db-shm
160 code1 { sqlite3 db file:test.db?readonly_shm=1 }
161 csql1 { SELECT * FROM sqlite_master }
162 } {1 {unable to open database file}}
165 close [open test.db-shm w]
166 file attributes test.db-shm -permissions r--r--r--
167 code1 { sqlite3 db file:test.db?readonly_shm=1 }
168 csql1 { SELECT * FROM t1 }
169 } {0 {a b c d e f g h i j k l}}
171 code1 { sqlite3_extended_errcode db }
174 #-----------------------------------------------------------------------
175 # Test cases 1.4.* check that checkpoints and log wraps don't prevent
176 # read-only connections from reading the database.
179 forcedelete test.db-shm
180 file exists test.db-shm
183 # Open one read-only and one read-write connection. Write some data
184 # and then run a checkpoint using the read-write connection. Then
185 # check the read-only connection can still read.
187 code1 { sqlite3 db file:test.db?readonly_shm=1 }
188 code2 { sqlite3 db2 test.db }
190 INSERT INTO t1 VALUES(1, 2);
191 INSERT INTO t1 VALUES(3, 4);
192 INSERT INTO t1 VALUES(5, 6);
193 PRAGMA wal_checkpoint;
197 csql1 { SELECT * FROM t1 }
198 } {0 {a b c d e f g h i j k l 1 2 3 4 5 6}}
200 # Using the read-write connection, open a transaction and write lots
201 # of data - causing a cache spill and a log wrap. Then check that the
202 # read-only connection can still read the database.
205 PRAGMA cache_size = 10;
207 CREATE TABLE t2(x, y);
208 INSERT INTO t2 VALUES('abc', 'xyz');
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;
213 INSERT INTO t2 SELECT x||y, y||x FROM t2;
214 INSERT INTO t2 SELECT x||y, y||x FROM t2;
215 INSERT INTO t2 SELECT x||y, y||x FROM t2;
216 INSERT INTO t2 SELECT x||y, y||x FROM t2;
217 INSERT INTO t2 SELECT x||y, y||x FROM t2;
219 file size test.db-wal
220 } [expr {[nonzero_reserved_bytes]?148848:147800}]
222 csql1 { SELECT * FROM t1 }
223 } {0 {a b c d e f g h i j k l 1 2 3 4 5 6}}
226 csql1 { SELECT count(*) FROM t2 }
236 #-----------------------------------------------------------------------
237 # Test cases 2.* check that a read-only connection may read the
238 # database file while a checkpoint operation is ongoing.
240 do_multiclient_test tn {
242 # Close all connections and delete the database.
250 # Do not run tests with the connections in the same process.
254 foreach c {code1 code2 code3} {
261 proc tv_hook {x file args} {
262 if {[file tail $file]=="test.db-wal"} {
264 code2 { sqlite3 db2 file:test.db?readonly_shm=1 }
265 csql2 { SELECT count(*) FROM t2 }
274 testvfs tv -default 1 -fullshm 1
277 code1 { sqlite3 db test.db }
279 PRAGMA auto_vacuum = 0;
280 PRAGMA journal_mode = WAL;
282 CREATE TABLE t2(x, y);
283 INSERT INTO t2 VALUES('abc', 'xyz');
284 INSERT INTO t2 SELECT x||y, y||x FROM t2;
285 INSERT INTO t2 SELECT x||y, y||x FROM t2;
291 set res [csql1 { PRAGMA wal_checkpoint }]
292 do_test 2.1.4 { set res } {0 {0 2 2}}