blob: af1ad672508744d55b8ae73011224bfc7f2696c2 [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 #
67 # NOTE: On Windows, we need to account for the fact that the current
68 # directory does not start with a forward slash.
69 #
70 set uri [string map [list PWD/ /[test_pwd /]] $uri]
dancd74b612011-04-22 19:37:32 +000071 } else {
mistachkin776f5c92012-03-08 20:39:08 +000072 set uri [string map [list PWD/ [test_pwd /]] $uri]
dancd74b612011-04-22 19:37:32 +000073 }
mistachkin4a41f342012-03-06 03:00:49 +000074
drh812d6082011-05-20 01:50:01 +000075 if {[file isdir $file]} {error "$file is a directory"}
dancd74b612011-04-22 19:37:32 +000076 forcedelete $file
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
80 sqlite3_close $DB
dan3a6d8ae2011-04-23 15:54:54 +000081 forcedelete $file
82
83 do_test 1.$tn.3 { file exists $file } 0
84 sqlite3 db xxx.db
dan4d7a4462011-05-05 18:53:48 +000085 catchsql { ATTACH $uri AS aux }
dan3a6d8ae2011-04-23 15:54:54 +000086 do_test 1.$tn.4 { file exists $file } 1
87 db close
dancd74b612011-04-22 19:37:32 +000088}
89
dancd74b612011-04-22 19:37:32 +000090#-------------------------------------------------------------------------
91# Test that URI query parameters are passed through to the VFS layer
92# correctly.
93#
dan4d7a4462011-05-05 18:53:48 +000094testvfs tvfs2
dancd74b612011-04-22 19:37:32 +000095testvfs tvfs -default 1
96tvfs filter xOpen
97tvfs script open_method
98proc open_method {method file arglist} {
99 set ::arglist $arglist
100}
dancd74b612011-04-22 19:37:32 +0000101foreach {tn uri kvlist} {
dan5de15372011-04-23 10:12:30 +0000102 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}
dan4d7a4462011-05-05 18:53:48 +0000105 4 file:test.db?hello=1&world=2&vfs=tvfs2 {}
dan5de15372011-04-23 10:12:30 +0000106 5 file:test.db?%68%65%6C%6C%6F=%77%6F%72%6C%64 {hello world}
drh812d6082011-05-20 01:50:01 +0000107 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}
dan5de15372011-04-23 10:12:30 +0000110 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}
dan4d7a4462011-05-05 18:53:48 +0000114
dan3a6d8ae2011-04-23 15:54:54 +0000115 13 test.db?&&&&&&&&hello=world&&&&&&& {}
116 14 http:test.db?hello&world {}
dancd74b612011-04-22 19:37:32 +0000117} {
dan4d7a4462011-05-05 18:53:48 +0000118
119 if {$tcl_platform(platform) == "windows" && $tn>12} {
120 continue
121 }
122
dancd74b612011-04-22 19:37:32 +0000123 set ::arglist ""
124 set DB [sqlite3_open $uri]
dan3a6d8ae2011-04-23 15:54:54 +0000125 do_test 2.$tn.1 { set ::arglist } $kvlist
dancd74b612011-04-22 19:37:32 +0000126 sqlite3_close $DB
dan3a6d8ae2011-04-23 15:54:54 +0000127
128 sqlite3 db xxx.db
129 set ::arglist ""
130 execsql { ATTACH $uri AS aux }
131 do_test 2.$tn.2 { set ::arglist } $kvlist
132 db close
dancd74b612011-04-22 19:37:32 +0000133}
dan5de15372011-04-23 10:12:30 +0000134tvfs delete
dan4d7a4462011-05-05 18:53:48 +0000135tvfs2 delete
dan5de15372011-04-23 10:12:30 +0000136
137#-------------------------------------------------------------------------
138# Test that specifying a non-existent VFS raises an error.
139#
140do_test 3.1 {
141 list [catch { sqlite3 db "file:test.db?vfs=nosuchvfs" } msg] $msg
142} {1 {no such vfs: nosuchvfs}}
dancd74b612011-04-22 19:37:32 +0000143
dan3a6d8ae2011-04-23 15:54:54 +0000144#-------------------------------------------------------------------------
dan8e980372011-04-23 19:06:26 +0000145# Test some of the other options (other than "vfs").
146#
dan78e9dd22011-05-03 10:22:32 +0000147foreach {tn mode create_ok write_ok readonly_ok} {
148 1 ro 0 0 1
149 2 rw 0 1 0
150 3 rwc 1 1 0
dan3a6d8ae2011-04-23 15:54:54 +0000151} {
dan78e9dd22011-05-03 10:22:32 +0000152 catch { db close }
153 forcedelete test.db
dan3a6d8ae2011-04-23 15:54:54 +0000154
dan78e9dd22011-05-03 10:22:32 +0000155 set A(1) {0 {}}
156 set A(0) {1 {unable to open database file}}
157 do_test 4.1.$tn.1 {
158 list [catch {sqlite3 db "file:test.db?mode=$mode"} msg] $msg
159 } $A($create_ok)
160
161 catch { db close }
162 forcedelete test.db
163 sqlite3 db test.db
164 db eval { CREATE TABLE t1(a, b) }
dan3a6d8ae2011-04-23 15:54:54 +0000165 db close
dan78e9dd22011-05-03 10:22:32 +0000166
167 set A(1) {0 {}}
168 set A(0) {1 {attempt to write a readonly database}}
169 do_test 4.1.$tn.2 {
170 sqlite3 db "file:test.db?mode=$mode"
171 catchsql { INSERT INTO t1 VALUES(1, 2) }
172 } $A($write_ok)
173
174 set A(1) {0 {}}
daneaadd592011-05-10 17:43:28 +0000175 set A(0) [list 1 "access mode not allowed: $mode"]
dan78e9dd22011-05-03 10:22:32 +0000176 do_test 4.1.$tn.3 {
177 list [catch {sqlite3 db "file:test.db?mode=$mode" -readonly 1} msg] $msg
178 } $A($readonly_ok)
dan3a6d8ae2011-04-23 15:54:54 +0000179}
180
dan78e9dd22011-05-03 10:22:32 +0000181set orig [sqlite3_enable_shared_cache]
182foreach {tn options sc_default is_shared} {
183 1 "" 1 1
184 2 "cache=private" 1 0
185 3 "cache=shared" 1 1
186 4 "" 0 0
187 5 "cache=private" 0 0
188 6 "cache=shared" 0 1
189} {
190 catch { db close }
191 forcedelete test.db
192
193 sqlite3_enable_shared_cache 1
194 sqlite3 db2 test.db
195 db2 eval {CREATE TABLE t1(a, b)}
196
197 sqlite3_enable_shared_cache $sc_default
198 sqlite3 db "file:test.db?$options"
199 db eval {SELECT * FROM t1}
200
201 set A(1) {1 {database table is locked: t1}}
202 set A(0) {0 {}}
203 do_test 4.2.$tn {
204 db2 eval {BEGIN; INSERT INTO t1 VALUES(1, 2);}
205 catchsql { SELECT * FROM t1 }
206 } $A($is_shared)
207
208 db2 close
209}
210
211do_test 4.3.1 {
212 list [catch {sqlite3 db "file:test.db?mode=rc"} msg] $msg
213} {1 {no such access mode: rc}}
214do_test 4.3.2 {
215 list [catch {sqlite3 db "file:test.db?cache=public"} msg] $msg
216} {1 {no such cache mode: public}}
217
dan8e980372011-04-23 19:06:26 +0000218#-------------------------------------------------------------------------
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.
223#
224# xOpen
225# xDelete
226# xAccess
227# xFullPathname
228#
229
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.
234#
dan52091322011-09-24 05:55:36 +0000235ifcapable wal {
236 testvfs tvfs1
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
dan8e980372011-04-23 19:06:26 +0000241 }
dan52091322011-09-24 05:55:36 +0000242 testvfs tvfs2
243 tvfs2 filter {xOpen xDelete xAccess xFullPathname}
244 tvfs2 script tvfs2_callback
245 proc tvfs2_callback {method filename args} {
246 set ::T2([file tail $filename]) 1
247 }
248
249 catch {db close}
250 eval forcedelete [glob test.db*]
251 do_test 5.1.1 {
252 sqlite3 db file:test.db1?vfs=tvfs1
253 execsql {
254 ATTACH 'file:test.db2?vfs=tvfs2' AS aux;
255 PRAGMA main.journal_mode = PERSIST;
256 PRAGMA aux.journal_mode = PERSIST;
257 CREATE TABLE t1(a, b);
258 CREATE TABLE aux.t2(a, b);
259 PRAGMA main.journal_mode = WAL;
260 PRAGMA aux.journal_mode = WAL;
261 INSERT INTO t1 VALUES('x', 'y');
262 INSERT INTO t2 VALUES('x', 'y');
263 }
264 lsort [array names ::T1]
265 } {test.db1 test.db1-journal test.db1-wal}
266
267 do_test 5.1.2 {
268 lsort [array names ::T2]
269 } {test.db2 test.db2-journal test.db2-wal}
270 db close
271
272 tvfs1 delete
273 tvfs2 delete
274}
dan8e980372011-04-23 19:06:26 +0000275
dan3b18a9a2011-05-03 11:53:20 +0000276#-------------------------------------------------------------------------
277# Check that only "" and "localhost" are acceptable as authorities.
278#
279catch {db close}
280foreach {tn uri res} {
281 1 "file://localhost/PWD/test.db" {not an error}
282 2 "file:///PWD/test.db" {not an error}
283 3 "file:/PWD/test.db" {not an error}
284 4 "file://l%6Fcalhost/PWD/test.db" {invalid uri authority: l%6Fcalhost}
285 5 "file://lbcalhost/PWD/test.db" {invalid uri authority: lbcalhost}
286 6 "file://x/PWD/test.db" {invalid uri authority: x}
287} {
dan4d7a4462011-05-05 18:53:48 +0000288
289 if {$tcl_platform(platform)=="windows"} {
mistachkinf8a78462012-03-08 20:00:36 +0000290 set uri [string map [list PWD [string range [get_pwd] 3 end]] $uri]
dan4d7a4462011-05-05 18:53:48 +0000291 } else {
mistachkinf8a78462012-03-08 20:00:36 +0000292 set uri [string map [list PWD [string range [get_pwd] 1 end]] $uri]
dan4d7a4462011-05-05 18:53:48 +0000293 }
294
dan3b18a9a2011-05-03 11:53:20 +0000295 do_test 6.$tn {
296 set DB [sqlite3_open $uri]
297 sqlite3_errmsg $DB
298 } $res
299 catch { sqlite3_close $DB }
300}
301
dan19432992011-05-10 18:39:10 +0000302forcedelete test.db test.db2
303do_test 7.1 {
304 sqlite3 db test.db
305 execsql {
306 CREATE TABLE t1(a, b);
307 INSERT INTO t1 VALUES(1, 2);
308 ATTACH 'test.db2' AS aux;
309 CREATE TABLE aux.t2(a, b);
310 INSERT INTO t1 VALUES('a', 'b');
311 }
312 db close
313} {}
314do_test 7.2 {
315 sqlite3 db file:test.db?mode=ro
316 execsql { ATTACH 'file:test.db2?mode=rw' AS aux }
317} {}
318do_execsql_test 7.3 {
319 INSERT INTO t2 VALUES('c', 'd')
320} {}
321do_catchsql_test 7.4 {
322 INSERT INTO t1 VALUES(3, 4)
323} {1 {attempt to write a readonly database}}
dancd74b612011-04-22 19:37:32 +0000324
dan19432992011-05-10 18:39:10 +0000325finish_test