blob: a0a88d22634119432a1bf7dcce2640d88f286865 [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
dan4d7a4462011-05-05 18:53:48 +000058 if {$tcl_platform(platform)=="windows"} {
mistachkin776f5c92012-03-08 20:39:08 +000059 #
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).
63 #
dan4d7a4462011-05-05 18:53:48 +000064 if {$tn>14} break
dancd74b612011-04-22 19:37:32 +000065
mistachkin776f5c92012-03-08 20:39:08 +000066 #
mistachkin6bd74562015-10-07 03:07:41 +000067 # NOTE: When running on Tcl 8.6 (or higher?) on Windows, a colon within
68 # the file name no longer tries to access an alternate data stream
69 # (ADS) named "test.db" for the "http" file, causing some spurious
70 # failures of this test.
71 #
mistachkin6418ffa2015-10-07 04:20:34 +000072 if {$tn==12 && $::tcl_version>=8.6} continue
mistachkin6bd74562015-10-07 03:07:41 +000073
74 #
mistachkin776f5c92012-03-08 20:39:08 +000075 # NOTE: On Windows, we need to account for the fact that the current
76 # directory does not start with a forward slash.
77 #
78 set uri [string map [list PWD/ /[test_pwd /]] $uri]
dancd74b612011-04-22 19:37:32 +000079 } else {
mistachkin776f5c92012-03-08 20:39:08 +000080 set uri [string map [list PWD/ [test_pwd /]] $uri]
dancd74b612011-04-22 19:37:32 +000081 }
mistachkin4a41f342012-03-06 03:00:49 +000082
drh812d6082011-05-20 01:50:01 +000083 if {[file isdir $file]} {error "$file is a directory"}
dancd74b612011-04-22 19:37:32 +000084 forcedelete $file
85 do_test 1.$tn.1 { file exists $file } 0
86 set DB [sqlite3_open $uri]
87 do_test 1.$tn.2 { file exists $file } 1
88 sqlite3_close $DB
dan3a6d8ae2011-04-23 15:54:54 +000089 forcedelete $file
90
91 do_test 1.$tn.3 { file exists $file } 0
92 sqlite3 db xxx.db
dan4d7a4462011-05-05 18:53:48 +000093 catchsql { ATTACH $uri AS aux }
dan3a6d8ae2011-04-23 15:54:54 +000094 do_test 1.$tn.4 { file exists $file } 1
95 db close
dancd74b612011-04-22 19:37:32 +000096}
97
dancd74b612011-04-22 19:37:32 +000098#-------------------------------------------------------------------------
99# Test that URI query parameters are passed through to the VFS layer
100# correctly.
101#
dan4d7a4462011-05-05 18:53:48 +0000102testvfs tvfs2
dancd74b612011-04-22 19:37:32 +0000103testvfs tvfs -default 1
104tvfs filter xOpen
105tvfs script open_method
106proc open_method {method file arglist} {
107 set ::arglist $arglist
108}
dancd74b612011-04-22 19:37:32 +0000109foreach {tn uri kvlist} {
dan5de15372011-04-23 10:12:30 +0000110 1 file:test.db?hello=world {hello world}
111 2 file:test.db?hello&world {hello {} world {}}
112 3 file:test.db?hello=1&world=2&vfs=tvfs {hello 1 world 2 vfs tvfs}
dan4d7a4462011-05-05 18:53:48 +0000113 4 file:test.db?hello=1&world=2&vfs=tvfs2 {}
dan5de15372011-04-23 10:12:30 +0000114 5 file:test.db?%68%65%6C%6C%6F=%77%6F%72%6C%64 {hello world}
drh812d6082011-05-20 01:50:01 +0000115 6 file:testdb%00.db?hello%00extra=world%00ex {hello world}
116 7 file:testdb%00.db?hello%00=world%00 {hello world}
117 8 file:testdb%00.db?=world&xyz=abc {xyz abc}
dan5de15372011-04-23 10:12:30 +0000118 9 file:test.db?%00hello=world&xyz=abc {xyz abc}
119 10 file:test.db?hello=%00world&xyz= {hello {} xyz {}}
120 11 file:test.db?=#ravada {}
121 12 file:test.db?&&&&&&&&hello=world&&&&&&& {hello world}
dan4d7a4462011-05-05 18:53:48 +0000122
dan3a6d8ae2011-04-23 15:54:54 +0000123 13 test.db?&&&&&&&&hello=world&&&&&&& {}
124 14 http:test.db?hello&world {}
dancd74b612011-04-22 19:37:32 +0000125} {
dan4d7a4462011-05-05 18:53:48 +0000126
127 if {$tcl_platform(platform) == "windows" && $tn>12} {
128 continue
129 }
130
dancd74b612011-04-22 19:37:32 +0000131 set ::arglist ""
132 set DB [sqlite3_open $uri]
dan3a6d8ae2011-04-23 15:54:54 +0000133 do_test 2.$tn.1 { set ::arglist } $kvlist
dancd74b612011-04-22 19:37:32 +0000134 sqlite3_close $DB
dan3a6d8ae2011-04-23 15:54:54 +0000135
136 sqlite3 db xxx.db
137 set ::arglist ""
138 execsql { ATTACH $uri AS aux }
139 do_test 2.$tn.2 { set ::arglist } $kvlist
140 db close
dancd74b612011-04-22 19:37:32 +0000141}
dan5de15372011-04-23 10:12:30 +0000142tvfs delete
dan4d7a4462011-05-05 18:53:48 +0000143tvfs2 delete
dan5de15372011-04-23 10:12:30 +0000144
145#-------------------------------------------------------------------------
146# Test that specifying a non-existent VFS raises an error.
147#
148do_test 3.1 {
149 list [catch { sqlite3 db "file:test.db?vfs=nosuchvfs" } msg] $msg
150} {1 {no such vfs: nosuchvfs}}
dancd74b612011-04-22 19:37:32 +0000151
dan3a6d8ae2011-04-23 15:54:54 +0000152#-------------------------------------------------------------------------
dan8e980372011-04-23 19:06:26 +0000153# Test some of the other options (other than "vfs").
154#
dan78e9dd22011-05-03 10:22:32 +0000155foreach {tn mode create_ok write_ok readonly_ok} {
156 1 ro 0 0 1
157 2 rw 0 1 0
158 3 rwc 1 1 0
dan3a6d8ae2011-04-23 15:54:54 +0000159} {
dan78e9dd22011-05-03 10:22:32 +0000160 catch { db close }
161 forcedelete test.db
dan3a6d8ae2011-04-23 15:54:54 +0000162
dan78e9dd22011-05-03 10:22:32 +0000163 set A(1) {0 {}}
164 set A(0) {1 {unable to open database file}}
165 do_test 4.1.$tn.1 {
166 list [catch {sqlite3 db "file:test.db?mode=$mode"} msg] $msg
167 } $A($create_ok)
168
169 catch { db close }
170 forcedelete test.db
171 sqlite3 db test.db
172 db eval { CREATE TABLE t1(a, b) }
dan3a6d8ae2011-04-23 15:54:54 +0000173 db close
dan78e9dd22011-05-03 10:22:32 +0000174
175 set A(1) {0 {}}
176 set A(0) {1 {attempt to write a readonly database}}
177 do_test 4.1.$tn.2 {
178 sqlite3 db "file:test.db?mode=$mode"
179 catchsql { INSERT INTO t1 VALUES(1, 2) }
180 } $A($write_ok)
181
182 set A(1) {0 {}}
daneaadd592011-05-10 17:43:28 +0000183 set A(0) [list 1 "access mode not allowed: $mode"]
dan78e9dd22011-05-03 10:22:32 +0000184 do_test 4.1.$tn.3 {
185 list [catch {sqlite3 db "file:test.db?mode=$mode" -readonly 1} msg] $msg
186 } $A($readonly_ok)
dan3a6d8ae2011-04-23 15:54:54 +0000187}
188
dan78e9dd22011-05-03 10:22:32 +0000189set orig [sqlite3_enable_shared_cache]
190foreach {tn options sc_default is_shared} {
191 1 "" 1 1
192 2 "cache=private" 1 0
193 3 "cache=shared" 1 1
194 4 "" 0 0
195 5 "cache=private" 0 0
196 6 "cache=shared" 0 1
197} {
198 catch { db close }
199 forcedelete test.db
200
201 sqlite3_enable_shared_cache 1
202 sqlite3 db2 test.db
203 db2 eval {CREATE TABLE t1(a, b)}
204
205 sqlite3_enable_shared_cache $sc_default
206 sqlite3 db "file:test.db?$options"
207 db eval {SELECT * FROM t1}
208
209 set A(1) {1 {database table is locked: t1}}
210 set A(0) {0 {}}
211 do_test 4.2.$tn {
212 db2 eval {BEGIN; INSERT INTO t1 VALUES(1, 2);}
213 catchsql { SELECT * FROM t1 }
214 } $A($is_shared)
215
216 db2 close
217}
218
219do_test 4.3.1 {
220 list [catch {sqlite3 db "file:test.db?mode=rc"} msg] $msg
221} {1 {no such access mode: rc}}
222do_test 4.3.2 {
223 list [catch {sqlite3 db "file:test.db?cache=public"} msg] $msg
224} {1 {no such cache mode: public}}
225
dan8e980372011-04-23 19:06:26 +0000226#-------------------------------------------------------------------------
227# Test that things work if an ATTACHed database uses a different VFS than
228# the main database. The important point is that for all operations
229# involving the ATTACHed database, the correct versions of the following
230# VFS are used for all operations involving the attached database.
231#
232# xOpen
233# xDelete
234# xAccess
235# xFullPathname
236#
237
238# This block of code creates two VFS - "tvfs1" and "tvfs2". Each time one
239# of the above methods is called using "tvfs1", global variable ::T1(X) is
240# set, where X is the file-name the method is called on. Calls to the above
241# methods using "tvfs2" set entries in the global T2 array.
242#
dan52091322011-09-24 05:55:36 +0000243ifcapable wal {
244 testvfs tvfs1
245 tvfs1 filter {xOpen xDelete xAccess xFullPathname}
246 tvfs1 script tvfs1_callback
247 proc tvfs1_callback {method filename args} {
248 set ::T1([file tail $filename]) 1
mistachkin88794812014-03-07 02:29:56 +0000249 return SQLITE_OK
dan8e980372011-04-23 19:06:26 +0000250 }
dan52091322011-09-24 05:55:36 +0000251 testvfs tvfs2
252 tvfs2 filter {xOpen xDelete xAccess xFullPathname}
253 tvfs2 script tvfs2_callback
254 proc tvfs2_callback {method filename args} {
255 set ::T2([file tail $filename]) 1
mistachkin88794812014-03-07 02:29:56 +0000256 return SQLITE_OK
dan52091322011-09-24 05:55:36 +0000257 }
258
259 catch {db close}
260 eval forcedelete [glob test.db*]
261 do_test 5.1.1 {
262 sqlite3 db file:test.db1?vfs=tvfs1
263 execsql {
264 ATTACH 'file:test.db2?vfs=tvfs2' AS aux;
265 PRAGMA main.journal_mode = PERSIST;
266 PRAGMA aux.journal_mode = PERSIST;
267 CREATE TABLE t1(a, b);
268 CREATE TABLE aux.t2(a, b);
269 PRAGMA main.journal_mode = WAL;
270 PRAGMA aux.journal_mode = WAL;
271 INSERT INTO t1 VALUES('x', 'y');
272 INSERT INTO t2 VALUES('x', 'y');
273 }
274 lsort [array names ::T1]
275 } {test.db1 test.db1-journal test.db1-wal}
276
277 do_test 5.1.2 {
278 lsort [array names ::T2]
279 } {test.db2 test.db2-journal test.db2-wal}
280 db close
281
282 tvfs1 delete
283 tvfs2 delete
284}
dan8e980372011-04-23 19:06:26 +0000285
dan3b18a9a2011-05-03 11:53:20 +0000286#-------------------------------------------------------------------------
287# Check that only "" and "localhost" are acceptable as authorities.
288#
289catch {db close}
290foreach {tn uri res} {
291 1 "file://localhost/PWD/test.db" {not an error}
292 2 "file:///PWD/test.db" {not an error}
293 3 "file:/PWD/test.db" {not an error}
294 4 "file://l%6Fcalhost/PWD/test.db" {invalid uri authority: l%6Fcalhost}
295 5 "file://lbcalhost/PWD/test.db" {invalid uri authority: lbcalhost}
296 6 "file://x/PWD/test.db" {invalid uri authority: x}
297} {
dan4d7a4462011-05-05 18:53:48 +0000298
299 if {$tcl_platform(platform)=="windows"} {
mistachkinf8a78462012-03-08 20:00:36 +0000300 set uri [string map [list PWD [string range [get_pwd] 3 end]] $uri]
dan4d7a4462011-05-05 18:53:48 +0000301 } else {
mistachkinf8a78462012-03-08 20:00:36 +0000302 set uri [string map [list PWD [string range [get_pwd] 1 end]] $uri]
dan4d7a4462011-05-05 18:53:48 +0000303 }
304
dan3b18a9a2011-05-03 11:53:20 +0000305 do_test 6.$tn {
306 set DB [sqlite3_open $uri]
307 sqlite3_errmsg $DB
308 } $res
309 catch { sqlite3_close $DB }
310}
311
dan19432992011-05-10 18:39:10 +0000312forcedelete test.db test.db2
313do_test 7.1 {
314 sqlite3 db test.db
315 execsql {
316 CREATE TABLE t1(a, b);
317 INSERT INTO t1 VALUES(1, 2);
318 ATTACH 'test.db2' AS aux;
319 CREATE TABLE aux.t2(a, b);
320 INSERT INTO t1 VALUES('a', 'b');
321 }
322 db close
323} {}
324do_test 7.2 {
325 sqlite3 db file:test.db?mode=ro
326 execsql { ATTACH 'file:test.db2?mode=rw' AS aux }
327} {}
328do_execsql_test 7.3 {
329 INSERT INTO t2 VALUES('c', 'd')
330} {}
331do_catchsql_test 7.4 {
332 INSERT INTO t1 VALUES(3, 4)
333} {1 {attempt to write a readonly database}}
dancd74b612011-04-22 19:37:32 +0000334
dan19432992011-05-10 18:39:10 +0000335finish_test