xref: /sqlite-3.40.0/test/fuzz_malloc.test (revision 44548ecc)
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#
15# $Id: fuzz_malloc.test,v 1.5 2007/06/18 12:22:43 drh Exp $
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
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
28source $testdir/fuzz_common.tcl
29source $testdir/malloc_common.tcl
30
31if {[info exists ISQUICK]} {
32  set ::REPEATS 20
33} elseif {[info exists SOAKTEST]} {
34  set ::REPEATS 100
35} else {
36  set ::REPEATS 40
37}
38
39#
40# Usage: do_fuzzy_malloc_test <testname> ?<options>?
41#
42#     -template
43#     -sqlprep
44#     -repeats
45#
46proc do_fuzzy_malloc_test {testname args} {
47  set ::fuzzyopts(-repeats) $::REPEATS
48  set ::fuzzyopts(-sqlprep) {}
49  array set ::fuzzyopts $args
50
51  sqlite_malloc_fail 0
52  db close
53  file delete test.db test.db-journal
54  sqlite3 db test.db
55  set ::prep $::fuzzyopts(-sqlprep)
56  execsql $::prep
57  set jj 0
58  for {set ii 0} {$ii < $::fuzzyopts(-repeats)} {incr ii} {
59    expr srand($jj)
60    incr jj
61    set ::sql [subst $::fuzzyopts(-template)]
62    foreach {rc res} [catchsql "$::sql"] {}
63    if {$rc==0} {
64      do_malloc_test $testname-$ii -sqlbody $::sql -sqlprep $::prep
65    } else {
66      incr ii -1
67    }
68  }
69}
70
71#----------------------------------------------------------------
72# Test malloc failure during parsing (and execution) of a fuzzily
73# generated expressions.
74#
75do_fuzzy_malloc_test fuzzy_malloc-1 -template {Select [Expr]}
76do_fuzzy_malloc_test fuzzy_malloc-2 -template {[Select]}
77
78set ::SQLPREP {
79  BEGIN;
80    CREATE TABLE abc(a, b, c);
81    CREATE TABLE def(a, b, c);
82    CREATE TABLE ghi(a, b, c);
83    INSERT INTO abc VALUES(1.5, 3, 'a short string');
84    INSERT INTO def VALUES(NULL, X'ABCDEF',
85        'a longer string. Long enough that it doesn''t fit in Mem.zShort');
86    INSERT INTO ghi VALUES(zeroblob(1000), 'hello world', -1257900987654321);
87  COMMIT;
88}
89set ::TableList  [list abc def ghi]
90set ::ColumnList [list a b c]
91
92do_fuzzy_malloc_test fuzzy_malloc-3 \
93  -template {[Select]}              \
94  -sqlprep $::SQLPREP
95
96sqlite_malloc_fail 0
97finish_test
98