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