3
# The author disclaims copyright to this source code. In place of
4
# a legal notice, here is a blessing:
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.
10
#***********************************************************************
12
# $Id: vtab_err.test,v 1.3 2006/08/15 14:21:16 drh Exp $
14
set testdir [file dirname $argv0]
15
source $testdir/tester.tcl
22
# Usage: do_malloc_test <test number> <options...>
24
# The first argument, <test number>, is an integer used to name the
25
# tests executed by this proc. Options are as follows:
27
# -tclprep TCL script to run to prepare test.
28
# -sqlprep SQL script to run to prepare test.
29
# -tclbody TCL script to run with malloc failure simulation.
30
# -sqlbody TCL script to run with malloc failure simulation.
31
# -cleanup TCL script to run after the test.
33
# This command runs a series of tests to verify SQLite's ability
34
# to handle an out-of-memory condition gracefully. It is assumed
35
# that if this condition occurs a malloc() call will return a
36
# NULL pointer. Linux, for example, doesn't do that by default. See
37
# the "BUGS" section of malloc(3).
39
# Each iteration of a loop, the TCL commands in any argument passed
40
# to the -tclbody switch, followed by the SQL commands in any argument
41
# passed to the -sqlbody switch are executed. Each iteration the
42
# Nth call to sqliteMalloc() is made to fail, where N is increased
43
# each time the loop runs starting from 1. When all commands execute
44
# successfully, the loop ends.
46
proc do_malloc_test {tn args} {
47
array unset ::mallocopts
48
array set ::mallocopts $args
51
for {set ::n 1} {$::go && $::n < 50000} {incr ::n} {
54
# Remove all traces of database files test.db and test2.db from the files
55
# system. Then open (empty database) "test.db" with the handle [db].
59
catch {file delete -force test.db}
60
catch {file delete -force test.db-journal}
61
catch {file delete -force test2.db}
62
catch {file delete -force test2.db-journal}
63
catch {sqlite3 db test.db}
64
set ::DB [sqlite3_connection_pointer db]
66
# Execute any -tclprep and -sqlprep scripts.
68
if {[info exists ::mallocopts(-tclprep)]} {
69
eval $::mallocopts(-tclprep)
71
if {[info exists ::mallocopts(-sqlprep)]} {
72
execsql $::mallocopts(-sqlprep)
75
# Now set the ${::n}th malloc() to fail and execute the -tclbody and
78
sqlite_malloc_fail $::n
80
if {[info exists ::mallocopts(-tclbody)]} {
81
append ::mallocbody "$::mallocopts(-tclbody)\n"
83
if {[info exists ::mallocopts(-sqlbody)]} {
84
append ::mallocbody "db eval {$::mallocopts(-sqlbody)}"
86
set v [catch $::mallocbody msg]
88
# If the test fails (if $v!=0) and the database connection actually
89
# exists, make sure the failure code is SQLITE_NOMEM.
90
if {$v&&[info command db]=="db"&&[info exists ::mallocopts(-sqlbody)]} {
91
if {[db errorcode]!=7 && $msg!="vtable constructor failed: e"} {
96
set leftover [lindex [sqlite_malloc_stat] 2]
98
if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v Message=$msg"}
101
puts "\nError message returned: $msg"
107
$msg == "" || $msg == "out of memory" ||
108
$msg == "vtable constructor failed: e"
110
if {!$v2} {puts "\nError message returned: $msg"}
115
if {[info exists ::mallocopts(-cleanup)]} {
116
catch [list uplevel #0 $::mallocopts(-cleanup)] msg
122
unset -nocomplain echo_module_begin_fail
123
do_ioerr_test vtab_err-1 -tclprep {
124
register_echo_module [sqlite3_connection_pointer db]
127
CREATE TABLE r(a PRIMARY KEY, b, c);
128
CREATE VIRTUAL TABLE e USING echo(r);
129
INSERT INTO e VALUES(1, 2, 3);
130
INSERT INTO e VALUES('a', 'b', 'c');
132
DELETE FROM e WHERE a = 'a';
135
CREATE TABLE r2(a, b, c);
136
INSERT INTO r2 SELECT * FROM e;
137
INSERT INTO e SELECT a||'x', b, c FROM r2;
142
do_malloc_test vtab_err-2 -tclprep {
143
register_echo_module [sqlite3_connection_pointer db]
146
CREATE TABLE r(a PRIMARY KEY, b, c);
147
CREATE VIRTUAL TABLE e USING echo(r);
148
INSERT INTO e VALUES(1, 2, 3);
149
INSERT INTO e VALUES('a', 'b', 'c');
151
DELETE FROM e WHERE a = 'a';
154
CREATE TABLE r2(a, b, c);
155
INSERT INTO r2 SELECT * FROM e;
156
INSERT INTO e SELECT a||'x', b, c FROM r2;