blob: 5bc7eb704da75819240c2982f04d2f0f47bc8472 [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#
mistachkinf8a78462012-03-08 20:00:36 +000022# get_pwd
danc1a60c52010-06-07 14:28:16 +000023# copy_file FROM TO
mistachkinfda06be2011-08-02 00:57:34 +000024# delete_file FILENAME
dan6f343962011-07-01 18:26:40 +000025# drop_all_tables ?DB?
mistachkinfda06be2011-08-02 00:57:34 +000026# forcecopy FROM TO
danc1a60c52010-06-07 14:28:16 +000027# forcedelete FILENAME
28#
29# Test the capability of the SQLite version built into the interpreter to
30# determine if a specific test can be run:
31#
32# ifcapable EXPR
33#
34# Calulate checksums based on database contents:
35#
36# dbcksum DB DBNAME
37# allcksum ?DB?
38# cksum ?DB?
39#
40# Commands to execute/explain SQL statements:
41#
42# stepsql DB SQL
43# execsql2 SQL
44# explain_no_trace SQL
45# explain SQL ?DB?
46# catchsql SQL ?DB?
47# execsql SQL ?DB?
48#
49# Commands to run test cases:
50#
51# do_ioerr_test TESTNAME ARGS...
52# crashsql ARGS...
53# integrity_check TESTNAME ?DB?
54# do_test TESTNAME SCRIPT EXPECTED
dan153eda02010-06-21 07:45:47 +000055# do_execsql_test TESTNAME SQL EXPECTED
56# do_catchsql_test TESTNAME SQL EXPECTED
danc1a60c52010-06-07 14:28:16 +000057#
dan430e74c2010-06-07 17:47:26 +000058# Commands providing a lower level interface to the global test counters:
danc1a60c52010-06-07 14:28:16 +000059#
60# set_test_counter COUNTER ?VALUE?
mistachkin6c3c1a02011-11-12 03:17:40 +000061# omit_test TESTNAME REASON ?APPEND?
danc1a60c52010-06-07 14:28:16 +000062# fail_test TESTNAME
63# incr_ntest
64#
65# Command run at the end of each test file:
66#
67# finish_test
68#
dan430e74c2010-06-07 17:47:26 +000069# Commands to help create test files that run with the "WAL" and other
70# permutations (see file permutations.test):
danc1a60c52010-06-07 14:28:16 +000071#
72# wal_is_wal_mode
73# wal_set_journal_mode ?DB?
74# wal_check_journal_mode TESTNAME?DB?
dan430e74c2010-06-07 17:47:26 +000075# permutation
dancb79e512010-08-06 13:50:07 +000076# presql
danc1a60c52010-06-07 14:28:16 +000077#
drh348784e2000-05-29 20:41:49 +000078
danc1a60c52010-06-07 14:28:16 +000079# Set the precision of FP arithmatic used by the interpreter. And
80# configure SQLite to take database file locks on the page that begins
81# 64KB into the database file instead of the one 1GB in. This means
82# the code that handles that special case can be tested without creating
83# very large database files.
84#
drh92febd92004-08-20 18:34:20 +000085set tcl_precision 15
drhc7a3bb92009-02-05 16:31:45 +000086sqlite3_test_control_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +000087
danc1a60c52010-06-07 14:28:16 +000088
89# If the pager codec is available, create a wrapper for the [sqlite3]
90# command that appends "-key {xyzzy}" to the command line. i.e. this:
drh3a7fb7c2007-08-10 16:41:08 +000091#
danc1a60c52010-06-07 14:28:16 +000092# sqlite3 db test.db
drh4a50aac2007-08-23 02:47:53 +000093#
danc1a60c52010-06-07 14:28:16 +000094# becomes
drh4a50aac2007-08-23 02:47:53 +000095#
danc1a60c52010-06-07 14:28:16 +000096# sqlite3 db test.db -key {xyzzy}
drh9eb9e262004-02-11 02:18:05 +000097#
dan430e74c2010-06-07 17:47:26 +000098if {[info command sqlite_orig]==""} {
drhef4ac8f2004-06-19 00:16:31 +000099 rename sqlite3 sqlite_orig
100 proc sqlite3 {args} {
dan68928b62010-06-22 13:46:43 +0000101 if {[llength $args]>=2 && [string index [lindex $args 0] 0]!="-"} {
dan430e74c2010-06-07 17:47:26 +0000102 # This command is opening a new database connection.
103 #
104 if {[info exists ::G(perm:sqlite3_args)]} {
105 set args [concat $args $::G(perm:sqlite3_args)]
106 }
dan68928b62010-06-22 13:46:43 +0000107 if {[sqlite_orig -has-codec] && ![info exists ::do_not_use_codec]} {
dan430e74c2010-06-07 17:47:26 +0000108 lappend args -key {xyzzy}
109 }
110
dan3ccd20a2010-06-09 19:01:02 +0000111 set res [uplevel 1 sqlite_orig $args]
dan430e74c2010-06-07 17:47:26 +0000112 if {[info exists ::G(perm:presql)]} {
113 [lindex $args 0] eval $::G(perm:presql)
114 }
dan1ce1b4a2010-12-07 14:32:28 +0000115 if {[info exists ::G(perm:dbconfig)]} {
116 set ::dbhandle [lindex $args 0]
117 uplevel #0 $::G(perm:dbconfig)
118 }
dan3ccd20a2010-06-09 19:01:02 +0000119 set res
dan430e74c2010-06-07 17:47:26 +0000120 } else {
121 # This command is not opening a new database connection. Pass the
122 # arguments through to the C implemenation as the are.
123 #
124 uplevel 1 sqlite_orig $args
drh9eb9e262004-02-11 02:18:05 +0000125 }
drh9eb9e262004-02-11 02:18:05 +0000126 }
127}
128
mistachkinfda06be2011-08-02 00:57:34 +0000129proc getFileRetries {} {
130 if {![info exists ::G(file-retries)]} {
131 #
132 # NOTE: Return the default number of retries for [file] operations. A
133 # value of zero or less here means "disabled".
134 #
135 return [expr {$::tcl_platform(platform) eq "windows" ? 10 : 0}]
136 }
137 return $::G(file-retries)
138}
139
140proc getFileRetryDelay {} {
141 if {![info exists ::G(file-retry-delay)]} {
142 #
143 # NOTE: Return the default number of milliseconds to wait when retrying
144 # failed [file] operations. A value of zero or less means "do not
145 # wait".
146 #
147 return 100; # TODO: Good default?
148 }
149 return $::G(file-retry-delay)
150}
151
mistachkinf8a78462012-03-08 20:00:36 +0000152# Return the string representing the name of the current directory. On
153# Windows, the result is "normalized" to whatever our parent command shell
154# is using to prevent case-mismatch issues.
155#
156proc get_pwd {} {
157 if {$::tcl_platform(platform) eq "windows"} {
mistachkin533b8f62012-03-08 20:28:31 +0000158 #
159 # NOTE: Cannot use [file normalize] here because it would alter the
160 # case of the result to what Tcl considers canonical, which would
161 # defeat the purpose of this procedure.
162 #
163 return [string map [list \\ /] \
164 [string trim [exec -- $::env(ComSpec) /c echo %CD%]]]
mistachkinf8a78462012-03-08 20:00:36 +0000165 } else {
166 return [pwd]
167 }
168}
169
mistachkinfda06be2011-08-02 00:57:34 +0000170# Copy file $from into $to. This is used because some versions of
171# TCL for windows (notably the 8.4.1 binary package shipped with the
172# current mingw release) have a broken "file copy" command.
173#
174proc copy_file {from to} {
175 do_copy_file false $from $to
176}
177
178proc forcecopy {from to} {
179 do_copy_file true $from $to
180}
181
182proc do_copy_file {force from to} {
183 set nRetry [getFileRetries] ;# Maximum number of retries.
184 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
185
186 # On windows, sometimes even a [file copy -force] can fail. The cause is
187 # usually "tag-alongs" - programs like anti-virus software, automatic backup
188 # tools and various explorer extensions that keep a file open a little longer
189 # than we expect, causing the delete to fail.
190 #
191 # The solution is to wait a short amount of time before retrying the copy.
192 #
193 if {$nRetry > 0} {
194 for {set i 0} {$i<$nRetry} {incr i} {
195 set rc [catch {
196 if {$force} {
197 file copy -force $from $to
198 } else {
199 file copy $from $to
200 }
201 } msg]
202 if {$rc==0} break
203 if {$nDelay > 0} { after $nDelay }
204 }
205 if {$rc} { error $msg }
206 } else {
207 if {$force} {
208 file copy -force $from $to
209 } else {
210 file copy $from $to
211 }
212 }
213}
214
215# Delete a file or directory
216#
217proc delete_file {args} {
218 do_delete_file false {*}$args
219}
220
221proc forcedelete {args} {
222 do_delete_file true {*}$args
223}
224
225proc do_delete_file {force args} {
226 set nRetry [getFileRetries] ;# Maximum number of retries.
227 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
228
229 foreach filename $args {
230 # On windows, sometimes even a [file delete -force] can fail just after
231 # a file is closed. The cause is usually "tag-alongs" - programs like
232 # anti-virus software, automatic backup tools and various explorer
233 # extensions that keep a file open a little longer than we expect, causing
234 # the delete to fail.
235 #
236 # The solution is to wait a short amount of time before retrying the
237 # delete.
238 #
239 if {$nRetry > 0} {
240 for {set i 0} {$i<$nRetry} {incr i} {
241 set rc [catch {
242 if {$force} {
243 file delete -force $filename
244 } else {
245 file delete $filename
246 }
247 } msg]
248 if {$rc==0} break
249 if {$nDelay > 0} { after $nDelay }
250 }
251 if {$rc} { error $msg }
252 } else {
253 if {$force} {
254 file delete -force $filename
255 } else {
256 file delete $filename
257 }
258 }
259 }
260}
261
dancb354602010-07-08 09:44:42 +0000262proc execpresql {handle args} {
263 trace remove execution $handle enter [list execpresql $handle]
264 if {[info exists ::G(perm:presql)]} {
265 $handle eval $::G(perm:presql)
266 }
267}
268
dan68928b62010-06-22 13:46:43 +0000269# This command should be called after loading tester.tcl from within
270# all test scripts that are incompatible with encryption codecs.
271#
272proc do_not_use_codec {} {
273 set ::do_not_use_codec 1
274 reset_db
275}
276
dan430e74c2010-06-07 17:47:26 +0000277# The following block only runs the first time this file is sourced. It
278# does not run in slave interpreters (since the ::cmdlinearg array is
279# populated before the test script is run in slave interpreters).
drhbec2bf42000-05-29 23:48:22 +0000280#
danc1a60c52010-06-07 14:28:16 +0000281if {[info exists cmdlinearg]==0} {
282
283 # Parse any options specified in the $argv array. This script accepts the
284 # following options:
285 #
286 # --pause
287 # --soft-heap-limit=NN
288 # --maxerror=NN
289 # --malloctrace=N
290 # --backtrace=N
291 # --binarylog=N
dan0626dfc2010-06-15 06:56:37 +0000292 # --soak=N
mistachkinfda06be2011-08-02 00:57:34 +0000293 # --file-retries=N
294 # --file-retry-delay=N
dan6a64d672011-04-04 15:38:16 +0000295 # --start=[$permutation:]$testfile
mistachkin6c3c1a02011-11-12 03:17:40 +0000296 # --match=$pattern
danc1a60c52010-06-07 14:28:16 +0000297 #
298 set cmdlinearg(soft-heap-limit) 0
299 set cmdlinearg(maxerror) 1000
300 set cmdlinearg(malloctrace) 0
301 set cmdlinearg(backtrace) 10
302 set cmdlinearg(binarylog) 0
dan0626dfc2010-06-15 06:56:37 +0000303 set cmdlinearg(soak) 0
mistachkinfda06be2011-08-02 00:57:34 +0000304 set cmdlinearg(file-retries) 0
305 set cmdlinearg(file-retry-delay) 0
mistachkin6c3c1a02011-11-12 03:17:40 +0000306 set cmdlinearg(start) ""
307 set cmdlinearg(match) ""
danc1a60c52010-06-07 14:28:16 +0000308
309 set leftover [list]
310 foreach a $argv {
311 switch -regexp -- $a {
312 {^-+pause$} {
313 # Wait for user input before continuing. This is to give the user an
314 # opportunity to connect profiling tools to the process.
315 puts -nonewline "Press RETURN to begin..."
316 flush stdout
317 gets stdin
318 }
319 {^-+soft-heap-limit=.+$} {
320 foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break
321 }
322 {^-+maxerror=.+$} {
323 foreach {dummy cmdlinearg(maxerror)} [split $a =] break
324 }
325 {^-+malloctrace=.+$} {
326 foreach {dummy cmdlinearg(malloctrace)} [split $a =] break
327 if {$cmdlinearg(malloctrace)} {
328 sqlite3_memdebug_log start
329 }
330 }
331 {^-+backtrace=.+$} {
332 foreach {dummy cmdlinearg(backtrace)} [split $a =] break
dan0626dfc2010-06-15 06:56:37 +0000333 sqlite3_memdebug_backtrace $value
danc1a60c52010-06-07 14:28:16 +0000334 }
danc1a60c52010-06-07 14:28:16 +0000335 {^-+binarylog=.+$} {
336 foreach {dummy cmdlinearg(binarylog)} [split $a =] break
337 }
dan0626dfc2010-06-15 06:56:37 +0000338 {^-+soak=.+$} {
339 foreach {dummy cmdlinearg(soak)} [split $a =] break
340 set ::G(issoak) $cmdlinearg(soak)
341 }
mistachkinfda06be2011-08-02 00:57:34 +0000342 {^-+file-retries=.+$} {
343 foreach {dummy cmdlinearg(file-retries)} [split $a =] break
344 set ::G(file-retries) $cmdlinearg(file-retries)
345 }
346 {^-+file-retry-delay=.+$} {
347 foreach {dummy cmdlinearg(file-retry-delay)} [split $a =] break
348 set ::G(file-retry-delay) $cmdlinearg(file-retry-delay)
349 }
dan6a64d672011-04-04 15:38:16 +0000350 {^-+start=.+$} {
351 foreach {dummy cmdlinearg(start)} [split $a =] break
352
353 set ::G(start:file) $cmdlinearg(start)
354 if {[regexp {(.*):(.*)} $cmdlinearg(start) -> s.perm s.file]} {
355 set ::G(start:permutation) ${s.perm}
356 set ::G(start:file) ${s.file}
357 }
358 if {$::G(start:file) == ""} {unset ::G(start:file)}
359 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000360 {^-+match=.+$} {
361 foreach {dummy cmdlinearg(match)} [split $a =] break
362
363 set ::G(match) $cmdlinearg(match)
364 if {$::G(match) == ""} {unset ::G(match)}
365 }
danc1a60c52010-06-07 14:28:16 +0000366 default {
367 lappend leftover $a
368 }
369 }
370 }
371 set argv $leftover
372
dan430e74c2010-06-07 17:47:26 +0000373 # Install the malloc layer used to inject OOM errors. And the 'automatic'
374 # extensions. This only needs to be done once for the process.
375 #
danielk1977f7b9d662008-06-23 18:49:43 +0000376 sqlite3_shutdown
377 install_malloc_faultsim 1
378 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000379 autoinstall_test_functions
danc1a60c52010-06-07 14:28:16 +0000380
dan430e74c2010-06-07 17:47:26 +0000381 # If the --binarylog option was specified, create the logging VFS. This
382 # call installs the new VFS as the default for all SQLite connections.
383 #
danc1a60c52010-06-07 14:28:16 +0000384 if {$cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000385 vfslog new binarylog {} vfslog.bin
danc1a60c52010-06-07 14:28:16 +0000386 }
387
388 # Set the backtrace depth, if malloc tracing is enabled.
389 #
390 if {$cmdlinearg(malloctrace)} {
391 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danielk1977185eac92008-07-12 15:55:54 +0000392 }
danielk1977f7b9d662008-06-23 18:49:43 +0000393}
danielk197703ba3fa2009-01-09 10:49:14 +0000394
danc1a60c52010-06-07 14:28:16 +0000395# Update the soft-heap-limit each time this script is run. In that
396# way if an individual test file changes the soft-heap-limit, it
397# will be reset at the start of the next test file.
398#
399sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
400
401# Create a test database
402#
danielk197703ba3fa2009-01-09 10:49:14 +0000403proc reset_db {} {
404 catch {db close}
mistachkinfda06be2011-08-02 00:57:34 +0000405 forcedelete test.db
406 forcedelete test.db-journal
407 forcedelete test.db-wal
danielk197703ba3fa2009-01-09 10:49:14 +0000408 sqlite3 db ./test.db
409 set ::DB [sqlite3_connection_pointer db]
410 if {[info exists ::SETUP_SQL]} {
411 db eval $::SETUP_SQL
412 }
drhcd61c282002-03-06 22:01:34 +0000413}
danielk197703ba3fa2009-01-09 10:49:14 +0000414reset_db
drhbec2bf42000-05-29 23:48:22 +0000415
416# Abort early if this script has been run before.
417#
danc1a60c52010-06-07 14:28:16 +0000418if {[info exists TC(count)]} return
drhbec2bf42000-05-29 23:48:22 +0000419
drhce2198c2010-09-10 13:22:59 +0000420# Make sure memory statistics are enabled.
421#
422sqlite3_config_memstatus 1
423
danc1a60c52010-06-07 14:28:16 +0000424# Initialize the test counters and set up commands to access them.
425# Or, if this is a slave interpreter, set up aliases to write the
426# counters in the parent interpreter.
drhbec2bf42000-05-29 23:48:22 +0000427#
danc1a60c52010-06-07 14:28:16 +0000428if {0==[info exists ::SLAVE]} {
429 set TC(errors) 0
430 set TC(count) 0
431 set TC(fail_list) [list]
432 set TC(omit_list) [list]
433
434 proc set_test_counter {counter args} {
435 if {[llength $args]} {
436 set ::TC($counter) [lindex $args 0]
437 }
438 set ::TC($counter)
439 }
drhb62c3352006-11-23 09:39:16 +0000440}
drh348784e2000-05-29 20:41:49 +0000441
drh521cc842008-04-15 02:36:33 +0000442# Record the fact that a sequence of tests were omitted.
443#
mistachkin6c3c1a02011-11-12 03:17:40 +0000444proc omit_test {name reason {append 1}} {
danc1a60c52010-06-07 14:28:16 +0000445 set omitList [set_test_counter omit_list]
mistachkin6c3c1a02011-11-12 03:17:40 +0000446 if {$append} {
447 lappend omitList [list $name $reason]
448 }
danc1a60c52010-06-07 14:28:16 +0000449 set_test_counter omit_list $omitList
drh521cc842008-04-15 02:36:33 +0000450}
451
danc1a60c52010-06-07 14:28:16 +0000452# Record the fact that a test failed.
453#
454proc fail_test {name} {
455 set f [set_test_counter fail_list]
456 lappend f $name
457 set_test_counter fail_list $f
458 set_test_counter errors [expr [set_test_counter errors] + 1]
459
460 set nFail [set_test_counter errors]
461 if {$nFail>=$::cmdlinearg(maxerror)} {
462 puts "*** Giving up..."
463 finalize_testing
464 }
465}
466
467# Increment the number of tests run
468#
469proc incr_ntest {} {
470 set_test_counter count [expr [set_test_counter count] + 1]
471}
472
473
drh348784e2000-05-29 20:41:49 +0000474# Invoke the do_test procedure to run a single test
475#
476proc do_test {name cmd expected} {
danc1a60c52010-06-07 14:28:16 +0000477
478 global argv cmdlinearg
479
dan8c408002010-11-01 17:38:24 +0000480 fix_testname name
481
drh4a50aac2007-08-23 02:47:53 +0000482 sqlite3_memdebug_settitle $name
danc1a60c52010-06-07 14:28:16 +0000483
dan92d516a2010-07-05 14:54:48 +0000484# if {[llength $argv]==0} {
485# set go 1
486# } else {
487# set go 0
488# foreach pattern $argv {
489# if {[string match $pattern $name]} {
490# set go 1
491# break
492# }
493# }
494# }
dan430e74c2010-06-07 17:47:26 +0000495
dand506de02010-07-03 13:59:01 +0000496 if {[info exists ::G(perm:prefix)]} {
497 set name "$::G(perm:prefix)$name"
dan430e74c2010-06-07 17:47:26 +0000498 }
499
danc1a60c52010-06-07 14:28:16 +0000500 incr_ntest
drh5edc3122001-09-13 21:53:09 +0000501 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000502 flush stdout
mistachkin6c3c1a02011-11-12 03:17:40 +0000503
504 if {![info exists ::G(match)] || [string match $::G(match) $name]} {
505 if {[catch {uplevel #0 "$cmd;\n"} result]} {
506 puts "\nError: $result"
507 fail_test $name
508 } elseif {[string compare $result $expected]} {
509 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
510 fail_test $name
511 } else {
512 puts " Ok"
513 }
drh348784e2000-05-29 20:41:49 +0000514 } else {
mistachkin6c3c1a02011-11-12 03:17:40 +0000515 puts " Omitted"
516 omit_test $name "pattern mismatch" 0
drh348784e2000-05-29 20:41:49 +0000517 }
drh6558db82007-04-13 03:23:21 +0000518 flush stdout
drh348784e2000-05-29 20:41:49 +0000519}
dana3f51082010-09-30 18:43:14 +0000520
shaneh55505172011-06-21 19:30:19 +0000521proc filepath_normalize {p} {
522 # test cases should be written to assume "unix"-like file paths
shaneh285a18f2011-06-21 19:39:59 +0000523 if {$::tcl_platform(platform)!="unix"} {
shaneh55505172011-06-21 19:30:19 +0000524 # lreverse*2 as a hack to remove any unneeded {} after the string map
525 lreverse [lreverse [string map {\\ /} [regsub -nocase -all {[a-z]:[/\\]+} $p {/}]]]
shanehc4896402011-06-21 19:38:16 +0000526 } {
527 set p
shaneh55505172011-06-21 19:30:19 +0000528 }
529}
530proc do_filepath_test {name cmd expected} {
531 uplevel [list do_test $name [
532 subst -nocommands { filepath_normalize [ $cmd ] }
533 ] [filepath_normalize $expected]]
534}
535
dan33f53792011-05-05 19:44:22 +0000536proc realnum_normalize {r} {
shaneh4f529e82011-06-20 17:41:41 +0000537 # different TCL versions display floating point values differently.
538 string map {1.#INF inf Inf inf .0e e} [regsub -all {(e[+-])0+} $r {\1}]
dan33f53792011-05-05 19:44:22 +0000539}
540proc do_realnum_test {name cmd expected} {
541 uplevel [list do_test $name [
542 subst -nocommands { realnum_normalize [ $cmd ] }
543 ] [realnum_normalize $expected]]
544}
545
dana3f51082010-09-30 18:43:14 +0000546proc fix_testname {varname} {
547 upvar $varname testname
548 if {[info exists ::testprefix]
549 && [string is digit [string range $testname 0 0]]
550 } {
551 set testname "${::testprefix}-$testname"
552 }
553}
dan153eda02010-06-21 07:45:47 +0000554
dan57f7f4b2010-10-01 19:04:37 +0000555proc do_execsql_test {testname sql {result {}}} {
dana3f51082010-09-30 18:43:14 +0000556 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000557 uplevel do_test [list $testname] [list "execsql {$sql}"] [list [list {*}$result]]
dan153eda02010-06-21 07:45:47 +0000558}
559proc do_catchsql_test {testname sql result} {
dana3f51082010-09-30 18:43:14 +0000560 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000561 uplevel do_test [list $testname] [list "catchsql {$sql}"] [list $result]
dan153eda02010-06-21 07:45:47 +0000562}
dan39854792010-11-15 16:12:58 +0000563proc do_eqp_test {name sql res} {
564 uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res]
565}
dan153eda02010-06-21 07:45:47 +0000566
dan51f06982010-09-18 19:00:12 +0000567#-------------------------------------------------------------------------
568# Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST
569#
570# Where switches are:
571#
572# -errorformat FMTSTRING
573# -count
danaf1dcab2010-09-20 19:17:53 +0000574# -query SQL
dana16d1062010-09-28 17:37:28 +0000575# -tclquery TCL
danaf7626f2010-09-23 18:47:36 +0000576# -repair TCL
dan51f06982010-09-18 19:00:12 +0000577#
578proc do_select_tests {prefix args} {
579
580 set testlist [lindex $args end]
581 set switches [lrange $args 0 end-1]
582
583 set errfmt ""
584 set countonly 0
dana16d1062010-09-28 17:37:28 +0000585 set tclquery ""
danaf7626f2010-09-23 18:47:36 +0000586 set repair ""
dan51f06982010-09-18 19:00:12 +0000587
588 for {set i 0} {$i < [llength $switches]} {incr i} {
589 set s [lindex $switches $i]
590 set n [string length $s]
danaf1dcab2010-09-20 19:17:53 +0000591 if {$n>=2 && [string equal -length $n $s "-query"]} {
dana16d1062010-09-28 17:37:28 +0000592 set tclquery [list execsql [lindex $switches [incr i]]]
593 } elseif {$n>=2 && [string equal -length $n $s "-tclquery"]} {
594 set tclquery [lindex $switches [incr i]]
danaf1dcab2010-09-20 19:17:53 +0000595 } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} {
dan51f06982010-09-18 19:00:12 +0000596 set errfmt [lindex $switches [incr i]]
danaf7626f2010-09-23 18:47:36 +0000597 } elseif {$n>=2 && [string equal -length $n $s "-repair"]} {
598 set repair [lindex $switches [incr i]]
dan51f06982010-09-18 19:00:12 +0000599 } elseif {$n>=2 && [string equal -length $n $s "-count"]} {
600 set countonly 1
601 } else {
602 error "unknown switch: $s"
603 }
604 }
605
606 if {$countonly && $errfmt!=""} {
607 error "Cannot use -count and -errorformat together"
608 }
609 set nTestlist [llength $testlist]
610 if {$nTestlist%3 || $nTestlist==0 } {
611 error "SELECT test list contains [llength $testlist] elements"
612 }
613
danaf7626f2010-09-23 18:47:36 +0000614 eval $repair
dan51f06982010-09-18 19:00:12 +0000615 foreach {tn sql res} $testlist {
dana16d1062010-09-28 17:37:28 +0000616 if {$tclquery != ""} {
danaf1dcab2010-09-20 19:17:53 +0000617 execsql $sql
dana16d1062010-09-28 17:37:28 +0000618 uplevel do_test ${prefix}.$tn [list $tclquery] [list [list {*}$res]]
619 } elseif {$countonly} {
dan51f06982010-09-18 19:00:12 +0000620 set nRow 0
621 db eval $sql {incr nRow}
622 uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res]
623 } elseif {$errfmt==""} {
624 uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]]
625 } else {
626 set res [list 1 [string trim [format $errfmt {*}$res]]]
627 uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res]
628 }
danaf7626f2010-09-23 18:47:36 +0000629 eval $repair
dan51f06982010-09-18 19:00:12 +0000630 }
danaf7626f2010-09-23 18:47:36 +0000631
dan51f06982010-09-18 19:00:12 +0000632}
633
danb04757a2010-09-21 16:59:16 +0000634proc delete_all_data {} {
635 db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} {
636 db eval "DELETE FROM '[string map {' ''} $t]'"
637 }
638}
drh348784e2000-05-29 20:41:49 +0000639
drhb62c3352006-11-23 09:39:16 +0000640# Run an SQL script.
641# Return the number of microseconds per statement.
642#
drh3590f152006-11-23 21:09:10 +0000643proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000644 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000645 flush stdout
646 set speed [time {sqlite3_exec_nr db $sql}]
647 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000648 if {$tm == 0} {
649 set rate [format %20s "many"]
650 } else {
651 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
652 }
drh3590f152006-11-23 21:09:10 +0000653 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000654 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000655 global total_time
656 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000657 lappend ::speed_trial_times $name $tm
drhdd924312007-03-31 22:34:16 +0000658}
drh45c236d2008-03-22 01:08:00 +0000659proc speed_trial_tcl {name numstmt units script} {
660 puts -nonewline [format {%-21.21s } $name...]
661 flush stdout
662 set speed [time {eval $script}]
663 set tm [lindex $speed 0]
664 if {$tm == 0} {
665 set rate [format %20s "many"]
666 } else {
667 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
668 }
669 set u2 $units/s
670 puts [format {%12d uS %s %s} $tm $rate $u2]
671 global total_time
672 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000673 lappend ::speed_trial_times $name $tm
drh45c236d2008-03-22 01:08:00 +0000674}
drhdd924312007-03-31 22:34:16 +0000675proc speed_trial_init {name} {
676 global total_time
677 set total_time 0
dana975b592010-10-08 16:09:43 +0000678 set ::speed_trial_times [list]
drhbb810a92010-07-03 12:00:53 +0000679 sqlite3 versdb :memory:
680 set vers [versdb one {SELECT sqlite_source_id()}]
681 versdb close
682 puts "SQLite $vers"
drhdd924312007-03-31 22:34:16 +0000683}
684proc speed_trial_summary {name} {
685 global total_time
686 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
dana975b592010-10-08 16:09:43 +0000687
688 if { 0 } {
689 sqlite3 versdb :memory:
690 set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0]
691 versdb close
692 puts "CREATE TABLE IF NOT EXISTS time(version, script, test, us);"
693 foreach {test us} $::speed_trial_times {
694 puts "INSERT INTO time VALUES('$vers', '$name', '$test', $us);"
695 }
696 }
drhb62c3352006-11-23 09:39:16 +0000697}
698
drh348784e2000-05-29 20:41:49 +0000699# Run this routine last
700#
701proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +0000702 catch {db close}
703 catch {db2 close}
704 catch {db3 close}
705 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +0000706}
707proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +0000708 global sqlite_open_file_count
709
710 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +0000711
drh6e142f52000-06-08 13:36:40 +0000712 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000713 catch {db2 close}
714 catch {db3 close}
715
drh9bc54492007-10-23 14:49:59 +0000716 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000717 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000718 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000719 db close
drh984bfaa2008-03-19 16:08:53 +0000720 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +0000721
drh3a7fb7c2007-08-10 16:41:08 +0000722 sqlite3_soft_heap_limit 0
danc1a60c52010-06-07 14:28:16 +0000723 set nTest [incr_ntest]
724 set nErr [set_test_counter errors]
725
drh348784e2000-05-29 20:41:49 +0000726 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000727 if {$nErr>0} {
danc1a60c52010-06-07 14:28:16 +0000728 puts "Failures on these tests: [set_test_counter fail_list]"
drhf3a65f72007-08-22 20:18:21 +0000729 }
danielk1977ee8b7992009-03-26 17:13:06 +0000730 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +0000731 if {[llength $omitList]>0} {
732 puts "Omitted test cases:"
733 set prec {}
734 foreach {rec} [lsort $omitList] {
735 if {$rec==$prec} continue
736 set prec $rec
737 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
738 }
739 }
drh80788d82006-09-02 14:50:23 +0000740 if {$nErr>0 && ![working_64bit_int]} {
741 puts "******************************************************************"
742 puts "N.B.: The version of TCL that you used to build this test harness"
743 puts "is defective in that it does not support 64-bit integers. Some or"
744 puts "all of the test failures above might be a result from this defect"
745 puts "in your TCL build."
746 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000747 }
danc1a60c52010-06-07 14:28:16 +0000748 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000749 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +0000750 }
drh94e92032003-02-16 22:21:32 +0000751 if {$sqlite_open_file_count} {
752 puts "$sqlite_open_file_count files were left open"
753 incr nErr
754 }
drheafc43b2010-07-26 18:43:40 +0000755 if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 ||
756 [sqlite3_memory_used]>0} {
757 puts "Unfreed memory: [sqlite3_memory_used] bytes in\
758 [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations"
drhf3a65f72007-08-22 20:18:21 +0000759 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000760 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000761 puts "Writing unfreed memory log to \"./memleak.txt\""
762 sqlite3_memdebug_dump ./memleak.txt
763 }
drhf3a65f72007-08-22 20:18:21 +0000764 } else {
765 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000766 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000767 sqlite3_memdebug_dump ./memusage.txt
768 }
drhf3a65f72007-08-22 20:18:21 +0000769 }
drhf7141992008-06-19 00:16:08 +0000770 show_memstats
drh91fd4d42008-01-19 20:11:25 +0000771 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
772 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000773 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
774 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
775 }
danc1a60c52010-06-07 14:28:16 +0000776 if {$::cmdlinearg(malloctrace)} {
danielk197735754ac2008-03-21 17:29:37 +0000777 puts "Writing mallocs.sql..."
778 memdebug_log_sql
779 sqlite3_memdebug_log stop
780 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000781
782 if {[sqlite3_memory_used]>0} {
783 puts "Writing leaks.sql..."
784 sqlite3_memdebug_log sync
785 memdebug_log_sql leaks.sql
786 }
danielk197735754ac2008-03-21 17:29:37 +0000787 }
drhd9910fe2006-10-04 11:55:49 +0000788 foreach f [glob -nocomplain test.db-*-journal] {
mistachkinfda06be2011-08-02 00:57:34 +0000789 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +0000790 }
791 foreach f [glob -nocomplain test.db-mj*] {
mistachkinfda06be2011-08-02 00:57:34 +0000792 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +0000793 }
drhdb25e382001-03-15 18:21:22 +0000794 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000795}
796
drhf7141992008-06-19 00:16:08 +0000797# Display memory statistics for analysis and debugging purposes.
798#
799proc show_memstats {} {
800 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +0000801 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
802 set val [format {now %10d max %10d max-size %10d} \
803 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000804 puts "Memory used: $val"
drheafc43b2010-07-26 18:43:40 +0000805 set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0]
806 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
807 puts "Allocation count: $val"
drhf7141992008-06-19 00:16:08 +0000808 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +0000809 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
810 set val [format {now %10d max %10d max-size %10d} \
811 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000812 puts "Page-cache used: $val"
813 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
814 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
815 puts "Page-cache overflow: $val"
816 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
817 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
818 puts "Scratch memory used: $val"
819 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +0000820 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
821 set val [format {now %10d max %10d max-size %10d} \
822 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000823 puts "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +0000824 ifcapable yytrackmaxstackdepth {
825 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
826 set val [format { max %10d} [lindex $x 2]]
827 puts "Parser stack depth: $val"
828 }
drhf7141992008-06-19 00:16:08 +0000829}
830
drh348784e2000-05-29 20:41:49 +0000831# A procedure to execute SQL
832#
drhc4a3c772001-04-04 11:48:57 +0000833proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000834 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000835 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000836}
drh3aadb2e2000-05-31 17:59:25 +0000837
drhadbca9c2001-09-27 15:11:53 +0000838# Execute SQL and catch exceptions.
839#
840proc catchsql {sql {db db}} {
841 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +0000842 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +0000843 lappend r $msg
844 return $r
845}
846
drh04096482001-11-09 22:41:44 +0000847# Do an VDBE code dump on the SQL given
848#
849proc explain {sql {db db}} {
850 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000851 puts "addr opcode p1 p2 p3 p4 p5 #"
852 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000853 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000854 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
855 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
856 ]
drh04096482001-11-09 22:41:44 +0000857 }
858}
859
drhdafc0ce2008-04-17 19:14:02 +0000860# Show the VDBE program for an SQL statement but omit the Trace
861# opcode at the beginning. This procedure can be used to prove
862# that different SQL statements generate exactly the same VDBE code.
863#
864proc explain_no_trace {sql} {
865 set tr [db eval "EXPLAIN $sql"]
866 return [lrange $tr 7 end]
867}
868
drh3aadb2e2000-05-31 17:59:25 +0000869# Another procedure to execute SQL. This one includes the field
870# names in the returned list.
871#
872proc execsql2 {sql} {
873 set result {}
874 db eval $sql data {
875 foreach f $data(*) {
876 lappend result $f $data($f)
877 }
878 }
879 return $result
880}
drh17a68932001-01-31 13:28:08 +0000881
drhdde85d92003-03-01 19:45:34 +0000882# Use the non-callback API to execute multiple SQL statements
883#
884proc stepsql {dbptr sql} {
885 set sql [string trim $sql]
886 set r 0
887 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000888 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000889 return [list 1 $vm]
890 }
891 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000892# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
893# foreach v $VAL {lappend r $v}
894# }
895 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
896 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
897 lappend r [sqlite3_column_text $vm $i]
898 }
drhdde85d92003-03-01 19:45:34 +0000899 }
danielk1977106bb232004-05-21 10:08:53 +0000900 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000901 return [list 1 $errmsg]
902 }
903 }
904 return $r
905}
906
drh21504322002-06-25 13:16:02 +0000907# Do an integrity check of the entire database
908#
danielk197704103022009-02-03 16:51:24 +0000909proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +0000910 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +0000911 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +0000912 }
913}
914
danc6055c72011-04-28 18:46:46 +0000915
916# Return true if the SQL statement passed as the second argument uses a
917# statement transaction.
918#
919proc sql_uses_stmt {db sql} {
920 set stmt [sqlite3_prepare $db $sql -1 dummy]
921 set uses [uses_stmt_journal $stmt]
922 sqlite3_finalize $stmt
923 return $uses
924}
925
danielk1977db4e8862008-01-16 08:24:46 +0000926proc fix_ifcapable_expr {expr} {
927 set ret ""
928 set state 0
929 for {set i 0} {$i < [string length $expr]} {incr i} {
930 set char [string range $expr $i $i]
931 set newstate [expr {[string is alnum $char] || $char eq "_"}]
932 if {$newstate && !$state} {
933 append ret {$::sqlite_options(}
934 }
935 if {!$newstate && $state} {
936 append ret )
937 }
938 append ret $char
939 set state $newstate
940 }
941 if {$state} {append ret )}
942 return $ret
943}
944
drh27d258a2004-10-30 20:23:09 +0000945# Evaluate a boolean expression of capabilities. If true, execute the
946# code. Omit the code if false.
947#
danielk19773e8c37e2005-01-21 03:12:14 +0000948proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +0000949 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
950 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +0000951 if ($e2) {
952 set c [catch {uplevel 1 $code} r]
953 } else {
954 set c [catch {uplevel 1 $elsecode} r]
955 }
956 return -code $c $r
drh21504322002-06-25 13:16:02 +0000957}
danielk197745901d62004-11-10 15:27:38 +0000958
danielk1977aca790a2005-01-13 11:07:52 +0000959# This proc execs a seperate process that crashes midway through executing
960# the SQL script $sql on database test.db.
961#
962# The crash occurs during a sync() of file $crashfile. When the crash
963# occurs a random subset of all unsynced writes made by the process are
964# written into the files on disk. Argument $crashdelay indicates the
965# number of file syncs to wait before crashing.
966#
967# The return value is a list of two elements. The first element is a
968# boolean, indicating whether or not the process actually crashed or
969# reported some other error. The second element in the returned list is the
970# error message. This is "child process exited abnormally" if the crash
971# occured.
972#
danielk1977f8940ae2007-08-23 11:07:10 +0000973# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +0000974#
975proc crashsql {args} {
danielk197759a33f92007-03-17 10:26:59 +0000976
977 set blocksize ""
978 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +0000979 set prngseed 0
drhf8587402008-01-08 16:03:49 +0000980 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +0000981 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +0000982 set dc ""
danielk197759a33f92007-03-17 10:26:59 +0000983 set sql [lindex $args end]
984
985 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
986 set z [lindex $args $ii]
987 set n [string length $z]
988 set z2 [lindex $args [expr $ii+1]]
989
990 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +0000991 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +0000992 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +0000993 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000994 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +0000995 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +0000996 else { error "Unrecognized option: $z" }
997 }
998
999 if {$crashfile eq ""} {
1000 error "Compulsory option -file missing"
1001 }
1002
shanehf2c08822010-07-08 16:30:44 +00001003 # $crashfile gets compared to the native filename in
1004 # cfSync(), which can be different then what TCL uses by
1005 # default, so here we force it to the "nativename" format.
mistachkinf8a78462012-03-08 20:00:36 +00001006 set cfile [string map {\\ \\\\} [file nativename [file join [get_pwd] $crashfile]]]
danielk1977aca790a2005-01-13 11:07:52 +00001007
1008 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +00001009 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +00001010 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +00001011 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +00001012 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +00001013
1014 # This block sets the cache size of the main database to 10
1015 # pages. This is done in case the build is configured to omit
1016 # "PRAGMA cache_size".
1017 puts $f {db eval {SELECT * FROM sqlite_master;}}
1018 puts $f {set bt [btree_from_db db]}
1019 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +00001020 if {$prngseed} {
1021 set seed [expr {$prngseed%10007+1}]
1022 # puts seed=$seed
1023 puts $f "db eval {SELECT randomblob($seed)}"
1024 }
danielk1977933bbd62007-03-17 07:22:42 +00001025
drhf8587402008-01-08 16:03:49 +00001026 if {[string length $tclbody]>0} {
1027 puts $f $tclbody
1028 }
1029 if {[string length $sql]>0} {
1030 puts $f "db eval {"
1031 puts $f "$sql"
1032 puts $f "}"
1033 }
danielk1977aca790a2005-01-13 11:07:52 +00001034 close $f
danielk1977aca790a2005-01-13 11:07:52 +00001035 set r [catch {
drh9c06c952005-11-26 00:25:00 +00001036 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +00001037 } msg]
shanehf2c08822010-07-08 16:30:44 +00001038
1039 # Windows/ActiveState TCL returns a slightly different
1040 # error message. We map that to the expected message
1041 # so that we don't have to change all of the test
1042 # cases.
1043 if {$::tcl_platform(platform)=="windows"} {
1044 if {$msg=="child killed: unknown signal"} {
1045 set msg "child process exited abnormally"
1046 }
1047 }
1048
danielk1977aca790a2005-01-13 11:07:52 +00001049 lappend r $msg
1050}
1051
danielk197732554c12005-01-22 03:39:39 +00001052# Usage: do_ioerr_test <test number> <options...>
1053#
1054# This proc is used to implement test cases that check that IO errors
1055# are correctly handled. The first argument, <test number>, is an integer
1056# used to name the tests executed by this proc. Options are as follows:
1057#
1058# -tclprep TCL script to run to prepare test.
1059# -sqlprep SQL script to run to prepare test.
1060# -tclbody TCL script to run with IO error simulation.
1061# -sqlbody TCL script to run with IO error simulation.
1062# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +00001063# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +00001064# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +00001065# -start Value of 'N' to begin with (default 1)
1066#
1067# -cksum Boolean. If true, test that the database does
1068# not change during the execution of the test case.
1069#
1070proc do_ioerr_test {testname args} {
1071
danielk197732554c12005-01-22 03:39:39 +00001072 set ::ioerropts(-start) 1
1073 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +00001074 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +00001075 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +00001076 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +00001077 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +00001078 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +00001079 array set ::ioerropts $args
1080
danielk197747cd39c2008-05-12 12:41:15 +00001081 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
1082 # a couple of obscure IO errors that do not return them.
1083 set ::ioerropts(-erc) 0
1084
danielk197732554c12005-01-22 03:39:39 +00001085 set ::go 1
drh93aed5a2008-01-16 17:46:38 +00001086 #reset_prng_state
1087 save_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +00001088 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +00001089 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +00001090 incr ::ioerropts(-count) -1
1091 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +00001092
1093 # Skip this IO error if it was specified with the "-exclude" option.
1094 if {[info exists ::ioerropts(-exclude)]} {
1095 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
1096 }
danielk1977861f7452008-06-05 11:39:11 +00001097 if {$::ioerropts(-restoreprng)} {
1098 restore_prng_state
1099 }
danielk197732554c12005-01-22 03:39:39 +00001100
1101 # Delete the files test.db and test2.db, then execute the TCL and
1102 # SQL (in that order) to prepare for the test case.
1103 do_test $testname.$n.1 {
1104 set ::sqlite_io_error_pending 0
1105 catch {db close}
aswiftaebf4132008-11-21 00:10:35 +00001106 catch {db2 close}
mistachkinfda06be2011-08-02 00:57:34 +00001107 catch {forcedelete test.db}
1108 catch {forcedelete test.db-journal}
1109 catch {forcedelete test2.db}
1110 catch {forcedelete test2.db-journal}
drha34c62d2006-01-06 22:11:20 +00001111 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +00001112 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +00001113 if {[info exists ::ioerropts(-tclprep)]} {
1114 eval $::ioerropts(-tclprep)
1115 }
1116 if {[info exists ::ioerropts(-sqlprep)]} {
1117 execsql $::ioerropts(-sqlprep)
1118 }
1119 expr 0
1120 } {0}
1121
1122 # Read the 'checksum' of the database.
1123 if {$::ioerropts(-cksum)} {
1124 set checksum [cksum]
1125 }
drh93aed5a2008-01-16 17:46:38 +00001126
danielk197732554c12005-01-22 03:39:39 +00001127 # Set the Nth IO error to fail.
1128 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +00001129 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +00001130 set ::sqlite_io_error_pending $n
1131 }] $n
1132
1133 # Create a single TCL script from the TCL and SQL specified
1134 # as the body of the test.
1135 set ::ioerrorbody {}
1136 if {[info exists ::ioerropts(-tclbody)]} {
1137 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
1138 }
1139 if {[info exists ::ioerropts(-sqlbody)]} {
1140 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
1141 }
1142
1143 # Execute the TCL Script created in the above block. If
1144 # there are at least N IO operations performed by SQLite as
1145 # a result of the script, the Nth will fail.
1146 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +00001147 set ::sqlite_io_error_hit 0
1148 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +00001149 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +00001150 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +00001151 set rc [sqlite3_errcode $::DB]
1152 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +00001153 # If we are in extended result code mode, make sure all of the
1154 # IOERRs we get back really do have their extended code values.
1155 # If an extended result code is returned, the sqlite3_errcode
1156 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
1157 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +00001158 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
1159 return $rc
1160 }
1161 } else {
drh5f7b5bf2007-04-19 12:30:54 +00001162 # If we are not in extended result code mode, make sure no
1163 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +00001164 if {[regexp {\+\d} $rc]} {
1165 return $rc
1166 }
1167 }
drh93aed5a2008-01-16 17:46:38 +00001168 # The test repeats as long as $::go is non-zero. $::go starts out
1169 # as 1. When a test runs to completion without hitting an I/O
1170 # error, that means there is no point in continuing with this test
1171 # case so set $::go to zero.
1172 #
1173 if {$::sqlite_io_error_pending>0} {
1174 set ::go 0
1175 set q 0
1176 set ::sqlite_io_error_pending 0
1177 } else {
1178 set q 1
1179 }
1180
drhc9ac5ca2005-11-04 22:03:30 +00001181 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +00001182 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
1183 set r 1
1184 }
danielk1977f2fa8312006-01-24 13:09:33 +00001185 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +00001186
1187 # One of two things must have happened. either
1188 # 1. We never hit the IO error and the SQL returned OK
1189 # 2. An IO error was hit and the SQL failed
1190 #
dand41a29a2010-05-06 15:56:28 +00001191 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +00001192 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +00001193 } {1}
1194
danielk197780daec62008-05-12 10:57:02 +00001195 set ::sqlite_io_error_hit 0
1196 set ::sqlite_io_error_pending 0
1197
danielk197752b472a2008-05-05 16:23:55 +00001198 # Check that no page references were leaked. There should be
1199 # a single reference if there is still an active transaction,
1200 # or zero otherwise.
1201 #
danielk197781620542008-06-07 05:19:37 +00001202 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
1203 # locks are established on database files (i.e. if the error
1204 # occurs while attempting to detect a hot-journal file), then
1205 # there may 0 page references and an active transaction according
1206 # to [sqlite3_get_autocommit].
1207 #
danielk19774abd5442008-05-05 15:26:50 +00001208 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +00001209 do_test $testname.$n.4 {
1210 set bt [btree_from_db db]
1211 db_enter db
1212 array set stats [btree_pager_stats $bt]
1213 db_leave db
danielk197781620542008-06-07 05:19:37 +00001214 set nRef $stats(ref)
1215 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
1216 } {1}
danielk19774abd5442008-05-05 15:26:50 +00001217 }
1218
danielk197752b472a2008-05-05 16:23:55 +00001219 # If there is an open database handle and no open transaction,
1220 # and the pager is not running in exclusive-locking mode,
1221 # check that the pager is in "unlocked" state. Theoretically,
1222 # if a call to xUnlock() failed due to an IO error the underlying
1223 # file may still be locked.
1224 #
1225 ifcapable pragma {
1226 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +00001227 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +00001228 && [db one {pragma locking_mode}] eq "normal"
1229 && [sqlite3_get_autocommit db]
1230 } {
1231 do_test $testname.$n.5 {
1232 set bt [btree_from_db db]
1233 db_enter db
1234 array set stats [btree_pager_stats $bt]
1235 db_leave db
1236 set stats(state)
1237 } 0
1238 }
1239 }
1240
danielk197732554c12005-01-22 03:39:39 +00001241 # If an IO error occured, then the checksum of the database should
1242 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +00001243 #
drh1aa5af12008-03-07 19:51:14 +00001244 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +00001245 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +00001246 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +00001247 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +00001248 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +00001249 cksum
1250 } $checksum
1251 }
1252
drh1aa5af12008-03-07 19:51:14 +00001253 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +00001254 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +00001255 if {[info exists ::ioerropts(-cleanup)]} {
1256 catch $::ioerropts(-cleanup)
1257 }
danielk197732554c12005-01-22 03:39:39 +00001258 }
1259 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +00001260 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +00001261 unset ::ioerropts
1262}
1263
drh93aed5a2008-01-16 17:46:38 +00001264# Return a checksum based on the contents of the main database associated
1265# with connection $db
danielk197732554c12005-01-22 03:39:39 +00001266#
1267proc cksum {{db db}} {
1268 set txt [$db eval {
1269 SELECT name, type, sql FROM sqlite_master order by name
1270 }]\n
1271 foreach tbl [$db eval {
1272 SELECT name FROM sqlite_master WHERE type='table' order by name
1273 }] {
1274 append txt [$db eval "SELECT * FROM $tbl"]\n
1275 }
1276 foreach prag {default_synchronous default_cache_size} {
1277 append txt $prag-[$db eval "PRAGMA $prag"]\n
1278 }
1279 set cksum [string length $txt]-[md5 $txt]
1280 # puts $cksum-[file size test.db]
1281 return $cksum
1282}
1283
drh93aed5a2008-01-16 17:46:38 +00001284# Generate a checksum based on the contents of the main and temp tables
1285# database $db. If the checksum of two databases is the same, and the
1286# integrity-check passes for both, the two databases are identical.
1287#
drh1db639c2008-01-17 02:36:28 +00001288proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +00001289 set ret [list]
1290 ifcapable tempdb {
1291 set sql {
1292 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1293 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
1294 SELECT 'sqlite_master' UNION
1295 SELECT 'sqlite_temp_master' ORDER BY 1
1296 }
1297 } else {
1298 set sql {
1299 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1300 SELECT 'sqlite_master' ORDER BY 1
1301 }
1302 }
1303 set tbllist [$db eval $sql]
1304 set txt {}
1305 foreach tbl $tbllist {
1306 append txt [$db eval "SELECT * FROM $tbl"]
1307 }
1308 foreach prag {default_cache_size} {
1309 append txt $prag-[$db eval "PRAGMA $prag"]\n
1310 }
1311 # puts txt=$txt
1312 return [md5 $txt]
1313}
1314
drhdc2c4912009-02-04 22:46:47 +00001315# Generate a checksum based on the contents of a single database with
1316# a database connection. The name of the database is $dbname.
1317# Examples of $dbname are "temp" or "main".
1318#
1319proc dbcksum {db dbname} {
1320 if {$dbname=="temp"} {
1321 set master sqlite_temp_master
1322 } else {
1323 set master $dbname.sqlite_master
1324 }
1325 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
1326 set txt [$db eval "SELECT * FROM $master"]\n
1327 foreach tab $alltab {
1328 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
1329 }
1330 return [md5 $txt]
1331}
1332
danielk197735754ac2008-03-21 17:29:37 +00001333proc memdebug_log_sql {{filename mallocs.sql}} {
1334
danielk19776f332c12008-03-21 14:22:44 +00001335 set data [sqlite3_memdebug_log dump]
1336 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +00001337 if {$nFrame < 0} { return "" }
1338
danielk197735754ac2008-03-21 17:29:37 +00001339 set database temp
1340
danielk19776ab3a2e2009-02-19 14:39:25 +00001341 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +00001342
1343 set sql ""
1344 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +00001345 set nCall [lindex $e 0]
1346 set nByte [lindex $e 1]
1347 set lStack [lrange $e 2 end]
1348 append sql "INSERT INTO ${database}.malloc VALUES"
1349 append sql "('test', $nCall, $nByte, '$lStack');\n"
1350 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +00001351 set frames($f) 1
1352 }
1353 }
1354
1355 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +00001356 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +00001357
1358 foreach f [array names frames] {
1359 set addr [format %x $f]
1360 set cmd "addr2line -e [info nameofexec] $addr"
1361 set line [eval exec $cmd]
1362 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00001363
1364 set file [lindex [split $line :] 0]
1365 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00001366 }
1367
danielk197735754ac2008-03-21 17:29:37 +00001368 foreach f [array names files] {
1369 set contents ""
1370 catch {
1371 set fd [open $f]
1372 set contents [read $fd]
1373 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001374 }
danielk197735754ac2008-03-21 17:29:37 +00001375 set contents [string map {' ''} $contents]
1376 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00001377 }
danielk19776f332c12008-03-21 14:22:44 +00001378
danielk197735754ac2008-03-21 17:29:37 +00001379 set fd [open $filename w]
1380 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
1381 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001382}
drh93aed5a2008-01-16 17:46:38 +00001383
danf5894502009-10-07 18:41:19 +00001384# Drop all tables in database [db]
1385proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00001386 ifcapable trigger&&foreignkey {
1387 set pk [$db one "PRAGMA foreign_keys"]
1388 $db eval "PRAGMA foreign_keys = OFF"
1389 }
drh9a6ffc82010-02-15 18:03:20 +00001390 foreach {idx name file} [db eval {PRAGMA database_list}] {
1391 if {$idx==1} {
1392 set master sqlite_temp_master
1393 } else {
1394 set master $name.sqlite_master
1395 }
1396 foreach {t type} [$db eval "
1397 SELECT name, type FROM $master
dana16d1062010-09-28 17:37:28 +00001398 WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X'
drh9a6ffc82010-02-15 18:03:20 +00001399 "] {
dana16d1062010-09-28 17:37:28 +00001400 $db eval "DROP $type \"$t\""
drh9a6ffc82010-02-15 18:03:20 +00001401 }
danf5894502009-10-07 18:41:19 +00001402 }
shaneh4e7b32f2009-12-17 22:12:51 +00001403 ifcapable trigger&&foreignkey {
1404 $db eval "PRAGMA foreign_keys = $pk"
1405 }
danf5894502009-10-07 18:41:19 +00001406}
1407
dan71cb5182010-04-26 12:39:03 +00001408#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00001409# If a test script is executed with global variable $::G(perm:name) set to
1410# "wal", then the tests are run in WAL mode. Otherwise, they should be run
1411# in rollback mode. The following Tcl procs are used to make this less
1412# intrusive:
dan71cb5182010-04-26 12:39:03 +00001413#
1414# wal_set_journal_mode ?DB?
1415#
1416# If running a WAL test, execute "PRAGMA journal_mode = wal" using
1417# connection handle DB. Otherwise, this command is a no-op.
1418#
1419# wal_check_journal_mode TESTNAME ?DB?
1420#
1421# If running a WAL test, execute a tests case that fails if the main
1422# database for connection handle DB is not currently a WAL database.
1423# Otherwise (if not running a WAL permutation) this is a no-op.
1424#
1425# wal_is_wal_mode
1426#
1427# Returns true if this test should be run in WAL mode. False otherwise.
1428#
1429proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00001430 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00001431}
1432proc wal_set_journal_mode {{db db}} {
1433 if { [wal_is_wal_mode] } {
1434 $db eval "PRAGMA journal_mode = WAL"
1435 }
1436}
1437proc wal_check_journal_mode {testname {db db}} {
1438 if { [wal_is_wal_mode] } {
1439 $db eval { SELECT * FROM sqlite_master }
1440 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
1441 }
1442}
1443
dan430e74c2010-06-07 17:47:26 +00001444proc permutation {} {
1445 set perm ""
1446 catch {set perm $::G(perm:name)}
1447 set perm
1448}
dancb79e512010-08-06 13:50:07 +00001449proc presql {} {
1450 set presql ""
1451 catch {set presql $::G(perm:presql)}
1452 set presql
1453}
dan430e74c2010-06-07 17:47:26 +00001454
danc1a60c52010-06-07 14:28:16 +00001455#-------------------------------------------------------------------------
1456#
1457proc slave_test_script {script} {
1458
1459 # Create the interpreter used to run the test script.
1460 interp create tinterp
1461
1462 # Populate some global variables that tester.tcl expects to see.
1463 foreach {var value} [list \
1464 ::argv0 $::argv0 \
1465 ::argv {} \
1466 ::SLAVE 1 \
1467 ] {
1468 interp eval tinterp [list set $var $value]
1469 }
1470
1471 # The alias used to access the global test counters.
1472 tinterp alias set_test_counter set_test_counter
1473
1474 # Set up the ::cmdlinearg array in the slave.
1475 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
1476
dan430e74c2010-06-07 17:47:26 +00001477 # Set up the ::G array in the slave.
1478 interp eval tinterp [list array set ::G [array get ::G]]
1479
danc1a60c52010-06-07 14:28:16 +00001480 # Load the various test interfaces implemented in C.
1481 load_testfixture_extensions tinterp
1482
1483 # Run the test script.
1484 interp eval tinterp $script
1485
danea5542d2010-07-06 11:26:15 +00001486 # Check if the interpreter call [run_thread_tests]
1487 if { [interp eval tinterp {info exists ::run_thread_tests_called}] } {
1488 set ::run_thread_tests_called 1
1489 }
1490
danc1a60c52010-06-07 14:28:16 +00001491 # Delete the interpreter used to run the test script.
1492 interp delete tinterp
1493}
1494
dan430e74c2010-06-07 17:47:26 +00001495proc slave_test_file {zFile} {
dan0626dfc2010-06-15 06:56:37 +00001496 set tail [file tail $zFile]
dan430e74c2010-06-07 17:47:26 +00001497
dan6a64d672011-04-04 15:38:16 +00001498 if {[info exists ::G(start:permutation)]} {
1499 if {[permutation] != $::G(start:permutation)} return
1500 unset ::G(start:permutation)
1501 }
1502 if {[info exists ::G(start:file)]} {
1503 if {$tail != $::G(start:file) && $tail!="$::G(start:file).test"} return
1504 unset ::G(start:file)
1505 }
1506
dan92d516a2010-07-05 14:54:48 +00001507 # Remember the value of the shared-cache setting. So that it is possible
1508 # to check afterwards that it was not modified by the test script.
1509 #
1510 ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] }
dan0626dfc2010-06-15 06:56:37 +00001511
dan92d516a2010-07-05 14:54:48 +00001512 # Run the test script in a slave interpreter.
1513 #
danea5542d2010-07-06 11:26:15 +00001514 unset -nocomplain ::run_thread_tests_called
dan0626dfc2010-06-15 06:56:37 +00001515 reset_prng_state
1516 set ::sqlite_open_file_count 0
1517 set time [time { slave_test_script [list source $zFile] }]
1518 set ms [expr [lindex $time 0] / 1000]
1519
dan92d516a2010-07-05 14:54:48 +00001520 # Test that all files opened by the test script were closed. Omit this
1521 # if the test script has "thread" in its name. The open file counter
1522 # is not thread-safe.
dan0626dfc2010-06-15 06:56:37 +00001523 #
danea5542d2010-07-06 11:26:15 +00001524 if {[info exists ::run_thread_tests_called]==0} {
dan92d516a2010-07-05 14:54:48 +00001525 do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0}
1526 }
dan430e74c2010-06-07 17:47:26 +00001527 set ::sqlite_open_file_count 0
1528
dan0626dfc2010-06-15 06:56:37 +00001529 # Test that the global "shared-cache" setting was not altered by
1530 # the test script.
1531 #
1532 ifcapable shared_cache {
1533 set res [expr {[sqlite3_enable_shared_cache] == $scs}]
1534 do_test ${tail}-sharedcachesetting [list set {} $res] 1
1535 }
dan430e74c2010-06-07 17:47:26 +00001536
dan92d516a2010-07-05 14:54:48 +00001537 # Add some info to the output.
1538 #
dan0626dfc2010-06-15 06:56:37 +00001539 puts "Time: $tail $ms ms"
dan430e74c2010-06-07 17:47:26 +00001540 show_memstats
danc1a60c52010-06-07 14:28:16 +00001541}
1542
dan59257dc2010-08-04 11:34:31 +00001543# Open a new connection on database test.db and execute the SQL script
1544# supplied as an argument. Before returning, close the new conection and
1545# restore the 4 byte fields starting at header offsets 28, 92 and 96
1546# to the values they held before the SQL was executed. This simulates
1547# a write by a pre-3.7.0 client.
1548#
1549proc sql36231 {sql} {
1550 set B [hexio_read test.db 92 8]
1551 set A [hexio_read test.db 28 4]
1552 sqlite3 db36231 test.db
1553 catch { db36231 func a_string a_string }
1554 execsql $sql db36231
1555 db36231 close
1556 hexio_write test.db 28 $A
1557 hexio_write test.db 92 $B
1558 return ""
1559}
danf5894502009-10-07 18:41:19 +00001560
danccc7ff42010-11-29 12:06:45 +00001561proc db_save {} {
1562 foreach f [glob -nocomplain sv_test.db*] { forcedelete $f }
1563 foreach f [glob -nocomplain test.db*] {
1564 set f2 "sv_$f"
mistachkinfda06be2011-08-02 00:57:34 +00001565 forcecopy $f $f2
danccc7ff42010-11-29 12:06:45 +00001566 }
1567}
1568proc db_save_and_close {} {
1569 db_save
1570 catch { db close }
1571 return ""
1572}
1573proc db_restore {} {
1574 foreach f [glob -nocomplain test.db*] { forcedelete $f }
1575 foreach f2 [glob -nocomplain sv_test.db*] {
1576 set f [string range $f2 3 end]
mistachkinfda06be2011-08-02 00:57:34 +00001577 forcecopy $f2 $f
danccc7ff42010-11-29 12:06:45 +00001578 }
1579}
1580proc db_restore_and_reopen {{dbfile test.db}} {
1581 catch { db close }
1582 db_restore
1583 sqlite3 db $dbfile
1584}
1585proc db_delete_and_reopen {{file test.db}} {
1586 catch { db close }
mistachkinfda06be2011-08-02 00:57:34 +00001587 foreach f [glob -nocomplain test.db*] { forcedelete $f }
danccc7ff42010-11-29 12:06:45 +00001588 sqlite3 db $file
1589}
1590
danielk197745901d62004-11-10 15:27:38 +00001591# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
1592# to non-zero, then set the global variable $AUTOVACUUM to 1.
1593set AUTOVACUUM $sqlite_options(default_autovacuum)
danielk1977ee8b7992009-03-26 17:13:06 +00001594
1595source $testdir/thread_common.tcl
danddf80eb2010-10-25 12:47:43 +00001596source $testdir/malloc_common.tcl