CoolFace
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
e_uri.test466 linesDownload Raw Back to test
1# 2011 May 062#3# The author disclaims copyright to this source code.  In place of4# 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.tcl15set testprefix e_uri16do_not_use_codec17db close18 19proc parse_uri {uri} {20  testvfs tvfs221  testvfs tvfs 22  tvfs filter xOpen23  tvfs script parse_uri_open_cb24 25  set ::uri_open [list]26  set DB [sqlite3_open_v2 $uri {27    SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE SQLITE_OPEN_WAL28    SQLITE_OPEN_EXRESCODE29  } tvfs]30  set fileName [sqlite3_db_filename $DB main]31  sqlite3_close $DB32  forcedelete $fileName33  tvfs delete34  tvfs2 delete35 36  set ::uri_open37}38proc parse_uri_open_cb {method file arglist} {39  set ::uri_open [list $file $arglist]40}41 42proc 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 $DB47  set e48}49 50# EVIDENCE-OF: R-35840-33204 If URI filename interpretation is enabled,51# and the filename argument begins with "file:", then the filename is52# interpreted as a URI.53#54# EVIDENCE-OF: R-27632-24205 URI filename interpretation is enabled if55# the SQLITE_OPEN_URI flag is set in the third argument to56# sqlite3_open_v2(), or if it has been enabled globally using the57# SQLITE_CONFIG_URI option with the sqlite3_config() method or by the58# SQLITE_USE_URI compile-time option.59#60if {$tcl_platform(platform) == "unix"} {61  set flags [list SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE]62 63  # Tests with SQLITE_CONFIG_URI configured to false. URI intepretation is64  # only enabled if the SQLITE_OPEN_URI flag is specified.65  sqlite3_shutdown66  sqlite3_config_uri 067  do_test 1.1 {68    forcedelete file:test.db test.db69    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.db274    set STMT [sqlite3_prepare $DB "ATTACH 'file:test.db2' AS aux" -1 dummy]75    sqlite3_step $STMT76    sqlite3_finalize $STMT77    list [file exists file:test.db2] [file exists test.db2]78  } {0 1}79  sqlite3_close $DB80  do_test 1.3 {81    forcedelete file:test.db test.db82    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.db287    set STMT [sqlite3_prepare $DB "ATTACH 'file:test.db2' AS aux" -1 dummy]88    sqlite3_step $STMT89    sqlite3_finalize $STMT90    list [file exists file:test.db2] [file exists test.db2]91  } {1 0}92  sqlite3_close $DB93 94  # Tests with SQLITE_CONFIG_URI configured to true. URI intepretation is95  # enabled with or without SQLITE_OPEN_URI.96  #97  sqlite3_shutdown98  sqlite3_config_uri 199  do_test 1.5 {100    forcedelete file:test.db test.db101    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.db2106    set STMT [sqlite3_prepare $DB "ATTACH 'file:test.db2' AS aux" -1 dummy]107    sqlite3_step $STMT108    sqlite3_finalize $STMT109    list [file exists file:test.db2] [file exists test.db2]110  } {0 1}111  sqlite3_close $DB112  do_test 1.7 {113    forcedelete file:test.db test.db114    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.db2119    set STMT [sqlite3_prepare $DB "ATTACH 'file:test.db2' AS aux" -1 dummy]120    sqlite3_step $STMT121    sqlite3_finalize $STMT122    list [file exists file:test.db2] [file exists test.db2]123  } {0 1}124  sqlite3_close $DB125}126 127# ensure uri processing enabled for the rest of the tests128sqlite3_shutdown129sqlite3_config_uri 1130 131# EVIDENCE-OF: R-06842-00595 If the URI contains an authority, then it132# must be either an empty string or the string "localhost".133#134# EVIDENCE-OF: R-17482-00398 If the authority is not an empty string or135# "localhost", an error is returned to the caller.136#137if {$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 $DB149      set e150    } $error151  }152}153 154# EVIDENCE-OF: R-45981-25528 The fragment component of a URI, if155# present, is ignored.156#157#   It is difficult to test that something is ignored correctly. So these tests158#   just show that adding a fragment does not interfere with the pathname or159#   parameters passed through to the VFS xOpen() methods.160#161foreach {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 } $parse167}168 169# EVIDENCE-OF: R-62557-09390 SQLite uses the path component of the URI170# as the name of the disk file which contains the database.171#172# EVIDENCE-OF: R-28659-11035 If the path begins with a '/' character,173# then it is interpreted as an absolute path.174#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 the177# path is interpreted as a relative path.178#179foreach {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 } $parse187}188 189# EVIDENCE-OF: R-01612-30877 The "vfs" parameter may be used to specify190# the name of a VFS object that provides the operating system interface191# that should be used to access the database file on disk.192#193#   The above is tested by cases 1.* below.194#195# EVIDENCE-OF: R-52293-58497 If this option is set to an empty string196# the default VFS object is used.197#198#   The above is tested by cases 2.* below.199#200# EVIDENCE-OF: R-31855-18665 If sqlite3_open_v2() is used and the vfs201# option is present, then the VFS specified by the option takes202# precedence over the value passed as the fourth parameter to203# sqlite3_open_v2().204#205#   The above is tested by cases 3.* below.206#207proc vfs_open_cb {name args} {208  set ::vfs $name209}210foreach {name default} {vfs1 0 vfs2 0 vfs3 1} {211  testvfs $name -default $default212  $name filter xOpen213  $name script [list vfs_open_cb $name]214}215foreach {tn uri defvfs vfs} {216  1.1    "file:test.db?vfs=vfs1"    ""    vfs1217  1.2    "file:test.db?vfs=vfs2"    ""    vfs2218 219  2.1    "file:test.db"             vfs1  vfs1220  2.2    "file:test.db?vfs="        vfs1  vfs3221 222  3.1    "file:test.db?vfs=vfs1"    vfs2  vfs1223  3.2    "file:test.db?vfs=vfs2"    vfs1  vfs2224  3.3    "file:test.db?xvfs=vfs1"   vfs2  vfs2225  3.4    "file:test.db?xvfs=vfs2"   vfs1  vfs1226} {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 $defvfs231    ]232    set ::vfs233  } $vfs234}235vfs1 delete236vfs2 delete237vfs3 delete238 239# EVIDENCE-OF: R-48365-36308 Specifying an unknown VFS is an error.240#241set flags [list SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE SQLITE_OPEN_URI]242do_test 6.1 {243  set DB [sqlite3_open_v2 file:test.db?vfs=nosuchvfs $flags ""]244  set errmsg [sqlite3_errmsg $DB]245  sqlite3_close $DB246  set errmsg247} {no such vfs: nosuchvfs}248 249 250# EVIDENCE-OF: R-44013-13102 The mode parameter may be set to either251# "ro", "rw", "rwc", or "memory". Attempting to set it to any other252# value is an error253#254sqlite3 db test.db255db close256foreach {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 } $error267}268 269 270# EVIDENCE-OF: R-43036-46756 If "ro" is specified, then the database is271# opened for read-only access, just as if the SQLITE_OPEN_READONLY flag272# had been set in the third argument to sqlite3_open_v2().273#274# EVIDENCE-OF: R-40137-26050 If the mode option is set to "rw", then the275# database is opened for read-write (but not create) access, as if276# SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had been set.277#278# EVIDENCE-OF: R-26845-32976 Value "rwc" is equivalent to setting both279# SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE.280#281foreach {tn uri read write create} {282  1    {file:test.db?mode=ro}     1 0 0283  2    {file:test.db?mode=rw}     1 1 0284  3    {file:test.db?mode=rwc}    1 1 1285} {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}}292 293  # Test CREATE access:294  forcedelete test.db295  do_test 8.$tn.c { list [catch { sqlite3 db $uri } msg] $msg } $RES(c,$create)296  catch { db close }297 298  sqlite3 db test.db299  db eval { CREATE TABLE t1(a, b) ; INSERT INTO t1 VALUES('a', 'b') ;}300  db close301  302  # Test READ access:303  do_test 8.$tn.r { 304    sqlite3 db $uri305    catchsql { SELECT * FROM t1 }306  } $RES(r,$read)307  308  # Test WRITE access:309  do_test 8.$tn.w { 310    sqlite3 db $uri311    catchsql { INSERT INTO t1 VALUES(1, 2) }312  } $RES(w,$write)313 314  catch {db close}315}316 317# EVIDENCE-OF: R-20590-08726 It is an error to specify a value for the318# mode parameter that is less restrictive than that specified by the319# flags passed in the third parameter to sqlite3_open_v2().320#321forcedelete test.db322sqlite3 db test.db323db close324foreach {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}328 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}332 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]340 341  set DB [sqlite3_open_v2 $uri $f($flags) ""]342  set e [sqlite3_errmsg $DB]343  sqlite3_close $DB344 345  do_test 9.$tn { set e } $error346}347 348# EVIDENCE-OF: R-23182-54295 The cache parameter may be set to either349# "shared" or "private".350sqlite3 db test.db351db close352foreach {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 } $error359}360 361ifcapable shared_cache {362# EVIDENCE-OF: R-23027-03515 Setting it to "shared" is equivalent to363# setting the SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed364# to sqlite3_open_v2().365#366# EVIDENCE-OF: R-49793-28525 Setting the cache parameter to "private" is367# equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.368#369# EVIDENCE-OF: R-31773-41793 If sqlite3_open_v2() is used and the370# "cache" parameter is present in a URI filename, its value overrides371# any behavior requested by setting SQLITE_OPEN_PRIVATECACHE or372# SQLITE_OPEN_SHAREDCACHE flag.373#374set orig [sqlite3_enable_shared_cache]375foreach {tn uri flags shared_default isshared} {376  1.1   "file:test.db"                  ""         0    0377  1.2   "file:test.db"                  ""         1    1378  1.3   "file:test.db"                  private    0    0379  1.4   "file:test.db"                  private    1    0380  1.5   "file:test.db"                  shared     0    1381  1.6   "file:test.db"                  shared     1    1382 383  2.1   "file:test.db?cache=private"    ""         0    0384  2.2   "file:test.db?cache=private"    ""         1    0385  2.3   "file:test.db?cache=private"    private    0    0386  2.4   "file:test.db?cache=private"    private    1    0387  2.5   "file:test.db?cache=private"    shared     0    0388  2.6   "file:test.db?cache=private"    shared     1    0389 390  3.1   "file:test.db?cache=shared"     ""         0    1391  3.2   "file:test.db?cache=shared"     ""         1    1392  3.3   "file:test.db?cache=shared"     private    0    1393  3.4   "file:test.db?cache=shared"     private    1    1394  3.5   "file:test.db?cache=shared"     shared     0    1395  3.6   "file:test.db?cache=shared"     shared     1    1396} {397  forcedelete test.db398  sqlite3_enable_shared_cache 1399  sqlite3 db test.db400  sqlite3_enable_shared_cache 0401 402  db eval {403    CREATE TABLE t1(x);404    INSERT INTO t1 VALUES('ok');405  }406 407  unset -nocomplain f408  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]411 412  sqlite3_enable_shared_cache $shared_default413  set DB [sqlite3_open_v2 $uri $f($flags) ""]414 415  set STMT [sqlite3_prepare $DB "SELECT * FROM t1" -1 dummy]416 417  db eval {418    BEGIN;419      INSERT INTO t1 VALUES('ko');420  }421 422  sqlite3_step $STMT423  sqlite3_finalize $STMT424 425  set RES(0) {not an error}426  set RES(1) {database table is locked: t1}427 428  do_test 11.$tn { sqlite3_errmsg $DB } $RES($isshared)429 430  sqlite3_close $DB431  db close432}433sqlite3_enable_shared_cache $orig434}  ;#  End ifcapable shared_chache435 436# EVIDENCE-OF: R-63472-46769 Specifying an unknown parameter in the437# query component of a URI is not an error.438#439do_filepath_test 12.1 {440  parse_uri file://localhost/test.db?an=unknown&parameter=is&ok=441} {/test.db {an unknown parameter is ok {}}}442do_filepath_test 12.2 {443  parse_uri file://localhost/test.db?an&unknown&parameter&is&ok444} {/test.db {an {} unknown {} parameter {} is {} ok {}}}445 446# EVIDENCE-OF: R-27458-04043 URI hexadecimal escape sequences (%HH) are447# supported within the path and query components of a URI.448#449# EVIDENCE-OF: R-52765-50368 Before the path or query components of a450# URI filename are interpreted, they are encoded using UTF-8 and all451# hexadecimal escape sequences replaced by a single byte containing the452# corresponding octet.453#454#   The second of the two statements above is tested by creating a455#   multi-byte utf-8 character using a sequence of %HH escapes.456#457foreach {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 } $parse463}464 465finish_test466