github.com/jdgcs/sqlite3@v1.12.1-0.20210908114423-bc5f96e4dd51/testdata/tcl/e_uri.test (about)

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