blob: 37ca69f497e30a57fd7fdb89d95cc61d74c79273 [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drh348784e2000-05-29 20:41:49 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drh348784e2000-05-29 20:41:49 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# May you do good and not evil.
7# May you find forgiveness for yourself and forgive others.
8# May you share freely, never taking more than you give.
drh348784e2000-05-29 20:41:49 +00009#
10#***********************************************************************
11# This file implements some common TCL routines used for regression
12# testing the SQLite library
13#
drhbb77b752009-04-09 01:23:49 +000014# $Id: tester.tcl,v 1.143 2009/04/09 01:23:49 drh Exp $
drhfbc3eab2001-04-06 16:13:42 +000015
danc1a60c52010-06-07 14:28:16 +000016#-------------------------------------------------------------------------
mistachkin1d406e02013-08-29 01:09:14 +000017# The commands provided by the code in this file to help with creating
danc1a60c52010-06-07 14:28:16 +000018# test cases are as follows:
drh8359d8c2008-03-29 11:00:54 +000019#
danc1a60c52010-06-07 14:28:16 +000020# Commands to manipulate the db and the file-system at a high level:
drh8359d8c2008-03-29 11:00:54 +000021#
mistachkinc5484652012-03-05 22:52:33 +000022# is_relative_file
mistachkin4a41f342012-03-06 03:00:49 +000023# test_pwd
mistachkinf8a78462012-03-08 20:00:36 +000024# get_pwd
danc1a60c52010-06-07 14:28:16 +000025# copy_file FROM TO
mistachkinfda06be2011-08-02 00:57:34 +000026# delete_file FILENAME
dan6f343962011-07-01 18:26:40 +000027# drop_all_tables ?DB?
mistachkinfda06be2011-08-02 00:57:34 +000028# forcecopy FROM TO
danc1a60c52010-06-07 14:28:16 +000029# forcedelete FILENAME
30#
31# Test the capability of the SQLite version built into the interpreter to
32# determine if a specific test can be run:
33#
mistachkinc60941f2012-09-13 01:51:02 +000034# capable EXPR
danc1a60c52010-06-07 14:28:16 +000035# ifcapable EXPR
36#
37# Calulate checksums based on database contents:
38#
39# dbcksum DB DBNAME
40# allcksum ?DB?
41# cksum ?DB?
42#
43# Commands to execute/explain SQL statements:
44#
mistachkin1d406e02013-08-29 01:09:14 +000045# memdbsql SQL
danc1a60c52010-06-07 14:28:16 +000046# stepsql DB SQL
47# execsql2 SQL
48# explain_no_trace SQL
49# explain SQL ?DB?
50# catchsql SQL ?DB?
51# execsql SQL ?DB?
52#
53# Commands to run test cases:
54#
55# do_ioerr_test TESTNAME ARGS...
56# crashsql ARGS...
57# integrity_check TESTNAME ?DB?
drh433dccf2013-02-09 15:37:11 +000058# verify_ex_errcode TESTNAME EXPECTED ?DB?
danc1a60c52010-06-07 14:28:16 +000059# do_test TESTNAME SCRIPT EXPECTED
dan153eda02010-06-21 07:45:47 +000060# do_execsql_test TESTNAME SQL EXPECTED
61# do_catchsql_test TESTNAME SQL EXPECTED
drh3c2aeae2014-01-24 14:37:44 +000062# do_timed_execsql_test TESTNAME SQL EXPECTED
danc1a60c52010-06-07 14:28:16 +000063#
dan430e74c2010-06-07 17:47:26 +000064# Commands providing a lower level interface to the global test counters:
danc1a60c52010-06-07 14:28:16 +000065#
66# set_test_counter COUNTER ?VALUE?
mistachkin6c3c1a02011-11-12 03:17:40 +000067# omit_test TESTNAME REASON ?APPEND?
danc1a60c52010-06-07 14:28:16 +000068# fail_test TESTNAME
69# incr_ntest
70#
71# Command run at the end of each test file:
72#
73# finish_test
74#
dan430e74c2010-06-07 17:47:26 +000075# Commands to help create test files that run with the "WAL" and other
76# permutations (see file permutations.test):
danc1a60c52010-06-07 14:28:16 +000077#
78# wal_is_wal_mode
79# wal_set_journal_mode ?DB?
80# wal_check_journal_mode TESTNAME?DB?
dan430e74c2010-06-07 17:47:26 +000081# permutation
dancb79e512010-08-06 13:50:07 +000082# presql
danc1a60c52010-06-07 14:28:16 +000083#
drh348784e2000-05-29 20:41:49 +000084
mistachkin1d406e02013-08-29 01:09:14 +000085# Set the precision of FP arithmatic used by the interpreter. And
danc1a60c52010-06-07 14:28:16 +000086# configure SQLite to take database file locks on the page that begins
87# 64KB into the database file instead of the one 1GB in. This means
88# the code that handles that special case can be tested without creating
89# very large database files.
90#
drh92febd92004-08-20 18:34:20 +000091set tcl_precision 15
drhc7a3bb92009-02-05 16:31:45 +000092sqlite3_test_control_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +000093
danc1a60c52010-06-07 14:28:16 +000094
mistachkin1d406e02013-08-29 01:09:14 +000095# If the pager codec is available, create a wrapper for the [sqlite3]
danc1a60c52010-06-07 14:28:16 +000096# command that appends "-key {xyzzy}" to the command line. i.e. this:
drh3a7fb7c2007-08-10 16:41:08 +000097#
danc1a60c52010-06-07 14:28:16 +000098# sqlite3 db test.db
drh4a50aac2007-08-23 02:47:53 +000099#
danc1a60c52010-06-07 14:28:16 +0000100# becomes
drh4a50aac2007-08-23 02:47:53 +0000101#
danc1a60c52010-06-07 14:28:16 +0000102# sqlite3 db test.db -key {xyzzy}
drh9eb9e262004-02-11 02:18:05 +0000103#
dan430e74c2010-06-07 17:47:26 +0000104if {[info command sqlite_orig]==""} {
drhef4ac8f2004-06-19 00:16:31 +0000105 rename sqlite3 sqlite_orig
106 proc sqlite3 {args} {
dan68928b62010-06-22 13:46:43 +0000107 if {[llength $args]>=2 && [string index [lindex $args 0] 0]!="-"} {
dan430e74c2010-06-07 17:47:26 +0000108 # This command is opening a new database connection.
109 #
110 if {[info exists ::G(perm:sqlite3_args)]} {
111 set args [concat $args $::G(perm:sqlite3_args)]
112 }
dan68928b62010-06-22 13:46:43 +0000113 if {[sqlite_orig -has-codec] && ![info exists ::do_not_use_codec]} {
dan430e74c2010-06-07 17:47:26 +0000114 lappend args -key {xyzzy}
115 }
116
dan3ccd20a2010-06-09 19:01:02 +0000117 set res [uplevel 1 sqlite_orig $args]
dan430e74c2010-06-07 17:47:26 +0000118 if {[info exists ::G(perm:presql)]} {
119 [lindex $args 0] eval $::G(perm:presql)
120 }
dan1ce1b4a2010-12-07 14:32:28 +0000121 if {[info exists ::G(perm:dbconfig)]} {
122 set ::dbhandle [lindex $args 0]
123 uplevel #0 $::G(perm:dbconfig)
124 }
dan3ccd20a2010-06-09 19:01:02 +0000125 set res
dan430e74c2010-06-07 17:47:26 +0000126 } else {
mistachkin1d406e02013-08-29 01:09:14 +0000127 # This command is not opening a new database connection. Pass the
mistachkin48864df2013-03-21 21:20:32 +0000128 # arguments through to the C implementation as the are.
dan430e74c2010-06-07 17:47:26 +0000129 #
130 uplevel 1 sqlite_orig $args
drh9eb9e262004-02-11 02:18:05 +0000131 }
drh9eb9e262004-02-11 02:18:05 +0000132 }
133}
134
mistachkinfda06be2011-08-02 00:57:34 +0000135proc getFileRetries {} {
136 if {![info exists ::G(file-retries)]} {
137 #
138 # NOTE: Return the default number of retries for [file] operations. A
139 # value of zero or less here means "disabled".
140 #
mistachkinc60941f2012-09-13 01:51:02 +0000141 return [expr {$::tcl_platform(platform) eq "windows" ? 50 : 0}]
mistachkinfda06be2011-08-02 00:57:34 +0000142 }
143 return $::G(file-retries)
144}
145
146proc getFileRetryDelay {} {
147 if {![info exists ::G(file-retry-delay)]} {
148 #
149 # NOTE: Return the default number of milliseconds to wait when retrying
150 # failed [file] operations. A value of zero or less means "do not
151 # wait".
152 #
153 return 100; # TODO: Good default?
154 }
155 return $::G(file-retry-delay)
156}
157
mistachkinf8a78462012-03-08 20:00:36 +0000158# Return the string representing the name of the current directory. On
159# Windows, the result is "normalized" to whatever our parent command shell
160# is using to prevent case-mismatch issues.
161#
162proc get_pwd {} {
163 if {$::tcl_platform(platform) eq "windows"} {
mistachkin533b8f62012-03-08 20:28:31 +0000164 #
165 # NOTE: Cannot use [file normalize] here because it would alter the
166 # case of the result to what Tcl considers canonical, which would
167 # defeat the purpose of this procedure.
168 #
169 return [string map [list \\ /] \
170 [string trim [exec -- $::env(ComSpec) /c echo %CD%]]]
mistachkinf8a78462012-03-08 20:00:36 +0000171 } else {
172 return [pwd]
173 }
174}
175
mistachkinfda06be2011-08-02 00:57:34 +0000176# Copy file $from into $to. This is used because some versions of
177# TCL for windows (notably the 8.4.1 binary package shipped with the
178# current mingw release) have a broken "file copy" command.
179#
180proc copy_file {from to} {
181 do_copy_file false $from $to
182}
183
184proc forcecopy {from to} {
185 do_copy_file true $from $to
186}
187
188proc do_copy_file {force from to} {
189 set nRetry [getFileRetries] ;# Maximum number of retries.
190 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
191
192 # On windows, sometimes even a [file copy -force] can fail. The cause is
193 # usually "tag-alongs" - programs like anti-virus software, automatic backup
194 # tools and various explorer extensions that keep a file open a little longer
195 # than we expect, causing the delete to fail.
196 #
197 # The solution is to wait a short amount of time before retrying the copy.
198 #
199 if {$nRetry > 0} {
200 for {set i 0} {$i<$nRetry} {incr i} {
201 set rc [catch {
202 if {$force} {
203 file copy -force $from $to
204 } else {
205 file copy $from $to
206 }
207 } msg]
208 if {$rc==0} break
209 if {$nDelay > 0} { after $nDelay }
210 }
211 if {$rc} { error $msg }
212 } else {
213 if {$force} {
214 file copy -force $from $to
215 } else {
216 file copy $from $to
217 }
218 }
219}
220
mistachkinc5484652012-03-05 22:52:33 +0000221# Check if a file name is relative
222#
223proc is_relative_file { file } {
224 return [expr {[file pathtype $file] != "absolute"}]
225}
226
mistachkin4a41f342012-03-06 03:00:49 +0000227# If the VFS supports using the current directory, returns [pwd];
228# otherwise, it returns only the provided suffix string (which is
229# empty by default).
230#
231proc test_pwd { args } {
232 if {[llength $args] > 0} {
233 set suffix1 [lindex $args 0]
234 if {[llength $args] > 1} {
235 set suffix2 [lindex $args 1]
236 } else {
237 set suffix2 $suffix1
238 }
239 } else {
240 set suffix1 ""; set suffix2 ""
241 }
242 ifcapable curdir {
mistachkin6aa18c92012-03-08 20:22:42 +0000243 return "[get_pwd]$suffix1"
mistachkin4a41f342012-03-06 03:00:49 +0000244 } else {
245 return $suffix2
246 }
247}
248
mistachkinfda06be2011-08-02 00:57:34 +0000249# Delete a file or directory
250#
251proc delete_file {args} {
252 do_delete_file false {*}$args
253}
254
255proc forcedelete {args} {
256 do_delete_file true {*}$args
257}
258
259proc do_delete_file {force args} {
260 set nRetry [getFileRetries] ;# Maximum number of retries.
261 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
262
263 foreach filename $args {
264 # On windows, sometimes even a [file delete -force] can fail just after
265 # a file is closed. The cause is usually "tag-alongs" - programs like
266 # anti-virus software, automatic backup tools and various explorer
267 # extensions that keep a file open a little longer than we expect, causing
268 # the delete to fail.
269 #
270 # The solution is to wait a short amount of time before retrying the
271 # delete.
272 #
273 if {$nRetry > 0} {
274 for {set i 0} {$i<$nRetry} {incr i} {
275 set rc [catch {
276 if {$force} {
277 file delete -force $filename
278 } else {
279 file delete $filename
280 }
281 } msg]
282 if {$rc==0} break
283 if {$nDelay > 0} { after $nDelay }
284 }
285 if {$rc} { error $msg }
286 } else {
287 if {$force} {
288 file delete -force $filename
289 } else {
290 file delete $filename
291 }
292 }
293 }
294}
295
mistachkin1d406e02013-08-29 01:09:14 +0000296if {$::tcl_platform(platform) eq "windows"} {
297 proc do_remove_win32_dir {args} {
298 set nRetry [getFileRetries] ;# Maximum number of retries.
299 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
300
301 foreach dirName $args {
302 # On windows, sometimes even a [remove_win32_dir] can fail just after
303 # a directory is emptied. The cause is usually "tag-alongs" - programs
304 # like anti-virus software, automatic backup tools and various explorer
305 # extensions that keep a file open a little longer than we expect,
306 # causing the delete to fail.
307 #
308 # The solution is to wait a short amount of time before retrying the
309 # removal.
310 #
311 if {$nRetry > 0} {
312 for {set i 0} {$i < $nRetry} {incr i} {
313 set rc [catch {
314 remove_win32_dir $dirName
315 } msg]
316 if {$rc == 0} break
317 if {$nDelay > 0} { after $nDelay }
318 }
319 if {$rc} { error $msg }
320 } else {
321 remove_win32_dir $dirName
322 }
323 }
324 }
325
326 proc do_delete_win32_file {args} {
327 set nRetry [getFileRetries] ;# Maximum number of retries.
328 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
329
330 foreach fileName $args {
331 # On windows, sometimes even a [delete_win32_file] can fail just after
332 # a file is closed. The cause is usually "tag-alongs" - programs like
333 # anti-virus software, automatic backup tools and various explorer
334 # extensions that keep a file open a little longer than we expect,
335 # causing the delete to fail.
336 #
337 # The solution is to wait a short amount of time before retrying the
338 # delete.
339 #
340 if {$nRetry > 0} {
341 for {set i 0} {$i < $nRetry} {incr i} {
342 set rc [catch {
343 delete_win32_file $fileName
344 } msg]
345 if {$rc == 0} break
346 if {$nDelay > 0} { after $nDelay }
347 }
348 if {$rc} { error $msg }
349 } else {
350 delete_win32_file $fileName
351 }
352 }
353 }
354}
355
dancb354602010-07-08 09:44:42 +0000356proc execpresql {handle args} {
357 trace remove execution $handle enter [list execpresql $handle]
358 if {[info exists ::G(perm:presql)]} {
359 $handle eval $::G(perm:presql)
360 }
361}
362
dan68928b62010-06-22 13:46:43 +0000363# This command should be called after loading tester.tcl from within
364# all test scripts that are incompatible with encryption codecs.
365#
366proc do_not_use_codec {} {
367 set ::do_not_use_codec 1
368 reset_db
369}
370
dan430e74c2010-06-07 17:47:26 +0000371# The following block only runs the first time this file is sourced. It
372# does not run in slave interpreters (since the ::cmdlinearg array is
373# populated before the test script is run in slave interpreters).
drhbec2bf42000-05-29 23:48:22 +0000374#
danc1a60c52010-06-07 14:28:16 +0000375if {[info exists cmdlinearg]==0} {
376
mistachkin1d406e02013-08-29 01:09:14 +0000377 # Parse any options specified in the $argv array. This script accepts the
378 # following options:
danc1a60c52010-06-07 14:28:16 +0000379 #
380 # --pause
381 # --soft-heap-limit=NN
382 # --maxerror=NN
383 # --malloctrace=N
384 # --backtrace=N
385 # --binarylog=N
dan0626dfc2010-06-15 06:56:37 +0000386 # --soak=N
mistachkinfda06be2011-08-02 00:57:34 +0000387 # --file-retries=N
388 # --file-retry-delay=N
dan6a64d672011-04-04 15:38:16 +0000389 # --start=[$permutation:]$testfile
mistachkin6c3c1a02011-11-12 03:17:40 +0000390 # --match=$pattern
danc1a60c52010-06-07 14:28:16 +0000391 #
392 set cmdlinearg(soft-heap-limit) 0
393 set cmdlinearg(maxerror) 1000
394 set cmdlinearg(malloctrace) 0
395 set cmdlinearg(backtrace) 10
396 set cmdlinearg(binarylog) 0
dan0626dfc2010-06-15 06:56:37 +0000397 set cmdlinearg(soak) 0
mistachkinfda06be2011-08-02 00:57:34 +0000398 set cmdlinearg(file-retries) 0
399 set cmdlinearg(file-retry-delay) 0
mistachkin6c3c1a02011-11-12 03:17:40 +0000400 set cmdlinearg(start) ""
401 set cmdlinearg(match) ""
danc1a60c52010-06-07 14:28:16 +0000402
403 set leftover [list]
404 foreach a $argv {
405 switch -regexp -- $a {
406 {^-+pause$} {
mistachkin1d406e02013-08-29 01:09:14 +0000407 # Wait for user input before continuing. This is to give the user an
danc1a60c52010-06-07 14:28:16 +0000408 # opportunity to connect profiling tools to the process.
409 puts -nonewline "Press RETURN to begin..."
410 flush stdout
411 gets stdin
412 }
413 {^-+soft-heap-limit=.+$} {
414 foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break
415 }
416 {^-+maxerror=.+$} {
417 foreach {dummy cmdlinearg(maxerror)} [split $a =] break
418 }
419 {^-+malloctrace=.+$} {
420 foreach {dummy cmdlinearg(malloctrace)} [split $a =] break
421 if {$cmdlinearg(malloctrace)} {
422 sqlite3_memdebug_log start
423 }
424 }
425 {^-+backtrace=.+$} {
426 foreach {dummy cmdlinearg(backtrace)} [split $a =] break
dan0626dfc2010-06-15 06:56:37 +0000427 sqlite3_memdebug_backtrace $value
danc1a60c52010-06-07 14:28:16 +0000428 }
danc1a60c52010-06-07 14:28:16 +0000429 {^-+binarylog=.+$} {
430 foreach {dummy cmdlinearg(binarylog)} [split $a =] break
431 }
dan0626dfc2010-06-15 06:56:37 +0000432 {^-+soak=.+$} {
433 foreach {dummy cmdlinearg(soak)} [split $a =] break
434 set ::G(issoak) $cmdlinearg(soak)
435 }
mistachkinfda06be2011-08-02 00:57:34 +0000436 {^-+file-retries=.+$} {
437 foreach {dummy cmdlinearg(file-retries)} [split $a =] break
438 set ::G(file-retries) $cmdlinearg(file-retries)
439 }
440 {^-+file-retry-delay=.+$} {
441 foreach {dummy cmdlinearg(file-retry-delay)} [split $a =] break
442 set ::G(file-retry-delay) $cmdlinearg(file-retry-delay)
443 }
dan6a64d672011-04-04 15:38:16 +0000444 {^-+start=.+$} {
445 foreach {dummy cmdlinearg(start)} [split $a =] break
446
447 set ::G(start:file) $cmdlinearg(start)
448 if {[regexp {(.*):(.*)} $cmdlinearg(start) -> s.perm s.file]} {
449 set ::G(start:permutation) ${s.perm}
450 set ::G(start:file) ${s.file}
451 }
452 if {$::G(start:file) == ""} {unset ::G(start:file)}
453 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000454 {^-+match=.+$} {
455 foreach {dummy cmdlinearg(match)} [split $a =] break
456
457 set ::G(match) $cmdlinearg(match)
458 if {$::G(match) == ""} {unset ::G(match)}
459 }
danc1a60c52010-06-07 14:28:16 +0000460 default {
461 lappend leftover $a
462 }
463 }
464 }
465 set argv $leftover
466
dan430e74c2010-06-07 17:47:26 +0000467 # Install the malloc layer used to inject OOM errors. And the 'automatic'
468 # extensions. This only needs to be done once for the process.
469 #
mistachkin1d406e02013-08-29 01:09:14 +0000470 sqlite3_shutdown
471 install_malloc_faultsim 1
danielk1977f7b9d662008-06-23 18:49:43 +0000472 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000473 autoinstall_test_functions
danc1a60c52010-06-07 14:28:16 +0000474
dan430e74c2010-06-07 17:47:26 +0000475 # If the --binarylog option was specified, create the logging VFS. This
476 # call installs the new VFS as the default for all SQLite connections.
477 #
danc1a60c52010-06-07 14:28:16 +0000478 if {$cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000479 vfslog new binarylog {} vfslog.bin
danc1a60c52010-06-07 14:28:16 +0000480 }
481
482 # Set the backtrace depth, if malloc tracing is enabled.
483 #
484 if {$cmdlinearg(malloctrace)} {
485 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danielk1977185eac92008-07-12 15:55:54 +0000486 }
danielk1977f7b9d662008-06-23 18:49:43 +0000487}
danielk197703ba3fa2009-01-09 10:49:14 +0000488
danc1a60c52010-06-07 14:28:16 +0000489# Update the soft-heap-limit each time this script is run. In that
490# way if an individual test file changes the soft-heap-limit, it
491# will be reset at the start of the next test file.
492#
493sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
494
495# Create a test database
496#
danielk197703ba3fa2009-01-09 10:49:14 +0000497proc reset_db {} {
498 catch {db close}
mistachkinfda06be2011-08-02 00:57:34 +0000499 forcedelete test.db
500 forcedelete test.db-journal
501 forcedelete test.db-wal
danielk197703ba3fa2009-01-09 10:49:14 +0000502 sqlite3 db ./test.db
503 set ::DB [sqlite3_connection_pointer db]
504 if {[info exists ::SETUP_SQL]} {
505 db eval $::SETUP_SQL
506 }
drhcd61c282002-03-06 22:01:34 +0000507}
danielk197703ba3fa2009-01-09 10:49:14 +0000508reset_db
drhbec2bf42000-05-29 23:48:22 +0000509
510# Abort early if this script has been run before.
511#
danc1a60c52010-06-07 14:28:16 +0000512if {[info exists TC(count)]} return
drhbec2bf42000-05-29 23:48:22 +0000513
drhce2198c2010-09-10 13:22:59 +0000514# Make sure memory statistics are enabled.
515#
516sqlite3_config_memstatus 1
517
danc1a60c52010-06-07 14:28:16 +0000518# Initialize the test counters and set up commands to access them.
519# Or, if this is a slave interpreter, set up aliases to write the
520# counters in the parent interpreter.
drhbec2bf42000-05-29 23:48:22 +0000521#
danc1a60c52010-06-07 14:28:16 +0000522if {0==[info exists ::SLAVE]} {
523 set TC(errors) 0
524 set TC(count) 0
525 set TC(fail_list) [list]
526 set TC(omit_list) [list]
drhcca17c32013-04-19 12:32:52 +0000527 set TC(warn_list) [list]
danc1a60c52010-06-07 14:28:16 +0000528
529 proc set_test_counter {counter args} {
530 if {[llength $args]} {
531 set ::TC($counter) [lindex $args 0]
532 }
533 set ::TC($counter)
534 }
drhb62c3352006-11-23 09:39:16 +0000535}
drh348784e2000-05-29 20:41:49 +0000536
drh521cc842008-04-15 02:36:33 +0000537# Record the fact that a sequence of tests were omitted.
538#
mistachkin6c3c1a02011-11-12 03:17:40 +0000539proc omit_test {name reason {append 1}} {
danc1a60c52010-06-07 14:28:16 +0000540 set omitList [set_test_counter omit_list]
mistachkin6c3c1a02011-11-12 03:17:40 +0000541 if {$append} {
542 lappend omitList [list $name $reason]
543 }
danc1a60c52010-06-07 14:28:16 +0000544 set_test_counter omit_list $omitList
drh521cc842008-04-15 02:36:33 +0000545}
546
danc1a60c52010-06-07 14:28:16 +0000547# Record the fact that a test failed.
548#
549proc fail_test {name} {
550 set f [set_test_counter fail_list]
551 lappend f $name
552 set_test_counter fail_list $f
553 set_test_counter errors [expr [set_test_counter errors] + 1]
554
555 set nFail [set_test_counter errors]
556 if {$nFail>=$::cmdlinearg(maxerror)} {
557 puts "*** Giving up..."
558 finalize_testing
559 }
560}
561
drhcca17c32013-04-19 12:32:52 +0000562# Remember a warning message to be displayed at the conclusion of all testing
563#
564proc warning {msg {append 1}} {
565 puts "Warning: $msg"
566 set warnList [set_test_counter warn_list]
567 if {$append} {
568 lappend warnList $msg
569 }
570 set_test_counter warn_list $warnList
571}
572
573
danc1a60c52010-06-07 14:28:16 +0000574# Increment the number of tests run
575#
576proc incr_ntest {} {
577 set_test_counter count [expr [set_test_counter count] + 1]
578}
579
580
mistachkin1d406e02013-08-29 01:09:14 +0000581# Invoke the do_test procedure to run a single test
drh348784e2000-05-29 20:41:49 +0000582#
583proc do_test {name cmd expected} {
danc1a60c52010-06-07 14:28:16 +0000584 global argv cmdlinearg
585
dan8c408002010-11-01 17:38:24 +0000586 fix_testname name
587
drh4a50aac2007-08-23 02:47:53 +0000588 sqlite3_memdebug_settitle $name
danc1a60c52010-06-07 14:28:16 +0000589
mistachkin1d406e02013-08-29 01:09:14 +0000590# if {[llength $argv]==0} {
dan92d516a2010-07-05 14:54:48 +0000591# set go 1
592# } else {
593# set go 0
594# foreach pattern $argv {
595# if {[string match $pattern $name]} {
596# set go 1
597# break
598# }
599# }
600# }
dan430e74c2010-06-07 17:47:26 +0000601
dand506de02010-07-03 13:59:01 +0000602 if {[info exists ::G(perm:prefix)]} {
603 set name "$::G(perm:prefix)$name"
dan430e74c2010-06-07 17:47:26 +0000604 }
605
danc1a60c52010-06-07 14:28:16 +0000606 incr_ntest
drh5edc3122001-09-13 21:53:09 +0000607 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000608 flush stdout
mistachkin6c3c1a02011-11-12 03:17:40 +0000609
610 if {![info exists ::G(match)] || [string match $::G(match) $name]} {
611 if {[catch {uplevel #0 "$cmd;\n"} result]} {
612 puts "\nError: $result"
613 fail_test $name
mistachkin6c3c1a02011-11-12 03:17:40 +0000614 } else {
drh3f17aef2012-04-27 01:08:02 +0000615 if {[regexp {^~?/.*/$} $expected]} {
drh70bdcc72013-05-30 19:28:34 +0000616 # "expected" is of the form "/PATTERN/" then the result if correct if
617 # regular expression PATTERN matches the result. "~/PATTERN/" means
618 # the regular expression must not match.
drh3f17aef2012-04-27 01:08:02 +0000619 if {[string index $expected 0]=="~"} {
drhc2b23e72013-11-13 15:32:15 +0000620 set re [string range $expected 2 end-1]
621 if {[string index $re 0]=="*"} {
622 # If the regular expression begins with * then treat it as a glob instead
623 set ok [string match $re $result]
624 } else {
625 set re [string map {# {[-0-9.]+}} $re]
626 set ok [regexp $re $result]
627 }
628 set ok [expr {!$ok}]
drh3f17aef2012-04-27 01:08:02 +0000629 } else {
drhc2b23e72013-11-13 15:32:15 +0000630 set re [string range $expected 1 end-1]
631 if {[string index $re 0]=="*"} {
632 # If the regular expression begins with * then treat it as a glob instead
633 set ok [string match $re $result]
634 } else {
635 set re [string map {# {[-0-9.]+}} $re]
636 set ok [regexp $re $result]
637 }
drh3f17aef2012-04-27 01:08:02 +0000638 }
drh70bdcc72013-05-30 19:28:34 +0000639 } elseif {[regexp {^~?\*.*\*$} $expected]} {
640 # "expected" is of the form "*GLOB*" then the result if correct if
641 # glob pattern GLOB matches the result. "~/GLOB/" means
642 # the glob must not match.
643 if {[string index $expected 0]=="~"} {
644 set e [string range $expected 1 end]
645 set ok [expr {![string match $e $result]}]
646 } else {
647 set ok [string match $expected $result]
648 }
drh3f17aef2012-04-27 01:08:02 +0000649 } else {
650 set ok [expr {[string compare $result $expected]==0}]
651 }
652 if {!$ok} {
mistachkinc60941f2012-09-13 01:51:02 +0000653 # if {![info exists ::testprefix] || $::testprefix eq ""} {
654 # error "no test prefix"
655 # }
drh3f17aef2012-04-27 01:08:02 +0000656 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
657 fail_test $name
658 } else {
659 puts " Ok"
660 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000661 }
drh348784e2000-05-29 20:41:49 +0000662 } else {
mistachkin6c3c1a02011-11-12 03:17:40 +0000663 puts " Omitted"
664 omit_test $name "pattern mismatch" 0
drh348784e2000-05-29 20:41:49 +0000665 }
drh6558db82007-04-13 03:23:21 +0000666 flush stdout
drh348784e2000-05-29 20:41:49 +0000667}
dana3f51082010-09-30 18:43:14 +0000668
mistachkinf21979d2015-01-18 05:35:01 +0000669proc dumpbytes {s} {
670 set r ""
671 for {set i 0} {$i < [string length $s]} {incr i} {
672 if {$i > 0} {append r " "}
673 append r [format %02X [scan [string index $s $i] %c]]
674 }
675 return $r
676}
677
drh8df91852012-04-24 12:46:05 +0000678proc catchcmd {db {cmd ""}} {
679 global CLI
680 set out [open cmds.txt w]
681 puts $out $cmd
682 close $out
683 set line "exec $CLI $db < cmds.txt"
684 set rc [catch { eval $line } msg]
685 list $rc $msg
686}
687
mistachkinf21979d2015-01-18 05:35:01 +0000688proc catchcmdex {db {cmd ""}} {
689 global CLI
690 set out [open cmds.txt w]
691 fconfigure $out -encoding binary -translation binary
692 puts -nonewline $out $cmd
693 close $out
694 set line "exec -keepnewline -- $CLI $db < cmds.txt"
695 set chans [list stdin stdout stderr]
696 foreach chan $chans {
697 catch {
698 set modes($chan) [fconfigure $chan]
699 fconfigure $chan -encoding binary -translation binary -buffering none
700 }
701 }
702 set rc [catch { eval $line } msg]
703 foreach chan $chans {
704 catch {
705 eval fconfigure [list $chan] $modes($chan)
706 }
707 }
708 # puts [dumpbytes $msg]
709 list $rc $msg
710}
711
shaneh55505172011-06-21 19:30:19 +0000712proc filepath_normalize {p} {
713 # test cases should be written to assume "unix"-like file paths
shaneh285a18f2011-06-21 19:39:59 +0000714 if {$::tcl_platform(platform)!="unix"} {
shaneh55505172011-06-21 19:30:19 +0000715 # lreverse*2 as a hack to remove any unneeded {} after the string map
716 lreverse [lreverse [string map {\\ /} [regsub -nocase -all {[a-z]:[/\\]+} $p {/}]]]
shanehc4896402011-06-21 19:38:16 +0000717 } {
718 set p
shaneh55505172011-06-21 19:30:19 +0000719 }
720}
721proc do_filepath_test {name cmd expected} {
722 uplevel [list do_test $name [
723 subst -nocommands { filepath_normalize [ $cmd ] }
724 ] [filepath_normalize $expected]]
725}
726
dan33f53792011-05-05 19:44:22 +0000727proc realnum_normalize {r} {
shaneh4f529e82011-06-20 17:41:41 +0000728 # different TCL versions display floating point values differently.
729 string map {1.#INF inf Inf inf .0e e} [regsub -all {(e[+-])0+} $r {\1}]
dan33f53792011-05-05 19:44:22 +0000730}
731proc do_realnum_test {name cmd expected} {
732 uplevel [list do_test $name [
733 subst -nocommands { realnum_normalize [ $cmd ] }
734 ] [realnum_normalize $expected]]
735}
736
dana3f51082010-09-30 18:43:14 +0000737proc fix_testname {varname} {
738 upvar $varname testname
mistachkin1d406e02013-08-29 01:09:14 +0000739 if {[info exists ::testprefix]
dana3f51082010-09-30 18:43:14 +0000740 && [string is digit [string range $testname 0 0]]
741 } {
742 set testname "${::testprefix}-$testname"
743 }
744}
mistachkin1d406e02013-08-29 01:09:14 +0000745
dan57f7f4b2010-10-01 19:04:37 +0000746proc do_execsql_test {testname sql {result {}}} {
dana3f51082010-09-30 18:43:14 +0000747 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000748 uplevel do_test [list $testname] [list "execsql {$sql}"] [list [list {*}$result]]
dan153eda02010-06-21 07:45:47 +0000749}
750proc do_catchsql_test {testname sql result} {
dana3f51082010-09-30 18:43:14 +0000751 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000752 uplevel do_test [list $testname] [list "catchsql {$sql}"] [list $result]
dan153eda02010-06-21 07:45:47 +0000753}
drh3c2aeae2014-01-24 14:37:44 +0000754proc do_timed_execsql_test {testname sql {result {}}} {
755 fix_testname testname
756 uplevel do_test [list $testname] [list "execsql_timed {$sql}"]\
757 [list [list {*}$result]]
758}
dan39854792010-11-15 16:12:58 +0000759proc do_eqp_test {name sql res} {
760 uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res]
761}
dan153eda02010-06-21 07:45:47 +0000762
dan51f06982010-09-18 19:00:12 +0000763#-------------------------------------------------------------------------
764# Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST
765#
766# Where switches are:
767#
768# -errorformat FMTSTRING
769# -count
danaf1dcab2010-09-20 19:17:53 +0000770# -query SQL
dana16d1062010-09-28 17:37:28 +0000771# -tclquery TCL
danaf7626f2010-09-23 18:47:36 +0000772# -repair TCL
dan51f06982010-09-18 19:00:12 +0000773#
774proc do_select_tests {prefix args} {
775
776 set testlist [lindex $args end]
777 set switches [lrange $args 0 end-1]
778
779 set errfmt ""
780 set countonly 0
dana16d1062010-09-28 17:37:28 +0000781 set tclquery ""
danaf7626f2010-09-23 18:47:36 +0000782 set repair ""
dan51f06982010-09-18 19:00:12 +0000783
784 for {set i 0} {$i < [llength $switches]} {incr i} {
785 set s [lindex $switches $i]
786 set n [string length $s]
danaf1dcab2010-09-20 19:17:53 +0000787 if {$n>=2 && [string equal -length $n $s "-query"]} {
dana16d1062010-09-28 17:37:28 +0000788 set tclquery [list execsql [lindex $switches [incr i]]]
789 } elseif {$n>=2 && [string equal -length $n $s "-tclquery"]} {
790 set tclquery [lindex $switches [incr i]]
danaf1dcab2010-09-20 19:17:53 +0000791 } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} {
dan51f06982010-09-18 19:00:12 +0000792 set errfmt [lindex $switches [incr i]]
danaf7626f2010-09-23 18:47:36 +0000793 } elseif {$n>=2 && [string equal -length $n $s "-repair"]} {
794 set repair [lindex $switches [incr i]]
dan51f06982010-09-18 19:00:12 +0000795 } elseif {$n>=2 && [string equal -length $n $s "-count"]} {
796 set countonly 1
797 } else {
798 error "unknown switch: $s"
799 }
800 }
801
802 if {$countonly && $errfmt!=""} {
803 error "Cannot use -count and -errorformat together"
804 }
805 set nTestlist [llength $testlist]
806 if {$nTestlist%3 || $nTestlist==0 } {
807 error "SELECT test list contains [llength $testlist] elements"
808 }
809
danaf7626f2010-09-23 18:47:36 +0000810 eval $repair
dan51f06982010-09-18 19:00:12 +0000811 foreach {tn sql res} $testlist {
dana16d1062010-09-28 17:37:28 +0000812 if {$tclquery != ""} {
danaf1dcab2010-09-20 19:17:53 +0000813 execsql $sql
dana16d1062010-09-28 17:37:28 +0000814 uplevel do_test ${prefix}.$tn [list $tclquery] [list [list {*}$res]]
815 } elseif {$countonly} {
dan51f06982010-09-18 19:00:12 +0000816 set nRow 0
817 db eval $sql {incr nRow}
818 uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res]
819 } elseif {$errfmt==""} {
820 uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]]
821 } else {
822 set res [list 1 [string trim [format $errfmt {*}$res]]]
823 uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res]
824 }
danaf7626f2010-09-23 18:47:36 +0000825 eval $repair
dan51f06982010-09-18 19:00:12 +0000826 }
danaf7626f2010-09-23 18:47:36 +0000827
dan51f06982010-09-18 19:00:12 +0000828}
829
danb04757a2010-09-21 16:59:16 +0000830proc delete_all_data {} {
831 db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} {
832 db eval "DELETE FROM '[string map {' ''} $t]'"
833 }
834}
drh348784e2000-05-29 20:41:49 +0000835
mistachkin1d406e02013-08-29 01:09:14 +0000836# Run an SQL script.
drhb62c3352006-11-23 09:39:16 +0000837# Return the number of microseconds per statement.
838#
drh3590f152006-11-23 21:09:10 +0000839proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000840 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000841 flush stdout
842 set speed [time {sqlite3_exec_nr db $sql}]
843 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000844 if {$tm == 0} {
845 set rate [format %20s "many"]
846 } else {
847 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
848 }
drh3590f152006-11-23 21:09:10 +0000849 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000850 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000851 global total_time
852 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000853 lappend ::speed_trial_times $name $tm
drhdd924312007-03-31 22:34:16 +0000854}
drh45c236d2008-03-22 01:08:00 +0000855proc speed_trial_tcl {name numstmt units script} {
856 puts -nonewline [format {%-21.21s } $name...]
857 flush stdout
858 set speed [time {eval $script}]
859 set tm [lindex $speed 0]
860 if {$tm == 0} {
861 set rate [format %20s "many"]
862 } else {
863 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
864 }
865 set u2 $units/s
866 puts [format {%12d uS %s %s} $tm $rate $u2]
867 global total_time
868 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000869 lappend ::speed_trial_times $name $tm
drh45c236d2008-03-22 01:08:00 +0000870}
drhdd924312007-03-31 22:34:16 +0000871proc speed_trial_init {name} {
872 global total_time
873 set total_time 0
dana975b592010-10-08 16:09:43 +0000874 set ::speed_trial_times [list]
drhbb810a92010-07-03 12:00:53 +0000875 sqlite3 versdb :memory:
876 set vers [versdb one {SELECT sqlite_source_id()}]
877 versdb close
878 puts "SQLite $vers"
drhdd924312007-03-31 22:34:16 +0000879}
880proc speed_trial_summary {name} {
881 global total_time
882 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
dana975b592010-10-08 16:09:43 +0000883
884 if { 0 } {
885 sqlite3 versdb :memory:
886 set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0]
887 versdb close
888 puts "CREATE TABLE IF NOT EXISTS time(version, script, test, us);"
889 foreach {test us} $::speed_trial_times {
890 puts "INSERT INTO time VALUES('$vers', '$name', '$test', $us);"
891 }
892 }
drhb62c3352006-11-23 09:39:16 +0000893}
894
drh348784e2000-05-29 20:41:49 +0000895# Run this routine last
896#
897proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +0000898 catch {db close}
dane8559832014-07-31 17:35:40 +0000899 catch {db1 close}
danc1a60c52010-06-07 14:28:16 +0000900 catch {db2 close}
901 catch {db3 close}
902 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +0000903}
904proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +0000905 global sqlite_open_file_count
906
907 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +0000908
drh6e142f52000-06-08 13:36:40 +0000909 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000910 catch {db2 close}
911 catch {db3 close}
912
drh9bc54492007-10-23 14:49:59 +0000913 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000914 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000915 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000916 db close
drh984bfaa2008-03-19 16:08:53 +0000917 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +0000918
drh3a7fb7c2007-08-10 16:41:08 +0000919 sqlite3_soft_heap_limit 0
danc1a60c52010-06-07 14:28:16 +0000920 set nTest [incr_ntest]
921 set nErr [set_test_counter errors]
922
drhef866372013-05-22 20:49:02 +0000923 set nKnown 0
924 if {[file readable known-problems.txt]} {
925 set fd [open known-problems.txt]
926 set content [read $fd]
927 close $fd
928 foreach x $content {set known_error($x) 1}
929 foreach x [set_test_counter fail_list] {
930 if {[info exists known_error($x)]} {incr nKnown}
931 }
932 }
933 if {$nKnown>0} {
934 puts "[expr {$nErr-$nKnown}] new errors and $nKnown known errors\
935 out of $nTest tests"
936 } else {
937 puts "$nErr errors out of $nTest tests"
938 }
939 if {$nErr>$nKnown} {
940 puts -nonewline "Failures on these tests:"
941 foreach x [set_test_counter fail_list] {
942 if {![info exists known_error($x)]} {puts -nonewline " $x"}
943 }
944 puts ""
drhf3a65f72007-08-22 20:18:21 +0000945 }
drhcca17c32013-04-19 12:32:52 +0000946 foreach warning [set_test_counter warn_list] {
947 puts "Warning: $warning"
948 }
danielk1977ee8b7992009-03-26 17:13:06 +0000949 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +0000950 if {[llength $omitList]>0} {
951 puts "Omitted test cases:"
952 set prec {}
953 foreach {rec} [lsort $omitList] {
954 if {$rec==$prec} continue
955 set prec $rec
956 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
957 }
958 }
drh80788d82006-09-02 14:50:23 +0000959 if {$nErr>0 && ![working_64bit_int]} {
960 puts "******************************************************************"
961 puts "N.B.: The version of TCL that you used to build this test harness"
962 puts "is defective in that it does not support 64-bit integers. Some or"
963 puts "all of the test failures above might be a result from this defect"
964 puts "in your TCL build."
965 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000966 }
danc1a60c52010-06-07 14:28:16 +0000967 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000968 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +0000969 }
drh94e92032003-02-16 22:21:32 +0000970 if {$sqlite_open_file_count} {
971 puts "$sqlite_open_file_count files were left open"
972 incr nErr
973 }
drheafc43b2010-07-26 18:43:40 +0000974 if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 ||
975 [sqlite3_memory_used]>0} {
976 puts "Unfreed memory: [sqlite3_memory_used] bytes in\
977 [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations"
drhf3a65f72007-08-22 20:18:21 +0000978 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000979 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000980 puts "Writing unfreed memory log to \"./memleak.txt\""
981 sqlite3_memdebug_dump ./memleak.txt
982 }
drhf3a65f72007-08-22 20:18:21 +0000983 } else {
984 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000985 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000986 sqlite3_memdebug_dump ./memusage.txt
987 }
drhf3a65f72007-08-22 20:18:21 +0000988 }
drhf7141992008-06-19 00:16:08 +0000989 show_memstats
drh91fd4d42008-01-19 20:11:25 +0000990 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
991 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000992 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
993 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
994 }
danc1a60c52010-06-07 14:28:16 +0000995 if {$::cmdlinearg(malloctrace)} {
danielk197735754ac2008-03-21 17:29:37 +0000996 puts "Writing mallocs.sql..."
997 memdebug_log_sql
998 sqlite3_memdebug_log stop
999 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +00001000
1001 if {[sqlite3_memory_used]>0} {
1002 puts "Writing leaks.sql..."
1003 sqlite3_memdebug_log sync
1004 memdebug_log_sql leaks.sql
1005 }
danielk197735754ac2008-03-21 17:29:37 +00001006 }
drhd9910fe2006-10-04 11:55:49 +00001007 foreach f [glob -nocomplain test.db-*-journal] {
mistachkinfda06be2011-08-02 00:57:34 +00001008 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +00001009 }
1010 foreach f [glob -nocomplain test.db-mj*] {
mistachkinfda06be2011-08-02 00:57:34 +00001011 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +00001012 }
drhdb25e382001-03-15 18:21:22 +00001013 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +00001014}
1015
drhf7141992008-06-19 00:16:08 +00001016# Display memory statistics for analysis and debugging purposes.
1017#
1018proc show_memstats {} {
1019 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +00001020 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
1021 set val [format {now %10d max %10d max-size %10d} \
1022 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +00001023 puts "Memory used: $val"
drheafc43b2010-07-26 18:43:40 +00001024 set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0]
1025 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
1026 puts "Allocation count: $val"
drhf7141992008-06-19 00:16:08 +00001027 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +00001028 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
1029 set val [format {now %10d max %10d max-size %10d} \
1030 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +00001031 puts "Page-cache used: $val"
1032 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
1033 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
1034 puts "Page-cache overflow: $val"
1035 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
1036 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
1037 puts "Scratch memory used: $val"
1038 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +00001039 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
1040 set val [format {now %10d max %10d max-size %10d} \
1041 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +00001042 puts "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +00001043 ifcapable yytrackmaxstackdepth {
1044 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
1045 set val [format { max %10d} [lindex $x 2]]
1046 puts "Parser stack depth: $val"
1047 }
drhf7141992008-06-19 00:16:08 +00001048}
1049
drh348784e2000-05-29 20:41:49 +00001050# A procedure to execute SQL
1051#
drhc4a3c772001-04-04 11:48:57 +00001052proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +00001053 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +00001054 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +00001055}
drh3c2aeae2014-01-24 14:37:44 +00001056proc execsql_timed {sql {db db}} {
1057 set tm [time {
1058 set x [uplevel [list $db eval $sql]]
1059 } 1]
1060 set tm [lindex $tm 0]
1061 puts -nonewline " ([expr {$tm*0.001}]ms) "
1062 set x
1063}
drh3aadb2e2000-05-31 17:59:25 +00001064
drhadbca9c2001-09-27 15:11:53 +00001065# Execute SQL and catch exceptions.
1066#
1067proc catchsql {sql {db db}} {
1068 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +00001069 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +00001070 lappend r $msg
1071 return $r
1072}
1073
drh04096482001-11-09 22:41:44 +00001074# Do an VDBE code dump on the SQL given
1075#
1076proc explain {sql {db db}} {
1077 puts ""
danielk1977287fb612008-01-04 19:10:28 +00001078 puts "addr opcode p1 p2 p3 p4 p5 #"
1079 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +00001080 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +00001081 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
1082 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
1083 ]
drh04096482001-11-09 22:41:44 +00001084 }
1085}
1086
dane83267d2013-11-05 14:19:22 +00001087proc explain_i {sql {db db}} {
1088 puts ""
1089 puts "addr opcode p1 p2 p3 p4 p5 #"
1090 puts "---- ------------ ------ ------ ------ ---------------- -- -"
1091
dane83267d2013-11-05 14:19:22 +00001092
dan5c663c32013-11-06 14:52:40 +00001093 # Set up colors for the different opcodes. Scheme is as follows:
1094 #
1095 # Red: Opcodes that write to a b-tree.
1096 # Blue: Opcodes that reposition or seek a cursor.
1097 # Green: The ResultRow opcode.
1098 #
dandea63f22014-03-03 16:48:47 +00001099 if { [catch {fconfigure stdout -mode}]==0 } {
1100 set R "\033\[31;1m" ;# Red fg
1101 set G "\033\[32;1m" ;# Green fg
1102 set B "\033\[34;1m" ;# Red fg
1103 set D "\033\[39;0m" ;# Default fg
1104 } else {
1105 set R ""
1106 set G ""
1107 set B ""
1108 set D ""
1109 }
dan5c663c32013-11-06 14:52:40 +00001110 foreach opcode {
1111 Seek SeekGe SeekGt SeekLe SeekLt NotFound Last Rewind
dane3ab7292013-11-12 12:30:09 +00001112 NoConflict Next Prev VNext VPrev VFilter
drhf45f2322014-03-23 17:45:03 +00001113 SorterSort SorterNext
dan5c663c32013-11-06 14:52:40 +00001114 } {
1115 set color($opcode) $B
1116 }
1117 foreach opcode {ResultRow} {
1118 set color($opcode) $G
1119 }
1120 foreach opcode {IdxInsert Insert Delete IdxDelete} {
1121 set color($opcode) $R
1122 }
1123
dan427ebba2013-11-05 16:39:31 +00001124 set bSeenGoto 0
dane83267d2013-11-05 14:19:22 +00001125 $db eval "explain $sql" {} {
1126 set x($addr) 0
1127 set op($addr) $opcode
1128
dan427ebba2013-11-05 16:39:31 +00001129 if {$opcode == "Goto" && ($bSeenGoto==0 || ($p2 > $addr+10))} {
1130 set linebreak($p2) 1
1131 set bSeenGoto 1
dane83267d2013-11-05 14:19:22 +00001132 }
1133
dane3ab7292013-11-12 12:30:09 +00001134 if {$opcode=="Next" || $opcode=="Prev"
1135 || $opcode=="VNext" || $opcode=="VPrev"
drhf45f2322014-03-23 17:45:03 +00001136 || $opcode=="SorterNext"
dane3ab7292013-11-12 12:30:09 +00001137 } {
dane83267d2013-11-05 14:19:22 +00001138 for {set i $p2} {$i<$addr} {incr i} {
1139 incr x($i) 2
1140 }
1141 }
1142
1143 if {$opcode == "Goto" && $p2<$addr && $op($p2)=="Yield"} {
1144 for {set i [expr $p2+1]} {$i<$addr} {incr i} {
1145 incr x($i) 2
1146 }
1147 }
dan427ebba2013-11-05 16:39:31 +00001148
1149 if {$opcode == "Halt" && $comment == "End of coroutine"} {
1150 set linebreak([expr $addr+1]) 1
1151 }
dane83267d2013-11-05 14:19:22 +00001152 }
1153
1154 $db eval "explain $sql" {} {
dan427ebba2013-11-05 16:39:31 +00001155 if {[info exists linebreak($addr)]} {
dane83267d2013-11-05 14:19:22 +00001156 puts ""
1157 }
1158 set I [string repeat " " $x($addr)]
dan5c663c32013-11-06 14:52:40 +00001159
1160 set col ""
1161 catch { set col $color($opcode) }
1162
1163 puts [format {%-4d %s%s%-12.12s%s %-6d %-6d %-6d % -17s %s %s} \
1164 $addr $I $col $opcode $D $p1 $p2 $p3 $p4 $p5 $comment
dane83267d2013-11-05 14:19:22 +00001165 ]
dane83267d2013-11-05 14:19:22 +00001166 }
1167 puts "---- ------------ ------ ------ ------ ---------------- -- -"
1168}
1169
drhdafc0ce2008-04-17 19:14:02 +00001170# Show the VDBE program for an SQL statement but omit the Trace
1171# opcode at the beginning. This procedure can be used to prove
1172# that different SQL statements generate exactly the same VDBE code.
1173#
1174proc explain_no_trace {sql} {
1175 set tr [db eval "EXPLAIN $sql"]
1176 return [lrange $tr 7 end]
1177}
1178
drh3aadb2e2000-05-31 17:59:25 +00001179# Another procedure to execute SQL. This one includes the field
1180# names in the returned list.
1181#
1182proc execsql2 {sql} {
1183 set result {}
1184 db eval $sql data {
1185 foreach f $data(*) {
1186 lappend result $f $data($f)
1187 }
1188 }
1189 return $result
1190}
drh17a68932001-01-31 13:28:08 +00001191
mistachkin1d406e02013-08-29 01:09:14 +00001192# Use a temporary in-memory database to execute SQL statements
1193#
1194proc memdbsql {sql} {
1195 sqlite3 memdb :memory:
1196 set result [memdb eval $sql]
1197 memdb close
1198 return $result
1199}
1200
drhdde85d92003-03-01 19:45:34 +00001201# Use the non-callback API to execute multiple SQL statements
1202#
1203proc stepsql {dbptr sql} {
1204 set sql [string trim $sql]
1205 set r 0
1206 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +00001207 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +00001208 return [list 1 $vm]
1209 }
1210 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +00001211# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
1212# foreach v $VAL {lappend r $v}
1213# }
1214 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
1215 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
1216 lappend r [sqlite3_column_text $vm $i]
1217 }
drhdde85d92003-03-01 19:45:34 +00001218 }
danielk1977106bb232004-05-21 10:08:53 +00001219 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +00001220 return [list 1 $errmsg]
1221 }
1222 }
1223 return $r
1224}
1225
drh21504322002-06-25 13:16:02 +00001226# Do an integrity check of the entire database
1227#
danielk197704103022009-02-03 16:51:24 +00001228proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +00001229 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +00001230 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +00001231 }
1232}
1233
drh433dccf2013-02-09 15:37:11 +00001234# Check the extended error code
1235#
1236proc verify_ex_errcode {name expected {db db}} {
1237 do_test $name [list sqlite3_extended_errcode $db] $expected
1238}
1239
danc6055c72011-04-28 18:46:46 +00001240
1241# Return true if the SQL statement passed as the second argument uses a
1242# statement transaction.
1243#
1244proc sql_uses_stmt {db sql} {
1245 set stmt [sqlite3_prepare $db $sql -1 dummy]
1246 set uses [uses_stmt_journal $stmt]
1247 sqlite3_finalize $stmt
1248 return $uses
1249}
1250
danielk1977db4e8862008-01-16 08:24:46 +00001251proc fix_ifcapable_expr {expr} {
1252 set ret ""
1253 set state 0
1254 for {set i 0} {$i < [string length $expr]} {incr i} {
1255 set char [string range $expr $i $i]
1256 set newstate [expr {[string is alnum $char] || $char eq "_"}]
1257 if {$newstate && !$state} {
1258 append ret {$::sqlite_options(}
1259 }
1260 if {!$newstate && $state} {
1261 append ret )
1262 }
1263 append ret $char
1264 set state $newstate
1265 }
1266 if {$state} {append ret )}
1267 return $ret
1268}
1269
mistachkinc60941f2012-09-13 01:51:02 +00001270# Returns non-zero if the capabilities are present; zero otherwise.
1271#
1272proc capable {expr} {
1273 set e [fix_ifcapable_expr $expr]; return [expr ($e)]
1274}
1275
drh27d258a2004-10-30 20:23:09 +00001276# Evaluate a boolean expression of capabilities. If true, execute the
1277# code. Omit the code if false.
1278#
danielk19773e8c37e2005-01-21 03:12:14 +00001279proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +00001280 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
1281 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +00001282 if ($e2) {
1283 set c [catch {uplevel 1 $code} r]
1284 } else {
1285 set c [catch {uplevel 1 $elsecode} r]
1286 }
1287 return -code $c $r
drh21504322002-06-25 13:16:02 +00001288}
danielk197745901d62004-11-10 15:27:38 +00001289
danielk1977aca790a2005-01-13 11:07:52 +00001290# This proc execs a seperate process that crashes midway through executing
1291# the SQL script $sql on database test.db.
1292#
1293# The crash occurs during a sync() of file $crashfile. When the crash
1294# occurs a random subset of all unsynced writes made by the process are
1295# written into the files on disk. Argument $crashdelay indicates the
1296# number of file syncs to wait before crashing.
1297#
1298# The return value is a list of two elements. The first element is a
1299# boolean, indicating whether or not the process actually crashed or
1300# reported some other error. The second element in the returned list is the
1301# error message. This is "child process exited abnormally" if the crash
mistachkin48864df2013-03-21 21:20:32 +00001302# occurred.
danielk1977aca790a2005-01-13 11:07:52 +00001303#
danielk1977f8940ae2007-08-23 11:07:10 +00001304# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +00001305#
1306proc crashsql {args} {
danielk197759a33f92007-03-17 10:26:59 +00001307
1308 set blocksize ""
1309 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +00001310 set prngseed 0
dan999cd082013-12-09 20:42:03 +00001311 set opendb { sqlite3 db test.db -vfs crash }
drhf8587402008-01-08 16:03:49 +00001312 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +00001313 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +00001314 set dc ""
danielk197759a33f92007-03-17 10:26:59 +00001315 set sql [lindex $args end]
mistachkin1d406e02013-08-29 01:09:14 +00001316
danielk197759a33f92007-03-17 10:26:59 +00001317 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
1318 set z [lindex $args $ii]
1319 set n [string length $z]
1320 set z2 [lindex $args [expr $ii+1]]
1321
1322 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
dan999cd082013-12-09 20:42:03 +00001323 elseif {$n>1 && [string first $z -opendb]==0} {set opendb $z2} \
drhcb1f0f62008-01-08 15:18:52 +00001324 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +00001325 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +00001326 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +00001327 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +00001328 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +00001329 else { error "Unrecognized option: $z" }
1330 }
1331
1332 if {$crashfile eq ""} {
1333 error "Compulsory option -file missing"
1334 }
1335
mistachkin1d406e02013-08-29 01:09:14 +00001336 # $crashfile gets compared to the native filename in
shanehf2c08822010-07-08 16:30:44 +00001337 # cfSync(), which can be different then what TCL uses by
1338 # default, so here we force it to the "nativename" format.
mistachkinf8a78462012-03-08 20:00:36 +00001339 set cfile [string map {\\ \\\\} [file nativename [file join [get_pwd] $crashfile]]]
danielk1977aca790a2005-01-13 11:07:52 +00001340
1341 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +00001342 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +00001343 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +00001344 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
dan999cd082013-12-09 20:42:03 +00001345 puts $f $opendb
danielk1977933bbd62007-03-17 07:22:42 +00001346
1347 # This block sets the cache size of the main database to 10
1348 # pages. This is done in case the build is configured to omit
1349 # "PRAGMA cache_size".
1350 puts $f {db eval {SELECT * FROM sqlite_master;}}
1351 puts $f {set bt [btree_from_db db]}
1352 puts $f {btree_set_cache_size $bt 10}
dan999cd082013-12-09 20:42:03 +00001353
drhcb1f0f62008-01-08 15:18:52 +00001354 if {$prngseed} {
1355 set seed [expr {$prngseed%10007+1}]
1356 # puts seed=$seed
1357 puts $f "db eval {SELECT randomblob($seed)}"
1358 }
danielk1977933bbd62007-03-17 07:22:42 +00001359
drhf8587402008-01-08 16:03:49 +00001360 if {[string length $tclbody]>0} {
1361 puts $f $tclbody
1362 }
1363 if {[string length $sql]>0} {
1364 puts $f "db eval {"
1365 puts $f "$sql"
1366 puts $f "}"
1367 }
danielk1977aca790a2005-01-13 11:07:52 +00001368 close $f
danielk1977aca790a2005-01-13 11:07:52 +00001369 set r [catch {
drh9c06c952005-11-26 00:25:00 +00001370 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +00001371 } msg]
mistachkin1d406e02013-08-29 01:09:14 +00001372
shanehf2c08822010-07-08 16:30:44 +00001373 # Windows/ActiveState TCL returns a slightly different
1374 # error message. We map that to the expected message
1375 # so that we don't have to change all of the test
1376 # cases.
1377 if {$::tcl_platform(platform)=="windows"} {
1378 if {$msg=="child killed: unknown signal"} {
1379 set msg "child process exited abnormally"
1380 }
1381 }
mistachkin1d406e02013-08-29 01:09:14 +00001382
danielk1977aca790a2005-01-13 11:07:52 +00001383 lappend r $msg
1384}
1385
danb88e24f2013-02-23 18:58:11 +00001386proc run_ioerr_prep {} {
1387 set ::sqlite_io_error_pending 0
1388 catch {db close}
1389 catch {db2 close}
1390 catch {forcedelete test.db}
1391 catch {forcedelete test.db-journal}
1392 catch {forcedelete test2.db}
1393 catch {forcedelete test2.db-journal}
1394 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
1395 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
1396 if {[info exists ::ioerropts(-tclprep)]} {
1397 eval $::ioerropts(-tclprep)
1398 }
1399 if {[info exists ::ioerropts(-sqlprep)]} {
1400 execsql $::ioerropts(-sqlprep)
1401 }
1402 expr 0
1403}
1404
danielk197732554c12005-01-22 03:39:39 +00001405# Usage: do_ioerr_test <test number> <options...>
1406#
1407# This proc is used to implement test cases that check that IO errors
mistachkin1d406e02013-08-29 01:09:14 +00001408# are correctly handled. The first argument, <test number>, is an integer
danielk197732554c12005-01-22 03:39:39 +00001409# used to name the tests executed by this proc. Options are as follows:
1410#
1411# -tclprep TCL script to run to prepare test.
1412# -sqlprep SQL script to run to prepare test.
1413# -tclbody TCL script to run with IO error simulation.
1414# -sqlbody TCL script to run with IO error simulation.
1415# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +00001416# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +00001417# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +00001418# -start Value of 'N' to begin with (default 1)
1419#
1420# -cksum Boolean. If true, test that the database does
1421# not change during the execution of the test case.
1422#
1423proc do_ioerr_test {testname args} {
1424
danielk197732554c12005-01-22 03:39:39 +00001425 set ::ioerropts(-start) 1
1426 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +00001427 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +00001428 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +00001429 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +00001430 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +00001431 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +00001432 array set ::ioerropts $args
1433
danielk197747cd39c2008-05-12 12:41:15 +00001434 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
1435 # a couple of obscure IO errors that do not return them.
1436 set ::ioerropts(-erc) 0
mistachkin1d406e02013-08-29 01:09:14 +00001437
danb88e24f2013-02-23 18:58:11 +00001438 # Create a single TCL script from the TCL and SQL specified
1439 # as the body of the test.
1440 set ::ioerrorbody {}
1441 if {[info exists ::ioerropts(-tclbody)]} {
1442 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
1443 }
1444 if {[info exists ::ioerropts(-sqlbody)]} {
1445 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
1446 }
1447
1448 save_prng_state
1449 if {$::ioerropts(-cksum)} {
1450 run_ioerr_prep
1451 eval $::ioerrorbody
1452 set ::goodcksum [cksum]
1453 }
danielk197747cd39c2008-05-12 12:41:15 +00001454
danielk197732554c12005-01-22 03:39:39 +00001455 set ::go 1
drh93aed5a2008-01-16 17:46:38 +00001456 #reset_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +00001457 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +00001458 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +00001459 incr ::ioerropts(-count) -1
1460 if {$::ioerropts(-count)<0} break
mistachkin1d406e02013-08-29 01:09:14 +00001461
danielk197732554c12005-01-22 03:39:39 +00001462 # Skip this IO error if it was specified with the "-exclude" option.
1463 if {[info exists ::ioerropts(-exclude)]} {
1464 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
1465 }
danielk1977861f7452008-06-05 11:39:11 +00001466 if {$::ioerropts(-restoreprng)} {
1467 restore_prng_state
1468 }
danielk197732554c12005-01-22 03:39:39 +00001469
mistachkin1d406e02013-08-29 01:09:14 +00001470 # Delete the files test.db and test2.db, then execute the TCL and
danielk197732554c12005-01-22 03:39:39 +00001471 # SQL (in that order) to prepare for the test case.
1472 do_test $testname.$n.1 {
danb88e24f2013-02-23 18:58:11 +00001473 run_ioerr_prep
danielk197732554c12005-01-22 03:39:39 +00001474 } {0}
1475
1476 # Read the 'checksum' of the database.
1477 if {$::ioerropts(-cksum)} {
danb88e24f2013-02-23 18:58:11 +00001478 set ::checksum [cksum]
danielk197732554c12005-01-22 03:39:39 +00001479 }
drh93aed5a2008-01-16 17:46:38 +00001480
danielk197732554c12005-01-22 03:39:39 +00001481 # Set the Nth IO error to fail.
1482 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +00001483 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +00001484 set ::sqlite_io_error_pending $n
1485 }] $n
danielk197732554c12005-01-22 03:39:39 +00001486
danb88e24f2013-02-23 18:58:11 +00001487 # Execute the TCL script created for the body of this test. If
mistachkin1d406e02013-08-29 01:09:14 +00001488 # at least N IO operations performed by SQLite as a result of
danb88e24f2013-02-23 18:58:11 +00001489 # the script, the Nth will fail.
danielk197732554c12005-01-22 03:39:39 +00001490 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +00001491 set ::sqlite_io_error_hit 0
1492 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +00001493 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +00001494 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +00001495 set rc [sqlite3_errcode $::DB]
1496 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +00001497 # If we are in extended result code mode, make sure all of the
1498 # IOERRs we get back really do have their extended code values.
1499 # If an extended result code is returned, the sqlite3_errcode
1500 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
1501 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +00001502 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
1503 return $rc
1504 }
1505 } else {
drh5f7b5bf2007-04-19 12:30:54 +00001506 # If we are not in extended result code mode, make sure no
1507 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +00001508 if {[regexp {\+\d} $rc]} {
1509 return $rc
1510 }
1511 }
drh93aed5a2008-01-16 17:46:38 +00001512 # The test repeats as long as $::go is non-zero. $::go starts out
1513 # as 1. When a test runs to completion without hitting an I/O
1514 # error, that means there is no point in continuing with this test
1515 # case so set $::go to zero.
1516 #
1517 if {$::sqlite_io_error_pending>0} {
1518 set ::go 0
1519 set q 0
1520 set ::sqlite_io_error_pending 0
1521 } else {
1522 set q 1
1523 }
1524
drhc9ac5ca2005-11-04 22:03:30 +00001525 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +00001526 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
1527 set r 1
1528 }
danielk1977f2fa8312006-01-24 13:09:33 +00001529 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +00001530
1531 # One of two things must have happened. either
1532 # 1. We never hit the IO error and the SQL returned OK
1533 # 2. An IO error was hit and the SQL failed
1534 #
dand41a29a2010-05-06 15:56:28 +00001535 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +00001536 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +00001537 } {1}
1538
danielk197780daec62008-05-12 10:57:02 +00001539 set ::sqlite_io_error_hit 0
1540 set ::sqlite_io_error_pending 0
1541
mistachkin1d406e02013-08-29 01:09:14 +00001542 # Check that no page references were leaked. There should be
1543 # a single reference if there is still an active transaction,
danielk197752b472a2008-05-05 16:23:55 +00001544 # or zero otherwise.
1545 #
danielk197781620542008-06-07 05:19:37 +00001546 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
mistachkin1d406e02013-08-29 01:09:14 +00001547 # locks are established on database files (i.e. if the error
danielk197781620542008-06-07 05:19:37 +00001548 # occurs while attempting to detect a hot-journal file), then
1549 # there may 0 page references and an active transaction according
1550 # to [sqlite3_get_autocommit].
1551 #
danielk19774abd5442008-05-05 15:26:50 +00001552 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +00001553 do_test $testname.$n.4 {
1554 set bt [btree_from_db db]
1555 db_enter db
1556 array set stats [btree_pager_stats $bt]
1557 db_leave db
danielk197781620542008-06-07 05:19:37 +00001558 set nRef $stats(ref)
1559 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
1560 } {1}
danielk19774abd5442008-05-05 15:26:50 +00001561 }
1562
mistachkin1d406e02013-08-29 01:09:14 +00001563 # If there is an open database handle and no open transaction,
danielk197752b472a2008-05-05 16:23:55 +00001564 # and the pager is not running in exclusive-locking mode,
1565 # check that the pager is in "unlocked" state. Theoretically,
1566 # if a call to xUnlock() failed due to an IO error the underlying
1567 # file may still be locked.
1568 #
1569 ifcapable pragma {
1570 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +00001571 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +00001572 && [db one {pragma locking_mode}] eq "normal"
1573 && [sqlite3_get_autocommit db]
1574 } {
1575 do_test $testname.$n.5 {
1576 set bt [btree_from_db db]
1577 db_enter db
1578 array set stats [btree_pager_stats $bt]
1579 db_leave db
1580 set stats(state)
1581 } 0
1582 }
1583 }
1584
mistachkin48864df2013-03-21 21:20:32 +00001585 # If an IO error occurred, then the checksum of the database should
danielk197732554c12005-01-22 03:39:39 +00001586 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +00001587 #
drh1aa5af12008-03-07 19:51:14 +00001588 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +00001589 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +00001590 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +00001591 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +00001592 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danb88e24f2013-02-23 18:58:11 +00001593 set nowcksum [cksum]
1594 set res [expr {$nowcksum==$::checksum || $nowcksum==$::goodcksum}]
1595 if {$res==0} {
1596 puts "now=$nowcksum"
1597 puts "the=$::checksum"
1598 puts "fwd=$::goodcksum"
1599 }
1600 set res
1601 } 1
danielk197732554c12005-01-22 03:39:39 +00001602 }
1603
drh1aa5af12008-03-07 19:51:14 +00001604 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +00001605 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +00001606 if {[info exists ::ioerropts(-cleanup)]} {
1607 catch $::ioerropts(-cleanup)
1608 }
danielk197732554c12005-01-22 03:39:39 +00001609 }
1610 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +00001611 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +00001612 unset ::ioerropts
1613}
1614
drh93aed5a2008-01-16 17:46:38 +00001615# Return a checksum based on the contents of the main database associated
1616# with connection $db
danielk197732554c12005-01-22 03:39:39 +00001617#
1618proc cksum {{db db}} {
1619 set txt [$db eval {
1620 SELECT name, type, sql FROM sqlite_master order by name
1621 }]\n
1622 foreach tbl [$db eval {
1623 SELECT name FROM sqlite_master WHERE type='table' order by name
1624 }] {
1625 append txt [$db eval "SELECT * FROM $tbl"]\n
1626 }
1627 foreach prag {default_synchronous default_cache_size} {
1628 append txt $prag-[$db eval "PRAGMA $prag"]\n
1629 }
1630 set cksum [string length $txt]-[md5 $txt]
1631 # puts $cksum-[file size test.db]
1632 return $cksum
1633}
1634
drh93aed5a2008-01-16 17:46:38 +00001635# Generate a checksum based on the contents of the main and temp tables
1636# database $db. If the checksum of two databases is the same, and the
1637# integrity-check passes for both, the two databases are identical.
1638#
drh1db639c2008-01-17 02:36:28 +00001639proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +00001640 set ret [list]
1641 ifcapable tempdb {
1642 set sql {
1643 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1644 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
1645 SELECT 'sqlite_master' UNION
1646 SELECT 'sqlite_temp_master' ORDER BY 1
1647 }
1648 } else {
1649 set sql {
1650 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1651 SELECT 'sqlite_master' ORDER BY 1
1652 }
1653 }
1654 set tbllist [$db eval $sql]
1655 set txt {}
1656 foreach tbl $tbllist {
1657 append txt [$db eval "SELECT * FROM $tbl"]
1658 }
1659 foreach prag {default_cache_size} {
1660 append txt $prag-[$db eval "PRAGMA $prag"]\n
1661 }
1662 # puts txt=$txt
1663 return [md5 $txt]
1664}
1665
drhdc2c4912009-02-04 22:46:47 +00001666# Generate a checksum based on the contents of a single database with
mistachkin1d406e02013-08-29 01:09:14 +00001667# a database connection. The name of the database is $dbname.
drhdc2c4912009-02-04 22:46:47 +00001668# Examples of $dbname are "temp" or "main".
1669#
1670proc dbcksum {db dbname} {
1671 if {$dbname=="temp"} {
1672 set master sqlite_temp_master
1673 } else {
1674 set master $dbname.sqlite_master
1675 }
1676 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
1677 set txt [$db eval "SELECT * FROM $master"]\n
1678 foreach tab $alltab {
1679 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
1680 }
1681 return [md5 $txt]
1682}
1683
danielk197735754ac2008-03-21 17:29:37 +00001684proc memdebug_log_sql {{filename mallocs.sql}} {
1685
danielk19776f332c12008-03-21 14:22:44 +00001686 set data [sqlite3_memdebug_log dump]
1687 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +00001688 if {$nFrame < 0} { return "" }
1689
danielk197735754ac2008-03-21 17:29:37 +00001690 set database temp
1691
danielk19776ab3a2e2009-02-19 14:39:25 +00001692 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +00001693
1694 set sql ""
1695 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +00001696 set nCall [lindex $e 0]
1697 set nByte [lindex $e 1]
1698 set lStack [lrange $e 2 end]
1699 append sql "INSERT INTO ${database}.malloc VALUES"
1700 append sql "('test', $nCall, $nByte, '$lStack');\n"
1701 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +00001702 set frames($f) 1
1703 }
1704 }
1705
1706 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +00001707 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +00001708
1709 foreach f [array names frames] {
1710 set addr [format %x $f]
1711 set cmd "addr2line -e [info nameofexec] $addr"
1712 set line [eval exec $cmd]
1713 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00001714
1715 set file [lindex [split $line :] 0]
1716 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00001717 }
1718
danielk197735754ac2008-03-21 17:29:37 +00001719 foreach f [array names files] {
1720 set contents ""
1721 catch {
1722 set fd [open $f]
1723 set contents [read $fd]
1724 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001725 }
danielk197735754ac2008-03-21 17:29:37 +00001726 set contents [string map {' ''} $contents]
1727 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00001728 }
danielk19776f332c12008-03-21 14:22:44 +00001729
danielk197735754ac2008-03-21 17:29:37 +00001730 set fd [open $filename w]
1731 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
1732 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001733}
drh93aed5a2008-01-16 17:46:38 +00001734
danf5894502009-10-07 18:41:19 +00001735# Drop all tables in database [db]
1736proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00001737 ifcapable trigger&&foreignkey {
1738 set pk [$db one "PRAGMA foreign_keys"]
1739 $db eval "PRAGMA foreign_keys = OFF"
1740 }
drh9a6ffc82010-02-15 18:03:20 +00001741 foreach {idx name file} [db eval {PRAGMA database_list}] {
1742 if {$idx==1} {
1743 set master sqlite_temp_master
1744 } else {
1745 set master $name.sqlite_master
1746 }
1747 foreach {t type} [$db eval "
1748 SELECT name, type FROM $master
dana16d1062010-09-28 17:37:28 +00001749 WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X'
drh9a6ffc82010-02-15 18:03:20 +00001750 "] {
dana16d1062010-09-28 17:37:28 +00001751 $db eval "DROP $type \"$t\""
drh9a6ffc82010-02-15 18:03:20 +00001752 }
danf5894502009-10-07 18:41:19 +00001753 }
shaneh4e7b32f2009-12-17 22:12:51 +00001754 ifcapable trigger&&foreignkey {
1755 $db eval "PRAGMA foreign_keys = $pk"
1756 }
danf5894502009-10-07 18:41:19 +00001757}
1758
dan71cb5182010-04-26 12:39:03 +00001759#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00001760# If a test script is executed with global variable $::G(perm:name) set to
mistachkin1d406e02013-08-29 01:09:14 +00001761# "wal", then the tests are run in WAL mode. Otherwise, they should be run
1762# in rollback mode. The following Tcl procs are used to make this less
dan430e74c2010-06-07 17:47:26 +00001763# intrusive:
dan71cb5182010-04-26 12:39:03 +00001764#
1765# wal_set_journal_mode ?DB?
1766#
1767# If running a WAL test, execute "PRAGMA journal_mode = wal" using
1768# connection handle DB. Otherwise, this command is a no-op.
1769#
1770# wal_check_journal_mode TESTNAME ?DB?
1771#
1772# If running a WAL test, execute a tests case that fails if the main
1773# database for connection handle DB is not currently a WAL database.
1774# Otherwise (if not running a WAL permutation) this is a no-op.
1775#
1776# wal_is_wal_mode
mistachkin1d406e02013-08-29 01:09:14 +00001777#
dan71cb5182010-04-26 12:39:03 +00001778# Returns true if this test should be run in WAL mode. False otherwise.
mistachkin1d406e02013-08-29 01:09:14 +00001779#
dan71cb5182010-04-26 12:39:03 +00001780proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00001781 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00001782}
1783proc wal_set_journal_mode {{db db}} {
1784 if { [wal_is_wal_mode] } {
1785 $db eval "PRAGMA journal_mode = WAL"
1786 }
1787}
1788proc wal_check_journal_mode {testname {db db}} {
1789 if { [wal_is_wal_mode] } {
1790 $db eval { SELECT * FROM sqlite_master }
1791 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
1792 }
1793}
1794
dan430e74c2010-06-07 17:47:26 +00001795proc permutation {} {
1796 set perm ""
1797 catch {set perm $::G(perm:name)}
1798 set perm
1799}
dancb79e512010-08-06 13:50:07 +00001800proc presql {} {
1801 set presql ""
1802 catch {set presql $::G(perm:presql)}
1803 set presql
1804}
dan430e74c2010-06-07 17:47:26 +00001805
danc1a60c52010-06-07 14:28:16 +00001806#-------------------------------------------------------------------------
1807#
1808proc slave_test_script {script} {
1809
1810 # Create the interpreter used to run the test script.
1811 interp create tinterp
1812
1813 # Populate some global variables that tester.tcl expects to see.
1814 foreach {var value} [list \
1815 ::argv0 $::argv0 \
1816 ::argv {} \
1817 ::SLAVE 1 \
1818 ] {
1819 interp eval tinterp [list set $var $value]
1820 }
1821
1822 # The alias used to access the global test counters.
1823 tinterp alias set_test_counter set_test_counter
1824
1825 # Set up the ::cmdlinearg array in the slave.
1826 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
1827
dan430e74c2010-06-07 17:47:26 +00001828 # Set up the ::G array in the slave.
1829 interp eval tinterp [list array set ::G [array get ::G]]
1830
danc1a60c52010-06-07 14:28:16 +00001831 # Load the various test interfaces implemented in C.
1832 load_testfixture_extensions tinterp
1833
1834 # Run the test script.
1835 interp eval tinterp $script
1836
danea5542d2010-07-06 11:26:15 +00001837 # Check if the interpreter call [run_thread_tests]
1838 if { [interp eval tinterp {info exists ::run_thread_tests_called}] } {
1839 set ::run_thread_tests_called 1
1840 }
1841
danc1a60c52010-06-07 14:28:16 +00001842 # Delete the interpreter used to run the test script.
1843 interp delete tinterp
1844}
1845
dan430e74c2010-06-07 17:47:26 +00001846proc slave_test_file {zFile} {
dan0626dfc2010-06-15 06:56:37 +00001847 set tail [file tail $zFile]
dan430e74c2010-06-07 17:47:26 +00001848
dan6a64d672011-04-04 15:38:16 +00001849 if {[info exists ::G(start:permutation)]} {
1850 if {[permutation] != $::G(start:permutation)} return
1851 unset ::G(start:permutation)
1852 }
1853 if {[info exists ::G(start:file)]} {
1854 if {$tail != $::G(start:file) && $tail!="$::G(start:file).test"} return
1855 unset ::G(start:file)
1856 }
1857
dan92d516a2010-07-05 14:54:48 +00001858 # Remember the value of the shared-cache setting. So that it is possible
1859 # to check afterwards that it was not modified by the test script.
1860 #
1861 ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] }
dan0626dfc2010-06-15 06:56:37 +00001862
dan92d516a2010-07-05 14:54:48 +00001863 # Run the test script in a slave interpreter.
1864 #
danea5542d2010-07-06 11:26:15 +00001865 unset -nocomplain ::run_thread_tests_called
dan0626dfc2010-06-15 06:56:37 +00001866 reset_prng_state
1867 set ::sqlite_open_file_count 0
1868 set time [time { slave_test_script [list source $zFile] }]
1869 set ms [expr [lindex $time 0] / 1000]
1870
dan92d516a2010-07-05 14:54:48 +00001871 # Test that all files opened by the test script were closed. Omit this
1872 # if the test script has "thread" in its name. The open file counter
1873 # is not thread-safe.
dan0626dfc2010-06-15 06:56:37 +00001874 #
danea5542d2010-07-06 11:26:15 +00001875 if {[info exists ::run_thread_tests_called]==0} {
dan92d516a2010-07-05 14:54:48 +00001876 do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0}
1877 }
dan430e74c2010-06-07 17:47:26 +00001878 set ::sqlite_open_file_count 0
1879
mistachkin1d406e02013-08-29 01:09:14 +00001880 # Test that the global "shared-cache" setting was not altered by
dan0626dfc2010-06-15 06:56:37 +00001881 # the test script.
1882 #
mistachkin1d406e02013-08-29 01:09:14 +00001883 ifcapable shared_cache {
dan0626dfc2010-06-15 06:56:37 +00001884 set res [expr {[sqlite3_enable_shared_cache] == $scs}]
1885 do_test ${tail}-sharedcachesetting [list set {} $res] 1
1886 }
dan430e74c2010-06-07 17:47:26 +00001887
dan92d516a2010-07-05 14:54:48 +00001888 # Add some info to the output.
1889 #
dan0626dfc2010-06-15 06:56:37 +00001890 puts "Time: $tail $ms ms"
dan430e74c2010-06-07 17:47:26 +00001891 show_memstats
danc1a60c52010-06-07 14:28:16 +00001892}
1893
dan59257dc2010-08-04 11:34:31 +00001894# Open a new connection on database test.db and execute the SQL script
1895# supplied as an argument. Before returning, close the new conection and
1896# restore the 4 byte fields starting at header offsets 28, 92 and 96
1897# to the values they held before the SQL was executed. This simulates
1898# a write by a pre-3.7.0 client.
1899#
1900proc sql36231 {sql} {
1901 set B [hexio_read test.db 92 8]
1902 set A [hexio_read test.db 28 4]
1903 sqlite3 db36231 test.db
1904 catch { db36231 func a_string a_string }
1905 execsql $sql db36231
1906 db36231 close
1907 hexio_write test.db 28 $A
1908 hexio_write test.db 92 $B
1909 return ""
1910}
danf5894502009-10-07 18:41:19 +00001911
danccc7ff42010-11-29 12:06:45 +00001912proc db_save {} {
1913 foreach f [glob -nocomplain sv_test.db*] { forcedelete $f }
1914 foreach f [glob -nocomplain test.db*] {
1915 set f2 "sv_$f"
mistachkinfda06be2011-08-02 00:57:34 +00001916 forcecopy $f $f2
danccc7ff42010-11-29 12:06:45 +00001917 }
1918}
1919proc db_save_and_close {} {
1920 db_save
1921 catch { db close }
1922 return ""
1923}
1924proc db_restore {} {
1925 foreach f [glob -nocomplain test.db*] { forcedelete $f }
1926 foreach f2 [glob -nocomplain sv_test.db*] {
1927 set f [string range $f2 3 end]
mistachkinfda06be2011-08-02 00:57:34 +00001928 forcecopy $f2 $f
danccc7ff42010-11-29 12:06:45 +00001929 }
1930}
1931proc db_restore_and_reopen {{dbfile test.db}} {
1932 catch { db close }
1933 db_restore
1934 sqlite3 db $dbfile
1935}
1936proc db_delete_and_reopen {{file test.db}} {
1937 catch { db close }
mistachkinfda06be2011-08-02 00:57:34 +00001938 foreach f [glob -nocomplain test.db*] { forcedelete $f }
danccc7ff42010-11-29 12:06:45 +00001939 sqlite3 db $file
1940}
1941
danielk197745901d62004-11-10 15:27:38 +00001942# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
1943# to non-zero, then set the global variable $AUTOVACUUM to 1.
1944set AUTOVACUUM $sqlite_options(default_autovacuum)
danielk1977ee8b7992009-03-26 17:13:06 +00001945
dan64b95bb2012-05-12 05:30:29 +00001946# Make sure the FTS enhanced query syntax is disabled.
1947set sqlite_fts3_enable_parentheses 0
1948
drh09fe6142013-11-29 15:06:27 +00001949# During testing, assume that all database files are well-formed. The
1950# few test cases that deliberately corrupt database files should rescind
1951# this setting by invoking "database_can_be_corrupt"
1952#
1953database_never_corrupt
1954
danielk1977ee8b7992009-03-26 17:13:06 +00001955source $testdir/thread_common.tcl
danddf80eb2010-10-25 12:47:43 +00001956source $testdir/malloc_common.tcl