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 #***********************************************************************
13 set testdir [file dirname $argv0]
14 source $testdir/tester.tcl
18 # 1.*: That file names are correctly extracted from URIs.
19 # 2.*: That URI options (query parameters) are correctly extracted from URIs.
20 # 3.*: That specifying an unknown VFS causes an error.
21 # 4.*: Tests for specifying other options (other than "vfs").
22 # 5.*: Test using a different VFS with an attached database.
23 # 6.*: Test that authorities other than "" and localhost cause errors.
24 # 7.*: Test that a read-write db can be attached to a read-only connection.
32 #-------------------------------------------------------------------------
33 # Test that file names are correctly extracted from URIs.
35 foreach {tn uri file} {
37 2 file:test.db test.db
38 3 file://PWD/test.db test.db
39 4 file:PWD/test.db test.db
40 5 file:test.db?mork=1 test.db
41 6 file:test.db?mork=1&tonglor=2 test.db
42 7 file:test.db?mork=1#boris test.db
43 8 file:test.db#boris test.db
44 9 test.db#boris test.db#boris
45 10 file:test%2Edb test.db
47 12 http:test.db http:test.db
48 13 file:test.db%00extra test.db
49 14 file:testdb%00.db%00extra testdb
51 15 test.db?mork=1#boris test.db?mork=1#boris
52 16 file://localhostPWD/test.db%3Fhello test.db?hello
56 ifcapable !curdir { if {$tn==3} break }
58 if {$tcl_platform(platform)=="windows"} {
60 # NOTE: Due to limits on legal characters for file names imposed by
61 # Windows, we must skip the final two tests here (i.e. the
62 # question mark is illegal in a file name on Windows).
67 # NOTE: On Windows, we need to account for the fact that the current
68 # directory does not start with a forward slash.
70 set uri [string map [list PWD/ /[test_pwd /]] $uri]
72 set uri [string map [list PWD/ [test_pwd /]] $uri]
75 if {[file isdir $file]} {error "$file is a directory"}
77 do_test 1.$tn.1 { file exists $file } 0
78 set DB [sqlite3_open $uri]
79 do_test 1.$tn.2 { file exists $file } 1
83 do_test 1.$tn.3 { file exists $file } 0
85 catchsql { ATTACH $uri AS aux }
86 do_test 1.$tn.4 { file exists $file } 1
90 #-------------------------------------------------------------------------
91 # Test that URI query parameters are passed through to the VFS layer
95 testvfs tvfs -default 1
97 tvfs script open_method
98 proc open_method {method file arglist} {
99 set ::arglist $arglist
101 foreach {tn uri kvlist} {
102 1 file:test.db?hello=world {hello world}
103 2 file:test.db?hello&world {hello {} world {}}
104 3 file:test.db?hello=1&world=2&vfs=tvfs {hello 1 world 2 vfs tvfs}
105 4 file:test.db?hello=1&world=2&vfs=tvfs2 {}
106 5 file:test.db?%68%65%6C%6C%6F=%77%6F%72%6C%64 {hello world}
107 6 file:testdb%00.db?hello%00extra=world%00ex {hello world}
108 7 file:testdb%00.db?hello%00=world%00 {hello world}
109 8 file:testdb%00.db?=world&xyz=abc {xyz abc}
110 9 file:test.db?%00hello=world&xyz=abc {xyz abc}
111 10 file:test.db?hello=%00world&xyz= {hello {} xyz {}}
112 11 file:test.db?=#ravada {}
113 12 file:test.db?&&&&&&&&hello=world&&&&&&& {hello world}
115 13 test.db?&&&&&&&&hello=world&&&&&&& {}
116 14 http:test.db?hello&world {}
119 if {$tcl_platform(platform) == "windows" && $tn>12} {
124 set DB [sqlite3_open $uri]
125 do_test 2.$tn.1 { set ::arglist } $kvlist
130 execsql { ATTACH $uri AS aux }
131 do_test 2.$tn.2 { set ::arglist } $kvlist
137 #-------------------------------------------------------------------------
138 # Test that specifying a non-existent VFS raises an error.
141 list [catch { sqlite3 db "file:test.db?vfs=nosuchvfs" } msg] $msg
142 } {1 {no such vfs: nosuchvfs}}
144 #-------------------------------------------------------------------------
145 # Test some of the other options (other than "vfs").
147 foreach {tn mode create_ok write_ok readonly_ok} {
156 set A(0) {1 {unable to open database file}}
158 list [catch {sqlite3 db "file:test.db?mode=$mode"} msg] $msg
164 db eval { CREATE TABLE t1(a, b) }
168 set A(0) {1 {attempt to write a readonly database}}
170 sqlite3 db "file:test.db?mode=$mode"
171 catchsql { INSERT INTO t1 VALUES(1, 2) }
175 set A(0) [list 1 "access mode not allowed: $mode"]
177 list [catch {sqlite3 db "file:test.db?mode=$mode" -readonly 1} msg] $msg
181 set orig [sqlite3_enable_shared_cache]
182 foreach {tn options sc_default is_shared} {
184 2 "cache=private" 1 0
187 5 "cache=private" 0 0
193 sqlite3_enable_shared_cache 1
195 db2 eval {CREATE TABLE t1(a, b)}
197 sqlite3_enable_shared_cache $sc_default
198 sqlite3 db "file:test.db?$options"
199 db eval {SELECT * FROM t1}
201 set A(1) {1 {database table is locked: t1}}
204 db2 eval {BEGIN; INSERT INTO t1 VALUES(1, 2);}
205 catchsql { SELECT * FROM t1 }
212 list [catch {sqlite3 db "file:test.db?mode=rc"} msg] $msg
213 } {1 {no such access mode: rc}}
215 list [catch {sqlite3 db "file:test.db?cache=public"} msg] $msg
216 } {1 {no such cache mode: public}}
218 #-------------------------------------------------------------------------
219 # Test that things work if an ATTACHed database uses a different VFS than
220 # the main database. The important point is that for all operations
221 # involving the ATTACHed database, the correct versions of the following
222 # VFS are used for all operations involving the attached database.
230 # This block of code creates two VFS - "tvfs1" and "tvfs2". Each time one
231 # of the above methods is called using "tvfs1", global variable ::T1(X) is
232 # set, where X is the file-name the method is called on. Calls to the above
233 # methods using "tvfs2" set entries in the global T2 array.
237 tvfs1 filter {xOpen xDelete xAccess xFullPathname}
238 tvfs1 script tvfs1_callback
239 proc tvfs1_callback {method filename args} {
240 set ::T1([file tail $filename]) 1
244 tvfs2 filter {xOpen xDelete xAccess xFullPathname}
245 tvfs2 script tvfs2_callback
246 proc tvfs2_callback {method filename args} {
247 set ::T2([file tail $filename]) 1
252 eval forcedelete [glob test.db*]
254 sqlite3 db file:test.db1?vfs=tvfs1
256 ATTACH 'file:test.db2?vfs=tvfs2' AS aux;
257 PRAGMA main.journal_mode = PERSIST;
258 PRAGMA aux.journal_mode = PERSIST;
259 CREATE TABLE t1(a, b);
260 CREATE TABLE aux.t2(a, b);
261 PRAGMA main.journal_mode = WAL;
262 PRAGMA aux.journal_mode = WAL;
263 INSERT INTO t1 VALUES('x', 'y');
264 INSERT INTO t2 VALUES('x', 'y');
266 lsort [array names ::T1]
267 } {test.db1 test.db1-journal test.db1-wal}
270 lsort [array names ::T2]
271 } {test.db2 test.db2-journal test.db2-wal}
278 #-------------------------------------------------------------------------
279 # Check that only "" and "localhost" are acceptable as authorities.
282 foreach {tn uri res} {
283 1 "file://localhost/PWD/test.db" {not an error}
284 2 "file:///PWD/test.db" {not an error}
285 3 "file:/PWD/test.db" {not an error}
286 4 "file://l%6Fcalhost/PWD/test.db" {invalid uri authority: l%6Fcalhost}
287 5 "file://lbcalhost/PWD/test.db" {invalid uri authority: lbcalhost}
288 6 "file://x/PWD/test.db" {invalid uri authority: x}
291 if {$tcl_platform(platform)=="windows"} {
292 set uri [string map [list PWD [string range [get_pwd] 3 end]] $uri]
294 set uri [string map [list PWD [string range [get_pwd] 1 end]] $uri]
298 set DB [sqlite3_open $uri]
301 catch { sqlite3_close $DB }
304 forcedelete test.db test.db2
308 CREATE TABLE t1(a, b);
309 INSERT INTO t1 VALUES(1, 2);
310 ATTACH 'test.db2' AS aux;
311 CREATE TABLE aux.t2(a, b);
312 INSERT INTO t1 VALUES('a', 'b');
317 sqlite3 db file:test.db?mode=ro
318 execsql { ATTACH 'file:test.db2?mode=rw' AS aux }
320 do_execsql_test 7.3 {
321 INSERT INTO t2 VALUES('c', 'd')
323 do_catchsql_test 7.4 {
324 INSERT INTO t1 VALUES(3, 4)
325 } {1 {attempt to write a readonly database}}