xref: /sqlite-3.40.0/test/fuzz_malloc.test (revision 44548ecc)
1c9cf901dSdanielk1977#
2c9cf901dSdanielk1977# 2007 May 10
3c9cf901dSdanielk1977#
4c9cf901dSdanielk1977# The author disclaims copyright to this source code.  In place of
5c9cf901dSdanielk1977# a legal notice, here is a blessing:
6c9cf901dSdanielk1977#
7c9cf901dSdanielk1977#    May you do good and not evil.
8c9cf901dSdanielk1977#    May you find forgiveness for yourself and forgive others.
9c9cf901dSdanielk1977#    May you share freely, never taking more than you give.
10c9cf901dSdanielk1977#
11c9cf901dSdanielk1977#***********************************************************************
12c9cf901dSdanielk1977#
13c9cf901dSdanielk1977# This file tests malloc failures in concert with fuzzy SQL generation.
14c9cf901dSdanielk1977#
15*44548eccSdrh# $Id: fuzz_malloc.test,v 1.5 2007/06/18 12:22:43 drh Exp $
16c9cf901dSdanielk1977
17c9cf901dSdanielk1977set testdir [file dirname $argv0]
18c9cf901dSdanielk1977source $testdir/tester.tcl
199142a83dSdrh
209142a83dSdrh# Only run these tests if memory debugging is turned on.
219142a83dSdrh#
229142a83dSdrhif {[info command sqlite_malloc_stat]==""} {
239142a83dSdrh  puts "Skipping fuzz_malloc tests: not compiled with -DSQLITE_MEMDEBUG=1"
249142a83dSdrh  finish_test
259142a83dSdrh  return
269142a83dSdrh}
279142a83dSdrh
28c9cf901dSdanielk1977source $testdir/fuzz_common.tcl
29c9cf901dSdanielk1977source $testdir/malloc_common.tcl
30c9cf901dSdanielk1977
31*44548eccSdrhif {[info exists ISQUICK]} {
32*44548eccSdrh  set ::REPEATS 20
33*44548eccSdrh} elseif {[info exists SOAKTEST]} {
34*44548eccSdrh  set ::REPEATS 100
35*44548eccSdrh} else {
3643b78826Sdrh  set ::REPEATS 40
37*44548eccSdrh}
38c9cf901dSdanielk1977
39c9cf901dSdanielk1977#
40c9cf901dSdanielk1977# Usage: do_fuzzy_malloc_test <testname> ?<options>?
41c9cf901dSdanielk1977#
42c9cf901dSdanielk1977#     -template
439afe689eSdanielk1977#     -sqlprep
44c9cf901dSdanielk1977#     -repeats
45c9cf901dSdanielk1977#
46c9cf901dSdanielk1977proc do_fuzzy_malloc_test {testname args} {
47c9cf901dSdanielk1977  set ::fuzzyopts(-repeats) $::REPEATS
489afe689eSdanielk1977  set ::fuzzyopts(-sqlprep) {}
49c9cf901dSdanielk1977  array set ::fuzzyopts $args
50c9cf901dSdanielk1977
519afe689eSdanielk1977  sqlite_malloc_fail 0
529afe689eSdanielk1977  db close
539afe689eSdanielk1977  file delete test.db test.db-journal
549afe689eSdanielk1977  sqlite3 db test.db
559afe689eSdanielk1977  set ::prep $::fuzzyopts(-sqlprep)
569afe689eSdanielk1977  execsql $::prep
5743b78826Sdrh  set jj 0
58c9cf901dSdanielk1977  for {set ii 0} {$ii < $::fuzzyopts(-repeats)} {incr ii} {
5943b78826Sdrh    expr srand($jj)
6043b78826Sdrh    incr jj
61c9cf901dSdanielk1977    set ::sql [subst $::fuzzyopts(-template)]
629afe689eSdanielk1977    foreach {rc res} [catchsql "$::sql"] {}
63c9cf901dSdanielk1977    if {$rc==0} {
649afe689eSdanielk1977      do_malloc_test $testname-$ii -sqlbody $::sql -sqlprep $::prep
65c9cf901dSdanielk1977    } else {
66c9cf901dSdanielk1977      incr ii -1
67c9cf901dSdanielk1977    }
68c9cf901dSdanielk1977  }
69c9cf901dSdanielk1977}
70c9cf901dSdanielk1977
71c9cf901dSdanielk1977#----------------------------------------------------------------
72c9cf901dSdanielk1977# Test malloc failure during parsing (and execution) of a fuzzily
73c9cf901dSdanielk1977# generated expressions.
74c9cf901dSdanielk1977#
75c9cf901dSdanielk1977do_fuzzy_malloc_test fuzzy_malloc-1 -template {Select [Expr]}
769afe689eSdanielk1977do_fuzzy_malloc_test fuzzy_malloc-2 -template {[Select]}
779afe689eSdanielk1977
789afe689eSdanielk1977set ::SQLPREP {
799afe689eSdanielk1977  BEGIN;
809afe689eSdanielk1977    CREATE TABLE abc(a, b, c);
819afe689eSdanielk1977    CREATE TABLE def(a, b, c);
829afe689eSdanielk1977    CREATE TABLE ghi(a, b, c);
839afe689eSdanielk1977    INSERT INTO abc VALUES(1.5, 3, 'a short string');
849afe689eSdanielk1977    INSERT INTO def VALUES(NULL, X'ABCDEF',
859afe689eSdanielk1977        'a longer string. Long enough that it doesn''t fit in Mem.zShort');
869afe689eSdanielk1977    INSERT INTO ghi VALUES(zeroblob(1000), 'hello world', -1257900987654321);
879afe689eSdanielk1977  COMMIT;
889afe689eSdanielk1977}
899afe689eSdanielk1977set ::TableList  [list abc def ghi]
909afe689eSdanielk1977set ::ColumnList [list a b c]
919afe689eSdanielk1977
929afe689eSdanielk1977do_fuzzy_malloc_test fuzzy_malloc-3 \
939afe689eSdanielk1977  -template {[Select]}              \
949afe689eSdanielk1977  -sqlprep $::SQLPREP
95c9cf901dSdanielk1977
96c9cf901dSdanielk1977sqlite_malloc_fail 0
97c9cf901dSdanielk1977finish_test
98