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