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