/ Artifact Content

Artifact 9f48048eeac5f8984a513aa289bd3e89b70a6bd7:

# 2007 May 10
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
# This file tests malloc failures in concert with fuzzy SQL generation.
# $Id: fuzz_malloc.test,v 1.2 2007/05/31 08:20:44 danielk1977 Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl
source $testdir/fuzz_common.tcl
source $testdir/malloc_common.tcl

set ::REPEATS 20

# Usage: do_fuzzy_malloc_test <testname> ?<options>?
#     -template
#     -sqlprep
#     -repeats
proc do_fuzzy_malloc_test {testname args} {
  set ::fuzzyopts(-repeats) $::REPEATS
  set ::fuzzyopts(-sqlprep) {}
  array set ::fuzzyopts $args

  sqlite_malloc_fail 0
  db close
  file delete test.db test.db-journal
  sqlite3 db test.db
  set ::prep $::fuzzyopts(-sqlprep)
  execsql $::prep

  for {set ii 0} {$ii < $::fuzzyopts(-repeats)} {incr ii} {
    set ::sql [subst $::fuzzyopts(-template)]
    foreach {rc res} [catchsql "$::sql"] {}
    if {$rc==0} {
      do_malloc_test $testname-$ii -sqlbody $::sql -sqlprep $::prep
    } else {
      incr ii -1

# Test malloc failure during parsing (and execution) of a fuzzily 
# generated expressions.
do_fuzzy_malloc_test fuzzy_malloc-1 -template {Select [Expr]}
do_fuzzy_malloc_test fuzzy_malloc-2 -template {[Select]}

set ::SQLPREP {
    CREATE TABLE abc(a, b, c);
    CREATE TABLE def(a, b, c);
    CREATE TABLE ghi(a, b, c);
    INSERT INTO abc VALUES(1.5, 3, 'a short string');
        'a longer string. Long enough that it doesn''t fit in Mem.zShort');
    INSERT INTO ghi VALUES(zeroblob(1000), 'hello world', -1257900987654321);
set ::TableList  [list abc def ghi]
set ::ColumnList [list a b c]

do_fuzzy_malloc_test fuzzy_malloc-3 \
  -template {[Select]}              \
  -sqlprep $::SQLPREP

sqlite_malloc_fail 0