blob: 1e12835f255178e0ee9c210933ed098c028fd4fc [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
109 if {[info exists ::G(perm:presql)]} {
110 [lindex $args 0] eval $::G(perm:presql)
111 }
dan3ccd20a2010-06-09 19:01:02 +0000112
113 set res
dan430e74c2010-06-07 17:47:26 +0000114 } else {
115 # This command is not opening a new database connection. Pass the
116 # arguments through to the C implemenation as the are.
117 #
118 uplevel 1 sqlite_orig $args
drh9eb9e262004-02-11 02:18:05 +0000119 }
drh9eb9e262004-02-11 02:18:05 +0000120 }
121}
122
dan68928b62010-06-22 13:46:43 +0000123# This command should be called after loading tester.tcl from within
124# all test scripts that are incompatible with encryption codecs.
125#
126proc do_not_use_codec {} {
127 set ::do_not_use_codec 1
128 reset_db
129}
130
dan430e74c2010-06-07 17:47:26 +0000131# The following block only runs the first time this file is sourced. It
132# does not run in slave interpreters (since the ::cmdlinearg array is
133# populated before the test script is run in slave interpreters).
drhbec2bf42000-05-29 23:48:22 +0000134#
danc1a60c52010-06-07 14:28:16 +0000135if {[info exists cmdlinearg]==0} {
136
137 # Parse any options specified in the $argv array. This script accepts the
138 # following options:
139 #
140 # --pause
141 # --soft-heap-limit=NN
142 # --maxerror=NN
143 # --malloctrace=N
144 # --backtrace=N
145 # --binarylog=N
dan0626dfc2010-06-15 06:56:37 +0000146 # --soak=N
danc1a60c52010-06-07 14:28:16 +0000147 #
148 set cmdlinearg(soft-heap-limit) 0
149 set cmdlinearg(maxerror) 1000
150 set cmdlinearg(malloctrace) 0
151 set cmdlinearg(backtrace) 10
152 set cmdlinearg(binarylog) 0
dan0626dfc2010-06-15 06:56:37 +0000153 set cmdlinearg(soak) 0
danc1a60c52010-06-07 14:28:16 +0000154
155 set leftover [list]
156 foreach a $argv {
157 switch -regexp -- $a {
158 {^-+pause$} {
159 # Wait for user input before continuing. This is to give the user an
160 # opportunity to connect profiling tools to the process.
161 puts -nonewline "Press RETURN to begin..."
162 flush stdout
163 gets stdin
164 }
165 {^-+soft-heap-limit=.+$} {
166 foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break
167 }
168 {^-+maxerror=.+$} {
169 foreach {dummy cmdlinearg(maxerror)} [split $a =] break
170 }
171 {^-+malloctrace=.+$} {
172 foreach {dummy cmdlinearg(malloctrace)} [split $a =] break
173 if {$cmdlinearg(malloctrace)} {
174 sqlite3_memdebug_log start
175 }
176 }
177 {^-+backtrace=.+$} {
178 foreach {dummy cmdlinearg(backtrace)} [split $a =] break
dan0626dfc2010-06-15 06:56:37 +0000179 sqlite3_memdebug_backtrace $value
danc1a60c52010-06-07 14:28:16 +0000180 }
danc1a60c52010-06-07 14:28:16 +0000181 {^-+binarylog=.+$} {
182 foreach {dummy cmdlinearg(binarylog)} [split $a =] break
183 }
dan0626dfc2010-06-15 06:56:37 +0000184 {^-+soak=.+$} {
185 foreach {dummy cmdlinearg(soak)} [split $a =] break
186 set ::G(issoak) $cmdlinearg(soak)
187 }
danc1a60c52010-06-07 14:28:16 +0000188 default {
189 lappend leftover $a
190 }
191 }
192 }
193 set argv $leftover
194
dan430e74c2010-06-07 17:47:26 +0000195 # Install the malloc layer used to inject OOM errors. And the 'automatic'
196 # extensions. This only needs to be done once for the process.
197 #
danielk1977f7b9d662008-06-23 18:49:43 +0000198 sqlite3_shutdown
199 install_malloc_faultsim 1
200 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000201 autoinstall_test_functions
danc1a60c52010-06-07 14:28:16 +0000202
dan430e74c2010-06-07 17:47:26 +0000203 # If the --binarylog option was specified, create the logging VFS. This
204 # call installs the new VFS as the default for all SQLite connections.
205 #
danc1a60c52010-06-07 14:28:16 +0000206 if {$cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000207 vfslog new binarylog {} vfslog.bin
danc1a60c52010-06-07 14:28:16 +0000208 }
209
210 # Set the backtrace depth, if malloc tracing is enabled.
211 #
212 if {$cmdlinearg(malloctrace)} {
213 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danielk1977185eac92008-07-12 15:55:54 +0000214 }
danielk1977f7b9d662008-06-23 18:49:43 +0000215}
danielk197703ba3fa2009-01-09 10:49:14 +0000216
danc1a60c52010-06-07 14:28:16 +0000217# Update the soft-heap-limit each time this script is run. In that
218# way if an individual test file changes the soft-heap-limit, it
219# will be reset at the start of the next test file.
220#
221sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
222
223# Create a test database
224#
danielk197703ba3fa2009-01-09 10:49:14 +0000225proc reset_db {} {
226 catch {db close}
227 file delete -force test.db
228 file delete -force test.db-journal
dand3f8f942010-04-13 11:35:01 +0000229 file delete -force test.db-wal
danielk197703ba3fa2009-01-09 10:49:14 +0000230 sqlite3 db ./test.db
231 set ::DB [sqlite3_connection_pointer db]
232 if {[info exists ::SETUP_SQL]} {
233 db eval $::SETUP_SQL
234 }
drhcd61c282002-03-06 22:01:34 +0000235}
danielk197703ba3fa2009-01-09 10:49:14 +0000236reset_db
drhbec2bf42000-05-29 23:48:22 +0000237
238# Abort early if this script has been run before.
239#
danc1a60c52010-06-07 14:28:16 +0000240if {[info exists TC(count)]} return
drhbec2bf42000-05-29 23:48:22 +0000241
danc1a60c52010-06-07 14:28:16 +0000242# Initialize the test counters and set up commands to access them.
243# Or, if this is a slave interpreter, set up aliases to write the
244# counters in the parent interpreter.
drhbec2bf42000-05-29 23:48:22 +0000245#
danc1a60c52010-06-07 14:28:16 +0000246if {0==[info exists ::SLAVE]} {
247 set TC(errors) 0
248 set TC(count) 0
249 set TC(fail_list) [list]
250 set TC(omit_list) [list]
251
252 proc set_test_counter {counter args} {
253 if {[llength $args]} {
254 set ::TC($counter) [lindex $args 0]
255 }
256 set ::TC($counter)
257 }
drhb62c3352006-11-23 09:39:16 +0000258}
drh348784e2000-05-29 20:41:49 +0000259
drh521cc842008-04-15 02:36:33 +0000260# Record the fact that a sequence of tests were omitted.
261#
262proc omit_test {name reason} {
danc1a60c52010-06-07 14:28:16 +0000263 set omitList [set_test_counter omit_list]
drh521cc842008-04-15 02:36:33 +0000264 lappend omitList [list $name $reason]
danc1a60c52010-06-07 14:28:16 +0000265 set_test_counter omit_list $omitList
drh521cc842008-04-15 02:36:33 +0000266}
267
danc1a60c52010-06-07 14:28:16 +0000268# Record the fact that a test failed.
269#
270proc fail_test {name} {
271 set f [set_test_counter fail_list]
272 lappend f $name
273 set_test_counter fail_list $f
274 set_test_counter errors [expr [set_test_counter errors] + 1]
275
276 set nFail [set_test_counter errors]
277 if {$nFail>=$::cmdlinearg(maxerror)} {
278 puts "*** Giving up..."
279 finalize_testing
280 }
281}
282
283# Increment the number of tests run
284#
285proc incr_ntest {} {
286 set_test_counter count [expr [set_test_counter count] + 1]
287}
288
289
drh348784e2000-05-29 20:41:49 +0000290# Invoke the do_test procedure to run a single test
291#
292proc do_test {name cmd expected} {
danc1a60c52010-06-07 14:28:16 +0000293
294 global argv cmdlinearg
295
drh4a50aac2007-08-23 02:47:53 +0000296 sqlite3_memdebug_settitle $name
danc1a60c52010-06-07 14:28:16 +0000297
dan92d516a2010-07-05 14:54:48 +0000298# if {[llength $argv]==0} {
299# set go 1
300# } else {
301# set go 0
302# foreach pattern $argv {
303# if {[string match $pattern $name]} {
304# set go 1
305# break
306# }
307# }
308# }
dan430e74c2010-06-07 17:47:26 +0000309
dand506de02010-07-03 13:59:01 +0000310 if {[info exists ::G(perm:prefix)]} {
311 set name "$::G(perm:prefix)$name"
dan430e74c2010-06-07 17:47:26 +0000312 }
313
danc1a60c52010-06-07 14:28:16 +0000314 incr_ntest
drh5edc3122001-09-13 21:53:09 +0000315 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000316 flush stdout
317 if {[catch {uplevel #0 "$cmd;\n"} result]} {
318 puts "\nError: $result"
danc1a60c52010-06-07 14:28:16 +0000319 fail_test $name
drh348784e2000-05-29 20:41:49 +0000320 } elseif {[string compare $result $expected]} {
321 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
danc1a60c52010-06-07 14:28:16 +0000322 fail_test $name
drh348784e2000-05-29 20:41:49 +0000323 } else {
324 puts " Ok"
325 }
drh6558db82007-04-13 03:23:21 +0000326 flush stdout
drh348784e2000-05-29 20:41:49 +0000327}
dan153eda02010-06-21 07:45:47 +0000328
329proc do_execsql_test {testname sql result} {
330 uplevel do_test $testname [list "execsql {$sql}"] [list $result]
331}
332proc do_catchsql_test {testname sql result} {
333 uplevel do_test $testname [list "catchsql {$sql}"] [list $result]
334}
335
drh348784e2000-05-29 20:41:49 +0000336
drhb62c3352006-11-23 09:39:16 +0000337# Run an SQL script.
338# Return the number of microseconds per statement.
339#
drh3590f152006-11-23 21:09:10 +0000340proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000341 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000342 flush stdout
343 set speed [time {sqlite3_exec_nr db $sql}]
344 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000345 if {$tm == 0} {
346 set rate [format %20s "many"]
347 } else {
348 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
349 }
drh3590f152006-11-23 21:09:10 +0000350 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000351 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000352 global total_time
353 set total_time [expr {$total_time+$tm}]
354}
drh45c236d2008-03-22 01:08:00 +0000355proc speed_trial_tcl {name numstmt units script} {
356 puts -nonewline [format {%-21.21s } $name...]
357 flush stdout
358 set speed [time {eval $script}]
359 set tm [lindex $speed 0]
360 if {$tm == 0} {
361 set rate [format %20s "many"]
362 } else {
363 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
364 }
365 set u2 $units/s
366 puts [format {%12d uS %s %s} $tm $rate $u2]
367 global total_time
368 set total_time [expr {$total_time+$tm}]
369}
drhdd924312007-03-31 22:34:16 +0000370proc speed_trial_init {name} {
371 global total_time
372 set total_time 0
drhbb810a92010-07-03 12:00:53 +0000373 sqlite3 versdb :memory:
374 set vers [versdb one {SELECT sqlite_source_id()}]
375 versdb close
376 puts "SQLite $vers"
drhdd924312007-03-31 22:34:16 +0000377}
378proc speed_trial_summary {name} {
379 global total_time
380 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
drhb62c3352006-11-23 09:39:16 +0000381}
382
drh348784e2000-05-29 20:41:49 +0000383# Run this routine last
384#
385proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +0000386 catch {db close}
387 catch {db2 close}
388 catch {db3 close}
389 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +0000390}
391proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +0000392 global sqlite_open_file_count
393
394 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +0000395
drh6e142f52000-06-08 13:36:40 +0000396 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000397 catch {db2 close}
398 catch {db3 close}
399
drh9bc54492007-10-23 14:49:59 +0000400 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000401 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000402 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000403 db close
drh984bfaa2008-03-19 16:08:53 +0000404 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +0000405
drh3a7fb7c2007-08-10 16:41:08 +0000406 sqlite3_soft_heap_limit 0
danc1a60c52010-06-07 14:28:16 +0000407 set nTest [incr_ntest]
408 set nErr [set_test_counter errors]
409
drh348784e2000-05-29 20:41:49 +0000410 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000411 if {$nErr>0} {
danc1a60c52010-06-07 14:28:16 +0000412 puts "Failures on these tests: [set_test_counter fail_list]"
drhf3a65f72007-08-22 20:18:21 +0000413 }
danielk1977ee8b7992009-03-26 17:13:06 +0000414 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +0000415 if {[llength $omitList]>0} {
416 puts "Omitted test cases:"
417 set prec {}
418 foreach {rec} [lsort $omitList] {
419 if {$rec==$prec} continue
420 set prec $rec
421 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
422 }
423 }
drh80788d82006-09-02 14:50:23 +0000424 if {$nErr>0 && ![working_64bit_int]} {
425 puts "******************************************************************"
426 puts "N.B.: The version of TCL that you used to build this test harness"
427 puts "is defective in that it does not support 64-bit integers. Some or"
428 puts "all of the test failures above might be a result from this defect"
429 puts "in your TCL build."
430 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000431 }
danc1a60c52010-06-07 14:28:16 +0000432 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000433 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +0000434 }
drh94e92032003-02-16 22:21:32 +0000435 if {$sqlite_open_file_count} {
436 puts "$sqlite_open_file_count files were left open"
437 incr nErr
438 }
drhf3a65f72007-08-22 20:18:21 +0000439 if {[sqlite3_memory_used]>0} {
440 puts "Unfreed memory: [sqlite3_memory_used] bytes"
441 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000442 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000443 puts "Writing unfreed memory log to \"./memleak.txt\""
444 sqlite3_memdebug_dump ./memleak.txt
445 }
drhf3a65f72007-08-22 20:18:21 +0000446 } else {
447 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000448 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000449 sqlite3_memdebug_dump ./memusage.txt
450 }
drhf3a65f72007-08-22 20:18:21 +0000451 }
drhf7141992008-06-19 00:16:08 +0000452 show_memstats
drh91fd4d42008-01-19 20:11:25 +0000453 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
454 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000455 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
456 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
457 }
danc1a60c52010-06-07 14:28:16 +0000458 if {$::cmdlinearg(malloctrace)} {
danielk197735754ac2008-03-21 17:29:37 +0000459 puts "Writing mallocs.sql..."
460 memdebug_log_sql
461 sqlite3_memdebug_log stop
462 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000463
464 if {[sqlite3_memory_used]>0} {
465 puts "Writing leaks.sql..."
466 sqlite3_memdebug_log sync
467 memdebug_log_sql leaks.sql
468 }
danielk197735754ac2008-03-21 17:29:37 +0000469 }
drhd9910fe2006-10-04 11:55:49 +0000470 foreach f [glob -nocomplain test.db-*-journal] {
471 file delete -force $f
472 }
473 foreach f [glob -nocomplain test.db-mj*] {
474 file delete -force $f
475 }
drhdb25e382001-03-15 18:21:22 +0000476 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000477}
478
drhf7141992008-06-19 00:16:08 +0000479# Display memory statistics for analysis and debugging purposes.
480#
481proc show_memstats {} {
482 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +0000483 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
484 set val [format {now %10d max %10d max-size %10d} \
485 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000486 puts "Memory used: $val"
487 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +0000488 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
489 set val [format {now %10d max %10d max-size %10d} \
490 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000491 puts "Page-cache used: $val"
492 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
493 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
494 puts "Page-cache overflow: $val"
495 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
496 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
497 puts "Scratch memory used: $val"
498 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +0000499 set y [sqlite3_status SQLITE_STATUS_SCRATCH_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 "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +0000503 ifcapable yytrackmaxstackdepth {
504 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
505 set val [format { max %10d} [lindex $x 2]]
506 puts "Parser stack depth: $val"
507 }
drhf7141992008-06-19 00:16:08 +0000508}
509
drh348784e2000-05-29 20:41:49 +0000510# A procedure to execute SQL
511#
drhc4a3c772001-04-04 11:48:57 +0000512proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000513 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000514 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000515}
drh3aadb2e2000-05-31 17:59:25 +0000516
drhadbca9c2001-09-27 15:11:53 +0000517# Execute SQL and catch exceptions.
518#
519proc catchsql {sql {db db}} {
520 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +0000521 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +0000522 lappend r $msg
523 return $r
524}
525
drh04096482001-11-09 22:41:44 +0000526# Do an VDBE code dump on the SQL given
527#
528proc explain {sql {db db}} {
529 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000530 puts "addr opcode p1 p2 p3 p4 p5 #"
531 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000532 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000533 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
534 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
535 ]
drh04096482001-11-09 22:41:44 +0000536 }
537}
538
drhdafc0ce2008-04-17 19:14:02 +0000539# Show the VDBE program for an SQL statement but omit the Trace
540# opcode at the beginning. This procedure can be used to prove
541# that different SQL statements generate exactly the same VDBE code.
542#
543proc explain_no_trace {sql} {
544 set tr [db eval "EXPLAIN $sql"]
545 return [lrange $tr 7 end]
546}
547
drh3aadb2e2000-05-31 17:59:25 +0000548# Another procedure to execute SQL. This one includes the field
549# names in the returned list.
550#
551proc execsql2 {sql} {
552 set result {}
553 db eval $sql data {
554 foreach f $data(*) {
555 lappend result $f $data($f)
556 }
557 }
558 return $result
559}
drh17a68932001-01-31 13:28:08 +0000560
drhdde85d92003-03-01 19:45:34 +0000561# Use the non-callback API to execute multiple SQL statements
562#
563proc stepsql {dbptr sql} {
564 set sql [string trim $sql]
565 set r 0
566 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000567 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000568 return [list 1 $vm]
569 }
570 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000571# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
572# foreach v $VAL {lappend r $v}
573# }
574 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
575 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
576 lappend r [sqlite3_column_text $vm $i]
577 }
drhdde85d92003-03-01 19:45:34 +0000578 }
danielk1977106bb232004-05-21 10:08:53 +0000579 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000580 return [list 1 $errmsg]
581 }
582 }
583 return $r
584}
585
drh17a68932001-01-31 13:28:08 +0000586# Delete a file or directory
587#
588proc forcedelete {filename} {
589 if {[catch {file delete -force $filename}]} {
590 exec rm -rf $filename
591 }
592}
drh21504322002-06-25 13:16:02 +0000593
594# Do an integrity check of the entire database
595#
danielk197704103022009-02-03 16:51:24 +0000596proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +0000597 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +0000598 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +0000599 }
600}
601
danielk1977db4e8862008-01-16 08:24:46 +0000602proc fix_ifcapable_expr {expr} {
603 set ret ""
604 set state 0
605 for {set i 0} {$i < [string length $expr]} {incr i} {
606 set char [string range $expr $i $i]
607 set newstate [expr {[string is alnum $char] || $char eq "_"}]
608 if {$newstate && !$state} {
609 append ret {$::sqlite_options(}
610 }
611 if {!$newstate && $state} {
612 append ret )
613 }
614 append ret $char
615 set state $newstate
616 }
617 if {$state} {append ret )}
618 return $ret
619}
620
drh27d258a2004-10-30 20:23:09 +0000621# Evaluate a boolean expression of capabilities. If true, execute the
622# code. Omit the code if false.
623#
danielk19773e8c37e2005-01-21 03:12:14 +0000624proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +0000625 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
626 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +0000627 if ($e2) {
628 set c [catch {uplevel 1 $code} r]
629 } else {
630 set c [catch {uplevel 1 $elsecode} r]
631 }
632 return -code $c $r
drh21504322002-06-25 13:16:02 +0000633}
danielk197745901d62004-11-10 15:27:38 +0000634
danielk1977aca790a2005-01-13 11:07:52 +0000635# This proc execs a seperate process that crashes midway through executing
636# the SQL script $sql on database test.db.
637#
638# The crash occurs during a sync() of file $crashfile. When the crash
639# occurs a random subset of all unsynced writes made by the process are
640# written into the files on disk. Argument $crashdelay indicates the
641# number of file syncs to wait before crashing.
642#
643# The return value is a list of two elements. The first element is a
644# boolean, indicating whether or not the process actually crashed or
645# reported some other error. The second element in the returned list is the
646# error message. This is "child process exited abnormally" if the crash
647# occured.
648#
danielk1977f8940ae2007-08-23 11:07:10 +0000649# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +0000650#
651proc crashsql {args} {
danielk1977aca790a2005-01-13 11:07:52 +0000652 if {$::tcl_platform(platform)!="unix"} {
653 error "crashsql should only be used on unix"
654 }
danielk197759a33f92007-03-17 10:26:59 +0000655
656 set blocksize ""
657 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +0000658 set prngseed 0
drhf8587402008-01-08 16:03:49 +0000659 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +0000660 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +0000661 set dc ""
danielk197759a33f92007-03-17 10:26:59 +0000662 set sql [lindex $args end]
663
664 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
665 set z [lindex $args $ii]
666 set n [string length $z]
667 set z2 [lindex $args [expr $ii+1]]
668
669 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +0000670 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +0000671 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +0000672 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000673 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +0000674 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +0000675 else { error "Unrecognized option: $z" }
676 }
677
678 if {$crashfile eq ""} {
679 error "Compulsory option -file missing"
680 }
681
danielk1977aca790a2005-01-13 11:07:52 +0000682 set cfile [file join [pwd] $crashfile]
683
684 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +0000685 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +0000686 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +0000687 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +0000688 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +0000689
690 # This block sets the cache size of the main database to 10
691 # pages. This is done in case the build is configured to omit
692 # "PRAGMA cache_size".
693 puts $f {db eval {SELECT * FROM sqlite_master;}}
694 puts $f {set bt [btree_from_db db]}
695 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +0000696 if {$prngseed} {
697 set seed [expr {$prngseed%10007+1}]
698 # puts seed=$seed
699 puts $f "db eval {SELECT randomblob($seed)}"
700 }
danielk1977933bbd62007-03-17 07:22:42 +0000701
drhf8587402008-01-08 16:03:49 +0000702 if {[string length $tclbody]>0} {
703 puts $f $tclbody
704 }
705 if {[string length $sql]>0} {
706 puts $f "db eval {"
707 puts $f "$sql"
708 puts $f "}"
709 }
danielk1977aca790a2005-01-13 11:07:52 +0000710 close $f
711
712 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000713 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000714 } msg]
715 lappend r $msg
716}
717
danielk197732554c12005-01-22 03:39:39 +0000718# Usage: do_ioerr_test <test number> <options...>
719#
720# This proc is used to implement test cases that check that IO errors
721# are correctly handled. The first argument, <test number>, is an integer
722# used to name the tests executed by this proc. Options are as follows:
723#
724# -tclprep TCL script to run to prepare test.
725# -sqlprep SQL script to run to prepare test.
726# -tclbody TCL script to run with IO error simulation.
727# -sqlbody TCL script to run with IO error simulation.
728# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000729# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +0000730# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +0000731# -start Value of 'N' to begin with (default 1)
732#
733# -cksum Boolean. If true, test that the database does
734# not change during the execution of the test case.
735#
736proc do_ioerr_test {testname args} {
737
danielk197732554c12005-01-22 03:39:39 +0000738 set ::ioerropts(-start) 1
739 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000740 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +0000741 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +0000742 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +0000743 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +0000744 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +0000745 array set ::ioerropts $args
746
danielk197747cd39c2008-05-12 12:41:15 +0000747 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
748 # a couple of obscure IO errors that do not return them.
749 set ::ioerropts(-erc) 0
750
danielk197732554c12005-01-22 03:39:39 +0000751 set ::go 1
drh93aed5a2008-01-16 17:46:38 +0000752 #reset_prng_state
753 save_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +0000754 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +0000755 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +0000756 incr ::ioerropts(-count) -1
757 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +0000758
759 # Skip this IO error if it was specified with the "-exclude" option.
760 if {[info exists ::ioerropts(-exclude)]} {
761 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
762 }
danielk1977861f7452008-06-05 11:39:11 +0000763 if {$::ioerropts(-restoreprng)} {
764 restore_prng_state
765 }
danielk197732554c12005-01-22 03:39:39 +0000766
767 # Delete the files test.db and test2.db, then execute the TCL and
768 # SQL (in that order) to prepare for the test case.
769 do_test $testname.$n.1 {
770 set ::sqlite_io_error_pending 0
771 catch {db close}
aswiftaebf4132008-11-21 00:10:35 +0000772 catch {db2 close}
danielk197732554c12005-01-22 03:39:39 +0000773 catch {file delete -force test.db}
774 catch {file delete -force test.db-journal}
775 catch {file delete -force test2.db}
776 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000777 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000778 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000779 if {[info exists ::ioerropts(-tclprep)]} {
780 eval $::ioerropts(-tclprep)
781 }
782 if {[info exists ::ioerropts(-sqlprep)]} {
783 execsql $::ioerropts(-sqlprep)
784 }
785 expr 0
786 } {0}
787
788 # Read the 'checksum' of the database.
789 if {$::ioerropts(-cksum)} {
790 set checksum [cksum]
791 }
drh93aed5a2008-01-16 17:46:38 +0000792
danielk197732554c12005-01-22 03:39:39 +0000793 # Set the Nth IO error to fail.
794 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +0000795 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +0000796 set ::sqlite_io_error_pending $n
797 }] $n
798
799 # Create a single TCL script from the TCL and SQL specified
800 # as the body of the test.
801 set ::ioerrorbody {}
802 if {[info exists ::ioerropts(-tclbody)]} {
803 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
804 }
805 if {[info exists ::ioerropts(-sqlbody)]} {
806 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
807 }
808
809 # Execute the TCL Script created in the above block. If
810 # there are at least N IO operations performed by SQLite as
811 # a result of the script, the Nth will fail.
812 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +0000813 set ::sqlite_io_error_hit 0
814 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +0000815 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +0000816 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +0000817 set rc [sqlite3_errcode $::DB]
818 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +0000819 # If we are in extended result code mode, make sure all of the
820 # IOERRs we get back really do have their extended code values.
821 # If an extended result code is returned, the sqlite3_errcode
822 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
823 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +0000824 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
825 return $rc
826 }
827 } else {
drh5f7b5bf2007-04-19 12:30:54 +0000828 # If we are not in extended result code mode, make sure no
829 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +0000830 if {[regexp {\+\d} $rc]} {
831 return $rc
832 }
833 }
drh93aed5a2008-01-16 17:46:38 +0000834 # The test repeats as long as $::go is non-zero. $::go starts out
835 # as 1. When a test runs to completion without hitting an I/O
836 # error, that means there is no point in continuing with this test
837 # case so set $::go to zero.
838 #
839 if {$::sqlite_io_error_pending>0} {
840 set ::go 0
841 set q 0
842 set ::sqlite_io_error_pending 0
843 } else {
844 set q 1
845 }
846
drhc9ac5ca2005-11-04 22:03:30 +0000847 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +0000848 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
849 set r 1
850 }
danielk1977f2fa8312006-01-24 13:09:33 +0000851 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +0000852
853 # One of two things must have happened. either
854 # 1. We never hit the IO error and the SQL returned OK
855 # 2. An IO error was hit and the SQL failed
856 #
dand41a29a2010-05-06 15:56:28 +0000857 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +0000858 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +0000859 } {1}
860
danielk197780daec62008-05-12 10:57:02 +0000861 set ::sqlite_io_error_hit 0
862 set ::sqlite_io_error_pending 0
863
danielk197752b472a2008-05-05 16:23:55 +0000864 # Check that no page references were leaked. There should be
865 # a single reference if there is still an active transaction,
866 # or zero otherwise.
867 #
danielk197781620542008-06-07 05:19:37 +0000868 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
869 # locks are established on database files (i.e. if the error
870 # occurs while attempting to detect a hot-journal file), then
871 # there may 0 page references and an active transaction according
872 # to [sqlite3_get_autocommit].
873 #
danielk19774abd5442008-05-05 15:26:50 +0000874 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +0000875 do_test $testname.$n.4 {
876 set bt [btree_from_db db]
877 db_enter db
878 array set stats [btree_pager_stats $bt]
879 db_leave db
danielk197781620542008-06-07 05:19:37 +0000880 set nRef $stats(ref)
881 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
882 } {1}
danielk19774abd5442008-05-05 15:26:50 +0000883 }
884
danielk197752b472a2008-05-05 16:23:55 +0000885 # If there is an open database handle and no open transaction,
886 # and the pager is not running in exclusive-locking mode,
887 # check that the pager is in "unlocked" state. Theoretically,
888 # if a call to xUnlock() failed due to an IO error the underlying
889 # file may still be locked.
890 #
891 ifcapable pragma {
892 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +0000893 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +0000894 && [db one {pragma locking_mode}] eq "normal"
895 && [sqlite3_get_autocommit db]
896 } {
897 do_test $testname.$n.5 {
898 set bt [btree_from_db db]
899 db_enter db
900 array set stats [btree_pager_stats $bt]
901 db_leave db
902 set stats(state)
903 } 0
904 }
905 }
906
danielk197732554c12005-01-22 03:39:39 +0000907 # If an IO error occured, then the checksum of the database should
908 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +0000909 #
drh1aa5af12008-03-07 19:51:14 +0000910 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +0000911 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +0000912 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +0000913 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +0000914 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +0000915 cksum
916 } $checksum
917 }
918
drh1aa5af12008-03-07 19:51:14 +0000919 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +0000920 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +0000921 if {[info exists ::ioerropts(-cleanup)]} {
922 catch $::ioerropts(-cleanup)
923 }
danielk197732554c12005-01-22 03:39:39 +0000924 }
925 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +0000926 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +0000927 unset ::ioerropts
928}
929
drh93aed5a2008-01-16 17:46:38 +0000930# Return a checksum based on the contents of the main database associated
931# with connection $db
danielk197732554c12005-01-22 03:39:39 +0000932#
933proc cksum {{db db}} {
934 set txt [$db eval {
935 SELECT name, type, sql FROM sqlite_master order by name
936 }]\n
937 foreach tbl [$db eval {
938 SELECT name FROM sqlite_master WHERE type='table' order by name
939 }] {
940 append txt [$db eval "SELECT * FROM $tbl"]\n
941 }
942 foreach prag {default_synchronous default_cache_size} {
943 append txt $prag-[$db eval "PRAGMA $prag"]\n
944 }
945 set cksum [string length $txt]-[md5 $txt]
946 # puts $cksum-[file size test.db]
947 return $cksum
948}
949
drh93aed5a2008-01-16 17:46:38 +0000950# Generate a checksum based on the contents of the main and temp tables
951# database $db. If the checksum of two databases is the same, and the
952# integrity-check passes for both, the two databases are identical.
953#
drh1db639c2008-01-17 02:36:28 +0000954proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +0000955 set ret [list]
956 ifcapable tempdb {
957 set sql {
958 SELECT name FROM sqlite_master WHERE type = 'table' UNION
959 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
960 SELECT 'sqlite_master' UNION
961 SELECT 'sqlite_temp_master' ORDER BY 1
962 }
963 } else {
964 set sql {
965 SELECT name FROM sqlite_master WHERE type = 'table' UNION
966 SELECT 'sqlite_master' ORDER BY 1
967 }
968 }
969 set tbllist [$db eval $sql]
970 set txt {}
971 foreach tbl $tbllist {
972 append txt [$db eval "SELECT * FROM $tbl"]
973 }
974 foreach prag {default_cache_size} {
975 append txt $prag-[$db eval "PRAGMA $prag"]\n
976 }
977 # puts txt=$txt
978 return [md5 $txt]
979}
980
drhdc2c4912009-02-04 22:46:47 +0000981# Generate a checksum based on the contents of a single database with
982# a database connection. The name of the database is $dbname.
983# Examples of $dbname are "temp" or "main".
984#
985proc dbcksum {db dbname} {
986 if {$dbname=="temp"} {
987 set master sqlite_temp_master
988 } else {
989 set master $dbname.sqlite_master
990 }
991 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
992 set txt [$db eval "SELECT * FROM $master"]\n
993 foreach tab $alltab {
994 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
995 }
996 return [md5 $txt]
997}
998
danielk197735754ac2008-03-21 17:29:37 +0000999proc memdebug_log_sql {{filename mallocs.sql}} {
1000
danielk19776f332c12008-03-21 14:22:44 +00001001 set data [sqlite3_memdebug_log dump]
1002 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +00001003 if {$nFrame < 0} { return "" }
1004
danielk197735754ac2008-03-21 17:29:37 +00001005 set database temp
1006
danielk19776ab3a2e2009-02-19 14:39:25 +00001007 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +00001008
1009 set sql ""
1010 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +00001011 set nCall [lindex $e 0]
1012 set nByte [lindex $e 1]
1013 set lStack [lrange $e 2 end]
1014 append sql "INSERT INTO ${database}.malloc VALUES"
1015 append sql "('test', $nCall, $nByte, '$lStack');\n"
1016 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +00001017 set frames($f) 1
1018 }
1019 }
1020
1021 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +00001022 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +00001023
1024 foreach f [array names frames] {
1025 set addr [format %x $f]
1026 set cmd "addr2line -e [info nameofexec] $addr"
1027 set line [eval exec $cmd]
1028 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00001029
1030 set file [lindex [split $line :] 0]
1031 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00001032 }
1033
danielk197735754ac2008-03-21 17:29:37 +00001034 foreach f [array names files] {
1035 set contents ""
1036 catch {
1037 set fd [open $f]
1038 set contents [read $fd]
1039 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001040 }
danielk197735754ac2008-03-21 17:29:37 +00001041 set contents [string map {' ''} $contents]
1042 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00001043 }
danielk19776f332c12008-03-21 14:22:44 +00001044
danielk197735754ac2008-03-21 17:29:37 +00001045 set fd [open $filename w]
1046 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
1047 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001048}
drh93aed5a2008-01-16 17:46:38 +00001049
danielk197732554c12005-01-22 03:39:39 +00001050# Copy file $from into $to. This is used because some versions of
1051# TCL for windows (notably the 8.4.1 binary package shipped with the
1052# current mingw release) have a broken "file copy" command.
1053#
1054proc copy_file {from to} {
1055 if {$::tcl_platform(platform)=="unix"} {
1056 file copy -force $from $to
1057 } else {
1058 set f [open $from]
1059 fconfigure $f -translation binary
1060 set t [open $to w]
1061 fconfigure $t -translation binary
1062 puts -nonewline $t [read $f [file size $from]]
1063 close $t
1064 close $f
1065 }
1066}
1067
danf5894502009-10-07 18:41:19 +00001068# Drop all tables in database [db]
1069proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00001070 ifcapable trigger&&foreignkey {
1071 set pk [$db one "PRAGMA foreign_keys"]
1072 $db eval "PRAGMA foreign_keys = OFF"
1073 }
drh9a6ffc82010-02-15 18:03:20 +00001074 foreach {idx name file} [db eval {PRAGMA database_list}] {
1075 if {$idx==1} {
1076 set master sqlite_temp_master
1077 } else {
1078 set master $name.sqlite_master
1079 }
1080 foreach {t type} [$db eval "
1081 SELECT name, type FROM $master
1082 WHERE type IN('table', 'view') AND name NOT like 'sqlite_%'
1083 "] {
1084 $db eval "DROP $type $t"
1085 }
danf5894502009-10-07 18:41:19 +00001086 }
shaneh4e7b32f2009-12-17 22:12:51 +00001087 ifcapable trigger&&foreignkey {
1088 $db eval "PRAGMA foreign_keys = $pk"
1089 }
danf5894502009-10-07 18:41:19 +00001090}
1091
dan71cb5182010-04-26 12:39:03 +00001092#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00001093# If a test script is executed with global variable $::G(perm:name) set to
1094# "wal", then the tests are run in WAL mode. Otherwise, they should be run
1095# in rollback mode. The following Tcl procs are used to make this less
1096# intrusive:
dan71cb5182010-04-26 12:39:03 +00001097#
1098# wal_set_journal_mode ?DB?
1099#
1100# If running a WAL test, execute "PRAGMA journal_mode = wal" using
1101# connection handle DB. Otherwise, this command is a no-op.
1102#
1103# wal_check_journal_mode TESTNAME ?DB?
1104#
1105# If running a WAL test, execute a tests case that fails if the main
1106# database for connection handle DB is not currently a WAL database.
1107# Otherwise (if not running a WAL permutation) this is a no-op.
1108#
1109# wal_is_wal_mode
1110#
1111# Returns true if this test should be run in WAL mode. False otherwise.
1112#
1113proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00001114 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00001115}
1116proc wal_set_journal_mode {{db db}} {
1117 if { [wal_is_wal_mode] } {
1118 $db eval "PRAGMA journal_mode = WAL"
1119 }
1120}
1121proc wal_check_journal_mode {testname {db db}} {
1122 if { [wal_is_wal_mode] } {
1123 $db eval { SELECT * FROM sqlite_master }
1124 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
1125 }
1126}
1127
dan430e74c2010-06-07 17:47:26 +00001128proc permutation {} {
1129 set perm ""
1130 catch {set perm $::G(perm:name)}
1131 set perm
1132}
1133
danc1a60c52010-06-07 14:28:16 +00001134#-------------------------------------------------------------------------
1135#
1136proc slave_test_script {script} {
1137
1138 # Create the interpreter used to run the test script.
1139 interp create tinterp
1140
1141 # Populate some global variables that tester.tcl expects to see.
1142 foreach {var value} [list \
1143 ::argv0 $::argv0 \
1144 ::argv {} \
1145 ::SLAVE 1 \
1146 ] {
1147 interp eval tinterp [list set $var $value]
1148 }
1149
1150 # The alias used to access the global test counters.
1151 tinterp alias set_test_counter set_test_counter
1152
1153 # Set up the ::cmdlinearg array in the slave.
1154 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
1155
dan430e74c2010-06-07 17:47:26 +00001156 # Set up the ::G array in the slave.
1157 interp eval tinterp [list array set ::G [array get ::G]]
1158
danc1a60c52010-06-07 14:28:16 +00001159 # Load the various test interfaces implemented in C.
1160 load_testfixture_extensions tinterp
1161
1162 # Run the test script.
1163 interp eval tinterp $script
1164
danea5542d2010-07-06 11:26:15 +00001165 # Check if the interpreter call [run_thread_tests]
1166 if { [interp eval tinterp {info exists ::run_thread_tests_called}] } {
1167 set ::run_thread_tests_called 1
1168 }
1169
danc1a60c52010-06-07 14:28:16 +00001170 # Delete the interpreter used to run the test script.
1171 interp delete tinterp
1172}
1173
dan430e74c2010-06-07 17:47:26 +00001174proc slave_test_file {zFile} {
dan0626dfc2010-06-15 06:56:37 +00001175 set tail [file tail $zFile]
dan430e74c2010-06-07 17:47:26 +00001176
dan92d516a2010-07-05 14:54:48 +00001177 # Remember the value of the shared-cache setting. So that it is possible
1178 # to check afterwards that it was not modified by the test script.
1179 #
1180 ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] }
dan0626dfc2010-06-15 06:56:37 +00001181
dan92d516a2010-07-05 14:54:48 +00001182 # Run the test script in a slave interpreter.
1183 #
danea5542d2010-07-06 11:26:15 +00001184 unset -nocomplain ::run_thread_tests_called
dan0626dfc2010-06-15 06:56:37 +00001185 reset_prng_state
1186 set ::sqlite_open_file_count 0
1187 set time [time { slave_test_script [list source $zFile] }]
1188 set ms [expr [lindex $time 0] / 1000]
1189
dan92d516a2010-07-05 14:54:48 +00001190 # Test that all files opened by the test script were closed. Omit this
1191 # if the test script has "thread" in its name. The open file counter
1192 # is not thread-safe.
dan0626dfc2010-06-15 06:56:37 +00001193 #
danea5542d2010-07-06 11:26:15 +00001194 if {[info exists ::run_thread_tests_called]==0} {
dan92d516a2010-07-05 14:54:48 +00001195 do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0}
1196 }
dan430e74c2010-06-07 17:47:26 +00001197 set ::sqlite_open_file_count 0
1198
dan0626dfc2010-06-15 06:56:37 +00001199 # Test that the global "shared-cache" setting was not altered by
1200 # the test script.
1201 #
1202 ifcapable shared_cache {
1203 set res [expr {[sqlite3_enable_shared_cache] == $scs}]
1204 do_test ${tail}-sharedcachesetting [list set {} $res] 1
1205 }
dan430e74c2010-06-07 17:47:26 +00001206
dan92d516a2010-07-05 14:54:48 +00001207 # Add some info to the output.
1208 #
dan0626dfc2010-06-15 06:56:37 +00001209 puts "Time: $tail $ms ms"
dan430e74c2010-06-07 17:47:26 +00001210 show_memstats
danc1a60c52010-06-07 14:28:16 +00001211}
1212
danf5894502009-10-07 18:41:19 +00001213
danielk197745901d62004-11-10 15:27:38 +00001214# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
1215# to non-zero, then set the global variable $AUTOVACUUM to 1.
1216set AUTOVACUUM $sqlite_options(default_autovacuum)
danielk1977ee8b7992009-03-26 17:13:06 +00001217
1218source $testdir/thread_common.tcl