blob: 814788ba4501c3690e7759412650c9a86566f92f [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?
dand05a7142016-08-02 17:07:51 +000028# drop_all_indexes ?DB?
mistachkinfda06be2011-08-02 00:57:34 +000029# forcecopy FROM TO
danc1a60c52010-06-07 14:28:16 +000030# forcedelete FILENAME
31#
32# Test the capability of the SQLite version built into the interpreter to
33# determine if a specific test can be run:
34#
mistachkinc60941f2012-09-13 01:51:02 +000035# capable EXPR
danc1a60c52010-06-07 14:28:16 +000036# ifcapable EXPR
37#
38# Calulate checksums based on database contents:
39#
40# dbcksum DB DBNAME
41# allcksum ?DB?
42# cksum ?DB?
43#
44# Commands to execute/explain SQL statements:
45#
mistachkin1d406e02013-08-29 01:09:14 +000046# memdbsql SQL
danc1a60c52010-06-07 14:28:16 +000047# stepsql DB SQL
48# execsql2 SQL
49# explain_no_trace SQL
50# explain SQL ?DB?
51# catchsql SQL ?DB?
52# execsql SQL ?DB?
53#
54# Commands to run test cases:
55#
56# do_ioerr_test TESTNAME ARGS...
57# crashsql ARGS...
58# integrity_check TESTNAME ?DB?
drh433dccf2013-02-09 15:37:11 +000059# verify_ex_errcode TESTNAME EXPECTED ?DB?
danc1a60c52010-06-07 14:28:16 +000060# do_test TESTNAME SCRIPT EXPECTED
dan153eda02010-06-21 07:45:47 +000061# do_execsql_test TESTNAME SQL EXPECTED
62# do_catchsql_test TESTNAME SQL EXPECTED
drh3c2aeae2014-01-24 14:37:44 +000063# do_timed_execsql_test TESTNAME SQL EXPECTED
danc1a60c52010-06-07 14:28:16 +000064#
dan430e74c2010-06-07 17:47:26 +000065# Commands providing a lower level interface to the global test counters:
danc1a60c52010-06-07 14:28:16 +000066#
67# set_test_counter COUNTER ?VALUE?
mistachkin6c3c1a02011-11-12 03:17:40 +000068# omit_test TESTNAME REASON ?APPEND?
danc1a60c52010-06-07 14:28:16 +000069# fail_test TESTNAME
70# incr_ntest
71#
72# Command run at the end of each test file:
73#
74# finish_test
75#
dan430e74c2010-06-07 17:47:26 +000076# Commands to help create test files that run with the "WAL" and other
77# permutations (see file permutations.test):
danc1a60c52010-06-07 14:28:16 +000078#
79# wal_is_wal_mode
80# wal_set_journal_mode ?DB?
81# wal_check_journal_mode TESTNAME?DB?
dan430e74c2010-06-07 17:47:26 +000082# permutation
dancb79e512010-08-06 13:50:07 +000083# presql
danc1a60c52010-06-07 14:28:16 +000084#
dan17c08232015-06-09 15:58:28 +000085# Command to test whether or not --verbose=1 was specified on the command
86# line (returns 0 for not-verbose, 1 for verbose and 2 for "verbose in the
87# output file only").
88#
89# verbose
90#
drh348784e2000-05-29 20:41:49 +000091
mistachkin1d406e02013-08-29 01:09:14 +000092# Set the precision of FP arithmatic used by the interpreter. And
danc1a60c52010-06-07 14:28:16 +000093# configure SQLite to take database file locks on the page that begins
94# 64KB into the database file instead of the one 1GB in. This means
95# the code that handles that special case can be tested without creating
96# very large database files.
97#
drh92febd92004-08-20 18:34:20 +000098set tcl_precision 15
drhc7a3bb92009-02-05 16:31:45 +000099sqlite3_test_control_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +0000100
danc1a60c52010-06-07 14:28:16 +0000101
mistachkin1d406e02013-08-29 01:09:14 +0000102# If the pager codec is available, create a wrapper for the [sqlite3]
danc1a60c52010-06-07 14:28:16 +0000103# command that appends "-key {xyzzy}" to the command line. i.e. this:
drh3a7fb7c2007-08-10 16:41:08 +0000104#
danc1a60c52010-06-07 14:28:16 +0000105# sqlite3 db test.db
drh4a50aac2007-08-23 02:47:53 +0000106#
danc1a60c52010-06-07 14:28:16 +0000107# becomes
drh4a50aac2007-08-23 02:47:53 +0000108#
danc1a60c52010-06-07 14:28:16 +0000109# sqlite3 db test.db -key {xyzzy}
drh9eb9e262004-02-11 02:18:05 +0000110#
dan430e74c2010-06-07 17:47:26 +0000111if {[info command sqlite_orig]==""} {
drhef4ac8f2004-06-19 00:16:31 +0000112 rename sqlite3 sqlite_orig
113 proc sqlite3 {args} {
dan68928b62010-06-22 13:46:43 +0000114 if {[llength $args]>=2 && [string index [lindex $args 0] 0]!="-"} {
dan430e74c2010-06-07 17:47:26 +0000115 # This command is opening a new database connection.
116 #
117 if {[info exists ::G(perm:sqlite3_args)]} {
118 set args [concat $args $::G(perm:sqlite3_args)]
119 }
dan68928b62010-06-22 13:46:43 +0000120 if {[sqlite_orig -has-codec] && ![info exists ::do_not_use_codec]} {
dan430e74c2010-06-07 17:47:26 +0000121 lappend args -key {xyzzy}
122 }
123
dan3ccd20a2010-06-09 19:01:02 +0000124 set res [uplevel 1 sqlite_orig $args]
dan430e74c2010-06-07 17:47:26 +0000125 if {[info exists ::G(perm:presql)]} {
126 [lindex $args 0] eval $::G(perm:presql)
127 }
dan1ce1b4a2010-12-07 14:32:28 +0000128 if {[info exists ::G(perm:dbconfig)]} {
129 set ::dbhandle [lindex $args 0]
130 uplevel #0 $::G(perm:dbconfig)
131 }
dan3ccd20a2010-06-09 19:01:02 +0000132 set res
dan430e74c2010-06-07 17:47:26 +0000133 } else {
mistachkin1d406e02013-08-29 01:09:14 +0000134 # This command is not opening a new database connection. Pass the
mistachkin48864df2013-03-21 21:20:32 +0000135 # arguments through to the C implementation as the are.
dan430e74c2010-06-07 17:47:26 +0000136 #
137 uplevel 1 sqlite_orig $args
drh9eb9e262004-02-11 02:18:05 +0000138 }
drh9eb9e262004-02-11 02:18:05 +0000139 }
140}
141
mistachkinfda06be2011-08-02 00:57:34 +0000142proc getFileRetries {} {
143 if {![info exists ::G(file-retries)]} {
144 #
145 # NOTE: Return the default number of retries for [file] operations. A
146 # value of zero or less here means "disabled".
147 #
mistachkinc60941f2012-09-13 01:51:02 +0000148 return [expr {$::tcl_platform(platform) eq "windows" ? 50 : 0}]
mistachkinfda06be2011-08-02 00:57:34 +0000149 }
150 return $::G(file-retries)
151}
152
153proc getFileRetryDelay {} {
154 if {![info exists ::G(file-retry-delay)]} {
155 #
156 # NOTE: Return the default number of milliseconds to wait when retrying
157 # failed [file] operations. A value of zero or less means "do not
158 # wait".
159 #
160 return 100; # TODO: Good default?
161 }
162 return $::G(file-retry-delay)
163}
164
mistachkinf8a78462012-03-08 20:00:36 +0000165# Return the string representing the name of the current directory. On
166# Windows, the result is "normalized" to whatever our parent command shell
167# is using to prevent case-mismatch issues.
168#
169proc get_pwd {} {
170 if {$::tcl_platform(platform) eq "windows"} {
mistachkin533b8f62012-03-08 20:28:31 +0000171 #
172 # NOTE: Cannot use [file normalize] here because it would alter the
173 # case of the result to what Tcl considers canonical, which would
174 # defeat the purpose of this procedure.
175 #
176 return [string map [list \\ /] \
177 [string trim [exec -- $::env(ComSpec) /c echo %CD%]]]
mistachkinf8a78462012-03-08 20:00:36 +0000178 } else {
179 return [pwd]
180 }
181}
182
mistachkinfda06be2011-08-02 00:57:34 +0000183# Copy file $from into $to. This is used because some versions of
184# TCL for windows (notably the 8.4.1 binary package shipped with the
185# current mingw release) have a broken "file copy" command.
186#
187proc copy_file {from to} {
188 do_copy_file false $from $to
189}
190
191proc forcecopy {from to} {
192 do_copy_file true $from $to
193}
194
195proc do_copy_file {force from to} {
196 set nRetry [getFileRetries] ;# Maximum number of retries.
197 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
198
199 # On windows, sometimes even a [file copy -force] can fail. The cause is
200 # usually "tag-alongs" - programs like anti-virus software, automatic backup
201 # tools and various explorer extensions that keep a file open a little longer
202 # than we expect, causing the delete to fail.
203 #
204 # The solution is to wait a short amount of time before retrying the copy.
205 #
206 if {$nRetry > 0} {
207 for {set i 0} {$i<$nRetry} {incr i} {
208 set rc [catch {
209 if {$force} {
210 file copy -force $from $to
211 } else {
212 file copy $from $to
213 }
214 } msg]
215 if {$rc==0} break
216 if {$nDelay > 0} { after $nDelay }
217 }
218 if {$rc} { error $msg }
219 } else {
220 if {$force} {
221 file copy -force $from $to
222 } else {
223 file copy $from $to
224 }
225 }
226}
227
mistachkinc5484652012-03-05 22:52:33 +0000228# Check if a file name is relative
229#
230proc is_relative_file { file } {
231 return [expr {[file pathtype $file] != "absolute"}]
232}
233
mistachkin4a41f342012-03-06 03:00:49 +0000234# If the VFS supports using the current directory, returns [pwd];
235# otherwise, it returns only the provided suffix string (which is
236# empty by default).
237#
238proc test_pwd { args } {
239 if {[llength $args] > 0} {
240 set suffix1 [lindex $args 0]
241 if {[llength $args] > 1} {
242 set suffix2 [lindex $args 1]
243 } else {
244 set suffix2 $suffix1
245 }
246 } else {
247 set suffix1 ""; set suffix2 ""
248 }
249 ifcapable curdir {
mistachkin6aa18c92012-03-08 20:22:42 +0000250 return "[get_pwd]$suffix1"
mistachkin4a41f342012-03-06 03:00:49 +0000251 } else {
252 return $suffix2
253 }
254}
255
mistachkinfda06be2011-08-02 00:57:34 +0000256# Delete a file or directory
257#
258proc delete_file {args} {
259 do_delete_file false {*}$args
260}
261
262proc forcedelete {args} {
263 do_delete_file true {*}$args
264}
265
266proc do_delete_file {force args} {
267 set nRetry [getFileRetries] ;# Maximum number of retries.
268 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
269
270 foreach filename $args {
271 # On windows, sometimes even a [file delete -force] can fail just after
272 # a file is closed. The cause is usually "tag-alongs" - programs like
273 # anti-virus software, automatic backup tools and various explorer
274 # extensions that keep a file open a little longer than we expect, causing
275 # the delete to fail.
276 #
277 # The solution is to wait a short amount of time before retrying the
278 # delete.
279 #
280 if {$nRetry > 0} {
281 for {set i 0} {$i<$nRetry} {incr i} {
282 set rc [catch {
283 if {$force} {
284 file delete -force $filename
285 } else {
286 file delete $filename
287 }
288 } msg]
289 if {$rc==0} break
290 if {$nDelay > 0} { after $nDelay }
291 }
292 if {$rc} { error $msg }
293 } else {
294 if {$force} {
295 file delete -force $filename
296 } else {
297 file delete $filename
298 }
299 }
300 }
301}
302
mistachkin1d406e02013-08-29 01:09:14 +0000303if {$::tcl_platform(platform) eq "windows"} {
304 proc do_remove_win32_dir {args} {
305 set nRetry [getFileRetries] ;# Maximum number of retries.
306 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
307
308 foreach dirName $args {
309 # On windows, sometimes even a [remove_win32_dir] can fail just after
310 # a directory is emptied. The cause is usually "tag-alongs" - programs
311 # like anti-virus software, automatic backup tools and various explorer
312 # extensions that keep a file open a little longer than we expect,
313 # causing the delete to fail.
314 #
315 # The solution is to wait a short amount of time before retrying the
316 # removal.
317 #
318 if {$nRetry > 0} {
319 for {set i 0} {$i < $nRetry} {incr i} {
320 set rc [catch {
321 remove_win32_dir $dirName
322 } msg]
323 if {$rc == 0} break
324 if {$nDelay > 0} { after $nDelay }
325 }
326 if {$rc} { error $msg }
327 } else {
328 remove_win32_dir $dirName
329 }
330 }
331 }
332
333 proc do_delete_win32_file {args} {
334 set nRetry [getFileRetries] ;# Maximum number of retries.
335 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
336
337 foreach fileName $args {
338 # On windows, sometimes even a [delete_win32_file] can fail just after
339 # a file is closed. The cause is usually "tag-alongs" - programs like
340 # anti-virus software, automatic backup tools and various explorer
341 # extensions that keep a file open a little longer than we expect,
342 # causing the delete to fail.
343 #
344 # The solution is to wait a short amount of time before retrying the
345 # delete.
346 #
347 if {$nRetry > 0} {
348 for {set i 0} {$i < $nRetry} {incr i} {
349 set rc [catch {
350 delete_win32_file $fileName
351 } msg]
352 if {$rc == 0} break
353 if {$nDelay > 0} { after $nDelay }
354 }
355 if {$rc} { error $msg }
356 } else {
357 delete_win32_file $fileName
358 }
359 }
360 }
361}
362
dancb354602010-07-08 09:44:42 +0000363proc execpresql {handle args} {
364 trace remove execution $handle enter [list execpresql $handle]
365 if {[info exists ::G(perm:presql)]} {
366 $handle eval $::G(perm:presql)
367 }
368}
369
dan68928b62010-06-22 13:46:43 +0000370# This command should be called after loading tester.tcl from within
371# all test scripts that are incompatible with encryption codecs.
372#
373proc do_not_use_codec {} {
374 set ::do_not_use_codec 1
375 reset_db
376}
drh422dded2016-07-25 16:10:43 +0000377unset -nocomplain do_not_use_codec
dan68928b62010-06-22 13:46:43 +0000378
drhaf3906a2016-03-14 17:05:04 +0000379# Return true if the "reserved_bytes" integer on database files is non-zero.
380#
381proc nonzero_reserved_bytes {} {
382 return [sqlite3 -has-codec]
383}
384
drh4b7b1c92016-02-15 19:38:17 +0000385# Print a HELP message and exit
386#
387proc print_help_and_quit {} {
388 puts {Options:
389 --pause Wait for user input before continuing
390 --soft-heap-limit=N Set the soft-heap-limit to N
391 --maxerror=N Quit after N errors
392 --verbose=(0|1) Control the amount of output. Default '1'
393 --output=FILE set --verbose=2 and output to FILE. Implies -q
394 -q Shorthand for --verbose=0
395 --help This message
396}
397 exit 1
398}
399
dan430e74c2010-06-07 17:47:26 +0000400# The following block only runs the first time this file is sourced. It
401# does not run in slave interpreters (since the ::cmdlinearg array is
402# populated before the test script is run in slave interpreters).
drhbec2bf42000-05-29 23:48:22 +0000403#
danc1a60c52010-06-07 14:28:16 +0000404if {[info exists cmdlinearg]==0} {
405
mistachkin1d406e02013-08-29 01:09:14 +0000406 # Parse any options specified in the $argv array. This script accepts the
407 # following options:
danc1a60c52010-06-07 14:28:16 +0000408 #
409 # --pause
410 # --soft-heap-limit=NN
411 # --maxerror=NN
412 # --malloctrace=N
413 # --backtrace=N
414 # --binarylog=N
dan0626dfc2010-06-15 06:56:37 +0000415 # --soak=N
mistachkinfda06be2011-08-02 00:57:34 +0000416 # --file-retries=N
417 # --file-retry-delay=N
dan6a64d672011-04-04 15:38:16 +0000418 # --start=[$permutation:]$testfile
mistachkin6c3c1a02011-11-12 03:17:40 +0000419 # --match=$pattern
dan17c08232015-06-09 15:58:28 +0000420 # --verbose=$val
421 # --output=$filename
drhd5704a82016-03-14 13:42:29 +0000422 # -q Reduce output
423 # --testdir=$dir Run tests in subdirectory $dir
dan17c08232015-06-09 15:58:28 +0000424 # --help
danc1a60c52010-06-07 14:28:16 +0000425 #
426 set cmdlinearg(soft-heap-limit) 0
427 set cmdlinearg(maxerror) 1000
428 set cmdlinearg(malloctrace) 0
429 set cmdlinearg(backtrace) 10
430 set cmdlinearg(binarylog) 0
dan0626dfc2010-06-15 06:56:37 +0000431 set cmdlinearg(soak) 0
mistachkinfda06be2011-08-02 00:57:34 +0000432 set cmdlinearg(file-retries) 0
433 set cmdlinearg(file-retry-delay) 0
mistachkin6c3c1a02011-11-12 03:17:40 +0000434 set cmdlinearg(start) ""
435 set cmdlinearg(match) ""
dan17c08232015-06-09 15:58:28 +0000436 set cmdlinearg(verbose) ""
437 set cmdlinearg(output) ""
drhd5704a82016-03-14 13:42:29 +0000438 set cmdlinearg(testdir) "testdir"
danc1a60c52010-06-07 14:28:16 +0000439
440 set leftover [list]
441 foreach a $argv {
442 switch -regexp -- $a {
443 {^-+pause$} {
mistachkin1d406e02013-08-29 01:09:14 +0000444 # Wait for user input before continuing. This is to give the user an
danc1a60c52010-06-07 14:28:16 +0000445 # opportunity to connect profiling tools to the process.
446 puts -nonewline "Press RETURN to begin..."
447 flush stdout
448 gets stdin
449 }
450 {^-+soft-heap-limit=.+$} {
451 foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break
452 }
453 {^-+maxerror=.+$} {
454 foreach {dummy cmdlinearg(maxerror)} [split $a =] break
455 }
456 {^-+malloctrace=.+$} {
457 foreach {dummy cmdlinearg(malloctrace)} [split $a =] break
458 if {$cmdlinearg(malloctrace)} {
459 sqlite3_memdebug_log start
460 }
461 }
462 {^-+backtrace=.+$} {
463 foreach {dummy cmdlinearg(backtrace)} [split $a =] break
dan0626dfc2010-06-15 06:56:37 +0000464 sqlite3_memdebug_backtrace $value
danc1a60c52010-06-07 14:28:16 +0000465 }
danc1a60c52010-06-07 14:28:16 +0000466 {^-+binarylog=.+$} {
467 foreach {dummy cmdlinearg(binarylog)} [split $a =] break
drhe500f652016-03-14 14:59:35 +0000468 set cmdlinearg(binarylog) [file normalize $cmdlinearg(binarylog)]
danc1a60c52010-06-07 14:28:16 +0000469 }
dan0626dfc2010-06-15 06:56:37 +0000470 {^-+soak=.+$} {
471 foreach {dummy cmdlinearg(soak)} [split $a =] break
472 set ::G(issoak) $cmdlinearg(soak)
473 }
mistachkinfda06be2011-08-02 00:57:34 +0000474 {^-+file-retries=.+$} {
475 foreach {dummy cmdlinearg(file-retries)} [split $a =] break
476 set ::G(file-retries) $cmdlinearg(file-retries)
477 }
478 {^-+file-retry-delay=.+$} {
479 foreach {dummy cmdlinearg(file-retry-delay)} [split $a =] break
480 set ::G(file-retry-delay) $cmdlinearg(file-retry-delay)
481 }
dan6a64d672011-04-04 15:38:16 +0000482 {^-+start=.+$} {
483 foreach {dummy cmdlinearg(start)} [split $a =] break
484
485 set ::G(start:file) $cmdlinearg(start)
486 if {[regexp {(.*):(.*)} $cmdlinearg(start) -> s.perm s.file]} {
487 set ::G(start:permutation) ${s.perm}
488 set ::G(start:file) ${s.file}
489 }
490 if {$::G(start:file) == ""} {unset ::G(start:file)}
491 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000492 {^-+match=.+$} {
493 foreach {dummy cmdlinearg(match)} [split $a =] break
494
495 set ::G(match) $cmdlinearg(match)
496 if {$::G(match) == ""} {unset ::G(match)}
497 }
dan17c08232015-06-09 15:58:28 +0000498
499 {^-+output=.+$} {
500 foreach {dummy cmdlinearg(output)} [split $a =] break
drhe500f652016-03-14 14:59:35 +0000501 set cmdlinearg(output) [file normalize $cmdlinearg(output)]
dan17c08232015-06-09 15:58:28 +0000502 if {$cmdlinearg(verbose)==""} {
503 set cmdlinearg(verbose) 2
504 }
505 }
506 {^-+verbose=.+$} {
507 foreach {dummy cmdlinearg(verbose)} [split $a =] break
508 if {$cmdlinearg(verbose)=="file"} {
509 set cmdlinearg(verbose) 2
510 } elseif {[string is boolean -strict $cmdlinearg(verbose)]==0} {
511 error "option --verbose= must be set to a boolean or to \"file\""
512 }
513 }
drhd5704a82016-03-14 13:42:29 +0000514 {^-+testdir=.*$} {
515 foreach {dummy cmdlinearg(testdir)} [split $a =] break
516 }
drh4b7b1c92016-02-15 19:38:17 +0000517 {.*help.*} {
518 print_help_and_quit
519 }
520 {^-q$} {
521 set cmdlinearg(output) test-out.txt
522 set cmdlinearg(verbose) 2
523 }
dan17c08232015-06-09 15:58:28 +0000524
danc1a60c52010-06-07 14:28:16 +0000525 default {
dan40cf36f2016-04-30 19:23:10 +0000526 if {[file tail $a]==$a} {
527 lappend leftover $a
528 } else {
529 lappend leftover [file normalize $a]
530 }
danc1a60c52010-06-07 14:28:16 +0000531 }
532 }
533 }
drhbea14132016-03-14 14:28:43 +0000534 set testdir [file normalize $testdir]
drhd5704a82016-03-14 13:42:29 +0000535 set cmdlinearg(TESTFIXTURE_HOME) [pwd]
drhaf3906a2016-03-14 17:05:04 +0000536 set cmdlinearg(INFO_SCRIPT) [file normalize [info script]]
drhbea14132016-03-14 14:28:43 +0000537 set argv0 [file normalize $argv0]
drhd5704a82016-03-14 13:42:29 +0000538 if {$cmdlinearg(testdir)!=""} {
539 file mkdir $cmdlinearg(testdir)
540 cd $cmdlinearg(testdir)
541 }
danc1a60c52010-06-07 14:28:16 +0000542 set argv $leftover
543
dan430e74c2010-06-07 17:47:26 +0000544 # Install the malloc layer used to inject OOM errors. And the 'automatic'
545 # extensions. This only needs to be done once for the process.
546 #
mistachkin1d406e02013-08-29 01:09:14 +0000547 sqlite3_shutdown
548 install_malloc_faultsim 1
danielk1977f7b9d662008-06-23 18:49:43 +0000549 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000550 autoinstall_test_functions
danc1a60c52010-06-07 14:28:16 +0000551
dan430e74c2010-06-07 17:47:26 +0000552 # If the --binarylog option was specified, create the logging VFS. This
553 # call installs the new VFS as the default for all SQLite connections.
554 #
danc1a60c52010-06-07 14:28:16 +0000555 if {$cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000556 vfslog new binarylog {} vfslog.bin
danc1a60c52010-06-07 14:28:16 +0000557 }
558
559 # Set the backtrace depth, if malloc tracing is enabled.
560 #
561 if {$cmdlinearg(malloctrace)} {
562 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danielk1977185eac92008-07-12 15:55:54 +0000563 }
dan17c08232015-06-09 15:58:28 +0000564
565 if {$cmdlinearg(output)!=""} {
566 puts "Copying output to file $cmdlinearg(output)"
567 set ::G(output_fd) [open $cmdlinearg(output) w]
568 fconfigure $::G(output_fd) -buffering line
569 }
570
571 if {$cmdlinearg(verbose)==""} {
572 set cmdlinearg(verbose) 1
573 }
danielk1977f7b9d662008-06-23 18:49:43 +0000574}
danielk197703ba3fa2009-01-09 10:49:14 +0000575
danc1a60c52010-06-07 14:28:16 +0000576# Update the soft-heap-limit each time this script is run. In that
577# way if an individual test file changes the soft-heap-limit, it
578# will be reset at the start of the next test file.
579#
580sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
581
582# Create a test database
583#
danielk197703ba3fa2009-01-09 10:49:14 +0000584proc reset_db {} {
585 catch {db close}
mistachkinfda06be2011-08-02 00:57:34 +0000586 forcedelete test.db
587 forcedelete test.db-journal
588 forcedelete test.db-wal
danielk197703ba3fa2009-01-09 10:49:14 +0000589 sqlite3 db ./test.db
590 set ::DB [sqlite3_connection_pointer db]
591 if {[info exists ::SETUP_SQL]} {
592 db eval $::SETUP_SQL
593 }
drhcd61c282002-03-06 22:01:34 +0000594}
danielk197703ba3fa2009-01-09 10:49:14 +0000595reset_db
drhbec2bf42000-05-29 23:48:22 +0000596
597# Abort early if this script has been run before.
598#
danc1a60c52010-06-07 14:28:16 +0000599if {[info exists TC(count)]} return
drhbec2bf42000-05-29 23:48:22 +0000600
drhce2198c2010-09-10 13:22:59 +0000601# Make sure memory statistics are enabled.
602#
603sqlite3_config_memstatus 1
604
danc1a60c52010-06-07 14:28:16 +0000605# Initialize the test counters and set up commands to access them.
606# Or, if this is a slave interpreter, set up aliases to write the
607# counters in the parent interpreter.
drhbec2bf42000-05-29 23:48:22 +0000608#
danc1a60c52010-06-07 14:28:16 +0000609if {0==[info exists ::SLAVE]} {
610 set TC(errors) 0
611 set TC(count) 0
612 set TC(fail_list) [list]
613 set TC(omit_list) [list]
drhcca17c32013-04-19 12:32:52 +0000614 set TC(warn_list) [list]
danc1a60c52010-06-07 14:28:16 +0000615
616 proc set_test_counter {counter args} {
617 if {[llength $args]} {
618 set ::TC($counter) [lindex $args 0]
619 }
620 set ::TC($counter)
621 }
drhb62c3352006-11-23 09:39:16 +0000622}
drh348784e2000-05-29 20:41:49 +0000623
drh521cc842008-04-15 02:36:33 +0000624# Record the fact that a sequence of tests were omitted.
625#
mistachkin6c3c1a02011-11-12 03:17:40 +0000626proc omit_test {name reason {append 1}} {
danc1a60c52010-06-07 14:28:16 +0000627 set omitList [set_test_counter omit_list]
mistachkin6c3c1a02011-11-12 03:17:40 +0000628 if {$append} {
629 lappend omitList [list $name $reason]
630 }
danc1a60c52010-06-07 14:28:16 +0000631 set_test_counter omit_list $omitList
drh521cc842008-04-15 02:36:33 +0000632}
633
danc1a60c52010-06-07 14:28:16 +0000634# Record the fact that a test failed.
635#
636proc fail_test {name} {
637 set f [set_test_counter fail_list]
638 lappend f $name
639 set_test_counter fail_list $f
640 set_test_counter errors [expr [set_test_counter errors] + 1]
641
642 set nFail [set_test_counter errors]
643 if {$nFail>=$::cmdlinearg(maxerror)} {
dan17c08232015-06-09 15:58:28 +0000644 output2 "*** Giving up..."
danc1a60c52010-06-07 14:28:16 +0000645 finalize_testing
646 }
647}
648
drhcca17c32013-04-19 12:32:52 +0000649# Remember a warning message to be displayed at the conclusion of all testing
650#
651proc warning {msg {append 1}} {
dan17c08232015-06-09 15:58:28 +0000652 output2 "Warning: $msg"
drhcca17c32013-04-19 12:32:52 +0000653 set warnList [set_test_counter warn_list]
654 if {$append} {
655 lappend warnList $msg
656 }
657 set_test_counter warn_list $warnList
658}
659
660
danc1a60c52010-06-07 14:28:16 +0000661# Increment the number of tests run
662#
663proc incr_ntest {} {
664 set_test_counter count [expr [set_test_counter count] + 1]
665}
666
dan17c08232015-06-09 15:58:28 +0000667# Return true if --verbose=1 was specified on the command line. Otherwise,
668# return false.
669#
670proc verbose {} {
671 return $::cmdlinearg(verbose)
672}
673
674# Use the following commands instead of [puts] for test output within
675# this file. Test scripts can still use regular [puts], which is directed
676# to stdout and, if one is open, the --output file.
677#
678# output1: output that should be printed if --verbose=1 was specified.
679# output2: output that should be printed unconditionally.
680# output2_if_no_verbose: output that should be printed only if --verbose=0.
681#
682proc output1 {args} {
683 set v [verbose]
684 if {$v==1} {
685 uplevel output2 $args
686 } elseif {$v==2} {
687 uplevel puts [lrange $args 0 end-1] $::G(output_fd) [lrange $args end end]
688 }
689}
690proc output2 {args} {
691 set nArg [llength $args]
692 uplevel puts $args
693}
694proc output2_if_no_verbose {args} {
695 set v [verbose]
696 if {$v==0} {
697 uplevel output2 $args
698 } elseif {$v==2} {
699 uplevel puts [lrange $args 0 end-1] stdout [lrange $args end end]
700 }
701}
702
703# Override the [puts] command so that if no channel is explicitly
704# specified the string is written to both stdout and to the file
705# specified by "--output=", if any.
706#
707proc puts_override {args} {
708 set nArg [llength $args]
709 if {$nArg==1 || ($nArg==2 && [string first [lindex $args 0] -nonewline]==0)} {
710 uplevel puts_original $args
711 if {[info exists ::G(output_fd)]} {
712 uplevel puts [lrange $args 0 end-1] $::G(output_fd) [lrange $args end end]
713 }
714 } else {
715 # A channel was explicitly specified.
716 uplevel puts_original $args
717 }
718}
719rename puts puts_original
720proc puts {args} { uplevel puts_override $args }
721
danc1a60c52010-06-07 14:28:16 +0000722
mistachkin1d406e02013-08-29 01:09:14 +0000723# Invoke the do_test procedure to run a single test
drh348784e2000-05-29 20:41:49 +0000724#
drhe39cd912016-07-09 17:47:01 +0000725# The $expected parameter is the expected result. The result is the return
726# value from the last TCL command in $cmd.
727#
728# Normally, $expected must match exactly. But if $expected is of the form
729# "/regexp/" then regular expression matching is used. If $expected is
730# "~/regexp/" then the regular expression must NOT match. If $expected is
731# of the form "#/value-list/" then each term in value-list must be numeric
732# and must approximately match the corresponding numeric term in $result.
733# Values must match within 10%. Or if the $expected term is A..B then the
734# $result term must be in between A and B.
735#
drh348784e2000-05-29 20:41:49 +0000736proc do_test {name cmd expected} {
danc1a60c52010-06-07 14:28:16 +0000737 global argv cmdlinearg
738
dan8c408002010-11-01 17:38:24 +0000739 fix_testname name
740
drh4a50aac2007-08-23 02:47:53 +0000741 sqlite3_memdebug_settitle $name
danc1a60c52010-06-07 14:28:16 +0000742
mistachkin1d406e02013-08-29 01:09:14 +0000743# if {[llength $argv]==0} {
dan92d516a2010-07-05 14:54:48 +0000744# set go 1
745# } else {
746# set go 0
747# foreach pattern $argv {
748# if {[string match $pattern $name]} {
749# set go 1
750# break
751# }
752# }
753# }
dan430e74c2010-06-07 17:47:26 +0000754
dand506de02010-07-03 13:59:01 +0000755 if {[info exists ::G(perm:prefix)]} {
756 set name "$::G(perm:prefix)$name"
dan430e74c2010-06-07 17:47:26 +0000757 }
758
danc1a60c52010-06-07 14:28:16 +0000759 incr_ntest
dan17c08232015-06-09 15:58:28 +0000760 output1 -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000761 flush stdout
mistachkin6c3c1a02011-11-12 03:17:40 +0000762
763 if {![info exists ::G(match)] || [string match $::G(match) $name]} {
764 if {[catch {uplevel #0 "$cmd;\n"} result]} {
dan17c08232015-06-09 15:58:28 +0000765 output2_if_no_verbose -nonewline $name...
766 output2 "\nError: $result"
mistachkin6c3c1a02011-11-12 03:17:40 +0000767 fail_test $name
mistachkin6c3c1a02011-11-12 03:17:40 +0000768 } else {
drhe39cd912016-07-09 17:47:01 +0000769 if {[regexp {^[~#]?/.*/$} $expected]} {
drh70bdcc72013-05-30 19:28:34 +0000770 # "expected" is of the form "/PATTERN/" then the result if correct if
771 # regular expression PATTERN matches the result. "~/PATTERN/" means
772 # the regular expression must not match.
drh3f17aef2012-04-27 01:08:02 +0000773 if {[string index $expected 0]=="~"} {
drhc2b23e72013-11-13 15:32:15 +0000774 set re [string range $expected 2 end-1]
775 if {[string index $re 0]=="*"} {
776 # If the regular expression begins with * then treat it as a glob instead
777 set ok [string match $re $result]
778 } else {
779 set re [string map {# {[-0-9.]+}} $re]
780 set ok [regexp $re $result]
781 }
782 set ok [expr {!$ok}]
drhe39cd912016-07-09 17:47:01 +0000783 } elseif {[string index $expected 0]=="#"} {
784 # Numeric range value comparison. Each term of the $result is matched
785 # against one term of $expect. Both $result and $expected terms must be
786 # numeric. The values must match within 10%. Or if $expected is of the
787 # form A..B then the $result term must be between A and B.
788 set e2 [string range $expected 2 end-1]
789 foreach i $result j $e2 {
790 if {[regexp {^(-?\d+)\.\.(-?\d)$} $j all A B]} {
791 set ok [expr {$i+0>=$A && $i+0<=$B}]
792 } else {
793 set ok [expr {$i+0>=0.9*$j && $i+0<=1.1*$j}]
794 }
795 if {!$ok} break
796 }
797 if {$ok && [llength $result]!=[llength $e2]} {set ok 0}
drh3f17aef2012-04-27 01:08:02 +0000798 } else {
drhc2b23e72013-11-13 15:32:15 +0000799 set re [string range $expected 1 end-1]
800 if {[string index $re 0]=="*"} {
801 # If the regular expression begins with * then treat it as a glob instead
802 set ok [string match $re $result]
803 } else {
804 set re [string map {# {[-0-9.]+}} $re]
805 set ok [regexp $re $result]
806 }
drh3f17aef2012-04-27 01:08:02 +0000807 }
drh70bdcc72013-05-30 19:28:34 +0000808 } elseif {[regexp {^~?\*.*\*$} $expected]} {
809 # "expected" is of the form "*GLOB*" then the result if correct if
810 # glob pattern GLOB matches the result. "~/GLOB/" means
811 # the glob must not match.
812 if {[string index $expected 0]=="~"} {
813 set e [string range $expected 1 end]
814 set ok [expr {![string match $e $result]}]
815 } else {
816 set ok [string match $expected $result]
817 }
drh3f17aef2012-04-27 01:08:02 +0000818 } else {
819 set ok [expr {[string compare $result $expected]==0}]
820 }
821 if {!$ok} {
mistachkinc60941f2012-09-13 01:51:02 +0000822 # if {![info exists ::testprefix] || $::testprefix eq ""} {
823 # error "no test prefix"
824 # }
drhd66b2e02015-11-12 21:42:40 +0000825 output1 ""
drh061d35c2015-11-13 00:03:14 +0000826 output2 "! $name expected: \[$expected\]\n! $name got: \[$result\]"
drh3f17aef2012-04-27 01:08:02 +0000827 fail_test $name
828 } else {
dan17c08232015-06-09 15:58:28 +0000829 output1 " Ok"
drh3f17aef2012-04-27 01:08:02 +0000830 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000831 }
drh348784e2000-05-29 20:41:49 +0000832 } else {
dan17c08232015-06-09 15:58:28 +0000833 output1 " Omitted"
mistachkin6c3c1a02011-11-12 03:17:40 +0000834 omit_test $name "pattern mismatch" 0
drh348784e2000-05-29 20:41:49 +0000835 }
drh6558db82007-04-13 03:23:21 +0000836 flush stdout
drh348784e2000-05-29 20:41:49 +0000837}
dana3f51082010-09-30 18:43:14 +0000838
mistachkinf21979d2015-01-18 05:35:01 +0000839proc dumpbytes {s} {
840 set r ""
841 for {set i 0} {$i < [string length $s]} {incr i} {
842 if {$i > 0} {append r " "}
843 append r [format %02X [scan [string index $s $i] %c]]
844 }
845 return $r
846}
847
drh8df91852012-04-24 12:46:05 +0000848proc catchcmd {db {cmd ""}} {
849 global CLI
850 set out [open cmds.txt w]
851 puts $out $cmd
852 close $out
853 set line "exec $CLI $db < cmds.txt"
854 set rc [catch { eval $line } msg]
855 list $rc $msg
856}
857
mistachkinf21979d2015-01-18 05:35:01 +0000858proc catchcmdex {db {cmd ""}} {
859 global CLI
860 set out [open cmds.txt w]
861 fconfigure $out -encoding binary -translation binary
862 puts -nonewline $out $cmd
863 close $out
864 set line "exec -keepnewline -- $CLI $db < cmds.txt"
865 set chans [list stdin stdout stderr]
866 foreach chan $chans {
867 catch {
868 set modes($chan) [fconfigure $chan]
869 fconfigure $chan -encoding binary -translation binary -buffering none
870 }
871 }
872 set rc [catch { eval $line } msg]
873 foreach chan $chans {
874 catch {
875 eval fconfigure [list $chan] $modes($chan)
876 }
877 }
878 # puts [dumpbytes $msg]
879 list $rc $msg
880}
881
shaneh55505172011-06-21 19:30:19 +0000882proc filepath_normalize {p} {
883 # test cases should be written to assume "unix"-like file paths
shaneh285a18f2011-06-21 19:39:59 +0000884 if {$::tcl_platform(platform)!="unix"} {
shaneh55505172011-06-21 19:30:19 +0000885 # lreverse*2 as a hack to remove any unneeded {} after the string map
886 lreverse [lreverse [string map {\\ /} [regsub -nocase -all {[a-z]:[/\\]+} $p {/}]]]
shanehc4896402011-06-21 19:38:16 +0000887 } {
888 set p
shaneh55505172011-06-21 19:30:19 +0000889 }
890}
891proc do_filepath_test {name cmd expected} {
892 uplevel [list do_test $name [
893 subst -nocommands { filepath_normalize [ $cmd ] }
894 ] [filepath_normalize $expected]]
895}
896
dan33f53792011-05-05 19:44:22 +0000897proc realnum_normalize {r} {
shaneh4f529e82011-06-20 17:41:41 +0000898 # different TCL versions display floating point values differently.
899 string map {1.#INF inf Inf inf .0e e} [regsub -all {(e[+-])0+} $r {\1}]
dan33f53792011-05-05 19:44:22 +0000900}
901proc do_realnum_test {name cmd expected} {
902 uplevel [list do_test $name [
903 subst -nocommands { realnum_normalize [ $cmd ] }
904 ] [realnum_normalize $expected]]
905}
906
dana3f51082010-09-30 18:43:14 +0000907proc fix_testname {varname} {
908 upvar $varname testname
mistachkin1d406e02013-08-29 01:09:14 +0000909 if {[info exists ::testprefix]
dana3f51082010-09-30 18:43:14 +0000910 && [string is digit [string range $testname 0 0]]
911 } {
912 set testname "${::testprefix}-$testname"
913 }
914}
dan67340072011-04-16 19:23:10 +0000915
916proc normalize_list {L} {
917 set L2 [list]
918 foreach l $L {lappend L2 $l}
919 set L2
920}
drh5f9742e2013-08-29 15:08:38 +0000921
dan57f7f4b2010-10-01 19:04:37 +0000922proc do_execsql_test {testname sql {result {}}} {
dana3f51082010-09-30 18:43:14 +0000923 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000924 uplevel do_test [list $testname] [list "execsql {$sql}"] [list [list {*}$result]]
dan153eda02010-06-21 07:45:47 +0000925}
926proc do_catchsql_test {testname sql result} {
dana3f51082010-09-30 18:43:14 +0000927 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000928 uplevel do_test [list $testname] [list "catchsql {$sql}"] [list $result]
dan153eda02010-06-21 07:45:47 +0000929}
drh3c2aeae2014-01-24 14:37:44 +0000930proc do_timed_execsql_test {testname sql {result {}}} {
931 fix_testname testname
932 uplevel do_test [list $testname] [list "execsql_timed {$sql}"]\
933 [list [list {*}$result]]
934}
dan39854792010-11-15 16:12:58 +0000935proc do_eqp_test {name sql res} {
936 uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res]
937}
dan153eda02010-06-21 07:45:47 +0000938
dan51f06982010-09-18 19:00:12 +0000939#-------------------------------------------------------------------------
940# Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST
941#
942# Where switches are:
943#
944# -errorformat FMTSTRING
945# -count
danaf1dcab2010-09-20 19:17:53 +0000946# -query SQL
dana16d1062010-09-28 17:37:28 +0000947# -tclquery TCL
danaf7626f2010-09-23 18:47:36 +0000948# -repair TCL
dan51f06982010-09-18 19:00:12 +0000949#
950proc do_select_tests {prefix args} {
951
952 set testlist [lindex $args end]
953 set switches [lrange $args 0 end-1]
954
955 set errfmt ""
956 set countonly 0
dana16d1062010-09-28 17:37:28 +0000957 set tclquery ""
danaf7626f2010-09-23 18:47:36 +0000958 set repair ""
dan51f06982010-09-18 19:00:12 +0000959
960 for {set i 0} {$i < [llength $switches]} {incr i} {
961 set s [lindex $switches $i]
962 set n [string length $s]
danaf1dcab2010-09-20 19:17:53 +0000963 if {$n>=2 && [string equal -length $n $s "-query"]} {
dana16d1062010-09-28 17:37:28 +0000964 set tclquery [list execsql [lindex $switches [incr i]]]
965 } elseif {$n>=2 && [string equal -length $n $s "-tclquery"]} {
966 set tclquery [lindex $switches [incr i]]
danaf1dcab2010-09-20 19:17:53 +0000967 } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} {
dan51f06982010-09-18 19:00:12 +0000968 set errfmt [lindex $switches [incr i]]
danaf7626f2010-09-23 18:47:36 +0000969 } elseif {$n>=2 && [string equal -length $n $s "-repair"]} {
970 set repair [lindex $switches [incr i]]
dan51f06982010-09-18 19:00:12 +0000971 } elseif {$n>=2 && [string equal -length $n $s "-count"]} {
972 set countonly 1
973 } else {
974 error "unknown switch: $s"
975 }
976 }
977
978 if {$countonly && $errfmt!=""} {
979 error "Cannot use -count and -errorformat together"
980 }
981 set nTestlist [llength $testlist]
982 if {$nTestlist%3 || $nTestlist==0 } {
983 error "SELECT test list contains [llength $testlist] elements"
984 }
985
danaf7626f2010-09-23 18:47:36 +0000986 eval $repair
dan51f06982010-09-18 19:00:12 +0000987 foreach {tn sql res} $testlist {
dana16d1062010-09-28 17:37:28 +0000988 if {$tclquery != ""} {
danaf1dcab2010-09-20 19:17:53 +0000989 execsql $sql
dana16d1062010-09-28 17:37:28 +0000990 uplevel do_test ${prefix}.$tn [list $tclquery] [list [list {*}$res]]
991 } elseif {$countonly} {
dan51f06982010-09-18 19:00:12 +0000992 set nRow 0
993 db eval $sql {incr nRow}
994 uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res]
995 } elseif {$errfmt==""} {
996 uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]]
997 } else {
998 set res [list 1 [string trim [format $errfmt {*}$res]]]
999 uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res]
1000 }
danaf7626f2010-09-23 18:47:36 +00001001 eval $repair
dan51f06982010-09-18 19:00:12 +00001002 }
danaf7626f2010-09-23 18:47:36 +00001003
dan51f06982010-09-18 19:00:12 +00001004}
1005
danb04757a2010-09-21 16:59:16 +00001006proc delete_all_data {} {
1007 db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} {
1008 db eval "DELETE FROM '[string map {' ''} $t]'"
1009 }
1010}
drh348784e2000-05-29 20:41:49 +00001011
mistachkin1d406e02013-08-29 01:09:14 +00001012# Run an SQL script.
drhb62c3352006-11-23 09:39:16 +00001013# Return the number of microseconds per statement.
1014#
drh3590f152006-11-23 21:09:10 +00001015proc speed_trial {name numstmt units sql} {
dan17c08232015-06-09 15:58:28 +00001016 output2 -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +00001017 flush stdout
1018 set speed [time {sqlite3_exec_nr db $sql}]
1019 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +00001020 if {$tm == 0} {
1021 set rate [format %20s "many"]
1022 } else {
1023 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
1024 }
drh3590f152006-11-23 21:09:10 +00001025 set u2 $units/s
dan17c08232015-06-09 15:58:28 +00001026 output2 [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +00001027 global total_time
1028 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +00001029 lappend ::speed_trial_times $name $tm
drhdd924312007-03-31 22:34:16 +00001030}
drh45c236d2008-03-22 01:08:00 +00001031proc speed_trial_tcl {name numstmt units script} {
dan17c08232015-06-09 15:58:28 +00001032 output2 -nonewline [format {%-21.21s } $name...]
drh45c236d2008-03-22 01:08:00 +00001033 flush stdout
1034 set speed [time {eval $script}]
1035 set tm [lindex $speed 0]
1036 if {$tm == 0} {
1037 set rate [format %20s "many"]
1038 } else {
1039 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
1040 }
1041 set u2 $units/s
dan17c08232015-06-09 15:58:28 +00001042 output2 [format {%12d uS %s %s} $tm $rate $u2]
drh45c236d2008-03-22 01:08:00 +00001043 global total_time
1044 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +00001045 lappend ::speed_trial_times $name $tm
drh45c236d2008-03-22 01:08:00 +00001046}
drhdd924312007-03-31 22:34:16 +00001047proc speed_trial_init {name} {
1048 global total_time
1049 set total_time 0
dana975b592010-10-08 16:09:43 +00001050 set ::speed_trial_times [list]
drhbb810a92010-07-03 12:00:53 +00001051 sqlite3 versdb :memory:
1052 set vers [versdb one {SELECT sqlite_source_id()}]
1053 versdb close
dan17c08232015-06-09 15:58:28 +00001054 output2 "SQLite $vers"
drhdd924312007-03-31 22:34:16 +00001055}
1056proc speed_trial_summary {name} {
1057 global total_time
dan17c08232015-06-09 15:58:28 +00001058 output2 [format {%-21.21s %12d uS TOTAL} $name $total_time]
dana975b592010-10-08 16:09:43 +00001059
1060 if { 0 } {
1061 sqlite3 versdb :memory:
1062 set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0]
1063 versdb close
dan17c08232015-06-09 15:58:28 +00001064 output2 "CREATE TABLE IF NOT EXISTS time(version, script, test, us);"
dana975b592010-10-08 16:09:43 +00001065 foreach {test us} $::speed_trial_times {
dan17c08232015-06-09 15:58:28 +00001066 output2 "INSERT INTO time VALUES('$vers', '$name', '$test', $us);"
dana975b592010-10-08 16:09:43 +00001067 }
1068 }
drhb62c3352006-11-23 09:39:16 +00001069}
1070
drh348784e2000-05-29 20:41:49 +00001071# Run this routine last
1072#
1073proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +00001074 catch {db close}
dane8559832014-07-31 17:35:40 +00001075 catch {db1 close}
danc1a60c52010-06-07 14:28:16 +00001076 catch {db2 close}
1077 catch {db3 close}
1078 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +00001079}
1080proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +00001081 global sqlite_open_file_count
1082
1083 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +00001084
drh6e142f52000-06-08 13:36:40 +00001085 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +00001086 catch {db2 close}
1087 catch {db3 close}
1088
drh9bc54492007-10-23 14:49:59 +00001089 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +00001090 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +00001091 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +00001092 db close
drh984bfaa2008-03-19 16:08:53 +00001093 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +00001094
drh3a7fb7c2007-08-10 16:41:08 +00001095 sqlite3_soft_heap_limit 0
danc1a60c52010-06-07 14:28:16 +00001096 set nTest [incr_ntest]
1097 set nErr [set_test_counter errors]
1098
drhef866372013-05-22 20:49:02 +00001099 set nKnown 0
1100 if {[file readable known-problems.txt]} {
1101 set fd [open known-problems.txt]
1102 set content [read $fd]
1103 close $fd
1104 foreach x $content {set known_error($x) 1}
1105 foreach x [set_test_counter fail_list] {
1106 if {[info exists known_error($x)]} {incr nKnown}
1107 }
1108 }
1109 if {$nKnown>0} {
dan17c08232015-06-09 15:58:28 +00001110 output2 "[expr {$nErr-$nKnown}] new errors and $nKnown known errors\
drhef866372013-05-22 20:49:02 +00001111 out of $nTest tests"
1112 } else {
drh72bf6a32016-01-07 02:06:55 +00001113 set cpuinfo {}
1114 if {[catch {exec hostname} hname]==0} {set cpuinfo [string trim $hname]}
1115 append cpuinfo " $::tcl_platform(os)"
1116 append cpuinfo " [expr {$::tcl_platform(pointerSize)*8}]-bit"
1117 append cpuinfo " [string map {E -e} $::tcl_platform(byteOrder)]"
1118 output2 "SQLite [sqlite3 -sourceid]"
1119 output2 "$nErr errors out of $nTest tests on $cpuinfo"
drhef866372013-05-22 20:49:02 +00001120 }
1121 if {$nErr>$nKnown} {
drh061d35c2015-11-13 00:03:14 +00001122 output2 -nonewline "!Failures on these tests:"
drhef866372013-05-22 20:49:02 +00001123 foreach x [set_test_counter fail_list] {
dan17c08232015-06-09 15:58:28 +00001124 if {![info exists known_error($x)]} {output2 -nonewline " $x"}
drhef866372013-05-22 20:49:02 +00001125 }
dan17c08232015-06-09 15:58:28 +00001126 output2 ""
drhf3a65f72007-08-22 20:18:21 +00001127 }
drhcca17c32013-04-19 12:32:52 +00001128 foreach warning [set_test_counter warn_list] {
dan17c08232015-06-09 15:58:28 +00001129 output2 "Warning: $warning"
drhcca17c32013-04-19 12:32:52 +00001130 }
danielk1977ee8b7992009-03-26 17:13:06 +00001131 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +00001132 if {[llength $omitList]>0} {
dan17c08232015-06-09 15:58:28 +00001133 output2 "Omitted test cases:"
drh521cc842008-04-15 02:36:33 +00001134 set prec {}
1135 foreach {rec} [lsort $omitList] {
1136 if {$rec==$prec} continue
1137 set prec $rec
drhd66b2e02015-11-12 21:42:40 +00001138 output2 [format {. %-12s %s} [lindex $rec 0] [lindex $rec 1]]
drh521cc842008-04-15 02:36:33 +00001139 }
1140 }
drh80788d82006-09-02 14:50:23 +00001141 if {$nErr>0 && ![working_64bit_int]} {
dan17c08232015-06-09 15:58:28 +00001142 output2 "******************************************************************"
1143 output2 "N.B.: The version of TCL that you used to build this test harness"
1144 output2 "is defective in that it does not support 64-bit integers. Some or"
1145 output2 "all of the test failures above might be a result from this defect"
1146 output2 "in your TCL build."
1147 output2 "******************************************************************"
drhdb25e382001-03-15 18:21:22 +00001148 }
danc1a60c52010-06-07 14:28:16 +00001149 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +00001150 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +00001151 }
drh94e92032003-02-16 22:21:32 +00001152 if {$sqlite_open_file_count} {
dan17c08232015-06-09 15:58:28 +00001153 output2 "$sqlite_open_file_count files were left open"
drh94e92032003-02-16 22:21:32 +00001154 incr nErr
1155 }
drheafc43b2010-07-26 18:43:40 +00001156 if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 ||
1157 [sqlite3_memory_used]>0} {
dan17c08232015-06-09 15:58:28 +00001158 output2 "Unfreed memory: [sqlite3_memory_used] bytes in\
drheafc43b2010-07-26 18:43:40 +00001159 [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations"
drhf3a65f72007-08-22 20:18:21 +00001160 incr nErr
drh2d7636e2008-02-16 16:21:45 +00001161 ifcapable memdebug||mem5||(mem3&&debug) {
dan17c08232015-06-09 15:58:28 +00001162 output2 "Writing unfreed memory log to \"./memleak.txt\""
drh4a50aac2007-08-23 02:47:53 +00001163 sqlite3_memdebug_dump ./memleak.txt
1164 }
drhf3a65f72007-08-22 20:18:21 +00001165 } else {
dan17c08232015-06-09 15:58:28 +00001166 output2 "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +00001167 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +00001168 sqlite3_memdebug_dump ./memusage.txt
1169 }
drhf3a65f72007-08-22 20:18:21 +00001170 }
drhf7141992008-06-19 00:16:08 +00001171 show_memstats
dan17c08232015-06-09 15:58:28 +00001172 output2 "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
1173 output2 "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +00001174 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
dan17c08232015-06-09 15:58:28 +00001175 output2 "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
danielk1977a7a8e142008-02-13 18:25:27 +00001176 }
danc1a60c52010-06-07 14:28:16 +00001177 if {$::cmdlinearg(malloctrace)} {
dan17c08232015-06-09 15:58:28 +00001178 output2 "Writing mallocs.sql..."
danielk197735754ac2008-03-21 17:29:37 +00001179 memdebug_log_sql
1180 sqlite3_memdebug_log stop
1181 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +00001182
1183 if {[sqlite3_memory_used]>0} {
dan17c08232015-06-09 15:58:28 +00001184 output2 "Writing leaks.sql..."
danielk1977dbdc4d42008-03-28 07:42:53 +00001185 sqlite3_memdebug_log sync
1186 memdebug_log_sql leaks.sql
1187 }
danielk197735754ac2008-03-21 17:29:37 +00001188 }
drhd9910fe2006-10-04 11:55:49 +00001189 foreach f [glob -nocomplain test.db-*-journal] {
mistachkinfda06be2011-08-02 00:57:34 +00001190 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +00001191 }
1192 foreach f [glob -nocomplain test.db-mj*] {
mistachkinfda06be2011-08-02 00:57:34 +00001193 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +00001194 }
drhdb25e382001-03-15 18:21:22 +00001195 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +00001196}
1197
drhf7141992008-06-19 00:16:08 +00001198# Display memory statistics for analysis and debugging purposes.
1199#
1200proc show_memstats {} {
1201 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +00001202 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
1203 set val [format {now %10d max %10d max-size %10d} \
1204 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
dan17c08232015-06-09 15:58:28 +00001205 output1 "Memory used: $val"
drheafc43b2010-07-26 18:43:40 +00001206 set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0]
1207 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
dan17c08232015-06-09 15:58:28 +00001208 output1 "Allocation count: $val"
drhf7141992008-06-19 00:16:08 +00001209 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +00001210 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
1211 set val [format {now %10d max %10d max-size %10d} \
1212 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
dan17c08232015-06-09 15:58:28 +00001213 output1 "Page-cache used: $val"
drhf7141992008-06-19 00:16:08 +00001214 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
1215 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
dan17c08232015-06-09 15:58:28 +00001216 output1 "Page-cache overflow: $val"
drhf7141992008-06-19 00:16:08 +00001217 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
1218 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
dan17c08232015-06-09 15:58:28 +00001219 output1 "Scratch memory used: $val"
drhf7141992008-06-19 00:16:08 +00001220 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +00001221 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
1222 set val [format {now %10d max %10d max-size %10d} \
1223 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
dan17c08232015-06-09 15:58:28 +00001224 output1 "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +00001225 ifcapable yytrackmaxstackdepth {
1226 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
1227 set val [format { max %10d} [lindex $x 2]]
dan17c08232015-06-09 15:58:28 +00001228 output2 "Parser stack depth: $val"
drhec424a52008-07-25 15:39:03 +00001229 }
drhf7141992008-06-19 00:16:08 +00001230}
1231
drh348784e2000-05-29 20:41:49 +00001232# A procedure to execute SQL
1233#
drhc4a3c772001-04-04 11:48:57 +00001234proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +00001235 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +00001236 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +00001237}
drh3c2aeae2014-01-24 14:37:44 +00001238proc execsql_timed {sql {db db}} {
1239 set tm [time {
1240 set x [uplevel [list $db eval $sql]]
1241 } 1]
1242 set tm [lindex $tm 0]
dan17c08232015-06-09 15:58:28 +00001243 output1 -nonewline " ([expr {$tm*0.001}]ms) "
drh3c2aeae2014-01-24 14:37:44 +00001244 set x
1245}
drh3aadb2e2000-05-31 17:59:25 +00001246
drhadbca9c2001-09-27 15:11:53 +00001247# Execute SQL and catch exceptions.
1248#
1249proc catchsql {sql {db db}} {
1250 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +00001251 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +00001252 lappend r $msg
1253 return $r
1254}
1255
drh04096482001-11-09 22:41:44 +00001256# Do an VDBE code dump on the SQL given
1257#
1258proc explain {sql {db db}} {
mistachkineeb31ff2015-06-10 23:02:38 +00001259 output2 ""
1260 output2 "addr opcode p1 p2 p3 p4 p5 #"
1261 output2 "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +00001262 $db eval "explain $sql" {} {
mistachkineeb31ff2015-06-10 23:02:38 +00001263 output2 [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
danielk1977287fb612008-01-04 19:10:28 +00001264 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
1265 ]
drh04096482001-11-09 22:41:44 +00001266 }
1267}
1268
dane83267d2013-11-05 14:19:22 +00001269proc explain_i {sql {db db}} {
mistachkineeb31ff2015-06-10 23:02:38 +00001270 output2 ""
1271 output2 "addr opcode p1 p2 p3 p4 p5 #"
1272 output2 "---- ------------ ------ ------ ------ ---------------- -- -"
dane83267d2013-11-05 14:19:22 +00001273
dane83267d2013-11-05 14:19:22 +00001274
dan5c663c32013-11-06 14:52:40 +00001275 # Set up colors for the different opcodes. Scheme is as follows:
1276 #
1277 # Red: Opcodes that write to a b-tree.
1278 # Blue: Opcodes that reposition or seek a cursor.
1279 # Green: The ResultRow opcode.
1280 #
dandea63f22014-03-03 16:48:47 +00001281 if { [catch {fconfigure stdout -mode}]==0 } {
1282 set R "\033\[31;1m" ;# Red fg
1283 set G "\033\[32;1m" ;# Green fg
1284 set B "\033\[34;1m" ;# Red fg
1285 set D "\033\[39;0m" ;# Default fg
1286 } else {
1287 set R ""
1288 set G ""
1289 set B ""
1290 set D ""
1291 }
dan5c663c32013-11-06 14:52:40 +00001292 foreach opcode {
dan8da209b2016-07-26 18:06:08 +00001293 Seek SeekGE SeekGT SeekLE SeekLT NotFound Last Rewind
dane3ab7292013-11-12 12:30:09 +00001294 NoConflict Next Prev VNext VPrev VFilter
dan8da209b2016-07-26 18:06:08 +00001295 SorterSort SorterNext NextIfOpen
dan5c663c32013-11-06 14:52:40 +00001296 } {
1297 set color($opcode) $B
1298 }
1299 foreach opcode {ResultRow} {
1300 set color($opcode) $G
1301 }
1302 foreach opcode {IdxInsert Insert Delete IdxDelete} {
1303 set color($opcode) $R
1304 }
1305
dan427ebba2013-11-05 16:39:31 +00001306 set bSeenGoto 0
dane83267d2013-11-05 14:19:22 +00001307 $db eval "explain $sql" {} {
1308 set x($addr) 0
1309 set op($addr) $opcode
1310
dan427ebba2013-11-05 16:39:31 +00001311 if {$opcode == "Goto" && ($bSeenGoto==0 || ($p2 > $addr+10))} {
1312 set linebreak($p2) 1
1313 set bSeenGoto 1
dane83267d2013-11-05 14:19:22 +00001314 }
1315
dan8da209b2016-07-26 18:06:08 +00001316 if {$opcode=="Once"} {
1317 for {set i $addr} {$i<$p2} {incr i} {
1318 set star($i) $addr
1319 }
1320 }
1321
dane3ab7292013-11-12 12:30:09 +00001322 if {$opcode=="Next" || $opcode=="Prev"
1323 || $opcode=="VNext" || $opcode=="VPrev"
dan8da209b2016-07-26 18:06:08 +00001324 || $opcode=="SorterNext" || $opcode=="NextIfOpen"
dane3ab7292013-11-12 12:30:09 +00001325 } {
dane83267d2013-11-05 14:19:22 +00001326 for {set i $p2} {$i<$addr} {incr i} {
1327 incr x($i) 2
1328 }
1329 }
1330
1331 if {$opcode == "Goto" && $p2<$addr && $op($p2)=="Yield"} {
1332 for {set i [expr $p2+1]} {$i<$addr} {incr i} {
1333 incr x($i) 2
1334 }
1335 }
dan427ebba2013-11-05 16:39:31 +00001336
1337 if {$opcode == "Halt" && $comment == "End of coroutine"} {
1338 set linebreak([expr $addr+1]) 1
1339 }
dane83267d2013-11-05 14:19:22 +00001340 }
1341
1342 $db eval "explain $sql" {} {
dan427ebba2013-11-05 16:39:31 +00001343 if {[info exists linebreak($addr)]} {
mistachkineeb31ff2015-06-10 23:02:38 +00001344 output2 ""
dane83267d2013-11-05 14:19:22 +00001345 }
1346 set I [string repeat " " $x($addr)]
dan5c663c32013-11-06 14:52:40 +00001347
dan8da209b2016-07-26 18:06:08 +00001348 if {[info exists star($addr)]} {
1349 set ii [expr $x($star($addr))]
1350 append I " "
1351 set I [string replace $I $ii $ii *]
1352 }
1353
dan5c663c32013-11-06 14:52:40 +00001354 set col ""
1355 catch { set col $color($opcode) }
1356
mistachkineeb31ff2015-06-10 23:02:38 +00001357 output2 [format {%-4d %s%s%-12.12s%s %-6d %-6d %-6d % -17s %s %s} \
dan5c663c32013-11-06 14:52:40 +00001358 $addr $I $col $opcode $D $p1 $p2 $p3 $p4 $p5 $comment
dane83267d2013-11-05 14:19:22 +00001359 ]
dane83267d2013-11-05 14:19:22 +00001360 }
mistachkineeb31ff2015-06-10 23:02:38 +00001361 output2 "---- ------------ ------ ------ ------ ---------------- -- -"
dane83267d2013-11-05 14:19:22 +00001362}
1363
drhdafc0ce2008-04-17 19:14:02 +00001364# Show the VDBE program for an SQL statement but omit the Trace
1365# opcode at the beginning. This procedure can be used to prove
1366# that different SQL statements generate exactly the same VDBE code.
1367#
1368proc explain_no_trace {sql} {
1369 set tr [db eval "EXPLAIN $sql"]
1370 return [lrange $tr 7 end]
1371}
1372
drh3aadb2e2000-05-31 17:59:25 +00001373# Another procedure to execute SQL. This one includes the field
1374# names in the returned list.
1375#
1376proc execsql2 {sql} {
1377 set result {}
1378 db eval $sql data {
1379 foreach f $data(*) {
1380 lappend result $f $data($f)
1381 }
1382 }
1383 return $result
1384}
drh17a68932001-01-31 13:28:08 +00001385
mistachkin1d406e02013-08-29 01:09:14 +00001386# Use a temporary in-memory database to execute SQL statements
1387#
1388proc memdbsql {sql} {
1389 sqlite3 memdb :memory:
1390 set result [memdb eval $sql]
1391 memdb close
1392 return $result
1393}
1394
drhdde85d92003-03-01 19:45:34 +00001395# Use the non-callback API to execute multiple SQL statements
1396#
1397proc stepsql {dbptr sql} {
1398 set sql [string trim $sql]
1399 set r 0
1400 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +00001401 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +00001402 return [list 1 $vm]
1403 }
1404 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +00001405# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
1406# foreach v $VAL {lappend r $v}
1407# }
1408 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
1409 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
1410 lappend r [sqlite3_column_text $vm $i]
1411 }
drhdde85d92003-03-01 19:45:34 +00001412 }
danielk1977106bb232004-05-21 10:08:53 +00001413 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +00001414 return [list 1 $errmsg]
1415 }
1416 }
1417 return $r
1418}
1419
drh21504322002-06-25 13:16:02 +00001420# Do an integrity check of the entire database
1421#
danielk197704103022009-02-03 16:51:24 +00001422proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +00001423 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +00001424 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +00001425 }
1426}
1427
drh433dccf2013-02-09 15:37:11 +00001428# Check the extended error code
1429#
1430proc verify_ex_errcode {name expected {db db}} {
1431 do_test $name [list sqlite3_extended_errcode $db] $expected
1432}
1433
danc6055c72011-04-28 18:46:46 +00001434
1435# Return true if the SQL statement passed as the second argument uses a
1436# statement transaction.
1437#
1438proc sql_uses_stmt {db sql} {
1439 set stmt [sqlite3_prepare $db $sql -1 dummy]
1440 set uses [uses_stmt_journal $stmt]
1441 sqlite3_finalize $stmt
1442 return $uses
1443}
1444
danielk1977db4e8862008-01-16 08:24:46 +00001445proc fix_ifcapable_expr {expr} {
1446 set ret ""
1447 set state 0
1448 for {set i 0} {$i < [string length $expr]} {incr i} {
1449 set char [string range $expr $i $i]
1450 set newstate [expr {[string is alnum $char] || $char eq "_"}]
1451 if {$newstate && !$state} {
1452 append ret {$::sqlite_options(}
1453 }
1454 if {!$newstate && $state} {
1455 append ret )
1456 }
1457 append ret $char
1458 set state $newstate
1459 }
1460 if {$state} {append ret )}
1461 return $ret
1462}
1463
mistachkinc60941f2012-09-13 01:51:02 +00001464# Returns non-zero if the capabilities are present; zero otherwise.
1465#
1466proc capable {expr} {
1467 set e [fix_ifcapable_expr $expr]; return [expr ($e)]
1468}
1469
drh27d258a2004-10-30 20:23:09 +00001470# Evaluate a boolean expression of capabilities. If true, execute the
1471# code. Omit the code if false.
1472#
danielk19773e8c37e2005-01-21 03:12:14 +00001473proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +00001474 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
1475 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +00001476 if ($e2) {
1477 set c [catch {uplevel 1 $code} r]
1478 } else {
1479 set c [catch {uplevel 1 $elsecode} r]
1480 }
1481 return -code $c $r
drh21504322002-06-25 13:16:02 +00001482}
danielk197745901d62004-11-10 15:27:38 +00001483
danielk1977aca790a2005-01-13 11:07:52 +00001484# This proc execs a seperate process that crashes midway through executing
1485# the SQL script $sql on database test.db.
1486#
1487# The crash occurs during a sync() of file $crashfile. When the crash
1488# occurs a random subset of all unsynced writes made by the process are
1489# written into the files on disk. Argument $crashdelay indicates the
1490# number of file syncs to wait before crashing.
1491#
1492# The return value is a list of two elements. The first element is a
1493# boolean, indicating whether or not the process actually crashed or
1494# reported some other error. The second element in the returned list is the
1495# error message. This is "child process exited abnormally" if the crash
mistachkin48864df2013-03-21 21:20:32 +00001496# occurred.
danielk1977aca790a2005-01-13 11:07:52 +00001497#
danielk1977f8940ae2007-08-23 11:07:10 +00001498# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +00001499#
1500proc crashsql {args} {
danielk197759a33f92007-03-17 10:26:59 +00001501
1502 set blocksize ""
1503 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +00001504 set prngseed 0
dan999cd082013-12-09 20:42:03 +00001505 set opendb { sqlite3 db test.db -vfs crash }
drhf8587402008-01-08 16:03:49 +00001506 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +00001507 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +00001508 set dc ""
danielk197759a33f92007-03-17 10:26:59 +00001509 set sql [lindex $args end]
mistachkin1d406e02013-08-29 01:09:14 +00001510
danielk197759a33f92007-03-17 10:26:59 +00001511 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
1512 set z [lindex $args $ii]
1513 set n [string length $z]
1514 set z2 [lindex $args [expr $ii+1]]
1515
1516 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
dan999cd082013-12-09 20:42:03 +00001517 elseif {$n>1 && [string first $z -opendb]==0} {set opendb $z2} \
drhcb1f0f62008-01-08 15:18:52 +00001518 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +00001519 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +00001520 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +00001521 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +00001522 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +00001523 else { error "Unrecognized option: $z" }
1524 }
1525
1526 if {$crashfile eq ""} {
1527 error "Compulsory option -file missing"
1528 }
1529
mistachkin1d406e02013-08-29 01:09:14 +00001530 # $crashfile gets compared to the native filename in
shanehf2c08822010-07-08 16:30:44 +00001531 # cfSync(), which can be different then what TCL uses by
1532 # default, so here we force it to the "nativename" format.
mistachkinf8a78462012-03-08 20:00:36 +00001533 set cfile [string map {\\ \\\\} [file nativename [file join [get_pwd] $crashfile]]]
danielk1977aca790a2005-01-13 11:07:52 +00001534
1535 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +00001536 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +00001537 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +00001538 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk1977933bbd62007-03-17 07:22:42 +00001539
1540 # This block sets the cache size of the main database to 10
1541 # pages. This is done in case the build is configured to omit
1542 # "PRAGMA cache_size".
danec16d982015-04-17 08:36:05 +00001543 if {$opendb!=""} {
1544 puts $f $opendb
1545 puts $f {db eval {SELECT * FROM sqlite_master;}}
1546 puts $f {set bt [btree_from_db db]}
1547 puts $f {btree_set_cache_size $bt 10}
1548 }
dan999cd082013-12-09 20:42:03 +00001549
drhcb1f0f62008-01-08 15:18:52 +00001550 if {$prngseed} {
1551 set seed [expr {$prngseed%10007+1}]
1552 # puts seed=$seed
1553 puts $f "db eval {SELECT randomblob($seed)}"
1554 }
danielk1977933bbd62007-03-17 07:22:42 +00001555
drhf8587402008-01-08 16:03:49 +00001556 if {[string length $tclbody]>0} {
1557 puts $f $tclbody
1558 }
1559 if {[string length $sql]>0} {
1560 puts $f "db eval {"
1561 puts $f "$sql"
1562 puts $f "}"
1563 }
danielk1977aca790a2005-01-13 11:07:52 +00001564 close $f
danielk1977aca790a2005-01-13 11:07:52 +00001565 set r [catch {
drh9c06c952005-11-26 00:25:00 +00001566 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +00001567 } msg]
mistachkin1d406e02013-08-29 01:09:14 +00001568
shanehf2c08822010-07-08 16:30:44 +00001569 # Windows/ActiveState TCL returns a slightly different
1570 # error message. We map that to the expected message
1571 # so that we don't have to change all of the test
1572 # cases.
1573 if {$::tcl_platform(platform)=="windows"} {
1574 if {$msg=="child killed: unknown signal"} {
1575 set msg "child process exited abnormally"
1576 }
1577 }
mistachkin1d406e02013-08-29 01:09:14 +00001578
danielk1977aca790a2005-01-13 11:07:52 +00001579 lappend r $msg
1580}
1581
danb88e24f2013-02-23 18:58:11 +00001582proc run_ioerr_prep {} {
1583 set ::sqlite_io_error_pending 0
1584 catch {db close}
1585 catch {db2 close}
1586 catch {forcedelete test.db}
1587 catch {forcedelete test.db-journal}
1588 catch {forcedelete test2.db}
1589 catch {forcedelete test2.db-journal}
1590 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
1591 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
1592 if {[info exists ::ioerropts(-tclprep)]} {
1593 eval $::ioerropts(-tclprep)
1594 }
1595 if {[info exists ::ioerropts(-sqlprep)]} {
1596 execsql $::ioerropts(-sqlprep)
1597 }
1598 expr 0
1599}
1600
danielk197732554c12005-01-22 03:39:39 +00001601# Usage: do_ioerr_test <test number> <options...>
1602#
1603# This proc is used to implement test cases that check that IO errors
mistachkin1d406e02013-08-29 01:09:14 +00001604# are correctly handled. The first argument, <test number>, is an integer
danielk197732554c12005-01-22 03:39:39 +00001605# used to name the tests executed by this proc. Options are as follows:
1606#
1607# -tclprep TCL script to run to prepare test.
1608# -sqlprep SQL script to run to prepare test.
1609# -tclbody TCL script to run with IO error simulation.
1610# -sqlbody TCL script to run with IO error simulation.
1611# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +00001612# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +00001613# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +00001614# -start Value of 'N' to begin with (default 1)
1615#
1616# -cksum Boolean. If true, test that the database does
1617# not change during the execution of the test case.
1618#
1619proc do_ioerr_test {testname args} {
1620
danielk197732554c12005-01-22 03:39:39 +00001621 set ::ioerropts(-start) 1
1622 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +00001623 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +00001624 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +00001625 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +00001626 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +00001627 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +00001628 array set ::ioerropts $args
1629
danielk197747cd39c2008-05-12 12:41:15 +00001630 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
1631 # a couple of obscure IO errors that do not return them.
1632 set ::ioerropts(-erc) 0
mistachkin1d406e02013-08-29 01:09:14 +00001633
danb88e24f2013-02-23 18:58:11 +00001634 # Create a single TCL script from the TCL and SQL specified
1635 # as the body of the test.
1636 set ::ioerrorbody {}
1637 if {[info exists ::ioerropts(-tclbody)]} {
1638 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
1639 }
1640 if {[info exists ::ioerropts(-sqlbody)]} {
1641 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
1642 }
1643
1644 save_prng_state
1645 if {$::ioerropts(-cksum)} {
1646 run_ioerr_prep
1647 eval $::ioerrorbody
1648 set ::goodcksum [cksum]
1649 }
danielk197747cd39c2008-05-12 12:41:15 +00001650
danielk197732554c12005-01-22 03:39:39 +00001651 set ::go 1
drh93aed5a2008-01-16 17:46:38 +00001652 #reset_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +00001653 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +00001654 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +00001655 incr ::ioerropts(-count) -1
1656 if {$::ioerropts(-count)<0} break
mistachkin1d406e02013-08-29 01:09:14 +00001657
danielk197732554c12005-01-22 03:39:39 +00001658 # Skip this IO error if it was specified with the "-exclude" option.
1659 if {[info exists ::ioerropts(-exclude)]} {
1660 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
1661 }
danielk1977861f7452008-06-05 11:39:11 +00001662 if {$::ioerropts(-restoreprng)} {
1663 restore_prng_state
1664 }
danielk197732554c12005-01-22 03:39:39 +00001665
mistachkin1d406e02013-08-29 01:09:14 +00001666 # Delete the files test.db and test2.db, then execute the TCL and
danielk197732554c12005-01-22 03:39:39 +00001667 # SQL (in that order) to prepare for the test case.
1668 do_test $testname.$n.1 {
danb88e24f2013-02-23 18:58:11 +00001669 run_ioerr_prep
danielk197732554c12005-01-22 03:39:39 +00001670 } {0}
1671
1672 # Read the 'checksum' of the database.
1673 if {$::ioerropts(-cksum)} {
danb88e24f2013-02-23 18:58:11 +00001674 set ::checksum [cksum]
danielk197732554c12005-01-22 03:39:39 +00001675 }
drh93aed5a2008-01-16 17:46:38 +00001676
danielk197732554c12005-01-22 03:39:39 +00001677 # Set the Nth IO error to fail.
1678 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +00001679 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +00001680 set ::sqlite_io_error_pending $n
1681 }] $n
danielk197732554c12005-01-22 03:39:39 +00001682
danb88e24f2013-02-23 18:58:11 +00001683 # Execute the TCL script created for the body of this test. If
mistachkin1d406e02013-08-29 01:09:14 +00001684 # at least N IO operations performed by SQLite as a result of
danb88e24f2013-02-23 18:58:11 +00001685 # the script, the Nth will fail.
danielk197732554c12005-01-22 03:39:39 +00001686 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +00001687 set ::sqlite_io_error_hit 0
1688 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +00001689 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +00001690 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +00001691 set rc [sqlite3_errcode $::DB]
1692 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +00001693 # If we are in extended result code mode, make sure all of the
1694 # IOERRs we get back really do have their extended code values.
1695 # If an extended result code is returned, the sqlite3_errcode
1696 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
1697 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +00001698 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
1699 return $rc
1700 }
1701 } else {
drh5f7b5bf2007-04-19 12:30:54 +00001702 # If we are not in extended result code mode, make sure no
1703 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +00001704 if {[regexp {\+\d} $rc]} {
1705 return $rc
1706 }
1707 }
drh93aed5a2008-01-16 17:46:38 +00001708 # The test repeats as long as $::go is non-zero. $::go starts out
1709 # as 1. When a test runs to completion without hitting an I/O
1710 # error, that means there is no point in continuing with this test
1711 # case so set $::go to zero.
1712 #
1713 if {$::sqlite_io_error_pending>0} {
1714 set ::go 0
1715 set q 0
1716 set ::sqlite_io_error_pending 0
1717 } else {
1718 set q 1
1719 }
1720
drhc9ac5ca2005-11-04 22:03:30 +00001721 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +00001722 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
1723 set r 1
1724 }
danielk1977f2fa8312006-01-24 13:09:33 +00001725 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +00001726
1727 # One of two things must have happened. either
1728 # 1. We never hit the IO error and the SQL returned OK
1729 # 2. An IO error was hit and the SQL failed
1730 #
dand41a29a2010-05-06 15:56:28 +00001731 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +00001732 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +00001733 } {1}
1734
danielk197780daec62008-05-12 10:57:02 +00001735 set ::sqlite_io_error_hit 0
1736 set ::sqlite_io_error_pending 0
1737
mistachkin1d406e02013-08-29 01:09:14 +00001738 # Check that no page references were leaked. There should be
1739 # a single reference if there is still an active transaction,
danielk197752b472a2008-05-05 16:23:55 +00001740 # or zero otherwise.
1741 #
danielk197781620542008-06-07 05:19:37 +00001742 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
mistachkin1d406e02013-08-29 01:09:14 +00001743 # locks are established on database files (i.e. if the error
danielk197781620542008-06-07 05:19:37 +00001744 # occurs while attempting to detect a hot-journal file), then
1745 # there may 0 page references and an active transaction according
1746 # to [sqlite3_get_autocommit].
1747 #
danielk19774abd5442008-05-05 15:26:50 +00001748 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +00001749 do_test $testname.$n.4 {
1750 set bt [btree_from_db db]
1751 db_enter db
1752 array set stats [btree_pager_stats $bt]
1753 db_leave db
danielk197781620542008-06-07 05:19:37 +00001754 set nRef $stats(ref)
1755 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
1756 } {1}
danielk19774abd5442008-05-05 15:26:50 +00001757 }
1758
mistachkin1d406e02013-08-29 01:09:14 +00001759 # If there is an open database handle and no open transaction,
danielk197752b472a2008-05-05 16:23:55 +00001760 # and the pager is not running in exclusive-locking mode,
1761 # check that the pager is in "unlocked" state. Theoretically,
1762 # if a call to xUnlock() failed due to an IO error the underlying
1763 # file may still be locked.
1764 #
1765 ifcapable pragma {
1766 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +00001767 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +00001768 && [db one {pragma locking_mode}] eq "normal"
1769 && [sqlite3_get_autocommit db]
1770 } {
1771 do_test $testname.$n.5 {
1772 set bt [btree_from_db db]
1773 db_enter db
1774 array set stats [btree_pager_stats $bt]
1775 db_leave db
1776 set stats(state)
1777 } 0
1778 }
1779 }
1780
mistachkin48864df2013-03-21 21:20:32 +00001781 # If an IO error occurred, then the checksum of the database should
danielk197732554c12005-01-22 03:39:39 +00001782 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +00001783 #
drh1aa5af12008-03-07 19:51:14 +00001784 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +00001785 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +00001786 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +00001787 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +00001788 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danb88e24f2013-02-23 18:58:11 +00001789 set nowcksum [cksum]
1790 set res [expr {$nowcksum==$::checksum || $nowcksum==$::goodcksum}]
1791 if {$res==0} {
dan17c08232015-06-09 15:58:28 +00001792 output2 "now=$nowcksum"
1793 output2 "the=$::checksum"
1794 output2 "fwd=$::goodcksum"
danb88e24f2013-02-23 18:58:11 +00001795 }
1796 set res
1797 } 1
danielk197732554c12005-01-22 03:39:39 +00001798 }
1799
drh1aa5af12008-03-07 19:51:14 +00001800 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +00001801 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +00001802 if {[info exists ::ioerropts(-cleanup)]} {
1803 catch $::ioerropts(-cleanup)
1804 }
danielk197732554c12005-01-22 03:39:39 +00001805 }
1806 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +00001807 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +00001808 unset ::ioerropts
1809}
1810
drh93aed5a2008-01-16 17:46:38 +00001811# Return a checksum based on the contents of the main database associated
1812# with connection $db
danielk197732554c12005-01-22 03:39:39 +00001813#
1814proc cksum {{db db}} {
1815 set txt [$db eval {
1816 SELECT name, type, sql FROM sqlite_master order by name
1817 }]\n
1818 foreach tbl [$db eval {
1819 SELECT name FROM sqlite_master WHERE type='table' order by name
1820 }] {
1821 append txt [$db eval "SELECT * FROM $tbl"]\n
1822 }
1823 foreach prag {default_synchronous default_cache_size} {
1824 append txt $prag-[$db eval "PRAGMA $prag"]\n
1825 }
1826 set cksum [string length $txt]-[md5 $txt]
1827 # puts $cksum-[file size test.db]
1828 return $cksum
1829}
1830
drh93aed5a2008-01-16 17:46:38 +00001831# Generate a checksum based on the contents of the main and temp tables
1832# database $db. If the checksum of two databases is the same, and the
1833# integrity-check passes for both, the two databases are identical.
1834#
drh1db639c2008-01-17 02:36:28 +00001835proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +00001836 set ret [list]
1837 ifcapable tempdb {
1838 set sql {
1839 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1840 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
1841 SELECT 'sqlite_master' UNION
1842 SELECT 'sqlite_temp_master' ORDER BY 1
1843 }
1844 } else {
1845 set sql {
1846 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1847 SELECT 'sqlite_master' ORDER BY 1
1848 }
1849 }
1850 set tbllist [$db eval $sql]
1851 set txt {}
1852 foreach tbl $tbllist {
1853 append txt [$db eval "SELECT * FROM $tbl"]
1854 }
1855 foreach prag {default_cache_size} {
1856 append txt $prag-[$db eval "PRAGMA $prag"]\n
1857 }
1858 # puts txt=$txt
1859 return [md5 $txt]
1860}
1861
drhdc2c4912009-02-04 22:46:47 +00001862# Generate a checksum based on the contents of a single database with
mistachkin1d406e02013-08-29 01:09:14 +00001863# a database connection. The name of the database is $dbname.
drhdc2c4912009-02-04 22:46:47 +00001864# Examples of $dbname are "temp" or "main".
1865#
1866proc dbcksum {db dbname} {
1867 if {$dbname=="temp"} {
1868 set master sqlite_temp_master
1869 } else {
1870 set master $dbname.sqlite_master
1871 }
1872 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
1873 set txt [$db eval "SELECT * FROM $master"]\n
1874 foreach tab $alltab {
1875 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
1876 }
1877 return [md5 $txt]
1878}
1879
danielk197735754ac2008-03-21 17:29:37 +00001880proc memdebug_log_sql {{filename mallocs.sql}} {
1881
danielk19776f332c12008-03-21 14:22:44 +00001882 set data [sqlite3_memdebug_log dump]
1883 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +00001884 if {$nFrame < 0} { return "" }
1885
danielk197735754ac2008-03-21 17:29:37 +00001886 set database temp
1887
danielk19776ab3a2e2009-02-19 14:39:25 +00001888 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +00001889
1890 set sql ""
1891 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +00001892 set nCall [lindex $e 0]
1893 set nByte [lindex $e 1]
1894 set lStack [lrange $e 2 end]
1895 append sql "INSERT INTO ${database}.malloc VALUES"
1896 append sql "('test', $nCall, $nByte, '$lStack');\n"
1897 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +00001898 set frames($f) 1
1899 }
1900 }
1901
1902 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +00001903 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +00001904
1905 foreach f [array names frames] {
1906 set addr [format %x $f]
1907 set cmd "addr2line -e [info nameofexec] $addr"
1908 set line [eval exec $cmd]
1909 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00001910
1911 set file [lindex [split $line :] 0]
1912 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00001913 }
1914
danielk197735754ac2008-03-21 17:29:37 +00001915 foreach f [array names files] {
1916 set contents ""
1917 catch {
1918 set fd [open $f]
1919 set contents [read $fd]
1920 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001921 }
danielk197735754ac2008-03-21 17:29:37 +00001922 set contents [string map {' ''} $contents]
1923 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00001924 }
danielk19776f332c12008-03-21 14:22:44 +00001925
danielk197735754ac2008-03-21 17:29:37 +00001926 set fd [open $filename w]
1927 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
1928 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001929}
drh93aed5a2008-01-16 17:46:38 +00001930
danf5894502009-10-07 18:41:19 +00001931# Drop all tables in database [db]
1932proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00001933 ifcapable trigger&&foreignkey {
1934 set pk [$db one "PRAGMA foreign_keys"]
1935 $db eval "PRAGMA foreign_keys = OFF"
1936 }
drh9a6ffc82010-02-15 18:03:20 +00001937 foreach {idx name file} [db eval {PRAGMA database_list}] {
1938 if {$idx==1} {
1939 set master sqlite_temp_master
1940 } else {
1941 set master $name.sqlite_master
1942 }
1943 foreach {t type} [$db eval "
1944 SELECT name, type FROM $master
dana16d1062010-09-28 17:37:28 +00001945 WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X'
drh9a6ffc82010-02-15 18:03:20 +00001946 "] {
dana16d1062010-09-28 17:37:28 +00001947 $db eval "DROP $type \"$t\""
drh9a6ffc82010-02-15 18:03:20 +00001948 }
danf5894502009-10-07 18:41:19 +00001949 }
shaneh4e7b32f2009-12-17 22:12:51 +00001950 ifcapable trigger&&foreignkey {
1951 $db eval "PRAGMA foreign_keys = $pk"
1952 }
danf5894502009-10-07 18:41:19 +00001953}
1954
dand05a7142016-08-02 17:07:51 +00001955# Drop all auxiliary indexes from the main database opened by handle [db].
1956#
1957proc drop_all_indexes {{db db}} {
1958 set L [$db eval {
1959 SELECT name FROM sqlite_master WHERE type='index' AND sql LIKE 'create%'
1960 }]
1961 foreach idx $L { $db eval "DROP INDEX $idx" }
1962}
1963
1964
dan71cb5182010-04-26 12:39:03 +00001965#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00001966# If a test script is executed with global variable $::G(perm:name) set to
mistachkin1d406e02013-08-29 01:09:14 +00001967# "wal", then the tests are run in WAL mode. Otherwise, they should be run
1968# in rollback mode. The following Tcl procs are used to make this less
dan430e74c2010-06-07 17:47:26 +00001969# intrusive:
dan71cb5182010-04-26 12:39:03 +00001970#
1971# wal_set_journal_mode ?DB?
1972#
1973# If running a WAL test, execute "PRAGMA journal_mode = wal" using
1974# connection handle DB. Otherwise, this command is a no-op.
1975#
1976# wal_check_journal_mode TESTNAME ?DB?
1977#
1978# If running a WAL test, execute a tests case that fails if the main
1979# database for connection handle DB is not currently a WAL database.
1980# Otherwise (if not running a WAL permutation) this is a no-op.
1981#
1982# wal_is_wal_mode
mistachkin1d406e02013-08-29 01:09:14 +00001983#
dan71cb5182010-04-26 12:39:03 +00001984# Returns true if this test should be run in WAL mode. False otherwise.
mistachkin1d406e02013-08-29 01:09:14 +00001985#
dan71cb5182010-04-26 12:39:03 +00001986proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00001987 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00001988}
1989proc wal_set_journal_mode {{db db}} {
1990 if { [wal_is_wal_mode] } {
1991 $db eval "PRAGMA journal_mode = WAL"
1992 }
1993}
1994proc wal_check_journal_mode {testname {db db}} {
1995 if { [wal_is_wal_mode] } {
1996 $db eval { SELECT * FROM sqlite_master }
1997 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
1998 }
1999}
2000
dan05accd22016-04-27 18:54:49 +00002001proc wal_is_capable {} {
2002 ifcapable !wal { return 0 }
2003 if {[permutation]=="journaltest"} { return 0 }
2004 return 1
2005}
2006
dan430e74c2010-06-07 17:47:26 +00002007proc permutation {} {
2008 set perm ""
2009 catch {set perm $::G(perm:name)}
2010 set perm
2011}
dancb79e512010-08-06 13:50:07 +00002012proc presql {} {
2013 set presql ""
2014 catch {set presql $::G(perm:presql)}
2015 set presql
2016}
dan430e74c2010-06-07 17:47:26 +00002017
danbe7721d2016-02-04 17:31:03 +00002018proc isquick {} {
2019 set ret 0
2020 catch {set ret $::G(isquick)}
2021 set ret
2022}
2023
danc1a60c52010-06-07 14:28:16 +00002024#-------------------------------------------------------------------------
2025#
2026proc slave_test_script {script} {
2027
2028 # Create the interpreter used to run the test script.
2029 interp create tinterp
2030
2031 # Populate some global variables that tester.tcl expects to see.
2032 foreach {var value} [list \
2033 ::argv0 $::argv0 \
2034 ::argv {} \
2035 ::SLAVE 1 \
2036 ] {
2037 interp eval tinterp [list set $var $value]
2038 }
2039
dan17c08232015-06-09 15:58:28 +00002040 # If output is being copied into a file, share the file-descriptor with
2041 # the interpreter.
2042 if {[info exists ::G(output_fd)]} {
2043 interp share {} $::G(output_fd) tinterp
2044 }
2045
danc1a60c52010-06-07 14:28:16 +00002046 # The alias used to access the global test counters.
2047 tinterp alias set_test_counter set_test_counter
2048
2049 # Set up the ::cmdlinearg array in the slave.
2050 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
2051
dan430e74c2010-06-07 17:47:26 +00002052 # Set up the ::G array in the slave.
2053 interp eval tinterp [list array set ::G [array get ::G]]
2054
danc1a60c52010-06-07 14:28:16 +00002055 # Load the various test interfaces implemented in C.
2056 load_testfixture_extensions tinterp
2057
2058 # Run the test script.
2059 interp eval tinterp $script
2060
danea5542d2010-07-06 11:26:15 +00002061 # Check if the interpreter call [run_thread_tests]
2062 if { [interp eval tinterp {info exists ::run_thread_tests_called}] } {
2063 set ::run_thread_tests_called 1
2064 }
2065
danc1a60c52010-06-07 14:28:16 +00002066 # Delete the interpreter used to run the test script.
2067 interp delete tinterp
2068}
2069
dan430e74c2010-06-07 17:47:26 +00002070proc slave_test_file {zFile} {
dan0626dfc2010-06-15 06:56:37 +00002071 set tail [file tail $zFile]
dan430e74c2010-06-07 17:47:26 +00002072
dan6a64d672011-04-04 15:38:16 +00002073 if {[info exists ::G(start:permutation)]} {
2074 if {[permutation] != $::G(start:permutation)} return
2075 unset ::G(start:permutation)
2076 }
2077 if {[info exists ::G(start:file)]} {
2078 if {$tail != $::G(start:file) && $tail!="$::G(start:file).test"} return
2079 unset ::G(start:file)
2080 }
2081
dan92d516a2010-07-05 14:54:48 +00002082 # Remember the value of the shared-cache setting. So that it is possible
2083 # to check afterwards that it was not modified by the test script.
2084 #
2085 ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] }
dan0626dfc2010-06-15 06:56:37 +00002086
dan92d516a2010-07-05 14:54:48 +00002087 # Run the test script in a slave interpreter.
2088 #
danea5542d2010-07-06 11:26:15 +00002089 unset -nocomplain ::run_thread_tests_called
dan0626dfc2010-06-15 06:56:37 +00002090 reset_prng_state
2091 set ::sqlite_open_file_count 0
2092 set time [time { slave_test_script [list source $zFile] }]
2093 set ms [expr [lindex $time 0] / 1000]
2094
dan92d516a2010-07-05 14:54:48 +00002095 # Test that all files opened by the test script were closed. Omit this
2096 # if the test script has "thread" in its name. The open file counter
2097 # is not thread-safe.
dan0626dfc2010-06-15 06:56:37 +00002098 #
danea5542d2010-07-06 11:26:15 +00002099 if {[info exists ::run_thread_tests_called]==0} {
dan92d516a2010-07-05 14:54:48 +00002100 do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0}
2101 }
dan430e74c2010-06-07 17:47:26 +00002102 set ::sqlite_open_file_count 0
2103
mistachkin1d406e02013-08-29 01:09:14 +00002104 # Test that the global "shared-cache" setting was not altered by
dan0626dfc2010-06-15 06:56:37 +00002105 # the test script.
2106 #
mistachkin1d406e02013-08-29 01:09:14 +00002107 ifcapable shared_cache {
dan0626dfc2010-06-15 06:56:37 +00002108 set res [expr {[sqlite3_enable_shared_cache] == $scs}]
2109 do_test ${tail}-sharedcachesetting [list set {} $res] 1
2110 }
dan430e74c2010-06-07 17:47:26 +00002111
dan92d516a2010-07-05 14:54:48 +00002112 # Add some info to the output.
2113 #
dan17c08232015-06-09 15:58:28 +00002114 output2 "Time: $tail $ms ms"
dan430e74c2010-06-07 17:47:26 +00002115 show_memstats
danc1a60c52010-06-07 14:28:16 +00002116}
2117
dan59257dc2010-08-04 11:34:31 +00002118# Open a new connection on database test.db and execute the SQL script
2119# supplied as an argument. Before returning, close the new conection and
2120# restore the 4 byte fields starting at header offsets 28, 92 and 96
2121# to the values they held before the SQL was executed. This simulates
2122# a write by a pre-3.7.0 client.
2123#
2124proc sql36231 {sql} {
2125 set B [hexio_read test.db 92 8]
2126 set A [hexio_read test.db 28 4]
2127 sqlite3 db36231 test.db
2128 catch { db36231 func a_string a_string }
2129 execsql $sql db36231
2130 db36231 close
2131 hexio_write test.db 28 $A
2132 hexio_write test.db 92 $B
2133 return ""
2134}
danf5894502009-10-07 18:41:19 +00002135
danccc7ff42010-11-29 12:06:45 +00002136proc db_save {} {
2137 foreach f [glob -nocomplain sv_test.db*] { forcedelete $f }
2138 foreach f [glob -nocomplain test.db*] {
2139 set f2 "sv_$f"
mistachkinfda06be2011-08-02 00:57:34 +00002140 forcecopy $f $f2
danccc7ff42010-11-29 12:06:45 +00002141 }
2142}
2143proc db_save_and_close {} {
2144 db_save
2145 catch { db close }
2146 return ""
2147}
2148proc db_restore {} {
2149 foreach f [glob -nocomplain test.db*] { forcedelete $f }
2150 foreach f2 [glob -nocomplain sv_test.db*] {
2151 set f [string range $f2 3 end]
mistachkinfda06be2011-08-02 00:57:34 +00002152 forcecopy $f2 $f
danccc7ff42010-11-29 12:06:45 +00002153 }
2154}
2155proc db_restore_and_reopen {{dbfile test.db}} {
2156 catch { db close }
2157 db_restore
2158 sqlite3 db $dbfile
2159}
2160proc db_delete_and_reopen {{file test.db}} {
2161 catch { db close }
mistachkinfda06be2011-08-02 00:57:34 +00002162 foreach f [glob -nocomplain test.db*] { forcedelete $f }
danccc7ff42010-11-29 12:06:45 +00002163 sqlite3 db $file
2164}
2165
dan03bc5252015-07-24 14:17:17 +00002166# Close any connections named [db], [db2] or [db3]. Then use sqlite3_config
2167# to configure the size of the PAGECACHE allocation using the parameters
2168# provided to this command. Save the old PAGECACHE parameters in a global
2169# variable so that [test_restore_config_pagecache] can restore the previous
2170# configuration.
2171#
2172# Before returning, reopen connection [db] on file test.db.
2173#
2174proc test_set_config_pagecache {sz nPg} {
2175 catch {db close}
2176 catch {db2 close}
2177 catch {db3 close}
2178
2179 sqlite3_shutdown
2180 set ::old_pagecache_config [sqlite3_config_pagecache $sz $nPg]
2181 sqlite3_initialize
2182 autoinstall_test_functions
dand716c392015-07-24 18:22:29 +00002183 reset_db
dan03bc5252015-07-24 14:17:17 +00002184}
2185
2186# Close any connections named [db], [db2] or [db3]. Then use sqlite3_config
2187# to configure the size of the PAGECACHE allocation to the size saved in
2188# the global variable by an earlier call to [test_set_config_pagecache].
2189#
2190# Before returning, reopen connection [db] on file test.db.
2191#
2192proc test_restore_config_pagecache {} {
2193 catch {db close}
2194 catch {db2 close}
2195 catch {db3 close}
2196
2197 sqlite3_shutdown
2198 eval sqlite3_config_pagecache $::old_pagecache_config
2199 unset ::old_pagecache_config
2200 sqlite3_initialize
2201 autoinstall_test_functions
2202 sqlite3 db test.db
2203}
2204
dan1e8dae02016-03-19 14:53:36 +00002205proc test_find_binary {nm} {
dan089555c2016-03-15 09:55:44 +00002206 if {$::tcl_platform(platform)=="windows"} {
dan1e8dae02016-03-19 14:53:36 +00002207 set ret "$nm.exe"
dan089555c2016-03-15 09:55:44 +00002208 } else {
dan1e8dae02016-03-19 14:53:36 +00002209 set ret $nm
dan089555c2016-03-15 09:55:44 +00002210 }
2211 set ret [file normalize [file join $::cmdlinearg(TESTFIXTURE_HOME) $ret]]
2212 if {![file executable $ret]} {
2213 finish_test
dan49aed582016-03-19 15:13:59 +00002214 return ""
dan089555c2016-03-15 09:55:44 +00002215 }
2216 return $ret
2217}
2218
danielk197745901d62004-11-10 15:27:38 +00002219# Find the name of the 'shell' executable (e.g. "sqlite3.exe") to use for
2220# the tests in shell[1-5].test. If no such executable can be found, invoke
2221# [finish_test ; return] in the callers context.
danielk1977ee8b7992009-03-26 17:13:06 +00002222#
dan64b95bb2012-05-12 05:30:29 +00002223proc test_find_cli {} {
danf27d7372016-03-19 17:48:12 +00002224 set prog [test_find_binary sqlite3]
dan49aed582016-03-19 15:13:59 +00002225 if {$prog==""} { return -code return }
2226 return $prog
drh348784e2000-05-29 20:41:49 +00002227}
2228
dan1e8dae02016-03-19 14:53:36 +00002229# Find the name of the 'sqldiff' executable (e.g. "sqlite3.exe") to use for
2230# the tests in sqldiff tests. If no such executable can be found, invoke
2231# [finish_test ; return] in the callers context.
2232#
2233proc test_find_sqldiff {} {
dan49aed582016-03-19 15:13:59 +00002234 set prog [test_find_binary sqldiff]
2235 if {$prog==""} { return -code return }
2236 return $prog
dan1e8dae02016-03-19 14:53:36 +00002237}
2238
2239
drh348784e2000-05-29 20:41:49 +00002240# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
2241# to non-zero, then set the global variable $AUTOVACUUM to 1.
2242set AUTOVACUUM $sqlite_options(default_autovacuum)
2243
dan64b95bb2012-05-12 05:30:29 +00002244# Make sure the FTS enhanced query syntax is disabled.
2245set sqlite_fts3_enable_parentheses 0
2246
drh348784e2000-05-29 20:41:49 +00002247# During testing, assume that all database files are well-formed. The
2248# few test cases that deliberately corrupt database files should rescind
2249# this setting by invoking "database_can_be_corrupt"
2250#
2251database_never_corrupt
2252
drh348784e2000-05-29 20:41:49 +00002253source $testdir/thread_common.tcl
2254source $testdir/malloc_common.tcl