blob: 8202b19aff0fabe5ec48d0bdac7de13b94efdf00 [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
79
dana4bc1232010-06-01 17:46:38 +000080
81#--------------------------------------------------------------------------
82# Usage do_faultsim_test NAME ?OPTIONS...?
83#
84# -faults List of fault types to simulate.
85#
86# -prep Script to execute before -body.
87#
88# -body Script to execute (with fault injection).
89#
90# -test Script to execute after -body.
91#
92proc do_faultsim_test {name args} {
dan1f55e282010-06-03 09:25:10 +000093 global FAULTSIM
94
95 set DEFAULT(-faults) [array names FAULTSIM]
dana4bc1232010-06-01 17:46:38 +000096 set DEFAULT(-prep) ""
97 set DEFAULT(-body) ""
98 set DEFAULT(-test) ""
99
100 array set O [array get DEFAULT]
101 array set O $args
102 foreach o [array names O] {
103 if {[info exists DEFAULT($o)]==0} { error "unknown option: $o" }
104 }
105
dan1f55e282010-06-03 09:25:10 +0000106 set faultlist [list]
dana4bc1232010-06-01 17:46:38 +0000107 foreach f $O(-faults) {
dan1f55e282010-06-03 09:25:10 +0000108 set flist [array names FAULTSIM $f]
109 if {[llength $flist]==0} { error "unknown fault: $f" }
110 set faultlist [concat $faultlist $flist]
dana4bc1232010-06-01 17:46:38 +0000111 }
dan1f55e282010-06-03 09:25:10 +0000112
dana4bc1232010-06-01 17:46:38 +0000113 set testspec [list -prep $O(-prep) -body $O(-body) -test $O(-test)]
dan1f55e282010-06-03 09:25:10 +0000114 foreach f [lsort -unique $faultlist] {
115 eval do_one_faultsim_test "$name-$f" $FAULTSIM($f) $testspec
dana4bc1232010-06-01 17:46:38 +0000116 }
117}
118
119#-------------------------------------------------------------------------
120# Procedures to save and restore the current file-system state:
121#
danb0ac3e32010-06-16 10:55:42 +0000122# faultsim_save
dana4bc1232010-06-01 17:46:38 +0000123# faultsim_save_and_close
124# faultsim_restore_and_reopen
danb0ac3e32010-06-16 10:55:42 +0000125# faultsim_delete_and_reopen
dana4bc1232010-06-01 17:46:38 +0000126#
danb0ac3e32010-06-16 10:55:42 +0000127proc faultsim_save {} {
128 foreach f [glob -nocomplain sv_test.db*] { file delete -force $f }
129 foreach f [glob -nocomplain test.db*] {
130 set f2 "sv_$f"
131 file copy -force $f $f2
dana4bc1232010-06-01 17:46:38 +0000132 }
danb0ac3e32010-06-16 10:55:42 +0000133}
134proc faultsim_save_and_close {} {
135 faultsim_save
dana4bc1232010-06-01 17:46:38 +0000136 catch { db close }
137 return ""
138}
dane08341c2010-06-21 12:34:29 +0000139proc faultsim_restore_and_reopen {{dbfile test.db}} {
dana4bc1232010-06-01 17:46:38 +0000140 catch { db close }
danb0ac3e32010-06-16 10:55:42 +0000141 foreach f [glob -nocomplain test.db*] { file delete -force $f }
142 foreach f2 [glob -nocomplain sv_test.db*] {
143 set f [string range $f2 3 end]
144 file copy -force $f2 $f
dana4bc1232010-06-01 17:46:38 +0000145 }
dane08341c2010-06-21 12:34:29 +0000146 sqlite3 db $dbfile
dana4bc1232010-06-01 17:46:38 +0000147 sqlite3_extended_result_codes db 1
148 sqlite3_db_config_lookaside db 0 0 0
149}
150
dan1f55e282010-06-03 09:25:10 +0000151proc faultsim_integrity_check {{db db}} {
152 set ic [$db eval { PRAGMA integrity_check }]
153 if {$ic != "ok"} { error "Integrity check: $ic" }
154}
dana4bc1232010-06-01 17:46:38 +0000155
dan1f55e282010-06-03 09:25:10 +0000156proc faultsim_delete_and_reopen {{file test.db}} {
157 catch { db close }
danb0ac3e32010-06-16 10:55:42 +0000158 foreach f [glob -nocomplain test.db*] { file delete -force $f }
dane08341c2010-06-21 12:34:29 +0000159 sqlite3 db $file
dan1f55e282010-06-03 09:25:10 +0000160}
161
162
163# The following procs are used as [do_one_faultsim_test] callbacks when
164# injecting OOM faults into test cases.
dan8521abf2010-05-31 06:38:34 +0000165#
166proc oom_injectstart {nRepeat iFail} {
167 sqlite3_memdebug_fail $iFail -repeat $nRepeat
168}
169proc oom_injectstop {} {
170 sqlite3_memdebug_fail -1
171}
172
dan1f55e282010-06-03 09:25:10 +0000173# The following procs are used as [do_one_faultsim_test] callbacks when
174# injecting IO error faults into test cases.
175#
dana4bc1232010-06-01 17:46:38 +0000176proc ioerr_injectstart {persist iFail} {
177 set ::sqlite_io_error_persist $persist
178 set ::sqlite_io_error_pending $iFail
179}
180proc ioerr_injectstop {} {
181 set sv $::sqlite_io_error_hit
182 set ::sqlite_io_error_persist 0
183 set ::sqlite_io_error_pending 0
184 set ::sqlite_io_error_hardhit 0
185 set ::sqlite_io_error_hit 0
186 set ::sqlite_io_error_pending 0
187 return $sv
188}
189
dan146ed782010-06-19 17:26:37 +0000190
dan1f55e282010-06-03 09:25:10 +0000191# The following procs are used as [do_one_faultsim_test] callbacks when
192# injecting shared-memory related error faults into test cases.
193#
194proc shmerr_injectinstall {} {
195 testvfs shmfault -default true
196}
197proc shmerr_injectuninstall {} {
198 catch {db close}
199 catch {db2 close}
200 shmfault delete
201}
202proc shmerr_injectstart {persist iFail} {
203 shmfault ioerr $iFail $persist
204}
205proc shmerr_injectstop {} {
206 shmfault ioerr 0 0
207}
208
dan146ed782010-06-19 17:26:37 +0000209proc fullerr_injectinstall {} {
210 testvfs shmfault -default true
211}
212proc fullerr_injectuninstall {} {
213 catch {db close}
214 catch {db2 close}
215 shmfault delete
216}
217proc fullerr_injectstart {iFail} {
218 shmfault full $iFail
219}
220proc fullerr_injectstop {} {
221 shmfault full 0
222}
223
224
dana4bc1232010-06-01 17:46:38 +0000225# This command is not called directly. It is used by the
226# [faultsim_test_result] command created by [do_faultsim_test] and used
227# by -test scripts.
dan8521abf2010-05-31 06:38:34 +0000228#
dana4bc1232010-06-01 17:46:38 +0000229proc faultsim_test_result_int {args} {
dan8521abf2010-05-31 06:38:34 +0000230 upvar testrc testrc testresult testresult testnfail testnfail
231 set t [list $testrc $testresult]
dana4bc1232010-06-01 17:46:38 +0000232 set r $args
dan8521abf2010-05-31 06:38:34 +0000233 if { ($testnfail==0 && $t != [lindex $r 0]) || [lsearch $r $t]<0 } {
234 error "nfail=$testnfail rc=$testrc result=$testresult"
235 }
236}
237
dana4bc1232010-06-01 17:46:38 +0000238#--------------------------------------------------------------------------
239# Usage do_one_faultsim_test NAME ?OPTIONS...?
dan8521abf2010-05-31 06:38:34 +0000240#
241# The first argument, <test number>, is used as a prefix of the test names
242# taken by tests executed by this command. Options are as follows. All
243# options take a single argument.
244#
245# -injectstart Script to enable fault-injection.
246#
247# -injectstop Script to disable fault-injection.
248#
dana4bc1232010-06-01 17:46:38 +0000249# -injecterrlist List of generally acceptable test results (i.e. error
250# messages). Example: [list {1 {out of memory}}]
251#
dan1f55e282010-06-03 09:25:10 +0000252# -injectinstall
253#
254# -injectuninstall
255#
dan8521abf2010-05-31 06:38:34 +0000256# -prep Script to execute before -body.
257#
258# -body Script to execute (with fault injection).
259#
260# -test Script to execute after -body.
261#
dana4bc1232010-06-01 17:46:38 +0000262proc do_one_faultsim_test {testname args} {
dan8521abf2010-05-31 06:38:34 +0000263
dan1f55e282010-06-03 09:25:10 +0000264 set DEFAULT(-injectstart) "expr"
265 set DEFAULT(-injectstop) "expr 0"
266 set DEFAULT(-injecterrlist) [list]
267 set DEFAULT(-injectinstall) ""
268 set DEFAULT(-injectuninstall) ""
269 set DEFAULT(-prep) ""
270 set DEFAULT(-body) ""
271 set DEFAULT(-test) ""
dan8521abf2010-05-31 06:38:34 +0000272
273 array set O [array get DEFAULT]
274 array set O $args
275 foreach o [array names O] {
276 if {[info exists DEFAULT($o)]==0} { error "unknown option: $o" }
277 }
278
279 proc faultsim_test_proc {testrc testresult testnfail} $O(-test)
dana4bc1232010-06-01 17:46:38 +0000280 proc faultsim_test_result {args} "
281 uplevel faultsim_test_result_int \$args [list $O(-injecterrlist)]
282 "
dan8521abf2010-05-31 06:38:34 +0000283
dan1f55e282010-06-03 09:25:10 +0000284 eval $O(-injectinstall)
285
dan8521abf2010-05-31 06:38:34 +0000286 set stop 0
287 for {set iFail 1} {!$stop} {incr iFail} {
288
289 # Evaluate the -prep script.
290 #
291 eval $O(-prep)
292
293 # Start the fault-injection. Run the -body script. Stop the fault
294 # injection. Local var $nfail is set to the total number of faults
295 # injected into the system this trial.
296 #
297 eval $O(-injectstart) $iFail
298 set rc [catch $O(-body) res]
299 set nfail [eval $O(-injectstop)]
300
301 # Run the -test script. If it throws no error, consider this trial
302 # sucessful. If it does throw an error, cause a [do_test] test to
303 # fail (and print out the unexpected exception thrown by the -test
304 # script at the same time).
305 #
306 set rc [catch [list faultsim_test_proc $rc $res $nfail] res]
307 if {$rc == 0} {set res ok}
308 do_test $testname.$iFail [list list $rc $res] {0 ok}
309
310 # If no faults where injected this trial, don't bother running
311 # any more. This test is finished.
312 #
313 if {$nfail==0} { set stop 1 }
314 }
dan1f55e282010-06-03 09:25:10 +0000315
316 eval $O(-injectuninstall)
dan8521abf2010-05-31 06:38:34 +0000317}
318
danielk1977c9cf9012007-05-30 10:36:47 +0000319# Usage: do_malloc_test <test number> <options...>
320#
321# The first argument, <test number>, is an integer used to name the
322# tests executed by this proc. Options are as follows:
323#
324# -tclprep TCL script to run to prepare test.
325# -sqlprep SQL script to run to prepare test.
326# -tclbody TCL script to run with malloc failure simulation.
327# -sqlbody TCL script to run with malloc failure simulation.
328# -cleanup TCL script to run after the test.
329#
330# This command runs a series of tests to verify SQLite's ability
331# to handle an out-of-memory condition gracefully. It is assumed
332# that if this condition occurs a malloc() call will return a
333# NULL pointer. Linux, for example, doesn't do that by default. See
334# the "BUGS" section of malloc(3).
335#
336# Each iteration of a loop, the TCL commands in any argument passed
337# to the -tclbody switch, followed by the SQL commands in any argument
338# passed to the -sqlbody switch are executed. Each iteration the
339# Nth call to sqliteMalloc() is made to fail, where N is increased
340# each time the loop runs starting from 1. When all commands execute
341# successfully, the loop ends.
342#
343proc do_malloc_test {tn args} {
344 array unset ::mallocopts
345 array set ::mallocopts $args
346
347 if {[string is integer $tn]} {
348 set tn malloc-$tn
349 }
drhf3a65f72007-08-22 20:18:21 +0000350 if {[info exists ::mallocopts(-start)]} {
351 set start $::mallocopts(-start)
352 } else {
danielk1977ae72d982007-10-03 08:46:44 +0000353 set start 0
drhf3a65f72007-08-22 20:18:21 +0000354 }
drh1527ff42008-01-18 17:03:32 +0000355 if {[info exists ::mallocopts(-end)]} {
356 set end $::mallocopts(-end)
357 } else {
358 set end 50000
359 }
drh93aed5a2008-01-16 17:46:38 +0000360 save_prng_state
danielk1977c9cf9012007-05-30 10:36:47 +0000361
drh643167f2008-01-22 21:30:53 +0000362 foreach ::iRepeat {0 10000000} {
danielk1977a1644fd2007-08-29 12:31:25 +0000363 set ::go 1
drh1527ff42008-01-18 17:03:32 +0000364 for {set ::n $start} {$::go && $::n <= $end} {incr ::n} {
danielk1977c9cf9012007-05-30 10:36:47 +0000365
danielk1977a1644fd2007-08-29 12:31:25 +0000366 # If $::iRepeat is 0, then the malloc() failure is transient - it
367 # fails and then subsequent calls succeed. If $::iRepeat is 1,
368 # then the failure is persistent - once malloc() fails it keeps
369 # failing.
danielk1977c9cf9012007-05-30 10:36:47 +0000370 #
danielk1977a1644fd2007-08-29 12:31:25 +0000371 set zRepeat "transient"
372 if {$::iRepeat} {set zRepeat "persistent"}
drh93aed5a2008-01-16 17:46:38 +0000373 restore_prng_state
374 foreach file [glob -nocomplain test.db-mj*] {file delete -force $file}
danielk1977c9cf9012007-05-30 10:36:47 +0000375
danielk1977a1644fd2007-08-29 12:31:25 +0000376 do_test ${tn}.${zRepeat}.${::n} {
377
378 # Remove all traces of database files test.db and test2.db
379 # from the file-system. Then open (empty database) "test.db"
380 # with the handle [db].
381 #
382 catch {db close}
383 catch {file delete -force test.db}
384 catch {file delete -force test.db-journal}
dan5e9e4822010-05-03 18:01:21 +0000385 catch {file delete -force test.db-wal}
danielk1977a1644fd2007-08-29 12:31:25 +0000386 catch {file delete -force test2.db}
387 catch {file delete -force test2.db-journal}
dan5e9e4822010-05-03 18:01:21 +0000388 catch {file delete -force test2.db-wal}
danielk1977a1644fd2007-08-29 12:31:25 +0000389 if {[info exists ::mallocopts(-testdb)]} {
390 file copy $::mallocopts(-testdb) test.db
danielk1977c9cf9012007-05-30 10:36:47 +0000391 }
danielk1977ae72d982007-10-03 08:46:44 +0000392 catch { sqlite3 db test.db }
393 if {[info commands db] ne ""} {
394 sqlite3_extended_result_codes db 1
395 }
drhe9d1c722008-08-04 20:13:26 +0000396 sqlite3_db_config_lookaside db 0 0 0
danielk1977a1644fd2007-08-29 12:31:25 +0000397
398 # Execute any -tclprep and -sqlprep scripts.
399 #
400 if {[info exists ::mallocopts(-tclprep)]} {
401 eval $::mallocopts(-tclprep)
402 }
403 if {[info exists ::mallocopts(-sqlprep)]} {
404 execsql $::mallocopts(-sqlprep)
405 }
406
407 # Now set the ${::n}th malloc() to fail and execute the -tclbody
408 # and -sqlbody scripts.
409 #
410 sqlite3_memdebug_fail $::n -repeat $::iRepeat
411 set ::mallocbody {}
412 if {[info exists ::mallocopts(-tclbody)]} {
413 append ::mallocbody "$::mallocopts(-tclbody)\n"
414 }
415 if {[info exists ::mallocopts(-sqlbody)]} {
416 append ::mallocbody "db eval {$::mallocopts(-sqlbody)}"
417 }
danielk1977c9cf9012007-05-30 10:36:47 +0000418
danielk1977a1644fd2007-08-29 12:31:25 +0000419 # The following block sets local variables as follows:
420 #
421 # isFail - True if an error (any error) was reported by sqlite.
422 # nFail - The total number of simulated malloc() failures.
423 # nBenign - The number of benign simulated malloc() failures.
424 #
425 set isFail [catch $::mallocbody msg]
426 set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign]
drh643167f2008-01-22 21:30:53 +0000427 # puts -nonewline " (isFail=$isFail nFail=$nFail nBenign=$nBenign) "
danielk1977a1644fd2007-08-29 12:31:25 +0000428
429 # If one or more mallocs failed, run this loop body again.
430 #
431 set go [expr {$nFail>0}]
432
433 if {($nFail-$nBenign)==0} {
434 if {$isFail} {
435 set v2 $msg
436 } else {
437 set isFail 1
438 set v2 1
439 }
440 } elseif {!$isFail} {
441 set v2 $msg
danielk1977ae72d982007-10-03 08:46:44 +0000442 } elseif {
443 [info command db]=="" ||
444 [db errorcode]==7 ||
danielk1977ae72d982007-10-03 08:46:44 +0000445 $msg=="out of memory"
446 } {
danielk1977a1644fd2007-08-29 12:31:25 +0000447 set v2 1
448 } else {
449 set v2 $msg
danielk1977ae72d982007-10-03 08:46:44 +0000450 puts [db errorcode]
danielk1977a1644fd2007-08-29 12:31:25 +0000451 }
452 lappend isFail $v2
453 } {1 1}
454
455 if {[info exists ::mallocopts(-cleanup)]} {
456 catch [list uplevel #0 $::mallocopts(-cleanup)] msg
457 }
danielk1977c9cf9012007-05-30 10:36:47 +0000458 }
459 }
460 unset ::mallocopts
danielk197777519402007-08-30 11:48:31 +0000461 sqlite3_memdebug_fail -1
danielk1977c9cf9012007-05-30 10:36:47 +0000462}
danef378022010-05-04 11:06:03 +0000463
464
465#-------------------------------------------------------------------------
466# This proc is used to test a single SELECT statement. Parameter $name is
467# passed a name for the test case (i.e. "fts3_malloc-1.4.1") and parameter
468# $sql is passed the text of the SELECT statement. Parameter $result is
469# set to the expected output if the SELECT statement is successfully
470# executed using [db eval].
471#
472# Example:
473#
474# do_select_test testcase-1.1 "SELECT 1+1, 1+2" {1 2}
475#
476# If global variable DO_MALLOC_TEST is set to a non-zero value, or if
477# it is not defined at all, then OOM testing is performed on the SELECT
478# statement. Each OOM test case is said to pass if either (a) executing
479# the SELECT statement succeeds and the results match those specified
480# by parameter $result, or (b) TCL throws an "out of memory" error.
481#
482# If DO_MALLOC_TEST is defined and set to zero, then the SELECT statement
483# is executed just once. In this case the test case passes if the results
484# match the expected results passed via parameter $result.
485#
486proc do_select_test {name sql result} {
487 uplevel [list doPassiveTest 0 $name $sql [list 0 $result]]
488}
489
490proc do_restart_select_test {name sql result} {
491 uplevel [list doPassiveTest 1 $name $sql [list 0 $result]]
492}
493
494proc do_error_test {name sql error} {
495 uplevel [list doPassiveTest 0 $name $sql [list 1 $error]]
496}
497
498proc doPassiveTest {isRestart name sql catchres} {
499 if {![info exists ::DO_MALLOC_TEST]} { set ::DO_MALLOC_TEST 1 }
500
501 switch $::DO_MALLOC_TEST {
502 0 { # No malloc failures.
503 do_test $name [list set {} [uplevel [list catchsql $sql]]] $catchres
504 return
505 }
506 1 { # Simulate transient failures.
507 set nRepeat 1
508 set zName "transient"
509 set nStartLimit 100000
510 set nBackup 1
511 }
512 2 { # Simulate persistent failures.
513 set nRepeat 1
514 set zName "persistent"
515 set nStartLimit 100000
516 set nBackup 1
517 }
518 3 { # Simulate transient failures with extra brute force.
519 set nRepeat 100000
520 set zName "ridiculous"
521 set nStartLimit 1
522 set nBackup 10
523 }
524 }
525
526 # The set of acceptable results from running [catchsql $sql].
527 #
528 set answers [list {1 {out of memory}} $catchres]
529 set str [join $answers " OR "]
530
531 set nFail 1
532 for {set iLimit $nStartLimit} {$nFail} {incr iLimit} {
533 for {set iFail 1} {$nFail && $iFail<=$iLimit} {incr iFail} {
534 for {set iTest 0} {$iTest<$nBackup && ($iFail-$iTest)>0} {incr iTest} {
535
536 if {$isRestart} { sqlite3 db test.db }
537
538 sqlite3_memdebug_fail [expr $iFail-$iTest] -repeat $nRepeat
539 set res [uplevel [list catchsql $sql]]
540 if {[lsearch -exact $answers $res]>=0} { set res $str }
541 set testname "$name.$zName.$iFail"
542 do_test "$name.$zName.$iLimit.$iFail" [list set {} $res] $str
543
544 set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign]
545 }
546 }
547 }
548}
549
550
551#-------------------------------------------------------------------------
552# Test a single write to the database. In this case a "write" is a
553# DELETE, UPDATE or INSERT statement.
554#
555# If OOM testing is performed, there are several acceptable outcomes:
556#
557# 1) The write succeeds. No error is returned.
558#
559# 2) An "out of memory" exception is thrown and:
560#
561# a) The statement has no effect, OR
562# b) The current transaction is rolled back, OR
563# c) The statement succeeds. This can only happen if the connection
564# is in auto-commit mode (after the statement is executed, so this
565# includes COMMIT statements).
566#
567# If the write operation eventually succeeds, zero is returned. If a
568# transaction is rolled back, non-zero is returned.
569#
570# Parameter $name is the name to use for the test case (or test cases).
571# The second parameter, $tbl, should be the name of the database table
572# being modified. Parameter $sql contains the SQL statement to test.
573#
574proc do_write_test {name tbl sql} {
575 if {![info exists ::DO_MALLOC_TEST]} { set ::DO_MALLOC_TEST 1 }
576
577 # Figure out an statement to get a checksum for table $tbl.
578 db eval "SELECT * FROM $tbl" V break
579 set cksumsql "SELECT md5sum([join [concat rowid $V(*)] ,]) FROM $tbl"
580
581 # Calculate the initial table checksum.
582 set cksum1 [db one $cksumsql]
583
584 if {$::DO_MALLOC_TEST } {
585 set answers [list {1 {out of memory}} {0 {}}]
586 if {$::DO_MALLOC_TEST==1} {
587 set modes {100000 transient}
588 } else {
589 set modes {1 persistent}
590 }
591 } else {
592 set answers [list {0 {}}]
593 set modes [list 0 nofail]
594 }
595 set str [join $answers " OR "]
596
597 foreach {nRepeat zName} $modes {
598 for {set iFail 1} 1 {incr iFail} {
599 if {$::DO_MALLOC_TEST} {sqlite3_memdebug_fail $iFail -repeat $nRepeat}
600
601 set res [uplevel [list catchsql $sql]]
602 set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign]
603 if {$nFail==0} {
604 do_test $name.$zName.$iFail [list set {} $res] {0 {}}
605 return
606 } else {
607 if {[lsearch $answers $res]>=0} {
608 set res $str
609 }
610 do_test $name.$zName.$iFail [list set {} $res] $str
611 set cksum2 [db one $cksumsql]
612 if {$cksum1 != $cksum2} return
613 }
614 }
615 }
616}