blob: f36084078987fc49d903402a11e79c532c2bdc4b [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
126 # arguments through to the C implemenation as the are.
127 #
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]
465
466 proc set_test_counter {counter args} {
467 if {[llength $args]} {
468 set ::TC($counter) [lindex $args 0]
469 }
470 set ::TC($counter)
471 }
drhb62c3352006-11-23 09:39:16 +0000472}
drh348784e2000-05-29 20:41:49 +0000473
drh521cc842008-04-15 02:36:33 +0000474# Record the fact that a sequence of tests were omitted.
475#
mistachkin6c3c1a02011-11-12 03:17:40 +0000476proc omit_test {name reason {append 1}} {
danc1a60c52010-06-07 14:28:16 +0000477 set omitList [set_test_counter omit_list]
mistachkin6c3c1a02011-11-12 03:17:40 +0000478 if {$append} {
479 lappend omitList [list $name $reason]
480 }
danc1a60c52010-06-07 14:28:16 +0000481 set_test_counter omit_list $omitList
drh521cc842008-04-15 02:36:33 +0000482}
483
danc1a60c52010-06-07 14:28:16 +0000484# Record the fact that a test failed.
485#
486proc fail_test {name} {
487 set f [set_test_counter fail_list]
488 lappend f $name
489 set_test_counter fail_list $f
490 set_test_counter errors [expr [set_test_counter errors] + 1]
491
492 set nFail [set_test_counter errors]
493 if {$nFail>=$::cmdlinearg(maxerror)} {
494 puts "*** Giving up..."
495 finalize_testing
496 }
497}
498
499# Increment the number of tests run
500#
501proc incr_ntest {} {
502 set_test_counter count [expr [set_test_counter count] + 1]
503}
504
505
drh348784e2000-05-29 20:41:49 +0000506# Invoke the do_test procedure to run a single test
507#
508proc do_test {name cmd expected} {
danc1a60c52010-06-07 14:28:16 +0000509 global argv cmdlinearg
510
dan8c408002010-11-01 17:38:24 +0000511 fix_testname name
512
drh4a50aac2007-08-23 02:47:53 +0000513 sqlite3_memdebug_settitle $name
danc1a60c52010-06-07 14:28:16 +0000514
dan92d516a2010-07-05 14:54:48 +0000515# if {[llength $argv]==0} {
516# set go 1
517# } else {
518# set go 0
519# foreach pattern $argv {
520# if {[string match $pattern $name]} {
521# set go 1
522# break
523# }
524# }
525# }
dan430e74c2010-06-07 17:47:26 +0000526
dand506de02010-07-03 13:59:01 +0000527 if {[info exists ::G(perm:prefix)]} {
528 set name "$::G(perm:prefix)$name"
dan430e74c2010-06-07 17:47:26 +0000529 }
530
danc1a60c52010-06-07 14:28:16 +0000531 incr_ntest
drh5edc3122001-09-13 21:53:09 +0000532 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000533 flush stdout
mistachkin6c3c1a02011-11-12 03:17:40 +0000534
535 if {![info exists ::G(match)] || [string match $::G(match) $name]} {
536 if {[catch {uplevel #0 "$cmd;\n"} result]} {
537 puts "\nError: $result"
538 fail_test $name
mistachkin6c3c1a02011-11-12 03:17:40 +0000539 } else {
drh3f17aef2012-04-27 01:08:02 +0000540 if {[regexp {^~?/.*/$} $expected]} {
541 if {[string index $expected 0]=="~"} {
drh5343b2d2012-09-27 19:53:38 +0000542 set re [string map {# {[-0-9.]+}} [string range $expected 2 end-1]]
drh3f17aef2012-04-27 01:08:02 +0000543 set ok [expr {![regexp $re $result]}]
544 } else {
drh5343b2d2012-09-27 19:53:38 +0000545 set re [string map {# {[-0-9.]+}} [string range $expected 1 end-1]]
drh3f17aef2012-04-27 01:08:02 +0000546 set ok [regexp $re $result]
547 }
548 } else {
549 set ok [expr {[string compare $result $expected]==0}]
550 }
551 if {!$ok} {
mistachkinc60941f2012-09-13 01:51:02 +0000552 # if {![info exists ::testprefix] || $::testprefix eq ""} {
553 # error "no test prefix"
554 # }
drh3f17aef2012-04-27 01:08:02 +0000555 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
556 fail_test $name
557 } else {
558 puts " Ok"
559 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000560 }
drh348784e2000-05-29 20:41:49 +0000561 } else {
mistachkin6c3c1a02011-11-12 03:17:40 +0000562 puts " Omitted"
563 omit_test $name "pattern mismatch" 0
drh348784e2000-05-29 20:41:49 +0000564 }
drh6558db82007-04-13 03:23:21 +0000565 flush stdout
drh348784e2000-05-29 20:41:49 +0000566}
dana3f51082010-09-30 18:43:14 +0000567
drh8df91852012-04-24 12:46:05 +0000568proc catchcmd {db {cmd ""}} {
569 global CLI
570 set out [open cmds.txt w]
571 puts $out $cmd
572 close $out
573 set line "exec $CLI $db < cmds.txt"
574 set rc [catch { eval $line } msg]
575 list $rc $msg
576}
577
shaneh55505172011-06-21 19:30:19 +0000578proc filepath_normalize {p} {
579 # test cases should be written to assume "unix"-like file paths
shaneh285a18f2011-06-21 19:39:59 +0000580 if {$::tcl_platform(platform)!="unix"} {
shaneh55505172011-06-21 19:30:19 +0000581 # lreverse*2 as a hack to remove any unneeded {} after the string map
582 lreverse [lreverse [string map {\\ /} [regsub -nocase -all {[a-z]:[/\\]+} $p {/}]]]
shanehc4896402011-06-21 19:38:16 +0000583 } {
584 set p
shaneh55505172011-06-21 19:30:19 +0000585 }
586}
587proc do_filepath_test {name cmd expected} {
588 uplevel [list do_test $name [
589 subst -nocommands { filepath_normalize [ $cmd ] }
590 ] [filepath_normalize $expected]]
591}
592
dan33f53792011-05-05 19:44:22 +0000593proc realnum_normalize {r} {
shaneh4f529e82011-06-20 17:41:41 +0000594 # different TCL versions display floating point values differently.
595 string map {1.#INF inf Inf inf .0e e} [regsub -all {(e[+-])0+} $r {\1}]
dan33f53792011-05-05 19:44:22 +0000596}
597proc do_realnum_test {name cmd expected} {
598 uplevel [list do_test $name [
599 subst -nocommands { realnum_normalize [ $cmd ] }
600 ] [realnum_normalize $expected]]
601}
602
dana3f51082010-09-30 18:43:14 +0000603proc fix_testname {varname} {
604 upvar $varname testname
605 if {[info exists ::testprefix]
606 && [string is digit [string range $testname 0 0]]
607 } {
608 set testname "${::testprefix}-$testname"
609 }
610}
dan153eda02010-06-21 07:45:47 +0000611
dan57f7f4b2010-10-01 19:04:37 +0000612proc do_execsql_test {testname sql {result {}}} {
dana3f51082010-09-30 18:43:14 +0000613 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000614 uplevel do_test [list $testname] [list "execsql {$sql}"] [list [list {*}$result]]
dan153eda02010-06-21 07:45:47 +0000615}
616proc do_catchsql_test {testname sql result} {
dana3f51082010-09-30 18:43:14 +0000617 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000618 uplevel do_test [list $testname] [list "catchsql {$sql}"] [list $result]
dan153eda02010-06-21 07:45:47 +0000619}
dan39854792010-11-15 16:12:58 +0000620proc do_eqp_test {name sql res} {
621 uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res]
622}
dan153eda02010-06-21 07:45:47 +0000623
dan51f06982010-09-18 19:00:12 +0000624#-------------------------------------------------------------------------
625# Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST
626#
627# Where switches are:
628#
629# -errorformat FMTSTRING
630# -count
danaf1dcab2010-09-20 19:17:53 +0000631# -query SQL
dana16d1062010-09-28 17:37:28 +0000632# -tclquery TCL
danaf7626f2010-09-23 18:47:36 +0000633# -repair TCL
dan51f06982010-09-18 19:00:12 +0000634#
635proc do_select_tests {prefix args} {
636
637 set testlist [lindex $args end]
638 set switches [lrange $args 0 end-1]
639
640 set errfmt ""
641 set countonly 0
dana16d1062010-09-28 17:37:28 +0000642 set tclquery ""
danaf7626f2010-09-23 18:47:36 +0000643 set repair ""
dan51f06982010-09-18 19:00:12 +0000644
645 for {set i 0} {$i < [llength $switches]} {incr i} {
646 set s [lindex $switches $i]
647 set n [string length $s]
danaf1dcab2010-09-20 19:17:53 +0000648 if {$n>=2 && [string equal -length $n $s "-query"]} {
dana16d1062010-09-28 17:37:28 +0000649 set tclquery [list execsql [lindex $switches [incr i]]]
650 } elseif {$n>=2 && [string equal -length $n $s "-tclquery"]} {
651 set tclquery [lindex $switches [incr i]]
danaf1dcab2010-09-20 19:17:53 +0000652 } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} {
dan51f06982010-09-18 19:00:12 +0000653 set errfmt [lindex $switches [incr i]]
danaf7626f2010-09-23 18:47:36 +0000654 } elseif {$n>=2 && [string equal -length $n $s "-repair"]} {
655 set repair [lindex $switches [incr i]]
dan51f06982010-09-18 19:00:12 +0000656 } elseif {$n>=2 && [string equal -length $n $s "-count"]} {
657 set countonly 1
658 } else {
659 error "unknown switch: $s"
660 }
661 }
662
663 if {$countonly && $errfmt!=""} {
664 error "Cannot use -count and -errorformat together"
665 }
666 set nTestlist [llength $testlist]
667 if {$nTestlist%3 || $nTestlist==0 } {
668 error "SELECT test list contains [llength $testlist] elements"
669 }
670
danaf7626f2010-09-23 18:47:36 +0000671 eval $repair
dan51f06982010-09-18 19:00:12 +0000672 foreach {tn sql res} $testlist {
dana16d1062010-09-28 17:37:28 +0000673 if {$tclquery != ""} {
danaf1dcab2010-09-20 19:17:53 +0000674 execsql $sql
dana16d1062010-09-28 17:37:28 +0000675 uplevel do_test ${prefix}.$tn [list $tclquery] [list [list {*}$res]]
676 } elseif {$countonly} {
dan51f06982010-09-18 19:00:12 +0000677 set nRow 0
678 db eval $sql {incr nRow}
679 uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res]
680 } elseif {$errfmt==""} {
681 uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]]
682 } else {
683 set res [list 1 [string trim [format $errfmt {*}$res]]]
684 uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res]
685 }
danaf7626f2010-09-23 18:47:36 +0000686 eval $repair
dan51f06982010-09-18 19:00:12 +0000687 }
danaf7626f2010-09-23 18:47:36 +0000688
dan51f06982010-09-18 19:00:12 +0000689}
690
danb04757a2010-09-21 16:59:16 +0000691proc delete_all_data {} {
692 db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} {
693 db eval "DELETE FROM '[string map {' ''} $t]'"
694 }
695}
drh348784e2000-05-29 20:41:49 +0000696
drhb62c3352006-11-23 09:39:16 +0000697# Run an SQL script.
698# Return the number of microseconds per statement.
699#
drh3590f152006-11-23 21:09:10 +0000700proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000701 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000702 flush stdout
703 set speed [time {sqlite3_exec_nr db $sql}]
704 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000705 if {$tm == 0} {
706 set rate [format %20s "many"]
707 } else {
708 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
709 }
drh3590f152006-11-23 21:09:10 +0000710 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000711 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000712 global total_time
713 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000714 lappend ::speed_trial_times $name $tm
drhdd924312007-03-31 22:34:16 +0000715}
drh45c236d2008-03-22 01:08:00 +0000716proc speed_trial_tcl {name numstmt units script} {
717 puts -nonewline [format {%-21.21s } $name...]
718 flush stdout
719 set speed [time {eval $script}]
720 set tm [lindex $speed 0]
721 if {$tm == 0} {
722 set rate [format %20s "many"]
723 } else {
724 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
725 }
726 set u2 $units/s
727 puts [format {%12d uS %s %s} $tm $rate $u2]
728 global total_time
729 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000730 lappend ::speed_trial_times $name $tm
drh45c236d2008-03-22 01:08:00 +0000731}
drhdd924312007-03-31 22:34:16 +0000732proc speed_trial_init {name} {
733 global total_time
734 set total_time 0
dana975b592010-10-08 16:09:43 +0000735 set ::speed_trial_times [list]
drhbb810a92010-07-03 12:00:53 +0000736 sqlite3 versdb :memory:
737 set vers [versdb one {SELECT sqlite_source_id()}]
738 versdb close
739 puts "SQLite $vers"
drhdd924312007-03-31 22:34:16 +0000740}
741proc speed_trial_summary {name} {
742 global total_time
743 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
dana975b592010-10-08 16:09:43 +0000744
745 if { 0 } {
746 sqlite3 versdb :memory:
747 set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0]
748 versdb close
749 puts "CREATE TABLE IF NOT EXISTS time(version, script, test, us);"
750 foreach {test us} $::speed_trial_times {
751 puts "INSERT INTO time VALUES('$vers', '$name', '$test', $us);"
752 }
753 }
drhb62c3352006-11-23 09:39:16 +0000754}
755
drh348784e2000-05-29 20:41:49 +0000756# Run this routine last
757#
758proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +0000759 catch {db close}
760 catch {db2 close}
761 catch {db3 close}
762 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +0000763}
764proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +0000765 global sqlite_open_file_count
766
767 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +0000768
drh6e142f52000-06-08 13:36:40 +0000769 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000770 catch {db2 close}
771 catch {db3 close}
772
drh9bc54492007-10-23 14:49:59 +0000773 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000774 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000775 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000776 db close
drh984bfaa2008-03-19 16:08:53 +0000777 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +0000778
drh3a7fb7c2007-08-10 16:41:08 +0000779 sqlite3_soft_heap_limit 0
danc1a60c52010-06-07 14:28:16 +0000780 set nTest [incr_ntest]
781 set nErr [set_test_counter errors]
782
drh348784e2000-05-29 20:41:49 +0000783 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000784 if {$nErr>0} {
danc1a60c52010-06-07 14:28:16 +0000785 puts "Failures on these tests: [set_test_counter fail_list]"
drhf3a65f72007-08-22 20:18:21 +0000786 }
danielk1977ee8b7992009-03-26 17:13:06 +0000787 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +0000788 if {[llength $omitList]>0} {
789 puts "Omitted test cases:"
790 set prec {}
791 foreach {rec} [lsort $omitList] {
792 if {$rec==$prec} continue
793 set prec $rec
794 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
795 }
796 }
drh80788d82006-09-02 14:50:23 +0000797 if {$nErr>0 && ![working_64bit_int]} {
798 puts "******************************************************************"
799 puts "N.B.: The version of TCL that you used to build this test harness"
800 puts "is defective in that it does not support 64-bit integers. Some or"
801 puts "all of the test failures above might be a result from this defect"
802 puts "in your TCL build."
803 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000804 }
danc1a60c52010-06-07 14:28:16 +0000805 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000806 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +0000807 }
drh94e92032003-02-16 22:21:32 +0000808 if {$sqlite_open_file_count} {
809 puts "$sqlite_open_file_count files were left open"
810 incr nErr
811 }
drheafc43b2010-07-26 18:43:40 +0000812 if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 ||
813 [sqlite3_memory_used]>0} {
814 puts "Unfreed memory: [sqlite3_memory_used] bytes in\
815 [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations"
drhf3a65f72007-08-22 20:18:21 +0000816 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000817 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000818 puts "Writing unfreed memory log to \"./memleak.txt\""
819 sqlite3_memdebug_dump ./memleak.txt
820 }
drhf3a65f72007-08-22 20:18:21 +0000821 } else {
822 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000823 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000824 sqlite3_memdebug_dump ./memusage.txt
825 }
drhf3a65f72007-08-22 20:18:21 +0000826 }
drhf7141992008-06-19 00:16:08 +0000827 show_memstats
drh91fd4d42008-01-19 20:11:25 +0000828 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
829 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000830 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
831 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
832 }
danc1a60c52010-06-07 14:28:16 +0000833 if {$::cmdlinearg(malloctrace)} {
danielk197735754ac2008-03-21 17:29:37 +0000834 puts "Writing mallocs.sql..."
835 memdebug_log_sql
836 sqlite3_memdebug_log stop
837 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000838
839 if {[sqlite3_memory_used]>0} {
840 puts "Writing leaks.sql..."
841 sqlite3_memdebug_log sync
842 memdebug_log_sql leaks.sql
843 }
danielk197735754ac2008-03-21 17:29:37 +0000844 }
drhd9910fe2006-10-04 11:55:49 +0000845 foreach f [glob -nocomplain test.db-*-journal] {
mistachkinfda06be2011-08-02 00:57:34 +0000846 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +0000847 }
848 foreach f [glob -nocomplain test.db-mj*] {
mistachkinfda06be2011-08-02 00:57:34 +0000849 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +0000850 }
drhdb25e382001-03-15 18:21:22 +0000851 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000852}
853
drhf7141992008-06-19 00:16:08 +0000854# Display memory statistics for analysis and debugging purposes.
855#
856proc show_memstats {} {
857 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +0000858 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
859 set val [format {now %10d max %10d max-size %10d} \
860 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000861 puts "Memory used: $val"
drheafc43b2010-07-26 18:43:40 +0000862 set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0]
863 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
864 puts "Allocation count: $val"
drhf7141992008-06-19 00:16:08 +0000865 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +0000866 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
867 set val [format {now %10d max %10d max-size %10d} \
868 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000869 puts "Page-cache used: $val"
870 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
871 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
872 puts "Page-cache overflow: $val"
873 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
874 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
875 puts "Scratch memory used: $val"
876 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +0000877 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
878 set val [format {now %10d max %10d max-size %10d} \
879 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000880 puts "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +0000881 ifcapable yytrackmaxstackdepth {
882 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
883 set val [format { max %10d} [lindex $x 2]]
884 puts "Parser stack depth: $val"
885 }
drhf7141992008-06-19 00:16:08 +0000886}
887
drh348784e2000-05-29 20:41:49 +0000888# A procedure to execute SQL
889#
drhc4a3c772001-04-04 11:48:57 +0000890proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000891 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000892 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000893}
drh3aadb2e2000-05-31 17:59:25 +0000894
drhadbca9c2001-09-27 15:11:53 +0000895# Execute SQL and catch exceptions.
896#
897proc catchsql {sql {db db}} {
898 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +0000899 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +0000900 lappend r $msg
901 return $r
902}
903
drh04096482001-11-09 22:41:44 +0000904# Do an VDBE code dump on the SQL given
905#
906proc explain {sql {db db}} {
907 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000908 puts "addr opcode p1 p2 p3 p4 p5 #"
909 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000910 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000911 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
912 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
913 ]
drh04096482001-11-09 22:41:44 +0000914 }
915}
916
drhdafc0ce2008-04-17 19:14:02 +0000917# Show the VDBE program for an SQL statement but omit the Trace
918# opcode at the beginning. This procedure can be used to prove
919# that different SQL statements generate exactly the same VDBE code.
920#
921proc explain_no_trace {sql} {
922 set tr [db eval "EXPLAIN $sql"]
923 return [lrange $tr 7 end]
924}
925
drh3aadb2e2000-05-31 17:59:25 +0000926# Another procedure to execute SQL. This one includes the field
927# names in the returned list.
928#
929proc execsql2 {sql} {
930 set result {}
931 db eval $sql data {
932 foreach f $data(*) {
933 lappend result $f $data($f)
934 }
935 }
936 return $result
937}
drh17a68932001-01-31 13:28:08 +0000938
drhdde85d92003-03-01 19:45:34 +0000939# Use the non-callback API to execute multiple SQL statements
940#
941proc stepsql {dbptr sql} {
942 set sql [string trim $sql]
943 set r 0
944 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000945 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000946 return [list 1 $vm]
947 }
948 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000949# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
950# foreach v $VAL {lappend r $v}
951# }
952 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
953 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
954 lappend r [sqlite3_column_text $vm $i]
955 }
drhdde85d92003-03-01 19:45:34 +0000956 }
danielk1977106bb232004-05-21 10:08:53 +0000957 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000958 return [list 1 $errmsg]
959 }
960 }
961 return $r
962}
963
drh21504322002-06-25 13:16:02 +0000964# Do an integrity check of the entire database
965#
danielk197704103022009-02-03 16:51:24 +0000966proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +0000967 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +0000968 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +0000969 }
970}
971
drh433dccf2013-02-09 15:37:11 +0000972# Check the extended error code
973#
974proc verify_ex_errcode {name expected {db db}} {
975 do_test $name [list sqlite3_extended_errcode $db] $expected
976}
977
danc6055c72011-04-28 18:46:46 +0000978
979# Return true if the SQL statement passed as the second argument uses a
980# statement transaction.
981#
982proc sql_uses_stmt {db sql} {
983 set stmt [sqlite3_prepare $db $sql -1 dummy]
984 set uses [uses_stmt_journal $stmt]
985 sqlite3_finalize $stmt
986 return $uses
987}
988
danielk1977db4e8862008-01-16 08:24:46 +0000989proc fix_ifcapable_expr {expr} {
990 set ret ""
991 set state 0
992 for {set i 0} {$i < [string length $expr]} {incr i} {
993 set char [string range $expr $i $i]
994 set newstate [expr {[string is alnum $char] || $char eq "_"}]
995 if {$newstate && !$state} {
996 append ret {$::sqlite_options(}
997 }
998 if {!$newstate && $state} {
999 append ret )
1000 }
1001 append ret $char
1002 set state $newstate
1003 }
1004 if {$state} {append ret )}
1005 return $ret
1006}
1007
mistachkinc60941f2012-09-13 01:51:02 +00001008# Returns non-zero if the capabilities are present; zero otherwise.
1009#
1010proc capable {expr} {
1011 set e [fix_ifcapable_expr $expr]; return [expr ($e)]
1012}
1013
drh27d258a2004-10-30 20:23:09 +00001014# Evaluate a boolean expression of capabilities. If true, execute the
1015# code. Omit the code if false.
1016#
danielk19773e8c37e2005-01-21 03:12:14 +00001017proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +00001018 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
1019 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +00001020 if ($e2) {
1021 set c [catch {uplevel 1 $code} r]
1022 } else {
1023 set c [catch {uplevel 1 $elsecode} r]
1024 }
1025 return -code $c $r
drh21504322002-06-25 13:16:02 +00001026}
danielk197745901d62004-11-10 15:27:38 +00001027
danielk1977aca790a2005-01-13 11:07:52 +00001028# This proc execs a seperate process that crashes midway through executing
1029# the SQL script $sql on database test.db.
1030#
1031# The crash occurs during a sync() of file $crashfile. When the crash
1032# occurs a random subset of all unsynced writes made by the process are
1033# written into the files on disk. Argument $crashdelay indicates the
1034# number of file syncs to wait before crashing.
1035#
1036# The return value is a list of two elements. The first element is a
1037# boolean, indicating whether or not the process actually crashed or
1038# reported some other error. The second element in the returned list is the
1039# error message. This is "child process exited abnormally" if the crash
1040# occured.
1041#
danielk1977f8940ae2007-08-23 11:07:10 +00001042# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +00001043#
1044proc crashsql {args} {
danielk197759a33f92007-03-17 10:26:59 +00001045
1046 set blocksize ""
1047 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +00001048 set prngseed 0
drhf8587402008-01-08 16:03:49 +00001049 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +00001050 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +00001051 set dc ""
danielk197759a33f92007-03-17 10:26:59 +00001052 set sql [lindex $args end]
1053
1054 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
1055 set z [lindex $args $ii]
1056 set n [string length $z]
1057 set z2 [lindex $args [expr $ii+1]]
1058
1059 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +00001060 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +00001061 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +00001062 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +00001063 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +00001064 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +00001065 else { error "Unrecognized option: $z" }
1066 }
1067
1068 if {$crashfile eq ""} {
1069 error "Compulsory option -file missing"
1070 }
1071
shanehf2c08822010-07-08 16:30:44 +00001072 # $crashfile gets compared to the native filename in
1073 # cfSync(), which can be different then what TCL uses by
1074 # default, so here we force it to the "nativename" format.
mistachkinf8a78462012-03-08 20:00:36 +00001075 set cfile [string map {\\ \\\\} [file nativename [file join [get_pwd] $crashfile]]]
danielk1977aca790a2005-01-13 11:07:52 +00001076
1077 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +00001078 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +00001079 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +00001080 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +00001081 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +00001082
1083 # This block sets the cache size of the main database to 10
1084 # pages. This is done in case the build is configured to omit
1085 # "PRAGMA cache_size".
1086 puts $f {db eval {SELECT * FROM sqlite_master;}}
1087 puts $f {set bt [btree_from_db db]}
1088 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +00001089 if {$prngseed} {
1090 set seed [expr {$prngseed%10007+1}]
1091 # puts seed=$seed
1092 puts $f "db eval {SELECT randomblob($seed)}"
1093 }
danielk1977933bbd62007-03-17 07:22:42 +00001094
drhf8587402008-01-08 16:03:49 +00001095 if {[string length $tclbody]>0} {
1096 puts $f $tclbody
1097 }
1098 if {[string length $sql]>0} {
1099 puts $f "db eval {"
1100 puts $f "$sql"
1101 puts $f "}"
1102 }
danielk1977aca790a2005-01-13 11:07:52 +00001103 close $f
danielk1977aca790a2005-01-13 11:07:52 +00001104 set r [catch {
drh9c06c952005-11-26 00:25:00 +00001105 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +00001106 } msg]
shanehf2c08822010-07-08 16:30:44 +00001107
1108 # Windows/ActiveState TCL returns a slightly different
1109 # error message. We map that to the expected message
1110 # so that we don't have to change all of the test
1111 # cases.
1112 if {$::tcl_platform(platform)=="windows"} {
1113 if {$msg=="child killed: unknown signal"} {
1114 set msg "child process exited abnormally"
1115 }
1116 }
1117
danielk1977aca790a2005-01-13 11:07:52 +00001118 lappend r $msg
1119}
1120
danb88e24f2013-02-23 18:58:11 +00001121proc run_ioerr_prep {} {
1122 set ::sqlite_io_error_pending 0
1123 catch {db close}
1124 catch {db2 close}
1125 catch {forcedelete test.db}
1126 catch {forcedelete test.db-journal}
1127 catch {forcedelete test2.db}
1128 catch {forcedelete test2.db-journal}
1129 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
1130 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
1131 if {[info exists ::ioerropts(-tclprep)]} {
1132 eval $::ioerropts(-tclprep)
1133 }
1134 if {[info exists ::ioerropts(-sqlprep)]} {
1135 execsql $::ioerropts(-sqlprep)
1136 }
1137 expr 0
1138}
1139
danielk197732554c12005-01-22 03:39:39 +00001140# Usage: do_ioerr_test <test number> <options...>
1141#
1142# This proc is used to implement test cases that check that IO errors
1143# are correctly handled. The first argument, <test number>, is an integer
1144# used to name the tests executed by this proc. Options are as follows:
1145#
1146# -tclprep TCL script to run to prepare test.
1147# -sqlprep SQL script to run to prepare test.
1148# -tclbody TCL script to run with IO error simulation.
1149# -sqlbody TCL script to run with IO error simulation.
1150# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +00001151# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +00001152# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +00001153# -start Value of 'N' to begin with (default 1)
1154#
1155# -cksum Boolean. If true, test that the database does
1156# not change during the execution of the test case.
1157#
1158proc do_ioerr_test {testname args} {
1159
danielk197732554c12005-01-22 03:39:39 +00001160 set ::ioerropts(-start) 1
1161 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +00001162 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +00001163 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +00001164 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +00001165 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +00001166 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +00001167 array set ::ioerropts $args
1168
danielk197747cd39c2008-05-12 12:41:15 +00001169 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
1170 # a couple of obscure IO errors that do not return them.
1171 set ::ioerropts(-erc) 0
danb88e24f2013-02-23 18:58:11 +00001172
1173 # Create a single TCL script from the TCL and SQL specified
1174 # as the body of the test.
1175 set ::ioerrorbody {}
1176 if {[info exists ::ioerropts(-tclbody)]} {
1177 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
1178 }
1179 if {[info exists ::ioerropts(-sqlbody)]} {
1180 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
1181 }
1182
1183 save_prng_state
1184 if {$::ioerropts(-cksum)} {
1185 run_ioerr_prep
1186 eval $::ioerrorbody
1187 set ::goodcksum [cksum]
1188 }
danielk197747cd39c2008-05-12 12:41:15 +00001189
danielk197732554c12005-01-22 03:39:39 +00001190 set ::go 1
drh93aed5a2008-01-16 17:46:38 +00001191 #reset_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +00001192 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +00001193 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +00001194 incr ::ioerropts(-count) -1
1195 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +00001196
1197 # Skip this IO error if it was specified with the "-exclude" option.
1198 if {[info exists ::ioerropts(-exclude)]} {
1199 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
1200 }
danielk1977861f7452008-06-05 11:39:11 +00001201 if {$::ioerropts(-restoreprng)} {
1202 restore_prng_state
1203 }
danielk197732554c12005-01-22 03:39:39 +00001204
1205 # Delete the files test.db and test2.db, then execute the TCL and
1206 # SQL (in that order) to prepare for the test case.
1207 do_test $testname.$n.1 {
danb88e24f2013-02-23 18:58:11 +00001208 run_ioerr_prep
danielk197732554c12005-01-22 03:39:39 +00001209 } {0}
1210
1211 # Read the 'checksum' of the database.
1212 if {$::ioerropts(-cksum)} {
danb88e24f2013-02-23 18:58:11 +00001213 set ::checksum [cksum]
danielk197732554c12005-01-22 03:39:39 +00001214 }
drh93aed5a2008-01-16 17:46:38 +00001215
danielk197732554c12005-01-22 03:39:39 +00001216 # Set the Nth IO error to fail.
1217 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +00001218 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +00001219 set ::sqlite_io_error_pending $n
1220 }] $n
danielk197732554c12005-01-22 03:39:39 +00001221
danb88e24f2013-02-23 18:58:11 +00001222 # Execute the TCL script created for the body of this test. If
1223 # at least N IO operations performed by SQLite as a result of
1224 # the script, the Nth will fail.
danielk197732554c12005-01-22 03:39:39 +00001225 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +00001226 set ::sqlite_io_error_hit 0
1227 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +00001228 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +00001229 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +00001230 set rc [sqlite3_errcode $::DB]
1231 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +00001232 # If we are in extended result code mode, make sure all of the
1233 # IOERRs we get back really do have their extended code values.
1234 # If an extended result code is returned, the sqlite3_errcode
1235 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
1236 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +00001237 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
1238 return $rc
1239 }
1240 } else {
drh5f7b5bf2007-04-19 12:30:54 +00001241 # If we are not in extended result code mode, make sure no
1242 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +00001243 if {[regexp {\+\d} $rc]} {
1244 return $rc
1245 }
1246 }
drh93aed5a2008-01-16 17:46:38 +00001247 # The test repeats as long as $::go is non-zero. $::go starts out
1248 # as 1. When a test runs to completion without hitting an I/O
1249 # error, that means there is no point in continuing with this test
1250 # case so set $::go to zero.
1251 #
1252 if {$::sqlite_io_error_pending>0} {
1253 set ::go 0
1254 set q 0
1255 set ::sqlite_io_error_pending 0
1256 } else {
1257 set q 1
1258 }
1259
drhc9ac5ca2005-11-04 22:03:30 +00001260 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +00001261 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
1262 set r 1
1263 }
danielk1977f2fa8312006-01-24 13:09:33 +00001264 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +00001265
1266 # One of two things must have happened. either
1267 # 1. We never hit the IO error and the SQL returned OK
1268 # 2. An IO error was hit and the SQL failed
1269 #
dand41a29a2010-05-06 15:56:28 +00001270 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +00001271 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +00001272 } {1}
1273
danielk197780daec62008-05-12 10:57:02 +00001274 set ::sqlite_io_error_hit 0
1275 set ::sqlite_io_error_pending 0
1276
danielk197752b472a2008-05-05 16:23:55 +00001277 # Check that no page references were leaked. There should be
1278 # a single reference if there is still an active transaction,
1279 # or zero otherwise.
1280 #
danielk197781620542008-06-07 05:19:37 +00001281 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
1282 # locks are established on database files (i.e. if the error
1283 # occurs while attempting to detect a hot-journal file), then
1284 # there may 0 page references and an active transaction according
1285 # to [sqlite3_get_autocommit].
1286 #
danielk19774abd5442008-05-05 15:26:50 +00001287 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +00001288 do_test $testname.$n.4 {
1289 set bt [btree_from_db db]
1290 db_enter db
1291 array set stats [btree_pager_stats $bt]
1292 db_leave db
danielk197781620542008-06-07 05:19:37 +00001293 set nRef $stats(ref)
1294 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
1295 } {1}
danielk19774abd5442008-05-05 15:26:50 +00001296 }
1297
danielk197752b472a2008-05-05 16:23:55 +00001298 # If there is an open database handle and no open transaction,
1299 # and the pager is not running in exclusive-locking mode,
1300 # check that the pager is in "unlocked" state. Theoretically,
1301 # if a call to xUnlock() failed due to an IO error the underlying
1302 # file may still be locked.
1303 #
1304 ifcapable pragma {
1305 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +00001306 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +00001307 && [db one {pragma locking_mode}] eq "normal"
1308 && [sqlite3_get_autocommit db]
1309 } {
1310 do_test $testname.$n.5 {
1311 set bt [btree_from_db db]
1312 db_enter db
1313 array set stats [btree_pager_stats $bt]
1314 db_leave db
1315 set stats(state)
1316 } 0
1317 }
1318 }
1319
danielk197732554c12005-01-22 03:39:39 +00001320 # If an IO error occured, then the checksum of the database should
1321 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +00001322 #
drh1aa5af12008-03-07 19:51:14 +00001323 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +00001324 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +00001325 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +00001326 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +00001327 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danb88e24f2013-02-23 18:58:11 +00001328 set nowcksum [cksum]
1329 set res [expr {$nowcksum==$::checksum || $nowcksum==$::goodcksum}]
1330 if {$res==0} {
1331 puts "now=$nowcksum"
1332 puts "the=$::checksum"
1333 puts "fwd=$::goodcksum"
1334 }
1335 set res
1336 } 1
danielk197732554c12005-01-22 03:39:39 +00001337 }
1338
drh1aa5af12008-03-07 19:51:14 +00001339 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +00001340 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +00001341 if {[info exists ::ioerropts(-cleanup)]} {
1342 catch $::ioerropts(-cleanup)
1343 }
danielk197732554c12005-01-22 03:39:39 +00001344 }
1345 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +00001346 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +00001347 unset ::ioerropts
1348}
1349
drh93aed5a2008-01-16 17:46:38 +00001350# Return a checksum based on the contents of the main database associated
1351# with connection $db
danielk197732554c12005-01-22 03:39:39 +00001352#
1353proc cksum {{db db}} {
1354 set txt [$db eval {
1355 SELECT name, type, sql FROM sqlite_master order by name
1356 }]\n
1357 foreach tbl [$db eval {
1358 SELECT name FROM sqlite_master WHERE type='table' order by name
1359 }] {
1360 append txt [$db eval "SELECT * FROM $tbl"]\n
1361 }
1362 foreach prag {default_synchronous default_cache_size} {
1363 append txt $prag-[$db eval "PRAGMA $prag"]\n
1364 }
1365 set cksum [string length $txt]-[md5 $txt]
1366 # puts $cksum-[file size test.db]
1367 return $cksum
1368}
1369
drh93aed5a2008-01-16 17:46:38 +00001370# Generate a checksum based on the contents of the main and temp tables
1371# database $db. If the checksum of two databases is the same, and the
1372# integrity-check passes for both, the two databases are identical.
1373#
drh1db639c2008-01-17 02:36:28 +00001374proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +00001375 set ret [list]
1376 ifcapable tempdb {
1377 set sql {
1378 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1379 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
1380 SELECT 'sqlite_master' UNION
1381 SELECT 'sqlite_temp_master' ORDER BY 1
1382 }
1383 } else {
1384 set sql {
1385 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1386 SELECT 'sqlite_master' ORDER BY 1
1387 }
1388 }
1389 set tbllist [$db eval $sql]
1390 set txt {}
1391 foreach tbl $tbllist {
1392 append txt [$db eval "SELECT * FROM $tbl"]
1393 }
1394 foreach prag {default_cache_size} {
1395 append txt $prag-[$db eval "PRAGMA $prag"]\n
1396 }
1397 # puts txt=$txt
1398 return [md5 $txt]
1399}
1400
drhdc2c4912009-02-04 22:46:47 +00001401# Generate a checksum based on the contents of a single database with
1402# a database connection. The name of the database is $dbname.
1403# Examples of $dbname are "temp" or "main".
1404#
1405proc dbcksum {db dbname} {
1406 if {$dbname=="temp"} {
1407 set master sqlite_temp_master
1408 } else {
1409 set master $dbname.sqlite_master
1410 }
1411 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
1412 set txt [$db eval "SELECT * FROM $master"]\n
1413 foreach tab $alltab {
1414 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
1415 }
1416 return [md5 $txt]
1417}
1418
danielk197735754ac2008-03-21 17:29:37 +00001419proc memdebug_log_sql {{filename mallocs.sql}} {
1420
danielk19776f332c12008-03-21 14:22:44 +00001421 set data [sqlite3_memdebug_log dump]
1422 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +00001423 if {$nFrame < 0} { return "" }
1424
danielk197735754ac2008-03-21 17:29:37 +00001425 set database temp
1426
danielk19776ab3a2e2009-02-19 14:39:25 +00001427 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +00001428
1429 set sql ""
1430 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +00001431 set nCall [lindex $e 0]
1432 set nByte [lindex $e 1]
1433 set lStack [lrange $e 2 end]
1434 append sql "INSERT INTO ${database}.malloc VALUES"
1435 append sql "('test', $nCall, $nByte, '$lStack');\n"
1436 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +00001437 set frames($f) 1
1438 }
1439 }
1440
1441 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +00001442 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +00001443
1444 foreach f [array names frames] {
1445 set addr [format %x $f]
1446 set cmd "addr2line -e [info nameofexec] $addr"
1447 set line [eval exec $cmd]
1448 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00001449
1450 set file [lindex [split $line :] 0]
1451 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00001452 }
1453
danielk197735754ac2008-03-21 17:29:37 +00001454 foreach f [array names files] {
1455 set contents ""
1456 catch {
1457 set fd [open $f]
1458 set contents [read $fd]
1459 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001460 }
danielk197735754ac2008-03-21 17:29:37 +00001461 set contents [string map {' ''} $contents]
1462 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00001463 }
danielk19776f332c12008-03-21 14:22:44 +00001464
danielk197735754ac2008-03-21 17:29:37 +00001465 set fd [open $filename w]
1466 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
1467 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001468}
drh93aed5a2008-01-16 17:46:38 +00001469
danf5894502009-10-07 18:41:19 +00001470# Drop all tables in database [db]
1471proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00001472 ifcapable trigger&&foreignkey {
1473 set pk [$db one "PRAGMA foreign_keys"]
1474 $db eval "PRAGMA foreign_keys = OFF"
1475 }
drh9a6ffc82010-02-15 18:03:20 +00001476 foreach {idx name file} [db eval {PRAGMA database_list}] {
1477 if {$idx==1} {
1478 set master sqlite_temp_master
1479 } else {
1480 set master $name.sqlite_master
1481 }
1482 foreach {t type} [$db eval "
1483 SELECT name, type FROM $master
dana16d1062010-09-28 17:37:28 +00001484 WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X'
drh9a6ffc82010-02-15 18:03:20 +00001485 "] {
dana16d1062010-09-28 17:37:28 +00001486 $db eval "DROP $type \"$t\""
drh9a6ffc82010-02-15 18:03:20 +00001487 }
danf5894502009-10-07 18:41:19 +00001488 }
shaneh4e7b32f2009-12-17 22:12:51 +00001489 ifcapable trigger&&foreignkey {
1490 $db eval "PRAGMA foreign_keys = $pk"
1491 }
danf5894502009-10-07 18:41:19 +00001492}
1493
dan71cb5182010-04-26 12:39:03 +00001494#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00001495# If a test script is executed with global variable $::G(perm:name) set to
1496# "wal", then the tests are run in WAL mode. Otherwise, they should be run
1497# in rollback mode. The following Tcl procs are used to make this less
1498# intrusive:
dan71cb5182010-04-26 12:39:03 +00001499#
1500# wal_set_journal_mode ?DB?
1501#
1502# If running a WAL test, execute "PRAGMA journal_mode = wal" using
1503# connection handle DB. Otherwise, this command is a no-op.
1504#
1505# wal_check_journal_mode TESTNAME ?DB?
1506#
1507# If running a WAL test, execute a tests case that fails if the main
1508# database for connection handle DB is not currently a WAL database.
1509# Otherwise (if not running a WAL permutation) this is a no-op.
1510#
1511# wal_is_wal_mode
1512#
1513# Returns true if this test should be run in WAL mode. False otherwise.
1514#
1515proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00001516 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00001517}
1518proc wal_set_journal_mode {{db db}} {
1519 if { [wal_is_wal_mode] } {
1520 $db eval "PRAGMA journal_mode = WAL"
1521 }
1522}
1523proc wal_check_journal_mode {testname {db db}} {
1524 if { [wal_is_wal_mode] } {
1525 $db eval { SELECT * FROM sqlite_master }
1526 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
1527 }
1528}
1529
dan430e74c2010-06-07 17:47:26 +00001530proc permutation {} {
1531 set perm ""
1532 catch {set perm $::G(perm:name)}
1533 set perm
1534}
dancb79e512010-08-06 13:50:07 +00001535proc presql {} {
1536 set presql ""
1537 catch {set presql $::G(perm:presql)}
1538 set presql
1539}
dan430e74c2010-06-07 17:47:26 +00001540
danc1a60c52010-06-07 14:28:16 +00001541#-------------------------------------------------------------------------
1542#
1543proc slave_test_script {script} {
1544
1545 # Create the interpreter used to run the test script.
1546 interp create tinterp
1547
1548 # Populate some global variables that tester.tcl expects to see.
1549 foreach {var value} [list \
1550 ::argv0 $::argv0 \
1551 ::argv {} \
1552 ::SLAVE 1 \
1553 ] {
1554 interp eval tinterp [list set $var $value]
1555 }
1556
1557 # The alias used to access the global test counters.
1558 tinterp alias set_test_counter set_test_counter
1559
1560 # Set up the ::cmdlinearg array in the slave.
1561 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
1562
dan430e74c2010-06-07 17:47:26 +00001563 # Set up the ::G array in the slave.
1564 interp eval tinterp [list array set ::G [array get ::G]]
1565
danc1a60c52010-06-07 14:28:16 +00001566 # Load the various test interfaces implemented in C.
1567 load_testfixture_extensions tinterp
1568
1569 # Run the test script.
1570 interp eval tinterp $script
1571
danea5542d2010-07-06 11:26:15 +00001572 # Check if the interpreter call [run_thread_tests]
1573 if { [interp eval tinterp {info exists ::run_thread_tests_called}] } {
1574 set ::run_thread_tests_called 1
1575 }
1576
danc1a60c52010-06-07 14:28:16 +00001577 # Delete the interpreter used to run the test script.
1578 interp delete tinterp
1579}
1580
dan430e74c2010-06-07 17:47:26 +00001581proc slave_test_file {zFile} {
dan0626dfc2010-06-15 06:56:37 +00001582 set tail [file tail $zFile]
dan430e74c2010-06-07 17:47:26 +00001583
dan6a64d672011-04-04 15:38:16 +00001584 if {[info exists ::G(start:permutation)]} {
1585 if {[permutation] != $::G(start:permutation)} return
1586 unset ::G(start:permutation)
1587 }
1588 if {[info exists ::G(start:file)]} {
1589 if {$tail != $::G(start:file) && $tail!="$::G(start:file).test"} return
1590 unset ::G(start:file)
1591 }
1592
dan92d516a2010-07-05 14:54:48 +00001593 # Remember the value of the shared-cache setting. So that it is possible
1594 # to check afterwards that it was not modified by the test script.
1595 #
1596 ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] }
dan0626dfc2010-06-15 06:56:37 +00001597
dan92d516a2010-07-05 14:54:48 +00001598 # Run the test script in a slave interpreter.
1599 #
danea5542d2010-07-06 11:26:15 +00001600 unset -nocomplain ::run_thread_tests_called
dan0626dfc2010-06-15 06:56:37 +00001601 reset_prng_state
1602 set ::sqlite_open_file_count 0
1603 set time [time { slave_test_script [list source $zFile] }]
1604 set ms [expr [lindex $time 0] / 1000]
1605
dan92d516a2010-07-05 14:54:48 +00001606 # Test that all files opened by the test script were closed. Omit this
1607 # if the test script has "thread" in its name. The open file counter
1608 # is not thread-safe.
dan0626dfc2010-06-15 06:56:37 +00001609 #
danea5542d2010-07-06 11:26:15 +00001610 if {[info exists ::run_thread_tests_called]==0} {
dan92d516a2010-07-05 14:54:48 +00001611 do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0}
1612 }
dan430e74c2010-06-07 17:47:26 +00001613 set ::sqlite_open_file_count 0
1614
dan0626dfc2010-06-15 06:56:37 +00001615 # Test that the global "shared-cache" setting was not altered by
1616 # the test script.
1617 #
1618 ifcapable shared_cache {
1619 set res [expr {[sqlite3_enable_shared_cache] == $scs}]
1620 do_test ${tail}-sharedcachesetting [list set {} $res] 1
1621 }
dan430e74c2010-06-07 17:47:26 +00001622
dan92d516a2010-07-05 14:54:48 +00001623 # Add some info to the output.
1624 #
dan0626dfc2010-06-15 06:56:37 +00001625 puts "Time: $tail $ms ms"
dan430e74c2010-06-07 17:47:26 +00001626 show_memstats
danc1a60c52010-06-07 14:28:16 +00001627}
1628
dan59257dc2010-08-04 11:34:31 +00001629# Open a new connection on database test.db and execute the SQL script
1630# supplied as an argument. Before returning, close the new conection and
1631# restore the 4 byte fields starting at header offsets 28, 92 and 96
1632# to the values they held before the SQL was executed. This simulates
1633# a write by a pre-3.7.0 client.
1634#
1635proc sql36231 {sql} {
1636 set B [hexio_read test.db 92 8]
1637 set A [hexio_read test.db 28 4]
1638 sqlite3 db36231 test.db
1639 catch { db36231 func a_string a_string }
1640 execsql $sql db36231
1641 db36231 close
1642 hexio_write test.db 28 $A
1643 hexio_write test.db 92 $B
1644 return ""
1645}
danf5894502009-10-07 18:41:19 +00001646
danccc7ff42010-11-29 12:06:45 +00001647proc db_save {} {
1648 foreach f [glob -nocomplain sv_test.db*] { forcedelete $f }
1649 foreach f [glob -nocomplain test.db*] {
1650 set f2 "sv_$f"
mistachkinfda06be2011-08-02 00:57:34 +00001651 forcecopy $f $f2
danccc7ff42010-11-29 12:06:45 +00001652 }
1653}
1654proc db_save_and_close {} {
1655 db_save
1656 catch { db close }
1657 return ""
1658}
1659proc db_restore {} {
1660 foreach f [glob -nocomplain test.db*] { forcedelete $f }
1661 foreach f2 [glob -nocomplain sv_test.db*] {
1662 set f [string range $f2 3 end]
mistachkinfda06be2011-08-02 00:57:34 +00001663 forcecopy $f2 $f
danccc7ff42010-11-29 12:06:45 +00001664 }
1665}
1666proc db_restore_and_reopen {{dbfile test.db}} {
1667 catch { db close }
1668 db_restore
1669 sqlite3 db $dbfile
1670}
1671proc db_delete_and_reopen {{file test.db}} {
1672 catch { db close }
mistachkinfda06be2011-08-02 00:57:34 +00001673 foreach f [glob -nocomplain test.db*] { forcedelete $f }
danccc7ff42010-11-29 12:06:45 +00001674 sqlite3 db $file
1675}
1676
danielk197745901d62004-11-10 15:27:38 +00001677# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
1678# to non-zero, then set the global variable $AUTOVACUUM to 1.
1679set AUTOVACUUM $sqlite_options(default_autovacuum)
danielk1977ee8b7992009-03-26 17:13:06 +00001680
dan64b95bb2012-05-12 05:30:29 +00001681# Make sure the FTS enhanced query syntax is disabled.
1682set sqlite_fts3_enable_parentheses 0
1683
danielk1977ee8b7992009-03-26 17:13:06 +00001684source $testdir/thread_common.tcl
danddf80eb2010-10-25 12:47:43 +00001685source $testdir/malloc_common.tcl