blob: 3d011b918e729b48f5b8377e4deb1e7a83c43dfc [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#
drhdddca282006-01-03 00:33:50 +000014# $Id: tester.tcl,v 1.56 2006/01/03 00:33:50 drh Exp $
drhfbc3eab2001-04-06 16:13:42 +000015
drhef4ac8f2004-06-19 00:16:31 +000016# Make sure tclsqlite3 was compiled correctly. Abort now with an
drhfbc3eab2001-04-06 16:13:42 +000017# error message if not.
18#
drhef4ac8f2004-06-19 00:16:31 +000019if {[sqlite3 -tcl-uses-utf]} {
drhfbc3eab2001-04-06 16:13:42 +000020 if {"\u1234"=="u1234"} {
21 puts stderr "***** BUILD PROBLEM *****"
22 puts stderr "$argv0 was linked against an older version"
23 puts stderr "of TCL that does not support Unicode, but uses a header"
24 puts stderr "file (\"tcl.h\") from a new TCL version that does support"
25 puts stderr "Unicode. This combination causes internal errors."
26 puts stderr "Recompile using a TCL library and header file that match"
27 puts stderr "and try again.\n**************************"
28 exit 1
29 }
30} else {
31 if {"\u1234"!="u1234"} {
32 puts stderr "***** BUILD PROBLEM *****"
33 puts stderr "$argv0 was linked against an newer version"
34 puts stderr "of TCL that supports Unicode, but uses a header file"
35 puts stderr "(\"tcl.h\") from a old TCL version that does not support"
36 puts stderr "Unicode. This combination causes internal errors."
37 puts stderr "Recompile using a TCL library and header file that match"
38 puts stderr "and try again.\n**************************"
39 exit 1
40 }
41}
drh348784e2000-05-29 20:41:49 +000042
drh92febd92004-08-20 18:34:20 +000043set tcl_precision 15
drh49285702005-09-17 15:20:26 +000044set sqlite_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +000045
drh9eb9e262004-02-11 02:18:05 +000046# Use the pager codec if it is available
47#
drhef4ac8f2004-06-19 00:16:31 +000048if {[sqlite3 -has-codec] && [info command sqlite_orig]==""} {
49 rename sqlite3 sqlite_orig
50 proc sqlite3 {args} {
drh9eb9e262004-02-11 02:18:05 +000051 if {[llength $args]==2 && [string index [lindex $args 0] 0]!="-"} {
52 lappend args -key {xyzzy}
53 }
54 uplevel 1 sqlite_orig $args
55 }
56}
57
58
drhbec2bf42000-05-29 23:48:22 +000059# Create a test database
60#
drh254cba22001-09-20 01:44:42 +000061catch {db close}
62file delete -force test.db
63file delete -force test.db-journal
drhdddca282006-01-03 00:33:50 +000064sqlite3 db ./test.db
65set ::DB [sqlite3_connection_pointer db]
drhcd61c282002-03-06 22:01:34 +000066if {[info exists ::SETUP_SQL]} {
67 db eval $::SETUP_SQL
68}
drhbec2bf42000-05-29 23:48:22 +000069
70# Abort early if this script has been run before.
71#
72if {[info exists nTest]} return
73
74# Set the test counters to zero
75#
drh348784e2000-05-29 20:41:49 +000076set nErr 0
77set nTest 0
drhdb25e382001-03-15 18:21:22 +000078set nProb 0
drh767c2002000-10-19 14:10:08 +000079set skip_test 0
drha1b351a2001-09-14 16:42:12 +000080set failList {}
drhd3d39e92004-05-20 22:16:29 +000081set maxErr 1000
drh348784e2000-05-29 20:41:49 +000082
83# Invoke the do_test procedure to run a single test
84#
85proc do_test {name cmd expected} {
drhd3d39e92004-05-20 22:16:29 +000086 global argv nErr nTest skip_test maxErr
danielk19772e588c72005-12-09 14:25:08 +000087 set ::sqlite_malloc_id $name
drh767c2002000-10-19 14:10:08 +000088 if {$skip_test} {
89 set skip_test 0
90 return
91 }
92 if {[llength $argv]==0} {
drh348784e2000-05-29 20:41:49 +000093 set go 1
94 } else {
95 set go 0
96 foreach pattern $argv {
97 if {[string match $pattern $name]} {
98 set go 1
99 break
100 }
101 }
102 }
103 if {!$go} return
104 incr nTest
drh5edc3122001-09-13 21:53:09 +0000105 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000106 flush stdout
107 if {[catch {uplevel #0 "$cmd;\n"} result]} {
108 puts "\nError: $result"
109 incr nErr
drha1b351a2001-09-14 16:42:12 +0000110 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000111 if {$nErr>$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000112 } elseif {[string compare $result $expected]} {
113 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
114 incr nErr
drha1b351a2001-09-14 16:42:12 +0000115 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000116 if {$nErr>=$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000117 } else {
118 puts " Ok"
119 }
120}
121
drhdaffd0e2001-04-11 14:28:42 +0000122# The procedure uses the special "sqlite_malloc_stat" command
drhc89b91b2005-01-03 01:32:59 +0000123# (which is only available if SQLite is compiled with -DSQLITE_DEBUG=1)
drh8c82b352000-12-10 18:23:50 +0000124# to see how many malloc()s have not been free()ed. The number
125# of surplus malloc()s is stored in the global variable $::Leak.
126# If the value in $::Leak grows, it may mean there is a memory leak
127# in the library.
128#
129proc memleak_check {} {
drhdaffd0e2001-04-11 14:28:42 +0000130 if {[info command sqlite_malloc_stat]!=""} {
131 set r [sqlite_malloc_stat]
132 set ::Leak [expr {[lindex $r 0]-[lindex $r 1]}]
133 }
drh8c82b352000-12-10 18:23:50 +0000134}
135
drh348784e2000-05-29 20:41:49 +0000136# Run this routine last
137#
138proc finish_test {} {
drha1b351a2001-09-14 16:42:12 +0000139 finalize_testing
140}
141proc finalize_testing {} {
drh94e92032003-02-16 22:21:32 +0000142 global nTest nErr nProb sqlite_open_file_count
drha1b351a2001-09-14 16:42:12 +0000143 if {$nErr==0} memleak_check
danielk19772e588c72005-12-09 14:25:08 +0000144
drh6e142f52000-06-08 13:36:40 +0000145 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000146 catch {db2 close}
147 catch {db3 close}
148
danielk197797cb2e92005-12-09 14:39:04 +0000149 catch {
150 pp_check_for_leaks
151 }
danielk19772e588c72005-12-09 14:25:08 +0000152
drh348784e2000-05-29 20:41:49 +0000153 puts "$nErr errors out of $nTest tests"
drha1b351a2001-09-14 16:42:12 +0000154 puts "Failures on these tests: $::failList"
drhdb25e382001-03-15 18:21:22 +0000155 if {$nProb>0} {
156 puts "$nProb probabilistic tests also failed, but this does"
157 puts "not necessarily indicate a malfunction."
158 }
drh3aac2dd2004-04-26 14:10:20 +0000159 if 0 {
drh94e92032003-02-16 22:21:32 +0000160 if {$sqlite_open_file_count} {
161 puts "$sqlite_open_file_count files were left open"
162 incr nErr
163 }
drh3aac2dd2004-04-26 14:10:20 +0000164 }
drhdb25e382001-03-15 18:21:22 +0000165 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000166}
167
drh348784e2000-05-29 20:41:49 +0000168# A procedure to execute SQL
169#
drhc4a3c772001-04-04 11:48:57 +0000170proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000171 # puts "SQL = $sql"
drhc4a3c772001-04-04 11:48:57 +0000172 return [$db eval $sql]
drh348784e2000-05-29 20:41:49 +0000173}
drh3aadb2e2000-05-31 17:59:25 +0000174
drhadbca9c2001-09-27 15:11:53 +0000175# Execute SQL and catch exceptions.
176#
177proc catchsql {sql {db db}} {
178 # puts "SQL = $sql"
179 set r [catch {$db eval $sql} msg]
180 lappend r $msg
181 return $r
182}
183
drh04096482001-11-09 22:41:44 +0000184# Do an VDBE code dump on the SQL given
185#
186proc explain {sql {db db}} {
187 puts ""
188 puts "addr opcode p1 p2 p3 "
189 puts "---- ------------ ------ ------ ---------------"
190 $db eval "explain $sql" {} {
191 puts [format {%-4d %-12.12s %-6d %-6d %s} $addr $opcode $p1 $p2 $p3]
192 }
193}
194
drh3aadb2e2000-05-31 17:59:25 +0000195# Another procedure to execute SQL. This one includes the field
196# names in the returned list.
197#
198proc execsql2 {sql} {
199 set result {}
200 db eval $sql data {
201 foreach f $data(*) {
202 lappend result $f $data($f)
203 }
204 }
205 return $result
206}
drh17a68932001-01-31 13:28:08 +0000207
drhdde85d92003-03-01 19:45:34 +0000208# Use the non-callback API to execute multiple SQL statements
209#
210proc stepsql {dbptr sql} {
211 set sql [string trim $sql]
212 set r 0
213 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000214 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000215 return [list 1 $vm]
216 }
217 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000218# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
219# foreach v $VAL {lappend r $v}
220# }
221 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
222 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
223 lappend r [sqlite3_column_text $vm $i]
224 }
drhdde85d92003-03-01 19:45:34 +0000225 }
danielk1977106bb232004-05-21 10:08:53 +0000226 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000227 return [list 1 $errmsg]
228 }
229 }
230 return $r
231}
232
drh17a68932001-01-31 13:28:08 +0000233# Delete a file or directory
234#
235proc forcedelete {filename} {
236 if {[catch {file delete -force $filename}]} {
237 exec rm -rf $filename
238 }
239}
drh21504322002-06-25 13:16:02 +0000240
241# Do an integrity check of the entire database
242#
243proc integrity_check {name} {
drh27d258a2004-10-30 20:23:09 +0000244 ifcapable integrityck {
245 do_test $name {
246 execsql {PRAGMA integrity_check}
247 } {ok}
248 }
249}
250
251# Evaluate a boolean expression of capabilities. If true, execute the
252# code. Omit the code if false.
253#
danielk19773e8c37e2005-01-21 03:12:14 +0000254proc ifcapable {expr code {else ""} {elsecode ""}} {
drh5436dc22004-11-14 04:04:17 +0000255 regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
danielk19773e8c37e2005-01-21 03:12:14 +0000256 if ($e2) {
257 set c [catch {uplevel 1 $code} r]
258 } else {
259 set c [catch {uplevel 1 $elsecode} r]
260 }
261 return -code $c $r
drh21504322002-06-25 13:16:02 +0000262}
danielk197745901d62004-11-10 15:27:38 +0000263
danielk1977aca790a2005-01-13 11:07:52 +0000264# This proc execs a seperate process that crashes midway through executing
265# the SQL script $sql on database test.db.
266#
267# The crash occurs during a sync() of file $crashfile. When the crash
268# occurs a random subset of all unsynced writes made by the process are
269# written into the files on disk. Argument $crashdelay indicates the
270# number of file syncs to wait before crashing.
271#
272# The return value is a list of two elements. The first element is a
273# boolean, indicating whether or not the process actually crashed or
274# reported some other error. The second element in the returned list is the
275# error message. This is "child process exited abnormally" if the crash
276# occured.
277#
278proc crashsql {crashdelay crashfile sql} {
279 if {$::tcl_platform(platform)!="unix"} {
280 error "crashsql should only be used on unix"
281 }
282 set cfile [file join [pwd] $crashfile]
283
284 set f [open crash.tcl w]
285 puts $f "sqlite3_crashparams $crashdelay $cfile"
286 puts $f "sqlite3 db test.db"
287 puts $f "db eval {pragma cache_size = 10}"
288 puts $f "db eval {"
289 puts $f "$sql"
290 puts $f "}"
291 close $f
292
293 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000294 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000295 } msg]
296 lappend r $msg
297}
298
danielk197732554c12005-01-22 03:39:39 +0000299# Usage: do_ioerr_test <test number> <options...>
300#
301# This proc is used to implement test cases that check that IO errors
302# are correctly handled. The first argument, <test number>, is an integer
303# used to name the tests executed by this proc. Options are as follows:
304#
305# -tclprep TCL script to run to prepare test.
306# -sqlprep SQL script to run to prepare test.
307# -tclbody TCL script to run with IO error simulation.
308# -sqlbody TCL script to run with IO error simulation.
309# -exclude List of 'N' values not to test.
310# -start Value of 'N' to begin with (default 1)
311#
312# -cksum Boolean. If true, test that the database does
313# not change during the execution of the test case.
314#
315proc do_ioerr_test {testname args} {
316
danielk197732554c12005-01-22 03:39:39 +0000317 set ::ioerropts(-start) 1
318 set ::ioerropts(-cksum) 0
319
320 array set ::ioerropts $args
321
322 set ::go 1
323 for {set n $::ioerropts(-start)} {$::go} {incr n} {
324
325 # Skip this IO error if it was specified with the "-exclude" option.
326 if {[info exists ::ioerropts(-exclude)]} {
327 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
328 }
329
330 # Delete the files test.db and test2.db, then execute the TCL and
331 # SQL (in that order) to prepare for the test case.
332 do_test $testname.$n.1 {
333 set ::sqlite_io_error_pending 0
334 catch {db close}
335 catch {file delete -force test.db}
336 catch {file delete -force test.db-journal}
337 catch {file delete -force test2.db}
338 catch {file delete -force test2.db-journal}
339 set ::DB [sqlite3 db test.db]
340 if {[info exists ::ioerropts(-tclprep)]} {
341 eval $::ioerropts(-tclprep)
342 }
343 if {[info exists ::ioerropts(-sqlprep)]} {
344 execsql $::ioerropts(-sqlprep)
345 }
346 expr 0
347 } {0}
348
349 # Read the 'checksum' of the database.
350 if {$::ioerropts(-cksum)} {
351 set checksum [cksum]
352 }
353
354 # Set the Nth IO error to fail.
355 do_test $testname.$n.2 [subst {
356 set ::sqlite_io_error_pending $n
357 }] $n
358
359 # Create a single TCL script from the TCL and SQL specified
360 # as the body of the test.
361 set ::ioerrorbody {}
362 if {[info exists ::ioerropts(-tclbody)]} {
363 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
364 }
365 if {[info exists ::ioerropts(-sqlbody)]} {
366 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
367 }
368
369 # Execute the TCL Script created in the above block. If
370 # there are at least N IO operations performed by SQLite as
371 # a result of the script, the Nth will fail.
372 do_test $testname.$n.3 {
373 set r [catch $::ioerrorbody msg]
374 set ::go [expr {$::sqlite_io_error_pending<=0}]
drhc9ac5ca2005-11-04 22:03:30 +0000375 set s [expr $::sqlite_io_error_hit==0]
danielk197732554c12005-01-22 03:39:39 +0000376 # puts "$::sqlite_io_error_pending $r $msg"
377 expr { ($s && !$r) || (!$s && $r) }
378 # expr {$::sqlite_io_error_pending>0 || $r!=0}
379 } {1}
380
381 # If an IO error occured, then the checksum of the database should
382 # be the same as before the script that caused the IO error was run.
383 if {$::go && $::ioerropts(-cksum)} {
384 do_test $testname.$n.4 {
385 catch {db close}
386 set ::DB [sqlite3 db test.db]
387 cksum
388 } $checksum
389 }
390
danielk1977105afed2005-05-26 15:20:53 +0000391 if {[info exists ::ioerropts(-cleanup)]} {
392 catch $::ioerropts(-cleanup)
393 }
danielk197732554c12005-01-22 03:39:39 +0000394 }
395 set ::sqlite_io_error_pending 0
396 unset ::ioerropts
397}
398
399# Return a checksum based on the contents of database 'db'.
400#
401proc cksum {{db db}} {
402 set txt [$db eval {
403 SELECT name, type, sql FROM sqlite_master order by name
404 }]\n
405 foreach tbl [$db eval {
406 SELECT name FROM sqlite_master WHERE type='table' order by name
407 }] {
408 append txt [$db eval "SELECT * FROM $tbl"]\n
409 }
410 foreach prag {default_synchronous default_cache_size} {
411 append txt $prag-[$db eval "PRAGMA $prag"]\n
412 }
413 set cksum [string length $txt]-[md5 $txt]
414 # puts $cksum-[file size test.db]
415 return $cksum
416}
417
418# Copy file $from into $to. This is used because some versions of
419# TCL for windows (notably the 8.4.1 binary package shipped with the
420# current mingw release) have a broken "file copy" command.
421#
422proc copy_file {from to} {
423 if {$::tcl_platform(platform)=="unix"} {
424 file copy -force $from $to
425 } else {
426 set f [open $from]
427 fconfigure $f -translation binary
428 set t [open $to w]
429 fconfigure $t -translation binary
430 puts -nonewline $t [read $f [file size $from]]
431 close $t
432 close $f
433 }
434}
435
danielk19772e588c72005-12-09 14:25:08 +0000436# This command checks for outstanding calls to sqliteMalloc() from within
437# the current thread. A list is returned with one entry for each outstanding
438# malloc. Each list entry is itself a list of 5 items, as follows:
439#
440# { <number-bytes> <file-name> <line-number> <test-case> <stack-dump> }
441#
442proc check_for_leaks {} {
443 set ret [list]
danielk1977aef0bf62005-12-30 16:28:01 +0000444 set cnt 0
danielk19772e588c72005-12-09 14:25:08 +0000445 foreach alloc [sqlite_malloc_outstanding] {
446 foreach {nBytes file iLine userstring backtrace} $alloc {}
447 set stack [list]
448 set skip 0
449
450 # The first command in this block will probably fail on windows. This
451 # means there will be no stack dump available.
danielk1977aef0bf62005-12-30 16:28:01 +0000452 if {$cnt < 25} {
453 catch {
454 set stuff [eval "exec addr2line -e ./testfixture -f $backtrace"]
455 foreach {func line} $stuff {
456 if {$func != "??" || $line != "??:0"} {
457 regexp {.*/(.*)} $line dummy line
458 lappend stack "${func}() $line"
459 } else {
460 if {[lindex $stack end] != "..."} {
461 lappend stack "..."
462 }
danielk19772e588c72005-12-09 14:25:08 +0000463 }
464 }
465 }
danielk1977aef0bf62005-12-30 16:28:01 +0000466 incr cnt
danielk19772e588c72005-12-09 14:25:08 +0000467 }
468
469 if {!$skip} {
470 lappend ret [list $nBytes $file $iLine $userstring $stack]
471 }
472 }
473 return $ret
474}
475
476# Pretty print a report based on the return value of [check_for_leaks] to
477# stdout.
478proc pp_check_for_leaks {} {
479 set l [check_for_leaks]
480 set n 0
481 foreach leak $l {
482 foreach {nBytes file iLine userstring stack} $leak {}
483 puts "$nBytes bytes leaked at $file:$iLine ($userstring)"
484 foreach frame $stack {
485 puts " $frame"
486 }
487 incr n $nBytes
488 }
489 puts "Memory leaked: $n bytes in [llength $l] allocations"
490 puts ""
491}
492
danielk197745901d62004-11-10 15:27:38 +0000493# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
494# to non-zero, then set the global variable $AUTOVACUUM to 1.
495set AUTOVACUUM $sqlite_options(default_autovacuum)