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

     1  # 2011 February 19
     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  # This file implements regression tests for SQLite library.  The
    12  # focus of this file is testing that error messages are logged via the
    13  # sqlite3_log() mechanism when certain errors are encountered in the
    14  # default unix or windows VFS modules.
    15  #
    16  
    17  set testdir [file dirname $argv0]
    18  source $testdir/tester.tcl
    19  if {$::tcl_platform(platform)!="unix"} { finish_test ; return }
    20  set ::testprefix oserror
    21  
    22  db close
    23  sqlite3_shutdown
    24  test_sqlite3_log xLog
    25  proc xLog {error_code msg} {
    26    if {[string match os_* $msg]} {
    27      lappend ::log $msg 
    28    }
    29  }
    30  
    31  proc do_re_test {tn script expression} {
    32    uplevel do_test $tn [list [subst -nocommands {
    33      set res [eval { $script }]
    34      if {[regexp {$expression} [set res]]} {
    35        set {} {$expression}
    36      } else {
    37        set res
    38      }
    39    }]] [list $expression]
    40    
    41  }
    42  
    43  #--------------------------------------------------------------------------
    44  # Tests oserror-1.* test failures in the open() system call.
    45  #
    46  
    47  # Test a failure in open() due to too many files. 
    48  #
    49  # The xOpen() method of the unix VFS calls getcwd() as well as open().
    50  # Although this does not appear to be documented in the man page, on OSX
    51  # a call to getcwd() may fail if there are no free file descriptors. So
    52  # an error may be reported for either open() or getcwd() here.
    53  #
    54  if {![clang_sanitize_address]} {
    55    unset -nocomplain rc
    56    unset -nocomplain nOpen
    57    set nOpen 20000
    58    do_test 1.1.1 {
    59      set ::log [list]
    60      set ::rc [catch {
    61        for {set i 0} {$i < $::nOpen} {incr i} { sqlite3 dbh_$i test.db -readonly 1 }
    62      } msg]
    63      if {$::rc==0} {
    64        # Some system (ex: Debian) are able to create 20000+ file descriptiors
    65        # such systems will not fail here
    66        set x ok
    67      } elseif {$::rc==1 && $msg=="unable to open database file"} {
    68        set x ok
    69      } else {
    70        set x [list $::rc $msg]
    71      }
    72    } {ok}
    73    do_test 1.1.2 {
    74      catch { for {set i 0} {$i < $::nOpen} {incr i} { dbh_$i close } }
    75    } $::rc
    76    if {$rc} {
    77      do_re_test 1.1.3 { 
    78        lindex $::log 0 
    79      } {^os_unix.c:\d+: \(\d+\) (open|getcwd)\(.*test.db\) - }
    80    }
    81  }
    82  
    83  
    84  # Test a failure in open() due to the path being a directory.
    85  #
    86  do_test 1.2.1 {
    87    file mkdir dir.db
    88    set ::log [list]
    89    list [catch { sqlite3 dbh dir.db } msg] $msg
    90  } {1 {unable to open database file}}
    91  
    92  do_re_test 1.2.2 { lindex $::log 0 } {^os_unix.c:\d+: \(\d+\) open\(.*dir.db\) - }
    93  
    94  # Test a failure in open() due to the path not existing.
    95  #
    96  do_test 1.3.1 {
    97    set ::log [list]
    98    list [catch { sqlite3 dbh /x/y/z/test.db } msg] $msg
    99  } {1 {unable to open database file}}
   100  
   101  do_re_test 1.3.2 { lindex $::log 0 } {^os_unix.c:\d+: \(\d+\) open\(.*test.db\) - }
   102  
   103  # Test a failure in open() due to the path not existing.
   104  #
   105  do_test 1.4.1 {
   106    set ::log [list]
   107    list [catch { sqlite3 dbh /root/test.db } msg] $msg
   108  } {1 {unable to open database file}}
   109  
   110  do_re_test 1.4.2 { 
   111    lindex $::log 0
   112  } {^os_unix.c:\d*: \(\d+\) (open|readlink|lstat)\(.*test.db\) - }
   113  
   114  #--------------------------------------------------------------------------
   115  # Tests oserror-1.* test failures in the unlink() system call.
   116  #
   117  ifcapable wal {
   118    do_test 2.1.1 {
   119      set ::log [list]
   120      file mkdir test.db-wal
   121      forcedelete test.db
   122      list [catch {
   123        sqlite3 dbh test.db
   124        execsql { SELECT * FROM sqlite_master } dbh
   125      } msg] $msg
   126    } {1 {disk I/O error}}
   127    
   128    do_re_test 2.1.2 { 
   129      lindex $::log 0 
   130    } {^os_unix.c:\d+: \(\d+\) unlink\(.*test.db-wal\) - }
   131    do_test 2.1.3 { 
   132      catch { dbh close }
   133      forcedelete test.db-wal
   134    } {}
   135  }
   136    
   137  
   138  test_syscall reset
   139  sqlite3_shutdown
   140  test_sqlite3_log 
   141  sqlite3_initialize
   142  finish_test