OLD | NEW |
| (Empty) |
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 | |
13 set testdir [file dirname $argv0] | |
14 source $testdir/tester.tcl | |
15 set testprefix e_uri | |
16 | |
17 db close | |
18 | |
19 proc 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 } | |
35 proc parse_uri_open_cb {method file arglist} { | |
36 set ::uri_open [list $file $arglist] | |
37 } | |
38 | |
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] | |
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 # | |
57 if {$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 | |
125 sqlite3_shutdown | |
126 sqlite3_config_uri 1 | |
127 | |
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". | |
130 # | |
131 # EVIDENCE-OF: R-17482-00398 If the authority is not an empty string or | |
132 # "localhost", an error is returned to the caller. | |
133 # | |
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} | |
141 " { | |
142 do_test 2.$tn { | |
143 set DB [sqlite3_open_v2 $uri $flags ""] | |
144 set e [sqlite3_errmsg $DB] | |
145 sqlite3_close $DB | |
146 set e | |
147 } $error | |
148 } | |
149 } | |
150 | |
151 # EVIDENCE-OF: R-45981-25528 The fragment component of a URI, if | |
152 # present, is ignored. | |
153 # | |
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. | |
157 # | |
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}} | |
162 " { | |
163 do_filepath_test 3.$tn { parse_uri $uri } $parse | |
164 } | |
165 | |
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. | |
168 # | |
169 # EVIDENCE-OF: R-28659-11035 If the path begins with a '/' character, | |
170 # then it is interpreted as an absolute path. | |
171 # | |
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. | |
175 # | |
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 {}} | |
182 " { | |
183 do_filepath_test 4.$tn { parse_uri $uri } $parse | |
184 } | |
185 | |
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. | |
189 # | |
190 # The above is tested by cases 1.* below. | |
191 # | |
192 # EVIDENCE-OF: R-52293-58497 If this option is set to an empty string | |
193 # the default VFS object is used. | |
194 # | |
195 # The above is tested by cases 2.* below. | |
196 # | |
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 | |
200 # sqlite3_open_v2(). | |
201 # | |
202 # The above is tested by cases 3.* below. | |
203 # | |
204 proc vfs_open_cb {name args} { | |
205 set ::vfs $name | |
206 } | |
207 foreach {name default} {vfs1 0 vfs2 0 vfs3 1} { | |
208 testvfs $name -default $default | |
209 $name filter xOpen | |
210 $name script [list vfs_open_cb $name] | |
211 } | |
212 foreach {tn uri defvfs vfs} { | |
213 1.1 "file:test.db?vfs=vfs1" "" vfs1 | |
214 1.2 "file:test.db?vfs=vfs2" "" vfs2 | |
215 | |
216 2.1 "file:test.db" vfs1 vfs1 | |
217 2.2 "file:test.db?vfs=" vfs1 vfs3 | |
218 | |
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 | |
223 } { | |
224 do_test 5.$tn { | |
225 set flags [list SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE SQLITE_OPEN_URI] | |
226 sqlite3_close [ | |
227 sqlite3_open_v2 $uri $flags $defvfs | |
228 ] | |
229 set ::vfs | |
230 } $vfs | |
231 } | |
232 vfs1 delete | |
233 vfs2 delete | |
234 vfs3 delete | |
235 | |
236 # EVIDENCE-OF: R-48365-36308 Specifying an unknown VFS is an error. | |
237 # | |
238 set flags [list SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE SQLITE_OPEN_URI] | |
239 do_test 6.1 { | |
240 set DB [sqlite3_open_v2 file:test.db?vfs=nosuchvfs $flags ""] | |
241 set errmsg [sqlite3_errmsg $DB] | |
242 sqlite3_close $DB | |
243 set errmsg | |
244 } {no such vfs: nosuchvfs} | |
245 | |
246 | |
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 | |
249 # value is an error | |
250 # | |
251 sqlite3 db test.db | |
252 db close | |
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} | |
262 " { | |
263 do_test 7.$tn { open_uri_error $uri } $error | |
264 } | |
265 | |
266 | |
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(). | |
270 # | |
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. | |
274 # | |
275 # EVIDENCE-OF: R-26845-32976 Value "rwc" is equivalent to setting both | |
276 # SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE. | |
277 # | |
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 | |
282 } { | |
283 set RES(c,0) {1 {unable to open database file}} | |
284 set RES(c,1) {0 {}} | |
285 set RES(w,0) {1 {attempt to write a readonly database}} | |
286 set RES(w,1) {0 {}} | |
287 set RES(r,0) {1 {this never happens}} | |
288 set RES(r,1) {0 {a b}} | |
289 | |
290 # Test CREATE access: | |
291 forcedelete test.db | |
292 do_test 8.$tn.c { list [catch { sqlite3 db $uri } msg] $msg } $RES(c,$create) | |
293 catch { db close } | |
294 | |
295 sqlite3 db test.db | |
296 db eval { CREATE TABLE t1(a, b) ; INSERT INTO t1 VALUES('a', 'b') ;} | |
297 db close | |
298 | |
299 # Test READ access: | |
300 do_test 8.$tn.r { | |
301 sqlite3 db $uri | |
302 catchsql { SELECT * FROM t1 } | |
303 } $RES(r,$read) | |
304 | |
305 # Test WRITE access: | |
306 do_test 8.$tn.w { | |
307 sqlite3 db $uri | |
308 catchsql { INSERT INTO t1 VALUES(1, 2) } | |
309 } $RES(w,$write) | |
310 | |
311 catch {db close} | |
312 } | |
313 | |
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(). | |
317 # | |
318 forcedelete test.db | |
319 sqlite3 db test.db | |
320 db close | |
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} | |
325 | |
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} | |
329 | |
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} | |
333 } { | |
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] | |
337 | |
338 set DB [sqlite3_open_v2 $uri $f($flags) ""] | |
339 set e [sqlite3_errmsg $DB] | |
340 sqlite3_close $DB | |
341 | |
342 do_test 9.$tn { set e } $error | |
343 } | |
344 | |
345 # EVIDENCE-OF: R-23182-54295 The cache parameter may be set to either | |
346 # "shared" or "private". | |
347 sqlite3 db test.db | |
348 db close | |
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: } | |
354 " { | |
355 do_test 10.$tn { open_uri_error $uri } $error | |
356 } | |
357 | |
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(). | |
361 # | |
362 # EVIDENCE-OF: R-49793-28525 Setting the cache parameter to "private" is | |
363 # equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit. | |
364 # | |
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. | |
369 # | |
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 | |
378 | |
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 | |
385 | |
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 | |
392 } { | |
393 forcedelete test.db | |
394 sqlite3_enable_shared_cache 1 | |
395 sqlite3 db test.db | |
396 sqlite3_enable_shared_cache 0 | |
397 | |
398 db eval { | |
399 CREATE TABLE t1(x); | |
400 INSERT INTO t1 VALUES('ok'); | |
401 } | |
402 | |
403 unset -nocomplain f | |
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] | |
407 | |
408 sqlite3_enable_shared_cache $shared_default | |
409 set DB [sqlite3_open_v2 $uri $f($flags) ""] | |
410 | |
411 set STMT [sqlite3_prepare $DB "SELECT * FROM t1" -1 dummy] | |
412 | |
413 db eval { | |
414 BEGIN; | |
415 INSERT INTO t1 VALUES('ko'); | |
416 } | |
417 | |
418 sqlite3_step $STMT | |
419 sqlite3_finalize $STMT | |
420 | |
421 set RES(0) {not an error} | |
422 set RES(1) {database table is locked: t1} | |
423 | |
424 do_test 11.$tn { sqlite3_errmsg $DB } $RES($isshared) | |
425 | |
426 sqlite3_close $DB | |
427 db close | |
428 } | |
429 sqlite3_enable_shared_cache $orig | |
430 | |
431 # EVIDENCE-OF: R-63472-46769 Specifying an unknown parameter in the | |
432 # query component of a URI is not an error. | |
433 # | |
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 {}}} | |
440 | |
441 # EVIDENCE-OF: R-27458-04043 URI hexadecimal escape sequences (%HH) are | |
442 # supported within the path and query components of a URI. | |
443 # | |
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. | |
448 # | |
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. | |
451 # | |
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 {}} | |
456 " { | |
457 do_filepath_test 13.$tn { parse_uri $uri } $parse | |
458 } | |
459 | |
460 finish_test | |
OLD | NEW |