xref: /sqlite-3.40.0/test/oserror.test (revision cc285c5a)
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
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19if {$::tcl_platform(platform)!="unix"} { finish_test ; return }
20set ::testprefix oserror
21
22db close
23sqlite3_shutdown
24test_sqlite3_log xLog
25proc xLog {error_code msg} {
26  if {[string match os_* $msg]} {
27    lappend ::log $msg
28  }
29}
30
31proc 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#
54if {![clang_sanitize_address]} {
55  do_test 1.1.1 {
56    set ::log [list]
57    list [catch {
58      for {set i 0} {$i < 2000} {incr i} { sqlite3 dbh_$i test.db -readonly 1 }
59    } msg] $msg
60  } {1 {unable to open database file}}
61  do_test 1.1.2 {
62    catch { for {set i 0} {$i < 2000} {incr i} { dbh_$i close } }
63  } {1}
64  do_re_test 1.1.3 {
65    lindex $::log 0
66  } {^os_unix.c:\d+: \(\d+\) (open|getcwd)\(.*test.db\) - }
67}
68
69
70# Test a failure in open() due to the path being a directory.
71#
72do_test 1.2.1 {
73  file mkdir dir.db
74  set ::log [list]
75  list [catch { sqlite3 dbh dir.db } msg] $msg
76} {1 {unable to open database file}}
77
78do_re_test 1.2.2 { lindex $::log 0 } {^os_unix.c:\d+: \(\d+\) open\(.*dir.db\) - }
79
80# Test a failure in open() due to the path not existing.
81#
82do_test 1.3.1 {
83  set ::log [list]
84  list [catch { sqlite3 dbh /x/y/z/test.db } msg] $msg
85} {1 {unable to open database file}}
86
87do_re_test 1.3.2 { lindex $::log 0 } {^os_unix.c:\d+: \(\d+\) open\(.*test.db\) - }
88
89# Test a failure in open() due to the path not existing.
90#
91do_test 1.4.1 {
92  set ::log [list]
93  list [catch { sqlite3 dbh /root/test.db } msg] $msg
94} {1 {unable to open database file}}
95
96do_re_test 1.4.2 { lindex $::log 0 } {^os_unix.c:\d*: \(\d+\) open\(.*test.db\) - }
97
98#--------------------------------------------------------------------------
99# Tests oserror-1.* test failures in the unlink() system call.
100#
101ifcapable wal {
102  do_test 2.1.1 {
103    set ::log [list]
104    file mkdir test.db-wal
105    forcedelete test.db
106    list [catch {
107      sqlite3 dbh test.db
108      execsql { SELECT * FROM sqlite_master } dbh
109    } msg] $msg
110  } {1 {disk I/O error}}
111
112  do_re_test 2.1.2 {
113    lindex $::log 0
114  } {^os_unix.c:\d+: \(\d+\) unlink\(.*test.db-wal\) - }
115  do_test 2.1.3 {
116    catch { dbh close }
117    forcedelete test.db-wal
118  } {}
119}
120
121
122test_syscall reset
123sqlite3_shutdown
124test_sqlite3_log
125sqlite3_initialize
126finish_test
127