blob: 6416cec1b2dcf59f931308b7b5b1e41f46e412b2 [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
52#
dan430e74c2010-06-07 17:47:26 +000053# Commands providing a lower level interface to the global test counters:
danc1a60c52010-06-07 14:28:16 +000054#
55# set_test_counter COUNTER ?VALUE?
56# omit_test TESTNAME REASON
57# fail_test TESTNAME
58# incr_ntest
59#
60# Command run at the end of each test file:
61#
62# finish_test
63#
dan430e74c2010-06-07 17:47:26 +000064# Commands to help create test files that run with the "WAL" and other
65# permutations (see file permutations.test):
danc1a60c52010-06-07 14:28:16 +000066#
67# wal_is_wal_mode
68# wal_set_journal_mode ?DB?
69# wal_check_journal_mode TESTNAME?DB?
dan430e74c2010-06-07 17:47:26 +000070# permutation
danc1a60c52010-06-07 14:28:16 +000071#
drh348784e2000-05-29 20:41:49 +000072
danc1a60c52010-06-07 14:28:16 +000073# Set the precision of FP arithmatic used by the interpreter. And
74# configure SQLite to take database file locks on the page that begins
75# 64KB into the database file instead of the one 1GB in. This means
76# the code that handles that special case can be tested without creating
77# very large database files.
78#
drh92febd92004-08-20 18:34:20 +000079set tcl_precision 15
drhc7a3bb92009-02-05 16:31:45 +000080sqlite3_test_control_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +000081
danc1a60c52010-06-07 14:28:16 +000082
83# If the pager codec is available, create a wrapper for the [sqlite3]
84# command that appends "-key {xyzzy}" to the command line. i.e. this:
drh3a7fb7c2007-08-10 16:41:08 +000085#
danc1a60c52010-06-07 14:28:16 +000086# sqlite3 db test.db
drh4a50aac2007-08-23 02:47:53 +000087#
danc1a60c52010-06-07 14:28:16 +000088# becomes
drh4a50aac2007-08-23 02:47:53 +000089#
danc1a60c52010-06-07 14:28:16 +000090# sqlite3 db test.db -key {xyzzy}
drh9eb9e262004-02-11 02:18:05 +000091#
dan430e74c2010-06-07 17:47:26 +000092if {[info command sqlite_orig]==""} {
drhef4ac8f2004-06-19 00:16:31 +000093 rename sqlite3 sqlite_orig
94 proc sqlite3 {args} {
drh9eb9e262004-02-11 02:18:05 +000095 if {[llength $args]==2 && [string index [lindex $args 0] 0]!="-"} {
dan430e74c2010-06-07 17:47:26 +000096 # This command is opening a new database connection.
97 #
98 if {[info exists ::G(perm:sqlite3_args)]} {
99 set args [concat $args $::G(perm:sqlite3_args)]
100 }
101 if {[sqlite_orig -has-codec]} {
102 lappend args -key {xyzzy}
103 }
104
dan3ccd20a2010-06-09 19:01:02 +0000105 set res [uplevel 1 sqlite_orig $args]
dan430e74c2010-06-07 17:47:26 +0000106
107 if {[info exists ::G(perm:presql)]} {
108 [lindex $args 0] eval $::G(perm:presql)
109 }
dan3ccd20a2010-06-09 19:01:02 +0000110
111 set res
dan430e74c2010-06-07 17:47:26 +0000112 } else {
113 # This command is not opening a new database connection. Pass the
114 # arguments through to the C implemenation as the are.
115 #
116 uplevel 1 sqlite_orig $args
drh9eb9e262004-02-11 02:18:05 +0000117 }
drh9eb9e262004-02-11 02:18:05 +0000118 }
119}
120
dan430e74c2010-06-07 17:47:26 +0000121# The following block only runs the first time this file is sourced. It
122# does not run in slave interpreters (since the ::cmdlinearg array is
123# populated before the test script is run in slave interpreters).
drhbec2bf42000-05-29 23:48:22 +0000124#
danc1a60c52010-06-07 14:28:16 +0000125if {[info exists cmdlinearg]==0} {
126
127 # Parse any options specified in the $argv array. This script accepts the
128 # following options:
129 #
130 # --pause
131 # --soft-heap-limit=NN
132 # --maxerror=NN
133 # --malloctrace=N
134 # --backtrace=N
135 # --binarylog=N
dan0626dfc2010-06-15 06:56:37 +0000136 # --soak=N
danc1a60c52010-06-07 14:28:16 +0000137 #
138 set cmdlinearg(soft-heap-limit) 0
139 set cmdlinearg(maxerror) 1000
140 set cmdlinearg(malloctrace) 0
141 set cmdlinearg(backtrace) 10
142 set cmdlinearg(binarylog) 0
dan0626dfc2010-06-15 06:56:37 +0000143 set cmdlinearg(soak) 0
danc1a60c52010-06-07 14:28:16 +0000144
145 set leftover [list]
146 foreach a $argv {
147 switch -regexp -- $a {
148 {^-+pause$} {
149 # Wait for user input before continuing. This is to give the user an
150 # opportunity to connect profiling tools to the process.
151 puts -nonewline "Press RETURN to begin..."
152 flush stdout
153 gets stdin
154 }
155 {^-+soft-heap-limit=.+$} {
156 foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break
157 }
158 {^-+maxerror=.+$} {
159 foreach {dummy cmdlinearg(maxerror)} [split $a =] break
160 }
161 {^-+malloctrace=.+$} {
162 foreach {dummy cmdlinearg(malloctrace)} [split $a =] break
163 if {$cmdlinearg(malloctrace)} {
164 sqlite3_memdebug_log start
165 }
166 }
167 {^-+backtrace=.+$} {
168 foreach {dummy cmdlinearg(backtrace)} [split $a =] break
dan0626dfc2010-06-15 06:56:37 +0000169 sqlite3_memdebug_backtrace $value
danc1a60c52010-06-07 14:28:16 +0000170 }
danc1a60c52010-06-07 14:28:16 +0000171 {^-+binarylog=.+$} {
172 foreach {dummy cmdlinearg(binarylog)} [split $a =] break
173 }
dan0626dfc2010-06-15 06:56:37 +0000174 {^-+soak=.+$} {
175 foreach {dummy cmdlinearg(soak)} [split $a =] break
176 set ::G(issoak) $cmdlinearg(soak)
177 }
danc1a60c52010-06-07 14:28:16 +0000178 default {
179 lappend leftover $a
180 }
181 }
182 }
183 set argv $leftover
184
dan430e74c2010-06-07 17:47:26 +0000185 # Install the malloc layer used to inject OOM errors. And the 'automatic'
186 # extensions. This only needs to be done once for the process.
187 #
danielk1977f7b9d662008-06-23 18:49:43 +0000188 sqlite3_shutdown
189 install_malloc_faultsim 1
190 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000191 autoinstall_test_functions
danc1a60c52010-06-07 14:28:16 +0000192
dan430e74c2010-06-07 17:47:26 +0000193 # If the --binarylog option was specified, create the logging VFS. This
194 # call installs the new VFS as the default for all SQLite connections.
195 #
danc1a60c52010-06-07 14:28:16 +0000196 if {$cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000197 vfslog new binarylog {} vfslog.bin
danc1a60c52010-06-07 14:28:16 +0000198 }
199
200 # Set the backtrace depth, if malloc tracing is enabled.
201 #
202 if {$cmdlinearg(malloctrace)} {
203 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danielk1977185eac92008-07-12 15:55:54 +0000204 }
danielk1977f7b9d662008-06-23 18:49:43 +0000205}
danielk197703ba3fa2009-01-09 10:49:14 +0000206
danc1a60c52010-06-07 14:28:16 +0000207# Update the soft-heap-limit each time this script is run. In that
208# way if an individual test file changes the soft-heap-limit, it
209# will be reset at the start of the next test file.
210#
211sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
212
213# Create a test database
214#
danielk197703ba3fa2009-01-09 10:49:14 +0000215proc reset_db {} {
216 catch {db close}
217 file delete -force test.db
218 file delete -force test.db-journal
dand3f8f942010-04-13 11:35:01 +0000219 file delete -force test.db-wal
danielk197703ba3fa2009-01-09 10:49:14 +0000220 sqlite3 db ./test.db
221 set ::DB [sqlite3_connection_pointer db]
222 if {[info exists ::SETUP_SQL]} {
223 db eval $::SETUP_SQL
224 }
drhcd61c282002-03-06 22:01:34 +0000225}
danielk197703ba3fa2009-01-09 10:49:14 +0000226reset_db
drhbec2bf42000-05-29 23:48:22 +0000227
228# Abort early if this script has been run before.
229#
danc1a60c52010-06-07 14:28:16 +0000230if {[info exists TC(count)]} return
drhbec2bf42000-05-29 23:48:22 +0000231
danc1a60c52010-06-07 14:28:16 +0000232# Initialize the test counters and set up commands to access them.
233# Or, if this is a slave interpreter, set up aliases to write the
234# counters in the parent interpreter.
drhbec2bf42000-05-29 23:48:22 +0000235#
danc1a60c52010-06-07 14:28:16 +0000236if {0==[info exists ::SLAVE]} {
237 set TC(errors) 0
238 set TC(count) 0
239 set TC(fail_list) [list]
240 set TC(omit_list) [list]
241
242 proc set_test_counter {counter args} {
243 if {[llength $args]} {
244 set ::TC($counter) [lindex $args 0]
245 }
246 set ::TC($counter)
247 }
drhb62c3352006-11-23 09:39:16 +0000248}
drh348784e2000-05-29 20:41:49 +0000249
drh521cc842008-04-15 02:36:33 +0000250# Record the fact that a sequence of tests were omitted.
251#
252proc omit_test {name reason} {
danc1a60c52010-06-07 14:28:16 +0000253 set omitList [set_test_counter omit_list]
drh521cc842008-04-15 02:36:33 +0000254 lappend omitList [list $name $reason]
danc1a60c52010-06-07 14:28:16 +0000255 set_test_counter omit_list $omitList
drh521cc842008-04-15 02:36:33 +0000256}
257
danc1a60c52010-06-07 14:28:16 +0000258# Record the fact that a test failed.
259#
260proc fail_test {name} {
261 set f [set_test_counter fail_list]
262 lappend f $name
263 set_test_counter fail_list $f
264 set_test_counter errors [expr [set_test_counter errors] + 1]
265
266 set nFail [set_test_counter errors]
267 if {$nFail>=$::cmdlinearg(maxerror)} {
268 puts "*** Giving up..."
269 finalize_testing
270 }
271}
272
273# Increment the number of tests run
274#
275proc incr_ntest {} {
276 set_test_counter count [expr [set_test_counter count] + 1]
277}
278
279
drh348784e2000-05-29 20:41:49 +0000280# Invoke the do_test procedure to run a single test
281#
282proc do_test {name cmd expected} {
danc1a60c52010-06-07 14:28:16 +0000283
284 global argv cmdlinearg
285
drh4a50aac2007-08-23 02:47:53 +0000286 sqlite3_memdebug_settitle $name
danc1a60c52010-06-07 14:28:16 +0000287
drh767c2002000-10-19 14:10:08 +0000288 if {[llength $argv]==0} {
drh348784e2000-05-29 20:41:49 +0000289 set go 1
290 } else {
291 set go 0
292 foreach pattern $argv {
293 if {[string match $pattern $name]} {
294 set go 1
295 break
296 }
297 }
298 }
299 if {!$go} return
dan430e74c2010-06-07 17:47:26 +0000300
301 if {[info exists ::G(perm:name)]} {
302 set name "$::G(perm:name)-$name"
303 }
304
danc1a60c52010-06-07 14:28:16 +0000305 incr_ntest
drh5edc3122001-09-13 21:53:09 +0000306 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000307 flush stdout
308 if {[catch {uplevel #0 "$cmd;\n"} result]} {
309 puts "\nError: $result"
danc1a60c52010-06-07 14:28:16 +0000310 fail_test $name
drh348784e2000-05-29 20:41:49 +0000311 } elseif {[string compare $result $expected]} {
312 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
danc1a60c52010-06-07 14:28:16 +0000313 fail_test $name
drh348784e2000-05-29 20:41:49 +0000314 } else {
315 puts " Ok"
316 }
drh6558db82007-04-13 03:23:21 +0000317 flush stdout
drh348784e2000-05-29 20:41:49 +0000318}
319
drhb62c3352006-11-23 09:39:16 +0000320# Run an SQL script.
321# Return the number of microseconds per statement.
322#
drh3590f152006-11-23 21:09:10 +0000323proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000324 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000325 flush stdout
326 set speed [time {sqlite3_exec_nr db $sql}]
327 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000328 if {$tm == 0} {
329 set rate [format %20s "many"]
330 } else {
331 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
332 }
drh3590f152006-11-23 21:09:10 +0000333 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000334 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000335 global total_time
336 set total_time [expr {$total_time+$tm}]
337}
drh45c236d2008-03-22 01:08:00 +0000338proc speed_trial_tcl {name numstmt units script} {
339 puts -nonewline [format {%-21.21s } $name...]
340 flush stdout
341 set speed [time {eval $script}]
342 set tm [lindex $speed 0]
343 if {$tm == 0} {
344 set rate [format %20s "many"]
345 } else {
346 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
347 }
348 set u2 $units/s
349 puts [format {%12d uS %s %s} $tm $rate $u2]
350 global total_time
351 set total_time [expr {$total_time+$tm}]
352}
drhdd924312007-03-31 22:34:16 +0000353proc speed_trial_init {name} {
354 global total_time
355 set total_time 0
356}
357proc speed_trial_summary {name} {
358 global total_time
359 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
drhb62c3352006-11-23 09:39:16 +0000360}
361
drh348784e2000-05-29 20:41:49 +0000362# Run this routine last
363#
364proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +0000365 catch {db close}
366 catch {db2 close}
367 catch {db3 close}
368 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +0000369}
370proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +0000371 global sqlite_open_file_count
372
373 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +0000374
drh6e142f52000-06-08 13:36:40 +0000375 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000376 catch {db2 close}
377 catch {db3 close}
378
drh9bc54492007-10-23 14:49:59 +0000379 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000380 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000381 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000382 db close
drh984bfaa2008-03-19 16:08:53 +0000383 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +0000384
drh3a7fb7c2007-08-10 16:41:08 +0000385 sqlite3_soft_heap_limit 0
danc1a60c52010-06-07 14:28:16 +0000386 set nTest [incr_ntest]
387 set nErr [set_test_counter errors]
388
drh348784e2000-05-29 20:41:49 +0000389 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000390 if {$nErr>0} {
danc1a60c52010-06-07 14:28:16 +0000391 puts "Failures on these tests: [set_test_counter fail_list]"
drhf3a65f72007-08-22 20:18:21 +0000392 }
danielk1977ee8b7992009-03-26 17:13:06 +0000393 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +0000394 if {[llength $omitList]>0} {
395 puts "Omitted test cases:"
396 set prec {}
397 foreach {rec} [lsort $omitList] {
398 if {$rec==$prec} continue
399 set prec $rec
400 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
401 }
402 }
drh80788d82006-09-02 14:50:23 +0000403 if {$nErr>0 && ![working_64bit_int]} {
404 puts "******************************************************************"
405 puts "N.B.: The version of TCL that you used to build this test harness"
406 puts "is defective in that it does not support 64-bit integers. Some or"
407 puts "all of the test failures above might be a result from this defect"
408 puts "in your TCL build."
409 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000410 }
danc1a60c52010-06-07 14:28:16 +0000411 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000412 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +0000413 }
drh94e92032003-02-16 22:21:32 +0000414 if {$sqlite_open_file_count} {
415 puts "$sqlite_open_file_count files were left open"
416 incr nErr
417 }
drhf3a65f72007-08-22 20:18:21 +0000418 if {[sqlite3_memory_used]>0} {
419 puts "Unfreed memory: [sqlite3_memory_used] bytes"
420 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000421 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000422 puts "Writing unfreed memory log to \"./memleak.txt\""
423 sqlite3_memdebug_dump ./memleak.txt
424 }
drhf3a65f72007-08-22 20:18:21 +0000425 } else {
426 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000427 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000428 sqlite3_memdebug_dump ./memusage.txt
429 }
drhf3a65f72007-08-22 20:18:21 +0000430 }
drhf7141992008-06-19 00:16:08 +0000431 show_memstats
drh91fd4d42008-01-19 20:11:25 +0000432 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
433 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000434 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
435 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
436 }
danc1a60c52010-06-07 14:28:16 +0000437 if {$::cmdlinearg(malloctrace)} {
danielk197735754ac2008-03-21 17:29:37 +0000438 puts "Writing mallocs.sql..."
439 memdebug_log_sql
440 sqlite3_memdebug_log stop
441 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000442
443 if {[sqlite3_memory_used]>0} {
444 puts "Writing leaks.sql..."
445 sqlite3_memdebug_log sync
446 memdebug_log_sql leaks.sql
447 }
danielk197735754ac2008-03-21 17:29:37 +0000448 }
drhd9910fe2006-10-04 11:55:49 +0000449 foreach f [glob -nocomplain test.db-*-journal] {
450 file delete -force $f
451 }
452 foreach f [glob -nocomplain test.db-mj*] {
453 file delete -force $f
454 }
drhdb25e382001-03-15 18:21:22 +0000455 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000456}
457
drhf7141992008-06-19 00:16:08 +0000458# Display memory statistics for analysis and debugging purposes.
459#
460proc show_memstats {} {
461 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +0000462 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
463 set val [format {now %10d max %10d max-size %10d} \
464 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000465 puts "Memory used: $val"
466 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +0000467 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
468 set val [format {now %10d max %10d max-size %10d} \
469 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000470 puts "Page-cache used: $val"
471 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
472 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
473 puts "Page-cache overflow: $val"
474 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
475 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
476 puts "Scratch memory used: $val"
477 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +0000478 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
479 set val [format {now %10d max %10d max-size %10d} \
480 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000481 puts "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +0000482 ifcapable yytrackmaxstackdepth {
483 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
484 set val [format { max %10d} [lindex $x 2]]
485 puts "Parser stack depth: $val"
486 }
drhf7141992008-06-19 00:16:08 +0000487}
488
drh348784e2000-05-29 20:41:49 +0000489# A procedure to execute SQL
490#
drhc4a3c772001-04-04 11:48:57 +0000491proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000492 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000493 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000494}
drh3aadb2e2000-05-31 17:59:25 +0000495
drhadbca9c2001-09-27 15:11:53 +0000496# Execute SQL and catch exceptions.
497#
498proc catchsql {sql {db db}} {
499 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +0000500 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +0000501 lappend r $msg
502 return $r
503}
504
drh04096482001-11-09 22:41:44 +0000505# Do an VDBE code dump on the SQL given
506#
507proc explain {sql {db db}} {
508 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000509 puts "addr opcode p1 p2 p3 p4 p5 #"
510 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000511 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000512 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
513 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
514 ]
drh04096482001-11-09 22:41:44 +0000515 }
516}
517
drhdafc0ce2008-04-17 19:14:02 +0000518# Show the VDBE program for an SQL statement but omit the Trace
519# opcode at the beginning. This procedure can be used to prove
520# that different SQL statements generate exactly the same VDBE code.
521#
522proc explain_no_trace {sql} {
523 set tr [db eval "EXPLAIN $sql"]
524 return [lrange $tr 7 end]
525}
526
drh3aadb2e2000-05-31 17:59:25 +0000527# Another procedure to execute SQL. This one includes the field
528# names in the returned list.
529#
530proc execsql2 {sql} {
531 set result {}
532 db eval $sql data {
533 foreach f $data(*) {
534 lappend result $f $data($f)
535 }
536 }
537 return $result
538}
drh17a68932001-01-31 13:28:08 +0000539
drhdde85d92003-03-01 19:45:34 +0000540# Use the non-callback API to execute multiple SQL statements
541#
542proc stepsql {dbptr sql} {
543 set sql [string trim $sql]
544 set r 0
545 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000546 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000547 return [list 1 $vm]
548 }
549 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000550# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
551# foreach v $VAL {lappend r $v}
552# }
553 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
554 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
555 lappend r [sqlite3_column_text $vm $i]
556 }
drhdde85d92003-03-01 19:45:34 +0000557 }
danielk1977106bb232004-05-21 10:08:53 +0000558 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000559 return [list 1 $errmsg]
560 }
561 }
562 return $r
563}
564
drh17a68932001-01-31 13:28:08 +0000565# Delete a file or directory
566#
567proc forcedelete {filename} {
568 if {[catch {file delete -force $filename}]} {
569 exec rm -rf $filename
570 }
571}
drh21504322002-06-25 13:16:02 +0000572
573# Do an integrity check of the entire database
574#
danielk197704103022009-02-03 16:51:24 +0000575proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +0000576 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +0000577 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +0000578 }
579}
580
danielk1977db4e8862008-01-16 08:24:46 +0000581proc fix_ifcapable_expr {expr} {
582 set ret ""
583 set state 0
584 for {set i 0} {$i < [string length $expr]} {incr i} {
585 set char [string range $expr $i $i]
586 set newstate [expr {[string is alnum $char] || $char eq "_"}]
587 if {$newstate && !$state} {
588 append ret {$::sqlite_options(}
589 }
590 if {!$newstate && $state} {
591 append ret )
592 }
593 append ret $char
594 set state $newstate
595 }
596 if {$state} {append ret )}
597 return $ret
598}
599
drh27d258a2004-10-30 20:23:09 +0000600# Evaluate a boolean expression of capabilities. If true, execute the
601# code. Omit the code if false.
602#
danielk19773e8c37e2005-01-21 03:12:14 +0000603proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +0000604 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
605 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +0000606 if ($e2) {
607 set c [catch {uplevel 1 $code} r]
608 } else {
609 set c [catch {uplevel 1 $elsecode} r]
610 }
611 return -code $c $r
drh21504322002-06-25 13:16:02 +0000612}
danielk197745901d62004-11-10 15:27:38 +0000613
danielk1977aca790a2005-01-13 11:07:52 +0000614# This proc execs a seperate process that crashes midway through executing
615# the SQL script $sql on database test.db.
616#
617# The crash occurs during a sync() of file $crashfile. When the crash
618# occurs a random subset of all unsynced writes made by the process are
619# written into the files on disk. Argument $crashdelay indicates the
620# number of file syncs to wait before crashing.
621#
622# The return value is a list of two elements. The first element is a
623# boolean, indicating whether or not the process actually crashed or
624# reported some other error. The second element in the returned list is the
625# error message. This is "child process exited abnormally" if the crash
626# occured.
627#
danielk1977f8940ae2007-08-23 11:07:10 +0000628# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +0000629#
630proc crashsql {args} {
danielk1977aca790a2005-01-13 11:07:52 +0000631 if {$::tcl_platform(platform)!="unix"} {
632 error "crashsql should only be used on unix"
633 }
danielk197759a33f92007-03-17 10:26:59 +0000634
635 set blocksize ""
636 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +0000637 set prngseed 0
drhf8587402008-01-08 16:03:49 +0000638 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +0000639 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +0000640 set dc ""
danielk197759a33f92007-03-17 10:26:59 +0000641 set sql [lindex $args end]
642
643 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
644 set z [lindex $args $ii]
645 set n [string length $z]
646 set z2 [lindex $args [expr $ii+1]]
647
648 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +0000649 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +0000650 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +0000651 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000652 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +0000653 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +0000654 else { error "Unrecognized option: $z" }
655 }
656
657 if {$crashfile eq ""} {
658 error "Compulsory option -file missing"
659 }
660
danielk1977aca790a2005-01-13 11:07:52 +0000661 set cfile [file join [pwd] $crashfile]
662
663 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +0000664 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +0000665 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +0000666 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +0000667 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +0000668
669 # This block sets the cache size of the main database to 10
670 # pages. This is done in case the build is configured to omit
671 # "PRAGMA cache_size".
672 puts $f {db eval {SELECT * FROM sqlite_master;}}
673 puts $f {set bt [btree_from_db db]}
674 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +0000675 if {$prngseed} {
676 set seed [expr {$prngseed%10007+1}]
677 # puts seed=$seed
678 puts $f "db eval {SELECT randomblob($seed)}"
679 }
danielk1977933bbd62007-03-17 07:22:42 +0000680
drhf8587402008-01-08 16:03:49 +0000681 if {[string length $tclbody]>0} {
682 puts $f $tclbody
683 }
684 if {[string length $sql]>0} {
685 puts $f "db eval {"
686 puts $f "$sql"
687 puts $f "}"
688 }
danielk1977aca790a2005-01-13 11:07:52 +0000689 close $f
690
691 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000692 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000693 } msg]
694 lappend r $msg
695}
696
danielk197732554c12005-01-22 03:39:39 +0000697# Usage: do_ioerr_test <test number> <options...>
698#
699# This proc is used to implement test cases that check that IO errors
700# are correctly handled. The first argument, <test number>, is an integer
701# used to name the tests executed by this proc. Options are as follows:
702#
703# -tclprep TCL script to run to prepare test.
704# -sqlprep SQL script to run to prepare test.
705# -tclbody TCL script to run with IO error simulation.
706# -sqlbody TCL script to run with IO error simulation.
707# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000708# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +0000709# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +0000710# -start Value of 'N' to begin with (default 1)
711#
712# -cksum Boolean. If true, test that the database does
713# not change during the execution of the test case.
714#
715proc do_ioerr_test {testname args} {
716
danielk197732554c12005-01-22 03:39:39 +0000717 set ::ioerropts(-start) 1
718 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000719 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +0000720 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +0000721 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +0000722 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +0000723 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +0000724 array set ::ioerropts $args
725
danielk197747cd39c2008-05-12 12:41:15 +0000726 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
727 # a couple of obscure IO errors that do not return them.
728 set ::ioerropts(-erc) 0
729
danielk197732554c12005-01-22 03:39:39 +0000730 set ::go 1
drh93aed5a2008-01-16 17:46:38 +0000731 #reset_prng_state
732 save_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +0000733 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +0000734 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +0000735 incr ::ioerropts(-count) -1
736 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +0000737
738 # Skip this IO error if it was specified with the "-exclude" option.
739 if {[info exists ::ioerropts(-exclude)]} {
740 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
741 }
danielk1977861f7452008-06-05 11:39:11 +0000742 if {$::ioerropts(-restoreprng)} {
743 restore_prng_state
744 }
danielk197732554c12005-01-22 03:39:39 +0000745
746 # Delete the files test.db and test2.db, then execute the TCL and
747 # SQL (in that order) to prepare for the test case.
748 do_test $testname.$n.1 {
749 set ::sqlite_io_error_pending 0
750 catch {db close}
aswiftaebf4132008-11-21 00:10:35 +0000751 catch {db2 close}
danielk197732554c12005-01-22 03:39:39 +0000752 catch {file delete -force test.db}
753 catch {file delete -force test.db-journal}
754 catch {file delete -force test2.db}
755 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000756 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000757 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000758 if {[info exists ::ioerropts(-tclprep)]} {
759 eval $::ioerropts(-tclprep)
760 }
761 if {[info exists ::ioerropts(-sqlprep)]} {
762 execsql $::ioerropts(-sqlprep)
763 }
764 expr 0
765 } {0}
766
767 # Read the 'checksum' of the database.
768 if {$::ioerropts(-cksum)} {
769 set checksum [cksum]
770 }
drh93aed5a2008-01-16 17:46:38 +0000771
danielk197732554c12005-01-22 03:39:39 +0000772 # Set the Nth IO error to fail.
773 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +0000774 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +0000775 set ::sqlite_io_error_pending $n
776 }] $n
777
778 # Create a single TCL script from the TCL and SQL specified
779 # as the body of the test.
780 set ::ioerrorbody {}
781 if {[info exists ::ioerropts(-tclbody)]} {
782 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
783 }
784 if {[info exists ::ioerropts(-sqlbody)]} {
785 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
786 }
787
788 # Execute the TCL Script created in the above block. If
789 # there are at least N IO operations performed by SQLite as
790 # a result of the script, the Nth will fail.
791 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +0000792 set ::sqlite_io_error_hit 0
793 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +0000794 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +0000795 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +0000796 set rc [sqlite3_errcode $::DB]
797 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +0000798 # If we are in extended result code mode, make sure all of the
799 # IOERRs we get back really do have their extended code values.
800 # If an extended result code is returned, the sqlite3_errcode
801 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
802 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +0000803 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
804 return $rc
805 }
806 } else {
drh5f7b5bf2007-04-19 12:30:54 +0000807 # If we are not in extended result code mode, make sure no
808 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +0000809 if {[regexp {\+\d} $rc]} {
810 return $rc
811 }
812 }
drh93aed5a2008-01-16 17:46:38 +0000813 # The test repeats as long as $::go is non-zero. $::go starts out
814 # as 1. When a test runs to completion without hitting an I/O
815 # error, that means there is no point in continuing with this test
816 # case so set $::go to zero.
817 #
818 if {$::sqlite_io_error_pending>0} {
819 set ::go 0
820 set q 0
821 set ::sqlite_io_error_pending 0
822 } else {
823 set q 1
824 }
825
drhc9ac5ca2005-11-04 22:03:30 +0000826 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +0000827 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
828 set r 1
829 }
danielk1977f2fa8312006-01-24 13:09:33 +0000830 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +0000831
832 # One of two things must have happened. either
833 # 1. We never hit the IO error and the SQL returned OK
834 # 2. An IO error was hit and the SQL failed
835 #
dand41a29a2010-05-06 15:56:28 +0000836 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +0000837 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +0000838 } {1}
839
danielk197780daec62008-05-12 10:57:02 +0000840 set ::sqlite_io_error_hit 0
841 set ::sqlite_io_error_pending 0
842
danielk197752b472a2008-05-05 16:23:55 +0000843 # Check that no page references were leaked. There should be
844 # a single reference if there is still an active transaction,
845 # or zero otherwise.
846 #
danielk197781620542008-06-07 05:19:37 +0000847 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
848 # locks are established on database files (i.e. if the error
849 # occurs while attempting to detect a hot-journal file), then
850 # there may 0 page references and an active transaction according
851 # to [sqlite3_get_autocommit].
852 #
danielk19774abd5442008-05-05 15:26:50 +0000853 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +0000854 do_test $testname.$n.4 {
855 set bt [btree_from_db db]
856 db_enter db
857 array set stats [btree_pager_stats $bt]
858 db_leave db
danielk197781620542008-06-07 05:19:37 +0000859 set nRef $stats(ref)
860 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
861 } {1}
danielk19774abd5442008-05-05 15:26:50 +0000862 }
863
danielk197752b472a2008-05-05 16:23:55 +0000864 # If there is an open database handle and no open transaction,
865 # and the pager is not running in exclusive-locking mode,
866 # check that the pager is in "unlocked" state. Theoretically,
867 # if a call to xUnlock() failed due to an IO error the underlying
868 # file may still be locked.
869 #
870 ifcapable pragma {
871 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +0000872 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +0000873 && [db one {pragma locking_mode}] eq "normal"
874 && [sqlite3_get_autocommit db]
875 } {
876 do_test $testname.$n.5 {
877 set bt [btree_from_db db]
878 db_enter db
879 array set stats [btree_pager_stats $bt]
880 db_leave db
881 set stats(state)
882 } 0
883 }
884 }
885
danielk197732554c12005-01-22 03:39:39 +0000886 # If an IO error occured, then the checksum of the database should
887 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +0000888 #
drh1aa5af12008-03-07 19:51:14 +0000889 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +0000890 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +0000891 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +0000892 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +0000893 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +0000894 cksum
895 } $checksum
896 }
897
drh1aa5af12008-03-07 19:51:14 +0000898 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +0000899 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +0000900 if {[info exists ::ioerropts(-cleanup)]} {
901 catch $::ioerropts(-cleanup)
902 }
danielk197732554c12005-01-22 03:39:39 +0000903 }
904 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +0000905 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +0000906 unset ::ioerropts
907}
908
drh93aed5a2008-01-16 17:46:38 +0000909# Return a checksum based on the contents of the main database associated
910# with connection $db
danielk197732554c12005-01-22 03:39:39 +0000911#
912proc cksum {{db db}} {
913 set txt [$db eval {
914 SELECT name, type, sql FROM sqlite_master order by name
915 }]\n
916 foreach tbl [$db eval {
917 SELECT name FROM sqlite_master WHERE type='table' order by name
918 }] {
919 append txt [$db eval "SELECT * FROM $tbl"]\n
920 }
921 foreach prag {default_synchronous default_cache_size} {
922 append txt $prag-[$db eval "PRAGMA $prag"]\n
923 }
924 set cksum [string length $txt]-[md5 $txt]
925 # puts $cksum-[file size test.db]
926 return $cksum
927}
928
drh93aed5a2008-01-16 17:46:38 +0000929# Generate a checksum based on the contents of the main and temp tables
930# database $db. If the checksum of two databases is the same, and the
931# integrity-check passes for both, the two databases are identical.
932#
drh1db639c2008-01-17 02:36:28 +0000933proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +0000934 set ret [list]
935 ifcapable tempdb {
936 set sql {
937 SELECT name FROM sqlite_master WHERE type = 'table' UNION
938 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
939 SELECT 'sqlite_master' UNION
940 SELECT 'sqlite_temp_master' ORDER BY 1
941 }
942 } else {
943 set sql {
944 SELECT name FROM sqlite_master WHERE type = 'table' UNION
945 SELECT 'sqlite_master' ORDER BY 1
946 }
947 }
948 set tbllist [$db eval $sql]
949 set txt {}
950 foreach tbl $tbllist {
951 append txt [$db eval "SELECT * FROM $tbl"]
952 }
953 foreach prag {default_cache_size} {
954 append txt $prag-[$db eval "PRAGMA $prag"]\n
955 }
956 # puts txt=$txt
957 return [md5 $txt]
958}
959
drhdc2c4912009-02-04 22:46:47 +0000960# Generate a checksum based on the contents of a single database with
961# a database connection. The name of the database is $dbname.
962# Examples of $dbname are "temp" or "main".
963#
964proc dbcksum {db dbname} {
965 if {$dbname=="temp"} {
966 set master sqlite_temp_master
967 } else {
968 set master $dbname.sqlite_master
969 }
970 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
971 set txt [$db eval "SELECT * FROM $master"]\n
972 foreach tab $alltab {
973 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
974 }
975 return [md5 $txt]
976}
977
danielk197735754ac2008-03-21 17:29:37 +0000978proc memdebug_log_sql {{filename mallocs.sql}} {
979
danielk19776f332c12008-03-21 14:22:44 +0000980 set data [sqlite3_memdebug_log dump]
981 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +0000982 if {$nFrame < 0} { return "" }
983
danielk197735754ac2008-03-21 17:29:37 +0000984 set database temp
985
danielk19776ab3a2e2009-02-19 14:39:25 +0000986 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +0000987
988 set sql ""
989 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +0000990 set nCall [lindex $e 0]
991 set nByte [lindex $e 1]
992 set lStack [lrange $e 2 end]
993 append sql "INSERT INTO ${database}.malloc VALUES"
994 append sql "('test', $nCall, $nByte, '$lStack');\n"
995 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +0000996 set frames($f) 1
997 }
998 }
999
1000 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +00001001 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +00001002
1003 foreach f [array names frames] {
1004 set addr [format %x $f]
1005 set cmd "addr2line -e [info nameofexec] $addr"
1006 set line [eval exec $cmd]
1007 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00001008
1009 set file [lindex [split $line :] 0]
1010 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00001011 }
1012
danielk197735754ac2008-03-21 17:29:37 +00001013 foreach f [array names files] {
1014 set contents ""
1015 catch {
1016 set fd [open $f]
1017 set contents [read $fd]
1018 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001019 }
danielk197735754ac2008-03-21 17:29:37 +00001020 set contents [string map {' ''} $contents]
1021 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00001022 }
danielk19776f332c12008-03-21 14:22:44 +00001023
danielk197735754ac2008-03-21 17:29:37 +00001024 set fd [open $filename w]
1025 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
1026 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001027}
drh93aed5a2008-01-16 17:46:38 +00001028
danielk197732554c12005-01-22 03:39:39 +00001029# Copy file $from into $to. This is used because some versions of
1030# TCL for windows (notably the 8.4.1 binary package shipped with the
1031# current mingw release) have a broken "file copy" command.
1032#
1033proc copy_file {from to} {
1034 if {$::tcl_platform(platform)=="unix"} {
1035 file copy -force $from $to
1036 } else {
1037 set f [open $from]
1038 fconfigure $f -translation binary
1039 set t [open $to w]
1040 fconfigure $t -translation binary
1041 puts -nonewline $t [read $f [file size $from]]
1042 close $t
1043 close $f
1044 }
1045}
1046
danf5894502009-10-07 18:41:19 +00001047# Drop all tables in database [db]
1048proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00001049 ifcapable trigger&&foreignkey {
1050 set pk [$db one "PRAGMA foreign_keys"]
1051 $db eval "PRAGMA foreign_keys = OFF"
1052 }
drh9a6ffc82010-02-15 18:03:20 +00001053 foreach {idx name file} [db eval {PRAGMA database_list}] {
1054 if {$idx==1} {
1055 set master sqlite_temp_master
1056 } else {
1057 set master $name.sqlite_master
1058 }
1059 foreach {t type} [$db eval "
1060 SELECT name, type FROM $master
1061 WHERE type IN('table', 'view') AND name NOT like 'sqlite_%'
1062 "] {
1063 $db eval "DROP $type $t"
1064 }
danf5894502009-10-07 18:41:19 +00001065 }
shaneh4e7b32f2009-12-17 22:12:51 +00001066 ifcapable trigger&&foreignkey {
1067 $db eval "PRAGMA foreign_keys = $pk"
1068 }
danf5894502009-10-07 18:41:19 +00001069}
1070
dan71cb5182010-04-26 12:39:03 +00001071#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00001072# If a test script is executed with global variable $::G(perm:name) set to
1073# "wal", then the tests are run in WAL mode. Otherwise, they should be run
1074# in rollback mode. The following Tcl procs are used to make this less
1075# intrusive:
dan71cb5182010-04-26 12:39:03 +00001076#
1077# wal_set_journal_mode ?DB?
1078#
1079# If running a WAL test, execute "PRAGMA journal_mode = wal" using
1080# connection handle DB. Otherwise, this command is a no-op.
1081#
1082# wal_check_journal_mode TESTNAME ?DB?
1083#
1084# If running a WAL test, execute a tests case that fails if the main
1085# database for connection handle DB is not currently a WAL database.
1086# Otherwise (if not running a WAL permutation) this is a no-op.
1087#
1088# wal_is_wal_mode
1089#
1090# Returns true if this test should be run in WAL mode. False otherwise.
1091#
1092proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00001093 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00001094}
1095proc wal_set_journal_mode {{db db}} {
1096 if { [wal_is_wal_mode] } {
1097 $db eval "PRAGMA journal_mode = WAL"
1098 }
1099}
1100proc wal_check_journal_mode {testname {db db}} {
1101 if { [wal_is_wal_mode] } {
1102 $db eval { SELECT * FROM sqlite_master }
1103 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
1104 }
1105}
1106
dan430e74c2010-06-07 17:47:26 +00001107proc permutation {} {
1108 set perm ""
1109 catch {set perm $::G(perm:name)}
1110 set perm
1111}
1112
danc1a60c52010-06-07 14:28:16 +00001113#-------------------------------------------------------------------------
1114#
1115proc slave_test_script {script} {
1116
1117 # Create the interpreter used to run the test script.
1118 interp create tinterp
1119
1120 # Populate some global variables that tester.tcl expects to see.
1121 foreach {var value} [list \
1122 ::argv0 $::argv0 \
1123 ::argv {} \
1124 ::SLAVE 1 \
1125 ] {
1126 interp eval tinterp [list set $var $value]
1127 }
1128
1129 # The alias used to access the global test counters.
1130 tinterp alias set_test_counter set_test_counter
1131
1132 # Set up the ::cmdlinearg array in the slave.
1133 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
1134
dan430e74c2010-06-07 17:47:26 +00001135 # Set up the ::G array in the slave.
1136 interp eval tinterp [list array set ::G [array get ::G]]
1137
danc1a60c52010-06-07 14:28:16 +00001138 # Load the various test interfaces implemented in C.
1139 load_testfixture_extensions tinterp
1140
1141 # Run the test script.
1142 interp eval tinterp $script
1143
1144 # Delete the interpreter used to run the test script.
1145 interp delete tinterp
1146}
1147
dan430e74c2010-06-07 17:47:26 +00001148proc slave_test_file {zFile} {
dan0626dfc2010-06-15 06:56:37 +00001149 set tail [file tail $zFile]
dan430e74c2010-06-07 17:47:26 +00001150
dan0626dfc2010-06-15 06:56:37 +00001151 ifcapable shared_cache {
1152 set scs [sqlite3_enable_shared_cache]
1153 }
1154
1155 reset_prng_state
1156 set ::sqlite_open_file_count 0
1157 set time [time { slave_test_script [list source $zFile] }]
1158 set ms [expr [lindex $time 0] / 1000]
1159
1160 # Test that all files opened by the test script were closed.
1161 #
1162 do_test ${tail}-closeallfiles {
1163 expr {$::sqlite_open_file_count>0}
1164 } {0}
dan430e74c2010-06-07 17:47:26 +00001165 set ::sqlite_open_file_count 0
1166
dan0626dfc2010-06-15 06:56:37 +00001167 # Test that the global "shared-cache" setting was not altered by
1168 # the test script.
1169 #
1170 ifcapable shared_cache {
1171 set res [expr {[sqlite3_enable_shared_cache] == $scs}]
1172 do_test ${tail}-sharedcachesetting [list set {} $res] 1
1173 }
dan430e74c2010-06-07 17:47:26 +00001174
dan430e74c2010-06-07 17:47:26 +00001175 if {$::sqlite_open_file_count>0} {
1176 puts "$tail did not close all files: $::sqlite_open_file_count"
dan0626dfc2010-06-15 06:56:37 +00001177 fail_test "$tail-closeallfiles"
dan430e74c2010-06-07 17:47:26 +00001178 set ::sqlite_open_file_count 0
dan430e74c2010-06-07 17:47:26 +00001179 }
dan0626dfc2010-06-15 06:56:37 +00001180 puts "Time: $tail $ms ms"
dan430e74c2010-06-07 17:47:26 +00001181
1182 show_memstats
danc1a60c52010-06-07 14:28:16 +00001183}
1184
danf5894502009-10-07 18:41:19 +00001185
danielk197745901d62004-11-10 15:27:38 +00001186# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
1187# to non-zero, then set the global variable $AUTOVACUUM to 1.
1188set AUTOVACUUM $sqlite_options(default_autovacuum)
danielk1977ee8b7992009-03-26 17:13:06 +00001189
1190source $testdir/thread_common.tcl