blob: 90074d0d272890cab9a1bdde7ab37dd02bc98fea [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
55 if {$tcl_platform(platform)=="windows"} {
56 if {$tn>14} break
57 set uri [string map [list PWD /[pwd]] $uri]
58 } else {
59 set uri [string map [list PWD [pwd]] $uri]
60 }
dancd74b612011-04-22 19:37:32 +000061
drh812d6082011-05-20 01:50:01 +000062 if {[file isdir $file]} {error "$file is a directory"}
dancd74b612011-04-22 19:37:32 +000063 forcedelete $file
64 do_test 1.$tn.1 { file exists $file } 0
65 set DB [sqlite3_open $uri]
66 do_test 1.$tn.2 { file exists $file } 1
67 sqlite3_close $DB
dan3a6d8ae2011-04-23 15:54:54 +000068 forcedelete $file
69
70 do_test 1.$tn.3 { file exists $file } 0
71 sqlite3 db xxx.db
dan4d7a4462011-05-05 18:53:48 +000072 catchsql { ATTACH $uri AS aux }
dan3a6d8ae2011-04-23 15:54:54 +000073 do_test 1.$tn.4 { file exists $file } 1
74 db close
dancd74b612011-04-22 19:37:32 +000075}
76
dancd74b612011-04-22 19:37:32 +000077#-------------------------------------------------------------------------
78# Test that URI query parameters are passed through to the VFS layer
79# correctly.
80#
dan4d7a4462011-05-05 18:53:48 +000081testvfs tvfs2
dancd74b612011-04-22 19:37:32 +000082testvfs tvfs -default 1
83tvfs filter xOpen
84tvfs script open_method
85proc open_method {method file arglist} {
86 set ::arglist $arglist
87}
dancd74b612011-04-22 19:37:32 +000088foreach {tn uri kvlist} {
dan5de15372011-04-23 10:12:30 +000089 1 file:test.db?hello=world {hello world}
90 2 file:test.db?hello&world {hello {} world {}}
91 3 file:test.db?hello=1&world=2&vfs=tvfs {hello 1 world 2 vfs tvfs}
dan4d7a4462011-05-05 18:53:48 +000092 4 file:test.db?hello=1&world=2&vfs=tvfs2 {}
dan5de15372011-04-23 10:12:30 +000093 5 file:test.db?%68%65%6C%6C%6F=%77%6F%72%6C%64 {hello world}
drh812d6082011-05-20 01:50:01 +000094 6 file:testdb%00.db?hello%00extra=world%00ex {hello world}
95 7 file:testdb%00.db?hello%00=world%00 {hello world}
96 8 file:testdb%00.db?=world&xyz=abc {xyz abc}
dan5de15372011-04-23 10:12:30 +000097 9 file:test.db?%00hello=world&xyz=abc {xyz abc}
98 10 file:test.db?hello=%00world&xyz= {hello {} xyz {}}
99 11 file:test.db?=#ravada {}
100 12 file:test.db?&&&&&&&&hello=world&&&&&&& {hello world}
dan4d7a4462011-05-05 18:53:48 +0000101
dan3a6d8ae2011-04-23 15:54:54 +0000102 13 test.db?&&&&&&&&hello=world&&&&&&& {}
103 14 http:test.db?hello&world {}
dancd74b612011-04-22 19:37:32 +0000104} {
dan4d7a4462011-05-05 18:53:48 +0000105
106 if {$tcl_platform(platform) == "windows" && $tn>12} {
107 continue
108 }
109
dancd74b612011-04-22 19:37:32 +0000110 set ::arglist ""
111 set DB [sqlite3_open $uri]
dan3a6d8ae2011-04-23 15:54:54 +0000112 do_test 2.$tn.1 { set ::arglist } $kvlist
dancd74b612011-04-22 19:37:32 +0000113 sqlite3_close $DB
dan3a6d8ae2011-04-23 15:54:54 +0000114
115 sqlite3 db xxx.db
116 set ::arglist ""
117 execsql { ATTACH $uri AS aux }
118 do_test 2.$tn.2 { set ::arglist } $kvlist
119 db close
dancd74b612011-04-22 19:37:32 +0000120}
dan5de15372011-04-23 10:12:30 +0000121tvfs delete
dan4d7a4462011-05-05 18:53:48 +0000122tvfs2 delete
dan5de15372011-04-23 10:12:30 +0000123
124#-------------------------------------------------------------------------
125# Test that specifying a non-existent VFS raises an error.
126#
127do_test 3.1 {
128 list [catch { sqlite3 db "file:test.db?vfs=nosuchvfs" } msg] $msg
129} {1 {no such vfs: nosuchvfs}}
dancd74b612011-04-22 19:37:32 +0000130
dan3a6d8ae2011-04-23 15:54:54 +0000131#-------------------------------------------------------------------------
dan8e980372011-04-23 19:06:26 +0000132# Test some of the other options (other than "vfs").
133#
dan78e9dd22011-05-03 10:22:32 +0000134foreach {tn mode create_ok write_ok readonly_ok} {
135 1 ro 0 0 1
136 2 rw 0 1 0
137 3 rwc 1 1 0
dan3a6d8ae2011-04-23 15:54:54 +0000138} {
dan78e9dd22011-05-03 10:22:32 +0000139 catch { db close }
140 forcedelete test.db
dan3a6d8ae2011-04-23 15:54:54 +0000141
dan78e9dd22011-05-03 10:22:32 +0000142 set A(1) {0 {}}
143 set A(0) {1 {unable to open database file}}
144 do_test 4.1.$tn.1 {
145 list [catch {sqlite3 db "file:test.db?mode=$mode"} msg] $msg
146 } $A($create_ok)
147
148 catch { db close }
149 forcedelete test.db
150 sqlite3 db test.db
151 db eval { CREATE TABLE t1(a, b) }
dan3a6d8ae2011-04-23 15:54:54 +0000152 db close
dan78e9dd22011-05-03 10:22:32 +0000153
154 set A(1) {0 {}}
155 set A(0) {1 {attempt to write a readonly database}}
156 do_test 4.1.$tn.2 {
157 sqlite3 db "file:test.db?mode=$mode"
158 catchsql { INSERT INTO t1 VALUES(1, 2) }
159 } $A($write_ok)
160
161 set A(1) {0 {}}
daneaadd592011-05-10 17:43:28 +0000162 set A(0) [list 1 "access mode not allowed: $mode"]
dan78e9dd22011-05-03 10:22:32 +0000163 do_test 4.1.$tn.3 {
164 list [catch {sqlite3 db "file:test.db?mode=$mode" -readonly 1} msg] $msg
165 } $A($readonly_ok)
dan3a6d8ae2011-04-23 15:54:54 +0000166}
167
dan78e9dd22011-05-03 10:22:32 +0000168set orig [sqlite3_enable_shared_cache]
169foreach {tn options sc_default is_shared} {
170 1 "" 1 1
171 2 "cache=private" 1 0
172 3 "cache=shared" 1 1
173 4 "" 0 0
174 5 "cache=private" 0 0
175 6 "cache=shared" 0 1
176} {
177 catch { db close }
178 forcedelete test.db
179
180 sqlite3_enable_shared_cache 1
181 sqlite3 db2 test.db
182 db2 eval {CREATE TABLE t1(a, b)}
183
184 sqlite3_enable_shared_cache $sc_default
185 sqlite3 db "file:test.db?$options"
186 db eval {SELECT * FROM t1}
187
188 set A(1) {1 {database table is locked: t1}}
189 set A(0) {0 {}}
190 do_test 4.2.$tn {
191 db2 eval {BEGIN; INSERT INTO t1 VALUES(1, 2);}
192 catchsql { SELECT * FROM t1 }
193 } $A($is_shared)
194
195 db2 close
196}
197
198do_test 4.3.1 {
199 list [catch {sqlite3 db "file:test.db?mode=rc"} msg] $msg
200} {1 {no such access mode: rc}}
201do_test 4.3.2 {
202 list [catch {sqlite3 db "file:test.db?cache=public"} msg] $msg
203} {1 {no such cache mode: public}}
204
dan8e980372011-04-23 19:06:26 +0000205#-------------------------------------------------------------------------
206# Test that things work if an ATTACHed database uses a different VFS than
207# the main database. The important point is that for all operations
208# involving the ATTACHed database, the correct versions of the following
209# VFS are used for all operations involving the attached database.
210#
211# xOpen
212# xDelete
213# xAccess
214# xFullPathname
215#
216
217# This block of code creates two VFS - "tvfs1" and "tvfs2". Each time one
218# of the above methods is called using "tvfs1", global variable ::T1(X) is
219# set, where X is the file-name the method is called on. Calls to the above
220# methods using "tvfs2" set entries in the global T2 array.
221#
dan52091322011-09-24 05:55:36 +0000222ifcapable wal {
223 testvfs tvfs1
224 tvfs1 filter {xOpen xDelete xAccess xFullPathname}
225 tvfs1 script tvfs1_callback
226 proc tvfs1_callback {method filename args} {
227 set ::T1([file tail $filename]) 1
dan8e980372011-04-23 19:06:26 +0000228 }
dan52091322011-09-24 05:55:36 +0000229 testvfs tvfs2
230 tvfs2 filter {xOpen xDelete xAccess xFullPathname}
231 tvfs2 script tvfs2_callback
232 proc tvfs2_callback {method filename args} {
233 set ::T2([file tail $filename]) 1
234 }
235
236 catch {db close}
237 eval forcedelete [glob test.db*]
238 do_test 5.1.1 {
239 sqlite3 db file:test.db1?vfs=tvfs1
240 execsql {
241 ATTACH 'file:test.db2?vfs=tvfs2' AS aux;
242 PRAGMA main.journal_mode = PERSIST;
243 PRAGMA aux.journal_mode = PERSIST;
244 CREATE TABLE t1(a, b);
245 CREATE TABLE aux.t2(a, b);
246 PRAGMA main.journal_mode = WAL;
247 PRAGMA aux.journal_mode = WAL;
248 INSERT INTO t1 VALUES('x', 'y');
249 INSERT INTO t2 VALUES('x', 'y');
250 }
251 lsort [array names ::T1]
252 } {test.db1 test.db1-journal test.db1-wal}
253
254 do_test 5.1.2 {
255 lsort [array names ::T2]
256 } {test.db2 test.db2-journal test.db2-wal}
257 db close
258
259 tvfs1 delete
260 tvfs2 delete
261}
dan8e980372011-04-23 19:06:26 +0000262
dan3b18a9a2011-05-03 11:53:20 +0000263#-------------------------------------------------------------------------
264# Check that only "" and "localhost" are acceptable as authorities.
265#
266catch {db close}
267foreach {tn uri res} {
268 1 "file://localhost/PWD/test.db" {not an error}
269 2 "file:///PWD/test.db" {not an error}
270 3 "file:/PWD/test.db" {not an error}
271 4 "file://l%6Fcalhost/PWD/test.db" {invalid uri authority: l%6Fcalhost}
272 5 "file://lbcalhost/PWD/test.db" {invalid uri authority: lbcalhost}
273 6 "file://x/PWD/test.db" {invalid uri authority: x}
274} {
dan4d7a4462011-05-05 18:53:48 +0000275
276 if {$tcl_platform(platform)=="windows"} {
277 set uri [string map [list PWD [string range [pwd] 3 end]] $uri]
278 } else {
279 set uri [string map [list PWD [string range [pwd] 1 end]] $uri]
280 }
281
dan3b18a9a2011-05-03 11:53:20 +0000282 do_test 6.$tn {
283 set DB [sqlite3_open $uri]
284 sqlite3_errmsg $DB
285 } $res
286 catch { sqlite3_close $DB }
287}
288
dan19432992011-05-10 18:39:10 +0000289forcedelete test.db test.db2
290do_test 7.1 {
291 sqlite3 db test.db
292 execsql {
293 CREATE TABLE t1(a, b);
294 INSERT INTO t1 VALUES(1, 2);
295 ATTACH 'test.db2' AS aux;
296 CREATE TABLE aux.t2(a, b);
297 INSERT INTO t1 VALUES('a', 'b');
298 }
299 db close
300} {}
301do_test 7.2 {
302 sqlite3 db file:test.db?mode=ro
303 execsql { ATTACH 'file:test.db2?mode=rw' AS aux }
304} {}
305do_execsql_test 7.3 {
306 INSERT INTO t2 VALUES('c', 'd')
307} {}
308do_catchsql_test 7.4 {
309 INSERT INTO t1 VALUES(3, 4)
310} {1 {attempt to write a readonly database}}
dancd74b612011-04-22 19:37:32 +0000311
dan19432992011-05-10 18:39:10 +0000312finish_test