xref: /sqlite-3.40.0/test/fuzz_malloc.test (revision 253c6ee1)
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
20c9cf901dSdanielk1977source $testdir/malloc_common.tcl
21222a757dSdanielk1977source $testdir/fuzz_common.tcl
22c9cf901dSdanielk1977
23430e74cdSdanif {[info exists G(isquick)]} {
2444548eccSdrh  set ::REPEATS 20
25430e74cdSdan} elseif {[info exists G(issoak)]} {
2644548eccSdrh  set ::REPEATS 100
2744548eccSdrh} else {
2843b78826Sdrh  set ::REPEATS 40
2944548eccSdrh}
30c9cf901dSdanielk1977
31c9cf901dSdanielk1977#
32c9cf901dSdanielk1977# Usage: do_fuzzy_malloc_test <testname> ?<options>?
33c9cf901dSdanielk1977#
34c9cf901dSdanielk1977#     -template
359afe689eSdanielk1977#     -sqlprep
36c9cf901dSdanielk1977#     -repeats
37c9cf901dSdanielk1977#
38c9cf901dSdanielk1977proc do_fuzzy_malloc_test {testname args} {
39c9cf901dSdanielk1977  set ::fuzzyopts(-repeats) $::REPEATS
409afe689eSdanielk1977  set ::fuzzyopts(-sqlprep) {}
41c9cf901dSdanielk1977  array set ::fuzzyopts $args
42c9cf901dSdanielk1977
437751940dSdanielk1977  sqlite3_memdebug_fail -1
449afe689eSdanielk1977  db close
45fda06befSmistachkin  delete_file test.db test.db-journal
469afe689eSdanielk1977  sqlite3 db test.db
479afe689eSdanielk1977  set ::prep $::fuzzyopts(-sqlprep)
489afe689eSdanielk1977  execsql $::prep
4943b78826Sdrh  set jj 0
50c9cf901dSdanielk1977  for {set ii 0} {$ii < $::fuzzyopts(-repeats)} {incr ii} {
5143b78826Sdrh    expr srand($jj)
5243b78826Sdrh    incr jj
53c9cf901dSdanielk1977    set ::sql [subst $::fuzzyopts(-template)]
547d10d5a6Sdrh    # puts fuzyy-sql=\[$::sql\]; flush stdout
55*0ef54fa6Sdan    foreach {rc ::fmtres} [catchsql "$::sql"] {}
56c9cf901dSdanielk1977    if {$rc==0} {
571e080749Sdan      set nErr1 [set_test_counter errors]
58*0ef54fa6Sdan      do_faultsim_test $testname-$ii -faults oom* -body {
59*0ef54fa6Sdan        execsql $::sql
60*0ef54fa6Sdan      } -test {
61*0ef54fa6Sdan        if {$testrc && $testresult!="datatype mismatch"} {
62*0ef54fa6Sdan          faultsim_test_result {0 {}}
63*0ef54fa6Sdan        }
64*0ef54fa6Sdan      }
651e080749Sdan      if {[set_test_counter errors]>$nErr1} {
661e080749Sdan        puts "Previous fuzzy-sql=\[$::sql\]"
671e080749Sdan        flush stdout
681e080749Sdan      }
69c9cf901dSdanielk1977    } else {
70c9cf901dSdanielk1977      incr ii -1
71c9cf901dSdanielk1977    }
72c9cf901dSdanielk1977  }
73c9cf901dSdanielk1977}
74c9cf901dSdanielk1977
75c9cf901dSdanielk1977#----------------------------------------------------------------
76c9cf901dSdanielk1977# Test malloc failure during parsing (and execution) of a fuzzily
77c9cf901dSdanielk1977# generated expressions.
78c9cf901dSdanielk1977#
79c9cf901dSdanielk1977do_fuzzy_malloc_test fuzzy_malloc-1 -template {Select [Expr]}
809afe689eSdanielk1977do_fuzzy_malloc_test fuzzy_malloc-2 -template {[Select]}
819afe689eSdanielk1977
829afe689eSdanielk1977set ::SQLPREP {
839afe689eSdanielk1977  BEGIN;
849afe689eSdanielk1977    CREATE TABLE abc(a, b, c);
859afe689eSdanielk1977    CREATE TABLE def(a, b, c);
869afe689eSdanielk1977    CREATE TABLE ghi(a, b, c);
879afe689eSdanielk1977    INSERT INTO abc VALUES(1.5, 3, 'a short string');
889afe689eSdanielk1977    INSERT INTO def VALUES(NULL, X'ABCDEF',
899afe689eSdanielk1977        'a longer string. Long enough that it doesn''t fit in Mem.zShort');
909afe689eSdanielk1977    INSERT INTO ghi VALUES(zeroblob(1000), 'hello world', -1257900987654321);
919afe689eSdanielk1977  COMMIT;
929afe689eSdanielk1977}
939afe689eSdanielk1977set ::TableList  [list abc def ghi]
949afe689eSdanielk1977set ::ColumnList [list a b c]
959afe689eSdanielk1977
969afe689eSdanielk1977do_fuzzy_malloc_test fuzzy_malloc-3 \
979afe689eSdanielk1977  -template {[Select]}              \
989afe689eSdanielk1977  -sqlprep $::SQLPREP
99c9cf901dSdanielk1977
100c9cf901dSdanielk1977finish_test
101