blob: eee0afc68efe14ea9303236f98c9b49efef3a4c4 [file] [log] [blame]
danielk197700fd9572005-12-07 06:27:43 +00001# 2005 November 30
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# This file contains tests to ensure that the library handles malloc() failures
13# correctly. The emphasis in this file is on sqlite3_column_XXX() APIs.
14#
15# $Id: malloc4.test,v 1.1 2005/12/07 06:27:45 danielk1977 Exp $
16
17#---------------------------------------------------------------------------
18# NOTES ON EXPECTED BEHAVIOUR
19#
20#---------------------------------------------------------------------------
21
22set testdir [file dirname $argv0]
23source $testdir/tester.tcl
24
25# Only run these tests if memory debugging is turned on.
26if {[info command sqlite_malloc_stat]==""} {
27 puts "Skipping malloc tests: not compiled with -DSQLITE_MEMDEBUG..."
28 finish_test
29 return
30}
31
32
33proc do_stmt_test {id sql} {
34 set ::sql $sql
35 set go 1
36 for {set n 1} {$go} {incr n} {
37 set testid "malloc4-$id.(iFail $n)"
38
39 # Prepare the statement
40 do_test ${testid}.1 {
41 set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL]
42 expr [string length $::STMT] > 0
43 } {1}
44
45 # Set the Nth malloc() to fail.
46 sqlite_malloc_fail $n
47
48 # Test malloc failure in the _name(), _name16(), decltype() and
49 # decltype16() APIs. Calls that occur after the malloc() failure should
50 # return NULL. No error is raised though.
51 do_test ${testid}.2.1 {
52 set mf1 [expr [lindex [sqlite_malloc_stat] 2] <= 0]
53 set ::name8 [sqlite3_column_name $::STMT 0]
54 set mf2 [expr [lindex [sqlite_malloc_stat] 2] <= 0]
55 expr {$mf1 == $mf2 || $::name8 == ""}
56 } {1}
57 do_test ${testid}.2.2 {
58 set mf1 [expr [lindex [sqlite_malloc_stat] 2] <= 0]
59 set ::name16 [sqlite3_column_name16 $::STMT 0]
60 set ::name16 [encoding convertfrom unicode $::name16]
61 set ::name16 [string range $::name16 0 end-1]
62 set mf2 [expr [lindex [sqlite_malloc_stat] 2] <= 0]
63 expr {$mf1 == $mf2 || $::name16 == ""}
64 } {1}
65 do_test ${testid}.2.3 {
66 set mf1 [expr [lindex [sqlite_malloc_stat] 2] <= 0]
67 set ::name8_2 [sqlite3_column_name $::STMT 0]
68 set mf2 [expr [lindex [sqlite_malloc_stat] 2] <= 0]
69 expr {$mf1 == $mf2 || $::name8_2 == ""}
70 } {1}
71 set ::mallocFailed [expr [lindex [sqlite_malloc_stat] 2] <= 0]
72 do_test ${testid}.2.4 {
73 expr {
74 $::name8 == $::name8_2 && $::name16 == $::name8 && !$::mallocFailed ||
75 $::name8 == $::name8_2 && $::name16 == "" && $::mallocFailed ||
76 $::name8 == $::name16 && $::name8_2 == "" && $::mallocFailed ||
77 $::name8_2 == $::name16 && $::name8 == "" && $::mallocFailed
78 }
79 } {1}
80
81 if {$::mallocFailed == 0} {
82 sqlite_malloc_fail 0
83 set go 0
84 }
85
86if 0 {
87 # Test that if a malloc() failed the next call to sqlite3_step() returns
88 # SQLITE_ERROR. If malloc() did not fail, it should return SQLITE_ROW.
89 #
90 # Before running sqlite3_step(), make sure that malloc() is not about to
91 # fail. Memory allocation failures that occur within sqlite3_step() are
92 # tested elsewhere.
93 do_test ${testid}.3 {
94 set rc [sqlite3_step $::STMT]
95 list [expr $rc=="SQLITE_ERROR"] [expr $rc=="SQLITE_ROW"]
96 } [list $mallocFailed [expr !$mallocFailed]]
97}
98
99 do_test ${testid}.4 {
100 sqlite3_finalize $::STMT
101 } {SQLITE_OK}
102 }
103}
104
105execsql {
106 CREATE TABLE tbl(
107 the_first_reasonably_long_column_name that_also_has_quite_a_lengthy_type
108 );
109}
110do_stmt_test 1 "SELECT * FROM tbl"
111
112sqlite_malloc_fail 0
113finish_test
114