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