blob: f520080192afaa49fc95fd665fbe64cf43ae18a9 [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drh348784e2000-05-29 20:41:49 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drh348784e2000-05-29 20:41:49 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# May you do good and not evil.
7# May you find forgiveness for yourself and forgive others.
8# May you share freely, never taking more than you give.
drh348784e2000-05-29 20:41:49 +00009#
10#***********************************************************************
11# This file implements some common TCL routines used for regression
12# testing the SQLite library
13#
drhbb77b752009-04-09 01:23:49 +000014# $Id: tester.tcl,v 1.143 2009/04/09 01:23:49 drh Exp $
drhfbc3eab2001-04-06 16:13:42 +000015
danc1a60c52010-06-07 14:28:16 +000016#-------------------------------------------------------------------------
mistachkin1d406e02013-08-29 01:09:14 +000017# The commands provided by the code in this file to help with creating
danc1a60c52010-06-07 14:28:16 +000018# test cases are as follows:
drh8359d8c2008-03-29 11:00:54 +000019#
danc1a60c52010-06-07 14:28:16 +000020# Commands to manipulate the db and the file-system at a high level:
drh8359d8c2008-03-29 11:00:54 +000021#
mistachkinc5484652012-03-05 22:52:33 +000022# is_relative_file
mistachkin4a41f342012-03-06 03:00:49 +000023# test_pwd
mistachkinf8a78462012-03-08 20:00:36 +000024# get_pwd
danc1a60c52010-06-07 14:28:16 +000025# copy_file FROM TO
mistachkinfda06be2011-08-02 00:57:34 +000026# delete_file FILENAME
dan67340072011-04-16 19:23:10 +000027# drop_all_tables ?DB?
mistachkinfda06be2011-08-02 00:57:34 +000028# forcecopy FROM TO
danc1a60c52010-06-07 14:28:16 +000029# forcedelete FILENAME
30#
31# Test the capability of the SQLite version built into the interpreter to
32# determine if a specific test can be run:
33#
mistachkinc60941f2012-09-13 01:51:02 +000034# capable EXPR
danc1a60c52010-06-07 14:28:16 +000035# ifcapable EXPR
36#
37# Calulate checksums based on database contents:
38#
39# dbcksum DB DBNAME
40# allcksum ?DB?
41# cksum ?DB?
42#
43# Commands to execute/explain SQL statements:
44#
mistachkin1d406e02013-08-29 01:09:14 +000045# memdbsql SQL
danc1a60c52010-06-07 14:28:16 +000046# stepsql DB SQL
47# execsql2 SQL
48# explain_no_trace SQL
49# explain SQL ?DB?
50# catchsql SQL ?DB?
51# execsql SQL ?DB?
52#
53# Commands to run test cases:
54#
55# do_ioerr_test TESTNAME ARGS...
56# crashsql ARGS...
57# integrity_check TESTNAME ?DB?
drh433dccf2013-02-09 15:37:11 +000058# verify_ex_errcode TESTNAME EXPECTED ?DB?
danc1a60c52010-06-07 14:28:16 +000059# do_test TESTNAME SCRIPT EXPECTED
dan153eda02010-06-21 07:45:47 +000060# do_execsql_test TESTNAME SQL EXPECTED
61# do_catchsql_test TESTNAME SQL EXPECTED
drh3c2aeae2014-01-24 14:37:44 +000062# do_timed_execsql_test TESTNAME SQL EXPECTED
danc1a60c52010-06-07 14:28:16 +000063#
dan430e74c2010-06-07 17:47:26 +000064# Commands providing a lower level interface to the global test counters:
danc1a60c52010-06-07 14:28:16 +000065#
66# set_test_counter COUNTER ?VALUE?
mistachkin6c3c1a02011-11-12 03:17:40 +000067# omit_test TESTNAME REASON ?APPEND?
danc1a60c52010-06-07 14:28:16 +000068# fail_test TESTNAME
69# incr_ntest
70#
71# Command run at the end of each test file:
72#
73# finish_test
74#
dan430e74c2010-06-07 17:47:26 +000075# Commands to help create test files that run with the "WAL" and other
76# permutations (see file permutations.test):
danc1a60c52010-06-07 14:28:16 +000077#
78# wal_is_wal_mode
79# wal_set_journal_mode ?DB?
80# wal_check_journal_mode TESTNAME?DB?
dan430e74c2010-06-07 17:47:26 +000081# permutation
dancb79e512010-08-06 13:50:07 +000082# presql
danc1a60c52010-06-07 14:28:16 +000083#
dan17c08232015-06-09 15:58:28 +000084# Command to test whether or not --verbose=1 was specified on the command
85# line (returns 0 for not-verbose, 1 for verbose and 2 for "verbose in the
86# output file only").
87#
88# verbose
89#
drh348784e2000-05-29 20:41:49 +000090
mistachkin1d406e02013-08-29 01:09:14 +000091# Set the precision of FP arithmatic used by the interpreter. And
danc1a60c52010-06-07 14:28:16 +000092# configure SQLite to take database file locks on the page that begins
93# 64KB into the database file instead of the one 1GB in. This means
94# the code that handles that special case can be tested without creating
95# very large database files.
96#
drh92febd92004-08-20 18:34:20 +000097set tcl_precision 15
drhc7a3bb92009-02-05 16:31:45 +000098sqlite3_test_control_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +000099
danc1a60c52010-06-07 14:28:16 +0000100
mistachkin1d406e02013-08-29 01:09:14 +0000101# If the pager codec is available, create a wrapper for the [sqlite3]
danc1a60c52010-06-07 14:28:16 +0000102# command that appends "-key {xyzzy}" to the command line. i.e. this:
drh3a7fb7c2007-08-10 16:41:08 +0000103#
danc1a60c52010-06-07 14:28:16 +0000104# sqlite3 db test.db
drh4a50aac2007-08-23 02:47:53 +0000105#
danc1a60c52010-06-07 14:28:16 +0000106# becomes
drh4a50aac2007-08-23 02:47:53 +0000107#
danc1a60c52010-06-07 14:28:16 +0000108# sqlite3 db test.db -key {xyzzy}
drh9eb9e262004-02-11 02:18:05 +0000109#
dan430e74c2010-06-07 17:47:26 +0000110if {[info command sqlite_orig]==""} {
drhef4ac8f2004-06-19 00:16:31 +0000111 rename sqlite3 sqlite_orig
112 proc sqlite3 {args} {
dan68928b62010-06-22 13:46:43 +0000113 if {[llength $args]>=2 && [string index [lindex $args 0] 0]!="-"} {
dan430e74c2010-06-07 17:47:26 +0000114 # This command is opening a new database connection.
115 #
116 if {[info exists ::G(perm:sqlite3_args)]} {
117 set args [concat $args $::G(perm:sqlite3_args)]
118 }
dan68928b62010-06-22 13:46:43 +0000119 if {[sqlite_orig -has-codec] && ![info exists ::do_not_use_codec]} {
dan430e74c2010-06-07 17:47:26 +0000120 lappend args -key {xyzzy}
121 }
122
dan3ccd20a2010-06-09 19:01:02 +0000123 set res [uplevel 1 sqlite_orig $args]
dan430e74c2010-06-07 17:47:26 +0000124 if {[info exists ::G(perm:presql)]} {
125 [lindex $args 0] eval $::G(perm:presql)
126 }
dan1ce1b4a2010-12-07 14:32:28 +0000127 if {[info exists ::G(perm:dbconfig)]} {
128 set ::dbhandle [lindex $args 0]
129 uplevel #0 $::G(perm:dbconfig)
130 }
dan3ccd20a2010-06-09 19:01:02 +0000131 set res
dan430e74c2010-06-07 17:47:26 +0000132 } else {
mistachkin1d406e02013-08-29 01:09:14 +0000133 # This command is not opening a new database connection. Pass the
mistachkin48864df2013-03-21 21:20:32 +0000134 # arguments through to the C implementation as the are.
dan430e74c2010-06-07 17:47:26 +0000135 #
136 uplevel 1 sqlite_orig $args
drh9eb9e262004-02-11 02:18:05 +0000137 }
drh9eb9e262004-02-11 02:18:05 +0000138 }
139}
140
mistachkinfda06be2011-08-02 00:57:34 +0000141proc getFileRetries {} {
142 if {![info exists ::G(file-retries)]} {
143 #
144 # NOTE: Return the default number of retries for [file] operations. A
145 # value of zero or less here means "disabled".
146 #
mistachkinc60941f2012-09-13 01:51:02 +0000147 return [expr {$::tcl_platform(platform) eq "windows" ? 50 : 0}]
mistachkinfda06be2011-08-02 00:57:34 +0000148 }
149 return $::G(file-retries)
150}
151
152proc getFileRetryDelay {} {
153 if {![info exists ::G(file-retry-delay)]} {
154 #
155 # NOTE: Return the default number of milliseconds to wait when retrying
156 # failed [file] operations. A value of zero or less means "do not
157 # wait".
158 #
159 return 100; # TODO: Good default?
160 }
161 return $::G(file-retry-delay)
162}
163
mistachkinf8a78462012-03-08 20:00:36 +0000164# Return the string representing the name of the current directory. On
165# Windows, the result is "normalized" to whatever our parent command shell
166# is using to prevent case-mismatch issues.
167#
168proc get_pwd {} {
169 if {$::tcl_platform(platform) eq "windows"} {
mistachkin533b8f62012-03-08 20:28:31 +0000170 #
171 # NOTE: Cannot use [file normalize] here because it would alter the
172 # case of the result to what Tcl considers canonical, which would
173 # defeat the purpose of this procedure.
174 #
175 return [string map [list \\ /] \
176 [string trim [exec -- $::env(ComSpec) /c echo %CD%]]]
mistachkinf8a78462012-03-08 20:00:36 +0000177 } else {
178 return [pwd]
179 }
180}
181
mistachkinfda06be2011-08-02 00:57:34 +0000182# Copy file $from into $to. This is used because some versions of
183# TCL for windows (notably the 8.4.1 binary package shipped with the
184# current mingw release) have a broken "file copy" command.
185#
186proc copy_file {from to} {
187 do_copy_file false $from $to
188}
189
190proc forcecopy {from to} {
191 do_copy_file true $from $to
192}
193
194proc do_copy_file {force from to} {
195 set nRetry [getFileRetries] ;# Maximum number of retries.
196 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
197
198 # On windows, sometimes even a [file copy -force] can fail. The cause is
199 # usually "tag-alongs" - programs like anti-virus software, automatic backup
200 # tools and various explorer extensions that keep a file open a little longer
201 # than we expect, causing the delete to fail.
202 #
203 # The solution is to wait a short amount of time before retrying the copy.
204 #
205 if {$nRetry > 0} {
206 for {set i 0} {$i<$nRetry} {incr i} {
207 set rc [catch {
208 if {$force} {
209 file copy -force $from $to
210 } else {
211 file copy $from $to
212 }
213 } msg]
214 if {$rc==0} break
215 if {$nDelay > 0} { after $nDelay }
216 }
217 if {$rc} { error $msg }
218 } else {
219 if {$force} {
220 file copy -force $from $to
221 } else {
222 file copy $from $to
223 }
224 }
225}
226
mistachkinc5484652012-03-05 22:52:33 +0000227# Check if a file name is relative
228#
229proc is_relative_file { file } {
230 return [expr {[file pathtype $file] != "absolute"}]
231}
232
mistachkin4a41f342012-03-06 03:00:49 +0000233# If the VFS supports using the current directory, returns [pwd];
234# otherwise, it returns only the provided suffix string (which is
235# empty by default).
236#
237proc test_pwd { args } {
238 if {[llength $args] > 0} {
239 set suffix1 [lindex $args 0]
240 if {[llength $args] > 1} {
241 set suffix2 [lindex $args 1]
242 } else {
243 set suffix2 $suffix1
244 }
245 } else {
246 set suffix1 ""; set suffix2 ""
247 }
248 ifcapable curdir {
mistachkin6aa18c92012-03-08 20:22:42 +0000249 return "[get_pwd]$suffix1"
mistachkin4a41f342012-03-06 03:00:49 +0000250 } else {
251 return $suffix2
252 }
253}
254
mistachkinfda06be2011-08-02 00:57:34 +0000255# Delete a file or directory
256#
257proc delete_file {args} {
258 do_delete_file false {*}$args
259}
260
261proc forcedelete {args} {
262 do_delete_file true {*}$args
263}
264
265proc do_delete_file {force args} {
266 set nRetry [getFileRetries] ;# Maximum number of retries.
267 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
268
269 foreach filename $args {
270 # On windows, sometimes even a [file delete -force] can fail just after
271 # a file is closed. The cause is usually "tag-alongs" - programs like
272 # anti-virus software, automatic backup tools and various explorer
273 # extensions that keep a file open a little longer than we expect, causing
274 # the delete to fail.
275 #
276 # The solution is to wait a short amount of time before retrying the
277 # delete.
278 #
279 if {$nRetry > 0} {
280 for {set i 0} {$i<$nRetry} {incr i} {
281 set rc [catch {
282 if {$force} {
283 file delete -force $filename
284 } else {
285 file delete $filename
286 }
287 } msg]
288 if {$rc==0} break
289 if {$nDelay > 0} { after $nDelay }
290 }
291 if {$rc} { error $msg }
292 } else {
293 if {$force} {
294 file delete -force $filename
295 } else {
296 file delete $filename
297 }
298 }
299 }
300}
301
mistachkin1d406e02013-08-29 01:09:14 +0000302if {$::tcl_platform(platform) eq "windows"} {
303 proc do_remove_win32_dir {args} {
304 set nRetry [getFileRetries] ;# Maximum number of retries.
305 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
306
307 foreach dirName $args {
308 # On windows, sometimes even a [remove_win32_dir] can fail just after
309 # a directory is emptied. The cause is usually "tag-alongs" - programs
310 # like anti-virus software, automatic backup tools and various explorer
311 # extensions that keep a file open a little longer than we expect,
312 # causing the delete to fail.
313 #
314 # The solution is to wait a short amount of time before retrying the
315 # removal.
316 #
317 if {$nRetry > 0} {
318 for {set i 0} {$i < $nRetry} {incr i} {
319 set rc [catch {
320 remove_win32_dir $dirName
321 } msg]
322 if {$rc == 0} break
323 if {$nDelay > 0} { after $nDelay }
324 }
325 if {$rc} { error $msg }
326 } else {
327 remove_win32_dir $dirName
328 }
329 }
330 }
331
332 proc do_delete_win32_file {args} {
333 set nRetry [getFileRetries] ;# Maximum number of retries.
334 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
335
336 foreach fileName $args {
337 # On windows, sometimes even a [delete_win32_file] can fail just after
338 # a file is closed. The cause is usually "tag-alongs" - programs like
339 # anti-virus software, automatic backup tools and various explorer
340 # extensions that keep a file open a little longer than we expect,
341 # causing the delete to fail.
342 #
343 # The solution is to wait a short amount of time before retrying the
344 # delete.
345 #
346 if {$nRetry > 0} {
347 for {set i 0} {$i < $nRetry} {incr i} {
348 set rc [catch {
349 delete_win32_file $fileName
350 } msg]
351 if {$rc == 0} break
352 if {$nDelay > 0} { after $nDelay }
353 }
354 if {$rc} { error $msg }
355 } else {
356 delete_win32_file $fileName
357 }
358 }
359 }
360}
361
dancb354602010-07-08 09:44:42 +0000362proc execpresql {handle args} {
363 trace remove execution $handle enter [list execpresql $handle]
364 if {[info exists ::G(perm:presql)]} {
365 $handle eval $::G(perm:presql)
366 }
367}
368
dan68928b62010-06-22 13:46:43 +0000369# This command should be called after loading tester.tcl from within
370# all test scripts that are incompatible with encryption codecs.
371#
372proc do_not_use_codec {} {
373 set ::do_not_use_codec 1
374 reset_db
375}
376
dan430e74c2010-06-07 17:47:26 +0000377# The following block only runs the first time this file is sourced. It
378# does not run in slave interpreters (since the ::cmdlinearg array is
379# populated before the test script is run in slave interpreters).
drhbec2bf42000-05-29 23:48:22 +0000380#
danc1a60c52010-06-07 14:28:16 +0000381if {[info exists cmdlinearg]==0} {
382
mistachkin1d406e02013-08-29 01:09:14 +0000383 # Parse any options specified in the $argv array. This script accepts the
384 # following options:
danc1a60c52010-06-07 14:28:16 +0000385 #
386 # --pause
387 # --soft-heap-limit=NN
388 # --maxerror=NN
389 # --malloctrace=N
390 # --backtrace=N
391 # --binarylog=N
dan0626dfc2010-06-15 06:56:37 +0000392 # --soak=N
mistachkinfda06be2011-08-02 00:57:34 +0000393 # --file-retries=N
394 # --file-retry-delay=N
dan6a64d672011-04-04 15:38:16 +0000395 # --start=[$permutation:]$testfile
mistachkin6c3c1a02011-11-12 03:17:40 +0000396 # --match=$pattern
dan17c08232015-06-09 15:58:28 +0000397 # --verbose=$val
398 # --output=$filename
399 # --help
danc1a60c52010-06-07 14:28:16 +0000400 #
401 set cmdlinearg(soft-heap-limit) 0
402 set cmdlinearg(maxerror) 1000
403 set cmdlinearg(malloctrace) 0
404 set cmdlinearg(backtrace) 10
405 set cmdlinearg(binarylog) 0
dan0626dfc2010-06-15 06:56:37 +0000406 set cmdlinearg(soak) 0
mistachkinfda06be2011-08-02 00:57:34 +0000407 set cmdlinearg(file-retries) 0
408 set cmdlinearg(file-retry-delay) 0
mistachkin6c3c1a02011-11-12 03:17:40 +0000409 set cmdlinearg(start) ""
410 set cmdlinearg(match) ""
dan17c08232015-06-09 15:58:28 +0000411 set cmdlinearg(verbose) ""
412 set cmdlinearg(output) ""
danc1a60c52010-06-07 14:28:16 +0000413
414 set leftover [list]
415 foreach a $argv {
416 switch -regexp -- $a {
417 {^-+pause$} {
mistachkin1d406e02013-08-29 01:09:14 +0000418 # Wait for user input before continuing. This is to give the user an
danc1a60c52010-06-07 14:28:16 +0000419 # opportunity to connect profiling tools to the process.
420 puts -nonewline "Press RETURN to begin..."
421 flush stdout
422 gets stdin
423 }
424 {^-+soft-heap-limit=.+$} {
425 foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break
426 }
427 {^-+maxerror=.+$} {
428 foreach {dummy cmdlinearg(maxerror)} [split $a =] break
429 }
430 {^-+malloctrace=.+$} {
431 foreach {dummy cmdlinearg(malloctrace)} [split $a =] break
432 if {$cmdlinearg(malloctrace)} {
433 sqlite3_memdebug_log start
434 }
435 }
436 {^-+backtrace=.+$} {
437 foreach {dummy cmdlinearg(backtrace)} [split $a =] break
dan0626dfc2010-06-15 06:56:37 +0000438 sqlite3_memdebug_backtrace $value
danc1a60c52010-06-07 14:28:16 +0000439 }
danc1a60c52010-06-07 14:28:16 +0000440 {^-+binarylog=.+$} {
441 foreach {dummy cmdlinearg(binarylog)} [split $a =] break
442 }
dan0626dfc2010-06-15 06:56:37 +0000443 {^-+soak=.+$} {
444 foreach {dummy cmdlinearg(soak)} [split $a =] break
445 set ::G(issoak) $cmdlinearg(soak)
446 }
mistachkinfda06be2011-08-02 00:57:34 +0000447 {^-+file-retries=.+$} {
448 foreach {dummy cmdlinearg(file-retries)} [split $a =] break
449 set ::G(file-retries) $cmdlinearg(file-retries)
450 }
451 {^-+file-retry-delay=.+$} {
452 foreach {dummy cmdlinearg(file-retry-delay)} [split $a =] break
453 set ::G(file-retry-delay) $cmdlinearg(file-retry-delay)
454 }
dan6a64d672011-04-04 15:38:16 +0000455 {^-+start=.+$} {
456 foreach {dummy cmdlinearg(start)} [split $a =] break
457
458 set ::G(start:file) $cmdlinearg(start)
459 if {[regexp {(.*):(.*)} $cmdlinearg(start) -> s.perm s.file]} {
460 set ::G(start:permutation) ${s.perm}
461 set ::G(start:file) ${s.file}
462 }
463 if {$::G(start:file) == ""} {unset ::G(start:file)}
464 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000465 {^-+match=.+$} {
466 foreach {dummy cmdlinearg(match)} [split $a =] break
467
468 set ::G(match) $cmdlinearg(match)
469 if {$::G(match) == ""} {unset ::G(match)}
470 }
dan17c08232015-06-09 15:58:28 +0000471
472 {^-+output=.+$} {
473 foreach {dummy cmdlinearg(output)} [split $a =] break
474 if {$cmdlinearg(verbose)==""} {
475 set cmdlinearg(verbose) 2
476 }
477 }
478 {^-+verbose=.+$} {
479 foreach {dummy cmdlinearg(verbose)} [split $a =] break
480 if {$cmdlinearg(verbose)=="file"} {
481 set cmdlinearg(verbose) 2
482 } elseif {[string is boolean -strict $cmdlinearg(verbose)]==0} {
483 error "option --verbose= must be set to a boolean or to \"file\""
484 }
485 }
486
danc1a60c52010-06-07 14:28:16 +0000487 default {
488 lappend leftover $a
489 }
490 }
491 }
492 set argv $leftover
493
dan430e74c2010-06-07 17:47:26 +0000494 # Install the malloc layer used to inject OOM errors. And the 'automatic'
495 # extensions. This only needs to be done once for the process.
496 #
mistachkin1d406e02013-08-29 01:09:14 +0000497 sqlite3_shutdown
498 install_malloc_faultsim 1
danielk1977f7b9d662008-06-23 18:49:43 +0000499 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000500 autoinstall_test_functions
danc1a60c52010-06-07 14:28:16 +0000501
dan430e74c2010-06-07 17:47:26 +0000502 # If the --binarylog option was specified, create the logging VFS. This
503 # call installs the new VFS as the default for all SQLite connections.
504 #
danc1a60c52010-06-07 14:28:16 +0000505 if {$cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000506 vfslog new binarylog {} vfslog.bin
danc1a60c52010-06-07 14:28:16 +0000507 }
508
509 # Set the backtrace depth, if malloc tracing is enabled.
510 #
511 if {$cmdlinearg(malloctrace)} {
512 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danielk1977185eac92008-07-12 15:55:54 +0000513 }
dan17c08232015-06-09 15:58:28 +0000514
515 if {$cmdlinearg(output)!=""} {
516 puts "Copying output to file $cmdlinearg(output)"
517 set ::G(output_fd) [open $cmdlinearg(output) w]
518 fconfigure $::G(output_fd) -buffering line
519 }
520
521 if {$cmdlinearg(verbose)==""} {
522 set cmdlinearg(verbose) 1
523 }
danielk1977f7b9d662008-06-23 18:49:43 +0000524}
danielk197703ba3fa2009-01-09 10:49:14 +0000525
danc1a60c52010-06-07 14:28:16 +0000526# Update the soft-heap-limit each time this script is run. In that
527# way if an individual test file changes the soft-heap-limit, it
528# will be reset at the start of the next test file.
529#
530sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
531
532# Create a test database
533#
danielk197703ba3fa2009-01-09 10:49:14 +0000534proc reset_db {} {
535 catch {db close}
mistachkinfda06be2011-08-02 00:57:34 +0000536 forcedelete test.db
537 forcedelete test.db-journal
538 forcedelete test.db-wal
danielk197703ba3fa2009-01-09 10:49:14 +0000539 sqlite3 db ./test.db
540 set ::DB [sqlite3_connection_pointer db]
541 if {[info exists ::SETUP_SQL]} {
542 db eval $::SETUP_SQL
543 }
drhcd61c282002-03-06 22:01:34 +0000544}
danielk197703ba3fa2009-01-09 10:49:14 +0000545reset_db
drhbec2bf42000-05-29 23:48:22 +0000546
547# Abort early if this script has been run before.
548#
danc1a60c52010-06-07 14:28:16 +0000549if {[info exists TC(count)]} return
drhbec2bf42000-05-29 23:48:22 +0000550
drhce2198c2010-09-10 13:22:59 +0000551# Make sure memory statistics are enabled.
552#
553sqlite3_config_memstatus 1
554
danc1a60c52010-06-07 14:28:16 +0000555# Initialize the test counters and set up commands to access them.
556# Or, if this is a slave interpreter, set up aliases to write the
557# counters in the parent interpreter.
drhbec2bf42000-05-29 23:48:22 +0000558#
danc1a60c52010-06-07 14:28:16 +0000559if {0==[info exists ::SLAVE]} {
560 set TC(errors) 0
561 set TC(count) 0
562 set TC(fail_list) [list]
563 set TC(omit_list) [list]
drhcca17c32013-04-19 12:32:52 +0000564 set TC(warn_list) [list]
danc1a60c52010-06-07 14:28:16 +0000565
566 proc set_test_counter {counter args} {
567 if {[llength $args]} {
568 set ::TC($counter) [lindex $args 0]
569 }
570 set ::TC($counter)
571 }
drhb62c3352006-11-23 09:39:16 +0000572}
drh348784e2000-05-29 20:41:49 +0000573
drh521cc842008-04-15 02:36:33 +0000574# Record the fact that a sequence of tests were omitted.
575#
mistachkin6c3c1a02011-11-12 03:17:40 +0000576proc omit_test {name reason {append 1}} {
danc1a60c52010-06-07 14:28:16 +0000577 set omitList [set_test_counter omit_list]
mistachkin6c3c1a02011-11-12 03:17:40 +0000578 if {$append} {
579 lappend omitList [list $name $reason]
580 }
danc1a60c52010-06-07 14:28:16 +0000581 set_test_counter omit_list $omitList
drh521cc842008-04-15 02:36:33 +0000582}
583
danc1a60c52010-06-07 14:28:16 +0000584# Record the fact that a test failed.
585#
586proc fail_test {name} {
587 set f [set_test_counter fail_list]
588 lappend f $name
589 set_test_counter fail_list $f
590 set_test_counter errors [expr [set_test_counter errors] + 1]
591
592 set nFail [set_test_counter errors]
593 if {$nFail>=$::cmdlinearg(maxerror)} {
dan17c08232015-06-09 15:58:28 +0000594 output2 "*** Giving up..."
danc1a60c52010-06-07 14:28:16 +0000595 finalize_testing
596 }
597}
598
drhcca17c32013-04-19 12:32:52 +0000599# Remember a warning message to be displayed at the conclusion of all testing
600#
601proc warning {msg {append 1}} {
dan17c08232015-06-09 15:58:28 +0000602 output2 "Warning: $msg"
drhcca17c32013-04-19 12:32:52 +0000603 set warnList [set_test_counter warn_list]
604 if {$append} {
605 lappend warnList $msg
606 }
607 set_test_counter warn_list $warnList
608}
609
610
danc1a60c52010-06-07 14:28:16 +0000611# Increment the number of tests run
612#
613proc incr_ntest {} {
614 set_test_counter count [expr [set_test_counter count] + 1]
615}
616
dan17c08232015-06-09 15:58:28 +0000617# Return true if --verbose=1 was specified on the command line. Otherwise,
618# return false.
619#
620proc verbose {} {
621 return $::cmdlinearg(verbose)
622}
623
624# Use the following commands instead of [puts] for test output within
625# this file. Test scripts can still use regular [puts], which is directed
626# to stdout and, if one is open, the --output file.
627#
628# output1: output that should be printed if --verbose=1 was specified.
629# output2: output that should be printed unconditionally.
630# output2_if_no_verbose: output that should be printed only if --verbose=0.
631#
632proc output1 {args} {
633 set v [verbose]
634 if {$v==1} {
635 uplevel output2 $args
636 } elseif {$v==2} {
637 uplevel puts [lrange $args 0 end-1] $::G(output_fd) [lrange $args end end]
638 }
639}
640proc output2 {args} {
641 set nArg [llength $args]
642 uplevel puts $args
643}
644proc output2_if_no_verbose {args} {
645 set v [verbose]
646 if {$v==0} {
647 uplevel output2 $args
648 } elseif {$v==2} {
649 uplevel puts [lrange $args 0 end-1] stdout [lrange $args end end]
650 }
651}
652
653# Override the [puts] command so that if no channel is explicitly
654# specified the string is written to both stdout and to the file
655# specified by "--output=", if any.
656#
657proc puts_override {args} {
658 set nArg [llength $args]
659 if {$nArg==1 || ($nArg==2 && [string first [lindex $args 0] -nonewline]==0)} {
660 uplevel puts_original $args
661 if {[info exists ::G(output_fd)]} {
662 uplevel puts [lrange $args 0 end-1] $::G(output_fd) [lrange $args end end]
663 }
664 } else {
665 # A channel was explicitly specified.
666 uplevel puts_original $args
667 }
668}
669rename puts puts_original
670proc puts {args} { uplevel puts_override $args }
671
danc1a60c52010-06-07 14:28:16 +0000672
mistachkin1d406e02013-08-29 01:09:14 +0000673# Invoke the do_test procedure to run a single test
drh348784e2000-05-29 20:41:49 +0000674#
675proc do_test {name cmd expected} {
danc1a60c52010-06-07 14:28:16 +0000676 global argv cmdlinearg
677
dan8c408002010-11-01 17:38:24 +0000678 fix_testname name
679
drh4a50aac2007-08-23 02:47:53 +0000680 sqlite3_memdebug_settitle $name
danc1a60c52010-06-07 14:28:16 +0000681
mistachkin1d406e02013-08-29 01:09:14 +0000682# if {[llength $argv]==0} {
dan92d516a2010-07-05 14:54:48 +0000683# set go 1
684# } else {
685# set go 0
686# foreach pattern $argv {
687# if {[string match $pattern $name]} {
688# set go 1
689# break
690# }
691# }
692# }
dan430e74c2010-06-07 17:47:26 +0000693
dand506de02010-07-03 13:59:01 +0000694 if {[info exists ::G(perm:prefix)]} {
695 set name "$::G(perm:prefix)$name"
dan430e74c2010-06-07 17:47:26 +0000696 }
697
danc1a60c52010-06-07 14:28:16 +0000698 incr_ntest
dan17c08232015-06-09 15:58:28 +0000699 output1 -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000700 flush stdout
mistachkin6c3c1a02011-11-12 03:17:40 +0000701
702 if {![info exists ::G(match)] || [string match $::G(match) $name]} {
703 if {[catch {uplevel #0 "$cmd;\n"} result]} {
dan17c08232015-06-09 15:58:28 +0000704 output2_if_no_verbose -nonewline $name...
705 output2 "\nError: $result"
mistachkin6c3c1a02011-11-12 03:17:40 +0000706 fail_test $name
mistachkin6c3c1a02011-11-12 03:17:40 +0000707 } else {
drh3f17aef2012-04-27 01:08:02 +0000708 if {[regexp {^~?/.*/$} $expected]} {
drh70bdcc72013-05-30 19:28:34 +0000709 # "expected" is of the form "/PATTERN/" then the result if correct if
710 # regular expression PATTERN matches the result. "~/PATTERN/" means
711 # the regular expression must not match.
drh3f17aef2012-04-27 01:08:02 +0000712 if {[string index $expected 0]=="~"} {
drhc2b23e72013-11-13 15:32:15 +0000713 set re [string range $expected 2 end-1]
714 if {[string index $re 0]=="*"} {
715 # If the regular expression begins with * then treat it as a glob instead
716 set ok [string match $re $result]
717 } else {
718 set re [string map {# {[-0-9.]+}} $re]
719 set ok [regexp $re $result]
720 }
721 set ok [expr {!$ok}]
drh3f17aef2012-04-27 01:08:02 +0000722 } else {
drhc2b23e72013-11-13 15:32:15 +0000723 set re [string range $expected 1 end-1]
724 if {[string index $re 0]=="*"} {
725 # If the regular expression begins with * then treat it as a glob instead
726 set ok [string match $re $result]
727 } else {
728 set re [string map {# {[-0-9.]+}} $re]
729 set ok [regexp $re $result]
730 }
drh3f17aef2012-04-27 01:08:02 +0000731 }
drh70bdcc72013-05-30 19:28:34 +0000732 } elseif {[regexp {^~?\*.*\*$} $expected]} {
733 # "expected" is of the form "*GLOB*" then the result if correct if
734 # glob pattern GLOB matches the result. "~/GLOB/" means
735 # the glob must not match.
736 if {[string index $expected 0]=="~"} {
737 set e [string range $expected 1 end]
738 set ok [expr {![string match $e $result]}]
739 } else {
740 set ok [string match $expected $result]
741 }
drh3f17aef2012-04-27 01:08:02 +0000742 } else {
743 set ok [expr {[string compare $result $expected]==0}]
744 }
745 if {!$ok} {
mistachkinc60941f2012-09-13 01:51:02 +0000746 # if {![info exists ::testprefix] || $::testprefix eq ""} {
747 # error "no test prefix"
748 # }
drhd66b2e02015-11-12 21:42:40 +0000749 output1 ""
drh061d35c2015-11-13 00:03:14 +0000750 output2 "! $name expected: \[$expected\]\n! $name got: \[$result\]"
drh3f17aef2012-04-27 01:08:02 +0000751 fail_test $name
752 } else {
dan17c08232015-06-09 15:58:28 +0000753 output1 " Ok"
drh3f17aef2012-04-27 01:08:02 +0000754 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000755 }
drh348784e2000-05-29 20:41:49 +0000756 } else {
dan17c08232015-06-09 15:58:28 +0000757 output1 " Omitted"
mistachkin6c3c1a02011-11-12 03:17:40 +0000758 omit_test $name "pattern mismatch" 0
drh348784e2000-05-29 20:41:49 +0000759 }
drh6558db82007-04-13 03:23:21 +0000760 flush stdout
drh348784e2000-05-29 20:41:49 +0000761}
dana3f51082010-09-30 18:43:14 +0000762
mistachkinf21979d2015-01-18 05:35:01 +0000763proc dumpbytes {s} {
764 set r ""
765 for {set i 0} {$i < [string length $s]} {incr i} {
766 if {$i > 0} {append r " "}
767 append r [format %02X [scan [string index $s $i] %c]]
768 }
769 return $r
770}
771
drh8df91852012-04-24 12:46:05 +0000772proc catchcmd {db {cmd ""}} {
773 global CLI
774 set out [open cmds.txt w]
775 puts $out $cmd
776 close $out
777 set line "exec $CLI $db < cmds.txt"
778 set rc [catch { eval $line } msg]
779 list $rc $msg
780}
781
mistachkinf21979d2015-01-18 05:35:01 +0000782proc catchcmdex {db {cmd ""}} {
783 global CLI
784 set out [open cmds.txt w]
785 fconfigure $out -encoding binary -translation binary
786 puts -nonewline $out $cmd
787 close $out
788 set line "exec -keepnewline -- $CLI $db < cmds.txt"
789 set chans [list stdin stdout stderr]
790 foreach chan $chans {
791 catch {
792 set modes($chan) [fconfigure $chan]
793 fconfigure $chan -encoding binary -translation binary -buffering none
794 }
795 }
796 set rc [catch { eval $line } msg]
797 foreach chan $chans {
798 catch {
799 eval fconfigure [list $chan] $modes($chan)
800 }
801 }
802 # puts [dumpbytes $msg]
803 list $rc $msg
804}
805
shaneh55505172011-06-21 19:30:19 +0000806proc filepath_normalize {p} {
807 # test cases should be written to assume "unix"-like file paths
shaneh285a18f2011-06-21 19:39:59 +0000808 if {$::tcl_platform(platform)!="unix"} {
shaneh55505172011-06-21 19:30:19 +0000809 # lreverse*2 as a hack to remove any unneeded {} after the string map
810 lreverse [lreverse [string map {\\ /} [regsub -nocase -all {[a-z]:[/\\]+} $p {/}]]]
shanehc4896402011-06-21 19:38:16 +0000811 } {
812 set p
shaneh55505172011-06-21 19:30:19 +0000813 }
814}
815proc do_filepath_test {name cmd expected} {
816 uplevel [list do_test $name [
817 subst -nocommands { filepath_normalize [ $cmd ] }
818 ] [filepath_normalize $expected]]
819}
820
dan33f53792011-05-05 19:44:22 +0000821proc realnum_normalize {r} {
shaneh4f529e82011-06-20 17:41:41 +0000822 # different TCL versions display floating point values differently.
823 string map {1.#INF inf Inf inf .0e e} [regsub -all {(e[+-])0+} $r {\1}]
dan33f53792011-05-05 19:44:22 +0000824}
825proc do_realnum_test {name cmd expected} {
826 uplevel [list do_test $name [
827 subst -nocommands { realnum_normalize [ $cmd ] }
828 ] [realnum_normalize $expected]]
829}
830
dana3f51082010-09-30 18:43:14 +0000831proc fix_testname {varname} {
832 upvar $varname testname
mistachkin1d406e02013-08-29 01:09:14 +0000833 if {[info exists ::testprefix]
dana3f51082010-09-30 18:43:14 +0000834 && [string is digit [string range $testname 0 0]]
835 } {
836 set testname "${::testprefix}-$testname"
837 }
838}
dan67340072011-04-16 19:23:10 +0000839
840proc normalize_list {L} {
841 set L2 [list]
842 foreach l $L {lappend L2 $l}
843 set L2
844}
drh5f9742e2013-08-29 15:08:38 +0000845
dan57f7f4b2010-10-01 19:04:37 +0000846proc do_execsql_test {testname sql {result {}}} {
dana3f51082010-09-30 18:43:14 +0000847 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000848 uplevel do_test [list $testname] [list "execsql {$sql}"] [list [list {*}$result]]
dan153eda02010-06-21 07:45:47 +0000849}
850proc do_catchsql_test {testname sql result} {
dana3f51082010-09-30 18:43:14 +0000851 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000852 uplevel do_test [list $testname] [list "catchsql {$sql}"] [list $result]
dan153eda02010-06-21 07:45:47 +0000853}
drh3c2aeae2014-01-24 14:37:44 +0000854proc do_timed_execsql_test {testname sql {result {}}} {
855 fix_testname testname
856 uplevel do_test [list $testname] [list "execsql_timed {$sql}"]\
857 [list [list {*}$result]]
858}
dan39854792010-11-15 16:12:58 +0000859proc do_eqp_test {name sql res} {
860 uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res]
861}
dan153eda02010-06-21 07:45:47 +0000862
dan51f06982010-09-18 19:00:12 +0000863#-------------------------------------------------------------------------
864# Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST
865#
866# Where switches are:
867#
868# -errorformat FMTSTRING
869# -count
danaf1dcab2010-09-20 19:17:53 +0000870# -query SQL
dana16d1062010-09-28 17:37:28 +0000871# -tclquery TCL
danaf7626f2010-09-23 18:47:36 +0000872# -repair TCL
dan51f06982010-09-18 19:00:12 +0000873#
874proc do_select_tests {prefix args} {
875
876 set testlist [lindex $args end]
877 set switches [lrange $args 0 end-1]
878
879 set errfmt ""
880 set countonly 0
dana16d1062010-09-28 17:37:28 +0000881 set tclquery ""
danaf7626f2010-09-23 18:47:36 +0000882 set repair ""
dan51f06982010-09-18 19:00:12 +0000883
884 for {set i 0} {$i < [llength $switches]} {incr i} {
885 set s [lindex $switches $i]
886 set n [string length $s]
danaf1dcab2010-09-20 19:17:53 +0000887 if {$n>=2 && [string equal -length $n $s "-query"]} {
dana16d1062010-09-28 17:37:28 +0000888 set tclquery [list execsql [lindex $switches [incr i]]]
889 } elseif {$n>=2 && [string equal -length $n $s "-tclquery"]} {
890 set tclquery [lindex $switches [incr i]]
danaf1dcab2010-09-20 19:17:53 +0000891 } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} {
dan51f06982010-09-18 19:00:12 +0000892 set errfmt [lindex $switches [incr i]]
danaf7626f2010-09-23 18:47:36 +0000893 } elseif {$n>=2 && [string equal -length $n $s "-repair"]} {
894 set repair [lindex $switches [incr i]]
dan51f06982010-09-18 19:00:12 +0000895 } elseif {$n>=2 && [string equal -length $n $s "-count"]} {
896 set countonly 1
897 } else {
898 error "unknown switch: $s"
899 }
900 }
901
902 if {$countonly && $errfmt!=""} {
903 error "Cannot use -count and -errorformat together"
904 }
905 set nTestlist [llength $testlist]
906 if {$nTestlist%3 || $nTestlist==0 } {
907 error "SELECT test list contains [llength $testlist] elements"
908 }
909
danaf7626f2010-09-23 18:47:36 +0000910 eval $repair
dan51f06982010-09-18 19:00:12 +0000911 foreach {tn sql res} $testlist {
dana16d1062010-09-28 17:37:28 +0000912 if {$tclquery != ""} {
danaf1dcab2010-09-20 19:17:53 +0000913 execsql $sql
dana16d1062010-09-28 17:37:28 +0000914 uplevel do_test ${prefix}.$tn [list $tclquery] [list [list {*}$res]]
915 } elseif {$countonly} {
dan51f06982010-09-18 19:00:12 +0000916 set nRow 0
917 db eval $sql {incr nRow}
918 uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res]
919 } elseif {$errfmt==""} {
920 uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]]
921 } else {
922 set res [list 1 [string trim [format $errfmt {*}$res]]]
923 uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res]
924 }
danaf7626f2010-09-23 18:47:36 +0000925 eval $repair
dan51f06982010-09-18 19:00:12 +0000926 }
danaf7626f2010-09-23 18:47:36 +0000927
dan51f06982010-09-18 19:00:12 +0000928}
929
danb04757a2010-09-21 16:59:16 +0000930proc delete_all_data {} {
931 db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} {
932 db eval "DELETE FROM '[string map {' ''} $t]'"
933 }
934}
drh348784e2000-05-29 20:41:49 +0000935
mistachkin1d406e02013-08-29 01:09:14 +0000936# Run an SQL script.
drhb62c3352006-11-23 09:39:16 +0000937# Return the number of microseconds per statement.
938#
drh3590f152006-11-23 21:09:10 +0000939proc speed_trial {name numstmt units sql} {
dan17c08232015-06-09 15:58:28 +0000940 output2 -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000941 flush stdout
942 set speed [time {sqlite3_exec_nr db $sql}]
943 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000944 if {$tm == 0} {
945 set rate [format %20s "many"]
946 } else {
947 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
948 }
drh3590f152006-11-23 21:09:10 +0000949 set u2 $units/s
dan17c08232015-06-09 15:58:28 +0000950 output2 [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000951 global total_time
952 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000953 lappend ::speed_trial_times $name $tm
drhdd924312007-03-31 22:34:16 +0000954}
drh45c236d2008-03-22 01:08:00 +0000955proc speed_trial_tcl {name numstmt units script} {
dan17c08232015-06-09 15:58:28 +0000956 output2 -nonewline [format {%-21.21s } $name...]
drh45c236d2008-03-22 01:08:00 +0000957 flush stdout
958 set speed [time {eval $script}]
959 set tm [lindex $speed 0]
960 if {$tm == 0} {
961 set rate [format %20s "many"]
962 } else {
963 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
964 }
965 set u2 $units/s
dan17c08232015-06-09 15:58:28 +0000966 output2 [format {%12d uS %s %s} $tm $rate $u2]
drh45c236d2008-03-22 01:08:00 +0000967 global total_time
968 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000969 lappend ::speed_trial_times $name $tm
drh45c236d2008-03-22 01:08:00 +0000970}
drhdd924312007-03-31 22:34:16 +0000971proc speed_trial_init {name} {
972 global total_time
973 set total_time 0
dana975b592010-10-08 16:09:43 +0000974 set ::speed_trial_times [list]
drhbb810a92010-07-03 12:00:53 +0000975 sqlite3 versdb :memory:
976 set vers [versdb one {SELECT sqlite_source_id()}]
977 versdb close
dan17c08232015-06-09 15:58:28 +0000978 output2 "SQLite $vers"
drhdd924312007-03-31 22:34:16 +0000979}
980proc speed_trial_summary {name} {
981 global total_time
dan17c08232015-06-09 15:58:28 +0000982 output2 [format {%-21.21s %12d uS TOTAL} $name $total_time]
dana975b592010-10-08 16:09:43 +0000983
984 if { 0 } {
985 sqlite3 versdb :memory:
986 set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0]
987 versdb close
dan17c08232015-06-09 15:58:28 +0000988 output2 "CREATE TABLE IF NOT EXISTS time(version, script, test, us);"
dana975b592010-10-08 16:09:43 +0000989 foreach {test us} $::speed_trial_times {
dan17c08232015-06-09 15:58:28 +0000990 output2 "INSERT INTO time VALUES('$vers', '$name', '$test', $us);"
dana975b592010-10-08 16:09:43 +0000991 }
992 }
drhb62c3352006-11-23 09:39:16 +0000993}
994
drh348784e2000-05-29 20:41:49 +0000995# Run this routine last
996#
997proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +0000998 catch {db close}
dane8559832014-07-31 17:35:40 +0000999 catch {db1 close}
danc1a60c52010-06-07 14:28:16 +00001000 catch {db2 close}
1001 catch {db3 close}
1002 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +00001003}
1004proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +00001005 global sqlite_open_file_count
1006
1007 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +00001008
drh6e142f52000-06-08 13:36:40 +00001009 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +00001010 catch {db2 close}
1011 catch {db3 close}
1012
drh9bc54492007-10-23 14:49:59 +00001013 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +00001014 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +00001015 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +00001016 db close
drh984bfaa2008-03-19 16:08:53 +00001017 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +00001018
drh3a7fb7c2007-08-10 16:41:08 +00001019 sqlite3_soft_heap_limit 0
danc1a60c52010-06-07 14:28:16 +00001020 set nTest [incr_ntest]
1021 set nErr [set_test_counter errors]
1022
drhef866372013-05-22 20:49:02 +00001023 set nKnown 0
1024 if {[file readable known-problems.txt]} {
1025 set fd [open known-problems.txt]
1026 set content [read $fd]
1027 close $fd
1028 foreach x $content {set known_error($x) 1}
1029 foreach x [set_test_counter fail_list] {
1030 if {[info exists known_error($x)]} {incr nKnown}
1031 }
1032 }
1033 if {$nKnown>0} {
dan17c08232015-06-09 15:58:28 +00001034 output2 "[expr {$nErr-$nKnown}] new errors and $nKnown known errors\
drhef866372013-05-22 20:49:02 +00001035 out of $nTest tests"
1036 } else {
drh72bf6a32016-01-07 02:06:55 +00001037 set cpuinfo {}
1038 if {[catch {exec hostname} hname]==0} {set cpuinfo [string trim $hname]}
1039 append cpuinfo " $::tcl_platform(os)"
1040 append cpuinfo " [expr {$::tcl_platform(pointerSize)*8}]-bit"
1041 append cpuinfo " [string map {E -e} $::tcl_platform(byteOrder)]"
1042 output2 "SQLite [sqlite3 -sourceid]"
1043 output2 "$nErr errors out of $nTest tests on $cpuinfo"
drhef866372013-05-22 20:49:02 +00001044 }
1045 if {$nErr>$nKnown} {
drh061d35c2015-11-13 00:03:14 +00001046 output2 -nonewline "!Failures on these tests:"
drhef866372013-05-22 20:49:02 +00001047 foreach x [set_test_counter fail_list] {
dan17c08232015-06-09 15:58:28 +00001048 if {![info exists known_error($x)]} {output2 -nonewline " $x"}
drhef866372013-05-22 20:49:02 +00001049 }
dan17c08232015-06-09 15:58:28 +00001050 output2 ""
drhf3a65f72007-08-22 20:18:21 +00001051 }
drhcca17c32013-04-19 12:32:52 +00001052 foreach warning [set_test_counter warn_list] {
dan17c08232015-06-09 15:58:28 +00001053 output2 "Warning: $warning"
drhcca17c32013-04-19 12:32:52 +00001054 }
danielk1977ee8b7992009-03-26 17:13:06 +00001055 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +00001056 if {[llength $omitList]>0} {
dan17c08232015-06-09 15:58:28 +00001057 output2 "Omitted test cases:"
drh521cc842008-04-15 02:36:33 +00001058 set prec {}
1059 foreach {rec} [lsort $omitList] {
1060 if {$rec==$prec} continue
1061 set prec $rec
drhd66b2e02015-11-12 21:42:40 +00001062 output2 [format {. %-12s %s} [lindex $rec 0] [lindex $rec 1]]
drh521cc842008-04-15 02:36:33 +00001063 }
1064 }
drh80788d82006-09-02 14:50:23 +00001065 if {$nErr>0 && ![working_64bit_int]} {
dan17c08232015-06-09 15:58:28 +00001066 output2 "******************************************************************"
1067 output2 "N.B.: The version of TCL that you used to build this test harness"
1068 output2 "is defective in that it does not support 64-bit integers. Some or"
1069 output2 "all of the test failures above might be a result from this defect"
1070 output2 "in your TCL build."
1071 output2 "******************************************************************"
drhdb25e382001-03-15 18:21:22 +00001072 }
danc1a60c52010-06-07 14:28:16 +00001073 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +00001074 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +00001075 }
drh94e92032003-02-16 22:21:32 +00001076 if {$sqlite_open_file_count} {
dan17c08232015-06-09 15:58:28 +00001077 output2 "$sqlite_open_file_count files were left open"
drh94e92032003-02-16 22:21:32 +00001078 incr nErr
1079 }
drheafc43b2010-07-26 18:43:40 +00001080 if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 ||
1081 [sqlite3_memory_used]>0} {
dan17c08232015-06-09 15:58:28 +00001082 output2 "Unfreed memory: [sqlite3_memory_used] bytes in\
drheafc43b2010-07-26 18:43:40 +00001083 [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations"
drhf3a65f72007-08-22 20:18:21 +00001084 incr nErr
drh2d7636e2008-02-16 16:21:45 +00001085 ifcapable memdebug||mem5||(mem3&&debug) {
dan17c08232015-06-09 15:58:28 +00001086 output2 "Writing unfreed memory log to \"./memleak.txt\""
drh4a50aac2007-08-23 02:47:53 +00001087 sqlite3_memdebug_dump ./memleak.txt
1088 }
drhf3a65f72007-08-22 20:18:21 +00001089 } else {
dan17c08232015-06-09 15:58:28 +00001090 output2 "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +00001091 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +00001092 sqlite3_memdebug_dump ./memusage.txt
1093 }
drhf3a65f72007-08-22 20:18:21 +00001094 }
drhf7141992008-06-19 00:16:08 +00001095 show_memstats
dan17c08232015-06-09 15:58:28 +00001096 output2 "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
1097 output2 "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +00001098 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
dan17c08232015-06-09 15:58:28 +00001099 output2 "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
danielk1977a7a8e142008-02-13 18:25:27 +00001100 }
danc1a60c52010-06-07 14:28:16 +00001101 if {$::cmdlinearg(malloctrace)} {
dan17c08232015-06-09 15:58:28 +00001102 output2 "Writing mallocs.sql..."
danielk197735754ac2008-03-21 17:29:37 +00001103 memdebug_log_sql
1104 sqlite3_memdebug_log stop
1105 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +00001106
1107 if {[sqlite3_memory_used]>0} {
dan17c08232015-06-09 15:58:28 +00001108 output2 "Writing leaks.sql..."
danielk1977dbdc4d42008-03-28 07:42:53 +00001109 sqlite3_memdebug_log sync
1110 memdebug_log_sql leaks.sql
1111 }
danielk197735754ac2008-03-21 17:29:37 +00001112 }
drhd9910fe2006-10-04 11:55:49 +00001113 foreach f [glob -nocomplain test.db-*-journal] {
mistachkinfda06be2011-08-02 00:57:34 +00001114 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +00001115 }
1116 foreach f [glob -nocomplain test.db-mj*] {
mistachkinfda06be2011-08-02 00:57:34 +00001117 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +00001118 }
drhdb25e382001-03-15 18:21:22 +00001119 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +00001120}
1121
drhf7141992008-06-19 00:16:08 +00001122# Display memory statistics for analysis and debugging purposes.
1123#
1124proc show_memstats {} {
1125 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +00001126 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
1127 set val [format {now %10d max %10d max-size %10d} \
1128 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
dan17c08232015-06-09 15:58:28 +00001129 output1 "Memory used: $val"
drheafc43b2010-07-26 18:43:40 +00001130 set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0]
1131 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
dan17c08232015-06-09 15:58:28 +00001132 output1 "Allocation count: $val"
drhf7141992008-06-19 00:16:08 +00001133 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +00001134 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
1135 set val [format {now %10d max %10d max-size %10d} \
1136 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
dan17c08232015-06-09 15:58:28 +00001137 output1 "Page-cache used: $val"
drhf7141992008-06-19 00:16:08 +00001138 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
1139 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
dan17c08232015-06-09 15:58:28 +00001140 output1 "Page-cache overflow: $val"
drhf7141992008-06-19 00:16:08 +00001141 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
1142 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
dan17c08232015-06-09 15:58:28 +00001143 output1 "Scratch memory used: $val"
drhf7141992008-06-19 00:16:08 +00001144 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +00001145 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
1146 set val [format {now %10d max %10d max-size %10d} \
1147 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
dan17c08232015-06-09 15:58:28 +00001148 output1 "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +00001149 ifcapable yytrackmaxstackdepth {
1150 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
1151 set val [format { max %10d} [lindex $x 2]]
dan17c08232015-06-09 15:58:28 +00001152 output2 "Parser stack depth: $val"
drhec424a52008-07-25 15:39:03 +00001153 }
drhf7141992008-06-19 00:16:08 +00001154}
1155
drh348784e2000-05-29 20:41:49 +00001156# A procedure to execute SQL
1157#
drhc4a3c772001-04-04 11:48:57 +00001158proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +00001159 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +00001160 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +00001161}
drh3c2aeae2014-01-24 14:37:44 +00001162proc execsql_timed {sql {db db}} {
1163 set tm [time {
1164 set x [uplevel [list $db eval $sql]]
1165 } 1]
1166 set tm [lindex $tm 0]
dan17c08232015-06-09 15:58:28 +00001167 output1 -nonewline " ([expr {$tm*0.001}]ms) "
drh3c2aeae2014-01-24 14:37:44 +00001168 set x
1169}
drh3aadb2e2000-05-31 17:59:25 +00001170
drhadbca9c2001-09-27 15:11:53 +00001171# Execute SQL and catch exceptions.
1172#
1173proc catchsql {sql {db db}} {
1174 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +00001175 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +00001176 lappend r $msg
1177 return $r
1178}
1179
drh04096482001-11-09 22:41:44 +00001180# Do an VDBE code dump on the SQL given
1181#
1182proc explain {sql {db db}} {
mistachkineeb31ff2015-06-10 23:02:38 +00001183 output2 ""
1184 output2 "addr opcode p1 p2 p3 p4 p5 #"
1185 output2 "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +00001186 $db eval "explain $sql" {} {
mistachkineeb31ff2015-06-10 23:02:38 +00001187 output2 [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
danielk1977287fb612008-01-04 19:10:28 +00001188 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
1189 ]
drh04096482001-11-09 22:41:44 +00001190 }
1191}
1192
dane83267d2013-11-05 14:19:22 +00001193proc explain_i {sql {db db}} {
mistachkineeb31ff2015-06-10 23:02:38 +00001194 output2 ""
1195 output2 "addr opcode p1 p2 p3 p4 p5 #"
1196 output2 "---- ------------ ------ ------ ------ ---------------- -- -"
dane83267d2013-11-05 14:19:22 +00001197
dane83267d2013-11-05 14:19:22 +00001198
dan5c663c32013-11-06 14:52:40 +00001199 # Set up colors for the different opcodes. Scheme is as follows:
1200 #
1201 # Red: Opcodes that write to a b-tree.
1202 # Blue: Opcodes that reposition or seek a cursor.
1203 # Green: The ResultRow opcode.
1204 #
dandea63f22014-03-03 16:48:47 +00001205 if { [catch {fconfigure stdout -mode}]==0 } {
1206 set R "\033\[31;1m" ;# Red fg
1207 set G "\033\[32;1m" ;# Green fg
1208 set B "\033\[34;1m" ;# Red fg
1209 set D "\033\[39;0m" ;# Default fg
1210 } else {
1211 set R ""
1212 set G ""
1213 set B ""
1214 set D ""
1215 }
dan5c663c32013-11-06 14:52:40 +00001216 foreach opcode {
1217 Seek SeekGe SeekGt SeekLe SeekLt NotFound Last Rewind
dane3ab7292013-11-12 12:30:09 +00001218 NoConflict Next Prev VNext VPrev VFilter
drhf45f2322014-03-23 17:45:03 +00001219 SorterSort SorterNext
dan5c663c32013-11-06 14:52:40 +00001220 } {
1221 set color($opcode) $B
1222 }
1223 foreach opcode {ResultRow} {
1224 set color($opcode) $G
1225 }
1226 foreach opcode {IdxInsert Insert Delete IdxDelete} {
1227 set color($opcode) $R
1228 }
1229
dan427ebba2013-11-05 16:39:31 +00001230 set bSeenGoto 0
dane83267d2013-11-05 14:19:22 +00001231 $db eval "explain $sql" {} {
1232 set x($addr) 0
1233 set op($addr) $opcode
1234
dan427ebba2013-11-05 16:39:31 +00001235 if {$opcode == "Goto" && ($bSeenGoto==0 || ($p2 > $addr+10))} {
1236 set linebreak($p2) 1
1237 set bSeenGoto 1
dane83267d2013-11-05 14:19:22 +00001238 }
1239
dane3ab7292013-11-12 12:30:09 +00001240 if {$opcode=="Next" || $opcode=="Prev"
1241 || $opcode=="VNext" || $opcode=="VPrev"
drhf45f2322014-03-23 17:45:03 +00001242 || $opcode=="SorterNext"
dane3ab7292013-11-12 12:30:09 +00001243 } {
dane83267d2013-11-05 14:19:22 +00001244 for {set i $p2} {$i<$addr} {incr i} {
1245 incr x($i) 2
1246 }
1247 }
1248
1249 if {$opcode == "Goto" && $p2<$addr && $op($p2)=="Yield"} {
1250 for {set i [expr $p2+1]} {$i<$addr} {incr i} {
1251 incr x($i) 2
1252 }
1253 }
dan427ebba2013-11-05 16:39:31 +00001254
1255 if {$opcode == "Halt" && $comment == "End of coroutine"} {
1256 set linebreak([expr $addr+1]) 1
1257 }
dane83267d2013-11-05 14:19:22 +00001258 }
1259
1260 $db eval "explain $sql" {} {
dan427ebba2013-11-05 16:39:31 +00001261 if {[info exists linebreak($addr)]} {
mistachkineeb31ff2015-06-10 23:02:38 +00001262 output2 ""
dane83267d2013-11-05 14:19:22 +00001263 }
1264 set I [string repeat " " $x($addr)]
dan5c663c32013-11-06 14:52:40 +00001265
1266 set col ""
1267 catch { set col $color($opcode) }
1268
mistachkineeb31ff2015-06-10 23:02:38 +00001269 output2 [format {%-4d %s%s%-12.12s%s %-6d %-6d %-6d % -17s %s %s} \
dan5c663c32013-11-06 14:52:40 +00001270 $addr $I $col $opcode $D $p1 $p2 $p3 $p4 $p5 $comment
dane83267d2013-11-05 14:19:22 +00001271 ]
dane83267d2013-11-05 14:19:22 +00001272 }
mistachkineeb31ff2015-06-10 23:02:38 +00001273 output2 "---- ------------ ------ ------ ------ ---------------- -- -"
dane83267d2013-11-05 14:19:22 +00001274}
1275
drhdafc0ce2008-04-17 19:14:02 +00001276# Show the VDBE program for an SQL statement but omit the Trace
1277# opcode at the beginning. This procedure can be used to prove
1278# that different SQL statements generate exactly the same VDBE code.
1279#
1280proc explain_no_trace {sql} {
1281 set tr [db eval "EXPLAIN $sql"]
1282 return [lrange $tr 7 end]
1283}
1284
drh3aadb2e2000-05-31 17:59:25 +00001285# Another procedure to execute SQL. This one includes the field
1286# names in the returned list.
1287#
1288proc execsql2 {sql} {
1289 set result {}
1290 db eval $sql data {
1291 foreach f $data(*) {
1292 lappend result $f $data($f)
1293 }
1294 }
1295 return $result
1296}
drh17a68932001-01-31 13:28:08 +00001297
mistachkin1d406e02013-08-29 01:09:14 +00001298# Use a temporary in-memory database to execute SQL statements
1299#
1300proc memdbsql {sql} {
1301 sqlite3 memdb :memory:
1302 set result [memdb eval $sql]
1303 memdb close
1304 return $result
1305}
1306
drhdde85d92003-03-01 19:45:34 +00001307# Use the non-callback API to execute multiple SQL statements
1308#
1309proc stepsql {dbptr sql} {
1310 set sql [string trim $sql]
1311 set r 0
1312 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +00001313 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +00001314 return [list 1 $vm]
1315 }
1316 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +00001317# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
1318# foreach v $VAL {lappend r $v}
1319# }
1320 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
1321 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
1322 lappend r [sqlite3_column_text $vm $i]
1323 }
drhdde85d92003-03-01 19:45:34 +00001324 }
danielk1977106bb232004-05-21 10:08:53 +00001325 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +00001326 return [list 1 $errmsg]
1327 }
1328 }
1329 return $r
1330}
1331
drh21504322002-06-25 13:16:02 +00001332# Do an integrity check of the entire database
1333#
danielk197704103022009-02-03 16:51:24 +00001334proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +00001335 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +00001336 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +00001337 }
1338}
1339
drh433dccf2013-02-09 15:37:11 +00001340# Check the extended error code
1341#
1342proc verify_ex_errcode {name expected {db db}} {
1343 do_test $name [list sqlite3_extended_errcode $db] $expected
1344}
1345
danc6055c72011-04-28 18:46:46 +00001346
1347# Return true if the SQL statement passed as the second argument uses a
1348# statement transaction.
1349#
1350proc sql_uses_stmt {db sql} {
1351 set stmt [sqlite3_prepare $db $sql -1 dummy]
1352 set uses [uses_stmt_journal $stmt]
1353 sqlite3_finalize $stmt
1354 return $uses
1355}
1356
danielk1977db4e8862008-01-16 08:24:46 +00001357proc fix_ifcapable_expr {expr} {
1358 set ret ""
1359 set state 0
1360 for {set i 0} {$i < [string length $expr]} {incr i} {
1361 set char [string range $expr $i $i]
1362 set newstate [expr {[string is alnum $char] || $char eq "_"}]
1363 if {$newstate && !$state} {
1364 append ret {$::sqlite_options(}
1365 }
1366 if {!$newstate && $state} {
1367 append ret )
1368 }
1369 append ret $char
1370 set state $newstate
1371 }
1372 if {$state} {append ret )}
1373 return $ret
1374}
1375
mistachkinc60941f2012-09-13 01:51:02 +00001376# Returns non-zero if the capabilities are present; zero otherwise.
1377#
1378proc capable {expr} {
1379 set e [fix_ifcapable_expr $expr]; return [expr ($e)]
1380}
1381
drh27d258a2004-10-30 20:23:09 +00001382# Evaluate a boolean expression of capabilities. If true, execute the
1383# code. Omit the code if false.
1384#
danielk19773e8c37e2005-01-21 03:12:14 +00001385proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +00001386 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
1387 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +00001388 if ($e2) {
1389 set c [catch {uplevel 1 $code} r]
1390 } else {
1391 set c [catch {uplevel 1 $elsecode} r]
1392 }
1393 return -code $c $r
drh21504322002-06-25 13:16:02 +00001394}
danielk197745901d62004-11-10 15:27:38 +00001395
danielk1977aca790a2005-01-13 11:07:52 +00001396# This proc execs a seperate process that crashes midway through executing
1397# the SQL script $sql on database test.db.
1398#
1399# The crash occurs during a sync() of file $crashfile. When the crash
1400# occurs a random subset of all unsynced writes made by the process are
1401# written into the files on disk. Argument $crashdelay indicates the
1402# number of file syncs to wait before crashing.
1403#
1404# The return value is a list of two elements. The first element is a
1405# boolean, indicating whether or not the process actually crashed or
1406# reported some other error. The second element in the returned list is the
1407# error message. This is "child process exited abnormally" if the crash
mistachkin48864df2013-03-21 21:20:32 +00001408# occurred.
danielk1977aca790a2005-01-13 11:07:52 +00001409#
danielk1977f8940ae2007-08-23 11:07:10 +00001410# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +00001411#
1412proc crashsql {args} {
danielk197759a33f92007-03-17 10:26:59 +00001413
1414 set blocksize ""
1415 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +00001416 set prngseed 0
dan999cd082013-12-09 20:42:03 +00001417 set opendb { sqlite3 db test.db -vfs crash }
drhf8587402008-01-08 16:03:49 +00001418 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +00001419 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +00001420 set dc ""
danielk197759a33f92007-03-17 10:26:59 +00001421 set sql [lindex $args end]
mistachkin1d406e02013-08-29 01:09:14 +00001422
danielk197759a33f92007-03-17 10:26:59 +00001423 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
1424 set z [lindex $args $ii]
1425 set n [string length $z]
1426 set z2 [lindex $args [expr $ii+1]]
1427
1428 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
dan999cd082013-12-09 20:42:03 +00001429 elseif {$n>1 && [string first $z -opendb]==0} {set opendb $z2} \
drhcb1f0f62008-01-08 15:18:52 +00001430 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +00001431 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +00001432 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +00001433 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +00001434 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +00001435 else { error "Unrecognized option: $z" }
1436 }
1437
1438 if {$crashfile eq ""} {
1439 error "Compulsory option -file missing"
1440 }
1441
mistachkin1d406e02013-08-29 01:09:14 +00001442 # $crashfile gets compared to the native filename in
shanehf2c08822010-07-08 16:30:44 +00001443 # cfSync(), which can be different then what TCL uses by
1444 # default, so here we force it to the "nativename" format.
mistachkinf8a78462012-03-08 20:00:36 +00001445 set cfile [string map {\\ \\\\} [file nativename [file join [get_pwd] $crashfile]]]
danielk1977aca790a2005-01-13 11:07:52 +00001446
1447 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +00001448 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +00001449 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +00001450 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk1977933bbd62007-03-17 07:22:42 +00001451
1452 # This block sets the cache size of the main database to 10
1453 # pages. This is done in case the build is configured to omit
1454 # "PRAGMA cache_size".
danec16d982015-04-17 08:36:05 +00001455 if {$opendb!=""} {
1456 puts $f $opendb
1457 puts $f {db eval {SELECT * FROM sqlite_master;}}
1458 puts $f {set bt [btree_from_db db]}
1459 puts $f {btree_set_cache_size $bt 10}
1460 }
dan999cd082013-12-09 20:42:03 +00001461
drhcb1f0f62008-01-08 15:18:52 +00001462 if {$prngseed} {
1463 set seed [expr {$prngseed%10007+1}]
1464 # puts seed=$seed
1465 puts $f "db eval {SELECT randomblob($seed)}"
1466 }
danielk1977933bbd62007-03-17 07:22:42 +00001467
drhf8587402008-01-08 16:03:49 +00001468 if {[string length $tclbody]>0} {
1469 puts $f $tclbody
1470 }
1471 if {[string length $sql]>0} {
1472 puts $f "db eval {"
1473 puts $f "$sql"
1474 puts $f "}"
1475 }
danielk1977aca790a2005-01-13 11:07:52 +00001476 close $f
danielk1977aca790a2005-01-13 11:07:52 +00001477 set r [catch {
drh9c06c952005-11-26 00:25:00 +00001478 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +00001479 } msg]
mistachkin1d406e02013-08-29 01:09:14 +00001480
shanehf2c08822010-07-08 16:30:44 +00001481 # Windows/ActiveState TCL returns a slightly different
1482 # error message. We map that to the expected message
1483 # so that we don't have to change all of the test
1484 # cases.
1485 if {$::tcl_platform(platform)=="windows"} {
1486 if {$msg=="child killed: unknown signal"} {
1487 set msg "child process exited abnormally"
1488 }
1489 }
mistachkin1d406e02013-08-29 01:09:14 +00001490
danielk1977aca790a2005-01-13 11:07:52 +00001491 lappend r $msg
1492}
1493
danb88e24f2013-02-23 18:58:11 +00001494proc run_ioerr_prep {} {
1495 set ::sqlite_io_error_pending 0
1496 catch {db close}
1497 catch {db2 close}
1498 catch {forcedelete test.db}
1499 catch {forcedelete test.db-journal}
1500 catch {forcedelete test2.db}
1501 catch {forcedelete test2.db-journal}
1502 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
1503 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
1504 if {[info exists ::ioerropts(-tclprep)]} {
1505 eval $::ioerropts(-tclprep)
1506 }
1507 if {[info exists ::ioerropts(-sqlprep)]} {
1508 execsql $::ioerropts(-sqlprep)
1509 }
1510 expr 0
1511}
1512
danielk197732554c12005-01-22 03:39:39 +00001513# Usage: do_ioerr_test <test number> <options...>
1514#
1515# This proc is used to implement test cases that check that IO errors
mistachkin1d406e02013-08-29 01:09:14 +00001516# are correctly handled. The first argument, <test number>, is an integer
danielk197732554c12005-01-22 03:39:39 +00001517# used to name the tests executed by this proc. Options are as follows:
1518#
1519# -tclprep TCL script to run to prepare test.
1520# -sqlprep SQL script to run to prepare test.
1521# -tclbody TCL script to run with IO error simulation.
1522# -sqlbody TCL script to run with IO error simulation.
1523# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +00001524# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +00001525# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +00001526# -start Value of 'N' to begin with (default 1)
1527#
1528# -cksum Boolean. If true, test that the database does
1529# not change during the execution of the test case.
1530#
1531proc do_ioerr_test {testname args} {
1532
danielk197732554c12005-01-22 03:39:39 +00001533 set ::ioerropts(-start) 1
1534 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +00001535 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +00001536 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +00001537 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +00001538 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +00001539 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +00001540 array set ::ioerropts $args
1541
danielk197747cd39c2008-05-12 12:41:15 +00001542 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
1543 # a couple of obscure IO errors that do not return them.
1544 set ::ioerropts(-erc) 0
mistachkin1d406e02013-08-29 01:09:14 +00001545
danb88e24f2013-02-23 18:58:11 +00001546 # Create a single TCL script from the TCL and SQL specified
1547 # as the body of the test.
1548 set ::ioerrorbody {}
1549 if {[info exists ::ioerropts(-tclbody)]} {
1550 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
1551 }
1552 if {[info exists ::ioerropts(-sqlbody)]} {
1553 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
1554 }
1555
1556 save_prng_state
1557 if {$::ioerropts(-cksum)} {
1558 run_ioerr_prep
1559 eval $::ioerrorbody
1560 set ::goodcksum [cksum]
1561 }
danielk197747cd39c2008-05-12 12:41:15 +00001562
danielk197732554c12005-01-22 03:39:39 +00001563 set ::go 1
drh93aed5a2008-01-16 17:46:38 +00001564 #reset_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +00001565 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +00001566 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +00001567 incr ::ioerropts(-count) -1
1568 if {$::ioerropts(-count)<0} break
mistachkin1d406e02013-08-29 01:09:14 +00001569
danielk197732554c12005-01-22 03:39:39 +00001570 # Skip this IO error if it was specified with the "-exclude" option.
1571 if {[info exists ::ioerropts(-exclude)]} {
1572 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
1573 }
danielk1977861f7452008-06-05 11:39:11 +00001574 if {$::ioerropts(-restoreprng)} {
1575 restore_prng_state
1576 }
danielk197732554c12005-01-22 03:39:39 +00001577
mistachkin1d406e02013-08-29 01:09:14 +00001578 # Delete the files test.db and test2.db, then execute the TCL and
danielk197732554c12005-01-22 03:39:39 +00001579 # SQL (in that order) to prepare for the test case.
1580 do_test $testname.$n.1 {
danb88e24f2013-02-23 18:58:11 +00001581 run_ioerr_prep
danielk197732554c12005-01-22 03:39:39 +00001582 } {0}
1583
1584 # Read the 'checksum' of the database.
1585 if {$::ioerropts(-cksum)} {
danb88e24f2013-02-23 18:58:11 +00001586 set ::checksum [cksum]
danielk197732554c12005-01-22 03:39:39 +00001587 }
drh93aed5a2008-01-16 17:46:38 +00001588
danielk197732554c12005-01-22 03:39:39 +00001589 # Set the Nth IO error to fail.
1590 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +00001591 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +00001592 set ::sqlite_io_error_pending $n
1593 }] $n
danielk197732554c12005-01-22 03:39:39 +00001594
danb88e24f2013-02-23 18:58:11 +00001595 # Execute the TCL script created for the body of this test. If
mistachkin1d406e02013-08-29 01:09:14 +00001596 # at least N IO operations performed by SQLite as a result of
danb88e24f2013-02-23 18:58:11 +00001597 # the script, the Nth will fail.
danielk197732554c12005-01-22 03:39:39 +00001598 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +00001599 set ::sqlite_io_error_hit 0
1600 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +00001601 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +00001602 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +00001603 set rc [sqlite3_errcode $::DB]
1604 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +00001605 # If we are in extended result code mode, make sure all of the
1606 # IOERRs we get back really do have their extended code values.
1607 # If an extended result code is returned, the sqlite3_errcode
1608 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
1609 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +00001610 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
1611 return $rc
1612 }
1613 } else {
drh5f7b5bf2007-04-19 12:30:54 +00001614 # If we are not in extended result code mode, make sure no
1615 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +00001616 if {[regexp {\+\d} $rc]} {
1617 return $rc
1618 }
1619 }
drh93aed5a2008-01-16 17:46:38 +00001620 # The test repeats as long as $::go is non-zero. $::go starts out
1621 # as 1. When a test runs to completion without hitting an I/O
1622 # error, that means there is no point in continuing with this test
1623 # case so set $::go to zero.
1624 #
1625 if {$::sqlite_io_error_pending>0} {
1626 set ::go 0
1627 set q 0
1628 set ::sqlite_io_error_pending 0
1629 } else {
1630 set q 1
1631 }
1632
drhc9ac5ca2005-11-04 22:03:30 +00001633 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +00001634 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
1635 set r 1
1636 }
danielk1977f2fa8312006-01-24 13:09:33 +00001637 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +00001638
1639 # One of two things must have happened. either
1640 # 1. We never hit the IO error and the SQL returned OK
1641 # 2. An IO error was hit and the SQL failed
1642 #
dand41a29a2010-05-06 15:56:28 +00001643 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +00001644 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +00001645 } {1}
1646
danielk197780daec62008-05-12 10:57:02 +00001647 set ::sqlite_io_error_hit 0
1648 set ::sqlite_io_error_pending 0
1649
mistachkin1d406e02013-08-29 01:09:14 +00001650 # Check that no page references were leaked. There should be
1651 # a single reference if there is still an active transaction,
danielk197752b472a2008-05-05 16:23:55 +00001652 # or zero otherwise.
1653 #
danielk197781620542008-06-07 05:19:37 +00001654 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
mistachkin1d406e02013-08-29 01:09:14 +00001655 # locks are established on database files (i.e. if the error
danielk197781620542008-06-07 05:19:37 +00001656 # occurs while attempting to detect a hot-journal file), then
1657 # there may 0 page references and an active transaction according
1658 # to [sqlite3_get_autocommit].
1659 #
danielk19774abd5442008-05-05 15:26:50 +00001660 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +00001661 do_test $testname.$n.4 {
1662 set bt [btree_from_db db]
1663 db_enter db
1664 array set stats [btree_pager_stats $bt]
1665 db_leave db
danielk197781620542008-06-07 05:19:37 +00001666 set nRef $stats(ref)
1667 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
1668 } {1}
danielk19774abd5442008-05-05 15:26:50 +00001669 }
1670
mistachkin1d406e02013-08-29 01:09:14 +00001671 # If there is an open database handle and no open transaction,
danielk197752b472a2008-05-05 16:23:55 +00001672 # and the pager is not running in exclusive-locking mode,
1673 # check that the pager is in "unlocked" state. Theoretically,
1674 # if a call to xUnlock() failed due to an IO error the underlying
1675 # file may still be locked.
1676 #
1677 ifcapable pragma {
1678 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +00001679 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +00001680 && [db one {pragma locking_mode}] eq "normal"
1681 && [sqlite3_get_autocommit db]
1682 } {
1683 do_test $testname.$n.5 {
1684 set bt [btree_from_db db]
1685 db_enter db
1686 array set stats [btree_pager_stats $bt]
1687 db_leave db
1688 set stats(state)
1689 } 0
1690 }
1691 }
1692
mistachkin48864df2013-03-21 21:20:32 +00001693 # If an IO error occurred, then the checksum of the database should
danielk197732554c12005-01-22 03:39:39 +00001694 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +00001695 #
drh1aa5af12008-03-07 19:51:14 +00001696 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +00001697 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +00001698 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +00001699 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +00001700 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danb88e24f2013-02-23 18:58:11 +00001701 set nowcksum [cksum]
1702 set res [expr {$nowcksum==$::checksum || $nowcksum==$::goodcksum}]
1703 if {$res==0} {
dan17c08232015-06-09 15:58:28 +00001704 output2 "now=$nowcksum"
1705 output2 "the=$::checksum"
1706 output2 "fwd=$::goodcksum"
danb88e24f2013-02-23 18:58:11 +00001707 }
1708 set res
1709 } 1
danielk197732554c12005-01-22 03:39:39 +00001710 }
1711
drh1aa5af12008-03-07 19:51:14 +00001712 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +00001713 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +00001714 if {[info exists ::ioerropts(-cleanup)]} {
1715 catch $::ioerropts(-cleanup)
1716 }
danielk197732554c12005-01-22 03:39:39 +00001717 }
1718 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +00001719 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +00001720 unset ::ioerropts
1721}
1722
drh93aed5a2008-01-16 17:46:38 +00001723# Return a checksum based on the contents of the main database associated
1724# with connection $db
danielk197732554c12005-01-22 03:39:39 +00001725#
1726proc cksum {{db db}} {
1727 set txt [$db eval {
1728 SELECT name, type, sql FROM sqlite_master order by name
1729 }]\n
1730 foreach tbl [$db eval {
1731 SELECT name FROM sqlite_master WHERE type='table' order by name
1732 }] {
1733 append txt [$db eval "SELECT * FROM $tbl"]\n
1734 }
1735 foreach prag {default_synchronous default_cache_size} {
1736 append txt $prag-[$db eval "PRAGMA $prag"]\n
1737 }
1738 set cksum [string length $txt]-[md5 $txt]
1739 # puts $cksum-[file size test.db]
1740 return $cksum
1741}
1742
drh93aed5a2008-01-16 17:46:38 +00001743# Generate a checksum based on the contents of the main and temp tables
1744# database $db. If the checksum of two databases is the same, and the
1745# integrity-check passes for both, the two databases are identical.
1746#
drh1db639c2008-01-17 02:36:28 +00001747proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +00001748 set ret [list]
1749 ifcapable tempdb {
1750 set sql {
1751 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1752 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
1753 SELECT 'sqlite_master' UNION
1754 SELECT 'sqlite_temp_master' ORDER BY 1
1755 }
1756 } else {
1757 set sql {
1758 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1759 SELECT 'sqlite_master' ORDER BY 1
1760 }
1761 }
1762 set tbllist [$db eval $sql]
1763 set txt {}
1764 foreach tbl $tbllist {
1765 append txt [$db eval "SELECT * FROM $tbl"]
1766 }
1767 foreach prag {default_cache_size} {
1768 append txt $prag-[$db eval "PRAGMA $prag"]\n
1769 }
1770 # puts txt=$txt
1771 return [md5 $txt]
1772}
1773
drhdc2c4912009-02-04 22:46:47 +00001774# Generate a checksum based on the contents of a single database with
mistachkin1d406e02013-08-29 01:09:14 +00001775# a database connection. The name of the database is $dbname.
drhdc2c4912009-02-04 22:46:47 +00001776# Examples of $dbname are "temp" or "main".
1777#
1778proc dbcksum {db dbname} {
1779 if {$dbname=="temp"} {
1780 set master sqlite_temp_master
1781 } else {
1782 set master $dbname.sqlite_master
1783 }
1784 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
1785 set txt [$db eval "SELECT * FROM $master"]\n
1786 foreach tab $alltab {
1787 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
1788 }
1789 return [md5 $txt]
1790}
1791
danielk197735754ac2008-03-21 17:29:37 +00001792proc memdebug_log_sql {{filename mallocs.sql}} {
1793
danielk19776f332c12008-03-21 14:22:44 +00001794 set data [sqlite3_memdebug_log dump]
1795 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +00001796 if {$nFrame < 0} { return "" }
1797
danielk197735754ac2008-03-21 17:29:37 +00001798 set database temp
1799
danielk19776ab3a2e2009-02-19 14:39:25 +00001800 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +00001801
1802 set sql ""
1803 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +00001804 set nCall [lindex $e 0]
1805 set nByte [lindex $e 1]
1806 set lStack [lrange $e 2 end]
1807 append sql "INSERT INTO ${database}.malloc VALUES"
1808 append sql "('test', $nCall, $nByte, '$lStack');\n"
1809 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +00001810 set frames($f) 1
1811 }
1812 }
1813
1814 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +00001815 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +00001816
1817 foreach f [array names frames] {
1818 set addr [format %x $f]
1819 set cmd "addr2line -e [info nameofexec] $addr"
1820 set line [eval exec $cmd]
1821 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00001822
1823 set file [lindex [split $line :] 0]
1824 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00001825 }
1826
danielk197735754ac2008-03-21 17:29:37 +00001827 foreach f [array names files] {
1828 set contents ""
1829 catch {
1830 set fd [open $f]
1831 set contents [read $fd]
1832 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001833 }
danielk197735754ac2008-03-21 17:29:37 +00001834 set contents [string map {' ''} $contents]
1835 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00001836 }
danielk19776f332c12008-03-21 14:22:44 +00001837
danielk197735754ac2008-03-21 17:29:37 +00001838 set fd [open $filename w]
1839 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
1840 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001841}
drh93aed5a2008-01-16 17:46:38 +00001842
danf5894502009-10-07 18:41:19 +00001843# Drop all tables in database [db]
1844proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00001845 ifcapable trigger&&foreignkey {
1846 set pk [$db one "PRAGMA foreign_keys"]
1847 $db eval "PRAGMA foreign_keys = OFF"
1848 }
drh9a6ffc82010-02-15 18:03:20 +00001849 foreach {idx name file} [db eval {PRAGMA database_list}] {
1850 if {$idx==1} {
1851 set master sqlite_temp_master
1852 } else {
1853 set master $name.sqlite_master
1854 }
1855 foreach {t type} [$db eval "
1856 SELECT name, type FROM $master
dana16d1062010-09-28 17:37:28 +00001857 WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X'
drh9a6ffc82010-02-15 18:03:20 +00001858 "] {
dana16d1062010-09-28 17:37:28 +00001859 $db eval "DROP $type \"$t\""
drh9a6ffc82010-02-15 18:03:20 +00001860 }
danf5894502009-10-07 18:41:19 +00001861 }
shaneh4e7b32f2009-12-17 22:12:51 +00001862 ifcapable trigger&&foreignkey {
1863 $db eval "PRAGMA foreign_keys = $pk"
1864 }
danf5894502009-10-07 18:41:19 +00001865}
1866
dan71cb5182010-04-26 12:39:03 +00001867#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00001868# If a test script is executed with global variable $::G(perm:name) set to
mistachkin1d406e02013-08-29 01:09:14 +00001869# "wal", then the tests are run in WAL mode. Otherwise, they should be run
1870# in rollback mode. The following Tcl procs are used to make this less
dan430e74c2010-06-07 17:47:26 +00001871# intrusive:
dan71cb5182010-04-26 12:39:03 +00001872#
1873# wal_set_journal_mode ?DB?
1874#
1875# If running a WAL test, execute "PRAGMA journal_mode = wal" using
1876# connection handle DB. Otherwise, this command is a no-op.
1877#
1878# wal_check_journal_mode TESTNAME ?DB?
1879#
1880# If running a WAL test, execute a tests case that fails if the main
1881# database for connection handle DB is not currently a WAL database.
1882# Otherwise (if not running a WAL permutation) this is a no-op.
1883#
1884# wal_is_wal_mode
mistachkin1d406e02013-08-29 01:09:14 +00001885#
dan71cb5182010-04-26 12:39:03 +00001886# Returns true if this test should be run in WAL mode. False otherwise.
mistachkin1d406e02013-08-29 01:09:14 +00001887#
dan71cb5182010-04-26 12:39:03 +00001888proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00001889 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00001890}
1891proc wal_set_journal_mode {{db db}} {
1892 if { [wal_is_wal_mode] } {
1893 $db eval "PRAGMA journal_mode = WAL"
1894 }
1895}
1896proc wal_check_journal_mode {testname {db db}} {
1897 if { [wal_is_wal_mode] } {
1898 $db eval { SELECT * FROM sqlite_master }
1899 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
1900 }
1901}
1902
dan430e74c2010-06-07 17:47:26 +00001903proc permutation {} {
1904 set perm ""
1905 catch {set perm $::G(perm:name)}
1906 set perm
1907}
dancb79e512010-08-06 13:50:07 +00001908proc presql {} {
1909 set presql ""
1910 catch {set presql $::G(perm:presql)}
1911 set presql
1912}
dan430e74c2010-06-07 17:47:26 +00001913
danc1a60c52010-06-07 14:28:16 +00001914#-------------------------------------------------------------------------
1915#
1916proc slave_test_script {script} {
1917
1918 # Create the interpreter used to run the test script.
1919 interp create tinterp
1920
1921 # Populate some global variables that tester.tcl expects to see.
1922 foreach {var value} [list \
1923 ::argv0 $::argv0 \
1924 ::argv {} \
1925 ::SLAVE 1 \
1926 ] {
1927 interp eval tinterp [list set $var $value]
1928 }
1929
dan17c08232015-06-09 15:58:28 +00001930 # If output is being copied into a file, share the file-descriptor with
1931 # the interpreter.
1932 if {[info exists ::G(output_fd)]} {
1933 interp share {} $::G(output_fd) tinterp
1934 }
1935
danc1a60c52010-06-07 14:28:16 +00001936 # The alias used to access the global test counters.
1937 tinterp alias set_test_counter set_test_counter
1938
1939 # Set up the ::cmdlinearg array in the slave.
1940 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
1941
dan430e74c2010-06-07 17:47:26 +00001942 # Set up the ::G array in the slave.
1943 interp eval tinterp [list array set ::G [array get ::G]]
1944
danc1a60c52010-06-07 14:28:16 +00001945 # Load the various test interfaces implemented in C.
1946 load_testfixture_extensions tinterp
1947
1948 # Run the test script.
1949 interp eval tinterp $script
1950
danea5542d2010-07-06 11:26:15 +00001951 # Check if the interpreter call [run_thread_tests]
1952 if { [interp eval tinterp {info exists ::run_thread_tests_called}] } {
1953 set ::run_thread_tests_called 1
1954 }
1955
danc1a60c52010-06-07 14:28:16 +00001956 # Delete the interpreter used to run the test script.
1957 interp delete tinterp
1958}
1959
dan430e74c2010-06-07 17:47:26 +00001960proc slave_test_file {zFile} {
dan0626dfc2010-06-15 06:56:37 +00001961 set tail [file tail $zFile]
dan430e74c2010-06-07 17:47:26 +00001962
dan6a64d672011-04-04 15:38:16 +00001963 if {[info exists ::G(start:permutation)]} {
1964 if {[permutation] != $::G(start:permutation)} return
1965 unset ::G(start:permutation)
1966 }
1967 if {[info exists ::G(start:file)]} {
1968 if {$tail != $::G(start:file) && $tail!="$::G(start:file).test"} return
1969 unset ::G(start:file)
1970 }
1971
dan92d516a2010-07-05 14:54:48 +00001972 # Remember the value of the shared-cache setting. So that it is possible
1973 # to check afterwards that it was not modified by the test script.
1974 #
1975 ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] }
dan0626dfc2010-06-15 06:56:37 +00001976
dan92d516a2010-07-05 14:54:48 +00001977 # Run the test script in a slave interpreter.
1978 #
danea5542d2010-07-06 11:26:15 +00001979 unset -nocomplain ::run_thread_tests_called
dan0626dfc2010-06-15 06:56:37 +00001980 reset_prng_state
1981 set ::sqlite_open_file_count 0
1982 set time [time { slave_test_script [list source $zFile] }]
1983 set ms [expr [lindex $time 0] / 1000]
1984
dan92d516a2010-07-05 14:54:48 +00001985 # Test that all files opened by the test script were closed. Omit this
1986 # if the test script has "thread" in its name. The open file counter
1987 # is not thread-safe.
dan0626dfc2010-06-15 06:56:37 +00001988 #
danea5542d2010-07-06 11:26:15 +00001989 if {[info exists ::run_thread_tests_called]==0} {
dan92d516a2010-07-05 14:54:48 +00001990 do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0}
1991 }
dan430e74c2010-06-07 17:47:26 +00001992 set ::sqlite_open_file_count 0
1993
mistachkin1d406e02013-08-29 01:09:14 +00001994 # Test that the global "shared-cache" setting was not altered by
dan0626dfc2010-06-15 06:56:37 +00001995 # the test script.
1996 #
mistachkin1d406e02013-08-29 01:09:14 +00001997 ifcapable shared_cache {
dan0626dfc2010-06-15 06:56:37 +00001998 set res [expr {[sqlite3_enable_shared_cache] == $scs}]
1999 do_test ${tail}-sharedcachesetting [list set {} $res] 1
2000 }
dan430e74c2010-06-07 17:47:26 +00002001
dan92d516a2010-07-05 14:54:48 +00002002 # Add some info to the output.
2003 #
dan17c08232015-06-09 15:58:28 +00002004 output2 "Time: $tail $ms ms"
dan430e74c2010-06-07 17:47:26 +00002005 show_memstats
danc1a60c52010-06-07 14:28:16 +00002006}
2007
dan59257dc2010-08-04 11:34:31 +00002008# Open a new connection on database test.db and execute the SQL script
2009# supplied as an argument. Before returning, close the new conection and
2010# restore the 4 byte fields starting at header offsets 28, 92 and 96
2011# to the values they held before the SQL was executed. This simulates
2012# a write by a pre-3.7.0 client.
2013#
2014proc sql36231 {sql} {
2015 set B [hexio_read test.db 92 8]
2016 set A [hexio_read test.db 28 4]
2017 sqlite3 db36231 test.db
2018 catch { db36231 func a_string a_string }
2019 execsql $sql db36231
2020 db36231 close
2021 hexio_write test.db 28 $A
2022 hexio_write test.db 92 $B
2023 return ""
2024}
danf5894502009-10-07 18:41:19 +00002025
danccc7ff42010-11-29 12:06:45 +00002026proc db_save {} {
2027 foreach f [glob -nocomplain sv_test.db*] { forcedelete $f }
2028 foreach f [glob -nocomplain test.db*] {
2029 set f2 "sv_$f"
mistachkinfda06be2011-08-02 00:57:34 +00002030 forcecopy $f $f2
danccc7ff42010-11-29 12:06:45 +00002031 }
2032}
2033proc db_save_and_close {} {
2034 db_save
2035 catch { db close }
2036 return ""
2037}
2038proc db_restore {} {
2039 foreach f [glob -nocomplain test.db*] { forcedelete $f }
2040 foreach f2 [glob -nocomplain sv_test.db*] {
2041 set f [string range $f2 3 end]
mistachkinfda06be2011-08-02 00:57:34 +00002042 forcecopy $f2 $f
danccc7ff42010-11-29 12:06:45 +00002043 }
2044}
2045proc db_restore_and_reopen {{dbfile test.db}} {
2046 catch { db close }
2047 db_restore
2048 sqlite3 db $dbfile
2049}
2050proc db_delete_and_reopen {{file test.db}} {
2051 catch { db close }
mistachkinfda06be2011-08-02 00:57:34 +00002052 foreach f [glob -nocomplain test.db*] { forcedelete $f }
danccc7ff42010-11-29 12:06:45 +00002053 sqlite3 db $file
2054}
2055
dan03bc5252015-07-24 14:17:17 +00002056# Close any connections named [db], [db2] or [db3]. Then use sqlite3_config
2057# to configure the size of the PAGECACHE allocation using the parameters
2058# provided to this command. Save the old PAGECACHE parameters in a global
2059# variable so that [test_restore_config_pagecache] can restore the previous
2060# configuration.
2061#
2062# Before returning, reopen connection [db] on file test.db.
2063#
2064proc test_set_config_pagecache {sz nPg} {
2065 catch {db close}
2066 catch {db2 close}
2067 catch {db3 close}
2068
2069 sqlite3_shutdown
2070 set ::old_pagecache_config [sqlite3_config_pagecache $sz $nPg]
2071 sqlite3_initialize
2072 autoinstall_test_functions
dand716c392015-07-24 18:22:29 +00002073 reset_db
dan03bc5252015-07-24 14:17:17 +00002074}
2075
2076# Close any connections named [db], [db2] or [db3]. Then use sqlite3_config
2077# to configure the size of the PAGECACHE allocation to the size saved in
2078# the global variable by an earlier call to [test_set_config_pagecache].
2079#
2080# Before returning, reopen connection [db] on file test.db.
2081#
2082proc test_restore_config_pagecache {} {
2083 catch {db close}
2084 catch {db2 close}
2085 catch {db3 close}
2086
2087 sqlite3_shutdown
2088 eval sqlite3_config_pagecache $::old_pagecache_config
2089 unset ::old_pagecache_config
2090 sqlite3_initialize
2091 autoinstall_test_functions
2092 sqlite3 db test.db
2093}
2094
danielk197745901d62004-11-10 15:27:38 +00002095# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
2096# to non-zero, then set the global variable $AUTOVACUUM to 1.
2097set AUTOVACUUM $sqlite_options(default_autovacuum)
danielk1977ee8b7992009-03-26 17:13:06 +00002098
dan64b95bb2012-05-12 05:30:29 +00002099# Make sure the FTS enhanced query syntax is disabled.
2100set sqlite_fts3_enable_parentheses 0
2101
drh09fe6142013-11-29 15:06:27 +00002102# During testing, assume that all database files are well-formed. The
2103# few test cases that deliberately corrupt database files should rescind
2104# this setting by invoking "database_can_be_corrupt"
2105#
2106database_never_corrupt
2107
danielk1977ee8b7992009-03-26 17:13:06 +00002108source $testdir/thread_common.tcl
danddf80eb2010-10-25 12:47:43 +00002109source $testdir/malloc_common.tcl