xref: /sqlite-3.40.0/test/vtab_err.test (revision d230f648)
1# 2006 June 10
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#
12# $Id: vtab_err.test,v 1.5 2007/04/19 11:09:02 danielk1977 Exp $
13
14set testdir [file dirname $argv0]
15source $testdir/tester.tcl
16
17# Only run these tests if memory debugging is turned on.
18#
19if {[info command sqlite_malloc_stat]==""} {
20  puts "Skipping vtab_err tests: not compiled with -DSQLITE_MEMDEBUG=1"
21  finish_test
22  return
23}
24
25ifcapable !vtab {
26  finish_test
27  return
28}
29
30# Usage: do_malloc_test <test number> <options...>
31#
32# The first argument, <test number>, is an integer used to name the
33# tests executed by this proc. Options are as follows:
34#
35#     -tclprep          TCL script to run to prepare test.
36#     -sqlprep          SQL script to run to prepare test.
37#     -tclbody          TCL script to run with malloc failure simulation.
38#     -sqlbody          TCL script to run with malloc failure simulation.
39#     -cleanup          TCL script to run after the test.
40#
41# This command runs a series of tests to verify SQLite's ability
42# to handle an out-of-memory condition gracefully. It is assumed
43# that if this condition occurs a malloc() call will return a
44# NULL pointer. Linux, for example, doesn't do that by default. See
45# the "BUGS" section of malloc(3).
46#
47# Each iteration of a loop, the TCL commands in any argument passed
48# to the -tclbody switch, followed by the SQL commands in any argument
49# passed to the -sqlbody switch are executed. Each iteration the
50# Nth call to sqliteMalloc() is made to fail, where N is increased
51# each time the loop runs starting from 1. When all commands execute
52# successfully, the loop ends.
53#
54proc do_malloc_test {tn args} {
55  array unset ::mallocopts
56  array set ::mallocopts $args
57
58  set ::go 1
59  for {set ::n 1} {$::go && $::n < 50000} {incr ::n} {
60    do_test $tn.$::n {
61
62      # Remove all traces of database files test.db and test2.db from the files
63      # system. Then open (empty database) "test.db" with the handle [db].
64      #
65      sqlite_malloc_fail 0
66      catch {db close}
67      catch {file delete -force test.db}
68      catch {file delete -force test.db-journal}
69      catch {file delete -force test2.db}
70      catch {file delete -force test2.db-journal}
71      catch {sqlite3 db test.db}
72      set ::DB [sqlite3_connection_pointer db]
73
74      # Execute any -tclprep and -sqlprep scripts.
75      #
76      if {[info exists ::mallocopts(-tclprep)]} {
77        eval $::mallocopts(-tclprep)
78      }
79      if {[info exists ::mallocopts(-sqlprep)]} {
80        execsql $::mallocopts(-sqlprep)
81      }
82
83      # Now set the ${::n}th malloc() to fail and execute the -tclbody and
84      # -sqlbody scripts.
85      #
86      sqlite_malloc_fail $::n
87      set ::mallocbody {}
88      if {[info exists ::mallocopts(-tclbody)]} {
89        append ::mallocbody "$::mallocopts(-tclbody)\n"
90      }
91      if {[info exists ::mallocopts(-sqlbody)]} {
92        append ::mallocbody "db eval {$::mallocopts(-sqlbody)}"
93      }
94      set v [catch $::mallocbody msg]
95
96      # If the test fails (if $v!=0) and the database connection actually
97      # exists, make sure the failure code is SQLITE_NOMEM.
98      if {$v&&[info command db]=="db"&&[info exists ::mallocopts(-sqlbody)]} {
99        if {[db errorcode]!=7 && $msg!="vtable constructor failed: e"} {
100          set v 999
101        }
102      }
103
104      set leftover [lindex [sqlite_malloc_stat] 2]
105      if {$leftover>0} {
106        if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v  Message=$msg"}
107        set ::go 0
108        if {$v} {
109          puts "\nError message returned: $msg"
110        } else {
111          set v {1 1}
112        }
113      } else {
114        set v2 [expr {
115          $msg == "" || $msg == "out of memory" ||
116          $msg == "vtable constructor failed: e"
117        }]
118        if {!$v2} {puts "\nError message returned: $msg"}
119        lappend v $v2
120      }
121    } {1 1}
122
123    if {[info exists ::mallocopts(-cleanup)]} {
124      catch [list uplevel #0 $::mallocopts(-cleanup)] msg
125    }
126  }
127  unset ::mallocopts
128}
129
130unset -nocomplain echo_module_begin_fail
131do_ioerr_test vtab_err-1 -tclprep {
132  register_echo_module [sqlite3_connection_pointer db]
133} -sqlbody {
134  BEGIN;
135  CREATE TABLE r(a PRIMARY KEY, b, c);
136  CREATE VIRTUAL TABLE e USING echo(r);
137  INSERT INTO e VALUES(1, 2, 3);
138  INSERT INTO e VALUES('a', 'b', 'c');
139  UPDATE e SET c = 10;
140  DELETE FROM e WHERE a = 'a';
141  COMMIT;
142  BEGIN;
143    CREATE TABLE r2(a, b, c);
144    INSERT INTO r2 SELECT * FROM e;
145    INSERT INTO e SELECT a||'x', b, c FROM r2;
146  COMMIT;
147}
148
149do_malloc_test vtab_err-2 -tclprep {
150  register_echo_module [sqlite3_connection_pointer db]
151} -sqlbody {
152  BEGIN;
153  CREATE TABLE r(a PRIMARY KEY, b, c);
154  CREATE VIRTUAL TABLE e USING echo(r);
155  INSERT INTO e VALUES(1, 2, 3);
156  INSERT INTO e VALUES('a', 'b', 'c');
157  UPDATE e SET c = 10;
158  DELETE FROM e WHERE a = 'a';
159  COMMIT;
160  BEGIN;
161    CREATE TABLE r2(a, b, c);
162    INSERT INTO r2 SELECT * FROM e;
163    INSERT INTO e SELECT a||'x', b, c FROM r2;
164  COMMIT;
165}
166
167sqlite_malloc_fail 0
168finish_test
169