danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 1 | # 2007 May 1 |
| 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 | # |
danielk1977 | 32a0d8b | 2007-05-04 19:03:02 +0000 | [diff] [blame^] | 12 | # $Id: incrblob_err.test,v 1.2 2007/05/04 19:03:03 danielk1977 Exp $ |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 13 | # |
| 14 | |
| 15 | set testdir [file dirname $argv0] |
| 16 | source $testdir/tester.tcl |
| 17 | |
danielk1977 | 32a0d8b | 2007-05-04 19:03:02 +0000 | [diff] [blame^] | 18 | ifcapable {!incrblob} { |
| 19 | finish_test |
| 20 | return |
| 21 | } |
| 22 | |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 23 | # Usage: do_malloc_test <test number> <options...> |
| 24 | # |
| 25 | # The first argument, <test number>, is an integer used to name the |
| 26 | # tests executed by this proc. Options are as follows: |
| 27 | # |
| 28 | # -tclprep TCL script to run to prepare test. |
| 29 | # -sqlprep SQL script to run to prepare test. |
| 30 | # -tclbody TCL script to run with malloc failure simulation. |
| 31 | # -sqlbody TCL script to run with malloc failure simulation. |
| 32 | # -cleanup TCL script to run after the test. |
| 33 | # |
| 34 | # This command runs a series of tests to verify SQLite's ability |
| 35 | # to handle an out-of-memory condition gracefully. It is assumed |
| 36 | # that if this condition occurs a malloc() call will return a |
| 37 | # NULL pointer. Linux, for example, doesn't do that by default. See |
| 38 | # the "BUGS" section of malloc(3). |
| 39 | # |
| 40 | # Each iteration of a loop, the TCL commands in any argument passed |
| 41 | # to the -tclbody switch, followed by the SQL commands in any argument |
| 42 | # passed to the -sqlbody switch are executed. Each iteration the |
| 43 | # Nth call to sqliteMalloc() is made to fail, where N is increased |
| 44 | # each time the loop runs starting from 1. When all commands execute |
| 45 | # successfully, the loop ends. |
| 46 | # |
| 47 | proc do_malloc_test {tn args} { |
| 48 | array unset ::mallocopts |
| 49 | array set ::mallocopts $args |
| 50 | |
| 51 | set ::go 1 |
| 52 | for {set ::n 1} {$::go && $::n < 50000} {incr ::n} { |
| 53 | do_test incrblob_err-$tn.$::n { |
| 54 | |
| 55 | # Remove all traces of database files test.db and test2.db from the files |
| 56 | # system. Then open (empty database) "test.db" with the handle [db]. |
| 57 | # |
| 58 | sqlite_malloc_fail 0 |
| 59 | catch {db close} |
| 60 | catch {file delete -force test.db} |
| 61 | catch {file delete -force test.db-journal} |
| 62 | catch {file delete -force test2.db} |
| 63 | catch {file delete -force test2.db-journal} |
| 64 | catch {sqlite3 db test.db} |
| 65 | set ::DB [sqlite3_connection_pointer db] |
| 66 | |
| 67 | # Execute any -tclprep and -sqlprep scripts. |
| 68 | # |
| 69 | if {[info exists ::mallocopts(-tclprep)]} { |
| 70 | eval $::mallocopts(-tclprep) |
| 71 | } |
| 72 | if {[info exists ::mallocopts(-sqlprep)]} { |
| 73 | execsql $::mallocopts(-sqlprep) |
| 74 | } |
| 75 | |
| 76 | # Now set the ${::n}th malloc() to fail and execute the -tclbody and |
| 77 | # -sqlbody scripts. |
| 78 | # |
| 79 | sqlite_malloc_fail $::n |
| 80 | set ::mallocbody {} |
| 81 | if {[info exists ::mallocopts(-tclbody)]} { |
| 82 | append ::mallocbody "$::mallocopts(-tclbody)\n" |
| 83 | } |
| 84 | if {[info exists ::mallocopts(-sqlbody)]} { |
| 85 | append ::mallocbody "db eval {$::mallocopts(-sqlbody)}" |
| 86 | } |
| 87 | set v [catch $::mallocbody msg] |
| 88 | |
| 89 | # If the test fails (if $v!=0) and the database connection actually |
| 90 | # exists, make sure the failure code is SQLITE_NOMEM. |
| 91 | if {$v && [info command db]=="db" && [info exists ::mallocopts(-sqlbody)] |
| 92 | && [db errorcode]!=7} { |
| 93 | set v 999 |
| 94 | } |
| 95 | |
| 96 | set leftover [lindex [sqlite_malloc_stat] 2] |
| 97 | if {$leftover>0} { |
| 98 | if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v Message=$msg"} |
| 99 | set ::go 0 |
| 100 | if {$v} { |
| 101 | puts "\nError message returned: $msg" |
| 102 | } else { |
| 103 | set v {1 1} |
| 104 | } |
| 105 | } else { |
| 106 | set v2 [expr {$msg=="" || $msg=="out of memory"}] |
| 107 | if {!$v2} {puts "\nError message returned: $msg"} |
| 108 | lappend v $v2 |
| 109 | } |
| 110 | } {1 1} |
| 111 | |
| 112 | if {[info exists ::mallocopts(-cleanup)]} { |
| 113 | catch [list uplevel #0 $::mallocopts(-cleanup)] msg |
| 114 | } |
| 115 | } |
| 116 | unset ::mallocopts |
| 117 | } |
| 118 | |
| 119 | set ::fd [open [info script]] |
| 120 | set ::data [read $::fd] |
| 121 | close $::fd |
| 122 | |
| 123 | do_malloc_test 1 -tclprep { |
| 124 | set bytes [file size [info script]] |
| 125 | execsql { |
| 126 | CREATE TABLE blobs(k, v BLOB); |
| 127 | INSERT INTO blobs VALUES(1, zeroblob($::bytes)); |
| 128 | } |
| 129 | } -tclbody { |
| 130 | set ::blob [db incrblob blobs v 1] |
| 131 | set rc [catch {puts -nonewline $::blob $::data}] |
| 132 | if {$rc} { error "out of memory" } |
| 133 | } |
| 134 | |
| 135 | do_malloc_test 2 -tclprep { |
| 136 | execsql { |
| 137 | CREATE TABLE blobs(k, v BLOB); |
| 138 | INSERT INTO blobs VALUES(1, $::data); |
| 139 | } |
| 140 | } -tclbody { |
| 141 | set ::blob [db incrblob blobs v 1] |
| 142 | set rc [catch {set ::r [read $::blob]}] |
| 143 | if {$rc} { |
| 144 | error "out of memory" |
| 145 | } elseif {$::r ne $::data} { |
| 146 | error "Bad data read..." |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | do_malloc_test 3 -tclprep { |
| 151 | execsql { |
| 152 | CREATE TABLE blobs(k, v BLOB); |
| 153 | INSERT INTO blobs VALUES(1, $::data); |
| 154 | } |
| 155 | } -tclbody { |
| 156 | set ::blob [db incrblob blobs v 1] |
| 157 | set rc [catch {set ::r [read $::blob]}] |
| 158 | if {$rc} { |
| 159 | error "out of memory" |
| 160 | } elseif {$::r ne $::data} { |
| 161 | error "Bad data read..." |
| 162 | } |
| 163 | set rc [catch {close $::blob}] |
| 164 | if {$rc} { |
| 165 | error "out of memory" |
| 166 | } |
| 167 | } |
| 168 | sqlite_malloc_fail 0 |
| 169 | |
| 170 | do_ioerr_test incrblob_err-4 -cksum 1 -sqlprep { |
| 171 | CREATE TABLE blobs(k, v BLOB); |
| 172 | INSERT INTO blobs VALUES(1, $::data); |
| 173 | } -tclbody { |
| 174 | set ::blob [db incrblob blobs v 1] |
| 175 | read $::blob |
| 176 | } |
| 177 | |
| 178 | do_ioerr_test incrblob_err-5 -cksum 1 -sqlprep { |
| 179 | CREATE TABLE blobs(k, v BLOB); |
| 180 | INSERT INTO blobs VALUES(1, zeroblob(length(CAST($::data AS BLOB)))); |
| 181 | } -tclbody { |
| 182 | set ::blob [db incrblob blobs v 1] |
| 183 | puts -nonewline $::blob $::data |
| 184 | close $::blob |
| 185 | } |
| 186 | |
| 187 | do_ioerr_test incrblob_err-6 -cksum 1 -sqlprep { |
| 188 | CREATE TABLE blobs(k, v BLOB); |
| 189 | INSERT INTO blobs VALUES(1, $::data || $::data || $::data); |
| 190 | } -tclbody { |
| 191 | set ::blob [db incrblob blobs v 1] |
| 192 | seek $::blob -20 end |
| 193 | puts -nonewline $::blob "12345678900987654321" |
| 194 | close $::blob |
| 195 | } |
| 196 | |
| 197 | finish_test |