blob: 68b2c8df4cc21a9ba8ccd94958ddba93c286e73b [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#
mistachkinc5484652012-03-05 22:52:33 +000022# is_relative_file
mistachkin4a41f342012-03-06 03:00:49 +000023# test_pwd
mistachkinf8a78462012-03-08 20:00:36 +000024# get_pwd
danc1a60c52010-06-07 14:28:16 +000025# copy_file FROM TO
mistachkinfda06be2011-08-02 00:57:34 +000026# delete_file FILENAME
dan6f343962011-07-01 18:26:40 +000027# drop_all_tables ?DB?
mistachkinfda06be2011-08-02 00:57:34 +000028# forcecopy FROM TO
danc1a60c52010-06-07 14:28:16 +000029# forcedelete FILENAME
30#
31# Test the capability of the SQLite version built into the interpreter to
32# determine if a specific test can be run:
33#
34# ifcapable EXPR
35#
36# Calulate checksums based on database contents:
37#
38# dbcksum DB DBNAME
39# allcksum ?DB?
40# cksum ?DB?
41#
42# Commands to execute/explain SQL statements:
43#
44# stepsql DB SQL
45# execsql2 SQL
46# explain_no_trace SQL
47# explain SQL ?DB?
48# catchsql SQL ?DB?
49# execsql SQL ?DB?
50#
51# Commands to run test cases:
52#
53# do_ioerr_test TESTNAME ARGS...
54# crashsql ARGS...
55# integrity_check TESTNAME ?DB?
56# do_test TESTNAME SCRIPT EXPECTED
dan153eda02010-06-21 07:45:47 +000057# do_execsql_test TESTNAME SQL EXPECTED
58# do_catchsql_test TESTNAME SQL EXPECTED
danc1a60c52010-06-07 14:28:16 +000059#
dan430e74c2010-06-07 17:47:26 +000060# Commands providing a lower level interface to the global test counters:
danc1a60c52010-06-07 14:28:16 +000061#
62# set_test_counter COUNTER ?VALUE?
mistachkin6c3c1a02011-11-12 03:17:40 +000063# omit_test TESTNAME REASON ?APPEND?
danc1a60c52010-06-07 14:28:16 +000064# fail_test TESTNAME
65# incr_ntest
66#
67# Command run at the end of each test file:
68#
69# finish_test
70#
dan430e74c2010-06-07 17:47:26 +000071# Commands to help create test files that run with the "WAL" and other
72# permutations (see file permutations.test):
danc1a60c52010-06-07 14:28:16 +000073#
74# wal_is_wal_mode
75# wal_set_journal_mode ?DB?
76# wal_check_journal_mode TESTNAME?DB?
dan430e74c2010-06-07 17:47:26 +000077# permutation
dancb79e512010-08-06 13:50:07 +000078# presql
danc1a60c52010-06-07 14:28:16 +000079#
drh348784e2000-05-29 20:41:49 +000080
danc1a60c52010-06-07 14:28:16 +000081# Set the precision of FP arithmatic used by the interpreter. And
82# configure SQLite to take database file locks on the page that begins
83# 64KB into the database file instead of the one 1GB in. This means
84# the code that handles that special case can be tested without creating
85# very large database files.
86#
drh92febd92004-08-20 18:34:20 +000087set tcl_precision 15
drhc7a3bb92009-02-05 16:31:45 +000088sqlite3_test_control_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +000089
danc1a60c52010-06-07 14:28:16 +000090
91# If the pager codec is available, create a wrapper for the [sqlite3]
92# command that appends "-key {xyzzy}" to the command line. i.e. this:
drh3a7fb7c2007-08-10 16:41:08 +000093#
danc1a60c52010-06-07 14:28:16 +000094# sqlite3 db test.db
drh4a50aac2007-08-23 02:47:53 +000095#
danc1a60c52010-06-07 14:28:16 +000096# becomes
drh4a50aac2007-08-23 02:47:53 +000097#
danc1a60c52010-06-07 14:28:16 +000098# sqlite3 db test.db -key {xyzzy}
drh9eb9e262004-02-11 02:18:05 +000099#
dan430e74c2010-06-07 17:47:26 +0000100if {[info command sqlite_orig]==""} {
drhef4ac8f2004-06-19 00:16:31 +0000101 rename sqlite3 sqlite_orig
102 proc sqlite3 {args} {
dan68928b62010-06-22 13:46:43 +0000103 if {[llength $args]>=2 && [string index [lindex $args 0] 0]!="-"} {
dan430e74c2010-06-07 17:47:26 +0000104 # This command is opening a new database connection.
105 #
106 if {[info exists ::G(perm:sqlite3_args)]} {
107 set args [concat $args $::G(perm:sqlite3_args)]
108 }
dan68928b62010-06-22 13:46:43 +0000109 if {[sqlite_orig -has-codec] && ![info exists ::do_not_use_codec]} {
dan430e74c2010-06-07 17:47:26 +0000110 lappend args -key {xyzzy}
111 }
112
dan3ccd20a2010-06-09 19:01:02 +0000113 set res [uplevel 1 sqlite_orig $args]
dan430e74c2010-06-07 17:47:26 +0000114 if {[info exists ::G(perm:presql)]} {
115 [lindex $args 0] eval $::G(perm:presql)
116 }
dan1ce1b4a2010-12-07 14:32:28 +0000117 if {[info exists ::G(perm:dbconfig)]} {
118 set ::dbhandle [lindex $args 0]
119 uplevel #0 $::G(perm:dbconfig)
120 }
dan3ccd20a2010-06-09 19:01:02 +0000121 set res
dan430e74c2010-06-07 17:47:26 +0000122 } else {
123 # This command is not opening a new database connection. Pass the
124 # arguments through to the C implemenation as the are.
125 #
126 uplevel 1 sqlite_orig $args
drh9eb9e262004-02-11 02:18:05 +0000127 }
drh9eb9e262004-02-11 02:18:05 +0000128 }
129}
130
mistachkinfda06be2011-08-02 00:57:34 +0000131proc getFileRetries {} {
132 if {![info exists ::G(file-retries)]} {
133 #
134 # NOTE: Return the default number of retries for [file] operations. A
135 # value of zero or less here means "disabled".
136 #
137 return [expr {$::tcl_platform(platform) eq "windows" ? 10 : 0}]
138 }
139 return $::G(file-retries)
140}
141
142proc getFileRetryDelay {} {
143 if {![info exists ::G(file-retry-delay)]} {
144 #
145 # NOTE: Return the default number of milliseconds to wait when retrying
146 # failed [file] operations. A value of zero or less means "do not
147 # wait".
148 #
149 return 100; # TODO: Good default?
150 }
151 return $::G(file-retry-delay)
152}
153
mistachkinf8a78462012-03-08 20:00:36 +0000154# Return the string representing the name of the current directory. On
155# Windows, the result is "normalized" to whatever our parent command shell
156# is using to prevent case-mismatch issues.
157#
158proc get_pwd {} {
159 if {$::tcl_platform(platform) eq "windows"} {
mistachkin533b8f62012-03-08 20:28:31 +0000160 #
161 # NOTE: Cannot use [file normalize] here because it would alter the
162 # case of the result to what Tcl considers canonical, which would
163 # defeat the purpose of this procedure.
164 #
165 return [string map [list \\ /] \
166 [string trim [exec -- $::env(ComSpec) /c echo %CD%]]]
mistachkinf8a78462012-03-08 20:00:36 +0000167 } else {
168 return [pwd]
169 }
170}
171
mistachkinfda06be2011-08-02 00:57:34 +0000172# Copy file $from into $to. This is used because some versions of
173# TCL for windows (notably the 8.4.1 binary package shipped with the
174# current mingw release) have a broken "file copy" command.
175#
176proc copy_file {from to} {
177 do_copy_file false $from $to
178}
179
180proc forcecopy {from to} {
181 do_copy_file true $from $to
182}
183
184proc do_copy_file {force from to} {
185 set nRetry [getFileRetries] ;# Maximum number of retries.
186 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
187
188 # On windows, sometimes even a [file copy -force] can fail. The cause is
189 # usually "tag-alongs" - programs like anti-virus software, automatic backup
190 # tools and various explorer extensions that keep a file open a little longer
191 # than we expect, causing the delete to fail.
192 #
193 # The solution is to wait a short amount of time before retrying the copy.
194 #
195 if {$nRetry > 0} {
196 for {set i 0} {$i<$nRetry} {incr i} {
197 set rc [catch {
198 if {$force} {
199 file copy -force $from $to
200 } else {
201 file copy $from $to
202 }
203 } msg]
204 if {$rc==0} break
205 if {$nDelay > 0} { after $nDelay }
206 }
207 if {$rc} { error $msg }
208 } else {
209 if {$force} {
210 file copy -force $from $to
211 } else {
212 file copy $from $to
213 }
214 }
215}
216
mistachkinc5484652012-03-05 22:52:33 +0000217# Check if a file name is relative
218#
219proc is_relative_file { file } {
220 return [expr {[file pathtype $file] != "absolute"}]
221}
222
mistachkin4a41f342012-03-06 03:00:49 +0000223# If the VFS supports using the current directory, returns [pwd];
224# otherwise, it returns only the provided suffix string (which is
225# empty by default).
226#
227proc test_pwd { args } {
228 if {[llength $args] > 0} {
229 set suffix1 [lindex $args 0]
230 if {[llength $args] > 1} {
231 set suffix2 [lindex $args 1]
232 } else {
233 set suffix2 $suffix1
234 }
235 } else {
236 set suffix1 ""; set suffix2 ""
237 }
238 ifcapable curdir {
mistachkin6aa18c92012-03-08 20:22:42 +0000239 return "[get_pwd]$suffix1"
mistachkin4a41f342012-03-06 03:00:49 +0000240 } else {
241 return $suffix2
242 }
243}
244
mistachkinfda06be2011-08-02 00:57:34 +0000245# Delete a file or directory
246#
247proc delete_file {args} {
248 do_delete_file false {*}$args
249}
250
251proc forcedelete {args} {
252 do_delete_file true {*}$args
253}
254
255proc do_delete_file {force args} {
256 set nRetry [getFileRetries] ;# Maximum number of retries.
257 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
258
259 foreach filename $args {
260 # On windows, sometimes even a [file delete -force] can fail just after
261 # a file is closed. The cause is usually "tag-alongs" - programs like
262 # anti-virus software, automatic backup tools and various explorer
263 # extensions that keep a file open a little longer than we expect, causing
264 # the delete to fail.
265 #
266 # The solution is to wait a short amount of time before retrying the
267 # delete.
268 #
269 if {$nRetry > 0} {
270 for {set i 0} {$i<$nRetry} {incr i} {
271 set rc [catch {
272 if {$force} {
273 file delete -force $filename
274 } else {
275 file delete $filename
276 }
277 } msg]
278 if {$rc==0} break
279 if {$nDelay > 0} { after $nDelay }
280 }
281 if {$rc} { error $msg }
282 } else {
283 if {$force} {
284 file delete -force $filename
285 } else {
286 file delete $filename
287 }
288 }
289 }
290}
291
dancb354602010-07-08 09:44:42 +0000292proc execpresql {handle args} {
293 trace remove execution $handle enter [list execpresql $handle]
294 if {[info exists ::G(perm:presql)]} {
295 $handle eval $::G(perm:presql)
296 }
297}
298
dan68928b62010-06-22 13:46:43 +0000299# This command should be called after loading tester.tcl from within
300# all test scripts that are incompatible with encryption codecs.
301#
302proc do_not_use_codec {} {
303 set ::do_not_use_codec 1
304 reset_db
305}
306
dan430e74c2010-06-07 17:47:26 +0000307# The following block only runs the first time this file is sourced. It
308# does not run in slave interpreters (since the ::cmdlinearg array is
309# populated before the test script is run in slave interpreters).
drhbec2bf42000-05-29 23:48:22 +0000310#
danc1a60c52010-06-07 14:28:16 +0000311if {[info exists cmdlinearg]==0} {
312
313 # Parse any options specified in the $argv array. This script accepts the
314 # following options:
315 #
316 # --pause
317 # --soft-heap-limit=NN
318 # --maxerror=NN
319 # --malloctrace=N
320 # --backtrace=N
321 # --binarylog=N
dan0626dfc2010-06-15 06:56:37 +0000322 # --soak=N
mistachkinfda06be2011-08-02 00:57:34 +0000323 # --file-retries=N
324 # --file-retry-delay=N
dan6a64d672011-04-04 15:38:16 +0000325 # --start=[$permutation:]$testfile
mistachkin6c3c1a02011-11-12 03:17:40 +0000326 # --match=$pattern
danc1a60c52010-06-07 14:28:16 +0000327 #
328 set cmdlinearg(soft-heap-limit) 0
329 set cmdlinearg(maxerror) 1000
330 set cmdlinearg(malloctrace) 0
331 set cmdlinearg(backtrace) 10
332 set cmdlinearg(binarylog) 0
dan0626dfc2010-06-15 06:56:37 +0000333 set cmdlinearg(soak) 0
mistachkinfda06be2011-08-02 00:57:34 +0000334 set cmdlinearg(file-retries) 0
335 set cmdlinearg(file-retry-delay) 0
mistachkin6c3c1a02011-11-12 03:17:40 +0000336 set cmdlinearg(start) ""
337 set cmdlinearg(match) ""
danc1a60c52010-06-07 14:28:16 +0000338
339 set leftover [list]
340 foreach a $argv {
341 switch -regexp -- $a {
342 {^-+pause$} {
343 # Wait for user input before continuing. This is to give the user an
344 # opportunity to connect profiling tools to the process.
345 puts -nonewline "Press RETURN to begin..."
346 flush stdout
347 gets stdin
348 }
349 {^-+soft-heap-limit=.+$} {
350 foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break
351 }
352 {^-+maxerror=.+$} {
353 foreach {dummy cmdlinearg(maxerror)} [split $a =] break
354 }
355 {^-+malloctrace=.+$} {
356 foreach {dummy cmdlinearg(malloctrace)} [split $a =] break
357 if {$cmdlinearg(malloctrace)} {
358 sqlite3_memdebug_log start
359 }
360 }
361 {^-+backtrace=.+$} {
362 foreach {dummy cmdlinearg(backtrace)} [split $a =] break
dan0626dfc2010-06-15 06:56:37 +0000363 sqlite3_memdebug_backtrace $value
danc1a60c52010-06-07 14:28:16 +0000364 }
danc1a60c52010-06-07 14:28:16 +0000365 {^-+binarylog=.+$} {
366 foreach {dummy cmdlinearg(binarylog)} [split $a =] break
367 }
dan0626dfc2010-06-15 06:56:37 +0000368 {^-+soak=.+$} {
369 foreach {dummy cmdlinearg(soak)} [split $a =] break
370 set ::G(issoak) $cmdlinearg(soak)
371 }
mistachkinfda06be2011-08-02 00:57:34 +0000372 {^-+file-retries=.+$} {
373 foreach {dummy cmdlinearg(file-retries)} [split $a =] break
374 set ::G(file-retries) $cmdlinearg(file-retries)
375 }
376 {^-+file-retry-delay=.+$} {
377 foreach {dummy cmdlinearg(file-retry-delay)} [split $a =] break
378 set ::G(file-retry-delay) $cmdlinearg(file-retry-delay)
379 }
dan6a64d672011-04-04 15:38:16 +0000380 {^-+start=.+$} {
381 foreach {dummy cmdlinearg(start)} [split $a =] break
382
383 set ::G(start:file) $cmdlinearg(start)
384 if {[regexp {(.*):(.*)} $cmdlinearg(start) -> s.perm s.file]} {
385 set ::G(start:permutation) ${s.perm}
386 set ::G(start:file) ${s.file}
387 }
388 if {$::G(start:file) == ""} {unset ::G(start:file)}
389 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000390 {^-+match=.+$} {
391 foreach {dummy cmdlinearg(match)} [split $a =] break
392
393 set ::G(match) $cmdlinearg(match)
394 if {$::G(match) == ""} {unset ::G(match)}
395 }
danc1a60c52010-06-07 14:28:16 +0000396 default {
397 lappend leftover $a
398 }
399 }
400 }
401 set argv $leftover
402
dan430e74c2010-06-07 17:47:26 +0000403 # Install the malloc layer used to inject OOM errors. And the 'automatic'
404 # extensions. This only needs to be done once for the process.
405 #
danielk1977f7b9d662008-06-23 18:49:43 +0000406 sqlite3_shutdown
407 install_malloc_faultsim 1
408 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000409 autoinstall_test_functions
danc1a60c52010-06-07 14:28:16 +0000410
dan430e74c2010-06-07 17:47:26 +0000411 # If the --binarylog option was specified, create the logging VFS. This
412 # call installs the new VFS as the default for all SQLite connections.
413 #
danc1a60c52010-06-07 14:28:16 +0000414 if {$cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000415 vfslog new binarylog {} vfslog.bin
danc1a60c52010-06-07 14:28:16 +0000416 }
417
418 # Set the backtrace depth, if malloc tracing is enabled.
419 #
420 if {$cmdlinearg(malloctrace)} {
421 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danielk1977185eac92008-07-12 15:55:54 +0000422 }
danielk1977f7b9d662008-06-23 18:49:43 +0000423}
danielk197703ba3fa2009-01-09 10:49:14 +0000424
danc1a60c52010-06-07 14:28:16 +0000425# Update the soft-heap-limit each time this script is run. In that
426# way if an individual test file changes the soft-heap-limit, it
427# will be reset at the start of the next test file.
428#
429sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
430
431# Create a test database
432#
danielk197703ba3fa2009-01-09 10:49:14 +0000433proc reset_db {} {
434 catch {db close}
mistachkinfda06be2011-08-02 00:57:34 +0000435 forcedelete test.db
436 forcedelete test.db-journal
437 forcedelete test.db-wal
danielk197703ba3fa2009-01-09 10:49:14 +0000438 sqlite3 db ./test.db
439 set ::DB [sqlite3_connection_pointer db]
440 if {[info exists ::SETUP_SQL]} {
441 db eval $::SETUP_SQL
442 }
drhcd61c282002-03-06 22:01:34 +0000443}
danielk197703ba3fa2009-01-09 10:49:14 +0000444reset_db
drhbec2bf42000-05-29 23:48:22 +0000445
446# Abort early if this script has been run before.
447#
danc1a60c52010-06-07 14:28:16 +0000448if {[info exists TC(count)]} return
drhbec2bf42000-05-29 23:48:22 +0000449
drhce2198c2010-09-10 13:22:59 +0000450# Make sure memory statistics are enabled.
451#
452sqlite3_config_memstatus 1
453
danc1a60c52010-06-07 14:28:16 +0000454# Initialize the test counters and set up commands to access them.
455# Or, if this is a slave interpreter, set up aliases to write the
456# counters in the parent interpreter.
drhbec2bf42000-05-29 23:48:22 +0000457#
danc1a60c52010-06-07 14:28:16 +0000458if {0==[info exists ::SLAVE]} {
459 set TC(errors) 0
460 set TC(count) 0
461 set TC(fail_list) [list]
462 set TC(omit_list) [list]
463
464 proc set_test_counter {counter args} {
465 if {[llength $args]} {
466 set ::TC($counter) [lindex $args 0]
467 }
468 set ::TC($counter)
469 }
drhb62c3352006-11-23 09:39:16 +0000470}
drh348784e2000-05-29 20:41:49 +0000471
drh521cc842008-04-15 02:36:33 +0000472# Record the fact that a sequence of tests were omitted.
473#
mistachkin6c3c1a02011-11-12 03:17:40 +0000474proc omit_test {name reason {append 1}} {
danc1a60c52010-06-07 14:28:16 +0000475 set omitList [set_test_counter omit_list]
mistachkin6c3c1a02011-11-12 03:17:40 +0000476 if {$append} {
477 lappend omitList [list $name $reason]
478 }
danc1a60c52010-06-07 14:28:16 +0000479 set_test_counter omit_list $omitList
drh521cc842008-04-15 02:36:33 +0000480}
481
danc1a60c52010-06-07 14:28:16 +0000482# Record the fact that a test failed.
483#
484proc fail_test {name} {
485 set f [set_test_counter fail_list]
486 lappend f $name
487 set_test_counter fail_list $f
488 set_test_counter errors [expr [set_test_counter errors] + 1]
489
490 set nFail [set_test_counter errors]
491 if {$nFail>=$::cmdlinearg(maxerror)} {
492 puts "*** Giving up..."
493 finalize_testing
494 }
495}
496
497# Increment the number of tests run
498#
499proc incr_ntest {} {
500 set_test_counter count [expr [set_test_counter count] + 1]
501}
502
503
drh348784e2000-05-29 20:41:49 +0000504# Invoke the do_test procedure to run a single test
505#
506proc do_test {name cmd expected} {
danc1a60c52010-06-07 14:28:16 +0000507 global argv cmdlinearg
508
dan8c408002010-11-01 17:38:24 +0000509 fix_testname name
510
drh4a50aac2007-08-23 02:47:53 +0000511 sqlite3_memdebug_settitle $name
danc1a60c52010-06-07 14:28:16 +0000512
dan92d516a2010-07-05 14:54:48 +0000513# if {[llength $argv]==0} {
514# set go 1
515# } else {
516# set go 0
517# foreach pattern $argv {
518# if {[string match $pattern $name]} {
519# set go 1
520# break
521# }
522# }
523# }
dan430e74c2010-06-07 17:47:26 +0000524
dand506de02010-07-03 13:59:01 +0000525 if {[info exists ::G(perm:prefix)]} {
526 set name "$::G(perm:prefix)$name"
dan430e74c2010-06-07 17:47:26 +0000527 }
528
danc1a60c52010-06-07 14:28:16 +0000529 incr_ntest
drh5edc3122001-09-13 21:53:09 +0000530 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000531 flush stdout
mistachkin6c3c1a02011-11-12 03:17:40 +0000532
533 if {![info exists ::G(match)] || [string match $::G(match) $name]} {
534 if {[catch {uplevel #0 "$cmd;\n"} result]} {
535 puts "\nError: $result"
536 fail_test $name
mistachkin6c3c1a02011-11-12 03:17:40 +0000537 } else {
drh3f17aef2012-04-27 01:08:02 +0000538 if {[regexp {^~?/.*/$} $expected]} {
539 if {[string index $expected 0]=="~"} {
540 set re [string range $expected 2 end-1]
541 set ok [expr {![regexp $re $result]}]
542 } else {
543 set re [string range $expected 1 end-1]
544 set ok [regexp $re $result]
545 }
546 } else {
547 set ok [expr {[string compare $result $expected]==0}]
548 }
549 if {!$ok} {
550 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
551 fail_test $name
552 } else {
553 puts " Ok"
554 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000555 }
drh348784e2000-05-29 20:41:49 +0000556 } else {
mistachkin6c3c1a02011-11-12 03:17:40 +0000557 puts " Omitted"
558 omit_test $name "pattern mismatch" 0
drh348784e2000-05-29 20:41:49 +0000559 }
drh6558db82007-04-13 03:23:21 +0000560 flush stdout
drh348784e2000-05-29 20:41:49 +0000561}
dana3f51082010-09-30 18:43:14 +0000562
drh8df91852012-04-24 12:46:05 +0000563proc catchcmd {db {cmd ""}} {
564 global CLI
565 set out [open cmds.txt w]
566 puts $out $cmd
567 close $out
568 set line "exec $CLI $db < cmds.txt"
569 set rc [catch { eval $line } msg]
570 list $rc $msg
571}
572
shaneh55505172011-06-21 19:30:19 +0000573proc filepath_normalize {p} {
574 # test cases should be written to assume "unix"-like file paths
shaneh285a18f2011-06-21 19:39:59 +0000575 if {$::tcl_platform(platform)!="unix"} {
shaneh55505172011-06-21 19:30:19 +0000576 # lreverse*2 as a hack to remove any unneeded {} after the string map
577 lreverse [lreverse [string map {\\ /} [regsub -nocase -all {[a-z]:[/\\]+} $p {/}]]]
shanehc4896402011-06-21 19:38:16 +0000578 } {
579 set p
shaneh55505172011-06-21 19:30:19 +0000580 }
581}
582proc do_filepath_test {name cmd expected} {
583 uplevel [list do_test $name [
584 subst -nocommands { filepath_normalize [ $cmd ] }
585 ] [filepath_normalize $expected]]
586}
587
dan33f53792011-05-05 19:44:22 +0000588proc realnum_normalize {r} {
shaneh4f529e82011-06-20 17:41:41 +0000589 # different TCL versions display floating point values differently.
590 string map {1.#INF inf Inf inf .0e e} [regsub -all {(e[+-])0+} $r {\1}]
dan33f53792011-05-05 19:44:22 +0000591}
592proc do_realnum_test {name cmd expected} {
593 uplevel [list do_test $name [
594 subst -nocommands { realnum_normalize [ $cmd ] }
595 ] [realnum_normalize $expected]]
596}
597
dana3f51082010-09-30 18:43:14 +0000598proc fix_testname {varname} {
599 upvar $varname testname
600 if {[info exists ::testprefix]
601 && [string is digit [string range $testname 0 0]]
602 } {
603 set testname "${::testprefix}-$testname"
604 }
605}
dan153eda02010-06-21 07:45:47 +0000606
dan57f7f4b2010-10-01 19:04:37 +0000607proc do_execsql_test {testname sql {result {}}} {
dana3f51082010-09-30 18:43:14 +0000608 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000609 uplevel do_test [list $testname] [list "execsql {$sql}"] [list [list {*}$result]]
dan153eda02010-06-21 07:45:47 +0000610}
611proc do_catchsql_test {testname sql result} {
dana3f51082010-09-30 18:43:14 +0000612 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000613 uplevel do_test [list $testname] [list "catchsql {$sql}"] [list $result]
dan153eda02010-06-21 07:45:47 +0000614}
dan39854792010-11-15 16:12:58 +0000615proc do_eqp_test {name sql res} {
616 uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res]
617}
dan153eda02010-06-21 07:45:47 +0000618
dan51f06982010-09-18 19:00:12 +0000619#-------------------------------------------------------------------------
620# Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST
621#
622# Where switches are:
623#
624# -errorformat FMTSTRING
625# -count
danaf1dcab2010-09-20 19:17:53 +0000626# -query SQL
dana16d1062010-09-28 17:37:28 +0000627# -tclquery TCL
danaf7626f2010-09-23 18:47:36 +0000628# -repair TCL
dan51f06982010-09-18 19:00:12 +0000629#
630proc do_select_tests {prefix args} {
631
632 set testlist [lindex $args end]
633 set switches [lrange $args 0 end-1]
634
635 set errfmt ""
636 set countonly 0
dana16d1062010-09-28 17:37:28 +0000637 set tclquery ""
danaf7626f2010-09-23 18:47:36 +0000638 set repair ""
dan51f06982010-09-18 19:00:12 +0000639
640 for {set i 0} {$i < [llength $switches]} {incr i} {
641 set s [lindex $switches $i]
642 set n [string length $s]
danaf1dcab2010-09-20 19:17:53 +0000643 if {$n>=2 && [string equal -length $n $s "-query"]} {
dana16d1062010-09-28 17:37:28 +0000644 set tclquery [list execsql [lindex $switches [incr i]]]
645 } elseif {$n>=2 && [string equal -length $n $s "-tclquery"]} {
646 set tclquery [lindex $switches [incr i]]
danaf1dcab2010-09-20 19:17:53 +0000647 } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} {
dan51f06982010-09-18 19:00:12 +0000648 set errfmt [lindex $switches [incr i]]
danaf7626f2010-09-23 18:47:36 +0000649 } elseif {$n>=2 && [string equal -length $n $s "-repair"]} {
650 set repair [lindex $switches [incr i]]
dan51f06982010-09-18 19:00:12 +0000651 } elseif {$n>=2 && [string equal -length $n $s "-count"]} {
652 set countonly 1
653 } else {
654 error "unknown switch: $s"
655 }
656 }
657
658 if {$countonly && $errfmt!=""} {
659 error "Cannot use -count and -errorformat together"
660 }
661 set nTestlist [llength $testlist]
662 if {$nTestlist%3 || $nTestlist==0 } {
663 error "SELECT test list contains [llength $testlist] elements"
664 }
665
danaf7626f2010-09-23 18:47:36 +0000666 eval $repair
dan51f06982010-09-18 19:00:12 +0000667 foreach {tn sql res} $testlist {
dana16d1062010-09-28 17:37:28 +0000668 if {$tclquery != ""} {
danaf1dcab2010-09-20 19:17:53 +0000669 execsql $sql
dana16d1062010-09-28 17:37:28 +0000670 uplevel do_test ${prefix}.$tn [list $tclquery] [list [list {*}$res]]
671 } elseif {$countonly} {
dan51f06982010-09-18 19:00:12 +0000672 set nRow 0
673 db eval $sql {incr nRow}
674 uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res]
675 } elseif {$errfmt==""} {
676 uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]]
677 } else {
678 set res [list 1 [string trim [format $errfmt {*}$res]]]
679 uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res]
680 }
danaf7626f2010-09-23 18:47:36 +0000681 eval $repair
dan51f06982010-09-18 19:00:12 +0000682 }
danaf7626f2010-09-23 18:47:36 +0000683
dan51f06982010-09-18 19:00:12 +0000684}
685
danb04757a2010-09-21 16:59:16 +0000686proc delete_all_data {} {
687 db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} {
688 db eval "DELETE FROM '[string map {' ''} $t]'"
689 }
690}
drh348784e2000-05-29 20:41:49 +0000691
drhb62c3352006-11-23 09:39:16 +0000692# Run an SQL script.
693# Return the number of microseconds per statement.
694#
drh3590f152006-11-23 21:09:10 +0000695proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000696 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000697 flush stdout
698 set speed [time {sqlite3_exec_nr db $sql}]
699 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000700 if {$tm == 0} {
701 set rate [format %20s "many"]
702 } else {
703 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
704 }
drh3590f152006-11-23 21:09:10 +0000705 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000706 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000707 global total_time
708 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000709 lappend ::speed_trial_times $name $tm
drhdd924312007-03-31 22:34:16 +0000710}
drh45c236d2008-03-22 01:08:00 +0000711proc speed_trial_tcl {name numstmt units script} {
712 puts -nonewline [format {%-21.21s } $name...]
713 flush stdout
714 set speed [time {eval $script}]
715 set tm [lindex $speed 0]
716 if {$tm == 0} {
717 set rate [format %20s "many"]
718 } else {
719 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
720 }
721 set u2 $units/s
722 puts [format {%12d uS %s %s} $tm $rate $u2]
723 global total_time
724 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000725 lappend ::speed_trial_times $name $tm
drh45c236d2008-03-22 01:08:00 +0000726}
drhdd924312007-03-31 22:34:16 +0000727proc speed_trial_init {name} {
728 global total_time
729 set total_time 0
dana975b592010-10-08 16:09:43 +0000730 set ::speed_trial_times [list]
drhbb810a92010-07-03 12:00:53 +0000731 sqlite3 versdb :memory:
732 set vers [versdb one {SELECT sqlite_source_id()}]
733 versdb close
734 puts "SQLite $vers"
drhdd924312007-03-31 22:34:16 +0000735}
736proc speed_trial_summary {name} {
737 global total_time
738 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
dana975b592010-10-08 16:09:43 +0000739
740 if { 0 } {
741 sqlite3 versdb :memory:
742 set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0]
743 versdb close
744 puts "CREATE TABLE IF NOT EXISTS time(version, script, test, us);"
745 foreach {test us} $::speed_trial_times {
746 puts "INSERT INTO time VALUES('$vers', '$name', '$test', $us);"
747 }
748 }
drhb62c3352006-11-23 09:39:16 +0000749}
750
drh348784e2000-05-29 20:41:49 +0000751# Run this routine last
752#
753proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +0000754 catch {db close}
755 catch {db2 close}
756 catch {db3 close}
757 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +0000758}
759proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +0000760 global sqlite_open_file_count
761
762 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +0000763
drh6e142f52000-06-08 13:36:40 +0000764 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000765 catch {db2 close}
766 catch {db3 close}
767
drh9bc54492007-10-23 14:49:59 +0000768 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000769 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000770 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000771 db close
drh984bfaa2008-03-19 16:08:53 +0000772 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +0000773
drh3a7fb7c2007-08-10 16:41:08 +0000774 sqlite3_soft_heap_limit 0
danc1a60c52010-06-07 14:28:16 +0000775 set nTest [incr_ntest]
776 set nErr [set_test_counter errors]
777
drh348784e2000-05-29 20:41:49 +0000778 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000779 if {$nErr>0} {
danc1a60c52010-06-07 14:28:16 +0000780 puts "Failures on these tests: [set_test_counter fail_list]"
drhf3a65f72007-08-22 20:18:21 +0000781 }
danielk1977ee8b7992009-03-26 17:13:06 +0000782 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +0000783 if {[llength $omitList]>0} {
784 puts "Omitted test cases:"
785 set prec {}
786 foreach {rec} [lsort $omitList] {
787 if {$rec==$prec} continue
788 set prec $rec
789 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
790 }
791 }
drh80788d82006-09-02 14:50:23 +0000792 if {$nErr>0 && ![working_64bit_int]} {
793 puts "******************************************************************"
794 puts "N.B.: The version of TCL that you used to build this test harness"
795 puts "is defective in that it does not support 64-bit integers. Some or"
796 puts "all of the test failures above might be a result from this defect"
797 puts "in your TCL build."
798 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000799 }
danc1a60c52010-06-07 14:28:16 +0000800 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000801 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +0000802 }
drh94e92032003-02-16 22:21:32 +0000803 if {$sqlite_open_file_count} {
804 puts "$sqlite_open_file_count files were left open"
805 incr nErr
806 }
drheafc43b2010-07-26 18:43:40 +0000807 if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 ||
808 [sqlite3_memory_used]>0} {
809 puts "Unfreed memory: [sqlite3_memory_used] bytes in\
810 [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations"
drhf3a65f72007-08-22 20:18:21 +0000811 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000812 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000813 puts "Writing unfreed memory log to \"./memleak.txt\""
814 sqlite3_memdebug_dump ./memleak.txt
815 }
drhf3a65f72007-08-22 20:18:21 +0000816 } else {
817 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000818 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000819 sqlite3_memdebug_dump ./memusage.txt
820 }
drhf3a65f72007-08-22 20:18:21 +0000821 }
drhf7141992008-06-19 00:16:08 +0000822 show_memstats
drh91fd4d42008-01-19 20:11:25 +0000823 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
824 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000825 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
826 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
827 }
danc1a60c52010-06-07 14:28:16 +0000828 if {$::cmdlinearg(malloctrace)} {
danielk197735754ac2008-03-21 17:29:37 +0000829 puts "Writing mallocs.sql..."
830 memdebug_log_sql
831 sqlite3_memdebug_log stop
832 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000833
834 if {[sqlite3_memory_used]>0} {
835 puts "Writing leaks.sql..."
836 sqlite3_memdebug_log sync
837 memdebug_log_sql leaks.sql
838 }
danielk197735754ac2008-03-21 17:29:37 +0000839 }
drhd9910fe2006-10-04 11:55:49 +0000840 foreach f [glob -nocomplain test.db-*-journal] {
mistachkinfda06be2011-08-02 00:57:34 +0000841 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +0000842 }
843 foreach f [glob -nocomplain test.db-mj*] {
mistachkinfda06be2011-08-02 00:57:34 +0000844 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +0000845 }
drhdb25e382001-03-15 18:21:22 +0000846 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000847}
848
drhf7141992008-06-19 00:16:08 +0000849# Display memory statistics for analysis and debugging purposes.
850#
851proc show_memstats {} {
852 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +0000853 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
854 set val [format {now %10d max %10d max-size %10d} \
855 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000856 puts "Memory used: $val"
drheafc43b2010-07-26 18:43:40 +0000857 set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0]
858 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
859 puts "Allocation count: $val"
drhf7141992008-06-19 00:16:08 +0000860 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +0000861 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
862 set val [format {now %10d max %10d max-size %10d} \
863 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000864 puts "Page-cache used: $val"
865 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
866 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
867 puts "Page-cache overflow: $val"
868 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
869 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
870 puts "Scratch memory used: $val"
871 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +0000872 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
873 set val [format {now %10d max %10d max-size %10d} \
874 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000875 puts "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +0000876 ifcapable yytrackmaxstackdepth {
877 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
878 set val [format { max %10d} [lindex $x 2]]
879 puts "Parser stack depth: $val"
880 }
drhf7141992008-06-19 00:16:08 +0000881}
882
drh348784e2000-05-29 20:41:49 +0000883# A procedure to execute SQL
884#
drhc4a3c772001-04-04 11:48:57 +0000885proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000886 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000887 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000888}
drh3aadb2e2000-05-31 17:59:25 +0000889
drhadbca9c2001-09-27 15:11:53 +0000890# Execute SQL and catch exceptions.
891#
892proc catchsql {sql {db db}} {
893 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +0000894 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +0000895 lappend r $msg
896 return $r
897}
898
drh04096482001-11-09 22:41:44 +0000899# Do an VDBE code dump on the SQL given
900#
901proc explain {sql {db db}} {
902 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000903 puts "addr opcode p1 p2 p3 p4 p5 #"
904 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000905 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000906 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
907 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
908 ]
drh04096482001-11-09 22:41:44 +0000909 }
910}
911
drhdafc0ce2008-04-17 19:14:02 +0000912# Show the VDBE program for an SQL statement but omit the Trace
913# opcode at the beginning. This procedure can be used to prove
914# that different SQL statements generate exactly the same VDBE code.
915#
916proc explain_no_trace {sql} {
917 set tr [db eval "EXPLAIN $sql"]
918 return [lrange $tr 7 end]
919}
920
drh3aadb2e2000-05-31 17:59:25 +0000921# Another procedure to execute SQL. This one includes the field
922# names in the returned list.
923#
924proc execsql2 {sql} {
925 set result {}
926 db eval $sql data {
927 foreach f $data(*) {
928 lappend result $f $data($f)
929 }
930 }
931 return $result
932}
drh17a68932001-01-31 13:28:08 +0000933
drhdde85d92003-03-01 19:45:34 +0000934# Use the non-callback API to execute multiple SQL statements
935#
936proc stepsql {dbptr sql} {
937 set sql [string trim $sql]
938 set r 0
939 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000940 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000941 return [list 1 $vm]
942 }
943 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000944# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
945# foreach v $VAL {lappend r $v}
946# }
947 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
948 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
949 lappend r [sqlite3_column_text $vm $i]
950 }
drhdde85d92003-03-01 19:45:34 +0000951 }
danielk1977106bb232004-05-21 10:08:53 +0000952 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000953 return [list 1 $errmsg]
954 }
955 }
956 return $r
957}
958
drh21504322002-06-25 13:16:02 +0000959# Do an integrity check of the entire database
960#
danielk197704103022009-02-03 16:51:24 +0000961proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +0000962 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +0000963 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +0000964 }
965}
966
danc6055c72011-04-28 18:46:46 +0000967
968# Return true if the SQL statement passed as the second argument uses a
969# statement transaction.
970#
971proc sql_uses_stmt {db sql} {
972 set stmt [sqlite3_prepare $db $sql -1 dummy]
973 set uses [uses_stmt_journal $stmt]
974 sqlite3_finalize $stmt
975 return $uses
976}
977
danielk1977db4e8862008-01-16 08:24:46 +0000978proc fix_ifcapable_expr {expr} {
979 set ret ""
980 set state 0
981 for {set i 0} {$i < [string length $expr]} {incr i} {
982 set char [string range $expr $i $i]
983 set newstate [expr {[string is alnum $char] || $char eq "_"}]
984 if {$newstate && !$state} {
985 append ret {$::sqlite_options(}
986 }
987 if {!$newstate && $state} {
988 append ret )
989 }
990 append ret $char
991 set state $newstate
992 }
993 if {$state} {append ret )}
994 return $ret
995}
996
drh27d258a2004-10-30 20:23:09 +0000997# Evaluate a boolean expression of capabilities. If true, execute the
998# code. Omit the code if false.
999#
danielk19773e8c37e2005-01-21 03:12:14 +00001000proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +00001001 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
1002 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +00001003 if ($e2) {
1004 set c [catch {uplevel 1 $code} r]
1005 } else {
1006 set c [catch {uplevel 1 $elsecode} r]
1007 }
1008 return -code $c $r
drh21504322002-06-25 13:16:02 +00001009}
danielk197745901d62004-11-10 15:27:38 +00001010
danielk1977aca790a2005-01-13 11:07:52 +00001011# This proc execs a seperate process that crashes midway through executing
1012# the SQL script $sql on database test.db.
1013#
1014# The crash occurs during a sync() of file $crashfile. When the crash
1015# occurs a random subset of all unsynced writes made by the process are
1016# written into the files on disk. Argument $crashdelay indicates the
1017# number of file syncs to wait before crashing.
1018#
1019# The return value is a list of two elements. The first element is a
1020# boolean, indicating whether or not the process actually crashed or
1021# reported some other error. The second element in the returned list is the
1022# error message. This is "child process exited abnormally" if the crash
1023# occured.
1024#
danielk1977f8940ae2007-08-23 11:07:10 +00001025# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +00001026#
1027proc crashsql {args} {
danielk197759a33f92007-03-17 10:26:59 +00001028
1029 set blocksize ""
1030 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +00001031 set prngseed 0
drhf8587402008-01-08 16:03:49 +00001032 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +00001033 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +00001034 set dc ""
danielk197759a33f92007-03-17 10:26:59 +00001035 set sql [lindex $args end]
1036
1037 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
1038 set z [lindex $args $ii]
1039 set n [string length $z]
1040 set z2 [lindex $args [expr $ii+1]]
1041
1042 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +00001043 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +00001044 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +00001045 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +00001046 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +00001047 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +00001048 else { error "Unrecognized option: $z" }
1049 }
1050
1051 if {$crashfile eq ""} {
1052 error "Compulsory option -file missing"
1053 }
1054
shanehf2c08822010-07-08 16:30:44 +00001055 # $crashfile gets compared to the native filename in
1056 # cfSync(), which can be different then what TCL uses by
1057 # default, so here we force it to the "nativename" format.
mistachkinf8a78462012-03-08 20:00:36 +00001058 set cfile [string map {\\ \\\\} [file nativename [file join [get_pwd] $crashfile]]]
danielk1977aca790a2005-01-13 11:07:52 +00001059
1060 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +00001061 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +00001062 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +00001063 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +00001064 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +00001065
1066 # This block sets the cache size of the main database to 10
1067 # pages. This is done in case the build is configured to omit
1068 # "PRAGMA cache_size".
1069 puts $f {db eval {SELECT * FROM sqlite_master;}}
1070 puts $f {set bt [btree_from_db db]}
1071 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +00001072 if {$prngseed} {
1073 set seed [expr {$prngseed%10007+1}]
1074 # puts seed=$seed
1075 puts $f "db eval {SELECT randomblob($seed)}"
1076 }
danielk1977933bbd62007-03-17 07:22:42 +00001077
drhf8587402008-01-08 16:03:49 +00001078 if {[string length $tclbody]>0} {
1079 puts $f $tclbody
1080 }
1081 if {[string length $sql]>0} {
1082 puts $f "db eval {"
1083 puts $f "$sql"
1084 puts $f "}"
1085 }
danielk1977aca790a2005-01-13 11:07:52 +00001086 close $f
danielk1977aca790a2005-01-13 11:07:52 +00001087 set r [catch {
drh9c06c952005-11-26 00:25:00 +00001088 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +00001089 } msg]
shanehf2c08822010-07-08 16:30:44 +00001090
1091 # Windows/ActiveState TCL returns a slightly different
1092 # error message. We map that to the expected message
1093 # so that we don't have to change all of the test
1094 # cases.
1095 if {$::tcl_platform(platform)=="windows"} {
1096 if {$msg=="child killed: unknown signal"} {
1097 set msg "child process exited abnormally"
1098 }
1099 }
1100
danielk1977aca790a2005-01-13 11:07:52 +00001101 lappend r $msg
1102}
1103
danielk197732554c12005-01-22 03:39:39 +00001104# Usage: do_ioerr_test <test number> <options...>
1105#
1106# This proc is used to implement test cases that check that IO errors
1107# are correctly handled. The first argument, <test number>, is an integer
1108# used to name the tests executed by this proc. Options are as follows:
1109#
1110# -tclprep TCL script to run to prepare test.
1111# -sqlprep SQL script to run to prepare test.
1112# -tclbody TCL script to run with IO error simulation.
1113# -sqlbody TCL script to run with IO error simulation.
1114# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +00001115# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +00001116# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +00001117# -start Value of 'N' to begin with (default 1)
1118#
1119# -cksum Boolean. If true, test that the database does
1120# not change during the execution of the test case.
1121#
1122proc do_ioerr_test {testname args} {
1123
danielk197732554c12005-01-22 03:39:39 +00001124 set ::ioerropts(-start) 1
1125 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +00001126 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +00001127 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +00001128 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +00001129 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +00001130 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +00001131 array set ::ioerropts $args
1132
danielk197747cd39c2008-05-12 12:41:15 +00001133 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
1134 # a couple of obscure IO errors that do not return them.
1135 set ::ioerropts(-erc) 0
1136
danielk197732554c12005-01-22 03:39:39 +00001137 set ::go 1
drh93aed5a2008-01-16 17:46:38 +00001138 #reset_prng_state
1139 save_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +00001140 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +00001141 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +00001142 incr ::ioerropts(-count) -1
1143 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +00001144
1145 # Skip this IO error if it was specified with the "-exclude" option.
1146 if {[info exists ::ioerropts(-exclude)]} {
1147 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
1148 }
danielk1977861f7452008-06-05 11:39:11 +00001149 if {$::ioerropts(-restoreprng)} {
1150 restore_prng_state
1151 }
danielk197732554c12005-01-22 03:39:39 +00001152
1153 # Delete the files test.db and test2.db, then execute the TCL and
1154 # SQL (in that order) to prepare for the test case.
1155 do_test $testname.$n.1 {
1156 set ::sqlite_io_error_pending 0
1157 catch {db close}
aswiftaebf4132008-11-21 00:10:35 +00001158 catch {db2 close}
mistachkinfda06be2011-08-02 00:57:34 +00001159 catch {forcedelete test.db}
1160 catch {forcedelete test.db-journal}
1161 catch {forcedelete test2.db}
1162 catch {forcedelete test2.db-journal}
drha34c62d2006-01-06 22:11:20 +00001163 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +00001164 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +00001165 if {[info exists ::ioerropts(-tclprep)]} {
1166 eval $::ioerropts(-tclprep)
1167 }
1168 if {[info exists ::ioerropts(-sqlprep)]} {
1169 execsql $::ioerropts(-sqlprep)
1170 }
1171 expr 0
1172 } {0}
1173
1174 # Read the 'checksum' of the database.
1175 if {$::ioerropts(-cksum)} {
1176 set checksum [cksum]
1177 }
drh93aed5a2008-01-16 17:46:38 +00001178
danielk197732554c12005-01-22 03:39:39 +00001179 # Set the Nth IO error to fail.
1180 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +00001181 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +00001182 set ::sqlite_io_error_pending $n
1183 }] $n
1184
1185 # Create a single TCL script from the TCL and SQL specified
1186 # as the body of the test.
1187 set ::ioerrorbody {}
1188 if {[info exists ::ioerropts(-tclbody)]} {
1189 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
1190 }
1191 if {[info exists ::ioerropts(-sqlbody)]} {
1192 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
1193 }
1194
1195 # Execute the TCL Script created in the above block. If
1196 # there are at least N IO operations performed by SQLite as
1197 # a result of the script, the Nth will fail.
1198 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +00001199 set ::sqlite_io_error_hit 0
1200 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +00001201 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +00001202 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +00001203 set rc [sqlite3_errcode $::DB]
1204 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +00001205 # If we are in extended result code mode, make sure all of the
1206 # IOERRs we get back really do have their extended code values.
1207 # If an extended result code is returned, the sqlite3_errcode
1208 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
1209 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +00001210 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
1211 return $rc
1212 }
1213 } else {
drh5f7b5bf2007-04-19 12:30:54 +00001214 # If we are not in extended result code mode, make sure no
1215 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +00001216 if {[regexp {\+\d} $rc]} {
1217 return $rc
1218 }
1219 }
drh93aed5a2008-01-16 17:46:38 +00001220 # The test repeats as long as $::go is non-zero. $::go starts out
1221 # as 1. When a test runs to completion without hitting an I/O
1222 # error, that means there is no point in continuing with this test
1223 # case so set $::go to zero.
1224 #
1225 if {$::sqlite_io_error_pending>0} {
1226 set ::go 0
1227 set q 0
1228 set ::sqlite_io_error_pending 0
1229 } else {
1230 set q 1
1231 }
1232
drhc9ac5ca2005-11-04 22:03:30 +00001233 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +00001234 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
1235 set r 1
1236 }
danielk1977f2fa8312006-01-24 13:09:33 +00001237 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +00001238
1239 # One of two things must have happened. either
1240 # 1. We never hit the IO error and the SQL returned OK
1241 # 2. An IO error was hit and the SQL failed
1242 #
dand41a29a2010-05-06 15:56:28 +00001243 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +00001244 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +00001245 } {1}
1246
danielk197780daec62008-05-12 10:57:02 +00001247 set ::sqlite_io_error_hit 0
1248 set ::sqlite_io_error_pending 0
1249
danielk197752b472a2008-05-05 16:23:55 +00001250 # Check that no page references were leaked. There should be
1251 # a single reference if there is still an active transaction,
1252 # or zero otherwise.
1253 #
danielk197781620542008-06-07 05:19:37 +00001254 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
1255 # locks are established on database files (i.e. if the error
1256 # occurs while attempting to detect a hot-journal file), then
1257 # there may 0 page references and an active transaction according
1258 # to [sqlite3_get_autocommit].
1259 #
danielk19774abd5442008-05-05 15:26:50 +00001260 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +00001261 do_test $testname.$n.4 {
1262 set bt [btree_from_db db]
1263 db_enter db
1264 array set stats [btree_pager_stats $bt]
1265 db_leave db
danielk197781620542008-06-07 05:19:37 +00001266 set nRef $stats(ref)
1267 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
1268 } {1}
danielk19774abd5442008-05-05 15:26:50 +00001269 }
1270
danielk197752b472a2008-05-05 16:23:55 +00001271 # If there is an open database handle and no open transaction,
1272 # and the pager is not running in exclusive-locking mode,
1273 # check that the pager is in "unlocked" state. Theoretically,
1274 # if a call to xUnlock() failed due to an IO error the underlying
1275 # file may still be locked.
1276 #
1277 ifcapable pragma {
1278 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +00001279 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +00001280 && [db one {pragma locking_mode}] eq "normal"
1281 && [sqlite3_get_autocommit db]
1282 } {
1283 do_test $testname.$n.5 {
1284 set bt [btree_from_db db]
1285 db_enter db
1286 array set stats [btree_pager_stats $bt]
1287 db_leave db
1288 set stats(state)
1289 } 0
1290 }
1291 }
1292
danielk197732554c12005-01-22 03:39:39 +00001293 # If an IO error occured, then the checksum of the database should
1294 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +00001295 #
drh1aa5af12008-03-07 19:51:14 +00001296 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +00001297 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +00001298 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +00001299 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +00001300 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +00001301 cksum
1302 } $checksum
1303 }
1304
drh1aa5af12008-03-07 19:51:14 +00001305 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +00001306 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +00001307 if {[info exists ::ioerropts(-cleanup)]} {
1308 catch $::ioerropts(-cleanup)
1309 }
danielk197732554c12005-01-22 03:39:39 +00001310 }
1311 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +00001312 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +00001313 unset ::ioerropts
1314}
1315
drh93aed5a2008-01-16 17:46:38 +00001316# Return a checksum based on the contents of the main database associated
1317# with connection $db
danielk197732554c12005-01-22 03:39:39 +00001318#
1319proc cksum {{db db}} {
1320 set txt [$db eval {
1321 SELECT name, type, sql FROM sqlite_master order by name
1322 }]\n
1323 foreach tbl [$db eval {
1324 SELECT name FROM sqlite_master WHERE type='table' order by name
1325 }] {
1326 append txt [$db eval "SELECT * FROM $tbl"]\n
1327 }
1328 foreach prag {default_synchronous default_cache_size} {
1329 append txt $prag-[$db eval "PRAGMA $prag"]\n
1330 }
1331 set cksum [string length $txt]-[md5 $txt]
1332 # puts $cksum-[file size test.db]
1333 return $cksum
1334}
1335
drh93aed5a2008-01-16 17:46:38 +00001336# Generate a checksum based on the contents of the main and temp tables
1337# database $db. If the checksum of two databases is the same, and the
1338# integrity-check passes for both, the two databases are identical.
1339#
drh1db639c2008-01-17 02:36:28 +00001340proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +00001341 set ret [list]
1342 ifcapable tempdb {
1343 set sql {
1344 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1345 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
1346 SELECT 'sqlite_master' UNION
1347 SELECT 'sqlite_temp_master' ORDER BY 1
1348 }
1349 } else {
1350 set sql {
1351 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1352 SELECT 'sqlite_master' ORDER BY 1
1353 }
1354 }
1355 set tbllist [$db eval $sql]
1356 set txt {}
1357 foreach tbl $tbllist {
1358 append txt [$db eval "SELECT * FROM $tbl"]
1359 }
1360 foreach prag {default_cache_size} {
1361 append txt $prag-[$db eval "PRAGMA $prag"]\n
1362 }
1363 # puts txt=$txt
1364 return [md5 $txt]
1365}
1366
drhdc2c4912009-02-04 22:46:47 +00001367# Generate a checksum based on the contents of a single database with
1368# a database connection. The name of the database is $dbname.
1369# Examples of $dbname are "temp" or "main".
1370#
1371proc dbcksum {db dbname} {
1372 if {$dbname=="temp"} {
1373 set master sqlite_temp_master
1374 } else {
1375 set master $dbname.sqlite_master
1376 }
1377 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
1378 set txt [$db eval "SELECT * FROM $master"]\n
1379 foreach tab $alltab {
1380 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
1381 }
1382 return [md5 $txt]
1383}
1384
danielk197735754ac2008-03-21 17:29:37 +00001385proc memdebug_log_sql {{filename mallocs.sql}} {
1386
danielk19776f332c12008-03-21 14:22:44 +00001387 set data [sqlite3_memdebug_log dump]
1388 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +00001389 if {$nFrame < 0} { return "" }
1390
danielk197735754ac2008-03-21 17:29:37 +00001391 set database temp
1392
danielk19776ab3a2e2009-02-19 14:39:25 +00001393 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +00001394
1395 set sql ""
1396 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +00001397 set nCall [lindex $e 0]
1398 set nByte [lindex $e 1]
1399 set lStack [lrange $e 2 end]
1400 append sql "INSERT INTO ${database}.malloc VALUES"
1401 append sql "('test', $nCall, $nByte, '$lStack');\n"
1402 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +00001403 set frames($f) 1
1404 }
1405 }
1406
1407 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +00001408 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +00001409
1410 foreach f [array names frames] {
1411 set addr [format %x $f]
1412 set cmd "addr2line -e [info nameofexec] $addr"
1413 set line [eval exec $cmd]
1414 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00001415
1416 set file [lindex [split $line :] 0]
1417 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00001418 }
1419
danielk197735754ac2008-03-21 17:29:37 +00001420 foreach f [array names files] {
1421 set contents ""
1422 catch {
1423 set fd [open $f]
1424 set contents [read $fd]
1425 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001426 }
danielk197735754ac2008-03-21 17:29:37 +00001427 set contents [string map {' ''} $contents]
1428 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00001429 }
danielk19776f332c12008-03-21 14:22:44 +00001430
danielk197735754ac2008-03-21 17:29:37 +00001431 set fd [open $filename w]
1432 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
1433 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001434}
drh93aed5a2008-01-16 17:46:38 +00001435
danf5894502009-10-07 18:41:19 +00001436# Drop all tables in database [db]
1437proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00001438 ifcapable trigger&&foreignkey {
1439 set pk [$db one "PRAGMA foreign_keys"]
1440 $db eval "PRAGMA foreign_keys = OFF"
1441 }
drh9a6ffc82010-02-15 18:03:20 +00001442 foreach {idx name file} [db eval {PRAGMA database_list}] {
1443 if {$idx==1} {
1444 set master sqlite_temp_master
1445 } else {
1446 set master $name.sqlite_master
1447 }
1448 foreach {t type} [$db eval "
1449 SELECT name, type FROM $master
dana16d1062010-09-28 17:37:28 +00001450 WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X'
drh9a6ffc82010-02-15 18:03:20 +00001451 "] {
dana16d1062010-09-28 17:37:28 +00001452 $db eval "DROP $type \"$t\""
drh9a6ffc82010-02-15 18:03:20 +00001453 }
danf5894502009-10-07 18:41:19 +00001454 }
shaneh4e7b32f2009-12-17 22:12:51 +00001455 ifcapable trigger&&foreignkey {
1456 $db eval "PRAGMA foreign_keys = $pk"
1457 }
danf5894502009-10-07 18:41:19 +00001458}
1459
dan71cb5182010-04-26 12:39:03 +00001460#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00001461# If a test script is executed with global variable $::G(perm:name) set to
1462# "wal", then the tests are run in WAL mode. Otherwise, they should be run
1463# in rollback mode. The following Tcl procs are used to make this less
1464# intrusive:
dan71cb5182010-04-26 12:39:03 +00001465#
1466# wal_set_journal_mode ?DB?
1467#
1468# If running a WAL test, execute "PRAGMA journal_mode = wal" using
1469# connection handle DB. Otherwise, this command is a no-op.
1470#
1471# wal_check_journal_mode TESTNAME ?DB?
1472#
1473# If running a WAL test, execute a tests case that fails if the main
1474# database for connection handle DB is not currently a WAL database.
1475# Otherwise (if not running a WAL permutation) this is a no-op.
1476#
1477# wal_is_wal_mode
1478#
1479# Returns true if this test should be run in WAL mode. False otherwise.
1480#
1481proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00001482 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00001483}
1484proc wal_set_journal_mode {{db db}} {
1485 if { [wal_is_wal_mode] } {
1486 $db eval "PRAGMA journal_mode = WAL"
1487 }
1488}
1489proc wal_check_journal_mode {testname {db db}} {
1490 if { [wal_is_wal_mode] } {
1491 $db eval { SELECT * FROM sqlite_master }
1492 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
1493 }
1494}
1495
dan430e74c2010-06-07 17:47:26 +00001496proc permutation {} {
1497 set perm ""
1498 catch {set perm $::G(perm:name)}
1499 set perm
1500}
dancb79e512010-08-06 13:50:07 +00001501proc presql {} {
1502 set presql ""
1503 catch {set presql $::G(perm:presql)}
1504 set presql
1505}
dan430e74c2010-06-07 17:47:26 +00001506
danc1a60c52010-06-07 14:28:16 +00001507#-------------------------------------------------------------------------
1508#
1509proc slave_test_script {script} {
1510
1511 # Create the interpreter used to run the test script.
1512 interp create tinterp
1513
1514 # Populate some global variables that tester.tcl expects to see.
1515 foreach {var value} [list \
1516 ::argv0 $::argv0 \
1517 ::argv {} \
1518 ::SLAVE 1 \
1519 ] {
1520 interp eval tinterp [list set $var $value]
1521 }
1522
1523 # The alias used to access the global test counters.
1524 tinterp alias set_test_counter set_test_counter
1525
1526 # Set up the ::cmdlinearg array in the slave.
1527 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
1528
dan430e74c2010-06-07 17:47:26 +00001529 # Set up the ::G array in the slave.
1530 interp eval tinterp [list array set ::G [array get ::G]]
1531
danc1a60c52010-06-07 14:28:16 +00001532 # Load the various test interfaces implemented in C.
1533 load_testfixture_extensions tinterp
1534
1535 # Run the test script.
1536 interp eval tinterp $script
1537
danea5542d2010-07-06 11:26:15 +00001538 # Check if the interpreter call [run_thread_tests]
1539 if { [interp eval tinterp {info exists ::run_thread_tests_called}] } {
1540 set ::run_thread_tests_called 1
1541 }
1542
danc1a60c52010-06-07 14:28:16 +00001543 # Delete the interpreter used to run the test script.
1544 interp delete tinterp
1545}
1546
dan430e74c2010-06-07 17:47:26 +00001547proc slave_test_file {zFile} {
dan0626dfc2010-06-15 06:56:37 +00001548 set tail [file tail $zFile]
dan430e74c2010-06-07 17:47:26 +00001549
dan6a64d672011-04-04 15:38:16 +00001550 if {[info exists ::G(start:permutation)]} {
1551 if {[permutation] != $::G(start:permutation)} return
1552 unset ::G(start:permutation)
1553 }
1554 if {[info exists ::G(start:file)]} {
1555 if {$tail != $::G(start:file) && $tail!="$::G(start:file).test"} return
1556 unset ::G(start:file)
1557 }
1558
dan92d516a2010-07-05 14:54:48 +00001559 # Remember the value of the shared-cache setting. So that it is possible
1560 # to check afterwards that it was not modified by the test script.
1561 #
1562 ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] }
dan0626dfc2010-06-15 06:56:37 +00001563
dan92d516a2010-07-05 14:54:48 +00001564 # Run the test script in a slave interpreter.
1565 #
danea5542d2010-07-06 11:26:15 +00001566 unset -nocomplain ::run_thread_tests_called
dan0626dfc2010-06-15 06:56:37 +00001567 reset_prng_state
1568 set ::sqlite_open_file_count 0
1569 set time [time { slave_test_script [list source $zFile] }]
1570 set ms [expr [lindex $time 0] / 1000]
1571
dan92d516a2010-07-05 14:54:48 +00001572 # Test that all files opened by the test script were closed. Omit this
1573 # if the test script has "thread" in its name. The open file counter
1574 # is not thread-safe.
dan0626dfc2010-06-15 06:56:37 +00001575 #
danea5542d2010-07-06 11:26:15 +00001576 if {[info exists ::run_thread_tests_called]==0} {
dan92d516a2010-07-05 14:54:48 +00001577 do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0}
1578 }
dan430e74c2010-06-07 17:47:26 +00001579 set ::sqlite_open_file_count 0
1580
dan0626dfc2010-06-15 06:56:37 +00001581 # Test that the global "shared-cache" setting was not altered by
1582 # the test script.
1583 #
1584 ifcapable shared_cache {
1585 set res [expr {[sqlite3_enable_shared_cache] == $scs}]
1586 do_test ${tail}-sharedcachesetting [list set {} $res] 1
1587 }
dan430e74c2010-06-07 17:47:26 +00001588
dan92d516a2010-07-05 14:54:48 +00001589 # Add some info to the output.
1590 #
dan0626dfc2010-06-15 06:56:37 +00001591 puts "Time: $tail $ms ms"
dan430e74c2010-06-07 17:47:26 +00001592 show_memstats
danc1a60c52010-06-07 14:28:16 +00001593}
1594
dan59257dc2010-08-04 11:34:31 +00001595# Open a new connection on database test.db and execute the SQL script
1596# supplied as an argument. Before returning, close the new conection and
1597# restore the 4 byte fields starting at header offsets 28, 92 and 96
1598# to the values they held before the SQL was executed. This simulates
1599# a write by a pre-3.7.0 client.
1600#
1601proc sql36231 {sql} {
1602 set B [hexio_read test.db 92 8]
1603 set A [hexio_read test.db 28 4]
1604 sqlite3 db36231 test.db
1605 catch { db36231 func a_string a_string }
1606 execsql $sql db36231
1607 db36231 close
1608 hexio_write test.db 28 $A
1609 hexio_write test.db 92 $B
1610 return ""
1611}
danf5894502009-10-07 18:41:19 +00001612
danccc7ff42010-11-29 12:06:45 +00001613proc db_save {} {
1614 foreach f [glob -nocomplain sv_test.db*] { forcedelete $f }
1615 foreach f [glob -nocomplain test.db*] {
1616 set f2 "sv_$f"
mistachkinfda06be2011-08-02 00:57:34 +00001617 forcecopy $f $f2
danccc7ff42010-11-29 12:06:45 +00001618 }
1619}
1620proc db_save_and_close {} {
1621 db_save
1622 catch { db close }
1623 return ""
1624}
1625proc db_restore {} {
1626 foreach f [glob -nocomplain test.db*] { forcedelete $f }
1627 foreach f2 [glob -nocomplain sv_test.db*] {
1628 set f [string range $f2 3 end]
mistachkinfda06be2011-08-02 00:57:34 +00001629 forcecopy $f2 $f
danccc7ff42010-11-29 12:06:45 +00001630 }
1631}
1632proc db_restore_and_reopen {{dbfile test.db}} {
1633 catch { db close }
1634 db_restore
1635 sqlite3 db $dbfile
1636}
1637proc db_delete_and_reopen {{file test.db}} {
1638 catch { db close }
mistachkinfda06be2011-08-02 00:57:34 +00001639 foreach f [glob -nocomplain test.db*] { forcedelete $f }
danccc7ff42010-11-29 12:06:45 +00001640 sqlite3 db $file
1641}
1642
danielk197745901d62004-11-10 15:27:38 +00001643# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
1644# to non-zero, then set the global variable $AUTOVACUUM to 1.
1645set AUTOVACUUM $sqlite_options(default_autovacuum)
danielk1977ee8b7992009-03-26 17:13:06 +00001646
dan64b95bb2012-05-12 05:30:29 +00001647# Make sure the FTS enhanced query syntax is disabled.
1648set sqlite_fts3_enable_parentheses 0
1649
danielk1977ee8b7992009-03-26 17:13:06 +00001650source $testdir/thread_common.tcl
danddf80eb2010-10-25 12:47:43 +00001651source $testdir/malloc_common.tcl