blob: c1272210d0b42c9518bac17bb1779b3cfedbae8e [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
dan6f343962011-07-01 18:26:40 +000027# drop_all_tables ?DB?
mistachkinfda06be2011-08-02 00:57:34 +000028# forcecopy FROM TO
danc1a60c52010-06-07 14:28:16 +000029# forcedelete FILENAME
30#
31# Test the capability of the SQLite version built into the interpreter to
32# determine if a specific test can be run:
33#
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
danc1a60c52010-06-07 14:28:16 +000062#
dan430e74c2010-06-07 17:47:26 +000063# Commands providing a lower level interface to the global test counters:
danc1a60c52010-06-07 14:28:16 +000064#
65# set_test_counter COUNTER ?VALUE?
mistachkin6c3c1a02011-11-12 03:17:40 +000066# omit_test TESTNAME REASON ?APPEND?
danc1a60c52010-06-07 14:28:16 +000067# fail_test TESTNAME
68# incr_ntest
69#
70# Command run at the end of each test file:
71#
72# finish_test
73#
dan430e74c2010-06-07 17:47:26 +000074# Commands to help create test files that run with the "WAL" and other
75# permutations (see file permutations.test):
danc1a60c52010-06-07 14:28:16 +000076#
77# wal_is_wal_mode
78# wal_set_journal_mode ?DB?
79# wal_check_journal_mode TESTNAME?DB?
dan430e74c2010-06-07 17:47:26 +000080# permutation
dancb79e512010-08-06 13:50:07 +000081# presql
danc1a60c52010-06-07 14:28:16 +000082#
drh348784e2000-05-29 20:41:49 +000083
mistachkin1d406e02013-08-29 01:09:14 +000084# Set the precision of FP arithmatic used by the interpreter. And
danc1a60c52010-06-07 14:28:16 +000085# configure SQLite to take database file locks on the page that begins
86# 64KB into the database file instead of the one 1GB in. This means
87# the code that handles that special case can be tested without creating
88# very large database files.
89#
drh92febd92004-08-20 18:34:20 +000090set tcl_precision 15
drhc7a3bb92009-02-05 16:31:45 +000091sqlite3_test_control_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +000092
danc1a60c52010-06-07 14:28:16 +000093
mistachkin1d406e02013-08-29 01:09:14 +000094# If the pager codec is available, create a wrapper for the [sqlite3]
danc1a60c52010-06-07 14:28:16 +000095# command that appends "-key {xyzzy}" to the command line. i.e. this:
drh3a7fb7c2007-08-10 16:41:08 +000096#
danc1a60c52010-06-07 14:28:16 +000097# sqlite3 db test.db
drh4a50aac2007-08-23 02:47:53 +000098#
danc1a60c52010-06-07 14:28:16 +000099# becomes
drh4a50aac2007-08-23 02:47:53 +0000100#
danc1a60c52010-06-07 14:28:16 +0000101# sqlite3 db test.db -key {xyzzy}
drh9eb9e262004-02-11 02:18:05 +0000102#
dan430e74c2010-06-07 17:47:26 +0000103if {[info command sqlite_orig]==""} {
drhef4ac8f2004-06-19 00:16:31 +0000104 rename sqlite3 sqlite_orig
105 proc sqlite3 {args} {
dan68928b62010-06-22 13:46:43 +0000106 if {[llength $args]>=2 && [string index [lindex $args 0] 0]!="-"} {
dan430e74c2010-06-07 17:47:26 +0000107 # This command is opening a new database connection.
108 #
109 if {[info exists ::G(perm:sqlite3_args)]} {
110 set args [concat $args $::G(perm:sqlite3_args)]
111 }
dan68928b62010-06-22 13:46:43 +0000112 if {[sqlite_orig -has-codec] && ![info exists ::do_not_use_codec]} {
dan430e74c2010-06-07 17:47:26 +0000113 lappend args -key {xyzzy}
114 }
115
dan3ccd20a2010-06-09 19:01:02 +0000116 set res [uplevel 1 sqlite_orig $args]
dan430e74c2010-06-07 17:47:26 +0000117 if {[info exists ::G(perm:presql)]} {
118 [lindex $args 0] eval $::G(perm:presql)
119 }
dan1ce1b4a2010-12-07 14:32:28 +0000120 if {[info exists ::G(perm:dbconfig)]} {
121 set ::dbhandle [lindex $args 0]
122 uplevel #0 $::G(perm:dbconfig)
123 }
dan3ccd20a2010-06-09 19:01:02 +0000124 set res
dan430e74c2010-06-07 17:47:26 +0000125 } else {
mistachkin1d406e02013-08-29 01:09:14 +0000126 # This command is not opening a new database connection. Pass the
mistachkin48864df2013-03-21 21:20:32 +0000127 # arguments through to the C implementation as the are.
dan430e74c2010-06-07 17:47:26 +0000128 #
129 uplevel 1 sqlite_orig $args
drh9eb9e262004-02-11 02:18:05 +0000130 }
drh9eb9e262004-02-11 02:18:05 +0000131 }
132}
133
mistachkinfda06be2011-08-02 00:57:34 +0000134proc getFileRetries {} {
135 if {![info exists ::G(file-retries)]} {
136 #
137 # NOTE: Return the default number of retries for [file] operations. A
138 # value of zero or less here means "disabled".
139 #
mistachkinc60941f2012-09-13 01:51:02 +0000140 return [expr {$::tcl_platform(platform) eq "windows" ? 50 : 0}]
mistachkinfda06be2011-08-02 00:57:34 +0000141 }
142 return $::G(file-retries)
143}
144
145proc getFileRetryDelay {} {
146 if {![info exists ::G(file-retry-delay)]} {
147 #
148 # NOTE: Return the default number of milliseconds to wait when retrying
149 # failed [file] operations. A value of zero or less means "do not
150 # wait".
151 #
152 return 100; # TODO: Good default?
153 }
154 return $::G(file-retry-delay)
155}
156
mistachkinf8a78462012-03-08 20:00:36 +0000157# Return the string representing the name of the current directory. On
158# Windows, the result is "normalized" to whatever our parent command shell
159# is using to prevent case-mismatch issues.
160#
161proc get_pwd {} {
162 if {$::tcl_platform(platform) eq "windows"} {
mistachkin533b8f62012-03-08 20:28:31 +0000163 #
164 # NOTE: Cannot use [file normalize] here because it would alter the
165 # case of the result to what Tcl considers canonical, which would
166 # defeat the purpose of this procedure.
167 #
168 return [string map [list \\ /] \
169 [string trim [exec -- $::env(ComSpec) /c echo %CD%]]]
mistachkinf8a78462012-03-08 20:00:36 +0000170 } else {
171 return [pwd]
172 }
173}
174
mistachkinfda06be2011-08-02 00:57:34 +0000175# Copy file $from into $to. This is used because some versions of
176# TCL for windows (notably the 8.4.1 binary package shipped with the
177# current mingw release) have a broken "file copy" command.
178#
179proc copy_file {from to} {
180 do_copy_file false $from $to
181}
182
183proc forcecopy {from to} {
184 do_copy_file true $from $to
185}
186
187proc do_copy_file {force from to} {
188 set nRetry [getFileRetries] ;# Maximum number of retries.
189 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
190
191 # On windows, sometimes even a [file copy -force] can fail. The cause is
192 # usually "tag-alongs" - programs like anti-virus software, automatic backup
193 # tools and various explorer extensions that keep a file open a little longer
194 # than we expect, causing the delete to fail.
195 #
196 # The solution is to wait a short amount of time before retrying the copy.
197 #
198 if {$nRetry > 0} {
199 for {set i 0} {$i<$nRetry} {incr i} {
200 set rc [catch {
201 if {$force} {
202 file copy -force $from $to
203 } else {
204 file copy $from $to
205 }
206 } msg]
207 if {$rc==0} break
208 if {$nDelay > 0} { after $nDelay }
209 }
210 if {$rc} { error $msg }
211 } else {
212 if {$force} {
213 file copy -force $from $to
214 } else {
215 file copy $from $to
216 }
217 }
218}
219
mistachkinc5484652012-03-05 22:52:33 +0000220# Check if a file name is relative
221#
222proc is_relative_file { file } {
223 return [expr {[file pathtype $file] != "absolute"}]
224}
225
mistachkin4a41f342012-03-06 03:00:49 +0000226# If the VFS supports using the current directory, returns [pwd];
227# otherwise, it returns only the provided suffix string (which is
228# empty by default).
229#
230proc test_pwd { args } {
231 if {[llength $args] > 0} {
232 set suffix1 [lindex $args 0]
233 if {[llength $args] > 1} {
234 set suffix2 [lindex $args 1]
235 } else {
236 set suffix2 $suffix1
237 }
238 } else {
239 set suffix1 ""; set suffix2 ""
240 }
241 ifcapable curdir {
mistachkin6aa18c92012-03-08 20:22:42 +0000242 return "[get_pwd]$suffix1"
mistachkin4a41f342012-03-06 03:00:49 +0000243 } else {
244 return $suffix2
245 }
246}
247
mistachkinfda06be2011-08-02 00:57:34 +0000248# Delete a file or directory
249#
250proc delete_file {args} {
251 do_delete_file false {*}$args
252}
253
254proc forcedelete {args} {
255 do_delete_file true {*}$args
256}
257
258proc do_delete_file {force args} {
259 set nRetry [getFileRetries] ;# Maximum number of retries.
260 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
261
262 foreach filename $args {
263 # On windows, sometimes even a [file delete -force] can fail just after
264 # a file is closed. The cause is usually "tag-alongs" - programs like
265 # anti-virus software, automatic backup tools and various explorer
266 # extensions that keep a file open a little longer than we expect, causing
267 # the delete to fail.
268 #
269 # The solution is to wait a short amount of time before retrying the
270 # delete.
271 #
272 if {$nRetry > 0} {
273 for {set i 0} {$i<$nRetry} {incr i} {
274 set rc [catch {
275 if {$force} {
276 file delete -force $filename
277 } else {
278 file delete $filename
279 }
280 } msg]
281 if {$rc==0} break
282 if {$nDelay > 0} { after $nDelay }
283 }
284 if {$rc} { error $msg }
285 } else {
286 if {$force} {
287 file delete -force $filename
288 } else {
289 file delete $filename
290 }
291 }
292 }
293}
294
mistachkin1d406e02013-08-29 01:09:14 +0000295if {$::tcl_platform(platform) eq "windows"} {
296 proc do_remove_win32_dir {args} {
297 set nRetry [getFileRetries] ;# Maximum number of retries.
298 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
299
300 foreach dirName $args {
301 # On windows, sometimes even a [remove_win32_dir] can fail just after
302 # a directory is emptied. The cause is usually "tag-alongs" - programs
303 # like anti-virus software, automatic backup tools and various explorer
304 # extensions that keep a file open a little longer than we expect,
305 # causing the delete to fail.
306 #
307 # The solution is to wait a short amount of time before retrying the
308 # removal.
309 #
310 if {$nRetry > 0} {
311 for {set i 0} {$i < $nRetry} {incr i} {
312 set rc [catch {
313 remove_win32_dir $dirName
314 } msg]
315 if {$rc == 0} break
316 if {$nDelay > 0} { after $nDelay }
317 }
318 if {$rc} { error $msg }
319 } else {
320 remove_win32_dir $dirName
321 }
322 }
323 }
324
325 proc do_delete_win32_file {args} {
326 set nRetry [getFileRetries] ;# Maximum number of retries.
327 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
328
329 foreach fileName $args {
330 # On windows, sometimes even a [delete_win32_file] can fail just after
331 # a file is closed. The cause is usually "tag-alongs" - programs like
332 # anti-virus software, automatic backup tools and various explorer
333 # extensions that keep a file open a little longer than we expect,
334 # causing the delete to fail.
335 #
336 # The solution is to wait a short amount of time before retrying the
337 # delete.
338 #
339 if {$nRetry > 0} {
340 for {set i 0} {$i < $nRetry} {incr i} {
341 set rc [catch {
342 delete_win32_file $fileName
343 } msg]
344 if {$rc == 0} break
345 if {$nDelay > 0} { after $nDelay }
346 }
347 if {$rc} { error $msg }
348 } else {
349 delete_win32_file $fileName
350 }
351 }
352 }
353}
354
dancb354602010-07-08 09:44:42 +0000355proc execpresql {handle args} {
356 trace remove execution $handle enter [list execpresql $handle]
357 if {[info exists ::G(perm:presql)]} {
358 $handle eval $::G(perm:presql)
359 }
360}
361
dan68928b62010-06-22 13:46:43 +0000362# This command should be called after loading tester.tcl from within
363# all test scripts that are incompatible with encryption codecs.
364#
365proc do_not_use_codec {} {
366 set ::do_not_use_codec 1
367 reset_db
368}
369
dan430e74c2010-06-07 17:47:26 +0000370# The following block only runs the first time this file is sourced. It
371# does not run in slave interpreters (since the ::cmdlinearg array is
372# populated before the test script is run in slave interpreters).
drhbec2bf42000-05-29 23:48:22 +0000373#
danc1a60c52010-06-07 14:28:16 +0000374if {[info exists cmdlinearg]==0} {
375
mistachkin1d406e02013-08-29 01:09:14 +0000376 # Parse any options specified in the $argv array. This script accepts the
377 # following options:
danc1a60c52010-06-07 14:28:16 +0000378 #
379 # --pause
380 # --soft-heap-limit=NN
381 # --maxerror=NN
382 # --malloctrace=N
383 # --backtrace=N
384 # --binarylog=N
dan0626dfc2010-06-15 06:56:37 +0000385 # --soak=N
mistachkinfda06be2011-08-02 00:57:34 +0000386 # --file-retries=N
387 # --file-retry-delay=N
dan6a64d672011-04-04 15:38:16 +0000388 # --start=[$permutation:]$testfile
mistachkin6c3c1a02011-11-12 03:17:40 +0000389 # --match=$pattern
danc1a60c52010-06-07 14:28:16 +0000390 #
391 set cmdlinearg(soft-heap-limit) 0
392 set cmdlinearg(maxerror) 1000
393 set cmdlinearg(malloctrace) 0
394 set cmdlinearg(backtrace) 10
395 set cmdlinearg(binarylog) 0
dan0626dfc2010-06-15 06:56:37 +0000396 set cmdlinearg(soak) 0
mistachkinfda06be2011-08-02 00:57:34 +0000397 set cmdlinearg(file-retries) 0
398 set cmdlinearg(file-retry-delay) 0
mistachkin6c3c1a02011-11-12 03:17:40 +0000399 set cmdlinearg(start) ""
400 set cmdlinearg(match) ""
danc1a60c52010-06-07 14:28:16 +0000401
402 set leftover [list]
403 foreach a $argv {
404 switch -regexp -- $a {
405 {^-+pause$} {
mistachkin1d406e02013-08-29 01:09:14 +0000406 # Wait for user input before continuing. This is to give the user an
danc1a60c52010-06-07 14:28:16 +0000407 # opportunity to connect profiling tools to the process.
408 puts -nonewline "Press RETURN to begin..."
409 flush stdout
410 gets stdin
411 }
412 {^-+soft-heap-limit=.+$} {
413 foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break
414 }
415 {^-+maxerror=.+$} {
416 foreach {dummy cmdlinearg(maxerror)} [split $a =] break
417 }
418 {^-+malloctrace=.+$} {
419 foreach {dummy cmdlinearg(malloctrace)} [split $a =] break
420 if {$cmdlinearg(malloctrace)} {
421 sqlite3_memdebug_log start
422 }
423 }
424 {^-+backtrace=.+$} {
425 foreach {dummy cmdlinearg(backtrace)} [split $a =] break
dan0626dfc2010-06-15 06:56:37 +0000426 sqlite3_memdebug_backtrace $value
danc1a60c52010-06-07 14:28:16 +0000427 }
danc1a60c52010-06-07 14:28:16 +0000428 {^-+binarylog=.+$} {
429 foreach {dummy cmdlinearg(binarylog)} [split $a =] break
430 }
dan0626dfc2010-06-15 06:56:37 +0000431 {^-+soak=.+$} {
432 foreach {dummy cmdlinearg(soak)} [split $a =] break
433 set ::G(issoak) $cmdlinearg(soak)
434 }
mistachkinfda06be2011-08-02 00:57:34 +0000435 {^-+file-retries=.+$} {
436 foreach {dummy cmdlinearg(file-retries)} [split $a =] break
437 set ::G(file-retries) $cmdlinearg(file-retries)
438 }
439 {^-+file-retry-delay=.+$} {
440 foreach {dummy cmdlinearg(file-retry-delay)} [split $a =] break
441 set ::G(file-retry-delay) $cmdlinearg(file-retry-delay)
442 }
dan6a64d672011-04-04 15:38:16 +0000443 {^-+start=.+$} {
444 foreach {dummy cmdlinearg(start)} [split $a =] break
445
446 set ::G(start:file) $cmdlinearg(start)
447 if {[regexp {(.*):(.*)} $cmdlinearg(start) -> s.perm s.file]} {
448 set ::G(start:permutation) ${s.perm}
449 set ::G(start:file) ${s.file}
450 }
451 if {$::G(start:file) == ""} {unset ::G(start:file)}
452 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000453 {^-+match=.+$} {
454 foreach {dummy cmdlinearg(match)} [split $a =] break
455
456 set ::G(match) $cmdlinearg(match)
457 if {$::G(match) == ""} {unset ::G(match)}
458 }
danc1a60c52010-06-07 14:28:16 +0000459 default {
460 lappend leftover $a
461 }
462 }
463 }
464 set argv $leftover
465
dan430e74c2010-06-07 17:47:26 +0000466 # Install the malloc layer used to inject OOM errors. And the 'automatic'
467 # extensions. This only needs to be done once for the process.
468 #
mistachkin1d406e02013-08-29 01:09:14 +0000469 sqlite3_shutdown
470 install_malloc_faultsim 1
danielk1977f7b9d662008-06-23 18:49:43 +0000471 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000472 autoinstall_test_functions
danc1a60c52010-06-07 14:28:16 +0000473
dan430e74c2010-06-07 17:47:26 +0000474 # If the --binarylog option was specified, create the logging VFS. This
475 # call installs the new VFS as the default for all SQLite connections.
476 #
danc1a60c52010-06-07 14:28:16 +0000477 if {$cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000478 vfslog new binarylog {} vfslog.bin
danc1a60c52010-06-07 14:28:16 +0000479 }
480
481 # Set the backtrace depth, if malloc tracing is enabled.
482 #
483 if {$cmdlinearg(malloctrace)} {
484 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danielk1977185eac92008-07-12 15:55:54 +0000485 }
danielk1977f7b9d662008-06-23 18:49:43 +0000486}
danielk197703ba3fa2009-01-09 10:49:14 +0000487
danc1a60c52010-06-07 14:28:16 +0000488# Update the soft-heap-limit each time this script is run. In that
489# way if an individual test file changes the soft-heap-limit, it
490# will be reset at the start of the next test file.
491#
492sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
493
494# Create a test database
495#
danielk197703ba3fa2009-01-09 10:49:14 +0000496proc reset_db {} {
497 catch {db close}
mistachkinfda06be2011-08-02 00:57:34 +0000498 forcedelete test.db
499 forcedelete test.db-journal
500 forcedelete test.db-wal
danielk197703ba3fa2009-01-09 10:49:14 +0000501 sqlite3 db ./test.db
502 set ::DB [sqlite3_connection_pointer db]
503 if {[info exists ::SETUP_SQL]} {
504 db eval $::SETUP_SQL
505 }
drhcd61c282002-03-06 22:01:34 +0000506}
danielk197703ba3fa2009-01-09 10:49:14 +0000507reset_db
drhbec2bf42000-05-29 23:48:22 +0000508
509# Abort early if this script has been run before.
510#
danc1a60c52010-06-07 14:28:16 +0000511if {[info exists TC(count)]} return
drhbec2bf42000-05-29 23:48:22 +0000512
drhce2198c2010-09-10 13:22:59 +0000513# Make sure memory statistics are enabled.
514#
515sqlite3_config_memstatus 1
516
danc1a60c52010-06-07 14:28:16 +0000517# Initialize the test counters and set up commands to access them.
518# Or, if this is a slave interpreter, set up aliases to write the
519# counters in the parent interpreter.
drhbec2bf42000-05-29 23:48:22 +0000520#
danc1a60c52010-06-07 14:28:16 +0000521if {0==[info exists ::SLAVE]} {
522 set TC(errors) 0
523 set TC(count) 0
524 set TC(fail_list) [list]
525 set TC(omit_list) [list]
drhcca17c32013-04-19 12:32:52 +0000526 set TC(warn_list) [list]
danc1a60c52010-06-07 14:28:16 +0000527
528 proc set_test_counter {counter args} {
529 if {[llength $args]} {
530 set ::TC($counter) [lindex $args 0]
531 }
532 set ::TC($counter)
533 }
drhb62c3352006-11-23 09:39:16 +0000534}
drh348784e2000-05-29 20:41:49 +0000535
drh521cc842008-04-15 02:36:33 +0000536# Record the fact that a sequence of tests were omitted.
537#
mistachkin6c3c1a02011-11-12 03:17:40 +0000538proc omit_test {name reason {append 1}} {
danc1a60c52010-06-07 14:28:16 +0000539 set omitList [set_test_counter omit_list]
mistachkin6c3c1a02011-11-12 03:17:40 +0000540 if {$append} {
541 lappend omitList [list $name $reason]
542 }
danc1a60c52010-06-07 14:28:16 +0000543 set_test_counter omit_list $omitList
drh521cc842008-04-15 02:36:33 +0000544}
545
danc1a60c52010-06-07 14:28:16 +0000546# Record the fact that a test failed.
547#
548proc fail_test {name} {
549 set f [set_test_counter fail_list]
550 lappend f $name
551 set_test_counter fail_list $f
552 set_test_counter errors [expr [set_test_counter errors] + 1]
553
554 set nFail [set_test_counter errors]
555 if {$nFail>=$::cmdlinearg(maxerror)} {
556 puts "*** Giving up..."
557 finalize_testing
558 }
559}
560
drhcca17c32013-04-19 12:32:52 +0000561# Remember a warning message to be displayed at the conclusion of all testing
562#
563proc warning {msg {append 1}} {
564 puts "Warning: $msg"
565 set warnList [set_test_counter warn_list]
566 if {$append} {
567 lappend warnList $msg
568 }
569 set_test_counter warn_list $warnList
570}
571
572
danc1a60c52010-06-07 14:28:16 +0000573# Increment the number of tests run
574#
575proc incr_ntest {} {
576 set_test_counter count [expr [set_test_counter count] + 1]
577}
578
579
mistachkin1d406e02013-08-29 01:09:14 +0000580# Invoke the do_test procedure to run a single test
drh348784e2000-05-29 20:41:49 +0000581#
582proc do_test {name cmd expected} {
danc1a60c52010-06-07 14:28:16 +0000583 global argv cmdlinearg
584
dan8c408002010-11-01 17:38:24 +0000585 fix_testname name
586
drh4a50aac2007-08-23 02:47:53 +0000587 sqlite3_memdebug_settitle $name
danc1a60c52010-06-07 14:28:16 +0000588
mistachkin1d406e02013-08-29 01:09:14 +0000589# if {[llength $argv]==0} {
dan92d516a2010-07-05 14:54:48 +0000590# set go 1
591# } else {
592# set go 0
593# foreach pattern $argv {
594# if {[string match $pattern $name]} {
595# set go 1
596# break
597# }
598# }
599# }
dan430e74c2010-06-07 17:47:26 +0000600
dand506de02010-07-03 13:59:01 +0000601 if {[info exists ::G(perm:prefix)]} {
602 set name "$::G(perm:prefix)$name"
dan430e74c2010-06-07 17:47:26 +0000603 }
604
danc1a60c52010-06-07 14:28:16 +0000605 incr_ntest
drh5edc3122001-09-13 21:53:09 +0000606 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000607 flush stdout
mistachkin6c3c1a02011-11-12 03:17:40 +0000608
609 if {![info exists ::G(match)] || [string match $::G(match) $name]} {
610 if {[catch {uplevel #0 "$cmd;\n"} result]} {
611 puts "\nError: $result"
612 fail_test $name
mistachkin6c3c1a02011-11-12 03:17:40 +0000613 } else {
drh3f17aef2012-04-27 01:08:02 +0000614 if {[regexp {^~?/.*/$} $expected]} {
drh70bdcc72013-05-30 19:28:34 +0000615 # "expected" is of the form "/PATTERN/" then the result if correct if
616 # regular expression PATTERN matches the result. "~/PATTERN/" means
617 # the regular expression must not match.
drh3f17aef2012-04-27 01:08:02 +0000618 if {[string index $expected 0]=="~"} {
drhc2b23e72013-11-13 15:32:15 +0000619 set re [string range $expected 2 end-1]
620 if {[string index $re 0]=="*"} {
621 # If the regular expression begins with * then treat it as a glob instead
622 set ok [string match $re $result]
623 } else {
624 set re [string map {# {[-0-9.]+}} $re]
625 set ok [regexp $re $result]
626 }
627 set ok [expr {!$ok}]
drh3f17aef2012-04-27 01:08:02 +0000628 } else {
drhc2b23e72013-11-13 15:32:15 +0000629 set re [string range $expected 1 end-1]
630 if {[string index $re 0]=="*"} {
631 # If the regular expression begins with * then treat it as a glob instead
632 set ok [string match $re $result]
633 } else {
634 set re [string map {# {[-0-9.]+}} $re]
635 set ok [regexp $re $result]
636 }
drh3f17aef2012-04-27 01:08:02 +0000637 }
drh70bdcc72013-05-30 19:28:34 +0000638 } elseif {[regexp {^~?\*.*\*$} $expected]} {
639 # "expected" is of the form "*GLOB*" then the result if correct if
640 # glob pattern GLOB matches the result. "~/GLOB/" means
641 # the glob must not match.
642 if {[string index $expected 0]=="~"} {
643 set e [string range $expected 1 end]
644 set ok [expr {![string match $e $result]}]
645 } else {
646 set ok [string match $expected $result]
647 }
drh3f17aef2012-04-27 01:08:02 +0000648 } else {
649 set ok [expr {[string compare $result $expected]==0}]
650 }
651 if {!$ok} {
mistachkinc60941f2012-09-13 01:51:02 +0000652 # if {![info exists ::testprefix] || $::testprefix eq ""} {
653 # error "no test prefix"
654 # }
drh3f17aef2012-04-27 01:08:02 +0000655 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
656 fail_test $name
657 } else {
658 puts " Ok"
659 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000660 }
drh348784e2000-05-29 20:41:49 +0000661 } else {
mistachkin6c3c1a02011-11-12 03:17:40 +0000662 puts " Omitted"
663 omit_test $name "pattern mismatch" 0
drh348784e2000-05-29 20:41:49 +0000664 }
drh6558db82007-04-13 03:23:21 +0000665 flush stdout
drh348784e2000-05-29 20:41:49 +0000666}
dana3f51082010-09-30 18:43:14 +0000667
drh8df91852012-04-24 12:46:05 +0000668proc catchcmd {db {cmd ""}} {
669 global CLI
670 set out [open cmds.txt w]
671 puts $out $cmd
672 close $out
673 set line "exec $CLI $db < cmds.txt"
674 set rc [catch { eval $line } msg]
675 list $rc $msg
676}
677
shaneh55505172011-06-21 19:30:19 +0000678proc filepath_normalize {p} {
679 # test cases should be written to assume "unix"-like file paths
shaneh285a18f2011-06-21 19:39:59 +0000680 if {$::tcl_platform(platform)!="unix"} {
shaneh55505172011-06-21 19:30:19 +0000681 # lreverse*2 as a hack to remove any unneeded {} after the string map
682 lreverse [lreverse [string map {\\ /} [regsub -nocase -all {[a-z]:[/\\]+} $p {/}]]]
shanehc4896402011-06-21 19:38:16 +0000683 } {
684 set p
shaneh55505172011-06-21 19:30:19 +0000685 }
686}
687proc do_filepath_test {name cmd expected} {
688 uplevel [list do_test $name [
689 subst -nocommands { filepath_normalize [ $cmd ] }
690 ] [filepath_normalize $expected]]
691}
692
dan33f53792011-05-05 19:44:22 +0000693proc realnum_normalize {r} {
shaneh4f529e82011-06-20 17:41:41 +0000694 # different TCL versions display floating point values differently.
695 string map {1.#INF inf Inf inf .0e e} [regsub -all {(e[+-])0+} $r {\1}]
dan33f53792011-05-05 19:44:22 +0000696}
697proc do_realnum_test {name cmd expected} {
698 uplevel [list do_test $name [
699 subst -nocommands { realnum_normalize [ $cmd ] }
700 ] [realnum_normalize $expected]]
701}
702
dana3f51082010-09-30 18:43:14 +0000703proc fix_testname {varname} {
704 upvar $varname testname
mistachkin1d406e02013-08-29 01:09:14 +0000705 if {[info exists ::testprefix]
dana3f51082010-09-30 18:43:14 +0000706 && [string is digit [string range $testname 0 0]]
707 } {
708 set testname "${::testprefix}-$testname"
709 }
710}
mistachkin1d406e02013-08-29 01:09:14 +0000711
dan57f7f4b2010-10-01 19:04:37 +0000712proc do_execsql_test {testname sql {result {}}} {
dana3f51082010-09-30 18:43:14 +0000713 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000714 uplevel do_test [list $testname] [list "execsql {$sql}"] [list [list {*}$result]]
dan153eda02010-06-21 07:45:47 +0000715}
716proc do_catchsql_test {testname sql result} {
dana3f51082010-09-30 18:43:14 +0000717 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000718 uplevel do_test [list $testname] [list "catchsql {$sql}"] [list $result]
dan153eda02010-06-21 07:45:47 +0000719}
dan39854792010-11-15 16:12:58 +0000720proc do_eqp_test {name sql res} {
721 uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res]
722}
dan153eda02010-06-21 07:45:47 +0000723
dan51f06982010-09-18 19:00:12 +0000724#-------------------------------------------------------------------------
725# Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST
726#
727# Where switches are:
728#
729# -errorformat FMTSTRING
730# -count
danaf1dcab2010-09-20 19:17:53 +0000731# -query SQL
dana16d1062010-09-28 17:37:28 +0000732# -tclquery TCL
danaf7626f2010-09-23 18:47:36 +0000733# -repair TCL
dan51f06982010-09-18 19:00:12 +0000734#
735proc do_select_tests {prefix args} {
736
737 set testlist [lindex $args end]
738 set switches [lrange $args 0 end-1]
739
740 set errfmt ""
741 set countonly 0
dana16d1062010-09-28 17:37:28 +0000742 set tclquery ""
danaf7626f2010-09-23 18:47:36 +0000743 set repair ""
dan51f06982010-09-18 19:00:12 +0000744
745 for {set i 0} {$i < [llength $switches]} {incr i} {
746 set s [lindex $switches $i]
747 set n [string length $s]
danaf1dcab2010-09-20 19:17:53 +0000748 if {$n>=2 && [string equal -length $n $s "-query"]} {
dana16d1062010-09-28 17:37:28 +0000749 set tclquery [list execsql [lindex $switches [incr i]]]
750 } elseif {$n>=2 && [string equal -length $n $s "-tclquery"]} {
751 set tclquery [lindex $switches [incr i]]
danaf1dcab2010-09-20 19:17:53 +0000752 } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} {
dan51f06982010-09-18 19:00:12 +0000753 set errfmt [lindex $switches [incr i]]
danaf7626f2010-09-23 18:47:36 +0000754 } elseif {$n>=2 && [string equal -length $n $s "-repair"]} {
755 set repair [lindex $switches [incr i]]
dan51f06982010-09-18 19:00:12 +0000756 } elseif {$n>=2 && [string equal -length $n $s "-count"]} {
757 set countonly 1
758 } else {
759 error "unknown switch: $s"
760 }
761 }
762
763 if {$countonly && $errfmt!=""} {
764 error "Cannot use -count and -errorformat together"
765 }
766 set nTestlist [llength $testlist]
767 if {$nTestlist%3 || $nTestlist==0 } {
768 error "SELECT test list contains [llength $testlist] elements"
769 }
770
danaf7626f2010-09-23 18:47:36 +0000771 eval $repair
dan51f06982010-09-18 19:00:12 +0000772 foreach {tn sql res} $testlist {
dana16d1062010-09-28 17:37:28 +0000773 if {$tclquery != ""} {
danaf1dcab2010-09-20 19:17:53 +0000774 execsql $sql
dana16d1062010-09-28 17:37:28 +0000775 uplevel do_test ${prefix}.$tn [list $tclquery] [list [list {*}$res]]
776 } elseif {$countonly} {
dan51f06982010-09-18 19:00:12 +0000777 set nRow 0
778 db eval $sql {incr nRow}
779 uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res]
780 } elseif {$errfmt==""} {
781 uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]]
782 } else {
783 set res [list 1 [string trim [format $errfmt {*}$res]]]
784 uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res]
785 }
danaf7626f2010-09-23 18:47:36 +0000786 eval $repair
dan51f06982010-09-18 19:00:12 +0000787 }
danaf7626f2010-09-23 18:47:36 +0000788
dan51f06982010-09-18 19:00:12 +0000789}
790
danb04757a2010-09-21 16:59:16 +0000791proc delete_all_data {} {
792 db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} {
793 db eval "DELETE FROM '[string map {' ''} $t]'"
794 }
795}
drh348784e2000-05-29 20:41:49 +0000796
mistachkin1d406e02013-08-29 01:09:14 +0000797# Run an SQL script.
drhb62c3352006-11-23 09:39:16 +0000798# Return the number of microseconds per statement.
799#
drh3590f152006-11-23 21:09:10 +0000800proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000801 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000802 flush stdout
803 set speed [time {sqlite3_exec_nr db $sql}]
804 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000805 if {$tm == 0} {
806 set rate [format %20s "many"]
807 } else {
808 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
809 }
drh3590f152006-11-23 21:09:10 +0000810 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000811 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000812 global total_time
813 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000814 lappend ::speed_trial_times $name $tm
drhdd924312007-03-31 22:34:16 +0000815}
drh45c236d2008-03-22 01:08:00 +0000816proc speed_trial_tcl {name numstmt units script} {
817 puts -nonewline [format {%-21.21s } $name...]
818 flush stdout
819 set speed [time {eval $script}]
820 set tm [lindex $speed 0]
821 if {$tm == 0} {
822 set rate [format %20s "many"]
823 } else {
824 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
825 }
826 set u2 $units/s
827 puts [format {%12d uS %s %s} $tm $rate $u2]
828 global total_time
829 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000830 lappend ::speed_trial_times $name $tm
drh45c236d2008-03-22 01:08:00 +0000831}
drhdd924312007-03-31 22:34:16 +0000832proc speed_trial_init {name} {
833 global total_time
834 set total_time 0
dana975b592010-10-08 16:09:43 +0000835 set ::speed_trial_times [list]
drhbb810a92010-07-03 12:00:53 +0000836 sqlite3 versdb :memory:
837 set vers [versdb one {SELECT sqlite_source_id()}]
838 versdb close
839 puts "SQLite $vers"
drhdd924312007-03-31 22:34:16 +0000840}
841proc speed_trial_summary {name} {
842 global total_time
843 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
dana975b592010-10-08 16:09:43 +0000844
845 if { 0 } {
846 sqlite3 versdb :memory:
847 set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0]
848 versdb close
849 puts "CREATE TABLE IF NOT EXISTS time(version, script, test, us);"
850 foreach {test us} $::speed_trial_times {
851 puts "INSERT INTO time VALUES('$vers', '$name', '$test', $us);"
852 }
853 }
drhb62c3352006-11-23 09:39:16 +0000854}
855
drh348784e2000-05-29 20:41:49 +0000856# Run this routine last
857#
858proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +0000859 catch {db close}
860 catch {db2 close}
861 catch {db3 close}
862 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +0000863}
864proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +0000865 global sqlite_open_file_count
866
867 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +0000868
drh6e142f52000-06-08 13:36:40 +0000869 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000870 catch {db2 close}
871 catch {db3 close}
872
drh9bc54492007-10-23 14:49:59 +0000873 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000874 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000875 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000876 db close
drh984bfaa2008-03-19 16:08:53 +0000877 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +0000878
drh3a7fb7c2007-08-10 16:41:08 +0000879 sqlite3_soft_heap_limit 0
danc1a60c52010-06-07 14:28:16 +0000880 set nTest [incr_ntest]
881 set nErr [set_test_counter errors]
882
drhef866372013-05-22 20:49:02 +0000883 set nKnown 0
884 if {[file readable known-problems.txt]} {
885 set fd [open known-problems.txt]
886 set content [read $fd]
887 close $fd
888 foreach x $content {set known_error($x) 1}
889 foreach x [set_test_counter fail_list] {
890 if {[info exists known_error($x)]} {incr nKnown}
891 }
892 }
893 if {$nKnown>0} {
894 puts "[expr {$nErr-$nKnown}] new errors and $nKnown known errors\
895 out of $nTest tests"
896 } else {
897 puts "$nErr errors out of $nTest tests"
898 }
899 if {$nErr>$nKnown} {
900 puts -nonewline "Failures on these tests:"
901 foreach x [set_test_counter fail_list] {
902 if {![info exists known_error($x)]} {puts -nonewline " $x"}
903 }
904 puts ""
drhf3a65f72007-08-22 20:18:21 +0000905 }
drhcca17c32013-04-19 12:32:52 +0000906 foreach warning [set_test_counter warn_list] {
907 puts "Warning: $warning"
908 }
danielk1977ee8b7992009-03-26 17:13:06 +0000909 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +0000910 if {[llength $omitList]>0} {
911 puts "Omitted test cases:"
912 set prec {}
913 foreach {rec} [lsort $omitList] {
914 if {$rec==$prec} continue
915 set prec $rec
916 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
917 }
918 }
drh80788d82006-09-02 14:50:23 +0000919 if {$nErr>0 && ![working_64bit_int]} {
920 puts "******************************************************************"
921 puts "N.B.: The version of TCL that you used to build this test harness"
922 puts "is defective in that it does not support 64-bit integers. Some or"
923 puts "all of the test failures above might be a result from this defect"
924 puts "in your TCL build."
925 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000926 }
danc1a60c52010-06-07 14:28:16 +0000927 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000928 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +0000929 }
drh94e92032003-02-16 22:21:32 +0000930 if {$sqlite_open_file_count} {
931 puts "$sqlite_open_file_count files were left open"
932 incr nErr
933 }
drheafc43b2010-07-26 18:43:40 +0000934 if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 ||
935 [sqlite3_memory_used]>0} {
936 puts "Unfreed memory: [sqlite3_memory_used] bytes in\
937 [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations"
drhf3a65f72007-08-22 20:18:21 +0000938 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000939 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000940 puts "Writing unfreed memory log to \"./memleak.txt\""
941 sqlite3_memdebug_dump ./memleak.txt
942 }
drhf3a65f72007-08-22 20:18:21 +0000943 } else {
944 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000945 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000946 sqlite3_memdebug_dump ./memusage.txt
947 }
drhf3a65f72007-08-22 20:18:21 +0000948 }
drhf7141992008-06-19 00:16:08 +0000949 show_memstats
drh91fd4d42008-01-19 20:11:25 +0000950 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
951 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000952 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
953 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
954 }
danc1a60c52010-06-07 14:28:16 +0000955 if {$::cmdlinearg(malloctrace)} {
danielk197735754ac2008-03-21 17:29:37 +0000956 puts "Writing mallocs.sql..."
957 memdebug_log_sql
958 sqlite3_memdebug_log stop
959 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000960
961 if {[sqlite3_memory_used]>0} {
962 puts "Writing leaks.sql..."
963 sqlite3_memdebug_log sync
964 memdebug_log_sql leaks.sql
965 }
danielk197735754ac2008-03-21 17:29:37 +0000966 }
drhd9910fe2006-10-04 11:55:49 +0000967 foreach f [glob -nocomplain test.db-*-journal] {
mistachkinfda06be2011-08-02 00:57:34 +0000968 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +0000969 }
970 foreach f [glob -nocomplain test.db-mj*] {
mistachkinfda06be2011-08-02 00:57:34 +0000971 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +0000972 }
drhdb25e382001-03-15 18:21:22 +0000973 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000974}
975
drhf7141992008-06-19 00:16:08 +0000976# Display memory statistics for analysis and debugging purposes.
977#
978proc show_memstats {} {
979 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +0000980 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
981 set val [format {now %10d max %10d max-size %10d} \
982 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000983 puts "Memory used: $val"
drheafc43b2010-07-26 18:43:40 +0000984 set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0]
985 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
986 puts "Allocation count: $val"
drhf7141992008-06-19 00:16:08 +0000987 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +0000988 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
989 set val [format {now %10d max %10d max-size %10d} \
990 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000991 puts "Page-cache used: $val"
992 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
993 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
994 puts "Page-cache overflow: $val"
995 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
996 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
997 puts "Scratch memory used: $val"
998 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +0000999 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
1000 set val [format {now %10d max %10d max-size %10d} \
1001 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +00001002 puts "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +00001003 ifcapable yytrackmaxstackdepth {
1004 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
1005 set val [format { max %10d} [lindex $x 2]]
1006 puts "Parser stack depth: $val"
1007 }
drhf7141992008-06-19 00:16:08 +00001008}
1009
drh348784e2000-05-29 20:41:49 +00001010# A procedure to execute SQL
1011#
drhc4a3c772001-04-04 11:48:57 +00001012proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +00001013 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +00001014 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +00001015}
drh3aadb2e2000-05-31 17:59:25 +00001016
drhadbca9c2001-09-27 15:11:53 +00001017# Execute SQL and catch exceptions.
1018#
1019proc catchsql {sql {db db}} {
1020 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +00001021 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +00001022 lappend r $msg
1023 return $r
1024}
1025
drh04096482001-11-09 22:41:44 +00001026# Do an VDBE code dump on the SQL given
1027#
1028proc explain {sql {db db}} {
1029 puts ""
danielk1977287fb612008-01-04 19:10:28 +00001030 puts "addr opcode p1 p2 p3 p4 p5 #"
1031 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +00001032 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +00001033 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
1034 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
1035 ]
drh04096482001-11-09 22:41:44 +00001036 }
1037}
1038
dane83267d2013-11-05 14:19:22 +00001039proc explain_i {sql {db db}} {
1040 puts ""
1041 puts "addr opcode p1 p2 p3 p4 p5 #"
1042 puts "---- ------------ ------ ------ ------ ---------------- -- -"
1043
dane83267d2013-11-05 14:19:22 +00001044
dan5c663c32013-11-06 14:52:40 +00001045 # Set up colors for the different opcodes. Scheme is as follows:
1046 #
1047 # Red: Opcodes that write to a b-tree.
1048 # Blue: Opcodes that reposition or seek a cursor.
1049 # Green: The ResultRow opcode.
1050 #
1051 set R "\033\[31;1m" ;# Red fg
1052 set G "\033\[32;1m" ;# Green fg
1053 set B "\033\[34;1m" ;# Red fg
1054 set D "\033\[39;0m" ;# Default fg
1055 foreach opcode {
1056 Seek SeekGe SeekGt SeekLe SeekLt NotFound Last Rewind
dane3ab7292013-11-12 12:30:09 +00001057 NoConflict Next Prev VNext VPrev VFilter
dan5c663c32013-11-06 14:52:40 +00001058 } {
1059 set color($opcode) $B
1060 }
1061 foreach opcode {ResultRow} {
1062 set color($opcode) $G
1063 }
1064 foreach opcode {IdxInsert Insert Delete IdxDelete} {
1065 set color($opcode) $R
1066 }
1067
dan427ebba2013-11-05 16:39:31 +00001068 set bSeenGoto 0
dane83267d2013-11-05 14:19:22 +00001069 $db eval "explain $sql" {} {
1070 set x($addr) 0
1071 set op($addr) $opcode
1072
dan427ebba2013-11-05 16:39:31 +00001073 if {$opcode == "Goto" && ($bSeenGoto==0 || ($p2 > $addr+10))} {
1074 set linebreak($p2) 1
1075 set bSeenGoto 1
dane83267d2013-11-05 14:19:22 +00001076 }
1077
dane3ab7292013-11-12 12:30:09 +00001078 if {$opcode=="Next" || $opcode=="Prev"
1079 || $opcode=="VNext" || $opcode=="VPrev"
1080 } {
dane83267d2013-11-05 14:19:22 +00001081 for {set i $p2} {$i<$addr} {incr i} {
1082 incr x($i) 2
1083 }
1084 }
1085
1086 if {$opcode == "Goto" && $p2<$addr && $op($p2)=="Yield"} {
1087 for {set i [expr $p2+1]} {$i<$addr} {incr i} {
1088 incr x($i) 2
1089 }
1090 }
dan427ebba2013-11-05 16:39:31 +00001091
1092 if {$opcode == "Halt" && $comment == "End of coroutine"} {
1093 set linebreak([expr $addr+1]) 1
1094 }
dane83267d2013-11-05 14:19:22 +00001095 }
1096
1097 $db eval "explain $sql" {} {
dan427ebba2013-11-05 16:39:31 +00001098 if {[info exists linebreak($addr)]} {
dane83267d2013-11-05 14:19:22 +00001099 puts ""
1100 }
1101 set I [string repeat " " $x($addr)]
dan5c663c32013-11-06 14:52:40 +00001102
1103 set col ""
1104 catch { set col $color($opcode) }
1105
1106 puts [format {%-4d %s%s%-12.12s%s %-6d %-6d %-6d % -17s %s %s} \
1107 $addr $I $col $opcode $D $p1 $p2 $p3 $p4 $p5 $comment
dane83267d2013-11-05 14:19:22 +00001108 ]
dane83267d2013-11-05 14:19:22 +00001109 }
1110 puts "---- ------------ ------ ------ ------ ---------------- -- -"
1111}
1112
drhdafc0ce2008-04-17 19:14:02 +00001113# Show the VDBE program for an SQL statement but omit the Trace
1114# opcode at the beginning. This procedure can be used to prove
1115# that different SQL statements generate exactly the same VDBE code.
1116#
1117proc explain_no_trace {sql} {
1118 set tr [db eval "EXPLAIN $sql"]
1119 return [lrange $tr 7 end]
1120}
1121
drh3aadb2e2000-05-31 17:59:25 +00001122# Another procedure to execute SQL. This one includes the field
1123# names in the returned list.
1124#
1125proc execsql2 {sql} {
1126 set result {}
1127 db eval $sql data {
1128 foreach f $data(*) {
1129 lappend result $f $data($f)
1130 }
1131 }
1132 return $result
1133}
drh17a68932001-01-31 13:28:08 +00001134
mistachkin1d406e02013-08-29 01:09:14 +00001135# Use a temporary in-memory database to execute SQL statements
1136#
1137proc memdbsql {sql} {
1138 sqlite3 memdb :memory:
1139 set result [memdb eval $sql]
1140 memdb close
1141 return $result
1142}
1143
drhdde85d92003-03-01 19:45:34 +00001144# Use the non-callback API to execute multiple SQL statements
1145#
1146proc stepsql {dbptr sql} {
1147 set sql [string trim $sql]
1148 set r 0
1149 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +00001150 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +00001151 return [list 1 $vm]
1152 }
1153 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +00001154# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
1155# foreach v $VAL {lappend r $v}
1156# }
1157 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
1158 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
1159 lappend r [sqlite3_column_text $vm $i]
1160 }
drhdde85d92003-03-01 19:45:34 +00001161 }
danielk1977106bb232004-05-21 10:08:53 +00001162 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +00001163 return [list 1 $errmsg]
1164 }
1165 }
1166 return $r
1167}
1168
drh21504322002-06-25 13:16:02 +00001169# Do an integrity check of the entire database
1170#
danielk197704103022009-02-03 16:51:24 +00001171proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +00001172 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +00001173 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +00001174 }
1175}
1176
drh433dccf2013-02-09 15:37:11 +00001177# Check the extended error code
1178#
1179proc verify_ex_errcode {name expected {db db}} {
1180 do_test $name [list sqlite3_extended_errcode $db] $expected
1181}
1182
danc6055c72011-04-28 18:46:46 +00001183
1184# Return true if the SQL statement passed as the second argument uses a
1185# statement transaction.
1186#
1187proc sql_uses_stmt {db sql} {
1188 set stmt [sqlite3_prepare $db $sql -1 dummy]
1189 set uses [uses_stmt_journal $stmt]
1190 sqlite3_finalize $stmt
1191 return $uses
1192}
1193
danielk1977db4e8862008-01-16 08:24:46 +00001194proc fix_ifcapable_expr {expr} {
1195 set ret ""
1196 set state 0
1197 for {set i 0} {$i < [string length $expr]} {incr i} {
1198 set char [string range $expr $i $i]
1199 set newstate [expr {[string is alnum $char] || $char eq "_"}]
1200 if {$newstate && !$state} {
1201 append ret {$::sqlite_options(}
1202 }
1203 if {!$newstate && $state} {
1204 append ret )
1205 }
1206 append ret $char
1207 set state $newstate
1208 }
1209 if {$state} {append ret )}
1210 return $ret
1211}
1212
mistachkinc60941f2012-09-13 01:51:02 +00001213# Returns non-zero if the capabilities are present; zero otherwise.
1214#
1215proc capable {expr} {
1216 set e [fix_ifcapable_expr $expr]; return [expr ($e)]
1217}
1218
drh27d258a2004-10-30 20:23:09 +00001219# Evaluate a boolean expression of capabilities. If true, execute the
1220# code. Omit the code if false.
1221#
danielk19773e8c37e2005-01-21 03:12:14 +00001222proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +00001223 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
1224 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +00001225 if ($e2) {
1226 set c [catch {uplevel 1 $code} r]
1227 } else {
1228 set c [catch {uplevel 1 $elsecode} r]
1229 }
1230 return -code $c $r
drh21504322002-06-25 13:16:02 +00001231}
danielk197745901d62004-11-10 15:27:38 +00001232
danielk1977aca790a2005-01-13 11:07:52 +00001233# This proc execs a seperate process that crashes midway through executing
1234# the SQL script $sql on database test.db.
1235#
1236# The crash occurs during a sync() of file $crashfile. When the crash
1237# occurs a random subset of all unsynced writes made by the process are
1238# written into the files on disk. Argument $crashdelay indicates the
1239# number of file syncs to wait before crashing.
1240#
1241# The return value is a list of two elements. The first element is a
1242# boolean, indicating whether or not the process actually crashed or
1243# reported some other error. The second element in the returned list is the
1244# error message. This is "child process exited abnormally" if the crash
mistachkin48864df2013-03-21 21:20:32 +00001245# occurred.
danielk1977aca790a2005-01-13 11:07:52 +00001246#
danielk1977f8940ae2007-08-23 11:07:10 +00001247# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +00001248#
1249proc crashsql {args} {
danielk197759a33f92007-03-17 10:26:59 +00001250
1251 set blocksize ""
1252 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +00001253 set prngseed 0
drhf8587402008-01-08 16:03:49 +00001254 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +00001255 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +00001256 set dc ""
danielk197759a33f92007-03-17 10:26:59 +00001257 set sql [lindex $args end]
mistachkin1d406e02013-08-29 01:09:14 +00001258
danielk197759a33f92007-03-17 10:26:59 +00001259 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
1260 set z [lindex $args $ii]
1261 set n [string length $z]
1262 set z2 [lindex $args [expr $ii+1]]
1263
1264 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +00001265 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +00001266 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +00001267 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +00001268 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +00001269 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +00001270 else { error "Unrecognized option: $z" }
1271 }
1272
1273 if {$crashfile eq ""} {
1274 error "Compulsory option -file missing"
1275 }
1276
mistachkin1d406e02013-08-29 01:09:14 +00001277 # $crashfile gets compared to the native filename in
shanehf2c08822010-07-08 16:30:44 +00001278 # cfSync(), which can be different then what TCL uses by
1279 # default, so here we force it to the "nativename" format.
mistachkinf8a78462012-03-08 20:00:36 +00001280 set cfile [string map {\\ \\\\} [file nativename [file join [get_pwd] $crashfile]]]
danielk1977aca790a2005-01-13 11:07:52 +00001281
1282 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +00001283 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +00001284 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +00001285 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +00001286 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +00001287
1288 # This block sets the cache size of the main database to 10
1289 # pages. This is done in case the build is configured to omit
1290 # "PRAGMA cache_size".
1291 puts $f {db eval {SELECT * FROM sqlite_master;}}
1292 puts $f {set bt [btree_from_db db]}
1293 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +00001294 if {$prngseed} {
1295 set seed [expr {$prngseed%10007+1}]
1296 # puts seed=$seed
1297 puts $f "db eval {SELECT randomblob($seed)}"
1298 }
danielk1977933bbd62007-03-17 07:22:42 +00001299
drhf8587402008-01-08 16:03:49 +00001300 if {[string length $tclbody]>0} {
1301 puts $f $tclbody
1302 }
1303 if {[string length $sql]>0} {
1304 puts $f "db eval {"
1305 puts $f "$sql"
1306 puts $f "}"
1307 }
danielk1977aca790a2005-01-13 11:07:52 +00001308 close $f
danielk1977aca790a2005-01-13 11:07:52 +00001309 set r [catch {
drh9c06c952005-11-26 00:25:00 +00001310 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +00001311 } msg]
mistachkin1d406e02013-08-29 01:09:14 +00001312
shanehf2c08822010-07-08 16:30:44 +00001313 # Windows/ActiveState TCL returns a slightly different
1314 # error message. We map that to the expected message
1315 # so that we don't have to change all of the test
1316 # cases.
1317 if {$::tcl_platform(platform)=="windows"} {
1318 if {$msg=="child killed: unknown signal"} {
1319 set msg "child process exited abnormally"
1320 }
1321 }
mistachkin1d406e02013-08-29 01:09:14 +00001322
danielk1977aca790a2005-01-13 11:07:52 +00001323 lappend r $msg
1324}
1325
danb88e24f2013-02-23 18:58:11 +00001326proc run_ioerr_prep {} {
1327 set ::sqlite_io_error_pending 0
1328 catch {db close}
1329 catch {db2 close}
1330 catch {forcedelete test.db}
1331 catch {forcedelete test.db-journal}
1332 catch {forcedelete test2.db}
1333 catch {forcedelete test2.db-journal}
1334 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
1335 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
1336 if {[info exists ::ioerropts(-tclprep)]} {
1337 eval $::ioerropts(-tclprep)
1338 }
1339 if {[info exists ::ioerropts(-sqlprep)]} {
1340 execsql $::ioerropts(-sqlprep)
1341 }
1342 expr 0
1343}
1344
danielk197732554c12005-01-22 03:39:39 +00001345# Usage: do_ioerr_test <test number> <options...>
1346#
1347# This proc is used to implement test cases that check that IO errors
mistachkin1d406e02013-08-29 01:09:14 +00001348# are correctly handled. The first argument, <test number>, is an integer
danielk197732554c12005-01-22 03:39:39 +00001349# used to name the tests executed by this proc. Options are as follows:
1350#
1351# -tclprep TCL script to run to prepare test.
1352# -sqlprep SQL script to run to prepare test.
1353# -tclbody TCL script to run with IO error simulation.
1354# -sqlbody TCL script to run with IO error simulation.
1355# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +00001356# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +00001357# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +00001358# -start Value of 'N' to begin with (default 1)
1359#
1360# -cksum Boolean. If true, test that the database does
1361# not change during the execution of the test case.
1362#
1363proc do_ioerr_test {testname args} {
1364
danielk197732554c12005-01-22 03:39:39 +00001365 set ::ioerropts(-start) 1
1366 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +00001367 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +00001368 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +00001369 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +00001370 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +00001371 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +00001372 array set ::ioerropts $args
1373
danielk197747cd39c2008-05-12 12:41:15 +00001374 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
1375 # a couple of obscure IO errors that do not return them.
1376 set ::ioerropts(-erc) 0
mistachkin1d406e02013-08-29 01:09:14 +00001377
danb88e24f2013-02-23 18:58:11 +00001378 # Create a single TCL script from the TCL and SQL specified
1379 # as the body of the test.
1380 set ::ioerrorbody {}
1381 if {[info exists ::ioerropts(-tclbody)]} {
1382 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
1383 }
1384 if {[info exists ::ioerropts(-sqlbody)]} {
1385 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
1386 }
1387
1388 save_prng_state
1389 if {$::ioerropts(-cksum)} {
1390 run_ioerr_prep
1391 eval $::ioerrorbody
1392 set ::goodcksum [cksum]
1393 }
danielk197747cd39c2008-05-12 12:41:15 +00001394
danielk197732554c12005-01-22 03:39:39 +00001395 set ::go 1
drh93aed5a2008-01-16 17:46:38 +00001396 #reset_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +00001397 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +00001398 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +00001399 incr ::ioerropts(-count) -1
1400 if {$::ioerropts(-count)<0} break
mistachkin1d406e02013-08-29 01:09:14 +00001401
danielk197732554c12005-01-22 03:39:39 +00001402 # Skip this IO error if it was specified with the "-exclude" option.
1403 if {[info exists ::ioerropts(-exclude)]} {
1404 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
1405 }
danielk1977861f7452008-06-05 11:39:11 +00001406 if {$::ioerropts(-restoreprng)} {
1407 restore_prng_state
1408 }
danielk197732554c12005-01-22 03:39:39 +00001409
mistachkin1d406e02013-08-29 01:09:14 +00001410 # Delete the files test.db and test2.db, then execute the TCL and
danielk197732554c12005-01-22 03:39:39 +00001411 # SQL (in that order) to prepare for the test case.
1412 do_test $testname.$n.1 {
danb88e24f2013-02-23 18:58:11 +00001413 run_ioerr_prep
danielk197732554c12005-01-22 03:39:39 +00001414 } {0}
1415
1416 # Read the 'checksum' of the database.
1417 if {$::ioerropts(-cksum)} {
danb88e24f2013-02-23 18:58:11 +00001418 set ::checksum [cksum]
danielk197732554c12005-01-22 03:39:39 +00001419 }
drh93aed5a2008-01-16 17:46:38 +00001420
danielk197732554c12005-01-22 03:39:39 +00001421 # Set the Nth IO error to fail.
1422 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +00001423 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +00001424 set ::sqlite_io_error_pending $n
1425 }] $n
danielk197732554c12005-01-22 03:39:39 +00001426
danb88e24f2013-02-23 18:58:11 +00001427 # Execute the TCL script created for the body of this test. If
mistachkin1d406e02013-08-29 01:09:14 +00001428 # at least N IO operations performed by SQLite as a result of
danb88e24f2013-02-23 18:58:11 +00001429 # the script, the Nth will fail.
danielk197732554c12005-01-22 03:39:39 +00001430 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +00001431 set ::sqlite_io_error_hit 0
1432 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +00001433 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +00001434 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +00001435 set rc [sqlite3_errcode $::DB]
1436 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +00001437 # If we are in extended result code mode, make sure all of the
1438 # IOERRs we get back really do have their extended code values.
1439 # If an extended result code is returned, the sqlite3_errcode
1440 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
1441 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +00001442 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
1443 return $rc
1444 }
1445 } else {
drh5f7b5bf2007-04-19 12:30:54 +00001446 # If we are not in extended result code mode, make sure no
1447 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +00001448 if {[regexp {\+\d} $rc]} {
1449 return $rc
1450 }
1451 }
drh93aed5a2008-01-16 17:46:38 +00001452 # The test repeats as long as $::go is non-zero. $::go starts out
1453 # as 1. When a test runs to completion without hitting an I/O
1454 # error, that means there is no point in continuing with this test
1455 # case so set $::go to zero.
1456 #
1457 if {$::sqlite_io_error_pending>0} {
1458 set ::go 0
1459 set q 0
1460 set ::sqlite_io_error_pending 0
1461 } else {
1462 set q 1
1463 }
1464
drhc9ac5ca2005-11-04 22:03:30 +00001465 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +00001466 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
1467 set r 1
1468 }
danielk1977f2fa8312006-01-24 13:09:33 +00001469 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +00001470
1471 # One of two things must have happened. either
1472 # 1. We never hit the IO error and the SQL returned OK
1473 # 2. An IO error was hit and the SQL failed
1474 #
dand41a29a2010-05-06 15:56:28 +00001475 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +00001476 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +00001477 } {1}
1478
danielk197780daec62008-05-12 10:57:02 +00001479 set ::sqlite_io_error_hit 0
1480 set ::sqlite_io_error_pending 0
1481
mistachkin1d406e02013-08-29 01:09:14 +00001482 # Check that no page references were leaked. There should be
1483 # a single reference if there is still an active transaction,
danielk197752b472a2008-05-05 16:23:55 +00001484 # or zero otherwise.
1485 #
danielk197781620542008-06-07 05:19:37 +00001486 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
mistachkin1d406e02013-08-29 01:09:14 +00001487 # locks are established on database files (i.e. if the error
danielk197781620542008-06-07 05:19:37 +00001488 # occurs while attempting to detect a hot-journal file), then
1489 # there may 0 page references and an active transaction according
1490 # to [sqlite3_get_autocommit].
1491 #
danielk19774abd5442008-05-05 15:26:50 +00001492 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +00001493 do_test $testname.$n.4 {
1494 set bt [btree_from_db db]
1495 db_enter db
1496 array set stats [btree_pager_stats $bt]
1497 db_leave db
danielk197781620542008-06-07 05:19:37 +00001498 set nRef $stats(ref)
1499 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
1500 } {1}
danielk19774abd5442008-05-05 15:26:50 +00001501 }
1502
mistachkin1d406e02013-08-29 01:09:14 +00001503 # If there is an open database handle and no open transaction,
danielk197752b472a2008-05-05 16:23:55 +00001504 # and the pager is not running in exclusive-locking mode,
1505 # check that the pager is in "unlocked" state. Theoretically,
1506 # if a call to xUnlock() failed due to an IO error the underlying
1507 # file may still be locked.
1508 #
1509 ifcapable pragma {
1510 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +00001511 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +00001512 && [db one {pragma locking_mode}] eq "normal"
1513 && [sqlite3_get_autocommit db]
1514 } {
1515 do_test $testname.$n.5 {
1516 set bt [btree_from_db db]
1517 db_enter db
1518 array set stats [btree_pager_stats $bt]
1519 db_leave db
1520 set stats(state)
1521 } 0
1522 }
1523 }
1524
mistachkin48864df2013-03-21 21:20:32 +00001525 # If an IO error occurred, then the checksum of the database should
danielk197732554c12005-01-22 03:39:39 +00001526 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +00001527 #
drh1aa5af12008-03-07 19:51:14 +00001528 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +00001529 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +00001530 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +00001531 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +00001532 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danb88e24f2013-02-23 18:58:11 +00001533 set nowcksum [cksum]
1534 set res [expr {$nowcksum==$::checksum || $nowcksum==$::goodcksum}]
1535 if {$res==0} {
1536 puts "now=$nowcksum"
1537 puts "the=$::checksum"
1538 puts "fwd=$::goodcksum"
1539 }
1540 set res
1541 } 1
danielk197732554c12005-01-22 03:39:39 +00001542 }
1543
drh1aa5af12008-03-07 19:51:14 +00001544 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +00001545 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +00001546 if {[info exists ::ioerropts(-cleanup)]} {
1547 catch $::ioerropts(-cleanup)
1548 }
danielk197732554c12005-01-22 03:39:39 +00001549 }
1550 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +00001551 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +00001552 unset ::ioerropts
1553}
1554
drh93aed5a2008-01-16 17:46:38 +00001555# Return a checksum based on the contents of the main database associated
1556# with connection $db
danielk197732554c12005-01-22 03:39:39 +00001557#
1558proc cksum {{db db}} {
1559 set txt [$db eval {
1560 SELECT name, type, sql FROM sqlite_master order by name
1561 }]\n
1562 foreach tbl [$db eval {
1563 SELECT name FROM sqlite_master WHERE type='table' order by name
1564 }] {
1565 append txt [$db eval "SELECT * FROM $tbl"]\n
1566 }
1567 foreach prag {default_synchronous default_cache_size} {
1568 append txt $prag-[$db eval "PRAGMA $prag"]\n
1569 }
1570 set cksum [string length $txt]-[md5 $txt]
1571 # puts $cksum-[file size test.db]
1572 return $cksum
1573}
1574
drh93aed5a2008-01-16 17:46:38 +00001575# Generate a checksum based on the contents of the main and temp tables
1576# database $db. If the checksum of two databases is the same, and the
1577# integrity-check passes for both, the two databases are identical.
1578#
drh1db639c2008-01-17 02:36:28 +00001579proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +00001580 set ret [list]
1581 ifcapable tempdb {
1582 set sql {
1583 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1584 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
1585 SELECT 'sqlite_master' UNION
1586 SELECT 'sqlite_temp_master' ORDER BY 1
1587 }
1588 } else {
1589 set sql {
1590 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1591 SELECT 'sqlite_master' ORDER BY 1
1592 }
1593 }
1594 set tbllist [$db eval $sql]
1595 set txt {}
1596 foreach tbl $tbllist {
1597 append txt [$db eval "SELECT * FROM $tbl"]
1598 }
1599 foreach prag {default_cache_size} {
1600 append txt $prag-[$db eval "PRAGMA $prag"]\n
1601 }
1602 # puts txt=$txt
1603 return [md5 $txt]
1604}
1605
drhdc2c4912009-02-04 22:46:47 +00001606# Generate a checksum based on the contents of a single database with
mistachkin1d406e02013-08-29 01:09:14 +00001607# a database connection. The name of the database is $dbname.
drhdc2c4912009-02-04 22:46:47 +00001608# Examples of $dbname are "temp" or "main".
1609#
1610proc dbcksum {db dbname} {
1611 if {$dbname=="temp"} {
1612 set master sqlite_temp_master
1613 } else {
1614 set master $dbname.sqlite_master
1615 }
1616 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
1617 set txt [$db eval "SELECT * FROM $master"]\n
1618 foreach tab $alltab {
1619 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
1620 }
1621 return [md5 $txt]
1622}
1623
danielk197735754ac2008-03-21 17:29:37 +00001624proc memdebug_log_sql {{filename mallocs.sql}} {
1625
danielk19776f332c12008-03-21 14:22:44 +00001626 set data [sqlite3_memdebug_log dump]
1627 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +00001628 if {$nFrame < 0} { return "" }
1629
danielk197735754ac2008-03-21 17:29:37 +00001630 set database temp
1631
danielk19776ab3a2e2009-02-19 14:39:25 +00001632 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +00001633
1634 set sql ""
1635 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +00001636 set nCall [lindex $e 0]
1637 set nByte [lindex $e 1]
1638 set lStack [lrange $e 2 end]
1639 append sql "INSERT INTO ${database}.malloc VALUES"
1640 append sql "('test', $nCall, $nByte, '$lStack');\n"
1641 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +00001642 set frames($f) 1
1643 }
1644 }
1645
1646 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +00001647 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +00001648
1649 foreach f [array names frames] {
1650 set addr [format %x $f]
1651 set cmd "addr2line -e [info nameofexec] $addr"
1652 set line [eval exec $cmd]
1653 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00001654
1655 set file [lindex [split $line :] 0]
1656 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00001657 }
1658
danielk197735754ac2008-03-21 17:29:37 +00001659 foreach f [array names files] {
1660 set contents ""
1661 catch {
1662 set fd [open $f]
1663 set contents [read $fd]
1664 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001665 }
danielk197735754ac2008-03-21 17:29:37 +00001666 set contents [string map {' ''} $contents]
1667 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00001668 }
danielk19776f332c12008-03-21 14:22:44 +00001669
danielk197735754ac2008-03-21 17:29:37 +00001670 set fd [open $filename w]
1671 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
1672 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001673}
drh93aed5a2008-01-16 17:46:38 +00001674
danf5894502009-10-07 18:41:19 +00001675# Drop all tables in database [db]
1676proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00001677 ifcapable trigger&&foreignkey {
1678 set pk [$db one "PRAGMA foreign_keys"]
1679 $db eval "PRAGMA foreign_keys = OFF"
1680 }
drh9a6ffc82010-02-15 18:03:20 +00001681 foreach {idx name file} [db eval {PRAGMA database_list}] {
1682 if {$idx==1} {
1683 set master sqlite_temp_master
1684 } else {
1685 set master $name.sqlite_master
1686 }
1687 foreach {t type} [$db eval "
1688 SELECT name, type FROM $master
dana16d1062010-09-28 17:37:28 +00001689 WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X'
drh9a6ffc82010-02-15 18:03:20 +00001690 "] {
dana16d1062010-09-28 17:37:28 +00001691 $db eval "DROP $type \"$t\""
drh9a6ffc82010-02-15 18:03:20 +00001692 }
danf5894502009-10-07 18:41:19 +00001693 }
shaneh4e7b32f2009-12-17 22:12:51 +00001694 ifcapable trigger&&foreignkey {
1695 $db eval "PRAGMA foreign_keys = $pk"
1696 }
danf5894502009-10-07 18:41:19 +00001697}
1698
dan71cb5182010-04-26 12:39:03 +00001699#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00001700# If a test script is executed with global variable $::G(perm:name) set to
mistachkin1d406e02013-08-29 01:09:14 +00001701# "wal", then the tests are run in WAL mode. Otherwise, they should be run
1702# in rollback mode. The following Tcl procs are used to make this less
dan430e74c2010-06-07 17:47:26 +00001703# intrusive:
dan71cb5182010-04-26 12:39:03 +00001704#
1705# wal_set_journal_mode ?DB?
1706#
1707# If running a WAL test, execute "PRAGMA journal_mode = wal" using
1708# connection handle DB. Otherwise, this command is a no-op.
1709#
1710# wal_check_journal_mode TESTNAME ?DB?
1711#
1712# If running a WAL test, execute a tests case that fails if the main
1713# database for connection handle DB is not currently a WAL database.
1714# Otherwise (if not running a WAL permutation) this is a no-op.
1715#
1716# wal_is_wal_mode
mistachkin1d406e02013-08-29 01:09:14 +00001717#
dan71cb5182010-04-26 12:39:03 +00001718# Returns true if this test should be run in WAL mode. False otherwise.
mistachkin1d406e02013-08-29 01:09:14 +00001719#
dan71cb5182010-04-26 12:39:03 +00001720proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00001721 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00001722}
1723proc wal_set_journal_mode {{db db}} {
1724 if { [wal_is_wal_mode] } {
1725 $db eval "PRAGMA journal_mode = WAL"
1726 }
1727}
1728proc wal_check_journal_mode {testname {db db}} {
1729 if { [wal_is_wal_mode] } {
1730 $db eval { SELECT * FROM sqlite_master }
1731 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
1732 }
1733}
1734
dan430e74c2010-06-07 17:47:26 +00001735proc permutation {} {
1736 set perm ""
1737 catch {set perm $::G(perm:name)}
1738 set perm
1739}
dancb79e512010-08-06 13:50:07 +00001740proc presql {} {
1741 set presql ""
1742 catch {set presql $::G(perm:presql)}
1743 set presql
1744}
dan430e74c2010-06-07 17:47:26 +00001745
danc1a60c52010-06-07 14:28:16 +00001746#-------------------------------------------------------------------------
1747#
1748proc slave_test_script {script} {
1749
1750 # Create the interpreter used to run the test script.
1751 interp create tinterp
1752
1753 # Populate some global variables that tester.tcl expects to see.
1754 foreach {var value} [list \
1755 ::argv0 $::argv0 \
1756 ::argv {} \
1757 ::SLAVE 1 \
1758 ] {
1759 interp eval tinterp [list set $var $value]
1760 }
1761
1762 # The alias used to access the global test counters.
1763 tinterp alias set_test_counter set_test_counter
1764
1765 # Set up the ::cmdlinearg array in the slave.
1766 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
1767
dan430e74c2010-06-07 17:47:26 +00001768 # Set up the ::G array in the slave.
1769 interp eval tinterp [list array set ::G [array get ::G]]
1770
danc1a60c52010-06-07 14:28:16 +00001771 # Load the various test interfaces implemented in C.
1772 load_testfixture_extensions tinterp
1773
1774 # Run the test script.
1775 interp eval tinterp $script
1776
danea5542d2010-07-06 11:26:15 +00001777 # Check if the interpreter call [run_thread_tests]
1778 if { [interp eval tinterp {info exists ::run_thread_tests_called}] } {
1779 set ::run_thread_tests_called 1
1780 }
1781
danc1a60c52010-06-07 14:28:16 +00001782 # Delete the interpreter used to run the test script.
1783 interp delete tinterp
1784}
1785
dan430e74c2010-06-07 17:47:26 +00001786proc slave_test_file {zFile} {
dan0626dfc2010-06-15 06:56:37 +00001787 set tail [file tail $zFile]
dan430e74c2010-06-07 17:47:26 +00001788
dan6a64d672011-04-04 15:38:16 +00001789 if {[info exists ::G(start:permutation)]} {
1790 if {[permutation] != $::G(start:permutation)} return
1791 unset ::G(start:permutation)
1792 }
1793 if {[info exists ::G(start:file)]} {
1794 if {$tail != $::G(start:file) && $tail!="$::G(start:file).test"} return
1795 unset ::G(start:file)
1796 }
1797
dan92d516a2010-07-05 14:54:48 +00001798 # Remember the value of the shared-cache setting. So that it is possible
1799 # to check afterwards that it was not modified by the test script.
1800 #
1801 ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] }
dan0626dfc2010-06-15 06:56:37 +00001802
dan92d516a2010-07-05 14:54:48 +00001803 # Run the test script in a slave interpreter.
1804 #
danea5542d2010-07-06 11:26:15 +00001805 unset -nocomplain ::run_thread_tests_called
dan0626dfc2010-06-15 06:56:37 +00001806 reset_prng_state
1807 set ::sqlite_open_file_count 0
1808 set time [time { slave_test_script [list source $zFile] }]
1809 set ms [expr [lindex $time 0] / 1000]
1810
dan92d516a2010-07-05 14:54:48 +00001811 # Test that all files opened by the test script were closed. Omit this
1812 # if the test script has "thread" in its name. The open file counter
1813 # is not thread-safe.
dan0626dfc2010-06-15 06:56:37 +00001814 #
danea5542d2010-07-06 11:26:15 +00001815 if {[info exists ::run_thread_tests_called]==0} {
dan92d516a2010-07-05 14:54:48 +00001816 do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0}
1817 }
dan430e74c2010-06-07 17:47:26 +00001818 set ::sqlite_open_file_count 0
1819
mistachkin1d406e02013-08-29 01:09:14 +00001820 # Test that the global "shared-cache" setting was not altered by
dan0626dfc2010-06-15 06:56:37 +00001821 # the test script.
1822 #
mistachkin1d406e02013-08-29 01:09:14 +00001823 ifcapable shared_cache {
dan0626dfc2010-06-15 06:56:37 +00001824 set res [expr {[sqlite3_enable_shared_cache] == $scs}]
1825 do_test ${tail}-sharedcachesetting [list set {} $res] 1
1826 }
dan430e74c2010-06-07 17:47:26 +00001827
dan92d516a2010-07-05 14:54:48 +00001828 # Add some info to the output.
1829 #
dan0626dfc2010-06-15 06:56:37 +00001830 puts "Time: $tail $ms ms"
dan430e74c2010-06-07 17:47:26 +00001831 show_memstats
danc1a60c52010-06-07 14:28:16 +00001832}
1833
dan59257dc2010-08-04 11:34:31 +00001834# Open a new connection on database test.db and execute the SQL script
1835# supplied as an argument. Before returning, close the new conection and
1836# restore the 4 byte fields starting at header offsets 28, 92 and 96
1837# to the values they held before the SQL was executed. This simulates
1838# a write by a pre-3.7.0 client.
1839#
1840proc sql36231 {sql} {
1841 set B [hexio_read test.db 92 8]
1842 set A [hexio_read test.db 28 4]
1843 sqlite3 db36231 test.db
1844 catch { db36231 func a_string a_string }
1845 execsql $sql db36231
1846 db36231 close
1847 hexio_write test.db 28 $A
1848 hexio_write test.db 92 $B
1849 return ""
1850}
danf5894502009-10-07 18:41:19 +00001851
danccc7ff42010-11-29 12:06:45 +00001852proc db_save {} {
1853 foreach f [glob -nocomplain sv_test.db*] { forcedelete $f }
1854 foreach f [glob -nocomplain test.db*] {
1855 set f2 "sv_$f"
mistachkinfda06be2011-08-02 00:57:34 +00001856 forcecopy $f $f2
danccc7ff42010-11-29 12:06:45 +00001857 }
1858}
1859proc db_save_and_close {} {
1860 db_save
1861 catch { db close }
1862 return ""
1863}
1864proc db_restore {} {
1865 foreach f [glob -nocomplain test.db*] { forcedelete $f }
1866 foreach f2 [glob -nocomplain sv_test.db*] {
1867 set f [string range $f2 3 end]
mistachkinfda06be2011-08-02 00:57:34 +00001868 forcecopy $f2 $f
danccc7ff42010-11-29 12:06:45 +00001869 }
1870}
1871proc db_restore_and_reopen {{dbfile test.db}} {
1872 catch { db close }
1873 db_restore
1874 sqlite3 db $dbfile
1875}
1876proc db_delete_and_reopen {{file test.db}} {
1877 catch { db close }
mistachkinfda06be2011-08-02 00:57:34 +00001878 foreach f [glob -nocomplain test.db*] { forcedelete $f }
danccc7ff42010-11-29 12:06:45 +00001879 sqlite3 db $file
1880}
1881
danielk197745901d62004-11-10 15:27:38 +00001882# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
1883# to non-zero, then set the global variable $AUTOVACUUM to 1.
1884set AUTOVACUUM $sqlite_options(default_autovacuum)
danielk1977ee8b7992009-03-26 17:13:06 +00001885
dan64b95bb2012-05-12 05:30:29 +00001886# Make sure the FTS enhanced query syntax is disabled.
1887set sqlite_fts3_enable_parentheses 0
1888
danielk1977ee8b7992009-03-26 17:13:06 +00001889source $testdir/thread_common.tcl
danddf80eb2010-10-25 12:47:43 +00001890source $testdir/malloc_common.tcl