blob: 5911f5a54454d10c6dec9fe7afc3bdccd95ed728 [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 }
dan3ccd20a2010-06-09 19:01:02 +0000112 set res
dan430e74c2010-06-07 17:47:26 +0000113 } else {
114 # This command is not opening a new database connection. Pass the
115 # arguments through to the C implemenation as the are.
116 #
117 uplevel 1 sqlite_orig $args
drh9eb9e262004-02-11 02:18:05 +0000118 }
drh9eb9e262004-02-11 02:18:05 +0000119 }
120}
121
dancb354602010-07-08 09:44:42 +0000122proc execpresql {handle args} {
123 trace remove execution $handle enter [list execpresql $handle]
124 if {[info exists ::G(perm:presql)]} {
125 $handle eval $::G(perm:presql)
126 }
127}
128
dan68928b62010-06-22 13:46:43 +0000129# This command should be called after loading tester.tcl from within
130# all test scripts that are incompatible with encryption codecs.
131#
132proc do_not_use_codec {} {
133 set ::do_not_use_codec 1
134 reset_db
135}
136
dan430e74c2010-06-07 17:47:26 +0000137# The following block only runs the first time this file is sourced. It
138# does not run in slave interpreters (since the ::cmdlinearg array is
139# populated before the test script is run in slave interpreters).
drhbec2bf42000-05-29 23:48:22 +0000140#
danc1a60c52010-06-07 14:28:16 +0000141if {[info exists cmdlinearg]==0} {
142
143 # Parse any options specified in the $argv array. This script accepts the
144 # following options:
145 #
146 # --pause
147 # --soft-heap-limit=NN
148 # --maxerror=NN
149 # --malloctrace=N
150 # --backtrace=N
151 # --binarylog=N
dan0626dfc2010-06-15 06:56:37 +0000152 # --soak=N
danc1a60c52010-06-07 14:28:16 +0000153 #
154 set cmdlinearg(soft-heap-limit) 0
155 set cmdlinearg(maxerror) 1000
156 set cmdlinearg(malloctrace) 0
157 set cmdlinearg(backtrace) 10
158 set cmdlinearg(binarylog) 0
dan0626dfc2010-06-15 06:56:37 +0000159 set cmdlinearg(soak) 0
danc1a60c52010-06-07 14:28:16 +0000160
161 set leftover [list]
162 foreach a $argv {
163 switch -regexp -- $a {
164 {^-+pause$} {
165 # Wait for user input before continuing. This is to give the user an
166 # opportunity to connect profiling tools to the process.
167 puts -nonewline "Press RETURN to begin..."
168 flush stdout
169 gets stdin
170 }
171 {^-+soft-heap-limit=.+$} {
172 foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break
173 }
174 {^-+maxerror=.+$} {
175 foreach {dummy cmdlinearg(maxerror)} [split $a =] break
176 }
177 {^-+malloctrace=.+$} {
178 foreach {dummy cmdlinearg(malloctrace)} [split $a =] break
179 if {$cmdlinearg(malloctrace)} {
180 sqlite3_memdebug_log start
181 }
182 }
183 {^-+backtrace=.+$} {
184 foreach {dummy cmdlinearg(backtrace)} [split $a =] break
dan0626dfc2010-06-15 06:56:37 +0000185 sqlite3_memdebug_backtrace $value
danc1a60c52010-06-07 14:28:16 +0000186 }
danc1a60c52010-06-07 14:28:16 +0000187 {^-+binarylog=.+$} {
188 foreach {dummy cmdlinearg(binarylog)} [split $a =] break
189 }
dan0626dfc2010-06-15 06:56:37 +0000190 {^-+soak=.+$} {
191 foreach {dummy cmdlinearg(soak)} [split $a =] break
192 set ::G(issoak) $cmdlinearg(soak)
193 }
danc1a60c52010-06-07 14:28:16 +0000194 default {
195 lappend leftover $a
196 }
197 }
198 }
199 set argv $leftover
200
dan430e74c2010-06-07 17:47:26 +0000201 # Install the malloc layer used to inject OOM errors. And the 'automatic'
202 # extensions. This only needs to be done once for the process.
203 #
danielk1977f7b9d662008-06-23 18:49:43 +0000204 sqlite3_shutdown
205 install_malloc_faultsim 1
206 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000207 autoinstall_test_functions
danc1a60c52010-06-07 14:28:16 +0000208
dan430e74c2010-06-07 17:47:26 +0000209 # If the --binarylog option was specified, create the logging VFS. This
210 # call installs the new VFS as the default for all SQLite connections.
211 #
danc1a60c52010-06-07 14:28:16 +0000212 if {$cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000213 vfslog new binarylog {} vfslog.bin
danc1a60c52010-06-07 14:28:16 +0000214 }
215
216 # Set the backtrace depth, if malloc tracing is enabled.
217 #
218 if {$cmdlinearg(malloctrace)} {
219 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danielk1977185eac92008-07-12 15:55:54 +0000220 }
danielk1977f7b9d662008-06-23 18:49:43 +0000221}
danielk197703ba3fa2009-01-09 10:49:14 +0000222
danc1a60c52010-06-07 14:28:16 +0000223# Update the soft-heap-limit each time this script is run. In that
224# way if an individual test file changes the soft-heap-limit, it
225# will be reset at the start of the next test file.
226#
227sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
228
229# Create a test database
230#
danielk197703ba3fa2009-01-09 10:49:14 +0000231proc reset_db {} {
232 catch {db close}
233 file delete -force test.db
234 file delete -force test.db-journal
dand3f8f942010-04-13 11:35:01 +0000235 file delete -force test.db-wal
danielk197703ba3fa2009-01-09 10:49:14 +0000236 sqlite3 db ./test.db
237 set ::DB [sqlite3_connection_pointer db]
238 if {[info exists ::SETUP_SQL]} {
239 db eval $::SETUP_SQL
240 }
drhcd61c282002-03-06 22:01:34 +0000241}
danielk197703ba3fa2009-01-09 10:49:14 +0000242reset_db
drhbec2bf42000-05-29 23:48:22 +0000243
244# Abort early if this script has been run before.
245#
danc1a60c52010-06-07 14:28:16 +0000246if {[info exists TC(count)]} return
drhbec2bf42000-05-29 23:48:22 +0000247
drhce2198c2010-09-10 13:22:59 +0000248# Make sure memory statistics are enabled.
249#
250sqlite3_config_memstatus 1
251
danc1a60c52010-06-07 14:28:16 +0000252# Initialize the test counters and set up commands to access them.
253# Or, if this is a slave interpreter, set up aliases to write the
254# counters in the parent interpreter.
drhbec2bf42000-05-29 23:48:22 +0000255#
danc1a60c52010-06-07 14:28:16 +0000256if {0==[info exists ::SLAVE]} {
257 set TC(errors) 0
258 set TC(count) 0
259 set TC(fail_list) [list]
260 set TC(omit_list) [list]
261
262 proc set_test_counter {counter args} {
263 if {[llength $args]} {
264 set ::TC($counter) [lindex $args 0]
265 }
266 set ::TC($counter)
267 }
drhb62c3352006-11-23 09:39:16 +0000268}
drh348784e2000-05-29 20:41:49 +0000269
drh521cc842008-04-15 02:36:33 +0000270# Record the fact that a sequence of tests were omitted.
271#
272proc omit_test {name reason} {
danc1a60c52010-06-07 14:28:16 +0000273 set omitList [set_test_counter omit_list]
drh521cc842008-04-15 02:36:33 +0000274 lappend omitList [list $name $reason]
danc1a60c52010-06-07 14:28:16 +0000275 set_test_counter omit_list $omitList
drh521cc842008-04-15 02:36:33 +0000276}
277
danc1a60c52010-06-07 14:28:16 +0000278# Record the fact that a test failed.
279#
280proc fail_test {name} {
281 set f [set_test_counter fail_list]
282 lappend f $name
283 set_test_counter fail_list $f
284 set_test_counter errors [expr [set_test_counter errors] + 1]
285
286 set nFail [set_test_counter errors]
287 if {$nFail>=$::cmdlinearg(maxerror)} {
288 puts "*** Giving up..."
289 finalize_testing
290 }
291}
292
293# Increment the number of tests run
294#
295proc incr_ntest {} {
296 set_test_counter count [expr [set_test_counter count] + 1]
297}
298
299
drh348784e2000-05-29 20:41:49 +0000300# Invoke the do_test procedure to run a single test
301#
302proc do_test {name cmd expected} {
danc1a60c52010-06-07 14:28:16 +0000303
304 global argv cmdlinearg
305
dan8c408002010-11-01 17:38:24 +0000306 fix_testname name
307
drh4a50aac2007-08-23 02:47:53 +0000308 sqlite3_memdebug_settitle $name
danc1a60c52010-06-07 14:28:16 +0000309
dan92d516a2010-07-05 14:54:48 +0000310# if {[llength $argv]==0} {
311# set go 1
312# } else {
313# set go 0
314# foreach pattern $argv {
315# if {[string match $pattern $name]} {
316# set go 1
317# break
318# }
319# }
320# }
dan430e74c2010-06-07 17:47:26 +0000321
dand506de02010-07-03 13:59:01 +0000322 if {[info exists ::G(perm:prefix)]} {
323 set name "$::G(perm:prefix)$name"
dan430e74c2010-06-07 17:47:26 +0000324 }
325
danc1a60c52010-06-07 14:28:16 +0000326 incr_ntest
drh5edc3122001-09-13 21:53:09 +0000327 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000328 flush stdout
329 if {[catch {uplevel #0 "$cmd;\n"} result]} {
330 puts "\nError: $result"
danc1a60c52010-06-07 14:28:16 +0000331 fail_test $name
drh348784e2000-05-29 20:41:49 +0000332 } elseif {[string compare $result $expected]} {
333 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
danc1a60c52010-06-07 14:28:16 +0000334 fail_test $name
drh348784e2000-05-29 20:41:49 +0000335 } else {
336 puts " Ok"
337 }
drh6558db82007-04-13 03:23:21 +0000338 flush stdout
drh348784e2000-05-29 20:41:49 +0000339}
dana3f51082010-09-30 18:43:14 +0000340
341proc fix_testname {varname} {
342 upvar $varname testname
343 if {[info exists ::testprefix]
344 && [string is digit [string range $testname 0 0]]
345 } {
346 set testname "${::testprefix}-$testname"
347 }
348}
dan153eda02010-06-21 07:45:47 +0000349
dan57f7f4b2010-10-01 19:04:37 +0000350proc do_execsql_test {testname sql {result {}}} {
dana3f51082010-09-30 18:43:14 +0000351 fix_testname testname
dan47eb16d2010-11-11 10:36:25 +0000352 uplevel do_test $testname [list "execsql {$sql}"] [list [list {*}$result]]
dan153eda02010-06-21 07:45:47 +0000353}
354proc do_catchsql_test {testname sql result} {
dana3f51082010-09-30 18:43:14 +0000355 fix_testname testname
dan153eda02010-06-21 07:45:47 +0000356 uplevel do_test $testname [list "catchsql {$sql}"] [list $result]
357}
dan39854792010-11-15 16:12:58 +0000358proc do_eqp_test {name sql res} {
359 uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res]
360}
dan153eda02010-06-21 07:45:47 +0000361
dan51f06982010-09-18 19:00:12 +0000362#-------------------------------------------------------------------------
363# Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST
364#
365# Where switches are:
366#
367# -errorformat FMTSTRING
368# -count
danaf1dcab2010-09-20 19:17:53 +0000369# -query SQL
dana16d1062010-09-28 17:37:28 +0000370# -tclquery TCL
danaf7626f2010-09-23 18:47:36 +0000371# -repair TCL
dan51f06982010-09-18 19:00:12 +0000372#
373proc do_select_tests {prefix args} {
374
375 set testlist [lindex $args end]
376 set switches [lrange $args 0 end-1]
377
378 set errfmt ""
379 set countonly 0
dana16d1062010-09-28 17:37:28 +0000380 set tclquery ""
danaf7626f2010-09-23 18:47:36 +0000381 set repair ""
dan51f06982010-09-18 19:00:12 +0000382
383 for {set i 0} {$i < [llength $switches]} {incr i} {
384 set s [lindex $switches $i]
385 set n [string length $s]
danaf1dcab2010-09-20 19:17:53 +0000386 if {$n>=2 && [string equal -length $n $s "-query"]} {
dana16d1062010-09-28 17:37:28 +0000387 set tclquery [list execsql [lindex $switches [incr i]]]
388 } elseif {$n>=2 && [string equal -length $n $s "-tclquery"]} {
389 set tclquery [lindex $switches [incr i]]
danaf1dcab2010-09-20 19:17:53 +0000390 } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} {
dan51f06982010-09-18 19:00:12 +0000391 set errfmt [lindex $switches [incr i]]
danaf7626f2010-09-23 18:47:36 +0000392 } elseif {$n>=2 && [string equal -length $n $s "-repair"]} {
393 set repair [lindex $switches [incr i]]
dan51f06982010-09-18 19:00:12 +0000394 } elseif {$n>=2 && [string equal -length $n $s "-count"]} {
395 set countonly 1
396 } else {
397 error "unknown switch: $s"
398 }
399 }
400
401 if {$countonly && $errfmt!=""} {
402 error "Cannot use -count and -errorformat together"
403 }
404 set nTestlist [llength $testlist]
405 if {$nTestlist%3 || $nTestlist==0 } {
406 error "SELECT test list contains [llength $testlist] elements"
407 }
408
danaf7626f2010-09-23 18:47:36 +0000409 eval $repair
dan51f06982010-09-18 19:00:12 +0000410 foreach {tn sql res} $testlist {
dana16d1062010-09-28 17:37:28 +0000411 if {$tclquery != ""} {
danaf1dcab2010-09-20 19:17:53 +0000412 execsql $sql
dana16d1062010-09-28 17:37:28 +0000413 uplevel do_test ${prefix}.$tn [list $tclquery] [list [list {*}$res]]
414 } elseif {$countonly} {
dan51f06982010-09-18 19:00:12 +0000415 set nRow 0
416 db eval $sql {incr nRow}
417 uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res]
418 } elseif {$errfmt==""} {
419 uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]]
420 } else {
421 set res [list 1 [string trim [format $errfmt {*}$res]]]
422 uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res]
423 }
danaf7626f2010-09-23 18:47:36 +0000424 eval $repair
dan51f06982010-09-18 19:00:12 +0000425 }
danaf7626f2010-09-23 18:47:36 +0000426
dan51f06982010-09-18 19:00:12 +0000427}
428
danb04757a2010-09-21 16:59:16 +0000429proc delete_all_data {} {
430 db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} {
431 db eval "DELETE FROM '[string map {' ''} $t]'"
432 }
433}
drh348784e2000-05-29 20:41:49 +0000434
drhb62c3352006-11-23 09:39:16 +0000435# Run an SQL script.
436# Return the number of microseconds per statement.
437#
drh3590f152006-11-23 21:09:10 +0000438proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000439 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000440 flush stdout
441 set speed [time {sqlite3_exec_nr db $sql}]
442 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000443 if {$tm == 0} {
444 set rate [format %20s "many"]
445 } else {
446 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
447 }
drh3590f152006-11-23 21:09:10 +0000448 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000449 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000450 global total_time
451 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000452 lappend ::speed_trial_times $name $tm
drhdd924312007-03-31 22:34:16 +0000453}
drh45c236d2008-03-22 01:08:00 +0000454proc speed_trial_tcl {name numstmt units script} {
455 puts -nonewline [format {%-21.21s } $name...]
456 flush stdout
457 set speed [time {eval $script}]
458 set tm [lindex $speed 0]
459 if {$tm == 0} {
460 set rate [format %20s "many"]
461 } else {
462 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
463 }
464 set u2 $units/s
465 puts [format {%12d uS %s %s} $tm $rate $u2]
466 global total_time
467 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000468 lappend ::speed_trial_times $name $tm
drh45c236d2008-03-22 01:08:00 +0000469}
drhdd924312007-03-31 22:34:16 +0000470proc speed_trial_init {name} {
471 global total_time
472 set total_time 0
dana975b592010-10-08 16:09:43 +0000473 set ::speed_trial_times [list]
drhbb810a92010-07-03 12:00:53 +0000474 sqlite3 versdb :memory:
475 set vers [versdb one {SELECT sqlite_source_id()}]
476 versdb close
477 puts "SQLite $vers"
drhdd924312007-03-31 22:34:16 +0000478}
479proc speed_trial_summary {name} {
480 global total_time
481 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
dana975b592010-10-08 16:09:43 +0000482
483 if { 0 } {
484 sqlite3 versdb :memory:
485 set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0]
486 versdb close
487 puts "CREATE TABLE IF NOT EXISTS time(version, script, test, us);"
488 foreach {test us} $::speed_trial_times {
489 puts "INSERT INTO time VALUES('$vers', '$name', '$test', $us);"
490 }
491 }
drhb62c3352006-11-23 09:39:16 +0000492}
493
drh348784e2000-05-29 20:41:49 +0000494# Run this routine last
495#
496proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +0000497 catch {db close}
498 catch {db2 close}
499 catch {db3 close}
500 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +0000501}
502proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +0000503 global sqlite_open_file_count
504
505 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +0000506
drh6e142f52000-06-08 13:36:40 +0000507 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000508 catch {db2 close}
509 catch {db3 close}
510
drh9bc54492007-10-23 14:49:59 +0000511 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000512 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000513 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000514 db close
drh984bfaa2008-03-19 16:08:53 +0000515 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +0000516
drh3a7fb7c2007-08-10 16:41:08 +0000517 sqlite3_soft_heap_limit 0
danc1a60c52010-06-07 14:28:16 +0000518 set nTest [incr_ntest]
519 set nErr [set_test_counter errors]
520
drh348784e2000-05-29 20:41:49 +0000521 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000522 if {$nErr>0} {
danc1a60c52010-06-07 14:28:16 +0000523 puts "Failures on these tests: [set_test_counter fail_list]"
drhf3a65f72007-08-22 20:18:21 +0000524 }
danielk1977ee8b7992009-03-26 17:13:06 +0000525 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +0000526 if {[llength $omitList]>0} {
527 puts "Omitted test cases:"
528 set prec {}
529 foreach {rec} [lsort $omitList] {
530 if {$rec==$prec} continue
531 set prec $rec
532 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
533 }
534 }
drh80788d82006-09-02 14:50:23 +0000535 if {$nErr>0 && ![working_64bit_int]} {
536 puts "******************************************************************"
537 puts "N.B.: The version of TCL that you used to build this test harness"
538 puts "is defective in that it does not support 64-bit integers. Some or"
539 puts "all of the test failures above might be a result from this defect"
540 puts "in your TCL build."
541 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000542 }
danc1a60c52010-06-07 14:28:16 +0000543 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000544 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +0000545 }
drh94e92032003-02-16 22:21:32 +0000546 if {$sqlite_open_file_count} {
547 puts "$sqlite_open_file_count files were left open"
548 incr nErr
549 }
drheafc43b2010-07-26 18:43:40 +0000550 if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 ||
551 [sqlite3_memory_used]>0} {
552 puts "Unfreed memory: [sqlite3_memory_used] bytes in\
553 [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations"
drhf3a65f72007-08-22 20:18:21 +0000554 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000555 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000556 puts "Writing unfreed memory log to \"./memleak.txt\""
557 sqlite3_memdebug_dump ./memleak.txt
558 }
drhf3a65f72007-08-22 20:18:21 +0000559 } else {
560 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000561 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000562 sqlite3_memdebug_dump ./memusage.txt
563 }
drhf3a65f72007-08-22 20:18:21 +0000564 }
drhf7141992008-06-19 00:16:08 +0000565 show_memstats
drh91fd4d42008-01-19 20:11:25 +0000566 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
567 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000568 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
569 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
570 }
danc1a60c52010-06-07 14:28:16 +0000571 if {$::cmdlinearg(malloctrace)} {
danielk197735754ac2008-03-21 17:29:37 +0000572 puts "Writing mallocs.sql..."
573 memdebug_log_sql
574 sqlite3_memdebug_log stop
575 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000576
577 if {[sqlite3_memory_used]>0} {
578 puts "Writing leaks.sql..."
579 sqlite3_memdebug_log sync
580 memdebug_log_sql leaks.sql
581 }
danielk197735754ac2008-03-21 17:29:37 +0000582 }
drhd9910fe2006-10-04 11:55:49 +0000583 foreach f [glob -nocomplain test.db-*-journal] {
584 file delete -force $f
585 }
586 foreach f [glob -nocomplain test.db-mj*] {
587 file delete -force $f
588 }
drhdb25e382001-03-15 18:21:22 +0000589 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000590}
591
drhf7141992008-06-19 00:16:08 +0000592# Display memory statistics for analysis and debugging purposes.
593#
594proc show_memstats {} {
595 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +0000596 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
597 set val [format {now %10d max %10d max-size %10d} \
598 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000599 puts "Memory used: $val"
drheafc43b2010-07-26 18:43:40 +0000600 set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0]
601 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
602 puts "Allocation count: $val"
drhf7141992008-06-19 00:16:08 +0000603 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +0000604 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
605 set val [format {now %10d max %10d max-size %10d} \
606 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000607 puts "Page-cache used: $val"
608 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
609 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
610 puts "Page-cache overflow: $val"
611 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
612 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
613 puts "Scratch memory used: $val"
614 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +0000615 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
616 set val [format {now %10d max %10d max-size %10d} \
617 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000618 puts "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +0000619 ifcapable yytrackmaxstackdepth {
620 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
621 set val [format { max %10d} [lindex $x 2]]
622 puts "Parser stack depth: $val"
623 }
drhf7141992008-06-19 00:16:08 +0000624}
625
drh348784e2000-05-29 20:41:49 +0000626# A procedure to execute SQL
627#
drhc4a3c772001-04-04 11:48:57 +0000628proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000629 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000630 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000631}
drh3aadb2e2000-05-31 17:59:25 +0000632
drhadbca9c2001-09-27 15:11:53 +0000633# Execute SQL and catch exceptions.
634#
635proc catchsql {sql {db db}} {
636 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +0000637 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +0000638 lappend r $msg
639 return $r
640}
641
drh04096482001-11-09 22:41:44 +0000642# Do an VDBE code dump on the SQL given
643#
644proc explain {sql {db db}} {
645 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000646 puts "addr opcode p1 p2 p3 p4 p5 #"
647 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000648 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000649 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
650 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
651 ]
drh04096482001-11-09 22:41:44 +0000652 }
653}
654
drhdafc0ce2008-04-17 19:14:02 +0000655# Show the VDBE program for an SQL statement but omit the Trace
656# opcode at the beginning. This procedure can be used to prove
657# that different SQL statements generate exactly the same VDBE code.
658#
659proc explain_no_trace {sql} {
660 set tr [db eval "EXPLAIN $sql"]
661 return [lrange $tr 7 end]
662}
663
drh3aadb2e2000-05-31 17:59:25 +0000664# Another procedure to execute SQL. This one includes the field
665# names in the returned list.
666#
667proc execsql2 {sql} {
668 set result {}
669 db eval $sql data {
670 foreach f $data(*) {
671 lappend result $f $data($f)
672 }
673 }
674 return $result
675}
drh17a68932001-01-31 13:28:08 +0000676
drhdde85d92003-03-01 19:45:34 +0000677# Use the non-callback API to execute multiple SQL statements
678#
679proc stepsql {dbptr sql} {
680 set sql [string trim $sql]
681 set r 0
682 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000683 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000684 return [list 1 $vm]
685 }
686 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000687# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
688# foreach v $VAL {lappend r $v}
689# }
690 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
691 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
692 lappend r [sqlite3_column_text $vm $i]
693 }
drhdde85d92003-03-01 19:45:34 +0000694 }
danielk1977106bb232004-05-21 10:08:53 +0000695 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000696 return [list 1 $errmsg]
697 }
698 }
699 return $r
700}
701
drh17a68932001-01-31 13:28:08 +0000702# Delete a file or directory
703#
dana16d1062010-09-28 17:37:28 +0000704proc forcedelete {args} {
705 foreach filename $args {
706 # On windows, sometimes even a [file delete -force] can fail just after
707 # a file is closed. The cause is usually "tag-alongs" - programs like
708 # anti-virus software, automatic backup tools and various explorer
709 # extensions that keep a file open a little longer than we expect, causing
710 # the delete to fail.
711 #
712 # The solution is to wait a short amount of time before retrying the
713 # delete.
714 #
715 set nRetry 50 ;# Maximum number of retries.
716 set nDelay 100 ;# Delay in ms before retrying.
717 for {set i 0} {$i<$nRetry} {incr i} {
718 set rc [catch {file delete -force $filename} msg]
719 if {$rc==0} break
720 after $nDelay
721 }
722 if {$rc} { error $msg }
drh17a68932001-01-31 13:28:08 +0000723 }
724}
drh21504322002-06-25 13:16:02 +0000725
726# Do an integrity check of the entire database
727#
danielk197704103022009-02-03 16:51:24 +0000728proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +0000729 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +0000730 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +0000731 }
732}
733
danielk1977db4e8862008-01-16 08:24:46 +0000734proc fix_ifcapable_expr {expr} {
735 set ret ""
736 set state 0
737 for {set i 0} {$i < [string length $expr]} {incr i} {
738 set char [string range $expr $i $i]
739 set newstate [expr {[string is alnum $char] || $char eq "_"}]
740 if {$newstate && !$state} {
741 append ret {$::sqlite_options(}
742 }
743 if {!$newstate && $state} {
744 append ret )
745 }
746 append ret $char
747 set state $newstate
748 }
749 if {$state} {append ret )}
750 return $ret
751}
752
drh27d258a2004-10-30 20:23:09 +0000753# Evaluate a boolean expression of capabilities. If true, execute the
754# code. Omit the code if false.
755#
danielk19773e8c37e2005-01-21 03:12:14 +0000756proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +0000757 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
758 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +0000759 if ($e2) {
760 set c [catch {uplevel 1 $code} r]
761 } else {
762 set c [catch {uplevel 1 $elsecode} r]
763 }
764 return -code $c $r
drh21504322002-06-25 13:16:02 +0000765}
danielk197745901d62004-11-10 15:27:38 +0000766
danielk1977aca790a2005-01-13 11:07:52 +0000767# This proc execs a seperate process that crashes midway through executing
768# the SQL script $sql on database test.db.
769#
770# The crash occurs during a sync() of file $crashfile. When the crash
771# occurs a random subset of all unsynced writes made by the process are
772# written into the files on disk. Argument $crashdelay indicates the
773# number of file syncs to wait before crashing.
774#
775# The return value is a list of two elements. The first element is a
776# boolean, indicating whether or not the process actually crashed or
777# reported some other error. The second element in the returned list is the
778# error message. This is "child process exited abnormally" if the crash
779# occured.
780#
danielk1977f8940ae2007-08-23 11:07:10 +0000781# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +0000782#
783proc crashsql {args} {
danielk197759a33f92007-03-17 10:26:59 +0000784
785 set blocksize ""
786 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +0000787 set prngseed 0
drhf8587402008-01-08 16:03:49 +0000788 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +0000789 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +0000790 set dc ""
danielk197759a33f92007-03-17 10:26:59 +0000791 set sql [lindex $args end]
792
793 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
794 set z [lindex $args $ii]
795 set n [string length $z]
796 set z2 [lindex $args [expr $ii+1]]
797
798 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +0000799 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +0000800 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +0000801 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000802 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +0000803 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +0000804 else { error "Unrecognized option: $z" }
805 }
806
807 if {$crashfile eq ""} {
808 error "Compulsory option -file missing"
809 }
810
shanehf2c08822010-07-08 16:30:44 +0000811 # $crashfile gets compared to the native filename in
812 # cfSync(), which can be different then what TCL uses by
813 # default, so here we force it to the "nativename" format.
814 set cfile [string map {\\ \\\\} [file nativename [file join [pwd] $crashfile]]]
danielk1977aca790a2005-01-13 11:07:52 +0000815
816 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +0000817 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +0000818 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +0000819 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +0000820 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +0000821
822 # This block sets the cache size of the main database to 10
823 # pages. This is done in case the build is configured to omit
824 # "PRAGMA cache_size".
825 puts $f {db eval {SELECT * FROM sqlite_master;}}
826 puts $f {set bt [btree_from_db db]}
827 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +0000828 if {$prngseed} {
829 set seed [expr {$prngseed%10007+1}]
830 # puts seed=$seed
831 puts $f "db eval {SELECT randomblob($seed)}"
832 }
danielk1977933bbd62007-03-17 07:22:42 +0000833
drhf8587402008-01-08 16:03:49 +0000834 if {[string length $tclbody]>0} {
835 puts $f $tclbody
836 }
837 if {[string length $sql]>0} {
838 puts $f "db eval {"
839 puts $f "$sql"
840 puts $f "}"
841 }
danielk1977aca790a2005-01-13 11:07:52 +0000842 close $f
danielk1977aca790a2005-01-13 11:07:52 +0000843 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000844 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000845 } msg]
shanehf2c08822010-07-08 16:30:44 +0000846
847 # Windows/ActiveState TCL returns a slightly different
848 # error message. We map that to the expected message
849 # so that we don't have to change all of the test
850 # cases.
851 if {$::tcl_platform(platform)=="windows"} {
852 if {$msg=="child killed: unknown signal"} {
853 set msg "child process exited abnormally"
854 }
855 }
856
danielk1977aca790a2005-01-13 11:07:52 +0000857 lappend r $msg
858}
859
danielk197732554c12005-01-22 03:39:39 +0000860# Usage: do_ioerr_test <test number> <options...>
861#
862# This proc is used to implement test cases that check that IO errors
863# are correctly handled. The first argument, <test number>, is an integer
864# used to name the tests executed by this proc. Options are as follows:
865#
866# -tclprep TCL script to run to prepare test.
867# -sqlprep SQL script to run to prepare test.
868# -tclbody TCL script to run with IO error simulation.
869# -sqlbody TCL script to run with IO error simulation.
870# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000871# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +0000872# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +0000873# -start Value of 'N' to begin with (default 1)
874#
875# -cksum Boolean. If true, test that the database does
876# not change during the execution of the test case.
877#
878proc do_ioerr_test {testname args} {
879
danielk197732554c12005-01-22 03:39:39 +0000880 set ::ioerropts(-start) 1
881 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000882 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +0000883 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +0000884 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +0000885 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +0000886 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +0000887 array set ::ioerropts $args
888
danielk197747cd39c2008-05-12 12:41:15 +0000889 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
890 # a couple of obscure IO errors that do not return them.
891 set ::ioerropts(-erc) 0
892
danielk197732554c12005-01-22 03:39:39 +0000893 set ::go 1
drh93aed5a2008-01-16 17:46:38 +0000894 #reset_prng_state
895 save_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +0000896 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +0000897 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +0000898 incr ::ioerropts(-count) -1
899 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +0000900
901 # Skip this IO error if it was specified with the "-exclude" option.
902 if {[info exists ::ioerropts(-exclude)]} {
903 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
904 }
danielk1977861f7452008-06-05 11:39:11 +0000905 if {$::ioerropts(-restoreprng)} {
906 restore_prng_state
907 }
danielk197732554c12005-01-22 03:39:39 +0000908
909 # Delete the files test.db and test2.db, then execute the TCL and
910 # SQL (in that order) to prepare for the test case.
911 do_test $testname.$n.1 {
912 set ::sqlite_io_error_pending 0
913 catch {db close}
aswiftaebf4132008-11-21 00:10:35 +0000914 catch {db2 close}
danielk197732554c12005-01-22 03:39:39 +0000915 catch {file delete -force test.db}
916 catch {file delete -force test.db-journal}
917 catch {file delete -force test2.db}
918 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000919 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000920 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000921 if {[info exists ::ioerropts(-tclprep)]} {
922 eval $::ioerropts(-tclprep)
923 }
924 if {[info exists ::ioerropts(-sqlprep)]} {
925 execsql $::ioerropts(-sqlprep)
926 }
927 expr 0
928 } {0}
929
930 # Read the 'checksum' of the database.
931 if {$::ioerropts(-cksum)} {
932 set checksum [cksum]
933 }
drh93aed5a2008-01-16 17:46:38 +0000934
danielk197732554c12005-01-22 03:39:39 +0000935 # Set the Nth IO error to fail.
936 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +0000937 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +0000938 set ::sqlite_io_error_pending $n
939 }] $n
940
941 # Create a single TCL script from the TCL and SQL specified
942 # as the body of the test.
943 set ::ioerrorbody {}
944 if {[info exists ::ioerropts(-tclbody)]} {
945 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
946 }
947 if {[info exists ::ioerropts(-sqlbody)]} {
948 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
949 }
950
951 # Execute the TCL Script created in the above block. If
952 # there are at least N IO operations performed by SQLite as
953 # a result of the script, the Nth will fail.
954 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +0000955 set ::sqlite_io_error_hit 0
956 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +0000957 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +0000958 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +0000959 set rc [sqlite3_errcode $::DB]
960 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +0000961 # If we are in extended result code mode, make sure all of the
962 # IOERRs we get back really do have their extended code values.
963 # If an extended result code is returned, the sqlite3_errcode
964 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
965 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +0000966 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
967 return $rc
968 }
969 } else {
drh5f7b5bf2007-04-19 12:30:54 +0000970 # If we are not in extended result code mode, make sure no
971 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +0000972 if {[regexp {\+\d} $rc]} {
973 return $rc
974 }
975 }
drh93aed5a2008-01-16 17:46:38 +0000976 # The test repeats as long as $::go is non-zero. $::go starts out
977 # as 1. When a test runs to completion without hitting an I/O
978 # error, that means there is no point in continuing with this test
979 # case so set $::go to zero.
980 #
981 if {$::sqlite_io_error_pending>0} {
982 set ::go 0
983 set q 0
984 set ::sqlite_io_error_pending 0
985 } else {
986 set q 1
987 }
988
drhc9ac5ca2005-11-04 22:03:30 +0000989 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +0000990 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
991 set r 1
992 }
danielk1977f2fa8312006-01-24 13:09:33 +0000993 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +0000994
995 # One of two things must have happened. either
996 # 1. We never hit the IO error and the SQL returned OK
997 # 2. An IO error was hit and the SQL failed
998 #
dand41a29a2010-05-06 15:56:28 +0000999 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +00001000 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +00001001 } {1}
1002
danielk197780daec62008-05-12 10:57:02 +00001003 set ::sqlite_io_error_hit 0
1004 set ::sqlite_io_error_pending 0
1005
danielk197752b472a2008-05-05 16:23:55 +00001006 # Check that no page references were leaked. There should be
1007 # a single reference if there is still an active transaction,
1008 # or zero otherwise.
1009 #
danielk197781620542008-06-07 05:19:37 +00001010 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
1011 # locks are established on database files (i.e. if the error
1012 # occurs while attempting to detect a hot-journal file), then
1013 # there may 0 page references and an active transaction according
1014 # to [sqlite3_get_autocommit].
1015 #
danielk19774abd5442008-05-05 15:26:50 +00001016 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +00001017 do_test $testname.$n.4 {
1018 set bt [btree_from_db db]
1019 db_enter db
1020 array set stats [btree_pager_stats $bt]
1021 db_leave db
danielk197781620542008-06-07 05:19:37 +00001022 set nRef $stats(ref)
1023 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
1024 } {1}
danielk19774abd5442008-05-05 15:26:50 +00001025 }
1026
danielk197752b472a2008-05-05 16:23:55 +00001027 # If there is an open database handle and no open transaction,
1028 # and the pager is not running in exclusive-locking mode,
1029 # check that the pager is in "unlocked" state. Theoretically,
1030 # if a call to xUnlock() failed due to an IO error the underlying
1031 # file may still be locked.
1032 #
1033 ifcapable pragma {
1034 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +00001035 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +00001036 && [db one {pragma locking_mode}] eq "normal"
1037 && [sqlite3_get_autocommit db]
1038 } {
1039 do_test $testname.$n.5 {
1040 set bt [btree_from_db db]
1041 db_enter db
1042 array set stats [btree_pager_stats $bt]
1043 db_leave db
1044 set stats(state)
1045 } 0
1046 }
1047 }
1048
danielk197732554c12005-01-22 03:39:39 +00001049 # If an IO error occured, then the checksum of the database should
1050 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +00001051 #
drh1aa5af12008-03-07 19:51:14 +00001052 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +00001053 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +00001054 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +00001055 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +00001056 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +00001057 cksum
1058 } $checksum
1059 }
1060
drh1aa5af12008-03-07 19:51:14 +00001061 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +00001062 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +00001063 if {[info exists ::ioerropts(-cleanup)]} {
1064 catch $::ioerropts(-cleanup)
1065 }
danielk197732554c12005-01-22 03:39:39 +00001066 }
1067 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +00001068 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +00001069 unset ::ioerropts
1070}
1071
drh93aed5a2008-01-16 17:46:38 +00001072# Return a checksum based on the contents of the main database associated
1073# with connection $db
danielk197732554c12005-01-22 03:39:39 +00001074#
1075proc cksum {{db db}} {
1076 set txt [$db eval {
1077 SELECT name, type, sql FROM sqlite_master order by name
1078 }]\n
1079 foreach tbl [$db eval {
1080 SELECT name FROM sqlite_master WHERE type='table' order by name
1081 }] {
1082 append txt [$db eval "SELECT * FROM $tbl"]\n
1083 }
1084 foreach prag {default_synchronous default_cache_size} {
1085 append txt $prag-[$db eval "PRAGMA $prag"]\n
1086 }
1087 set cksum [string length $txt]-[md5 $txt]
1088 # puts $cksum-[file size test.db]
1089 return $cksum
1090}
1091
drh93aed5a2008-01-16 17:46:38 +00001092# Generate a checksum based on the contents of the main and temp tables
1093# database $db. If the checksum of two databases is the same, and the
1094# integrity-check passes for both, the two databases are identical.
1095#
drh1db639c2008-01-17 02:36:28 +00001096proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +00001097 set ret [list]
1098 ifcapable tempdb {
1099 set sql {
1100 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1101 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
1102 SELECT 'sqlite_master' UNION
1103 SELECT 'sqlite_temp_master' ORDER BY 1
1104 }
1105 } else {
1106 set sql {
1107 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1108 SELECT 'sqlite_master' ORDER BY 1
1109 }
1110 }
1111 set tbllist [$db eval $sql]
1112 set txt {}
1113 foreach tbl $tbllist {
1114 append txt [$db eval "SELECT * FROM $tbl"]
1115 }
1116 foreach prag {default_cache_size} {
1117 append txt $prag-[$db eval "PRAGMA $prag"]\n
1118 }
1119 # puts txt=$txt
1120 return [md5 $txt]
1121}
1122
drhdc2c4912009-02-04 22:46:47 +00001123# Generate a checksum based on the contents of a single database with
1124# a database connection. The name of the database is $dbname.
1125# Examples of $dbname are "temp" or "main".
1126#
1127proc dbcksum {db dbname} {
1128 if {$dbname=="temp"} {
1129 set master sqlite_temp_master
1130 } else {
1131 set master $dbname.sqlite_master
1132 }
1133 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
1134 set txt [$db eval "SELECT * FROM $master"]\n
1135 foreach tab $alltab {
1136 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
1137 }
1138 return [md5 $txt]
1139}
1140
danielk197735754ac2008-03-21 17:29:37 +00001141proc memdebug_log_sql {{filename mallocs.sql}} {
1142
danielk19776f332c12008-03-21 14:22:44 +00001143 set data [sqlite3_memdebug_log dump]
1144 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +00001145 if {$nFrame < 0} { return "" }
1146
danielk197735754ac2008-03-21 17:29:37 +00001147 set database temp
1148
danielk19776ab3a2e2009-02-19 14:39:25 +00001149 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +00001150
1151 set sql ""
1152 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +00001153 set nCall [lindex $e 0]
1154 set nByte [lindex $e 1]
1155 set lStack [lrange $e 2 end]
1156 append sql "INSERT INTO ${database}.malloc VALUES"
1157 append sql "('test', $nCall, $nByte, '$lStack');\n"
1158 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +00001159 set frames($f) 1
1160 }
1161 }
1162
1163 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +00001164 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +00001165
1166 foreach f [array names frames] {
1167 set addr [format %x $f]
1168 set cmd "addr2line -e [info nameofexec] $addr"
1169 set line [eval exec $cmd]
1170 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00001171
1172 set file [lindex [split $line :] 0]
1173 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00001174 }
1175
danielk197735754ac2008-03-21 17:29:37 +00001176 foreach f [array names files] {
1177 set contents ""
1178 catch {
1179 set fd [open $f]
1180 set contents [read $fd]
1181 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001182 }
danielk197735754ac2008-03-21 17:29:37 +00001183 set contents [string map {' ''} $contents]
1184 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00001185 }
danielk19776f332c12008-03-21 14:22:44 +00001186
danielk197735754ac2008-03-21 17:29:37 +00001187 set fd [open $filename w]
1188 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
1189 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001190}
drh93aed5a2008-01-16 17:46:38 +00001191
danielk197732554c12005-01-22 03:39:39 +00001192# Copy file $from into $to. This is used because some versions of
1193# TCL for windows (notably the 8.4.1 binary package shipped with the
1194# current mingw release) have a broken "file copy" command.
1195#
1196proc copy_file {from to} {
1197 if {$::tcl_platform(platform)=="unix"} {
1198 file copy -force $from $to
1199 } else {
1200 set f [open $from]
1201 fconfigure $f -translation binary
1202 set t [open $to w]
1203 fconfigure $t -translation binary
1204 puts -nonewline $t [read $f [file size $from]]
1205 close $t
1206 close $f
1207 }
1208}
1209
danf5894502009-10-07 18:41:19 +00001210# Drop all tables in database [db]
1211proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00001212 ifcapable trigger&&foreignkey {
1213 set pk [$db one "PRAGMA foreign_keys"]
1214 $db eval "PRAGMA foreign_keys = OFF"
1215 }
drh9a6ffc82010-02-15 18:03:20 +00001216 foreach {idx name file} [db eval {PRAGMA database_list}] {
1217 if {$idx==1} {
1218 set master sqlite_temp_master
1219 } else {
1220 set master $name.sqlite_master
1221 }
1222 foreach {t type} [$db eval "
1223 SELECT name, type FROM $master
dana16d1062010-09-28 17:37:28 +00001224 WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X'
drh9a6ffc82010-02-15 18:03:20 +00001225 "] {
dana16d1062010-09-28 17:37:28 +00001226 $db eval "DROP $type \"$t\""
drh9a6ffc82010-02-15 18:03:20 +00001227 }
danf5894502009-10-07 18:41:19 +00001228 }
shaneh4e7b32f2009-12-17 22:12:51 +00001229 ifcapable trigger&&foreignkey {
1230 $db eval "PRAGMA foreign_keys = $pk"
1231 }
danf5894502009-10-07 18:41:19 +00001232}
1233
dan71cb5182010-04-26 12:39:03 +00001234#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00001235# If a test script is executed with global variable $::G(perm:name) set to
1236# "wal", then the tests are run in WAL mode. Otherwise, they should be run
1237# in rollback mode. The following Tcl procs are used to make this less
1238# intrusive:
dan71cb5182010-04-26 12:39:03 +00001239#
1240# wal_set_journal_mode ?DB?
1241#
1242# If running a WAL test, execute "PRAGMA journal_mode = wal" using
1243# connection handle DB. Otherwise, this command is a no-op.
1244#
1245# wal_check_journal_mode TESTNAME ?DB?
1246#
1247# If running a WAL test, execute a tests case that fails if the main
1248# database for connection handle DB is not currently a WAL database.
1249# Otherwise (if not running a WAL permutation) this is a no-op.
1250#
1251# wal_is_wal_mode
1252#
1253# Returns true if this test should be run in WAL mode. False otherwise.
1254#
1255proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00001256 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00001257}
1258proc wal_set_journal_mode {{db db}} {
1259 if { [wal_is_wal_mode] } {
1260 $db eval "PRAGMA journal_mode = WAL"
1261 }
1262}
1263proc wal_check_journal_mode {testname {db db}} {
1264 if { [wal_is_wal_mode] } {
1265 $db eval { SELECT * FROM sqlite_master }
1266 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
1267 }
1268}
1269
dan430e74c2010-06-07 17:47:26 +00001270proc permutation {} {
1271 set perm ""
1272 catch {set perm $::G(perm:name)}
1273 set perm
1274}
dancb79e512010-08-06 13:50:07 +00001275proc presql {} {
1276 set presql ""
1277 catch {set presql $::G(perm:presql)}
1278 set presql
1279}
dan430e74c2010-06-07 17:47:26 +00001280
danc1a60c52010-06-07 14:28:16 +00001281#-------------------------------------------------------------------------
1282#
1283proc slave_test_script {script} {
1284
1285 # Create the interpreter used to run the test script.
1286 interp create tinterp
1287
1288 # Populate some global variables that tester.tcl expects to see.
1289 foreach {var value} [list \
1290 ::argv0 $::argv0 \
1291 ::argv {} \
1292 ::SLAVE 1 \
1293 ] {
1294 interp eval tinterp [list set $var $value]
1295 }
1296
1297 # The alias used to access the global test counters.
1298 tinterp alias set_test_counter set_test_counter
1299
1300 # Set up the ::cmdlinearg array in the slave.
1301 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
1302
dan430e74c2010-06-07 17:47:26 +00001303 # Set up the ::G array in the slave.
1304 interp eval tinterp [list array set ::G [array get ::G]]
1305
danc1a60c52010-06-07 14:28:16 +00001306 # Load the various test interfaces implemented in C.
1307 load_testfixture_extensions tinterp
1308
1309 # Run the test script.
1310 interp eval tinterp $script
1311
danea5542d2010-07-06 11:26:15 +00001312 # Check if the interpreter call [run_thread_tests]
1313 if { [interp eval tinterp {info exists ::run_thread_tests_called}] } {
1314 set ::run_thread_tests_called 1
1315 }
1316
danc1a60c52010-06-07 14:28:16 +00001317 # Delete the interpreter used to run the test script.
1318 interp delete tinterp
1319}
1320
dan430e74c2010-06-07 17:47:26 +00001321proc slave_test_file {zFile} {
dan0626dfc2010-06-15 06:56:37 +00001322 set tail [file tail $zFile]
dan430e74c2010-06-07 17:47:26 +00001323
dan92d516a2010-07-05 14:54:48 +00001324 # Remember the value of the shared-cache setting. So that it is possible
1325 # to check afterwards that it was not modified by the test script.
1326 #
1327 ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] }
dan0626dfc2010-06-15 06:56:37 +00001328
dan92d516a2010-07-05 14:54:48 +00001329 # Run the test script in a slave interpreter.
1330 #
danea5542d2010-07-06 11:26:15 +00001331 unset -nocomplain ::run_thread_tests_called
dan0626dfc2010-06-15 06:56:37 +00001332 reset_prng_state
1333 set ::sqlite_open_file_count 0
1334 set time [time { slave_test_script [list source $zFile] }]
1335 set ms [expr [lindex $time 0] / 1000]
1336
dan92d516a2010-07-05 14:54:48 +00001337 # Test that all files opened by the test script were closed. Omit this
1338 # if the test script has "thread" in its name. The open file counter
1339 # is not thread-safe.
dan0626dfc2010-06-15 06:56:37 +00001340 #
danea5542d2010-07-06 11:26:15 +00001341 if {[info exists ::run_thread_tests_called]==0} {
dan92d516a2010-07-05 14:54:48 +00001342 do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0}
1343 }
dan430e74c2010-06-07 17:47:26 +00001344 set ::sqlite_open_file_count 0
1345
dan0626dfc2010-06-15 06:56:37 +00001346 # Test that the global "shared-cache" setting was not altered by
1347 # the test script.
1348 #
1349 ifcapable shared_cache {
1350 set res [expr {[sqlite3_enable_shared_cache] == $scs}]
1351 do_test ${tail}-sharedcachesetting [list set {} $res] 1
1352 }
dan430e74c2010-06-07 17:47:26 +00001353
dan92d516a2010-07-05 14:54:48 +00001354 # Add some info to the output.
1355 #
dan0626dfc2010-06-15 06:56:37 +00001356 puts "Time: $tail $ms ms"
dan430e74c2010-06-07 17:47:26 +00001357 show_memstats
danc1a60c52010-06-07 14:28:16 +00001358}
1359
dan59257dc2010-08-04 11:34:31 +00001360# Open a new connection on database test.db and execute the SQL script
1361# supplied as an argument. Before returning, close the new conection and
1362# restore the 4 byte fields starting at header offsets 28, 92 and 96
1363# to the values they held before the SQL was executed. This simulates
1364# a write by a pre-3.7.0 client.
1365#
1366proc sql36231 {sql} {
1367 set B [hexio_read test.db 92 8]
1368 set A [hexio_read test.db 28 4]
1369 sqlite3 db36231 test.db
1370 catch { db36231 func a_string a_string }
1371 execsql $sql db36231
1372 db36231 close
1373 hexio_write test.db 28 $A
1374 hexio_write test.db 92 $B
1375 return ""
1376}
danf5894502009-10-07 18:41:19 +00001377
danielk197745901d62004-11-10 15:27:38 +00001378# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
1379# to non-zero, then set the global variable $AUTOVACUUM to 1.
1380set AUTOVACUUM $sqlite_options(default_autovacuum)
danielk1977ee8b7992009-03-26 17:13:06 +00001381
1382source $testdir/thread_common.tcl
danddf80eb2010-10-25 12:47:43 +00001383source $testdir/malloc_common.tcl