blob: 4c7c10ed733b56f72f8cd6072fee531b196841d6 [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
danc1a60c52010-06-07 14:28:16 +0000157 #
158 set cmdlinearg(soft-heap-limit) 0
159 set cmdlinearg(maxerror) 1000
160 set cmdlinearg(malloctrace) 0
161 set cmdlinearg(backtrace) 10
162 set cmdlinearg(binarylog) 0
dan0626dfc2010-06-15 06:56:37 +0000163 set cmdlinearg(soak) 0
danc1a60c52010-06-07 14:28:16 +0000164
165 set leftover [list]
166 foreach a $argv {
167 switch -regexp -- $a {
168 {^-+pause$} {
169 # Wait for user input before continuing. This is to give the user an
170 # opportunity to connect profiling tools to the process.
171 puts -nonewline "Press RETURN to begin..."
172 flush stdout
173 gets stdin
174 }
175 {^-+soft-heap-limit=.+$} {
176 foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break
177 }
178 {^-+maxerror=.+$} {
179 foreach {dummy cmdlinearg(maxerror)} [split $a =] break
180 }
181 {^-+malloctrace=.+$} {
182 foreach {dummy cmdlinearg(malloctrace)} [split $a =] break
183 if {$cmdlinearg(malloctrace)} {
184 sqlite3_memdebug_log start
185 }
186 }
187 {^-+backtrace=.+$} {
188 foreach {dummy cmdlinearg(backtrace)} [split $a =] break
dan0626dfc2010-06-15 06:56:37 +0000189 sqlite3_memdebug_backtrace $value
danc1a60c52010-06-07 14:28:16 +0000190 }
danc1a60c52010-06-07 14:28:16 +0000191 {^-+binarylog=.+$} {
192 foreach {dummy cmdlinearg(binarylog)} [split $a =] break
193 }
dan0626dfc2010-06-15 06:56:37 +0000194 {^-+soak=.+$} {
195 foreach {dummy cmdlinearg(soak)} [split $a =] break
196 set ::G(issoak) $cmdlinearg(soak)
197 }
danc1a60c52010-06-07 14:28:16 +0000198 default {
199 lappend leftover $a
200 }
201 }
202 }
203 set argv $leftover
204
dan430e74c2010-06-07 17:47:26 +0000205 # Install the malloc layer used to inject OOM errors. And the 'automatic'
206 # extensions. This only needs to be done once for the process.
207 #
danielk1977f7b9d662008-06-23 18:49:43 +0000208 sqlite3_shutdown
209 install_malloc_faultsim 1
210 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000211 autoinstall_test_functions
danc1a60c52010-06-07 14:28:16 +0000212
dan430e74c2010-06-07 17:47:26 +0000213 # If the --binarylog option was specified, create the logging VFS. This
214 # call installs the new VFS as the default for all SQLite connections.
215 #
danc1a60c52010-06-07 14:28:16 +0000216 if {$cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000217 vfslog new binarylog {} vfslog.bin
danc1a60c52010-06-07 14:28:16 +0000218 }
219
220 # Set the backtrace depth, if malloc tracing is enabled.
221 #
222 if {$cmdlinearg(malloctrace)} {
223 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danielk1977185eac92008-07-12 15:55:54 +0000224 }
danielk1977f7b9d662008-06-23 18:49:43 +0000225}
danielk197703ba3fa2009-01-09 10:49:14 +0000226
danc1a60c52010-06-07 14:28:16 +0000227# Update the soft-heap-limit each time this script is run. In that
228# way if an individual test file changes the soft-heap-limit, it
229# will be reset at the start of the next test file.
230#
231sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
232
233# Create a test database
234#
danielk197703ba3fa2009-01-09 10:49:14 +0000235proc reset_db {} {
236 catch {db close}
237 file delete -force test.db
238 file delete -force test.db-journal
dand3f8f942010-04-13 11:35:01 +0000239 file delete -force test.db-wal
danielk197703ba3fa2009-01-09 10:49:14 +0000240 sqlite3 db ./test.db
241 set ::DB [sqlite3_connection_pointer db]
242 if {[info exists ::SETUP_SQL]} {
243 db eval $::SETUP_SQL
244 }
drhcd61c282002-03-06 22:01:34 +0000245}
danielk197703ba3fa2009-01-09 10:49:14 +0000246reset_db
drhbec2bf42000-05-29 23:48:22 +0000247
248# Abort early if this script has been run before.
249#
danc1a60c52010-06-07 14:28:16 +0000250if {[info exists TC(count)]} return
drhbec2bf42000-05-29 23:48:22 +0000251
drhce2198c2010-09-10 13:22:59 +0000252# Make sure memory statistics are enabled.
253#
254sqlite3_config_memstatus 1
255
danc1a60c52010-06-07 14:28:16 +0000256# Initialize the test counters and set up commands to access them.
257# Or, if this is a slave interpreter, set up aliases to write the
258# counters in the parent interpreter.
drhbec2bf42000-05-29 23:48:22 +0000259#
danc1a60c52010-06-07 14:28:16 +0000260if {0==[info exists ::SLAVE]} {
261 set TC(errors) 0
262 set TC(count) 0
263 set TC(fail_list) [list]
264 set TC(omit_list) [list]
265
266 proc set_test_counter {counter args} {
267 if {[llength $args]} {
268 set ::TC($counter) [lindex $args 0]
269 }
270 set ::TC($counter)
271 }
drhb62c3352006-11-23 09:39:16 +0000272}
drh348784e2000-05-29 20:41:49 +0000273
drh521cc842008-04-15 02:36:33 +0000274# Record the fact that a sequence of tests were omitted.
275#
276proc omit_test {name reason} {
danc1a60c52010-06-07 14:28:16 +0000277 set omitList [set_test_counter omit_list]
drh521cc842008-04-15 02:36:33 +0000278 lappend omitList [list $name $reason]
danc1a60c52010-06-07 14:28:16 +0000279 set_test_counter omit_list $omitList
drh521cc842008-04-15 02:36:33 +0000280}
281
danc1a60c52010-06-07 14:28:16 +0000282# Record the fact that a test failed.
283#
284proc fail_test {name} {
285 set f [set_test_counter fail_list]
286 lappend f $name
287 set_test_counter fail_list $f
288 set_test_counter errors [expr [set_test_counter errors] + 1]
289
290 set nFail [set_test_counter errors]
291 if {$nFail>=$::cmdlinearg(maxerror)} {
292 puts "*** Giving up..."
293 finalize_testing
294 }
295}
296
297# Increment the number of tests run
298#
299proc incr_ntest {} {
300 set_test_counter count [expr [set_test_counter count] + 1]
301}
302
303
drh348784e2000-05-29 20:41:49 +0000304# Invoke the do_test procedure to run a single test
305#
306proc do_test {name cmd expected} {
danc1a60c52010-06-07 14:28:16 +0000307
308 global argv cmdlinearg
309
dan8c408002010-11-01 17:38:24 +0000310 fix_testname name
311
drh4a50aac2007-08-23 02:47:53 +0000312 sqlite3_memdebug_settitle $name
danc1a60c52010-06-07 14:28:16 +0000313
dan92d516a2010-07-05 14:54:48 +0000314# if {[llength $argv]==0} {
315# set go 1
316# } else {
317# set go 0
318# foreach pattern $argv {
319# if {[string match $pattern $name]} {
320# set go 1
321# break
322# }
323# }
324# }
dan430e74c2010-06-07 17:47:26 +0000325
dand506de02010-07-03 13:59:01 +0000326 if {[info exists ::G(perm:prefix)]} {
327 set name "$::G(perm:prefix)$name"
dan430e74c2010-06-07 17:47:26 +0000328 }
329
danc1a60c52010-06-07 14:28:16 +0000330 incr_ntest
drh5edc3122001-09-13 21:53:09 +0000331 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000332 flush stdout
333 if {[catch {uplevel #0 "$cmd;\n"} result]} {
334 puts "\nError: $result"
danc1a60c52010-06-07 14:28:16 +0000335 fail_test $name
drh348784e2000-05-29 20:41:49 +0000336 } elseif {[string compare $result $expected]} {
337 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
danc1a60c52010-06-07 14:28:16 +0000338 fail_test $name
drh348784e2000-05-29 20:41:49 +0000339 } else {
340 puts " Ok"
341 }
drh6558db82007-04-13 03:23:21 +0000342 flush stdout
drh348784e2000-05-29 20:41:49 +0000343}
dana3f51082010-09-30 18:43:14 +0000344
345proc fix_testname {varname} {
346 upvar $varname testname
347 if {[info exists ::testprefix]
348 && [string is digit [string range $testname 0 0]]
349 } {
350 set testname "${::testprefix}-$testname"
351 }
352}
dan153eda02010-06-21 07:45:47 +0000353
dan57f7f4b2010-10-01 19:04:37 +0000354proc do_execsql_test {testname sql {result {}}} {
dana3f51082010-09-30 18:43:14 +0000355 fix_testname testname
dan47eb16d2010-11-11 10:36:25 +0000356 uplevel do_test $testname [list "execsql {$sql}"] [list [list {*}$result]]
dan153eda02010-06-21 07:45:47 +0000357}
358proc do_catchsql_test {testname sql result} {
dana3f51082010-09-30 18:43:14 +0000359 fix_testname testname
dan153eda02010-06-21 07:45:47 +0000360 uplevel do_test $testname [list "catchsql {$sql}"] [list $result]
361}
dan39854792010-11-15 16:12:58 +0000362proc do_eqp_test {name sql res} {
363 uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res]
364}
dan153eda02010-06-21 07:45:47 +0000365
dan51f06982010-09-18 19:00:12 +0000366#-------------------------------------------------------------------------
367# Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST
368#
369# Where switches are:
370#
371# -errorformat FMTSTRING
372# -count
danaf1dcab2010-09-20 19:17:53 +0000373# -query SQL
dana16d1062010-09-28 17:37:28 +0000374# -tclquery TCL
danaf7626f2010-09-23 18:47:36 +0000375# -repair TCL
dan51f06982010-09-18 19:00:12 +0000376#
377proc do_select_tests {prefix args} {
378
379 set testlist [lindex $args end]
380 set switches [lrange $args 0 end-1]
381
382 set errfmt ""
383 set countonly 0
dana16d1062010-09-28 17:37:28 +0000384 set tclquery ""
danaf7626f2010-09-23 18:47:36 +0000385 set repair ""
dan51f06982010-09-18 19:00:12 +0000386
387 for {set i 0} {$i < [llength $switches]} {incr i} {
388 set s [lindex $switches $i]
389 set n [string length $s]
danaf1dcab2010-09-20 19:17:53 +0000390 if {$n>=2 && [string equal -length $n $s "-query"]} {
dana16d1062010-09-28 17:37:28 +0000391 set tclquery [list execsql [lindex $switches [incr i]]]
392 } elseif {$n>=2 && [string equal -length $n $s "-tclquery"]} {
393 set tclquery [lindex $switches [incr i]]
danaf1dcab2010-09-20 19:17:53 +0000394 } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} {
dan51f06982010-09-18 19:00:12 +0000395 set errfmt [lindex $switches [incr i]]
danaf7626f2010-09-23 18:47:36 +0000396 } elseif {$n>=2 && [string equal -length $n $s "-repair"]} {
397 set repair [lindex $switches [incr i]]
dan51f06982010-09-18 19:00:12 +0000398 } elseif {$n>=2 && [string equal -length $n $s "-count"]} {
399 set countonly 1
400 } else {
401 error "unknown switch: $s"
402 }
403 }
404
405 if {$countonly && $errfmt!=""} {
406 error "Cannot use -count and -errorformat together"
407 }
408 set nTestlist [llength $testlist]
409 if {$nTestlist%3 || $nTestlist==0 } {
410 error "SELECT test list contains [llength $testlist] elements"
411 }
412
danaf7626f2010-09-23 18:47:36 +0000413 eval $repair
dan51f06982010-09-18 19:00:12 +0000414 foreach {tn sql res} $testlist {
dana16d1062010-09-28 17:37:28 +0000415 if {$tclquery != ""} {
danaf1dcab2010-09-20 19:17:53 +0000416 execsql $sql
dana16d1062010-09-28 17:37:28 +0000417 uplevel do_test ${prefix}.$tn [list $tclquery] [list [list {*}$res]]
418 } elseif {$countonly} {
dan51f06982010-09-18 19:00:12 +0000419 set nRow 0
420 db eval $sql {incr nRow}
421 uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res]
422 } elseif {$errfmt==""} {
423 uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]]
424 } else {
425 set res [list 1 [string trim [format $errfmt {*}$res]]]
426 uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res]
427 }
danaf7626f2010-09-23 18:47:36 +0000428 eval $repair
dan51f06982010-09-18 19:00:12 +0000429 }
danaf7626f2010-09-23 18:47:36 +0000430
dan51f06982010-09-18 19:00:12 +0000431}
432
danb04757a2010-09-21 16:59:16 +0000433proc delete_all_data {} {
434 db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} {
435 db eval "DELETE FROM '[string map {' ''} $t]'"
436 }
437}
drh348784e2000-05-29 20:41:49 +0000438
drhb62c3352006-11-23 09:39:16 +0000439# Run an SQL script.
440# Return the number of microseconds per statement.
441#
drh3590f152006-11-23 21:09:10 +0000442proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000443 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000444 flush stdout
445 set speed [time {sqlite3_exec_nr db $sql}]
446 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000447 if {$tm == 0} {
448 set rate [format %20s "many"]
449 } else {
450 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
451 }
drh3590f152006-11-23 21:09:10 +0000452 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000453 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000454 global total_time
455 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000456 lappend ::speed_trial_times $name $tm
drhdd924312007-03-31 22:34:16 +0000457}
drh45c236d2008-03-22 01:08:00 +0000458proc speed_trial_tcl {name numstmt units script} {
459 puts -nonewline [format {%-21.21s } $name...]
460 flush stdout
461 set speed [time {eval $script}]
462 set tm [lindex $speed 0]
463 if {$tm == 0} {
464 set rate [format %20s "many"]
465 } else {
466 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
467 }
468 set u2 $units/s
469 puts [format {%12d uS %s %s} $tm $rate $u2]
470 global total_time
471 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000472 lappend ::speed_trial_times $name $tm
drh45c236d2008-03-22 01:08:00 +0000473}
drhdd924312007-03-31 22:34:16 +0000474proc speed_trial_init {name} {
475 global total_time
476 set total_time 0
dana975b592010-10-08 16:09:43 +0000477 set ::speed_trial_times [list]
drhbb810a92010-07-03 12:00:53 +0000478 sqlite3 versdb :memory:
479 set vers [versdb one {SELECT sqlite_source_id()}]
480 versdb close
481 puts "SQLite $vers"
drhdd924312007-03-31 22:34:16 +0000482}
483proc speed_trial_summary {name} {
484 global total_time
485 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
dana975b592010-10-08 16:09:43 +0000486
487 if { 0 } {
488 sqlite3 versdb :memory:
489 set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0]
490 versdb close
491 puts "CREATE TABLE IF NOT EXISTS time(version, script, test, us);"
492 foreach {test us} $::speed_trial_times {
493 puts "INSERT INTO time VALUES('$vers', '$name', '$test', $us);"
494 }
495 }
drhb62c3352006-11-23 09:39:16 +0000496}
497
drh348784e2000-05-29 20:41:49 +0000498# Run this routine last
499#
500proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +0000501 catch {db close}
502 catch {db2 close}
503 catch {db3 close}
504 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +0000505}
506proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +0000507 global sqlite_open_file_count
508
509 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +0000510
drh6e142f52000-06-08 13:36:40 +0000511 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000512 catch {db2 close}
513 catch {db3 close}
514
drh9bc54492007-10-23 14:49:59 +0000515 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000516 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000517 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000518 db close
drh984bfaa2008-03-19 16:08:53 +0000519 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +0000520
drh3a7fb7c2007-08-10 16:41:08 +0000521 sqlite3_soft_heap_limit 0
danc1a60c52010-06-07 14:28:16 +0000522 set nTest [incr_ntest]
523 set nErr [set_test_counter errors]
524
drh348784e2000-05-29 20:41:49 +0000525 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000526 if {$nErr>0} {
danc1a60c52010-06-07 14:28:16 +0000527 puts "Failures on these tests: [set_test_counter fail_list]"
drhf3a65f72007-08-22 20:18:21 +0000528 }
danielk1977ee8b7992009-03-26 17:13:06 +0000529 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +0000530 if {[llength $omitList]>0} {
531 puts "Omitted test cases:"
532 set prec {}
533 foreach {rec} [lsort $omitList] {
534 if {$rec==$prec} continue
535 set prec $rec
536 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
537 }
538 }
drh80788d82006-09-02 14:50:23 +0000539 if {$nErr>0 && ![working_64bit_int]} {
540 puts "******************************************************************"
541 puts "N.B.: The version of TCL that you used to build this test harness"
542 puts "is defective in that it does not support 64-bit integers. Some or"
543 puts "all of the test failures above might be a result from this defect"
544 puts "in your TCL build."
545 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000546 }
danc1a60c52010-06-07 14:28:16 +0000547 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000548 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +0000549 }
drh94e92032003-02-16 22:21:32 +0000550 if {$sqlite_open_file_count} {
551 puts "$sqlite_open_file_count files were left open"
552 incr nErr
553 }
drheafc43b2010-07-26 18:43:40 +0000554 if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 ||
555 [sqlite3_memory_used]>0} {
556 puts "Unfreed memory: [sqlite3_memory_used] bytes in\
557 [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations"
drhf3a65f72007-08-22 20:18:21 +0000558 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000559 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000560 puts "Writing unfreed memory log to \"./memleak.txt\""
561 sqlite3_memdebug_dump ./memleak.txt
562 }
drhf3a65f72007-08-22 20:18:21 +0000563 } else {
564 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000565 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000566 sqlite3_memdebug_dump ./memusage.txt
567 }
drhf3a65f72007-08-22 20:18:21 +0000568 }
drhf7141992008-06-19 00:16:08 +0000569 show_memstats
drh91fd4d42008-01-19 20:11:25 +0000570 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
571 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000572 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
573 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
574 }
danc1a60c52010-06-07 14:28:16 +0000575 if {$::cmdlinearg(malloctrace)} {
danielk197735754ac2008-03-21 17:29:37 +0000576 puts "Writing mallocs.sql..."
577 memdebug_log_sql
578 sqlite3_memdebug_log stop
579 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000580
581 if {[sqlite3_memory_used]>0} {
582 puts "Writing leaks.sql..."
583 sqlite3_memdebug_log sync
584 memdebug_log_sql leaks.sql
585 }
danielk197735754ac2008-03-21 17:29:37 +0000586 }
drhd9910fe2006-10-04 11:55:49 +0000587 foreach f [glob -nocomplain test.db-*-journal] {
588 file delete -force $f
589 }
590 foreach f [glob -nocomplain test.db-mj*] {
591 file delete -force $f
592 }
drhdb25e382001-03-15 18:21:22 +0000593 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000594}
595
drhf7141992008-06-19 00:16:08 +0000596# Display memory statistics for analysis and debugging purposes.
597#
598proc show_memstats {} {
599 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +0000600 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
601 set val [format {now %10d max %10d max-size %10d} \
602 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000603 puts "Memory used: $val"
drheafc43b2010-07-26 18:43:40 +0000604 set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0]
605 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
606 puts "Allocation count: $val"
drhf7141992008-06-19 00:16:08 +0000607 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +0000608 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
609 set val [format {now %10d max %10d max-size %10d} \
610 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000611 puts "Page-cache used: $val"
612 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
613 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
614 puts "Page-cache overflow: $val"
615 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
616 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
617 puts "Scratch memory used: $val"
618 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +0000619 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
620 set val [format {now %10d max %10d max-size %10d} \
621 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000622 puts "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +0000623 ifcapable yytrackmaxstackdepth {
624 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
625 set val [format { max %10d} [lindex $x 2]]
626 puts "Parser stack depth: $val"
627 }
drhf7141992008-06-19 00:16:08 +0000628}
629
drh348784e2000-05-29 20:41:49 +0000630# A procedure to execute SQL
631#
drhc4a3c772001-04-04 11:48:57 +0000632proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000633 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000634 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000635}
drh3aadb2e2000-05-31 17:59:25 +0000636
drhadbca9c2001-09-27 15:11:53 +0000637# Execute SQL and catch exceptions.
638#
639proc catchsql {sql {db db}} {
640 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +0000641 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +0000642 lappend r $msg
643 return $r
644}
645
drh04096482001-11-09 22:41:44 +0000646# Do an VDBE code dump on the SQL given
647#
648proc explain {sql {db db}} {
649 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000650 puts "addr opcode p1 p2 p3 p4 p5 #"
651 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000652 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000653 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
654 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
655 ]
drh04096482001-11-09 22:41:44 +0000656 }
657}
658
drhdafc0ce2008-04-17 19:14:02 +0000659# Show the VDBE program for an SQL statement but omit the Trace
660# opcode at the beginning. This procedure can be used to prove
661# that different SQL statements generate exactly the same VDBE code.
662#
663proc explain_no_trace {sql} {
664 set tr [db eval "EXPLAIN $sql"]
665 return [lrange $tr 7 end]
666}
667
drh3aadb2e2000-05-31 17:59:25 +0000668# Another procedure to execute SQL. This one includes the field
669# names in the returned list.
670#
671proc execsql2 {sql} {
672 set result {}
673 db eval $sql data {
674 foreach f $data(*) {
675 lappend result $f $data($f)
676 }
677 }
678 return $result
679}
drh17a68932001-01-31 13:28:08 +0000680
drhdde85d92003-03-01 19:45:34 +0000681# Use the non-callback API to execute multiple SQL statements
682#
683proc stepsql {dbptr sql} {
684 set sql [string trim $sql]
685 set r 0
686 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000687 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000688 return [list 1 $vm]
689 }
690 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000691# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
692# foreach v $VAL {lappend r $v}
693# }
694 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
695 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
696 lappend r [sqlite3_column_text $vm $i]
697 }
drhdde85d92003-03-01 19:45:34 +0000698 }
danielk1977106bb232004-05-21 10:08:53 +0000699 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000700 return [list 1 $errmsg]
701 }
702 }
703 return $r
704}
705
drh17a68932001-01-31 13:28:08 +0000706# Delete a file or directory
707#
dana16d1062010-09-28 17:37:28 +0000708proc forcedelete {args} {
709 foreach filename $args {
710 # On windows, sometimes even a [file delete -force] can fail just after
711 # a file is closed. The cause is usually "tag-alongs" - programs like
712 # anti-virus software, automatic backup tools and various explorer
713 # extensions that keep a file open a little longer than we expect, causing
714 # the delete to fail.
715 #
716 # The solution is to wait a short amount of time before retrying the
717 # delete.
718 #
719 set nRetry 50 ;# Maximum number of retries.
720 set nDelay 100 ;# Delay in ms before retrying.
721 for {set i 0} {$i<$nRetry} {incr i} {
722 set rc [catch {file delete -force $filename} msg]
723 if {$rc==0} break
724 after $nDelay
725 }
726 if {$rc} { error $msg }
drh17a68932001-01-31 13:28:08 +0000727 }
728}
drh21504322002-06-25 13:16:02 +0000729
730# Do an integrity check of the entire database
731#
danielk197704103022009-02-03 16:51:24 +0000732proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +0000733 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +0000734 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +0000735 }
736}
737
danielk1977db4e8862008-01-16 08:24:46 +0000738proc fix_ifcapable_expr {expr} {
739 set ret ""
740 set state 0
741 for {set i 0} {$i < [string length $expr]} {incr i} {
742 set char [string range $expr $i $i]
743 set newstate [expr {[string is alnum $char] || $char eq "_"}]
744 if {$newstate && !$state} {
745 append ret {$::sqlite_options(}
746 }
747 if {!$newstate && $state} {
748 append ret )
749 }
750 append ret $char
751 set state $newstate
752 }
753 if {$state} {append ret )}
754 return $ret
755}
756
drh27d258a2004-10-30 20:23:09 +0000757# Evaluate a boolean expression of capabilities. If true, execute the
758# code. Omit the code if false.
759#
danielk19773e8c37e2005-01-21 03:12:14 +0000760proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +0000761 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
762 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +0000763 if ($e2) {
764 set c [catch {uplevel 1 $code} r]
765 } else {
766 set c [catch {uplevel 1 $elsecode} r]
767 }
768 return -code $c $r
drh21504322002-06-25 13:16:02 +0000769}
danielk197745901d62004-11-10 15:27:38 +0000770
danielk1977aca790a2005-01-13 11:07:52 +0000771# This proc execs a seperate process that crashes midway through executing
772# the SQL script $sql on database test.db.
773#
774# The crash occurs during a sync() of file $crashfile. When the crash
775# occurs a random subset of all unsynced writes made by the process are
776# written into the files on disk. Argument $crashdelay indicates the
777# number of file syncs to wait before crashing.
778#
779# The return value is a list of two elements. The first element is a
780# boolean, indicating whether or not the process actually crashed or
781# reported some other error. The second element in the returned list is the
782# error message. This is "child process exited abnormally" if the crash
783# occured.
784#
danielk1977f8940ae2007-08-23 11:07:10 +0000785# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +0000786#
787proc crashsql {args} {
danielk197759a33f92007-03-17 10:26:59 +0000788
789 set blocksize ""
790 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +0000791 set prngseed 0
drhf8587402008-01-08 16:03:49 +0000792 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +0000793 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +0000794 set dc ""
danielk197759a33f92007-03-17 10:26:59 +0000795 set sql [lindex $args end]
796
797 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
798 set z [lindex $args $ii]
799 set n [string length $z]
800 set z2 [lindex $args [expr $ii+1]]
801
802 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +0000803 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +0000804 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +0000805 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000806 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +0000807 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +0000808 else { error "Unrecognized option: $z" }
809 }
810
811 if {$crashfile eq ""} {
812 error "Compulsory option -file missing"
813 }
814
shanehf2c08822010-07-08 16:30:44 +0000815 # $crashfile gets compared to the native filename in
816 # cfSync(), which can be different then what TCL uses by
817 # default, so here we force it to the "nativename" format.
818 set cfile [string map {\\ \\\\} [file nativename [file join [pwd] $crashfile]]]
danielk1977aca790a2005-01-13 11:07:52 +0000819
820 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +0000821 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +0000822 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +0000823 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +0000824 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +0000825
826 # This block sets the cache size of the main database to 10
827 # pages. This is done in case the build is configured to omit
828 # "PRAGMA cache_size".
829 puts $f {db eval {SELECT * FROM sqlite_master;}}
830 puts $f {set bt [btree_from_db db]}
831 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +0000832 if {$prngseed} {
833 set seed [expr {$prngseed%10007+1}]
834 # puts seed=$seed
835 puts $f "db eval {SELECT randomblob($seed)}"
836 }
danielk1977933bbd62007-03-17 07:22:42 +0000837
drhf8587402008-01-08 16:03:49 +0000838 if {[string length $tclbody]>0} {
839 puts $f $tclbody
840 }
841 if {[string length $sql]>0} {
842 puts $f "db eval {"
843 puts $f "$sql"
844 puts $f "}"
845 }
danielk1977aca790a2005-01-13 11:07:52 +0000846 close $f
danielk1977aca790a2005-01-13 11:07:52 +0000847 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000848 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000849 } msg]
shanehf2c08822010-07-08 16:30:44 +0000850
851 # Windows/ActiveState TCL returns a slightly different
852 # error message. We map that to the expected message
853 # so that we don't have to change all of the test
854 # cases.
855 if {$::tcl_platform(platform)=="windows"} {
856 if {$msg=="child killed: unknown signal"} {
857 set msg "child process exited abnormally"
858 }
859 }
860
danielk1977aca790a2005-01-13 11:07:52 +0000861 lappend r $msg
862}
863
danielk197732554c12005-01-22 03:39:39 +0000864# Usage: do_ioerr_test <test number> <options...>
865#
866# This proc is used to implement test cases that check that IO errors
867# are correctly handled. The first argument, <test number>, is an integer
868# used to name the tests executed by this proc. Options are as follows:
869#
870# -tclprep TCL script to run to prepare test.
871# -sqlprep SQL script to run to prepare test.
872# -tclbody TCL script to run with IO error simulation.
873# -sqlbody TCL script to run with IO error simulation.
874# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000875# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +0000876# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +0000877# -start Value of 'N' to begin with (default 1)
878#
879# -cksum Boolean. If true, test that the database does
880# not change during the execution of the test case.
881#
882proc do_ioerr_test {testname args} {
883
danielk197732554c12005-01-22 03:39:39 +0000884 set ::ioerropts(-start) 1
885 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000886 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +0000887 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +0000888 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +0000889 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +0000890 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +0000891 array set ::ioerropts $args
892
danielk197747cd39c2008-05-12 12:41:15 +0000893 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
894 # a couple of obscure IO errors that do not return them.
895 set ::ioerropts(-erc) 0
896
danielk197732554c12005-01-22 03:39:39 +0000897 set ::go 1
drh93aed5a2008-01-16 17:46:38 +0000898 #reset_prng_state
899 save_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +0000900 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +0000901 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +0000902 incr ::ioerropts(-count) -1
903 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +0000904
905 # Skip this IO error if it was specified with the "-exclude" option.
906 if {[info exists ::ioerropts(-exclude)]} {
907 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
908 }
danielk1977861f7452008-06-05 11:39:11 +0000909 if {$::ioerropts(-restoreprng)} {
910 restore_prng_state
911 }
danielk197732554c12005-01-22 03:39:39 +0000912
913 # Delete the files test.db and test2.db, then execute the TCL and
914 # SQL (in that order) to prepare for the test case.
915 do_test $testname.$n.1 {
916 set ::sqlite_io_error_pending 0
917 catch {db close}
aswiftaebf4132008-11-21 00:10:35 +0000918 catch {db2 close}
danielk197732554c12005-01-22 03:39:39 +0000919 catch {file delete -force test.db}
920 catch {file delete -force test.db-journal}
921 catch {file delete -force test2.db}
922 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000923 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000924 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000925 if {[info exists ::ioerropts(-tclprep)]} {
926 eval $::ioerropts(-tclprep)
927 }
928 if {[info exists ::ioerropts(-sqlprep)]} {
929 execsql $::ioerropts(-sqlprep)
930 }
931 expr 0
932 } {0}
933
934 # Read the 'checksum' of the database.
935 if {$::ioerropts(-cksum)} {
936 set checksum [cksum]
937 }
drh93aed5a2008-01-16 17:46:38 +0000938
danielk197732554c12005-01-22 03:39:39 +0000939 # Set the Nth IO error to fail.
940 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +0000941 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +0000942 set ::sqlite_io_error_pending $n
943 }] $n
944
945 # Create a single TCL script from the TCL and SQL specified
946 # as the body of the test.
947 set ::ioerrorbody {}
948 if {[info exists ::ioerropts(-tclbody)]} {
949 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
950 }
951 if {[info exists ::ioerropts(-sqlbody)]} {
952 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
953 }
954
955 # Execute the TCL Script created in the above block. If
956 # there are at least N IO operations performed by SQLite as
957 # a result of the script, the Nth will fail.
958 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +0000959 set ::sqlite_io_error_hit 0
960 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +0000961 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +0000962 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +0000963 set rc [sqlite3_errcode $::DB]
964 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +0000965 # If we are in extended result code mode, make sure all of the
966 # IOERRs we get back really do have their extended code values.
967 # If an extended result code is returned, the sqlite3_errcode
968 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
969 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +0000970 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
971 return $rc
972 }
973 } else {
drh5f7b5bf2007-04-19 12:30:54 +0000974 # If we are not in extended result code mode, make sure no
975 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +0000976 if {[regexp {\+\d} $rc]} {
977 return $rc
978 }
979 }
drh93aed5a2008-01-16 17:46:38 +0000980 # The test repeats as long as $::go is non-zero. $::go starts out
981 # as 1. When a test runs to completion without hitting an I/O
982 # error, that means there is no point in continuing with this test
983 # case so set $::go to zero.
984 #
985 if {$::sqlite_io_error_pending>0} {
986 set ::go 0
987 set q 0
988 set ::sqlite_io_error_pending 0
989 } else {
990 set q 1
991 }
992
drhc9ac5ca2005-11-04 22:03:30 +0000993 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +0000994 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
995 set r 1
996 }
danielk1977f2fa8312006-01-24 13:09:33 +0000997 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +0000998
999 # One of two things must have happened. either
1000 # 1. We never hit the IO error and the SQL returned OK
1001 # 2. An IO error was hit and the SQL failed
1002 #
dand41a29a2010-05-06 15:56:28 +00001003 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +00001004 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +00001005 } {1}
1006
danielk197780daec62008-05-12 10:57:02 +00001007 set ::sqlite_io_error_hit 0
1008 set ::sqlite_io_error_pending 0
1009
danielk197752b472a2008-05-05 16:23:55 +00001010 # Check that no page references were leaked. There should be
1011 # a single reference if there is still an active transaction,
1012 # or zero otherwise.
1013 #
danielk197781620542008-06-07 05:19:37 +00001014 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
1015 # locks are established on database files (i.e. if the error
1016 # occurs while attempting to detect a hot-journal file), then
1017 # there may 0 page references and an active transaction according
1018 # to [sqlite3_get_autocommit].
1019 #
danielk19774abd5442008-05-05 15:26:50 +00001020 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +00001021 do_test $testname.$n.4 {
1022 set bt [btree_from_db db]
1023 db_enter db
1024 array set stats [btree_pager_stats $bt]
1025 db_leave db
danielk197781620542008-06-07 05:19:37 +00001026 set nRef $stats(ref)
1027 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
1028 } {1}
danielk19774abd5442008-05-05 15:26:50 +00001029 }
1030
danielk197752b472a2008-05-05 16:23:55 +00001031 # If there is an open database handle and no open transaction,
1032 # and the pager is not running in exclusive-locking mode,
1033 # check that the pager is in "unlocked" state. Theoretically,
1034 # if a call to xUnlock() failed due to an IO error the underlying
1035 # file may still be locked.
1036 #
1037 ifcapable pragma {
1038 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +00001039 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +00001040 && [db one {pragma locking_mode}] eq "normal"
1041 && [sqlite3_get_autocommit db]
1042 } {
1043 do_test $testname.$n.5 {
1044 set bt [btree_from_db db]
1045 db_enter db
1046 array set stats [btree_pager_stats $bt]
1047 db_leave db
1048 set stats(state)
1049 } 0
1050 }
1051 }
1052
danielk197732554c12005-01-22 03:39:39 +00001053 # If an IO error occured, then the checksum of the database should
1054 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +00001055 #
drh1aa5af12008-03-07 19:51:14 +00001056 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +00001057 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +00001058 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +00001059 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +00001060 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +00001061 cksum
1062 } $checksum
1063 }
1064
drh1aa5af12008-03-07 19:51:14 +00001065 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +00001066 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +00001067 if {[info exists ::ioerropts(-cleanup)]} {
1068 catch $::ioerropts(-cleanup)
1069 }
danielk197732554c12005-01-22 03:39:39 +00001070 }
1071 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +00001072 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +00001073 unset ::ioerropts
1074}
1075
drh93aed5a2008-01-16 17:46:38 +00001076# Return a checksum based on the contents of the main database associated
1077# with connection $db
danielk197732554c12005-01-22 03:39:39 +00001078#
1079proc cksum {{db db}} {
1080 set txt [$db eval {
1081 SELECT name, type, sql FROM sqlite_master order by name
1082 }]\n
1083 foreach tbl [$db eval {
1084 SELECT name FROM sqlite_master WHERE type='table' order by name
1085 }] {
1086 append txt [$db eval "SELECT * FROM $tbl"]\n
1087 }
1088 foreach prag {default_synchronous default_cache_size} {
1089 append txt $prag-[$db eval "PRAGMA $prag"]\n
1090 }
1091 set cksum [string length $txt]-[md5 $txt]
1092 # puts $cksum-[file size test.db]
1093 return $cksum
1094}
1095
drh93aed5a2008-01-16 17:46:38 +00001096# Generate a checksum based on the contents of the main and temp tables
1097# database $db. If the checksum of two databases is the same, and the
1098# integrity-check passes for both, the two databases are identical.
1099#
drh1db639c2008-01-17 02:36:28 +00001100proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +00001101 set ret [list]
1102 ifcapable tempdb {
1103 set sql {
1104 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1105 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
1106 SELECT 'sqlite_master' UNION
1107 SELECT 'sqlite_temp_master' ORDER BY 1
1108 }
1109 } else {
1110 set sql {
1111 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1112 SELECT 'sqlite_master' ORDER BY 1
1113 }
1114 }
1115 set tbllist [$db eval $sql]
1116 set txt {}
1117 foreach tbl $tbllist {
1118 append txt [$db eval "SELECT * FROM $tbl"]
1119 }
1120 foreach prag {default_cache_size} {
1121 append txt $prag-[$db eval "PRAGMA $prag"]\n
1122 }
1123 # puts txt=$txt
1124 return [md5 $txt]
1125}
1126
drhdc2c4912009-02-04 22:46:47 +00001127# Generate a checksum based on the contents of a single database with
1128# a database connection. The name of the database is $dbname.
1129# Examples of $dbname are "temp" or "main".
1130#
1131proc dbcksum {db dbname} {
1132 if {$dbname=="temp"} {
1133 set master sqlite_temp_master
1134 } else {
1135 set master $dbname.sqlite_master
1136 }
1137 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
1138 set txt [$db eval "SELECT * FROM $master"]\n
1139 foreach tab $alltab {
1140 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
1141 }
1142 return [md5 $txt]
1143}
1144
danielk197735754ac2008-03-21 17:29:37 +00001145proc memdebug_log_sql {{filename mallocs.sql}} {
1146
danielk19776f332c12008-03-21 14:22:44 +00001147 set data [sqlite3_memdebug_log dump]
1148 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +00001149 if {$nFrame < 0} { return "" }
1150
danielk197735754ac2008-03-21 17:29:37 +00001151 set database temp
1152
danielk19776ab3a2e2009-02-19 14:39:25 +00001153 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +00001154
1155 set sql ""
1156 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +00001157 set nCall [lindex $e 0]
1158 set nByte [lindex $e 1]
1159 set lStack [lrange $e 2 end]
1160 append sql "INSERT INTO ${database}.malloc VALUES"
1161 append sql "('test', $nCall, $nByte, '$lStack');\n"
1162 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +00001163 set frames($f) 1
1164 }
1165 }
1166
1167 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +00001168 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +00001169
1170 foreach f [array names frames] {
1171 set addr [format %x $f]
1172 set cmd "addr2line -e [info nameofexec] $addr"
1173 set line [eval exec $cmd]
1174 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00001175
1176 set file [lindex [split $line :] 0]
1177 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00001178 }
1179
danielk197735754ac2008-03-21 17:29:37 +00001180 foreach f [array names files] {
1181 set contents ""
1182 catch {
1183 set fd [open $f]
1184 set contents [read $fd]
1185 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001186 }
danielk197735754ac2008-03-21 17:29:37 +00001187 set contents [string map {' ''} $contents]
1188 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00001189 }
danielk19776f332c12008-03-21 14:22:44 +00001190
danielk197735754ac2008-03-21 17:29:37 +00001191 set fd [open $filename w]
1192 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
1193 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001194}
drh93aed5a2008-01-16 17:46:38 +00001195
danielk197732554c12005-01-22 03:39:39 +00001196# Copy file $from into $to. This is used because some versions of
1197# TCL for windows (notably the 8.4.1 binary package shipped with the
1198# current mingw release) have a broken "file copy" command.
1199#
1200proc copy_file {from to} {
1201 if {$::tcl_platform(platform)=="unix"} {
1202 file copy -force $from $to
1203 } else {
1204 set f [open $from]
1205 fconfigure $f -translation binary
1206 set t [open $to w]
1207 fconfigure $t -translation binary
1208 puts -nonewline $t [read $f [file size $from]]
1209 close $t
1210 close $f
1211 }
1212}
1213
danf5894502009-10-07 18:41:19 +00001214# Drop all tables in database [db]
1215proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00001216 ifcapable trigger&&foreignkey {
1217 set pk [$db one "PRAGMA foreign_keys"]
1218 $db eval "PRAGMA foreign_keys = OFF"
1219 }
drh9a6ffc82010-02-15 18:03:20 +00001220 foreach {idx name file} [db eval {PRAGMA database_list}] {
1221 if {$idx==1} {
1222 set master sqlite_temp_master
1223 } else {
1224 set master $name.sqlite_master
1225 }
1226 foreach {t type} [$db eval "
1227 SELECT name, type FROM $master
dana16d1062010-09-28 17:37:28 +00001228 WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X'
drh9a6ffc82010-02-15 18:03:20 +00001229 "] {
dana16d1062010-09-28 17:37:28 +00001230 $db eval "DROP $type \"$t\""
drh9a6ffc82010-02-15 18:03:20 +00001231 }
danf5894502009-10-07 18:41:19 +00001232 }
shaneh4e7b32f2009-12-17 22:12:51 +00001233 ifcapable trigger&&foreignkey {
1234 $db eval "PRAGMA foreign_keys = $pk"
1235 }
danf5894502009-10-07 18:41:19 +00001236}
1237
dan71cb5182010-04-26 12:39:03 +00001238#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00001239# If a test script is executed with global variable $::G(perm:name) set to
1240# "wal", then the tests are run in WAL mode. Otherwise, they should be run
1241# in rollback mode. The following Tcl procs are used to make this less
1242# intrusive:
dan71cb5182010-04-26 12:39:03 +00001243#
1244# wal_set_journal_mode ?DB?
1245#
1246# If running a WAL test, execute "PRAGMA journal_mode = wal" using
1247# connection handle DB. Otherwise, this command is a no-op.
1248#
1249# wal_check_journal_mode TESTNAME ?DB?
1250#
1251# If running a WAL test, execute a tests case that fails if the main
1252# database for connection handle DB is not currently a WAL database.
1253# Otherwise (if not running a WAL permutation) this is a no-op.
1254#
1255# wal_is_wal_mode
1256#
1257# Returns true if this test should be run in WAL mode. False otherwise.
1258#
1259proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00001260 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00001261}
1262proc wal_set_journal_mode {{db db}} {
1263 if { [wal_is_wal_mode] } {
1264 $db eval "PRAGMA journal_mode = WAL"
1265 }
1266}
1267proc wal_check_journal_mode {testname {db db}} {
1268 if { [wal_is_wal_mode] } {
1269 $db eval { SELECT * FROM sqlite_master }
1270 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
1271 }
1272}
1273
dan430e74c2010-06-07 17:47:26 +00001274proc permutation {} {
1275 set perm ""
1276 catch {set perm $::G(perm:name)}
1277 set perm
1278}
dancb79e512010-08-06 13:50:07 +00001279proc presql {} {
1280 set presql ""
1281 catch {set presql $::G(perm:presql)}
1282 set presql
1283}
dan430e74c2010-06-07 17:47:26 +00001284
danc1a60c52010-06-07 14:28:16 +00001285#-------------------------------------------------------------------------
1286#
1287proc slave_test_script {script} {
1288
1289 # Create the interpreter used to run the test script.
1290 interp create tinterp
1291
1292 # Populate some global variables that tester.tcl expects to see.
1293 foreach {var value} [list \
1294 ::argv0 $::argv0 \
1295 ::argv {} \
1296 ::SLAVE 1 \
1297 ] {
1298 interp eval tinterp [list set $var $value]
1299 }
1300
1301 # The alias used to access the global test counters.
1302 tinterp alias set_test_counter set_test_counter
1303
1304 # Set up the ::cmdlinearg array in the slave.
1305 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
1306
dan430e74c2010-06-07 17:47:26 +00001307 # Set up the ::G array in the slave.
1308 interp eval tinterp [list array set ::G [array get ::G]]
1309
danc1a60c52010-06-07 14:28:16 +00001310 # Load the various test interfaces implemented in C.
1311 load_testfixture_extensions tinterp
1312
1313 # Run the test script.
1314 interp eval tinterp $script
1315
danea5542d2010-07-06 11:26:15 +00001316 # Check if the interpreter call [run_thread_tests]
1317 if { [interp eval tinterp {info exists ::run_thread_tests_called}] } {
1318 set ::run_thread_tests_called 1
1319 }
1320
danc1a60c52010-06-07 14:28:16 +00001321 # Delete the interpreter used to run the test script.
1322 interp delete tinterp
1323}
1324
dan430e74c2010-06-07 17:47:26 +00001325proc slave_test_file {zFile} {
dan0626dfc2010-06-15 06:56:37 +00001326 set tail [file tail $zFile]
dan430e74c2010-06-07 17:47:26 +00001327
dan92d516a2010-07-05 14:54:48 +00001328 # Remember the value of the shared-cache setting. So that it is possible
1329 # to check afterwards that it was not modified by the test script.
1330 #
1331 ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] }
dan0626dfc2010-06-15 06:56:37 +00001332
dan92d516a2010-07-05 14:54:48 +00001333 # Run the test script in a slave interpreter.
1334 #
danea5542d2010-07-06 11:26:15 +00001335 unset -nocomplain ::run_thread_tests_called
dan0626dfc2010-06-15 06:56:37 +00001336 reset_prng_state
1337 set ::sqlite_open_file_count 0
1338 set time [time { slave_test_script [list source $zFile] }]
1339 set ms [expr [lindex $time 0] / 1000]
1340
dan92d516a2010-07-05 14:54:48 +00001341 # Test that all files opened by the test script were closed. Omit this
1342 # if the test script has "thread" in its name. The open file counter
1343 # is not thread-safe.
dan0626dfc2010-06-15 06:56:37 +00001344 #
danea5542d2010-07-06 11:26:15 +00001345 if {[info exists ::run_thread_tests_called]==0} {
dan92d516a2010-07-05 14:54:48 +00001346 do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0}
1347 }
dan430e74c2010-06-07 17:47:26 +00001348 set ::sqlite_open_file_count 0
1349
dan0626dfc2010-06-15 06:56:37 +00001350 # Test that the global "shared-cache" setting was not altered by
1351 # the test script.
1352 #
1353 ifcapable shared_cache {
1354 set res [expr {[sqlite3_enable_shared_cache] == $scs}]
1355 do_test ${tail}-sharedcachesetting [list set {} $res] 1
1356 }
dan430e74c2010-06-07 17:47:26 +00001357
dan92d516a2010-07-05 14:54:48 +00001358 # Add some info to the output.
1359 #
dan0626dfc2010-06-15 06:56:37 +00001360 puts "Time: $tail $ms ms"
dan430e74c2010-06-07 17:47:26 +00001361 show_memstats
danc1a60c52010-06-07 14:28:16 +00001362}
1363
dan59257dc2010-08-04 11:34:31 +00001364# Open a new connection on database test.db and execute the SQL script
1365# supplied as an argument. Before returning, close the new conection and
1366# restore the 4 byte fields starting at header offsets 28, 92 and 96
1367# to the values they held before the SQL was executed. This simulates
1368# a write by a pre-3.7.0 client.
1369#
1370proc sql36231 {sql} {
1371 set B [hexio_read test.db 92 8]
1372 set A [hexio_read test.db 28 4]
1373 sqlite3 db36231 test.db
1374 catch { db36231 func a_string a_string }
1375 execsql $sql db36231
1376 db36231 close
1377 hexio_write test.db 28 $A
1378 hexio_write test.db 92 $B
1379 return ""
1380}
danf5894502009-10-07 18:41:19 +00001381
danccc7ff42010-11-29 12:06:45 +00001382proc db_save {} {
1383 foreach f [glob -nocomplain sv_test.db*] { forcedelete $f }
1384 foreach f [glob -nocomplain test.db*] {
1385 set f2 "sv_$f"
1386 file copy -force $f $f2
1387 }
1388}
1389proc db_save_and_close {} {
1390 db_save
1391 catch { db close }
1392 return ""
1393}
1394proc db_restore {} {
1395 foreach f [glob -nocomplain test.db*] { forcedelete $f }
1396 foreach f2 [glob -nocomplain sv_test.db*] {
1397 set f [string range $f2 3 end]
1398 file copy -force $f2 $f
1399 }
1400}
1401proc db_restore_and_reopen {{dbfile test.db}} {
1402 catch { db close }
1403 db_restore
1404 sqlite3 db $dbfile
1405}
1406proc db_delete_and_reopen {{file test.db}} {
1407 catch { db close }
1408 foreach f [glob -nocomplain test.db*] { file delete -force $f }
1409 sqlite3 db $file
1410}
1411
danielk197745901d62004-11-10 15:27:38 +00001412# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
1413# to non-zero, then set the global variable $AUTOVACUUM to 1.
1414set AUTOVACUUM $sqlite_options(default_autovacuum)
danielk1977ee8b7992009-03-26 17:13:06 +00001415
1416source $testdir/thread_common.tcl
danddf80eb2010-10-25 12:47:43 +00001417source $testdir/malloc_common.tcl