e_uri.test 15 KB

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