blob: 86663ff33cc3a3c3c9f25210af6dbab8bf2e7792 [file] [log] [blame]
danielk1977c9cf9012007-05-30 10:36:47 +00001#
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#
drh9142a832007-06-15 13:57:19 +000015# $Id: fuzz_malloc.test,v 1.3 2007/06/15 13:57:20 drh Exp $
danielk1977c9cf9012007-05-30 10:36:47 +000016
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
drh9142a832007-06-15 13:57:19 +000019
20# Only run these tests if memory debugging is turned on.
21#
22if {[info command sqlite_malloc_stat]==""} {
23 puts "Skipping fuzz_malloc tests: not compiled with -DSQLITE_MEMDEBUG=1"
24 finish_test
25 return
26}
27
28
danielk1977c9cf9012007-05-30 10:36:47 +000029source $testdir/fuzz_common.tcl
30source $testdir/malloc_common.tcl
31
32set ::REPEATS 20
33
34#
35# Usage: do_fuzzy_malloc_test <testname> ?<options>?
36#
37# -template
danielk19779afe6892007-05-31 08:20:43 +000038# -sqlprep
danielk1977c9cf9012007-05-30 10:36:47 +000039# -repeats
40#
41proc do_fuzzy_malloc_test {testname args} {
42 set ::fuzzyopts(-repeats) $::REPEATS
danielk19779afe6892007-05-31 08:20:43 +000043 set ::fuzzyopts(-sqlprep) {}
danielk1977c9cf9012007-05-30 10:36:47 +000044 array set ::fuzzyopts $args
45
danielk19779afe6892007-05-31 08:20:43 +000046 sqlite_malloc_fail 0
47 db close
48 file delete test.db test.db-journal
49 sqlite3 db test.db
50 set ::prep $::fuzzyopts(-sqlprep)
51 execsql $::prep
52
danielk1977c9cf9012007-05-30 10:36:47 +000053 for {set ii 0} {$ii < $::fuzzyopts(-repeats)} {incr ii} {
54 set ::sql [subst $::fuzzyopts(-template)]
danielk19779afe6892007-05-31 08:20:43 +000055 foreach {rc res} [catchsql "$::sql"] {}
danielk1977c9cf9012007-05-30 10:36:47 +000056 if {$rc==0} {
danielk19779afe6892007-05-31 08:20:43 +000057 do_malloc_test $testname-$ii -sqlbody $::sql -sqlprep $::prep
danielk1977c9cf9012007-05-30 10:36:47 +000058 } else {
59 incr ii -1
60 }
61 }
62}
63
64#----------------------------------------------------------------
65# Test malloc failure during parsing (and execution) of a fuzzily
66# generated expressions.
67#
68do_fuzzy_malloc_test fuzzy_malloc-1 -template {Select [Expr]}
danielk19779afe6892007-05-31 08:20:43 +000069do_fuzzy_malloc_test fuzzy_malloc-2 -template {[Select]}
70
71set ::SQLPREP {
72 BEGIN;
73 CREATE TABLE abc(a, b, c);
74 CREATE TABLE def(a, b, c);
75 CREATE TABLE ghi(a, b, c);
76 INSERT INTO abc VALUES(1.5, 3, 'a short string');
77 INSERT INTO def VALUES(NULL, X'ABCDEF',
78 'a longer string. Long enough that it doesn''t fit in Mem.zShort');
79 INSERT INTO ghi VALUES(zeroblob(1000), 'hello world', -1257900987654321);
80 COMMIT;
81}
82set ::TableList [list abc def ghi]
83set ::ColumnList [list a b c]
84
85do_fuzzy_malloc_test fuzzy_malloc-3 \
86 -template {[Select]} \
87 -sqlprep $::SQLPREP
danielk1977c9cf9012007-05-30 10:36:47 +000088
89sqlite_malloc_fail 0
90finish_test