blob: f2209aaba56880f6823f8242492072f94cc05157 [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#
drh7d10d5a2008-08-20 16:35:10 +000015# $Id: fuzz_malloc.test,v 1.10 2008/08/20 16:35:10 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
drh47538892007-09-03 15:42:47 +000020ifcapable !memdebug {
21 finish_test
22 return
23}
24
danielk1977c9cf9012007-05-30 10:36:47 +000025source $testdir/malloc_common.tcl
danielk1977222a7572007-08-25 13:37:48 +000026source $testdir/fuzz_common.tcl
danielk1977c9cf9012007-05-30 10:36:47 +000027
dan430e74c2010-06-07 17:47:26 +000028if {[info exists G(isquick)]} {
drh44548ec2007-06-18 12:22:43 +000029 set ::REPEATS 20
dan430e74c2010-06-07 17:47:26 +000030} elseif {[info exists G(issoak)]} {
drh44548ec2007-06-18 12:22:43 +000031 set ::REPEATS 100
32} else {
33 set ::REPEATS 40
34}
danielk1977c9cf9012007-05-30 10:36:47 +000035
36#
37# Usage: do_fuzzy_malloc_test <testname> ?<options>?
38#
39# -template
danielk19779afe6892007-05-31 08:20:43 +000040# -sqlprep
danielk1977c9cf9012007-05-30 10:36:47 +000041# -repeats
42#
43proc do_fuzzy_malloc_test {testname args} {
44 set ::fuzzyopts(-repeats) $::REPEATS
danielk19779afe6892007-05-31 08:20:43 +000045 set ::fuzzyopts(-sqlprep) {}
danielk1977c9cf9012007-05-30 10:36:47 +000046 array set ::fuzzyopts $args
47
danielk197777519402007-08-30 11:48:31 +000048 sqlite3_memdebug_fail -1
danielk19779afe6892007-05-31 08:20:43 +000049 db close
50 file delete test.db test.db-journal
51 sqlite3 db test.db
52 set ::prep $::fuzzyopts(-sqlprep)
53 execsql $::prep
drh43b78822007-06-15 17:03:14 +000054 set jj 0
danielk1977c9cf9012007-05-30 10:36:47 +000055 for {set ii 0} {$ii < $::fuzzyopts(-repeats)} {incr ii} {
drh43b78822007-06-15 17:03:14 +000056 expr srand($jj)
57 incr jj
danielk1977c9cf9012007-05-30 10:36:47 +000058 set ::sql [subst $::fuzzyopts(-template)]
drh7d10d5a2008-08-20 16:35:10 +000059 # puts fuzyy-sql=\[$::sql\]; flush stdout
danielk19779afe6892007-05-31 08:20:43 +000060 foreach {rc res} [catchsql "$::sql"] {}
danielk1977c9cf9012007-05-30 10:36:47 +000061 if {$rc==0} {
danielk19779afe6892007-05-31 08:20:43 +000062 do_malloc_test $testname-$ii -sqlbody $::sql -sqlprep $::prep
danielk1977c9cf9012007-05-30 10:36:47 +000063 } else {
64 incr ii -1
65 }
66 }
67}
68
69#----------------------------------------------------------------
70# Test malloc failure during parsing (and execution) of a fuzzily
71# generated expressions.
72#
73do_fuzzy_malloc_test fuzzy_malloc-1 -template {Select [Expr]}
danielk19779afe6892007-05-31 08:20:43 +000074do_fuzzy_malloc_test fuzzy_malloc-2 -template {[Select]}
75
76set ::SQLPREP {
77 BEGIN;
78 CREATE TABLE abc(a, b, c);
79 CREATE TABLE def(a, b, c);
80 CREATE TABLE ghi(a, b, c);
81 INSERT INTO abc VALUES(1.5, 3, 'a short string');
82 INSERT INTO def VALUES(NULL, X'ABCDEF',
83 'a longer string. Long enough that it doesn''t fit in Mem.zShort');
84 INSERT INTO ghi VALUES(zeroblob(1000), 'hello world', -1257900987654321);
85 COMMIT;
86}
87set ::TableList [list abc def ghi]
88set ::ColumnList [list a b c]
89
90do_fuzzy_malloc_test fuzzy_malloc-3 \
91 -template {[Select]} \
92 -sqlprep $::SQLPREP
danielk1977c9cf9012007-05-30 10:36:47 +000093
danielk1977c9cf9012007-05-30 10:36:47 +000094finish_test