blob: 8cc925cd198983c6d69b209e1faf1caec8cff198 [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#
danielk19772e588c72005-12-09 14:25:08 +000014# $Id: tester.tcl,v 1.53 2005/12/09 14:25:12 danielk1977 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
danielk1977a21c6b62005-01-24 10:25:59 +000064set ::DB [sqlite3 db ./test.db]
drhcd61c282002-03-06 22:01:34 +000065if {[info exists ::SETUP_SQL]} {
66 db eval $::SETUP_SQL
67}
drhbec2bf42000-05-29 23:48:22 +000068
69# Abort early if this script has been run before.
70#
71if {[info exists nTest]} return
72
73# Set the test counters to zero
74#
drh348784e2000-05-29 20:41:49 +000075set nErr 0
76set nTest 0
drhdb25e382001-03-15 18:21:22 +000077set nProb 0
drh767c2002000-10-19 14:10:08 +000078set skip_test 0
drha1b351a2001-09-14 16:42:12 +000079set failList {}
drhd3d39e92004-05-20 22:16:29 +000080set maxErr 1000
drh348784e2000-05-29 20:41:49 +000081
82# Invoke the do_test procedure to run a single test
83#
84proc do_test {name cmd expected} {
drhd3d39e92004-05-20 22:16:29 +000085 global argv nErr nTest skip_test maxErr
danielk19772e588c72005-12-09 14:25:08 +000086 set ::sqlite_malloc_id $name
drh767c2002000-10-19 14:10:08 +000087 if {$skip_test} {
88 set skip_test 0
89 return
90 }
91 if {[llength $argv]==0} {
drh348784e2000-05-29 20:41:49 +000092 set go 1
93 } else {
94 set go 0
95 foreach pattern $argv {
96 if {[string match $pattern $name]} {
97 set go 1
98 break
99 }
100 }
101 }
102 if {!$go} return
103 incr nTest
drh5edc3122001-09-13 21:53:09 +0000104 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000105 flush stdout
106 if {[catch {uplevel #0 "$cmd;\n"} result]} {
107 puts "\nError: $result"
108 incr nErr
drha1b351a2001-09-14 16:42:12 +0000109 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000110 if {$nErr>$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000111 } elseif {[string compare $result $expected]} {
112 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
113 incr nErr
drha1b351a2001-09-14 16:42:12 +0000114 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000115 if {$nErr>=$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000116 } else {
117 puts " Ok"
118 }
119}
120
drhdaffd0e2001-04-11 14:28:42 +0000121# The procedure uses the special "sqlite_malloc_stat" command
drhc89b91b2005-01-03 01:32:59 +0000122# (which is only available if SQLite is compiled with -DSQLITE_DEBUG=1)
drh8c82b352000-12-10 18:23:50 +0000123# to see how many malloc()s have not been free()ed. The number
124# of surplus malloc()s is stored in the global variable $::Leak.
125# If the value in $::Leak grows, it may mean there is a memory leak
126# in the library.
127#
128proc memleak_check {} {
drhdaffd0e2001-04-11 14:28:42 +0000129 if {[info command sqlite_malloc_stat]!=""} {
130 set r [sqlite_malloc_stat]
131 set ::Leak [expr {[lindex $r 0]-[lindex $r 1]}]
132 }
drh8c82b352000-12-10 18:23:50 +0000133}
134
drh348784e2000-05-29 20:41:49 +0000135# Run this routine last
136#
137proc finish_test {} {
drha1b351a2001-09-14 16:42:12 +0000138 finalize_testing
139}
140proc finalize_testing {} {
drh94e92032003-02-16 22:21:32 +0000141 global nTest nErr nProb sqlite_open_file_count
drha1b351a2001-09-14 16:42:12 +0000142 if {$nErr==0} memleak_check
danielk19772e588c72005-12-09 14:25:08 +0000143
drh6e142f52000-06-08 13:36:40 +0000144 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000145 catch {db2 close}
146 catch {db3 close}
147
148pp_check_for_leaks
149
drh348784e2000-05-29 20:41:49 +0000150 puts "$nErr errors out of $nTest tests"
drha1b351a2001-09-14 16:42:12 +0000151 puts "Failures on these tests: $::failList"
drhdb25e382001-03-15 18:21:22 +0000152 if {$nProb>0} {
153 puts "$nProb probabilistic tests also failed, but this does"
154 puts "not necessarily indicate a malfunction."
155 }
drh3aac2dd2004-04-26 14:10:20 +0000156 if 0 {
drh94e92032003-02-16 22:21:32 +0000157 if {$sqlite_open_file_count} {
158 puts "$sqlite_open_file_count files were left open"
159 incr nErr
160 }
drh3aac2dd2004-04-26 14:10:20 +0000161 }
drhdb25e382001-03-15 18:21:22 +0000162 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000163}
164
drh348784e2000-05-29 20:41:49 +0000165# A procedure to execute SQL
166#
drhc4a3c772001-04-04 11:48:57 +0000167proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000168 # puts "SQL = $sql"
drhc4a3c772001-04-04 11:48:57 +0000169 return [$db eval $sql]
drh348784e2000-05-29 20:41:49 +0000170}
drh3aadb2e2000-05-31 17:59:25 +0000171
drhadbca9c2001-09-27 15:11:53 +0000172# Execute SQL and catch exceptions.
173#
174proc catchsql {sql {db db}} {
175 # puts "SQL = $sql"
176 set r [catch {$db eval $sql} msg]
177 lappend r $msg
178 return $r
179}
180
drh04096482001-11-09 22:41:44 +0000181# Do an VDBE code dump on the SQL given
182#
183proc explain {sql {db db}} {
184 puts ""
185 puts "addr opcode p1 p2 p3 "
186 puts "---- ------------ ------ ------ ---------------"
187 $db eval "explain $sql" {} {
188 puts [format {%-4d %-12.12s %-6d %-6d %s} $addr $opcode $p1 $p2 $p3]
189 }
190}
191
drh3aadb2e2000-05-31 17:59:25 +0000192# Another procedure to execute SQL. This one includes the field
193# names in the returned list.
194#
195proc execsql2 {sql} {
196 set result {}
197 db eval $sql data {
198 foreach f $data(*) {
199 lappend result $f $data($f)
200 }
201 }
202 return $result
203}
drh17a68932001-01-31 13:28:08 +0000204
drhdde85d92003-03-01 19:45:34 +0000205# Use the non-callback API to execute multiple SQL statements
206#
207proc stepsql {dbptr sql} {
208 set sql [string trim $sql]
209 set r 0
210 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000211 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000212 return [list 1 $vm]
213 }
214 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000215# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
216# foreach v $VAL {lappend r $v}
217# }
218 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
219 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
220 lappend r [sqlite3_column_text $vm $i]
221 }
drhdde85d92003-03-01 19:45:34 +0000222 }
danielk1977106bb232004-05-21 10:08:53 +0000223 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000224 return [list 1 $errmsg]
225 }
226 }
227 return $r
228}
229
drh17a68932001-01-31 13:28:08 +0000230# Delete a file or directory
231#
232proc forcedelete {filename} {
233 if {[catch {file delete -force $filename}]} {
234 exec rm -rf $filename
235 }
236}
drh21504322002-06-25 13:16:02 +0000237
238# Do an integrity check of the entire database
239#
240proc integrity_check {name} {
drh27d258a2004-10-30 20:23:09 +0000241 ifcapable integrityck {
242 do_test $name {
243 execsql {PRAGMA integrity_check}
244 } {ok}
245 }
246}
247
248# Evaluate a boolean expression of capabilities. If true, execute the
249# code. Omit the code if false.
250#
danielk19773e8c37e2005-01-21 03:12:14 +0000251proc ifcapable {expr code {else ""} {elsecode ""}} {
drh5436dc22004-11-14 04:04:17 +0000252 regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
danielk19773e8c37e2005-01-21 03:12:14 +0000253 if ($e2) {
254 set c [catch {uplevel 1 $code} r]
255 } else {
256 set c [catch {uplevel 1 $elsecode} r]
257 }
258 return -code $c $r
drh21504322002-06-25 13:16:02 +0000259}
danielk197745901d62004-11-10 15:27:38 +0000260
danielk1977aca790a2005-01-13 11:07:52 +0000261# This proc execs a seperate process that crashes midway through executing
262# the SQL script $sql on database test.db.
263#
264# The crash occurs during a sync() of file $crashfile. When the crash
265# occurs a random subset of all unsynced writes made by the process are
266# written into the files on disk. Argument $crashdelay indicates the
267# number of file syncs to wait before crashing.
268#
269# The return value is a list of two elements. The first element is a
270# boolean, indicating whether or not the process actually crashed or
271# reported some other error. The second element in the returned list is the
272# error message. This is "child process exited abnormally" if the crash
273# occured.
274#
275proc crashsql {crashdelay crashfile sql} {
276 if {$::tcl_platform(platform)!="unix"} {
277 error "crashsql should only be used on unix"
278 }
279 set cfile [file join [pwd] $crashfile]
280
281 set f [open crash.tcl w]
282 puts $f "sqlite3_crashparams $crashdelay $cfile"
283 puts $f "sqlite3 db test.db"
284 puts $f "db eval {pragma cache_size = 10}"
285 puts $f "db eval {"
286 puts $f "$sql"
287 puts $f "}"
288 close $f
289
290 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000291 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000292 } msg]
293 lappend r $msg
294}
295
danielk197732554c12005-01-22 03:39:39 +0000296# Usage: do_ioerr_test <test number> <options...>
297#
298# This proc is used to implement test cases that check that IO errors
299# are correctly handled. The first argument, <test number>, is an integer
300# used to name the tests executed by this proc. Options are as follows:
301#
302# -tclprep TCL script to run to prepare test.
303# -sqlprep SQL script to run to prepare test.
304# -tclbody TCL script to run with IO error simulation.
305# -sqlbody TCL script to run with IO error simulation.
306# -exclude List of 'N' values not to test.
307# -start Value of 'N' to begin with (default 1)
308#
309# -cksum Boolean. If true, test that the database does
310# not change during the execution of the test case.
311#
312proc do_ioerr_test {testname args} {
313
danielk197732554c12005-01-22 03:39:39 +0000314 set ::ioerropts(-start) 1
315 set ::ioerropts(-cksum) 0
316
317 array set ::ioerropts $args
318
319 set ::go 1
320 for {set n $::ioerropts(-start)} {$::go} {incr n} {
321
322 # Skip this IO error if it was specified with the "-exclude" option.
323 if {[info exists ::ioerropts(-exclude)]} {
324 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
325 }
326
327 # Delete the files test.db and test2.db, then execute the TCL and
328 # SQL (in that order) to prepare for the test case.
329 do_test $testname.$n.1 {
330 set ::sqlite_io_error_pending 0
331 catch {db close}
332 catch {file delete -force test.db}
333 catch {file delete -force test.db-journal}
334 catch {file delete -force test2.db}
335 catch {file delete -force test2.db-journal}
336 set ::DB [sqlite3 db test.db]
337 if {[info exists ::ioerropts(-tclprep)]} {
338 eval $::ioerropts(-tclprep)
339 }
340 if {[info exists ::ioerropts(-sqlprep)]} {
341 execsql $::ioerropts(-sqlprep)
342 }
343 expr 0
344 } {0}
345
346 # Read the 'checksum' of the database.
347 if {$::ioerropts(-cksum)} {
348 set checksum [cksum]
349 }
350
351 # Set the Nth IO error to fail.
352 do_test $testname.$n.2 [subst {
353 set ::sqlite_io_error_pending $n
354 }] $n
355
356 # Create a single TCL script from the TCL and SQL specified
357 # as the body of the test.
358 set ::ioerrorbody {}
359 if {[info exists ::ioerropts(-tclbody)]} {
360 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
361 }
362 if {[info exists ::ioerropts(-sqlbody)]} {
363 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
364 }
365
366 # Execute the TCL Script created in the above block. If
367 # there are at least N IO operations performed by SQLite as
368 # a result of the script, the Nth will fail.
369 do_test $testname.$n.3 {
370 set r [catch $::ioerrorbody msg]
371 set ::go [expr {$::sqlite_io_error_pending<=0}]
drhc9ac5ca2005-11-04 22:03:30 +0000372 set s [expr $::sqlite_io_error_hit==0]
danielk197732554c12005-01-22 03:39:39 +0000373 # puts "$::sqlite_io_error_pending $r $msg"
374 expr { ($s && !$r) || (!$s && $r) }
375 # expr {$::sqlite_io_error_pending>0 || $r!=0}
376 } {1}
377
378 # If an IO error occured, then the checksum of the database should
379 # be the same as before the script that caused the IO error was run.
380 if {$::go && $::ioerropts(-cksum)} {
381 do_test $testname.$n.4 {
382 catch {db close}
383 set ::DB [sqlite3 db test.db]
384 cksum
385 } $checksum
386 }
387
danielk1977105afed2005-05-26 15:20:53 +0000388 if {[info exists ::ioerropts(-cleanup)]} {
389 catch $::ioerropts(-cleanup)
390 }
danielk197732554c12005-01-22 03:39:39 +0000391 }
392 set ::sqlite_io_error_pending 0
393 unset ::ioerropts
394}
395
396# Return a checksum based on the contents of database 'db'.
397#
398proc cksum {{db db}} {
399 set txt [$db eval {
400 SELECT name, type, sql FROM sqlite_master order by name
401 }]\n
402 foreach tbl [$db eval {
403 SELECT name FROM sqlite_master WHERE type='table' order by name
404 }] {
405 append txt [$db eval "SELECT * FROM $tbl"]\n
406 }
407 foreach prag {default_synchronous default_cache_size} {
408 append txt $prag-[$db eval "PRAGMA $prag"]\n
409 }
410 set cksum [string length $txt]-[md5 $txt]
411 # puts $cksum-[file size test.db]
412 return $cksum
413}
414
415# Copy file $from into $to. This is used because some versions of
416# TCL for windows (notably the 8.4.1 binary package shipped with the
417# current mingw release) have a broken "file copy" command.
418#
419proc copy_file {from to} {
420 if {$::tcl_platform(platform)=="unix"} {
421 file copy -force $from $to
422 } else {
423 set f [open $from]
424 fconfigure $f -translation binary
425 set t [open $to w]
426 fconfigure $t -translation binary
427 puts -nonewline $t [read $f [file size $from]]
428 close $t
429 close $f
430 }
431}
432
danielk19772e588c72005-12-09 14:25:08 +0000433# This command checks for outstanding calls to sqliteMalloc() from within
434# the current thread. A list is returned with one entry for each outstanding
435# malloc. Each list entry is itself a list of 5 items, as follows:
436#
437# { <number-bytes> <file-name> <line-number> <test-case> <stack-dump> }
438#
439proc check_for_leaks {} {
440 set ret [list]
441 foreach alloc [sqlite_malloc_outstanding] {
442 foreach {nBytes file iLine userstring backtrace} $alloc {}
443 set stack [list]
444 set skip 0
445
446 # The first command in this block will probably fail on windows. This
447 # means there will be no stack dump available.
448 catch {
449 set stuff [eval "exec addr2line -e ./testfixture -f $backtrace"]
450 foreach {func line} $stuff {
451 if {$func != "??" || $line != "??:0"} {
452 regexp {.*/(.*)} $line dummy line
453 lappend stack "${func}() $line"
454 } else {
455 if {[lindex $stack end] != "..."} {
456 lappend stack "..."
457 }
458 }
459 }
460 }
461
462 if {!$skip} {
463 lappend ret [list $nBytes $file $iLine $userstring $stack]
464 }
465 }
466 return $ret
467}
468
469# Pretty print a report based on the return value of [check_for_leaks] to
470# stdout.
471proc pp_check_for_leaks {} {
472 set l [check_for_leaks]
473 set n 0
474 foreach leak $l {
475 foreach {nBytes file iLine userstring stack} $leak {}
476 puts "$nBytes bytes leaked at $file:$iLine ($userstring)"
477 foreach frame $stack {
478 puts " $frame"
479 }
480 incr n $nBytes
481 }
482 puts "Memory leaked: $n bytes in [llength $l] allocations"
483 puts ""
484}
485
danielk197745901d62004-11-10 15:27:38 +0000486# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
487# to non-zero, then set the global variable $AUTOVACUUM to 1.
488set AUTOVACUUM $sqlite_options(default_autovacuum)