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
19 proc parse_uri {uri} {
23 tvfs script parse_uri_open_cb
26 set DB [sqlite3_open_v2 $uri {
27 SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE SQLITE_OPEN_WAL
35 proc parse_uri_open_cb {method file arglist} {
36 set ::uri_open [list $file $arglist]
39 proc open_uri_error {uri} {
40 set flags {SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE SQLITE_OPEN_WAL}
41 set DB [sqlite3_open_v2 $uri $flags ""]
42 set e [sqlite3_errmsg $DB]
47 # EVIDENCE-OF: R-35840-33204 If URI filename interpretation is enabled,
48 # and the filename argument begins with "file:", then the filename is
49 # interpreted as a URI.
51 # EVIDENCE-OF: R-24124-56960 URI filename interpretation is enabled if
52 # the SQLITE_OPEN_URI flag is set in the fourth argument to
53 # sqlite3_open_v2(), or if it has been enabled globally using the
54 # SQLITE_CONFIG_URI option with the sqlite3_config() method or by the
55 # SQLITE_USE_URI compile-time option.
57 if {$tcl_platform(platform) == "unix"} {
58 set flags [list SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE]
60 # Tests with SQLITE_CONFIG_URI configured to false. URI intepretation is
61 # only enabled if the SQLITE_OPEN_URI flag is specified.
65 forcedelete file:test.db test.db
66 set DB [sqlite3_open_v2 file:test.db [concat $flags SQLITE_OPEN_URI] ""]
67 list [file exists file:test.db] [file exists test.db]
70 forcedelete file:test.db2 test.db2
71 set STMT [sqlite3_prepare $DB "ATTACH 'file:test.db2' AS aux" -1 dummy]
73 sqlite3_finalize $STMT
74 list [file exists file:test.db2] [file exists test.db2]
78 forcedelete file:test.db test.db
79 set DB [sqlite3_open_v2 file:test.db [concat $flags] ""]
80 list [file exists file:test.db] [file exists test.db]
83 forcedelete file:test.db2 test.db2
84 set STMT [sqlite3_prepare $DB "ATTACH 'file:test.db2' AS aux" -1 dummy]
86 sqlite3_finalize $STMT
87 list [file exists file:test.db2] [file exists test.db2]
91 # Tests with SQLITE_CONFIG_URI configured to true. URI intepretation is
92 # enabled with or without SQLITE_OPEN_URI.
97 forcedelete file:test.db test.db
98 set DB [sqlite3_open_v2 file:test.db [concat $flags SQLITE_OPEN_URI] ""]
99 list [file exists file:test.db] [file exists test.db]
102 forcedelete file:test.db2 test.db2
103 set STMT [sqlite3_prepare $DB "ATTACH 'file:test.db2' AS aux" -1 dummy]
105 sqlite3_finalize $STMT
106 list [file exists file:test.db2] [file exists test.db2]
110 forcedelete file:test.db test.db
111 set DB [sqlite3_open_v2 file:test.db [concat $flags] ""]
112 list [file exists file:test.db] [file exists test.db]
115 forcedelete file:test.db2 test.db2
116 set STMT [sqlite3_prepare $DB "ATTACH 'file:test.db2' AS aux" -1 dummy]
118 sqlite3_finalize $STMT
119 list [file exists file:test.db2] [file exists test.db2]
124 # ensure uri processing enabled for the rest of the tests
128 # EVIDENCE-OF: R-06842-00595 If the URI contains an authority, then it
129 # must be either an empty string or the string "localhost".
131 # EVIDENCE-OF: R-17482-00398 If the authority is not an empty string or
132 # "localhost", an error is returned to the caller.
134 if {$tcl_platform(platform) == "unix"} {
135 set flags [list SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE SQLITE_OPEN_URI]
136 foreach {tn uri error} "
137 1 {file://localhost[test_pwd /]test.db} {not an error}
138 2 {file://[test_pwd /]test.db} {not an error}
139 3 {file://x[test_pwd /]test.db} {invalid uri authority: x}
140 4 {file://invalid[test_pwd /]test.db} {invalid uri authority: invalid}
143 set DB [sqlite3_open_v2 $uri $flags ""]
144 set e [sqlite3_errmsg $DB]
151 # EVIDENCE-OF: R-45981-25528 The fragment component of a URI, if
152 # present, is ignored.
154 # It is difficult to test that something is ignored correctly. So these tests
155 # just show that adding a fragment does not interfere with the pathname or
156 # parameters passed through to the VFS xOpen() methods.
158 foreach {tn uri parse} "
159 1 {file:test.db#abc} {[test_pwd / {}]test.db {}}
160 2 {file:test.db?a=b#abc} {[test_pwd / {}]test.db {a b}}
161 3 {file:test.db?a=b#?c=d} {[test_pwd / {}]test.db {a b}}
163 do_filepath_test 3.$tn { parse_uri $uri } $parse
166 # EVIDENCE-OF: R-62557-09390 SQLite uses the path component of the URI
167 # as the name of the disk file which contains the database.
169 # EVIDENCE-OF: R-28659-11035 If the path begins with a '/' character,
170 # then it is interpreted as an absolute path.
172 # EVIDENCE-OF: R-46234-61323 If the path does not begin with a '/'
173 # (meaning that the authority section is omitted from the URI) then the
174 # path is interpreted as a relative path.
176 foreach {tn uri parse} "
177 1 {file:test.db} {[test_pwd / {}]test.db {}}
178 2 {file:/test.db} {/test.db {}}
179 3 {file:///test.db} {/test.db {}}
180 4 {file://localhost/test.db} {/test.db {}}
181 5 {file:/a/b/c/test.db} {/a/b/c/test.db {}}
183 do_filepath_test 4.$tn { parse_uri $uri } $parse
186 # EVIDENCE-OF: R-01612-30877 The "vfs" parameter may be used to specify
187 # the name of a VFS object that provides the operating system interface
188 # that should be used to access the database file on disk.
190 # The above is tested by cases 1.* below.
192 # EVIDENCE-OF: R-52293-58497 If this option is set to an empty string
193 # the default VFS object is used.
195 # The above is tested by cases 2.* below.
197 # EVIDENCE-OF: R-31855-18665 If sqlite3_open_v2() is used and the vfs
198 # option is present, then the VFS specified by the option takes
199 # precedence over the value passed as the fourth parameter to
202 # The above is tested by cases 3.* below.
204 proc vfs_open_cb {name args} {
207 foreach {name default} {vfs1 0 vfs2 0 vfs3 1} {
208 testvfs $name -default $default
210 $name script [list vfs_open_cb $name]
212 foreach {tn uri defvfs vfs} {
213 1.1 "file:test.db?vfs=vfs1" "" vfs1
214 1.2 "file:test.db?vfs=vfs2" "" vfs2
216 2.1 "file:test.db" vfs1 vfs1
217 2.2 "file:test.db?vfs=" vfs1 vfs3
219 3.1 "file:test.db?vfs=vfs1" vfs2 vfs1
220 3.2 "file:test.db?vfs=vfs2" vfs1 vfs2
221 3.3 "file:test.db?xvfs=vfs1" vfs2 vfs2
222 3.4 "file:test.db?xvfs=vfs2" vfs1 vfs1
225 set flags [list SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE SQLITE_OPEN_URI]
227 sqlite3_open_v2 $uri $flags $defvfs
236 # EVIDENCE-OF: R-48365-36308 Specifying an unknown VFS is an error.
238 set flags [list SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE SQLITE_OPEN_URI]
240 set DB [sqlite3_open_v2 file:test.db?vfs=nosuchvfs $flags ""]
241 set errmsg [sqlite3_errmsg $DB]
244 } {no such vfs: nosuchvfs}
247 # EVIDENCE-OF: R-44013-13102 The mode parameter may be set to either
248 # "ro", "rw", "rwc", or "memory". Attempting to set it to any other
253 foreach {tn uri error} "
254 1 {file:test.db?mode=ro} {not an error}
255 2 {file:test.db?mode=rw} {not an error}
256 3 {file:test.db?mode=rwc} {not an error}
257 4 {file:test.db?mode=Ro} {no such access mode: Ro}
258 5 {file:test.db?mode=Rw} {no such access mode: Rw}
259 6 {file:test.db?mode=Rwc} {no such access mode: Rwc}
260 7 {file:test.db?mode=memory} {not an error}
261 8 {file:test.db?mode=MEMORY} {no such access mode: MEMORY}
263 do_test 7.$tn { open_uri_error $uri } $error
267 # EVIDENCE-OF: R-43036-46756 If "ro" is specified, then the database is
268 # opened for read-only access, just as if the SQLITE_OPEN_READONLY flag
269 # had been set in the third argument to sqlite3_open_v2().
271 # EVIDENCE-OF: R-40137-26050 If the mode option is set to "rw", then the
272 # database is opened for read-write (but not create) access, as if
273 # SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had been set.
275 # EVIDENCE-OF: R-26845-32976 Value "rwc" is equivalent to setting both
276 # SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE.
278 foreach {tn uri read write create} {
279 1 {file:test.db?mode=ro} 1 0 0
280 2 {file:test.db?mode=rw} 1 1 0
281 3 {file:test.db?mode=rwc} 1 1 1
283 set RES(c,0) {1 {unable to open database file}}
285 set RES(w,0) {1 {attempt to write a readonly database}}
287 set RES(r,0) {1 {this never happens}}
288 set RES(r,1) {0 {a b}}
290 # Test CREATE access:
292 do_test 8.$tn.c { list [catch { sqlite3 db $uri } msg] $msg } $RES(c,$create)
296 db eval { CREATE TABLE t1(a, b) ; INSERT INTO t1 VALUES('a', 'b') ;}
302 catchsql { SELECT * FROM t1 }
308 catchsql { INSERT INTO t1 VALUES(1, 2) }
314 # EVIDENCE-OF: R-20590-08726 It is an error to specify a value for the
315 # mode parameter that is less restrictive than that specified by the
316 # flags passed in the third parameter to sqlite3_open_v2().
321 foreach {tn uri flags error} {
322 1 {file:test.db?mode=ro} ro {not an error}
323 2 {file:test.db?mode=ro} rw {not an error}
324 3 {file:test.db?mode=ro} rwc {not an error}
326 4 {file:test.db?mode=rw} ro {access mode not allowed: rw}
327 5 {file:test.db?mode=rw} rw {not an error}
328 6 {file:test.db?mode=rw} rwc {not an error}
330 7 {file:test.db?mode=rwc} ro {access mode not allowed: rwc}
331 8 {file:test.db?mode=rwc} rw {access mode not allowed: rwc}
332 9 {file:test.db?mode=rwc} rwc {not an error}
334 set f(ro) [list SQLITE_OPEN_READONLY SQLITE_OPEN_URI]
335 set f(rw) [list SQLITE_OPEN_READWRITE SQLITE_OPEN_URI]
336 set f(rwc) [list SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE SQLITE_OPEN_URI]
338 set DB [sqlite3_open_v2 $uri $f($flags) ""]
339 set e [sqlite3_errmsg $DB]
342 do_test 9.$tn { set e } $error
345 # EVIDENCE-OF: R-23182-54295 The cache parameter may be set to either
346 # "shared" or "private".
349 foreach {tn uri error} "
350 1 {file:test.db?cache=private} {not an error}
351 2 {file:test.db?cache=shared} {not an error}
352 3 {file:test.db?cache=yes} {no such cache mode: yes}
353 4 {file:test.db?cache=} {no such cache mode: }
355 do_test 10.$tn { open_uri_error $uri } $error
358 # EVIDENCE-OF: R-23027-03515 Setting it to "shared" is equivalent to
359 # setting the SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed
360 # to sqlite3_open_v2().
362 # EVIDENCE-OF: R-49793-28525 Setting the cache parameter to "private" is
363 # equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
365 # EVIDENCE-OF: R-31773-41793 If sqlite3_open_v2() is used and the
366 # "cache" parameter is present in a URI filename, its value overrides
367 # any behavior requested by setting SQLITE_OPEN_PRIVATECACHE or
368 # SQLITE_OPEN_SHAREDCACHE flag.
370 set orig [sqlite3_enable_shared_cache]
371 foreach {tn uri flags shared_default isshared} {
372 1.1 "file:test.db" "" 0 0
373 1.2 "file:test.db" "" 1 1
374 1.3 "file:test.db" private 0 0
375 1.4 "file:test.db" private 1 0
376 1.5 "file:test.db" shared 0 1
377 1.6 "file:test.db" shared 1 1
379 2.1 "file:test.db?cache=private" "" 0 0
380 2.2 "file:test.db?cache=private" "" 1 0
381 2.3 "file:test.db?cache=private" private 0 0
382 2.4 "file:test.db?cache=private" private 1 0
383 2.5 "file:test.db?cache=private" shared 0 0
384 2.6 "file:test.db?cache=private" shared 1 0
386 3.1 "file:test.db?cache=shared" "" 0 1
387 3.2 "file:test.db?cache=shared" "" 1 1
388 3.3 "file:test.db?cache=shared" private 0 1
389 3.4 "file:test.db?cache=shared" private 1 1
390 3.5 "file:test.db?cache=shared" shared 0 1
391 3.6 "file:test.db?cache=shared" shared 1 1
394 sqlite3_enable_shared_cache 1
396 sqlite3_enable_shared_cache 0
400 INSERT INTO t1 VALUES('ok');
404 set f() {SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE SQLITE_OPEN_URI}
405 set f(shared) [concat $f() SQLITE_OPEN_SHAREDCACHE]
406 set f(private) [concat $f() SQLITE_OPEN_PRIVATECACHE]
408 sqlite3_enable_shared_cache $shared_default
409 set DB [sqlite3_open_v2 $uri $f($flags) ""]
411 set STMT [sqlite3_prepare $DB "SELECT * FROM t1" -1 dummy]
415 INSERT INTO t1 VALUES('ko');
419 sqlite3_finalize $STMT
421 set RES(0) {not an error}
422 set RES(1) {database table is locked: t1}
424 do_test 11.$tn { sqlite3_errmsg $DB } $RES($isshared)
429 sqlite3_enable_shared_cache $orig
431 # EVIDENCE-OF: R-63472-46769 Specifying an unknown parameter in the
432 # query component of a URI is not an error.
434 do_filepath_test 12.1 {
435 parse_uri file://localhost/test.db?an=unknown¶meter=is&ok=
436 } {/test.db {an unknown parameter is ok {}}}
437 do_filepath_test 12.2 {
438 parse_uri file://localhost/test.db?an&unknown¶meter&is&ok
439 } {/test.db {an {} unknown {} parameter {} is {} ok {}}}
441 # EVIDENCE-OF: R-27458-04043 URI hexadecimal escape sequences (%HH) are
442 # supported within the path and query components of a URI.
444 # EVIDENCE-OF: R-52765-50368 Before the path or query components of a
445 # URI filename are interpreted, they are encoded using UTF-8 and all
446 # hexadecimal escape sequences replaced by a single byte containing the
447 # corresponding octet.
449 # The second of the two statements above is tested by creating a
450 # multi-byte utf-8 character using a sequence of %HH escapes.
452 foreach {tn uri parse} "
453 1 {file:/test.%64%62} {/test.db {}}
454 2 {file:/test.db?%68%65%6c%6c%6f=%77%6f%72%6c%64} {/test.db {hello world}}
455 3 {file:/%C3%BF.db} {/\xFF.db {}}
457 do_filepath_test 13.$tn { parse_uri $uri } $parse