blob: d918a9da9dc832304bbb6d640d015a1a257617fa [file] [log] [blame]
drh0bbaa1b2005-08-19 19:14:12 +00001# 2005 September 19
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# This file implements regression tests for SQLite library. The
12# focus of this script is testing the ALTER TABLE statement and
13# specifically out-of-memory conditions within that command.
14#
15# $Id: altermalloc.test,v 1.1 2005/08/19 19:14:13 drh Exp $
16#
17
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
21# If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
22ifcapable !altertable {
23 finish_test
24 return
25}
26
27# Usage: do_malloc_test <test name> <options...>
28#
29# The first argument, <test number>, is an integer used to name the
30# tests executed by this proc. Options are as follows:
31#
32# -tclprep TCL script to run to prepare test.
33# -sqlprep SQL script to run to prepare test.
34# -tclbody TCL script to run with malloc failure simulation.
35# -sqlbody TCL script to run with malloc failure simulation.
36# -cleanup TCL script to run after the test.
37#
38# This command runs a series of tests to verify SQLite's ability
39# to handle an out-of-memory condition gracefully. It is assumed
40# that if this condition occurs a malloc() call will return a
41# NULL pointer. Linux, for example, doesn't do that by default. See
42# the "BUGS" section of malloc(3).
43#
44# Each iteration of a loop, the TCL commands in any argument passed
45# to the -tclbody switch, followed by the SQL commands in any argument
46# passed to the -sqlbody switch are executed. Each iteration the
47# Nth call to sqliteMalloc() is made to fail, where N is increased
48# each time the loop runs starting from 1. When all commands execute
49# successfully, the loop ends.
50#
51proc do_malloc_test {tn args} {
52 array set ::mallocopts $args
53
54 set ::go 1
55 for {set ::n 1} {$::go} {incr ::n} {
56
57 do_test $tn.$::n {
58
59 sqlite_malloc_fail 0
60 catch {db close}
61 catch {file delete -force test.db}
62 catch {file delete -force test.db-journal}
63 catch {file delete -force test2.db}
64 catch {file delete -force test2.db-journal}
65 set ::DB [sqlite3 db test.db]
66
67 if {[info exists ::mallocopts(-tclprep)]} {
68 eval $::mallocopts(-tclprep)
69 }
70 if {[info exists ::mallocopts(-sqlprep)]} {
71 execsql $::mallocopts(-sqlprep)
72 }
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
83 set v [catch $::mallocbody msg]
84
85 set leftover [lindex [sqlite_malloc_stat] 2]
86 if {$leftover>0} {
87 if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v Message=$msg"}
88 set ::go 0
89 set v {1 1}
90 } else {
91 set v2 [expr {$msg=="" || $msg=="out of memory"}]
92 if {!$v2} {puts "\nError message returned: $msg"}
93 lappend v $v2
94 }
95 } {1 1}
96 sqlite_malloc_fail 0
97
98 if {[info exists ::mallocopts(-cleanup)]} {
99 catch $::mallocopts(-cleanup)
100 }
101 }
102 unset ::mallocopts
103}
104
105do_malloc_test altermalloc-1 -tclprep {
106 db close
107} -tclbody {
108 if {[catch {sqlite3 db test.db}]} {
109 error "out of memory"
110 }
111} -sqlbody {
112 CREATE TABLE t1(a int);
113 ALTER TABLE t1 ADD COLUMN b INTEGER DEFAULT NULL;
114 ALTER TABLE t1 ADD COLUMN c TEXT DEFAULT 'default-text';
115 ALTER TABLE t1 RENAME TO t2;
116}
117
118finish_test