blob: 27aa08dbe1c90aff8c960dc527fa18743d1b3e47 [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]=="~"} {
drh5343b2d2012-09-27 19:53:38 +0000619 set re [string map {# {[-0-9.]+}} [string range $expected 2 end-1]]
drh3f17aef2012-04-27 01:08:02 +0000620 set ok [expr {![regexp $re $result]}]
621 } else {
drh5343b2d2012-09-27 19:53:38 +0000622 set re [string map {# {[-0-9.]+}} [string range $expected 1 end-1]]
drh3f17aef2012-04-27 01:08:02 +0000623 set ok [regexp $re $result]
624 }
drh70bdcc72013-05-30 19:28:34 +0000625 } elseif {[regexp {^~?\*.*\*$} $expected]} {
626 # "expected" is of the form "*GLOB*" then the result if correct if
627 # glob pattern GLOB matches the result. "~/GLOB/" means
628 # the glob must not match.
629 if {[string index $expected 0]=="~"} {
630 set e [string range $expected 1 end]
631 set ok [expr {![string match $e $result]}]
632 } else {
633 set ok [string match $expected $result]
634 }
drh3f17aef2012-04-27 01:08:02 +0000635 } else {
636 set ok [expr {[string compare $result $expected]==0}]
637 }
638 if {!$ok} {
mistachkinc60941f2012-09-13 01:51:02 +0000639 # if {![info exists ::testprefix] || $::testprefix eq ""} {
640 # error "no test prefix"
641 # }
drh3f17aef2012-04-27 01:08:02 +0000642 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
643 fail_test $name
644 } else {
645 puts " Ok"
646 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000647 }
drh348784e2000-05-29 20:41:49 +0000648 } else {
mistachkin6c3c1a02011-11-12 03:17:40 +0000649 puts " Omitted"
650 omit_test $name "pattern mismatch" 0
drh348784e2000-05-29 20:41:49 +0000651 }
drh6558db82007-04-13 03:23:21 +0000652 flush stdout
drh348784e2000-05-29 20:41:49 +0000653}
dana3f51082010-09-30 18:43:14 +0000654
drh8df91852012-04-24 12:46:05 +0000655proc catchcmd {db {cmd ""}} {
656 global CLI
657 set out [open cmds.txt w]
658 puts $out $cmd
659 close $out
660 set line "exec $CLI $db < cmds.txt"
661 set rc [catch { eval $line } msg]
662 list $rc $msg
663}
664
shaneh55505172011-06-21 19:30:19 +0000665proc filepath_normalize {p} {
666 # test cases should be written to assume "unix"-like file paths
shaneh285a18f2011-06-21 19:39:59 +0000667 if {$::tcl_platform(platform)!="unix"} {
shaneh55505172011-06-21 19:30:19 +0000668 # lreverse*2 as a hack to remove any unneeded {} after the string map
669 lreverse [lreverse [string map {\\ /} [regsub -nocase -all {[a-z]:[/\\]+} $p {/}]]]
shanehc4896402011-06-21 19:38:16 +0000670 } {
671 set p
shaneh55505172011-06-21 19:30:19 +0000672 }
673}
674proc do_filepath_test {name cmd expected} {
675 uplevel [list do_test $name [
676 subst -nocommands { filepath_normalize [ $cmd ] }
677 ] [filepath_normalize $expected]]
678}
679
dan33f53792011-05-05 19:44:22 +0000680proc realnum_normalize {r} {
shaneh4f529e82011-06-20 17:41:41 +0000681 # different TCL versions display floating point values differently.
682 string map {1.#INF inf Inf inf .0e e} [regsub -all {(e[+-])0+} $r {\1}]
dan33f53792011-05-05 19:44:22 +0000683}
684proc do_realnum_test {name cmd expected} {
685 uplevel [list do_test $name [
686 subst -nocommands { realnum_normalize [ $cmd ] }
687 ] [realnum_normalize $expected]]
688}
689
dana3f51082010-09-30 18:43:14 +0000690proc fix_testname {varname} {
691 upvar $varname testname
mistachkin1d406e02013-08-29 01:09:14 +0000692 if {[info exists ::testprefix]
dana3f51082010-09-30 18:43:14 +0000693 && [string is digit [string range $testname 0 0]]
694 } {
695 set testname "${::testprefix}-$testname"
696 }
697}
mistachkin1d406e02013-08-29 01:09:14 +0000698
dan57f7f4b2010-10-01 19:04:37 +0000699proc do_execsql_test {testname sql {result {}}} {
dana3f51082010-09-30 18:43:14 +0000700 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000701 uplevel do_test [list $testname] [list "execsql {$sql}"] [list [list {*}$result]]
dan153eda02010-06-21 07:45:47 +0000702}
703proc do_catchsql_test {testname sql result} {
dana3f51082010-09-30 18:43:14 +0000704 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000705 uplevel do_test [list $testname] [list "catchsql {$sql}"] [list $result]
dan153eda02010-06-21 07:45:47 +0000706}
dan39854792010-11-15 16:12:58 +0000707proc do_eqp_test {name sql res} {
708 uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res]
709}
dan153eda02010-06-21 07:45:47 +0000710
dan51f06982010-09-18 19:00:12 +0000711#-------------------------------------------------------------------------
712# Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST
713#
714# Where switches are:
715#
716# -errorformat FMTSTRING
717# -count
danaf1dcab2010-09-20 19:17:53 +0000718# -query SQL
dana16d1062010-09-28 17:37:28 +0000719# -tclquery TCL
danaf7626f2010-09-23 18:47:36 +0000720# -repair TCL
dan51f06982010-09-18 19:00:12 +0000721#
722proc do_select_tests {prefix args} {
723
724 set testlist [lindex $args end]
725 set switches [lrange $args 0 end-1]
726
727 set errfmt ""
728 set countonly 0
dana16d1062010-09-28 17:37:28 +0000729 set tclquery ""
danaf7626f2010-09-23 18:47:36 +0000730 set repair ""
dan51f06982010-09-18 19:00:12 +0000731
732 for {set i 0} {$i < [llength $switches]} {incr i} {
733 set s [lindex $switches $i]
734 set n [string length $s]
danaf1dcab2010-09-20 19:17:53 +0000735 if {$n>=2 && [string equal -length $n $s "-query"]} {
dana16d1062010-09-28 17:37:28 +0000736 set tclquery [list execsql [lindex $switches [incr i]]]
737 } elseif {$n>=2 && [string equal -length $n $s "-tclquery"]} {
738 set tclquery [lindex $switches [incr i]]
danaf1dcab2010-09-20 19:17:53 +0000739 } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} {
dan51f06982010-09-18 19:00:12 +0000740 set errfmt [lindex $switches [incr i]]
danaf7626f2010-09-23 18:47:36 +0000741 } elseif {$n>=2 && [string equal -length $n $s "-repair"]} {
742 set repair [lindex $switches [incr i]]
dan51f06982010-09-18 19:00:12 +0000743 } elseif {$n>=2 && [string equal -length $n $s "-count"]} {
744 set countonly 1
745 } else {
746 error "unknown switch: $s"
747 }
748 }
749
750 if {$countonly && $errfmt!=""} {
751 error "Cannot use -count and -errorformat together"
752 }
753 set nTestlist [llength $testlist]
754 if {$nTestlist%3 || $nTestlist==0 } {
755 error "SELECT test list contains [llength $testlist] elements"
756 }
757
danaf7626f2010-09-23 18:47:36 +0000758 eval $repair
dan51f06982010-09-18 19:00:12 +0000759 foreach {tn sql res} $testlist {
dana16d1062010-09-28 17:37:28 +0000760 if {$tclquery != ""} {
danaf1dcab2010-09-20 19:17:53 +0000761 execsql $sql
dana16d1062010-09-28 17:37:28 +0000762 uplevel do_test ${prefix}.$tn [list $tclquery] [list [list {*}$res]]
763 } elseif {$countonly} {
dan51f06982010-09-18 19:00:12 +0000764 set nRow 0
765 db eval $sql {incr nRow}
766 uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res]
767 } elseif {$errfmt==""} {
768 uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]]
769 } else {
770 set res [list 1 [string trim [format $errfmt {*}$res]]]
771 uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res]
772 }
danaf7626f2010-09-23 18:47:36 +0000773 eval $repair
dan51f06982010-09-18 19:00:12 +0000774 }
danaf7626f2010-09-23 18:47:36 +0000775
dan51f06982010-09-18 19:00:12 +0000776}
777
danb04757a2010-09-21 16:59:16 +0000778proc delete_all_data {} {
779 db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} {
780 db eval "DELETE FROM '[string map {' ''} $t]'"
781 }
782}
drh348784e2000-05-29 20:41:49 +0000783
mistachkin1d406e02013-08-29 01:09:14 +0000784# Run an SQL script.
drhb62c3352006-11-23 09:39:16 +0000785# Return the number of microseconds per statement.
786#
drh3590f152006-11-23 21:09:10 +0000787proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000788 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000789 flush stdout
790 set speed [time {sqlite3_exec_nr db $sql}]
791 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000792 if {$tm == 0} {
793 set rate [format %20s "many"]
794 } else {
795 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
796 }
drh3590f152006-11-23 21:09:10 +0000797 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000798 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000799 global total_time
800 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000801 lappend ::speed_trial_times $name $tm
drhdd924312007-03-31 22:34:16 +0000802}
drh45c236d2008-03-22 01:08:00 +0000803proc speed_trial_tcl {name numstmt units script} {
804 puts -nonewline [format {%-21.21s } $name...]
805 flush stdout
806 set speed [time {eval $script}]
807 set tm [lindex $speed 0]
808 if {$tm == 0} {
809 set rate [format %20s "many"]
810 } else {
811 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
812 }
813 set u2 $units/s
814 puts [format {%12d uS %s %s} $tm $rate $u2]
815 global total_time
816 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000817 lappend ::speed_trial_times $name $tm
drh45c236d2008-03-22 01:08:00 +0000818}
drhdd924312007-03-31 22:34:16 +0000819proc speed_trial_init {name} {
820 global total_time
821 set total_time 0
dana975b592010-10-08 16:09:43 +0000822 set ::speed_trial_times [list]
drhbb810a92010-07-03 12:00:53 +0000823 sqlite3 versdb :memory:
824 set vers [versdb one {SELECT sqlite_source_id()}]
825 versdb close
826 puts "SQLite $vers"
drhdd924312007-03-31 22:34:16 +0000827}
828proc speed_trial_summary {name} {
829 global total_time
830 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
dana975b592010-10-08 16:09:43 +0000831
832 if { 0 } {
833 sqlite3 versdb :memory:
834 set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0]
835 versdb close
836 puts "CREATE TABLE IF NOT EXISTS time(version, script, test, us);"
837 foreach {test us} $::speed_trial_times {
838 puts "INSERT INTO time VALUES('$vers', '$name', '$test', $us);"
839 }
840 }
drhb62c3352006-11-23 09:39:16 +0000841}
842
drh348784e2000-05-29 20:41:49 +0000843# Run this routine last
844#
845proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +0000846 catch {db close}
847 catch {db2 close}
848 catch {db3 close}
849 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +0000850}
851proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +0000852 global sqlite_open_file_count
853
854 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +0000855
drh6e142f52000-06-08 13:36:40 +0000856 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000857 catch {db2 close}
858 catch {db3 close}
859
drh9bc54492007-10-23 14:49:59 +0000860 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000861 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000862 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000863 db close
drh984bfaa2008-03-19 16:08:53 +0000864 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +0000865
drh3a7fb7c2007-08-10 16:41:08 +0000866 sqlite3_soft_heap_limit 0
danc1a60c52010-06-07 14:28:16 +0000867 set nTest [incr_ntest]
868 set nErr [set_test_counter errors]
869
drhef866372013-05-22 20:49:02 +0000870 set nKnown 0
871 if {[file readable known-problems.txt]} {
872 set fd [open known-problems.txt]
873 set content [read $fd]
874 close $fd
875 foreach x $content {set known_error($x) 1}
876 foreach x [set_test_counter fail_list] {
877 if {[info exists known_error($x)]} {incr nKnown}
878 }
879 }
880 if {$nKnown>0} {
881 puts "[expr {$nErr-$nKnown}] new errors and $nKnown known errors\
882 out of $nTest tests"
883 } else {
884 puts "$nErr errors out of $nTest tests"
885 }
886 if {$nErr>$nKnown} {
887 puts -nonewline "Failures on these tests:"
888 foreach x [set_test_counter fail_list] {
889 if {![info exists known_error($x)]} {puts -nonewline " $x"}
890 }
891 puts ""
drhf3a65f72007-08-22 20:18:21 +0000892 }
drhcca17c32013-04-19 12:32:52 +0000893 foreach warning [set_test_counter warn_list] {
894 puts "Warning: $warning"
895 }
danielk1977ee8b7992009-03-26 17:13:06 +0000896 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +0000897 if {[llength $omitList]>0} {
898 puts "Omitted test cases:"
899 set prec {}
900 foreach {rec} [lsort $omitList] {
901 if {$rec==$prec} continue
902 set prec $rec
903 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
904 }
905 }
drh80788d82006-09-02 14:50:23 +0000906 if {$nErr>0 && ![working_64bit_int]} {
907 puts "******************************************************************"
908 puts "N.B.: The version of TCL that you used to build this test harness"
909 puts "is defective in that it does not support 64-bit integers. Some or"
910 puts "all of the test failures above might be a result from this defect"
911 puts "in your TCL build."
912 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000913 }
danc1a60c52010-06-07 14:28:16 +0000914 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000915 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +0000916 }
drh94e92032003-02-16 22:21:32 +0000917 if {$sqlite_open_file_count} {
918 puts "$sqlite_open_file_count files were left open"
919 incr nErr
920 }
drheafc43b2010-07-26 18:43:40 +0000921 if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 ||
922 [sqlite3_memory_used]>0} {
923 puts "Unfreed memory: [sqlite3_memory_used] bytes in\
924 [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations"
drhf3a65f72007-08-22 20:18:21 +0000925 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000926 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000927 puts "Writing unfreed memory log to \"./memleak.txt\""
928 sqlite3_memdebug_dump ./memleak.txt
929 }
drhf3a65f72007-08-22 20:18:21 +0000930 } else {
931 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000932 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000933 sqlite3_memdebug_dump ./memusage.txt
934 }
drhf3a65f72007-08-22 20:18:21 +0000935 }
drhf7141992008-06-19 00:16:08 +0000936 show_memstats
drh91fd4d42008-01-19 20:11:25 +0000937 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
938 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000939 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
940 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
941 }
danc1a60c52010-06-07 14:28:16 +0000942 if {$::cmdlinearg(malloctrace)} {
danielk197735754ac2008-03-21 17:29:37 +0000943 puts "Writing mallocs.sql..."
944 memdebug_log_sql
945 sqlite3_memdebug_log stop
946 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000947
948 if {[sqlite3_memory_used]>0} {
949 puts "Writing leaks.sql..."
950 sqlite3_memdebug_log sync
951 memdebug_log_sql leaks.sql
952 }
danielk197735754ac2008-03-21 17:29:37 +0000953 }
drhd9910fe2006-10-04 11:55:49 +0000954 foreach f [glob -nocomplain test.db-*-journal] {
mistachkinfda06be2011-08-02 00:57:34 +0000955 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +0000956 }
957 foreach f [glob -nocomplain test.db-mj*] {
mistachkinfda06be2011-08-02 00:57:34 +0000958 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +0000959 }
drhdb25e382001-03-15 18:21:22 +0000960 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000961}
962
drhf7141992008-06-19 00:16:08 +0000963# Display memory statistics for analysis and debugging purposes.
964#
965proc show_memstats {} {
966 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +0000967 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
968 set val [format {now %10d max %10d max-size %10d} \
969 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000970 puts "Memory used: $val"
drheafc43b2010-07-26 18:43:40 +0000971 set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0]
972 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
973 puts "Allocation count: $val"
drhf7141992008-06-19 00:16:08 +0000974 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +0000975 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
976 set val [format {now %10d max %10d max-size %10d} \
977 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000978 puts "Page-cache used: $val"
979 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
980 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
981 puts "Page-cache overflow: $val"
982 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
983 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
984 puts "Scratch memory used: $val"
985 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +0000986 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
987 set val [format {now %10d max %10d max-size %10d} \
988 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000989 puts "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +0000990 ifcapable yytrackmaxstackdepth {
991 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
992 set val [format { max %10d} [lindex $x 2]]
993 puts "Parser stack depth: $val"
994 }
drhf7141992008-06-19 00:16:08 +0000995}
996
drh348784e2000-05-29 20:41:49 +0000997# A procedure to execute SQL
998#
drhc4a3c772001-04-04 11:48:57 +0000999proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +00001000 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +00001001 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +00001002}
drh3aadb2e2000-05-31 17:59:25 +00001003
drhadbca9c2001-09-27 15:11:53 +00001004# Execute SQL and catch exceptions.
1005#
1006proc catchsql {sql {db db}} {
1007 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +00001008 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +00001009 lappend r $msg
1010 return $r
1011}
1012
drh04096482001-11-09 22:41:44 +00001013# Do an VDBE code dump on the SQL given
1014#
1015proc explain {sql {db db}} {
1016 puts ""
danielk1977287fb612008-01-04 19:10:28 +00001017 puts "addr opcode p1 p2 p3 p4 p5 #"
1018 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +00001019 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +00001020 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
1021 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
1022 ]
drh04096482001-11-09 22:41:44 +00001023 }
1024}
1025
dane83267d2013-11-05 14:19:22 +00001026proc explain_i {sql {db db}} {
1027 puts ""
1028 puts "addr opcode p1 p2 p3 p4 p5 #"
1029 puts "---- ------------ ------ ------ ------ ---------------- -- -"
1030
dane83267d2013-11-05 14:19:22 +00001031
dan427ebba2013-11-05 16:39:31 +00001032 set bSeenGoto 0
dane83267d2013-11-05 14:19:22 +00001033 $db eval "explain $sql" {} {
1034 set x($addr) 0
1035 set op($addr) $opcode
1036
dan427ebba2013-11-05 16:39:31 +00001037 if {$opcode == "Goto" && ($bSeenGoto==0 || ($p2 > $addr+10))} {
1038 set linebreak($p2) 1
1039 set bSeenGoto 1
dane83267d2013-11-05 14:19:22 +00001040 }
1041
1042 if {$opcode == "Next"} {
1043 for {set i $p2} {$i<$addr} {incr i} {
1044 incr x($i) 2
1045 }
1046 }
1047
1048 if {$opcode == "Goto" && $p2<$addr && $op($p2)=="Yield"} {
1049 for {set i [expr $p2+1]} {$i<$addr} {incr i} {
1050 incr x($i) 2
1051 }
1052 }
dan427ebba2013-11-05 16:39:31 +00001053
1054 if {$opcode == "Halt" && $comment == "End of coroutine"} {
1055 set linebreak([expr $addr+1]) 1
1056 }
dane83267d2013-11-05 14:19:22 +00001057 }
1058
1059 $db eval "explain $sql" {} {
dan427ebba2013-11-05 16:39:31 +00001060 if {[info exists linebreak($addr)]} {
dane83267d2013-11-05 14:19:22 +00001061 puts ""
1062 }
1063 set I [string repeat " " $x($addr)]
1064 puts [format {%-4d %s%-12.12s %-6d %-6d %-6d % -17s %s %s} \
1065 $addr $I $opcode $p1 $p2 $p3 $p4 $p5 $comment
1066 ]
dane83267d2013-11-05 14:19:22 +00001067 }
1068 puts "---- ------------ ------ ------ ------ ---------------- -- -"
1069}
1070
drhdafc0ce2008-04-17 19:14:02 +00001071# Show the VDBE program for an SQL statement but omit the Trace
1072# opcode at the beginning. This procedure can be used to prove
1073# that different SQL statements generate exactly the same VDBE code.
1074#
1075proc explain_no_trace {sql} {
1076 set tr [db eval "EXPLAIN $sql"]
1077 return [lrange $tr 7 end]
1078}
1079
drh3aadb2e2000-05-31 17:59:25 +00001080# Another procedure to execute SQL. This one includes the field
1081# names in the returned list.
1082#
1083proc execsql2 {sql} {
1084 set result {}
1085 db eval $sql data {
1086 foreach f $data(*) {
1087 lappend result $f $data($f)
1088 }
1089 }
1090 return $result
1091}
drh17a68932001-01-31 13:28:08 +00001092
mistachkin1d406e02013-08-29 01:09:14 +00001093# Use a temporary in-memory database to execute SQL statements
1094#
1095proc memdbsql {sql} {
1096 sqlite3 memdb :memory:
1097 set result [memdb eval $sql]
1098 memdb close
1099 return $result
1100}
1101
drhdde85d92003-03-01 19:45:34 +00001102# Use the non-callback API to execute multiple SQL statements
1103#
1104proc stepsql {dbptr sql} {
1105 set sql [string trim $sql]
1106 set r 0
1107 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +00001108 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +00001109 return [list 1 $vm]
1110 }
1111 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +00001112# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
1113# foreach v $VAL {lappend r $v}
1114# }
1115 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
1116 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
1117 lappend r [sqlite3_column_text $vm $i]
1118 }
drhdde85d92003-03-01 19:45:34 +00001119 }
danielk1977106bb232004-05-21 10:08:53 +00001120 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +00001121 return [list 1 $errmsg]
1122 }
1123 }
1124 return $r
1125}
1126
drh21504322002-06-25 13:16:02 +00001127# Do an integrity check of the entire database
1128#
danielk197704103022009-02-03 16:51:24 +00001129proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +00001130 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +00001131 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +00001132 }
1133}
1134
drh433dccf2013-02-09 15:37:11 +00001135# Check the extended error code
1136#
1137proc verify_ex_errcode {name expected {db db}} {
1138 do_test $name [list sqlite3_extended_errcode $db] $expected
1139}
1140
danc6055c72011-04-28 18:46:46 +00001141
1142# Return true if the SQL statement passed as the second argument uses a
1143# statement transaction.
1144#
1145proc sql_uses_stmt {db sql} {
1146 set stmt [sqlite3_prepare $db $sql -1 dummy]
1147 set uses [uses_stmt_journal $stmt]
1148 sqlite3_finalize $stmt
1149 return $uses
1150}
1151
danielk1977db4e8862008-01-16 08:24:46 +00001152proc fix_ifcapable_expr {expr} {
1153 set ret ""
1154 set state 0
1155 for {set i 0} {$i < [string length $expr]} {incr i} {
1156 set char [string range $expr $i $i]
1157 set newstate [expr {[string is alnum $char] || $char eq "_"}]
1158 if {$newstate && !$state} {
1159 append ret {$::sqlite_options(}
1160 }
1161 if {!$newstate && $state} {
1162 append ret )
1163 }
1164 append ret $char
1165 set state $newstate
1166 }
1167 if {$state} {append ret )}
1168 return $ret
1169}
1170
mistachkinc60941f2012-09-13 01:51:02 +00001171# Returns non-zero if the capabilities are present; zero otherwise.
1172#
1173proc capable {expr} {
1174 set e [fix_ifcapable_expr $expr]; return [expr ($e)]
1175}
1176
drh27d258a2004-10-30 20:23:09 +00001177# Evaluate a boolean expression of capabilities. If true, execute the
1178# code. Omit the code if false.
1179#
danielk19773e8c37e2005-01-21 03:12:14 +00001180proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +00001181 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
1182 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +00001183 if ($e2) {
1184 set c [catch {uplevel 1 $code} r]
1185 } else {
1186 set c [catch {uplevel 1 $elsecode} r]
1187 }
1188 return -code $c $r
drh21504322002-06-25 13:16:02 +00001189}
danielk197745901d62004-11-10 15:27:38 +00001190
danielk1977aca790a2005-01-13 11:07:52 +00001191# This proc execs a seperate process that crashes midway through executing
1192# the SQL script $sql on database test.db.
1193#
1194# The crash occurs during a sync() of file $crashfile. When the crash
1195# occurs a random subset of all unsynced writes made by the process are
1196# written into the files on disk. Argument $crashdelay indicates the
1197# number of file syncs to wait before crashing.
1198#
1199# The return value is a list of two elements. The first element is a
1200# boolean, indicating whether or not the process actually crashed or
1201# reported some other error. The second element in the returned list is the
1202# error message. This is "child process exited abnormally" if the crash
mistachkin48864df2013-03-21 21:20:32 +00001203# occurred.
danielk1977aca790a2005-01-13 11:07:52 +00001204#
danielk1977f8940ae2007-08-23 11:07:10 +00001205# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +00001206#
1207proc crashsql {args} {
danielk197759a33f92007-03-17 10:26:59 +00001208
1209 set blocksize ""
1210 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +00001211 set prngseed 0
drhf8587402008-01-08 16:03:49 +00001212 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +00001213 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +00001214 set dc ""
danielk197759a33f92007-03-17 10:26:59 +00001215 set sql [lindex $args end]
mistachkin1d406e02013-08-29 01:09:14 +00001216
danielk197759a33f92007-03-17 10:26:59 +00001217 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
1218 set z [lindex $args $ii]
1219 set n [string length $z]
1220 set z2 [lindex $args [expr $ii+1]]
1221
1222 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +00001223 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +00001224 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +00001225 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +00001226 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +00001227 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +00001228 else { error "Unrecognized option: $z" }
1229 }
1230
1231 if {$crashfile eq ""} {
1232 error "Compulsory option -file missing"
1233 }
1234
mistachkin1d406e02013-08-29 01:09:14 +00001235 # $crashfile gets compared to the native filename in
shanehf2c08822010-07-08 16:30:44 +00001236 # cfSync(), which can be different then what TCL uses by
1237 # default, so here we force it to the "nativename" format.
mistachkinf8a78462012-03-08 20:00:36 +00001238 set cfile [string map {\\ \\\\} [file nativename [file join [get_pwd] $crashfile]]]
danielk1977aca790a2005-01-13 11:07:52 +00001239
1240 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +00001241 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +00001242 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +00001243 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +00001244 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +00001245
1246 # This block sets the cache size of the main database to 10
1247 # pages. This is done in case the build is configured to omit
1248 # "PRAGMA cache_size".
1249 puts $f {db eval {SELECT * FROM sqlite_master;}}
1250 puts $f {set bt [btree_from_db db]}
1251 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +00001252 if {$prngseed} {
1253 set seed [expr {$prngseed%10007+1}]
1254 # puts seed=$seed
1255 puts $f "db eval {SELECT randomblob($seed)}"
1256 }
danielk1977933bbd62007-03-17 07:22:42 +00001257
drhf8587402008-01-08 16:03:49 +00001258 if {[string length $tclbody]>0} {
1259 puts $f $tclbody
1260 }
1261 if {[string length $sql]>0} {
1262 puts $f "db eval {"
1263 puts $f "$sql"
1264 puts $f "}"
1265 }
danielk1977aca790a2005-01-13 11:07:52 +00001266 close $f
danielk1977aca790a2005-01-13 11:07:52 +00001267 set r [catch {
drh9c06c952005-11-26 00:25:00 +00001268 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +00001269 } msg]
mistachkin1d406e02013-08-29 01:09:14 +00001270
shanehf2c08822010-07-08 16:30:44 +00001271 # Windows/ActiveState TCL returns a slightly different
1272 # error message. We map that to the expected message
1273 # so that we don't have to change all of the test
1274 # cases.
1275 if {$::tcl_platform(platform)=="windows"} {
1276 if {$msg=="child killed: unknown signal"} {
1277 set msg "child process exited abnormally"
1278 }
1279 }
mistachkin1d406e02013-08-29 01:09:14 +00001280
danielk1977aca790a2005-01-13 11:07:52 +00001281 lappend r $msg
1282}
1283
danb88e24f2013-02-23 18:58:11 +00001284proc run_ioerr_prep {} {
1285 set ::sqlite_io_error_pending 0
1286 catch {db close}
1287 catch {db2 close}
1288 catch {forcedelete test.db}
1289 catch {forcedelete test.db-journal}
1290 catch {forcedelete test2.db}
1291 catch {forcedelete test2.db-journal}
1292 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
1293 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
1294 if {[info exists ::ioerropts(-tclprep)]} {
1295 eval $::ioerropts(-tclprep)
1296 }
1297 if {[info exists ::ioerropts(-sqlprep)]} {
1298 execsql $::ioerropts(-sqlprep)
1299 }
1300 expr 0
1301}
1302
danielk197732554c12005-01-22 03:39:39 +00001303# Usage: do_ioerr_test <test number> <options...>
1304#
1305# This proc is used to implement test cases that check that IO errors
mistachkin1d406e02013-08-29 01:09:14 +00001306# are correctly handled. The first argument, <test number>, is an integer
danielk197732554c12005-01-22 03:39:39 +00001307# used to name the tests executed by this proc. Options are as follows:
1308#
1309# -tclprep TCL script to run to prepare test.
1310# -sqlprep SQL script to run to prepare test.
1311# -tclbody TCL script to run with IO error simulation.
1312# -sqlbody TCL script to run with IO error simulation.
1313# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +00001314# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +00001315# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +00001316# -start Value of 'N' to begin with (default 1)
1317#
1318# -cksum Boolean. If true, test that the database does
1319# not change during the execution of the test case.
1320#
1321proc do_ioerr_test {testname args} {
1322
danielk197732554c12005-01-22 03:39:39 +00001323 set ::ioerropts(-start) 1
1324 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +00001325 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +00001326 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +00001327 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +00001328 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +00001329 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +00001330 array set ::ioerropts $args
1331
danielk197747cd39c2008-05-12 12:41:15 +00001332 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
1333 # a couple of obscure IO errors that do not return them.
1334 set ::ioerropts(-erc) 0
mistachkin1d406e02013-08-29 01:09:14 +00001335
danb88e24f2013-02-23 18:58:11 +00001336 # Create a single TCL script from the TCL and SQL specified
1337 # as the body of the test.
1338 set ::ioerrorbody {}
1339 if {[info exists ::ioerropts(-tclbody)]} {
1340 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
1341 }
1342 if {[info exists ::ioerropts(-sqlbody)]} {
1343 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
1344 }
1345
1346 save_prng_state
1347 if {$::ioerropts(-cksum)} {
1348 run_ioerr_prep
1349 eval $::ioerrorbody
1350 set ::goodcksum [cksum]
1351 }
danielk197747cd39c2008-05-12 12:41:15 +00001352
danielk197732554c12005-01-22 03:39:39 +00001353 set ::go 1
drh93aed5a2008-01-16 17:46:38 +00001354 #reset_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +00001355 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +00001356 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +00001357 incr ::ioerropts(-count) -1
1358 if {$::ioerropts(-count)<0} break
mistachkin1d406e02013-08-29 01:09:14 +00001359
danielk197732554c12005-01-22 03:39:39 +00001360 # Skip this IO error if it was specified with the "-exclude" option.
1361 if {[info exists ::ioerropts(-exclude)]} {
1362 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
1363 }
danielk1977861f7452008-06-05 11:39:11 +00001364 if {$::ioerropts(-restoreprng)} {
1365 restore_prng_state
1366 }
danielk197732554c12005-01-22 03:39:39 +00001367
mistachkin1d406e02013-08-29 01:09:14 +00001368 # Delete the files test.db and test2.db, then execute the TCL and
danielk197732554c12005-01-22 03:39:39 +00001369 # SQL (in that order) to prepare for the test case.
1370 do_test $testname.$n.1 {
danb88e24f2013-02-23 18:58:11 +00001371 run_ioerr_prep
danielk197732554c12005-01-22 03:39:39 +00001372 } {0}
1373
1374 # Read the 'checksum' of the database.
1375 if {$::ioerropts(-cksum)} {
danb88e24f2013-02-23 18:58:11 +00001376 set ::checksum [cksum]
danielk197732554c12005-01-22 03:39:39 +00001377 }
drh93aed5a2008-01-16 17:46:38 +00001378
danielk197732554c12005-01-22 03:39:39 +00001379 # Set the Nth IO error to fail.
1380 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +00001381 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +00001382 set ::sqlite_io_error_pending $n
1383 }] $n
danielk197732554c12005-01-22 03:39:39 +00001384
danb88e24f2013-02-23 18:58:11 +00001385 # Execute the TCL script created for the body of this test. If
mistachkin1d406e02013-08-29 01:09:14 +00001386 # at least N IO operations performed by SQLite as a result of
danb88e24f2013-02-23 18:58:11 +00001387 # the script, the Nth will fail.
danielk197732554c12005-01-22 03:39:39 +00001388 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +00001389 set ::sqlite_io_error_hit 0
1390 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +00001391 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +00001392 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +00001393 set rc [sqlite3_errcode $::DB]
1394 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +00001395 # If we are in extended result code mode, make sure all of the
1396 # IOERRs we get back really do have their extended code values.
1397 # If an extended result code is returned, the sqlite3_errcode
1398 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
1399 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +00001400 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
1401 return $rc
1402 }
1403 } else {
drh5f7b5bf2007-04-19 12:30:54 +00001404 # If we are not in extended result code mode, make sure no
1405 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +00001406 if {[regexp {\+\d} $rc]} {
1407 return $rc
1408 }
1409 }
drh93aed5a2008-01-16 17:46:38 +00001410 # The test repeats as long as $::go is non-zero. $::go starts out
1411 # as 1. When a test runs to completion without hitting an I/O
1412 # error, that means there is no point in continuing with this test
1413 # case so set $::go to zero.
1414 #
1415 if {$::sqlite_io_error_pending>0} {
1416 set ::go 0
1417 set q 0
1418 set ::sqlite_io_error_pending 0
1419 } else {
1420 set q 1
1421 }
1422
drhc9ac5ca2005-11-04 22:03:30 +00001423 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +00001424 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
1425 set r 1
1426 }
danielk1977f2fa8312006-01-24 13:09:33 +00001427 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +00001428
1429 # One of two things must have happened. either
1430 # 1. We never hit the IO error and the SQL returned OK
1431 # 2. An IO error was hit and the SQL failed
1432 #
dand41a29a2010-05-06 15:56:28 +00001433 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +00001434 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +00001435 } {1}
1436
danielk197780daec62008-05-12 10:57:02 +00001437 set ::sqlite_io_error_hit 0
1438 set ::sqlite_io_error_pending 0
1439
mistachkin1d406e02013-08-29 01:09:14 +00001440 # Check that no page references were leaked. There should be
1441 # a single reference if there is still an active transaction,
danielk197752b472a2008-05-05 16:23:55 +00001442 # or zero otherwise.
1443 #
danielk197781620542008-06-07 05:19:37 +00001444 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
mistachkin1d406e02013-08-29 01:09:14 +00001445 # locks are established on database files (i.e. if the error
danielk197781620542008-06-07 05:19:37 +00001446 # occurs while attempting to detect a hot-journal file), then
1447 # there may 0 page references and an active transaction according
1448 # to [sqlite3_get_autocommit].
1449 #
danielk19774abd5442008-05-05 15:26:50 +00001450 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +00001451 do_test $testname.$n.4 {
1452 set bt [btree_from_db db]
1453 db_enter db
1454 array set stats [btree_pager_stats $bt]
1455 db_leave db
danielk197781620542008-06-07 05:19:37 +00001456 set nRef $stats(ref)
1457 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
1458 } {1}
danielk19774abd5442008-05-05 15:26:50 +00001459 }
1460
mistachkin1d406e02013-08-29 01:09:14 +00001461 # If there is an open database handle and no open transaction,
danielk197752b472a2008-05-05 16:23:55 +00001462 # and the pager is not running in exclusive-locking mode,
1463 # check that the pager is in "unlocked" state. Theoretically,
1464 # if a call to xUnlock() failed due to an IO error the underlying
1465 # file may still be locked.
1466 #
1467 ifcapable pragma {
1468 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +00001469 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +00001470 && [db one {pragma locking_mode}] eq "normal"
1471 && [sqlite3_get_autocommit db]
1472 } {
1473 do_test $testname.$n.5 {
1474 set bt [btree_from_db db]
1475 db_enter db
1476 array set stats [btree_pager_stats $bt]
1477 db_leave db
1478 set stats(state)
1479 } 0
1480 }
1481 }
1482
mistachkin48864df2013-03-21 21:20:32 +00001483 # If an IO error occurred, then the checksum of the database should
danielk197732554c12005-01-22 03:39:39 +00001484 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +00001485 #
drh1aa5af12008-03-07 19:51:14 +00001486 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +00001487 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +00001488 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +00001489 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +00001490 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danb88e24f2013-02-23 18:58:11 +00001491 set nowcksum [cksum]
1492 set res [expr {$nowcksum==$::checksum || $nowcksum==$::goodcksum}]
1493 if {$res==0} {
1494 puts "now=$nowcksum"
1495 puts "the=$::checksum"
1496 puts "fwd=$::goodcksum"
1497 }
1498 set res
1499 } 1
danielk197732554c12005-01-22 03:39:39 +00001500 }
1501
drh1aa5af12008-03-07 19:51:14 +00001502 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +00001503 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +00001504 if {[info exists ::ioerropts(-cleanup)]} {
1505 catch $::ioerropts(-cleanup)
1506 }
danielk197732554c12005-01-22 03:39:39 +00001507 }
1508 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +00001509 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +00001510 unset ::ioerropts
1511}
1512
drh93aed5a2008-01-16 17:46:38 +00001513# Return a checksum based on the contents of the main database associated
1514# with connection $db
danielk197732554c12005-01-22 03:39:39 +00001515#
1516proc cksum {{db db}} {
1517 set txt [$db eval {
1518 SELECT name, type, sql FROM sqlite_master order by name
1519 }]\n
1520 foreach tbl [$db eval {
1521 SELECT name FROM sqlite_master WHERE type='table' order by name
1522 }] {
1523 append txt [$db eval "SELECT * FROM $tbl"]\n
1524 }
1525 foreach prag {default_synchronous default_cache_size} {
1526 append txt $prag-[$db eval "PRAGMA $prag"]\n
1527 }
1528 set cksum [string length $txt]-[md5 $txt]
1529 # puts $cksum-[file size test.db]
1530 return $cksum
1531}
1532
drh93aed5a2008-01-16 17:46:38 +00001533# Generate a checksum based on the contents of the main and temp tables
1534# database $db. If the checksum of two databases is the same, and the
1535# integrity-check passes for both, the two databases are identical.
1536#
drh1db639c2008-01-17 02:36:28 +00001537proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +00001538 set ret [list]
1539 ifcapable tempdb {
1540 set sql {
1541 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1542 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
1543 SELECT 'sqlite_master' UNION
1544 SELECT 'sqlite_temp_master' ORDER BY 1
1545 }
1546 } else {
1547 set sql {
1548 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1549 SELECT 'sqlite_master' ORDER BY 1
1550 }
1551 }
1552 set tbllist [$db eval $sql]
1553 set txt {}
1554 foreach tbl $tbllist {
1555 append txt [$db eval "SELECT * FROM $tbl"]
1556 }
1557 foreach prag {default_cache_size} {
1558 append txt $prag-[$db eval "PRAGMA $prag"]\n
1559 }
1560 # puts txt=$txt
1561 return [md5 $txt]
1562}
1563
drhdc2c4912009-02-04 22:46:47 +00001564# Generate a checksum based on the contents of a single database with
mistachkin1d406e02013-08-29 01:09:14 +00001565# a database connection. The name of the database is $dbname.
drhdc2c4912009-02-04 22:46:47 +00001566# Examples of $dbname are "temp" or "main".
1567#
1568proc dbcksum {db dbname} {
1569 if {$dbname=="temp"} {
1570 set master sqlite_temp_master
1571 } else {
1572 set master $dbname.sqlite_master
1573 }
1574 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
1575 set txt [$db eval "SELECT * FROM $master"]\n
1576 foreach tab $alltab {
1577 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
1578 }
1579 return [md5 $txt]
1580}
1581
danielk197735754ac2008-03-21 17:29:37 +00001582proc memdebug_log_sql {{filename mallocs.sql}} {
1583
danielk19776f332c12008-03-21 14:22:44 +00001584 set data [sqlite3_memdebug_log dump]
1585 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +00001586 if {$nFrame < 0} { return "" }
1587
danielk197735754ac2008-03-21 17:29:37 +00001588 set database temp
1589
danielk19776ab3a2e2009-02-19 14:39:25 +00001590 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +00001591
1592 set sql ""
1593 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +00001594 set nCall [lindex $e 0]
1595 set nByte [lindex $e 1]
1596 set lStack [lrange $e 2 end]
1597 append sql "INSERT INTO ${database}.malloc VALUES"
1598 append sql "('test', $nCall, $nByte, '$lStack');\n"
1599 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +00001600 set frames($f) 1
1601 }
1602 }
1603
1604 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +00001605 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +00001606
1607 foreach f [array names frames] {
1608 set addr [format %x $f]
1609 set cmd "addr2line -e [info nameofexec] $addr"
1610 set line [eval exec $cmd]
1611 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00001612
1613 set file [lindex [split $line :] 0]
1614 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00001615 }
1616
danielk197735754ac2008-03-21 17:29:37 +00001617 foreach f [array names files] {
1618 set contents ""
1619 catch {
1620 set fd [open $f]
1621 set contents [read $fd]
1622 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001623 }
danielk197735754ac2008-03-21 17:29:37 +00001624 set contents [string map {' ''} $contents]
1625 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00001626 }
danielk19776f332c12008-03-21 14:22:44 +00001627
danielk197735754ac2008-03-21 17:29:37 +00001628 set fd [open $filename w]
1629 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
1630 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001631}
drh93aed5a2008-01-16 17:46:38 +00001632
danf5894502009-10-07 18:41:19 +00001633# Drop all tables in database [db]
1634proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00001635 ifcapable trigger&&foreignkey {
1636 set pk [$db one "PRAGMA foreign_keys"]
1637 $db eval "PRAGMA foreign_keys = OFF"
1638 }
drh9a6ffc82010-02-15 18:03:20 +00001639 foreach {idx name file} [db eval {PRAGMA database_list}] {
1640 if {$idx==1} {
1641 set master sqlite_temp_master
1642 } else {
1643 set master $name.sqlite_master
1644 }
1645 foreach {t type} [$db eval "
1646 SELECT name, type FROM $master
dana16d1062010-09-28 17:37:28 +00001647 WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X'
drh9a6ffc82010-02-15 18:03:20 +00001648 "] {
dana16d1062010-09-28 17:37:28 +00001649 $db eval "DROP $type \"$t\""
drh9a6ffc82010-02-15 18:03:20 +00001650 }
danf5894502009-10-07 18:41:19 +00001651 }
shaneh4e7b32f2009-12-17 22:12:51 +00001652 ifcapable trigger&&foreignkey {
1653 $db eval "PRAGMA foreign_keys = $pk"
1654 }
danf5894502009-10-07 18:41:19 +00001655}
1656
dan71cb5182010-04-26 12:39:03 +00001657#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00001658# If a test script is executed with global variable $::G(perm:name) set to
mistachkin1d406e02013-08-29 01:09:14 +00001659# "wal", then the tests are run in WAL mode. Otherwise, they should be run
1660# in rollback mode. The following Tcl procs are used to make this less
dan430e74c2010-06-07 17:47:26 +00001661# intrusive:
dan71cb5182010-04-26 12:39:03 +00001662#
1663# wal_set_journal_mode ?DB?
1664#
1665# If running a WAL test, execute "PRAGMA journal_mode = wal" using
1666# connection handle DB. Otherwise, this command is a no-op.
1667#
1668# wal_check_journal_mode TESTNAME ?DB?
1669#
1670# If running a WAL test, execute a tests case that fails if the main
1671# database for connection handle DB is not currently a WAL database.
1672# Otherwise (if not running a WAL permutation) this is a no-op.
1673#
1674# wal_is_wal_mode
mistachkin1d406e02013-08-29 01:09:14 +00001675#
dan71cb5182010-04-26 12:39:03 +00001676# Returns true if this test should be run in WAL mode. False otherwise.
mistachkin1d406e02013-08-29 01:09:14 +00001677#
dan71cb5182010-04-26 12:39:03 +00001678proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00001679 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00001680}
1681proc wal_set_journal_mode {{db db}} {
1682 if { [wal_is_wal_mode] } {
1683 $db eval "PRAGMA journal_mode = WAL"
1684 }
1685}
1686proc wal_check_journal_mode {testname {db db}} {
1687 if { [wal_is_wal_mode] } {
1688 $db eval { SELECT * FROM sqlite_master }
1689 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
1690 }
1691}
1692
dan430e74c2010-06-07 17:47:26 +00001693proc permutation {} {
1694 set perm ""
1695 catch {set perm $::G(perm:name)}
1696 set perm
1697}
dancb79e512010-08-06 13:50:07 +00001698proc presql {} {
1699 set presql ""
1700 catch {set presql $::G(perm:presql)}
1701 set presql
1702}
dan430e74c2010-06-07 17:47:26 +00001703
danc1a60c52010-06-07 14:28:16 +00001704#-------------------------------------------------------------------------
1705#
1706proc slave_test_script {script} {
1707
1708 # Create the interpreter used to run the test script.
1709 interp create tinterp
1710
1711 # Populate some global variables that tester.tcl expects to see.
1712 foreach {var value} [list \
1713 ::argv0 $::argv0 \
1714 ::argv {} \
1715 ::SLAVE 1 \
1716 ] {
1717 interp eval tinterp [list set $var $value]
1718 }
1719
1720 # The alias used to access the global test counters.
1721 tinterp alias set_test_counter set_test_counter
1722
1723 # Set up the ::cmdlinearg array in the slave.
1724 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
1725
dan430e74c2010-06-07 17:47:26 +00001726 # Set up the ::G array in the slave.
1727 interp eval tinterp [list array set ::G [array get ::G]]
1728
danc1a60c52010-06-07 14:28:16 +00001729 # Load the various test interfaces implemented in C.
1730 load_testfixture_extensions tinterp
1731
1732 # Run the test script.
1733 interp eval tinterp $script
1734
danea5542d2010-07-06 11:26:15 +00001735 # Check if the interpreter call [run_thread_tests]
1736 if { [interp eval tinterp {info exists ::run_thread_tests_called}] } {
1737 set ::run_thread_tests_called 1
1738 }
1739
danc1a60c52010-06-07 14:28:16 +00001740 # Delete the interpreter used to run the test script.
1741 interp delete tinterp
1742}
1743
dan430e74c2010-06-07 17:47:26 +00001744proc slave_test_file {zFile} {
dan0626dfc2010-06-15 06:56:37 +00001745 set tail [file tail $zFile]
dan430e74c2010-06-07 17:47:26 +00001746
dan6a64d672011-04-04 15:38:16 +00001747 if {[info exists ::G(start:permutation)]} {
1748 if {[permutation] != $::G(start:permutation)} return
1749 unset ::G(start:permutation)
1750 }
1751 if {[info exists ::G(start:file)]} {
1752 if {$tail != $::G(start:file) && $tail!="$::G(start:file).test"} return
1753 unset ::G(start:file)
1754 }
1755
dan92d516a2010-07-05 14:54:48 +00001756 # Remember the value of the shared-cache setting. So that it is possible
1757 # to check afterwards that it was not modified by the test script.
1758 #
1759 ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] }
dan0626dfc2010-06-15 06:56:37 +00001760
dan92d516a2010-07-05 14:54:48 +00001761 # Run the test script in a slave interpreter.
1762 #
danea5542d2010-07-06 11:26:15 +00001763 unset -nocomplain ::run_thread_tests_called
dan0626dfc2010-06-15 06:56:37 +00001764 reset_prng_state
1765 set ::sqlite_open_file_count 0
1766 set time [time { slave_test_script [list source $zFile] }]
1767 set ms [expr [lindex $time 0] / 1000]
1768
dan92d516a2010-07-05 14:54:48 +00001769 # Test that all files opened by the test script were closed. Omit this
1770 # if the test script has "thread" in its name. The open file counter
1771 # is not thread-safe.
dan0626dfc2010-06-15 06:56:37 +00001772 #
danea5542d2010-07-06 11:26:15 +00001773 if {[info exists ::run_thread_tests_called]==0} {
dan92d516a2010-07-05 14:54:48 +00001774 do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0}
1775 }
dan430e74c2010-06-07 17:47:26 +00001776 set ::sqlite_open_file_count 0
1777
mistachkin1d406e02013-08-29 01:09:14 +00001778 # Test that the global "shared-cache" setting was not altered by
dan0626dfc2010-06-15 06:56:37 +00001779 # the test script.
1780 #
mistachkin1d406e02013-08-29 01:09:14 +00001781 ifcapable shared_cache {
dan0626dfc2010-06-15 06:56:37 +00001782 set res [expr {[sqlite3_enable_shared_cache] == $scs}]
1783 do_test ${tail}-sharedcachesetting [list set {} $res] 1
1784 }
dan430e74c2010-06-07 17:47:26 +00001785
dan92d516a2010-07-05 14:54:48 +00001786 # Add some info to the output.
1787 #
dan0626dfc2010-06-15 06:56:37 +00001788 puts "Time: $tail $ms ms"
dan430e74c2010-06-07 17:47:26 +00001789 show_memstats
danc1a60c52010-06-07 14:28:16 +00001790}
1791
dan59257dc2010-08-04 11:34:31 +00001792# Open a new connection on database test.db and execute the SQL script
1793# supplied as an argument. Before returning, close the new conection and
1794# restore the 4 byte fields starting at header offsets 28, 92 and 96
1795# to the values they held before the SQL was executed. This simulates
1796# a write by a pre-3.7.0 client.
1797#
1798proc sql36231 {sql} {
1799 set B [hexio_read test.db 92 8]
1800 set A [hexio_read test.db 28 4]
1801 sqlite3 db36231 test.db
1802 catch { db36231 func a_string a_string }
1803 execsql $sql db36231
1804 db36231 close
1805 hexio_write test.db 28 $A
1806 hexio_write test.db 92 $B
1807 return ""
1808}
danf5894502009-10-07 18:41:19 +00001809
danccc7ff42010-11-29 12:06:45 +00001810proc db_save {} {
1811 foreach f [glob -nocomplain sv_test.db*] { forcedelete $f }
1812 foreach f [glob -nocomplain test.db*] {
1813 set f2 "sv_$f"
mistachkinfda06be2011-08-02 00:57:34 +00001814 forcecopy $f $f2
danccc7ff42010-11-29 12:06:45 +00001815 }
1816}
1817proc db_save_and_close {} {
1818 db_save
1819 catch { db close }
1820 return ""
1821}
1822proc db_restore {} {
1823 foreach f [glob -nocomplain test.db*] { forcedelete $f }
1824 foreach f2 [glob -nocomplain sv_test.db*] {
1825 set f [string range $f2 3 end]
mistachkinfda06be2011-08-02 00:57:34 +00001826 forcecopy $f2 $f
danccc7ff42010-11-29 12:06:45 +00001827 }
1828}
1829proc db_restore_and_reopen {{dbfile test.db}} {
1830 catch { db close }
1831 db_restore
1832 sqlite3 db $dbfile
1833}
1834proc db_delete_and_reopen {{file test.db}} {
1835 catch { db close }
mistachkinfda06be2011-08-02 00:57:34 +00001836 foreach f [glob -nocomplain test.db*] { forcedelete $f }
danccc7ff42010-11-29 12:06:45 +00001837 sqlite3 db $file
1838}
1839
danielk197745901d62004-11-10 15:27:38 +00001840# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
1841# to non-zero, then set the global variable $AUTOVACUUM to 1.
1842set AUTOVACUUM $sqlite_options(default_autovacuum)
danielk1977ee8b7992009-03-26 17:13:06 +00001843
dan64b95bb2012-05-12 05:30:29 +00001844# Make sure the FTS enhanced query syntax is disabled.
1845set sqlite_fts3_enable_parentheses 0
1846
danielk1977ee8b7992009-03-26 17:13:06 +00001847source $testdir/thread_common.tcl
danddf80eb2010-10-25 12:47:43 +00001848source $testdir/malloc_common.tcl