danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 1 | # |
| 2 | # 2007 May 10 |
| 3 | # |
| 4 | # The author disclaims copyright to this source code. In place of |
| 5 | # a legal notice, here is a blessing: |
| 6 | # |
| 7 | # May you do good and not evil. |
| 8 | # May you find forgiveness for yourself and forgive others. |
| 9 | # May you share freely, never taking more than you give. |
| 10 | # |
| 11 | #*********************************************************************** |
| 12 | # |
| 13 | # This file tests malloc failures in concert with fuzzy SQL generation. |
| 14 | # |
drh | 43b7882 | 2007-06-15 17:03:14 +0000 | [diff] [blame^] | 15 | # $Id: fuzz_malloc.test,v 1.4 2007/06/15 17:03:15 drh Exp $ |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 16 | |
| 17 | set testdir [file dirname $argv0] |
| 18 | source $testdir/tester.tcl |
drh | 9142a83 | 2007-06-15 13:57:19 +0000 | [diff] [blame] | 19 | |
| 20 | # Only run these tests if memory debugging is turned on. |
| 21 | # |
| 22 | if {[info command sqlite_malloc_stat]==""} { |
| 23 | puts "Skipping fuzz_malloc tests: not compiled with -DSQLITE_MEMDEBUG=1" |
| 24 | finish_test |
| 25 | return |
| 26 | } |
| 27 | |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 28 | source $testdir/fuzz_common.tcl |
| 29 | source $testdir/malloc_common.tcl |
| 30 | |
drh | 43b7882 | 2007-06-15 17:03:14 +0000 | [diff] [blame^] | 31 | set ::REPEATS 40 |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 32 | |
| 33 | # |
| 34 | # Usage: do_fuzzy_malloc_test <testname> ?<options>? |
| 35 | # |
| 36 | # -template |
danielk1977 | 9afe689 | 2007-05-31 08:20:43 +0000 | [diff] [blame] | 37 | # -sqlprep |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 38 | # -repeats |
| 39 | # |
| 40 | proc do_fuzzy_malloc_test {testname args} { |
| 41 | set ::fuzzyopts(-repeats) $::REPEATS |
danielk1977 | 9afe689 | 2007-05-31 08:20:43 +0000 | [diff] [blame] | 42 | set ::fuzzyopts(-sqlprep) {} |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 43 | array set ::fuzzyopts $args |
| 44 | |
danielk1977 | 9afe689 | 2007-05-31 08:20:43 +0000 | [diff] [blame] | 45 | sqlite_malloc_fail 0 |
| 46 | db close |
| 47 | file delete test.db test.db-journal |
| 48 | sqlite3 db test.db |
| 49 | set ::prep $::fuzzyopts(-sqlprep) |
| 50 | execsql $::prep |
drh | 43b7882 | 2007-06-15 17:03:14 +0000 | [diff] [blame^] | 51 | set jj 0 |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 52 | for {set ii 0} {$ii < $::fuzzyopts(-repeats)} {incr ii} { |
drh | 43b7882 | 2007-06-15 17:03:14 +0000 | [diff] [blame^] | 53 | expr srand($jj) |
| 54 | incr jj |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 55 | set ::sql [subst $::fuzzyopts(-template)] |
danielk1977 | 9afe689 | 2007-05-31 08:20:43 +0000 | [diff] [blame] | 56 | foreach {rc res} [catchsql "$::sql"] {} |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 57 | if {$rc==0} { |
danielk1977 | 9afe689 | 2007-05-31 08:20:43 +0000 | [diff] [blame] | 58 | do_malloc_test $testname-$ii -sqlbody $::sql -sqlprep $::prep |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 59 | } else { |
| 60 | incr ii -1 |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | #---------------------------------------------------------------- |
| 66 | # Test malloc failure during parsing (and execution) of a fuzzily |
| 67 | # generated expressions. |
| 68 | # |
| 69 | do_fuzzy_malloc_test fuzzy_malloc-1 -template {Select [Expr]} |
danielk1977 | 9afe689 | 2007-05-31 08:20:43 +0000 | [diff] [blame] | 70 | do_fuzzy_malloc_test fuzzy_malloc-2 -template {[Select]} |
| 71 | |
| 72 | set ::SQLPREP { |
| 73 | BEGIN; |
| 74 | CREATE TABLE abc(a, b, c); |
| 75 | CREATE TABLE def(a, b, c); |
| 76 | CREATE TABLE ghi(a, b, c); |
| 77 | INSERT INTO abc VALUES(1.5, 3, 'a short string'); |
| 78 | INSERT INTO def VALUES(NULL, X'ABCDEF', |
| 79 | 'a longer string. Long enough that it doesn''t fit in Mem.zShort'); |
| 80 | INSERT INTO ghi VALUES(zeroblob(1000), 'hello world', -1257900987654321); |
| 81 | COMMIT; |
| 82 | } |
| 83 | set ::TableList [list abc def ghi] |
| 84 | set ::ColumnList [list a b c] |
| 85 | |
| 86 | do_fuzzy_malloc_test fuzzy_malloc-3 \ |
| 87 | -template {[Select]} \ |
| 88 | -sqlprep $::SQLPREP |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 89 | |
| 90 | sqlite_malloc_fail 0 |
| 91 | finish_test |