blob: e4b5edeb809acb491512b40b2e6317be29a7d774 [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drh348784e2000-05-29 20:41:49 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drh348784e2000-05-29 20:41:49 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# May you do good and not evil.
7# May you find forgiveness for yourself and forgive others.
8# May you share freely, never taking more than you give.
drh348784e2000-05-29 20:41:49 +00009#
10#***********************************************************************
11# This file implements some common TCL routines used for regression
12# testing the SQLite library
13#
drhbb77b752009-04-09 01:23:49 +000014# $Id: tester.tcl,v 1.143 2009/04/09 01:23:49 drh Exp $
drhfbc3eab2001-04-06 16:13:42 +000015
danc1a60c52010-06-07 14:28:16 +000016#-------------------------------------------------------------------------
mistachkin1d406e02013-08-29 01:09:14 +000017# The commands provided by the code in this file to help with creating
danc1a60c52010-06-07 14:28:16 +000018# test cases are as follows:
drh8359d8c2008-03-29 11:00:54 +000019#
danc1a60c52010-06-07 14:28:16 +000020# Commands to manipulate the db and the file-system at a high level:
drh8359d8c2008-03-29 11:00:54 +000021#
mistachkinc5484652012-03-05 22:52:33 +000022# is_relative_file
mistachkin4a41f342012-03-06 03:00:49 +000023# test_pwd
mistachkinf8a78462012-03-08 20:00:36 +000024# get_pwd
danc1a60c52010-06-07 14:28:16 +000025# copy_file FROM TO
mistachkinfda06be2011-08-02 00:57:34 +000026# delete_file FILENAME
dan6f343962011-07-01 18:26:40 +000027# drop_all_tables ?DB?
mistachkinfda06be2011-08-02 00:57:34 +000028# forcecopy FROM TO
danc1a60c52010-06-07 14:28:16 +000029# forcedelete FILENAME
30#
31# Test the capability of the SQLite version built into the interpreter to
32# determine if a specific test can be run:
33#
mistachkinc60941f2012-09-13 01:51:02 +000034# capable EXPR
danc1a60c52010-06-07 14:28:16 +000035# ifcapable EXPR
36#
37# Calulate checksums based on database contents:
38#
39# dbcksum DB DBNAME
40# allcksum ?DB?
41# cksum ?DB?
42#
43# Commands to execute/explain SQL statements:
44#
mistachkin1d406e02013-08-29 01:09:14 +000045# memdbsql SQL
danc1a60c52010-06-07 14:28:16 +000046# stepsql DB SQL
47# execsql2 SQL
48# explain_no_trace SQL
49# explain SQL ?DB?
50# catchsql SQL ?DB?
51# execsql SQL ?DB?
52#
53# Commands to run test cases:
54#
55# do_ioerr_test TESTNAME ARGS...
56# crashsql ARGS...
57# integrity_check TESTNAME ?DB?
drh433dccf2013-02-09 15:37:11 +000058# verify_ex_errcode TESTNAME EXPECTED ?DB?
danc1a60c52010-06-07 14:28:16 +000059# do_test TESTNAME SCRIPT EXPECTED
dan153eda02010-06-21 07:45:47 +000060# do_execsql_test TESTNAME SQL EXPECTED
61# do_catchsql_test TESTNAME SQL EXPECTED
danc1a60c52010-06-07 14:28:16 +000062#
dan430e74c2010-06-07 17:47:26 +000063# Commands providing a lower level interface to the global test counters:
danc1a60c52010-06-07 14:28:16 +000064#
65# set_test_counter COUNTER ?VALUE?
mistachkin6c3c1a02011-11-12 03:17:40 +000066# omit_test TESTNAME REASON ?APPEND?
danc1a60c52010-06-07 14:28:16 +000067# fail_test TESTNAME
68# incr_ntest
69#
70# Command run at the end of each test file:
71#
72# finish_test
73#
dan430e74c2010-06-07 17:47:26 +000074# Commands to help create test files that run with the "WAL" and other
75# permutations (see file permutations.test):
danc1a60c52010-06-07 14:28:16 +000076#
77# wal_is_wal_mode
78# wal_set_journal_mode ?DB?
79# wal_check_journal_mode TESTNAME?DB?
dan430e74c2010-06-07 17:47:26 +000080# permutation
dancb79e512010-08-06 13:50:07 +000081# presql
danc1a60c52010-06-07 14:28:16 +000082#
drh348784e2000-05-29 20:41:49 +000083
mistachkin1d406e02013-08-29 01:09:14 +000084# Set the precision of FP arithmatic used by the interpreter. And
danc1a60c52010-06-07 14:28:16 +000085# configure SQLite to take database file locks on the page that begins
86# 64KB into the database file instead of the one 1GB in. This means
87# the code that handles that special case can be tested without creating
88# very large database files.
89#
drh92febd92004-08-20 18:34:20 +000090set tcl_precision 15
drhc7a3bb92009-02-05 16:31:45 +000091sqlite3_test_control_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +000092
danc1a60c52010-06-07 14:28:16 +000093
mistachkin1d406e02013-08-29 01:09:14 +000094# If the pager codec is available, create a wrapper for the [sqlite3]
danc1a60c52010-06-07 14:28:16 +000095# command that appends "-key {xyzzy}" to the command line. i.e. this:
drh3a7fb7c2007-08-10 16:41:08 +000096#
danc1a60c52010-06-07 14:28:16 +000097# sqlite3 db test.db
drh4a50aac2007-08-23 02:47:53 +000098#
danc1a60c52010-06-07 14:28:16 +000099# becomes
drh4a50aac2007-08-23 02:47:53 +0000100#
danc1a60c52010-06-07 14:28:16 +0000101# sqlite3 db test.db -key {xyzzy}
drh9eb9e262004-02-11 02:18:05 +0000102#
dan430e74c2010-06-07 17:47:26 +0000103if {[info command sqlite_orig]==""} {
drhef4ac8f2004-06-19 00:16:31 +0000104 rename sqlite3 sqlite_orig
105 proc sqlite3 {args} {
dan68928b62010-06-22 13:46:43 +0000106 if {[llength $args]>=2 && [string index [lindex $args 0] 0]!="-"} {
dan430e74c2010-06-07 17:47:26 +0000107 # This command is opening a new database connection.
108 #
109 if {[info exists ::G(perm:sqlite3_args)]} {
110 set args [concat $args $::G(perm:sqlite3_args)]
111 }
dan68928b62010-06-22 13:46:43 +0000112 if {[sqlite_orig -has-codec] && ![info exists ::do_not_use_codec]} {
dan430e74c2010-06-07 17:47:26 +0000113 lappend args -key {xyzzy}
114 }
115
dan3ccd20a2010-06-09 19:01:02 +0000116 set res [uplevel 1 sqlite_orig $args]
dan430e74c2010-06-07 17:47:26 +0000117 if {[info exists ::G(perm:presql)]} {
118 [lindex $args 0] eval $::G(perm:presql)
119 }
dan1ce1b4a2010-12-07 14:32:28 +0000120 if {[info exists ::G(perm:dbconfig)]} {
121 set ::dbhandle [lindex $args 0]
122 uplevel #0 $::G(perm:dbconfig)
123 }
dan3ccd20a2010-06-09 19:01:02 +0000124 set res
dan430e74c2010-06-07 17:47:26 +0000125 } else {
mistachkin1d406e02013-08-29 01:09:14 +0000126 # This command is not opening a new database connection. Pass the
mistachkin48864df2013-03-21 21:20:32 +0000127 # arguments through to the C implementation as the are.
dan430e74c2010-06-07 17:47:26 +0000128 #
129 uplevel 1 sqlite_orig $args
drh9eb9e262004-02-11 02:18:05 +0000130 }
drh9eb9e262004-02-11 02:18:05 +0000131 }
132}
133
mistachkinfda06be2011-08-02 00:57:34 +0000134proc getFileRetries {} {
135 if {![info exists ::G(file-retries)]} {
136 #
137 # NOTE: Return the default number of retries for [file] operations. A
138 # value of zero or less here means "disabled".
139 #
mistachkinc60941f2012-09-13 01:51:02 +0000140 return [expr {$::tcl_platform(platform) eq "windows" ? 50 : 0}]
mistachkinfda06be2011-08-02 00:57:34 +0000141 }
142 return $::G(file-retries)
143}
144
145proc getFileRetryDelay {} {
146 if {![info exists ::G(file-retry-delay)]} {
147 #
148 # NOTE: Return the default number of milliseconds to wait when retrying
149 # failed [file] operations. A value of zero or less means "do not
150 # wait".
151 #
152 return 100; # TODO: Good default?
153 }
154 return $::G(file-retry-delay)
155}
156
mistachkinf8a78462012-03-08 20:00:36 +0000157# Return the string representing the name of the current directory. On
158# Windows, the result is "normalized" to whatever our parent command shell
159# is using to prevent case-mismatch issues.
160#
161proc get_pwd {} {
162 if {$::tcl_platform(platform) eq "windows"} {
mistachkin533b8f62012-03-08 20:28:31 +0000163 #
164 # NOTE: Cannot use [file normalize] here because it would alter the
165 # case of the result to what Tcl considers canonical, which would
166 # defeat the purpose of this procedure.
167 #
168 return [string map [list \\ /] \
169 [string trim [exec -- $::env(ComSpec) /c echo %CD%]]]
mistachkinf8a78462012-03-08 20:00:36 +0000170 } else {
171 return [pwd]
172 }
173}
174
mistachkinfda06be2011-08-02 00:57:34 +0000175# Copy file $from into $to. This is used because some versions of
176# TCL for windows (notably the 8.4.1 binary package shipped with the
177# current mingw release) have a broken "file copy" command.
178#
179proc copy_file {from to} {
180 do_copy_file false $from $to
181}
182
183proc forcecopy {from to} {
184 do_copy_file true $from $to
185}
186
187proc do_copy_file {force from to} {
188 set nRetry [getFileRetries] ;# Maximum number of retries.
189 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
190
191 # On windows, sometimes even a [file copy -force] can fail. The cause is
192 # usually "tag-alongs" - programs like anti-virus software, automatic backup
193 # tools and various explorer extensions that keep a file open a little longer
194 # than we expect, causing the delete to fail.
195 #
196 # The solution is to wait a short amount of time before retrying the copy.
197 #
198 if {$nRetry > 0} {
199 for {set i 0} {$i<$nRetry} {incr i} {
200 set rc [catch {
201 if {$force} {
202 file copy -force $from $to
203 } else {
204 file copy $from $to
205 }
206 } msg]
207 if {$rc==0} break
208 if {$nDelay > 0} { after $nDelay }
209 }
210 if {$rc} { error $msg }
211 } else {
212 if {$force} {
213 file copy -force $from $to
214 } else {
215 file copy $from $to
216 }
217 }
218}
219
mistachkinc5484652012-03-05 22:52:33 +0000220# Check if a file name is relative
221#
222proc is_relative_file { file } {
223 return [expr {[file pathtype $file] != "absolute"}]
224}
225
mistachkin4a41f342012-03-06 03:00:49 +0000226# If the VFS supports using the current directory, returns [pwd];
227# otherwise, it returns only the provided suffix string (which is
228# empty by default).
229#
230proc test_pwd { args } {
231 if {[llength $args] > 0} {
232 set suffix1 [lindex $args 0]
233 if {[llength $args] > 1} {
234 set suffix2 [lindex $args 1]
235 } else {
236 set suffix2 $suffix1
237 }
238 } else {
239 set suffix1 ""; set suffix2 ""
240 }
241 ifcapable curdir {
mistachkin6aa18c92012-03-08 20:22:42 +0000242 return "[get_pwd]$suffix1"
mistachkin4a41f342012-03-06 03:00:49 +0000243 } else {
244 return $suffix2
245 }
246}
247
mistachkinfda06be2011-08-02 00:57:34 +0000248# Delete a file or directory
249#
250proc delete_file {args} {
251 do_delete_file false {*}$args
252}
253
254proc forcedelete {args} {
255 do_delete_file true {*}$args
256}
257
258proc do_delete_file {force args} {
259 set nRetry [getFileRetries] ;# Maximum number of retries.
260 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
261
262 foreach filename $args {
263 # On windows, sometimes even a [file delete -force] can fail just after
264 # a file is closed. The cause is usually "tag-alongs" - programs like
265 # anti-virus software, automatic backup tools and various explorer
266 # extensions that keep a file open a little longer than we expect, causing
267 # the delete to fail.
268 #
269 # The solution is to wait a short amount of time before retrying the
270 # delete.
271 #
272 if {$nRetry > 0} {
273 for {set i 0} {$i<$nRetry} {incr i} {
274 set rc [catch {
275 if {$force} {
276 file delete -force $filename
277 } else {
278 file delete $filename
279 }
280 } msg]
281 if {$rc==0} break
282 if {$nDelay > 0} { after $nDelay }
283 }
284 if {$rc} { error $msg }
285 } else {
286 if {$force} {
287 file delete -force $filename
288 } else {
289 file delete $filename
290 }
291 }
292 }
293}
294
mistachkin1d406e02013-08-29 01:09:14 +0000295if {$::tcl_platform(platform) eq "windows"} {
296 proc do_remove_win32_dir {args} {
297 set nRetry [getFileRetries] ;# Maximum number of retries.
298 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
299
300 foreach dirName $args {
301 # On windows, sometimes even a [remove_win32_dir] can fail just after
302 # a directory is emptied. The cause is usually "tag-alongs" - programs
303 # like anti-virus software, automatic backup tools and various explorer
304 # extensions that keep a file open a little longer than we expect,
305 # causing the delete to fail.
306 #
307 # The solution is to wait a short amount of time before retrying the
308 # removal.
309 #
310 if {$nRetry > 0} {
311 for {set i 0} {$i < $nRetry} {incr i} {
312 set rc [catch {
313 remove_win32_dir $dirName
314 } msg]
315 if {$rc == 0} break
316 if {$nDelay > 0} { after $nDelay }
317 }
318 if {$rc} { error $msg }
319 } else {
320 remove_win32_dir $dirName
321 }
322 }
323 }
324
325 proc do_delete_win32_file {args} {
326 set nRetry [getFileRetries] ;# Maximum number of retries.
327 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
328
329 foreach fileName $args {
330 # On windows, sometimes even a [delete_win32_file] can fail just after
331 # a file is closed. The cause is usually "tag-alongs" - programs like
332 # anti-virus software, automatic backup tools and various explorer
333 # extensions that keep a file open a little longer than we expect,
334 # causing the delete to fail.
335 #
336 # The solution is to wait a short amount of time before retrying the
337 # delete.
338 #
339 if {$nRetry > 0} {
340 for {set i 0} {$i < $nRetry} {incr i} {
341 set rc [catch {
342 delete_win32_file $fileName
343 } msg]
344 if {$rc == 0} break
345 if {$nDelay > 0} { after $nDelay }
346 }
347 if {$rc} { error $msg }
348 } else {
349 delete_win32_file $fileName
350 }
351 }
352 }
353}
354
dancb354602010-07-08 09:44:42 +0000355proc execpresql {handle args} {
356 trace remove execution $handle enter [list execpresql $handle]
357 if {[info exists ::G(perm:presql)]} {
358 $handle eval $::G(perm:presql)
359 }
360}
361
dan68928b62010-06-22 13:46:43 +0000362# This command should be called after loading tester.tcl from within
363# all test scripts that are incompatible with encryption codecs.
364#
365proc do_not_use_codec {} {
366 set ::do_not_use_codec 1
367 reset_db
368}
369
dan430e74c2010-06-07 17:47:26 +0000370# The following block only runs the first time this file is sourced. It
371# does not run in slave interpreters (since the ::cmdlinearg array is
372# populated before the test script is run in slave interpreters).
drhbec2bf42000-05-29 23:48:22 +0000373#
danc1a60c52010-06-07 14:28:16 +0000374if {[info exists cmdlinearg]==0} {
375
mistachkin1d406e02013-08-29 01:09:14 +0000376 # Parse any options specified in the $argv array. This script accepts the
377 # following options:
danc1a60c52010-06-07 14:28:16 +0000378 #
379 # --pause
380 # --soft-heap-limit=NN
381 # --maxerror=NN
382 # --malloctrace=N
383 # --backtrace=N
384 # --binarylog=N
dan0626dfc2010-06-15 06:56:37 +0000385 # --soak=N
mistachkinfda06be2011-08-02 00:57:34 +0000386 # --file-retries=N
387 # --file-retry-delay=N
dan6a64d672011-04-04 15:38:16 +0000388 # --start=[$permutation:]$testfile
mistachkin6c3c1a02011-11-12 03:17:40 +0000389 # --match=$pattern
danc1a60c52010-06-07 14:28:16 +0000390 #
391 set cmdlinearg(soft-heap-limit) 0
392 set cmdlinearg(maxerror) 1000
393 set cmdlinearg(malloctrace) 0
394 set cmdlinearg(backtrace) 10
395 set cmdlinearg(binarylog) 0
dan0626dfc2010-06-15 06:56:37 +0000396 set cmdlinearg(soak) 0
mistachkinfda06be2011-08-02 00:57:34 +0000397 set cmdlinearg(file-retries) 0
398 set cmdlinearg(file-retry-delay) 0
mistachkin6c3c1a02011-11-12 03:17:40 +0000399 set cmdlinearg(start) ""
400 set cmdlinearg(match) ""
danc1a60c52010-06-07 14:28:16 +0000401
402 set leftover [list]
403 foreach a $argv {
404 switch -regexp -- $a {
405 {^-+pause$} {
mistachkin1d406e02013-08-29 01:09:14 +0000406 # Wait for user input before continuing. This is to give the user an
danc1a60c52010-06-07 14:28:16 +0000407 # opportunity to connect profiling tools to the process.
408 puts -nonewline "Press RETURN to begin..."
409 flush stdout
410 gets stdin
411 }
412 {^-+soft-heap-limit=.+$} {
413 foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break
414 }
415 {^-+maxerror=.+$} {
416 foreach {dummy cmdlinearg(maxerror)} [split $a =] break
417 }
418 {^-+malloctrace=.+$} {
419 foreach {dummy cmdlinearg(malloctrace)} [split $a =] break
420 if {$cmdlinearg(malloctrace)} {
421 sqlite3_memdebug_log start
422 }
423 }
424 {^-+backtrace=.+$} {
425 foreach {dummy cmdlinearg(backtrace)} [split $a =] break
dan0626dfc2010-06-15 06:56:37 +0000426 sqlite3_memdebug_backtrace $value
danc1a60c52010-06-07 14:28:16 +0000427 }
danc1a60c52010-06-07 14:28:16 +0000428 {^-+binarylog=.+$} {
429 foreach {dummy cmdlinearg(binarylog)} [split $a =] break
430 }
dan0626dfc2010-06-15 06:56:37 +0000431 {^-+soak=.+$} {
432 foreach {dummy cmdlinearg(soak)} [split $a =] break
433 set ::G(issoak) $cmdlinearg(soak)
434 }
mistachkinfda06be2011-08-02 00:57:34 +0000435 {^-+file-retries=.+$} {
436 foreach {dummy cmdlinearg(file-retries)} [split $a =] break
437 set ::G(file-retries) $cmdlinearg(file-retries)
438 }
439 {^-+file-retry-delay=.+$} {
440 foreach {dummy cmdlinearg(file-retry-delay)} [split $a =] break
441 set ::G(file-retry-delay) $cmdlinearg(file-retry-delay)
442 }
dan6a64d672011-04-04 15:38:16 +0000443 {^-+start=.+$} {
444 foreach {dummy cmdlinearg(start)} [split $a =] break
445
446 set ::G(start:file) $cmdlinearg(start)
447 if {[regexp {(.*):(.*)} $cmdlinearg(start) -> s.perm s.file]} {
448 set ::G(start:permutation) ${s.perm}
449 set ::G(start:file) ${s.file}
450 }
451 if {$::G(start:file) == ""} {unset ::G(start:file)}
452 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000453 {^-+match=.+$} {
454 foreach {dummy cmdlinearg(match)} [split $a =] break
455
456 set ::G(match) $cmdlinearg(match)
457 if {$::G(match) == ""} {unset ::G(match)}
458 }
danc1a60c52010-06-07 14:28:16 +0000459 default {
460 lappend leftover $a
461 }
462 }
463 }
464 set argv $leftover
465
dan430e74c2010-06-07 17:47:26 +0000466 # Install the malloc layer used to inject OOM errors. And the 'automatic'
467 # extensions. This only needs to be done once for the process.
468 #
mistachkin1d406e02013-08-29 01:09:14 +0000469 sqlite3_shutdown
470 install_malloc_faultsim 1
danielk1977f7b9d662008-06-23 18:49:43 +0000471 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000472 autoinstall_test_functions
danc1a60c52010-06-07 14:28:16 +0000473
dan430e74c2010-06-07 17:47:26 +0000474 # If the --binarylog option was specified, create the logging VFS. This
475 # call installs the new VFS as the default for all SQLite connections.
476 #
danc1a60c52010-06-07 14:28:16 +0000477 if {$cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000478 vfslog new binarylog {} vfslog.bin
danc1a60c52010-06-07 14:28:16 +0000479 }
480
481 # Set the backtrace depth, if malloc tracing is enabled.
482 #
483 if {$cmdlinearg(malloctrace)} {
484 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danielk1977185eac92008-07-12 15:55:54 +0000485 }
danielk1977f7b9d662008-06-23 18:49:43 +0000486}
danielk197703ba3fa2009-01-09 10:49:14 +0000487
danc1a60c52010-06-07 14:28:16 +0000488# Update the soft-heap-limit each time this script is run. In that
489# way if an individual test file changes the soft-heap-limit, it
490# will be reset at the start of the next test file.
491#
492sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
493
494# Create a test database
495#
danielk197703ba3fa2009-01-09 10:49:14 +0000496proc reset_db {} {
497 catch {db close}
mistachkinfda06be2011-08-02 00:57:34 +0000498 forcedelete test.db
499 forcedelete test.db-journal
500 forcedelete test.db-wal
danielk197703ba3fa2009-01-09 10:49:14 +0000501 sqlite3 db ./test.db
502 set ::DB [sqlite3_connection_pointer db]
503 if {[info exists ::SETUP_SQL]} {
504 db eval $::SETUP_SQL
505 }
drhcd61c282002-03-06 22:01:34 +0000506}
danielk197703ba3fa2009-01-09 10:49:14 +0000507reset_db
drhbec2bf42000-05-29 23:48:22 +0000508
509# Abort early if this script has been run before.
510#
danc1a60c52010-06-07 14:28:16 +0000511if {[info exists TC(count)]} return
drhbec2bf42000-05-29 23:48:22 +0000512
drhce2198c2010-09-10 13:22:59 +0000513# Make sure memory statistics are enabled.
514#
515sqlite3_config_memstatus 1
516
danc1a60c52010-06-07 14:28:16 +0000517# Initialize the test counters and set up commands to access them.
518# Or, if this is a slave interpreter, set up aliases to write the
519# counters in the parent interpreter.
drhbec2bf42000-05-29 23:48:22 +0000520#
danc1a60c52010-06-07 14:28:16 +0000521if {0==[info exists ::SLAVE]} {
522 set TC(errors) 0
523 set TC(count) 0
524 set TC(fail_list) [list]
525 set TC(omit_list) [list]
drhcca17c32013-04-19 12:32:52 +0000526 set TC(warn_list) [list]
danc1a60c52010-06-07 14:28:16 +0000527
528 proc set_test_counter {counter args} {
529 if {[llength $args]} {
530 set ::TC($counter) [lindex $args 0]
531 }
532 set ::TC($counter)
533 }
drhb62c3352006-11-23 09:39:16 +0000534}
drh348784e2000-05-29 20:41:49 +0000535
drh521cc842008-04-15 02:36:33 +0000536# Record the fact that a sequence of tests were omitted.
537#
mistachkin6c3c1a02011-11-12 03:17:40 +0000538proc omit_test {name reason {append 1}} {
danc1a60c52010-06-07 14:28:16 +0000539 set omitList [set_test_counter omit_list]
mistachkin6c3c1a02011-11-12 03:17:40 +0000540 if {$append} {
541 lappend omitList [list $name $reason]
542 }
danc1a60c52010-06-07 14:28:16 +0000543 set_test_counter omit_list $omitList
drh521cc842008-04-15 02:36:33 +0000544}
545
danc1a60c52010-06-07 14:28:16 +0000546# Record the fact that a test failed.
547#
548proc fail_test {name} {
549 set f [set_test_counter fail_list]
550 lappend f $name
551 set_test_counter fail_list $f
552 set_test_counter errors [expr [set_test_counter errors] + 1]
553
554 set nFail [set_test_counter errors]
555 if {$nFail>=$::cmdlinearg(maxerror)} {
556 puts "*** Giving up..."
557 finalize_testing
558 }
559}
560
drhcca17c32013-04-19 12:32:52 +0000561# Remember a warning message to be displayed at the conclusion of all testing
562#
563proc warning {msg {append 1}} {
564 puts "Warning: $msg"
565 set warnList [set_test_counter warn_list]
566 if {$append} {
567 lappend warnList $msg
568 }
569 set_test_counter warn_list $warnList
570}
571
572
danc1a60c52010-06-07 14:28:16 +0000573# Increment the number of tests run
574#
575proc incr_ntest {} {
576 set_test_counter count [expr [set_test_counter count] + 1]
577}
578
579
mistachkin1d406e02013-08-29 01:09:14 +0000580# Invoke the do_test procedure to run a single test
drh348784e2000-05-29 20:41:49 +0000581#
582proc do_test {name cmd expected} {
danc1a60c52010-06-07 14:28:16 +0000583 global argv cmdlinearg
584
dan8c408002010-11-01 17:38:24 +0000585 fix_testname name
586
drh4a50aac2007-08-23 02:47:53 +0000587 sqlite3_memdebug_settitle $name
danc1a60c52010-06-07 14:28:16 +0000588
mistachkin1d406e02013-08-29 01:09:14 +0000589# if {[llength $argv]==0} {
dan92d516a2010-07-05 14:54:48 +0000590# set go 1
591# } else {
592# set go 0
593# foreach pattern $argv {
594# if {[string match $pattern $name]} {
595# set go 1
596# break
597# }
598# }
599# }
dan430e74c2010-06-07 17:47:26 +0000600
dand506de02010-07-03 13:59:01 +0000601 if {[info exists ::G(perm:prefix)]} {
602 set name "$::G(perm:prefix)$name"
dan430e74c2010-06-07 17:47:26 +0000603 }
604
danc1a60c52010-06-07 14:28:16 +0000605 incr_ntest
drh5edc3122001-09-13 21:53:09 +0000606 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000607 flush stdout
mistachkin6c3c1a02011-11-12 03:17:40 +0000608
609 if {![info exists ::G(match)] || [string match $::G(match) $name]} {
610 if {[catch {uplevel #0 "$cmd;\n"} result]} {
611 puts "\nError: $result"
612 fail_test $name
mistachkin6c3c1a02011-11-12 03:17:40 +0000613 } else {
drh3f17aef2012-04-27 01:08:02 +0000614 if {[regexp {^~?/.*/$} $expected]} {
drh70bdcc72013-05-30 19:28:34 +0000615 # "expected" is of the form "/PATTERN/" then the result if correct if
616 # regular expression PATTERN matches the result. "~/PATTERN/" means
617 # the regular expression must not match.
drh3f17aef2012-04-27 01:08:02 +0000618 if {[string index $expected 0]=="~"} {
drh5343b2d2012-09-27 19:53:38 +0000619 set re [string map {# {[-0-9.]+}} [string range $expected 2 end-1]]
drh3f17aef2012-04-27 01:08:02 +0000620 set ok [expr {![regexp $re $result]}]
621 } else {
drh5343b2d2012-09-27 19:53:38 +0000622 set re [string map {# {[-0-9.]+}} [string range $expected 1 end-1]]
drh3f17aef2012-04-27 01:08:02 +0000623 set ok [regexp $re $result]
624 }
drh70bdcc72013-05-30 19:28:34 +0000625 } elseif {[regexp {^~?\*.*\*$} $expected]} {
626 # "expected" is of the form "*GLOB*" then the result if correct if
627 # glob pattern GLOB matches the result. "~/GLOB/" means
628 # the glob must not match.
629 if {[string index $expected 0]=="~"} {
630 set e [string range $expected 1 end]
631 set ok [expr {![string match $e $result]}]
632 } else {
633 set ok [string match $expected $result]
634 }
drh3f17aef2012-04-27 01:08:02 +0000635 } else {
636 set ok [expr {[string compare $result $expected]==0}]
637 }
638 if {!$ok} {
mistachkinc60941f2012-09-13 01:51:02 +0000639 # if {![info exists ::testprefix] || $::testprefix eq ""} {
640 # error "no test prefix"
641 # }
drh3f17aef2012-04-27 01:08:02 +0000642 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
643 fail_test $name
644 } else {
645 puts " Ok"
646 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000647 }
drh348784e2000-05-29 20:41:49 +0000648 } else {
mistachkin6c3c1a02011-11-12 03:17:40 +0000649 puts " Omitted"
650 omit_test $name "pattern mismatch" 0
drh348784e2000-05-29 20:41:49 +0000651 }
drh6558db82007-04-13 03:23:21 +0000652 flush stdout
drh348784e2000-05-29 20:41:49 +0000653}
dana3f51082010-09-30 18:43:14 +0000654
drh8df91852012-04-24 12:46:05 +0000655proc catchcmd {db {cmd ""}} {
656 global CLI
657 set out [open cmds.txt w]
658 puts $out $cmd
659 close $out
660 set line "exec $CLI $db < cmds.txt"
661 set rc [catch { eval $line } msg]
662 list $rc $msg
663}
664
shaneh55505172011-06-21 19:30:19 +0000665proc filepath_normalize {p} {
666 # test cases should be written to assume "unix"-like file paths
shaneh285a18f2011-06-21 19:39:59 +0000667 if {$::tcl_platform(platform)!="unix"} {
shaneh55505172011-06-21 19:30:19 +0000668 # lreverse*2 as a hack to remove any unneeded {} after the string map
669 lreverse [lreverse [string map {\\ /} [regsub -nocase -all {[a-z]:[/\\]+} $p {/}]]]
shanehc4896402011-06-21 19:38:16 +0000670 } {
671 set p
shaneh55505172011-06-21 19:30:19 +0000672 }
673}
674proc do_filepath_test {name cmd expected} {
675 uplevel [list do_test $name [
676 subst -nocommands { filepath_normalize [ $cmd ] }
677 ] [filepath_normalize $expected]]
678}
679
dan33f53792011-05-05 19:44:22 +0000680proc realnum_normalize {r} {
shaneh4f529e82011-06-20 17:41:41 +0000681 # different TCL versions display floating point values differently.
682 string map {1.#INF inf Inf inf .0e e} [regsub -all {(e[+-])0+} $r {\1}]
dan33f53792011-05-05 19:44:22 +0000683}
684proc do_realnum_test {name cmd expected} {
685 uplevel [list do_test $name [
686 subst -nocommands { realnum_normalize [ $cmd ] }
687 ] [realnum_normalize $expected]]
688}
689
dana3f51082010-09-30 18:43:14 +0000690proc fix_testname {varname} {
691 upvar $varname testname
mistachkin1d406e02013-08-29 01:09:14 +0000692 if {[info exists ::testprefix]
dana3f51082010-09-30 18:43:14 +0000693 && [string is digit [string range $testname 0 0]]
694 } {
695 set testname "${::testprefix}-$testname"
696 }
697}
mistachkin1d406e02013-08-29 01:09:14 +0000698
dan57f7f4b2010-10-01 19:04:37 +0000699proc do_execsql_test {testname sql {result {}}} {
dana3f51082010-09-30 18:43:14 +0000700 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000701 uplevel do_test [list $testname] [list "execsql {$sql}"] [list [list {*}$result]]
dan153eda02010-06-21 07:45:47 +0000702}
703proc do_catchsql_test {testname sql result} {
dana3f51082010-09-30 18:43:14 +0000704 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000705 uplevel do_test [list $testname] [list "catchsql {$sql}"] [list $result]
dan153eda02010-06-21 07:45:47 +0000706}
dan39854792010-11-15 16:12:58 +0000707proc do_eqp_test {name sql res} {
708 uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res]
709}
dan153eda02010-06-21 07:45:47 +0000710
dan51f06982010-09-18 19:00:12 +0000711#-------------------------------------------------------------------------
712# Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST
713#
714# Where switches are:
715#
716# -errorformat FMTSTRING
717# -count
danaf1dcab2010-09-20 19:17:53 +0000718# -query SQL
dana16d1062010-09-28 17:37:28 +0000719# -tclquery TCL
danaf7626f2010-09-23 18:47:36 +0000720# -repair TCL
dan51f06982010-09-18 19:00:12 +0000721#
722proc do_select_tests {prefix args} {
723
724 set testlist [lindex $args end]
725 set switches [lrange $args 0 end-1]
726
727 set errfmt ""
728 set countonly 0
dana16d1062010-09-28 17:37:28 +0000729 set tclquery ""
danaf7626f2010-09-23 18:47:36 +0000730 set repair ""
dan51f06982010-09-18 19:00:12 +0000731
732 for {set i 0} {$i < [llength $switches]} {incr i} {
733 set s [lindex $switches $i]
734 set n [string length $s]
danaf1dcab2010-09-20 19:17:53 +0000735 if {$n>=2 && [string equal -length $n $s "-query"]} {
dana16d1062010-09-28 17:37:28 +0000736 set tclquery [list execsql [lindex $switches [incr i]]]
737 } elseif {$n>=2 && [string equal -length $n $s "-tclquery"]} {
738 set tclquery [lindex $switches [incr i]]
danaf1dcab2010-09-20 19:17:53 +0000739 } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} {
dan51f06982010-09-18 19:00:12 +0000740 set errfmt [lindex $switches [incr i]]
danaf7626f2010-09-23 18:47:36 +0000741 } elseif {$n>=2 && [string equal -length $n $s "-repair"]} {
742 set repair [lindex $switches [incr i]]
dan51f06982010-09-18 19:00:12 +0000743 } elseif {$n>=2 && [string equal -length $n $s "-count"]} {
744 set countonly 1
745 } else {
746 error "unknown switch: $s"
747 }
748 }
749
750 if {$countonly && $errfmt!=""} {
751 error "Cannot use -count and -errorformat together"
752 }
753 set nTestlist [llength $testlist]
754 if {$nTestlist%3 || $nTestlist==0 } {
755 error "SELECT test list contains [llength $testlist] elements"
756 }
757
danaf7626f2010-09-23 18:47:36 +0000758 eval $repair
dan51f06982010-09-18 19:00:12 +0000759 foreach {tn sql res} $testlist {
dana16d1062010-09-28 17:37:28 +0000760 if {$tclquery != ""} {
danaf1dcab2010-09-20 19:17:53 +0000761 execsql $sql
dana16d1062010-09-28 17:37:28 +0000762 uplevel do_test ${prefix}.$tn [list $tclquery] [list [list {*}$res]]
763 } elseif {$countonly} {
dan51f06982010-09-18 19:00:12 +0000764 set nRow 0
765 db eval $sql {incr nRow}
766 uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res]
767 } elseif {$errfmt==""} {
768 uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]]
769 } else {
770 set res [list 1 [string trim [format $errfmt {*}$res]]]
771 uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res]
772 }
danaf7626f2010-09-23 18:47:36 +0000773 eval $repair
dan51f06982010-09-18 19:00:12 +0000774 }
danaf7626f2010-09-23 18:47:36 +0000775
dan51f06982010-09-18 19:00:12 +0000776}
777
danb04757a2010-09-21 16:59:16 +0000778proc delete_all_data {} {
779 db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} {
780 db eval "DELETE FROM '[string map {' ''} $t]'"
781 }
782}
drh348784e2000-05-29 20:41:49 +0000783
mistachkin1d406e02013-08-29 01:09:14 +0000784# Run an SQL script.
drhb62c3352006-11-23 09:39:16 +0000785# Return the number of microseconds per statement.
786#
drh3590f152006-11-23 21:09:10 +0000787proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000788 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000789 flush stdout
790 set speed [time {sqlite3_exec_nr db $sql}]
791 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000792 if {$tm == 0} {
793 set rate [format %20s "many"]
794 } else {
795 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
796 }
drh3590f152006-11-23 21:09:10 +0000797 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000798 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000799 global total_time
800 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000801 lappend ::speed_trial_times $name $tm
drhdd924312007-03-31 22:34:16 +0000802}
drh45c236d2008-03-22 01:08:00 +0000803proc speed_trial_tcl {name numstmt units script} {
804 puts -nonewline [format {%-21.21s } $name...]
805 flush stdout
806 set speed [time {eval $script}]
807 set tm [lindex $speed 0]
808 if {$tm == 0} {
809 set rate [format %20s "many"]
810 } else {
811 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
812 }
813 set u2 $units/s
814 puts [format {%12d uS %s %s} $tm $rate $u2]
815 global total_time
816 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000817 lappend ::speed_trial_times $name $tm
drh45c236d2008-03-22 01:08:00 +0000818}
drhdd924312007-03-31 22:34:16 +0000819proc speed_trial_init {name} {
820 global total_time
821 set total_time 0
dana975b592010-10-08 16:09:43 +0000822 set ::speed_trial_times [list]
drhbb810a92010-07-03 12:00:53 +0000823 sqlite3 versdb :memory:
824 set vers [versdb one {SELECT sqlite_source_id()}]
825 versdb close
826 puts "SQLite $vers"
drhdd924312007-03-31 22:34:16 +0000827}
828proc speed_trial_summary {name} {
829 global total_time
830 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
dana975b592010-10-08 16:09:43 +0000831
832 if { 0 } {
833 sqlite3 versdb :memory:
834 set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0]
835 versdb close
836 puts "CREATE TABLE IF NOT EXISTS time(version, script, test, us);"
837 foreach {test us} $::speed_trial_times {
838 puts "INSERT INTO time VALUES('$vers', '$name', '$test', $us);"
839 }
840 }
drhb62c3352006-11-23 09:39:16 +0000841}
842
drh348784e2000-05-29 20:41:49 +0000843# Run this routine last
844#
845proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +0000846 catch {db close}
847 catch {db2 close}
848 catch {db3 close}
849 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +0000850}
851proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +0000852 global sqlite_open_file_count
853
854 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +0000855
drh6e142f52000-06-08 13:36:40 +0000856 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000857 catch {db2 close}
858 catch {db3 close}
859
drh9bc54492007-10-23 14:49:59 +0000860 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000861 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000862 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000863 db close
drh984bfaa2008-03-19 16:08:53 +0000864 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +0000865
drh3a7fb7c2007-08-10 16:41:08 +0000866 sqlite3_soft_heap_limit 0
danc1a60c52010-06-07 14:28:16 +0000867 set nTest [incr_ntest]
868 set nErr [set_test_counter errors]
869
drhef866372013-05-22 20:49:02 +0000870 set nKnown 0
871 if {[file readable known-problems.txt]} {
872 set fd [open known-problems.txt]
873 set content [read $fd]
874 close $fd
875 foreach x $content {set known_error($x) 1}
876 foreach x [set_test_counter fail_list] {
877 if {[info exists known_error($x)]} {incr nKnown}
878 }
879 }
880 if {$nKnown>0} {
881 puts "[expr {$nErr-$nKnown}] new errors and $nKnown known errors\
882 out of $nTest tests"
883 } else {
884 puts "$nErr errors out of $nTest tests"
885 }
886 if {$nErr>$nKnown} {
887 puts -nonewline "Failures on these tests:"
888 foreach x [set_test_counter fail_list] {
889 if {![info exists known_error($x)]} {puts -nonewline " $x"}
890 }
891 puts ""
drhf3a65f72007-08-22 20:18:21 +0000892 }
drhcca17c32013-04-19 12:32:52 +0000893 foreach warning [set_test_counter warn_list] {
894 puts "Warning: $warning"
895 }
danielk1977ee8b7992009-03-26 17:13:06 +0000896 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +0000897 if {[llength $omitList]>0} {
898 puts "Omitted test cases:"
899 set prec {}
900 foreach {rec} [lsort $omitList] {
901 if {$rec==$prec} continue
902 set prec $rec
903 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
904 }
905 }
drh80788d82006-09-02 14:50:23 +0000906 if {$nErr>0 && ![working_64bit_int]} {
907 puts "******************************************************************"
908 puts "N.B.: The version of TCL that you used to build this test harness"
909 puts "is defective in that it does not support 64-bit integers. Some or"
910 puts "all of the test failures above might be a result from this defect"
911 puts "in your TCL build."
912 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000913 }
danc1a60c52010-06-07 14:28:16 +0000914 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000915 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +0000916 }
drh94e92032003-02-16 22:21:32 +0000917 if {$sqlite_open_file_count} {
918 puts "$sqlite_open_file_count files were left open"
919 incr nErr
920 }
drheafc43b2010-07-26 18:43:40 +0000921 if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 ||
922 [sqlite3_memory_used]>0} {
923 puts "Unfreed memory: [sqlite3_memory_used] bytes in\
924 [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations"
drhf3a65f72007-08-22 20:18:21 +0000925 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000926 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000927 puts "Writing unfreed memory log to \"./memleak.txt\""
928 sqlite3_memdebug_dump ./memleak.txt
929 }
drhf3a65f72007-08-22 20:18:21 +0000930 } else {
931 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000932 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000933 sqlite3_memdebug_dump ./memusage.txt
934 }
drhf3a65f72007-08-22 20:18:21 +0000935 }
drhf7141992008-06-19 00:16:08 +0000936 show_memstats
drh91fd4d42008-01-19 20:11:25 +0000937 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
938 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000939 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
940 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
941 }
danc1a60c52010-06-07 14:28:16 +0000942 if {$::cmdlinearg(malloctrace)} {
danielk197735754ac2008-03-21 17:29:37 +0000943 puts "Writing mallocs.sql..."
944 memdebug_log_sql
945 sqlite3_memdebug_log stop
946 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000947
948 if {[sqlite3_memory_used]>0} {
949 puts "Writing leaks.sql..."
950 sqlite3_memdebug_log sync
951 memdebug_log_sql leaks.sql
952 }
danielk197735754ac2008-03-21 17:29:37 +0000953 }
drhd9910fe2006-10-04 11:55:49 +0000954 foreach f [glob -nocomplain test.db-*-journal] {
mistachkinfda06be2011-08-02 00:57:34 +0000955 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +0000956 }
957 foreach f [glob -nocomplain test.db-mj*] {
mistachkinfda06be2011-08-02 00:57:34 +0000958 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +0000959 }
drhdb25e382001-03-15 18:21:22 +0000960 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000961}
962
drhf7141992008-06-19 00:16:08 +0000963# Display memory statistics for analysis and debugging purposes.
964#
965proc show_memstats {} {
966 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +0000967 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
968 set val [format {now %10d max %10d max-size %10d} \
969 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000970 puts "Memory used: $val"
drheafc43b2010-07-26 18:43:40 +0000971 set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0]
972 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
973 puts "Allocation count: $val"
drhf7141992008-06-19 00:16:08 +0000974 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +0000975 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
976 set val [format {now %10d max %10d max-size %10d} \
977 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000978 puts "Page-cache used: $val"
979 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
980 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
981 puts "Page-cache overflow: $val"
982 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
983 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
984 puts "Scratch memory used: $val"
985 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +0000986 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
987 set val [format {now %10d max %10d max-size %10d} \
988 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000989 puts "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +0000990 ifcapable yytrackmaxstackdepth {
991 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
992 set val [format { max %10d} [lindex $x 2]]
993 puts "Parser stack depth: $val"
994 }
drhf7141992008-06-19 00:16:08 +0000995}
996
drh348784e2000-05-29 20:41:49 +0000997# A procedure to execute SQL
998#
drhc4a3c772001-04-04 11:48:57 +0000999proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +00001000 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +00001001 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +00001002}
drh3aadb2e2000-05-31 17:59:25 +00001003
drhadbca9c2001-09-27 15:11:53 +00001004# Execute SQL and catch exceptions.
1005#
1006proc catchsql {sql {db db}} {
1007 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +00001008 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +00001009 lappend r $msg
1010 return $r
1011}
1012
drh04096482001-11-09 22:41:44 +00001013# Do an VDBE code dump on the SQL given
1014#
1015proc explain {sql {db db}} {
1016 puts ""
danielk1977287fb612008-01-04 19:10:28 +00001017 puts "addr opcode p1 p2 p3 p4 p5 #"
1018 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +00001019 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +00001020 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
1021 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
1022 ]
drh04096482001-11-09 22:41:44 +00001023 }
1024}
1025
drhdafc0ce2008-04-17 19:14:02 +00001026# Show the VDBE program for an SQL statement but omit the Trace
1027# opcode at the beginning. This procedure can be used to prove
1028# that different SQL statements generate exactly the same VDBE code.
1029#
1030proc explain_no_trace {sql} {
1031 set tr [db eval "EXPLAIN $sql"]
1032 return [lrange $tr 7 end]
1033}
1034
drh3aadb2e2000-05-31 17:59:25 +00001035# Another procedure to execute SQL. This one includes the field
1036# names in the returned list.
1037#
1038proc execsql2 {sql} {
1039 set result {}
1040 db eval $sql data {
1041 foreach f $data(*) {
1042 lappend result $f $data($f)
1043 }
1044 }
1045 return $result
1046}
drh17a68932001-01-31 13:28:08 +00001047
mistachkin1d406e02013-08-29 01:09:14 +00001048# Use a temporary in-memory database to execute SQL statements
1049#
1050proc memdbsql {sql} {
1051 sqlite3 memdb :memory:
1052 set result [memdb eval $sql]
1053 memdb close
1054 return $result
1055}
1056
drhdde85d92003-03-01 19:45:34 +00001057# Use the non-callback API to execute multiple SQL statements
1058#
1059proc stepsql {dbptr sql} {
1060 set sql [string trim $sql]
1061 set r 0
1062 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +00001063 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +00001064 return [list 1 $vm]
1065 }
1066 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +00001067# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
1068# foreach v $VAL {lappend r $v}
1069# }
1070 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
1071 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
1072 lappend r [sqlite3_column_text $vm $i]
1073 }
drhdde85d92003-03-01 19:45:34 +00001074 }
danielk1977106bb232004-05-21 10:08:53 +00001075 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +00001076 return [list 1 $errmsg]
1077 }
1078 }
1079 return $r
1080}
1081
drh21504322002-06-25 13:16:02 +00001082# Do an integrity check of the entire database
1083#
danielk197704103022009-02-03 16:51:24 +00001084proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +00001085 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +00001086 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +00001087 }
1088}
1089
drh433dccf2013-02-09 15:37:11 +00001090# Check the extended error code
1091#
1092proc verify_ex_errcode {name expected {db db}} {
1093 do_test $name [list sqlite3_extended_errcode $db] $expected
1094}
1095
danc6055c72011-04-28 18:46:46 +00001096
1097# Return true if the SQL statement passed as the second argument uses a
1098# statement transaction.
1099#
1100proc sql_uses_stmt {db sql} {
1101 set stmt [sqlite3_prepare $db $sql -1 dummy]
1102 set uses [uses_stmt_journal $stmt]
1103 sqlite3_finalize $stmt
1104 return $uses
1105}
1106
danielk1977db4e8862008-01-16 08:24:46 +00001107proc fix_ifcapable_expr {expr} {
1108 set ret ""
1109 set state 0
1110 for {set i 0} {$i < [string length $expr]} {incr i} {
1111 set char [string range $expr $i $i]
1112 set newstate [expr {[string is alnum $char] || $char eq "_"}]
1113 if {$newstate && !$state} {
1114 append ret {$::sqlite_options(}
1115 }
1116 if {!$newstate && $state} {
1117 append ret )
1118 }
1119 append ret $char
1120 set state $newstate
1121 }
1122 if {$state} {append ret )}
1123 return $ret
1124}
1125
mistachkinc60941f2012-09-13 01:51:02 +00001126# Returns non-zero if the capabilities are present; zero otherwise.
1127#
1128proc capable {expr} {
1129 set e [fix_ifcapable_expr $expr]; return [expr ($e)]
1130}
1131
drh27d258a2004-10-30 20:23:09 +00001132# Evaluate a boolean expression of capabilities. If true, execute the
1133# code. Omit the code if false.
1134#
danielk19773e8c37e2005-01-21 03:12:14 +00001135proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +00001136 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
1137 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +00001138 if ($e2) {
1139 set c [catch {uplevel 1 $code} r]
1140 } else {
1141 set c [catch {uplevel 1 $elsecode} r]
1142 }
1143 return -code $c $r
drh21504322002-06-25 13:16:02 +00001144}
danielk197745901d62004-11-10 15:27:38 +00001145
danielk1977aca790a2005-01-13 11:07:52 +00001146# This proc execs a seperate process that crashes midway through executing
1147# the SQL script $sql on database test.db.
1148#
1149# The crash occurs during a sync() of file $crashfile. When the crash
1150# occurs a random subset of all unsynced writes made by the process are
1151# written into the files on disk. Argument $crashdelay indicates the
1152# number of file syncs to wait before crashing.
1153#
1154# The return value is a list of two elements. The first element is a
1155# boolean, indicating whether or not the process actually crashed or
1156# reported some other error. The second element in the returned list is the
1157# error message. This is "child process exited abnormally" if the crash
mistachkin48864df2013-03-21 21:20:32 +00001158# occurred.
danielk1977aca790a2005-01-13 11:07:52 +00001159#
danielk1977f8940ae2007-08-23 11:07:10 +00001160# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +00001161#
1162proc crashsql {args} {
danielk197759a33f92007-03-17 10:26:59 +00001163
1164 set blocksize ""
1165 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +00001166 set prngseed 0
drhf8587402008-01-08 16:03:49 +00001167 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +00001168 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +00001169 set dc ""
danielk197759a33f92007-03-17 10:26:59 +00001170 set sql [lindex $args end]
mistachkin1d406e02013-08-29 01:09:14 +00001171
danielk197759a33f92007-03-17 10:26:59 +00001172 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
1173 set z [lindex $args $ii]
1174 set n [string length $z]
1175 set z2 [lindex $args [expr $ii+1]]
1176
1177 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +00001178 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +00001179 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +00001180 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +00001181 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +00001182 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +00001183 else { error "Unrecognized option: $z" }
1184 }
1185
1186 if {$crashfile eq ""} {
1187 error "Compulsory option -file missing"
1188 }
1189
mistachkin1d406e02013-08-29 01:09:14 +00001190 # $crashfile gets compared to the native filename in
shanehf2c08822010-07-08 16:30:44 +00001191 # cfSync(), which can be different then what TCL uses by
1192 # default, so here we force it to the "nativename" format.
mistachkinf8a78462012-03-08 20:00:36 +00001193 set cfile [string map {\\ \\\\} [file nativename [file join [get_pwd] $crashfile]]]
danielk1977aca790a2005-01-13 11:07:52 +00001194
1195 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +00001196 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +00001197 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +00001198 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +00001199 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +00001200
1201 # This block sets the cache size of the main database to 10
1202 # pages. This is done in case the build is configured to omit
1203 # "PRAGMA cache_size".
1204 puts $f {db eval {SELECT * FROM sqlite_master;}}
1205 puts $f {set bt [btree_from_db db]}
1206 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +00001207 if {$prngseed} {
1208 set seed [expr {$prngseed%10007+1}]
1209 # puts seed=$seed
1210 puts $f "db eval {SELECT randomblob($seed)}"
1211 }
danielk1977933bbd62007-03-17 07:22:42 +00001212
drhf8587402008-01-08 16:03:49 +00001213 if {[string length $tclbody]>0} {
1214 puts $f $tclbody
1215 }
1216 if {[string length $sql]>0} {
1217 puts $f "db eval {"
1218 puts $f "$sql"
1219 puts $f "}"
1220 }
danielk1977aca790a2005-01-13 11:07:52 +00001221 close $f
danielk1977aca790a2005-01-13 11:07:52 +00001222 set r [catch {
drh9c06c952005-11-26 00:25:00 +00001223 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +00001224 } msg]
mistachkin1d406e02013-08-29 01:09:14 +00001225
shanehf2c08822010-07-08 16:30:44 +00001226 # Windows/ActiveState TCL returns a slightly different
1227 # error message. We map that to the expected message
1228 # so that we don't have to change all of the test
1229 # cases.
1230 if {$::tcl_platform(platform)=="windows"} {
1231 if {$msg=="child killed: unknown signal"} {
1232 set msg "child process exited abnormally"
1233 }
1234 }
mistachkin1d406e02013-08-29 01:09:14 +00001235
danielk1977aca790a2005-01-13 11:07:52 +00001236 lappend r $msg
1237}
1238
danb88e24f2013-02-23 18:58:11 +00001239proc run_ioerr_prep {} {
1240 set ::sqlite_io_error_pending 0
1241 catch {db close}
1242 catch {db2 close}
1243 catch {forcedelete test.db}
1244 catch {forcedelete test.db-journal}
1245 catch {forcedelete test2.db}
1246 catch {forcedelete test2.db-journal}
1247 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
1248 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
1249 if {[info exists ::ioerropts(-tclprep)]} {
1250 eval $::ioerropts(-tclprep)
1251 }
1252 if {[info exists ::ioerropts(-sqlprep)]} {
1253 execsql $::ioerropts(-sqlprep)
1254 }
1255 expr 0
1256}
1257
danielk197732554c12005-01-22 03:39:39 +00001258# Usage: do_ioerr_test <test number> <options...>
1259#
1260# This proc is used to implement test cases that check that IO errors
mistachkin1d406e02013-08-29 01:09:14 +00001261# are correctly handled. The first argument, <test number>, is an integer
danielk197732554c12005-01-22 03:39:39 +00001262# used to name the tests executed by this proc. Options are as follows:
1263#
1264# -tclprep TCL script to run to prepare test.
1265# -sqlprep SQL script to run to prepare test.
1266# -tclbody TCL script to run with IO error simulation.
1267# -sqlbody TCL script to run with IO error simulation.
1268# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +00001269# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +00001270# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +00001271# -start Value of 'N' to begin with (default 1)
1272#
1273# -cksum Boolean. If true, test that the database does
1274# not change during the execution of the test case.
1275#
1276proc do_ioerr_test {testname args} {
1277
danielk197732554c12005-01-22 03:39:39 +00001278 set ::ioerropts(-start) 1
1279 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +00001280 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +00001281 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +00001282 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +00001283 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +00001284 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +00001285 array set ::ioerropts $args
1286
danielk197747cd39c2008-05-12 12:41:15 +00001287 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
1288 # a couple of obscure IO errors that do not return them.
1289 set ::ioerropts(-erc) 0
mistachkin1d406e02013-08-29 01:09:14 +00001290
danb88e24f2013-02-23 18:58:11 +00001291 # Create a single TCL script from the TCL and SQL specified
1292 # as the body of the test.
1293 set ::ioerrorbody {}
1294 if {[info exists ::ioerropts(-tclbody)]} {
1295 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
1296 }
1297 if {[info exists ::ioerropts(-sqlbody)]} {
1298 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
1299 }
1300
1301 save_prng_state
1302 if {$::ioerropts(-cksum)} {
1303 run_ioerr_prep
1304 eval $::ioerrorbody
1305 set ::goodcksum [cksum]
1306 }
danielk197747cd39c2008-05-12 12:41:15 +00001307
danielk197732554c12005-01-22 03:39:39 +00001308 set ::go 1
drh93aed5a2008-01-16 17:46:38 +00001309 #reset_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +00001310 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +00001311 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +00001312 incr ::ioerropts(-count) -1
1313 if {$::ioerropts(-count)<0} break
mistachkin1d406e02013-08-29 01:09:14 +00001314
danielk197732554c12005-01-22 03:39:39 +00001315 # Skip this IO error if it was specified with the "-exclude" option.
1316 if {[info exists ::ioerropts(-exclude)]} {
1317 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
1318 }
danielk1977861f7452008-06-05 11:39:11 +00001319 if {$::ioerropts(-restoreprng)} {
1320 restore_prng_state
1321 }
danielk197732554c12005-01-22 03:39:39 +00001322
mistachkin1d406e02013-08-29 01:09:14 +00001323 # Delete the files test.db and test2.db, then execute the TCL and
danielk197732554c12005-01-22 03:39:39 +00001324 # SQL (in that order) to prepare for the test case.
1325 do_test $testname.$n.1 {
danb88e24f2013-02-23 18:58:11 +00001326 run_ioerr_prep
danielk197732554c12005-01-22 03:39:39 +00001327 } {0}
1328
1329 # Read the 'checksum' of the database.
1330 if {$::ioerropts(-cksum)} {
danb88e24f2013-02-23 18:58:11 +00001331 set ::checksum [cksum]
danielk197732554c12005-01-22 03:39:39 +00001332 }
drh93aed5a2008-01-16 17:46:38 +00001333
danielk197732554c12005-01-22 03:39:39 +00001334 # Set the Nth IO error to fail.
1335 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +00001336 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +00001337 set ::sqlite_io_error_pending $n
1338 }] $n
danielk197732554c12005-01-22 03:39:39 +00001339
danb88e24f2013-02-23 18:58:11 +00001340 # Execute the TCL script created for the body of this test. If
mistachkin1d406e02013-08-29 01:09:14 +00001341 # at least N IO operations performed by SQLite as a result of
danb88e24f2013-02-23 18:58:11 +00001342 # the script, the Nth will fail.
danielk197732554c12005-01-22 03:39:39 +00001343 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +00001344 set ::sqlite_io_error_hit 0
1345 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +00001346 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +00001347 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +00001348 set rc [sqlite3_errcode $::DB]
1349 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +00001350 # If we are in extended result code mode, make sure all of the
1351 # IOERRs we get back really do have their extended code values.
1352 # If an extended result code is returned, the sqlite3_errcode
1353 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
1354 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +00001355 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
1356 return $rc
1357 }
1358 } else {
drh5f7b5bf2007-04-19 12:30:54 +00001359 # If we are not in extended result code mode, make sure no
1360 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +00001361 if {[regexp {\+\d} $rc]} {
1362 return $rc
1363 }
1364 }
drh93aed5a2008-01-16 17:46:38 +00001365 # The test repeats as long as $::go is non-zero. $::go starts out
1366 # as 1. When a test runs to completion without hitting an I/O
1367 # error, that means there is no point in continuing with this test
1368 # case so set $::go to zero.
1369 #
1370 if {$::sqlite_io_error_pending>0} {
1371 set ::go 0
1372 set q 0
1373 set ::sqlite_io_error_pending 0
1374 } else {
1375 set q 1
1376 }
1377
drhc9ac5ca2005-11-04 22:03:30 +00001378 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +00001379 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
1380 set r 1
1381 }
danielk1977f2fa8312006-01-24 13:09:33 +00001382 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +00001383
1384 # One of two things must have happened. either
1385 # 1. We never hit the IO error and the SQL returned OK
1386 # 2. An IO error was hit and the SQL failed
1387 #
dand41a29a2010-05-06 15:56:28 +00001388 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +00001389 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +00001390 } {1}
1391
danielk197780daec62008-05-12 10:57:02 +00001392 set ::sqlite_io_error_hit 0
1393 set ::sqlite_io_error_pending 0
1394
mistachkin1d406e02013-08-29 01:09:14 +00001395 # Check that no page references were leaked. There should be
1396 # a single reference if there is still an active transaction,
danielk197752b472a2008-05-05 16:23:55 +00001397 # or zero otherwise.
1398 #
danielk197781620542008-06-07 05:19:37 +00001399 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
mistachkin1d406e02013-08-29 01:09:14 +00001400 # locks are established on database files (i.e. if the error
danielk197781620542008-06-07 05:19:37 +00001401 # occurs while attempting to detect a hot-journal file), then
1402 # there may 0 page references and an active transaction according
1403 # to [sqlite3_get_autocommit].
1404 #
danielk19774abd5442008-05-05 15:26:50 +00001405 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +00001406 do_test $testname.$n.4 {
1407 set bt [btree_from_db db]
1408 db_enter db
1409 array set stats [btree_pager_stats $bt]
1410 db_leave db
danielk197781620542008-06-07 05:19:37 +00001411 set nRef $stats(ref)
1412 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
1413 } {1}
danielk19774abd5442008-05-05 15:26:50 +00001414 }
1415
mistachkin1d406e02013-08-29 01:09:14 +00001416 # If there is an open database handle and no open transaction,
danielk197752b472a2008-05-05 16:23:55 +00001417 # and the pager is not running in exclusive-locking mode,
1418 # check that the pager is in "unlocked" state. Theoretically,
1419 # if a call to xUnlock() failed due to an IO error the underlying
1420 # file may still be locked.
1421 #
1422 ifcapable pragma {
1423 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +00001424 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +00001425 && [db one {pragma locking_mode}] eq "normal"
1426 && [sqlite3_get_autocommit db]
1427 } {
1428 do_test $testname.$n.5 {
1429 set bt [btree_from_db db]
1430 db_enter db
1431 array set stats [btree_pager_stats $bt]
1432 db_leave db
1433 set stats(state)
1434 } 0
1435 }
1436 }
1437
mistachkin48864df2013-03-21 21:20:32 +00001438 # If an IO error occurred, then the checksum of the database should
danielk197732554c12005-01-22 03:39:39 +00001439 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +00001440 #
drh1aa5af12008-03-07 19:51:14 +00001441 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +00001442 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +00001443 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +00001444 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +00001445 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danb88e24f2013-02-23 18:58:11 +00001446 set nowcksum [cksum]
1447 set res [expr {$nowcksum==$::checksum || $nowcksum==$::goodcksum}]
1448 if {$res==0} {
1449 puts "now=$nowcksum"
1450 puts "the=$::checksum"
1451 puts "fwd=$::goodcksum"
1452 }
1453 set res
1454 } 1
danielk197732554c12005-01-22 03:39:39 +00001455 }
1456
drh1aa5af12008-03-07 19:51:14 +00001457 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +00001458 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +00001459 if {[info exists ::ioerropts(-cleanup)]} {
1460 catch $::ioerropts(-cleanup)
1461 }
danielk197732554c12005-01-22 03:39:39 +00001462 }
1463 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +00001464 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +00001465 unset ::ioerropts
1466}
1467
drh93aed5a2008-01-16 17:46:38 +00001468# Return a checksum based on the contents of the main database associated
1469# with connection $db
danielk197732554c12005-01-22 03:39:39 +00001470#
1471proc cksum {{db db}} {
1472 set txt [$db eval {
1473 SELECT name, type, sql FROM sqlite_master order by name
1474 }]\n
1475 foreach tbl [$db eval {
1476 SELECT name FROM sqlite_master WHERE type='table' order by name
1477 }] {
1478 append txt [$db eval "SELECT * FROM $tbl"]\n
1479 }
1480 foreach prag {default_synchronous default_cache_size} {
1481 append txt $prag-[$db eval "PRAGMA $prag"]\n
1482 }
1483 set cksum [string length $txt]-[md5 $txt]
1484 # puts $cksum-[file size test.db]
1485 return $cksum
1486}
1487
drh93aed5a2008-01-16 17:46:38 +00001488# Generate a checksum based on the contents of the main and temp tables
1489# database $db. If the checksum of two databases is the same, and the
1490# integrity-check passes for both, the two databases are identical.
1491#
drh1db639c2008-01-17 02:36:28 +00001492proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +00001493 set ret [list]
1494 ifcapable tempdb {
1495 set sql {
1496 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1497 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
1498 SELECT 'sqlite_master' UNION
1499 SELECT 'sqlite_temp_master' ORDER BY 1
1500 }
1501 } else {
1502 set sql {
1503 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1504 SELECT 'sqlite_master' ORDER BY 1
1505 }
1506 }
1507 set tbllist [$db eval $sql]
1508 set txt {}
1509 foreach tbl $tbllist {
1510 append txt [$db eval "SELECT * FROM $tbl"]
1511 }
1512 foreach prag {default_cache_size} {
1513 append txt $prag-[$db eval "PRAGMA $prag"]\n
1514 }
1515 # puts txt=$txt
1516 return [md5 $txt]
1517}
1518
drhdc2c4912009-02-04 22:46:47 +00001519# Generate a checksum based on the contents of a single database with
mistachkin1d406e02013-08-29 01:09:14 +00001520# a database connection. The name of the database is $dbname.
drhdc2c4912009-02-04 22:46:47 +00001521# Examples of $dbname are "temp" or "main".
1522#
1523proc dbcksum {db dbname} {
1524 if {$dbname=="temp"} {
1525 set master sqlite_temp_master
1526 } else {
1527 set master $dbname.sqlite_master
1528 }
1529 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
1530 set txt [$db eval "SELECT * FROM $master"]\n
1531 foreach tab $alltab {
1532 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
1533 }
1534 return [md5 $txt]
1535}
1536
danielk197735754ac2008-03-21 17:29:37 +00001537proc memdebug_log_sql {{filename mallocs.sql}} {
1538
danielk19776f332c12008-03-21 14:22:44 +00001539 set data [sqlite3_memdebug_log dump]
1540 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +00001541 if {$nFrame < 0} { return "" }
1542
danielk197735754ac2008-03-21 17:29:37 +00001543 set database temp
1544
danielk19776ab3a2e2009-02-19 14:39:25 +00001545 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +00001546
1547 set sql ""
1548 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +00001549 set nCall [lindex $e 0]
1550 set nByte [lindex $e 1]
1551 set lStack [lrange $e 2 end]
1552 append sql "INSERT INTO ${database}.malloc VALUES"
1553 append sql "('test', $nCall, $nByte, '$lStack');\n"
1554 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +00001555 set frames($f) 1
1556 }
1557 }
1558
1559 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +00001560 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +00001561
1562 foreach f [array names frames] {
1563 set addr [format %x $f]
1564 set cmd "addr2line -e [info nameofexec] $addr"
1565 set line [eval exec $cmd]
1566 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00001567
1568 set file [lindex [split $line :] 0]
1569 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00001570 }
1571
danielk197735754ac2008-03-21 17:29:37 +00001572 foreach f [array names files] {
1573 set contents ""
1574 catch {
1575 set fd [open $f]
1576 set contents [read $fd]
1577 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001578 }
danielk197735754ac2008-03-21 17:29:37 +00001579 set contents [string map {' ''} $contents]
1580 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00001581 }
danielk19776f332c12008-03-21 14:22:44 +00001582
danielk197735754ac2008-03-21 17:29:37 +00001583 set fd [open $filename w]
1584 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
1585 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001586}
drh93aed5a2008-01-16 17:46:38 +00001587
danf5894502009-10-07 18:41:19 +00001588# Drop all tables in database [db]
1589proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00001590 ifcapable trigger&&foreignkey {
1591 set pk [$db one "PRAGMA foreign_keys"]
1592 $db eval "PRAGMA foreign_keys = OFF"
1593 }
drh9a6ffc82010-02-15 18:03:20 +00001594 foreach {idx name file} [db eval {PRAGMA database_list}] {
1595 if {$idx==1} {
1596 set master sqlite_temp_master
1597 } else {
1598 set master $name.sqlite_master
1599 }
1600 foreach {t type} [$db eval "
1601 SELECT name, type FROM $master
dana16d1062010-09-28 17:37:28 +00001602 WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X'
drh9a6ffc82010-02-15 18:03:20 +00001603 "] {
dana16d1062010-09-28 17:37:28 +00001604 $db eval "DROP $type \"$t\""
drh9a6ffc82010-02-15 18:03:20 +00001605 }
danf5894502009-10-07 18:41:19 +00001606 }
shaneh4e7b32f2009-12-17 22:12:51 +00001607 ifcapable trigger&&foreignkey {
1608 $db eval "PRAGMA foreign_keys = $pk"
1609 }
danf5894502009-10-07 18:41:19 +00001610}
1611
dan71cb5182010-04-26 12:39:03 +00001612#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00001613# If a test script is executed with global variable $::G(perm:name) set to
mistachkin1d406e02013-08-29 01:09:14 +00001614# "wal", then the tests are run in WAL mode. Otherwise, they should be run
1615# in rollback mode. The following Tcl procs are used to make this less
dan430e74c2010-06-07 17:47:26 +00001616# intrusive:
dan71cb5182010-04-26 12:39:03 +00001617#
1618# wal_set_journal_mode ?DB?
1619#
1620# If running a WAL test, execute "PRAGMA journal_mode = wal" using
1621# connection handle DB. Otherwise, this command is a no-op.
1622#
1623# wal_check_journal_mode TESTNAME ?DB?
1624#
1625# If running a WAL test, execute a tests case that fails if the main
1626# database for connection handle DB is not currently a WAL database.
1627# Otherwise (if not running a WAL permutation) this is a no-op.
1628#
1629# wal_is_wal_mode
mistachkin1d406e02013-08-29 01:09:14 +00001630#
dan71cb5182010-04-26 12:39:03 +00001631# Returns true if this test should be run in WAL mode. False otherwise.
mistachkin1d406e02013-08-29 01:09:14 +00001632#
dan71cb5182010-04-26 12:39:03 +00001633proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00001634 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00001635}
1636proc wal_set_journal_mode {{db db}} {
1637 if { [wal_is_wal_mode] } {
1638 $db eval "PRAGMA journal_mode = WAL"
1639 }
1640}
1641proc wal_check_journal_mode {testname {db db}} {
1642 if { [wal_is_wal_mode] } {
1643 $db eval { SELECT * FROM sqlite_master }
1644 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
1645 }
1646}
1647
dan430e74c2010-06-07 17:47:26 +00001648proc permutation {} {
1649 set perm ""
1650 catch {set perm $::G(perm:name)}
1651 set perm
1652}
dancb79e512010-08-06 13:50:07 +00001653proc presql {} {
1654 set presql ""
1655 catch {set presql $::G(perm:presql)}
1656 set presql
1657}
dan430e74c2010-06-07 17:47:26 +00001658
danc1a60c52010-06-07 14:28:16 +00001659#-------------------------------------------------------------------------
1660#
1661proc slave_test_script {script} {
1662
1663 # Create the interpreter used to run the test script.
1664 interp create tinterp
1665
1666 # Populate some global variables that tester.tcl expects to see.
1667 foreach {var value} [list \
1668 ::argv0 $::argv0 \
1669 ::argv {} \
1670 ::SLAVE 1 \
1671 ] {
1672 interp eval tinterp [list set $var $value]
1673 }
1674
1675 # The alias used to access the global test counters.
1676 tinterp alias set_test_counter set_test_counter
1677
1678 # Set up the ::cmdlinearg array in the slave.
1679 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
1680
dan430e74c2010-06-07 17:47:26 +00001681 # Set up the ::G array in the slave.
1682 interp eval tinterp [list array set ::G [array get ::G]]
1683
danc1a60c52010-06-07 14:28:16 +00001684 # Load the various test interfaces implemented in C.
1685 load_testfixture_extensions tinterp
1686
1687 # Run the test script.
1688 interp eval tinterp $script
1689
danea5542d2010-07-06 11:26:15 +00001690 # Check if the interpreter call [run_thread_tests]
1691 if { [interp eval tinterp {info exists ::run_thread_tests_called}] } {
1692 set ::run_thread_tests_called 1
1693 }
1694
danc1a60c52010-06-07 14:28:16 +00001695 # Delete the interpreter used to run the test script.
1696 interp delete tinterp
1697}
1698
dan430e74c2010-06-07 17:47:26 +00001699proc slave_test_file {zFile} {
dan0626dfc2010-06-15 06:56:37 +00001700 set tail [file tail $zFile]
dan430e74c2010-06-07 17:47:26 +00001701
dan6a64d672011-04-04 15:38:16 +00001702 if {[info exists ::G(start:permutation)]} {
1703 if {[permutation] != $::G(start:permutation)} return
1704 unset ::G(start:permutation)
1705 }
1706 if {[info exists ::G(start:file)]} {
1707 if {$tail != $::G(start:file) && $tail!="$::G(start:file).test"} return
1708 unset ::G(start:file)
1709 }
1710
dan92d516a2010-07-05 14:54:48 +00001711 # Remember the value of the shared-cache setting. So that it is possible
1712 # to check afterwards that it was not modified by the test script.
1713 #
1714 ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] }
dan0626dfc2010-06-15 06:56:37 +00001715
dan92d516a2010-07-05 14:54:48 +00001716 # Run the test script in a slave interpreter.
1717 #
danea5542d2010-07-06 11:26:15 +00001718 unset -nocomplain ::run_thread_tests_called
dan0626dfc2010-06-15 06:56:37 +00001719 reset_prng_state
1720 set ::sqlite_open_file_count 0
1721 set time [time { slave_test_script [list source $zFile] }]
1722 set ms [expr [lindex $time 0] / 1000]
1723
dan92d516a2010-07-05 14:54:48 +00001724 # Test that all files opened by the test script were closed. Omit this
1725 # if the test script has "thread" in its name. The open file counter
1726 # is not thread-safe.
dan0626dfc2010-06-15 06:56:37 +00001727 #
danea5542d2010-07-06 11:26:15 +00001728 if {[info exists ::run_thread_tests_called]==0} {
dan92d516a2010-07-05 14:54:48 +00001729 do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0}
1730 }
dan430e74c2010-06-07 17:47:26 +00001731 set ::sqlite_open_file_count 0
1732
mistachkin1d406e02013-08-29 01:09:14 +00001733 # Test that the global "shared-cache" setting was not altered by
dan0626dfc2010-06-15 06:56:37 +00001734 # the test script.
1735 #
mistachkin1d406e02013-08-29 01:09:14 +00001736 ifcapable shared_cache {
dan0626dfc2010-06-15 06:56:37 +00001737 set res [expr {[sqlite3_enable_shared_cache] == $scs}]
1738 do_test ${tail}-sharedcachesetting [list set {} $res] 1
1739 }
dan430e74c2010-06-07 17:47:26 +00001740
dan92d516a2010-07-05 14:54:48 +00001741 # Add some info to the output.
1742 #
dan0626dfc2010-06-15 06:56:37 +00001743 puts "Time: $tail $ms ms"
dan430e74c2010-06-07 17:47:26 +00001744 show_memstats
danc1a60c52010-06-07 14:28:16 +00001745}
1746
dan59257dc2010-08-04 11:34:31 +00001747# Open a new connection on database test.db and execute the SQL script
1748# supplied as an argument. Before returning, close the new conection and
1749# restore the 4 byte fields starting at header offsets 28, 92 and 96
1750# to the values they held before the SQL was executed. This simulates
1751# a write by a pre-3.7.0 client.
1752#
1753proc sql36231 {sql} {
1754 set B [hexio_read test.db 92 8]
1755 set A [hexio_read test.db 28 4]
1756 sqlite3 db36231 test.db
1757 catch { db36231 func a_string a_string }
1758 execsql $sql db36231
1759 db36231 close
1760 hexio_write test.db 28 $A
1761 hexio_write test.db 92 $B
1762 return ""
1763}
danf5894502009-10-07 18:41:19 +00001764
danccc7ff42010-11-29 12:06:45 +00001765proc db_save {} {
1766 foreach f [glob -nocomplain sv_test.db*] { forcedelete $f }
1767 foreach f [glob -nocomplain test.db*] {
1768 set f2 "sv_$f"
mistachkinfda06be2011-08-02 00:57:34 +00001769 forcecopy $f $f2
danccc7ff42010-11-29 12:06:45 +00001770 }
1771}
1772proc db_save_and_close {} {
1773 db_save
1774 catch { db close }
1775 return ""
1776}
1777proc db_restore {} {
1778 foreach f [glob -nocomplain test.db*] { forcedelete $f }
1779 foreach f2 [glob -nocomplain sv_test.db*] {
1780 set f [string range $f2 3 end]
mistachkinfda06be2011-08-02 00:57:34 +00001781 forcecopy $f2 $f
danccc7ff42010-11-29 12:06:45 +00001782 }
1783}
1784proc db_restore_and_reopen {{dbfile test.db}} {
1785 catch { db close }
1786 db_restore
1787 sqlite3 db $dbfile
1788}
1789proc db_delete_and_reopen {{file test.db}} {
1790 catch { db close }
mistachkinfda06be2011-08-02 00:57:34 +00001791 foreach f [glob -nocomplain test.db*] { forcedelete $f }
danccc7ff42010-11-29 12:06:45 +00001792 sqlite3 db $file
1793}
1794
danielk197745901d62004-11-10 15:27:38 +00001795# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
1796# to non-zero, then set the global variable $AUTOVACUUM to 1.
1797set AUTOVACUUM $sqlite_options(default_autovacuum)
danielk1977ee8b7992009-03-26 17:13:06 +00001798
dan64b95bb2012-05-12 05:30:29 +00001799# Make sure the FTS enhanced query syntax is disabled.
1800set sqlite_fts3_enable_parentheses 0
1801
danielk1977ee8b7992009-03-26 17:13:06 +00001802source $testdir/thread_common.tcl
danddf80eb2010-10-25 12:47:43 +00001803source $testdir/malloc_common.tcl