blob: 2fcae7b5a212aeb75ee1021ee081315aff7f9a52 [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#-------------------------------------------------------------------------
mistachkin1d406e02013-08-29 01:09:14 +000017# The commands provided by the code in this file to help with creating
danc1a60c52010-06-07 14:28:16 +000018# 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
dan67340072011-04-16 19:23:10 +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#
mistachkinc60941f2012-09-13 01:51:02 +000034# capable EXPR
danc1a60c52010-06-07 14:28:16 +000035# ifcapable EXPR
36#
37# Calulate checksums based on database contents:
38#
39# dbcksum DB DBNAME
40# allcksum ?DB?
41# cksum ?DB?
42#
43# Commands to execute/explain SQL statements:
44#
mistachkin1d406e02013-08-29 01:09:14 +000045# memdbsql SQL
danc1a60c52010-06-07 14:28:16 +000046# stepsql DB SQL
47# execsql2 SQL
48# explain_no_trace SQL
49# explain SQL ?DB?
50# catchsql SQL ?DB?
51# execsql SQL ?DB?
52#
53# Commands to run test cases:
54#
55# do_ioerr_test TESTNAME ARGS...
56# crashsql ARGS...
57# integrity_check TESTNAME ?DB?
drh433dccf2013-02-09 15:37:11 +000058# verify_ex_errcode TESTNAME EXPECTED ?DB?
danc1a60c52010-06-07 14:28:16 +000059# do_test TESTNAME SCRIPT EXPECTED
dan153eda02010-06-21 07:45:47 +000060# do_execsql_test TESTNAME SQL EXPECTED
61# do_catchsql_test TESTNAME SQL EXPECTED
drh3c2aeae2014-01-24 14:37:44 +000062# do_timed_execsql_test TESTNAME SQL EXPECTED
danc1a60c52010-06-07 14:28:16 +000063#
dan430e74c2010-06-07 17:47:26 +000064# Commands providing a lower level interface to the global test counters:
danc1a60c52010-06-07 14:28:16 +000065#
66# set_test_counter COUNTER ?VALUE?
mistachkin6c3c1a02011-11-12 03:17:40 +000067# omit_test TESTNAME REASON ?APPEND?
danc1a60c52010-06-07 14:28:16 +000068# fail_test TESTNAME
69# incr_ntest
70#
71# Command run at the end of each test file:
72#
73# finish_test
74#
dan430e74c2010-06-07 17:47:26 +000075# Commands to help create test files that run with the "WAL" and other
76# permutations (see file permutations.test):
danc1a60c52010-06-07 14:28:16 +000077#
78# wal_is_wal_mode
79# wal_set_journal_mode ?DB?
80# wal_check_journal_mode TESTNAME?DB?
dan430e74c2010-06-07 17:47:26 +000081# permutation
dancb79e512010-08-06 13:50:07 +000082# presql
danc1a60c52010-06-07 14:28:16 +000083#
dan17c08232015-06-09 15:58:28 +000084# Command to test whether or not --verbose=1 was specified on the command
85# line (returns 0 for not-verbose, 1 for verbose and 2 for "verbose in the
86# output file only").
87#
88# verbose
89#
drh348784e2000-05-29 20:41:49 +000090
mistachkin1d406e02013-08-29 01:09:14 +000091# Set the precision of FP arithmatic used by the interpreter. And
danc1a60c52010-06-07 14:28:16 +000092# configure SQLite to take database file locks on the page that begins
93# 64KB into the database file instead of the one 1GB in. This means
94# the code that handles that special case can be tested without creating
95# very large database files.
96#
drh92febd92004-08-20 18:34:20 +000097set tcl_precision 15
drhc7a3bb92009-02-05 16:31:45 +000098sqlite3_test_control_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +000099
danc1a60c52010-06-07 14:28:16 +0000100
mistachkin1d406e02013-08-29 01:09:14 +0000101# If the pager codec is available, create a wrapper for the [sqlite3]
danc1a60c52010-06-07 14:28:16 +0000102# command that appends "-key {xyzzy}" to the command line. i.e. this:
drh3a7fb7c2007-08-10 16:41:08 +0000103#
danc1a60c52010-06-07 14:28:16 +0000104# sqlite3 db test.db
drh4a50aac2007-08-23 02:47:53 +0000105#
danc1a60c52010-06-07 14:28:16 +0000106# becomes
drh4a50aac2007-08-23 02:47:53 +0000107#
danc1a60c52010-06-07 14:28:16 +0000108# sqlite3 db test.db -key {xyzzy}
drh9eb9e262004-02-11 02:18:05 +0000109#
dan430e74c2010-06-07 17:47:26 +0000110if {[info command sqlite_orig]==""} {
drhef4ac8f2004-06-19 00:16:31 +0000111 rename sqlite3 sqlite_orig
112 proc sqlite3 {args} {
dan68928b62010-06-22 13:46:43 +0000113 if {[llength $args]>=2 && [string index [lindex $args 0] 0]!="-"} {
dan430e74c2010-06-07 17:47:26 +0000114 # This command is opening a new database connection.
115 #
116 if {[info exists ::G(perm:sqlite3_args)]} {
117 set args [concat $args $::G(perm:sqlite3_args)]
118 }
dan68928b62010-06-22 13:46:43 +0000119 if {[sqlite_orig -has-codec] && ![info exists ::do_not_use_codec]} {
dan430e74c2010-06-07 17:47:26 +0000120 lappend args -key {xyzzy}
121 }
122
dan3ccd20a2010-06-09 19:01:02 +0000123 set res [uplevel 1 sqlite_orig $args]
dan430e74c2010-06-07 17:47:26 +0000124 if {[info exists ::G(perm:presql)]} {
125 [lindex $args 0] eval $::G(perm:presql)
126 }
dan1ce1b4a2010-12-07 14:32:28 +0000127 if {[info exists ::G(perm:dbconfig)]} {
128 set ::dbhandle [lindex $args 0]
129 uplevel #0 $::G(perm:dbconfig)
130 }
dan3ccd20a2010-06-09 19:01:02 +0000131 set res
dan430e74c2010-06-07 17:47:26 +0000132 } else {
mistachkin1d406e02013-08-29 01:09:14 +0000133 # This command is not opening a new database connection. Pass the
mistachkin48864df2013-03-21 21:20:32 +0000134 # arguments through to the C implementation as the are.
dan430e74c2010-06-07 17:47:26 +0000135 #
136 uplevel 1 sqlite_orig $args
drh9eb9e262004-02-11 02:18:05 +0000137 }
drh9eb9e262004-02-11 02:18:05 +0000138 }
139}
140
mistachkinfda06be2011-08-02 00:57:34 +0000141proc getFileRetries {} {
142 if {![info exists ::G(file-retries)]} {
143 #
144 # NOTE: Return the default number of retries for [file] operations. A
145 # value of zero or less here means "disabled".
146 #
mistachkinc60941f2012-09-13 01:51:02 +0000147 return [expr {$::tcl_platform(platform) eq "windows" ? 50 : 0}]
mistachkinfda06be2011-08-02 00:57:34 +0000148 }
149 return $::G(file-retries)
150}
151
152proc getFileRetryDelay {} {
153 if {![info exists ::G(file-retry-delay)]} {
154 #
155 # NOTE: Return the default number of milliseconds to wait when retrying
156 # failed [file] operations. A value of zero or less means "do not
157 # wait".
158 #
159 return 100; # TODO: Good default?
160 }
161 return $::G(file-retry-delay)
162}
163
mistachkinf8a78462012-03-08 20:00:36 +0000164# Return the string representing the name of the current directory. On
165# Windows, the result is "normalized" to whatever our parent command shell
166# is using to prevent case-mismatch issues.
167#
168proc get_pwd {} {
169 if {$::tcl_platform(platform) eq "windows"} {
mistachkin533b8f62012-03-08 20:28:31 +0000170 #
171 # NOTE: Cannot use [file normalize] here because it would alter the
172 # case of the result to what Tcl considers canonical, which would
173 # defeat the purpose of this procedure.
174 #
175 return [string map [list \\ /] \
176 [string trim [exec -- $::env(ComSpec) /c echo %CD%]]]
mistachkinf8a78462012-03-08 20:00:36 +0000177 } else {
178 return [pwd]
179 }
180}
181
mistachkinfda06be2011-08-02 00:57:34 +0000182# Copy file $from into $to. This is used because some versions of
183# TCL for windows (notably the 8.4.1 binary package shipped with the
184# current mingw release) have a broken "file copy" command.
185#
186proc copy_file {from to} {
187 do_copy_file false $from $to
188}
189
190proc forcecopy {from to} {
191 do_copy_file true $from $to
192}
193
194proc do_copy_file {force from to} {
195 set nRetry [getFileRetries] ;# Maximum number of retries.
196 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
197
198 # On windows, sometimes even a [file copy -force] can fail. The cause is
199 # usually "tag-alongs" - programs like anti-virus software, automatic backup
200 # tools and various explorer extensions that keep a file open a little longer
201 # than we expect, causing the delete to fail.
202 #
203 # The solution is to wait a short amount of time before retrying the copy.
204 #
205 if {$nRetry > 0} {
206 for {set i 0} {$i<$nRetry} {incr i} {
207 set rc [catch {
208 if {$force} {
209 file copy -force $from $to
210 } else {
211 file copy $from $to
212 }
213 } msg]
214 if {$rc==0} break
215 if {$nDelay > 0} { after $nDelay }
216 }
217 if {$rc} { error $msg }
218 } else {
219 if {$force} {
220 file copy -force $from $to
221 } else {
222 file copy $from $to
223 }
224 }
225}
226
mistachkinc5484652012-03-05 22:52:33 +0000227# Check if a file name is relative
228#
229proc is_relative_file { file } {
230 return [expr {[file pathtype $file] != "absolute"}]
231}
232
mistachkin4a41f342012-03-06 03:00:49 +0000233# If the VFS supports using the current directory, returns [pwd];
234# otherwise, it returns only the provided suffix string (which is
235# empty by default).
236#
237proc test_pwd { args } {
238 if {[llength $args] > 0} {
239 set suffix1 [lindex $args 0]
240 if {[llength $args] > 1} {
241 set suffix2 [lindex $args 1]
242 } else {
243 set suffix2 $suffix1
244 }
245 } else {
246 set suffix1 ""; set suffix2 ""
247 }
248 ifcapable curdir {
mistachkin6aa18c92012-03-08 20:22:42 +0000249 return "[get_pwd]$suffix1"
mistachkin4a41f342012-03-06 03:00:49 +0000250 } else {
251 return $suffix2
252 }
253}
254
mistachkinfda06be2011-08-02 00:57:34 +0000255# Delete a file or directory
256#
257proc delete_file {args} {
258 do_delete_file false {*}$args
259}
260
261proc forcedelete {args} {
262 do_delete_file true {*}$args
263}
264
265proc do_delete_file {force args} {
266 set nRetry [getFileRetries] ;# Maximum number of retries.
267 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
268
269 foreach filename $args {
270 # On windows, sometimes even a [file delete -force] can fail just after
271 # a file is closed. The cause is usually "tag-alongs" - programs like
272 # anti-virus software, automatic backup tools and various explorer
273 # extensions that keep a file open a little longer than we expect, causing
274 # the delete to fail.
275 #
276 # The solution is to wait a short amount of time before retrying the
277 # delete.
278 #
279 if {$nRetry > 0} {
280 for {set i 0} {$i<$nRetry} {incr i} {
281 set rc [catch {
282 if {$force} {
283 file delete -force $filename
284 } else {
285 file delete $filename
286 }
287 } msg]
288 if {$rc==0} break
289 if {$nDelay > 0} { after $nDelay }
290 }
291 if {$rc} { error $msg }
292 } else {
293 if {$force} {
294 file delete -force $filename
295 } else {
296 file delete $filename
297 }
298 }
299 }
300}
301
mistachkin1d406e02013-08-29 01:09:14 +0000302if {$::tcl_platform(platform) eq "windows"} {
303 proc do_remove_win32_dir {args} {
304 set nRetry [getFileRetries] ;# Maximum number of retries.
305 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
306
307 foreach dirName $args {
308 # On windows, sometimes even a [remove_win32_dir] can fail just after
309 # a directory is emptied. The cause is usually "tag-alongs" - programs
310 # like anti-virus software, automatic backup tools and various explorer
311 # extensions that keep a file open a little longer than we expect,
312 # causing the delete to fail.
313 #
314 # The solution is to wait a short amount of time before retrying the
315 # removal.
316 #
317 if {$nRetry > 0} {
318 for {set i 0} {$i < $nRetry} {incr i} {
319 set rc [catch {
320 remove_win32_dir $dirName
321 } msg]
322 if {$rc == 0} break
323 if {$nDelay > 0} { after $nDelay }
324 }
325 if {$rc} { error $msg }
326 } else {
327 remove_win32_dir $dirName
328 }
329 }
330 }
331
332 proc do_delete_win32_file {args} {
333 set nRetry [getFileRetries] ;# Maximum number of retries.
334 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
335
336 foreach fileName $args {
337 # On windows, sometimes even a [delete_win32_file] can fail just after
338 # a file is closed. The cause is usually "tag-alongs" - programs like
339 # anti-virus software, automatic backup tools and various explorer
340 # extensions that keep a file open a little longer than we expect,
341 # causing the delete to fail.
342 #
343 # The solution is to wait a short amount of time before retrying the
344 # delete.
345 #
346 if {$nRetry > 0} {
347 for {set i 0} {$i < $nRetry} {incr i} {
348 set rc [catch {
349 delete_win32_file $fileName
350 } msg]
351 if {$rc == 0} break
352 if {$nDelay > 0} { after $nDelay }
353 }
354 if {$rc} { error $msg }
355 } else {
356 delete_win32_file $fileName
357 }
358 }
359 }
360}
361
dancb354602010-07-08 09:44:42 +0000362proc execpresql {handle args} {
363 trace remove execution $handle enter [list execpresql $handle]
364 if {[info exists ::G(perm:presql)]} {
365 $handle eval $::G(perm:presql)
366 }
367}
368
dan68928b62010-06-22 13:46:43 +0000369# This command should be called after loading tester.tcl from within
370# all test scripts that are incompatible with encryption codecs.
371#
372proc do_not_use_codec {} {
373 set ::do_not_use_codec 1
374 reset_db
375}
376
drhaf3906a2016-03-14 17:05:04 +0000377# Return true if the "reserved_bytes" integer on database files is non-zero.
378#
379proc nonzero_reserved_bytes {} {
380 return [sqlite3 -has-codec]
381}
382
drh4b7b1c92016-02-15 19:38:17 +0000383# Print a HELP message and exit
384#
385proc print_help_and_quit {} {
386 puts {Options:
387 --pause Wait for user input before continuing
388 --soft-heap-limit=N Set the soft-heap-limit to N
389 --maxerror=N Quit after N errors
390 --verbose=(0|1) Control the amount of output. Default '1'
391 --output=FILE set --verbose=2 and output to FILE. Implies -q
392 -q Shorthand for --verbose=0
393 --help This message
394}
395 exit 1
396}
397
dan430e74c2010-06-07 17:47:26 +0000398# The following block only runs the first time this file is sourced. It
399# does not run in slave interpreters (since the ::cmdlinearg array is
400# populated before the test script is run in slave interpreters).
drhbec2bf42000-05-29 23:48:22 +0000401#
danc1a60c52010-06-07 14:28:16 +0000402if {[info exists cmdlinearg]==0} {
403
mistachkin1d406e02013-08-29 01:09:14 +0000404 # Parse any options specified in the $argv array. This script accepts the
405 # following options:
danc1a60c52010-06-07 14:28:16 +0000406 #
407 # --pause
408 # --soft-heap-limit=NN
409 # --maxerror=NN
410 # --malloctrace=N
411 # --backtrace=N
412 # --binarylog=N
dan0626dfc2010-06-15 06:56:37 +0000413 # --soak=N
mistachkinfda06be2011-08-02 00:57:34 +0000414 # --file-retries=N
415 # --file-retry-delay=N
dan6a64d672011-04-04 15:38:16 +0000416 # --start=[$permutation:]$testfile
mistachkin6c3c1a02011-11-12 03:17:40 +0000417 # --match=$pattern
dan17c08232015-06-09 15:58:28 +0000418 # --verbose=$val
419 # --output=$filename
drhd5704a82016-03-14 13:42:29 +0000420 # -q Reduce output
421 # --testdir=$dir Run tests in subdirectory $dir
dan17c08232015-06-09 15:58:28 +0000422 # --help
danc1a60c52010-06-07 14:28:16 +0000423 #
424 set cmdlinearg(soft-heap-limit) 0
425 set cmdlinearg(maxerror) 1000
426 set cmdlinearg(malloctrace) 0
427 set cmdlinearg(backtrace) 10
428 set cmdlinearg(binarylog) 0
dan0626dfc2010-06-15 06:56:37 +0000429 set cmdlinearg(soak) 0
mistachkinfda06be2011-08-02 00:57:34 +0000430 set cmdlinearg(file-retries) 0
431 set cmdlinearg(file-retry-delay) 0
mistachkin6c3c1a02011-11-12 03:17:40 +0000432 set cmdlinearg(start) ""
433 set cmdlinearg(match) ""
dan17c08232015-06-09 15:58:28 +0000434 set cmdlinearg(verbose) ""
435 set cmdlinearg(output) ""
drhd5704a82016-03-14 13:42:29 +0000436 set cmdlinearg(testdir) "testdir"
danc1a60c52010-06-07 14:28:16 +0000437
438 set leftover [list]
439 foreach a $argv {
440 switch -regexp -- $a {
441 {^-+pause$} {
mistachkin1d406e02013-08-29 01:09:14 +0000442 # Wait for user input before continuing. This is to give the user an
danc1a60c52010-06-07 14:28:16 +0000443 # opportunity to connect profiling tools to the process.
444 puts -nonewline "Press RETURN to begin..."
445 flush stdout
446 gets stdin
447 }
448 {^-+soft-heap-limit=.+$} {
449 foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break
450 }
451 {^-+maxerror=.+$} {
452 foreach {dummy cmdlinearg(maxerror)} [split $a =] break
453 }
454 {^-+malloctrace=.+$} {
455 foreach {dummy cmdlinearg(malloctrace)} [split $a =] break
456 if {$cmdlinearg(malloctrace)} {
457 sqlite3_memdebug_log start
458 }
459 }
460 {^-+backtrace=.+$} {
461 foreach {dummy cmdlinearg(backtrace)} [split $a =] break
dan0626dfc2010-06-15 06:56:37 +0000462 sqlite3_memdebug_backtrace $value
danc1a60c52010-06-07 14:28:16 +0000463 }
danc1a60c52010-06-07 14:28:16 +0000464 {^-+binarylog=.+$} {
465 foreach {dummy cmdlinearg(binarylog)} [split $a =] break
drhe500f652016-03-14 14:59:35 +0000466 set cmdlinearg(binarylog) [file normalize $cmdlinearg(binarylog)]
danc1a60c52010-06-07 14:28:16 +0000467 }
dan0626dfc2010-06-15 06:56:37 +0000468 {^-+soak=.+$} {
469 foreach {dummy cmdlinearg(soak)} [split $a =] break
470 set ::G(issoak) $cmdlinearg(soak)
471 }
mistachkinfda06be2011-08-02 00:57:34 +0000472 {^-+file-retries=.+$} {
473 foreach {dummy cmdlinearg(file-retries)} [split $a =] break
474 set ::G(file-retries) $cmdlinearg(file-retries)
475 }
476 {^-+file-retry-delay=.+$} {
477 foreach {dummy cmdlinearg(file-retry-delay)} [split $a =] break
478 set ::G(file-retry-delay) $cmdlinearg(file-retry-delay)
479 }
dan6a64d672011-04-04 15:38:16 +0000480 {^-+start=.+$} {
481 foreach {dummy cmdlinearg(start)} [split $a =] break
482
483 set ::G(start:file) $cmdlinearg(start)
484 if {[regexp {(.*):(.*)} $cmdlinearg(start) -> s.perm s.file]} {
485 set ::G(start:permutation) ${s.perm}
486 set ::G(start:file) ${s.file}
487 }
488 if {$::G(start:file) == ""} {unset ::G(start:file)}
489 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000490 {^-+match=.+$} {
491 foreach {dummy cmdlinearg(match)} [split $a =] break
492
493 set ::G(match) $cmdlinearg(match)
494 if {$::G(match) == ""} {unset ::G(match)}
495 }
dan17c08232015-06-09 15:58:28 +0000496
497 {^-+output=.+$} {
498 foreach {dummy cmdlinearg(output)} [split $a =] break
drhe500f652016-03-14 14:59:35 +0000499 set cmdlinearg(output) [file normalize $cmdlinearg(output)]
dan17c08232015-06-09 15:58:28 +0000500 if {$cmdlinearg(verbose)==""} {
501 set cmdlinearg(verbose) 2
502 }
503 }
504 {^-+verbose=.+$} {
505 foreach {dummy cmdlinearg(verbose)} [split $a =] break
506 if {$cmdlinearg(verbose)=="file"} {
507 set cmdlinearg(verbose) 2
508 } elseif {[string is boolean -strict $cmdlinearg(verbose)]==0} {
509 error "option --verbose= must be set to a boolean or to \"file\""
510 }
511 }
drhd5704a82016-03-14 13:42:29 +0000512 {^-+testdir=.*$} {
513 foreach {dummy cmdlinearg(testdir)} [split $a =] break
514 }
drh4b7b1c92016-02-15 19:38:17 +0000515 {.*help.*} {
516 print_help_and_quit
517 }
518 {^-q$} {
519 set cmdlinearg(output) test-out.txt
520 set cmdlinearg(verbose) 2
521 }
dan17c08232015-06-09 15:58:28 +0000522
danc1a60c52010-06-07 14:28:16 +0000523 default {
drhd5704a82016-03-14 13:42:29 +0000524 lappend leftover [file normalize $a]
danc1a60c52010-06-07 14:28:16 +0000525 }
526 }
527 }
drhbea14132016-03-14 14:28:43 +0000528 set testdir [file normalize $testdir]
drhd5704a82016-03-14 13:42:29 +0000529 set cmdlinearg(TESTFIXTURE_HOME) [pwd]
drhaf3906a2016-03-14 17:05:04 +0000530 set cmdlinearg(INFO_SCRIPT) [file normalize [info script]]
drhbea14132016-03-14 14:28:43 +0000531 set argv0 [file normalize $argv0]
drhd5704a82016-03-14 13:42:29 +0000532 if {$cmdlinearg(testdir)!=""} {
533 file mkdir $cmdlinearg(testdir)
534 cd $cmdlinearg(testdir)
535 }
danc1a60c52010-06-07 14:28:16 +0000536 set argv $leftover
537
dan430e74c2010-06-07 17:47:26 +0000538 # Install the malloc layer used to inject OOM errors. And the 'automatic'
539 # extensions. This only needs to be done once for the process.
540 #
mistachkin1d406e02013-08-29 01:09:14 +0000541 sqlite3_shutdown
542 install_malloc_faultsim 1
danielk1977f7b9d662008-06-23 18:49:43 +0000543 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000544 autoinstall_test_functions
danc1a60c52010-06-07 14:28:16 +0000545
dan430e74c2010-06-07 17:47:26 +0000546 # If the --binarylog option was specified, create the logging VFS. This
547 # call installs the new VFS as the default for all SQLite connections.
548 #
danc1a60c52010-06-07 14:28:16 +0000549 if {$cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000550 vfslog new binarylog {} vfslog.bin
danc1a60c52010-06-07 14:28:16 +0000551 }
552
553 # Set the backtrace depth, if malloc tracing is enabled.
554 #
555 if {$cmdlinearg(malloctrace)} {
556 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danielk1977185eac92008-07-12 15:55:54 +0000557 }
dan17c08232015-06-09 15:58:28 +0000558
559 if {$cmdlinearg(output)!=""} {
560 puts "Copying output to file $cmdlinearg(output)"
561 set ::G(output_fd) [open $cmdlinearg(output) w]
562 fconfigure $::G(output_fd) -buffering line
563 }
564
565 if {$cmdlinearg(verbose)==""} {
566 set cmdlinearg(verbose) 1
567 }
danielk1977f7b9d662008-06-23 18:49:43 +0000568}
danielk197703ba3fa2009-01-09 10:49:14 +0000569
danc1a60c52010-06-07 14:28:16 +0000570# Update the soft-heap-limit each time this script is run. In that
571# way if an individual test file changes the soft-heap-limit, it
572# will be reset at the start of the next test file.
573#
574sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
575
576# Create a test database
577#
danielk197703ba3fa2009-01-09 10:49:14 +0000578proc reset_db {} {
579 catch {db close}
mistachkinfda06be2011-08-02 00:57:34 +0000580 forcedelete test.db
581 forcedelete test.db-journal
582 forcedelete test.db-wal
danielk197703ba3fa2009-01-09 10:49:14 +0000583 sqlite3 db ./test.db
584 set ::DB [sqlite3_connection_pointer db]
585 if {[info exists ::SETUP_SQL]} {
586 db eval $::SETUP_SQL
587 }
drhcd61c282002-03-06 22:01:34 +0000588}
danielk197703ba3fa2009-01-09 10:49:14 +0000589reset_db
drhbec2bf42000-05-29 23:48:22 +0000590
591# Abort early if this script has been run before.
592#
danc1a60c52010-06-07 14:28:16 +0000593if {[info exists TC(count)]} return
drhbec2bf42000-05-29 23:48:22 +0000594
drhce2198c2010-09-10 13:22:59 +0000595# Make sure memory statistics are enabled.
596#
597sqlite3_config_memstatus 1
598
danc1a60c52010-06-07 14:28:16 +0000599# Initialize the test counters and set up commands to access them.
600# Or, if this is a slave interpreter, set up aliases to write the
601# counters in the parent interpreter.
drhbec2bf42000-05-29 23:48:22 +0000602#
danc1a60c52010-06-07 14:28:16 +0000603if {0==[info exists ::SLAVE]} {
604 set TC(errors) 0
605 set TC(count) 0
606 set TC(fail_list) [list]
607 set TC(omit_list) [list]
drhcca17c32013-04-19 12:32:52 +0000608 set TC(warn_list) [list]
danc1a60c52010-06-07 14:28:16 +0000609
610 proc set_test_counter {counter args} {
611 if {[llength $args]} {
612 set ::TC($counter) [lindex $args 0]
613 }
614 set ::TC($counter)
615 }
drhb62c3352006-11-23 09:39:16 +0000616}
drh348784e2000-05-29 20:41:49 +0000617
drh521cc842008-04-15 02:36:33 +0000618# Record the fact that a sequence of tests were omitted.
619#
mistachkin6c3c1a02011-11-12 03:17:40 +0000620proc omit_test {name reason {append 1}} {
danc1a60c52010-06-07 14:28:16 +0000621 set omitList [set_test_counter omit_list]
mistachkin6c3c1a02011-11-12 03:17:40 +0000622 if {$append} {
623 lappend omitList [list $name $reason]
624 }
danc1a60c52010-06-07 14:28:16 +0000625 set_test_counter omit_list $omitList
drh521cc842008-04-15 02:36:33 +0000626}
627
danc1a60c52010-06-07 14:28:16 +0000628# Record the fact that a test failed.
629#
630proc fail_test {name} {
631 set f [set_test_counter fail_list]
632 lappend f $name
633 set_test_counter fail_list $f
634 set_test_counter errors [expr [set_test_counter errors] + 1]
635
636 set nFail [set_test_counter errors]
637 if {$nFail>=$::cmdlinearg(maxerror)} {
dan17c08232015-06-09 15:58:28 +0000638 output2 "*** Giving up..."
danc1a60c52010-06-07 14:28:16 +0000639 finalize_testing
640 }
641}
642
drhcca17c32013-04-19 12:32:52 +0000643# Remember a warning message to be displayed at the conclusion of all testing
644#
645proc warning {msg {append 1}} {
dan17c08232015-06-09 15:58:28 +0000646 output2 "Warning: $msg"
drhcca17c32013-04-19 12:32:52 +0000647 set warnList [set_test_counter warn_list]
648 if {$append} {
649 lappend warnList $msg
650 }
651 set_test_counter warn_list $warnList
652}
653
654
danc1a60c52010-06-07 14:28:16 +0000655# Increment the number of tests run
656#
657proc incr_ntest {} {
658 set_test_counter count [expr [set_test_counter count] + 1]
659}
660
dan17c08232015-06-09 15:58:28 +0000661# Return true if --verbose=1 was specified on the command line. Otherwise,
662# return false.
663#
664proc verbose {} {
665 return $::cmdlinearg(verbose)
666}
667
668# Use the following commands instead of [puts] for test output within
669# this file. Test scripts can still use regular [puts], which is directed
670# to stdout and, if one is open, the --output file.
671#
672# output1: output that should be printed if --verbose=1 was specified.
673# output2: output that should be printed unconditionally.
674# output2_if_no_verbose: output that should be printed only if --verbose=0.
675#
676proc output1 {args} {
677 set v [verbose]
678 if {$v==1} {
679 uplevel output2 $args
680 } elseif {$v==2} {
681 uplevel puts [lrange $args 0 end-1] $::G(output_fd) [lrange $args end end]
682 }
683}
684proc output2 {args} {
685 set nArg [llength $args]
686 uplevel puts $args
687}
688proc output2_if_no_verbose {args} {
689 set v [verbose]
690 if {$v==0} {
691 uplevel output2 $args
692 } elseif {$v==2} {
693 uplevel puts [lrange $args 0 end-1] stdout [lrange $args end end]
694 }
695}
696
697# Override the [puts] command so that if no channel is explicitly
698# specified the string is written to both stdout and to the file
699# specified by "--output=", if any.
700#
701proc puts_override {args} {
702 set nArg [llength $args]
703 if {$nArg==1 || ($nArg==2 && [string first [lindex $args 0] -nonewline]==0)} {
704 uplevel puts_original $args
705 if {[info exists ::G(output_fd)]} {
706 uplevel puts [lrange $args 0 end-1] $::G(output_fd) [lrange $args end end]
707 }
708 } else {
709 # A channel was explicitly specified.
710 uplevel puts_original $args
711 }
712}
713rename puts puts_original
714proc puts {args} { uplevel puts_override $args }
715
danc1a60c52010-06-07 14:28:16 +0000716
mistachkin1d406e02013-08-29 01:09:14 +0000717# Invoke the do_test procedure to run a single test
drh348784e2000-05-29 20:41:49 +0000718#
719proc do_test {name cmd expected} {
danc1a60c52010-06-07 14:28:16 +0000720 global argv cmdlinearg
721
dan8c408002010-11-01 17:38:24 +0000722 fix_testname name
723
drh4a50aac2007-08-23 02:47:53 +0000724 sqlite3_memdebug_settitle $name
danc1a60c52010-06-07 14:28:16 +0000725
mistachkin1d406e02013-08-29 01:09:14 +0000726# if {[llength $argv]==0} {
dan92d516a2010-07-05 14:54:48 +0000727# set go 1
728# } else {
729# set go 0
730# foreach pattern $argv {
731# if {[string match $pattern $name]} {
732# set go 1
733# break
734# }
735# }
736# }
dan430e74c2010-06-07 17:47:26 +0000737
dand506de02010-07-03 13:59:01 +0000738 if {[info exists ::G(perm:prefix)]} {
739 set name "$::G(perm:prefix)$name"
dan430e74c2010-06-07 17:47:26 +0000740 }
741
danc1a60c52010-06-07 14:28:16 +0000742 incr_ntest
dan17c08232015-06-09 15:58:28 +0000743 output1 -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000744 flush stdout
mistachkin6c3c1a02011-11-12 03:17:40 +0000745
746 if {![info exists ::G(match)] || [string match $::G(match) $name]} {
747 if {[catch {uplevel #0 "$cmd;\n"} result]} {
dan17c08232015-06-09 15:58:28 +0000748 output2_if_no_verbose -nonewline $name...
749 output2 "\nError: $result"
mistachkin6c3c1a02011-11-12 03:17:40 +0000750 fail_test $name
mistachkin6c3c1a02011-11-12 03:17:40 +0000751 } else {
drh3f17aef2012-04-27 01:08:02 +0000752 if {[regexp {^~?/.*/$} $expected]} {
drh70bdcc72013-05-30 19:28:34 +0000753 # "expected" is of the form "/PATTERN/" then the result if correct if
754 # regular expression PATTERN matches the result. "~/PATTERN/" means
755 # the regular expression must not match.
drh3f17aef2012-04-27 01:08:02 +0000756 if {[string index $expected 0]=="~"} {
drhc2b23e72013-11-13 15:32:15 +0000757 set re [string range $expected 2 end-1]
758 if {[string index $re 0]=="*"} {
759 # If the regular expression begins with * then treat it as a glob instead
760 set ok [string match $re $result]
761 } else {
762 set re [string map {# {[-0-9.]+}} $re]
763 set ok [regexp $re $result]
764 }
765 set ok [expr {!$ok}]
drh3f17aef2012-04-27 01:08:02 +0000766 } else {
drhc2b23e72013-11-13 15:32:15 +0000767 set re [string range $expected 1 end-1]
768 if {[string index $re 0]=="*"} {
769 # If the regular expression begins with * then treat it as a glob instead
770 set ok [string match $re $result]
771 } else {
772 set re [string map {# {[-0-9.]+}} $re]
773 set ok [regexp $re $result]
774 }
drh3f17aef2012-04-27 01:08:02 +0000775 }
drh70bdcc72013-05-30 19:28:34 +0000776 } elseif {[regexp {^~?\*.*\*$} $expected]} {
777 # "expected" is of the form "*GLOB*" then the result if correct if
778 # glob pattern GLOB matches the result. "~/GLOB/" means
779 # the glob must not match.
780 if {[string index $expected 0]=="~"} {
781 set e [string range $expected 1 end]
782 set ok [expr {![string match $e $result]}]
783 } else {
784 set ok [string match $expected $result]
785 }
drh3f17aef2012-04-27 01:08:02 +0000786 } else {
787 set ok [expr {[string compare $result $expected]==0}]
788 }
789 if {!$ok} {
mistachkinc60941f2012-09-13 01:51:02 +0000790 # if {![info exists ::testprefix] || $::testprefix eq ""} {
791 # error "no test prefix"
792 # }
drhd66b2e02015-11-12 21:42:40 +0000793 output1 ""
drh061d35c2015-11-13 00:03:14 +0000794 output2 "! $name expected: \[$expected\]\n! $name got: \[$result\]"
drh3f17aef2012-04-27 01:08:02 +0000795 fail_test $name
796 } else {
dan17c08232015-06-09 15:58:28 +0000797 output1 " Ok"
drh3f17aef2012-04-27 01:08:02 +0000798 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000799 }
drh348784e2000-05-29 20:41:49 +0000800 } else {
dan17c08232015-06-09 15:58:28 +0000801 output1 " Omitted"
mistachkin6c3c1a02011-11-12 03:17:40 +0000802 omit_test $name "pattern mismatch" 0
drh348784e2000-05-29 20:41:49 +0000803 }
drh6558db82007-04-13 03:23:21 +0000804 flush stdout
drh348784e2000-05-29 20:41:49 +0000805}
dana3f51082010-09-30 18:43:14 +0000806
mistachkinf21979d2015-01-18 05:35:01 +0000807proc dumpbytes {s} {
808 set r ""
809 for {set i 0} {$i < [string length $s]} {incr i} {
810 if {$i > 0} {append r " "}
811 append r [format %02X [scan [string index $s $i] %c]]
812 }
813 return $r
814}
815
drh8df91852012-04-24 12:46:05 +0000816proc catchcmd {db {cmd ""}} {
817 global CLI
818 set out [open cmds.txt w]
819 puts $out $cmd
820 close $out
821 set line "exec $CLI $db < cmds.txt"
822 set rc [catch { eval $line } msg]
823 list $rc $msg
824}
825
mistachkinf21979d2015-01-18 05:35:01 +0000826proc catchcmdex {db {cmd ""}} {
827 global CLI
828 set out [open cmds.txt w]
829 fconfigure $out -encoding binary -translation binary
830 puts -nonewline $out $cmd
831 close $out
832 set line "exec -keepnewline -- $CLI $db < cmds.txt"
833 set chans [list stdin stdout stderr]
834 foreach chan $chans {
835 catch {
836 set modes($chan) [fconfigure $chan]
837 fconfigure $chan -encoding binary -translation binary -buffering none
838 }
839 }
840 set rc [catch { eval $line } msg]
841 foreach chan $chans {
842 catch {
843 eval fconfigure [list $chan] $modes($chan)
844 }
845 }
846 # puts [dumpbytes $msg]
847 list $rc $msg
848}
849
shaneh55505172011-06-21 19:30:19 +0000850proc filepath_normalize {p} {
851 # test cases should be written to assume "unix"-like file paths
shaneh285a18f2011-06-21 19:39:59 +0000852 if {$::tcl_platform(platform)!="unix"} {
shaneh55505172011-06-21 19:30:19 +0000853 # lreverse*2 as a hack to remove any unneeded {} after the string map
854 lreverse [lreverse [string map {\\ /} [regsub -nocase -all {[a-z]:[/\\]+} $p {/}]]]
shanehc4896402011-06-21 19:38:16 +0000855 } {
856 set p
shaneh55505172011-06-21 19:30:19 +0000857 }
858}
859proc do_filepath_test {name cmd expected} {
860 uplevel [list do_test $name [
861 subst -nocommands { filepath_normalize [ $cmd ] }
862 ] [filepath_normalize $expected]]
863}
864
dan33f53792011-05-05 19:44:22 +0000865proc realnum_normalize {r} {
shaneh4f529e82011-06-20 17:41:41 +0000866 # different TCL versions display floating point values differently.
867 string map {1.#INF inf Inf inf .0e e} [regsub -all {(e[+-])0+} $r {\1}]
dan33f53792011-05-05 19:44:22 +0000868}
869proc do_realnum_test {name cmd expected} {
870 uplevel [list do_test $name [
871 subst -nocommands { realnum_normalize [ $cmd ] }
872 ] [realnum_normalize $expected]]
873}
874
dana3f51082010-09-30 18:43:14 +0000875proc fix_testname {varname} {
876 upvar $varname testname
mistachkin1d406e02013-08-29 01:09:14 +0000877 if {[info exists ::testprefix]
dana3f51082010-09-30 18:43:14 +0000878 && [string is digit [string range $testname 0 0]]
879 } {
880 set testname "${::testprefix}-$testname"
881 }
882}
dan67340072011-04-16 19:23:10 +0000883
884proc normalize_list {L} {
885 set L2 [list]
886 foreach l $L {lappend L2 $l}
887 set L2
888}
drh5f9742e2013-08-29 15:08:38 +0000889
dan57f7f4b2010-10-01 19:04:37 +0000890proc do_execsql_test {testname sql {result {}}} {
dana3f51082010-09-30 18:43:14 +0000891 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000892 uplevel do_test [list $testname] [list "execsql {$sql}"] [list [list {*}$result]]
dan153eda02010-06-21 07:45:47 +0000893}
894proc do_catchsql_test {testname sql result} {
dana3f51082010-09-30 18:43:14 +0000895 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000896 uplevel do_test [list $testname] [list "catchsql {$sql}"] [list $result]
dan153eda02010-06-21 07:45:47 +0000897}
drh3c2aeae2014-01-24 14:37:44 +0000898proc do_timed_execsql_test {testname sql {result {}}} {
899 fix_testname testname
900 uplevel do_test [list $testname] [list "execsql_timed {$sql}"]\
901 [list [list {*}$result]]
902}
dan39854792010-11-15 16:12:58 +0000903proc do_eqp_test {name sql res} {
904 uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res]
905}
dan153eda02010-06-21 07:45:47 +0000906
dan51f06982010-09-18 19:00:12 +0000907#-------------------------------------------------------------------------
908# Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST
909#
910# Where switches are:
911#
912# -errorformat FMTSTRING
913# -count
danaf1dcab2010-09-20 19:17:53 +0000914# -query SQL
dana16d1062010-09-28 17:37:28 +0000915# -tclquery TCL
danaf7626f2010-09-23 18:47:36 +0000916# -repair TCL
dan51f06982010-09-18 19:00:12 +0000917#
918proc do_select_tests {prefix args} {
919
920 set testlist [lindex $args end]
921 set switches [lrange $args 0 end-1]
922
923 set errfmt ""
924 set countonly 0
dana16d1062010-09-28 17:37:28 +0000925 set tclquery ""
danaf7626f2010-09-23 18:47:36 +0000926 set repair ""
dan51f06982010-09-18 19:00:12 +0000927
928 for {set i 0} {$i < [llength $switches]} {incr i} {
929 set s [lindex $switches $i]
930 set n [string length $s]
danaf1dcab2010-09-20 19:17:53 +0000931 if {$n>=2 && [string equal -length $n $s "-query"]} {
dana16d1062010-09-28 17:37:28 +0000932 set tclquery [list execsql [lindex $switches [incr i]]]
933 } elseif {$n>=2 && [string equal -length $n $s "-tclquery"]} {
934 set tclquery [lindex $switches [incr i]]
danaf1dcab2010-09-20 19:17:53 +0000935 } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} {
dan51f06982010-09-18 19:00:12 +0000936 set errfmt [lindex $switches [incr i]]
danaf7626f2010-09-23 18:47:36 +0000937 } elseif {$n>=2 && [string equal -length $n $s "-repair"]} {
938 set repair [lindex $switches [incr i]]
dan51f06982010-09-18 19:00:12 +0000939 } elseif {$n>=2 && [string equal -length $n $s "-count"]} {
940 set countonly 1
941 } else {
942 error "unknown switch: $s"
943 }
944 }
945
946 if {$countonly && $errfmt!=""} {
947 error "Cannot use -count and -errorformat together"
948 }
949 set nTestlist [llength $testlist]
950 if {$nTestlist%3 || $nTestlist==0 } {
951 error "SELECT test list contains [llength $testlist] elements"
952 }
953
danaf7626f2010-09-23 18:47:36 +0000954 eval $repair
dan51f06982010-09-18 19:00:12 +0000955 foreach {tn sql res} $testlist {
dana16d1062010-09-28 17:37:28 +0000956 if {$tclquery != ""} {
danaf1dcab2010-09-20 19:17:53 +0000957 execsql $sql
dana16d1062010-09-28 17:37:28 +0000958 uplevel do_test ${prefix}.$tn [list $tclquery] [list [list {*}$res]]
959 } elseif {$countonly} {
dan51f06982010-09-18 19:00:12 +0000960 set nRow 0
961 db eval $sql {incr nRow}
962 uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res]
963 } elseif {$errfmt==""} {
964 uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]]
965 } else {
966 set res [list 1 [string trim [format $errfmt {*}$res]]]
967 uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res]
968 }
danaf7626f2010-09-23 18:47:36 +0000969 eval $repair
dan51f06982010-09-18 19:00:12 +0000970 }
danaf7626f2010-09-23 18:47:36 +0000971
dan51f06982010-09-18 19:00:12 +0000972}
973
danb04757a2010-09-21 16:59:16 +0000974proc delete_all_data {} {
975 db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} {
976 db eval "DELETE FROM '[string map {' ''} $t]'"
977 }
978}
drh348784e2000-05-29 20:41:49 +0000979
mistachkin1d406e02013-08-29 01:09:14 +0000980# Run an SQL script.
drhb62c3352006-11-23 09:39:16 +0000981# Return the number of microseconds per statement.
982#
drh3590f152006-11-23 21:09:10 +0000983proc speed_trial {name numstmt units sql} {
dan17c08232015-06-09 15:58:28 +0000984 output2 -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000985 flush stdout
986 set speed [time {sqlite3_exec_nr db $sql}]
987 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000988 if {$tm == 0} {
989 set rate [format %20s "many"]
990 } else {
991 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
992 }
drh3590f152006-11-23 21:09:10 +0000993 set u2 $units/s
dan17c08232015-06-09 15:58:28 +0000994 output2 [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000995 global total_time
996 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000997 lappend ::speed_trial_times $name $tm
drhdd924312007-03-31 22:34:16 +0000998}
drh45c236d2008-03-22 01:08:00 +0000999proc speed_trial_tcl {name numstmt units script} {
dan17c08232015-06-09 15:58:28 +00001000 output2 -nonewline [format {%-21.21s } $name...]
drh45c236d2008-03-22 01:08:00 +00001001 flush stdout
1002 set speed [time {eval $script}]
1003 set tm [lindex $speed 0]
1004 if {$tm == 0} {
1005 set rate [format %20s "many"]
1006 } else {
1007 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
1008 }
1009 set u2 $units/s
dan17c08232015-06-09 15:58:28 +00001010 output2 [format {%12d uS %s %s} $tm $rate $u2]
drh45c236d2008-03-22 01:08:00 +00001011 global total_time
1012 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +00001013 lappend ::speed_trial_times $name $tm
drh45c236d2008-03-22 01:08:00 +00001014}
drhdd924312007-03-31 22:34:16 +00001015proc speed_trial_init {name} {
1016 global total_time
1017 set total_time 0
dana975b592010-10-08 16:09:43 +00001018 set ::speed_trial_times [list]
drhbb810a92010-07-03 12:00:53 +00001019 sqlite3 versdb :memory:
1020 set vers [versdb one {SELECT sqlite_source_id()}]
1021 versdb close
dan17c08232015-06-09 15:58:28 +00001022 output2 "SQLite $vers"
drhdd924312007-03-31 22:34:16 +00001023}
1024proc speed_trial_summary {name} {
1025 global total_time
dan17c08232015-06-09 15:58:28 +00001026 output2 [format {%-21.21s %12d uS TOTAL} $name $total_time]
dana975b592010-10-08 16:09:43 +00001027
1028 if { 0 } {
1029 sqlite3 versdb :memory:
1030 set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0]
1031 versdb close
dan17c08232015-06-09 15:58:28 +00001032 output2 "CREATE TABLE IF NOT EXISTS time(version, script, test, us);"
dana975b592010-10-08 16:09:43 +00001033 foreach {test us} $::speed_trial_times {
dan17c08232015-06-09 15:58:28 +00001034 output2 "INSERT INTO time VALUES('$vers', '$name', '$test', $us);"
dana975b592010-10-08 16:09:43 +00001035 }
1036 }
drhb62c3352006-11-23 09:39:16 +00001037}
1038
drh348784e2000-05-29 20:41:49 +00001039# Run this routine last
1040#
1041proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +00001042 catch {db close}
dane8559832014-07-31 17:35:40 +00001043 catch {db1 close}
danc1a60c52010-06-07 14:28:16 +00001044 catch {db2 close}
1045 catch {db3 close}
1046 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +00001047}
1048proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +00001049 global sqlite_open_file_count
1050
1051 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +00001052
drh6e142f52000-06-08 13:36:40 +00001053 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +00001054 catch {db2 close}
1055 catch {db3 close}
1056
drh9bc54492007-10-23 14:49:59 +00001057 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +00001058 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +00001059 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +00001060 db close
drh984bfaa2008-03-19 16:08:53 +00001061 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +00001062
drh3a7fb7c2007-08-10 16:41:08 +00001063 sqlite3_soft_heap_limit 0
danc1a60c52010-06-07 14:28:16 +00001064 set nTest [incr_ntest]
1065 set nErr [set_test_counter errors]
1066
drhef866372013-05-22 20:49:02 +00001067 set nKnown 0
1068 if {[file readable known-problems.txt]} {
1069 set fd [open known-problems.txt]
1070 set content [read $fd]
1071 close $fd
1072 foreach x $content {set known_error($x) 1}
1073 foreach x [set_test_counter fail_list] {
1074 if {[info exists known_error($x)]} {incr nKnown}
1075 }
1076 }
1077 if {$nKnown>0} {
dan17c08232015-06-09 15:58:28 +00001078 output2 "[expr {$nErr-$nKnown}] new errors and $nKnown known errors\
drhef866372013-05-22 20:49:02 +00001079 out of $nTest tests"
1080 } else {
drh72bf6a32016-01-07 02:06:55 +00001081 set cpuinfo {}
1082 if {[catch {exec hostname} hname]==0} {set cpuinfo [string trim $hname]}
1083 append cpuinfo " $::tcl_platform(os)"
1084 append cpuinfo " [expr {$::tcl_platform(pointerSize)*8}]-bit"
1085 append cpuinfo " [string map {E -e} $::tcl_platform(byteOrder)]"
1086 output2 "SQLite [sqlite3 -sourceid]"
1087 output2 "$nErr errors out of $nTest tests on $cpuinfo"
drhef866372013-05-22 20:49:02 +00001088 }
1089 if {$nErr>$nKnown} {
drh061d35c2015-11-13 00:03:14 +00001090 output2 -nonewline "!Failures on these tests:"
drhef866372013-05-22 20:49:02 +00001091 foreach x [set_test_counter fail_list] {
dan17c08232015-06-09 15:58:28 +00001092 if {![info exists known_error($x)]} {output2 -nonewline " $x"}
drhef866372013-05-22 20:49:02 +00001093 }
dan17c08232015-06-09 15:58:28 +00001094 output2 ""
drhf3a65f72007-08-22 20:18:21 +00001095 }
drhcca17c32013-04-19 12:32:52 +00001096 foreach warning [set_test_counter warn_list] {
dan17c08232015-06-09 15:58:28 +00001097 output2 "Warning: $warning"
drhcca17c32013-04-19 12:32:52 +00001098 }
danielk1977ee8b7992009-03-26 17:13:06 +00001099 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +00001100 if {[llength $omitList]>0} {
dan17c08232015-06-09 15:58:28 +00001101 output2 "Omitted test cases:"
drh521cc842008-04-15 02:36:33 +00001102 set prec {}
1103 foreach {rec} [lsort $omitList] {
1104 if {$rec==$prec} continue
1105 set prec $rec
drhd66b2e02015-11-12 21:42:40 +00001106 output2 [format {. %-12s %s} [lindex $rec 0] [lindex $rec 1]]
drh521cc842008-04-15 02:36:33 +00001107 }
1108 }
drh80788d82006-09-02 14:50:23 +00001109 if {$nErr>0 && ![working_64bit_int]} {
dan17c08232015-06-09 15:58:28 +00001110 output2 "******************************************************************"
1111 output2 "N.B.: The version of TCL that you used to build this test harness"
1112 output2 "is defective in that it does not support 64-bit integers. Some or"
1113 output2 "all of the test failures above might be a result from this defect"
1114 output2 "in your TCL build."
1115 output2 "******************************************************************"
drhdb25e382001-03-15 18:21:22 +00001116 }
danc1a60c52010-06-07 14:28:16 +00001117 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +00001118 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +00001119 }
drh94e92032003-02-16 22:21:32 +00001120 if {$sqlite_open_file_count} {
dan17c08232015-06-09 15:58:28 +00001121 output2 "$sqlite_open_file_count files were left open"
drh94e92032003-02-16 22:21:32 +00001122 incr nErr
1123 }
drheafc43b2010-07-26 18:43:40 +00001124 if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 ||
1125 [sqlite3_memory_used]>0} {
dan17c08232015-06-09 15:58:28 +00001126 output2 "Unfreed memory: [sqlite3_memory_used] bytes in\
drheafc43b2010-07-26 18:43:40 +00001127 [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations"
drhf3a65f72007-08-22 20:18:21 +00001128 incr nErr
drh2d7636e2008-02-16 16:21:45 +00001129 ifcapable memdebug||mem5||(mem3&&debug) {
dan17c08232015-06-09 15:58:28 +00001130 output2 "Writing unfreed memory log to \"./memleak.txt\""
drh4a50aac2007-08-23 02:47:53 +00001131 sqlite3_memdebug_dump ./memleak.txt
1132 }
drhf3a65f72007-08-22 20:18:21 +00001133 } else {
dan17c08232015-06-09 15:58:28 +00001134 output2 "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +00001135 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +00001136 sqlite3_memdebug_dump ./memusage.txt
1137 }
drhf3a65f72007-08-22 20:18:21 +00001138 }
drhf7141992008-06-19 00:16:08 +00001139 show_memstats
dan17c08232015-06-09 15:58:28 +00001140 output2 "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
1141 output2 "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +00001142 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
dan17c08232015-06-09 15:58:28 +00001143 output2 "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
danielk1977a7a8e142008-02-13 18:25:27 +00001144 }
danc1a60c52010-06-07 14:28:16 +00001145 if {$::cmdlinearg(malloctrace)} {
dan17c08232015-06-09 15:58:28 +00001146 output2 "Writing mallocs.sql..."
danielk197735754ac2008-03-21 17:29:37 +00001147 memdebug_log_sql
1148 sqlite3_memdebug_log stop
1149 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +00001150
1151 if {[sqlite3_memory_used]>0} {
dan17c08232015-06-09 15:58:28 +00001152 output2 "Writing leaks.sql..."
danielk1977dbdc4d42008-03-28 07:42:53 +00001153 sqlite3_memdebug_log sync
1154 memdebug_log_sql leaks.sql
1155 }
danielk197735754ac2008-03-21 17:29:37 +00001156 }
drhd9910fe2006-10-04 11:55:49 +00001157 foreach f [glob -nocomplain test.db-*-journal] {
mistachkinfda06be2011-08-02 00:57:34 +00001158 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +00001159 }
1160 foreach f [glob -nocomplain test.db-mj*] {
mistachkinfda06be2011-08-02 00:57:34 +00001161 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +00001162 }
drhdb25e382001-03-15 18:21:22 +00001163 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +00001164}
1165
drhf7141992008-06-19 00:16:08 +00001166# Display memory statistics for analysis and debugging purposes.
1167#
1168proc show_memstats {} {
1169 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +00001170 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
1171 set val [format {now %10d max %10d max-size %10d} \
1172 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
dan17c08232015-06-09 15:58:28 +00001173 output1 "Memory used: $val"
drheafc43b2010-07-26 18:43:40 +00001174 set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0]
1175 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
dan17c08232015-06-09 15:58:28 +00001176 output1 "Allocation count: $val"
drhf7141992008-06-19 00:16:08 +00001177 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +00001178 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
1179 set val [format {now %10d max %10d max-size %10d} \
1180 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
dan17c08232015-06-09 15:58:28 +00001181 output1 "Page-cache used: $val"
drhf7141992008-06-19 00:16:08 +00001182 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
1183 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
dan17c08232015-06-09 15:58:28 +00001184 output1 "Page-cache overflow: $val"
drhf7141992008-06-19 00:16:08 +00001185 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
1186 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
dan17c08232015-06-09 15:58:28 +00001187 output1 "Scratch memory used: $val"
drhf7141992008-06-19 00:16:08 +00001188 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +00001189 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
1190 set val [format {now %10d max %10d max-size %10d} \
1191 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
dan17c08232015-06-09 15:58:28 +00001192 output1 "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +00001193 ifcapable yytrackmaxstackdepth {
1194 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
1195 set val [format { max %10d} [lindex $x 2]]
dan17c08232015-06-09 15:58:28 +00001196 output2 "Parser stack depth: $val"
drhec424a52008-07-25 15:39:03 +00001197 }
drhf7141992008-06-19 00:16:08 +00001198}
1199
drh348784e2000-05-29 20:41:49 +00001200# A procedure to execute SQL
1201#
drhc4a3c772001-04-04 11:48:57 +00001202proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +00001203 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +00001204 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +00001205}
drh3c2aeae2014-01-24 14:37:44 +00001206proc execsql_timed {sql {db db}} {
1207 set tm [time {
1208 set x [uplevel [list $db eval $sql]]
1209 } 1]
1210 set tm [lindex $tm 0]
dan17c08232015-06-09 15:58:28 +00001211 output1 -nonewline " ([expr {$tm*0.001}]ms) "
drh3c2aeae2014-01-24 14:37:44 +00001212 set x
1213}
drh3aadb2e2000-05-31 17:59:25 +00001214
drhadbca9c2001-09-27 15:11:53 +00001215# Execute SQL and catch exceptions.
1216#
1217proc catchsql {sql {db db}} {
1218 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +00001219 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +00001220 lappend r $msg
1221 return $r
1222}
1223
drh04096482001-11-09 22:41:44 +00001224# Do an VDBE code dump on the SQL given
1225#
1226proc explain {sql {db db}} {
mistachkineeb31ff2015-06-10 23:02:38 +00001227 output2 ""
1228 output2 "addr opcode p1 p2 p3 p4 p5 #"
1229 output2 "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +00001230 $db eval "explain $sql" {} {
mistachkineeb31ff2015-06-10 23:02:38 +00001231 output2 [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
danielk1977287fb612008-01-04 19:10:28 +00001232 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
1233 ]
drh04096482001-11-09 22:41:44 +00001234 }
1235}
1236
dane83267d2013-11-05 14:19:22 +00001237proc explain_i {sql {db db}} {
mistachkineeb31ff2015-06-10 23:02:38 +00001238 output2 ""
1239 output2 "addr opcode p1 p2 p3 p4 p5 #"
1240 output2 "---- ------------ ------ ------ ------ ---------------- -- -"
dane83267d2013-11-05 14:19:22 +00001241
dane83267d2013-11-05 14:19:22 +00001242
dan5c663c32013-11-06 14:52:40 +00001243 # Set up colors for the different opcodes. Scheme is as follows:
1244 #
1245 # Red: Opcodes that write to a b-tree.
1246 # Blue: Opcodes that reposition or seek a cursor.
1247 # Green: The ResultRow opcode.
1248 #
dandea63f22014-03-03 16:48:47 +00001249 if { [catch {fconfigure stdout -mode}]==0 } {
1250 set R "\033\[31;1m" ;# Red fg
1251 set G "\033\[32;1m" ;# Green fg
1252 set B "\033\[34;1m" ;# Red fg
1253 set D "\033\[39;0m" ;# Default fg
1254 } else {
1255 set R ""
1256 set G ""
1257 set B ""
1258 set D ""
1259 }
dan5c663c32013-11-06 14:52:40 +00001260 foreach opcode {
1261 Seek SeekGe SeekGt SeekLe SeekLt NotFound Last Rewind
dane3ab7292013-11-12 12:30:09 +00001262 NoConflict Next Prev VNext VPrev VFilter
drhf45f2322014-03-23 17:45:03 +00001263 SorterSort SorterNext
dan5c663c32013-11-06 14:52:40 +00001264 } {
1265 set color($opcode) $B
1266 }
1267 foreach opcode {ResultRow} {
1268 set color($opcode) $G
1269 }
1270 foreach opcode {IdxInsert Insert Delete IdxDelete} {
1271 set color($opcode) $R
1272 }
1273
dan427ebba2013-11-05 16:39:31 +00001274 set bSeenGoto 0
dane83267d2013-11-05 14:19:22 +00001275 $db eval "explain $sql" {} {
1276 set x($addr) 0
1277 set op($addr) $opcode
1278
dan427ebba2013-11-05 16:39:31 +00001279 if {$opcode == "Goto" && ($bSeenGoto==0 || ($p2 > $addr+10))} {
1280 set linebreak($p2) 1
1281 set bSeenGoto 1
dane83267d2013-11-05 14:19:22 +00001282 }
1283
dane3ab7292013-11-12 12:30:09 +00001284 if {$opcode=="Next" || $opcode=="Prev"
1285 || $opcode=="VNext" || $opcode=="VPrev"
drhf45f2322014-03-23 17:45:03 +00001286 || $opcode=="SorterNext"
dane3ab7292013-11-12 12:30:09 +00001287 } {
dane83267d2013-11-05 14:19:22 +00001288 for {set i $p2} {$i<$addr} {incr i} {
1289 incr x($i) 2
1290 }
1291 }
1292
1293 if {$opcode == "Goto" && $p2<$addr && $op($p2)=="Yield"} {
1294 for {set i [expr $p2+1]} {$i<$addr} {incr i} {
1295 incr x($i) 2
1296 }
1297 }
dan427ebba2013-11-05 16:39:31 +00001298
1299 if {$opcode == "Halt" && $comment == "End of coroutine"} {
1300 set linebreak([expr $addr+1]) 1
1301 }
dane83267d2013-11-05 14:19:22 +00001302 }
1303
1304 $db eval "explain $sql" {} {
dan427ebba2013-11-05 16:39:31 +00001305 if {[info exists linebreak($addr)]} {
mistachkineeb31ff2015-06-10 23:02:38 +00001306 output2 ""
dane83267d2013-11-05 14:19:22 +00001307 }
1308 set I [string repeat " " $x($addr)]
dan5c663c32013-11-06 14:52:40 +00001309
1310 set col ""
1311 catch { set col $color($opcode) }
1312
mistachkineeb31ff2015-06-10 23:02:38 +00001313 output2 [format {%-4d %s%s%-12.12s%s %-6d %-6d %-6d % -17s %s %s} \
dan5c663c32013-11-06 14:52:40 +00001314 $addr $I $col $opcode $D $p1 $p2 $p3 $p4 $p5 $comment
dane83267d2013-11-05 14:19:22 +00001315 ]
dane83267d2013-11-05 14:19:22 +00001316 }
mistachkineeb31ff2015-06-10 23:02:38 +00001317 output2 "---- ------------ ------ ------ ------ ---------------- -- -"
dane83267d2013-11-05 14:19:22 +00001318}
1319
drhdafc0ce2008-04-17 19:14:02 +00001320# Show the VDBE program for an SQL statement but omit the Trace
1321# opcode at the beginning. This procedure can be used to prove
1322# that different SQL statements generate exactly the same VDBE code.
1323#
1324proc explain_no_trace {sql} {
1325 set tr [db eval "EXPLAIN $sql"]
1326 return [lrange $tr 7 end]
1327}
1328
drh3aadb2e2000-05-31 17:59:25 +00001329# Another procedure to execute SQL. This one includes the field
1330# names in the returned list.
1331#
1332proc execsql2 {sql} {
1333 set result {}
1334 db eval $sql data {
1335 foreach f $data(*) {
1336 lappend result $f $data($f)
1337 }
1338 }
1339 return $result
1340}
drh17a68932001-01-31 13:28:08 +00001341
mistachkin1d406e02013-08-29 01:09:14 +00001342# Use a temporary in-memory database to execute SQL statements
1343#
1344proc memdbsql {sql} {
1345 sqlite3 memdb :memory:
1346 set result [memdb eval $sql]
1347 memdb close
1348 return $result
1349}
1350
drhdde85d92003-03-01 19:45:34 +00001351# Use the non-callback API to execute multiple SQL statements
1352#
1353proc stepsql {dbptr sql} {
1354 set sql [string trim $sql]
1355 set r 0
1356 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +00001357 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +00001358 return [list 1 $vm]
1359 }
1360 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +00001361# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
1362# foreach v $VAL {lappend r $v}
1363# }
1364 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
1365 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
1366 lappend r [sqlite3_column_text $vm $i]
1367 }
drhdde85d92003-03-01 19:45:34 +00001368 }
danielk1977106bb232004-05-21 10:08:53 +00001369 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +00001370 return [list 1 $errmsg]
1371 }
1372 }
1373 return $r
1374}
1375
drh21504322002-06-25 13:16:02 +00001376# Do an integrity check of the entire database
1377#
danielk197704103022009-02-03 16:51:24 +00001378proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +00001379 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +00001380 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +00001381 }
1382}
1383
drh433dccf2013-02-09 15:37:11 +00001384# Check the extended error code
1385#
1386proc verify_ex_errcode {name expected {db db}} {
1387 do_test $name [list sqlite3_extended_errcode $db] $expected
1388}
1389
danc6055c72011-04-28 18:46:46 +00001390
1391# Return true if the SQL statement passed as the second argument uses a
1392# statement transaction.
1393#
1394proc sql_uses_stmt {db sql} {
1395 set stmt [sqlite3_prepare $db $sql -1 dummy]
1396 set uses [uses_stmt_journal $stmt]
1397 sqlite3_finalize $stmt
1398 return $uses
1399}
1400
danielk1977db4e8862008-01-16 08:24:46 +00001401proc fix_ifcapable_expr {expr} {
1402 set ret ""
1403 set state 0
1404 for {set i 0} {$i < [string length $expr]} {incr i} {
1405 set char [string range $expr $i $i]
1406 set newstate [expr {[string is alnum $char] || $char eq "_"}]
1407 if {$newstate && !$state} {
1408 append ret {$::sqlite_options(}
1409 }
1410 if {!$newstate && $state} {
1411 append ret )
1412 }
1413 append ret $char
1414 set state $newstate
1415 }
1416 if {$state} {append ret )}
1417 return $ret
1418}
1419
mistachkinc60941f2012-09-13 01:51:02 +00001420# Returns non-zero if the capabilities are present; zero otherwise.
1421#
1422proc capable {expr} {
1423 set e [fix_ifcapable_expr $expr]; return [expr ($e)]
1424}
1425
drh27d258a2004-10-30 20:23:09 +00001426# Evaluate a boolean expression of capabilities. If true, execute the
1427# code. Omit the code if false.
1428#
danielk19773e8c37e2005-01-21 03:12:14 +00001429proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +00001430 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
1431 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +00001432 if ($e2) {
1433 set c [catch {uplevel 1 $code} r]
1434 } else {
1435 set c [catch {uplevel 1 $elsecode} r]
1436 }
1437 return -code $c $r
drh21504322002-06-25 13:16:02 +00001438}
danielk197745901d62004-11-10 15:27:38 +00001439
danielk1977aca790a2005-01-13 11:07:52 +00001440# This proc execs a seperate process that crashes midway through executing
1441# the SQL script $sql on database test.db.
1442#
1443# The crash occurs during a sync() of file $crashfile. When the crash
1444# occurs a random subset of all unsynced writes made by the process are
1445# written into the files on disk. Argument $crashdelay indicates the
1446# number of file syncs to wait before crashing.
1447#
1448# The return value is a list of two elements. The first element is a
1449# boolean, indicating whether or not the process actually crashed or
1450# reported some other error. The second element in the returned list is the
1451# error message. This is "child process exited abnormally" if the crash
mistachkin48864df2013-03-21 21:20:32 +00001452# occurred.
danielk1977aca790a2005-01-13 11:07:52 +00001453#
danielk1977f8940ae2007-08-23 11:07:10 +00001454# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +00001455#
1456proc crashsql {args} {
danielk197759a33f92007-03-17 10:26:59 +00001457
1458 set blocksize ""
1459 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +00001460 set prngseed 0
dan999cd082013-12-09 20:42:03 +00001461 set opendb { sqlite3 db test.db -vfs crash }
drhf8587402008-01-08 16:03:49 +00001462 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +00001463 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +00001464 set dc ""
danielk197759a33f92007-03-17 10:26:59 +00001465 set sql [lindex $args end]
mistachkin1d406e02013-08-29 01:09:14 +00001466
danielk197759a33f92007-03-17 10:26:59 +00001467 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
1468 set z [lindex $args $ii]
1469 set n [string length $z]
1470 set z2 [lindex $args [expr $ii+1]]
1471
1472 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
dan999cd082013-12-09 20:42:03 +00001473 elseif {$n>1 && [string first $z -opendb]==0} {set opendb $z2} \
drhcb1f0f62008-01-08 15:18:52 +00001474 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +00001475 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +00001476 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +00001477 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +00001478 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +00001479 else { error "Unrecognized option: $z" }
1480 }
1481
1482 if {$crashfile eq ""} {
1483 error "Compulsory option -file missing"
1484 }
1485
mistachkin1d406e02013-08-29 01:09:14 +00001486 # $crashfile gets compared to the native filename in
shanehf2c08822010-07-08 16:30:44 +00001487 # cfSync(), which can be different then what TCL uses by
1488 # default, so here we force it to the "nativename" format.
mistachkinf8a78462012-03-08 20:00:36 +00001489 set cfile [string map {\\ \\\\} [file nativename [file join [get_pwd] $crashfile]]]
danielk1977aca790a2005-01-13 11:07:52 +00001490
1491 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +00001492 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +00001493 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +00001494 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk1977933bbd62007-03-17 07:22:42 +00001495
1496 # This block sets the cache size of the main database to 10
1497 # pages. This is done in case the build is configured to omit
1498 # "PRAGMA cache_size".
danec16d982015-04-17 08:36:05 +00001499 if {$opendb!=""} {
1500 puts $f $opendb
1501 puts $f {db eval {SELECT * FROM sqlite_master;}}
1502 puts $f {set bt [btree_from_db db]}
1503 puts $f {btree_set_cache_size $bt 10}
1504 }
dan999cd082013-12-09 20:42:03 +00001505
drhcb1f0f62008-01-08 15:18:52 +00001506 if {$prngseed} {
1507 set seed [expr {$prngseed%10007+1}]
1508 # puts seed=$seed
1509 puts $f "db eval {SELECT randomblob($seed)}"
1510 }
danielk1977933bbd62007-03-17 07:22:42 +00001511
drhf8587402008-01-08 16:03:49 +00001512 if {[string length $tclbody]>0} {
1513 puts $f $tclbody
1514 }
1515 if {[string length $sql]>0} {
1516 puts $f "db eval {"
1517 puts $f "$sql"
1518 puts $f "}"
1519 }
danielk1977aca790a2005-01-13 11:07:52 +00001520 close $f
danielk1977aca790a2005-01-13 11:07:52 +00001521 set r [catch {
drh9c06c952005-11-26 00:25:00 +00001522 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +00001523 } msg]
mistachkin1d406e02013-08-29 01:09:14 +00001524
shanehf2c08822010-07-08 16:30:44 +00001525 # Windows/ActiveState TCL returns a slightly different
1526 # error message. We map that to the expected message
1527 # so that we don't have to change all of the test
1528 # cases.
1529 if {$::tcl_platform(platform)=="windows"} {
1530 if {$msg=="child killed: unknown signal"} {
1531 set msg "child process exited abnormally"
1532 }
1533 }
mistachkin1d406e02013-08-29 01:09:14 +00001534
danielk1977aca790a2005-01-13 11:07:52 +00001535 lappend r $msg
1536}
1537
danb88e24f2013-02-23 18:58:11 +00001538proc run_ioerr_prep {} {
1539 set ::sqlite_io_error_pending 0
1540 catch {db close}
1541 catch {db2 close}
1542 catch {forcedelete test.db}
1543 catch {forcedelete test.db-journal}
1544 catch {forcedelete test2.db}
1545 catch {forcedelete test2.db-journal}
1546 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
1547 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
1548 if {[info exists ::ioerropts(-tclprep)]} {
1549 eval $::ioerropts(-tclprep)
1550 }
1551 if {[info exists ::ioerropts(-sqlprep)]} {
1552 execsql $::ioerropts(-sqlprep)
1553 }
1554 expr 0
1555}
1556
danielk197732554c12005-01-22 03:39:39 +00001557# Usage: do_ioerr_test <test number> <options...>
1558#
1559# This proc is used to implement test cases that check that IO errors
mistachkin1d406e02013-08-29 01:09:14 +00001560# are correctly handled. The first argument, <test number>, is an integer
danielk197732554c12005-01-22 03:39:39 +00001561# used to name the tests executed by this proc. Options are as follows:
1562#
1563# -tclprep TCL script to run to prepare test.
1564# -sqlprep SQL script to run to prepare test.
1565# -tclbody TCL script to run with IO error simulation.
1566# -sqlbody TCL script to run with IO error simulation.
1567# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +00001568# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +00001569# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +00001570# -start Value of 'N' to begin with (default 1)
1571#
1572# -cksum Boolean. If true, test that the database does
1573# not change during the execution of the test case.
1574#
1575proc do_ioerr_test {testname args} {
1576
danielk197732554c12005-01-22 03:39:39 +00001577 set ::ioerropts(-start) 1
1578 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +00001579 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +00001580 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +00001581 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +00001582 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +00001583 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +00001584 array set ::ioerropts $args
1585
danielk197747cd39c2008-05-12 12:41:15 +00001586 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
1587 # a couple of obscure IO errors that do not return them.
1588 set ::ioerropts(-erc) 0
mistachkin1d406e02013-08-29 01:09:14 +00001589
danb88e24f2013-02-23 18:58:11 +00001590 # Create a single TCL script from the TCL and SQL specified
1591 # as the body of the test.
1592 set ::ioerrorbody {}
1593 if {[info exists ::ioerropts(-tclbody)]} {
1594 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
1595 }
1596 if {[info exists ::ioerropts(-sqlbody)]} {
1597 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
1598 }
1599
1600 save_prng_state
1601 if {$::ioerropts(-cksum)} {
1602 run_ioerr_prep
1603 eval $::ioerrorbody
1604 set ::goodcksum [cksum]
1605 }
danielk197747cd39c2008-05-12 12:41:15 +00001606
danielk197732554c12005-01-22 03:39:39 +00001607 set ::go 1
drh93aed5a2008-01-16 17:46:38 +00001608 #reset_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +00001609 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +00001610 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +00001611 incr ::ioerropts(-count) -1
1612 if {$::ioerropts(-count)<0} break
mistachkin1d406e02013-08-29 01:09:14 +00001613
danielk197732554c12005-01-22 03:39:39 +00001614 # Skip this IO error if it was specified with the "-exclude" option.
1615 if {[info exists ::ioerropts(-exclude)]} {
1616 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
1617 }
danielk1977861f7452008-06-05 11:39:11 +00001618 if {$::ioerropts(-restoreprng)} {
1619 restore_prng_state
1620 }
danielk197732554c12005-01-22 03:39:39 +00001621
mistachkin1d406e02013-08-29 01:09:14 +00001622 # Delete the files test.db and test2.db, then execute the TCL and
danielk197732554c12005-01-22 03:39:39 +00001623 # SQL (in that order) to prepare for the test case.
1624 do_test $testname.$n.1 {
danb88e24f2013-02-23 18:58:11 +00001625 run_ioerr_prep
danielk197732554c12005-01-22 03:39:39 +00001626 } {0}
1627
1628 # Read the 'checksum' of the database.
1629 if {$::ioerropts(-cksum)} {
danb88e24f2013-02-23 18:58:11 +00001630 set ::checksum [cksum]
danielk197732554c12005-01-22 03:39:39 +00001631 }
drh93aed5a2008-01-16 17:46:38 +00001632
danielk197732554c12005-01-22 03:39:39 +00001633 # Set the Nth IO error to fail.
1634 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +00001635 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +00001636 set ::sqlite_io_error_pending $n
1637 }] $n
danielk197732554c12005-01-22 03:39:39 +00001638
danb88e24f2013-02-23 18:58:11 +00001639 # Execute the TCL script created for the body of this test. If
mistachkin1d406e02013-08-29 01:09:14 +00001640 # at least N IO operations performed by SQLite as a result of
danb88e24f2013-02-23 18:58:11 +00001641 # the script, the Nth will fail.
danielk197732554c12005-01-22 03:39:39 +00001642 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +00001643 set ::sqlite_io_error_hit 0
1644 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +00001645 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +00001646 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +00001647 set rc [sqlite3_errcode $::DB]
1648 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +00001649 # If we are in extended result code mode, make sure all of the
1650 # IOERRs we get back really do have their extended code values.
1651 # If an extended result code is returned, the sqlite3_errcode
1652 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
1653 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +00001654 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
1655 return $rc
1656 }
1657 } else {
drh5f7b5bf2007-04-19 12:30:54 +00001658 # If we are not in extended result code mode, make sure no
1659 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +00001660 if {[regexp {\+\d} $rc]} {
1661 return $rc
1662 }
1663 }
drh93aed5a2008-01-16 17:46:38 +00001664 # The test repeats as long as $::go is non-zero. $::go starts out
1665 # as 1. When a test runs to completion without hitting an I/O
1666 # error, that means there is no point in continuing with this test
1667 # case so set $::go to zero.
1668 #
1669 if {$::sqlite_io_error_pending>0} {
1670 set ::go 0
1671 set q 0
1672 set ::sqlite_io_error_pending 0
1673 } else {
1674 set q 1
1675 }
1676
drhc9ac5ca2005-11-04 22:03:30 +00001677 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +00001678 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
1679 set r 1
1680 }
danielk1977f2fa8312006-01-24 13:09:33 +00001681 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +00001682
1683 # One of two things must have happened. either
1684 # 1. We never hit the IO error and the SQL returned OK
1685 # 2. An IO error was hit and the SQL failed
1686 #
dand41a29a2010-05-06 15:56:28 +00001687 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +00001688 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +00001689 } {1}
1690
danielk197780daec62008-05-12 10:57:02 +00001691 set ::sqlite_io_error_hit 0
1692 set ::sqlite_io_error_pending 0
1693
mistachkin1d406e02013-08-29 01:09:14 +00001694 # Check that no page references were leaked. There should be
1695 # a single reference if there is still an active transaction,
danielk197752b472a2008-05-05 16:23:55 +00001696 # or zero otherwise.
1697 #
danielk197781620542008-06-07 05:19:37 +00001698 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
mistachkin1d406e02013-08-29 01:09:14 +00001699 # locks are established on database files (i.e. if the error
danielk197781620542008-06-07 05:19:37 +00001700 # occurs while attempting to detect a hot-journal file), then
1701 # there may 0 page references and an active transaction according
1702 # to [sqlite3_get_autocommit].
1703 #
danielk19774abd5442008-05-05 15:26:50 +00001704 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +00001705 do_test $testname.$n.4 {
1706 set bt [btree_from_db db]
1707 db_enter db
1708 array set stats [btree_pager_stats $bt]
1709 db_leave db
danielk197781620542008-06-07 05:19:37 +00001710 set nRef $stats(ref)
1711 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
1712 } {1}
danielk19774abd5442008-05-05 15:26:50 +00001713 }
1714
mistachkin1d406e02013-08-29 01:09:14 +00001715 # If there is an open database handle and no open transaction,
danielk197752b472a2008-05-05 16:23:55 +00001716 # and the pager is not running in exclusive-locking mode,
1717 # check that the pager is in "unlocked" state. Theoretically,
1718 # if a call to xUnlock() failed due to an IO error the underlying
1719 # file may still be locked.
1720 #
1721 ifcapable pragma {
1722 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +00001723 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +00001724 && [db one {pragma locking_mode}] eq "normal"
1725 && [sqlite3_get_autocommit db]
1726 } {
1727 do_test $testname.$n.5 {
1728 set bt [btree_from_db db]
1729 db_enter db
1730 array set stats [btree_pager_stats $bt]
1731 db_leave db
1732 set stats(state)
1733 } 0
1734 }
1735 }
1736
mistachkin48864df2013-03-21 21:20:32 +00001737 # If an IO error occurred, then the checksum of the database should
danielk197732554c12005-01-22 03:39:39 +00001738 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +00001739 #
drh1aa5af12008-03-07 19:51:14 +00001740 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +00001741 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +00001742 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +00001743 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +00001744 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danb88e24f2013-02-23 18:58:11 +00001745 set nowcksum [cksum]
1746 set res [expr {$nowcksum==$::checksum || $nowcksum==$::goodcksum}]
1747 if {$res==0} {
dan17c08232015-06-09 15:58:28 +00001748 output2 "now=$nowcksum"
1749 output2 "the=$::checksum"
1750 output2 "fwd=$::goodcksum"
danb88e24f2013-02-23 18:58:11 +00001751 }
1752 set res
1753 } 1
danielk197732554c12005-01-22 03:39:39 +00001754 }
1755
drh1aa5af12008-03-07 19:51:14 +00001756 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +00001757 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +00001758 if {[info exists ::ioerropts(-cleanup)]} {
1759 catch $::ioerropts(-cleanup)
1760 }
danielk197732554c12005-01-22 03:39:39 +00001761 }
1762 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +00001763 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +00001764 unset ::ioerropts
1765}
1766
drh93aed5a2008-01-16 17:46:38 +00001767# Return a checksum based on the contents of the main database associated
1768# with connection $db
danielk197732554c12005-01-22 03:39:39 +00001769#
1770proc cksum {{db db}} {
1771 set txt [$db eval {
1772 SELECT name, type, sql FROM sqlite_master order by name
1773 }]\n
1774 foreach tbl [$db eval {
1775 SELECT name FROM sqlite_master WHERE type='table' order by name
1776 }] {
1777 append txt [$db eval "SELECT * FROM $tbl"]\n
1778 }
1779 foreach prag {default_synchronous default_cache_size} {
1780 append txt $prag-[$db eval "PRAGMA $prag"]\n
1781 }
1782 set cksum [string length $txt]-[md5 $txt]
1783 # puts $cksum-[file size test.db]
1784 return $cksum
1785}
1786
drh93aed5a2008-01-16 17:46:38 +00001787# Generate a checksum based on the contents of the main and temp tables
1788# database $db. If the checksum of two databases is the same, and the
1789# integrity-check passes for both, the two databases are identical.
1790#
drh1db639c2008-01-17 02:36:28 +00001791proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +00001792 set ret [list]
1793 ifcapable tempdb {
1794 set sql {
1795 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1796 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
1797 SELECT 'sqlite_master' UNION
1798 SELECT 'sqlite_temp_master' ORDER BY 1
1799 }
1800 } else {
1801 set sql {
1802 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1803 SELECT 'sqlite_master' ORDER BY 1
1804 }
1805 }
1806 set tbllist [$db eval $sql]
1807 set txt {}
1808 foreach tbl $tbllist {
1809 append txt [$db eval "SELECT * FROM $tbl"]
1810 }
1811 foreach prag {default_cache_size} {
1812 append txt $prag-[$db eval "PRAGMA $prag"]\n
1813 }
1814 # puts txt=$txt
1815 return [md5 $txt]
1816}
1817
drhdc2c4912009-02-04 22:46:47 +00001818# Generate a checksum based on the contents of a single database with
mistachkin1d406e02013-08-29 01:09:14 +00001819# a database connection. The name of the database is $dbname.
drhdc2c4912009-02-04 22:46:47 +00001820# Examples of $dbname are "temp" or "main".
1821#
1822proc dbcksum {db dbname} {
1823 if {$dbname=="temp"} {
1824 set master sqlite_temp_master
1825 } else {
1826 set master $dbname.sqlite_master
1827 }
1828 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
1829 set txt [$db eval "SELECT * FROM $master"]\n
1830 foreach tab $alltab {
1831 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
1832 }
1833 return [md5 $txt]
1834}
1835
danielk197735754ac2008-03-21 17:29:37 +00001836proc memdebug_log_sql {{filename mallocs.sql}} {
1837
danielk19776f332c12008-03-21 14:22:44 +00001838 set data [sqlite3_memdebug_log dump]
1839 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +00001840 if {$nFrame < 0} { return "" }
1841
danielk197735754ac2008-03-21 17:29:37 +00001842 set database temp
1843
danielk19776ab3a2e2009-02-19 14:39:25 +00001844 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +00001845
1846 set sql ""
1847 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +00001848 set nCall [lindex $e 0]
1849 set nByte [lindex $e 1]
1850 set lStack [lrange $e 2 end]
1851 append sql "INSERT INTO ${database}.malloc VALUES"
1852 append sql "('test', $nCall, $nByte, '$lStack');\n"
1853 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +00001854 set frames($f) 1
1855 }
1856 }
1857
1858 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +00001859 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +00001860
1861 foreach f [array names frames] {
1862 set addr [format %x $f]
1863 set cmd "addr2line -e [info nameofexec] $addr"
1864 set line [eval exec $cmd]
1865 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00001866
1867 set file [lindex [split $line :] 0]
1868 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00001869 }
1870
danielk197735754ac2008-03-21 17:29:37 +00001871 foreach f [array names files] {
1872 set contents ""
1873 catch {
1874 set fd [open $f]
1875 set contents [read $fd]
1876 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001877 }
danielk197735754ac2008-03-21 17:29:37 +00001878 set contents [string map {' ''} $contents]
1879 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00001880 }
danielk19776f332c12008-03-21 14:22:44 +00001881
danielk197735754ac2008-03-21 17:29:37 +00001882 set fd [open $filename w]
1883 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
1884 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001885}
drh93aed5a2008-01-16 17:46:38 +00001886
danf5894502009-10-07 18:41:19 +00001887# Drop all tables in database [db]
1888proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00001889 ifcapable trigger&&foreignkey {
1890 set pk [$db one "PRAGMA foreign_keys"]
1891 $db eval "PRAGMA foreign_keys = OFF"
1892 }
drh9a6ffc82010-02-15 18:03:20 +00001893 foreach {idx name file} [db eval {PRAGMA database_list}] {
1894 if {$idx==1} {
1895 set master sqlite_temp_master
1896 } else {
1897 set master $name.sqlite_master
1898 }
1899 foreach {t type} [$db eval "
1900 SELECT name, type FROM $master
dana16d1062010-09-28 17:37:28 +00001901 WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X'
drh9a6ffc82010-02-15 18:03:20 +00001902 "] {
dana16d1062010-09-28 17:37:28 +00001903 $db eval "DROP $type \"$t\""
drh9a6ffc82010-02-15 18:03:20 +00001904 }
danf5894502009-10-07 18:41:19 +00001905 }
shaneh4e7b32f2009-12-17 22:12:51 +00001906 ifcapable trigger&&foreignkey {
1907 $db eval "PRAGMA foreign_keys = $pk"
1908 }
danf5894502009-10-07 18:41:19 +00001909}
1910
dan71cb5182010-04-26 12:39:03 +00001911#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00001912# If a test script is executed with global variable $::G(perm:name) set to
mistachkin1d406e02013-08-29 01:09:14 +00001913# "wal", then the tests are run in WAL mode. Otherwise, they should be run
1914# in rollback mode. The following Tcl procs are used to make this less
dan430e74c2010-06-07 17:47:26 +00001915# intrusive:
dan71cb5182010-04-26 12:39:03 +00001916#
1917# wal_set_journal_mode ?DB?
1918#
1919# If running a WAL test, execute "PRAGMA journal_mode = wal" using
1920# connection handle DB. Otherwise, this command is a no-op.
1921#
1922# wal_check_journal_mode TESTNAME ?DB?
1923#
1924# If running a WAL test, execute a tests case that fails if the main
1925# database for connection handle DB is not currently a WAL database.
1926# Otherwise (if not running a WAL permutation) this is a no-op.
1927#
1928# wal_is_wal_mode
mistachkin1d406e02013-08-29 01:09:14 +00001929#
dan71cb5182010-04-26 12:39:03 +00001930# Returns true if this test should be run in WAL mode. False otherwise.
mistachkin1d406e02013-08-29 01:09:14 +00001931#
dan71cb5182010-04-26 12:39:03 +00001932proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00001933 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00001934}
1935proc wal_set_journal_mode {{db db}} {
1936 if { [wal_is_wal_mode] } {
1937 $db eval "PRAGMA journal_mode = WAL"
1938 }
1939}
1940proc wal_check_journal_mode {testname {db db}} {
1941 if { [wal_is_wal_mode] } {
1942 $db eval { SELECT * FROM sqlite_master }
1943 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
1944 }
1945}
1946
dan430e74c2010-06-07 17:47:26 +00001947proc permutation {} {
1948 set perm ""
1949 catch {set perm $::G(perm:name)}
1950 set perm
1951}
dancb79e512010-08-06 13:50:07 +00001952proc presql {} {
1953 set presql ""
1954 catch {set presql $::G(perm:presql)}
1955 set presql
1956}
dan430e74c2010-06-07 17:47:26 +00001957
danbe7721d2016-02-04 17:31:03 +00001958proc isquick {} {
1959 set ret 0
1960 catch {set ret $::G(isquick)}
1961 set ret
1962}
1963
danc1a60c52010-06-07 14:28:16 +00001964#-------------------------------------------------------------------------
1965#
1966proc slave_test_script {script} {
1967
1968 # Create the interpreter used to run the test script.
1969 interp create tinterp
1970
1971 # Populate some global variables that tester.tcl expects to see.
1972 foreach {var value} [list \
1973 ::argv0 $::argv0 \
1974 ::argv {} \
1975 ::SLAVE 1 \
1976 ] {
1977 interp eval tinterp [list set $var $value]
1978 }
1979
dan17c08232015-06-09 15:58:28 +00001980 # If output is being copied into a file, share the file-descriptor with
1981 # the interpreter.
1982 if {[info exists ::G(output_fd)]} {
1983 interp share {} $::G(output_fd) tinterp
1984 }
1985
danc1a60c52010-06-07 14:28:16 +00001986 # The alias used to access the global test counters.
1987 tinterp alias set_test_counter set_test_counter
1988
1989 # Set up the ::cmdlinearg array in the slave.
1990 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
1991
dan430e74c2010-06-07 17:47:26 +00001992 # Set up the ::G array in the slave.
1993 interp eval tinterp [list array set ::G [array get ::G]]
1994
danc1a60c52010-06-07 14:28:16 +00001995 # Load the various test interfaces implemented in C.
1996 load_testfixture_extensions tinterp
1997
1998 # Run the test script.
1999 interp eval tinterp $script
2000
danea5542d2010-07-06 11:26:15 +00002001 # Check if the interpreter call [run_thread_tests]
2002 if { [interp eval tinterp {info exists ::run_thread_tests_called}] } {
2003 set ::run_thread_tests_called 1
2004 }
2005
danc1a60c52010-06-07 14:28:16 +00002006 # Delete the interpreter used to run the test script.
2007 interp delete tinterp
2008}
2009
dan430e74c2010-06-07 17:47:26 +00002010proc slave_test_file {zFile} {
dan0626dfc2010-06-15 06:56:37 +00002011 set tail [file tail $zFile]
dan430e74c2010-06-07 17:47:26 +00002012
dan6a64d672011-04-04 15:38:16 +00002013 if {[info exists ::G(start:permutation)]} {
2014 if {[permutation] != $::G(start:permutation)} return
2015 unset ::G(start:permutation)
2016 }
2017 if {[info exists ::G(start:file)]} {
2018 if {$tail != $::G(start:file) && $tail!="$::G(start:file).test"} return
2019 unset ::G(start:file)
2020 }
2021
dan92d516a2010-07-05 14:54:48 +00002022 # Remember the value of the shared-cache setting. So that it is possible
2023 # to check afterwards that it was not modified by the test script.
2024 #
2025 ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] }
dan0626dfc2010-06-15 06:56:37 +00002026
dan92d516a2010-07-05 14:54:48 +00002027 # Run the test script in a slave interpreter.
2028 #
danea5542d2010-07-06 11:26:15 +00002029 unset -nocomplain ::run_thread_tests_called
dan0626dfc2010-06-15 06:56:37 +00002030 reset_prng_state
2031 set ::sqlite_open_file_count 0
2032 set time [time { slave_test_script [list source $zFile] }]
2033 set ms [expr [lindex $time 0] / 1000]
2034
dan92d516a2010-07-05 14:54:48 +00002035 # Test that all files opened by the test script were closed. Omit this
2036 # if the test script has "thread" in its name. The open file counter
2037 # is not thread-safe.
dan0626dfc2010-06-15 06:56:37 +00002038 #
danea5542d2010-07-06 11:26:15 +00002039 if {[info exists ::run_thread_tests_called]==0} {
dan92d516a2010-07-05 14:54:48 +00002040 do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0}
2041 }
dan430e74c2010-06-07 17:47:26 +00002042 set ::sqlite_open_file_count 0
2043
mistachkin1d406e02013-08-29 01:09:14 +00002044 # Test that the global "shared-cache" setting was not altered by
dan0626dfc2010-06-15 06:56:37 +00002045 # the test script.
2046 #
mistachkin1d406e02013-08-29 01:09:14 +00002047 ifcapable shared_cache {
dan0626dfc2010-06-15 06:56:37 +00002048 set res [expr {[sqlite3_enable_shared_cache] == $scs}]
2049 do_test ${tail}-sharedcachesetting [list set {} $res] 1
2050 }
dan430e74c2010-06-07 17:47:26 +00002051
dan92d516a2010-07-05 14:54:48 +00002052 # Add some info to the output.
2053 #
dan17c08232015-06-09 15:58:28 +00002054 output2 "Time: $tail $ms ms"
dan430e74c2010-06-07 17:47:26 +00002055 show_memstats
danc1a60c52010-06-07 14:28:16 +00002056}
2057
dan59257dc2010-08-04 11:34:31 +00002058# Open a new connection on database test.db and execute the SQL script
2059# supplied as an argument. Before returning, close the new conection and
2060# restore the 4 byte fields starting at header offsets 28, 92 and 96
2061# to the values they held before the SQL was executed. This simulates
2062# a write by a pre-3.7.0 client.
2063#
2064proc sql36231 {sql} {
2065 set B [hexio_read test.db 92 8]
2066 set A [hexio_read test.db 28 4]
2067 sqlite3 db36231 test.db
2068 catch { db36231 func a_string a_string }
2069 execsql $sql db36231
2070 db36231 close
2071 hexio_write test.db 28 $A
2072 hexio_write test.db 92 $B
2073 return ""
2074}
danf5894502009-10-07 18:41:19 +00002075
danccc7ff42010-11-29 12:06:45 +00002076proc db_save {} {
2077 foreach f [glob -nocomplain sv_test.db*] { forcedelete $f }
2078 foreach f [glob -nocomplain test.db*] {
2079 set f2 "sv_$f"
mistachkinfda06be2011-08-02 00:57:34 +00002080 forcecopy $f $f2
danccc7ff42010-11-29 12:06:45 +00002081 }
2082}
2083proc db_save_and_close {} {
2084 db_save
2085 catch { db close }
2086 return ""
2087}
2088proc db_restore {} {
2089 foreach f [glob -nocomplain test.db*] { forcedelete $f }
2090 foreach f2 [glob -nocomplain sv_test.db*] {
2091 set f [string range $f2 3 end]
mistachkinfda06be2011-08-02 00:57:34 +00002092 forcecopy $f2 $f
danccc7ff42010-11-29 12:06:45 +00002093 }
2094}
2095proc db_restore_and_reopen {{dbfile test.db}} {
2096 catch { db close }
2097 db_restore
2098 sqlite3 db $dbfile
2099}
2100proc db_delete_and_reopen {{file test.db}} {
2101 catch { db close }
mistachkinfda06be2011-08-02 00:57:34 +00002102 foreach f [glob -nocomplain test.db*] { forcedelete $f }
danccc7ff42010-11-29 12:06:45 +00002103 sqlite3 db $file
2104}
2105
dan03bc5252015-07-24 14:17:17 +00002106# Close any connections named [db], [db2] or [db3]. Then use sqlite3_config
2107# to configure the size of the PAGECACHE allocation using the parameters
2108# provided to this command. Save the old PAGECACHE parameters in a global
2109# variable so that [test_restore_config_pagecache] can restore the previous
2110# configuration.
2111#
2112# Before returning, reopen connection [db] on file test.db.
2113#
2114proc test_set_config_pagecache {sz nPg} {
2115 catch {db close}
2116 catch {db2 close}
2117 catch {db3 close}
2118
2119 sqlite3_shutdown
2120 set ::old_pagecache_config [sqlite3_config_pagecache $sz $nPg]
2121 sqlite3_initialize
2122 autoinstall_test_functions
dand716c392015-07-24 18:22:29 +00002123 reset_db
dan03bc5252015-07-24 14:17:17 +00002124}
2125
2126# Close any connections named [db], [db2] or [db3]. Then use sqlite3_config
2127# to configure the size of the PAGECACHE allocation to the size saved in
2128# the global variable by an earlier call to [test_set_config_pagecache].
2129#
2130# Before returning, reopen connection [db] on file test.db.
2131#
2132proc test_restore_config_pagecache {} {
2133 catch {db close}
2134 catch {db2 close}
2135 catch {db3 close}
2136
2137 sqlite3_shutdown
2138 eval sqlite3_config_pagecache $::old_pagecache_config
2139 unset ::old_pagecache_config
2140 sqlite3_initialize
2141 autoinstall_test_functions
2142 sqlite3 db test.db
2143}
2144
dan1e8dae02016-03-19 14:53:36 +00002145proc test_find_binary {nm} {
dan089555c2016-03-15 09:55:44 +00002146 if {$::tcl_platform(platform)=="windows"} {
dan1e8dae02016-03-19 14:53:36 +00002147 set ret "$nm.exe"
dan089555c2016-03-15 09:55:44 +00002148 } else {
dan1e8dae02016-03-19 14:53:36 +00002149 set ret $nm
dan089555c2016-03-15 09:55:44 +00002150 }
2151 set ret [file normalize [file join $::cmdlinearg(TESTFIXTURE_HOME) $ret]]
2152 if {![file executable $ret]} {
2153 finish_test
dan49aed582016-03-19 15:13:59 +00002154 return ""
dan089555c2016-03-15 09:55:44 +00002155 }
2156 return $ret
2157}
2158
danielk197745901d62004-11-10 15:27:38 +00002159# Find the name of the 'shell' executable (e.g. "sqlite3.exe") to use for
2160# the tests in shell[1-5].test. If no such executable can be found, invoke
2161# [finish_test ; return] in the callers context.
danielk1977ee8b7992009-03-26 17:13:06 +00002162#
dan64b95bb2012-05-12 05:30:29 +00002163proc test_find_cli {} {
danf27d7372016-03-19 17:48:12 +00002164 set prog [test_find_binary sqlite3]
dan49aed582016-03-19 15:13:59 +00002165 if {$prog==""} { return -code return }
2166 return $prog
drh348784e2000-05-29 20:41:49 +00002167}
2168
dan1e8dae02016-03-19 14:53:36 +00002169# Find the name of the 'sqldiff' executable (e.g. "sqlite3.exe") to use for
2170# the tests in sqldiff tests. If no such executable can be found, invoke
2171# [finish_test ; return] in the callers context.
2172#
2173proc test_find_sqldiff {} {
dan49aed582016-03-19 15:13:59 +00002174 set prog [test_find_binary sqldiff]
2175 if {$prog==""} { return -code return }
2176 return $prog
dan1e8dae02016-03-19 14:53:36 +00002177}
2178
2179
drh348784e2000-05-29 20:41:49 +00002180# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
2181# to non-zero, then set the global variable $AUTOVACUUM to 1.
2182set AUTOVACUUM $sqlite_options(default_autovacuum)
2183
dan64b95bb2012-05-12 05:30:29 +00002184# Make sure the FTS enhanced query syntax is disabled.
2185set sqlite_fts3_enable_parentheses 0
2186
drh348784e2000-05-29 20:41:49 +00002187# During testing, assume that all database files are well-formed. The
2188# few test cases that deliberately corrupt database files should rescind
2189# this setting by invoking "database_can_be_corrupt"
2190#
2191database_never_corrupt
2192
drh348784e2000-05-29 20:41:49 +00002193source $testdir/thread_common.tcl
2194source $testdir/malloc_common.tcl