xref: /sqlite-3.40.0/test/attachmalloc.test (revision 85c23c61)
1# 2005 September 19
2#
3# The author disclaims copyright to this source code.  In place of
4# a legal notice, here is a blessing:
5#
6#    May you do good and not evil.
7#    May you find forgiveness for yourself and forgive others.
8#    May you share freely, never taking more than you give.
9#
10#*************************************************************************
11# This file implements regression tests for SQLite library.  The
12# focus of this script is testing the ATTACH statement and
13# specifically out-of-memory conditions within that command.
14#
15# $Id: attachmalloc.test,v 1.1 2005/08/20 03:03:04 drh Exp $
16#
17
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
21# Usage: do_malloc_test <test name> <options...>
22#
23# The first argument, <test number>, is an integer used to name the
24# tests executed by this proc. Options are as follows:
25#
26#     -tclprep          TCL script to run to prepare test.
27#     -sqlprep          SQL script to run to prepare test.
28#     -tclbody          TCL script to run with malloc failure simulation.
29#     -sqlbody          TCL script to run with malloc failure simulation.
30#     -cleanup          TCL script to run after the test.
31#
32# This command runs a series of tests to verify SQLite's ability
33# to handle an out-of-memory condition gracefully. It is assumed
34# that if this condition occurs a malloc() call will return a
35# NULL pointer. Linux, for example, doesn't do that by default. See
36# the "BUGS" section of malloc(3).
37#
38# Each iteration of a loop, the TCL commands in any argument passed
39# to the -tclbody switch, followed by the SQL commands in any argument
40# passed to the -sqlbody switch are executed. Each iteration the
41# Nth call to sqliteMalloc() is made to fail, where N is increased
42# each time the loop runs starting from 1. When all commands execute
43# successfully, the loop ends.
44#
45proc do_malloc_test {tn args} {
46  array set ::mallocopts $args
47
48  set ::go 1
49  for {set ::n 1} {$::go} {incr ::n} {
50
51    do_test $tn.$::n {
52
53      sqlite_malloc_fail 0
54      catch {db close}
55      catch {file delete -force test.db}
56      catch {file delete -force test.db-journal}
57      catch {file delete -force test2.db}
58      catch {file delete -force test2.db-journal}
59      set ::DB [sqlite3 db test.db]
60
61      if {[info exists ::mallocopts(-tclprep)]} {
62        eval $::mallocopts(-tclprep)
63      }
64      if {[info exists ::mallocopts(-sqlprep)]} {
65        execsql $::mallocopts(-sqlprep)
66      }
67
68      sqlite_malloc_fail $::n
69      set ::mallocbody {}
70      if {[info exists ::mallocopts(-tclbody)]} {
71        append ::mallocbody "$::mallocopts(-tclbody)\n"
72      }
73      if {[info exists ::mallocopts(-sqlbody)]} {
74        append ::mallocbody "db eval {$::mallocopts(-sqlbody)}"
75      }
76
77      set v [catch $::mallocbody msg]
78
79      set leftover [lindex [sqlite_malloc_stat] 2]
80      if {$leftover>0} {
81        if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v  Message=$msg"}
82        set ::go 0
83        set v {1 1}
84      } else {
85        set v2 [expr {$msg=="" || $msg=="out of memory"}]
86        if {!$v2} {puts "\nError message returned: $msg"}
87        lappend v $v2
88      }
89    } {1 1}
90    sqlite_malloc_fail 0
91
92    if {[info exists ::mallocopts(-cleanup)]} {
93      catch $::mallocopts(-cleanup)
94    }
95  }
96  unset ::mallocopts
97}
98
99do_malloc_test attachmalloc-1 -tclprep {
100  db close
101  for {set i 2} {$i<=4} {incr i} {
102    file delete -force test$i.db
103    file delete -force test$i.db-journal
104  }
105} -tclbody {
106  if {[catch {sqlite3 db test.db}]} {
107    error "out of memory"
108  }
109} -sqlbody {
110  ATTACH 'test2.db' AS two;
111  CREATE TABLE two.t1(x);
112  ATTACH 'test3.db' AS three;
113  CREATE TABLE three.t1(x);
114  ATTACH 'test4.db' AS four;
115  CREATE TABLE four.t1(x);
116}
117
118finish_test
119