xref: /sqlite-3.40.0/test/e_uri.test (revision a3fdec71)
1# 2011 May 06
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
15set testprefix e_uri
16
17db close
18
19proc parse_uri {uri} {
20  testvfs tvfs2
21  testvfs tvfs
22  tvfs filter xOpen
23  tvfs script parse_uri_open_cb
24
25  set ::uri_open [list]
26  set DB [sqlite3_open_v2 $uri {
27    SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE SQLITE_OPEN_WAL
28  } tvfs]
29  sqlite3_close $DB
30  tvfs delete
31  tvfs2 delete
32
33  set ::uri_open
34}
35proc parse_uri_open_cb {method file arglist} {
36  set ::uri_open [list $file $arglist]
37}
38
39proc 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]
43  sqlite3_close $DB
44  set e
45}
46
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.
50#
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.
56#
57if {$tcl_platform(platform) == "unix"} {
58  set flags [list SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE]
59
60  # Tests with SQLITE_CONFIG_URI configured to false. URI intepretation is
61  # only enabled if the SQLITE_OPEN_URI flag is specified.
62  sqlite3_shutdown
63  sqlite3_config_uri 0
64  do_test 1.1 {
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]
68  } {0 1}
69  do_test 1.2 {
70    forcedelete file:test.db2 test.db2
71    set STMT [sqlite3_prepare $DB "ATTACH 'file:test.db2' AS aux" -1 dummy]
72    sqlite3_step $STMT
73    sqlite3_finalize $STMT
74    list [file exists file:test.db2] [file exists test.db2]
75  } {0 1}
76  sqlite3_close $DB
77  do_test 1.3 {
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]
81  } {1 0}
82  do_test 1.4 {
83    forcedelete file:test.db2 test.db2
84    set STMT [sqlite3_prepare $DB "ATTACH 'file:test.db2' AS aux" -1 dummy]
85    sqlite3_step $STMT
86    sqlite3_finalize $STMT
87    list [file exists file:test.db2] [file exists test.db2]
88  } {1 0}
89  sqlite3_close $DB
90
91  # Tests with SQLITE_CONFIG_URI configured to true. URI intepretation is
92  # enabled with or without SQLITE_OPEN_URI.
93  #
94  sqlite3_shutdown
95  sqlite3_config_uri 1
96  do_test 1.5 {
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]
100  } {0 1}
101  do_test 1.6 {
102    forcedelete file:test.db2 test.db2
103    set STMT [sqlite3_prepare $DB "ATTACH 'file:test.db2' AS aux" -1 dummy]
104    sqlite3_step $STMT
105    sqlite3_finalize $STMT
106    list [file exists file:test.db2] [file exists test.db2]
107  } {0 1}
108  sqlite3_close $DB
109  do_test 1.7 {
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]
113  } {0 1}
114  do_test 1.8 {
115    forcedelete file:test.db2 test.db2
116    set STMT [sqlite3_prepare $DB "ATTACH 'file:test.db2' AS aux" -1 dummy]
117    sqlite3_step $STMT
118    sqlite3_finalize $STMT
119    list [file exists file:test.db2] [file exists test.db2]
120  } {0 1}
121  sqlite3_close $DB
122}
123
124# ensure uri processing enabled for the rest of the tests
125sqlite3_shutdown
126sqlite3_config_uri 1
127
128# EVIDENCE-OF: R-17482-00398 If the authority is not an empty string or
129# "localhost", an error is returned to the caller.
130#
131if {$tcl_platform(platform) == "unix"} {
132  set flags [list SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE SQLITE_OPEN_URI]
133  foreach {tn uri error} "
134    1  {file://localhost[test_pwd /]test.db}   {not an error}
135    2  {file://[test_pwd /]test.db}            {not an error}
136    3  {file://x[test_pwd /]test.db}           {invalid uri authority: x}
137    4  {file://invalid[test_pwd /]test.db}     {invalid uri authority: invalid}
138  " {
139    do_test 2.$tn {
140      set DB [sqlite3_open_v2 $uri $flags ""]
141      set e [sqlite3_errmsg $DB]
142      sqlite3_close $DB
143      set e
144    } $error
145  }
146}
147
148# EVIDENCE-OF: R-45981-25528 The fragment component of a URI, if
149# present, is ignored.
150#
151#   It is difficult to test that something is ignored correctly. So these tests
152#   just show that adding a fragment does not interfere with the pathname or
153#   parameters passed through to the VFS xOpen() methods.
154#
155foreach {tn uri parse} "
156  1    {file:test.db#abc}      {[test_pwd / {}]test.db {}}
157  2    {file:test.db?a=b#abc}  {[test_pwd / {}]test.db {a b}}
158  3    {file:test.db?a=b#?c=d} {[test_pwd / {}]test.db {a b}}
159" {
160  do_filepath_test 3.$tn { parse_uri $uri } $parse
161}
162
163# EVIDENCE-OF: R-62557-09390 SQLite uses the path component of the URI
164# as the name of the disk file which contains the database.
165#
166# EVIDENCE-OF: R-28659-11035 If the path begins with a '/' character,
167# then it is interpreted as an absolute path.
168#
169# EVIDENCE-OF: R-46234-61323 If the path does not begin with a '/'
170# (meaning that the authority section is omitted from the URI) then the
171# path is interpreted as a relative path.
172#
173foreach {tn uri parse} "
174  1    {file:test.db}             {[test_pwd / {}]test.db {}}
175  2    {file:/test.db}            {/test.db {}}
176  3    {file:///test.db}          {/test.db {}}
177  4    {file://localhost/test.db} {/test.db {}}
178  5    {file:/a/b/c/test.db}      {/a/b/c/test.db {}}
179" {
180  do_filepath_test 4.$tn { parse_uri $uri } $parse
181}
182
183# EVIDENCE-OF: R-01612-30877 The "vfs" parameter may be used to specify
184# the name of a VFS object that provides the operating system interface
185# that should be used to access the database file on disk.
186#
187#   The above is tested by cases 1.* below.
188#
189# EVIDENCE-OF: R-52293-58497 If this option is set to an empty string
190# the default VFS object is used.
191#
192#   The above is tested by cases 2.* below.
193#
194# EVIDENCE-OF: R-31855-18665 If sqlite3_open_v2() is used and the vfs
195# option is present, then the VFS specified by the option takes
196# precedence over the value passed as the fourth parameter to
197# sqlite3_open_v2().
198#
199#   The above is tested by cases 3.* below.
200#
201proc vfs_open_cb {name args} {
202  set ::vfs $name
203}
204foreach {name default} {vfs1 0 vfs2 0 vfs3 1} {
205  testvfs $name -default $default
206  $name filter xOpen
207  $name script [list vfs_open_cb $name]
208}
209foreach {tn uri defvfs vfs} {
210  1.1    "file:test.db?vfs=vfs1"    ""    vfs1
211  1.2    "file:test.db?vfs=vfs2"    ""    vfs2
212
213  2.1    "file:test.db"             vfs1  vfs1
214  2.2    "file:test.db?vfs="        vfs1  vfs3
215
216  3.1    "file:test.db?vfs=vfs1"    vfs2  vfs1
217  3.2    "file:test.db?vfs=vfs2"    vfs1  vfs2
218  3.3    "file:test.db?xvfs=vfs1"   vfs2  vfs2
219  3.4    "file:test.db?xvfs=vfs2"   vfs1  vfs1
220} {
221  do_test 5.$tn {
222    set flags [list SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE SQLITE_OPEN_URI]
223    sqlite3_close [
224      sqlite3_open_v2 $uri $flags $defvfs
225    ]
226    set ::vfs
227  } $vfs
228}
229vfs1 delete
230vfs2 delete
231vfs3 delete
232
233# EVIDENCE-OF: R-48365-36308 Specifying an unknown VFS is an error.
234#
235set flags [list SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE SQLITE_OPEN_URI]
236do_test 6.1 {
237  set DB [sqlite3_open_v2 file:test.db?vfs=nosuchvfs $flags ""]
238  set errmsg [sqlite3_errmsg $DB]
239  sqlite3_close $DB
240  set errmsg
241} {no such vfs: nosuchvfs}
242
243
244# EVIDENCE-OF: R-44013-13102 The mode parameter may be set to either
245# "ro", "rw", "rwc", or "memory". Attempting to set it to any other
246# value is an error
247#
248sqlite3 db test.db
249db close
250foreach {tn uri error} "
251  1    {file:test.db?mode=ro}    {not an error}
252  2    {file:test.db?mode=rw}    {not an error}
253  3    {file:test.db?mode=rwc}   {not an error}
254  4    {file:test.db?mode=Ro}    {no such access mode: Ro}
255  5    {file:test.db?mode=Rw}    {no such access mode: Rw}
256  6    {file:test.db?mode=Rwc}   {no such access mode: Rwc}
257  7    {file:test.db?mode=memory} {not an error}
258  8    {file:test.db?mode=MEMORY} {no such access mode: MEMORY}
259" {
260  do_test 7.$tn { open_uri_error $uri } $error
261}
262
263
264# EVIDENCE-OF: R-43036-46756 If "ro" is specified, then the database is
265# opened for read-only access, just as if the SQLITE_OPEN_READONLY flag
266# had been set in the third argument to sqlite3_open_v2().
267#
268# EVIDENCE-OF: R-40137-26050 If the mode option is set to "rw", then the
269# database is opened for read-write (but not create) access, as if
270# SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had been set.
271#
272# EVIDENCE-OF: R-26845-32976 Value "rwc" is equivalent to setting both
273# SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE.
274#
275foreach {tn uri read write create} {
276  1    {file:test.db?mode=ro}     1 0 0
277  2    {file:test.db?mode=rw}     1 1 0
278  3    {file:test.db?mode=rwc}    1 1 1
279} {
280  set RES(c,0) {1 {unable to open database file}}
281  set RES(c,1) {0 {}}
282  set RES(w,0) {1 {attempt to write a readonly database}}
283  set RES(w,1) {0 {}}
284  set RES(r,0) {1 {this never happens}}
285  set RES(r,1) {0 {a b}}
286
287  # Test CREATE access:
288  forcedelete test.db
289  do_test 8.$tn.c { list [catch { sqlite3 db $uri } msg] $msg } $RES(c,$create)
290  catch { db close }
291
292  sqlite3 db test.db
293  db eval { CREATE TABLE t1(a, b) ; INSERT INTO t1 VALUES('a', 'b') ;}
294  db close
295
296  # Test READ access:
297  do_test 8.$tn.r {
298    sqlite3 db $uri
299    catchsql { SELECT * FROM t1 }
300  } $RES(r,$read)
301
302  # Test WRITE access:
303  do_test 8.$tn.w {
304    sqlite3 db $uri
305    catchsql { INSERT INTO t1 VALUES(1, 2) }
306  } $RES(w,$write)
307
308  catch {db close}
309}
310
311# EVIDENCE-OF: R-20590-08726 It is an error to specify a value for the
312# mode parameter that is less restrictive than that specified by the
313# flags passed in the third parameter to sqlite3_open_v2().
314#
315forcedelete test.db
316sqlite3 db test.db
317db close
318foreach {tn uri flags error} {
319  1   {file:test.db?mode=ro}   ro    {not an error}
320  2   {file:test.db?mode=ro}   rw    {not an error}
321  3   {file:test.db?mode=ro}   rwc   {not an error}
322
323  4   {file:test.db?mode=rw}   ro    {access mode not allowed: rw}
324  5   {file:test.db?mode=rw}   rw    {not an error}
325  6   {file:test.db?mode=rw}   rwc   {not an error}
326
327  7   {file:test.db?mode=rwc}  ro    {access mode not allowed: rwc}
328  8   {file:test.db?mode=rwc}  rw    {access mode not allowed: rwc}
329  9   {file:test.db?mode=rwc}  rwc   {not an error}
330} {
331  set f(ro)  [list SQLITE_OPEN_READONLY SQLITE_OPEN_URI]
332  set f(rw)  [list SQLITE_OPEN_READWRITE SQLITE_OPEN_URI]
333  set f(rwc) [list SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE SQLITE_OPEN_URI]
334
335  set DB [sqlite3_open_v2 $uri $f($flags) ""]
336  set e [sqlite3_errmsg $DB]
337  sqlite3_close $DB
338
339  do_test 9.$tn { set e } $error
340}
341
342# EVIDENCE-OF: R-23182-54295 The cache parameter may be set to either
343# "shared" or "private".
344sqlite3 db test.db
345db close
346foreach {tn uri error} "
347  1    {file:test.db?cache=private}    {not an error}
348  2    {file:test.db?cache=shared}     {not an error}
349  3    {file:test.db?cache=yes}        {no such cache mode: yes}
350  4    {file:test.db?cache=}           {no such cache mode: }
351" {
352  do_test 10.$tn { open_uri_error $uri } $error
353}
354
355# EVIDENCE-OF: R-23027-03515 Setting it to "shared" is equivalent to
356# setting the SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed
357# to sqlite3_open_v2().
358#
359# EVIDENCE-OF: R-49793-28525 Setting the cache parameter to "private" is
360# equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
361#
362# EVIDENCE-OF: R-31773-41793 If sqlite3_open_v2() is used and the
363# "cache" parameter is present in a URI filename, its value overrides
364# any behavior requested by setting SQLITE_OPEN_PRIVATECACHE or
365# SQLITE_OPEN_SHAREDCACHE flag.
366#
367set orig [sqlite3_enable_shared_cache]
368foreach {tn uri flags shared_default isshared} {
369  1.1   "file:test.db"                  ""         0    0
370  1.2   "file:test.db"                  ""         1    1
371  1.3   "file:test.db"                  private    0    0
372  1.4   "file:test.db"                  private    1    0
373  1.5   "file:test.db"                  shared     0    1
374  1.6   "file:test.db"                  shared     1    1
375
376  2.1   "file:test.db?cache=private"    ""         0    0
377  2.2   "file:test.db?cache=private"    ""         1    0
378  2.3   "file:test.db?cache=private"    private    0    0
379  2.4   "file:test.db?cache=private"    private    1    0
380  2.5   "file:test.db?cache=private"    shared     0    0
381  2.6   "file:test.db?cache=private"    shared     1    0
382
383  3.1   "file:test.db?cache=shared"     ""         0    1
384  3.2   "file:test.db?cache=shared"     ""         1    1
385  3.3   "file:test.db?cache=shared"     private    0    1
386  3.4   "file:test.db?cache=shared"     private    1    1
387  3.5   "file:test.db?cache=shared"     shared     0    1
388  3.6   "file:test.db?cache=shared"     shared     1    1
389} {
390  forcedelete test.db
391  sqlite3_enable_shared_cache 1
392  sqlite3 db test.db
393  sqlite3_enable_shared_cache 0
394
395  db eval {
396    CREATE TABLE t1(x);
397    INSERT INTO t1 VALUES('ok');
398  }
399
400  unset -nocomplain f
401  set f()        {SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE SQLITE_OPEN_URI}
402  set f(shared)  [concat $f() SQLITE_OPEN_SHAREDCACHE]
403  set f(private) [concat $f() SQLITE_OPEN_PRIVATECACHE]
404
405  sqlite3_enable_shared_cache $shared_default
406  set DB [sqlite3_open_v2 $uri $f($flags) ""]
407
408  set STMT [sqlite3_prepare $DB "SELECT * FROM t1" -1 dummy]
409
410  db eval {
411    BEGIN;
412      INSERT INTO t1 VALUES('ko');
413  }
414
415  sqlite3_step $STMT
416  sqlite3_finalize $STMT
417
418  set RES(0) {not an error}
419  set RES(1) {database table is locked: t1}
420
421  do_test 11.$tn { sqlite3_errmsg $DB } $RES($isshared)
422
423  sqlite3_close $DB
424  db close
425}
426sqlite3_enable_shared_cache $orig
427
428# EVIDENCE-OF: R-63472-46769 Specifying an unknown parameter in the
429# query component of a URI is not an error.
430#
431do_filepath_test 12.1 {
432  parse_uri file://localhost/test.db?an=unknown&parameter=is&ok=
433} {/test.db {an unknown parameter is ok {}}}
434do_filepath_test 12.2 {
435  parse_uri file://localhost/test.db?an&unknown&parameter&is&ok
436} {/test.db {an {} unknown {} parameter {} is {} ok {}}}
437
438# EVIDENCE-OF: R-27458-04043 URI hexadecimal escape sequences (%HH) are
439# supported within the path and query components of a URI.
440#
441# EVIDENCE-OF: R-52765-50368 Before the path or query components of a
442# URI filename are interpreted, they are encoded using UTF-8 and all
443# hexadecimal escape sequences replaced by a single byte containing the
444# corresponding octet.
445#
446#   The second of the two statements above is tested by creating a
447#   multi-byte utf-8 character using a sequence of %HH escapes.
448#
449foreach {tn uri parse} "
450  1  {file:/test.%64%62}                             {/test.db {}}
451  2  {file:/test.db?%68%65%6c%6c%6f=%77%6f%72%6c%64} {/test.db {hello world}}
452  3  {file:/%C3%BF.db}                               {/\xFF.db {}}
453" {
454  do_filepath_test 13.$tn { parse_uri $uri } $parse
455}
456
457finish_test
458