oserror.test 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. if {$::tcl_platform(platform)!="unix"} { finish_test ; return }
  19. set ::testprefix oserror
  20. db close
  21. sqlite3_shutdown
  22. test_sqlite3_log xLog
  23. proc xLog {error_code msg} {
  24. if {[string match os_* $msg]} {
  25. lappend ::log $msg
  26. }
  27. }
  28. proc do_re_test {tn script expression} {
  29. uplevel do_test $tn [list [subst -nocommands {
  30. set res [eval { $script }]
  31. if {[regexp {$expression} [set res]]} {
  32. set {} {$expression}
  33. } else {
  34. set res
  35. }
  36. }]] [list $expression]
  37. }
  38. #--------------------------------------------------------------------------
  39. # Tests oserror-1.* test failures in the open() system call.
  40. #
  41. # Test a failure in open() due to too many files.
  42. #
  43. # The xOpen() method of the unix VFS calls getcwd() as well as open().
  44. # Although this does not appear to be documented in the man page, on OSX
  45. # a call to getcwd() may fail if there are no free file descriptors. So
  46. # an error may be reported for either open() or getcwd() here.
  47. #
  48. puts "Possible valgrind error about invalid file descriptor follows:"
  49. do_test 1.1.1 {
  50. set ::log [list]
  51. list [catch {
  52. for {set i 0} {$i < 2000} {incr i} { sqlite3 dbh_$i test.db -readonly 1 }
  53. } msg] $msg
  54. } {1 {unable to open database file}}
  55. do_test 1.1.2 {
  56. catch { for {set i 0} {$i < 2000} {incr i} { dbh_$i close } }
  57. } {1}
  58. do_re_test 1.1.3 {
  59. lindex $::log 0
  60. } {^os_unix.c:\d+: \(\d+\) (open|getcwd)\(.*test.db\) - }
  61. # Test a failure in open() due to the path being a directory.
  62. #
  63. do_test 1.2.1 {
  64. file mkdir dir.db
  65. set ::log [list]
  66. list [catch { sqlite3 dbh dir.db } msg] $msg
  67. } {1 {unable to open database file}}
  68. do_re_test 1.2.2 { lindex $::log 0 } {^os_unix.c:\d+: \(\d+\) open\(.*dir.db\) - }
  69. # Test a failure in open() due to the path not existing.
  70. #
  71. do_test 1.3.1 {
  72. set ::log [list]
  73. list [catch { sqlite3 dbh /x/y/z/test.db } msg] $msg
  74. } {1 {unable to open database file}}
  75. do_re_test 1.3.2 { lindex $::log 0 } {^os_unix.c:\d+: \(\d+\) open\(.*test.db\) - }
  76. # Test a failure in open() due to the path not existing.
  77. #
  78. do_test 1.4.1 {
  79. set ::log [list]
  80. list [catch { sqlite3 dbh /root/test.db } msg] $msg
  81. } {1 {unable to open database file}}
  82. do_re_test 1.4.2 { lindex $::log 0 } {^os_unix.c:\d*: \(\d+\) open\(.*test.db\) - }
  83. #--------------------------------------------------------------------------
  84. # Tests oserror-1.* test failures in the unlink() system call.
  85. #
  86. ifcapable wal {
  87. do_test 2.1.1 {
  88. set ::log [list]
  89. file mkdir test.db-wal
  90. forcedelete test.db
  91. list [catch {
  92. sqlite3 dbh test.db
  93. execsql { SELECT * FROM sqlite_master } dbh
  94. } msg] $msg
  95. } {1 {disk I/O error}}
  96. do_re_test 2.1.2 {
  97. lindex $::log 0
  98. } {^os_unix.c:\d+: \(\d+\) unlink\(.*test.db-wal\) - }
  99. do_test 2.1.3 {
  100. catch { dbh close }
  101. forcedelete test.db-wal
  102. } {}
  103. }
  104. test_syscall reset
  105. sqlite3_shutdown
  106. test_sqlite3_log
  107. sqlite3_initialize
  108. finish_test