blob: fb26561b231f92eccbef7040b872da7a2b96f40e [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drh348784e2000-05-29 20:41:49 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drh348784e2000-05-29 20:41:49 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# May you do good and not evil.
7# May you find forgiveness for yourself and forgive others.
8# May you share freely, never taking more than you give.
drh348784e2000-05-29 20:41:49 +00009#
10#***********************************************************************
11# This file implements some common TCL routines used for regression
12# testing the SQLite library
13#
drhbb77b752009-04-09 01:23:49 +000014# $Id: tester.tcl,v 1.143 2009/04/09 01:23:49 drh Exp $
drhfbc3eab2001-04-06 16:13:42 +000015
danc1a60c52010-06-07 14:28:16 +000016#-------------------------------------------------------------------------
17# The commands provided by the code in this file to help with creating
18# test cases are as follows:
drh8359d8c2008-03-29 11:00:54 +000019#
danc1a60c52010-06-07 14:28:16 +000020# Commands to manipulate the db and the file-system at a high level:
drh8359d8c2008-03-29 11:00:54 +000021#
danc1a60c52010-06-07 14:28:16 +000022# copy_file FROM TO
23# drop_all_table ?DB?
24# forcedelete FILENAME
25#
26# Test the capability of the SQLite version built into the interpreter to
27# determine if a specific test can be run:
28#
29# ifcapable EXPR
30#
31# Calulate checksums based on database contents:
32#
33# dbcksum DB DBNAME
34# allcksum ?DB?
35# cksum ?DB?
36#
37# Commands to execute/explain SQL statements:
38#
39# stepsql DB SQL
40# execsql2 SQL
41# explain_no_trace SQL
42# explain SQL ?DB?
43# catchsql SQL ?DB?
44# execsql SQL ?DB?
45#
46# Commands to run test cases:
47#
48# do_ioerr_test TESTNAME ARGS...
49# crashsql ARGS...
50# integrity_check TESTNAME ?DB?
51# do_test TESTNAME SCRIPT EXPECTED
dan153eda02010-06-21 07:45:47 +000052# do_execsql_test TESTNAME SQL EXPECTED
53# do_catchsql_test TESTNAME SQL EXPECTED
danc1a60c52010-06-07 14:28:16 +000054#
dan430e74c2010-06-07 17:47:26 +000055# Commands providing a lower level interface to the global test counters:
danc1a60c52010-06-07 14:28:16 +000056#
57# set_test_counter COUNTER ?VALUE?
58# omit_test TESTNAME REASON
59# fail_test TESTNAME
60# incr_ntest
61#
62# Command run at the end of each test file:
63#
64# finish_test
65#
dan430e74c2010-06-07 17:47:26 +000066# Commands to help create test files that run with the "WAL" and other
67# permutations (see file permutations.test):
danc1a60c52010-06-07 14:28:16 +000068#
69# wal_is_wal_mode
70# wal_set_journal_mode ?DB?
71# wal_check_journal_mode TESTNAME?DB?
dan430e74c2010-06-07 17:47:26 +000072# permutation
dancb79e512010-08-06 13:50:07 +000073# presql
danc1a60c52010-06-07 14:28:16 +000074#
drh348784e2000-05-29 20:41:49 +000075
danc1a60c52010-06-07 14:28:16 +000076# Set the precision of FP arithmatic used by the interpreter. And
77# configure SQLite to take database file locks on the page that begins
78# 64KB into the database file instead of the one 1GB in. This means
79# the code that handles that special case can be tested without creating
80# very large database files.
81#
drh92febd92004-08-20 18:34:20 +000082set tcl_precision 15
drhc7a3bb92009-02-05 16:31:45 +000083sqlite3_test_control_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +000084
danc1a60c52010-06-07 14:28:16 +000085
86# If the pager codec is available, create a wrapper for the [sqlite3]
87# command that appends "-key {xyzzy}" to the command line. i.e. this:
drh3a7fb7c2007-08-10 16:41:08 +000088#
danc1a60c52010-06-07 14:28:16 +000089# sqlite3 db test.db
drh4a50aac2007-08-23 02:47:53 +000090#
danc1a60c52010-06-07 14:28:16 +000091# becomes
drh4a50aac2007-08-23 02:47:53 +000092#
danc1a60c52010-06-07 14:28:16 +000093# sqlite3 db test.db -key {xyzzy}
drh9eb9e262004-02-11 02:18:05 +000094#
dan430e74c2010-06-07 17:47:26 +000095if {[info command sqlite_orig]==""} {
drhef4ac8f2004-06-19 00:16:31 +000096 rename sqlite3 sqlite_orig
97 proc sqlite3 {args} {
dan68928b62010-06-22 13:46:43 +000098 if {[llength $args]>=2 && [string index [lindex $args 0] 0]!="-"} {
dan430e74c2010-06-07 17:47:26 +000099 # This command is opening a new database connection.
100 #
101 if {[info exists ::G(perm:sqlite3_args)]} {
102 set args [concat $args $::G(perm:sqlite3_args)]
103 }
dan68928b62010-06-22 13:46:43 +0000104 if {[sqlite_orig -has-codec] && ![info exists ::do_not_use_codec]} {
dan430e74c2010-06-07 17:47:26 +0000105 lappend args -key {xyzzy}
106 }
107
dan3ccd20a2010-06-09 19:01:02 +0000108 set res [uplevel 1 sqlite_orig $args]
dan430e74c2010-06-07 17:47:26 +0000109 if {[info exists ::G(perm:presql)]} {
110 [lindex $args 0] eval $::G(perm:presql)
111 }
dan3ccd20a2010-06-09 19:01:02 +0000112 set res
dan430e74c2010-06-07 17:47:26 +0000113 } else {
114 # This command is not opening a new database connection. Pass the
115 # arguments through to the C implemenation as the are.
116 #
117 uplevel 1 sqlite_orig $args
drh9eb9e262004-02-11 02:18:05 +0000118 }
drh9eb9e262004-02-11 02:18:05 +0000119 }
120}
121
dancb354602010-07-08 09:44:42 +0000122proc execpresql {handle args} {
123 trace remove execution $handle enter [list execpresql $handle]
124 if {[info exists ::G(perm:presql)]} {
125 $handle eval $::G(perm:presql)
126 }
127}
128
dan68928b62010-06-22 13:46:43 +0000129# This command should be called after loading tester.tcl from within
130# all test scripts that are incompatible with encryption codecs.
131#
132proc do_not_use_codec {} {
133 set ::do_not_use_codec 1
134 reset_db
135}
136
dan430e74c2010-06-07 17:47:26 +0000137# The following block only runs the first time this file is sourced. It
138# does not run in slave interpreters (since the ::cmdlinearg array is
139# populated before the test script is run in slave interpreters).
drhbec2bf42000-05-29 23:48:22 +0000140#
danc1a60c52010-06-07 14:28:16 +0000141if {[info exists cmdlinearg]==0} {
142
143 # Parse any options specified in the $argv array. This script accepts the
144 # following options:
145 #
146 # --pause
147 # --soft-heap-limit=NN
148 # --maxerror=NN
149 # --malloctrace=N
150 # --backtrace=N
151 # --binarylog=N
dan0626dfc2010-06-15 06:56:37 +0000152 # --soak=N
danc1a60c52010-06-07 14:28:16 +0000153 #
154 set cmdlinearg(soft-heap-limit) 0
155 set cmdlinearg(maxerror) 1000
156 set cmdlinearg(malloctrace) 0
157 set cmdlinearg(backtrace) 10
158 set cmdlinearg(binarylog) 0
dan0626dfc2010-06-15 06:56:37 +0000159 set cmdlinearg(soak) 0
danc1a60c52010-06-07 14:28:16 +0000160
161 set leftover [list]
162 foreach a $argv {
163 switch -regexp -- $a {
164 {^-+pause$} {
165 # Wait for user input before continuing. This is to give the user an
166 # opportunity to connect profiling tools to the process.
167 puts -nonewline "Press RETURN to begin..."
168 flush stdout
169 gets stdin
170 }
171 {^-+soft-heap-limit=.+$} {
172 foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break
173 }
174 {^-+maxerror=.+$} {
175 foreach {dummy cmdlinearg(maxerror)} [split $a =] break
176 }
177 {^-+malloctrace=.+$} {
178 foreach {dummy cmdlinearg(malloctrace)} [split $a =] break
179 if {$cmdlinearg(malloctrace)} {
180 sqlite3_memdebug_log start
181 }
182 }
183 {^-+backtrace=.+$} {
184 foreach {dummy cmdlinearg(backtrace)} [split $a =] break
dan0626dfc2010-06-15 06:56:37 +0000185 sqlite3_memdebug_backtrace $value
danc1a60c52010-06-07 14:28:16 +0000186 }
danc1a60c52010-06-07 14:28:16 +0000187 {^-+binarylog=.+$} {
188 foreach {dummy cmdlinearg(binarylog)} [split $a =] break
189 }
dan0626dfc2010-06-15 06:56:37 +0000190 {^-+soak=.+$} {
191 foreach {dummy cmdlinearg(soak)} [split $a =] break
192 set ::G(issoak) $cmdlinearg(soak)
193 }
danc1a60c52010-06-07 14:28:16 +0000194 default {
195 lappend leftover $a
196 }
197 }
198 }
199 set argv $leftover
200
dan430e74c2010-06-07 17:47:26 +0000201 # Install the malloc layer used to inject OOM errors. And the 'automatic'
202 # extensions. This only needs to be done once for the process.
203 #
danielk1977f7b9d662008-06-23 18:49:43 +0000204 sqlite3_shutdown
205 install_malloc_faultsim 1
206 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000207 autoinstall_test_functions
danc1a60c52010-06-07 14:28:16 +0000208
dan430e74c2010-06-07 17:47:26 +0000209 # If the --binarylog option was specified, create the logging VFS. This
210 # call installs the new VFS as the default for all SQLite connections.
211 #
danc1a60c52010-06-07 14:28:16 +0000212 if {$cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000213 vfslog new binarylog {} vfslog.bin
danc1a60c52010-06-07 14:28:16 +0000214 }
215
216 # Set the backtrace depth, if malloc tracing is enabled.
217 #
218 if {$cmdlinearg(malloctrace)} {
219 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danielk1977185eac92008-07-12 15:55:54 +0000220 }
danielk1977f7b9d662008-06-23 18:49:43 +0000221}
danielk197703ba3fa2009-01-09 10:49:14 +0000222
danc1a60c52010-06-07 14:28:16 +0000223# Update the soft-heap-limit each time this script is run. In that
224# way if an individual test file changes the soft-heap-limit, it
225# will be reset at the start of the next test file.
226#
227sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
228
229# Create a test database
230#
danielk197703ba3fa2009-01-09 10:49:14 +0000231proc reset_db {} {
232 catch {db close}
233 file delete -force test.db
234 file delete -force test.db-journal
dand3f8f942010-04-13 11:35:01 +0000235 file delete -force test.db-wal
danielk197703ba3fa2009-01-09 10:49:14 +0000236 sqlite3 db ./test.db
237 set ::DB [sqlite3_connection_pointer db]
238 if {[info exists ::SETUP_SQL]} {
239 db eval $::SETUP_SQL
240 }
drhcd61c282002-03-06 22:01:34 +0000241}
danielk197703ba3fa2009-01-09 10:49:14 +0000242reset_db
drhbec2bf42000-05-29 23:48:22 +0000243
244# Abort early if this script has been run before.
245#
danc1a60c52010-06-07 14:28:16 +0000246if {[info exists TC(count)]} return
drhbec2bf42000-05-29 23:48:22 +0000247
danc1a60c52010-06-07 14:28:16 +0000248# Initialize the test counters and set up commands to access them.
249# Or, if this is a slave interpreter, set up aliases to write the
250# counters in the parent interpreter.
drhbec2bf42000-05-29 23:48:22 +0000251#
danc1a60c52010-06-07 14:28:16 +0000252if {0==[info exists ::SLAVE]} {
253 set TC(errors) 0
254 set TC(count) 0
255 set TC(fail_list) [list]
256 set TC(omit_list) [list]
257
258 proc set_test_counter {counter args} {
259 if {[llength $args]} {
260 set ::TC($counter) [lindex $args 0]
261 }
262 set ::TC($counter)
263 }
drhb62c3352006-11-23 09:39:16 +0000264}
drh348784e2000-05-29 20:41:49 +0000265
drh521cc842008-04-15 02:36:33 +0000266# Record the fact that a sequence of tests were omitted.
267#
268proc omit_test {name reason} {
danc1a60c52010-06-07 14:28:16 +0000269 set omitList [set_test_counter omit_list]
drh521cc842008-04-15 02:36:33 +0000270 lappend omitList [list $name $reason]
danc1a60c52010-06-07 14:28:16 +0000271 set_test_counter omit_list $omitList
drh521cc842008-04-15 02:36:33 +0000272}
273
danc1a60c52010-06-07 14:28:16 +0000274# Record the fact that a test failed.
275#
276proc fail_test {name} {
277 set f [set_test_counter fail_list]
278 lappend f $name
279 set_test_counter fail_list $f
280 set_test_counter errors [expr [set_test_counter errors] + 1]
281
282 set nFail [set_test_counter errors]
283 if {$nFail>=$::cmdlinearg(maxerror)} {
284 puts "*** Giving up..."
285 finalize_testing
286 }
287}
288
289# Increment the number of tests run
290#
291proc incr_ntest {} {
292 set_test_counter count [expr [set_test_counter count] + 1]
293}
294
295
drh348784e2000-05-29 20:41:49 +0000296# Invoke the do_test procedure to run a single test
297#
298proc do_test {name cmd expected} {
danc1a60c52010-06-07 14:28:16 +0000299
300 global argv cmdlinearg
301
drh4a50aac2007-08-23 02:47:53 +0000302 sqlite3_memdebug_settitle $name
danc1a60c52010-06-07 14:28:16 +0000303
dan92d516a2010-07-05 14:54:48 +0000304# if {[llength $argv]==0} {
305# set go 1
306# } else {
307# set go 0
308# foreach pattern $argv {
309# if {[string match $pattern $name]} {
310# set go 1
311# break
312# }
313# }
314# }
dan430e74c2010-06-07 17:47:26 +0000315
dand506de02010-07-03 13:59:01 +0000316 if {[info exists ::G(perm:prefix)]} {
317 set name "$::G(perm:prefix)$name"
dan430e74c2010-06-07 17:47:26 +0000318 }
319
danc1a60c52010-06-07 14:28:16 +0000320 incr_ntest
drh5edc3122001-09-13 21:53:09 +0000321 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000322 flush stdout
323 if {[catch {uplevel #0 "$cmd;\n"} result]} {
324 puts "\nError: $result"
danc1a60c52010-06-07 14:28:16 +0000325 fail_test $name
drh348784e2000-05-29 20:41:49 +0000326 } elseif {[string compare $result $expected]} {
327 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
danc1a60c52010-06-07 14:28:16 +0000328 fail_test $name
drh348784e2000-05-29 20:41:49 +0000329 } else {
330 puts " Ok"
331 }
drh6558db82007-04-13 03:23:21 +0000332 flush stdout
drh348784e2000-05-29 20:41:49 +0000333}
dan153eda02010-06-21 07:45:47 +0000334
335proc do_execsql_test {testname sql result} {
336 uplevel do_test $testname [list "execsql {$sql}"] [list $result]
337}
338proc do_catchsql_test {testname sql result} {
339 uplevel do_test $testname [list "catchsql {$sql}"] [list $result]
340}
341
drh348784e2000-05-29 20:41:49 +0000342
drhb62c3352006-11-23 09:39:16 +0000343# Run an SQL script.
344# Return the number of microseconds per statement.
345#
drh3590f152006-11-23 21:09:10 +0000346proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000347 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000348 flush stdout
349 set speed [time {sqlite3_exec_nr db $sql}]
350 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000351 if {$tm == 0} {
352 set rate [format %20s "many"]
353 } else {
354 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
355 }
drh3590f152006-11-23 21:09:10 +0000356 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000357 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000358 global total_time
359 set total_time [expr {$total_time+$tm}]
360}
drh45c236d2008-03-22 01:08:00 +0000361proc speed_trial_tcl {name numstmt units script} {
362 puts -nonewline [format {%-21.21s } $name...]
363 flush stdout
364 set speed [time {eval $script}]
365 set tm [lindex $speed 0]
366 if {$tm == 0} {
367 set rate [format %20s "many"]
368 } else {
369 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
370 }
371 set u2 $units/s
372 puts [format {%12d uS %s %s} $tm $rate $u2]
373 global total_time
374 set total_time [expr {$total_time+$tm}]
375}
drhdd924312007-03-31 22:34:16 +0000376proc speed_trial_init {name} {
377 global total_time
378 set total_time 0
drhbb810a92010-07-03 12:00:53 +0000379 sqlite3 versdb :memory:
380 set vers [versdb one {SELECT sqlite_source_id()}]
381 versdb close
382 puts "SQLite $vers"
drhdd924312007-03-31 22:34:16 +0000383}
384proc speed_trial_summary {name} {
385 global total_time
386 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
drhb62c3352006-11-23 09:39:16 +0000387}
388
drh348784e2000-05-29 20:41:49 +0000389# Run this routine last
390#
391proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +0000392 catch {db close}
393 catch {db2 close}
394 catch {db3 close}
395 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +0000396}
397proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +0000398 global sqlite_open_file_count
399
400 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +0000401
drh6e142f52000-06-08 13:36:40 +0000402 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000403 catch {db2 close}
404 catch {db3 close}
405
drh9bc54492007-10-23 14:49:59 +0000406 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000407 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000408 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000409 db close
drh984bfaa2008-03-19 16:08:53 +0000410 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +0000411
drh3a7fb7c2007-08-10 16:41:08 +0000412 sqlite3_soft_heap_limit 0
danc1a60c52010-06-07 14:28:16 +0000413 set nTest [incr_ntest]
414 set nErr [set_test_counter errors]
415
drh348784e2000-05-29 20:41:49 +0000416 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000417 if {$nErr>0} {
danc1a60c52010-06-07 14:28:16 +0000418 puts "Failures on these tests: [set_test_counter fail_list]"
drhf3a65f72007-08-22 20:18:21 +0000419 }
danielk1977ee8b7992009-03-26 17:13:06 +0000420 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +0000421 if {[llength $omitList]>0} {
422 puts "Omitted test cases:"
423 set prec {}
424 foreach {rec} [lsort $omitList] {
425 if {$rec==$prec} continue
426 set prec $rec
427 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
428 }
429 }
drh80788d82006-09-02 14:50:23 +0000430 if {$nErr>0 && ![working_64bit_int]} {
431 puts "******************************************************************"
432 puts "N.B.: The version of TCL that you used to build this test harness"
433 puts "is defective in that it does not support 64-bit integers. Some or"
434 puts "all of the test failures above might be a result from this defect"
435 puts "in your TCL build."
436 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000437 }
danc1a60c52010-06-07 14:28:16 +0000438 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000439 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +0000440 }
drh94e92032003-02-16 22:21:32 +0000441 if {$sqlite_open_file_count} {
442 puts "$sqlite_open_file_count files were left open"
443 incr nErr
444 }
drheafc43b2010-07-26 18:43:40 +0000445 if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 ||
446 [sqlite3_memory_used]>0} {
447 puts "Unfreed memory: [sqlite3_memory_used] bytes in\
448 [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations"
drhf3a65f72007-08-22 20:18:21 +0000449 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000450 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000451 puts "Writing unfreed memory log to \"./memleak.txt\""
452 sqlite3_memdebug_dump ./memleak.txt
453 }
drhf3a65f72007-08-22 20:18:21 +0000454 } else {
455 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000456 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000457 sqlite3_memdebug_dump ./memusage.txt
458 }
drhf3a65f72007-08-22 20:18:21 +0000459 }
drhf7141992008-06-19 00:16:08 +0000460 show_memstats
drh91fd4d42008-01-19 20:11:25 +0000461 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
462 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000463 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
464 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
465 }
danc1a60c52010-06-07 14:28:16 +0000466 if {$::cmdlinearg(malloctrace)} {
danielk197735754ac2008-03-21 17:29:37 +0000467 puts "Writing mallocs.sql..."
468 memdebug_log_sql
469 sqlite3_memdebug_log stop
470 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000471
472 if {[sqlite3_memory_used]>0} {
473 puts "Writing leaks.sql..."
474 sqlite3_memdebug_log sync
475 memdebug_log_sql leaks.sql
476 }
danielk197735754ac2008-03-21 17:29:37 +0000477 }
drhd9910fe2006-10-04 11:55:49 +0000478 foreach f [glob -nocomplain test.db-*-journal] {
479 file delete -force $f
480 }
481 foreach f [glob -nocomplain test.db-mj*] {
482 file delete -force $f
483 }
drhdb25e382001-03-15 18:21:22 +0000484 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000485}
486
drhf7141992008-06-19 00:16:08 +0000487# Display memory statistics for analysis and debugging purposes.
488#
489proc show_memstats {} {
490 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +0000491 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
492 set val [format {now %10d max %10d max-size %10d} \
493 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000494 puts "Memory used: $val"
drheafc43b2010-07-26 18:43:40 +0000495 set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0]
496 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
497 puts "Allocation count: $val"
drhf7141992008-06-19 00:16:08 +0000498 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +0000499 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
500 set val [format {now %10d max %10d max-size %10d} \
501 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000502 puts "Page-cache used: $val"
503 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
504 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
505 puts "Page-cache overflow: $val"
506 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
507 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
508 puts "Scratch memory used: $val"
509 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +0000510 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
511 set val [format {now %10d max %10d max-size %10d} \
512 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000513 puts "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +0000514 ifcapable yytrackmaxstackdepth {
515 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
516 set val [format { max %10d} [lindex $x 2]]
517 puts "Parser stack depth: $val"
518 }
drhf7141992008-06-19 00:16:08 +0000519}
520
drh348784e2000-05-29 20:41:49 +0000521# A procedure to execute SQL
522#
drhc4a3c772001-04-04 11:48:57 +0000523proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000524 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000525 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000526}
drh3aadb2e2000-05-31 17:59:25 +0000527
drhadbca9c2001-09-27 15:11:53 +0000528# Execute SQL and catch exceptions.
529#
530proc catchsql {sql {db db}} {
531 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +0000532 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +0000533 lappend r $msg
534 return $r
535}
536
drh04096482001-11-09 22:41:44 +0000537# Do an VDBE code dump on the SQL given
538#
539proc explain {sql {db db}} {
540 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000541 puts "addr opcode p1 p2 p3 p4 p5 #"
542 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000543 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000544 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
545 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
546 ]
drh04096482001-11-09 22:41:44 +0000547 }
548}
549
drhdafc0ce2008-04-17 19:14:02 +0000550# Show the VDBE program for an SQL statement but omit the Trace
551# opcode at the beginning. This procedure can be used to prove
552# that different SQL statements generate exactly the same VDBE code.
553#
554proc explain_no_trace {sql} {
555 set tr [db eval "EXPLAIN $sql"]
556 return [lrange $tr 7 end]
557}
558
drh3aadb2e2000-05-31 17:59:25 +0000559# Another procedure to execute SQL. This one includes the field
560# names in the returned list.
561#
562proc execsql2 {sql} {
563 set result {}
564 db eval $sql data {
565 foreach f $data(*) {
566 lappend result $f $data($f)
567 }
568 }
569 return $result
570}
drh17a68932001-01-31 13:28:08 +0000571
drhdde85d92003-03-01 19:45:34 +0000572# Use the non-callback API to execute multiple SQL statements
573#
574proc stepsql {dbptr sql} {
575 set sql [string trim $sql]
576 set r 0
577 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000578 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000579 return [list 1 $vm]
580 }
581 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000582# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
583# foreach v $VAL {lappend r $v}
584# }
585 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
586 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
587 lappend r [sqlite3_column_text $vm $i]
588 }
drhdde85d92003-03-01 19:45:34 +0000589 }
danielk1977106bb232004-05-21 10:08:53 +0000590 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000591 return [list 1 $errmsg]
592 }
593 }
594 return $r
595}
596
drh17a68932001-01-31 13:28:08 +0000597# Delete a file or directory
598#
599proc forcedelete {filename} {
600 if {[catch {file delete -force $filename}]} {
601 exec rm -rf $filename
602 }
603}
drh21504322002-06-25 13:16:02 +0000604
605# Do an integrity check of the entire database
606#
danielk197704103022009-02-03 16:51:24 +0000607proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +0000608 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +0000609 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +0000610 }
611}
612
danielk1977db4e8862008-01-16 08:24:46 +0000613proc fix_ifcapable_expr {expr} {
614 set ret ""
615 set state 0
616 for {set i 0} {$i < [string length $expr]} {incr i} {
617 set char [string range $expr $i $i]
618 set newstate [expr {[string is alnum $char] || $char eq "_"}]
619 if {$newstate && !$state} {
620 append ret {$::sqlite_options(}
621 }
622 if {!$newstate && $state} {
623 append ret )
624 }
625 append ret $char
626 set state $newstate
627 }
628 if {$state} {append ret )}
629 return $ret
630}
631
drh27d258a2004-10-30 20:23:09 +0000632# Evaluate a boolean expression of capabilities. If true, execute the
633# code. Omit the code if false.
634#
danielk19773e8c37e2005-01-21 03:12:14 +0000635proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +0000636 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
637 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +0000638 if ($e2) {
639 set c [catch {uplevel 1 $code} r]
640 } else {
641 set c [catch {uplevel 1 $elsecode} r]
642 }
643 return -code $c $r
drh21504322002-06-25 13:16:02 +0000644}
danielk197745901d62004-11-10 15:27:38 +0000645
danielk1977aca790a2005-01-13 11:07:52 +0000646# This proc execs a seperate process that crashes midway through executing
647# the SQL script $sql on database test.db.
648#
649# The crash occurs during a sync() of file $crashfile. When the crash
650# occurs a random subset of all unsynced writes made by the process are
651# written into the files on disk. Argument $crashdelay indicates the
652# number of file syncs to wait before crashing.
653#
654# The return value is a list of two elements. The first element is a
655# boolean, indicating whether or not the process actually crashed or
656# reported some other error. The second element in the returned list is the
657# error message. This is "child process exited abnormally" if the crash
658# occured.
659#
danielk1977f8940ae2007-08-23 11:07:10 +0000660# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +0000661#
662proc crashsql {args} {
danielk197759a33f92007-03-17 10:26:59 +0000663
664 set blocksize ""
665 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +0000666 set prngseed 0
drhf8587402008-01-08 16:03:49 +0000667 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +0000668 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +0000669 set dc ""
danielk197759a33f92007-03-17 10:26:59 +0000670 set sql [lindex $args end]
671
672 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
673 set z [lindex $args $ii]
674 set n [string length $z]
675 set z2 [lindex $args [expr $ii+1]]
676
677 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +0000678 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +0000679 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +0000680 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000681 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +0000682 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +0000683 else { error "Unrecognized option: $z" }
684 }
685
686 if {$crashfile eq ""} {
687 error "Compulsory option -file missing"
688 }
689
shanehf2c08822010-07-08 16:30:44 +0000690 # $crashfile gets compared to the native filename in
691 # cfSync(), which can be different then what TCL uses by
692 # default, so here we force it to the "nativename" format.
693 set cfile [string map {\\ \\\\} [file nativename [file join [pwd] $crashfile]]]
danielk1977aca790a2005-01-13 11:07:52 +0000694
695 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +0000696 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +0000697 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +0000698 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +0000699 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +0000700
701 # This block sets the cache size of the main database to 10
702 # pages. This is done in case the build is configured to omit
703 # "PRAGMA cache_size".
704 puts $f {db eval {SELECT * FROM sqlite_master;}}
705 puts $f {set bt [btree_from_db db]}
706 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +0000707 if {$prngseed} {
708 set seed [expr {$prngseed%10007+1}]
709 # puts seed=$seed
710 puts $f "db eval {SELECT randomblob($seed)}"
711 }
danielk1977933bbd62007-03-17 07:22:42 +0000712
drhf8587402008-01-08 16:03:49 +0000713 if {[string length $tclbody]>0} {
714 puts $f $tclbody
715 }
716 if {[string length $sql]>0} {
717 puts $f "db eval {"
718 puts $f "$sql"
719 puts $f "}"
720 }
danielk1977aca790a2005-01-13 11:07:52 +0000721 close $f
danielk1977aca790a2005-01-13 11:07:52 +0000722 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000723 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000724 } msg]
shanehf2c08822010-07-08 16:30:44 +0000725
726 # Windows/ActiveState TCL returns a slightly different
727 # error message. We map that to the expected message
728 # so that we don't have to change all of the test
729 # cases.
730 if {$::tcl_platform(platform)=="windows"} {
731 if {$msg=="child killed: unknown signal"} {
732 set msg "child process exited abnormally"
733 }
734 }
735
danielk1977aca790a2005-01-13 11:07:52 +0000736 lappend r $msg
737}
738
danielk197732554c12005-01-22 03:39:39 +0000739# Usage: do_ioerr_test <test number> <options...>
740#
741# This proc is used to implement test cases that check that IO errors
742# are correctly handled. The first argument, <test number>, is an integer
743# used to name the tests executed by this proc. Options are as follows:
744#
745# -tclprep TCL script to run to prepare test.
746# -sqlprep SQL script to run to prepare test.
747# -tclbody TCL script to run with IO error simulation.
748# -sqlbody TCL script to run with IO error simulation.
749# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000750# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +0000751# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +0000752# -start Value of 'N' to begin with (default 1)
753#
754# -cksum Boolean. If true, test that the database does
755# not change during the execution of the test case.
756#
757proc do_ioerr_test {testname args} {
758
danielk197732554c12005-01-22 03:39:39 +0000759 set ::ioerropts(-start) 1
760 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000761 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +0000762 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +0000763 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +0000764 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +0000765 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +0000766 array set ::ioerropts $args
767
danielk197747cd39c2008-05-12 12:41:15 +0000768 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
769 # a couple of obscure IO errors that do not return them.
770 set ::ioerropts(-erc) 0
771
danielk197732554c12005-01-22 03:39:39 +0000772 set ::go 1
drh93aed5a2008-01-16 17:46:38 +0000773 #reset_prng_state
774 save_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +0000775 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +0000776 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +0000777 incr ::ioerropts(-count) -1
778 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +0000779
780 # Skip this IO error if it was specified with the "-exclude" option.
781 if {[info exists ::ioerropts(-exclude)]} {
782 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
783 }
danielk1977861f7452008-06-05 11:39:11 +0000784 if {$::ioerropts(-restoreprng)} {
785 restore_prng_state
786 }
danielk197732554c12005-01-22 03:39:39 +0000787
788 # Delete the files test.db and test2.db, then execute the TCL and
789 # SQL (in that order) to prepare for the test case.
790 do_test $testname.$n.1 {
791 set ::sqlite_io_error_pending 0
792 catch {db close}
aswiftaebf4132008-11-21 00:10:35 +0000793 catch {db2 close}
danielk197732554c12005-01-22 03:39:39 +0000794 catch {file delete -force test.db}
795 catch {file delete -force test.db-journal}
796 catch {file delete -force test2.db}
797 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000798 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000799 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000800 if {[info exists ::ioerropts(-tclprep)]} {
801 eval $::ioerropts(-tclprep)
802 }
803 if {[info exists ::ioerropts(-sqlprep)]} {
804 execsql $::ioerropts(-sqlprep)
805 }
806 expr 0
807 } {0}
808
809 # Read the 'checksum' of the database.
810 if {$::ioerropts(-cksum)} {
811 set checksum [cksum]
812 }
drh93aed5a2008-01-16 17:46:38 +0000813
danielk197732554c12005-01-22 03:39:39 +0000814 # Set the Nth IO error to fail.
815 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +0000816 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +0000817 set ::sqlite_io_error_pending $n
818 }] $n
819
820 # Create a single TCL script from the TCL and SQL specified
821 # as the body of the test.
822 set ::ioerrorbody {}
823 if {[info exists ::ioerropts(-tclbody)]} {
824 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
825 }
826 if {[info exists ::ioerropts(-sqlbody)]} {
827 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
828 }
829
830 # Execute the TCL Script created in the above block. If
831 # there are at least N IO operations performed by SQLite as
832 # a result of the script, the Nth will fail.
833 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +0000834 set ::sqlite_io_error_hit 0
835 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +0000836 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +0000837 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +0000838 set rc [sqlite3_errcode $::DB]
839 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +0000840 # If we are in extended result code mode, make sure all of the
841 # IOERRs we get back really do have their extended code values.
842 # If an extended result code is returned, the sqlite3_errcode
843 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
844 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +0000845 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
846 return $rc
847 }
848 } else {
drh5f7b5bf2007-04-19 12:30:54 +0000849 # If we are not in extended result code mode, make sure no
850 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +0000851 if {[regexp {\+\d} $rc]} {
852 return $rc
853 }
854 }
drh93aed5a2008-01-16 17:46:38 +0000855 # The test repeats as long as $::go is non-zero. $::go starts out
856 # as 1. When a test runs to completion without hitting an I/O
857 # error, that means there is no point in continuing with this test
858 # case so set $::go to zero.
859 #
860 if {$::sqlite_io_error_pending>0} {
861 set ::go 0
862 set q 0
863 set ::sqlite_io_error_pending 0
864 } else {
865 set q 1
866 }
867
drhc9ac5ca2005-11-04 22:03:30 +0000868 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +0000869 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
870 set r 1
871 }
danielk1977f2fa8312006-01-24 13:09:33 +0000872 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +0000873
874 # One of two things must have happened. either
875 # 1. We never hit the IO error and the SQL returned OK
876 # 2. An IO error was hit and the SQL failed
877 #
dand41a29a2010-05-06 15:56:28 +0000878 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +0000879 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +0000880 } {1}
881
danielk197780daec62008-05-12 10:57:02 +0000882 set ::sqlite_io_error_hit 0
883 set ::sqlite_io_error_pending 0
884
danielk197752b472a2008-05-05 16:23:55 +0000885 # Check that no page references were leaked. There should be
886 # a single reference if there is still an active transaction,
887 # or zero otherwise.
888 #
danielk197781620542008-06-07 05:19:37 +0000889 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
890 # locks are established on database files (i.e. if the error
891 # occurs while attempting to detect a hot-journal file), then
892 # there may 0 page references and an active transaction according
893 # to [sqlite3_get_autocommit].
894 #
danielk19774abd5442008-05-05 15:26:50 +0000895 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +0000896 do_test $testname.$n.4 {
897 set bt [btree_from_db db]
898 db_enter db
899 array set stats [btree_pager_stats $bt]
900 db_leave db
danielk197781620542008-06-07 05:19:37 +0000901 set nRef $stats(ref)
902 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
903 } {1}
danielk19774abd5442008-05-05 15:26:50 +0000904 }
905
danielk197752b472a2008-05-05 16:23:55 +0000906 # If there is an open database handle and no open transaction,
907 # and the pager is not running in exclusive-locking mode,
908 # check that the pager is in "unlocked" state. Theoretically,
909 # if a call to xUnlock() failed due to an IO error the underlying
910 # file may still be locked.
911 #
912 ifcapable pragma {
913 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +0000914 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +0000915 && [db one {pragma locking_mode}] eq "normal"
916 && [sqlite3_get_autocommit db]
917 } {
918 do_test $testname.$n.5 {
919 set bt [btree_from_db db]
920 db_enter db
921 array set stats [btree_pager_stats $bt]
922 db_leave db
923 set stats(state)
924 } 0
925 }
926 }
927
danielk197732554c12005-01-22 03:39:39 +0000928 # If an IO error occured, then the checksum of the database should
929 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +0000930 #
drh1aa5af12008-03-07 19:51:14 +0000931 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +0000932 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +0000933 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +0000934 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +0000935 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +0000936 cksum
937 } $checksum
938 }
939
drh1aa5af12008-03-07 19:51:14 +0000940 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +0000941 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +0000942 if {[info exists ::ioerropts(-cleanup)]} {
943 catch $::ioerropts(-cleanup)
944 }
danielk197732554c12005-01-22 03:39:39 +0000945 }
946 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +0000947 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +0000948 unset ::ioerropts
949}
950
drh93aed5a2008-01-16 17:46:38 +0000951# Return a checksum based on the contents of the main database associated
952# with connection $db
danielk197732554c12005-01-22 03:39:39 +0000953#
954proc cksum {{db db}} {
955 set txt [$db eval {
956 SELECT name, type, sql FROM sqlite_master order by name
957 }]\n
958 foreach tbl [$db eval {
959 SELECT name FROM sqlite_master WHERE type='table' order by name
960 }] {
961 append txt [$db eval "SELECT * FROM $tbl"]\n
962 }
963 foreach prag {default_synchronous default_cache_size} {
964 append txt $prag-[$db eval "PRAGMA $prag"]\n
965 }
966 set cksum [string length $txt]-[md5 $txt]
967 # puts $cksum-[file size test.db]
968 return $cksum
969}
970
drh93aed5a2008-01-16 17:46:38 +0000971# Generate a checksum based on the contents of the main and temp tables
972# database $db. If the checksum of two databases is the same, and the
973# integrity-check passes for both, the two databases are identical.
974#
drh1db639c2008-01-17 02:36:28 +0000975proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +0000976 set ret [list]
977 ifcapable tempdb {
978 set sql {
979 SELECT name FROM sqlite_master WHERE type = 'table' UNION
980 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
981 SELECT 'sqlite_master' UNION
982 SELECT 'sqlite_temp_master' ORDER BY 1
983 }
984 } else {
985 set sql {
986 SELECT name FROM sqlite_master WHERE type = 'table' UNION
987 SELECT 'sqlite_master' ORDER BY 1
988 }
989 }
990 set tbllist [$db eval $sql]
991 set txt {}
992 foreach tbl $tbllist {
993 append txt [$db eval "SELECT * FROM $tbl"]
994 }
995 foreach prag {default_cache_size} {
996 append txt $prag-[$db eval "PRAGMA $prag"]\n
997 }
998 # puts txt=$txt
999 return [md5 $txt]
1000}
1001
drhdc2c4912009-02-04 22:46:47 +00001002# Generate a checksum based on the contents of a single database with
1003# a database connection. The name of the database is $dbname.
1004# Examples of $dbname are "temp" or "main".
1005#
1006proc dbcksum {db dbname} {
1007 if {$dbname=="temp"} {
1008 set master sqlite_temp_master
1009 } else {
1010 set master $dbname.sqlite_master
1011 }
1012 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
1013 set txt [$db eval "SELECT * FROM $master"]\n
1014 foreach tab $alltab {
1015 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
1016 }
1017 return [md5 $txt]
1018}
1019
danielk197735754ac2008-03-21 17:29:37 +00001020proc memdebug_log_sql {{filename mallocs.sql}} {
1021
danielk19776f332c12008-03-21 14:22:44 +00001022 set data [sqlite3_memdebug_log dump]
1023 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +00001024 if {$nFrame < 0} { return "" }
1025
danielk197735754ac2008-03-21 17:29:37 +00001026 set database temp
1027
danielk19776ab3a2e2009-02-19 14:39:25 +00001028 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +00001029
1030 set sql ""
1031 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +00001032 set nCall [lindex $e 0]
1033 set nByte [lindex $e 1]
1034 set lStack [lrange $e 2 end]
1035 append sql "INSERT INTO ${database}.malloc VALUES"
1036 append sql "('test', $nCall, $nByte, '$lStack');\n"
1037 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +00001038 set frames($f) 1
1039 }
1040 }
1041
1042 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +00001043 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +00001044
1045 foreach f [array names frames] {
1046 set addr [format %x $f]
1047 set cmd "addr2line -e [info nameofexec] $addr"
1048 set line [eval exec $cmd]
1049 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00001050
1051 set file [lindex [split $line :] 0]
1052 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00001053 }
1054
danielk197735754ac2008-03-21 17:29:37 +00001055 foreach f [array names files] {
1056 set contents ""
1057 catch {
1058 set fd [open $f]
1059 set contents [read $fd]
1060 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001061 }
danielk197735754ac2008-03-21 17:29:37 +00001062 set contents [string map {' ''} $contents]
1063 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00001064 }
danielk19776f332c12008-03-21 14:22:44 +00001065
danielk197735754ac2008-03-21 17:29:37 +00001066 set fd [open $filename w]
1067 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
1068 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001069}
drh93aed5a2008-01-16 17:46:38 +00001070
danielk197732554c12005-01-22 03:39:39 +00001071# Copy file $from into $to. This is used because some versions of
1072# TCL for windows (notably the 8.4.1 binary package shipped with the
1073# current mingw release) have a broken "file copy" command.
1074#
1075proc copy_file {from to} {
1076 if {$::tcl_platform(platform)=="unix"} {
1077 file copy -force $from $to
1078 } else {
1079 set f [open $from]
1080 fconfigure $f -translation binary
1081 set t [open $to w]
1082 fconfigure $t -translation binary
1083 puts -nonewline $t [read $f [file size $from]]
1084 close $t
1085 close $f
1086 }
1087}
1088
danf5894502009-10-07 18:41:19 +00001089# Drop all tables in database [db]
1090proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00001091 ifcapable trigger&&foreignkey {
1092 set pk [$db one "PRAGMA foreign_keys"]
1093 $db eval "PRAGMA foreign_keys = OFF"
1094 }
drh9a6ffc82010-02-15 18:03:20 +00001095 foreach {idx name file} [db eval {PRAGMA database_list}] {
1096 if {$idx==1} {
1097 set master sqlite_temp_master
1098 } else {
1099 set master $name.sqlite_master
1100 }
1101 foreach {t type} [$db eval "
1102 SELECT name, type FROM $master
1103 WHERE type IN('table', 'view') AND name NOT like 'sqlite_%'
1104 "] {
1105 $db eval "DROP $type $t"
1106 }
danf5894502009-10-07 18:41:19 +00001107 }
shaneh4e7b32f2009-12-17 22:12:51 +00001108 ifcapable trigger&&foreignkey {
1109 $db eval "PRAGMA foreign_keys = $pk"
1110 }
danf5894502009-10-07 18:41:19 +00001111}
1112
dan71cb5182010-04-26 12:39:03 +00001113#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00001114# If a test script is executed with global variable $::G(perm:name) set to
1115# "wal", then the tests are run in WAL mode. Otherwise, they should be run
1116# in rollback mode. The following Tcl procs are used to make this less
1117# intrusive:
dan71cb5182010-04-26 12:39:03 +00001118#
1119# wal_set_journal_mode ?DB?
1120#
1121# If running a WAL test, execute "PRAGMA journal_mode = wal" using
1122# connection handle DB. Otherwise, this command is a no-op.
1123#
1124# wal_check_journal_mode TESTNAME ?DB?
1125#
1126# If running a WAL test, execute a tests case that fails if the main
1127# database for connection handle DB is not currently a WAL database.
1128# Otherwise (if not running a WAL permutation) this is a no-op.
1129#
1130# wal_is_wal_mode
1131#
1132# Returns true if this test should be run in WAL mode. False otherwise.
1133#
1134proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00001135 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00001136}
1137proc wal_set_journal_mode {{db db}} {
1138 if { [wal_is_wal_mode] } {
1139 $db eval "PRAGMA journal_mode = WAL"
1140 }
1141}
1142proc wal_check_journal_mode {testname {db db}} {
1143 if { [wal_is_wal_mode] } {
1144 $db eval { SELECT * FROM sqlite_master }
1145 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
1146 }
1147}
1148
dan430e74c2010-06-07 17:47:26 +00001149proc permutation {} {
1150 set perm ""
1151 catch {set perm $::G(perm:name)}
1152 set perm
1153}
dancb79e512010-08-06 13:50:07 +00001154proc presql {} {
1155 set presql ""
1156 catch {set presql $::G(perm:presql)}
1157 set presql
1158}
dan430e74c2010-06-07 17:47:26 +00001159
danc1a60c52010-06-07 14:28:16 +00001160#-------------------------------------------------------------------------
1161#
1162proc slave_test_script {script} {
1163
1164 # Create the interpreter used to run the test script.
1165 interp create tinterp
1166
1167 # Populate some global variables that tester.tcl expects to see.
1168 foreach {var value} [list \
1169 ::argv0 $::argv0 \
1170 ::argv {} \
1171 ::SLAVE 1 \
1172 ] {
1173 interp eval tinterp [list set $var $value]
1174 }
1175
1176 # The alias used to access the global test counters.
1177 tinterp alias set_test_counter set_test_counter
1178
1179 # Set up the ::cmdlinearg array in the slave.
1180 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
1181
dan430e74c2010-06-07 17:47:26 +00001182 # Set up the ::G array in the slave.
1183 interp eval tinterp [list array set ::G [array get ::G]]
1184
danc1a60c52010-06-07 14:28:16 +00001185 # Load the various test interfaces implemented in C.
1186 load_testfixture_extensions tinterp
1187
1188 # Run the test script.
1189 interp eval tinterp $script
1190
danea5542d2010-07-06 11:26:15 +00001191 # Check if the interpreter call [run_thread_tests]
1192 if { [interp eval tinterp {info exists ::run_thread_tests_called}] } {
1193 set ::run_thread_tests_called 1
1194 }
1195
danc1a60c52010-06-07 14:28:16 +00001196 # Delete the interpreter used to run the test script.
1197 interp delete tinterp
1198}
1199
dan430e74c2010-06-07 17:47:26 +00001200proc slave_test_file {zFile} {
dan0626dfc2010-06-15 06:56:37 +00001201 set tail [file tail $zFile]
dan430e74c2010-06-07 17:47:26 +00001202
dan92d516a2010-07-05 14:54:48 +00001203 # Remember the value of the shared-cache setting. So that it is possible
1204 # to check afterwards that it was not modified by the test script.
1205 #
1206 ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] }
dan0626dfc2010-06-15 06:56:37 +00001207
dan92d516a2010-07-05 14:54:48 +00001208 # Run the test script in a slave interpreter.
1209 #
danea5542d2010-07-06 11:26:15 +00001210 unset -nocomplain ::run_thread_tests_called
dan0626dfc2010-06-15 06:56:37 +00001211 reset_prng_state
1212 set ::sqlite_open_file_count 0
1213 set time [time { slave_test_script [list source $zFile] }]
1214 set ms [expr [lindex $time 0] / 1000]
1215
dan92d516a2010-07-05 14:54:48 +00001216 # Test that all files opened by the test script were closed. Omit this
1217 # if the test script has "thread" in its name. The open file counter
1218 # is not thread-safe.
dan0626dfc2010-06-15 06:56:37 +00001219 #
danea5542d2010-07-06 11:26:15 +00001220 if {[info exists ::run_thread_tests_called]==0} {
dan92d516a2010-07-05 14:54:48 +00001221 do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0}
1222 }
dan430e74c2010-06-07 17:47:26 +00001223 set ::sqlite_open_file_count 0
1224
dan0626dfc2010-06-15 06:56:37 +00001225 # Test that the global "shared-cache" setting was not altered by
1226 # the test script.
1227 #
1228 ifcapable shared_cache {
1229 set res [expr {[sqlite3_enable_shared_cache] == $scs}]
1230 do_test ${tail}-sharedcachesetting [list set {} $res] 1
1231 }
dan430e74c2010-06-07 17:47:26 +00001232
dan92d516a2010-07-05 14:54:48 +00001233 # Add some info to the output.
1234 #
dan0626dfc2010-06-15 06:56:37 +00001235 puts "Time: $tail $ms ms"
dan430e74c2010-06-07 17:47:26 +00001236 show_memstats
danc1a60c52010-06-07 14:28:16 +00001237}
1238
dan59257dc2010-08-04 11:34:31 +00001239# Open a new connection on database test.db and execute the SQL script
1240# supplied as an argument. Before returning, close the new conection and
1241# restore the 4 byte fields starting at header offsets 28, 92 and 96
1242# to the values they held before the SQL was executed. This simulates
1243# a write by a pre-3.7.0 client.
1244#
1245proc sql36231 {sql} {
1246 set B [hexio_read test.db 92 8]
1247 set A [hexio_read test.db 28 4]
1248 sqlite3 db36231 test.db
1249 catch { db36231 func a_string a_string }
1250 execsql $sql db36231
1251 db36231 close
1252 hexio_write test.db 28 $A
1253 hexio_write test.db 92 $B
1254 return ""
1255}
danf5894502009-10-07 18:41:19 +00001256
danielk197745901d62004-11-10 15:27:38 +00001257# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
1258# to non-zero, then set the global variable $AUTOVACUUM to 1.
1259set AUTOVACUUM $sqlite_options(default_autovacuum)
danielk1977ee8b7992009-03-26 17:13:06 +00001260
1261source $testdir/thread_common.tcl