blob: 972973cdaf031720e32e57ad2ea2bdc848f1fa61 [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drh348784e2000-05-29 20:41:49 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drh348784e2000-05-29 20:41:49 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# 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.
drh348784e2000-05-29 20:41:49 +00009#
10#***********************************************************************
11# This file implements some common TCL routines used for regression
12# testing the SQLite library
13#
drhbb77b752009-04-09 01:23:49 +000014# $Id: tester.tcl,v 1.143 2009/04/09 01:23:49 drh Exp $
drhfbc3eab2001-04-06 16:13:42 +000015
danc1a60c52010-06-07 14:28:16 +000016#-------------------------------------------------------------------------
17# The commands provided by the code in this file to help with creating
18# test cases are as follows:
drh8359d8c2008-03-29 11:00:54 +000019#
danc1a60c52010-06-07 14:28:16 +000020# Commands to manipulate the db and the file-system at a high level:
drh8359d8c2008-03-29 11:00:54 +000021#
danc1a60c52010-06-07 14:28:16 +000022# copy_file FROM TO
23# drop_all_table ?DB?
24# forcedelete FILENAME
25#
26# Test the capability of the SQLite version built into the interpreter to
27# determine if a specific test can be run:
28#
29# ifcapable EXPR
30#
31# Calulate checksums based on database contents:
32#
33# dbcksum DB DBNAME
34# allcksum ?DB?
35# cksum ?DB?
36#
37# Commands to execute/explain SQL statements:
38#
39# stepsql DB SQL
40# execsql2 SQL
41# explain_no_trace SQL
42# explain SQL ?DB?
43# catchsql SQL ?DB?
44# execsql SQL ?DB?
45#
46# Commands to run test cases:
47#
48# do_ioerr_test TESTNAME ARGS...
49# crashsql ARGS...
50# integrity_check TESTNAME ?DB?
51# do_test TESTNAME SCRIPT EXPECTED
dan153eda02010-06-21 07:45:47 +000052# do_execsql_test TESTNAME SQL EXPECTED
53# do_catchsql_test TESTNAME SQL EXPECTED
danc1a60c52010-06-07 14:28:16 +000054#
dan430e74c2010-06-07 17:47:26 +000055# Commands providing a lower level interface to the global test counters:
danc1a60c52010-06-07 14:28:16 +000056#
57# set_test_counter COUNTER ?VALUE?
58# omit_test TESTNAME REASON
59# fail_test TESTNAME
60# incr_ntest
61#
62# Command run at the end of each test file:
63#
64# finish_test
65#
dan430e74c2010-06-07 17:47:26 +000066# Commands to help create test files that run with the "WAL" and other
67# permutations (see file permutations.test):
danc1a60c52010-06-07 14:28:16 +000068#
69# wal_is_wal_mode
70# wal_set_journal_mode ?DB?
71# wal_check_journal_mode TESTNAME?DB?
dan430e74c2010-06-07 17:47:26 +000072# permutation
dancb79e512010-08-06 13:50:07 +000073# presql
danc1a60c52010-06-07 14:28:16 +000074#
drh348784e2000-05-29 20:41:49 +000075
danc1a60c52010-06-07 14:28:16 +000076# Set the precision of FP arithmatic used by the interpreter. And
77# configure SQLite to take database file locks on the page that begins
78# 64KB into the database file instead of the one 1GB in. This means
79# the code that handles that special case can be tested without creating
80# very large database files.
81#
drh92febd92004-08-20 18:34:20 +000082set tcl_precision 15
drhc7a3bb92009-02-05 16:31:45 +000083sqlite3_test_control_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +000084
danc1a60c52010-06-07 14:28:16 +000085
86# If the pager codec is available, create a wrapper for the [sqlite3]
87# command that appends "-key {xyzzy}" to the command line. i.e. this:
drh3a7fb7c2007-08-10 16:41:08 +000088#
danc1a60c52010-06-07 14:28:16 +000089# sqlite3 db test.db
drh4a50aac2007-08-23 02:47:53 +000090#
danc1a60c52010-06-07 14:28:16 +000091# becomes
drh4a50aac2007-08-23 02:47:53 +000092#
danc1a60c52010-06-07 14:28:16 +000093# sqlite3 db test.db -key {xyzzy}
drh9eb9e262004-02-11 02:18:05 +000094#
dan430e74c2010-06-07 17:47:26 +000095if {[info command sqlite_orig]==""} {
drhef4ac8f2004-06-19 00:16:31 +000096 rename sqlite3 sqlite_orig
97 proc sqlite3 {args} {
dan68928b62010-06-22 13:46:43 +000098 if {[llength $args]>=2 && [string index [lindex $args 0] 0]!="-"} {
dan430e74c2010-06-07 17:47:26 +000099 # This command is opening a new database connection.
100 #
101 if {[info exists ::G(perm:sqlite3_args)]} {
102 set args [concat $args $::G(perm:sqlite3_args)]
103 }
dan68928b62010-06-22 13:46:43 +0000104 if {[sqlite_orig -has-codec] && ![info exists ::do_not_use_codec]} {
dan430e74c2010-06-07 17:47:26 +0000105 lappend args -key {xyzzy}
106 }
107
dan3ccd20a2010-06-09 19:01:02 +0000108 set res [uplevel 1 sqlite_orig $args]
dan430e74c2010-06-07 17:47:26 +0000109 if {[info exists ::G(perm:presql)]} {
110 [lindex $args 0] eval $::G(perm:presql)
111 }
dan1ce1b4a2010-12-07 14:32:28 +0000112 if {[info exists ::G(perm:dbconfig)]} {
113 set ::dbhandle [lindex $args 0]
114 uplevel #0 $::G(perm:dbconfig)
115 }
dan3ccd20a2010-06-09 19:01:02 +0000116 set res
dan430e74c2010-06-07 17:47:26 +0000117 } else {
118 # This command is not opening a new database connection. Pass the
119 # arguments through to the C implemenation as the are.
120 #
121 uplevel 1 sqlite_orig $args
drh9eb9e262004-02-11 02:18:05 +0000122 }
drh9eb9e262004-02-11 02:18:05 +0000123 }
124}
125
dancb354602010-07-08 09:44:42 +0000126proc execpresql {handle args} {
127 trace remove execution $handle enter [list execpresql $handle]
128 if {[info exists ::G(perm:presql)]} {
129 $handle eval $::G(perm:presql)
130 }
131}
132
dan68928b62010-06-22 13:46:43 +0000133# This command should be called after loading tester.tcl from within
134# all test scripts that are incompatible with encryption codecs.
135#
136proc do_not_use_codec {} {
137 set ::do_not_use_codec 1
138 reset_db
139}
140
dan430e74c2010-06-07 17:47:26 +0000141# The following block only runs the first time this file is sourced. It
142# does not run in slave interpreters (since the ::cmdlinearg array is
143# populated before the test script is run in slave interpreters).
drhbec2bf42000-05-29 23:48:22 +0000144#
danc1a60c52010-06-07 14:28:16 +0000145if {[info exists cmdlinearg]==0} {
146
147 # Parse any options specified in the $argv array. This script accepts the
148 # following options:
149 #
150 # --pause
151 # --soft-heap-limit=NN
152 # --maxerror=NN
153 # --malloctrace=N
154 # --backtrace=N
155 # --binarylog=N
dan0626dfc2010-06-15 06:56:37 +0000156 # --soak=N
dan6a64d672011-04-04 15:38:16 +0000157 # --start=[$permutation:]$testfile
danc1a60c52010-06-07 14:28:16 +0000158 #
159 set cmdlinearg(soft-heap-limit) 0
160 set cmdlinearg(maxerror) 1000
161 set cmdlinearg(malloctrace) 0
162 set cmdlinearg(backtrace) 10
163 set cmdlinearg(binarylog) 0
dan0626dfc2010-06-15 06:56:37 +0000164 set cmdlinearg(soak) 0
dan6a64d672011-04-04 15:38:16 +0000165 set cmdlinearg(start) ""
danc1a60c52010-06-07 14:28:16 +0000166
167 set leftover [list]
168 foreach a $argv {
169 switch -regexp -- $a {
170 {^-+pause$} {
171 # Wait for user input before continuing. This is to give the user an
172 # opportunity to connect profiling tools to the process.
173 puts -nonewline "Press RETURN to begin..."
174 flush stdout
175 gets stdin
176 }
177 {^-+soft-heap-limit=.+$} {
178 foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break
179 }
180 {^-+maxerror=.+$} {
181 foreach {dummy cmdlinearg(maxerror)} [split $a =] break
182 }
183 {^-+malloctrace=.+$} {
184 foreach {dummy cmdlinearg(malloctrace)} [split $a =] break
185 if {$cmdlinearg(malloctrace)} {
186 sqlite3_memdebug_log start
187 }
188 }
189 {^-+backtrace=.+$} {
190 foreach {dummy cmdlinearg(backtrace)} [split $a =] break
dan0626dfc2010-06-15 06:56:37 +0000191 sqlite3_memdebug_backtrace $value
danc1a60c52010-06-07 14:28:16 +0000192 }
danc1a60c52010-06-07 14:28:16 +0000193 {^-+binarylog=.+$} {
194 foreach {dummy cmdlinearg(binarylog)} [split $a =] break
195 }
dan0626dfc2010-06-15 06:56:37 +0000196 {^-+soak=.+$} {
197 foreach {dummy cmdlinearg(soak)} [split $a =] break
198 set ::G(issoak) $cmdlinearg(soak)
199 }
dan6a64d672011-04-04 15:38:16 +0000200 {^-+start=.+$} {
201 foreach {dummy cmdlinearg(start)} [split $a =] break
202
203 set ::G(start:file) $cmdlinearg(start)
204 if {[regexp {(.*):(.*)} $cmdlinearg(start) -> s.perm s.file]} {
205 set ::G(start:permutation) ${s.perm}
206 set ::G(start:file) ${s.file}
207 }
208 if {$::G(start:file) == ""} {unset ::G(start:file)}
209 }
danc1a60c52010-06-07 14:28:16 +0000210 default {
211 lappend leftover $a
212 }
213 }
214 }
215 set argv $leftover
216
dan430e74c2010-06-07 17:47:26 +0000217 # Install the malloc layer used to inject OOM errors. And the 'automatic'
218 # extensions. This only needs to be done once for the process.
219 #
danielk1977f7b9d662008-06-23 18:49:43 +0000220 sqlite3_shutdown
221 install_malloc_faultsim 1
222 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000223 autoinstall_test_functions
danc1a60c52010-06-07 14:28:16 +0000224
dan430e74c2010-06-07 17:47:26 +0000225 # If the --binarylog option was specified, create the logging VFS. This
226 # call installs the new VFS as the default for all SQLite connections.
227 #
danc1a60c52010-06-07 14:28:16 +0000228 if {$cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000229 vfslog new binarylog {} vfslog.bin
danc1a60c52010-06-07 14:28:16 +0000230 }
231
232 # Set the backtrace depth, if malloc tracing is enabled.
233 #
234 if {$cmdlinearg(malloctrace)} {
235 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danielk1977185eac92008-07-12 15:55:54 +0000236 }
danielk1977f7b9d662008-06-23 18:49:43 +0000237}
danielk197703ba3fa2009-01-09 10:49:14 +0000238
danc1a60c52010-06-07 14:28:16 +0000239# Update the soft-heap-limit each time this script is run. In that
240# way if an individual test file changes the soft-heap-limit, it
241# will be reset at the start of the next test file.
242#
243sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
244
245# Create a test database
246#
danielk197703ba3fa2009-01-09 10:49:14 +0000247proc reset_db {} {
248 catch {db close}
249 file delete -force test.db
250 file delete -force test.db-journal
dand3f8f942010-04-13 11:35:01 +0000251 file delete -force test.db-wal
danielk197703ba3fa2009-01-09 10:49:14 +0000252 sqlite3 db ./test.db
253 set ::DB [sqlite3_connection_pointer db]
254 if {[info exists ::SETUP_SQL]} {
255 db eval $::SETUP_SQL
256 }
drhcd61c282002-03-06 22:01:34 +0000257}
danielk197703ba3fa2009-01-09 10:49:14 +0000258reset_db
drhbec2bf42000-05-29 23:48:22 +0000259
260# Abort early if this script has been run before.
261#
danc1a60c52010-06-07 14:28:16 +0000262if {[info exists TC(count)]} return
drhbec2bf42000-05-29 23:48:22 +0000263
drhce2198c2010-09-10 13:22:59 +0000264# Make sure memory statistics are enabled.
265#
266sqlite3_config_memstatus 1
267
danc1a60c52010-06-07 14:28:16 +0000268# Initialize the test counters and set up commands to access them.
269# Or, if this is a slave interpreter, set up aliases to write the
270# counters in the parent interpreter.
drhbec2bf42000-05-29 23:48:22 +0000271#
danc1a60c52010-06-07 14:28:16 +0000272if {0==[info exists ::SLAVE]} {
273 set TC(errors) 0
274 set TC(count) 0
275 set TC(fail_list) [list]
276 set TC(omit_list) [list]
277
278 proc set_test_counter {counter args} {
279 if {[llength $args]} {
280 set ::TC($counter) [lindex $args 0]
281 }
282 set ::TC($counter)
283 }
drhb62c3352006-11-23 09:39:16 +0000284}
drh348784e2000-05-29 20:41:49 +0000285
drh521cc842008-04-15 02:36:33 +0000286# Record the fact that a sequence of tests were omitted.
287#
288proc omit_test {name reason} {
danc1a60c52010-06-07 14:28:16 +0000289 set omitList [set_test_counter omit_list]
drh521cc842008-04-15 02:36:33 +0000290 lappend omitList [list $name $reason]
danc1a60c52010-06-07 14:28:16 +0000291 set_test_counter omit_list $omitList
drh521cc842008-04-15 02:36:33 +0000292}
293
danc1a60c52010-06-07 14:28:16 +0000294# Record the fact that a test failed.
295#
296proc fail_test {name} {
297 set f [set_test_counter fail_list]
298 lappend f $name
299 set_test_counter fail_list $f
300 set_test_counter errors [expr [set_test_counter errors] + 1]
301
302 set nFail [set_test_counter errors]
303 if {$nFail>=$::cmdlinearg(maxerror)} {
304 puts "*** Giving up..."
305 finalize_testing
306 }
307}
308
309# Increment the number of tests run
310#
311proc incr_ntest {} {
312 set_test_counter count [expr [set_test_counter count] + 1]
313}
314
315
drh348784e2000-05-29 20:41:49 +0000316# Invoke the do_test procedure to run a single test
317#
318proc do_test {name cmd expected} {
danc1a60c52010-06-07 14:28:16 +0000319
320 global argv cmdlinearg
321
dan8c408002010-11-01 17:38:24 +0000322 fix_testname name
323
drh4a50aac2007-08-23 02:47:53 +0000324 sqlite3_memdebug_settitle $name
danc1a60c52010-06-07 14:28:16 +0000325
dan92d516a2010-07-05 14:54:48 +0000326# if {[llength $argv]==0} {
327# set go 1
328# } else {
329# set go 0
330# foreach pattern $argv {
331# if {[string match $pattern $name]} {
332# set go 1
333# break
334# }
335# }
336# }
dan430e74c2010-06-07 17:47:26 +0000337
dand506de02010-07-03 13:59:01 +0000338 if {[info exists ::G(perm:prefix)]} {
339 set name "$::G(perm:prefix)$name"
dan430e74c2010-06-07 17:47:26 +0000340 }
341
danc1a60c52010-06-07 14:28:16 +0000342 incr_ntest
drh5edc3122001-09-13 21:53:09 +0000343 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000344 flush stdout
345 if {[catch {uplevel #0 "$cmd;\n"} result]} {
346 puts "\nError: $result"
danc1a60c52010-06-07 14:28:16 +0000347 fail_test $name
drh348784e2000-05-29 20:41:49 +0000348 } elseif {[string compare $result $expected]} {
349 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
danc1a60c52010-06-07 14:28:16 +0000350 fail_test $name
drh348784e2000-05-29 20:41:49 +0000351 } else {
352 puts " Ok"
353 }
drh6558db82007-04-13 03:23:21 +0000354 flush stdout
drh348784e2000-05-29 20:41:49 +0000355}
dana3f51082010-09-30 18:43:14 +0000356
shaneh55505172011-06-21 19:30:19 +0000357proc filepath_normalize {p} {
358 # test cases should be written to assume "unix"-like file paths
shanehc4896402011-06-21 19:38:16 +0000359 if {$::tcl_platform(platform)=="unix"} {
shaneh55505172011-06-21 19:30:19 +0000360 # lreverse*2 as a hack to remove any unneeded {} after the string map
361 lreverse [lreverse [string map {\\ /} [regsub -nocase -all {[a-z]:[/\\]+} $p {/}]]]
shanehc4896402011-06-21 19:38:16 +0000362 } {
363 set p
shaneh55505172011-06-21 19:30:19 +0000364 }
365}
366proc do_filepath_test {name cmd expected} {
367 uplevel [list do_test $name [
368 subst -nocommands { filepath_normalize [ $cmd ] }
369 ] [filepath_normalize $expected]]
370}
371
dan33f53792011-05-05 19:44:22 +0000372proc realnum_normalize {r} {
shaneh4f529e82011-06-20 17:41:41 +0000373 # different TCL versions display floating point values differently.
374 string map {1.#INF inf Inf inf .0e e} [regsub -all {(e[+-])0+} $r {\1}]
dan33f53792011-05-05 19:44:22 +0000375}
376proc do_realnum_test {name cmd expected} {
377 uplevel [list do_test $name [
378 subst -nocommands { realnum_normalize [ $cmd ] }
379 ] [realnum_normalize $expected]]
380}
381
dana3f51082010-09-30 18:43:14 +0000382proc fix_testname {varname} {
383 upvar $varname testname
384 if {[info exists ::testprefix]
385 && [string is digit [string range $testname 0 0]]
386 } {
387 set testname "${::testprefix}-$testname"
388 }
389}
dan153eda02010-06-21 07:45:47 +0000390
dan57f7f4b2010-10-01 19:04:37 +0000391proc do_execsql_test {testname sql {result {}}} {
dana3f51082010-09-30 18:43:14 +0000392 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000393 uplevel do_test [list $testname] [list "execsql {$sql}"] [list [list {*}$result]]
dan153eda02010-06-21 07:45:47 +0000394}
395proc do_catchsql_test {testname sql result} {
dana3f51082010-09-30 18:43:14 +0000396 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000397 uplevel do_test [list $testname] [list "catchsql {$sql}"] [list $result]
dan153eda02010-06-21 07:45:47 +0000398}
dan39854792010-11-15 16:12:58 +0000399proc do_eqp_test {name sql res} {
400 uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res]
401}
dan153eda02010-06-21 07:45:47 +0000402
dan51f06982010-09-18 19:00:12 +0000403#-------------------------------------------------------------------------
404# Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST
405#
406# Where switches are:
407#
408# -errorformat FMTSTRING
409# -count
danaf1dcab2010-09-20 19:17:53 +0000410# -query SQL
dana16d1062010-09-28 17:37:28 +0000411# -tclquery TCL
danaf7626f2010-09-23 18:47:36 +0000412# -repair TCL
dan51f06982010-09-18 19:00:12 +0000413#
414proc do_select_tests {prefix args} {
415
416 set testlist [lindex $args end]
417 set switches [lrange $args 0 end-1]
418
419 set errfmt ""
420 set countonly 0
dana16d1062010-09-28 17:37:28 +0000421 set tclquery ""
danaf7626f2010-09-23 18:47:36 +0000422 set repair ""
dan51f06982010-09-18 19:00:12 +0000423
424 for {set i 0} {$i < [llength $switches]} {incr i} {
425 set s [lindex $switches $i]
426 set n [string length $s]
danaf1dcab2010-09-20 19:17:53 +0000427 if {$n>=2 && [string equal -length $n $s "-query"]} {
dana16d1062010-09-28 17:37:28 +0000428 set tclquery [list execsql [lindex $switches [incr i]]]
429 } elseif {$n>=2 && [string equal -length $n $s "-tclquery"]} {
430 set tclquery [lindex $switches [incr i]]
danaf1dcab2010-09-20 19:17:53 +0000431 } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} {
dan51f06982010-09-18 19:00:12 +0000432 set errfmt [lindex $switches [incr i]]
danaf7626f2010-09-23 18:47:36 +0000433 } elseif {$n>=2 && [string equal -length $n $s "-repair"]} {
434 set repair [lindex $switches [incr i]]
dan51f06982010-09-18 19:00:12 +0000435 } elseif {$n>=2 && [string equal -length $n $s "-count"]} {
436 set countonly 1
437 } else {
438 error "unknown switch: $s"
439 }
440 }
441
442 if {$countonly && $errfmt!=""} {
443 error "Cannot use -count and -errorformat together"
444 }
445 set nTestlist [llength $testlist]
446 if {$nTestlist%3 || $nTestlist==0 } {
447 error "SELECT test list contains [llength $testlist] elements"
448 }
449
danaf7626f2010-09-23 18:47:36 +0000450 eval $repair
dan51f06982010-09-18 19:00:12 +0000451 foreach {tn sql res} $testlist {
dana16d1062010-09-28 17:37:28 +0000452 if {$tclquery != ""} {
danaf1dcab2010-09-20 19:17:53 +0000453 execsql $sql
dana16d1062010-09-28 17:37:28 +0000454 uplevel do_test ${prefix}.$tn [list $tclquery] [list [list {*}$res]]
455 } elseif {$countonly} {
dan51f06982010-09-18 19:00:12 +0000456 set nRow 0
457 db eval $sql {incr nRow}
458 uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res]
459 } elseif {$errfmt==""} {
460 uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]]
461 } else {
462 set res [list 1 [string trim [format $errfmt {*}$res]]]
463 uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res]
464 }
danaf7626f2010-09-23 18:47:36 +0000465 eval $repair
dan51f06982010-09-18 19:00:12 +0000466 }
danaf7626f2010-09-23 18:47:36 +0000467
dan51f06982010-09-18 19:00:12 +0000468}
469
danb04757a2010-09-21 16:59:16 +0000470proc delete_all_data {} {
471 db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} {
472 db eval "DELETE FROM '[string map {' ''} $t]'"
473 }
474}
drh348784e2000-05-29 20:41:49 +0000475
drhb62c3352006-11-23 09:39:16 +0000476# Run an SQL script.
477# Return the number of microseconds per statement.
478#
drh3590f152006-11-23 21:09:10 +0000479proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000480 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000481 flush stdout
482 set speed [time {sqlite3_exec_nr db $sql}]
483 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000484 if {$tm == 0} {
485 set rate [format %20s "many"]
486 } else {
487 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
488 }
drh3590f152006-11-23 21:09:10 +0000489 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000490 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000491 global total_time
492 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000493 lappend ::speed_trial_times $name $tm
drhdd924312007-03-31 22:34:16 +0000494}
drh45c236d2008-03-22 01:08:00 +0000495proc speed_trial_tcl {name numstmt units script} {
496 puts -nonewline [format {%-21.21s } $name...]
497 flush stdout
498 set speed [time {eval $script}]
499 set tm [lindex $speed 0]
500 if {$tm == 0} {
501 set rate [format %20s "many"]
502 } else {
503 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
504 }
505 set u2 $units/s
506 puts [format {%12d uS %s %s} $tm $rate $u2]
507 global total_time
508 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000509 lappend ::speed_trial_times $name $tm
drh45c236d2008-03-22 01:08:00 +0000510}
drhdd924312007-03-31 22:34:16 +0000511proc speed_trial_init {name} {
512 global total_time
513 set total_time 0
dana975b592010-10-08 16:09:43 +0000514 set ::speed_trial_times [list]
drhbb810a92010-07-03 12:00:53 +0000515 sqlite3 versdb :memory:
516 set vers [versdb one {SELECT sqlite_source_id()}]
517 versdb close
518 puts "SQLite $vers"
drhdd924312007-03-31 22:34:16 +0000519}
520proc speed_trial_summary {name} {
521 global total_time
522 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
dana975b592010-10-08 16:09:43 +0000523
524 if { 0 } {
525 sqlite3 versdb :memory:
526 set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0]
527 versdb close
528 puts "CREATE TABLE IF NOT EXISTS time(version, script, test, us);"
529 foreach {test us} $::speed_trial_times {
530 puts "INSERT INTO time VALUES('$vers', '$name', '$test', $us);"
531 }
532 }
drhb62c3352006-11-23 09:39:16 +0000533}
534
drh348784e2000-05-29 20:41:49 +0000535# Run this routine last
536#
537proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +0000538 catch {db close}
539 catch {db2 close}
540 catch {db3 close}
541 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +0000542}
543proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +0000544 global sqlite_open_file_count
545
546 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +0000547
drh6e142f52000-06-08 13:36:40 +0000548 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000549 catch {db2 close}
550 catch {db3 close}
551
drh9bc54492007-10-23 14:49:59 +0000552 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000553 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000554 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000555 db close
drh984bfaa2008-03-19 16:08:53 +0000556 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +0000557
drh3a7fb7c2007-08-10 16:41:08 +0000558 sqlite3_soft_heap_limit 0
danc1a60c52010-06-07 14:28:16 +0000559 set nTest [incr_ntest]
560 set nErr [set_test_counter errors]
561
drh348784e2000-05-29 20:41:49 +0000562 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000563 if {$nErr>0} {
danc1a60c52010-06-07 14:28:16 +0000564 puts "Failures on these tests: [set_test_counter fail_list]"
drhf3a65f72007-08-22 20:18:21 +0000565 }
danielk1977ee8b7992009-03-26 17:13:06 +0000566 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +0000567 if {[llength $omitList]>0} {
568 puts "Omitted test cases:"
569 set prec {}
570 foreach {rec} [lsort $omitList] {
571 if {$rec==$prec} continue
572 set prec $rec
573 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
574 }
575 }
drh80788d82006-09-02 14:50:23 +0000576 if {$nErr>0 && ![working_64bit_int]} {
577 puts "******************************************************************"
578 puts "N.B.: The version of TCL that you used to build this test harness"
579 puts "is defective in that it does not support 64-bit integers. Some or"
580 puts "all of the test failures above might be a result from this defect"
581 puts "in your TCL build."
582 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000583 }
danc1a60c52010-06-07 14:28:16 +0000584 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000585 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +0000586 }
drh94e92032003-02-16 22:21:32 +0000587 if {$sqlite_open_file_count} {
588 puts "$sqlite_open_file_count files were left open"
589 incr nErr
590 }
drheafc43b2010-07-26 18:43:40 +0000591 if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 ||
592 [sqlite3_memory_used]>0} {
593 puts "Unfreed memory: [sqlite3_memory_used] bytes in\
594 [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations"
drhf3a65f72007-08-22 20:18:21 +0000595 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000596 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000597 puts "Writing unfreed memory log to \"./memleak.txt\""
598 sqlite3_memdebug_dump ./memleak.txt
599 }
drhf3a65f72007-08-22 20:18:21 +0000600 } else {
601 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000602 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000603 sqlite3_memdebug_dump ./memusage.txt
604 }
drhf3a65f72007-08-22 20:18:21 +0000605 }
drhf7141992008-06-19 00:16:08 +0000606 show_memstats
drh91fd4d42008-01-19 20:11:25 +0000607 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
608 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000609 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
610 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
611 }
danc1a60c52010-06-07 14:28:16 +0000612 if {$::cmdlinearg(malloctrace)} {
danielk197735754ac2008-03-21 17:29:37 +0000613 puts "Writing mallocs.sql..."
614 memdebug_log_sql
615 sqlite3_memdebug_log stop
616 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000617
618 if {[sqlite3_memory_used]>0} {
619 puts "Writing leaks.sql..."
620 sqlite3_memdebug_log sync
621 memdebug_log_sql leaks.sql
622 }
danielk197735754ac2008-03-21 17:29:37 +0000623 }
drhd9910fe2006-10-04 11:55:49 +0000624 foreach f [glob -nocomplain test.db-*-journal] {
625 file delete -force $f
626 }
627 foreach f [glob -nocomplain test.db-mj*] {
628 file delete -force $f
629 }
drhdb25e382001-03-15 18:21:22 +0000630 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000631}
632
drhf7141992008-06-19 00:16:08 +0000633# Display memory statistics for analysis and debugging purposes.
634#
635proc show_memstats {} {
636 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +0000637 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
638 set val [format {now %10d max %10d max-size %10d} \
639 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000640 puts "Memory used: $val"
drheafc43b2010-07-26 18:43:40 +0000641 set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0]
642 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
643 puts "Allocation count: $val"
drhf7141992008-06-19 00:16:08 +0000644 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +0000645 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
646 set val [format {now %10d max %10d max-size %10d} \
647 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000648 puts "Page-cache used: $val"
649 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
650 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
651 puts "Page-cache overflow: $val"
652 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
653 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
654 puts "Scratch memory used: $val"
655 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +0000656 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
657 set val [format {now %10d max %10d max-size %10d} \
658 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000659 puts "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +0000660 ifcapable yytrackmaxstackdepth {
661 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
662 set val [format { max %10d} [lindex $x 2]]
663 puts "Parser stack depth: $val"
664 }
drhf7141992008-06-19 00:16:08 +0000665}
666
drh348784e2000-05-29 20:41:49 +0000667# A procedure to execute SQL
668#
drhc4a3c772001-04-04 11:48:57 +0000669proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000670 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000671 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000672}
drh3aadb2e2000-05-31 17:59:25 +0000673
drhadbca9c2001-09-27 15:11:53 +0000674# Execute SQL and catch exceptions.
675#
676proc catchsql {sql {db db}} {
677 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +0000678 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +0000679 lappend r $msg
680 return $r
681}
682
drh04096482001-11-09 22:41:44 +0000683# Do an VDBE code dump on the SQL given
684#
685proc explain {sql {db db}} {
686 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000687 puts "addr opcode p1 p2 p3 p4 p5 #"
688 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000689 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000690 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
691 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
692 ]
drh04096482001-11-09 22:41:44 +0000693 }
694}
695
drhdafc0ce2008-04-17 19:14:02 +0000696# Show the VDBE program for an SQL statement but omit the Trace
697# opcode at the beginning. This procedure can be used to prove
698# that different SQL statements generate exactly the same VDBE code.
699#
700proc explain_no_trace {sql} {
701 set tr [db eval "EXPLAIN $sql"]
702 return [lrange $tr 7 end]
703}
704
drh3aadb2e2000-05-31 17:59:25 +0000705# Another procedure to execute SQL. This one includes the field
706# names in the returned list.
707#
708proc execsql2 {sql} {
709 set result {}
710 db eval $sql data {
711 foreach f $data(*) {
712 lappend result $f $data($f)
713 }
714 }
715 return $result
716}
drh17a68932001-01-31 13:28:08 +0000717
drhdde85d92003-03-01 19:45:34 +0000718# Use the non-callback API to execute multiple SQL statements
719#
720proc stepsql {dbptr sql} {
721 set sql [string trim $sql]
722 set r 0
723 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000724 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000725 return [list 1 $vm]
726 }
727 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000728# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
729# foreach v $VAL {lappend r $v}
730# }
731 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
732 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
733 lappend r [sqlite3_column_text $vm $i]
734 }
drhdde85d92003-03-01 19:45:34 +0000735 }
danielk1977106bb232004-05-21 10:08:53 +0000736 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000737 return [list 1 $errmsg]
738 }
739 }
740 return $r
741}
742
drh17a68932001-01-31 13:28:08 +0000743# Delete a file or directory
744#
dana16d1062010-09-28 17:37:28 +0000745proc forcedelete {args} {
746 foreach filename $args {
747 # On windows, sometimes even a [file delete -force] can fail just after
748 # a file is closed. The cause is usually "tag-alongs" - programs like
749 # anti-virus software, automatic backup tools and various explorer
750 # extensions that keep a file open a little longer than we expect, causing
751 # the delete to fail.
752 #
753 # The solution is to wait a short amount of time before retrying the
754 # delete.
755 #
756 set nRetry 50 ;# Maximum number of retries.
757 set nDelay 100 ;# Delay in ms before retrying.
758 for {set i 0} {$i<$nRetry} {incr i} {
759 set rc [catch {file delete -force $filename} msg]
760 if {$rc==0} break
761 after $nDelay
762 }
763 if {$rc} { error $msg }
drh17a68932001-01-31 13:28:08 +0000764 }
765}
drh21504322002-06-25 13:16:02 +0000766
767# Do an integrity check of the entire database
768#
danielk197704103022009-02-03 16:51:24 +0000769proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +0000770 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +0000771 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +0000772 }
773}
774
danc6055c72011-04-28 18:46:46 +0000775
776# Return true if the SQL statement passed as the second argument uses a
777# statement transaction.
778#
779proc sql_uses_stmt {db sql} {
780 set stmt [sqlite3_prepare $db $sql -1 dummy]
781 set uses [uses_stmt_journal $stmt]
782 sqlite3_finalize $stmt
783 return $uses
784}
785
danielk1977db4e8862008-01-16 08:24:46 +0000786proc fix_ifcapable_expr {expr} {
787 set ret ""
788 set state 0
789 for {set i 0} {$i < [string length $expr]} {incr i} {
790 set char [string range $expr $i $i]
791 set newstate [expr {[string is alnum $char] || $char eq "_"}]
792 if {$newstate && !$state} {
793 append ret {$::sqlite_options(}
794 }
795 if {!$newstate && $state} {
796 append ret )
797 }
798 append ret $char
799 set state $newstate
800 }
801 if {$state} {append ret )}
802 return $ret
803}
804
drh27d258a2004-10-30 20:23:09 +0000805# Evaluate a boolean expression of capabilities. If true, execute the
806# code. Omit the code if false.
807#
danielk19773e8c37e2005-01-21 03:12:14 +0000808proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +0000809 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
810 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +0000811 if ($e2) {
812 set c [catch {uplevel 1 $code} r]
813 } else {
814 set c [catch {uplevel 1 $elsecode} r]
815 }
816 return -code $c $r
drh21504322002-06-25 13:16:02 +0000817}
danielk197745901d62004-11-10 15:27:38 +0000818
danielk1977aca790a2005-01-13 11:07:52 +0000819# This proc execs a seperate process that crashes midway through executing
820# the SQL script $sql on database test.db.
821#
822# The crash occurs during a sync() of file $crashfile. When the crash
823# occurs a random subset of all unsynced writes made by the process are
824# written into the files on disk. Argument $crashdelay indicates the
825# number of file syncs to wait before crashing.
826#
827# The return value is a list of two elements. The first element is a
828# boolean, indicating whether or not the process actually crashed or
829# reported some other error. The second element in the returned list is the
830# error message. This is "child process exited abnormally" if the crash
831# occured.
832#
danielk1977f8940ae2007-08-23 11:07:10 +0000833# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +0000834#
835proc crashsql {args} {
danielk197759a33f92007-03-17 10:26:59 +0000836
837 set blocksize ""
838 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +0000839 set prngseed 0
drhf8587402008-01-08 16:03:49 +0000840 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +0000841 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +0000842 set dc ""
danielk197759a33f92007-03-17 10:26:59 +0000843 set sql [lindex $args end]
844
845 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
846 set z [lindex $args $ii]
847 set n [string length $z]
848 set z2 [lindex $args [expr $ii+1]]
849
850 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +0000851 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +0000852 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +0000853 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000854 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +0000855 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +0000856 else { error "Unrecognized option: $z" }
857 }
858
859 if {$crashfile eq ""} {
860 error "Compulsory option -file missing"
861 }
862
shanehf2c08822010-07-08 16:30:44 +0000863 # $crashfile gets compared to the native filename in
864 # cfSync(), which can be different then what TCL uses by
865 # default, so here we force it to the "nativename" format.
866 set cfile [string map {\\ \\\\} [file nativename [file join [pwd] $crashfile]]]
danielk1977aca790a2005-01-13 11:07:52 +0000867
868 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +0000869 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +0000870 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +0000871 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +0000872 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +0000873
874 # This block sets the cache size of the main database to 10
875 # pages. This is done in case the build is configured to omit
876 # "PRAGMA cache_size".
877 puts $f {db eval {SELECT * FROM sqlite_master;}}
878 puts $f {set bt [btree_from_db db]}
879 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +0000880 if {$prngseed} {
881 set seed [expr {$prngseed%10007+1}]
882 # puts seed=$seed
883 puts $f "db eval {SELECT randomblob($seed)}"
884 }
danielk1977933bbd62007-03-17 07:22:42 +0000885
drhf8587402008-01-08 16:03:49 +0000886 if {[string length $tclbody]>0} {
887 puts $f $tclbody
888 }
889 if {[string length $sql]>0} {
890 puts $f "db eval {"
891 puts $f "$sql"
892 puts $f "}"
893 }
danielk1977aca790a2005-01-13 11:07:52 +0000894 close $f
danielk1977aca790a2005-01-13 11:07:52 +0000895 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000896 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000897 } msg]
shanehf2c08822010-07-08 16:30:44 +0000898
899 # Windows/ActiveState TCL returns a slightly different
900 # error message. We map that to the expected message
901 # so that we don't have to change all of the test
902 # cases.
903 if {$::tcl_platform(platform)=="windows"} {
904 if {$msg=="child killed: unknown signal"} {
905 set msg "child process exited abnormally"
906 }
907 }
908
danielk1977aca790a2005-01-13 11:07:52 +0000909 lappend r $msg
910}
911
danielk197732554c12005-01-22 03:39:39 +0000912# Usage: do_ioerr_test <test number> <options...>
913#
914# This proc is used to implement test cases that check that IO errors
915# are correctly handled. The first argument, <test number>, is an integer
916# used to name the tests executed by this proc. Options are as follows:
917#
918# -tclprep TCL script to run to prepare test.
919# -sqlprep SQL script to run to prepare test.
920# -tclbody TCL script to run with IO error simulation.
921# -sqlbody TCL script to run with IO error simulation.
922# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000923# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +0000924# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +0000925# -start Value of 'N' to begin with (default 1)
926#
927# -cksum Boolean. If true, test that the database does
928# not change during the execution of the test case.
929#
930proc do_ioerr_test {testname args} {
931
danielk197732554c12005-01-22 03:39:39 +0000932 set ::ioerropts(-start) 1
933 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000934 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +0000935 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +0000936 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +0000937 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +0000938 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +0000939 array set ::ioerropts $args
940
danielk197747cd39c2008-05-12 12:41:15 +0000941 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
942 # a couple of obscure IO errors that do not return them.
943 set ::ioerropts(-erc) 0
944
danielk197732554c12005-01-22 03:39:39 +0000945 set ::go 1
drh93aed5a2008-01-16 17:46:38 +0000946 #reset_prng_state
947 save_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +0000948 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +0000949 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +0000950 incr ::ioerropts(-count) -1
951 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +0000952
953 # Skip this IO error if it was specified with the "-exclude" option.
954 if {[info exists ::ioerropts(-exclude)]} {
955 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
956 }
danielk1977861f7452008-06-05 11:39:11 +0000957 if {$::ioerropts(-restoreprng)} {
958 restore_prng_state
959 }
danielk197732554c12005-01-22 03:39:39 +0000960
961 # Delete the files test.db and test2.db, then execute the TCL and
962 # SQL (in that order) to prepare for the test case.
963 do_test $testname.$n.1 {
964 set ::sqlite_io_error_pending 0
965 catch {db close}
aswiftaebf4132008-11-21 00:10:35 +0000966 catch {db2 close}
danielk197732554c12005-01-22 03:39:39 +0000967 catch {file delete -force test.db}
968 catch {file delete -force test.db-journal}
969 catch {file delete -force test2.db}
970 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000971 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000972 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000973 if {[info exists ::ioerropts(-tclprep)]} {
974 eval $::ioerropts(-tclprep)
975 }
976 if {[info exists ::ioerropts(-sqlprep)]} {
977 execsql $::ioerropts(-sqlprep)
978 }
979 expr 0
980 } {0}
981
982 # Read the 'checksum' of the database.
983 if {$::ioerropts(-cksum)} {
984 set checksum [cksum]
985 }
drh93aed5a2008-01-16 17:46:38 +0000986
danielk197732554c12005-01-22 03:39:39 +0000987 # Set the Nth IO error to fail.
988 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +0000989 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +0000990 set ::sqlite_io_error_pending $n
991 }] $n
992
993 # Create a single TCL script from the TCL and SQL specified
994 # as the body of the test.
995 set ::ioerrorbody {}
996 if {[info exists ::ioerropts(-tclbody)]} {
997 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
998 }
999 if {[info exists ::ioerropts(-sqlbody)]} {
1000 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
1001 }
1002
1003 # Execute the TCL Script created in the above block. If
1004 # there are at least N IO operations performed by SQLite as
1005 # a result of the script, the Nth will fail.
1006 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +00001007 set ::sqlite_io_error_hit 0
1008 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +00001009 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +00001010 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +00001011 set rc [sqlite3_errcode $::DB]
1012 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +00001013 # If we are in extended result code mode, make sure all of the
1014 # IOERRs we get back really do have their extended code values.
1015 # If an extended result code is returned, the sqlite3_errcode
1016 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
1017 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +00001018 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
1019 return $rc
1020 }
1021 } else {
drh5f7b5bf2007-04-19 12:30:54 +00001022 # If we are not in extended result code mode, make sure no
1023 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +00001024 if {[regexp {\+\d} $rc]} {
1025 return $rc
1026 }
1027 }
drh93aed5a2008-01-16 17:46:38 +00001028 # The test repeats as long as $::go is non-zero. $::go starts out
1029 # as 1. When a test runs to completion without hitting an I/O
1030 # error, that means there is no point in continuing with this test
1031 # case so set $::go to zero.
1032 #
1033 if {$::sqlite_io_error_pending>0} {
1034 set ::go 0
1035 set q 0
1036 set ::sqlite_io_error_pending 0
1037 } else {
1038 set q 1
1039 }
1040
drhc9ac5ca2005-11-04 22:03:30 +00001041 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +00001042 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
1043 set r 1
1044 }
danielk1977f2fa8312006-01-24 13:09:33 +00001045 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +00001046
1047 # One of two things must have happened. either
1048 # 1. We never hit the IO error and the SQL returned OK
1049 # 2. An IO error was hit and the SQL failed
1050 #
dand41a29a2010-05-06 15:56:28 +00001051 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +00001052 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +00001053 } {1}
1054
danielk197780daec62008-05-12 10:57:02 +00001055 set ::sqlite_io_error_hit 0
1056 set ::sqlite_io_error_pending 0
1057
danielk197752b472a2008-05-05 16:23:55 +00001058 # Check that no page references were leaked. There should be
1059 # a single reference if there is still an active transaction,
1060 # or zero otherwise.
1061 #
danielk197781620542008-06-07 05:19:37 +00001062 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
1063 # locks are established on database files (i.e. if the error
1064 # occurs while attempting to detect a hot-journal file), then
1065 # there may 0 page references and an active transaction according
1066 # to [sqlite3_get_autocommit].
1067 #
danielk19774abd5442008-05-05 15:26:50 +00001068 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +00001069 do_test $testname.$n.4 {
1070 set bt [btree_from_db db]
1071 db_enter db
1072 array set stats [btree_pager_stats $bt]
1073 db_leave db
danielk197781620542008-06-07 05:19:37 +00001074 set nRef $stats(ref)
1075 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
1076 } {1}
danielk19774abd5442008-05-05 15:26:50 +00001077 }
1078
danielk197752b472a2008-05-05 16:23:55 +00001079 # If there is an open database handle and no open transaction,
1080 # and the pager is not running in exclusive-locking mode,
1081 # check that the pager is in "unlocked" state. Theoretically,
1082 # if a call to xUnlock() failed due to an IO error the underlying
1083 # file may still be locked.
1084 #
1085 ifcapable pragma {
1086 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +00001087 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +00001088 && [db one {pragma locking_mode}] eq "normal"
1089 && [sqlite3_get_autocommit db]
1090 } {
1091 do_test $testname.$n.5 {
1092 set bt [btree_from_db db]
1093 db_enter db
1094 array set stats [btree_pager_stats $bt]
1095 db_leave db
1096 set stats(state)
1097 } 0
1098 }
1099 }
1100
danielk197732554c12005-01-22 03:39:39 +00001101 # If an IO error occured, then the checksum of the database should
1102 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +00001103 #
drh1aa5af12008-03-07 19:51:14 +00001104 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +00001105 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +00001106 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +00001107 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +00001108 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +00001109 cksum
1110 } $checksum
1111 }
1112
drh1aa5af12008-03-07 19:51:14 +00001113 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +00001114 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +00001115 if {[info exists ::ioerropts(-cleanup)]} {
1116 catch $::ioerropts(-cleanup)
1117 }
danielk197732554c12005-01-22 03:39:39 +00001118 }
1119 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +00001120 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +00001121 unset ::ioerropts
1122}
1123
drh93aed5a2008-01-16 17:46:38 +00001124# Return a checksum based on the contents of the main database associated
1125# with connection $db
danielk197732554c12005-01-22 03:39:39 +00001126#
1127proc cksum {{db db}} {
1128 set txt [$db eval {
1129 SELECT name, type, sql FROM sqlite_master order by name
1130 }]\n
1131 foreach tbl [$db eval {
1132 SELECT name FROM sqlite_master WHERE type='table' order by name
1133 }] {
1134 append txt [$db eval "SELECT * FROM $tbl"]\n
1135 }
1136 foreach prag {default_synchronous default_cache_size} {
1137 append txt $prag-[$db eval "PRAGMA $prag"]\n
1138 }
1139 set cksum [string length $txt]-[md5 $txt]
1140 # puts $cksum-[file size test.db]
1141 return $cksum
1142}
1143
drh93aed5a2008-01-16 17:46:38 +00001144# Generate a checksum based on the contents of the main and temp tables
1145# database $db. If the checksum of two databases is the same, and the
1146# integrity-check passes for both, the two databases are identical.
1147#
drh1db639c2008-01-17 02:36:28 +00001148proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +00001149 set ret [list]
1150 ifcapable tempdb {
1151 set sql {
1152 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1153 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
1154 SELECT 'sqlite_master' UNION
1155 SELECT 'sqlite_temp_master' ORDER BY 1
1156 }
1157 } else {
1158 set sql {
1159 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1160 SELECT 'sqlite_master' ORDER BY 1
1161 }
1162 }
1163 set tbllist [$db eval $sql]
1164 set txt {}
1165 foreach tbl $tbllist {
1166 append txt [$db eval "SELECT * FROM $tbl"]
1167 }
1168 foreach prag {default_cache_size} {
1169 append txt $prag-[$db eval "PRAGMA $prag"]\n
1170 }
1171 # puts txt=$txt
1172 return [md5 $txt]
1173}
1174
drhdc2c4912009-02-04 22:46:47 +00001175# Generate a checksum based on the contents of a single database with
1176# a database connection. The name of the database is $dbname.
1177# Examples of $dbname are "temp" or "main".
1178#
1179proc dbcksum {db dbname} {
1180 if {$dbname=="temp"} {
1181 set master sqlite_temp_master
1182 } else {
1183 set master $dbname.sqlite_master
1184 }
1185 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
1186 set txt [$db eval "SELECT * FROM $master"]\n
1187 foreach tab $alltab {
1188 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
1189 }
1190 return [md5 $txt]
1191}
1192
danielk197735754ac2008-03-21 17:29:37 +00001193proc memdebug_log_sql {{filename mallocs.sql}} {
1194
danielk19776f332c12008-03-21 14:22:44 +00001195 set data [sqlite3_memdebug_log dump]
1196 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +00001197 if {$nFrame < 0} { return "" }
1198
danielk197735754ac2008-03-21 17:29:37 +00001199 set database temp
1200
danielk19776ab3a2e2009-02-19 14:39:25 +00001201 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +00001202
1203 set sql ""
1204 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +00001205 set nCall [lindex $e 0]
1206 set nByte [lindex $e 1]
1207 set lStack [lrange $e 2 end]
1208 append sql "INSERT INTO ${database}.malloc VALUES"
1209 append sql "('test', $nCall, $nByte, '$lStack');\n"
1210 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +00001211 set frames($f) 1
1212 }
1213 }
1214
1215 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +00001216 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +00001217
1218 foreach f [array names frames] {
1219 set addr [format %x $f]
1220 set cmd "addr2line -e [info nameofexec] $addr"
1221 set line [eval exec $cmd]
1222 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00001223
1224 set file [lindex [split $line :] 0]
1225 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00001226 }
1227
danielk197735754ac2008-03-21 17:29:37 +00001228 foreach f [array names files] {
1229 set contents ""
1230 catch {
1231 set fd [open $f]
1232 set contents [read $fd]
1233 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001234 }
danielk197735754ac2008-03-21 17:29:37 +00001235 set contents [string map {' ''} $contents]
1236 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00001237 }
danielk19776f332c12008-03-21 14:22:44 +00001238
danielk197735754ac2008-03-21 17:29:37 +00001239 set fd [open $filename w]
1240 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
1241 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001242}
drh93aed5a2008-01-16 17:46:38 +00001243
danielk197732554c12005-01-22 03:39:39 +00001244# Copy file $from into $to. This is used because some versions of
1245# TCL for windows (notably the 8.4.1 binary package shipped with the
1246# current mingw release) have a broken "file copy" command.
1247#
1248proc copy_file {from to} {
1249 if {$::tcl_platform(platform)=="unix"} {
1250 file copy -force $from $to
1251 } else {
1252 set f [open $from]
1253 fconfigure $f -translation binary
1254 set t [open $to w]
1255 fconfigure $t -translation binary
1256 puts -nonewline $t [read $f [file size $from]]
1257 close $t
1258 close $f
1259 }
1260}
1261
danf5894502009-10-07 18:41:19 +00001262# Drop all tables in database [db]
1263proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00001264 ifcapable trigger&&foreignkey {
1265 set pk [$db one "PRAGMA foreign_keys"]
1266 $db eval "PRAGMA foreign_keys = OFF"
1267 }
drh9a6ffc82010-02-15 18:03:20 +00001268 foreach {idx name file} [db eval {PRAGMA database_list}] {
1269 if {$idx==1} {
1270 set master sqlite_temp_master
1271 } else {
1272 set master $name.sqlite_master
1273 }
1274 foreach {t type} [$db eval "
1275 SELECT name, type FROM $master
dana16d1062010-09-28 17:37:28 +00001276 WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X'
drh9a6ffc82010-02-15 18:03:20 +00001277 "] {
dana16d1062010-09-28 17:37:28 +00001278 $db eval "DROP $type \"$t\""
drh9a6ffc82010-02-15 18:03:20 +00001279 }
danf5894502009-10-07 18:41:19 +00001280 }
shaneh4e7b32f2009-12-17 22:12:51 +00001281 ifcapable trigger&&foreignkey {
1282 $db eval "PRAGMA foreign_keys = $pk"
1283 }
danf5894502009-10-07 18:41:19 +00001284}
1285
dan71cb5182010-04-26 12:39:03 +00001286#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00001287# If a test script is executed with global variable $::G(perm:name) set to
1288# "wal", then the tests are run in WAL mode. Otherwise, they should be run
1289# in rollback mode. The following Tcl procs are used to make this less
1290# intrusive:
dan71cb5182010-04-26 12:39:03 +00001291#
1292# wal_set_journal_mode ?DB?
1293#
1294# If running a WAL test, execute "PRAGMA journal_mode = wal" using
1295# connection handle DB. Otherwise, this command is a no-op.
1296#
1297# wal_check_journal_mode TESTNAME ?DB?
1298#
1299# If running a WAL test, execute a tests case that fails if the main
1300# database for connection handle DB is not currently a WAL database.
1301# Otherwise (if not running a WAL permutation) this is a no-op.
1302#
1303# wal_is_wal_mode
1304#
1305# Returns true if this test should be run in WAL mode. False otherwise.
1306#
1307proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00001308 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00001309}
1310proc wal_set_journal_mode {{db db}} {
1311 if { [wal_is_wal_mode] } {
1312 $db eval "PRAGMA journal_mode = WAL"
1313 }
1314}
1315proc wal_check_journal_mode {testname {db db}} {
1316 if { [wal_is_wal_mode] } {
1317 $db eval { SELECT * FROM sqlite_master }
1318 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
1319 }
1320}
1321
dan430e74c2010-06-07 17:47:26 +00001322proc permutation {} {
1323 set perm ""
1324 catch {set perm $::G(perm:name)}
1325 set perm
1326}
dancb79e512010-08-06 13:50:07 +00001327proc presql {} {
1328 set presql ""
1329 catch {set presql $::G(perm:presql)}
1330 set presql
1331}
dan430e74c2010-06-07 17:47:26 +00001332
danc1a60c52010-06-07 14:28:16 +00001333#-------------------------------------------------------------------------
1334#
1335proc slave_test_script {script} {
1336
1337 # Create the interpreter used to run the test script.
1338 interp create tinterp
1339
1340 # Populate some global variables that tester.tcl expects to see.
1341 foreach {var value} [list \
1342 ::argv0 $::argv0 \
1343 ::argv {} \
1344 ::SLAVE 1 \
1345 ] {
1346 interp eval tinterp [list set $var $value]
1347 }
1348
1349 # The alias used to access the global test counters.
1350 tinterp alias set_test_counter set_test_counter
1351
1352 # Set up the ::cmdlinearg array in the slave.
1353 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
1354
dan430e74c2010-06-07 17:47:26 +00001355 # Set up the ::G array in the slave.
1356 interp eval tinterp [list array set ::G [array get ::G]]
1357
danc1a60c52010-06-07 14:28:16 +00001358 # Load the various test interfaces implemented in C.
1359 load_testfixture_extensions tinterp
1360
1361 # Run the test script.
1362 interp eval tinterp $script
1363
danea5542d2010-07-06 11:26:15 +00001364 # Check if the interpreter call [run_thread_tests]
1365 if { [interp eval tinterp {info exists ::run_thread_tests_called}] } {
1366 set ::run_thread_tests_called 1
1367 }
1368
danc1a60c52010-06-07 14:28:16 +00001369 # Delete the interpreter used to run the test script.
1370 interp delete tinterp
1371}
1372
dan430e74c2010-06-07 17:47:26 +00001373proc slave_test_file {zFile} {
dan0626dfc2010-06-15 06:56:37 +00001374 set tail [file tail $zFile]
dan430e74c2010-06-07 17:47:26 +00001375
dan6a64d672011-04-04 15:38:16 +00001376 if {[info exists ::G(start:permutation)]} {
1377 if {[permutation] != $::G(start:permutation)} return
1378 unset ::G(start:permutation)
1379 }
1380 if {[info exists ::G(start:file)]} {
1381 if {$tail != $::G(start:file) && $tail!="$::G(start:file).test"} return
1382 unset ::G(start:file)
1383 }
1384
dan92d516a2010-07-05 14:54:48 +00001385 # Remember the value of the shared-cache setting. So that it is possible
1386 # to check afterwards that it was not modified by the test script.
1387 #
1388 ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] }
dan0626dfc2010-06-15 06:56:37 +00001389
dan92d516a2010-07-05 14:54:48 +00001390 # Run the test script in a slave interpreter.
1391 #
danea5542d2010-07-06 11:26:15 +00001392 unset -nocomplain ::run_thread_tests_called
dan0626dfc2010-06-15 06:56:37 +00001393 reset_prng_state
1394 set ::sqlite_open_file_count 0
1395 set time [time { slave_test_script [list source $zFile] }]
1396 set ms [expr [lindex $time 0] / 1000]
1397
dan92d516a2010-07-05 14:54:48 +00001398 # Test that all files opened by the test script were closed. Omit this
1399 # if the test script has "thread" in its name. The open file counter
1400 # is not thread-safe.
dan0626dfc2010-06-15 06:56:37 +00001401 #
danea5542d2010-07-06 11:26:15 +00001402 if {[info exists ::run_thread_tests_called]==0} {
dan92d516a2010-07-05 14:54:48 +00001403 do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0}
1404 }
dan430e74c2010-06-07 17:47:26 +00001405 set ::sqlite_open_file_count 0
1406
dan0626dfc2010-06-15 06:56:37 +00001407 # Test that the global "shared-cache" setting was not altered by
1408 # the test script.
1409 #
1410 ifcapable shared_cache {
1411 set res [expr {[sqlite3_enable_shared_cache] == $scs}]
1412 do_test ${tail}-sharedcachesetting [list set {} $res] 1
1413 }
dan430e74c2010-06-07 17:47:26 +00001414
dan92d516a2010-07-05 14:54:48 +00001415 # Add some info to the output.
1416 #
dan0626dfc2010-06-15 06:56:37 +00001417 puts "Time: $tail $ms ms"
dan430e74c2010-06-07 17:47:26 +00001418 show_memstats
danc1a60c52010-06-07 14:28:16 +00001419}
1420
dan59257dc2010-08-04 11:34:31 +00001421# Open a new connection on database test.db and execute the SQL script
1422# supplied as an argument. Before returning, close the new conection and
1423# restore the 4 byte fields starting at header offsets 28, 92 and 96
1424# to the values they held before the SQL was executed. This simulates
1425# a write by a pre-3.7.0 client.
1426#
1427proc sql36231 {sql} {
1428 set B [hexio_read test.db 92 8]
1429 set A [hexio_read test.db 28 4]
1430 sqlite3 db36231 test.db
1431 catch { db36231 func a_string a_string }
1432 execsql $sql db36231
1433 db36231 close
1434 hexio_write test.db 28 $A
1435 hexio_write test.db 92 $B
1436 return ""
1437}
danf5894502009-10-07 18:41:19 +00001438
danccc7ff42010-11-29 12:06:45 +00001439proc db_save {} {
1440 foreach f [glob -nocomplain sv_test.db*] { forcedelete $f }
1441 foreach f [glob -nocomplain test.db*] {
1442 set f2 "sv_$f"
1443 file copy -force $f $f2
1444 }
1445}
1446proc db_save_and_close {} {
1447 db_save
1448 catch { db close }
1449 return ""
1450}
1451proc db_restore {} {
1452 foreach f [glob -nocomplain test.db*] { forcedelete $f }
1453 foreach f2 [glob -nocomplain sv_test.db*] {
1454 set f [string range $f2 3 end]
1455 file copy -force $f2 $f
1456 }
1457}
1458proc db_restore_and_reopen {{dbfile test.db}} {
1459 catch { db close }
1460 db_restore
1461 sqlite3 db $dbfile
1462}
1463proc db_delete_and_reopen {{file test.db}} {
1464 catch { db close }
1465 foreach f [glob -nocomplain test.db*] { file delete -force $f }
1466 sqlite3 db $file
1467}
1468
danielk197745901d62004-11-10 15:27:38 +00001469# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
1470# to non-zero, then set the global variable $AUTOVACUUM to 1.
1471set AUTOVACUUM $sqlite_options(default_autovacuum)
danielk1977ee8b7992009-03-26 17:13:06 +00001472
1473source $testdir/thread_common.tcl
danddf80eb2010-10-25 12:47:43 +00001474source $testdir/malloc_common.tcl