blob: e4193b48f888e217f915482f549d092e97c533d3 [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
danc1a60c52010-06-07 14:28:16 +000073#
drh348784e2000-05-29 20:41:49 +000074
danc1a60c52010-06-07 14:28:16 +000075# Set the precision of FP arithmatic used by the interpreter. And
76# configure SQLite to take database file locks on the page that begins
77# 64KB into the database file instead of the one 1GB in. This means
78# the code that handles that special case can be tested without creating
79# very large database files.
80#
drh92febd92004-08-20 18:34:20 +000081set tcl_precision 15
drhc7a3bb92009-02-05 16:31:45 +000082sqlite3_test_control_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +000083
danc1a60c52010-06-07 14:28:16 +000084
85# If the pager codec is available, create a wrapper for the [sqlite3]
86# command that appends "-key {xyzzy}" to the command line. i.e. this:
drh3a7fb7c2007-08-10 16:41:08 +000087#
danc1a60c52010-06-07 14:28:16 +000088# sqlite3 db test.db
drh4a50aac2007-08-23 02:47:53 +000089#
danc1a60c52010-06-07 14:28:16 +000090# becomes
drh4a50aac2007-08-23 02:47:53 +000091#
danc1a60c52010-06-07 14:28:16 +000092# sqlite3 db test.db -key {xyzzy}
drh9eb9e262004-02-11 02:18:05 +000093#
dan430e74c2010-06-07 17:47:26 +000094if {[info command sqlite_orig]==""} {
drhef4ac8f2004-06-19 00:16:31 +000095 rename sqlite3 sqlite_orig
96 proc sqlite3 {args} {
dan68928b62010-06-22 13:46:43 +000097 if {[llength $args]>=2 && [string index [lindex $args 0] 0]!="-"} {
dan430e74c2010-06-07 17:47:26 +000098 # This command is opening a new database connection.
99 #
100 if {[info exists ::G(perm:sqlite3_args)]} {
101 set args [concat $args $::G(perm:sqlite3_args)]
102 }
dan68928b62010-06-22 13:46:43 +0000103 if {[sqlite_orig -has-codec] && ![info exists ::do_not_use_codec]} {
dan430e74c2010-06-07 17:47:26 +0000104 lappend args -key {xyzzy}
105 }
106
dan3ccd20a2010-06-09 19:01:02 +0000107 set res [uplevel 1 sqlite_orig $args]
dan430e74c2010-06-07 17:47:26 +0000108 if {[info exists ::G(perm:presql)]} {
109 [lindex $args 0] eval $::G(perm:presql)
110 }
dan3ccd20a2010-06-09 19:01:02 +0000111 set res
dan430e74c2010-06-07 17:47:26 +0000112 } else {
113 # This command is not opening a new database connection. Pass the
114 # arguments through to the C implemenation as the are.
115 #
116 uplevel 1 sqlite_orig $args
drh9eb9e262004-02-11 02:18:05 +0000117 }
drh9eb9e262004-02-11 02:18:05 +0000118 }
119}
120
dancb354602010-07-08 09:44:42 +0000121proc execpresql {handle args} {
122 trace remove execution $handle enter [list execpresql $handle]
123 if {[info exists ::G(perm:presql)]} {
124 $handle eval $::G(perm:presql)
125 }
126}
127
dan68928b62010-06-22 13:46:43 +0000128# This command should be called after loading tester.tcl from within
129# all test scripts that are incompatible with encryption codecs.
130#
131proc do_not_use_codec {} {
132 set ::do_not_use_codec 1
133 reset_db
134}
135
dan430e74c2010-06-07 17:47:26 +0000136# The following block only runs the first time this file is sourced. It
137# does not run in slave interpreters (since the ::cmdlinearg array is
138# populated before the test script is run in slave interpreters).
drhbec2bf42000-05-29 23:48:22 +0000139#
danc1a60c52010-06-07 14:28:16 +0000140if {[info exists cmdlinearg]==0} {
141
142 # Parse any options specified in the $argv array. This script accepts the
143 # following options:
144 #
145 # --pause
146 # --soft-heap-limit=NN
147 # --maxerror=NN
148 # --malloctrace=N
149 # --backtrace=N
150 # --binarylog=N
dan0626dfc2010-06-15 06:56:37 +0000151 # --soak=N
danc1a60c52010-06-07 14:28:16 +0000152 #
153 set cmdlinearg(soft-heap-limit) 0
154 set cmdlinearg(maxerror) 1000
155 set cmdlinearg(malloctrace) 0
156 set cmdlinearg(backtrace) 10
157 set cmdlinearg(binarylog) 0
dan0626dfc2010-06-15 06:56:37 +0000158 set cmdlinearg(soak) 0
danc1a60c52010-06-07 14:28:16 +0000159
160 set leftover [list]
161 foreach a $argv {
162 switch -regexp -- $a {
163 {^-+pause$} {
164 # Wait for user input before continuing. This is to give the user an
165 # opportunity to connect profiling tools to the process.
166 puts -nonewline "Press RETURN to begin..."
167 flush stdout
168 gets stdin
169 }
170 {^-+soft-heap-limit=.+$} {
171 foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break
172 }
173 {^-+maxerror=.+$} {
174 foreach {dummy cmdlinearg(maxerror)} [split $a =] break
175 }
176 {^-+malloctrace=.+$} {
177 foreach {dummy cmdlinearg(malloctrace)} [split $a =] break
178 if {$cmdlinearg(malloctrace)} {
179 sqlite3_memdebug_log start
180 }
181 }
182 {^-+backtrace=.+$} {
183 foreach {dummy cmdlinearg(backtrace)} [split $a =] break
dan0626dfc2010-06-15 06:56:37 +0000184 sqlite3_memdebug_backtrace $value
danc1a60c52010-06-07 14:28:16 +0000185 }
danc1a60c52010-06-07 14:28:16 +0000186 {^-+binarylog=.+$} {
187 foreach {dummy cmdlinearg(binarylog)} [split $a =] break
188 }
dan0626dfc2010-06-15 06:56:37 +0000189 {^-+soak=.+$} {
190 foreach {dummy cmdlinearg(soak)} [split $a =] break
191 set ::G(issoak) $cmdlinearg(soak)
192 }
danc1a60c52010-06-07 14:28:16 +0000193 default {
194 lappend leftover $a
195 }
196 }
197 }
198 set argv $leftover
199
dan430e74c2010-06-07 17:47:26 +0000200 # Install the malloc layer used to inject OOM errors. And the 'automatic'
201 # extensions. This only needs to be done once for the process.
202 #
danielk1977f7b9d662008-06-23 18:49:43 +0000203 sqlite3_shutdown
204 install_malloc_faultsim 1
205 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000206 autoinstall_test_functions
danc1a60c52010-06-07 14:28:16 +0000207
dan430e74c2010-06-07 17:47:26 +0000208 # If the --binarylog option was specified, create the logging VFS. This
209 # call installs the new VFS as the default for all SQLite connections.
210 #
danc1a60c52010-06-07 14:28:16 +0000211 if {$cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000212 vfslog new binarylog {} vfslog.bin
danc1a60c52010-06-07 14:28:16 +0000213 }
214
215 # Set the backtrace depth, if malloc tracing is enabled.
216 #
217 if {$cmdlinearg(malloctrace)} {
218 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danielk1977185eac92008-07-12 15:55:54 +0000219 }
danielk1977f7b9d662008-06-23 18:49:43 +0000220}
danielk197703ba3fa2009-01-09 10:49:14 +0000221
danc1a60c52010-06-07 14:28:16 +0000222# Update the soft-heap-limit each time this script is run. In that
223# way if an individual test file changes the soft-heap-limit, it
224# will be reset at the start of the next test file.
225#
226sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
227
228# Create a test database
229#
danielk197703ba3fa2009-01-09 10:49:14 +0000230proc reset_db {} {
231 catch {db close}
232 file delete -force test.db
233 file delete -force test.db-journal
dand3f8f942010-04-13 11:35:01 +0000234 file delete -force test.db-wal
danielk197703ba3fa2009-01-09 10:49:14 +0000235 sqlite3 db ./test.db
236 set ::DB [sqlite3_connection_pointer db]
237 if {[info exists ::SETUP_SQL]} {
238 db eval $::SETUP_SQL
239 }
drhcd61c282002-03-06 22:01:34 +0000240}
danielk197703ba3fa2009-01-09 10:49:14 +0000241reset_db
drhbec2bf42000-05-29 23:48:22 +0000242
243# Abort early if this script has been run before.
244#
danc1a60c52010-06-07 14:28:16 +0000245if {[info exists TC(count)]} return
drhbec2bf42000-05-29 23:48:22 +0000246
danc1a60c52010-06-07 14:28:16 +0000247# Initialize the test counters and set up commands to access them.
248# Or, if this is a slave interpreter, set up aliases to write the
249# counters in the parent interpreter.
drhbec2bf42000-05-29 23:48:22 +0000250#
danc1a60c52010-06-07 14:28:16 +0000251if {0==[info exists ::SLAVE]} {
252 set TC(errors) 0
253 set TC(count) 0
254 set TC(fail_list) [list]
255 set TC(omit_list) [list]
256
257 proc set_test_counter {counter args} {
258 if {[llength $args]} {
259 set ::TC($counter) [lindex $args 0]
260 }
261 set ::TC($counter)
262 }
drhb62c3352006-11-23 09:39:16 +0000263}
drh348784e2000-05-29 20:41:49 +0000264
drh521cc842008-04-15 02:36:33 +0000265# Record the fact that a sequence of tests were omitted.
266#
267proc omit_test {name reason} {
danc1a60c52010-06-07 14:28:16 +0000268 set omitList [set_test_counter omit_list]
drh521cc842008-04-15 02:36:33 +0000269 lappend omitList [list $name $reason]
danc1a60c52010-06-07 14:28:16 +0000270 set_test_counter omit_list $omitList
drh521cc842008-04-15 02:36:33 +0000271}
272
danc1a60c52010-06-07 14:28:16 +0000273# Record the fact that a test failed.
274#
275proc fail_test {name} {
276 set f [set_test_counter fail_list]
277 lappend f $name
278 set_test_counter fail_list $f
279 set_test_counter errors [expr [set_test_counter errors] + 1]
280
281 set nFail [set_test_counter errors]
282 if {$nFail>=$::cmdlinearg(maxerror)} {
283 puts "*** Giving up..."
284 finalize_testing
285 }
286}
287
288# Increment the number of tests run
289#
290proc incr_ntest {} {
291 set_test_counter count [expr [set_test_counter count] + 1]
292}
293
294
drh348784e2000-05-29 20:41:49 +0000295# Invoke the do_test procedure to run a single test
296#
297proc do_test {name cmd expected} {
danc1a60c52010-06-07 14:28:16 +0000298
299 global argv cmdlinearg
300
drh4a50aac2007-08-23 02:47:53 +0000301 sqlite3_memdebug_settitle $name
danc1a60c52010-06-07 14:28:16 +0000302
dan92d516a2010-07-05 14:54:48 +0000303# if {[llength $argv]==0} {
304# set go 1
305# } else {
306# set go 0
307# foreach pattern $argv {
308# if {[string match $pattern $name]} {
309# set go 1
310# break
311# }
312# }
313# }
dan430e74c2010-06-07 17:47:26 +0000314
dand506de02010-07-03 13:59:01 +0000315 if {[info exists ::G(perm:prefix)]} {
316 set name "$::G(perm:prefix)$name"
dan430e74c2010-06-07 17:47:26 +0000317 }
318
danc1a60c52010-06-07 14:28:16 +0000319 incr_ntest
drh5edc3122001-09-13 21:53:09 +0000320 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000321 flush stdout
322 if {[catch {uplevel #0 "$cmd;\n"} result]} {
323 puts "\nError: $result"
danc1a60c52010-06-07 14:28:16 +0000324 fail_test $name
drh348784e2000-05-29 20:41:49 +0000325 } elseif {[string compare $result $expected]} {
326 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
danc1a60c52010-06-07 14:28:16 +0000327 fail_test $name
drh348784e2000-05-29 20:41:49 +0000328 } else {
329 puts " Ok"
330 }
drh6558db82007-04-13 03:23:21 +0000331 flush stdout
drh348784e2000-05-29 20:41:49 +0000332}
dan153eda02010-06-21 07:45:47 +0000333
334proc do_execsql_test {testname sql result} {
335 uplevel do_test $testname [list "execsql {$sql}"] [list $result]
336}
337proc do_catchsql_test {testname sql result} {
338 uplevel do_test $testname [list "catchsql {$sql}"] [list $result]
339}
340
drh348784e2000-05-29 20:41:49 +0000341
drhb62c3352006-11-23 09:39:16 +0000342# Run an SQL script.
343# Return the number of microseconds per statement.
344#
drh3590f152006-11-23 21:09:10 +0000345proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000346 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000347 flush stdout
348 set speed [time {sqlite3_exec_nr db $sql}]
349 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000350 if {$tm == 0} {
351 set rate [format %20s "many"]
352 } else {
353 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
354 }
drh3590f152006-11-23 21:09:10 +0000355 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000356 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000357 global total_time
358 set total_time [expr {$total_time+$tm}]
359}
drh45c236d2008-03-22 01:08:00 +0000360proc speed_trial_tcl {name numstmt units script} {
361 puts -nonewline [format {%-21.21s } $name...]
362 flush stdout
363 set speed [time {eval $script}]
364 set tm [lindex $speed 0]
365 if {$tm == 0} {
366 set rate [format %20s "many"]
367 } else {
368 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
369 }
370 set u2 $units/s
371 puts [format {%12d uS %s %s} $tm $rate $u2]
372 global total_time
373 set total_time [expr {$total_time+$tm}]
374}
drhdd924312007-03-31 22:34:16 +0000375proc speed_trial_init {name} {
376 global total_time
377 set total_time 0
drhbb810a92010-07-03 12:00:53 +0000378 sqlite3 versdb :memory:
379 set vers [versdb one {SELECT sqlite_source_id()}]
380 versdb close
381 puts "SQLite $vers"
drhdd924312007-03-31 22:34:16 +0000382}
383proc speed_trial_summary {name} {
384 global total_time
385 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
drhb62c3352006-11-23 09:39:16 +0000386}
387
drh348784e2000-05-29 20:41:49 +0000388# Run this routine last
389#
390proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +0000391 catch {db close}
392 catch {db2 close}
393 catch {db3 close}
394 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +0000395}
396proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +0000397 global sqlite_open_file_count
398
399 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +0000400
drh6e142f52000-06-08 13:36:40 +0000401 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000402 catch {db2 close}
403 catch {db3 close}
404
drh9bc54492007-10-23 14:49:59 +0000405 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000406 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000407 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000408 db close
drh984bfaa2008-03-19 16:08:53 +0000409 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +0000410
drh3a7fb7c2007-08-10 16:41:08 +0000411 sqlite3_soft_heap_limit 0
danc1a60c52010-06-07 14:28:16 +0000412 set nTest [incr_ntest]
413 set nErr [set_test_counter errors]
414
drh348784e2000-05-29 20:41:49 +0000415 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000416 if {$nErr>0} {
danc1a60c52010-06-07 14:28:16 +0000417 puts "Failures on these tests: [set_test_counter fail_list]"
drhf3a65f72007-08-22 20:18:21 +0000418 }
danielk1977ee8b7992009-03-26 17:13:06 +0000419 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +0000420 if {[llength $omitList]>0} {
421 puts "Omitted test cases:"
422 set prec {}
423 foreach {rec} [lsort $omitList] {
424 if {$rec==$prec} continue
425 set prec $rec
426 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
427 }
428 }
drh80788d82006-09-02 14:50:23 +0000429 if {$nErr>0 && ![working_64bit_int]} {
430 puts "******************************************************************"
431 puts "N.B.: The version of TCL that you used to build this test harness"
432 puts "is defective in that it does not support 64-bit integers. Some or"
433 puts "all of the test failures above might be a result from this defect"
434 puts "in your TCL build."
435 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000436 }
danc1a60c52010-06-07 14:28:16 +0000437 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000438 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +0000439 }
drh94e92032003-02-16 22:21:32 +0000440 if {$sqlite_open_file_count} {
441 puts "$sqlite_open_file_count files were left open"
442 incr nErr
443 }
drheafc43b2010-07-26 18:43:40 +0000444 if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 ||
445 [sqlite3_memory_used]>0} {
446 puts "Unfreed memory: [sqlite3_memory_used] bytes in\
447 [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations"
drhf3a65f72007-08-22 20:18:21 +0000448 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000449 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000450 puts "Writing unfreed memory log to \"./memleak.txt\""
451 sqlite3_memdebug_dump ./memleak.txt
452 }
drhf3a65f72007-08-22 20:18:21 +0000453 } else {
454 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000455 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000456 sqlite3_memdebug_dump ./memusage.txt
457 }
drhf3a65f72007-08-22 20:18:21 +0000458 }
drhf7141992008-06-19 00:16:08 +0000459 show_memstats
drh91fd4d42008-01-19 20:11:25 +0000460 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
461 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000462 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
463 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
464 }
danc1a60c52010-06-07 14:28:16 +0000465 if {$::cmdlinearg(malloctrace)} {
danielk197735754ac2008-03-21 17:29:37 +0000466 puts "Writing mallocs.sql..."
467 memdebug_log_sql
468 sqlite3_memdebug_log stop
469 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000470
471 if {[sqlite3_memory_used]>0} {
472 puts "Writing leaks.sql..."
473 sqlite3_memdebug_log sync
474 memdebug_log_sql leaks.sql
475 }
danielk197735754ac2008-03-21 17:29:37 +0000476 }
drhd9910fe2006-10-04 11:55:49 +0000477 foreach f [glob -nocomplain test.db-*-journal] {
478 file delete -force $f
479 }
480 foreach f [glob -nocomplain test.db-mj*] {
481 file delete -force $f
482 }
drhdb25e382001-03-15 18:21:22 +0000483 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000484}
485
drhf7141992008-06-19 00:16:08 +0000486# Display memory statistics for analysis and debugging purposes.
487#
488proc show_memstats {} {
489 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +0000490 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
491 set val [format {now %10d max %10d max-size %10d} \
492 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000493 puts "Memory used: $val"
drheafc43b2010-07-26 18:43:40 +0000494 set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0]
495 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
496 puts "Allocation count: $val"
drhf7141992008-06-19 00:16:08 +0000497 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +0000498 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
499 set val [format {now %10d max %10d max-size %10d} \
500 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000501 puts "Page-cache used: $val"
502 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
503 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
504 puts "Page-cache overflow: $val"
505 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
506 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
507 puts "Scratch memory used: $val"
508 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +0000509 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
510 set val [format {now %10d max %10d max-size %10d} \
511 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000512 puts "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +0000513 ifcapable yytrackmaxstackdepth {
514 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
515 set val [format { max %10d} [lindex $x 2]]
516 puts "Parser stack depth: $val"
517 }
drhf7141992008-06-19 00:16:08 +0000518}
519
drh348784e2000-05-29 20:41:49 +0000520# A procedure to execute SQL
521#
drhc4a3c772001-04-04 11:48:57 +0000522proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000523 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000524 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000525}
drh3aadb2e2000-05-31 17:59:25 +0000526
drhadbca9c2001-09-27 15:11:53 +0000527# Execute SQL and catch exceptions.
528#
529proc catchsql {sql {db db}} {
530 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +0000531 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +0000532 lappend r $msg
533 return $r
534}
535
drh04096482001-11-09 22:41:44 +0000536# Do an VDBE code dump on the SQL given
537#
538proc explain {sql {db db}} {
539 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000540 puts "addr opcode p1 p2 p3 p4 p5 #"
541 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000542 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000543 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
544 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
545 ]
drh04096482001-11-09 22:41:44 +0000546 }
547}
548
drhdafc0ce2008-04-17 19:14:02 +0000549# Show the VDBE program for an SQL statement but omit the Trace
550# opcode at the beginning. This procedure can be used to prove
551# that different SQL statements generate exactly the same VDBE code.
552#
553proc explain_no_trace {sql} {
554 set tr [db eval "EXPLAIN $sql"]
555 return [lrange $tr 7 end]
556}
557
drh3aadb2e2000-05-31 17:59:25 +0000558# Another procedure to execute SQL. This one includes the field
559# names in the returned list.
560#
561proc execsql2 {sql} {
562 set result {}
563 db eval $sql data {
564 foreach f $data(*) {
565 lappend result $f $data($f)
566 }
567 }
568 return $result
569}
drh17a68932001-01-31 13:28:08 +0000570
drhdde85d92003-03-01 19:45:34 +0000571# Use the non-callback API to execute multiple SQL statements
572#
573proc stepsql {dbptr sql} {
574 set sql [string trim $sql]
575 set r 0
576 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000577 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000578 return [list 1 $vm]
579 }
580 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000581# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
582# foreach v $VAL {lappend r $v}
583# }
584 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
585 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
586 lappend r [sqlite3_column_text $vm $i]
587 }
drhdde85d92003-03-01 19:45:34 +0000588 }
danielk1977106bb232004-05-21 10:08:53 +0000589 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000590 return [list 1 $errmsg]
591 }
592 }
593 return $r
594}
595
drh17a68932001-01-31 13:28:08 +0000596# Delete a file or directory
597#
598proc forcedelete {filename} {
599 if {[catch {file delete -force $filename}]} {
600 exec rm -rf $filename
601 }
602}
drh21504322002-06-25 13:16:02 +0000603
604# Do an integrity check of the entire database
605#
danielk197704103022009-02-03 16:51:24 +0000606proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +0000607 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +0000608 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +0000609 }
610}
611
danielk1977db4e8862008-01-16 08:24:46 +0000612proc fix_ifcapable_expr {expr} {
613 set ret ""
614 set state 0
615 for {set i 0} {$i < [string length $expr]} {incr i} {
616 set char [string range $expr $i $i]
617 set newstate [expr {[string is alnum $char] || $char eq "_"}]
618 if {$newstate && !$state} {
619 append ret {$::sqlite_options(}
620 }
621 if {!$newstate && $state} {
622 append ret )
623 }
624 append ret $char
625 set state $newstate
626 }
627 if {$state} {append ret )}
628 return $ret
629}
630
drh27d258a2004-10-30 20:23:09 +0000631# Evaluate a boolean expression of capabilities. If true, execute the
632# code. Omit the code if false.
633#
danielk19773e8c37e2005-01-21 03:12:14 +0000634proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +0000635 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
636 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +0000637 if ($e2) {
638 set c [catch {uplevel 1 $code} r]
639 } else {
640 set c [catch {uplevel 1 $elsecode} r]
641 }
642 return -code $c $r
drh21504322002-06-25 13:16:02 +0000643}
danielk197745901d62004-11-10 15:27:38 +0000644
danielk1977aca790a2005-01-13 11:07:52 +0000645# This proc execs a seperate process that crashes midway through executing
646# the SQL script $sql on database test.db.
647#
648# The crash occurs during a sync() of file $crashfile. When the crash
649# occurs a random subset of all unsynced writes made by the process are
650# written into the files on disk. Argument $crashdelay indicates the
651# number of file syncs to wait before crashing.
652#
653# The return value is a list of two elements. The first element is a
654# boolean, indicating whether or not the process actually crashed or
655# reported some other error. The second element in the returned list is the
656# error message. This is "child process exited abnormally" if the crash
657# occured.
658#
danielk1977f8940ae2007-08-23 11:07:10 +0000659# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +0000660#
661proc crashsql {args} {
danielk197759a33f92007-03-17 10:26:59 +0000662
663 set blocksize ""
664 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +0000665 set prngseed 0
drhf8587402008-01-08 16:03:49 +0000666 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +0000667 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +0000668 set dc ""
danielk197759a33f92007-03-17 10:26:59 +0000669 set sql [lindex $args end]
670
671 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
672 set z [lindex $args $ii]
673 set n [string length $z]
674 set z2 [lindex $args [expr $ii+1]]
675
676 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +0000677 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +0000678 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +0000679 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000680 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +0000681 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +0000682 else { error "Unrecognized option: $z" }
683 }
684
685 if {$crashfile eq ""} {
686 error "Compulsory option -file missing"
687 }
688
shanehf2c08822010-07-08 16:30:44 +0000689 # $crashfile gets compared to the native filename in
690 # cfSync(), which can be different then what TCL uses by
691 # default, so here we force it to the "nativename" format.
692 set cfile [string map {\\ \\\\} [file nativename [file join [pwd] $crashfile]]]
danielk1977aca790a2005-01-13 11:07:52 +0000693
694 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +0000695 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +0000696 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +0000697 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +0000698 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +0000699
700 # This block sets the cache size of the main database to 10
701 # pages. This is done in case the build is configured to omit
702 # "PRAGMA cache_size".
703 puts $f {db eval {SELECT * FROM sqlite_master;}}
704 puts $f {set bt [btree_from_db db]}
705 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +0000706 if {$prngseed} {
707 set seed [expr {$prngseed%10007+1}]
708 # puts seed=$seed
709 puts $f "db eval {SELECT randomblob($seed)}"
710 }
danielk1977933bbd62007-03-17 07:22:42 +0000711
drhf8587402008-01-08 16:03:49 +0000712 if {[string length $tclbody]>0} {
713 puts $f $tclbody
714 }
715 if {[string length $sql]>0} {
716 puts $f "db eval {"
717 puts $f "$sql"
718 puts $f "}"
719 }
danielk1977aca790a2005-01-13 11:07:52 +0000720 close $f
danielk1977aca790a2005-01-13 11:07:52 +0000721 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000722 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000723 } msg]
shanehf2c08822010-07-08 16:30:44 +0000724
725 # Windows/ActiveState TCL returns a slightly different
726 # error message. We map that to the expected message
727 # so that we don't have to change all of the test
728 # cases.
729 if {$::tcl_platform(platform)=="windows"} {
730 if {$msg=="child killed: unknown signal"} {
731 set msg "child process exited abnormally"
732 }
733 }
734
danielk1977aca790a2005-01-13 11:07:52 +0000735 lappend r $msg
736}
737
danielk197732554c12005-01-22 03:39:39 +0000738# Usage: do_ioerr_test <test number> <options...>
739#
740# This proc is used to implement test cases that check that IO errors
741# are correctly handled. The first argument, <test number>, is an integer
742# used to name the tests executed by this proc. Options are as follows:
743#
744# -tclprep TCL script to run to prepare test.
745# -sqlprep SQL script to run to prepare test.
746# -tclbody TCL script to run with IO error simulation.
747# -sqlbody TCL script to run with IO error simulation.
748# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000749# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +0000750# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +0000751# -start Value of 'N' to begin with (default 1)
752#
753# -cksum Boolean. If true, test that the database does
754# not change during the execution of the test case.
755#
756proc do_ioerr_test {testname args} {
757
danielk197732554c12005-01-22 03:39:39 +0000758 set ::ioerropts(-start) 1
759 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000760 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +0000761 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +0000762 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +0000763 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +0000764 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +0000765 array set ::ioerropts $args
766
danielk197747cd39c2008-05-12 12:41:15 +0000767 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
768 # a couple of obscure IO errors that do not return them.
769 set ::ioerropts(-erc) 0
770
danielk197732554c12005-01-22 03:39:39 +0000771 set ::go 1
drh93aed5a2008-01-16 17:46:38 +0000772 #reset_prng_state
773 save_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +0000774 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +0000775 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +0000776 incr ::ioerropts(-count) -1
777 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +0000778
779 # Skip this IO error if it was specified with the "-exclude" option.
780 if {[info exists ::ioerropts(-exclude)]} {
781 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
782 }
danielk1977861f7452008-06-05 11:39:11 +0000783 if {$::ioerropts(-restoreprng)} {
784 restore_prng_state
785 }
danielk197732554c12005-01-22 03:39:39 +0000786
787 # Delete the files test.db and test2.db, then execute the TCL and
788 # SQL (in that order) to prepare for the test case.
789 do_test $testname.$n.1 {
790 set ::sqlite_io_error_pending 0
791 catch {db close}
aswiftaebf4132008-11-21 00:10:35 +0000792 catch {db2 close}
danielk197732554c12005-01-22 03:39:39 +0000793 catch {file delete -force test.db}
794 catch {file delete -force test.db-journal}
795 catch {file delete -force test2.db}
796 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000797 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000798 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000799 if {[info exists ::ioerropts(-tclprep)]} {
800 eval $::ioerropts(-tclprep)
801 }
802 if {[info exists ::ioerropts(-sqlprep)]} {
803 execsql $::ioerropts(-sqlprep)
804 }
805 expr 0
806 } {0}
807
808 # Read the 'checksum' of the database.
809 if {$::ioerropts(-cksum)} {
810 set checksum [cksum]
811 }
drh93aed5a2008-01-16 17:46:38 +0000812
danielk197732554c12005-01-22 03:39:39 +0000813 # Set the Nth IO error to fail.
814 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +0000815 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +0000816 set ::sqlite_io_error_pending $n
817 }] $n
818
819 # Create a single TCL script from the TCL and SQL specified
820 # as the body of the test.
821 set ::ioerrorbody {}
822 if {[info exists ::ioerropts(-tclbody)]} {
823 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
824 }
825 if {[info exists ::ioerropts(-sqlbody)]} {
826 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
827 }
828
829 # Execute the TCL Script created in the above block. If
830 # there are at least N IO operations performed by SQLite as
831 # a result of the script, the Nth will fail.
832 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +0000833 set ::sqlite_io_error_hit 0
834 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +0000835 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +0000836 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +0000837 set rc [sqlite3_errcode $::DB]
838 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +0000839 # If we are in extended result code mode, make sure all of the
840 # IOERRs we get back really do have their extended code values.
841 # If an extended result code is returned, the sqlite3_errcode
842 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
843 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +0000844 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
845 return $rc
846 }
847 } else {
drh5f7b5bf2007-04-19 12:30:54 +0000848 # If we are not in extended result code mode, make sure no
849 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +0000850 if {[regexp {\+\d} $rc]} {
851 return $rc
852 }
853 }
drh93aed5a2008-01-16 17:46:38 +0000854 # The test repeats as long as $::go is non-zero. $::go starts out
855 # as 1. When a test runs to completion without hitting an I/O
856 # error, that means there is no point in continuing with this test
857 # case so set $::go to zero.
858 #
859 if {$::sqlite_io_error_pending>0} {
860 set ::go 0
861 set q 0
862 set ::sqlite_io_error_pending 0
863 } else {
864 set q 1
865 }
866
drhc9ac5ca2005-11-04 22:03:30 +0000867 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +0000868 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
869 set r 1
870 }
danielk1977f2fa8312006-01-24 13:09:33 +0000871 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +0000872
873 # One of two things must have happened. either
874 # 1. We never hit the IO error and the SQL returned OK
875 # 2. An IO error was hit and the SQL failed
876 #
dand41a29a2010-05-06 15:56:28 +0000877 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +0000878 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +0000879 } {1}
880
danielk197780daec62008-05-12 10:57:02 +0000881 set ::sqlite_io_error_hit 0
882 set ::sqlite_io_error_pending 0
883
danielk197752b472a2008-05-05 16:23:55 +0000884 # Check that no page references were leaked. There should be
885 # a single reference if there is still an active transaction,
886 # or zero otherwise.
887 #
danielk197781620542008-06-07 05:19:37 +0000888 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
889 # locks are established on database files (i.e. if the error
890 # occurs while attempting to detect a hot-journal file), then
891 # there may 0 page references and an active transaction according
892 # to [sqlite3_get_autocommit].
893 #
danielk19774abd5442008-05-05 15:26:50 +0000894 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +0000895 do_test $testname.$n.4 {
896 set bt [btree_from_db db]
897 db_enter db
898 array set stats [btree_pager_stats $bt]
899 db_leave db
danielk197781620542008-06-07 05:19:37 +0000900 set nRef $stats(ref)
901 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
902 } {1}
danielk19774abd5442008-05-05 15:26:50 +0000903 }
904
danielk197752b472a2008-05-05 16:23:55 +0000905 # If there is an open database handle and no open transaction,
906 # and the pager is not running in exclusive-locking mode,
907 # check that the pager is in "unlocked" state. Theoretically,
908 # if a call to xUnlock() failed due to an IO error the underlying
909 # file may still be locked.
910 #
911 ifcapable pragma {
912 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +0000913 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +0000914 && [db one {pragma locking_mode}] eq "normal"
915 && [sqlite3_get_autocommit db]
916 } {
917 do_test $testname.$n.5 {
918 set bt [btree_from_db db]
919 db_enter db
920 array set stats [btree_pager_stats $bt]
921 db_leave db
922 set stats(state)
923 } 0
924 }
925 }
926
danielk197732554c12005-01-22 03:39:39 +0000927 # If an IO error occured, then the checksum of the database should
928 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +0000929 #
drh1aa5af12008-03-07 19:51:14 +0000930 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +0000931 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +0000932 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +0000933 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +0000934 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +0000935 cksum
936 } $checksum
937 }
938
drh1aa5af12008-03-07 19:51:14 +0000939 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +0000940 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +0000941 if {[info exists ::ioerropts(-cleanup)]} {
942 catch $::ioerropts(-cleanup)
943 }
danielk197732554c12005-01-22 03:39:39 +0000944 }
945 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +0000946 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +0000947 unset ::ioerropts
948}
949
drh93aed5a2008-01-16 17:46:38 +0000950# Return a checksum based on the contents of the main database associated
951# with connection $db
danielk197732554c12005-01-22 03:39:39 +0000952#
953proc cksum {{db db}} {
954 set txt [$db eval {
955 SELECT name, type, sql FROM sqlite_master order by name
956 }]\n
957 foreach tbl [$db eval {
958 SELECT name FROM sqlite_master WHERE type='table' order by name
959 }] {
960 append txt [$db eval "SELECT * FROM $tbl"]\n
961 }
962 foreach prag {default_synchronous default_cache_size} {
963 append txt $prag-[$db eval "PRAGMA $prag"]\n
964 }
965 set cksum [string length $txt]-[md5 $txt]
966 # puts $cksum-[file size test.db]
967 return $cksum
968}
969
drh93aed5a2008-01-16 17:46:38 +0000970# Generate a checksum based on the contents of the main and temp tables
971# database $db. If the checksum of two databases is the same, and the
972# integrity-check passes for both, the two databases are identical.
973#
drh1db639c2008-01-17 02:36:28 +0000974proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +0000975 set ret [list]
976 ifcapable tempdb {
977 set sql {
978 SELECT name FROM sqlite_master WHERE type = 'table' UNION
979 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
980 SELECT 'sqlite_master' UNION
981 SELECT 'sqlite_temp_master' ORDER BY 1
982 }
983 } else {
984 set sql {
985 SELECT name FROM sqlite_master WHERE type = 'table' UNION
986 SELECT 'sqlite_master' ORDER BY 1
987 }
988 }
989 set tbllist [$db eval $sql]
990 set txt {}
991 foreach tbl $tbllist {
992 append txt [$db eval "SELECT * FROM $tbl"]
993 }
994 foreach prag {default_cache_size} {
995 append txt $prag-[$db eval "PRAGMA $prag"]\n
996 }
997 # puts txt=$txt
998 return [md5 $txt]
999}
1000
drhdc2c4912009-02-04 22:46:47 +00001001# Generate a checksum based on the contents of a single database with
1002# a database connection. The name of the database is $dbname.
1003# Examples of $dbname are "temp" or "main".
1004#
1005proc dbcksum {db dbname} {
1006 if {$dbname=="temp"} {
1007 set master sqlite_temp_master
1008 } else {
1009 set master $dbname.sqlite_master
1010 }
1011 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
1012 set txt [$db eval "SELECT * FROM $master"]\n
1013 foreach tab $alltab {
1014 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
1015 }
1016 return [md5 $txt]
1017}
1018
danielk197735754ac2008-03-21 17:29:37 +00001019proc memdebug_log_sql {{filename mallocs.sql}} {
1020
danielk19776f332c12008-03-21 14:22:44 +00001021 set data [sqlite3_memdebug_log dump]
1022 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +00001023 if {$nFrame < 0} { return "" }
1024
danielk197735754ac2008-03-21 17:29:37 +00001025 set database temp
1026
danielk19776ab3a2e2009-02-19 14:39:25 +00001027 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +00001028
1029 set sql ""
1030 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +00001031 set nCall [lindex $e 0]
1032 set nByte [lindex $e 1]
1033 set lStack [lrange $e 2 end]
1034 append sql "INSERT INTO ${database}.malloc VALUES"
1035 append sql "('test', $nCall, $nByte, '$lStack');\n"
1036 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +00001037 set frames($f) 1
1038 }
1039 }
1040
1041 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +00001042 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +00001043
1044 foreach f [array names frames] {
1045 set addr [format %x $f]
1046 set cmd "addr2line -e [info nameofexec] $addr"
1047 set line [eval exec $cmd]
1048 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00001049
1050 set file [lindex [split $line :] 0]
1051 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00001052 }
1053
danielk197735754ac2008-03-21 17:29:37 +00001054 foreach f [array names files] {
1055 set contents ""
1056 catch {
1057 set fd [open $f]
1058 set contents [read $fd]
1059 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001060 }
danielk197735754ac2008-03-21 17:29:37 +00001061 set contents [string map {' ''} $contents]
1062 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00001063 }
danielk19776f332c12008-03-21 14:22:44 +00001064
danielk197735754ac2008-03-21 17:29:37 +00001065 set fd [open $filename w]
1066 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
1067 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001068}
drh93aed5a2008-01-16 17:46:38 +00001069
danielk197732554c12005-01-22 03:39:39 +00001070# Copy file $from into $to. This is used because some versions of
1071# TCL for windows (notably the 8.4.1 binary package shipped with the
1072# current mingw release) have a broken "file copy" command.
1073#
1074proc copy_file {from to} {
1075 if {$::tcl_platform(platform)=="unix"} {
1076 file copy -force $from $to
1077 } else {
1078 set f [open $from]
1079 fconfigure $f -translation binary
1080 set t [open $to w]
1081 fconfigure $t -translation binary
1082 puts -nonewline $t [read $f [file size $from]]
1083 close $t
1084 close $f
1085 }
1086}
1087
danf5894502009-10-07 18:41:19 +00001088# Drop all tables in database [db]
1089proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00001090 ifcapable trigger&&foreignkey {
1091 set pk [$db one "PRAGMA foreign_keys"]
1092 $db eval "PRAGMA foreign_keys = OFF"
1093 }
drh9a6ffc82010-02-15 18:03:20 +00001094 foreach {idx name file} [db eval {PRAGMA database_list}] {
1095 if {$idx==1} {
1096 set master sqlite_temp_master
1097 } else {
1098 set master $name.sqlite_master
1099 }
1100 foreach {t type} [$db eval "
1101 SELECT name, type FROM $master
1102 WHERE type IN('table', 'view') AND name NOT like 'sqlite_%'
1103 "] {
1104 $db eval "DROP $type $t"
1105 }
danf5894502009-10-07 18:41:19 +00001106 }
shaneh4e7b32f2009-12-17 22:12:51 +00001107 ifcapable trigger&&foreignkey {
1108 $db eval "PRAGMA foreign_keys = $pk"
1109 }
danf5894502009-10-07 18:41:19 +00001110}
1111
dan71cb5182010-04-26 12:39:03 +00001112#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00001113# If a test script is executed with global variable $::G(perm:name) set to
1114# "wal", then the tests are run in WAL mode. Otherwise, they should be run
1115# in rollback mode. The following Tcl procs are used to make this less
1116# intrusive:
dan71cb5182010-04-26 12:39:03 +00001117#
1118# wal_set_journal_mode ?DB?
1119#
1120# If running a WAL test, execute "PRAGMA journal_mode = wal" using
1121# connection handle DB. Otherwise, this command is a no-op.
1122#
1123# wal_check_journal_mode TESTNAME ?DB?
1124#
1125# If running a WAL test, execute a tests case that fails if the main
1126# database for connection handle DB is not currently a WAL database.
1127# Otherwise (if not running a WAL permutation) this is a no-op.
1128#
1129# wal_is_wal_mode
1130#
1131# Returns true if this test should be run in WAL mode. False otherwise.
1132#
1133proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00001134 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00001135}
1136proc wal_set_journal_mode {{db db}} {
1137 if { [wal_is_wal_mode] } {
1138 $db eval "PRAGMA journal_mode = WAL"
1139 }
1140}
1141proc wal_check_journal_mode {testname {db db}} {
1142 if { [wal_is_wal_mode] } {
1143 $db eval { SELECT * FROM sqlite_master }
1144 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
1145 }
1146}
1147
dan430e74c2010-06-07 17:47:26 +00001148proc permutation {} {
1149 set perm ""
1150 catch {set perm $::G(perm:name)}
1151 set perm
1152}
1153
danc1a60c52010-06-07 14:28:16 +00001154#-------------------------------------------------------------------------
1155#
1156proc slave_test_script {script} {
1157
1158 # Create the interpreter used to run the test script.
1159 interp create tinterp
1160
1161 # Populate some global variables that tester.tcl expects to see.
1162 foreach {var value} [list \
1163 ::argv0 $::argv0 \
1164 ::argv {} \
1165 ::SLAVE 1 \
1166 ] {
1167 interp eval tinterp [list set $var $value]
1168 }
1169
1170 # The alias used to access the global test counters.
1171 tinterp alias set_test_counter set_test_counter
1172
1173 # Set up the ::cmdlinearg array in the slave.
1174 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
1175
dan430e74c2010-06-07 17:47:26 +00001176 # Set up the ::G array in the slave.
1177 interp eval tinterp [list array set ::G [array get ::G]]
1178
danc1a60c52010-06-07 14:28:16 +00001179 # Load the various test interfaces implemented in C.
1180 load_testfixture_extensions tinterp
1181
1182 # Run the test script.
1183 interp eval tinterp $script
1184
danea5542d2010-07-06 11:26:15 +00001185 # Check if the interpreter call [run_thread_tests]
1186 if { [interp eval tinterp {info exists ::run_thread_tests_called}] } {
1187 set ::run_thread_tests_called 1
1188 }
1189
danc1a60c52010-06-07 14:28:16 +00001190 # Delete the interpreter used to run the test script.
1191 interp delete tinterp
1192}
1193
dan430e74c2010-06-07 17:47:26 +00001194proc slave_test_file {zFile} {
dan0626dfc2010-06-15 06:56:37 +00001195 set tail [file tail $zFile]
dan430e74c2010-06-07 17:47:26 +00001196
dan92d516a2010-07-05 14:54:48 +00001197 # Remember the value of the shared-cache setting. So that it is possible
1198 # to check afterwards that it was not modified by the test script.
1199 #
1200 ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] }
dan0626dfc2010-06-15 06:56:37 +00001201
dan92d516a2010-07-05 14:54:48 +00001202 # Run the test script in a slave interpreter.
1203 #
danea5542d2010-07-06 11:26:15 +00001204 unset -nocomplain ::run_thread_tests_called
dan0626dfc2010-06-15 06:56:37 +00001205 reset_prng_state
1206 set ::sqlite_open_file_count 0
1207 set time [time { slave_test_script [list source $zFile] }]
1208 set ms [expr [lindex $time 0] / 1000]
1209
dan92d516a2010-07-05 14:54:48 +00001210 # Test that all files opened by the test script were closed. Omit this
1211 # if the test script has "thread" in its name. The open file counter
1212 # is not thread-safe.
dan0626dfc2010-06-15 06:56:37 +00001213 #
danea5542d2010-07-06 11:26:15 +00001214 if {[info exists ::run_thread_tests_called]==0} {
dan92d516a2010-07-05 14:54:48 +00001215 do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0}
1216 }
dan430e74c2010-06-07 17:47:26 +00001217 set ::sqlite_open_file_count 0
1218
dan0626dfc2010-06-15 06:56:37 +00001219 # Test that the global "shared-cache" setting was not altered by
1220 # the test script.
1221 #
1222 ifcapable shared_cache {
1223 set res [expr {[sqlite3_enable_shared_cache] == $scs}]
1224 do_test ${tail}-sharedcachesetting [list set {} $res] 1
1225 }
dan430e74c2010-06-07 17:47:26 +00001226
dan92d516a2010-07-05 14:54:48 +00001227 # Add some info to the output.
1228 #
dan0626dfc2010-06-15 06:56:37 +00001229 puts "Time: $tail $ms ms"
dan430e74c2010-06-07 17:47:26 +00001230 show_memstats
danc1a60c52010-06-07 14:28:16 +00001231}
1232
dan59257dc2010-08-04 11:34:31 +00001233# Open a new connection on database test.db and execute the SQL script
1234# supplied as an argument. Before returning, close the new conection and
1235# restore the 4 byte fields starting at header offsets 28, 92 and 96
1236# to the values they held before the SQL was executed. This simulates
1237# a write by a pre-3.7.0 client.
1238#
1239proc sql36231 {sql} {
1240 set B [hexio_read test.db 92 8]
1241 set A [hexio_read test.db 28 4]
1242 sqlite3 db36231 test.db
1243 catch { db36231 func a_string a_string }
1244 execsql $sql db36231
1245 db36231 close
1246 hexio_write test.db 28 $A
1247 hexio_write test.db 92 $B
1248 return ""
1249}
danf5894502009-10-07 18:41:19 +00001250
danielk197745901d62004-11-10 15:27:38 +00001251# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
1252# to non-zero, then set the global variable $AUTOVACUUM to 1.
1253set AUTOVACUUM $sqlite_options(default_autovacuum)
danielk1977ee8b7992009-03-26 17:13:06 +00001254
1255source $testdir/thread_common.tcl