xref: /sqlite-3.40.0/test/fuzz_malloc.test (revision fda06bef)
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#
157d10d5a6Sdrh# $Id: fuzz_malloc.test,v 1.10 2008/08/20 16:35:10 drh Exp $
16c9cf901dSdanielk1977
17c9cf901dSdanielk1977set testdir [file dirname $argv0]
18c9cf901dSdanielk1977source $testdir/tester.tcl
199142a83dSdrh
204753889cSdrhifcapable !memdebug {
214753889cSdrh  finish_test
224753889cSdrh  return
234753889cSdrh}
244753889cSdrh
25c9cf901dSdanielk1977source $testdir/malloc_common.tcl
26222a757dSdanielk1977source $testdir/fuzz_common.tcl
27c9cf901dSdanielk1977
28430e74cdSdanif {[info exists G(isquick)]} {
2944548eccSdrh  set ::REPEATS 20
30430e74cdSdan} elseif {[info exists G(issoak)]} {
3144548eccSdrh  set ::REPEATS 100
3244548eccSdrh} else {
3343b78826Sdrh  set ::REPEATS 40
3444548eccSdrh}
35c9cf901dSdanielk1977
36c9cf901dSdanielk1977#
37c9cf901dSdanielk1977# Usage: do_fuzzy_malloc_test <testname> ?<options>?
38c9cf901dSdanielk1977#
39c9cf901dSdanielk1977#     -template
409afe689eSdanielk1977#     -sqlprep
41c9cf901dSdanielk1977#     -repeats
42c9cf901dSdanielk1977#
43c9cf901dSdanielk1977proc do_fuzzy_malloc_test {testname args} {
44c9cf901dSdanielk1977  set ::fuzzyopts(-repeats) $::REPEATS
459afe689eSdanielk1977  set ::fuzzyopts(-sqlprep) {}
46c9cf901dSdanielk1977  array set ::fuzzyopts $args
47c9cf901dSdanielk1977
487751940dSdanielk1977  sqlite3_memdebug_fail -1
499afe689eSdanielk1977  db close
50*fda06befSmistachkin  delete_file test.db test.db-journal
519afe689eSdanielk1977  sqlite3 db test.db
529afe689eSdanielk1977  set ::prep $::fuzzyopts(-sqlprep)
539afe689eSdanielk1977  execsql $::prep
5443b78826Sdrh  set jj 0
55c9cf901dSdanielk1977  for {set ii 0} {$ii < $::fuzzyopts(-repeats)} {incr ii} {
5643b78826Sdrh    expr srand($jj)
5743b78826Sdrh    incr jj
58c9cf901dSdanielk1977    set ::sql [subst $::fuzzyopts(-template)]
597d10d5a6Sdrh    # puts fuzyy-sql=\[$::sql\]; flush stdout
609afe689eSdanielk1977    foreach {rc res} [catchsql "$::sql"] {}
61c9cf901dSdanielk1977    if {$rc==0} {
629afe689eSdanielk1977      do_malloc_test $testname-$ii -sqlbody $::sql -sqlprep $::prep
63c9cf901dSdanielk1977    } else {
64c9cf901dSdanielk1977      incr ii -1
65c9cf901dSdanielk1977    }
66c9cf901dSdanielk1977  }
67c9cf901dSdanielk1977}
68c9cf901dSdanielk1977
69c9cf901dSdanielk1977#----------------------------------------------------------------
70c9cf901dSdanielk1977# Test malloc failure during parsing (and execution) of a fuzzily
71c9cf901dSdanielk1977# generated expressions.
72c9cf901dSdanielk1977#
73c9cf901dSdanielk1977do_fuzzy_malloc_test fuzzy_malloc-1 -template {Select [Expr]}
749afe689eSdanielk1977do_fuzzy_malloc_test fuzzy_malloc-2 -template {[Select]}
759afe689eSdanielk1977
769afe689eSdanielk1977set ::SQLPREP {
779afe689eSdanielk1977  BEGIN;
789afe689eSdanielk1977    CREATE TABLE abc(a, b, c);
799afe689eSdanielk1977    CREATE TABLE def(a, b, c);
809afe689eSdanielk1977    CREATE TABLE ghi(a, b, c);
819afe689eSdanielk1977    INSERT INTO abc VALUES(1.5, 3, 'a short string');
829afe689eSdanielk1977    INSERT INTO def VALUES(NULL, X'ABCDEF',
839afe689eSdanielk1977        'a longer string. Long enough that it doesn''t fit in Mem.zShort');
849afe689eSdanielk1977    INSERT INTO ghi VALUES(zeroblob(1000), 'hello world', -1257900987654321);
859afe689eSdanielk1977  COMMIT;
869afe689eSdanielk1977}
879afe689eSdanielk1977set ::TableList  [list abc def ghi]
889afe689eSdanielk1977set ::ColumnList [list a b c]
899afe689eSdanielk1977
909afe689eSdanielk1977do_fuzzy_malloc_test fuzzy_malloc-3 \
919afe689eSdanielk1977  -template {[Select]}              \
929afe689eSdanielk1977  -sqlprep $::SQLPREP
93c9cf901dSdanielk1977
94c9cf901dSdanielk1977finish_test
95