blob: 3f1edc32ae81e7f6b94d869596ab1cfba45b32a2 [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 {
dan40cf36f2016-04-30 19:23:10 +0000524 if {[file tail $a]==$a} {
525 lappend leftover $a
526 } else {
527 lappend leftover [file normalize $a]
528 }
danc1a60c52010-06-07 14:28:16 +0000529 }
530 }
531 }
drhbea14132016-03-14 14:28:43 +0000532 set testdir [file normalize $testdir]
drhd5704a82016-03-14 13:42:29 +0000533 set cmdlinearg(TESTFIXTURE_HOME) [pwd]
drhaf3906a2016-03-14 17:05:04 +0000534 set cmdlinearg(INFO_SCRIPT) [file normalize [info script]]
drhbea14132016-03-14 14:28:43 +0000535 set argv0 [file normalize $argv0]
drhd5704a82016-03-14 13:42:29 +0000536 if {$cmdlinearg(testdir)!=""} {
537 file mkdir $cmdlinearg(testdir)
538 cd $cmdlinearg(testdir)
539 }
danc1a60c52010-06-07 14:28:16 +0000540 set argv $leftover
541
dan430e74c2010-06-07 17:47:26 +0000542 # Install the malloc layer used to inject OOM errors. And the 'automatic'
543 # extensions. This only needs to be done once for the process.
544 #
mistachkin1d406e02013-08-29 01:09:14 +0000545 sqlite3_shutdown
546 install_malloc_faultsim 1
danielk1977f7b9d662008-06-23 18:49:43 +0000547 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000548 autoinstall_test_functions
danc1a60c52010-06-07 14:28:16 +0000549
dan430e74c2010-06-07 17:47:26 +0000550 # If the --binarylog option was specified, create the logging VFS. This
551 # call installs the new VFS as the default for all SQLite connections.
552 #
danc1a60c52010-06-07 14:28:16 +0000553 if {$cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000554 vfslog new binarylog {} vfslog.bin
danc1a60c52010-06-07 14:28:16 +0000555 }
556
557 # Set the backtrace depth, if malloc tracing is enabled.
558 #
559 if {$cmdlinearg(malloctrace)} {
560 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danielk1977185eac92008-07-12 15:55:54 +0000561 }
dan17c08232015-06-09 15:58:28 +0000562
563 if {$cmdlinearg(output)!=""} {
564 puts "Copying output to file $cmdlinearg(output)"
565 set ::G(output_fd) [open $cmdlinearg(output) w]
566 fconfigure $::G(output_fd) -buffering line
567 }
568
569 if {$cmdlinearg(verbose)==""} {
570 set cmdlinearg(verbose) 1
571 }
danielk1977f7b9d662008-06-23 18:49:43 +0000572}
danielk197703ba3fa2009-01-09 10:49:14 +0000573
danc1a60c52010-06-07 14:28:16 +0000574# Update the soft-heap-limit each time this script is run. In that
575# way if an individual test file changes the soft-heap-limit, it
576# will be reset at the start of the next test file.
577#
578sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
579
580# Create a test database
581#
danielk197703ba3fa2009-01-09 10:49:14 +0000582proc reset_db {} {
583 catch {db close}
mistachkinfda06be2011-08-02 00:57:34 +0000584 forcedelete test.db
585 forcedelete test.db-journal
586 forcedelete test.db-wal
danielk197703ba3fa2009-01-09 10:49:14 +0000587 sqlite3 db ./test.db
588 set ::DB [sqlite3_connection_pointer db]
589 if {[info exists ::SETUP_SQL]} {
590 db eval $::SETUP_SQL
591 }
drhcd61c282002-03-06 22:01:34 +0000592}
danielk197703ba3fa2009-01-09 10:49:14 +0000593reset_db
drhbec2bf42000-05-29 23:48:22 +0000594
595# Abort early if this script has been run before.
596#
danc1a60c52010-06-07 14:28:16 +0000597if {[info exists TC(count)]} return
drhbec2bf42000-05-29 23:48:22 +0000598
drhce2198c2010-09-10 13:22:59 +0000599# Make sure memory statistics are enabled.
600#
601sqlite3_config_memstatus 1
602
danc1a60c52010-06-07 14:28:16 +0000603# Initialize the test counters and set up commands to access them.
604# Or, if this is a slave interpreter, set up aliases to write the
605# counters in the parent interpreter.
drhbec2bf42000-05-29 23:48:22 +0000606#
danc1a60c52010-06-07 14:28:16 +0000607if {0==[info exists ::SLAVE]} {
608 set TC(errors) 0
609 set TC(count) 0
610 set TC(fail_list) [list]
611 set TC(omit_list) [list]
drhcca17c32013-04-19 12:32:52 +0000612 set TC(warn_list) [list]
danc1a60c52010-06-07 14:28:16 +0000613
614 proc set_test_counter {counter args} {
615 if {[llength $args]} {
616 set ::TC($counter) [lindex $args 0]
617 }
618 set ::TC($counter)
619 }
drhb62c3352006-11-23 09:39:16 +0000620}
drh348784e2000-05-29 20:41:49 +0000621
drh521cc842008-04-15 02:36:33 +0000622# Record the fact that a sequence of tests were omitted.
623#
mistachkin6c3c1a02011-11-12 03:17:40 +0000624proc omit_test {name reason {append 1}} {
danc1a60c52010-06-07 14:28:16 +0000625 set omitList [set_test_counter omit_list]
mistachkin6c3c1a02011-11-12 03:17:40 +0000626 if {$append} {
627 lappend omitList [list $name $reason]
628 }
danc1a60c52010-06-07 14:28:16 +0000629 set_test_counter omit_list $omitList
drh521cc842008-04-15 02:36:33 +0000630}
631
danc1a60c52010-06-07 14:28:16 +0000632# Record the fact that a test failed.
633#
634proc fail_test {name} {
635 set f [set_test_counter fail_list]
636 lappend f $name
637 set_test_counter fail_list $f
638 set_test_counter errors [expr [set_test_counter errors] + 1]
639
640 set nFail [set_test_counter errors]
641 if {$nFail>=$::cmdlinearg(maxerror)} {
dan17c08232015-06-09 15:58:28 +0000642 output2 "*** Giving up..."
danc1a60c52010-06-07 14:28:16 +0000643 finalize_testing
644 }
645}
646
drhcca17c32013-04-19 12:32:52 +0000647# Remember a warning message to be displayed at the conclusion of all testing
648#
649proc warning {msg {append 1}} {
dan17c08232015-06-09 15:58:28 +0000650 output2 "Warning: $msg"
drhcca17c32013-04-19 12:32:52 +0000651 set warnList [set_test_counter warn_list]
652 if {$append} {
653 lappend warnList $msg
654 }
655 set_test_counter warn_list $warnList
656}
657
658
danc1a60c52010-06-07 14:28:16 +0000659# Increment the number of tests run
660#
661proc incr_ntest {} {
662 set_test_counter count [expr [set_test_counter count] + 1]
663}
664
dan17c08232015-06-09 15:58:28 +0000665# Return true if --verbose=1 was specified on the command line. Otherwise,
666# return false.
667#
668proc verbose {} {
669 return $::cmdlinearg(verbose)
670}
671
672# Use the following commands instead of [puts] for test output within
673# this file. Test scripts can still use regular [puts], which is directed
674# to stdout and, if one is open, the --output file.
675#
676# output1: output that should be printed if --verbose=1 was specified.
677# output2: output that should be printed unconditionally.
678# output2_if_no_verbose: output that should be printed only if --verbose=0.
679#
680proc output1 {args} {
681 set v [verbose]
682 if {$v==1} {
683 uplevel output2 $args
684 } elseif {$v==2} {
685 uplevel puts [lrange $args 0 end-1] $::G(output_fd) [lrange $args end end]
686 }
687}
688proc output2 {args} {
689 set nArg [llength $args]
690 uplevel puts $args
691}
692proc output2_if_no_verbose {args} {
693 set v [verbose]
694 if {$v==0} {
695 uplevel output2 $args
696 } elseif {$v==2} {
697 uplevel puts [lrange $args 0 end-1] stdout [lrange $args end end]
698 }
699}
700
701# Override the [puts] command so that if no channel is explicitly
702# specified the string is written to both stdout and to the file
703# specified by "--output=", if any.
704#
705proc puts_override {args} {
706 set nArg [llength $args]
707 if {$nArg==1 || ($nArg==2 && [string first [lindex $args 0] -nonewline]==0)} {
708 uplevel puts_original $args
709 if {[info exists ::G(output_fd)]} {
710 uplevel puts [lrange $args 0 end-1] $::G(output_fd) [lrange $args end end]
711 }
712 } else {
713 # A channel was explicitly specified.
714 uplevel puts_original $args
715 }
716}
717rename puts puts_original
718proc puts {args} { uplevel puts_override $args }
719
danc1a60c52010-06-07 14:28:16 +0000720
mistachkin1d406e02013-08-29 01:09:14 +0000721# Invoke the do_test procedure to run a single test
drh348784e2000-05-29 20:41:49 +0000722#
drhe39cd912016-07-09 17:47:01 +0000723# The $expected parameter is the expected result. The result is the return
724# value from the last TCL command in $cmd.
725#
726# Normally, $expected must match exactly. But if $expected is of the form
727# "/regexp/" then regular expression matching is used. If $expected is
728# "~/regexp/" then the regular expression must NOT match. If $expected is
729# of the form "#/value-list/" then each term in value-list must be numeric
730# and must approximately match the corresponding numeric term in $result.
731# Values must match within 10%. Or if the $expected term is A..B then the
732# $result term must be in between A and B.
733#
drh348784e2000-05-29 20:41:49 +0000734proc do_test {name cmd expected} {
danc1a60c52010-06-07 14:28:16 +0000735 global argv cmdlinearg
736
dan8c408002010-11-01 17:38:24 +0000737 fix_testname name
738
drh4a50aac2007-08-23 02:47:53 +0000739 sqlite3_memdebug_settitle $name
danc1a60c52010-06-07 14:28:16 +0000740
mistachkin1d406e02013-08-29 01:09:14 +0000741# if {[llength $argv]==0} {
dan92d516a2010-07-05 14:54:48 +0000742# set go 1
743# } else {
744# set go 0
745# foreach pattern $argv {
746# if {[string match $pattern $name]} {
747# set go 1
748# break
749# }
750# }
751# }
dan430e74c2010-06-07 17:47:26 +0000752
dand506de02010-07-03 13:59:01 +0000753 if {[info exists ::G(perm:prefix)]} {
754 set name "$::G(perm:prefix)$name"
dan430e74c2010-06-07 17:47:26 +0000755 }
756
danc1a60c52010-06-07 14:28:16 +0000757 incr_ntest
dan17c08232015-06-09 15:58:28 +0000758 output1 -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000759 flush stdout
mistachkin6c3c1a02011-11-12 03:17:40 +0000760
761 if {![info exists ::G(match)] || [string match $::G(match) $name]} {
762 if {[catch {uplevel #0 "$cmd;\n"} result]} {
dan17c08232015-06-09 15:58:28 +0000763 output2_if_no_verbose -nonewline $name...
764 output2 "\nError: $result"
mistachkin6c3c1a02011-11-12 03:17:40 +0000765 fail_test $name
mistachkin6c3c1a02011-11-12 03:17:40 +0000766 } else {
drhe39cd912016-07-09 17:47:01 +0000767 if {[regexp {^[~#]?/.*/$} $expected]} {
drh70bdcc72013-05-30 19:28:34 +0000768 # "expected" is of the form "/PATTERN/" then the result if correct if
769 # regular expression PATTERN matches the result. "~/PATTERN/" means
770 # the regular expression must not match.
drh3f17aef2012-04-27 01:08:02 +0000771 if {[string index $expected 0]=="~"} {
drhc2b23e72013-11-13 15:32:15 +0000772 set re [string range $expected 2 end-1]
773 if {[string index $re 0]=="*"} {
774 # If the regular expression begins with * then treat it as a glob instead
775 set ok [string match $re $result]
776 } else {
777 set re [string map {# {[-0-9.]+}} $re]
778 set ok [regexp $re $result]
779 }
780 set ok [expr {!$ok}]
drhe39cd912016-07-09 17:47:01 +0000781 } elseif {[string index $expected 0]=="#"} {
782 # Numeric range value comparison. Each term of the $result is matched
783 # against one term of $expect. Both $result and $expected terms must be
784 # numeric. The values must match within 10%. Or if $expected is of the
785 # form A..B then the $result term must be between A and B.
786 set e2 [string range $expected 2 end-1]
787 foreach i $result j $e2 {
788 if {[regexp {^(-?\d+)\.\.(-?\d)$} $j all A B]} {
789 set ok [expr {$i+0>=$A && $i+0<=$B}]
790 } else {
791 set ok [expr {$i+0>=0.9*$j && $i+0<=1.1*$j}]
792 }
793 if {!$ok} break
794 }
795 if {$ok && [llength $result]!=[llength $e2]} {set ok 0}
drh3f17aef2012-04-27 01:08:02 +0000796 } else {
drhc2b23e72013-11-13 15:32:15 +0000797 set re [string range $expected 1 end-1]
798 if {[string index $re 0]=="*"} {
799 # If the regular expression begins with * then treat it as a glob instead
800 set ok [string match $re $result]
801 } else {
802 set re [string map {# {[-0-9.]+}} $re]
803 set ok [regexp $re $result]
804 }
drh3f17aef2012-04-27 01:08:02 +0000805 }
drh70bdcc72013-05-30 19:28:34 +0000806 } elseif {[regexp {^~?\*.*\*$} $expected]} {
807 # "expected" is of the form "*GLOB*" then the result if correct if
808 # glob pattern GLOB matches the result. "~/GLOB/" means
809 # the glob must not match.
810 if {[string index $expected 0]=="~"} {
811 set e [string range $expected 1 end]
812 set ok [expr {![string match $e $result]}]
813 } else {
814 set ok [string match $expected $result]
815 }
drh3f17aef2012-04-27 01:08:02 +0000816 } else {
817 set ok [expr {[string compare $result $expected]==0}]
818 }
819 if {!$ok} {
mistachkinc60941f2012-09-13 01:51:02 +0000820 # if {![info exists ::testprefix] || $::testprefix eq ""} {
821 # error "no test prefix"
822 # }
drhd66b2e02015-11-12 21:42:40 +0000823 output1 ""
drh061d35c2015-11-13 00:03:14 +0000824 output2 "! $name expected: \[$expected\]\n! $name got: \[$result\]"
drh3f17aef2012-04-27 01:08:02 +0000825 fail_test $name
826 } else {
dan17c08232015-06-09 15:58:28 +0000827 output1 " Ok"
drh3f17aef2012-04-27 01:08:02 +0000828 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000829 }
drh348784e2000-05-29 20:41:49 +0000830 } else {
dan17c08232015-06-09 15:58:28 +0000831 output1 " Omitted"
mistachkin6c3c1a02011-11-12 03:17:40 +0000832 omit_test $name "pattern mismatch" 0
drh348784e2000-05-29 20:41:49 +0000833 }
drh6558db82007-04-13 03:23:21 +0000834 flush stdout
drh348784e2000-05-29 20:41:49 +0000835}
dana3f51082010-09-30 18:43:14 +0000836
mistachkinf21979d2015-01-18 05:35:01 +0000837proc dumpbytes {s} {
838 set r ""
839 for {set i 0} {$i < [string length $s]} {incr i} {
840 if {$i > 0} {append r " "}
841 append r [format %02X [scan [string index $s $i] %c]]
842 }
843 return $r
844}
845
drh8df91852012-04-24 12:46:05 +0000846proc catchcmd {db {cmd ""}} {
847 global CLI
848 set out [open cmds.txt w]
849 puts $out $cmd
850 close $out
851 set line "exec $CLI $db < cmds.txt"
852 set rc [catch { eval $line } msg]
853 list $rc $msg
854}
855
mistachkinf21979d2015-01-18 05:35:01 +0000856proc catchcmdex {db {cmd ""}} {
857 global CLI
858 set out [open cmds.txt w]
859 fconfigure $out -encoding binary -translation binary
860 puts -nonewline $out $cmd
861 close $out
862 set line "exec -keepnewline -- $CLI $db < cmds.txt"
863 set chans [list stdin stdout stderr]
864 foreach chan $chans {
865 catch {
866 set modes($chan) [fconfigure $chan]
867 fconfigure $chan -encoding binary -translation binary -buffering none
868 }
869 }
870 set rc [catch { eval $line } msg]
871 foreach chan $chans {
872 catch {
873 eval fconfigure [list $chan] $modes($chan)
874 }
875 }
876 # puts [dumpbytes $msg]
877 list $rc $msg
878}
879
shaneh55505172011-06-21 19:30:19 +0000880proc filepath_normalize {p} {
881 # test cases should be written to assume "unix"-like file paths
shaneh285a18f2011-06-21 19:39:59 +0000882 if {$::tcl_platform(platform)!="unix"} {
shaneh55505172011-06-21 19:30:19 +0000883 # lreverse*2 as a hack to remove any unneeded {} after the string map
884 lreverse [lreverse [string map {\\ /} [regsub -nocase -all {[a-z]:[/\\]+} $p {/}]]]
shanehc4896402011-06-21 19:38:16 +0000885 } {
886 set p
shaneh55505172011-06-21 19:30:19 +0000887 }
888}
889proc do_filepath_test {name cmd expected} {
890 uplevel [list do_test $name [
891 subst -nocommands { filepath_normalize [ $cmd ] }
892 ] [filepath_normalize $expected]]
893}
894
dan33f53792011-05-05 19:44:22 +0000895proc realnum_normalize {r} {
shaneh4f529e82011-06-20 17:41:41 +0000896 # different TCL versions display floating point values differently.
897 string map {1.#INF inf Inf inf .0e e} [regsub -all {(e[+-])0+} $r {\1}]
dan33f53792011-05-05 19:44:22 +0000898}
899proc do_realnum_test {name cmd expected} {
900 uplevel [list do_test $name [
901 subst -nocommands { realnum_normalize [ $cmd ] }
902 ] [realnum_normalize $expected]]
903}
904
dana3f51082010-09-30 18:43:14 +0000905proc fix_testname {varname} {
906 upvar $varname testname
mistachkin1d406e02013-08-29 01:09:14 +0000907 if {[info exists ::testprefix]
dana3f51082010-09-30 18:43:14 +0000908 && [string is digit [string range $testname 0 0]]
909 } {
910 set testname "${::testprefix}-$testname"
911 }
912}
dan67340072011-04-16 19:23:10 +0000913
914proc normalize_list {L} {
915 set L2 [list]
916 foreach l $L {lappend L2 $l}
917 set L2
918}
drh5f9742e2013-08-29 15:08:38 +0000919
dan57f7f4b2010-10-01 19:04:37 +0000920proc do_execsql_test {testname sql {result {}}} {
dana3f51082010-09-30 18:43:14 +0000921 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000922 uplevel do_test [list $testname] [list "execsql {$sql}"] [list [list {*}$result]]
dan153eda02010-06-21 07:45:47 +0000923}
924proc do_catchsql_test {testname sql result} {
dana3f51082010-09-30 18:43:14 +0000925 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000926 uplevel do_test [list $testname] [list "catchsql {$sql}"] [list $result]
dan153eda02010-06-21 07:45:47 +0000927}
drh3c2aeae2014-01-24 14:37:44 +0000928proc do_timed_execsql_test {testname sql {result {}}} {
929 fix_testname testname
930 uplevel do_test [list $testname] [list "execsql_timed {$sql}"]\
931 [list [list {*}$result]]
932}
dan39854792010-11-15 16:12:58 +0000933proc do_eqp_test {name sql res} {
934 uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res]
935}
dan153eda02010-06-21 07:45:47 +0000936
dan51f06982010-09-18 19:00:12 +0000937#-------------------------------------------------------------------------
938# Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST
939#
940# Where switches are:
941#
942# -errorformat FMTSTRING
943# -count
danaf1dcab2010-09-20 19:17:53 +0000944# -query SQL
dana16d1062010-09-28 17:37:28 +0000945# -tclquery TCL
danaf7626f2010-09-23 18:47:36 +0000946# -repair TCL
dan51f06982010-09-18 19:00:12 +0000947#
948proc do_select_tests {prefix args} {
949
950 set testlist [lindex $args end]
951 set switches [lrange $args 0 end-1]
952
953 set errfmt ""
954 set countonly 0
dana16d1062010-09-28 17:37:28 +0000955 set tclquery ""
danaf7626f2010-09-23 18:47:36 +0000956 set repair ""
dan51f06982010-09-18 19:00:12 +0000957
958 for {set i 0} {$i < [llength $switches]} {incr i} {
959 set s [lindex $switches $i]
960 set n [string length $s]
danaf1dcab2010-09-20 19:17:53 +0000961 if {$n>=2 && [string equal -length $n $s "-query"]} {
dana16d1062010-09-28 17:37:28 +0000962 set tclquery [list execsql [lindex $switches [incr i]]]
963 } elseif {$n>=2 && [string equal -length $n $s "-tclquery"]} {
964 set tclquery [lindex $switches [incr i]]
danaf1dcab2010-09-20 19:17:53 +0000965 } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} {
dan51f06982010-09-18 19:00:12 +0000966 set errfmt [lindex $switches [incr i]]
danaf7626f2010-09-23 18:47:36 +0000967 } elseif {$n>=2 && [string equal -length $n $s "-repair"]} {
968 set repair [lindex $switches [incr i]]
dan51f06982010-09-18 19:00:12 +0000969 } elseif {$n>=2 && [string equal -length $n $s "-count"]} {
970 set countonly 1
971 } else {
972 error "unknown switch: $s"
973 }
974 }
975
976 if {$countonly && $errfmt!=""} {
977 error "Cannot use -count and -errorformat together"
978 }
979 set nTestlist [llength $testlist]
980 if {$nTestlist%3 || $nTestlist==0 } {
981 error "SELECT test list contains [llength $testlist] elements"
982 }
983
danaf7626f2010-09-23 18:47:36 +0000984 eval $repair
dan51f06982010-09-18 19:00:12 +0000985 foreach {tn sql res} $testlist {
dana16d1062010-09-28 17:37:28 +0000986 if {$tclquery != ""} {
danaf1dcab2010-09-20 19:17:53 +0000987 execsql $sql
dana16d1062010-09-28 17:37:28 +0000988 uplevel do_test ${prefix}.$tn [list $tclquery] [list [list {*}$res]]
989 } elseif {$countonly} {
dan51f06982010-09-18 19:00:12 +0000990 set nRow 0
991 db eval $sql {incr nRow}
992 uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res]
993 } elseif {$errfmt==""} {
994 uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]]
995 } else {
996 set res [list 1 [string trim [format $errfmt {*}$res]]]
997 uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res]
998 }
danaf7626f2010-09-23 18:47:36 +0000999 eval $repair
dan51f06982010-09-18 19:00:12 +00001000 }
danaf7626f2010-09-23 18:47:36 +00001001
dan51f06982010-09-18 19:00:12 +00001002}
1003
danb04757a2010-09-21 16:59:16 +00001004proc delete_all_data {} {
1005 db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} {
1006 db eval "DELETE FROM '[string map {' ''} $t]'"
1007 }
1008}
drh348784e2000-05-29 20:41:49 +00001009
mistachkin1d406e02013-08-29 01:09:14 +00001010# Run an SQL script.
drhb62c3352006-11-23 09:39:16 +00001011# Return the number of microseconds per statement.
1012#
drh3590f152006-11-23 21:09:10 +00001013proc speed_trial {name numstmt units sql} {
dan17c08232015-06-09 15:58:28 +00001014 output2 -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +00001015 flush stdout
1016 set speed [time {sqlite3_exec_nr db $sql}]
1017 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +00001018 if {$tm == 0} {
1019 set rate [format %20s "many"]
1020 } else {
1021 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
1022 }
drh3590f152006-11-23 21:09:10 +00001023 set u2 $units/s
dan17c08232015-06-09 15:58:28 +00001024 output2 [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +00001025 global total_time
1026 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +00001027 lappend ::speed_trial_times $name $tm
drhdd924312007-03-31 22:34:16 +00001028}
drh45c236d2008-03-22 01:08:00 +00001029proc speed_trial_tcl {name numstmt units script} {
dan17c08232015-06-09 15:58:28 +00001030 output2 -nonewline [format {%-21.21s } $name...]
drh45c236d2008-03-22 01:08:00 +00001031 flush stdout
1032 set speed [time {eval $script}]
1033 set tm [lindex $speed 0]
1034 if {$tm == 0} {
1035 set rate [format %20s "many"]
1036 } else {
1037 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
1038 }
1039 set u2 $units/s
dan17c08232015-06-09 15:58:28 +00001040 output2 [format {%12d uS %s %s} $tm $rate $u2]
drh45c236d2008-03-22 01:08:00 +00001041 global total_time
1042 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +00001043 lappend ::speed_trial_times $name $tm
drh45c236d2008-03-22 01:08:00 +00001044}
drhdd924312007-03-31 22:34:16 +00001045proc speed_trial_init {name} {
1046 global total_time
1047 set total_time 0
dana975b592010-10-08 16:09:43 +00001048 set ::speed_trial_times [list]
drhbb810a92010-07-03 12:00:53 +00001049 sqlite3 versdb :memory:
1050 set vers [versdb one {SELECT sqlite_source_id()}]
1051 versdb close
dan17c08232015-06-09 15:58:28 +00001052 output2 "SQLite $vers"
drhdd924312007-03-31 22:34:16 +00001053}
1054proc speed_trial_summary {name} {
1055 global total_time
dan17c08232015-06-09 15:58:28 +00001056 output2 [format {%-21.21s %12d uS TOTAL} $name $total_time]
dana975b592010-10-08 16:09:43 +00001057
1058 if { 0 } {
1059 sqlite3 versdb :memory:
1060 set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0]
1061 versdb close
dan17c08232015-06-09 15:58:28 +00001062 output2 "CREATE TABLE IF NOT EXISTS time(version, script, test, us);"
dana975b592010-10-08 16:09:43 +00001063 foreach {test us} $::speed_trial_times {
dan17c08232015-06-09 15:58:28 +00001064 output2 "INSERT INTO time VALUES('$vers', '$name', '$test', $us);"
dana975b592010-10-08 16:09:43 +00001065 }
1066 }
drhb62c3352006-11-23 09:39:16 +00001067}
1068
drh348784e2000-05-29 20:41:49 +00001069# Run this routine last
1070#
1071proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +00001072 catch {db close}
dane8559832014-07-31 17:35:40 +00001073 catch {db1 close}
danc1a60c52010-06-07 14:28:16 +00001074 catch {db2 close}
1075 catch {db3 close}
1076 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +00001077}
1078proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +00001079 global sqlite_open_file_count
1080
1081 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +00001082
drh6e142f52000-06-08 13:36:40 +00001083 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +00001084 catch {db2 close}
1085 catch {db3 close}
1086
drh9bc54492007-10-23 14:49:59 +00001087 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +00001088 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +00001089 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +00001090 db close
drh984bfaa2008-03-19 16:08:53 +00001091 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +00001092
drh3a7fb7c2007-08-10 16:41:08 +00001093 sqlite3_soft_heap_limit 0
danc1a60c52010-06-07 14:28:16 +00001094 set nTest [incr_ntest]
1095 set nErr [set_test_counter errors]
1096
drhef866372013-05-22 20:49:02 +00001097 set nKnown 0
1098 if {[file readable known-problems.txt]} {
1099 set fd [open known-problems.txt]
1100 set content [read $fd]
1101 close $fd
1102 foreach x $content {set known_error($x) 1}
1103 foreach x [set_test_counter fail_list] {
1104 if {[info exists known_error($x)]} {incr nKnown}
1105 }
1106 }
1107 if {$nKnown>0} {
dan17c08232015-06-09 15:58:28 +00001108 output2 "[expr {$nErr-$nKnown}] new errors and $nKnown known errors\
drhef866372013-05-22 20:49:02 +00001109 out of $nTest tests"
1110 } else {
drh72bf6a32016-01-07 02:06:55 +00001111 set cpuinfo {}
1112 if {[catch {exec hostname} hname]==0} {set cpuinfo [string trim $hname]}
1113 append cpuinfo " $::tcl_platform(os)"
1114 append cpuinfo " [expr {$::tcl_platform(pointerSize)*8}]-bit"
1115 append cpuinfo " [string map {E -e} $::tcl_platform(byteOrder)]"
1116 output2 "SQLite [sqlite3 -sourceid]"
1117 output2 "$nErr errors out of $nTest tests on $cpuinfo"
drhef866372013-05-22 20:49:02 +00001118 }
1119 if {$nErr>$nKnown} {
drh061d35c2015-11-13 00:03:14 +00001120 output2 -nonewline "!Failures on these tests:"
drhef866372013-05-22 20:49:02 +00001121 foreach x [set_test_counter fail_list] {
dan17c08232015-06-09 15:58:28 +00001122 if {![info exists known_error($x)]} {output2 -nonewline " $x"}
drhef866372013-05-22 20:49:02 +00001123 }
dan17c08232015-06-09 15:58:28 +00001124 output2 ""
drhf3a65f72007-08-22 20:18:21 +00001125 }
drhcca17c32013-04-19 12:32:52 +00001126 foreach warning [set_test_counter warn_list] {
dan17c08232015-06-09 15:58:28 +00001127 output2 "Warning: $warning"
drhcca17c32013-04-19 12:32:52 +00001128 }
danielk1977ee8b7992009-03-26 17:13:06 +00001129 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +00001130 if {[llength $omitList]>0} {
dan17c08232015-06-09 15:58:28 +00001131 output2 "Omitted test cases:"
drh521cc842008-04-15 02:36:33 +00001132 set prec {}
1133 foreach {rec} [lsort $omitList] {
1134 if {$rec==$prec} continue
1135 set prec $rec
drhd66b2e02015-11-12 21:42:40 +00001136 output2 [format {. %-12s %s} [lindex $rec 0] [lindex $rec 1]]
drh521cc842008-04-15 02:36:33 +00001137 }
1138 }
drh80788d82006-09-02 14:50:23 +00001139 if {$nErr>0 && ![working_64bit_int]} {
dan17c08232015-06-09 15:58:28 +00001140 output2 "******************************************************************"
1141 output2 "N.B.: The version of TCL that you used to build this test harness"
1142 output2 "is defective in that it does not support 64-bit integers. Some or"
1143 output2 "all of the test failures above might be a result from this defect"
1144 output2 "in your TCL build."
1145 output2 "******************************************************************"
drhdb25e382001-03-15 18:21:22 +00001146 }
danc1a60c52010-06-07 14:28:16 +00001147 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +00001148 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +00001149 }
drh94e92032003-02-16 22:21:32 +00001150 if {$sqlite_open_file_count} {
dan17c08232015-06-09 15:58:28 +00001151 output2 "$sqlite_open_file_count files were left open"
drh94e92032003-02-16 22:21:32 +00001152 incr nErr
1153 }
drheafc43b2010-07-26 18:43:40 +00001154 if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 ||
1155 [sqlite3_memory_used]>0} {
dan17c08232015-06-09 15:58:28 +00001156 output2 "Unfreed memory: [sqlite3_memory_used] bytes in\
drheafc43b2010-07-26 18:43:40 +00001157 [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations"
drhf3a65f72007-08-22 20:18:21 +00001158 incr nErr
drh2d7636e2008-02-16 16:21:45 +00001159 ifcapable memdebug||mem5||(mem3&&debug) {
dan17c08232015-06-09 15:58:28 +00001160 output2 "Writing unfreed memory log to \"./memleak.txt\""
drh4a50aac2007-08-23 02:47:53 +00001161 sqlite3_memdebug_dump ./memleak.txt
1162 }
drhf3a65f72007-08-22 20:18:21 +00001163 } else {
dan17c08232015-06-09 15:58:28 +00001164 output2 "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +00001165 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +00001166 sqlite3_memdebug_dump ./memusage.txt
1167 }
drhf3a65f72007-08-22 20:18:21 +00001168 }
drhf7141992008-06-19 00:16:08 +00001169 show_memstats
dan17c08232015-06-09 15:58:28 +00001170 output2 "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
1171 output2 "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +00001172 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
dan17c08232015-06-09 15:58:28 +00001173 output2 "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
danielk1977a7a8e142008-02-13 18:25:27 +00001174 }
danc1a60c52010-06-07 14:28:16 +00001175 if {$::cmdlinearg(malloctrace)} {
dan17c08232015-06-09 15:58:28 +00001176 output2 "Writing mallocs.sql..."
danielk197735754ac2008-03-21 17:29:37 +00001177 memdebug_log_sql
1178 sqlite3_memdebug_log stop
1179 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +00001180
1181 if {[sqlite3_memory_used]>0} {
dan17c08232015-06-09 15:58:28 +00001182 output2 "Writing leaks.sql..."
danielk1977dbdc4d42008-03-28 07:42:53 +00001183 sqlite3_memdebug_log sync
1184 memdebug_log_sql leaks.sql
1185 }
danielk197735754ac2008-03-21 17:29:37 +00001186 }
drhd9910fe2006-10-04 11:55:49 +00001187 foreach f [glob -nocomplain test.db-*-journal] {
mistachkinfda06be2011-08-02 00:57:34 +00001188 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +00001189 }
1190 foreach f [glob -nocomplain test.db-mj*] {
mistachkinfda06be2011-08-02 00:57:34 +00001191 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +00001192 }
drhdb25e382001-03-15 18:21:22 +00001193 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +00001194}
1195
drhf7141992008-06-19 00:16:08 +00001196# Display memory statistics for analysis and debugging purposes.
1197#
1198proc show_memstats {} {
1199 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +00001200 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
1201 set val [format {now %10d max %10d max-size %10d} \
1202 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
dan17c08232015-06-09 15:58:28 +00001203 output1 "Memory used: $val"
drheafc43b2010-07-26 18:43:40 +00001204 set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0]
1205 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
dan17c08232015-06-09 15:58:28 +00001206 output1 "Allocation count: $val"
drhf7141992008-06-19 00:16:08 +00001207 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +00001208 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
1209 set val [format {now %10d max %10d max-size %10d} \
1210 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
dan17c08232015-06-09 15:58:28 +00001211 output1 "Page-cache used: $val"
drhf7141992008-06-19 00:16:08 +00001212 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
1213 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
dan17c08232015-06-09 15:58:28 +00001214 output1 "Page-cache overflow: $val"
drhf7141992008-06-19 00:16:08 +00001215 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
1216 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
dan17c08232015-06-09 15:58:28 +00001217 output1 "Scratch memory used: $val"
drhf7141992008-06-19 00:16:08 +00001218 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +00001219 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
1220 set val [format {now %10d max %10d max-size %10d} \
1221 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
dan17c08232015-06-09 15:58:28 +00001222 output1 "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +00001223 ifcapable yytrackmaxstackdepth {
1224 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
1225 set val [format { max %10d} [lindex $x 2]]
dan17c08232015-06-09 15:58:28 +00001226 output2 "Parser stack depth: $val"
drhec424a52008-07-25 15:39:03 +00001227 }
drhf7141992008-06-19 00:16:08 +00001228}
1229
drh348784e2000-05-29 20:41:49 +00001230# A procedure to execute SQL
1231#
drhc4a3c772001-04-04 11:48:57 +00001232proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +00001233 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +00001234 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +00001235}
drh3c2aeae2014-01-24 14:37:44 +00001236proc execsql_timed {sql {db db}} {
1237 set tm [time {
1238 set x [uplevel [list $db eval $sql]]
1239 } 1]
1240 set tm [lindex $tm 0]
dan17c08232015-06-09 15:58:28 +00001241 output1 -nonewline " ([expr {$tm*0.001}]ms) "
drh3c2aeae2014-01-24 14:37:44 +00001242 set x
1243}
drh3aadb2e2000-05-31 17:59:25 +00001244
drhadbca9c2001-09-27 15:11:53 +00001245# Execute SQL and catch exceptions.
1246#
1247proc catchsql {sql {db db}} {
1248 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +00001249 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +00001250 lappend r $msg
1251 return $r
1252}
1253
drh04096482001-11-09 22:41:44 +00001254# Do an VDBE code dump on the SQL given
1255#
1256proc explain {sql {db db}} {
mistachkineeb31ff2015-06-10 23:02:38 +00001257 output2 ""
1258 output2 "addr opcode p1 p2 p3 p4 p5 #"
1259 output2 "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +00001260 $db eval "explain $sql" {} {
mistachkineeb31ff2015-06-10 23:02:38 +00001261 output2 [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
danielk1977287fb612008-01-04 19:10:28 +00001262 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
1263 ]
drh04096482001-11-09 22:41:44 +00001264 }
1265}
1266
dane83267d2013-11-05 14:19:22 +00001267proc explain_i {sql {db db}} {
mistachkineeb31ff2015-06-10 23:02:38 +00001268 output2 ""
1269 output2 "addr opcode p1 p2 p3 p4 p5 #"
1270 output2 "---- ------------ ------ ------ ------ ---------------- -- -"
dane83267d2013-11-05 14:19:22 +00001271
dane83267d2013-11-05 14:19:22 +00001272
dan5c663c32013-11-06 14:52:40 +00001273 # Set up colors for the different opcodes. Scheme is as follows:
1274 #
1275 # Red: Opcodes that write to a b-tree.
1276 # Blue: Opcodes that reposition or seek a cursor.
1277 # Green: The ResultRow opcode.
1278 #
dandea63f22014-03-03 16:48:47 +00001279 if { [catch {fconfigure stdout -mode}]==0 } {
1280 set R "\033\[31;1m" ;# Red fg
1281 set G "\033\[32;1m" ;# Green fg
1282 set B "\033\[34;1m" ;# Red fg
1283 set D "\033\[39;0m" ;# Default fg
1284 } else {
1285 set R ""
1286 set G ""
1287 set B ""
1288 set D ""
1289 }
dan5c663c32013-11-06 14:52:40 +00001290 foreach opcode {
1291 Seek SeekGe SeekGt SeekLe SeekLt NotFound Last Rewind
dane3ab7292013-11-12 12:30:09 +00001292 NoConflict Next Prev VNext VPrev VFilter
drhf45f2322014-03-23 17:45:03 +00001293 SorterSort SorterNext
dan5c663c32013-11-06 14:52:40 +00001294 } {
1295 set color($opcode) $B
1296 }
1297 foreach opcode {ResultRow} {
1298 set color($opcode) $G
1299 }
1300 foreach opcode {IdxInsert Insert Delete IdxDelete} {
1301 set color($opcode) $R
1302 }
1303
dan427ebba2013-11-05 16:39:31 +00001304 set bSeenGoto 0
dane83267d2013-11-05 14:19:22 +00001305 $db eval "explain $sql" {} {
1306 set x($addr) 0
1307 set op($addr) $opcode
1308
dan427ebba2013-11-05 16:39:31 +00001309 if {$opcode == "Goto" && ($bSeenGoto==0 || ($p2 > $addr+10))} {
1310 set linebreak($p2) 1
1311 set bSeenGoto 1
dane83267d2013-11-05 14:19:22 +00001312 }
1313
dane3ab7292013-11-12 12:30:09 +00001314 if {$opcode=="Next" || $opcode=="Prev"
1315 || $opcode=="VNext" || $opcode=="VPrev"
drhf45f2322014-03-23 17:45:03 +00001316 || $opcode=="SorterNext"
dane3ab7292013-11-12 12:30:09 +00001317 } {
dane83267d2013-11-05 14:19:22 +00001318 for {set i $p2} {$i<$addr} {incr i} {
1319 incr x($i) 2
1320 }
1321 }
1322
1323 if {$opcode == "Goto" && $p2<$addr && $op($p2)=="Yield"} {
1324 for {set i [expr $p2+1]} {$i<$addr} {incr i} {
1325 incr x($i) 2
1326 }
1327 }
dan427ebba2013-11-05 16:39:31 +00001328
1329 if {$opcode == "Halt" && $comment == "End of coroutine"} {
1330 set linebreak([expr $addr+1]) 1
1331 }
dane83267d2013-11-05 14:19:22 +00001332 }
1333
1334 $db eval "explain $sql" {} {
dan427ebba2013-11-05 16:39:31 +00001335 if {[info exists linebreak($addr)]} {
mistachkineeb31ff2015-06-10 23:02:38 +00001336 output2 ""
dane83267d2013-11-05 14:19:22 +00001337 }
1338 set I [string repeat " " $x($addr)]
dan5c663c32013-11-06 14:52:40 +00001339
1340 set col ""
1341 catch { set col $color($opcode) }
1342
mistachkineeb31ff2015-06-10 23:02:38 +00001343 output2 [format {%-4d %s%s%-12.12s%s %-6d %-6d %-6d % -17s %s %s} \
dan5c663c32013-11-06 14:52:40 +00001344 $addr $I $col $opcode $D $p1 $p2 $p3 $p4 $p5 $comment
dane83267d2013-11-05 14:19:22 +00001345 ]
dane83267d2013-11-05 14:19:22 +00001346 }
mistachkineeb31ff2015-06-10 23:02:38 +00001347 output2 "---- ------------ ------ ------ ------ ---------------- -- -"
dane83267d2013-11-05 14:19:22 +00001348}
1349
drhdafc0ce2008-04-17 19:14:02 +00001350# Show the VDBE program for an SQL statement but omit the Trace
1351# opcode at the beginning. This procedure can be used to prove
1352# that different SQL statements generate exactly the same VDBE code.
1353#
1354proc explain_no_trace {sql} {
1355 set tr [db eval "EXPLAIN $sql"]
1356 return [lrange $tr 7 end]
1357}
1358
drh3aadb2e2000-05-31 17:59:25 +00001359# Another procedure to execute SQL. This one includes the field
1360# names in the returned list.
1361#
1362proc execsql2 {sql} {
1363 set result {}
1364 db eval $sql data {
1365 foreach f $data(*) {
1366 lappend result $f $data($f)
1367 }
1368 }
1369 return $result
1370}
drh17a68932001-01-31 13:28:08 +00001371
mistachkin1d406e02013-08-29 01:09:14 +00001372# Use a temporary in-memory database to execute SQL statements
1373#
1374proc memdbsql {sql} {
1375 sqlite3 memdb :memory:
1376 set result [memdb eval $sql]
1377 memdb close
1378 return $result
1379}
1380
drhdde85d92003-03-01 19:45:34 +00001381# Use the non-callback API to execute multiple SQL statements
1382#
1383proc stepsql {dbptr sql} {
1384 set sql [string trim $sql]
1385 set r 0
1386 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +00001387 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +00001388 return [list 1 $vm]
1389 }
1390 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +00001391# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
1392# foreach v $VAL {lappend r $v}
1393# }
1394 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
1395 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
1396 lappend r [sqlite3_column_text $vm $i]
1397 }
drhdde85d92003-03-01 19:45:34 +00001398 }
danielk1977106bb232004-05-21 10:08:53 +00001399 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +00001400 return [list 1 $errmsg]
1401 }
1402 }
1403 return $r
1404}
1405
drh21504322002-06-25 13:16:02 +00001406# Do an integrity check of the entire database
1407#
danielk197704103022009-02-03 16:51:24 +00001408proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +00001409 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +00001410 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +00001411 }
1412}
1413
drh433dccf2013-02-09 15:37:11 +00001414# Check the extended error code
1415#
1416proc verify_ex_errcode {name expected {db db}} {
1417 do_test $name [list sqlite3_extended_errcode $db] $expected
1418}
1419
danc6055c72011-04-28 18:46:46 +00001420
1421# Return true if the SQL statement passed as the second argument uses a
1422# statement transaction.
1423#
1424proc sql_uses_stmt {db sql} {
1425 set stmt [sqlite3_prepare $db $sql -1 dummy]
1426 set uses [uses_stmt_journal $stmt]
1427 sqlite3_finalize $stmt
1428 return $uses
1429}
1430
danielk1977db4e8862008-01-16 08:24:46 +00001431proc fix_ifcapable_expr {expr} {
1432 set ret ""
1433 set state 0
1434 for {set i 0} {$i < [string length $expr]} {incr i} {
1435 set char [string range $expr $i $i]
1436 set newstate [expr {[string is alnum $char] || $char eq "_"}]
1437 if {$newstate && !$state} {
1438 append ret {$::sqlite_options(}
1439 }
1440 if {!$newstate && $state} {
1441 append ret )
1442 }
1443 append ret $char
1444 set state $newstate
1445 }
1446 if {$state} {append ret )}
1447 return $ret
1448}
1449
mistachkinc60941f2012-09-13 01:51:02 +00001450# Returns non-zero if the capabilities are present; zero otherwise.
1451#
1452proc capable {expr} {
1453 set e [fix_ifcapable_expr $expr]; return [expr ($e)]
1454}
1455
drh27d258a2004-10-30 20:23:09 +00001456# Evaluate a boolean expression of capabilities. If true, execute the
1457# code. Omit the code if false.
1458#
danielk19773e8c37e2005-01-21 03:12:14 +00001459proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +00001460 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
1461 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +00001462 if ($e2) {
1463 set c [catch {uplevel 1 $code} r]
1464 } else {
1465 set c [catch {uplevel 1 $elsecode} r]
1466 }
1467 return -code $c $r
drh21504322002-06-25 13:16:02 +00001468}
danielk197745901d62004-11-10 15:27:38 +00001469
danielk1977aca790a2005-01-13 11:07:52 +00001470# This proc execs a seperate process that crashes midway through executing
1471# the SQL script $sql on database test.db.
1472#
1473# The crash occurs during a sync() of file $crashfile. When the crash
1474# occurs a random subset of all unsynced writes made by the process are
1475# written into the files on disk. Argument $crashdelay indicates the
1476# number of file syncs to wait before crashing.
1477#
1478# The return value is a list of two elements. The first element is a
1479# boolean, indicating whether or not the process actually crashed or
1480# reported some other error. The second element in the returned list is the
1481# error message. This is "child process exited abnormally" if the crash
mistachkin48864df2013-03-21 21:20:32 +00001482# occurred.
danielk1977aca790a2005-01-13 11:07:52 +00001483#
danielk1977f8940ae2007-08-23 11:07:10 +00001484# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +00001485#
1486proc crashsql {args} {
danielk197759a33f92007-03-17 10:26:59 +00001487
1488 set blocksize ""
1489 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +00001490 set prngseed 0
dan999cd082013-12-09 20:42:03 +00001491 set opendb { sqlite3 db test.db -vfs crash }
drhf8587402008-01-08 16:03:49 +00001492 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +00001493 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +00001494 set dc ""
danielk197759a33f92007-03-17 10:26:59 +00001495 set sql [lindex $args end]
mistachkin1d406e02013-08-29 01:09:14 +00001496
danielk197759a33f92007-03-17 10:26:59 +00001497 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
1498 set z [lindex $args $ii]
1499 set n [string length $z]
1500 set z2 [lindex $args [expr $ii+1]]
1501
1502 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
dan999cd082013-12-09 20:42:03 +00001503 elseif {$n>1 && [string first $z -opendb]==0} {set opendb $z2} \
drhcb1f0f62008-01-08 15:18:52 +00001504 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +00001505 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +00001506 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +00001507 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +00001508 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +00001509 else { error "Unrecognized option: $z" }
1510 }
1511
1512 if {$crashfile eq ""} {
1513 error "Compulsory option -file missing"
1514 }
1515
mistachkin1d406e02013-08-29 01:09:14 +00001516 # $crashfile gets compared to the native filename in
shanehf2c08822010-07-08 16:30:44 +00001517 # cfSync(), which can be different then what TCL uses by
1518 # default, so here we force it to the "nativename" format.
mistachkinf8a78462012-03-08 20:00:36 +00001519 set cfile [string map {\\ \\\\} [file nativename [file join [get_pwd] $crashfile]]]
danielk1977aca790a2005-01-13 11:07:52 +00001520
1521 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +00001522 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +00001523 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +00001524 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk1977933bbd62007-03-17 07:22:42 +00001525
1526 # This block sets the cache size of the main database to 10
1527 # pages. This is done in case the build is configured to omit
1528 # "PRAGMA cache_size".
danec16d982015-04-17 08:36:05 +00001529 if {$opendb!=""} {
1530 puts $f $opendb
1531 puts $f {db eval {SELECT * FROM sqlite_master;}}
1532 puts $f {set bt [btree_from_db db]}
1533 puts $f {btree_set_cache_size $bt 10}
1534 }
dan999cd082013-12-09 20:42:03 +00001535
drhcb1f0f62008-01-08 15:18:52 +00001536 if {$prngseed} {
1537 set seed [expr {$prngseed%10007+1}]
1538 # puts seed=$seed
1539 puts $f "db eval {SELECT randomblob($seed)}"
1540 }
danielk1977933bbd62007-03-17 07:22:42 +00001541
drhf8587402008-01-08 16:03:49 +00001542 if {[string length $tclbody]>0} {
1543 puts $f $tclbody
1544 }
1545 if {[string length $sql]>0} {
1546 puts $f "db eval {"
1547 puts $f "$sql"
1548 puts $f "}"
1549 }
danielk1977aca790a2005-01-13 11:07:52 +00001550 close $f
danielk1977aca790a2005-01-13 11:07:52 +00001551 set r [catch {
drh9c06c952005-11-26 00:25:00 +00001552 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +00001553 } msg]
mistachkin1d406e02013-08-29 01:09:14 +00001554
shanehf2c08822010-07-08 16:30:44 +00001555 # Windows/ActiveState TCL returns a slightly different
1556 # error message. We map that to the expected message
1557 # so that we don't have to change all of the test
1558 # cases.
1559 if {$::tcl_platform(platform)=="windows"} {
1560 if {$msg=="child killed: unknown signal"} {
1561 set msg "child process exited abnormally"
1562 }
1563 }
mistachkin1d406e02013-08-29 01:09:14 +00001564
danielk1977aca790a2005-01-13 11:07:52 +00001565 lappend r $msg
1566}
1567
danb88e24f2013-02-23 18:58:11 +00001568proc run_ioerr_prep {} {
1569 set ::sqlite_io_error_pending 0
1570 catch {db close}
1571 catch {db2 close}
1572 catch {forcedelete test.db}
1573 catch {forcedelete test.db-journal}
1574 catch {forcedelete test2.db}
1575 catch {forcedelete test2.db-journal}
1576 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
1577 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
1578 if {[info exists ::ioerropts(-tclprep)]} {
1579 eval $::ioerropts(-tclprep)
1580 }
1581 if {[info exists ::ioerropts(-sqlprep)]} {
1582 execsql $::ioerropts(-sqlprep)
1583 }
1584 expr 0
1585}
1586
danielk197732554c12005-01-22 03:39:39 +00001587# Usage: do_ioerr_test <test number> <options...>
1588#
1589# This proc is used to implement test cases that check that IO errors
mistachkin1d406e02013-08-29 01:09:14 +00001590# are correctly handled. The first argument, <test number>, is an integer
danielk197732554c12005-01-22 03:39:39 +00001591# used to name the tests executed by this proc. Options are as follows:
1592#
1593# -tclprep TCL script to run to prepare test.
1594# -sqlprep SQL script to run to prepare test.
1595# -tclbody TCL script to run with IO error simulation.
1596# -sqlbody TCL script to run with IO error simulation.
1597# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +00001598# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +00001599# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +00001600# -start Value of 'N' to begin with (default 1)
1601#
1602# -cksum Boolean. If true, test that the database does
1603# not change during the execution of the test case.
1604#
1605proc do_ioerr_test {testname args} {
1606
danielk197732554c12005-01-22 03:39:39 +00001607 set ::ioerropts(-start) 1
1608 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +00001609 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +00001610 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +00001611 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +00001612 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +00001613 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +00001614 array set ::ioerropts $args
1615
danielk197747cd39c2008-05-12 12:41:15 +00001616 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
1617 # a couple of obscure IO errors that do not return them.
1618 set ::ioerropts(-erc) 0
mistachkin1d406e02013-08-29 01:09:14 +00001619
danb88e24f2013-02-23 18:58:11 +00001620 # Create a single TCL script from the TCL and SQL specified
1621 # as the body of the test.
1622 set ::ioerrorbody {}
1623 if {[info exists ::ioerropts(-tclbody)]} {
1624 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
1625 }
1626 if {[info exists ::ioerropts(-sqlbody)]} {
1627 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
1628 }
1629
1630 save_prng_state
1631 if {$::ioerropts(-cksum)} {
1632 run_ioerr_prep
1633 eval $::ioerrorbody
1634 set ::goodcksum [cksum]
1635 }
danielk197747cd39c2008-05-12 12:41:15 +00001636
danielk197732554c12005-01-22 03:39:39 +00001637 set ::go 1
drh93aed5a2008-01-16 17:46:38 +00001638 #reset_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +00001639 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +00001640 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +00001641 incr ::ioerropts(-count) -1
1642 if {$::ioerropts(-count)<0} break
mistachkin1d406e02013-08-29 01:09:14 +00001643
danielk197732554c12005-01-22 03:39:39 +00001644 # Skip this IO error if it was specified with the "-exclude" option.
1645 if {[info exists ::ioerropts(-exclude)]} {
1646 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
1647 }
danielk1977861f7452008-06-05 11:39:11 +00001648 if {$::ioerropts(-restoreprng)} {
1649 restore_prng_state
1650 }
danielk197732554c12005-01-22 03:39:39 +00001651
mistachkin1d406e02013-08-29 01:09:14 +00001652 # Delete the files test.db and test2.db, then execute the TCL and
danielk197732554c12005-01-22 03:39:39 +00001653 # SQL (in that order) to prepare for the test case.
1654 do_test $testname.$n.1 {
danb88e24f2013-02-23 18:58:11 +00001655 run_ioerr_prep
danielk197732554c12005-01-22 03:39:39 +00001656 } {0}
1657
1658 # Read the 'checksum' of the database.
1659 if {$::ioerropts(-cksum)} {
danb88e24f2013-02-23 18:58:11 +00001660 set ::checksum [cksum]
danielk197732554c12005-01-22 03:39:39 +00001661 }
drh93aed5a2008-01-16 17:46:38 +00001662
danielk197732554c12005-01-22 03:39:39 +00001663 # Set the Nth IO error to fail.
1664 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +00001665 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +00001666 set ::sqlite_io_error_pending $n
1667 }] $n
danielk197732554c12005-01-22 03:39:39 +00001668
danb88e24f2013-02-23 18:58:11 +00001669 # Execute the TCL script created for the body of this test. If
mistachkin1d406e02013-08-29 01:09:14 +00001670 # at least N IO operations performed by SQLite as a result of
danb88e24f2013-02-23 18:58:11 +00001671 # the script, the Nth will fail.
danielk197732554c12005-01-22 03:39:39 +00001672 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +00001673 set ::sqlite_io_error_hit 0
1674 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +00001675 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +00001676 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +00001677 set rc [sqlite3_errcode $::DB]
1678 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +00001679 # If we are in extended result code mode, make sure all of the
1680 # IOERRs we get back really do have their extended code values.
1681 # If an extended result code is returned, the sqlite3_errcode
1682 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
1683 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +00001684 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
1685 return $rc
1686 }
1687 } else {
drh5f7b5bf2007-04-19 12:30:54 +00001688 # If we are not in extended result code mode, make sure no
1689 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +00001690 if {[regexp {\+\d} $rc]} {
1691 return $rc
1692 }
1693 }
drh93aed5a2008-01-16 17:46:38 +00001694 # The test repeats as long as $::go is non-zero. $::go starts out
1695 # as 1. When a test runs to completion without hitting an I/O
1696 # error, that means there is no point in continuing with this test
1697 # case so set $::go to zero.
1698 #
1699 if {$::sqlite_io_error_pending>0} {
1700 set ::go 0
1701 set q 0
1702 set ::sqlite_io_error_pending 0
1703 } else {
1704 set q 1
1705 }
1706
drhc9ac5ca2005-11-04 22:03:30 +00001707 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +00001708 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
1709 set r 1
1710 }
danielk1977f2fa8312006-01-24 13:09:33 +00001711 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +00001712
1713 # One of two things must have happened. either
1714 # 1. We never hit the IO error and the SQL returned OK
1715 # 2. An IO error was hit and the SQL failed
1716 #
dand41a29a2010-05-06 15:56:28 +00001717 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +00001718 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +00001719 } {1}
1720
danielk197780daec62008-05-12 10:57:02 +00001721 set ::sqlite_io_error_hit 0
1722 set ::sqlite_io_error_pending 0
1723
mistachkin1d406e02013-08-29 01:09:14 +00001724 # Check that no page references were leaked. There should be
1725 # a single reference if there is still an active transaction,
danielk197752b472a2008-05-05 16:23:55 +00001726 # or zero otherwise.
1727 #
danielk197781620542008-06-07 05:19:37 +00001728 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
mistachkin1d406e02013-08-29 01:09:14 +00001729 # locks are established on database files (i.e. if the error
danielk197781620542008-06-07 05:19:37 +00001730 # occurs while attempting to detect a hot-journal file), then
1731 # there may 0 page references and an active transaction according
1732 # to [sqlite3_get_autocommit].
1733 #
danielk19774abd5442008-05-05 15:26:50 +00001734 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +00001735 do_test $testname.$n.4 {
1736 set bt [btree_from_db db]
1737 db_enter db
1738 array set stats [btree_pager_stats $bt]
1739 db_leave db
danielk197781620542008-06-07 05:19:37 +00001740 set nRef $stats(ref)
1741 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
1742 } {1}
danielk19774abd5442008-05-05 15:26:50 +00001743 }
1744
mistachkin1d406e02013-08-29 01:09:14 +00001745 # If there is an open database handle and no open transaction,
danielk197752b472a2008-05-05 16:23:55 +00001746 # and the pager is not running in exclusive-locking mode,
1747 # check that the pager is in "unlocked" state. Theoretically,
1748 # if a call to xUnlock() failed due to an IO error the underlying
1749 # file may still be locked.
1750 #
1751 ifcapable pragma {
1752 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +00001753 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +00001754 && [db one {pragma locking_mode}] eq "normal"
1755 && [sqlite3_get_autocommit db]
1756 } {
1757 do_test $testname.$n.5 {
1758 set bt [btree_from_db db]
1759 db_enter db
1760 array set stats [btree_pager_stats $bt]
1761 db_leave db
1762 set stats(state)
1763 } 0
1764 }
1765 }
1766
mistachkin48864df2013-03-21 21:20:32 +00001767 # If an IO error occurred, then the checksum of the database should
danielk197732554c12005-01-22 03:39:39 +00001768 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +00001769 #
drh1aa5af12008-03-07 19:51:14 +00001770 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +00001771 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +00001772 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +00001773 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +00001774 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danb88e24f2013-02-23 18:58:11 +00001775 set nowcksum [cksum]
1776 set res [expr {$nowcksum==$::checksum || $nowcksum==$::goodcksum}]
1777 if {$res==0} {
dan17c08232015-06-09 15:58:28 +00001778 output2 "now=$nowcksum"
1779 output2 "the=$::checksum"
1780 output2 "fwd=$::goodcksum"
danb88e24f2013-02-23 18:58:11 +00001781 }
1782 set res
1783 } 1
danielk197732554c12005-01-22 03:39:39 +00001784 }
1785
drh1aa5af12008-03-07 19:51:14 +00001786 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +00001787 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +00001788 if {[info exists ::ioerropts(-cleanup)]} {
1789 catch $::ioerropts(-cleanup)
1790 }
danielk197732554c12005-01-22 03:39:39 +00001791 }
1792 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +00001793 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +00001794 unset ::ioerropts
1795}
1796
drh93aed5a2008-01-16 17:46:38 +00001797# Return a checksum based on the contents of the main database associated
1798# with connection $db
danielk197732554c12005-01-22 03:39:39 +00001799#
1800proc cksum {{db db}} {
1801 set txt [$db eval {
1802 SELECT name, type, sql FROM sqlite_master order by name
1803 }]\n
1804 foreach tbl [$db eval {
1805 SELECT name FROM sqlite_master WHERE type='table' order by name
1806 }] {
1807 append txt [$db eval "SELECT * FROM $tbl"]\n
1808 }
1809 foreach prag {default_synchronous default_cache_size} {
1810 append txt $prag-[$db eval "PRAGMA $prag"]\n
1811 }
1812 set cksum [string length $txt]-[md5 $txt]
1813 # puts $cksum-[file size test.db]
1814 return $cksum
1815}
1816
drh93aed5a2008-01-16 17:46:38 +00001817# Generate a checksum based on the contents of the main and temp tables
1818# database $db. If the checksum of two databases is the same, and the
1819# integrity-check passes for both, the two databases are identical.
1820#
drh1db639c2008-01-17 02:36:28 +00001821proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +00001822 set ret [list]
1823 ifcapable tempdb {
1824 set sql {
1825 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1826 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
1827 SELECT 'sqlite_master' UNION
1828 SELECT 'sqlite_temp_master' ORDER BY 1
1829 }
1830 } else {
1831 set sql {
1832 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1833 SELECT 'sqlite_master' ORDER BY 1
1834 }
1835 }
1836 set tbllist [$db eval $sql]
1837 set txt {}
1838 foreach tbl $tbllist {
1839 append txt [$db eval "SELECT * FROM $tbl"]
1840 }
1841 foreach prag {default_cache_size} {
1842 append txt $prag-[$db eval "PRAGMA $prag"]\n
1843 }
1844 # puts txt=$txt
1845 return [md5 $txt]
1846}
1847
drhdc2c4912009-02-04 22:46:47 +00001848# Generate a checksum based on the contents of a single database with
mistachkin1d406e02013-08-29 01:09:14 +00001849# a database connection. The name of the database is $dbname.
drhdc2c4912009-02-04 22:46:47 +00001850# Examples of $dbname are "temp" or "main".
1851#
1852proc dbcksum {db dbname} {
1853 if {$dbname=="temp"} {
1854 set master sqlite_temp_master
1855 } else {
1856 set master $dbname.sqlite_master
1857 }
1858 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
1859 set txt [$db eval "SELECT * FROM $master"]\n
1860 foreach tab $alltab {
1861 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
1862 }
1863 return [md5 $txt]
1864}
1865
danielk197735754ac2008-03-21 17:29:37 +00001866proc memdebug_log_sql {{filename mallocs.sql}} {
1867
danielk19776f332c12008-03-21 14:22:44 +00001868 set data [sqlite3_memdebug_log dump]
1869 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +00001870 if {$nFrame < 0} { return "" }
1871
danielk197735754ac2008-03-21 17:29:37 +00001872 set database temp
1873
danielk19776ab3a2e2009-02-19 14:39:25 +00001874 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +00001875
1876 set sql ""
1877 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +00001878 set nCall [lindex $e 0]
1879 set nByte [lindex $e 1]
1880 set lStack [lrange $e 2 end]
1881 append sql "INSERT INTO ${database}.malloc VALUES"
1882 append sql "('test', $nCall, $nByte, '$lStack');\n"
1883 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +00001884 set frames($f) 1
1885 }
1886 }
1887
1888 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +00001889 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +00001890
1891 foreach f [array names frames] {
1892 set addr [format %x $f]
1893 set cmd "addr2line -e [info nameofexec] $addr"
1894 set line [eval exec $cmd]
1895 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00001896
1897 set file [lindex [split $line :] 0]
1898 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00001899 }
1900
danielk197735754ac2008-03-21 17:29:37 +00001901 foreach f [array names files] {
1902 set contents ""
1903 catch {
1904 set fd [open $f]
1905 set contents [read $fd]
1906 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001907 }
danielk197735754ac2008-03-21 17:29:37 +00001908 set contents [string map {' ''} $contents]
1909 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00001910 }
danielk19776f332c12008-03-21 14:22:44 +00001911
danielk197735754ac2008-03-21 17:29:37 +00001912 set fd [open $filename w]
1913 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
1914 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001915}
drh93aed5a2008-01-16 17:46:38 +00001916
danf5894502009-10-07 18:41:19 +00001917# Drop all tables in database [db]
1918proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00001919 ifcapable trigger&&foreignkey {
1920 set pk [$db one "PRAGMA foreign_keys"]
1921 $db eval "PRAGMA foreign_keys = OFF"
1922 }
drh9a6ffc82010-02-15 18:03:20 +00001923 foreach {idx name file} [db eval {PRAGMA database_list}] {
1924 if {$idx==1} {
1925 set master sqlite_temp_master
1926 } else {
1927 set master $name.sqlite_master
1928 }
1929 foreach {t type} [$db eval "
1930 SELECT name, type FROM $master
dana16d1062010-09-28 17:37:28 +00001931 WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X'
drh9a6ffc82010-02-15 18:03:20 +00001932 "] {
dana16d1062010-09-28 17:37:28 +00001933 $db eval "DROP $type \"$t\""
drh9a6ffc82010-02-15 18:03:20 +00001934 }
danf5894502009-10-07 18:41:19 +00001935 }
shaneh4e7b32f2009-12-17 22:12:51 +00001936 ifcapable trigger&&foreignkey {
1937 $db eval "PRAGMA foreign_keys = $pk"
1938 }
danf5894502009-10-07 18:41:19 +00001939}
1940
dan71cb5182010-04-26 12:39:03 +00001941#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00001942# If a test script is executed with global variable $::G(perm:name) set to
mistachkin1d406e02013-08-29 01:09:14 +00001943# "wal", then the tests are run in WAL mode. Otherwise, they should be run
1944# in rollback mode. The following Tcl procs are used to make this less
dan430e74c2010-06-07 17:47:26 +00001945# intrusive:
dan71cb5182010-04-26 12:39:03 +00001946#
1947# wal_set_journal_mode ?DB?
1948#
1949# If running a WAL test, execute "PRAGMA journal_mode = wal" using
1950# connection handle DB. Otherwise, this command is a no-op.
1951#
1952# wal_check_journal_mode TESTNAME ?DB?
1953#
1954# If running a WAL test, execute a tests case that fails if the main
1955# database for connection handle DB is not currently a WAL database.
1956# Otherwise (if not running a WAL permutation) this is a no-op.
1957#
1958# wal_is_wal_mode
mistachkin1d406e02013-08-29 01:09:14 +00001959#
dan71cb5182010-04-26 12:39:03 +00001960# Returns true if this test should be run in WAL mode. False otherwise.
mistachkin1d406e02013-08-29 01:09:14 +00001961#
dan71cb5182010-04-26 12:39:03 +00001962proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00001963 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00001964}
1965proc wal_set_journal_mode {{db db}} {
1966 if { [wal_is_wal_mode] } {
1967 $db eval "PRAGMA journal_mode = WAL"
1968 }
1969}
1970proc wal_check_journal_mode {testname {db db}} {
1971 if { [wal_is_wal_mode] } {
1972 $db eval { SELECT * FROM sqlite_master }
1973 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
1974 }
1975}
1976
dan05accd22016-04-27 18:54:49 +00001977proc wal_is_capable {} {
1978 ifcapable !wal { return 0 }
1979 if {[permutation]=="journaltest"} { return 0 }
1980 return 1
1981}
1982
dan430e74c2010-06-07 17:47:26 +00001983proc permutation {} {
1984 set perm ""
1985 catch {set perm $::G(perm:name)}
1986 set perm
1987}
dancb79e512010-08-06 13:50:07 +00001988proc presql {} {
1989 set presql ""
1990 catch {set presql $::G(perm:presql)}
1991 set presql
1992}
dan430e74c2010-06-07 17:47:26 +00001993
danbe7721d2016-02-04 17:31:03 +00001994proc isquick {} {
1995 set ret 0
1996 catch {set ret $::G(isquick)}
1997 set ret
1998}
1999
danc1a60c52010-06-07 14:28:16 +00002000#-------------------------------------------------------------------------
2001#
2002proc slave_test_script {script} {
2003
2004 # Create the interpreter used to run the test script.
2005 interp create tinterp
2006
2007 # Populate some global variables that tester.tcl expects to see.
2008 foreach {var value} [list \
2009 ::argv0 $::argv0 \
2010 ::argv {} \
2011 ::SLAVE 1 \
2012 ] {
2013 interp eval tinterp [list set $var $value]
2014 }
2015
dan17c08232015-06-09 15:58:28 +00002016 # If output is being copied into a file, share the file-descriptor with
2017 # the interpreter.
2018 if {[info exists ::G(output_fd)]} {
2019 interp share {} $::G(output_fd) tinterp
2020 }
2021
danc1a60c52010-06-07 14:28:16 +00002022 # The alias used to access the global test counters.
2023 tinterp alias set_test_counter set_test_counter
2024
2025 # Set up the ::cmdlinearg array in the slave.
2026 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
2027
dan430e74c2010-06-07 17:47:26 +00002028 # Set up the ::G array in the slave.
2029 interp eval tinterp [list array set ::G [array get ::G]]
2030
danc1a60c52010-06-07 14:28:16 +00002031 # Load the various test interfaces implemented in C.
2032 load_testfixture_extensions tinterp
2033
2034 # Run the test script.
2035 interp eval tinterp $script
2036
danea5542d2010-07-06 11:26:15 +00002037 # Check if the interpreter call [run_thread_tests]
2038 if { [interp eval tinterp {info exists ::run_thread_tests_called}] } {
2039 set ::run_thread_tests_called 1
2040 }
2041
danc1a60c52010-06-07 14:28:16 +00002042 # Delete the interpreter used to run the test script.
2043 interp delete tinterp
2044}
2045
dan430e74c2010-06-07 17:47:26 +00002046proc slave_test_file {zFile} {
dan0626dfc2010-06-15 06:56:37 +00002047 set tail [file tail $zFile]
dan430e74c2010-06-07 17:47:26 +00002048
dan6a64d672011-04-04 15:38:16 +00002049 if {[info exists ::G(start:permutation)]} {
2050 if {[permutation] != $::G(start:permutation)} return
2051 unset ::G(start:permutation)
2052 }
2053 if {[info exists ::G(start:file)]} {
2054 if {$tail != $::G(start:file) && $tail!="$::G(start:file).test"} return
2055 unset ::G(start:file)
2056 }
2057
dan92d516a2010-07-05 14:54:48 +00002058 # Remember the value of the shared-cache setting. So that it is possible
2059 # to check afterwards that it was not modified by the test script.
2060 #
2061 ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] }
dan0626dfc2010-06-15 06:56:37 +00002062
dan92d516a2010-07-05 14:54:48 +00002063 # Run the test script in a slave interpreter.
2064 #
danea5542d2010-07-06 11:26:15 +00002065 unset -nocomplain ::run_thread_tests_called
dan0626dfc2010-06-15 06:56:37 +00002066 reset_prng_state
2067 set ::sqlite_open_file_count 0
2068 set time [time { slave_test_script [list source $zFile] }]
2069 set ms [expr [lindex $time 0] / 1000]
2070
dan92d516a2010-07-05 14:54:48 +00002071 # Test that all files opened by the test script were closed. Omit this
2072 # if the test script has "thread" in its name. The open file counter
2073 # is not thread-safe.
dan0626dfc2010-06-15 06:56:37 +00002074 #
danea5542d2010-07-06 11:26:15 +00002075 if {[info exists ::run_thread_tests_called]==0} {
dan92d516a2010-07-05 14:54:48 +00002076 do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0}
2077 }
dan430e74c2010-06-07 17:47:26 +00002078 set ::sqlite_open_file_count 0
2079
mistachkin1d406e02013-08-29 01:09:14 +00002080 # Test that the global "shared-cache" setting was not altered by
dan0626dfc2010-06-15 06:56:37 +00002081 # the test script.
2082 #
mistachkin1d406e02013-08-29 01:09:14 +00002083 ifcapable shared_cache {
dan0626dfc2010-06-15 06:56:37 +00002084 set res [expr {[sqlite3_enable_shared_cache] == $scs}]
2085 do_test ${tail}-sharedcachesetting [list set {} $res] 1
2086 }
dan430e74c2010-06-07 17:47:26 +00002087
dan92d516a2010-07-05 14:54:48 +00002088 # Add some info to the output.
2089 #
dan17c08232015-06-09 15:58:28 +00002090 output2 "Time: $tail $ms ms"
dan430e74c2010-06-07 17:47:26 +00002091 show_memstats
danc1a60c52010-06-07 14:28:16 +00002092}
2093
dan59257dc2010-08-04 11:34:31 +00002094# Open a new connection on database test.db and execute the SQL script
2095# supplied as an argument. Before returning, close the new conection and
2096# restore the 4 byte fields starting at header offsets 28, 92 and 96
2097# to the values they held before the SQL was executed. This simulates
2098# a write by a pre-3.7.0 client.
2099#
2100proc sql36231 {sql} {
2101 set B [hexio_read test.db 92 8]
2102 set A [hexio_read test.db 28 4]
2103 sqlite3 db36231 test.db
2104 catch { db36231 func a_string a_string }
2105 execsql $sql db36231
2106 db36231 close
2107 hexio_write test.db 28 $A
2108 hexio_write test.db 92 $B
2109 return ""
2110}
danf5894502009-10-07 18:41:19 +00002111
danccc7ff42010-11-29 12:06:45 +00002112proc db_save {} {
2113 foreach f [glob -nocomplain sv_test.db*] { forcedelete $f }
2114 foreach f [glob -nocomplain test.db*] {
2115 set f2 "sv_$f"
mistachkinfda06be2011-08-02 00:57:34 +00002116 forcecopy $f $f2
danccc7ff42010-11-29 12:06:45 +00002117 }
2118}
2119proc db_save_and_close {} {
2120 db_save
2121 catch { db close }
2122 return ""
2123}
2124proc db_restore {} {
2125 foreach f [glob -nocomplain test.db*] { forcedelete $f }
2126 foreach f2 [glob -nocomplain sv_test.db*] {
2127 set f [string range $f2 3 end]
mistachkinfda06be2011-08-02 00:57:34 +00002128 forcecopy $f2 $f
danccc7ff42010-11-29 12:06:45 +00002129 }
2130}
2131proc db_restore_and_reopen {{dbfile test.db}} {
2132 catch { db close }
2133 db_restore
2134 sqlite3 db $dbfile
2135}
2136proc db_delete_and_reopen {{file test.db}} {
2137 catch { db close }
mistachkinfda06be2011-08-02 00:57:34 +00002138 foreach f [glob -nocomplain test.db*] { forcedelete $f }
danccc7ff42010-11-29 12:06:45 +00002139 sqlite3 db $file
2140}
2141
dan03bc5252015-07-24 14:17:17 +00002142# Close any connections named [db], [db2] or [db3]. Then use sqlite3_config
2143# to configure the size of the PAGECACHE allocation using the parameters
2144# provided to this command. Save the old PAGECACHE parameters in a global
2145# variable so that [test_restore_config_pagecache] can restore the previous
2146# configuration.
2147#
2148# Before returning, reopen connection [db] on file test.db.
2149#
2150proc test_set_config_pagecache {sz nPg} {
2151 catch {db close}
2152 catch {db2 close}
2153 catch {db3 close}
2154
2155 sqlite3_shutdown
2156 set ::old_pagecache_config [sqlite3_config_pagecache $sz $nPg]
2157 sqlite3_initialize
2158 autoinstall_test_functions
dand716c392015-07-24 18:22:29 +00002159 reset_db
dan03bc5252015-07-24 14:17:17 +00002160}
2161
2162# Close any connections named [db], [db2] or [db3]. Then use sqlite3_config
2163# to configure the size of the PAGECACHE allocation to the size saved in
2164# the global variable by an earlier call to [test_set_config_pagecache].
2165#
2166# Before returning, reopen connection [db] on file test.db.
2167#
2168proc test_restore_config_pagecache {} {
2169 catch {db close}
2170 catch {db2 close}
2171 catch {db3 close}
2172
2173 sqlite3_shutdown
2174 eval sqlite3_config_pagecache $::old_pagecache_config
2175 unset ::old_pagecache_config
2176 sqlite3_initialize
2177 autoinstall_test_functions
2178 sqlite3 db test.db
2179}
2180
dan1e8dae02016-03-19 14:53:36 +00002181proc test_find_binary {nm} {
dan089555c2016-03-15 09:55:44 +00002182 if {$::tcl_platform(platform)=="windows"} {
dan1e8dae02016-03-19 14:53:36 +00002183 set ret "$nm.exe"
dan089555c2016-03-15 09:55:44 +00002184 } else {
dan1e8dae02016-03-19 14:53:36 +00002185 set ret $nm
dan089555c2016-03-15 09:55:44 +00002186 }
2187 set ret [file normalize [file join $::cmdlinearg(TESTFIXTURE_HOME) $ret]]
2188 if {![file executable $ret]} {
2189 finish_test
dan49aed582016-03-19 15:13:59 +00002190 return ""
dan089555c2016-03-15 09:55:44 +00002191 }
2192 return $ret
2193}
2194
danielk197745901d62004-11-10 15:27:38 +00002195# Find the name of the 'shell' executable (e.g. "sqlite3.exe") to use for
2196# the tests in shell[1-5].test. If no such executable can be found, invoke
2197# [finish_test ; return] in the callers context.
danielk1977ee8b7992009-03-26 17:13:06 +00002198#
dan64b95bb2012-05-12 05:30:29 +00002199proc test_find_cli {} {
danf27d7372016-03-19 17:48:12 +00002200 set prog [test_find_binary sqlite3]
dan49aed582016-03-19 15:13:59 +00002201 if {$prog==""} { return -code return }
2202 return $prog
drh348784e2000-05-29 20:41:49 +00002203}
2204
dan1e8dae02016-03-19 14:53:36 +00002205# Find the name of the 'sqldiff' executable (e.g. "sqlite3.exe") to use for
2206# the tests in sqldiff tests. If no such executable can be found, invoke
2207# [finish_test ; return] in the callers context.
2208#
2209proc test_find_sqldiff {} {
dan49aed582016-03-19 15:13:59 +00002210 set prog [test_find_binary sqldiff]
2211 if {$prog==""} { return -code return }
2212 return $prog
dan1e8dae02016-03-19 14:53:36 +00002213}
2214
2215
drh348784e2000-05-29 20:41:49 +00002216# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
2217# to non-zero, then set the global variable $AUTOVACUUM to 1.
2218set AUTOVACUUM $sqlite_options(default_autovacuum)
2219
dan64b95bb2012-05-12 05:30:29 +00002220# Make sure the FTS enhanced query syntax is disabled.
2221set sqlite_fts3_enable_parentheses 0
2222
drh348784e2000-05-29 20:41:49 +00002223# During testing, assume that all database files are well-formed. The
2224# few test cases that deliberately corrupt database files should rescind
2225# this setting by invoking "database_can_be_corrupt"
2226#
2227database_never_corrupt
2228
drh348784e2000-05-29 20:41:49 +00002229source $testdir/thread_common.tcl
2230source $testdir/malloc_common.tcl