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