blob: e86c4f3661b42e993f02a149338ac117001e16a0 [file] [log] [blame]
drh5a3032b2007-09-03 16:12:09 +00001# 2007 May 05
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# This file contains common code used by many different malloc tests
13# within the test suite.
14#
danielk197798c21902008-09-23 16:41:29 +000015# $Id: malloc_common.tcl,v 1.22 2008/09/23 16:41:30 danielk1977 Exp $
danielk1977c9cf9012007-05-30 10:36:47 +000016
drh5a3032b2007-09-03 16:12:09 +000017# If we did not compile with malloc testing enabled, then do nothing.
18#
drh3088d592008-03-21 16:45:47 +000019ifcapable builtin_test {
drh5efaf072008-03-18 00:07:10 +000020 set MEMDEBUG 1
21} else {
drheee4c8c2008-02-18 22:24:57 +000022 set MEMDEBUG 0
danielk1977369ff422007-09-03 07:31:09 +000023 return 0
danielk1977cdc3a6b2007-08-25 13:09:26 +000024}
25
dan1f55e282010-06-03 09:25:10 +000026# Transient and persistent OOM errors:
27#
28set FAULTSIM(oom-transient) [list \
29 -injectstart {oom_injectstart 0} \
30 -injectstop oom_injectstop \
31 -injecterrlist {{1 {out of memory}}} \
32]
33set FAULTSIM(oom-persistent) [list \
34 -injectstart {oom_injectstart 1000000} \
35 -injectstop oom_injectstop \
36 -injecterrlist {{1 {out of memory}}} \
37]
38
39# Transient and persistent IO errors:
40#
41set FAULTSIM(ioerr-transient) [list \
42 -injectstart {ioerr_injectstart 0} \
43 -injectstop ioerr_injectstop \
44 -injecterrlist {{1 {disk I/O error}}} \
45]
46set FAULTSIM(ioerr-persistent) [list \
47 -injectstart {ioerr_injectstart 1} \
48 -injectstop ioerr_injectstop \
49 -injecterrlist {{1 {disk I/O error}}} \
50]
51
dan146ed782010-06-19 17:26:37 +000052# SQLITE_FULL errors (always persistent):
53#
54set FAULTSIM(full) [list \
55 -injectinstall fullerr_injectinstall \
56 -injectstart fullerr_injectstart \
57 -injectstop fullerr_injectstop \
58 -injecterrlist {{1 {database or disk is full}}} \
59 -injectuninstall fullerr_injectuninstall \
60]
61
dan1f55e282010-06-03 09:25:10 +000062# Transient and persistent SHM errors:
63#
64set FAULTSIM(shmerr-transient) [list \
65 -injectinstall shmerr_injectinstall \
66 -injectstart {shmerr_injectstart 0} \
67 -injectstop shmerr_injectstop \
68 -injecterrlist {{1 {disk I/O error}}} \
69 -injectuninstall shmerr_injectuninstall \
70]
71set FAULTSIM(shmerr-persistent) [list \
72 -injectinstall shmerr_injectinstall \
73 -injectstart {shmerr_injectstart 1} \
74 -injectstop shmerr_injectstop \
75 -injecterrlist {{1 {disk I/O error}}} \
76 -injectuninstall shmerr_injectuninstall \
77]
78
danf9b44192010-06-25 19:09:48 +000079# Transient and persistent CANTOPEN errors:
80#
81set FAULTSIM(cantopen-transient) [list \
82 -injectinstall cantopen_injectinstall \
83 -injectstart {cantopen_injectstart 0} \
84 -injectstop cantopen_injectstop \
85 -injecterrlist {{1 {unable to open database file}}} \
86 -injectuninstall cantopen_injectuninstall \
87]
88set FAULTSIM(cantopen-persistent) [list \
89 -injectinstall cantopen_injectinstall \
90 -injectstart {cantopen_injectstart 1} \
91 -injectstop cantopen_injectstop \
92 -injecterrlist {{1 {unable to open database file}}} \
93 -injectuninstall cantopen_injectuninstall \
94]
95
dan1f55e282010-06-03 09:25:10 +000096
dana4bc1232010-06-01 17:46:38 +000097
98#--------------------------------------------------------------------------
99# Usage do_faultsim_test NAME ?OPTIONS...?
100#
101# -faults List of fault types to simulate.
102#
103# -prep Script to execute before -body.
104#
105# -body Script to execute (with fault injection).
106#
107# -test Script to execute after -body.
108#
109proc do_faultsim_test {name args} {
dan1f55e282010-06-03 09:25:10 +0000110 global FAULTSIM
111
112 set DEFAULT(-faults) [array names FAULTSIM]
dana4bc1232010-06-01 17:46:38 +0000113 set DEFAULT(-prep) ""
114 set DEFAULT(-body) ""
115 set DEFAULT(-test) ""
116
117 array set O [array get DEFAULT]
118 array set O $args
119 foreach o [array names O] {
120 if {[info exists DEFAULT($o)]==0} { error "unknown option: $o" }
121 }
122
dan1f55e282010-06-03 09:25:10 +0000123 set faultlist [list]
dana4bc1232010-06-01 17:46:38 +0000124 foreach f $O(-faults) {
dan1f55e282010-06-03 09:25:10 +0000125 set flist [array names FAULTSIM $f]
126 if {[llength $flist]==0} { error "unknown fault: $f" }
127 set faultlist [concat $faultlist $flist]
dana4bc1232010-06-01 17:46:38 +0000128 }
dan1f55e282010-06-03 09:25:10 +0000129
dana4bc1232010-06-01 17:46:38 +0000130 set testspec [list -prep $O(-prep) -body $O(-body) -test $O(-test)]
dan1f55e282010-06-03 09:25:10 +0000131 foreach f [lsort -unique $faultlist] {
132 eval do_one_faultsim_test "$name-$f" $FAULTSIM($f) $testspec
dana4bc1232010-06-01 17:46:38 +0000133 }
134}
135
136#-------------------------------------------------------------------------
137# Procedures to save and restore the current file-system state:
138#
danb0ac3e32010-06-16 10:55:42 +0000139# faultsim_save
dana4bc1232010-06-01 17:46:38 +0000140# faultsim_save_and_close
141# faultsim_restore_and_reopen
danb0ac3e32010-06-16 10:55:42 +0000142# faultsim_delete_and_reopen
dana4bc1232010-06-01 17:46:38 +0000143#
danb0ac3e32010-06-16 10:55:42 +0000144proc faultsim_save {} {
145 foreach f [glob -nocomplain sv_test.db*] { file delete -force $f }
146 foreach f [glob -nocomplain test.db*] {
147 set f2 "sv_$f"
148 file copy -force $f $f2
dana4bc1232010-06-01 17:46:38 +0000149 }
danb0ac3e32010-06-16 10:55:42 +0000150}
151proc faultsim_save_and_close {} {
152 faultsim_save
dana4bc1232010-06-01 17:46:38 +0000153 catch { db close }
154 return ""
155}
dane08341c2010-06-21 12:34:29 +0000156proc faultsim_restore_and_reopen {{dbfile test.db}} {
dana4bc1232010-06-01 17:46:38 +0000157 catch { db close }
danb0ac3e32010-06-16 10:55:42 +0000158 foreach f [glob -nocomplain test.db*] { file delete -force $f }
159 foreach f2 [glob -nocomplain sv_test.db*] {
160 set f [string range $f2 3 end]
161 file copy -force $f2 $f
dana4bc1232010-06-01 17:46:38 +0000162 }
dane08341c2010-06-21 12:34:29 +0000163 sqlite3 db $dbfile
dana4bc1232010-06-01 17:46:38 +0000164 sqlite3_extended_result_codes db 1
165 sqlite3_db_config_lookaside db 0 0 0
166}
167
dan1f55e282010-06-03 09:25:10 +0000168proc faultsim_integrity_check {{db db}} {
169 set ic [$db eval { PRAGMA integrity_check }]
170 if {$ic != "ok"} { error "Integrity check: $ic" }
171}
dana4bc1232010-06-01 17:46:38 +0000172
dan1f55e282010-06-03 09:25:10 +0000173proc faultsim_delete_and_reopen {{file test.db}} {
174 catch { db close }
danb0ac3e32010-06-16 10:55:42 +0000175 foreach f [glob -nocomplain test.db*] { file delete -force $f }
dane08341c2010-06-21 12:34:29 +0000176 sqlite3 db $file
dan1f55e282010-06-03 09:25:10 +0000177}
178
179
180# The following procs are used as [do_one_faultsim_test] callbacks when
181# injecting OOM faults into test cases.
dan8521abf2010-05-31 06:38:34 +0000182#
183proc oom_injectstart {nRepeat iFail} {
184 sqlite3_memdebug_fail $iFail -repeat $nRepeat
185}
186proc oom_injectstop {} {
187 sqlite3_memdebug_fail -1
188}
189
dan1f55e282010-06-03 09:25:10 +0000190# The following procs are used as [do_one_faultsim_test] callbacks when
191# injecting IO error faults into test cases.
192#
dana4bc1232010-06-01 17:46:38 +0000193proc ioerr_injectstart {persist iFail} {
194 set ::sqlite_io_error_persist $persist
195 set ::sqlite_io_error_pending $iFail
196}
197proc ioerr_injectstop {} {
198 set sv $::sqlite_io_error_hit
199 set ::sqlite_io_error_persist 0
200 set ::sqlite_io_error_pending 0
201 set ::sqlite_io_error_hardhit 0
202 set ::sqlite_io_error_hit 0
203 set ::sqlite_io_error_pending 0
204 return $sv
205}
206
dan1f55e282010-06-03 09:25:10 +0000207# The following procs are used as [do_one_faultsim_test] callbacks when
208# injecting shared-memory related error faults into test cases.
209#
210proc shmerr_injectinstall {} {
211 testvfs shmfault -default true
dan346e4262010-06-23 19:27:36 +0000212 shmfault filter {xShmOpen xShmMap xShmLock}
dan1f55e282010-06-03 09:25:10 +0000213}
214proc shmerr_injectuninstall {} {
215 catch {db close}
216 catch {db2 close}
217 shmfault delete
218}
219proc shmerr_injectstart {persist iFail} {
220 shmfault ioerr $iFail $persist
221}
222proc shmerr_injectstop {} {
danf9b44192010-06-25 19:09:48 +0000223 shmfault ioerr
dan1f55e282010-06-03 09:25:10 +0000224}
225
dandca321a2010-06-24 10:50:17 +0000226# The following procs are used as [do_one_faultsim_test] callbacks when
227# injecting SQLITE_FULL error faults into test cases.
228#
dan146ed782010-06-19 17:26:37 +0000229proc fullerr_injectinstall {} {
230 testvfs shmfault -default true
231}
232proc fullerr_injectuninstall {} {
233 catch {db close}
234 catch {db2 close}
235 shmfault delete
236}
237proc fullerr_injectstart {iFail} {
danf9b44192010-06-25 19:09:48 +0000238 shmfault full $iFail 1
dan146ed782010-06-19 17:26:37 +0000239}
240proc fullerr_injectstop {} {
danf9b44192010-06-25 19:09:48 +0000241 shmfault full
242}
243
244# The following procs are used as [do_one_faultsim_test] callbacks when
245# injecting SQLITE_CANTOPEN error faults into test cases.
246#
247proc cantopen_injectinstall {} {
248 testvfs shmfault -default true
249}
250proc cantopen_injectuninstall {} {
251 catch {db close}
252 catch {db2 close}
253 shmfault delete
254}
255proc cantopen_injectstart {persist iFail} {
256 shmfault cantopen $iFail $persist
257}
258proc cantopen_injectstop {} {
259 shmfault cantopen
dan146ed782010-06-19 17:26:37 +0000260}
261
dana4bc1232010-06-01 17:46:38 +0000262# This command is not called directly. It is used by the
263# [faultsim_test_result] command created by [do_faultsim_test] and used
264# by -test scripts.
dan8521abf2010-05-31 06:38:34 +0000265#
dana4bc1232010-06-01 17:46:38 +0000266proc faultsim_test_result_int {args} {
dan8521abf2010-05-31 06:38:34 +0000267 upvar testrc testrc testresult testresult testnfail testnfail
268 set t [list $testrc $testresult]
dana4bc1232010-06-01 17:46:38 +0000269 set r $args
dan8521abf2010-05-31 06:38:34 +0000270 if { ($testnfail==0 && $t != [lindex $r 0]) || [lsearch $r $t]<0 } {
271 error "nfail=$testnfail rc=$testrc result=$testresult"
272 }
273}
274
dana4bc1232010-06-01 17:46:38 +0000275#--------------------------------------------------------------------------
276# Usage do_one_faultsim_test NAME ?OPTIONS...?
dan8521abf2010-05-31 06:38:34 +0000277#
278# The first argument, <test number>, is used as a prefix of the test names
279# taken by tests executed by this command. Options are as follows. All
280# options take a single argument.
281#
282# -injectstart Script to enable fault-injection.
283#
284# -injectstop Script to disable fault-injection.
285#
dana4bc1232010-06-01 17:46:38 +0000286# -injecterrlist List of generally acceptable test results (i.e. error
287# messages). Example: [list {1 {out of memory}}]
288#
dan1f55e282010-06-03 09:25:10 +0000289# -injectinstall
290#
291# -injectuninstall
292#
dan8521abf2010-05-31 06:38:34 +0000293# -prep Script to execute before -body.
294#
295# -body Script to execute (with fault injection).
296#
297# -test Script to execute after -body.
298#
dana4bc1232010-06-01 17:46:38 +0000299proc do_one_faultsim_test {testname args} {
dan8521abf2010-05-31 06:38:34 +0000300
dan1f55e282010-06-03 09:25:10 +0000301 set DEFAULT(-injectstart) "expr"
302 set DEFAULT(-injectstop) "expr 0"
303 set DEFAULT(-injecterrlist) [list]
304 set DEFAULT(-injectinstall) ""
305 set DEFAULT(-injectuninstall) ""
306 set DEFAULT(-prep) ""
307 set DEFAULT(-body) ""
308 set DEFAULT(-test) ""
dan8521abf2010-05-31 06:38:34 +0000309
310 array set O [array get DEFAULT]
311 array set O $args
312 foreach o [array names O] {
313 if {[info exists DEFAULT($o)]==0} { error "unknown option: $o" }
314 }
315
316 proc faultsim_test_proc {testrc testresult testnfail} $O(-test)
dana4bc1232010-06-01 17:46:38 +0000317 proc faultsim_test_result {args} "
318 uplevel faultsim_test_result_int \$args [list $O(-injecterrlist)]
319 "
dan8521abf2010-05-31 06:38:34 +0000320
dan1f55e282010-06-03 09:25:10 +0000321 eval $O(-injectinstall)
322
dan8521abf2010-05-31 06:38:34 +0000323 set stop 0
324 for {set iFail 1} {!$stop} {incr iFail} {
325
326 # Evaluate the -prep script.
327 #
328 eval $O(-prep)
329
330 # Start the fault-injection. Run the -body script. Stop the fault
331 # injection. Local var $nfail is set to the total number of faults
332 # injected into the system this trial.
333 #
334 eval $O(-injectstart) $iFail
335 set rc [catch $O(-body) res]
336 set nfail [eval $O(-injectstop)]
337
338 # Run the -test script. If it throws no error, consider this trial
339 # sucessful. If it does throw an error, cause a [do_test] test to
340 # fail (and print out the unexpected exception thrown by the -test
341 # script at the same time).
342 #
343 set rc [catch [list faultsim_test_proc $rc $res $nfail] res]
344 if {$rc == 0} {set res ok}
345 do_test $testname.$iFail [list list $rc $res] {0 ok}
346
347 # If no faults where injected this trial, don't bother running
348 # any more. This test is finished.
349 #
350 if {$nfail==0} { set stop 1 }
351 }
dan1f55e282010-06-03 09:25:10 +0000352
353 eval $O(-injectuninstall)
dan8521abf2010-05-31 06:38:34 +0000354}
355
danielk1977c9cf9012007-05-30 10:36:47 +0000356# Usage: do_malloc_test <test number> <options...>
357#
358# The first argument, <test number>, is an integer used to name the
359# tests executed by this proc. Options are as follows:
360#
361# -tclprep TCL script to run to prepare test.
362# -sqlprep SQL script to run to prepare test.
363# -tclbody TCL script to run with malloc failure simulation.
364# -sqlbody TCL script to run with malloc failure simulation.
365# -cleanup TCL script to run after the test.
366#
367# This command runs a series of tests to verify SQLite's ability
368# to handle an out-of-memory condition gracefully. It is assumed
369# that if this condition occurs a malloc() call will return a
370# NULL pointer. Linux, for example, doesn't do that by default. See
371# the "BUGS" section of malloc(3).
372#
373# Each iteration of a loop, the TCL commands in any argument passed
374# to the -tclbody switch, followed by the SQL commands in any argument
375# passed to the -sqlbody switch are executed. Each iteration the
376# Nth call to sqliteMalloc() is made to fail, where N is increased
377# each time the loop runs starting from 1. When all commands execute
378# successfully, the loop ends.
379#
380proc do_malloc_test {tn args} {
381 array unset ::mallocopts
382 array set ::mallocopts $args
383
384 if {[string is integer $tn]} {
385 set tn malloc-$tn
386 }
drhf3a65f72007-08-22 20:18:21 +0000387 if {[info exists ::mallocopts(-start)]} {
388 set start $::mallocopts(-start)
389 } else {
danielk1977ae72d982007-10-03 08:46:44 +0000390 set start 0
drhf3a65f72007-08-22 20:18:21 +0000391 }
drh1527ff42008-01-18 17:03:32 +0000392 if {[info exists ::mallocopts(-end)]} {
393 set end $::mallocopts(-end)
394 } else {
395 set end 50000
396 }
drh93aed5a2008-01-16 17:46:38 +0000397 save_prng_state
danielk1977c9cf9012007-05-30 10:36:47 +0000398
drh643167f2008-01-22 21:30:53 +0000399 foreach ::iRepeat {0 10000000} {
danielk1977a1644fd2007-08-29 12:31:25 +0000400 set ::go 1
drh1527ff42008-01-18 17:03:32 +0000401 for {set ::n $start} {$::go && $::n <= $end} {incr ::n} {
danielk1977c9cf9012007-05-30 10:36:47 +0000402
danielk1977a1644fd2007-08-29 12:31:25 +0000403 # If $::iRepeat is 0, then the malloc() failure is transient - it
404 # fails and then subsequent calls succeed. If $::iRepeat is 1,
405 # then the failure is persistent - once malloc() fails it keeps
406 # failing.
danielk1977c9cf9012007-05-30 10:36:47 +0000407 #
danielk1977a1644fd2007-08-29 12:31:25 +0000408 set zRepeat "transient"
409 if {$::iRepeat} {set zRepeat "persistent"}
drh93aed5a2008-01-16 17:46:38 +0000410 restore_prng_state
411 foreach file [glob -nocomplain test.db-mj*] {file delete -force $file}
danielk1977c9cf9012007-05-30 10:36:47 +0000412
danielk1977a1644fd2007-08-29 12:31:25 +0000413 do_test ${tn}.${zRepeat}.${::n} {
414
415 # Remove all traces of database files test.db and test2.db
416 # from the file-system. Then open (empty database) "test.db"
417 # with the handle [db].
418 #
419 catch {db close}
420 catch {file delete -force test.db}
421 catch {file delete -force test.db-journal}
dan5e9e4822010-05-03 18:01:21 +0000422 catch {file delete -force test.db-wal}
danielk1977a1644fd2007-08-29 12:31:25 +0000423 catch {file delete -force test2.db}
424 catch {file delete -force test2.db-journal}
dan5e9e4822010-05-03 18:01:21 +0000425 catch {file delete -force test2.db-wal}
danielk1977a1644fd2007-08-29 12:31:25 +0000426 if {[info exists ::mallocopts(-testdb)]} {
427 file copy $::mallocopts(-testdb) test.db
danielk1977c9cf9012007-05-30 10:36:47 +0000428 }
danielk1977ae72d982007-10-03 08:46:44 +0000429 catch { sqlite3 db test.db }
430 if {[info commands db] ne ""} {
431 sqlite3_extended_result_codes db 1
432 }
drhe9d1c722008-08-04 20:13:26 +0000433 sqlite3_db_config_lookaside db 0 0 0
danielk1977a1644fd2007-08-29 12:31:25 +0000434
435 # Execute any -tclprep and -sqlprep scripts.
436 #
437 if {[info exists ::mallocopts(-tclprep)]} {
438 eval $::mallocopts(-tclprep)
439 }
440 if {[info exists ::mallocopts(-sqlprep)]} {
441 execsql $::mallocopts(-sqlprep)
442 }
443
444 # Now set the ${::n}th malloc() to fail and execute the -tclbody
445 # and -sqlbody scripts.
446 #
447 sqlite3_memdebug_fail $::n -repeat $::iRepeat
448 set ::mallocbody {}
449 if {[info exists ::mallocopts(-tclbody)]} {
450 append ::mallocbody "$::mallocopts(-tclbody)\n"
451 }
452 if {[info exists ::mallocopts(-sqlbody)]} {
453 append ::mallocbody "db eval {$::mallocopts(-sqlbody)}"
454 }
danielk1977c9cf9012007-05-30 10:36:47 +0000455
danielk1977a1644fd2007-08-29 12:31:25 +0000456 # The following block sets local variables as follows:
457 #
458 # isFail - True if an error (any error) was reported by sqlite.
459 # nFail - The total number of simulated malloc() failures.
460 # nBenign - The number of benign simulated malloc() failures.
461 #
462 set isFail [catch $::mallocbody msg]
463 set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign]
drh643167f2008-01-22 21:30:53 +0000464 # puts -nonewline " (isFail=$isFail nFail=$nFail nBenign=$nBenign) "
danielk1977a1644fd2007-08-29 12:31:25 +0000465
466 # If one or more mallocs failed, run this loop body again.
467 #
468 set go [expr {$nFail>0}]
469
470 if {($nFail-$nBenign)==0} {
471 if {$isFail} {
472 set v2 $msg
473 } else {
474 set isFail 1
475 set v2 1
476 }
477 } elseif {!$isFail} {
478 set v2 $msg
danielk1977ae72d982007-10-03 08:46:44 +0000479 } elseif {
480 [info command db]=="" ||
481 [db errorcode]==7 ||
danielk1977ae72d982007-10-03 08:46:44 +0000482 $msg=="out of memory"
483 } {
danielk1977a1644fd2007-08-29 12:31:25 +0000484 set v2 1
485 } else {
486 set v2 $msg
danielk1977ae72d982007-10-03 08:46:44 +0000487 puts [db errorcode]
danielk1977a1644fd2007-08-29 12:31:25 +0000488 }
489 lappend isFail $v2
490 } {1 1}
491
492 if {[info exists ::mallocopts(-cleanup)]} {
493 catch [list uplevel #0 $::mallocopts(-cleanup)] msg
494 }
danielk1977c9cf9012007-05-30 10:36:47 +0000495 }
496 }
497 unset ::mallocopts
danielk197777519402007-08-30 11:48:31 +0000498 sqlite3_memdebug_fail -1
danielk1977c9cf9012007-05-30 10:36:47 +0000499}
danef378022010-05-04 11:06:03 +0000500
501
502#-------------------------------------------------------------------------
503# This proc is used to test a single SELECT statement. Parameter $name is
504# passed a name for the test case (i.e. "fts3_malloc-1.4.1") and parameter
505# $sql is passed the text of the SELECT statement. Parameter $result is
506# set to the expected output if the SELECT statement is successfully
507# executed using [db eval].
508#
509# Example:
510#
511# do_select_test testcase-1.1 "SELECT 1+1, 1+2" {1 2}
512#
513# If global variable DO_MALLOC_TEST is set to a non-zero value, or if
514# it is not defined at all, then OOM testing is performed on the SELECT
515# statement. Each OOM test case is said to pass if either (a) executing
516# the SELECT statement succeeds and the results match those specified
517# by parameter $result, or (b) TCL throws an "out of memory" error.
518#
519# If DO_MALLOC_TEST is defined and set to zero, then the SELECT statement
520# is executed just once. In this case the test case passes if the results
521# match the expected results passed via parameter $result.
522#
523proc do_select_test {name sql result} {
524 uplevel [list doPassiveTest 0 $name $sql [list 0 $result]]
525}
526
527proc do_restart_select_test {name sql result} {
528 uplevel [list doPassiveTest 1 $name $sql [list 0 $result]]
529}
530
531proc do_error_test {name sql error} {
532 uplevel [list doPassiveTest 0 $name $sql [list 1 $error]]
533}
534
535proc doPassiveTest {isRestart name sql catchres} {
536 if {![info exists ::DO_MALLOC_TEST]} { set ::DO_MALLOC_TEST 1 }
537
538 switch $::DO_MALLOC_TEST {
539 0 { # No malloc failures.
540 do_test $name [list set {} [uplevel [list catchsql $sql]]] $catchres
541 return
542 }
543 1 { # Simulate transient failures.
544 set nRepeat 1
545 set zName "transient"
546 set nStartLimit 100000
547 set nBackup 1
548 }
549 2 { # Simulate persistent failures.
550 set nRepeat 1
551 set zName "persistent"
552 set nStartLimit 100000
553 set nBackup 1
554 }
555 3 { # Simulate transient failures with extra brute force.
556 set nRepeat 100000
557 set zName "ridiculous"
558 set nStartLimit 1
559 set nBackup 10
560 }
561 }
562
563 # The set of acceptable results from running [catchsql $sql].
564 #
565 set answers [list {1 {out of memory}} $catchres]
566 set str [join $answers " OR "]
567
568 set nFail 1
569 for {set iLimit $nStartLimit} {$nFail} {incr iLimit} {
570 for {set iFail 1} {$nFail && $iFail<=$iLimit} {incr iFail} {
571 for {set iTest 0} {$iTest<$nBackup && ($iFail-$iTest)>0} {incr iTest} {
572
573 if {$isRestart} { sqlite3 db test.db }
574
575 sqlite3_memdebug_fail [expr $iFail-$iTest] -repeat $nRepeat
576 set res [uplevel [list catchsql $sql]]
577 if {[lsearch -exact $answers $res]>=0} { set res $str }
578 set testname "$name.$zName.$iFail"
579 do_test "$name.$zName.$iLimit.$iFail" [list set {} $res] $str
580
581 set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign]
582 }
583 }
584 }
585}
586
587
588#-------------------------------------------------------------------------
589# Test a single write to the database. In this case a "write" is a
590# DELETE, UPDATE or INSERT statement.
591#
592# If OOM testing is performed, there are several acceptable outcomes:
593#
594# 1) The write succeeds. No error is returned.
595#
596# 2) An "out of memory" exception is thrown and:
597#
598# a) The statement has no effect, OR
599# b) The current transaction is rolled back, OR
600# c) The statement succeeds. This can only happen if the connection
601# is in auto-commit mode (after the statement is executed, so this
602# includes COMMIT statements).
603#
604# If the write operation eventually succeeds, zero is returned. If a
605# transaction is rolled back, non-zero is returned.
606#
607# Parameter $name is the name to use for the test case (or test cases).
608# The second parameter, $tbl, should be the name of the database table
609# being modified. Parameter $sql contains the SQL statement to test.
610#
611proc do_write_test {name tbl sql} {
612 if {![info exists ::DO_MALLOC_TEST]} { set ::DO_MALLOC_TEST 1 }
613
614 # Figure out an statement to get a checksum for table $tbl.
615 db eval "SELECT * FROM $tbl" V break
616 set cksumsql "SELECT md5sum([join [concat rowid $V(*)] ,]) FROM $tbl"
617
618 # Calculate the initial table checksum.
619 set cksum1 [db one $cksumsql]
620
621 if {$::DO_MALLOC_TEST } {
622 set answers [list {1 {out of memory}} {0 {}}]
623 if {$::DO_MALLOC_TEST==1} {
shanehdecd09c2010-07-09 18:43:40 +0000624 set modes {100000 persistent}
danef378022010-05-04 11:06:03 +0000625 } else {
shanehdecd09c2010-07-09 18:43:40 +0000626 set modes {1 transient}
danef378022010-05-04 11:06:03 +0000627 }
628 } else {
629 set answers [list {0 {}}]
630 set modes [list 0 nofail]
631 }
632 set str [join $answers " OR "]
633
634 foreach {nRepeat zName} $modes {
635 for {set iFail 1} 1 {incr iFail} {
636 if {$::DO_MALLOC_TEST} {sqlite3_memdebug_fail $iFail -repeat $nRepeat}
637
638 set res [uplevel [list catchsql $sql]]
639 set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign]
640 if {$nFail==0} {
641 do_test $name.$zName.$iFail [list set {} $res] {0 {}}
642 return
643 } else {
644 if {[lsearch $answers $res]>=0} {
645 set res $str
646 }
647 do_test $name.$zName.$iFail [list set {} $res] $str
648 set cksum2 [db one $cksumsql]
649 if {$cksum1 != $cksum2} return
650 }
651 }
652 }
653}