blob: d3fa607d324d8709ed56750c4498d720b388e911 [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?
57# do_test TESTNAME SCRIPT EXPECTED
dan153eda02010-06-21 07:45:47 +000058# do_execsql_test TESTNAME SQL EXPECTED
59# do_catchsql_test TESTNAME SQL EXPECTED
danc1a60c52010-06-07 14:28:16 +000060#
dan430e74c2010-06-07 17:47:26 +000061# Commands providing a lower level interface to the global test counters:
danc1a60c52010-06-07 14:28:16 +000062#
63# set_test_counter COUNTER ?VALUE?
mistachkin6c3c1a02011-11-12 03:17:40 +000064# omit_test TESTNAME REASON ?APPEND?
danc1a60c52010-06-07 14:28:16 +000065# fail_test TESTNAME
66# incr_ntest
67#
68# Command run at the end of each test file:
69#
70# finish_test
71#
dan430e74c2010-06-07 17:47:26 +000072# Commands to help create test files that run with the "WAL" and other
73# permutations (see file permutations.test):
danc1a60c52010-06-07 14:28:16 +000074#
75# wal_is_wal_mode
76# wal_set_journal_mode ?DB?
77# wal_check_journal_mode TESTNAME?DB?
dan430e74c2010-06-07 17:47:26 +000078# permutation
dancb79e512010-08-06 13:50:07 +000079# presql
danc1a60c52010-06-07 14:28:16 +000080#
drh348784e2000-05-29 20:41:49 +000081
danc1a60c52010-06-07 14:28:16 +000082# Set the precision of FP arithmatic used by the interpreter. And
83# configure SQLite to take database file locks on the page that begins
84# 64KB into the database file instead of the one 1GB in. This means
85# the code that handles that special case can be tested without creating
86# very large database files.
87#
drh92febd92004-08-20 18:34:20 +000088set tcl_precision 15
drhc7a3bb92009-02-05 16:31:45 +000089sqlite3_test_control_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +000090
danc1a60c52010-06-07 14:28:16 +000091
92# If the pager codec is available, create a wrapper for the [sqlite3]
93# command that appends "-key {xyzzy}" to the command line. i.e. this:
drh3a7fb7c2007-08-10 16:41:08 +000094#
danc1a60c52010-06-07 14:28:16 +000095# sqlite3 db test.db
drh4a50aac2007-08-23 02:47:53 +000096#
danc1a60c52010-06-07 14:28:16 +000097# becomes
drh4a50aac2007-08-23 02:47:53 +000098#
danc1a60c52010-06-07 14:28:16 +000099# sqlite3 db test.db -key {xyzzy}
drh9eb9e262004-02-11 02:18:05 +0000100#
dan430e74c2010-06-07 17:47:26 +0000101if {[info command sqlite_orig]==""} {
drhef4ac8f2004-06-19 00:16:31 +0000102 rename sqlite3 sqlite_orig
103 proc sqlite3 {args} {
dan68928b62010-06-22 13:46:43 +0000104 if {[llength $args]>=2 && [string index [lindex $args 0] 0]!="-"} {
dan430e74c2010-06-07 17:47:26 +0000105 # This command is opening a new database connection.
106 #
107 if {[info exists ::G(perm:sqlite3_args)]} {
108 set args [concat $args $::G(perm:sqlite3_args)]
109 }
dan68928b62010-06-22 13:46:43 +0000110 if {[sqlite_orig -has-codec] && ![info exists ::do_not_use_codec]} {
dan430e74c2010-06-07 17:47:26 +0000111 lappend args -key {xyzzy}
112 }
113
dan3ccd20a2010-06-09 19:01:02 +0000114 set res [uplevel 1 sqlite_orig $args]
dan430e74c2010-06-07 17:47:26 +0000115 if {[info exists ::G(perm:presql)]} {
116 [lindex $args 0] eval $::G(perm:presql)
117 }
dan1ce1b4a2010-12-07 14:32:28 +0000118 if {[info exists ::G(perm:dbconfig)]} {
119 set ::dbhandle [lindex $args 0]
120 uplevel #0 $::G(perm:dbconfig)
121 }
dan3ccd20a2010-06-09 19:01:02 +0000122 set res
dan430e74c2010-06-07 17:47:26 +0000123 } else {
124 # This command is not opening a new database connection. Pass the
125 # arguments through to the C implemenation as the are.
126 #
127 uplevel 1 sqlite_orig $args
drh9eb9e262004-02-11 02:18:05 +0000128 }
drh9eb9e262004-02-11 02:18:05 +0000129 }
130}
131
mistachkinfda06be2011-08-02 00:57:34 +0000132proc getFileRetries {} {
133 if {![info exists ::G(file-retries)]} {
134 #
135 # NOTE: Return the default number of retries for [file] operations. A
136 # value of zero or less here means "disabled".
137 #
mistachkinc60941f2012-09-13 01:51:02 +0000138 return [expr {$::tcl_platform(platform) eq "windows" ? 50 : 0}]
mistachkinfda06be2011-08-02 00:57:34 +0000139 }
140 return $::G(file-retries)
141}
142
143proc getFileRetryDelay {} {
144 if {![info exists ::G(file-retry-delay)]} {
145 #
146 # NOTE: Return the default number of milliseconds to wait when retrying
147 # failed [file] operations. A value of zero or less means "do not
148 # wait".
149 #
150 return 100; # TODO: Good default?
151 }
152 return $::G(file-retry-delay)
153}
154
mistachkinf8a78462012-03-08 20:00:36 +0000155# Return the string representing the name of the current directory. On
156# Windows, the result is "normalized" to whatever our parent command shell
157# is using to prevent case-mismatch issues.
158#
159proc get_pwd {} {
160 if {$::tcl_platform(platform) eq "windows"} {
mistachkin533b8f62012-03-08 20:28:31 +0000161 #
162 # NOTE: Cannot use [file normalize] here because it would alter the
163 # case of the result to what Tcl considers canonical, which would
164 # defeat the purpose of this procedure.
165 #
166 return [string map [list \\ /] \
167 [string trim [exec -- $::env(ComSpec) /c echo %CD%]]]
mistachkinf8a78462012-03-08 20:00:36 +0000168 } else {
169 return [pwd]
170 }
171}
172
mistachkinfda06be2011-08-02 00:57:34 +0000173# Copy file $from into $to. This is used because some versions of
174# TCL for windows (notably the 8.4.1 binary package shipped with the
175# current mingw release) have a broken "file copy" command.
176#
177proc copy_file {from to} {
178 do_copy_file false $from $to
179}
180
181proc forcecopy {from to} {
182 do_copy_file true $from $to
183}
184
185proc do_copy_file {force from to} {
186 set nRetry [getFileRetries] ;# Maximum number of retries.
187 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
188
189 # On windows, sometimes even a [file copy -force] can fail. The cause is
190 # usually "tag-alongs" - programs like anti-virus software, automatic backup
191 # tools and various explorer extensions that keep a file open a little longer
192 # than we expect, causing the delete to fail.
193 #
194 # The solution is to wait a short amount of time before retrying the copy.
195 #
196 if {$nRetry > 0} {
197 for {set i 0} {$i<$nRetry} {incr i} {
198 set rc [catch {
199 if {$force} {
200 file copy -force $from $to
201 } else {
202 file copy $from $to
203 }
204 } msg]
205 if {$rc==0} break
206 if {$nDelay > 0} { after $nDelay }
207 }
208 if {$rc} { error $msg }
209 } else {
210 if {$force} {
211 file copy -force $from $to
212 } else {
213 file copy $from $to
214 }
215 }
216}
217
mistachkinc5484652012-03-05 22:52:33 +0000218# Check if a file name is relative
219#
220proc is_relative_file { file } {
221 return [expr {[file pathtype $file] != "absolute"}]
222}
223
mistachkin4a41f342012-03-06 03:00:49 +0000224# If the VFS supports using the current directory, returns [pwd];
225# otherwise, it returns only the provided suffix string (which is
226# empty by default).
227#
228proc test_pwd { args } {
229 if {[llength $args] > 0} {
230 set suffix1 [lindex $args 0]
231 if {[llength $args] > 1} {
232 set suffix2 [lindex $args 1]
233 } else {
234 set suffix2 $suffix1
235 }
236 } else {
237 set suffix1 ""; set suffix2 ""
238 }
239 ifcapable curdir {
mistachkin6aa18c92012-03-08 20:22:42 +0000240 return "[get_pwd]$suffix1"
mistachkin4a41f342012-03-06 03:00:49 +0000241 } else {
242 return $suffix2
243 }
244}
245
mistachkinfda06be2011-08-02 00:57:34 +0000246# Delete a file or directory
247#
248proc delete_file {args} {
249 do_delete_file false {*}$args
250}
251
252proc forcedelete {args} {
253 do_delete_file true {*}$args
254}
255
256proc do_delete_file {force args} {
257 set nRetry [getFileRetries] ;# Maximum number of retries.
258 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
259
260 foreach filename $args {
261 # On windows, sometimes even a [file delete -force] can fail just after
262 # a file is closed. The cause is usually "tag-alongs" - programs like
263 # anti-virus software, automatic backup tools and various explorer
264 # extensions that keep a file open a little longer than we expect, causing
265 # the delete to fail.
266 #
267 # The solution is to wait a short amount of time before retrying the
268 # delete.
269 #
270 if {$nRetry > 0} {
271 for {set i 0} {$i<$nRetry} {incr i} {
272 set rc [catch {
273 if {$force} {
274 file delete -force $filename
275 } else {
276 file delete $filename
277 }
278 } msg]
279 if {$rc==0} break
280 if {$nDelay > 0} { after $nDelay }
281 }
282 if {$rc} { error $msg }
283 } else {
284 if {$force} {
285 file delete -force $filename
286 } else {
287 file delete $filename
288 }
289 }
290 }
291}
292
dancb354602010-07-08 09:44:42 +0000293proc execpresql {handle args} {
294 trace remove execution $handle enter [list execpresql $handle]
295 if {[info exists ::G(perm:presql)]} {
296 $handle eval $::G(perm:presql)
297 }
298}
299
dan68928b62010-06-22 13:46:43 +0000300# This command should be called after loading tester.tcl from within
301# all test scripts that are incompatible with encryption codecs.
302#
303proc do_not_use_codec {} {
304 set ::do_not_use_codec 1
305 reset_db
306}
307
dan430e74c2010-06-07 17:47:26 +0000308# The following block only runs the first time this file is sourced. It
309# does not run in slave interpreters (since the ::cmdlinearg array is
310# populated before the test script is run in slave interpreters).
drhbec2bf42000-05-29 23:48:22 +0000311#
danc1a60c52010-06-07 14:28:16 +0000312if {[info exists cmdlinearg]==0} {
313
314 # Parse any options specified in the $argv array. This script accepts the
315 # following options:
316 #
317 # --pause
318 # --soft-heap-limit=NN
319 # --maxerror=NN
320 # --malloctrace=N
321 # --backtrace=N
322 # --binarylog=N
dan0626dfc2010-06-15 06:56:37 +0000323 # --soak=N
mistachkinfda06be2011-08-02 00:57:34 +0000324 # --file-retries=N
325 # --file-retry-delay=N
dan6a64d672011-04-04 15:38:16 +0000326 # --start=[$permutation:]$testfile
mistachkin6c3c1a02011-11-12 03:17:40 +0000327 # --match=$pattern
danc1a60c52010-06-07 14:28:16 +0000328 #
329 set cmdlinearg(soft-heap-limit) 0
330 set cmdlinearg(maxerror) 1000
331 set cmdlinearg(malloctrace) 0
332 set cmdlinearg(backtrace) 10
333 set cmdlinearg(binarylog) 0
dan0626dfc2010-06-15 06:56:37 +0000334 set cmdlinearg(soak) 0
mistachkinfda06be2011-08-02 00:57:34 +0000335 set cmdlinearg(file-retries) 0
336 set cmdlinearg(file-retry-delay) 0
mistachkin6c3c1a02011-11-12 03:17:40 +0000337 set cmdlinearg(start) ""
338 set cmdlinearg(match) ""
danc1a60c52010-06-07 14:28:16 +0000339
340 set leftover [list]
341 foreach a $argv {
342 switch -regexp -- $a {
343 {^-+pause$} {
344 # Wait for user input before continuing. This is to give the user an
345 # opportunity to connect profiling tools to the process.
346 puts -nonewline "Press RETURN to begin..."
347 flush stdout
348 gets stdin
349 }
350 {^-+soft-heap-limit=.+$} {
351 foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break
352 }
353 {^-+maxerror=.+$} {
354 foreach {dummy cmdlinearg(maxerror)} [split $a =] break
355 }
356 {^-+malloctrace=.+$} {
357 foreach {dummy cmdlinearg(malloctrace)} [split $a =] break
358 if {$cmdlinearg(malloctrace)} {
359 sqlite3_memdebug_log start
360 }
361 }
362 {^-+backtrace=.+$} {
363 foreach {dummy cmdlinearg(backtrace)} [split $a =] break
dan0626dfc2010-06-15 06:56:37 +0000364 sqlite3_memdebug_backtrace $value
danc1a60c52010-06-07 14:28:16 +0000365 }
danc1a60c52010-06-07 14:28:16 +0000366 {^-+binarylog=.+$} {
367 foreach {dummy cmdlinearg(binarylog)} [split $a =] break
368 }
dan0626dfc2010-06-15 06:56:37 +0000369 {^-+soak=.+$} {
370 foreach {dummy cmdlinearg(soak)} [split $a =] break
371 set ::G(issoak) $cmdlinearg(soak)
372 }
mistachkinfda06be2011-08-02 00:57:34 +0000373 {^-+file-retries=.+$} {
374 foreach {dummy cmdlinearg(file-retries)} [split $a =] break
375 set ::G(file-retries) $cmdlinearg(file-retries)
376 }
377 {^-+file-retry-delay=.+$} {
378 foreach {dummy cmdlinearg(file-retry-delay)} [split $a =] break
379 set ::G(file-retry-delay) $cmdlinearg(file-retry-delay)
380 }
dan6a64d672011-04-04 15:38:16 +0000381 {^-+start=.+$} {
382 foreach {dummy cmdlinearg(start)} [split $a =] break
383
384 set ::G(start:file) $cmdlinearg(start)
385 if {[regexp {(.*):(.*)} $cmdlinearg(start) -> s.perm s.file]} {
386 set ::G(start:permutation) ${s.perm}
387 set ::G(start:file) ${s.file}
388 }
389 if {$::G(start:file) == ""} {unset ::G(start:file)}
390 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000391 {^-+match=.+$} {
392 foreach {dummy cmdlinearg(match)} [split $a =] break
393
394 set ::G(match) $cmdlinearg(match)
395 if {$::G(match) == ""} {unset ::G(match)}
396 }
danc1a60c52010-06-07 14:28:16 +0000397 default {
398 lappend leftover $a
399 }
400 }
401 }
402 set argv $leftover
403
dan430e74c2010-06-07 17:47:26 +0000404 # Install the malloc layer used to inject OOM errors. And the 'automatic'
405 # extensions. This only needs to be done once for the process.
406 #
danielk1977f7b9d662008-06-23 18:49:43 +0000407 sqlite3_shutdown
408 install_malloc_faultsim 1
409 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000410 autoinstall_test_functions
danc1a60c52010-06-07 14:28:16 +0000411
dan430e74c2010-06-07 17:47:26 +0000412 # If the --binarylog option was specified, create the logging VFS. This
413 # call installs the new VFS as the default for all SQLite connections.
414 #
danc1a60c52010-06-07 14:28:16 +0000415 if {$cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000416 vfslog new binarylog {} vfslog.bin
danc1a60c52010-06-07 14:28:16 +0000417 }
418
419 # Set the backtrace depth, if malloc tracing is enabled.
420 #
421 if {$cmdlinearg(malloctrace)} {
422 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danielk1977185eac92008-07-12 15:55:54 +0000423 }
danielk1977f7b9d662008-06-23 18:49:43 +0000424}
danielk197703ba3fa2009-01-09 10:49:14 +0000425
danc1a60c52010-06-07 14:28:16 +0000426# Update the soft-heap-limit each time this script is run. In that
427# way if an individual test file changes the soft-heap-limit, it
428# will be reset at the start of the next test file.
429#
430sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
431
432# Create a test database
433#
danielk197703ba3fa2009-01-09 10:49:14 +0000434proc reset_db {} {
435 catch {db close}
mistachkinfda06be2011-08-02 00:57:34 +0000436 forcedelete test.db
437 forcedelete test.db-journal
438 forcedelete test.db-wal
danielk197703ba3fa2009-01-09 10:49:14 +0000439 sqlite3 db ./test.db
440 set ::DB [sqlite3_connection_pointer db]
441 if {[info exists ::SETUP_SQL]} {
442 db eval $::SETUP_SQL
443 }
drhcd61c282002-03-06 22:01:34 +0000444}
danielk197703ba3fa2009-01-09 10:49:14 +0000445reset_db
drhbec2bf42000-05-29 23:48:22 +0000446
447# Abort early if this script has been run before.
448#
danc1a60c52010-06-07 14:28:16 +0000449if {[info exists TC(count)]} return
drhbec2bf42000-05-29 23:48:22 +0000450
drhce2198c2010-09-10 13:22:59 +0000451# Make sure memory statistics are enabled.
452#
453sqlite3_config_memstatus 1
454
danc1a60c52010-06-07 14:28:16 +0000455# Initialize the test counters and set up commands to access them.
456# Or, if this is a slave interpreter, set up aliases to write the
457# counters in the parent interpreter.
drhbec2bf42000-05-29 23:48:22 +0000458#
danc1a60c52010-06-07 14:28:16 +0000459if {0==[info exists ::SLAVE]} {
460 set TC(errors) 0
461 set TC(count) 0
462 set TC(fail_list) [list]
463 set TC(omit_list) [list]
464
465 proc set_test_counter {counter args} {
466 if {[llength $args]} {
467 set ::TC($counter) [lindex $args 0]
468 }
469 set ::TC($counter)
470 }
drhb62c3352006-11-23 09:39:16 +0000471}
drh348784e2000-05-29 20:41:49 +0000472
drh521cc842008-04-15 02:36:33 +0000473# Record the fact that a sequence of tests were omitted.
474#
mistachkin6c3c1a02011-11-12 03:17:40 +0000475proc omit_test {name reason {append 1}} {
danc1a60c52010-06-07 14:28:16 +0000476 set omitList [set_test_counter omit_list]
mistachkin6c3c1a02011-11-12 03:17:40 +0000477 if {$append} {
478 lappend omitList [list $name $reason]
479 }
danc1a60c52010-06-07 14:28:16 +0000480 set_test_counter omit_list $omitList
drh521cc842008-04-15 02:36:33 +0000481}
482
danc1a60c52010-06-07 14:28:16 +0000483# Record the fact that a test failed.
484#
485proc fail_test {name} {
486 set f [set_test_counter fail_list]
487 lappend f $name
488 set_test_counter fail_list $f
489 set_test_counter errors [expr [set_test_counter errors] + 1]
490
491 set nFail [set_test_counter errors]
492 if {$nFail>=$::cmdlinearg(maxerror)} {
493 puts "*** Giving up..."
494 finalize_testing
495 }
496}
497
498# Increment the number of tests run
499#
500proc incr_ntest {} {
501 set_test_counter count [expr [set_test_counter count] + 1]
502}
503
504
drh348784e2000-05-29 20:41:49 +0000505# Invoke the do_test procedure to run a single test
506#
507proc do_test {name cmd expected} {
danc1a60c52010-06-07 14:28:16 +0000508 global argv cmdlinearg
509
dan8c408002010-11-01 17:38:24 +0000510 fix_testname name
511
drh4a50aac2007-08-23 02:47:53 +0000512 sqlite3_memdebug_settitle $name
danc1a60c52010-06-07 14:28:16 +0000513
dan92d516a2010-07-05 14:54:48 +0000514# if {[llength $argv]==0} {
515# set go 1
516# } else {
517# set go 0
518# foreach pattern $argv {
519# if {[string match $pattern $name]} {
520# set go 1
521# break
522# }
523# }
524# }
dan430e74c2010-06-07 17:47:26 +0000525
dand506de02010-07-03 13:59:01 +0000526 if {[info exists ::G(perm:prefix)]} {
527 set name "$::G(perm:prefix)$name"
dan430e74c2010-06-07 17:47:26 +0000528 }
529
danc1a60c52010-06-07 14:28:16 +0000530 incr_ntest
drh5edc3122001-09-13 21:53:09 +0000531 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000532 flush stdout
mistachkin6c3c1a02011-11-12 03:17:40 +0000533
534 if {![info exists ::G(match)] || [string match $::G(match) $name]} {
535 if {[catch {uplevel #0 "$cmd;\n"} result]} {
536 puts "\nError: $result"
537 fail_test $name
mistachkin6c3c1a02011-11-12 03:17:40 +0000538 } else {
drh3f17aef2012-04-27 01:08:02 +0000539 if {[regexp {^~?/.*/$} $expected]} {
540 if {[string index $expected 0]=="~"} {
drh5343b2d2012-09-27 19:53:38 +0000541 set re [string map {# {[-0-9.]+}} [string range $expected 2 end-1]]
drh3f17aef2012-04-27 01:08:02 +0000542 set ok [expr {![regexp $re $result]}]
543 } else {
drh5343b2d2012-09-27 19:53:38 +0000544 set re [string map {# {[-0-9.]+}} [string range $expected 1 end-1]]
drh3f17aef2012-04-27 01:08:02 +0000545 set ok [regexp $re $result]
546 }
547 } else {
548 set ok [expr {[string compare $result $expected]==0}]
549 }
550 if {!$ok} {
mistachkinc60941f2012-09-13 01:51:02 +0000551 # if {![info exists ::testprefix] || $::testprefix eq ""} {
552 # error "no test prefix"
553 # }
drh3f17aef2012-04-27 01:08:02 +0000554 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
555 fail_test $name
556 } else {
557 puts " Ok"
558 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000559 }
drh348784e2000-05-29 20:41:49 +0000560 } else {
mistachkin6c3c1a02011-11-12 03:17:40 +0000561 puts " Omitted"
562 omit_test $name "pattern mismatch" 0
drh348784e2000-05-29 20:41:49 +0000563 }
drh6558db82007-04-13 03:23:21 +0000564 flush stdout
drh348784e2000-05-29 20:41:49 +0000565}
dana3f51082010-09-30 18:43:14 +0000566
drh8df91852012-04-24 12:46:05 +0000567proc catchcmd {db {cmd ""}} {
568 global CLI
569 set out [open cmds.txt w]
570 puts $out $cmd
571 close $out
572 set line "exec $CLI $db < cmds.txt"
573 set rc [catch { eval $line } msg]
574 list $rc $msg
575}
576
shaneh55505172011-06-21 19:30:19 +0000577proc filepath_normalize {p} {
578 # test cases should be written to assume "unix"-like file paths
shaneh285a18f2011-06-21 19:39:59 +0000579 if {$::tcl_platform(platform)!="unix"} {
shaneh55505172011-06-21 19:30:19 +0000580 # lreverse*2 as a hack to remove any unneeded {} after the string map
581 lreverse [lreverse [string map {\\ /} [regsub -nocase -all {[a-z]:[/\\]+} $p {/}]]]
shanehc4896402011-06-21 19:38:16 +0000582 } {
583 set p
shaneh55505172011-06-21 19:30:19 +0000584 }
585}
586proc do_filepath_test {name cmd expected} {
587 uplevel [list do_test $name [
588 subst -nocommands { filepath_normalize [ $cmd ] }
589 ] [filepath_normalize $expected]]
590}
591
dan33f53792011-05-05 19:44:22 +0000592proc realnum_normalize {r} {
shaneh4f529e82011-06-20 17:41:41 +0000593 # different TCL versions display floating point values differently.
594 string map {1.#INF inf Inf inf .0e e} [regsub -all {(e[+-])0+} $r {\1}]
dan33f53792011-05-05 19:44:22 +0000595}
596proc do_realnum_test {name cmd expected} {
597 uplevel [list do_test $name [
598 subst -nocommands { realnum_normalize [ $cmd ] }
599 ] [realnum_normalize $expected]]
600}
601
dana3f51082010-09-30 18:43:14 +0000602proc fix_testname {varname} {
603 upvar $varname testname
604 if {[info exists ::testprefix]
605 && [string is digit [string range $testname 0 0]]
606 } {
607 set testname "${::testprefix}-$testname"
608 }
609}
dan153eda02010-06-21 07:45:47 +0000610
dan57f7f4b2010-10-01 19:04:37 +0000611proc do_execsql_test {testname sql {result {}}} {
dana3f51082010-09-30 18:43:14 +0000612 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000613 uplevel do_test [list $testname] [list "execsql {$sql}"] [list [list {*}$result]]
dan153eda02010-06-21 07:45:47 +0000614}
615proc do_catchsql_test {testname sql result} {
dana3f51082010-09-30 18:43:14 +0000616 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000617 uplevel do_test [list $testname] [list "catchsql {$sql}"] [list $result]
dan153eda02010-06-21 07:45:47 +0000618}
dan39854792010-11-15 16:12:58 +0000619proc do_eqp_test {name sql res} {
620 uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res]
621}
dan153eda02010-06-21 07:45:47 +0000622
dan51f06982010-09-18 19:00:12 +0000623#-------------------------------------------------------------------------
624# Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST
625#
626# Where switches are:
627#
628# -errorformat FMTSTRING
629# -count
danaf1dcab2010-09-20 19:17:53 +0000630# -query SQL
dana16d1062010-09-28 17:37:28 +0000631# -tclquery TCL
danaf7626f2010-09-23 18:47:36 +0000632# -repair TCL
dan51f06982010-09-18 19:00:12 +0000633#
634proc do_select_tests {prefix args} {
635
636 set testlist [lindex $args end]
637 set switches [lrange $args 0 end-1]
638
639 set errfmt ""
640 set countonly 0
dana16d1062010-09-28 17:37:28 +0000641 set tclquery ""
danaf7626f2010-09-23 18:47:36 +0000642 set repair ""
dan51f06982010-09-18 19:00:12 +0000643
644 for {set i 0} {$i < [llength $switches]} {incr i} {
645 set s [lindex $switches $i]
646 set n [string length $s]
danaf1dcab2010-09-20 19:17:53 +0000647 if {$n>=2 && [string equal -length $n $s "-query"]} {
dana16d1062010-09-28 17:37:28 +0000648 set tclquery [list execsql [lindex $switches [incr i]]]
649 } elseif {$n>=2 && [string equal -length $n $s "-tclquery"]} {
650 set tclquery [lindex $switches [incr i]]
danaf1dcab2010-09-20 19:17:53 +0000651 } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} {
dan51f06982010-09-18 19:00:12 +0000652 set errfmt [lindex $switches [incr i]]
danaf7626f2010-09-23 18:47:36 +0000653 } elseif {$n>=2 && [string equal -length $n $s "-repair"]} {
654 set repair [lindex $switches [incr i]]
dan51f06982010-09-18 19:00:12 +0000655 } elseif {$n>=2 && [string equal -length $n $s "-count"]} {
656 set countonly 1
657 } else {
658 error "unknown switch: $s"
659 }
660 }
661
662 if {$countonly && $errfmt!=""} {
663 error "Cannot use -count and -errorformat together"
664 }
665 set nTestlist [llength $testlist]
666 if {$nTestlist%3 || $nTestlist==0 } {
667 error "SELECT test list contains [llength $testlist] elements"
668 }
669
danaf7626f2010-09-23 18:47:36 +0000670 eval $repair
dan51f06982010-09-18 19:00:12 +0000671 foreach {tn sql res} $testlist {
dana16d1062010-09-28 17:37:28 +0000672 if {$tclquery != ""} {
danaf1dcab2010-09-20 19:17:53 +0000673 execsql $sql
dana16d1062010-09-28 17:37:28 +0000674 uplevel do_test ${prefix}.$tn [list $tclquery] [list [list {*}$res]]
675 } elseif {$countonly} {
dan51f06982010-09-18 19:00:12 +0000676 set nRow 0
677 db eval $sql {incr nRow}
678 uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res]
679 } elseif {$errfmt==""} {
680 uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]]
681 } else {
682 set res [list 1 [string trim [format $errfmt {*}$res]]]
683 uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res]
684 }
danaf7626f2010-09-23 18:47:36 +0000685 eval $repair
dan51f06982010-09-18 19:00:12 +0000686 }
danaf7626f2010-09-23 18:47:36 +0000687
dan51f06982010-09-18 19:00:12 +0000688}
689
danb04757a2010-09-21 16:59:16 +0000690proc delete_all_data {} {
691 db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} {
692 db eval "DELETE FROM '[string map {' ''} $t]'"
693 }
694}
drh348784e2000-05-29 20:41:49 +0000695
drhb62c3352006-11-23 09:39:16 +0000696# Run an SQL script.
697# Return the number of microseconds per statement.
698#
drh3590f152006-11-23 21:09:10 +0000699proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000700 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000701 flush stdout
702 set speed [time {sqlite3_exec_nr db $sql}]
703 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000704 if {$tm == 0} {
705 set rate [format %20s "many"]
706 } else {
707 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
708 }
drh3590f152006-11-23 21:09:10 +0000709 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000710 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000711 global total_time
712 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000713 lappend ::speed_trial_times $name $tm
drhdd924312007-03-31 22:34:16 +0000714}
drh45c236d2008-03-22 01:08:00 +0000715proc speed_trial_tcl {name numstmt units script} {
716 puts -nonewline [format {%-21.21s } $name...]
717 flush stdout
718 set speed [time {eval $script}]
719 set tm [lindex $speed 0]
720 if {$tm == 0} {
721 set rate [format %20s "many"]
722 } else {
723 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
724 }
725 set u2 $units/s
726 puts [format {%12d uS %s %s} $tm $rate $u2]
727 global total_time
728 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000729 lappend ::speed_trial_times $name $tm
drh45c236d2008-03-22 01:08:00 +0000730}
drhdd924312007-03-31 22:34:16 +0000731proc speed_trial_init {name} {
732 global total_time
733 set total_time 0
dana975b592010-10-08 16:09:43 +0000734 set ::speed_trial_times [list]
drhbb810a92010-07-03 12:00:53 +0000735 sqlite3 versdb :memory:
736 set vers [versdb one {SELECT sqlite_source_id()}]
737 versdb close
738 puts "SQLite $vers"
drhdd924312007-03-31 22:34:16 +0000739}
740proc speed_trial_summary {name} {
741 global total_time
742 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
dana975b592010-10-08 16:09:43 +0000743
744 if { 0 } {
745 sqlite3 versdb :memory:
746 set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0]
747 versdb close
748 puts "CREATE TABLE IF NOT EXISTS time(version, script, test, us);"
749 foreach {test us} $::speed_trial_times {
750 puts "INSERT INTO time VALUES('$vers', '$name', '$test', $us);"
751 }
752 }
drhb62c3352006-11-23 09:39:16 +0000753}
754
drh348784e2000-05-29 20:41:49 +0000755# Run this routine last
756#
757proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +0000758 catch {db close}
759 catch {db2 close}
760 catch {db3 close}
761 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +0000762}
763proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +0000764 global sqlite_open_file_count
765
766 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +0000767
drh6e142f52000-06-08 13:36:40 +0000768 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000769 catch {db2 close}
770 catch {db3 close}
771
drh9bc54492007-10-23 14:49:59 +0000772 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000773 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000774 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000775 db close
drh984bfaa2008-03-19 16:08:53 +0000776 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +0000777
drh3a7fb7c2007-08-10 16:41:08 +0000778 sqlite3_soft_heap_limit 0
danc1a60c52010-06-07 14:28:16 +0000779 set nTest [incr_ntest]
780 set nErr [set_test_counter errors]
781
drh348784e2000-05-29 20:41:49 +0000782 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000783 if {$nErr>0} {
danc1a60c52010-06-07 14:28:16 +0000784 puts "Failures on these tests: [set_test_counter fail_list]"
drhf3a65f72007-08-22 20:18:21 +0000785 }
danielk1977ee8b7992009-03-26 17:13:06 +0000786 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +0000787 if {[llength $omitList]>0} {
788 puts "Omitted test cases:"
789 set prec {}
790 foreach {rec} [lsort $omitList] {
791 if {$rec==$prec} continue
792 set prec $rec
793 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
794 }
795 }
drh80788d82006-09-02 14:50:23 +0000796 if {$nErr>0 && ![working_64bit_int]} {
797 puts "******************************************************************"
798 puts "N.B.: The version of TCL that you used to build this test harness"
799 puts "is defective in that it does not support 64-bit integers. Some or"
800 puts "all of the test failures above might be a result from this defect"
801 puts "in your TCL build."
802 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000803 }
danc1a60c52010-06-07 14:28:16 +0000804 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000805 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +0000806 }
drh94e92032003-02-16 22:21:32 +0000807 if {$sqlite_open_file_count} {
808 puts "$sqlite_open_file_count files were left open"
809 incr nErr
810 }
drheafc43b2010-07-26 18:43:40 +0000811 if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 ||
812 [sqlite3_memory_used]>0} {
813 puts "Unfreed memory: [sqlite3_memory_used] bytes in\
814 [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations"
drhf3a65f72007-08-22 20:18:21 +0000815 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000816 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000817 puts "Writing unfreed memory log to \"./memleak.txt\""
818 sqlite3_memdebug_dump ./memleak.txt
819 }
drhf3a65f72007-08-22 20:18:21 +0000820 } else {
821 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000822 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000823 sqlite3_memdebug_dump ./memusage.txt
824 }
drhf3a65f72007-08-22 20:18:21 +0000825 }
drhf7141992008-06-19 00:16:08 +0000826 show_memstats
drh91fd4d42008-01-19 20:11:25 +0000827 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
828 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000829 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
830 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
831 }
danc1a60c52010-06-07 14:28:16 +0000832 if {$::cmdlinearg(malloctrace)} {
danielk197735754ac2008-03-21 17:29:37 +0000833 puts "Writing mallocs.sql..."
834 memdebug_log_sql
835 sqlite3_memdebug_log stop
836 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000837
838 if {[sqlite3_memory_used]>0} {
839 puts "Writing leaks.sql..."
840 sqlite3_memdebug_log sync
841 memdebug_log_sql leaks.sql
842 }
danielk197735754ac2008-03-21 17:29:37 +0000843 }
drhd9910fe2006-10-04 11:55:49 +0000844 foreach f [glob -nocomplain test.db-*-journal] {
mistachkinfda06be2011-08-02 00:57:34 +0000845 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +0000846 }
847 foreach f [glob -nocomplain test.db-mj*] {
mistachkinfda06be2011-08-02 00:57:34 +0000848 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +0000849 }
drhdb25e382001-03-15 18:21:22 +0000850 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000851}
852
drhf7141992008-06-19 00:16:08 +0000853# Display memory statistics for analysis and debugging purposes.
854#
855proc show_memstats {} {
856 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +0000857 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
858 set val [format {now %10d max %10d max-size %10d} \
859 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000860 puts "Memory used: $val"
drheafc43b2010-07-26 18:43:40 +0000861 set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0]
862 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
863 puts "Allocation count: $val"
drhf7141992008-06-19 00:16:08 +0000864 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +0000865 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
866 set val [format {now %10d max %10d max-size %10d} \
867 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000868 puts "Page-cache used: $val"
869 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
870 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
871 puts "Page-cache overflow: $val"
872 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
873 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
874 puts "Scratch memory used: $val"
875 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +0000876 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
877 set val [format {now %10d max %10d max-size %10d} \
878 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000879 puts "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +0000880 ifcapable yytrackmaxstackdepth {
881 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
882 set val [format { max %10d} [lindex $x 2]]
883 puts "Parser stack depth: $val"
884 }
drhf7141992008-06-19 00:16:08 +0000885}
886
drh348784e2000-05-29 20:41:49 +0000887# A procedure to execute SQL
888#
drhc4a3c772001-04-04 11:48:57 +0000889proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000890 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000891 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000892}
drh3aadb2e2000-05-31 17:59:25 +0000893
drhadbca9c2001-09-27 15:11:53 +0000894# Execute SQL and catch exceptions.
895#
896proc catchsql {sql {db db}} {
897 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +0000898 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +0000899 lappend r $msg
900 return $r
901}
902
drh04096482001-11-09 22:41:44 +0000903# Do an VDBE code dump on the SQL given
904#
905proc explain {sql {db db}} {
906 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000907 puts "addr opcode p1 p2 p3 p4 p5 #"
908 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000909 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000910 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
911 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
912 ]
drh04096482001-11-09 22:41:44 +0000913 }
914}
915
drhdafc0ce2008-04-17 19:14:02 +0000916# Show the VDBE program for an SQL statement but omit the Trace
917# opcode at the beginning. This procedure can be used to prove
918# that different SQL statements generate exactly the same VDBE code.
919#
920proc explain_no_trace {sql} {
921 set tr [db eval "EXPLAIN $sql"]
922 return [lrange $tr 7 end]
923}
924
drh3aadb2e2000-05-31 17:59:25 +0000925# Another procedure to execute SQL. This one includes the field
926# names in the returned list.
927#
928proc execsql2 {sql} {
929 set result {}
930 db eval $sql data {
931 foreach f $data(*) {
932 lappend result $f $data($f)
933 }
934 }
935 return $result
936}
drh17a68932001-01-31 13:28:08 +0000937
drhdde85d92003-03-01 19:45:34 +0000938# Use the non-callback API to execute multiple SQL statements
939#
940proc stepsql {dbptr sql} {
941 set sql [string trim $sql]
942 set r 0
943 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000944 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000945 return [list 1 $vm]
946 }
947 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000948# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
949# foreach v $VAL {lappend r $v}
950# }
951 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
952 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
953 lappend r [sqlite3_column_text $vm $i]
954 }
drhdde85d92003-03-01 19:45:34 +0000955 }
danielk1977106bb232004-05-21 10:08:53 +0000956 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000957 return [list 1 $errmsg]
958 }
959 }
960 return $r
961}
962
drh21504322002-06-25 13:16:02 +0000963# Do an integrity check of the entire database
964#
danielk197704103022009-02-03 16:51:24 +0000965proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +0000966 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +0000967 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +0000968 }
969}
970
danc6055c72011-04-28 18:46:46 +0000971
972# Return true if the SQL statement passed as the second argument uses a
973# statement transaction.
974#
975proc sql_uses_stmt {db sql} {
976 set stmt [sqlite3_prepare $db $sql -1 dummy]
977 set uses [uses_stmt_journal $stmt]
978 sqlite3_finalize $stmt
979 return $uses
980}
981
danielk1977db4e8862008-01-16 08:24:46 +0000982proc fix_ifcapable_expr {expr} {
983 set ret ""
984 set state 0
985 for {set i 0} {$i < [string length $expr]} {incr i} {
986 set char [string range $expr $i $i]
987 set newstate [expr {[string is alnum $char] || $char eq "_"}]
988 if {$newstate && !$state} {
989 append ret {$::sqlite_options(}
990 }
991 if {!$newstate && $state} {
992 append ret )
993 }
994 append ret $char
995 set state $newstate
996 }
997 if {$state} {append ret )}
998 return $ret
999}
1000
mistachkinc60941f2012-09-13 01:51:02 +00001001# Returns non-zero if the capabilities are present; zero otherwise.
1002#
1003proc capable {expr} {
1004 set e [fix_ifcapable_expr $expr]; return [expr ($e)]
1005}
1006
drh27d258a2004-10-30 20:23:09 +00001007# Evaluate a boolean expression of capabilities. If true, execute the
1008# code. Omit the code if false.
1009#
danielk19773e8c37e2005-01-21 03:12:14 +00001010proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +00001011 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
1012 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +00001013 if ($e2) {
1014 set c [catch {uplevel 1 $code} r]
1015 } else {
1016 set c [catch {uplevel 1 $elsecode} r]
1017 }
1018 return -code $c $r
drh21504322002-06-25 13:16:02 +00001019}
danielk197745901d62004-11-10 15:27:38 +00001020
danielk1977aca790a2005-01-13 11:07:52 +00001021# This proc execs a seperate process that crashes midway through executing
1022# the SQL script $sql on database test.db.
1023#
1024# The crash occurs during a sync() of file $crashfile. When the crash
1025# occurs a random subset of all unsynced writes made by the process are
1026# written into the files on disk. Argument $crashdelay indicates the
1027# number of file syncs to wait before crashing.
1028#
1029# The return value is a list of two elements. The first element is a
1030# boolean, indicating whether or not the process actually crashed or
1031# reported some other error. The second element in the returned list is the
1032# error message. This is "child process exited abnormally" if the crash
1033# occured.
1034#
danielk1977f8940ae2007-08-23 11:07:10 +00001035# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +00001036#
1037proc crashsql {args} {
danielk197759a33f92007-03-17 10:26:59 +00001038
1039 set blocksize ""
1040 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +00001041 set prngseed 0
drhf8587402008-01-08 16:03:49 +00001042 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +00001043 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +00001044 set dc ""
danielk197759a33f92007-03-17 10:26:59 +00001045 set sql [lindex $args end]
1046
1047 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
1048 set z [lindex $args $ii]
1049 set n [string length $z]
1050 set z2 [lindex $args [expr $ii+1]]
1051
1052 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +00001053 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +00001054 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +00001055 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +00001056 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +00001057 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +00001058 else { error "Unrecognized option: $z" }
1059 }
1060
1061 if {$crashfile eq ""} {
1062 error "Compulsory option -file missing"
1063 }
1064
shanehf2c08822010-07-08 16:30:44 +00001065 # $crashfile gets compared to the native filename in
1066 # cfSync(), which can be different then what TCL uses by
1067 # default, so here we force it to the "nativename" format.
mistachkinf8a78462012-03-08 20:00:36 +00001068 set cfile [string map {\\ \\\\} [file nativename [file join [get_pwd] $crashfile]]]
danielk1977aca790a2005-01-13 11:07:52 +00001069
1070 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +00001071 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +00001072 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +00001073 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +00001074 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +00001075
1076 # This block sets the cache size of the main database to 10
1077 # pages. This is done in case the build is configured to omit
1078 # "PRAGMA cache_size".
1079 puts $f {db eval {SELECT * FROM sqlite_master;}}
1080 puts $f {set bt [btree_from_db db]}
1081 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +00001082 if {$prngseed} {
1083 set seed [expr {$prngseed%10007+1}]
1084 # puts seed=$seed
1085 puts $f "db eval {SELECT randomblob($seed)}"
1086 }
danielk1977933bbd62007-03-17 07:22:42 +00001087
drhf8587402008-01-08 16:03:49 +00001088 if {[string length $tclbody]>0} {
1089 puts $f $tclbody
1090 }
1091 if {[string length $sql]>0} {
1092 puts $f "db eval {"
1093 puts $f "$sql"
1094 puts $f "}"
1095 }
danielk1977aca790a2005-01-13 11:07:52 +00001096 close $f
danielk1977aca790a2005-01-13 11:07:52 +00001097 set r [catch {
drh9c06c952005-11-26 00:25:00 +00001098 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +00001099 } msg]
shanehf2c08822010-07-08 16:30:44 +00001100
1101 # Windows/ActiveState TCL returns a slightly different
1102 # error message. We map that to the expected message
1103 # so that we don't have to change all of the test
1104 # cases.
1105 if {$::tcl_platform(platform)=="windows"} {
1106 if {$msg=="child killed: unknown signal"} {
1107 set msg "child process exited abnormally"
1108 }
1109 }
1110
danielk1977aca790a2005-01-13 11:07:52 +00001111 lappend r $msg
1112}
1113
danielk197732554c12005-01-22 03:39:39 +00001114# Usage: do_ioerr_test <test number> <options...>
1115#
1116# This proc is used to implement test cases that check that IO errors
1117# are correctly handled. The first argument, <test number>, is an integer
1118# used to name the tests executed by this proc. Options are as follows:
1119#
1120# -tclprep TCL script to run to prepare test.
1121# -sqlprep SQL script to run to prepare test.
1122# -tclbody TCL script to run with IO error simulation.
1123# -sqlbody TCL script to run with IO error simulation.
1124# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +00001125# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +00001126# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +00001127# -start Value of 'N' to begin with (default 1)
1128#
1129# -cksum Boolean. If true, test that the database does
1130# not change during the execution of the test case.
1131#
1132proc do_ioerr_test {testname args} {
1133
danielk197732554c12005-01-22 03:39:39 +00001134 set ::ioerropts(-start) 1
1135 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +00001136 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +00001137 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +00001138 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +00001139 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +00001140 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +00001141 array set ::ioerropts $args
1142
danielk197747cd39c2008-05-12 12:41:15 +00001143 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
1144 # a couple of obscure IO errors that do not return them.
1145 set ::ioerropts(-erc) 0
1146
danielk197732554c12005-01-22 03:39:39 +00001147 set ::go 1
drh93aed5a2008-01-16 17:46:38 +00001148 #reset_prng_state
1149 save_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +00001150 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +00001151 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +00001152 incr ::ioerropts(-count) -1
1153 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +00001154
1155 # Skip this IO error if it was specified with the "-exclude" option.
1156 if {[info exists ::ioerropts(-exclude)]} {
1157 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
1158 }
danielk1977861f7452008-06-05 11:39:11 +00001159 if {$::ioerropts(-restoreprng)} {
1160 restore_prng_state
1161 }
danielk197732554c12005-01-22 03:39:39 +00001162
1163 # Delete the files test.db and test2.db, then execute the TCL and
1164 # SQL (in that order) to prepare for the test case.
1165 do_test $testname.$n.1 {
1166 set ::sqlite_io_error_pending 0
1167 catch {db close}
aswiftaebf4132008-11-21 00:10:35 +00001168 catch {db2 close}
mistachkinfda06be2011-08-02 00:57:34 +00001169 catch {forcedelete test.db}
1170 catch {forcedelete test.db-journal}
1171 catch {forcedelete test2.db}
1172 catch {forcedelete test2.db-journal}
drha34c62d2006-01-06 22:11:20 +00001173 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +00001174 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +00001175 if {[info exists ::ioerropts(-tclprep)]} {
1176 eval $::ioerropts(-tclprep)
1177 }
1178 if {[info exists ::ioerropts(-sqlprep)]} {
1179 execsql $::ioerropts(-sqlprep)
1180 }
1181 expr 0
1182 } {0}
1183
1184 # Read the 'checksum' of the database.
1185 if {$::ioerropts(-cksum)} {
1186 set checksum [cksum]
1187 }
drh93aed5a2008-01-16 17:46:38 +00001188
danielk197732554c12005-01-22 03:39:39 +00001189 # Set the Nth IO error to fail.
1190 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +00001191 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +00001192 set ::sqlite_io_error_pending $n
1193 }] $n
1194
1195 # Create a single TCL script from the TCL and SQL specified
1196 # as the body of the test.
1197 set ::ioerrorbody {}
1198 if {[info exists ::ioerropts(-tclbody)]} {
1199 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
1200 }
1201 if {[info exists ::ioerropts(-sqlbody)]} {
1202 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
1203 }
1204
1205 # Execute the TCL Script created in the above block. If
1206 # there are at least N IO operations performed by SQLite as
1207 # a result of the script, the Nth will fail.
1208 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +00001209 set ::sqlite_io_error_hit 0
1210 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +00001211 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +00001212 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +00001213 set rc [sqlite3_errcode $::DB]
1214 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +00001215 # If we are in extended result code mode, make sure all of the
1216 # IOERRs we get back really do have their extended code values.
1217 # If an extended result code is returned, the sqlite3_errcode
1218 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
1219 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +00001220 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
1221 return $rc
1222 }
1223 } else {
drh5f7b5bf2007-04-19 12:30:54 +00001224 # If we are not in extended result code mode, make sure no
1225 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +00001226 if {[regexp {\+\d} $rc]} {
1227 return $rc
1228 }
1229 }
drh93aed5a2008-01-16 17:46:38 +00001230 # The test repeats as long as $::go is non-zero. $::go starts out
1231 # as 1. When a test runs to completion without hitting an I/O
1232 # error, that means there is no point in continuing with this test
1233 # case so set $::go to zero.
1234 #
1235 if {$::sqlite_io_error_pending>0} {
1236 set ::go 0
1237 set q 0
1238 set ::sqlite_io_error_pending 0
1239 } else {
1240 set q 1
1241 }
1242
drhc9ac5ca2005-11-04 22:03:30 +00001243 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +00001244 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
1245 set r 1
1246 }
danielk1977f2fa8312006-01-24 13:09:33 +00001247 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +00001248
1249 # One of two things must have happened. either
1250 # 1. We never hit the IO error and the SQL returned OK
1251 # 2. An IO error was hit and the SQL failed
1252 #
dand41a29a2010-05-06 15:56:28 +00001253 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +00001254 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +00001255 } {1}
1256
danielk197780daec62008-05-12 10:57:02 +00001257 set ::sqlite_io_error_hit 0
1258 set ::sqlite_io_error_pending 0
1259
danielk197752b472a2008-05-05 16:23:55 +00001260 # Check that no page references were leaked. There should be
1261 # a single reference if there is still an active transaction,
1262 # or zero otherwise.
1263 #
danielk197781620542008-06-07 05:19:37 +00001264 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
1265 # locks are established on database files (i.e. if the error
1266 # occurs while attempting to detect a hot-journal file), then
1267 # there may 0 page references and an active transaction according
1268 # to [sqlite3_get_autocommit].
1269 #
danielk19774abd5442008-05-05 15:26:50 +00001270 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +00001271 do_test $testname.$n.4 {
1272 set bt [btree_from_db db]
1273 db_enter db
1274 array set stats [btree_pager_stats $bt]
1275 db_leave db
danielk197781620542008-06-07 05:19:37 +00001276 set nRef $stats(ref)
1277 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
1278 } {1}
danielk19774abd5442008-05-05 15:26:50 +00001279 }
1280
danielk197752b472a2008-05-05 16:23:55 +00001281 # If there is an open database handle and no open transaction,
1282 # and the pager is not running in exclusive-locking mode,
1283 # check that the pager is in "unlocked" state. Theoretically,
1284 # if a call to xUnlock() failed due to an IO error the underlying
1285 # file may still be locked.
1286 #
1287 ifcapable pragma {
1288 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +00001289 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +00001290 && [db one {pragma locking_mode}] eq "normal"
1291 && [sqlite3_get_autocommit db]
1292 } {
1293 do_test $testname.$n.5 {
1294 set bt [btree_from_db db]
1295 db_enter db
1296 array set stats [btree_pager_stats $bt]
1297 db_leave db
1298 set stats(state)
1299 } 0
1300 }
1301 }
1302
danielk197732554c12005-01-22 03:39:39 +00001303 # If an IO error occured, then the checksum of the database should
1304 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +00001305 #
drh1aa5af12008-03-07 19:51:14 +00001306 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +00001307 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +00001308 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +00001309 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +00001310 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +00001311 cksum
1312 } $checksum
1313 }
1314
drh1aa5af12008-03-07 19:51:14 +00001315 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +00001316 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +00001317 if {[info exists ::ioerropts(-cleanup)]} {
1318 catch $::ioerropts(-cleanup)
1319 }
danielk197732554c12005-01-22 03:39:39 +00001320 }
1321 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +00001322 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +00001323 unset ::ioerropts
1324}
1325
drh93aed5a2008-01-16 17:46:38 +00001326# Return a checksum based on the contents of the main database associated
1327# with connection $db
danielk197732554c12005-01-22 03:39:39 +00001328#
1329proc cksum {{db db}} {
1330 set txt [$db eval {
1331 SELECT name, type, sql FROM sqlite_master order by name
1332 }]\n
1333 foreach tbl [$db eval {
1334 SELECT name FROM sqlite_master WHERE type='table' order by name
1335 }] {
1336 append txt [$db eval "SELECT * FROM $tbl"]\n
1337 }
1338 foreach prag {default_synchronous default_cache_size} {
1339 append txt $prag-[$db eval "PRAGMA $prag"]\n
1340 }
1341 set cksum [string length $txt]-[md5 $txt]
1342 # puts $cksum-[file size test.db]
1343 return $cksum
1344}
1345
drh93aed5a2008-01-16 17:46:38 +00001346# Generate a checksum based on the contents of the main and temp tables
1347# database $db. If the checksum of two databases is the same, and the
1348# integrity-check passes for both, the two databases are identical.
1349#
drh1db639c2008-01-17 02:36:28 +00001350proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +00001351 set ret [list]
1352 ifcapable tempdb {
1353 set sql {
1354 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1355 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
1356 SELECT 'sqlite_master' UNION
1357 SELECT 'sqlite_temp_master' ORDER BY 1
1358 }
1359 } else {
1360 set sql {
1361 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1362 SELECT 'sqlite_master' ORDER BY 1
1363 }
1364 }
1365 set tbllist [$db eval $sql]
1366 set txt {}
1367 foreach tbl $tbllist {
1368 append txt [$db eval "SELECT * FROM $tbl"]
1369 }
1370 foreach prag {default_cache_size} {
1371 append txt $prag-[$db eval "PRAGMA $prag"]\n
1372 }
1373 # puts txt=$txt
1374 return [md5 $txt]
1375}
1376
drhdc2c4912009-02-04 22:46:47 +00001377# Generate a checksum based on the contents of a single database with
1378# a database connection. The name of the database is $dbname.
1379# Examples of $dbname are "temp" or "main".
1380#
1381proc dbcksum {db dbname} {
1382 if {$dbname=="temp"} {
1383 set master sqlite_temp_master
1384 } else {
1385 set master $dbname.sqlite_master
1386 }
1387 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
1388 set txt [$db eval "SELECT * FROM $master"]\n
1389 foreach tab $alltab {
1390 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
1391 }
1392 return [md5 $txt]
1393}
1394
danielk197735754ac2008-03-21 17:29:37 +00001395proc memdebug_log_sql {{filename mallocs.sql}} {
1396
danielk19776f332c12008-03-21 14:22:44 +00001397 set data [sqlite3_memdebug_log dump]
1398 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +00001399 if {$nFrame < 0} { return "" }
1400
danielk197735754ac2008-03-21 17:29:37 +00001401 set database temp
1402
danielk19776ab3a2e2009-02-19 14:39:25 +00001403 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +00001404
1405 set sql ""
1406 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +00001407 set nCall [lindex $e 0]
1408 set nByte [lindex $e 1]
1409 set lStack [lrange $e 2 end]
1410 append sql "INSERT INTO ${database}.malloc VALUES"
1411 append sql "('test', $nCall, $nByte, '$lStack');\n"
1412 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +00001413 set frames($f) 1
1414 }
1415 }
1416
1417 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +00001418 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +00001419
1420 foreach f [array names frames] {
1421 set addr [format %x $f]
1422 set cmd "addr2line -e [info nameofexec] $addr"
1423 set line [eval exec $cmd]
1424 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00001425
1426 set file [lindex [split $line :] 0]
1427 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00001428 }
1429
danielk197735754ac2008-03-21 17:29:37 +00001430 foreach f [array names files] {
1431 set contents ""
1432 catch {
1433 set fd [open $f]
1434 set contents [read $fd]
1435 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001436 }
danielk197735754ac2008-03-21 17:29:37 +00001437 set contents [string map {' ''} $contents]
1438 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00001439 }
danielk19776f332c12008-03-21 14:22:44 +00001440
danielk197735754ac2008-03-21 17:29:37 +00001441 set fd [open $filename w]
1442 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
1443 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001444}
drh93aed5a2008-01-16 17:46:38 +00001445
danf5894502009-10-07 18:41:19 +00001446# Drop all tables in database [db]
1447proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00001448 ifcapable trigger&&foreignkey {
1449 set pk [$db one "PRAGMA foreign_keys"]
1450 $db eval "PRAGMA foreign_keys = OFF"
1451 }
drh9a6ffc82010-02-15 18:03:20 +00001452 foreach {idx name file} [db eval {PRAGMA database_list}] {
1453 if {$idx==1} {
1454 set master sqlite_temp_master
1455 } else {
1456 set master $name.sqlite_master
1457 }
1458 foreach {t type} [$db eval "
1459 SELECT name, type FROM $master
dana16d1062010-09-28 17:37:28 +00001460 WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X'
drh9a6ffc82010-02-15 18:03:20 +00001461 "] {
dana16d1062010-09-28 17:37:28 +00001462 $db eval "DROP $type \"$t\""
drh9a6ffc82010-02-15 18:03:20 +00001463 }
danf5894502009-10-07 18:41:19 +00001464 }
shaneh4e7b32f2009-12-17 22:12:51 +00001465 ifcapable trigger&&foreignkey {
1466 $db eval "PRAGMA foreign_keys = $pk"
1467 }
danf5894502009-10-07 18:41:19 +00001468}
1469
dan71cb5182010-04-26 12:39:03 +00001470#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00001471# If a test script is executed with global variable $::G(perm:name) set to
1472# "wal", then the tests are run in WAL mode. Otherwise, they should be run
1473# in rollback mode. The following Tcl procs are used to make this less
1474# intrusive:
dan71cb5182010-04-26 12:39:03 +00001475#
1476# wal_set_journal_mode ?DB?
1477#
1478# If running a WAL test, execute "PRAGMA journal_mode = wal" using
1479# connection handle DB. Otherwise, this command is a no-op.
1480#
1481# wal_check_journal_mode TESTNAME ?DB?
1482#
1483# If running a WAL test, execute a tests case that fails if the main
1484# database for connection handle DB is not currently a WAL database.
1485# Otherwise (if not running a WAL permutation) this is a no-op.
1486#
1487# wal_is_wal_mode
1488#
1489# Returns true if this test should be run in WAL mode. False otherwise.
1490#
1491proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00001492 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00001493}
1494proc wal_set_journal_mode {{db db}} {
1495 if { [wal_is_wal_mode] } {
1496 $db eval "PRAGMA journal_mode = WAL"
1497 }
1498}
1499proc wal_check_journal_mode {testname {db db}} {
1500 if { [wal_is_wal_mode] } {
1501 $db eval { SELECT * FROM sqlite_master }
1502 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
1503 }
1504}
1505
dan430e74c2010-06-07 17:47:26 +00001506proc permutation {} {
1507 set perm ""
1508 catch {set perm $::G(perm:name)}
1509 set perm
1510}
dancb79e512010-08-06 13:50:07 +00001511proc presql {} {
1512 set presql ""
1513 catch {set presql $::G(perm:presql)}
1514 set presql
1515}
dan430e74c2010-06-07 17:47:26 +00001516
danc1a60c52010-06-07 14:28:16 +00001517#-------------------------------------------------------------------------
1518#
1519proc slave_test_script {script} {
1520
1521 # Create the interpreter used to run the test script.
1522 interp create tinterp
1523
1524 # Populate some global variables that tester.tcl expects to see.
1525 foreach {var value} [list \
1526 ::argv0 $::argv0 \
1527 ::argv {} \
1528 ::SLAVE 1 \
1529 ] {
1530 interp eval tinterp [list set $var $value]
1531 }
1532
1533 # The alias used to access the global test counters.
1534 tinterp alias set_test_counter set_test_counter
1535
1536 # Set up the ::cmdlinearg array in the slave.
1537 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
1538
dan430e74c2010-06-07 17:47:26 +00001539 # Set up the ::G array in the slave.
1540 interp eval tinterp [list array set ::G [array get ::G]]
1541
danc1a60c52010-06-07 14:28:16 +00001542 # Load the various test interfaces implemented in C.
1543 load_testfixture_extensions tinterp
1544
1545 # Run the test script.
1546 interp eval tinterp $script
1547
danea5542d2010-07-06 11:26:15 +00001548 # Check if the interpreter call [run_thread_tests]
1549 if { [interp eval tinterp {info exists ::run_thread_tests_called}] } {
1550 set ::run_thread_tests_called 1
1551 }
1552
danc1a60c52010-06-07 14:28:16 +00001553 # Delete the interpreter used to run the test script.
1554 interp delete tinterp
1555}
1556
dan430e74c2010-06-07 17:47:26 +00001557proc slave_test_file {zFile} {
dan0626dfc2010-06-15 06:56:37 +00001558 set tail [file tail $zFile]
dan430e74c2010-06-07 17:47:26 +00001559
dan6a64d672011-04-04 15:38:16 +00001560 if {[info exists ::G(start:permutation)]} {
1561 if {[permutation] != $::G(start:permutation)} return
1562 unset ::G(start:permutation)
1563 }
1564 if {[info exists ::G(start:file)]} {
1565 if {$tail != $::G(start:file) && $tail!="$::G(start:file).test"} return
1566 unset ::G(start:file)
1567 }
1568
dan92d516a2010-07-05 14:54:48 +00001569 # Remember the value of the shared-cache setting. So that it is possible
1570 # to check afterwards that it was not modified by the test script.
1571 #
1572 ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] }
dan0626dfc2010-06-15 06:56:37 +00001573
dan92d516a2010-07-05 14:54:48 +00001574 # Run the test script in a slave interpreter.
1575 #
danea5542d2010-07-06 11:26:15 +00001576 unset -nocomplain ::run_thread_tests_called
dan0626dfc2010-06-15 06:56:37 +00001577 reset_prng_state
1578 set ::sqlite_open_file_count 0
1579 set time [time { slave_test_script [list source $zFile] }]
1580 set ms [expr [lindex $time 0] / 1000]
1581
dan92d516a2010-07-05 14:54:48 +00001582 # Test that all files opened by the test script were closed. Omit this
1583 # if the test script has "thread" in its name. The open file counter
1584 # is not thread-safe.
dan0626dfc2010-06-15 06:56:37 +00001585 #
danea5542d2010-07-06 11:26:15 +00001586 if {[info exists ::run_thread_tests_called]==0} {
dan92d516a2010-07-05 14:54:48 +00001587 do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0}
1588 }
dan430e74c2010-06-07 17:47:26 +00001589 set ::sqlite_open_file_count 0
1590
dan0626dfc2010-06-15 06:56:37 +00001591 # Test that the global "shared-cache" setting was not altered by
1592 # the test script.
1593 #
1594 ifcapable shared_cache {
1595 set res [expr {[sqlite3_enable_shared_cache] == $scs}]
1596 do_test ${tail}-sharedcachesetting [list set {} $res] 1
1597 }
dan430e74c2010-06-07 17:47:26 +00001598
dan92d516a2010-07-05 14:54:48 +00001599 # Add some info to the output.
1600 #
dan0626dfc2010-06-15 06:56:37 +00001601 puts "Time: $tail $ms ms"
dan430e74c2010-06-07 17:47:26 +00001602 show_memstats
danc1a60c52010-06-07 14:28:16 +00001603}
1604
dan59257dc2010-08-04 11:34:31 +00001605# Open a new connection on database test.db and execute the SQL script
1606# supplied as an argument. Before returning, close the new conection and
1607# restore the 4 byte fields starting at header offsets 28, 92 and 96
1608# to the values they held before the SQL was executed. This simulates
1609# a write by a pre-3.7.0 client.
1610#
1611proc sql36231 {sql} {
1612 set B [hexio_read test.db 92 8]
1613 set A [hexio_read test.db 28 4]
1614 sqlite3 db36231 test.db
1615 catch { db36231 func a_string a_string }
1616 execsql $sql db36231
1617 db36231 close
1618 hexio_write test.db 28 $A
1619 hexio_write test.db 92 $B
1620 return ""
1621}
danf5894502009-10-07 18:41:19 +00001622
danccc7ff42010-11-29 12:06:45 +00001623proc db_save {} {
1624 foreach f [glob -nocomplain sv_test.db*] { forcedelete $f }
1625 foreach f [glob -nocomplain test.db*] {
1626 set f2 "sv_$f"
mistachkinfda06be2011-08-02 00:57:34 +00001627 forcecopy $f $f2
danccc7ff42010-11-29 12:06:45 +00001628 }
1629}
1630proc db_save_and_close {} {
1631 db_save
1632 catch { db close }
1633 return ""
1634}
1635proc db_restore {} {
1636 foreach f [glob -nocomplain test.db*] { forcedelete $f }
1637 foreach f2 [glob -nocomplain sv_test.db*] {
1638 set f [string range $f2 3 end]
mistachkinfda06be2011-08-02 00:57:34 +00001639 forcecopy $f2 $f
danccc7ff42010-11-29 12:06:45 +00001640 }
1641}
1642proc db_restore_and_reopen {{dbfile test.db}} {
1643 catch { db close }
1644 db_restore
1645 sqlite3 db $dbfile
1646}
1647proc db_delete_and_reopen {{file test.db}} {
1648 catch { db close }
mistachkinfda06be2011-08-02 00:57:34 +00001649 foreach f [glob -nocomplain test.db*] { forcedelete $f }
danccc7ff42010-11-29 12:06:45 +00001650 sqlite3 db $file
1651}
1652
danielk197745901d62004-11-10 15:27:38 +00001653# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
1654# to non-zero, then set the global variable $AUTOVACUUM to 1.
1655set AUTOVACUUM $sqlite_options(default_autovacuum)
danielk1977ee8b7992009-03-26 17:13:06 +00001656
dan64b95bb2012-05-12 05:30:29 +00001657# Make sure the FTS enhanced query syntax is disabled.
1658set sqlite_fts3_enable_parentheses 0
1659
danielk1977ee8b7992009-03-26 17:13:06 +00001660source $testdir/thread_common.tcl
danddf80eb2010-10-25 12:47:43 +00001661source $testdir/malloc_common.tcl