1
0

uri.test 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. # 2011 April 22
  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. # Test organization:
  15. #
  16. # 1.*: That file names are correctly extracted from URIs.
  17. # 2.*: That URI options (query parameters) are correctly extracted from URIs.
  18. # 3.*: That specifying an unknown VFS causes an error.
  19. # 4.*: Tests for specifying other options (other than "vfs").
  20. # 5.*: Test using a different VFS with an attached database.
  21. # 6.*: Test that authorities other than "" and localhost cause errors.
  22. # 7.*: Test that a read-write db can be attached to a read-only connection.
  23. #
  24. set testprefix uri
  25. db close
  26. sqlite3_shutdown
  27. sqlite3_config_uri 1
  28. #-------------------------------------------------------------------------
  29. # Test that file names are correctly extracted from URIs.
  30. #
  31. foreach {tn uri file} {
  32. 1 test.db test.db
  33. 2 file:test.db test.db
  34. 3 file://PWD/test.db test.db
  35. 4 file:PWD/test.db test.db
  36. 5 file:test.db?mork=1 test.db
  37. 6 file:test.db?mork=1&tonglor=2 test.db
  38. 7 file:test.db?mork=1#boris test.db
  39. 8 file:test.db#boris test.db
  40. 9 test.db#boris test.db#boris
  41. 10 file:test%2Edb test.db
  42. 11 file file
  43. 12 http:test.db http:test.db
  44. 13 file:test.db%00extra test.db
  45. 14 file:testdb%00.db%00extra testdb
  46. 15 test.db?mork=1#boris test.db?mork=1#boris
  47. 16 file://localhostPWD/test.db%3Fhello test.db?hello
  48. } {
  49. ifcapable !curdir { if {$tn==3} break }
  50. if {$tcl_platform(platform)=="windows"} {
  51. #
  52. # NOTE: Due to limits on legal characters for file names imposed by
  53. # Windows, we must skip the final two tests here (i.e. the
  54. # question mark is illegal in a file name on Windows).
  55. #
  56. if {$tn>14} break
  57. #
  58. # NOTE: On Windows, we need to account for the fact that the current
  59. # directory does not start with a forward slash.
  60. #
  61. set uri [string map [list PWD/ /[test_pwd /]] $uri]
  62. } else {
  63. set uri [string map [list PWD/ [test_pwd /]] $uri]
  64. }
  65. if {[file isdir $file]} {error "$file is a directory"}
  66. forcedelete $file
  67. do_test 1.$tn.1 { file exists $file } 0
  68. set DB [sqlite3_open $uri]
  69. do_test 1.$tn.2 { file exists $file } 1
  70. sqlite3_close $DB
  71. forcedelete $file
  72. do_test 1.$tn.3 { file exists $file } 0
  73. sqlite3 db xxx.db
  74. catchsql { ATTACH $uri AS aux }
  75. do_test 1.$tn.4 { file exists $file } 1
  76. db close
  77. }
  78. #-------------------------------------------------------------------------
  79. # Test that URI query parameters are passed through to the VFS layer
  80. # correctly.
  81. #
  82. testvfs tvfs2
  83. testvfs tvfs -default 1
  84. tvfs filter xOpen
  85. tvfs script open_method
  86. proc open_method {method file arglist} {
  87. set ::arglist $arglist
  88. }
  89. foreach {tn uri kvlist} {
  90. 1 file:test.db?hello=world {hello world}
  91. 2 file:test.db?hello&world {hello {} world {}}
  92. 3 file:test.db?hello=1&world=2&vfs=tvfs {hello 1 world 2 vfs tvfs}
  93. 4 file:test.db?hello=1&world=2&vfs=tvfs2 {}
  94. 5 file:test.db?%68%65%6C%6C%6F=%77%6F%72%6C%64 {hello world}
  95. 6 file:testdb%00.db?hello%00extra=world%00ex {hello world}
  96. 7 file:testdb%00.db?hello%00=world%00 {hello world}
  97. 8 file:testdb%00.db?=world&xyz=abc {xyz abc}
  98. 9 file:test.db?%00hello=world&xyz=abc {xyz abc}
  99. 10 file:test.db?hello=%00world&xyz= {hello {} xyz {}}
  100. 11 file:test.db?=#ravada {}
  101. 12 file:test.db?&&&&&&&&hello=world&&&&&&& {hello world}
  102. 13 test.db?&&&&&&&&hello=world&&&&&&& {}
  103. 14 http:test.db?hello&world {}
  104. } {
  105. if {$tcl_platform(platform) == "windows" && $tn>12} {
  106. continue
  107. }
  108. set ::arglist ""
  109. set DB [sqlite3_open $uri]
  110. do_test 2.$tn.1 { set ::arglist } $kvlist
  111. sqlite3_close $DB
  112. sqlite3 db xxx.db
  113. set ::arglist ""
  114. execsql { ATTACH $uri AS aux }
  115. do_test 2.$tn.2 { set ::arglist } $kvlist
  116. db close
  117. }
  118. tvfs delete
  119. tvfs2 delete
  120. #-------------------------------------------------------------------------
  121. # Test that specifying a non-existent VFS raises an error.
  122. #
  123. do_test 3.1 {
  124. list [catch { sqlite3 db "file:test.db?vfs=nosuchvfs" } msg] $msg
  125. } {1 {no such vfs: nosuchvfs}}
  126. #-------------------------------------------------------------------------
  127. # Test some of the other options (other than "vfs").
  128. #
  129. foreach {tn mode create_ok write_ok readonly_ok} {
  130. 1 ro 0 0 1
  131. 2 rw 0 1 0
  132. 3 rwc 1 1 0
  133. } {
  134. catch { db close }
  135. forcedelete test.db
  136. set A(1) {0 {}}
  137. set A(0) {1 {unable to open database file}}
  138. do_test 4.1.$tn.1 {
  139. list [catch {sqlite3 db "file:test.db?mode=$mode"} msg] $msg
  140. } $A($create_ok)
  141. catch { db close }
  142. forcedelete test.db
  143. sqlite3 db test.db
  144. db eval { CREATE TABLE t1(a, b) }
  145. db close
  146. set A(1) {0 {}}
  147. set A(0) {1 {attempt to write a readonly database}}
  148. do_test 4.1.$tn.2 {
  149. sqlite3 db "file:test.db?mode=$mode"
  150. catchsql { INSERT INTO t1 VALUES(1, 2) }
  151. } $A($write_ok)
  152. set A(1) {0 {}}
  153. set A(0) [list 1 "access mode not allowed: $mode"]
  154. do_test 4.1.$tn.3 {
  155. list [catch {sqlite3 db "file:test.db?mode=$mode" -readonly 1} msg] $msg
  156. } $A($readonly_ok)
  157. }
  158. set orig [sqlite3_enable_shared_cache]
  159. foreach {tn options sc_default is_shared} {
  160. 1 "" 1 1
  161. 2 "cache=private" 1 0
  162. 3 "cache=shared" 1 1
  163. 4 "" 0 0
  164. 5 "cache=private" 0 0
  165. 6 "cache=shared" 0 1
  166. } {
  167. catch { db close }
  168. forcedelete test.db
  169. sqlite3_enable_shared_cache 1
  170. sqlite3 db2 test.db
  171. db2 eval {CREATE TABLE t1(a, b)}
  172. sqlite3_enable_shared_cache $sc_default
  173. sqlite3 db "file:test.db?$options"
  174. db eval {SELECT * FROM t1}
  175. set A(1) {1 {database table is locked: t1}}
  176. set A(0) {0 {}}
  177. do_test 4.2.$tn {
  178. db2 eval {BEGIN; INSERT INTO t1 VALUES(1, 2);}
  179. catchsql { SELECT * FROM t1 }
  180. } $A($is_shared)
  181. db2 close
  182. }
  183. do_test 4.3.1 {
  184. list [catch {sqlite3 db "file:test.db?mode=rc"} msg] $msg
  185. } {1 {no such access mode: rc}}
  186. do_test 4.3.2 {
  187. list [catch {sqlite3 db "file:test.db?cache=public"} msg] $msg
  188. } {1 {no such cache mode: public}}
  189. #-------------------------------------------------------------------------
  190. # Test that things work if an ATTACHed database uses a different VFS than
  191. # the main database. The important point is that for all operations
  192. # involving the ATTACHed database, the correct versions of the following
  193. # VFS are used for all operations involving the attached database.
  194. #
  195. # xOpen
  196. # xDelete
  197. # xAccess
  198. # xFullPathname
  199. #
  200. # This block of code creates two VFS - "tvfs1" and "tvfs2". Each time one
  201. # of the above methods is called using "tvfs1", global variable ::T1(X) is
  202. # set, where X is the file-name the method is called on. Calls to the above
  203. # methods using "tvfs2" set entries in the global T2 array.
  204. #
  205. ifcapable wal {
  206. testvfs tvfs1
  207. tvfs1 filter {xOpen xDelete xAccess xFullPathname}
  208. tvfs1 script tvfs1_callback
  209. proc tvfs1_callback {method filename args} {
  210. set ::T1([file tail $filename]) 1
  211. }
  212. testvfs tvfs2
  213. tvfs2 filter {xOpen xDelete xAccess xFullPathname}
  214. tvfs2 script tvfs2_callback
  215. proc tvfs2_callback {method filename args} {
  216. set ::T2([file tail $filename]) 1
  217. }
  218. catch {db close}
  219. eval forcedelete [glob test.db*]
  220. do_test 5.1.1 {
  221. sqlite3 db file:test.db1?vfs=tvfs1
  222. execsql {
  223. ATTACH 'file:test.db2?vfs=tvfs2' AS aux;
  224. PRAGMA main.journal_mode = PERSIST;
  225. PRAGMA aux.journal_mode = PERSIST;
  226. CREATE TABLE t1(a, b);
  227. CREATE TABLE aux.t2(a, b);
  228. PRAGMA main.journal_mode = WAL;
  229. PRAGMA aux.journal_mode = WAL;
  230. INSERT INTO t1 VALUES('x', 'y');
  231. INSERT INTO t2 VALUES('x', 'y');
  232. }
  233. lsort [array names ::T1]
  234. } {test.db1 test.db1-journal test.db1-wal}
  235. do_test 5.1.2 {
  236. lsort [array names ::T2]
  237. } {test.db2 test.db2-journal test.db2-wal}
  238. db close
  239. tvfs1 delete
  240. tvfs2 delete
  241. }
  242. #-------------------------------------------------------------------------
  243. # Check that only "" and "localhost" are acceptable as authorities.
  244. #
  245. catch {db close}
  246. foreach {tn uri res} {
  247. 1 "file://localhost/PWD/test.db" {not an error}
  248. 2 "file:///PWD/test.db" {not an error}
  249. 3 "file:/PWD/test.db" {not an error}
  250. 4 "file://l%6Fcalhost/PWD/test.db" {invalid uri authority: l%6Fcalhost}
  251. 5 "file://lbcalhost/PWD/test.db" {invalid uri authority: lbcalhost}
  252. 6 "file://x/PWD/test.db" {invalid uri authority: x}
  253. } {
  254. if {$tcl_platform(platform)=="windows"} {
  255. set uri [string map [list PWD [string range [get_pwd] 3 end]] $uri]
  256. } else {
  257. set uri [string map [list PWD [string range [get_pwd] 1 end]] $uri]
  258. }
  259. do_test 6.$tn {
  260. set DB [sqlite3_open $uri]
  261. sqlite3_errmsg $DB
  262. } $res
  263. catch { sqlite3_close $DB }
  264. }
  265. forcedelete test.db test.db2
  266. do_test 7.1 {
  267. sqlite3 db test.db
  268. execsql {
  269. CREATE TABLE t1(a, b);
  270. INSERT INTO t1 VALUES(1, 2);
  271. ATTACH 'test.db2' AS aux;
  272. CREATE TABLE aux.t2(a, b);
  273. INSERT INTO t1 VALUES('a', 'b');
  274. }
  275. db close
  276. } {}
  277. do_test 7.2 {
  278. sqlite3 db file:test.db?mode=ro
  279. execsql { ATTACH 'file:test.db2?mode=rw' AS aux }
  280. } {}
  281. do_execsql_test 7.3 {
  282. INSERT INTO t2 VALUES('c', 'd')
  283. } {}
  284. do_catchsql_test 7.4 {
  285. INSERT INTO t1 VALUES(3, 4)
  286. } {1 {attempt to write a readonly database}}
  287. finish_test