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