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
29 set fileName [sqlite3_db_filename $DB main]
37 proc parse_uri_open_cb {method file arglist} {
38 set ::uri_open [list $file $arglist]
41 proc open_uri_error {uri} {
42 set flags {SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE SQLITE_OPEN_WAL}
43 set DB [sqlite3_open_v2 $uri $flags ""]
44 set e [sqlite3_errmsg $DB]
49 # EVIDENCE-OF: R-35840-33204 If URI filename interpretation is enabled,
50 # and the filename argument begins with "file:", then the filename is
51 # interpreted as a URI.
53 # EVIDENCE-OF: R-27632-24205 URI filename interpretation is enabled if
54 # the SQLITE_OPEN_URI flag is set in the third argument to
55 # sqlite3_open_v2(), or if it has been enabled globally using the
56 # SQLITE_CONFIG_URI option with the sqlite3_config() method or by the
57 # SQLITE_USE_URI compile-time option.
59 if {$tcl_platform(platform) == "unix"} {
60 set flags [list SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE]
62 # Tests with SQLITE_CONFIG_URI configured to false. URI intepretation is
63 # only enabled if the SQLITE_OPEN_URI flag is specified.
67 forcedelete file:test.db test.db
68 set DB [sqlite3_open_v2 file:test.db [concat $flags SQLITE_OPEN_URI] ""]
69 list [file exists file:test.db] [file exists test.db]
72 forcedelete file:test.db2 test.db2
73 set STMT [sqlite3_prepare $DB "ATTACH 'file:test.db2' AS aux" -1 dummy]
75 sqlite3_finalize $STMT
76 list [file exists file:test.db2] [file exists test.db2]
80 forcedelete file:test.db test.db
81 set DB [sqlite3_open_v2 file:test.db [concat $flags] ""]
82 list [file exists file:test.db] [file exists test.db]
85 forcedelete file:test.db2 test.db2
86 set STMT [sqlite3_prepare $DB "ATTACH 'file:test.db2' AS aux" -1 dummy]
88 sqlite3_finalize $STMT
89 list [file exists file:test.db2] [file exists test.db2]
93 # Tests with SQLITE_CONFIG_URI configured to true. URI intepretation is
94 # enabled with or without SQLITE_OPEN_URI.
99 forcedelete file:test.db test.db
100 set DB [sqlite3_open_v2 file:test.db [concat $flags SQLITE_OPEN_URI] ""]
101 list [file exists file:test.db] [file exists test.db]
104 forcedelete file:test.db2 test.db2
105 set STMT [sqlite3_prepare $DB "ATTACH 'file:test.db2' AS aux" -1 dummy]
107 sqlite3_finalize $STMT
108 list [file exists file:test.db2] [file exists test.db2]
112 forcedelete file:test.db test.db
113 set DB [sqlite3_open_v2 file:test.db [concat $flags] ""]
114 list [file exists file:test.db] [file exists test.db]
117 forcedelete file:test.db2 test.db2
118 set STMT [sqlite3_prepare $DB "ATTACH 'file:test.db2' AS aux" -1 dummy]
120 sqlite3_finalize $STMT
121 list [file exists file:test.db2] [file exists test.db2]
126 # ensure uri processing enabled for the rest of the tests
130 # EVIDENCE-OF: R-06842-00595 If the URI contains an authority, then it
131 # must be either an empty string or the string "localhost".
133 # EVIDENCE-OF: R-17482-00398 If the authority is not an empty string or
134 # "localhost", an error is returned to the caller.
136 if {$tcl_platform(platform) == "unix"} {
137 set flags [list SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE SQLITE_OPEN_URI]
138 foreach {tn uri error} "
139 1 {file://localhost[test_pwd /]test.db} {not an error}
140 2 {file://[test_pwd /]test.db} {not an error}
141 3 {file://x[test_pwd /]test.db} {invalid uri authority: x}
142 4 {file://invalid[test_pwd /]test.db} {invalid uri authority: invalid}
145 set DB [sqlite3_open_v2 $uri $flags ""]
146 set e [sqlite3_errmsg $DB]
153 # EVIDENCE-OF: R-45981-25528 The fragment component of a URI, if
154 # present, is ignored.
156 # It is difficult to test that something is ignored correctly. So these tests
157 # just show that adding a fragment does not interfere with the pathname or
158 # parameters passed through to the VFS xOpen() methods.
160 foreach {tn uri parse} "
161 1 {file:test.db#abc} {[test_pwd / {}]test.db {}}
162 2 {file:test.db?a=b#abc} {[test_pwd / {}]test.db {a b}}
163 3 {file:test.db?a=b#?c=d} {[test_pwd / {}]test.db {a b}}
165 do_filepath_test 3.$tn { parse_uri $uri } $parse
168 # EVIDENCE-OF: R-62557-09390 SQLite uses the path component of the URI
169 # as the name of the disk file which contains the database.
171 # EVIDENCE-OF: R-28659-11035 If the path begins with a '/' character,
172 # then it is interpreted as an absolute path.
174 # EVIDENCE-OF: R-46234-61323 If the path does not begin with a '/'
175 # (meaning that the authority section is omitted from the URI) then the
176 # path is interpreted as a relative path.
178 foreach {tn uri parse} "
179 1 {file:test.db} {[test_pwd / {}]test.db {}}
180 2 {file:/test.db} {/test.db {}}
181 3 {file:///test.db} {/test.db {}}
182 4 {file://localhost/test.db} {/test.db {}}
183 5 {file:/a/b/c/test.db} {/a/b/c/test.db {}}
185 do_filepath_test 4.$tn { parse_uri $uri } $parse
188 # EVIDENCE-OF: R-01612-30877 The "vfs" parameter may be used to specify
189 # the name of a VFS object that provides the operating system interface
190 # that should be used to access the database file on disk.
192 # The above is tested by cases 1.* below.
194 # EVIDENCE-OF: R-52293-58497 If this option is set to an empty string
195 # the default VFS object is used.
197 # The above is tested by cases 2.* below.
199 # EVIDENCE-OF: R-31855-18665 If sqlite3_open_v2() is used and the vfs
200 # option is present, then the VFS specified by the option takes
201 # precedence over the value passed as the fourth parameter to
204 # The above is tested by cases 3.* below.
206 proc vfs_open_cb {name args} {
209 foreach {name default} {vfs1 0 vfs2 0 vfs3 1} {
210 testvfs $name -default $default
212 $name script [list vfs_open_cb $name]
214 foreach {tn uri defvfs vfs} {
215 1.1 "file:test.db?vfs=vfs1" "" vfs1
216 1.2 "file:test.db?vfs=vfs2" "" vfs2
218 2.1 "file:test.db" vfs1 vfs1
219 2.2 "file:test.db?vfs=" vfs1 vfs3
221 3.1 "file:test.db?vfs=vfs1" vfs2 vfs1
222 3.2 "file:test.db?vfs=vfs2" vfs1 vfs2
223 3.3 "file:test.db?xvfs=vfs1" vfs2 vfs2
224 3.4 "file:test.db?xvfs=vfs2" vfs1 vfs1
227 set flags [list SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE SQLITE_OPEN_URI]
229 sqlite3_open_v2 $uri $flags $defvfs
238 # EVIDENCE-OF: R-48365-36308 Specifying an unknown VFS is an error.
240 set flags [list SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE SQLITE_OPEN_URI]
242 set DB [sqlite3_open_v2 file:test.db?vfs=nosuchvfs $flags ""]
243 set errmsg [sqlite3_errmsg $DB]
246 } {no such vfs: nosuchvfs}
249 # EVIDENCE-OF: R-44013-13102 The mode parameter may be set to either
250 # "ro", "rw", "rwc", or "memory". Attempting to set it to any other
255 foreach {tn uri error} "
256 1 {file:test.db?mode=ro} {not an error}
257 2 {file:test.db?mode=rw} {not an error}
258 3 {file:test.db?mode=rwc} {not an error}
259 4 {file:test.db?mode=Ro} {no such access mode: Ro}
260 5 {file:test.db?mode=Rw} {no such access mode: Rw}
261 6 {file:test.db?mode=Rwc} {no such access mode: Rwc}
262 7 {file:test.db?mode=memory} {not an error}
263 8 {file:test.db?mode=MEMORY} {no such access mode: MEMORY}
265 do_test 7.$tn { open_uri_error $uri } $error
269 # EVIDENCE-OF: R-43036-46756 If "ro" is specified, then the database is
270 # opened for read-only access, just as if the SQLITE_OPEN_READONLY flag
271 # had been set in the third argument to sqlite3_open_v2().
273 # EVIDENCE-OF: R-40137-26050 If the mode option is set to "rw", then the
274 # database is opened for read-write (but not create) access, as if
275 # SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had been set.
277 # EVIDENCE-OF: R-26845-32976 Value "rwc" is equivalent to setting both
278 # SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE.
280 foreach {tn uri read write create} {
281 1 {file:test.db?mode=ro} 1 0 0
282 2 {file:test.db?mode=rw} 1 1 0
283 3 {file:test.db?mode=rwc} 1 1 1
285 set RES(c,0) {1 {unable to open database file}}
287 set RES(w,0) {1 {attempt to write a readonly database}}
289 set RES(r,0) {1 {this never happens}}
290 set RES(r,1) {0 {a b}}
292 # Test CREATE access:
294 do_test 8.$tn.c { list [catch { sqlite3 db $uri } msg] $msg } $RES(c,$create)
298 db eval { CREATE TABLE t1(a, b) ; INSERT INTO t1 VALUES('a', 'b') ;}
304 catchsql { SELECT * FROM t1 }
310 catchsql { INSERT INTO t1 VALUES(1, 2) }
316 # EVIDENCE-OF: R-20590-08726 It is an error to specify a value for the
317 # mode parameter that is less restrictive than that specified by the
318 # flags passed in the third parameter to sqlite3_open_v2().
323 foreach {tn uri flags error} {
324 1 {file:test.db?mode=ro} ro {not an error}
325 2 {file:test.db?mode=ro} rw {not an error}
326 3 {file:test.db?mode=ro} rwc {not an error}
328 4 {file:test.db?mode=rw} ro {access mode not allowed: rw}
329 5 {file:test.db?mode=rw} rw {not an error}
330 6 {file:test.db?mode=rw} rwc {not an error}
332 7 {file:test.db?mode=rwc} ro {access mode not allowed: rwc}
333 8 {file:test.db?mode=rwc} rw {access mode not allowed: rwc}
334 9 {file:test.db?mode=rwc} rwc {not an error}
336 set f(ro) [list SQLITE_OPEN_READONLY SQLITE_OPEN_URI]
337 set f(rw) [list SQLITE_OPEN_READWRITE SQLITE_OPEN_URI]
338 set f(rwc) [list SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE SQLITE_OPEN_URI]
340 set DB [sqlite3_open_v2 $uri $f($flags) ""]
341 set e [sqlite3_errmsg $DB]
344 do_test 9.$tn { set e } $error
347 # EVIDENCE-OF: R-23182-54295 The cache parameter may be set to either
348 # "shared" or "private".
351 foreach {tn uri error} "
352 1 {file:test.db?cache=private} {not an error}
353 2 {file:test.db?cache=shared} {not an error}
354 3 {file:test.db?cache=yes} {no such cache mode: yes}
355 4 {file:test.db?cache=} {no such cache mode: }
357 do_test 10.$tn { open_uri_error $uri } $error
360 # EVIDENCE-OF: R-23027-03515 Setting it to "shared" is equivalent to
361 # setting the SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed
362 # to sqlite3_open_v2().
364 # EVIDENCE-OF: R-49793-28525 Setting the cache parameter to "private" is
365 # equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
367 # EVIDENCE-OF: R-31773-41793 If sqlite3_open_v2() is used and the
368 # "cache" parameter is present in a URI filename, its value overrides
369 # any behavior requested by setting SQLITE_OPEN_PRIVATECACHE or
370 # SQLITE_OPEN_SHAREDCACHE flag.
372 set orig [sqlite3_enable_shared_cache]
373 foreach {tn uri flags shared_default isshared} {
374 1.1 "file:test.db" "" 0 0
375 1.2 "file:test.db" "" 1 1
376 1.3 "file:test.db" private 0 0
377 1.4 "file:test.db" private 1 0
378 1.5 "file:test.db" shared 0 1
379 1.6 "file:test.db" shared 1 1
381 2.1 "file:test.db?cache=private" "" 0 0
382 2.2 "file:test.db?cache=private" "" 1 0
383 2.3 "file:test.db?cache=private" private 0 0
384 2.4 "file:test.db?cache=private" private 1 0
385 2.5 "file:test.db?cache=private" shared 0 0
386 2.6 "file:test.db?cache=private" shared 1 0
388 3.1 "file:test.db?cache=shared" "" 0 1
389 3.2 "file:test.db?cache=shared" "" 1 1
390 3.3 "file:test.db?cache=shared" private 0 1
391 3.4 "file:test.db?cache=shared" private 1 1
392 3.5 "file:test.db?cache=shared" shared 0 1
393 3.6 "file:test.db?cache=shared" shared 1 1
396 sqlite3_enable_shared_cache 1
398 sqlite3_enable_shared_cache 0
402 INSERT INTO t1 VALUES('ok');
406 set f() {SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE SQLITE_OPEN_URI}
407 set f(shared) [concat $f() SQLITE_OPEN_SHAREDCACHE]
408 set f(private) [concat $f() SQLITE_OPEN_PRIVATECACHE]
410 sqlite3_enable_shared_cache $shared_default
411 set DB [sqlite3_open_v2 $uri $f($flags) ""]
413 set STMT [sqlite3_prepare $DB "SELECT * FROM t1" -1 dummy]
417 INSERT INTO t1 VALUES('ko');
421 sqlite3_finalize $STMT
423 set RES(0) {not an error}
424 set RES(1) {database table is locked: t1}
426 do_test 11.$tn { sqlite3_errmsg $DB } $RES($isshared)
431 sqlite3_enable_shared_cache $orig
433 # EVIDENCE-OF: R-63472-46769 Specifying an unknown parameter in the
434 # query component of a URI is not an error.
436 do_filepath_test 12.1 {
437 parse_uri file://localhost/test.db?an=unknown¶meter=is&ok=
438 } {/test.db {an unknown parameter is ok {}}}
439 do_filepath_test 12.2 {
440 parse_uri file://localhost/test.db?an&unknown¶meter&is&ok
441 } {/test.db {an {} unknown {} parameter {} is {} ok {}}}
443 # EVIDENCE-OF: R-27458-04043 URI hexadecimal escape sequences (%HH) are
444 # supported within the path and query components of a URI.
446 # EVIDENCE-OF: R-52765-50368 Before the path or query components of a
447 # URI filename are interpreted, they are encoded using UTF-8 and all
448 # hexadecimal escape sequences replaced by a single byte containing the
449 # corresponding octet.
451 # The second of the two statements above is tested by creating a
452 # multi-byte utf-8 character using a sequence of %HH escapes.
454 foreach {tn uri parse} "
455 1 {file:/test.%64%62} {/test.db {}}
456 2 {file:/test.db?%68%65%6c%6c%6f=%77%6f%72%6c%64} {/test.db {hello world}}
457 3 {file:/%C3%BF.db} {/\xFF.db {}}
459 do_filepath_test 13.$tn { parse_uri $uri } $parse