blob: 2b388c40073aac10cdcce4336ebbc265a667663c [file] [log] [blame]
dancd74b612011-04-22 19:37:32 +00001# 2011 April 22
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#
12
13set testdir [file dirname $argv0]
14source $testdir/tester.tcl
15
dan8e980372011-04-23 19:06:26 +000016# Test organization:
17#
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.
dan286ab7c2011-05-06 18:34:54 +000023# 6.*: Test that authorities other than "" and localhost cause errors.
dan19432992011-05-10 18:39:10 +000024# 7.*: Test that a read-write db can be attached to a read-only connection.
dan8e980372011-04-23 19:06:26 +000025#
26
dancd74b612011-04-22 19:37:32 +000027set testprefix uri
28db close
dancd74b612011-04-22 19:37:32 +000029sqlite3_shutdown
30sqlite3_config_uri 1
31
32#-------------------------------------------------------------------------
33# Test that file names are correctly extracted from URIs.
34#
35foreach {tn uri file} {
dan4d7a4462011-05-05 18:53:48 +000036 1 test.db test.db
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
46 11 file file
47 12 http:test.db http:test.db
48 13 file:test.db%00extra test.db
drh812d6082011-05-20 01:50:01 +000049 14 file:testdb%00.db%00extra testdb
dan4d7a4462011-05-05 18:53:48 +000050
51 15 test.db?mork=1#boris test.db?mork=1#boris
52 16 file://localhostPWD/test.db%3Fhello test.db?hello
dancd74b612011-04-22 19:37:32 +000053} {
dan4d7a4462011-05-05 18:53:48 +000054
mistachkin4a41f342012-03-06 03:00:49 +000055
56 ifcapable !curdir { if {$tn==3} break }
57
dan5c35e902016-10-26 12:15:41 +000058 ifcapable uri_00_error {
59 if {[string first %00 $uri]>=0} continue
60 }
61
dan4d7a4462011-05-05 18:53:48 +000062 if {$tcl_platform(platform)=="windows"} {
mistachkin776f5c92012-03-08 20:39:08 +000063 #
64 # NOTE: Due to limits on legal characters for file names imposed by
65 # Windows, we must skip the final two tests here (i.e. the
66 # question mark is illegal in a file name on Windows).
67 #
dan4d7a4462011-05-05 18:53:48 +000068 if {$tn>14} break
dancd74b612011-04-22 19:37:32 +000069
mistachkin776f5c92012-03-08 20:39:08 +000070 #
mistachkin6bd74562015-10-07 03:07:41 +000071 # NOTE: When running on Tcl 8.6 (or higher?) on Windows, a colon within
72 # the file name no longer tries to access an alternate data stream
73 # (ADS) named "test.db" for the "http" file, causing some spurious
74 # failures of this test.
75 #
mistachkin6418ffa2015-10-07 04:20:34 +000076 if {$tn==12 && $::tcl_version>=8.6} continue
mistachkin6bd74562015-10-07 03:07:41 +000077
78 #
mistachkin776f5c92012-03-08 20:39:08 +000079 # NOTE: On Windows, we need to account for the fact that the current
80 # directory does not start with a forward slash.
81 #
82 set uri [string map [list PWD/ /[test_pwd /]] $uri]
dancd74b612011-04-22 19:37:32 +000083 } else {
mistachkin776f5c92012-03-08 20:39:08 +000084 set uri [string map [list PWD/ [test_pwd /]] $uri]
dancd74b612011-04-22 19:37:32 +000085 }
mistachkin4a41f342012-03-06 03:00:49 +000086
drh812d6082011-05-20 01:50:01 +000087 if {[file isdir $file]} {error "$file is a directory"}
dancd74b612011-04-22 19:37:32 +000088 forcedelete $file
89 do_test 1.$tn.1 { file exists $file } 0
90 set DB [sqlite3_open $uri]
91 do_test 1.$tn.2 { file exists $file } 1
92 sqlite3_close $DB
dan3a6d8ae2011-04-23 15:54:54 +000093 forcedelete $file
94
95 do_test 1.$tn.3 { file exists $file } 0
96 sqlite3 db xxx.db
dan4d7a4462011-05-05 18:53:48 +000097 catchsql { ATTACH $uri AS aux }
dan3a6d8ae2011-04-23 15:54:54 +000098 do_test 1.$tn.4 { file exists $file } 1
99 db close
dancd74b612011-04-22 19:37:32 +0000100}
101
dancd74b612011-04-22 19:37:32 +0000102#-------------------------------------------------------------------------
103# Test that URI query parameters are passed through to the VFS layer
104# correctly.
105#
dan4d7a4462011-05-05 18:53:48 +0000106testvfs tvfs2
dancd74b612011-04-22 19:37:32 +0000107testvfs tvfs -default 1
108tvfs filter xOpen
109tvfs script open_method
110proc open_method {method file arglist} {
111 set ::arglist $arglist
112}
dancd74b612011-04-22 19:37:32 +0000113foreach {tn uri kvlist} {
dan5de15372011-04-23 10:12:30 +0000114 1 file:test.db?hello=world {hello world}
115 2 file:test.db?hello&world {hello {} world {}}
116 3 file:test.db?hello=1&world=2&vfs=tvfs {hello 1 world 2 vfs tvfs}
dan4d7a4462011-05-05 18:53:48 +0000117 4 file:test.db?hello=1&world=2&vfs=tvfs2 {}
dan5de15372011-04-23 10:12:30 +0000118 5 file:test.db?%68%65%6C%6C%6F=%77%6F%72%6C%64 {hello world}
drh812d6082011-05-20 01:50:01 +0000119 6 file:testdb%00.db?hello%00extra=world%00ex {hello world}
120 7 file:testdb%00.db?hello%00=world%00 {hello world}
121 8 file:testdb%00.db?=world&xyz=abc {xyz abc}
dan5de15372011-04-23 10:12:30 +0000122 9 file:test.db?%00hello=world&xyz=abc {xyz abc}
123 10 file:test.db?hello=%00world&xyz= {hello {} xyz {}}
124 11 file:test.db?=#ravada {}
125 12 file:test.db?&&&&&&&&hello=world&&&&&&& {hello world}
dan4d7a4462011-05-05 18:53:48 +0000126
dan3a6d8ae2011-04-23 15:54:54 +0000127 13 test.db?&&&&&&&&hello=world&&&&&&& {}
128 14 http:test.db?hello&world {}
dancd74b612011-04-22 19:37:32 +0000129} {
dan4d7a4462011-05-05 18:53:48 +0000130
dan5c35e902016-10-26 12:15:41 +0000131 ifcapable uri_00_error {
132 if {[string first %00 $uri]>=0} continue
133 }
134
dan4d7a4462011-05-05 18:53:48 +0000135 if {$tcl_platform(platform) == "windows" && $tn>12} {
136 continue
137 }
138
dancd74b612011-04-22 19:37:32 +0000139 set ::arglist ""
140 set DB [sqlite3_open $uri]
dan3a6d8ae2011-04-23 15:54:54 +0000141 do_test 2.$tn.1 { set ::arglist } $kvlist
dancd74b612011-04-22 19:37:32 +0000142 sqlite3_close $DB
dan3a6d8ae2011-04-23 15:54:54 +0000143
144 sqlite3 db xxx.db
145 set ::arglist ""
146 execsql { ATTACH $uri AS aux }
147 do_test 2.$tn.2 { set ::arglist } $kvlist
148 db close
dancd74b612011-04-22 19:37:32 +0000149}
dan5de15372011-04-23 10:12:30 +0000150tvfs delete
dan4d7a4462011-05-05 18:53:48 +0000151tvfs2 delete
dan5de15372011-04-23 10:12:30 +0000152
153#-------------------------------------------------------------------------
154# Test that specifying a non-existent VFS raises an error.
155#
156do_test 3.1 {
157 list [catch { sqlite3 db "file:test.db?vfs=nosuchvfs" } msg] $msg
158} {1 {no such vfs: nosuchvfs}}
dancd74b612011-04-22 19:37:32 +0000159
dan3a6d8ae2011-04-23 15:54:54 +0000160#-------------------------------------------------------------------------
dan8e980372011-04-23 19:06:26 +0000161# Test some of the other options (other than "vfs").
162#
dan78e9dd22011-05-03 10:22:32 +0000163foreach {tn mode create_ok write_ok readonly_ok} {
164 1 ro 0 0 1
165 2 rw 0 1 0
166 3 rwc 1 1 0
dan3a6d8ae2011-04-23 15:54:54 +0000167} {
dan78e9dd22011-05-03 10:22:32 +0000168 catch { db close }
169 forcedelete test.db
dan3a6d8ae2011-04-23 15:54:54 +0000170
dan78e9dd22011-05-03 10:22:32 +0000171 set A(1) {0 {}}
172 set A(0) {1 {unable to open database file}}
173 do_test 4.1.$tn.1 {
174 list [catch {sqlite3 db "file:test.db?mode=$mode"} msg] $msg
175 } $A($create_ok)
176
177 catch { db close }
178 forcedelete test.db
179 sqlite3 db test.db
180 db eval { CREATE TABLE t1(a, b) }
dan3a6d8ae2011-04-23 15:54:54 +0000181 db close
dan78e9dd22011-05-03 10:22:32 +0000182
183 set A(1) {0 {}}
184 set A(0) {1 {attempt to write a readonly database}}
185 do_test 4.1.$tn.2 {
186 sqlite3 db "file:test.db?mode=$mode"
187 catchsql { INSERT INTO t1 VALUES(1, 2) }
188 } $A($write_ok)
189
190 set A(1) {0 {}}
daneaadd592011-05-10 17:43:28 +0000191 set A(0) [list 1 "access mode not allowed: $mode"]
dan78e9dd22011-05-03 10:22:32 +0000192 do_test 4.1.$tn.3 {
193 list [catch {sqlite3 db "file:test.db?mode=$mode" -readonly 1} msg] $msg
194 } $A($readonly_ok)
dan3a6d8ae2011-04-23 15:54:54 +0000195}
196
drh9c5e1e42022-08-15 12:26:26 +0000197ifcapable shared_cache {
dan78e9dd22011-05-03 10:22:32 +0000198set orig [sqlite3_enable_shared_cache]
199foreach {tn options sc_default is_shared} {
200 1 "" 1 1
201 2 "cache=private" 1 0
202 3 "cache=shared" 1 1
203 4 "" 0 0
204 5 "cache=private" 0 0
205 6 "cache=shared" 0 1
206} {
207 catch { db close }
208 forcedelete test.db
209
210 sqlite3_enable_shared_cache 1
211 sqlite3 db2 test.db
212 db2 eval {CREATE TABLE t1(a, b)}
213
214 sqlite3_enable_shared_cache $sc_default
215 sqlite3 db "file:test.db?$options"
216 db eval {SELECT * FROM t1}
217
218 set A(1) {1 {database table is locked: t1}}
219 set A(0) {0 {}}
220 do_test 4.2.$tn {
221 db2 eval {BEGIN; INSERT INTO t1 VALUES(1, 2);}
222 catchsql { SELECT * FROM t1 }
223 } $A($is_shared)
224
225 db2 close
226}
drh9c5e1e42022-08-15 12:26:26 +0000227} ;# end ifcapable shared_cache
dan78e9dd22011-05-03 10:22:32 +0000228
229do_test 4.3.1 {
230 list [catch {sqlite3 db "file:test.db?mode=rc"} msg] $msg
231} {1 {no such access mode: rc}}
232do_test 4.3.2 {
233 list [catch {sqlite3 db "file:test.db?cache=public"} msg] $msg
234} {1 {no such cache mode: public}}
235
dan8e980372011-04-23 19:06:26 +0000236#-------------------------------------------------------------------------
237# Test that things work if an ATTACHed database uses a different VFS than
238# the main database. The important point is that for all operations
239# involving the ATTACHed database, the correct versions of the following
240# VFS are used for all operations involving the attached database.
241#
242# xOpen
243# xDelete
244# xAccess
245# xFullPathname
246#
247
248# This block of code creates two VFS - "tvfs1" and "tvfs2". Each time one
249# of the above methods is called using "tvfs1", global variable ::T1(X) is
250# set, where X is the file-name the method is called on. Calls to the above
251# methods using "tvfs2" set entries in the global T2 array.
252#
dan52091322011-09-24 05:55:36 +0000253ifcapable wal {
254 testvfs tvfs1
255 tvfs1 filter {xOpen xDelete xAccess xFullPathname}
256 tvfs1 script tvfs1_callback
257 proc tvfs1_callback {method filename args} {
258 set ::T1([file tail $filename]) 1
mistachkin88794812014-03-07 02:29:56 +0000259 return SQLITE_OK
dan8e980372011-04-23 19:06:26 +0000260 }
dan52091322011-09-24 05:55:36 +0000261 testvfs tvfs2
262 tvfs2 filter {xOpen xDelete xAccess xFullPathname}
263 tvfs2 script tvfs2_callback
264 proc tvfs2_callback {method filename args} {
265 set ::T2([file tail $filename]) 1
mistachkin88794812014-03-07 02:29:56 +0000266 return SQLITE_OK
dan52091322011-09-24 05:55:36 +0000267 }
268
269 catch {db close}
270 eval forcedelete [glob test.db*]
271 do_test 5.1.1 {
272 sqlite3 db file:test.db1?vfs=tvfs1
273 execsql {
274 ATTACH 'file:test.db2?vfs=tvfs2' AS aux;
275 PRAGMA main.journal_mode = PERSIST;
276 PRAGMA aux.journal_mode = PERSIST;
277 CREATE TABLE t1(a, b);
278 CREATE TABLE aux.t2(a, b);
279 PRAGMA main.journal_mode = WAL;
280 PRAGMA aux.journal_mode = WAL;
281 INSERT INTO t1 VALUES('x', 'y');
282 INSERT INTO t2 VALUES('x', 'y');
283 }
284 lsort [array names ::T1]
285 } {test.db1 test.db1-journal test.db1-wal}
286
287 do_test 5.1.2 {
288 lsort [array names ::T2]
289 } {test.db2 test.db2-journal test.db2-wal}
290 db close
291
292 tvfs1 delete
293 tvfs2 delete
294}
dan8e980372011-04-23 19:06:26 +0000295
dan3b18a9a2011-05-03 11:53:20 +0000296#-------------------------------------------------------------------------
297# Check that only "" and "localhost" are acceptable as authorities.
298#
299catch {db close}
300foreach {tn uri res} {
301 1 "file://localhost/PWD/test.db" {not an error}
302 2 "file:///PWD/test.db" {not an error}
303 3 "file:/PWD/test.db" {not an error}
304 4 "file://l%6Fcalhost/PWD/test.db" {invalid uri authority: l%6Fcalhost}
305 5 "file://lbcalhost/PWD/test.db" {invalid uri authority: lbcalhost}
306 6 "file://x/PWD/test.db" {invalid uri authority: x}
307} {
dan4d7a4462011-05-05 18:53:48 +0000308
309 if {$tcl_platform(platform)=="windows"} {
mistachkinf8a78462012-03-08 20:00:36 +0000310 set uri [string map [list PWD [string range [get_pwd] 3 end]] $uri]
dan4d7a4462011-05-05 18:53:48 +0000311 } else {
mistachkinf8a78462012-03-08 20:00:36 +0000312 set uri [string map [list PWD [string range [get_pwd] 1 end]] $uri]
dan4d7a4462011-05-05 18:53:48 +0000313 }
314
dan3b18a9a2011-05-03 11:53:20 +0000315 do_test 6.$tn {
316 set DB [sqlite3_open $uri]
317 sqlite3_errmsg $DB
318 } $res
319 catch { sqlite3_close $DB }
320}
321
dan19432992011-05-10 18:39:10 +0000322forcedelete test.db test.db2
323do_test 7.1 {
324 sqlite3 db test.db
325 execsql {
326 CREATE TABLE t1(a, b);
327 INSERT INTO t1 VALUES(1, 2);
328 ATTACH 'test.db2' AS aux;
329 CREATE TABLE aux.t2(a, b);
330 INSERT INTO t1 VALUES('a', 'b');
331 }
332 db close
333} {}
334do_test 7.2 {
335 sqlite3 db file:test.db?mode=ro
336 execsql { ATTACH 'file:test.db2?mode=rw' AS aux }
337} {}
338do_execsql_test 7.3 {
339 INSERT INTO t2 VALUES('c', 'd')
340} {}
341do_catchsql_test 7.4 {
342 INSERT INTO t1 VALUES(3, 4)
343} {1 {attempt to write a readonly database}}
dancd74b612011-04-22 19:37:32 +0000344
dan19432992011-05-10 18:39:10 +0000345finish_test