blob: 761a36e355a91999c154ed8249418bd93e20b538 [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#-------------------------------------------------------------------------
17# The commands provided by the code in this file to help with creating
18# 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#
45# stepsql DB SQL
46# execsql2 SQL
47# explain_no_trace SQL
48# explain SQL ?DB?
49# catchsql SQL ?DB?
50# execsql SQL ?DB?
51#
52# Commands to run test cases:
53#
54# do_ioerr_test TESTNAME ARGS...
55# crashsql ARGS...
56# integrity_check TESTNAME ?DB?
drh433dccf2013-02-09 15:37:11 +000057# verify_ex_errcode TESTNAME EXPECTED ?DB?
danc1a60c52010-06-07 14:28:16 +000058# do_test TESTNAME SCRIPT EXPECTED
dan153eda02010-06-21 07:45:47 +000059# do_execsql_test TESTNAME SQL EXPECTED
60# do_catchsql_test TESTNAME SQL EXPECTED
danc1a60c52010-06-07 14:28:16 +000061#
dan430e74c2010-06-07 17:47:26 +000062# Commands providing a lower level interface to the global test counters:
danc1a60c52010-06-07 14:28:16 +000063#
64# set_test_counter COUNTER ?VALUE?
mistachkin6c3c1a02011-11-12 03:17:40 +000065# omit_test TESTNAME REASON ?APPEND?
danc1a60c52010-06-07 14:28:16 +000066# fail_test TESTNAME
67# incr_ntest
68#
69# Command run at the end of each test file:
70#
71# finish_test
72#
dan430e74c2010-06-07 17:47:26 +000073# Commands to help create test files that run with the "WAL" and other
74# permutations (see file permutations.test):
danc1a60c52010-06-07 14:28:16 +000075#
76# wal_is_wal_mode
77# wal_set_journal_mode ?DB?
78# wal_check_journal_mode TESTNAME?DB?
dan430e74c2010-06-07 17:47:26 +000079# permutation
dancb79e512010-08-06 13:50:07 +000080# presql
danc1a60c52010-06-07 14:28:16 +000081#
drh348784e2000-05-29 20:41:49 +000082
danc1a60c52010-06-07 14:28:16 +000083# Set the precision of FP arithmatic used by the interpreter. And
84# configure SQLite to take database file locks on the page that begins
85# 64KB into the database file instead of the one 1GB in. This means
86# the code that handles that special case can be tested without creating
87# very large database files.
88#
drh92febd92004-08-20 18:34:20 +000089set tcl_precision 15
drhc7a3bb92009-02-05 16:31:45 +000090sqlite3_test_control_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +000091
danc1a60c52010-06-07 14:28:16 +000092
93# If the pager codec is available, create a wrapper for the [sqlite3]
94# command that appends "-key {xyzzy}" to the command line. i.e. this:
drh3a7fb7c2007-08-10 16:41:08 +000095#
danc1a60c52010-06-07 14:28:16 +000096# sqlite3 db test.db
drh4a50aac2007-08-23 02:47:53 +000097#
danc1a60c52010-06-07 14:28:16 +000098# becomes
drh4a50aac2007-08-23 02:47:53 +000099#
danc1a60c52010-06-07 14:28:16 +0000100# sqlite3 db test.db -key {xyzzy}
drh9eb9e262004-02-11 02:18:05 +0000101#
dan430e74c2010-06-07 17:47:26 +0000102if {[info command sqlite_orig]==""} {
drhef4ac8f2004-06-19 00:16:31 +0000103 rename sqlite3 sqlite_orig
104 proc sqlite3 {args} {
dan68928b62010-06-22 13:46:43 +0000105 if {[llength $args]>=2 && [string index [lindex $args 0] 0]!="-"} {
dan430e74c2010-06-07 17:47:26 +0000106 # This command is opening a new database connection.
107 #
108 if {[info exists ::G(perm:sqlite3_args)]} {
109 set args [concat $args $::G(perm:sqlite3_args)]
110 }
dan68928b62010-06-22 13:46:43 +0000111 if {[sqlite_orig -has-codec] && ![info exists ::do_not_use_codec]} {
dan430e74c2010-06-07 17:47:26 +0000112 lappend args -key {xyzzy}
113 }
114
dan3ccd20a2010-06-09 19:01:02 +0000115 set res [uplevel 1 sqlite_orig $args]
dan430e74c2010-06-07 17:47:26 +0000116 if {[info exists ::G(perm:presql)]} {
117 [lindex $args 0] eval $::G(perm:presql)
118 }
dan1ce1b4a2010-12-07 14:32:28 +0000119 if {[info exists ::G(perm:dbconfig)]} {
120 set ::dbhandle [lindex $args 0]
121 uplevel #0 $::G(perm:dbconfig)
122 }
dan3ccd20a2010-06-09 19:01:02 +0000123 set res
dan430e74c2010-06-07 17:47:26 +0000124 } else {
125 # This command is not opening a new database connection. Pass the
mistachkin48864df2013-03-21 21:20:32 +0000126 # arguments through to the C implementation as the are.
dan430e74c2010-06-07 17:47:26 +0000127 #
128 uplevel 1 sqlite_orig $args
drh9eb9e262004-02-11 02:18:05 +0000129 }
drh9eb9e262004-02-11 02:18:05 +0000130 }
131}
132
mistachkinfda06be2011-08-02 00:57:34 +0000133proc getFileRetries {} {
134 if {![info exists ::G(file-retries)]} {
135 #
136 # NOTE: Return the default number of retries for [file] operations. A
137 # value of zero or less here means "disabled".
138 #
mistachkinc60941f2012-09-13 01:51:02 +0000139 return [expr {$::tcl_platform(platform) eq "windows" ? 50 : 0}]
mistachkinfda06be2011-08-02 00:57:34 +0000140 }
141 return $::G(file-retries)
142}
143
144proc getFileRetryDelay {} {
145 if {![info exists ::G(file-retry-delay)]} {
146 #
147 # NOTE: Return the default number of milliseconds to wait when retrying
148 # failed [file] operations. A value of zero or less means "do not
149 # wait".
150 #
151 return 100; # TODO: Good default?
152 }
153 return $::G(file-retry-delay)
154}
155
mistachkinf8a78462012-03-08 20:00:36 +0000156# Return the string representing the name of the current directory. On
157# Windows, the result is "normalized" to whatever our parent command shell
158# is using to prevent case-mismatch issues.
159#
160proc get_pwd {} {
161 if {$::tcl_platform(platform) eq "windows"} {
mistachkin533b8f62012-03-08 20:28:31 +0000162 #
163 # NOTE: Cannot use [file normalize] here because it would alter the
164 # case of the result to what Tcl considers canonical, which would
165 # defeat the purpose of this procedure.
166 #
167 return [string map [list \\ /] \
168 [string trim [exec -- $::env(ComSpec) /c echo %CD%]]]
mistachkinf8a78462012-03-08 20:00:36 +0000169 } else {
170 return [pwd]
171 }
172}
173
mistachkinfda06be2011-08-02 00:57:34 +0000174# Copy file $from into $to. This is used because some versions of
175# TCL for windows (notably the 8.4.1 binary package shipped with the
176# current mingw release) have a broken "file copy" command.
177#
178proc copy_file {from to} {
179 do_copy_file false $from $to
180}
181
182proc forcecopy {from to} {
183 do_copy_file true $from $to
184}
185
186proc do_copy_file {force from to} {
187 set nRetry [getFileRetries] ;# Maximum number of retries.
188 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
189
190 # On windows, sometimes even a [file copy -force] can fail. The cause is
191 # usually "tag-alongs" - programs like anti-virus software, automatic backup
192 # tools and various explorer extensions that keep a file open a little longer
193 # than we expect, causing the delete to fail.
194 #
195 # The solution is to wait a short amount of time before retrying the copy.
196 #
197 if {$nRetry > 0} {
198 for {set i 0} {$i<$nRetry} {incr i} {
199 set rc [catch {
200 if {$force} {
201 file copy -force $from $to
202 } else {
203 file copy $from $to
204 }
205 } msg]
206 if {$rc==0} break
207 if {$nDelay > 0} { after $nDelay }
208 }
209 if {$rc} { error $msg }
210 } else {
211 if {$force} {
212 file copy -force $from $to
213 } else {
214 file copy $from $to
215 }
216 }
217}
218
mistachkinc5484652012-03-05 22:52:33 +0000219# Check if a file name is relative
220#
221proc is_relative_file { file } {
222 return [expr {[file pathtype $file] != "absolute"}]
223}
224
mistachkin4a41f342012-03-06 03:00:49 +0000225# If the VFS supports using the current directory, returns [pwd];
226# otherwise, it returns only the provided suffix string (which is
227# empty by default).
228#
229proc test_pwd { args } {
230 if {[llength $args] > 0} {
231 set suffix1 [lindex $args 0]
232 if {[llength $args] > 1} {
233 set suffix2 [lindex $args 1]
234 } else {
235 set suffix2 $suffix1
236 }
237 } else {
238 set suffix1 ""; set suffix2 ""
239 }
240 ifcapable curdir {
mistachkin6aa18c92012-03-08 20:22:42 +0000241 return "[get_pwd]$suffix1"
mistachkin4a41f342012-03-06 03:00:49 +0000242 } else {
243 return $suffix2
244 }
245}
246
mistachkinfda06be2011-08-02 00:57:34 +0000247# Delete a file or directory
248#
249proc delete_file {args} {
250 do_delete_file false {*}$args
251}
252
253proc forcedelete {args} {
254 do_delete_file true {*}$args
255}
256
257proc do_delete_file {force args} {
258 set nRetry [getFileRetries] ;# Maximum number of retries.
259 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
260
261 foreach filename $args {
262 # On windows, sometimes even a [file delete -force] can fail just after
263 # a file is closed. The cause is usually "tag-alongs" - programs like
264 # anti-virus software, automatic backup tools and various explorer
265 # extensions that keep a file open a little longer than we expect, causing
266 # the delete to fail.
267 #
268 # The solution is to wait a short amount of time before retrying the
269 # delete.
270 #
271 if {$nRetry > 0} {
272 for {set i 0} {$i<$nRetry} {incr i} {
273 set rc [catch {
274 if {$force} {
275 file delete -force $filename
276 } else {
277 file delete $filename
278 }
279 } msg]
280 if {$rc==0} break
281 if {$nDelay > 0} { after $nDelay }
282 }
283 if {$rc} { error $msg }
284 } else {
285 if {$force} {
286 file delete -force $filename
287 } else {
288 file delete $filename
289 }
290 }
291 }
292}
293
dancb354602010-07-08 09:44:42 +0000294proc execpresql {handle args} {
295 trace remove execution $handle enter [list execpresql $handle]
296 if {[info exists ::G(perm:presql)]} {
297 $handle eval $::G(perm:presql)
298 }
299}
300
dan68928b62010-06-22 13:46:43 +0000301# This command should be called after loading tester.tcl from within
302# all test scripts that are incompatible with encryption codecs.
303#
304proc do_not_use_codec {} {
305 set ::do_not_use_codec 1
306 reset_db
307}
308
dan430e74c2010-06-07 17:47:26 +0000309# The following block only runs the first time this file is sourced. It
310# does not run in slave interpreters (since the ::cmdlinearg array is
311# populated before the test script is run in slave interpreters).
drhbec2bf42000-05-29 23:48:22 +0000312#
danc1a60c52010-06-07 14:28:16 +0000313if {[info exists cmdlinearg]==0} {
314
315 # Parse any options specified in the $argv array. This script accepts the
316 # following options:
317 #
318 # --pause
319 # --soft-heap-limit=NN
320 # --maxerror=NN
321 # --malloctrace=N
322 # --backtrace=N
323 # --binarylog=N
dan0626dfc2010-06-15 06:56:37 +0000324 # --soak=N
mistachkinfda06be2011-08-02 00:57:34 +0000325 # --file-retries=N
326 # --file-retry-delay=N
dan6a64d672011-04-04 15:38:16 +0000327 # --start=[$permutation:]$testfile
mistachkin6c3c1a02011-11-12 03:17:40 +0000328 # --match=$pattern
danc1a60c52010-06-07 14:28:16 +0000329 #
330 set cmdlinearg(soft-heap-limit) 0
331 set cmdlinearg(maxerror) 1000
332 set cmdlinearg(malloctrace) 0
333 set cmdlinearg(backtrace) 10
334 set cmdlinearg(binarylog) 0
dan0626dfc2010-06-15 06:56:37 +0000335 set cmdlinearg(soak) 0
mistachkinfda06be2011-08-02 00:57:34 +0000336 set cmdlinearg(file-retries) 0
337 set cmdlinearg(file-retry-delay) 0
mistachkin6c3c1a02011-11-12 03:17:40 +0000338 set cmdlinearg(start) ""
339 set cmdlinearg(match) ""
danc1a60c52010-06-07 14:28:16 +0000340
341 set leftover [list]
342 foreach a $argv {
343 switch -regexp -- $a {
344 {^-+pause$} {
345 # Wait for user input before continuing. This is to give the user an
346 # opportunity to connect profiling tools to the process.
347 puts -nonewline "Press RETURN to begin..."
348 flush stdout
349 gets stdin
350 }
351 {^-+soft-heap-limit=.+$} {
352 foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break
353 }
354 {^-+maxerror=.+$} {
355 foreach {dummy cmdlinearg(maxerror)} [split $a =] break
356 }
357 {^-+malloctrace=.+$} {
358 foreach {dummy cmdlinearg(malloctrace)} [split $a =] break
359 if {$cmdlinearg(malloctrace)} {
360 sqlite3_memdebug_log start
361 }
362 }
363 {^-+backtrace=.+$} {
364 foreach {dummy cmdlinearg(backtrace)} [split $a =] break
dan0626dfc2010-06-15 06:56:37 +0000365 sqlite3_memdebug_backtrace $value
danc1a60c52010-06-07 14:28:16 +0000366 }
danc1a60c52010-06-07 14:28:16 +0000367 {^-+binarylog=.+$} {
368 foreach {dummy cmdlinearg(binarylog)} [split $a =] break
369 }
dan0626dfc2010-06-15 06:56:37 +0000370 {^-+soak=.+$} {
371 foreach {dummy cmdlinearg(soak)} [split $a =] break
372 set ::G(issoak) $cmdlinearg(soak)
373 }
mistachkinfda06be2011-08-02 00:57:34 +0000374 {^-+file-retries=.+$} {
375 foreach {dummy cmdlinearg(file-retries)} [split $a =] break
376 set ::G(file-retries) $cmdlinearg(file-retries)
377 }
378 {^-+file-retry-delay=.+$} {
379 foreach {dummy cmdlinearg(file-retry-delay)} [split $a =] break
380 set ::G(file-retry-delay) $cmdlinearg(file-retry-delay)
381 }
dan6a64d672011-04-04 15:38:16 +0000382 {^-+start=.+$} {
383 foreach {dummy cmdlinearg(start)} [split $a =] break
384
385 set ::G(start:file) $cmdlinearg(start)
386 if {[regexp {(.*):(.*)} $cmdlinearg(start) -> s.perm s.file]} {
387 set ::G(start:permutation) ${s.perm}
388 set ::G(start:file) ${s.file}
389 }
390 if {$::G(start:file) == ""} {unset ::G(start:file)}
391 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000392 {^-+match=.+$} {
393 foreach {dummy cmdlinearg(match)} [split $a =] break
394
395 set ::G(match) $cmdlinearg(match)
396 if {$::G(match) == ""} {unset ::G(match)}
397 }
danc1a60c52010-06-07 14:28:16 +0000398 default {
399 lappend leftover $a
400 }
401 }
402 }
403 set argv $leftover
404
dan430e74c2010-06-07 17:47:26 +0000405 # Install the malloc layer used to inject OOM errors. And the 'automatic'
406 # extensions. This only needs to be done once for the process.
407 #
danielk1977f7b9d662008-06-23 18:49:43 +0000408 sqlite3_shutdown
409 install_malloc_faultsim 1
410 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000411 autoinstall_test_functions
danc1a60c52010-06-07 14:28:16 +0000412
dan430e74c2010-06-07 17:47:26 +0000413 # If the --binarylog option was specified, create the logging VFS. This
414 # call installs the new VFS as the default for all SQLite connections.
415 #
danc1a60c52010-06-07 14:28:16 +0000416 if {$cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000417 vfslog new binarylog {} vfslog.bin
danc1a60c52010-06-07 14:28:16 +0000418 }
419
420 # Set the backtrace depth, if malloc tracing is enabled.
421 #
422 if {$cmdlinearg(malloctrace)} {
423 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danielk1977185eac92008-07-12 15:55:54 +0000424 }
danielk1977f7b9d662008-06-23 18:49:43 +0000425}
danielk197703ba3fa2009-01-09 10:49:14 +0000426
danc1a60c52010-06-07 14:28:16 +0000427# Update the soft-heap-limit each time this script is run. In that
428# way if an individual test file changes the soft-heap-limit, it
429# will be reset at the start of the next test file.
430#
431sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
432
433# Create a test database
434#
danielk197703ba3fa2009-01-09 10:49:14 +0000435proc reset_db {} {
436 catch {db close}
mistachkinfda06be2011-08-02 00:57:34 +0000437 forcedelete test.db
438 forcedelete test.db-journal
439 forcedelete test.db-wal
danielk197703ba3fa2009-01-09 10:49:14 +0000440 sqlite3 db ./test.db
441 set ::DB [sqlite3_connection_pointer db]
442 if {[info exists ::SETUP_SQL]} {
443 db eval $::SETUP_SQL
444 }
drhcd61c282002-03-06 22:01:34 +0000445}
danielk197703ba3fa2009-01-09 10:49:14 +0000446reset_db
drhbec2bf42000-05-29 23:48:22 +0000447
448# Abort early if this script has been run before.
449#
danc1a60c52010-06-07 14:28:16 +0000450if {[info exists TC(count)]} return
drhbec2bf42000-05-29 23:48:22 +0000451
drhce2198c2010-09-10 13:22:59 +0000452# Make sure memory statistics are enabled.
453#
454sqlite3_config_memstatus 1
455
danc1a60c52010-06-07 14:28:16 +0000456# Initialize the test counters and set up commands to access them.
457# Or, if this is a slave interpreter, set up aliases to write the
458# counters in the parent interpreter.
drhbec2bf42000-05-29 23:48:22 +0000459#
danc1a60c52010-06-07 14:28:16 +0000460if {0==[info exists ::SLAVE]} {
461 set TC(errors) 0
462 set TC(count) 0
463 set TC(fail_list) [list]
464 set TC(omit_list) [list]
drhcca17c32013-04-19 12:32:52 +0000465 set TC(warn_list) [list]
danc1a60c52010-06-07 14:28:16 +0000466
467 proc set_test_counter {counter args} {
468 if {[llength $args]} {
469 set ::TC($counter) [lindex $args 0]
470 }
471 set ::TC($counter)
472 }
drhb62c3352006-11-23 09:39:16 +0000473}
drh348784e2000-05-29 20:41:49 +0000474
drh521cc842008-04-15 02:36:33 +0000475# Record the fact that a sequence of tests were omitted.
476#
mistachkin6c3c1a02011-11-12 03:17:40 +0000477proc omit_test {name reason {append 1}} {
danc1a60c52010-06-07 14:28:16 +0000478 set omitList [set_test_counter omit_list]
mistachkin6c3c1a02011-11-12 03:17:40 +0000479 if {$append} {
480 lappend omitList [list $name $reason]
481 }
danc1a60c52010-06-07 14:28:16 +0000482 set_test_counter omit_list $omitList
drh521cc842008-04-15 02:36:33 +0000483}
484
danc1a60c52010-06-07 14:28:16 +0000485# Record the fact that a test failed.
486#
487proc fail_test {name} {
488 set f [set_test_counter fail_list]
489 lappend f $name
490 set_test_counter fail_list $f
491 set_test_counter errors [expr [set_test_counter errors] + 1]
492
493 set nFail [set_test_counter errors]
494 if {$nFail>=$::cmdlinearg(maxerror)} {
495 puts "*** Giving up..."
496 finalize_testing
497 }
498}
499
drhcca17c32013-04-19 12:32:52 +0000500# Remember a warning message to be displayed at the conclusion of all testing
501#
502proc warning {msg {append 1}} {
503 puts "Warning: $msg"
504 set warnList [set_test_counter warn_list]
505 if {$append} {
506 lappend warnList $msg
507 }
508 set_test_counter warn_list $warnList
509}
510
511
danc1a60c52010-06-07 14:28:16 +0000512# Increment the number of tests run
513#
514proc incr_ntest {} {
515 set_test_counter count [expr [set_test_counter count] + 1]
516}
517
518
drh348784e2000-05-29 20:41:49 +0000519# Invoke the do_test procedure to run a single test
520#
521proc do_test {name cmd expected} {
danc1a60c52010-06-07 14:28:16 +0000522 global argv cmdlinearg
523
dan8c408002010-11-01 17:38:24 +0000524 fix_testname name
525
drh4a50aac2007-08-23 02:47:53 +0000526 sqlite3_memdebug_settitle $name
danc1a60c52010-06-07 14:28:16 +0000527
dan92d516a2010-07-05 14:54:48 +0000528# if {[llength $argv]==0} {
529# set go 1
530# } else {
531# set go 0
532# foreach pattern $argv {
533# if {[string match $pattern $name]} {
534# set go 1
535# break
536# }
537# }
538# }
dan430e74c2010-06-07 17:47:26 +0000539
dand506de02010-07-03 13:59:01 +0000540 if {[info exists ::G(perm:prefix)]} {
541 set name "$::G(perm:prefix)$name"
dan430e74c2010-06-07 17:47:26 +0000542 }
543
danc1a60c52010-06-07 14:28:16 +0000544 incr_ntest
drh5edc3122001-09-13 21:53:09 +0000545 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000546 flush stdout
mistachkin6c3c1a02011-11-12 03:17:40 +0000547
548 if {![info exists ::G(match)] || [string match $::G(match) $name]} {
549 if {[catch {uplevel #0 "$cmd;\n"} result]} {
550 puts "\nError: $result"
551 fail_test $name
mistachkin6c3c1a02011-11-12 03:17:40 +0000552 } else {
drh3f17aef2012-04-27 01:08:02 +0000553 if {[regexp {^~?/.*/$} $expected]} {
554 if {[string index $expected 0]=="~"} {
drh5343b2d2012-09-27 19:53:38 +0000555 set re [string map {# {[-0-9.]+}} [string range $expected 2 end-1]]
drh3f17aef2012-04-27 01:08:02 +0000556 set ok [expr {![regexp $re $result]}]
557 } else {
drh5343b2d2012-09-27 19:53:38 +0000558 set re [string map {# {[-0-9.]+}} [string range $expected 1 end-1]]
drh3f17aef2012-04-27 01:08:02 +0000559 set ok [regexp $re $result]
560 }
561 } else {
562 set ok [expr {[string compare $result $expected]==0}]
563 }
564 if {!$ok} {
mistachkinc60941f2012-09-13 01:51:02 +0000565 # if {![info exists ::testprefix] || $::testprefix eq ""} {
566 # error "no test prefix"
567 # }
drh3f17aef2012-04-27 01:08:02 +0000568 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
569 fail_test $name
570 } else {
571 puts " Ok"
572 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000573 }
drh348784e2000-05-29 20:41:49 +0000574 } else {
mistachkin6c3c1a02011-11-12 03:17:40 +0000575 puts " Omitted"
576 omit_test $name "pattern mismatch" 0
drh348784e2000-05-29 20:41:49 +0000577 }
drh6558db82007-04-13 03:23:21 +0000578 flush stdout
drh348784e2000-05-29 20:41:49 +0000579}
dana3f51082010-09-30 18:43:14 +0000580
drh8df91852012-04-24 12:46:05 +0000581proc catchcmd {db {cmd ""}} {
582 global CLI
583 set out [open cmds.txt w]
584 puts $out $cmd
585 close $out
586 set line "exec $CLI $db < cmds.txt"
587 set rc [catch { eval $line } msg]
588 list $rc $msg
589}
590
shaneh55505172011-06-21 19:30:19 +0000591proc filepath_normalize {p} {
592 # test cases should be written to assume "unix"-like file paths
shaneh285a18f2011-06-21 19:39:59 +0000593 if {$::tcl_platform(platform)!="unix"} {
shaneh55505172011-06-21 19:30:19 +0000594 # lreverse*2 as a hack to remove any unneeded {} after the string map
595 lreverse [lreverse [string map {\\ /} [regsub -nocase -all {[a-z]:[/\\]+} $p {/}]]]
shanehc4896402011-06-21 19:38:16 +0000596 } {
597 set p
shaneh55505172011-06-21 19:30:19 +0000598 }
599}
600proc do_filepath_test {name cmd expected} {
601 uplevel [list do_test $name [
602 subst -nocommands { filepath_normalize [ $cmd ] }
603 ] [filepath_normalize $expected]]
604}
605
dan33f53792011-05-05 19:44:22 +0000606proc realnum_normalize {r} {
shaneh4f529e82011-06-20 17:41:41 +0000607 # different TCL versions display floating point values differently.
608 string map {1.#INF inf Inf inf .0e e} [regsub -all {(e[+-])0+} $r {\1}]
dan33f53792011-05-05 19:44:22 +0000609}
610proc do_realnum_test {name cmd expected} {
611 uplevel [list do_test $name [
612 subst -nocommands { realnum_normalize [ $cmd ] }
613 ] [realnum_normalize $expected]]
614}
615
dana3f51082010-09-30 18:43:14 +0000616proc fix_testname {varname} {
617 upvar $varname testname
618 if {[info exists ::testprefix]
619 && [string is digit [string range $testname 0 0]]
620 } {
621 set testname "${::testprefix}-$testname"
622 }
623}
dan153eda02010-06-21 07:45:47 +0000624
dan57f7f4b2010-10-01 19:04:37 +0000625proc do_execsql_test {testname sql {result {}}} {
dana3f51082010-09-30 18:43:14 +0000626 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000627 uplevel do_test [list $testname] [list "execsql {$sql}"] [list [list {*}$result]]
dan153eda02010-06-21 07:45:47 +0000628}
629proc do_catchsql_test {testname sql result} {
dana3f51082010-09-30 18:43:14 +0000630 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000631 uplevel do_test [list $testname] [list "catchsql {$sql}"] [list $result]
dan153eda02010-06-21 07:45:47 +0000632}
dan39854792010-11-15 16:12:58 +0000633proc do_eqp_test {name sql res} {
634 uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res]
635}
dan153eda02010-06-21 07:45:47 +0000636
dan51f06982010-09-18 19:00:12 +0000637#-------------------------------------------------------------------------
638# Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST
639#
640# Where switches are:
641#
642# -errorformat FMTSTRING
643# -count
danaf1dcab2010-09-20 19:17:53 +0000644# -query SQL
dana16d1062010-09-28 17:37:28 +0000645# -tclquery TCL
danaf7626f2010-09-23 18:47:36 +0000646# -repair TCL
dan51f06982010-09-18 19:00:12 +0000647#
648proc do_select_tests {prefix args} {
649
650 set testlist [lindex $args end]
651 set switches [lrange $args 0 end-1]
652
653 set errfmt ""
654 set countonly 0
dana16d1062010-09-28 17:37:28 +0000655 set tclquery ""
danaf7626f2010-09-23 18:47:36 +0000656 set repair ""
dan51f06982010-09-18 19:00:12 +0000657
658 for {set i 0} {$i < [llength $switches]} {incr i} {
659 set s [lindex $switches $i]
660 set n [string length $s]
danaf1dcab2010-09-20 19:17:53 +0000661 if {$n>=2 && [string equal -length $n $s "-query"]} {
dana16d1062010-09-28 17:37:28 +0000662 set tclquery [list execsql [lindex $switches [incr i]]]
663 } elseif {$n>=2 && [string equal -length $n $s "-tclquery"]} {
664 set tclquery [lindex $switches [incr i]]
danaf1dcab2010-09-20 19:17:53 +0000665 } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} {
dan51f06982010-09-18 19:00:12 +0000666 set errfmt [lindex $switches [incr i]]
danaf7626f2010-09-23 18:47:36 +0000667 } elseif {$n>=2 && [string equal -length $n $s "-repair"]} {
668 set repair [lindex $switches [incr i]]
dan51f06982010-09-18 19:00:12 +0000669 } elseif {$n>=2 && [string equal -length $n $s "-count"]} {
670 set countonly 1
671 } else {
672 error "unknown switch: $s"
673 }
674 }
675
676 if {$countonly && $errfmt!=""} {
677 error "Cannot use -count and -errorformat together"
678 }
679 set nTestlist [llength $testlist]
680 if {$nTestlist%3 || $nTestlist==0 } {
681 error "SELECT test list contains [llength $testlist] elements"
682 }
683
danaf7626f2010-09-23 18:47:36 +0000684 eval $repair
dan51f06982010-09-18 19:00:12 +0000685 foreach {tn sql res} $testlist {
dana16d1062010-09-28 17:37:28 +0000686 if {$tclquery != ""} {
danaf1dcab2010-09-20 19:17:53 +0000687 execsql $sql
dana16d1062010-09-28 17:37:28 +0000688 uplevel do_test ${prefix}.$tn [list $tclquery] [list [list {*}$res]]
689 } elseif {$countonly} {
dan51f06982010-09-18 19:00:12 +0000690 set nRow 0
691 db eval $sql {incr nRow}
692 uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res]
693 } elseif {$errfmt==""} {
694 uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]]
695 } else {
696 set res [list 1 [string trim [format $errfmt {*}$res]]]
697 uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res]
698 }
danaf7626f2010-09-23 18:47:36 +0000699 eval $repair
dan51f06982010-09-18 19:00:12 +0000700 }
danaf7626f2010-09-23 18:47:36 +0000701
dan51f06982010-09-18 19:00:12 +0000702}
703
danb04757a2010-09-21 16:59:16 +0000704proc delete_all_data {} {
705 db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} {
706 db eval "DELETE FROM '[string map {' ''} $t]'"
707 }
708}
drh348784e2000-05-29 20:41:49 +0000709
drhb62c3352006-11-23 09:39:16 +0000710# Run an SQL script.
711# Return the number of microseconds per statement.
712#
drh3590f152006-11-23 21:09:10 +0000713proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000714 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000715 flush stdout
716 set speed [time {sqlite3_exec_nr db $sql}]
717 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000718 if {$tm == 0} {
719 set rate [format %20s "many"]
720 } else {
721 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
722 }
drh3590f152006-11-23 21:09:10 +0000723 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000724 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000725 global total_time
726 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000727 lappend ::speed_trial_times $name $tm
drhdd924312007-03-31 22:34:16 +0000728}
drh45c236d2008-03-22 01:08:00 +0000729proc speed_trial_tcl {name numstmt units script} {
730 puts -nonewline [format {%-21.21s } $name...]
731 flush stdout
732 set speed [time {eval $script}]
733 set tm [lindex $speed 0]
734 if {$tm == 0} {
735 set rate [format %20s "many"]
736 } else {
737 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
738 }
739 set u2 $units/s
740 puts [format {%12d uS %s %s} $tm $rate $u2]
741 global total_time
742 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000743 lappend ::speed_trial_times $name $tm
drh45c236d2008-03-22 01:08:00 +0000744}
drhdd924312007-03-31 22:34:16 +0000745proc speed_trial_init {name} {
746 global total_time
747 set total_time 0
dana975b592010-10-08 16:09:43 +0000748 set ::speed_trial_times [list]
drhbb810a92010-07-03 12:00:53 +0000749 sqlite3 versdb :memory:
750 set vers [versdb one {SELECT sqlite_source_id()}]
751 versdb close
752 puts "SQLite $vers"
drhdd924312007-03-31 22:34:16 +0000753}
754proc speed_trial_summary {name} {
755 global total_time
756 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
dana975b592010-10-08 16:09:43 +0000757
758 if { 0 } {
759 sqlite3 versdb :memory:
760 set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0]
761 versdb close
762 puts "CREATE TABLE IF NOT EXISTS time(version, script, test, us);"
763 foreach {test us} $::speed_trial_times {
764 puts "INSERT INTO time VALUES('$vers', '$name', '$test', $us);"
765 }
766 }
drhb62c3352006-11-23 09:39:16 +0000767}
768
drh348784e2000-05-29 20:41:49 +0000769# Run this routine last
770#
771proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +0000772 catch {db close}
773 catch {db2 close}
774 catch {db3 close}
775 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +0000776}
777proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +0000778 global sqlite_open_file_count
779
780 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +0000781
drh6e142f52000-06-08 13:36:40 +0000782 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000783 catch {db2 close}
784 catch {db3 close}
785
drh9bc54492007-10-23 14:49:59 +0000786 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000787 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000788 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000789 db close
drh984bfaa2008-03-19 16:08:53 +0000790 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +0000791
drh3a7fb7c2007-08-10 16:41:08 +0000792 sqlite3_soft_heap_limit 0
danc1a60c52010-06-07 14:28:16 +0000793 set nTest [incr_ntest]
794 set nErr [set_test_counter errors]
795
drh348784e2000-05-29 20:41:49 +0000796 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000797 if {$nErr>0} {
danc1a60c52010-06-07 14:28:16 +0000798 puts "Failures on these tests: [set_test_counter fail_list]"
drhf3a65f72007-08-22 20:18:21 +0000799 }
drhcca17c32013-04-19 12:32:52 +0000800 foreach warning [set_test_counter warn_list] {
801 puts "Warning: $warning"
802 }
danielk1977ee8b7992009-03-26 17:13:06 +0000803 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +0000804 if {[llength $omitList]>0} {
805 puts "Omitted test cases:"
806 set prec {}
807 foreach {rec} [lsort $omitList] {
808 if {$rec==$prec} continue
809 set prec $rec
810 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
811 }
812 }
drh80788d82006-09-02 14:50:23 +0000813 if {$nErr>0 && ![working_64bit_int]} {
814 puts "******************************************************************"
815 puts "N.B.: The version of TCL that you used to build this test harness"
816 puts "is defective in that it does not support 64-bit integers. Some or"
817 puts "all of the test failures above might be a result from this defect"
818 puts "in your TCL build."
819 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000820 }
danc1a60c52010-06-07 14:28:16 +0000821 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000822 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +0000823 }
drh94e92032003-02-16 22:21:32 +0000824 if {$sqlite_open_file_count} {
825 puts "$sqlite_open_file_count files were left open"
826 incr nErr
827 }
drheafc43b2010-07-26 18:43:40 +0000828 if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 ||
829 [sqlite3_memory_used]>0} {
830 puts "Unfreed memory: [sqlite3_memory_used] bytes in\
831 [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations"
drhf3a65f72007-08-22 20:18:21 +0000832 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000833 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000834 puts "Writing unfreed memory log to \"./memleak.txt\""
835 sqlite3_memdebug_dump ./memleak.txt
836 }
drhf3a65f72007-08-22 20:18:21 +0000837 } else {
838 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000839 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000840 sqlite3_memdebug_dump ./memusage.txt
841 }
drhf3a65f72007-08-22 20:18:21 +0000842 }
drhf7141992008-06-19 00:16:08 +0000843 show_memstats
drh91fd4d42008-01-19 20:11:25 +0000844 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
845 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000846 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
847 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
848 }
danc1a60c52010-06-07 14:28:16 +0000849 if {$::cmdlinearg(malloctrace)} {
danielk197735754ac2008-03-21 17:29:37 +0000850 puts "Writing mallocs.sql..."
851 memdebug_log_sql
852 sqlite3_memdebug_log stop
853 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000854
855 if {[sqlite3_memory_used]>0} {
856 puts "Writing leaks.sql..."
857 sqlite3_memdebug_log sync
858 memdebug_log_sql leaks.sql
859 }
danielk197735754ac2008-03-21 17:29:37 +0000860 }
drhd9910fe2006-10-04 11:55:49 +0000861 foreach f [glob -nocomplain test.db-*-journal] {
mistachkinfda06be2011-08-02 00:57:34 +0000862 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +0000863 }
864 foreach f [glob -nocomplain test.db-mj*] {
mistachkinfda06be2011-08-02 00:57:34 +0000865 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +0000866 }
drhdb25e382001-03-15 18:21:22 +0000867 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000868}
869
drhf7141992008-06-19 00:16:08 +0000870# Display memory statistics for analysis and debugging purposes.
871#
872proc show_memstats {} {
873 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +0000874 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
875 set val [format {now %10d max %10d max-size %10d} \
876 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000877 puts "Memory used: $val"
drheafc43b2010-07-26 18:43:40 +0000878 set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0]
879 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
880 puts "Allocation count: $val"
drhf7141992008-06-19 00:16:08 +0000881 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +0000882 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
883 set val [format {now %10d max %10d max-size %10d} \
884 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000885 puts "Page-cache used: $val"
886 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
887 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
888 puts "Page-cache overflow: $val"
889 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
890 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
891 puts "Scratch memory used: $val"
892 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +0000893 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
894 set val [format {now %10d max %10d max-size %10d} \
895 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000896 puts "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +0000897 ifcapable yytrackmaxstackdepth {
898 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
899 set val [format { max %10d} [lindex $x 2]]
900 puts "Parser stack depth: $val"
901 }
drhf7141992008-06-19 00:16:08 +0000902}
903
drh348784e2000-05-29 20:41:49 +0000904# A procedure to execute SQL
905#
drhc4a3c772001-04-04 11:48:57 +0000906proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000907 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000908 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000909}
drh3aadb2e2000-05-31 17:59:25 +0000910
drhadbca9c2001-09-27 15:11:53 +0000911# Execute SQL and catch exceptions.
912#
913proc catchsql {sql {db db}} {
914 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +0000915 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +0000916 lappend r $msg
917 return $r
918}
919
drh04096482001-11-09 22:41:44 +0000920# Do an VDBE code dump on the SQL given
921#
922proc explain {sql {db db}} {
923 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000924 puts "addr opcode p1 p2 p3 p4 p5 #"
925 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000926 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000927 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
928 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
929 ]
drh04096482001-11-09 22:41:44 +0000930 }
931}
932
drhdafc0ce2008-04-17 19:14:02 +0000933# Show the VDBE program for an SQL statement but omit the Trace
934# opcode at the beginning. This procedure can be used to prove
935# that different SQL statements generate exactly the same VDBE code.
936#
937proc explain_no_trace {sql} {
938 set tr [db eval "EXPLAIN $sql"]
939 return [lrange $tr 7 end]
940}
941
drh3aadb2e2000-05-31 17:59:25 +0000942# Another procedure to execute SQL. This one includes the field
943# names in the returned list.
944#
945proc execsql2 {sql} {
946 set result {}
947 db eval $sql data {
948 foreach f $data(*) {
949 lappend result $f $data($f)
950 }
951 }
952 return $result
953}
drh17a68932001-01-31 13:28:08 +0000954
drhdde85d92003-03-01 19:45:34 +0000955# Use the non-callback API to execute multiple SQL statements
956#
957proc stepsql {dbptr sql} {
958 set sql [string trim $sql]
959 set r 0
960 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000961 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000962 return [list 1 $vm]
963 }
964 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000965# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
966# foreach v $VAL {lappend r $v}
967# }
968 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
969 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
970 lappend r [sqlite3_column_text $vm $i]
971 }
drhdde85d92003-03-01 19:45:34 +0000972 }
danielk1977106bb232004-05-21 10:08:53 +0000973 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000974 return [list 1 $errmsg]
975 }
976 }
977 return $r
978}
979
drh21504322002-06-25 13:16:02 +0000980# Do an integrity check of the entire database
981#
danielk197704103022009-02-03 16:51:24 +0000982proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +0000983 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +0000984 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +0000985 }
986}
987
drh433dccf2013-02-09 15:37:11 +0000988# Check the extended error code
989#
990proc verify_ex_errcode {name expected {db db}} {
991 do_test $name [list sqlite3_extended_errcode $db] $expected
992}
993
danc6055c72011-04-28 18:46:46 +0000994
995# Return true if the SQL statement passed as the second argument uses a
996# statement transaction.
997#
998proc sql_uses_stmt {db sql} {
999 set stmt [sqlite3_prepare $db $sql -1 dummy]
1000 set uses [uses_stmt_journal $stmt]
1001 sqlite3_finalize $stmt
1002 return $uses
1003}
1004
danielk1977db4e8862008-01-16 08:24:46 +00001005proc fix_ifcapable_expr {expr} {
1006 set ret ""
1007 set state 0
1008 for {set i 0} {$i < [string length $expr]} {incr i} {
1009 set char [string range $expr $i $i]
1010 set newstate [expr {[string is alnum $char] || $char eq "_"}]
1011 if {$newstate && !$state} {
1012 append ret {$::sqlite_options(}
1013 }
1014 if {!$newstate && $state} {
1015 append ret )
1016 }
1017 append ret $char
1018 set state $newstate
1019 }
1020 if {$state} {append ret )}
1021 return $ret
1022}
1023
mistachkinc60941f2012-09-13 01:51:02 +00001024# Returns non-zero if the capabilities are present; zero otherwise.
1025#
1026proc capable {expr} {
1027 set e [fix_ifcapable_expr $expr]; return [expr ($e)]
1028}
1029
drh27d258a2004-10-30 20:23:09 +00001030# Evaluate a boolean expression of capabilities. If true, execute the
1031# code. Omit the code if false.
1032#
danielk19773e8c37e2005-01-21 03:12:14 +00001033proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +00001034 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
1035 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +00001036 if ($e2) {
1037 set c [catch {uplevel 1 $code} r]
1038 } else {
1039 set c [catch {uplevel 1 $elsecode} r]
1040 }
1041 return -code $c $r
drh21504322002-06-25 13:16:02 +00001042}
danielk197745901d62004-11-10 15:27:38 +00001043
danielk1977aca790a2005-01-13 11:07:52 +00001044# This proc execs a seperate process that crashes midway through executing
1045# the SQL script $sql on database test.db.
1046#
1047# The crash occurs during a sync() of file $crashfile. When the crash
1048# occurs a random subset of all unsynced writes made by the process are
1049# written into the files on disk. Argument $crashdelay indicates the
1050# number of file syncs to wait before crashing.
1051#
1052# The return value is a list of two elements. The first element is a
1053# boolean, indicating whether or not the process actually crashed or
1054# reported some other error. The second element in the returned list is the
1055# error message. This is "child process exited abnormally" if the crash
mistachkin48864df2013-03-21 21:20:32 +00001056# occurred.
danielk1977aca790a2005-01-13 11:07:52 +00001057#
danielk1977f8940ae2007-08-23 11:07:10 +00001058# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +00001059#
1060proc crashsql {args} {
danielk197759a33f92007-03-17 10:26:59 +00001061
1062 set blocksize ""
1063 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +00001064 set prngseed 0
drhf8587402008-01-08 16:03:49 +00001065 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +00001066 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +00001067 set dc ""
danielk197759a33f92007-03-17 10:26:59 +00001068 set sql [lindex $args end]
1069
1070 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
1071 set z [lindex $args $ii]
1072 set n [string length $z]
1073 set z2 [lindex $args [expr $ii+1]]
1074
1075 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +00001076 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +00001077 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +00001078 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +00001079 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +00001080 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +00001081 else { error "Unrecognized option: $z" }
1082 }
1083
1084 if {$crashfile eq ""} {
1085 error "Compulsory option -file missing"
1086 }
1087
shanehf2c08822010-07-08 16:30:44 +00001088 # $crashfile gets compared to the native filename in
1089 # cfSync(), which can be different then what TCL uses by
1090 # default, so here we force it to the "nativename" format.
mistachkinf8a78462012-03-08 20:00:36 +00001091 set cfile [string map {\\ \\\\} [file nativename [file join [get_pwd] $crashfile]]]
danielk1977aca790a2005-01-13 11:07:52 +00001092
1093 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +00001094 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +00001095 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +00001096 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +00001097 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +00001098
1099 # This block sets the cache size of the main database to 10
1100 # pages. This is done in case the build is configured to omit
1101 # "PRAGMA cache_size".
1102 puts $f {db eval {SELECT * FROM sqlite_master;}}
1103 puts $f {set bt [btree_from_db db]}
1104 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +00001105 if {$prngseed} {
1106 set seed [expr {$prngseed%10007+1}]
1107 # puts seed=$seed
1108 puts $f "db eval {SELECT randomblob($seed)}"
1109 }
danielk1977933bbd62007-03-17 07:22:42 +00001110
drhf8587402008-01-08 16:03:49 +00001111 if {[string length $tclbody]>0} {
1112 puts $f $tclbody
1113 }
1114 if {[string length $sql]>0} {
1115 puts $f "db eval {"
1116 puts $f "$sql"
1117 puts $f "}"
1118 }
danielk1977aca790a2005-01-13 11:07:52 +00001119 close $f
danielk1977aca790a2005-01-13 11:07:52 +00001120 set r [catch {
drh9c06c952005-11-26 00:25:00 +00001121 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +00001122 } msg]
shanehf2c08822010-07-08 16:30:44 +00001123
1124 # Windows/ActiveState TCL returns a slightly different
1125 # error message. We map that to the expected message
1126 # so that we don't have to change all of the test
1127 # cases.
1128 if {$::tcl_platform(platform)=="windows"} {
1129 if {$msg=="child killed: unknown signal"} {
1130 set msg "child process exited abnormally"
1131 }
1132 }
1133
danielk1977aca790a2005-01-13 11:07:52 +00001134 lappend r $msg
1135}
1136
danb88e24f2013-02-23 18:58:11 +00001137proc run_ioerr_prep {} {
1138 set ::sqlite_io_error_pending 0
1139 catch {db close}
1140 catch {db2 close}
1141 catch {forcedelete test.db}
1142 catch {forcedelete test.db-journal}
1143 catch {forcedelete test2.db}
1144 catch {forcedelete test2.db-journal}
1145 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
1146 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
1147 if {[info exists ::ioerropts(-tclprep)]} {
1148 eval $::ioerropts(-tclprep)
1149 }
1150 if {[info exists ::ioerropts(-sqlprep)]} {
1151 execsql $::ioerropts(-sqlprep)
1152 }
1153 expr 0
1154}
1155
danielk197732554c12005-01-22 03:39:39 +00001156# Usage: do_ioerr_test <test number> <options...>
1157#
1158# This proc is used to implement test cases that check that IO errors
1159# are correctly handled. The first argument, <test number>, is an integer
1160# used to name the tests executed by this proc. Options are as follows:
1161#
1162# -tclprep TCL script to run to prepare test.
1163# -sqlprep SQL script to run to prepare test.
1164# -tclbody TCL script to run with IO error simulation.
1165# -sqlbody TCL script to run with IO error simulation.
1166# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +00001167# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +00001168# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +00001169# -start Value of 'N' to begin with (default 1)
1170#
1171# -cksum Boolean. If true, test that the database does
1172# not change during the execution of the test case.
1173#
1174proc do_ioerr_test {testname args} {
1175
danielk197732554c12005-01-22 03:39:39 +00001176 set ::ioerropts(-start) 1
1177 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +00001178 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +00001179 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +00001180 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +00001181 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +00001182 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +00001183 array set ::ioerropts $args
1184
danielk197747cd39c2008-05-12 12:41:15 +00001185 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
1186 # a couple of obscure IO errors that do not return them.
1187 set ::ioerropts(-erc) 0
danb88e24f2013-02-23 18:58:11 +00001188
1189 # Create a single TCL script from the TCL and SQL specified
1190 # as the body of the test.
1191 set ::ioerrorbody {}
1192 if {[info exists ::ioerropts(-tclbody)]} {
1193 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
1194 }
1195 if {[info exists ::ioerropts(-sqlbody)]} {
1196 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
1197 }
1198
1199 save_prng_state
1200 if {$::ioerropts(-cksum)} {
1201 run_ioerr_prep
1202 eval $::ioerrorbody
1203 set ::goodcksum [cksum]
1204 }
danielk197747cd39c2008-05-12 12:41:15 +00001205
danielk197732554c12005-01-22 03:39:39 +00001206 set ::go 1
drh93aed5a2008-01-16 17:46:38 +00001207 #reset_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +00001208 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +00001209 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +00001210 incr ::ioerropts(-count) -1
1211 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +00001212
1213 # Skip this IO error if it was specified with the "-exclude" option.
1214 if {[info exists ::ioerropts(-exclude)]} {
1215 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
1216 }
danielk1977861f7452008-06-05 11:39:11 +00001217 if {$::ioerropts(-restoreprng)} {
1218 restore_prng_state
1219 }
danielk197732554c12005-01-22 03:39:39 +00001220
1221 # Delete the files test.db and test2.db, then execute the TCL and
1222 # SQL (in that order) to prepare for the test case.
1223 do_test $testname.$n.1 {
danb88e24f2013-02-23 18:58:11 +00001224 run_ioerr_prep
danielk197732554c12005-01-22 03:39:39 +00001225 } {0}
1226
1227 # Read the 'checksum' of the database.
1228 if {$::ioerropts(-cksum)} {
danb88e24f2013-02-23 18:58:11 +00001229 set ::checksum [cksum]
danielk197732554c12005-01-22 03:39:39 +00001230 }
drh93aed5a2008-01-16 17:46:38 +00001231
danielk197732554c12005-01-22 03:39:39 +00001232 # Set the Nth IO error to fail.
1233 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +00001234 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +00001235 set ::sqlite_io_error_pending $n
1236 }] $n
danielk197732554c12005-01-22 03:39:39 +00001237
danb88e24f2013-02-23 18:58:11 +00001238 # Execute the TCL script created for the body of this test. If
1239 # at least N IO operations performed by SQLite as a result of
1240 # the script, the Nth will fail.
danielk197732554c12005-01-22 03:39:39 +00001241 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +00001242 set ::sqlite_io_error_hit 0
1243 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +00001244 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +00001245 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +00001246 set rc [sqlite3_errcode $::DB]
1247 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +00001248 # If we are in extended result code mode, make sure all of the
1249 # IOERRs we get back really do have their extended code values.
1250 # If an extended result code is returned, the sqlite3_errcode
1251 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
1252 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +00001253 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
1254 return $rc
1255 }
1256 } else {
drh5f7b5bf2007-04-19 12:30:54 +00001257 # If we are not in extended result code mode, make sure no
1258 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +00001259 if {[regexp {\+\d} $rc]} {
1260 return $rc
1261 }
1262 }
drh93aed5a2008-01-16 17:46:38 +00001263 # The test repeats as long as $::go is non-zero. $::go starts out
1264 # as 1. When a test runs to completion without hitting an I/O
1265 # error, that means there is no point in continuing with this test
1266 # case so set $::go to zero.
1267 #
1268 if {$::sqlite_io_error_pending>0} {
1269 set ::go 0
1270 set q 0
1271 set ::sqlite_io_error_pending 0
1272 } else {
1273 set q 1
1274 }
1275
drhc9ac5ca2005-11-04 22:03:30 +00001276 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +00001277 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
1278 set r 1
1279 }
danielk1977f2fa8312006-01-24 13:09:33 +00001280 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +00001281
1282 # One of two things must have happened. either
1283 # 1. We never hit the IO error and the SQL returned OK
1284 # 2. An IO error was hit and the SQL failed
1285 #
dand41a29a2010-05-06 15:56:28 +00001286 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +00001287 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +00001288 } {1}
1289
danielk197780daec62008-05-12 10:57:02 +00001290 set ::sqlite_io_error_hit 0
1291 set ::sqlite_io_error_pending 0
1292
danielk197752b472a2008-05-05 16:23:55 +00001293 # Check that no page references were leaked. There should be
1294 # a single reference if there is still an active transaction,
1295 # or zero otherwise.
1296 #
danielk197781620542008-06-07 05:19:37 +00001297 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
1298 # locks are established on database files (i.e. if the error
1299 # occurs while attempting to detect a hot-journal file), then
1300 # there may 0 page references and an active transaction according
1301 # to [sqlite3_get_autocommit].
1302 #
danielk19774abd5442008-05-05 15:26:50 +00001303 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +00001304 do_test $testname.$n.4 {
1305 set bt [btree_from_db db]
1306 db_enter db
1307 array set stats [btree_pager_stats $bt]
1308 db_leave db
danielk197781620542008-06-07 05:19:37 +00001309 set nRef $stats(ref)
1310 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
1311 } {1}
danielk19774abd5442008-05-05 15:26:50 +00001312 }
1313
danielk197752b472a2008-05-05 16:23:55 +00001314 # If there is an open database handle and no open transaction,
1315 # and the pager is not running in exclusive-locking mode,
1316 # check that the pager is in "unlocked" state. Theoretically,
1317 # if a call to xUnlock() failed due to an IO error the underlying
1318 # file may still be locked.
1319 #
1320 ifcapable pragma {
1321 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +00001322 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +00001323 && [db one {pragma locking_mode}] eq "normal"
1324 && [sqlite3_get_autocommit db]
1325 } {
1326 do_test $testname.$n.5 {
1327 set bt [btree_from_db db]
1328 db_enter db
1329 array set stats [btree_pager_stats $bt]
1330 db_leave db
1331 set stats(state)
1332 } 0
1333 }
1334 }
1335
mistachkin48864df2013-03-21 21:20:32 +00001336 # If an IO error occurred, then the checksum of the database should
danielk197732554c12005-01-22 03:39:39 +00001337 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +00001338 #
drh1aa5af12008-03-07 19:51:14 +00001339 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +00001340 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +00001341 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +00001342 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +00001343 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danb88e24f2013-02-23 18:58:11 +00001344 set nowcksum [cksum]
1345 set res [expr {$nowcksum==$::checksum || $nowcksum==$::goodcksum}]
1346 if {$res==0} {
1347 puts "now=$nowcksum"
1348 puts "the=$::checksum"
1349 puts "fwd=$::goodcksum"
1350 }
1351 set res
1352 } 1
danielk197732554c12005-01-22 03:39:39 +00001353 }
1354
drh1aa5af12008-03-07 19:51:14 +00001355 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +00001356 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +00001357 if {[info exists ::ioerropts(-cleanup)]} {
1358 catch $::ioerropts(-cleanup)
1359 }
danielk197732554c12005-01-22 03:39:39 +00001360 }
1361 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +00001362 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +00001363 unset ::ioerropts
1364}
1365
drh93aed5a2008-01-16 17:46:38 +00001366# Return a checksum based on the contents of the main database associated
1367# with connection $db
danielk197732554c12005-01-22 03:39:39 +00001368#
1369proc cksum {{db db}} {
1370 set txt [$db eval {
1371 SELECT name, type, sql FROM sqlite_master order by name
1372 }]\n
1373 foreach tbl [$db eval {
1374 SELECT name FROM sqlite_master WHERE type='table' order by name
1375 }] {
1376 append txt [$db eval "SELECT * FROM $tbl"]\n
1377 }
1378 foreach prag {default_synchronous default_cache_size} {
1379 append txt $prag-[$db eval "PRAGMA $prag"]\n
1380 }
1381 set cksum [string length $txt]-[md5 $txt]
1382 # puts $cksum-[file size test.db]
1383 return $cksum
1384}
1385
drh93aed5a2008-01-16 17:46:38 +00001386# Generate a checksum based on the contents of the main and temp tables
1387# database $db. If the checksum of two databases is the same, and the
1388# integrity-check passes for both, the two databases are identical.
1389#
drh1db639c2008-01-17 02:36:28 +00001390proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +00001391 set ret [list]
1392 ifcapable tempdb {
1393 set sql {
1394 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1395 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
1396 SELECT 'sqlite_master' UNION
1397 SELECT 'sqlite_temp_master' ORDER BY 1
1398 }
1399 } else {
1400 set sql {
1401 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1402 SELECT 'sqlite_master' ORDER BY 1
1403 }
1404 }
1405 set tbllist [$db eval $sql]
1406 set txt {}
1407 foreach tbl $tbllist {
1408 append txt [$db eval "SELECT * FROM $tbl"]
1409 }
1410 foreach prag {default_cache_size} {
1411 append txt $prag-[$db eval "PRAGMA $prag"]\n
1412 }
1413 # puts txt=$txt
1414 return [md5 $txt]
1415}
1416
drhdc2c4912009-02-04 22:46:47 +00001417# Generate a checksum based on the contents of a single database with
1418# a database connection. The name of the database is $dbname.
1419# Examples of $dbname are "temp" or "main".
1420#
1421proc dbcksum {db dbname} {
1422 if {$dbname=="temp"} {
1423 set master sqlite_temp_master
1424 } else {
1425 set master $dbname.sqlite_master
1426 }
1427 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
1428 set txt [$db eval "SELECT * FROM $master"]\n
1429 foreach tab $alltab {
1430 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
1431 }
1432 return [md5 $txt]
1433}
1434
danielk197735754ac2008-03-21 17:29:37 +00001435proc memdebug_log_sql {{filename mallocs.sql}} {
1436
danielk19776f332c12008-03-21 14:22:44 +00001437 set data [sqlite3_memdebug_log dump]
1438 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +00001439 if {$nFrame < 0} { return "" }
1440
danielk197735754ac2008-03-21 17:29:37 +00001441 set database temp
1442
danielk19776ab3a2e2009-02-19 14:39:25 +00001443 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +00001444
1445 set sql ""
1446 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +00001447 set nCall [lindex $e 0]
1448 set nByte [lindex $e 1]
1449 set lStack [lrange $e 2 end]
1450 append sql "INSERT INTO ${database}.malloc VALUES"
1451 append sql "('test', $nCall, $nByte, '$lStack');\n"
1452 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +00001453 set frames($f) 1
1454 }
1455 }
1456
1457 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +00001458 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +00001459
1460 foreach f [array names frames] {
1461 set addr [format %x $f]
1462 set cmd "addr2line -e [info nameofexec] $addr"
1463 set line [eval exec $cmd]
1464 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00001465
1466 set file [lindex [split $line :] 0]
1467 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00001468 }
1469
danielk197735754ac2008-03-21 17:29:37 +00001470 foreach f [array names files] {
1471 set contents ""
1472 catch {
1473 set fd [open $f]
1474 set contents [read $fd]
1475 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001476 }
danielk197735754ac2008-03-21 17:29:37 +00001477 set contents [string map {' ''} $contents]
1478 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00001479 }
danielk19776f332c12008-03-21 14:22:44 +00001480
danielk197735754ac2008-03-21 17:29:37 +00001481 set fd [open $filename w]
1482 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
1483 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001484}
drh93aed5a2008-01-16 17:46:38 +00001485
danf5894502009-10-07 18:41:19 +00001486# Drop all tables in database [db]
1487proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00001488 ifcapable trigger&&foreignkey {
1489 set pk [$db one "PRAGMA foreign_keys"]
1490 $db eval "PRAGMA foreign_keys = OFF"
1491 }
drh9a6ffc82010-02-15 18:03:20 +00001492 foreach {idx name file} [db eval {PRAGMA database_list}] {
1493 if {$idx==1} {
1494 set master sqlite_temp_master
1495 } else {
1496 set master $name.sqlite_master
1497 }
1498 foreach {t type} [$db eval "
1499 SELECT name, type FROM $master
dana16d1062010-09-28 17:37:28 +00001500 WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X'
drh9a6ffc82010-02-15 18:03:20 +00001501 "] {
dana16d1062010-09-28 17:37:28 +00001502 $db eval "DROP $type \"$t\""
drh9a6ffc82010-02-15 18:03:20 +00001503 }
danf5894502009-10-07 18:41:19 +00001504 }
shaneh4e7b32f2009-12-17 22:12:51 +00001505 ifcapable trigger&&foreignkey {
1506 $db eval "PRAGMA foreign_keys = $pk"
1507 }
danf5894502009-10-07 18:41:19 +00001508}
1509
dan71cb5182010-04-26 12:39:03 +00001510#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00001511# If a test script is executed with global variable $::G(perm:name) set to
1512# "wal", then the tests are run in WAL mode. Otherwise, they should be run
1513# in rollback mode. The following Tcl procs are used to make this less
1514# intrusive:
dan71cb5182010-04-26 12:39:03 +00001515#
1516# wal_set_journal_mode ?DB?
1517#
1518# If running a WAL test, execute "PRAGMA journal_mode = wal" using
1519# connection handle DB. Otherwise, this command is a no-op.
1520#
1521# wal_check_journal_mode TESTNAME ?DB?
1522#
1523# If running a WAL test, execute a tests case that fails if the main
1524# database for connection handle DB is not currently a WAL database.
1525# Otherwise (if not running a WAL permutation) this is a no-op.
1526#
1527# wal_is_wal_mode
1528#
1529# Returns true if this test should be run in WAL mode. False otherwise.
1530#
1531proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00001532 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00001533}
1534proc wal_set_journal_mode {{db db}} {
1535 if { [wal_is_wal_mode] } {
1536 $db eval "PRAGMA journal_mode = WAL"
1537 }
1538}
1539proc wal_check_journal_mode {testname {db db}} {
1540 if { [wal_is_wal_mode] } {
1541 $db eval { SELECT * FROM sqlite_master }
1542 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
1543 }
1544}
1545
dan430e74c2010-06-07 17:47:26 +00001546proc permutation {} {
1547 set perm ""
1548 catch {set perm $::G(perm:name)}
1549 set perm
1550}
dancb79e512010-08-06 13:50:07 +00001551proc presql {} {
1552 set presql ""
1553 catch {set presql $::G(perm:presql)}
1554 set presql
1555}
dan430e74c2010-06-07 17:47:26 +00001556
danc1a60c52010-06-07 14:28:16 +00001557#-------------------------------------------------------------------------
1558#
1559proc slave_test_script {script} {
1560
1561 # Create the interpreter used to run the test script.
1562 interp create tinterp
1563
1564 # Populate some global variables that tester.tcl expects to see.
1565 foreach {var value} [list \
1566 ::argv0 $::argv0 \
1567 ::argv {} \
1568 ::SLAVE 1 \
1569 ] {
1570 interp eval tinterp [list set $var $value]
1571 }
1572
1573 # The alias used to access the global test counters.
1574 tinterp alias set_test_counter set_test_counter
1575
1576 # Set up the ::cmdlinearg array in the slave.
1577 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
1578
dan430e74c2010-06-07 17:47:26 +00001579 # Set up the ::G array in the slave.
1580 interp eval tinterp [list array set ::G [array get ::G]]
1581
danc1a60c52010-06-07 14:28:16 +00001582 # Load the various test interfaces implemented in C.
1583 load_testfixture_extensions tinterp
1584
1585 # Run the test script.
1586 interp eval tinterp $script
1587
danea5542d2010-07-06 11:26:15 +00001588 # Check if the interpreter call [run_thread_tests]
1589 if { [interp eval tinterp {info exists ::run_thread_tests_called}] } {
1590 set ::run_thread_tests_called 1
1591 }
1592
danc1a60c52010-06-07 14:28:16 +00001593 # Delete the interpreter used to run the test script.
1594 interp delete tinterp
1595}
1596
dan430e74c2010-06-07 17:47:26 +00001597proc slave_test_file {zFile} {
dan0626dfc2010-06-15 06:56:37 +00001598 set tail [file tail $zFile]
dan430e74c2010-06-07 17:47:26 +00001599
dan6a64d672011-04-04 15:38:16 +00001600 if {[info exists ::G(start:permutation)]} {
1601 if {[permutation] != $::G(start:permutation)} return
1602 unset ::G(start:permutation)
1603 }
1604 if {[info exists ::G(start:file)]} {
1605 if {$tail != $::G(start:file) && $tail!="$::G(start:file).test"} return
1606 unset ::G(start:file)
1607 }
1608
dan92d516a2010-07-05 14:54:48 +00001609 # Remember the value of the shared-cache setting. So that it is possible
1610 # to check afterwards that it was not modified by the test script.
1611 #
1612 ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] }
dan0626dfc2010-06-15 06:56:37 +00001613
dan92d516a2010-07-05 14:54:48 +00001614 # Run the test script in a slave interpreter.
1615 #
danea5542d2010-07-06 11:26:15 +00001616 unset -nocomplain ::run_thread_tests_called
dan0626dfc2010-06-15 06:56:37 +00001617 reset_prng_state
1618 set ::sqlite_open_file_count 0
1619 set time [time { slave_test_script [list source $zFile] }]
1620 set ms [expr [lindex $time 0] / 1000]
1621
dan92d516a2010-07-05 14:54:48 +00001622 # Test that all files opened by the test script were closed. Omit this
1623 # if the test script has "thread" in its name. The open file counter
1624 # is not thread-safe.
dan0626dfc2010-06-15 06:56:37 +00001625 #
danea5542d2010-07-06 11:26:15 +00001626 if {[info exists ::run_thread_tests_called]==0} {
dan92d516a2010-07-05 14:54:48 +00001627 do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0}
1628 }
dan430e74c2010-06-07 17:47:26 +00001629 set ::sqlite_open_file_count 0
1630
dan0626dfc2010-06-15 06:56:37 +00001631 # Test that the global "shared-cache" setting was not altered by
1632 # the test script.
1633 #
1634 ifcapable shared_cache {
1635 set res [expr {[sqlite3_enable_shared_cache] == $scs}]
1636 do_test ${tail}-sharedcachesetting [list set {} $res] 1
1637 }
dan430e74c2010-06-07 17:47:26 +00001638
dan92d516a2010-07-05 14:54:48 +00001639 # Add some info to the output.
1640 #
dan0626dfc2010-06-15 06:56:37 +00001641 puts "Time: $tail $ms ms"
dan430e74c2010-06-07 17:47:26 +00001642 show_memstats
danc1a60c52010-06-07 14:28:16 +00001643}
1644
dan59257dc2010-08-04 11:34:31 +00001645# Open a new connection on database test.db and execute the SQL script
1646# supplied as an argument. Before returning, close the new conection and
1647# restore the 4 byte fields starting at header offsets 28, 92 and 96
1648# to the values they held before the SQL was executed. This simulates
1649# a write by a pre-3.7.0 client.
1650#
1651proc sql36231 {sql} {
1652 set B [hexio_read test.db 92 8]
1653 set A [hexio_read test.db 28 4]
1654 sqlite3 db36231 test.db
1655 catch { db36231 func a_string a_string }
1656 execsql $sql db36231
1657 db36231 close
1658 hexio_write test.db 28 $A
1659 hexio_write test.db 92 $B
1660 return ""
1661}
danf5894502009-10-07 18:41:19 +00001662
danccc7ff42010-11-29 12:06:45 +00001663proc db_save {} {
1664 foreach f [glob -nocomplain sv_test.db*] { forcedelete $f }
1665 foreach f [glob -nocomplain test.db*] {
1666 set f2 "sv_$f"
mistachkinfda06be2011-08-02 00:57:34 +00001667 forcecopy $f $f2
danccc7ff42010-11-29 12:06:45 +00001668 }
1669}
1670proc db_save_and_close {} {
1671 db_save
1672 catch { db close }
1673 return ""
1674}
1675proc db_restore {} {
1676 foreach f [glob -nocomplain test.db*] { forcedelete $f }
1677 foreach f2 [glob -nocomplain sv_test.db*] {
1678 set f [string range $f2 3 end]
mistachkinfda06be2011-08-02 00:57:34 +00001679 forcecopy $f2 $f
danccc7ff42010-11-29 12:06:45 +00001680 }
1681}
1682proc db_restore_and_reopen {{dbfile test.db}} {
1683 catch { db close }
1684 db_restore
1685 sqlite3 db $dbfile
1686}
1687proc db_delete_and_reopen {{file test.db}} {
1688 catch { db close }
mistachkinfda06be2011-08-02 00:57:34 +00001689 foreach f [glob -nocomplain test.db*] { forcedelete $f }
danccc7ff42010-11-29 12:06:45 +00001690 sqlite3 db $file
1691}
1692
danielk197745901d62004-11-10 15:27:38 +00001693# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
1694# to non-zero, then set the global variable $AUTOVACUUM to 1.
1695set AUTOVACUUM $sqlite_options(default_autovacuum)
danielk1977ee8b7992009-03-26 17:13:06 +00001696
dan64b95bb2012-05-12 05:30:29 +00001697# Make sure the FTS enhanced query syntax is disabled.
1698set sqlite_fts3_enable_parentheses 0
1699
danielk1977ee8b7992009-03-26 17:13:06 +00001700source $testdir/thread_common.tcl
danddf80eb2010-10-25 12:47:43 +00001701source $testdir/malloc_common.tcl