blob: d6ce7eac2830f7dfba3a281a44e9bc93faa3fd26 [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} {
drh9eb9e262004-02-11 02:18:05 +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 }
103 if {[sqlite_orig -has-codec]} {
104 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
dan430e74c2010-06-07 17:47:26 +0000123# The following block only runs the first time this file is sourced. It
124# does not run in slave interpreters (since the ::cmdlinearg array is
125# populated before the test script is run in slave interpreters).
drhbec2bf42000-05-29 23:48:22 +0000126#
danc1a60c52010-06-07 14:28:16 +0000127if {[info exists cmdlinearg]==0} {
128
129 # Parse any options specified in the $argv array. This script accepts the
130 # following options:
131 #
132 # --pause
133 # --soft-heap-limit=NN
134 # --maxerror=NN
135 # --malloctrace=N
136 # --backtrace=N
137 # --binarylog=N
dan0626dfc2010-06-15 06:56:37 +0000138 # --soak=N
danc1a60c52010-06-07 14:28:16 +0000139 #
140 set cmdlinearg(soft-heap-limit) 0
141 set cmdlinearg(maxerror) 1000
142 set cmdlinearg(malloctrace) 0
143 set cmdlinearg(backtrace) 10
144 set cmdlinearg(binarylog) 0
dan0626dfc2010-06-15 06:56:37 +0000145 set cmdlinearg(soak) 0
danc1a60c52010-06-07 14:28:16 +0000146
147 set leftover [list]
148 foreach a $argv {
149 switch -regexp -- $a {
150 {^-+pause$} {
151 # Wait for user input before continuing. This is to give the user an
152 # opportunity to connect profiling tools to the process.
153 puts -nonewline "Press RETURN to begin..."
154 flush stdout
155 gets stdin
156 }
157 {^-+soft-heap-limit=.+$} {
158 foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break
159 }
160 {^-+maxerror=.+$} {
161 foreach {dummy cmdlinearg(maxerror)} [split $a =] break
162 }
163 {^-+malloctrace=.+$} {
164 foreach {dummy cmdlinearg(malloctrace)} [split $a =] break
165 if {$cmdlinearg(malloctrace)} {
166 sqlite3_memdebug_log start
167 }
168 }
169 {^-+backtrace=.+$} {
170 foreach {dummy cmdlinearg(backtrace)} [split $a =] break
dan0626dfc2010-06-15 06:56:37 +0000171 sqlite3_memdebug_backtrace $value
danc1a60c52010-06-07 14:28:16 +0000172 }
danc1a60c52010-06-07 14:28:16 +0000173 {^-+binarylog=.+$} {
174 foreach {dummy cmdlinearg(binarylog)} [split $a =] break
175 }
dan0626dfc2010-06-15 06:56:37 +0000176 {^-+soak=.+$} {
177 foreach {dummy cmdlinearg(soak)} [split $a =] break
178 set ::G(issoak) $cmdlinearg(soak)
179 }
danc1a60c52010-06-07 14:28:16 +0000180 default {
181 lappend leftover $a
182 }
183 }
184 }
185 set argv $leftover
186
dan430e74c2010-06-07 17:47:26 +0000187 # Install the malloc layer used to inject OOM errors. And the 'automatic'
188 # extensions. This only needs to be done once for the process.
189 #
danielk1977f7b9d662008-06-23 18:49:43 +0000190 sqlite3_shutdown
191 install_malloc_faultsim 1
192 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000193 autoinstall_test_functions
danc1a60c52010-06-07 14:28:16 +0000194
dan430e74c2010-06-07 17:47:26 +0000195 # If the --binarylog option was specified, create the logging VFS. This
196 # call installs the new VFS as the default for all SQLite connections.
197 #
danc1a60c52010-06-07 14:28:16 +0000198 if {$cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000199 vfslog new binarylog {} vfslog.bin
danc1a60c52010-06-07 14:28:16 +0000200 }
201
202 # Set the backtrace depth, if malloc tracing is enabled.
203 #
204 if {$cmdlinearg(malloctrace)} {
205 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danielk1977185eac92008-07-12 15:55:54 +0000206 }
danielk1977f7b9d662008-06-23 18:49:43 +0000207}
danielk197703ba3fa2009-01-09 10:49:14 +0000208
danc1a60c52010-06-07 14:28:16 +0000209# Update the soft-heap-limit each time this script is run. In that
210# way if an individual test file changes the soft-heap-limit, it
211# will be reset at the start of the next test file.
212#
213sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
214
215# Create a test database
216#
danielk197703ba3fa2009-01-09 10:49:14 +0000217proc reset_db {} {
218 catch {db close}
219 file delete -force test.db
220 file delete -force test.db-journal
dand3f8f942010-04-13 11:35:01 +0000221 file delete -force test.db-wal
danielk197703ba3fa2009-01-09 10:49:14 +0000222 sqlite3 db ./test.db
223 set ::DB [sqlite3_connection_pointer db]
224 if {[info exists ::SETUP_SQL]} {
225 db eval $::SETUP_SQL
226 }
drhcd61c282002-03-06 22:01:34 +0000227}
danielk197703ba3fa2009-01-09 10:49:14 +0000228reset_db
drhbec2bf42000-05-29 23:48:22 +0000229
230# Abort early if this script has been run before.
231#
danc1a60c52010-06-07 14:28:16 +0000232if {[info exists TC(count)]} return
drhbec2bf42000-05-29 23:48:22 +0000233
danc1a60c52010-06-07 14:28:16 +0000234# Initialize the test counters and set up commands to access them.
235# Or, if this is a slave interpreter, set up aliases to write the
236# counters in the parent interpreter.
drhbec2bf42000-05-29 23:48:22 +0000237#
danc1a60c52010-06-07 14:28:16 +0000238if {0==[info exists ::SLAVE]} {
239 set TC(errors) 0
240 set TC(count) 0
241 set TC(fail_list) [list]
242 set TC(omit_list) [list]
243
244 proc set_test_counter {counter args} {
245 if {[llength $args]} {
246 set ::TC($counter) [lindex $args 0]
247 }
248 set ::TC($counter)
249 }
drhb62c3352006-11-23 09:39:16 +0000250}
drh348784e2000-05-29 20:41:49 +0000251
drh521cc842008-04-15 02:36:33 +0000252# Record the fact that a sequence of tests were omitted.
253#
254proc omit_test {name reason} {
danc1a60c52010-06-07 14:28:16 +0000255 set omitList [set_test_counter omit_list]
drh521cc842008-04-15 02:36:33 +0000256 lappend omitList [list $name $reason]
danc1a60c52010-06-07 14:28:16 +0000257 set_test_counter omit_list $omitList
drh521cc842008-04-15 02:36:33 +0000258}
259
danc1a60c52010-06-07 14:28:16 +0000260# Record the fact that a test failed.
261#
262proc fail_test {name} {
263 set f [set_test_counter fail_list]
264 lappend f $name
265 set_test_counter fail_list $f
266 set_test_counter errors [expr [set_test_counter errors] + 1]
267
268 set nFail [set_test_counter errors]
269 if {$nFail>=$::cmdlinearg(maxerror)} {
270 puts "*** Giving up..."
271 finalize_testing
272 }
273}
274
275# Increment the number of tests run
276#
277proc incr_ntest {} {
278 set_test_counter count [expr [set_test_counter count] + 1]
279}
280
281
drh348784e2000-05-29 20:41:49 +0000282# Invoke the do_test procedure to run a single test
283#
284proc do_test {name cmd expected} {
danc1a60c52010-06-07 14:28:16 +0000285
286 global argv cmdlinearg
287
drh4a50aac2007-08-23 02:47:53 +0000288 sqlite3_memdebug_settitle $name
danc1a60c52010-06-07 14:28:16 +0000289
drh767c2002000-10-19 14:10:08 +0000290 if {[llength $argv]==0} {
drh348784e2000-05-29 20:41:49 +0000291 set go 1
292 } else {
293 set go 0
294 foreach pattern $argv {
295 if {[string match $pattern $name]} {
296 set go 1
297 break
298 }
299 }
300 }
301 if {!$go} return
dan430e74c2010-06-07 17:47:26 +0000302
303 if {[info exists ::G(perm:name)]} {
dan2fce9ab2010-06-15 18:00:06 +0000304 set name "$::G(perm:name)$name"
dan430e74c2010-06-07 17:47:26 +0000305 }
306
danc1a60c52010-06-07 14:28:16 +0000307 incr_ntest
drh5edc3122001-09-13 21:53:09 +0000308 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000309 flush stdout
310 if {[catch {uplevel #0 "$cmd;\n"} result]} {
311 puts "\nError: $result"
danc1a60c52010-06-07 14:28:16 +0000312 fail_test $name
drh348784e2000-05-29 20:41:49 +0000313 } elseif {[string compare $result $expected]} {
314 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
danc1a60c52010-06-07 14:28:16 +0000315 fail_test $name
drh348784e2000-05-29 20:41:49 +0000316 } else {
317 puts " Ok"
318 }
drh6558db82007-04-13 03:23:21 +0000319 flush stdout
drh348784e2000-05-29 20:41:49 +0000320}
dan153eda02010-06-21 07:45:47 +0000321
322proc do_execsql_test {testname sql result} {
323 uplevel do_test $testname [list "execsql {$sql}"] [list $result]
324}
325proc do_catchsql_test {testname sql result} {
326 uplevel do_test $testname [list "catchsql {$sql}"] [list $result]
327}
328
drh348784e2000-05-29 20:41:49 +0000329
drhb62c3352006-11-23 09:39:16 +0000330# Run an SQL script.
331# Return the number of microseconds per statement.
332#
drh3590f152006-11-23 21:09:10 +0000333proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000334 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000335 flush stdout
336 set speed [time {sqlite3_exec_nr db $sql}]
337 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000338 if {$tm == 0} {
339 set rate [format %20s "many"]
340 } else {
341 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
342 }
drh3590f152006-11-23 21:09:10 +0000343 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000344 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000345 global total_time
346 set total_time [expr {$total_time+$tm}]
347}
drh45c236d2008-03-22 01:08:00 +0000348proc speed_trial_tcl {name numstmt units script} {
349 puts -nonewline [format {%-21.21s } $name...]
350 flush stdout
351 set speed [time {eval $script}]
352 set tm [lindex $speed 0]
353 if {$tm == 0} {
354 set rate [format %20s "many"]
355 } else {
356 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
357 }
358 set u2 $units/s
359 puts [format {%12d uS %s %s} $tm $rate $u2]
360 global total_time
361 set total_time [expr {$total_time+$tm}]
362}
drhdd924312007-03-31 22:34:16 +0000363proc speed_trial_init {name} {
364 global total_time
365 set total_time 0
366}
367proc speed_trial_summary {name} {
368 global total_time
369 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
drhb62c3352006-11-23 09:39:16 +0000370}
371
drh348784e2000-05-29 20:41:49 +0000372# Run this routine last
373#
374proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +0000375 catch {db close}
376 catch {db2 close}
377 catch {db3 close}
378 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +0000379}
380proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +0000381 global sqlite_open_file_count
382
383 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +0000384
drh6e142f52000-06-08 13:36:40 +0000385 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000386 catch {db2 close}
387 catch {db3 close}
388
drh9bc54492007-10-23 14:49:59 +0000389 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000390 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000391 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000392 db close
drh984bfaa2008-03-19 16:08:53 +0000393 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +0000394
drh3a7fb7c2007-08-10 16:41:08 +0000395 sqlite3_soft_heap_limit 0
danc1a60c52010-06-07 14:28:16 +0000396 set nTest [incr_ntest]
397 set nErr [set_test_counter errors]
398
drh348784e2000-05-29 20:41:49 +0000399 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000400 if {$nErr>0} {
danc1a60c52010-06-07 14:28:16 +0000401 puts "Failures on these tests: [set_test_counter fail_list]"
drhf3a65f72007-08-22 20:18:21 +0000402 }
danielk1977ee8b7992009-03-26 17:13:06 +0000403 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +0000404 if {[llength $omitList]>0} {
405 puts "Omitted test cases:"
406 set prec {}
407 foreach {rec} [lsort $omitList] {
408 if {$rec==$prec} continue
409 set prec $rec
410 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
411 }
412 }
drh80788d82006-09-02 14:50:23 +0000413 if {$nErr>0 && ![working_64bit_int]} {
414 puts "******************************************************************"
415 puts "N.B.: The version of TCL that you used to build this test harness"
416 puts "is defective in that it does not support 64-bit integers. Some or"
417 puts "all of the test failures above might be a result from this defect"
418 puts "in your TCL build."
419 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000420 }
danc1a60c52010-06-07 14:28:16 +0000421 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000422 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +0000423 }
drh94e92032003-02-16 22:21:32 +0000424 if {$sqlite_open_file_count} {
425 puts "$sqlite_open_file_count files were left open"
426 incr nErr
427 }
drhf3a65f72007-08-22 20:18:21 +0000428 if {[sqlite3_memory_used]>0} {
429 puts "Unfreed memory: [sqlite3_memory_used] bytes"
430 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000431 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000432 puts "Writing unfreed memory log to \"./memleak.txt\""
433 sqlite3_memdebug_dump ./memleak.txt
434 }
drhf3a65f72007-08-22 20:18:21 +0000435 } else {
436 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000437 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000438 sqlite3_memdebug_dump ./memusage.txt
439 }
drhf3a65f72007-08-22 20:18:21 +0000440 }
drhf7141992008-06-19 00:16:08 +0000441 show_memstats
drh91fd4d42008-01-19 20:11:25 +0000442 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
443 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000444 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
445 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
446 }
danc1a60c52010-06-07 14:28:16 +0000447 if {$::cmdlinearg(malloctrace)} {
danielk197735754ac2008-03-21 17:29:37 +0000448 puts "Writing mallocs.sql..."
449 memdebug_log_sql
450 sqlite3_memdebug_log stop
451 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000452
453 if {[sqlite3_memory_used]>0} {
454 puts "Writing leaks.sql..."
455 sqlite3_memdebug_log sync
456 memdebug_log_sql leaks.sql
457 }
danielk197735754ac2008-03-21 17:29:37 +0000458 }
drhd9910fe2006-10-04 11:55:49 +0000459 foreach f [glob -nocomplain test.db-*-journal] {
460 file delete -force $f
461 }
462 foreach f [glob -nocomplain test.db-mj*] {
463 file delete -force $f
464 }
drhdb25e382001-03-15 18:21:22 +0000465 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000466}
467
drhf7141992008-06-19 00:16:08 +0000468# Display memory statistics for analysis and debugging purposes.
469#
470proc show_memstats {} {
471 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +0000472 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
473 set val [format {now %10d max %10d max-size %10d} \
474 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000475 puts "Memory used: $val"
476 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +0000477 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
478 set val [format {now %10d max %10d max-size %10d} \
479 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000480 puts "Page-cache used: $val"
481 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
482 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
483 puts "Page-cache overflow: $val"
484 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
485 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
486 puts "Scratch memory used: $val"
487 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +0000488 set y [sqlite3_status SQLITE_STATUS_SCRATCH_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 "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +0000492 ifcapable yytrackmaxstackdepth {
493 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
494 set val [format { max %10d} [lindex $x 2]]
495 puts "Parser stack depth: $val"
496 }
drhf7141992008-06-19 00:16:08 +0000497}
498
drh348784e2000-05-29 20:41:49 +0000499# A procedure to execute SQL
500#
drhc4a3c772001-04-04 11:48:57 +0000501proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000502 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000503 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000504}
drh3aadb2e2000-05-31 17:59:25 +0000505
drhadbca9c2001-09-27 15:11:53 +0000506# Execute SQL and catch exceptions.
507#
508proc catchsql {sql {db db}} {
509 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +0000510 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +0000511 lappend r $msg
512 return $r
513}
514
drh04096482001-11-09 22:41:44 +0000515# Do an VDBE code dump on the SQL given
516#
517proc explain {sql {db db}} {
518 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000519 puts "addr opcode p1 p2 p3 p4 p5 #"
520 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000521 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000522 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
523 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
524 ]
drh04096482001-11-09 22:41:44 +0000525 }
526}
527
drhdafc0ce2008-04-17 19:14:02 +0000528# Show the VDBE program for an SQL statement but omit the Trace
529# opcode at the beginning. This procedure can be used to prove
530# that different SQL statements generate exactly the same VDBE code.
531#
532proc explain_no_trace {sql} {
533 set tr [db eval "EXPLAIN $sql"]
534 return [lrange $tr 7 end]
535}
536
drh3aadb2e2000-05-31 17:59:25 +0000537# Another procedure to execute SQL. This one includes the field
538# names in the returned list.
539#
540proc execsql2 {sql} {
541 set result {}
542 db eval $sql data {
543 foreach f $data(*) {
544 lappend result $f $data($f)
545 }
546 }
547 return $result
548}
drh17a68932001-01-31 13:28:08 +0000549
drhdde85d92003-03-01 19:45:34 +0000550# Use the non-callback API to execute multiple SQL statements
551#
552proc stepsql {dbptr sql} {
553 set sql [string trim $sql]
554 set r 0
555 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000556 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000557 return [list 1 $vm]
558 }
559 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000560# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
561# foreach v $VAL {lappend r $v}
562# }
563 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
564 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
565 lappend r [sqlite3_column_text $vm $i]
566 }
drhdde85d92003-03-01 19:45:34 +0000567 }
danielk1977106bb232004-05-21 10:08:53 +0000568 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000569 return [list 1 $errmsg]
570 }
571 }
572 return $r
573}
574
drh17a68932001-01-31 13:28:08 +0000575# Delete a file or directory
576#
577proc forcedelete {filename} {
578 if {[catch {file delete -force $filename}]} {
579 exec rm -rf $filename
580 }
581}
drh21504322002-06-25 13:16:02 +0000582
583# Do an integrity check of the entire database
584#
danielk197704103022009-02-03 16:51:24 +0000585proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +0000586 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +0000587 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +0000588 }
589}
590
danielk1977db4e8862008-01-16 08:24:46 +0000591proc fix_ifcapable_expr {expr} {
592 set ret ""
593 set state 0
594 for {set i 0} {$i < [string length $expr]} {incr i} {
595 set char [string range $expr $i $i]
596 set newstate [expr {[string is alnum $char] || $char eq "_"}]
597 if {$newstate && !$state} {
598 append ret {$::sqlite_options(}
599 }
600 if {!$newstate && $state} {
601 append ret )
602 }
603 append ret $char
604 set state $newstate
605 }
606 if {$state} {append ret )}
607 return $ret
608}
609
drh27d258a2004-10-30 20:23:09 +0000610# Evaluate a boolean expression of capabilities. If true, execute the
611# code. Omit the code if false.
612#
danielk19773e8c37e2005-01-21 03:12:14 +0000613proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +0000614 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
615 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +0000616 if ($e2) {
617 set c [catch {uplevel 1 $code} r]
618 } else {
619 set c [catch {uplevel 1 $elsecode} r]
620 }
621 return -code $c $r
drh21504322002-06-25 13:16:02 +0000622}
danielk197745901d62004-11-10 15:27:38 +0000623
danielk1977aca790a2005-01-13 11:07:52 +0000624# This proc execs a seperate process that crashes midway through executing
625# the SQL script $sql on database test.db.
626#
627# The crash occurs during a sync() of file $crashfile. When the crash
628# occurs a random subset of all unsynced writes made by the process are
629# written into the files on disk. Argument $crashdelay indicates the
630# number of file syncs to wait before crashing.
631#
632# The return value is a list of two elements. The first element is a
633# boolean, indicating whether or not the process actually crashed or
634# reported some other error. The second element in the returned list is the
635# error message. This is "child process exited abnormally" if the crash
636# occured.
637#
danielk1977f8940ae2007-08-23 11:07:10 +0000638# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +0000639#
640proc crashsql {args} {
danielk1977aca790a2005-01-13 11:07:52 +0000641 if {$::tcl_platform(platform)!="unix"} {
642 error "crashsql should only be used on unix"
643 }
danielk197759a33f92007-03-17 10:26:59 +0000644
645 set blocksize ""
646 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +0000647 set prngseed 0
drhf8587402008-01-08 16:03:49 +0000648 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +0000649 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +0000650 set dc ""
danielk197759a33f92007-03-17 10:26:59 +0000651 set sql [lindex $args end]
652
653 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
654 set z [lindex $args $ii]
655 set n [string length $z]
656 set z2 [lindex $args [expr $ii+1]]
657
658 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +0000659 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +0000660 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +0000661 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000662 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +0000663 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +0000664 else { error "Unrecognized option: $z" }
665 }
666
667 if {$crashfile eq ""} {
668 error "Compulsory option -file missing"
669 }
670
danielk1977aca790a2005-01-13 11:07:52 +0000671 set cfile [file join [pwd] $crashfile]
672
673 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +0000674 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +0000675 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +0000676 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +0000677 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +0000678
679 # This block sets the cache size of the main database to 10
680 # pages. This is done in case the build is configured to omit
681 # "PRAGMA cache_size".
682 puts $f {db eval {SELECT * FROM sqlite_master;}}
683 puts $f {set bt [btree_from_db db]}
684 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +0000685 if {$prngseed} {
686 set seed [expr {$prngseed%10007+1}]
687 # puts seed=$seed
688 puts $f "db eval {SELECT randomblob($seed)}"
689 }
danielk1977933bbd62007-03-17 07:22:42 +0000690
drhf8587402008-01-08 16:03:49 +0000691 if {[string length $tclbody]>0} {
692 puts $f $tclbody
693 }
694 if {[string length $sql]>0} {
695 puts $f "db eval {"
696 puts $f "$sql"
697 puts $f "}"
698 }
danielk1977aca790a2005-01-13 11:07:52 +0000699 close $f
700
701 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000702 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000703 } msg]
704 lappend r $msg
705}
706
danielk197732554c12005-01-22 03:39:39 +0000707# Usage: do_ioerr_test <test number> <options...>
708#
709# This proc is used to implement test cases that check that IO errors
710# are correctly handled. The first argument, <test number>, is an integer
711# used to name the tests executed by this proc. Options are as follows:
712#
713# -tclprep TCL script to run to prepare test.
714# -sqlprep SQL script to run to prepare test.
715# -tclbody TCL script to run with IO error simulation.
716# -sqlbody TCL script to run with IO error simulation.
717# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000718# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +0000719# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +0000720# -start Value of 'N' to begin with (default 1)
721#
722# -cksum Boolean. If true, test that the database does
723# not change during the execution of the test case.
724#
725proc do_ioerr_test {testname args} {
726
danielk197732554c12005-01-22 03:39:39 +0000727 set ::ioerropts(-start) 1
728 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000729 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +0000730 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +0000731 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +0000732 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +0000733 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +0000734 array set ::ioerropts $args
735
danielk197747cd39c2008-05-12 12:41:15 +0000736 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
737 # a couple of obscure IO errors that do not return them.
738 set ::ioerropts(-erc) 0
739
danielk197732554c12005-01-22 03:39:39 +0000740 set ::go 1
drh93aed5a2008-01-16 17:46:38 +0000741 #reset_prng_state
742 save_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +0000743 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +0000744 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +0000745 incr ::ioerropts(-count) -1
746 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +0000747
748 # Skip this IO error if it was specified with the "-exclude" option.
749 if {[info exists ::ioerropts(-exclude)]} {
750 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
751 }
danielk1977861f7452008-06-05 11:39:11 +0000752 if {$::ioerropts(-restoreprng)} {
753 restore_prng_state
754 }
danielk197732554c12005-01-22 03:39:39 +0000755
756 # Delete the files test.db and test2.db, then execute the TCL and
757 # SQL (in that order) to prepare for the test case.
758 do_test $testname.$n.1 {
759 set ::sqlite_io_error_pending 0
760 catch {db close}
aswiftaebf4132008-11-21 00:10:35 +0000761 catch {db2 close}
danielk197732554c12005-01-22 03:39:39 +0000762 catch {file delete -force test.db}
763 catch {file delete -force test.db-journal}
764 catch {file delete -force test2.db}
765 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000766 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000767 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000768 if {[info exists ::ioerropts(-tclprep)]} {
769 eval $::ioerropts(-tclprep)
770 }
771 if {[info exists ::ioerropts(-sqlprep)]} {
772 execsql $::ioerropts(-sqlprep)
773 }
774 expr 0
775 } {0}
776
777 # Read the 'checksum' of the database.
778 if {$::ioerropts(-cksum)} {
779 set checksum [cksum]
780 }
drh93aed5a2008-01-16 17:46:38 +0000781
danielk197732554c12005-01-22 03:39:39 +0000782 # Set the Nth IO error to fail.
783 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +0000784 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +0000785 set ::sqlite_io_error_pending $n
786 }] $n
787
788 # Create a single TCL script from the TCL and SQL specified
789 # as the body of the test.
790 set ::ioerrorbody {}
791 if {[info exists ::ioerropts(-tclbody)]} {
792 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
793 }
794 if {[info exists ::ioerropts(-sqlbody)]} {
795 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
796 }
797
798 # Execute the TCL Script created in the above block. If
799 # there are at least N IO operations performed by SQLite as
800 # a result of the script, the Nth will fail.
801 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +0000802 set ::sqlite_io_error_hit 0
803 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +0000804 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +0000805 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +0000806 set rc [sqlite3_errcode $::DB]
807 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +0000808 # If we are in extended result code mode, make sure all of the
809 # IOERRs we get back really do have their extended code values.
810 # If an extended result code is returned, the sqlite3_errcode
811 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
812 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +0000813 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
814 return $rc
815 }
816 } else {
drh5f7b5bf2007-04-19 12:30:54 +0000817 # If we are not in extended result code mode, make sure no
818 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +0000819 if {[regexp {\+\d} $rc]} {
820 return $rc
821 }
822 }
drh93aed5a2008-01-16 17:46:38 +0000823 # The test repeats as long as $::go is non-zero. $::go starts out
824 # as 1. When a test runs to completion without hitting an I/O
825 # error, that means there is no point in continuing with this test
826 # case so set $::go to zero.
827 #
828 if {$::sqlite_io_error_pending>0} {
829 set ::go 0
830 set q 0
831 set ::sqlite_io_error_pending 0
832 } else {
833 set q 1
834 }
835
drhc9ac5ca2005-11-04 22:03:30 +0000836 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +0000837 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
838 set r 1
839 }
danielk1977f2fa8312006-01-24 13:09:33 +0000840 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +0000841
842 # One of two things must have happened. either
843 # 1. We never hit the IO error and the SQL returned OK
844 # 2. An IO error was hit and the SQL failed
845 #
dand41a29a2010-05-06 15:56:28 +0000846 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +0000847 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +0000848 } {1}
849
danielk197780daec62008-05-12 10:57:02 +0000850 set ::sqlite_io_error_hit 0
851 set ::sqlite_io_error_pending 0
852
danielk197752b472a2008-05-05 16:23:55 +0000853 # Check that no page references were leaked. There should be
854 # a single reference if there is still an active transaction,
855 # or zero otherwise.
856 #
danielk197781620542008-06-07 05:19:37 +0000857 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
858 # locks are established on database files (i.e. if the error
859 # occurs while attempting to detect a hot-journal file), then
860 # there may 0 page references and an active transaction according
861 # to [sqlite3_get_autocommit].
862 #
danielk19774abd5442008-05-05 15:26:50 +0000863 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +0000864 do_test $testname.$n.4 {
865 set bt [btree_from_db db]
866 db_enter db
867 array set stats [btree_pager_stats $bt]
868 db_leave db
danielk197781620542008-06-07 05:19:37 +0000869 set nRef $stats(ref)
870 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
871 } {1}
danielk19774abd5442008-05-05 15:26:50 +0000872 }
873
danielk197752b472a2008-05-05 16:23:55 +0000874 # If there is an open database handle and no open transaction,
875 # and the pager is not running in exclusive-locking mode,
876 # check that the pager is in "unlocked" state. Theoretically,
877 # if a call to xUnlock() failed due to an IO error the underlying
878 # file may still be locked.
879 #
880 ifcapable pragma {
881 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +0000882 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +0000883 && [db one {pragma locking_mode}] eq "normal"
884 && [sqlite3_get_autocommit db]
885 } {
886 do_test $testname.$n.5 {
887 set bt [btree_from_db db]
888 db_enter db
889 array set stats [btree_pager_stats $bt]
890 db_leave db
891 set stats(state)
892 } 0
893 }
894 }
895
danielk197732554c12005-01-22 03:39:39 +0000896 # If an IO error occured, then the checksum of the database should
897 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +0000898 #
drh1aa5af12008-03-07 19:51:14 +0000899 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +0000900 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +0000901 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +0000902 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +0000903 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +0000904 cksum
905 } $checksum
906 }
907
drh1aa5af12008-03-07 19:51:14 +0000908 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +0000909 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +0000910 if {[info exists ::ioerropts(-cleanup)]} {
911 catch $::ioerropts(-cleanup)
912 }
danielk197732554c12005-01-22 03:39:39 +0000913 }
914 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +0000915 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +0000916 unset ::ioerropts
917}
918
drh93aed5a2008-01-16 17:46:38 +0000919# Return a checksum based on the contents of the main database associated
920# with connection $db
danielk197732554c12005-01-22 03:39:39 +0000921#
922proc cksum {{db db}} {
923 set txt [$db eval {
924 SELECT name, type, sql FROM sqlite_master order by name
925 }]\n
926 foreach tbl [$db eval {
927 SELECT name FROM sqlite_master WHERE type='table' order by name
928 }] {
929 append txt [$db eval "SELECT * FROM $tbl"]\n
930 }
931 foreach prag {default_synchronous default_cache_size} {
932 append txt $prag-[$db eval "PRAGMA $prag"]\n
933 }
934 set cksum [string length $txt]-[md5 $txt]
935 # puts $cksum-[file size test.db]
936 return $cksum
937}
938
drh93aed5a2008-01-16 17:46:38 +0000939# Generate a checksum based on the contents of the main and temp tables
940# database $db. If the checksum of two databases is the same, and the
941# integrity-check passes for both, the two databases are identical.
942#
drh1db639c2008-01-17 02:36:28 +0000943proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +0000944 set ret [list]
945 ifcapable tempdb {
946 set sql {
947 SELECT name FROM sqlite_master WHERE type = 'table' UNION
948 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
949 SELECT 'sqlite_master' UNION
950 SELECT 'sqlite_temp_master' ORDER BY 1
951 }
952 } else {
953 set sql {
954 SELECT name FROM sqlite_master WHERE type = 'table' UNION
955 SELECT 'sqlite_master' ORDER BY 1
956 }
957 }
958 set tbllist [$db eval $sql]
959 set txt {}
960 foreach tbl $tbllist {
961 append txt [$db eval "SELECT * FROM $tbl"]
962 }
963 foreach prag {default_cache_size} {
964 append txt $prag-[$db eval "PRAGMA $prag"]\n
965 }
966 # puts txt=$txt
967 return [md5 $txt]
968}
969
drhdc2c4912009-02-04 22:46:47 +0000970# Generate a checksum based on the contents of a single database with
971# a database connection. The name of the database is $dbname.
972# Examples of $dbname are "temp" or "main".
973#
974proc dbcksum {db dbname} {
975 if {$dbname=="temp"} {
976 set master sqlite_temp_master
977 } else {
978 set master $dbname.sqlite_master
979 }
980 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
981 set txt [$db eval "SELECT * FROM $master"]\n
982 foreach tab $alltab {
983 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
984 }
985 return [md5 $txt]
986}
987
danielk197735754ac2008-03-21 17:29:37 +0000988proc memdebug_log_sql {{filename mallocs.sql}} {
989
danielk19776f332c12008-03-21 14:22:44 +0000990 set data [sqlite3_memdebug_log dump]
991 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +0000992 if {$nFrame < 0} { return "" }
993
danielk197735754ac2008-03-21 17:29:37 +0000994 set database temp
995
danielk19776ab3a2e2009-02-19 14:39:25 +0000996 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +0000997
998 set sql ""
999 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +00001000 set nCall [lindex $e 0]
1001 set nByte [lindex $e 1]
1002 set lStack [lrange $e 2 end]
1003 append sql "INSERT INTO ${database}.malloc VALUES"
1004 append sql "('test', $nCall, $nByte, '$lStack');\n"
1005 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +00001006 set frames($f) 1
1007 }
1008 }
1009
1010 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +00001011 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +00001012
1013 foreach f [array names frames] {
1014 set addr [format %x $f]
1015 set cmd "addr2line -e [info nameofexec] $addr"
1016 set line [eval exec $cmd]
1017 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00001018
1019 set file [lindex [split $line :] 0]
1020 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00001021 }
1022
danielk197735754ac2008-03-21 17:29:37 +00001023 foreach f [array names files] {
1024 set contents ""
1025 catch {
1026 set fd [open $f]
1027 set contents [read $fd]
1028 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001029 }
danielk197735754ac2008-03-21 17:29:37 +00001030 set contents [string map {' ''} $contents]
1031 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00001032 }
danielk19776f332c12008-03-21 14:22:44 +00001033
danielk197735754ac2008-03-21 17:29:37 +00001034 set fd [open $filename w]
1035 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
1036 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001037}
drh93aed5a2008-01-16 17:46:38 +00001038
danielk197732554c12005-01-22 03:39:39 +00001039# Copy file $from into $to. This is used because some versions of
1040# TCL for windows (notably the 8.4.1 binary package shipped with the
1041# current mingw release) have a broken "file copy" command.
1042#
1043proc copy_file {from to} {
1044 if {$::tcl_platform(platform)=="unix"} {
1045 file copy -force $from $to
1046 } else {
1047 set f [open $from]
1048 fconfigure $f -translation binary
1049 set t [open $to w]
1050 fconfigure $t -translation binary
1051 puts -nonewline $t [read $f [file size $from]]
1052 close $t
1053 close $f
1054 }
1055}
1056
danf5894502009-10-07 18:41:19 +00001057# Drop all tables in database [db]
1058proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00001059 ifcapable trigger&&foreignkey {
1060 set pk [$db one "PRAGMA foreign_keys"]
1061 $db eval "PRAGMA foreign_keys = OFF"
1062 }
drh9a6ffc82010-02-15 18:03:20 +00001063 foreach {idx name file} [db eval {PRAGMA database_list}] {
1064 if {$idx==1} {
1065 set master sqlite_temp_master
1066 } else {
1067 set master $name.sqlite_master
1068 }
1069 foreach {t type} [$db eval "
1070 SELECT name, type FROM $master
1071 WHERE type IN('table', 'view') AND name NOT like 'sqlite_%'
1072 "] {
1073 $db eval "DROP $type $t"
1074 }
danf5894502009-10-07 18:41:19 +00001075 }
shaneh4e7b32f2009-12-17 22:12:51 +00001076 ifcapable trigger&&foreignkey {
1077 $db eval "PRAGMA foreign_keys = $pk"
1078 }
danf5894502009-10-07 18:41:19 +00001079}
1080
dan71cb5182010-04-26 12:39:03 +00001081#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00001082# If a test script is executed with global variable $::G(perm:name) set to
1083# "wal", then the tests are run in WAL mode. Otherwise, they should be run
1084# in rollback mode. The following Tcl procs are used to make this less
1085# intrusive:
dan71cb5182010-04-26 12:39:03 +00001086#
1087# wal_set_journal_mode ?DB?
1088#
1089# If running a WAL test, execute "PRAGMA journal_mode = wal" using
1090# connection handle DB. Otherwise, this command is a no-op.
1091#
1092# wal_check_journal_mode TESTNAME ?DB?
1093#
1094# If running a WAL test, execute a tests case that fails if the main
1095# database for connection handle DB is not currently a WAL database.
1096# Otherwise (if not running a WAL permutation) this is a no-op.
1097#
1098# wal_is_wal_mode
1099#
1100# Returns true if this test should be run in WAL mode. False otherwise.
1101#
1102proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00001103 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00001104}
1105proc wal_set_journal_mode {{db db}} {
1106 if { [wal_is_wal_mode] } {
1107 $db eval "PRAGMA journal_mode = WAL"
1108 }
1109}
1110proc wal_check_journal_mode {testname {db db}} {
1111 if { [wal_is_wal_mode] } {
1112 $db eval { SELECT * FROM sqlite_master }
1113 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
1114 }
1115}
1116
dan430e74c2010-06-07 17:47:26 +00001117proc permutation {} {
1118 set perm ""
1119 catch {set perm $::G(perm:name)}
1120 set perm
1121}
1122
danc1a60c52010-06-07 14:28:16 +00001123#-------------------------------------------------------------------------
1124#
1125proc slave_test_script {script} {
1126
1127 # Create the interpreter used to run the test script.
1128 interp create tinterp
1129
1130 # Populate some global variables that tester.tcl expects to see.
1131 foreach {var value} [list \
1132 ::argv0 $::argv0 \
1133 ::argv {} \
1134 ::SLAVE 1 \
1135 ] {
1136 interp eval tinterp [list set $var $value]
1137 }
1138
1139 # The alias used to access the global test counters.
1140 tinterp alias set_test_counter set_test_counter
1141
1142 # Set up the ::cmdlinearg array in the slave.
1143 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
1144
dan430e74c2010-06-07 17:47:26 +00001145 # Set up the ::G array in the slave.
1146 interp eval tinterp [list array set ::G [array get ::G]]
1147
danc1a60c52010-06-07 14:28:16 +00001148 # Load the various test interfaces implemented in C.
1149 load_testfixture_extensions tinterp
1150
1151 # Run the test script.
1152 interp eval tinterp $script
1153
1154 # Delete the interpreter used to run the test script.
1155 interp delete tinterp
1156}
1157
dan430e74c2010-06-07 17:47:26 +00001158proc slave_test_file {zFile} {
dan0626dfc2010-06-15 06:56:37 +00001159 set tail [file tail $zFile]
dan430e74c2010-06-07 17:47:26 +00001160
dan0626dfc2010-06-15 06:56:37 +00001161 ifcapable shared_cache {
1162 set scs [sqlite3_enable_shared_cache]
1163 }
1164
1165 reset_prng_state
1166 set ::sqlite_open_file_count 0
1167 set time [time { slave_test_script [list source $zFile] }]
1168 set ms [expr [lindex $time 0] / 1000]
1169
1170 # Test that all files opened by the test script were closed.
1171 #
1172 do_test ${tail}-closeallfiles {
1173 expr {$::sqlite_open_file_count>0}
1174 } {0}
dan430e74c2010-06-07 17:47:26 +00001175 set ::sqlite_open_file_count 0
1176
dan0626dfc2010-06-15 06:56:37 +00001177 # Test that the global "shared-cache" setting was not altered by
1178 # the test script.
1179 #
1180 ifcapable shared_cache {
1181 set res [expr {[sqlite3_enable_shared_cache] == $scs}]
1182 do_test ${tail}-sharedcachesetting [list set {} $res] 1
1183 }
dan430e74c2010-06-07 17:47:26 +00001184
dan430e74c2010-06-07 17:47:26 +00001185 if {$::sqlite_open_file_count>0} {
1186 puts "$tail did not close all files: $::sqlite_open_file_count"
dan0626dfc2010-06-15 06:56:37 +00001187 fail_test "$tail-closeallfiles"
dan430e74c2010-06-07 17:47:26 +00001188 set ::sqlite_open_file_count 0
dan430e74c2010-06-07 17:47:26 +00001189 }
dan0626dfc2010-06-15 06:56:37 +00001190 puts "Time: $tail $ms ms"
dan430e74c2010-06-07 17:47:26 +00001191
1192 show_memstats
danc1a60c52010-06-07 14:28:16 +00001193}
1194
danf5894502009-10-07 18:41:19 +00001195
danielk197745901d62004-11-10 15:27:38 +00001196# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
1197# to non-zero, then set the global variable $AUTOVACUUM to 1.
1198set AUTOVACUUM $sqlite_options(default_autovacuum)
danielk1977ee8b7992009-03-26 17:13:06 +00001199
1200source $testdir/thread_common.tcl