mirror of
https://github.com/sqlite/sqlite.git
synced 2025-07-30 19:03:16 +03:00
Test cases and corrections to IO and malloc() error handling in incremental blob IO functions. (CVS 3915)
FossilOrigin-Name: 641e55284e1ba6070073c83ac6ed78ffb29f7e60
This commit is contained in:
192
test/incrblob_err.test
Normal file
192
test/incrblob_err.test
Normal file
@ -0,0 +1,192 @@
|
||||
# 2007 May 1
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
#***********************************************************************
|
||||
#
|
||||
# $Id: incrblob_err.test,v 1.1 2007/05/04 12:05:56 danielk1977 Exp $
|
||||
#
|
||||
|
||||
set testdir [file dirname $argv0]
|
||||
source $testdir/tester.tcl
|
||||
|
||||
# Usage: do_malloc_test <test number> <options...>
|
||||
#
|
||||
# The first argument, <test number>, is an integer used to name the
|
||||
# tests executed by this proc. Options are as follows:
|
||||
#
|
||||
# -tclprep TCL script to run to prepare test.
|
||||
# -sqlprep SQL script to run to prepare test.
|
||||
# -tclbody TCL script to run with malloc failure simulation.
|
||||
# -sqlbody TCL script to run with malloc failure simulation.
|
||||
# -cleanup TCL script to run after the test.
|
||||
#
|
||||
# This command runs a series of tests to verify SQLite's ability
|
||||
# to handle an out-of-memory condition gracefully. It is assumed
|
||||
# that if this condition occurs a malloc() call will return a
|
||||
# NULL pointer. Linux, for example, doesn't do that by default. See
|
||||
# the "BUGS" section of malloc(3).
|
||||
#
|
||||
# Each iteration of a loop, the TCL commands in any argument passed
|
||||
# to the -tclbody switch, followed by the SQL commands in any argument
|
||||
# passed to the -sqlbody switch are executed. Each iteration the
|
||||
# Nth call to sqliteMalloc() is made to fail, where N is increased
|
||||
# each time the loop runs starting from 1. When all commands execute
|
||||
# successfully, the loop ends.
|
||||
#
|
||||
proc do_malloc_test {tn args} {
|
||||
array unset ::mallocopts
|
||||
array set ::mallocopts $args
|
||||
|
||||
set ::go 1
|
||||
for {set ::n 1} {$::go && $::n < 50000} {incr ::n} {
|
||||
do_test incrblob_err-$tn.$::n {
|
||||
|
||||
# Remove all traces of database files test.db and test2.db from the files
|
||||
# system. Then open (empty database) "test.db" with the handle [db].
|
||||
#
|
||||
sqlite_malloc_fail 0
|
||||
catch {db close}
|
||||
catch {file delete -force test.db}
|
||||
catch {file delete -force test.db-journal}
|
||||
catch {file delete -force test2.db}
|
||||
catch {file delete -force test2.db-journal}
|
||||
catch {sqlite3 db test.db}
|
||||
set ::DB [sqlite3_connection_pointer db]
|
||||
|
||||
# Execute any -tclprep and -sqlprep scripts.
|
||||
#
|
||||
if {[info exists ::mallocopts(-tclprep)]} {
|
||||
eval $::mallocopts(-tclprep)
|
||||
}
|
||||
if {[info exists ::mallocopts(-sqlprep)]} {
|
||||
execsql $::mallocopts(-sqlprep)
|
||||
}
|
||||
|
||||
# Now set the ${::n}th malloc() to fail and execute the -tclbody and
|
||||
# -sqlbody scripts.
|
||||
#
|
||||
sqlite_malloc_fail $::n
|
||||
set ::mallocbody {}
|
||||
if {[info exists ::mallocopts(-tclbody)]} {
|
||||
append ::mallocbody "$::mallocopts(-tclbody)\n"
|
||||
}
|
||||
if {[info exists ::mallocopts(-sqlbody)]} {
|
||||
append ::mallocbody "db eval {$::mallocopts(-sqlbody)}"
|
||||
}
|
||||
set v [catch $::mallocbody msg]
|
||||
|
||||
# If the test fails (if $v!=0) and the database connection actually
|
||||
# exists, make sure the failure code is SQLITE_NOMEM.
|
||||
if {$v && [info command db]=="db" && [info exists ::mallocopts(-sqlbody)]
|
||||
&& [db errorcode]!=7} {
|
||||
set v 999
|
||||
}
|
||||
|
||||
set leftover [lindex [sqlite_malloc_stat] 2]
|
||||
if {$leftover>0} {
|
||||
if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v Message=$msg"}
|
||||
set ::go 0
|
||||
if {$v} {
|
||||
puts "\nError message returned: $msg"
|
||||
} else {
|
||||
set v {1 1}
|
||||
}
|
||||
} else {
|
||||
set v2 [expr {$msg=="" || $msg=="out of memory"}]
|
||||
if {!$v2} {puts "\nError message returned: $msg"}
|
||||
lappend v $v2
|
||||
}
|
||||
} {1 1}
|
||||
|
||||
if {[info exists ::mallocopts(-cleanup)]} {
|
||||
catch [list uplevel #0 $::mallocopts(-cleanup)] msg
|
||||
}
|
||||
}
|
||||
unset ::mallocopts
|
||||
}
|
||||
|
||||
set ::fd [open [info script]]
|
||||
set ::data [read $::fd]
|
||||
close $::fd
|
||||
|
||||
do_malloc_test 1 -tclprep {
|
||||
set bytes [file size [info script]]
|
||||
execsql {
|
||||
CREATE TABLE blobs(k, v BLOB);
|
||||
INSERT INTO blobs VALUES(1, zeroblob($::bytes));
|
||||
}
|
||||
} -tclbody {
|
||||
set ::blob [db incrblob blobs v 1]
|
||||
set rc [catch {puts -nonewline $::blob $::data}]
|
||||
if {$rc} { error "out of memory" }
|
||||
}
|
||||
|
||||
do_malloc_test 2 -tclprep {
|
||||
execsql {
|
||||
CREATE TABLE blobs(k, v BLOB);
|
||||
INSERT INTO blobs VALUES(1, $::data);
|
||||
}
|
||||
} -tclbody {
|
||||
set ::blob [db incrblob blobs v 1]
|
||||
set rc [catch {set ::r [read $::blob]}]
|
||||
if {$rc} {
|
||||
error "out of memory"
|
||||
} elseif {$::r ne $::data} {
|
||||
error "Bad data read..."
|
||||
}
|
||||
}
|
||||
|
||||
do_malloc_test 3 -tclprep {
|
||||
execsql {
|
||||
CREATE TABLE blobs(k, v BLOB);
|
||||
INSERT INTO blobs VALUES(1, $::data);
|
||||
}
|
||||
} -tclbody {
|
||||
set ::blob [db incrblob blobs v 1]
|
||||
set rc [catch {set ::r [read $::blob]}]
|
||||
if {$rc} {
|
||||
error "out of memory"
|
||||
} elseif {$::r ne $::data} {
|
||||
error "Bad data read..."
|
||||
}
|
||||
set rc [catch {close $::blob}]
|
||||
if {$rc} {
|
||||
error "out of memory"
|
||||
}
|
||||
}
|
||||
sqlite_malloc_fail 0
|
||||
|
||||
do_ioerr_test incrblob_err-4 -cksum 1 -sqlprep {
|
||||
CREATE TABLE blobs(k, v BLOB);
|
||||
INSERT INTO blobs VALUES(1, $::data);
|
||||
} -tclbody {
|
||||
set ::blob [db incrblob blobs v 1]
|
||||
read $::blob
|
||||
}
|
||||
|
||||
do_ioerr_test incrblob_err-5 -cksum 1 -sqlprep {
|
||||
CREATE TABLE blobs(k, v BLOB);
|
||||
INSERT INTO blobs VALUES(1, zeroblob(length(CAST($::data AS BLOB))));
|
||||
} -tclbody {
|
||||
set ::blob [db incrblob blobs v 1]
|
||||
puts -nonewline $::blob $::data
|
||||
close $::blob
|
||||
}
|
||||
|
||||
do_ioerr_test incrblob_err-6 -cksum 1 -sqlprep {
|
||||
CREATE TABLE blobs(k, v BLOB);
|
||||
INSERT INTO blobs VALUES(1, $::data || $::data || $::data);
|
||||
} -tclbody {
|
||||
set ::blob [db incrblob blobs v 1]
|
||||
seek $::blob -20 end
|
||||
puts -nonewline $::blob "12345678900987654321"
|
||||
close $::blob
|
||||
}
|
||||
|
||||
finish_test
|
Reference in New Issue
Block a user