blob: 6f68006afc2203e0c10c3aed169b038a9d1ef938 [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#
drhe49f9822006-09-15 12:29:16 +000014# $Id: tester.tcl,v 1.68 2006/09/15 12:29:16 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
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 {} {
drh80788d82006-09-02 14:50:23 +0000141 global nTest nErr 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
danielk197797cb2e92005-12-09 14:39:04 +0000148 catch {
149 pp_check_for_leaks
150 }
drhb4bc7052006-01-11 23:40:33 +0000151 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000152 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000153 db close
154 if {$::sqlite3_tsd_count} {
155 puts "Thread-specific data leak: $::sqlite3_tsd_count instances"
156 incr nErr
157 } else {
158 puts "Thread-specific data deallocated properly"
159 }
160 incr nTest
drh348784e2000-05-29 20:41:49 +0000161 puts "$nErr errors out of $nTest tests"
drha1b351a2001-09-14 16:42:12 +0000162 puts "Failures on these tests: $::failList"
drh80788d82006-09-02 14:50:23 +0000163 if {$nErr>0 && ![working_64bit_int]} {
164 puts "******************************************************************"
165 puts "N.B.: The version of TCL that you used to build this test harness"
166 puts "is defective in that it does not support 64-bit integers. Some or"
167 puts "all of the test failures above might be a result from this defect"
168 puts "in your TCL build."
169 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000170 }
drh94e92032003-02-16 22:21:32 +0000171 if {$sqlite_open_file_count} {
172 puts "$sqlite_open_file_count files were left open"
173 incr nErr
174 }
drhdb25e382001-03-15 18:21:22 +0000175 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000176}
177
drh348784e2000-05-29 20:41:49 +0000178# A procedure to execute SQL
179#
drhc4a3c772001-04-04 11:48:57 +0000180proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000181 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000182 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000183}
drh3aadb2e2000-05-31 17:59:25 +0000184
drhadbca9c2001-09-27 15:11:53 +0000185# Execute SQL and catch exceptions.
186#
187proc catchsql {sql {db db}} {
188 # puts "SQL = $sql"
189 set r [catch {$db eval $sql} msg]
190 lappend r $msg
191 return $r
192}
193
drh04096482001-11-09 22:41:44 +0000194# Do an VDBE code dump on the SQL given
195#
196proc explain {sql {db db}} {
197 puts ""
198 puts "addr opcode p1 p2 p3 "
199 puts "---- ------------ ------ ------ ---------------"
200 $db eval "explain $sql" {} {
201 puts [format {%-4d %-12.12s %-6d %-6d %s} $addr $opcode $p1 $p2 $p3]
202 }
203}
204
drh3aadb2e2000-05-31 17:59:25 +0000205# Another procedure to execute SQL. This one includes the field
206# names in the returned list.
207#
208proc execsql2 {sql} {
209 set result {}
210 db eval $sql data {
211 foreach f $data(*) {
212 lappend result $f $data($f)
213 }
214 }
215 return $result
216}
drh17a68932001-01-31 13:28:08 +0000217
drhdde85d92003-03-01 19:45:34 +0000218# Use the non-callback API to execute multiple SQL statements
219#
220proc stepsql {dbptr sql} {
221 set sql [string trim $sql]
222 set r 0
223 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000224 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000225 return [list 1 $vm]
226 }
227 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000228# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
229# foreach v $VAL {lappend r $v}
230# }
231 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
232 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
233 lappend r [sqlite3_column_text $vm $i]
234 }
drhdde85d92003-03-01 19:45:34 +0000235 }
danielk1977106bb232004-05-21 10:08:53 +0000236 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000237 return [list 1 $errmsg]
238 }
239 }
240 return $r
241}
242
drh17a68932001-01-31 13:28:08 +0000243# Delete a file or directory
244#
245proc forcedelete {filename} {
246 if {[catch {file delete -force $filename}]} {
247 exec rm -rf $filename
248 }
249}
drh21504322002-06-25 13:16:02 +0000250
251# Do an integrity check of the entire database
252#
253proc integrity_check {name} {
drh27d258a2004-10-30 20:23:09 +0000254 ifcapable integrityck {
255 do_test $name {
256 execsql {PRAGMA integrity_check}
257 } {ok}
258 }
259}
260
261# Evaluate a boolean expression of capabilities. If true, execute the
262# code. Omit the code if false.
263#
danielk19773e8c37e2005-01-21 03:12:14 +0000264proc ifcapable {expr code {else ""} {elsecode ""}} {
drh5436dc22004-11-14 04:04:17 +0000265 regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
danielk19773e8c37e2005-01-21 03:12:14 +0000266 if ($e2) {
267 set c [catch {uplevel 1 $code} r]
268 } else {
269 set c [catch {uplevel 1 $elsecode} r]
270 }
271 return -code $c $r
drh21504322002-06-25 13:16:02 +0000272}
danielk197745901d62004-11-10 15:27:38 +0000273
danielk1977aca790a2005-01-13 11:07:52 +0000274# This proc execs a seperate process that crashes midway through executing
275# the SQL script $sql on database test.db.
276#
277# The crash occurs during a sync() of file $crashfile. When the crash
278# occurs a random subset of all unsynced writes made by the process are
279# written into the files on disk. Argument $crashdelay indicates the
280# number of file syncs to wait before crashing.
281#
282# The return value is a list of two elements. The first element is a
283# boolean, indicating whether or not the process actually crashed or
284# reported some other error. The second element in the returned list is the
285# error message. This is "child process exited abnormally" if the crash
286# occured.
287#
288proc crashsql {crashdelay crashfile sql} {
289 if {$::tcl_platform(platform)!="unix"} {
290 error "crashsql should only be used on unix"
291 }
292 set cfile [file join [pwd] $crashfile]
293
294 set f [open crash.tcl w]
295 puts $f "sqlite3_crashparams $crashdelay $cfile"
296 puts $f "sqlite3 db test.db"
297 puts $f "db eval {pragma cache_size = 10}"
298 puts $f "db eval {"
299 puts $f "$sql"
300 puts $f "}"
301 close $f
302
303 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000304 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000305 } msg]
306 lappend r $msg
307}
308
danielk197732554c12005-01-22 03:39:39 +0000309# Usage: do_ioerr_test <test number> <options...>
310#
311# This proc is used to implement test cases that check that IO errors
312# are correctly handled. The first argument, <test number>, is an integer
313# used to name the tests executed by this proc. Options are as follows:
314#
315# -tclprep TCL script to run to prepare test.
316# -sqlprep SQL script to run to prepare test.
317# -tclbody TCL script to run with IO error simulation.
318# -sqlbody TCL script to run with IO error simulation.
319# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000320# -erc Use extended result codes
danielk197732554c12005-01-22 03:39:39 +0000321# -start Value of 'N' to begin with (default 1)
322#
323# -cksum Boolean. If true, test that the database does
324# not change during the execution of the test case.
325#
326proc do_ioerr_test {testname args} {
327
danielk197732554c12005-01-22 03:39:39 +0000328 set ::ioerropts(-start) 1
329 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000330 set ::ioerropts(-erc) 0
danielk197732554c12005-01-22 03:39:39 +0000331 array set ::ioerropts $args
332
333 set ::go 1
334 for {set n $::ioerropts(-start)} {$::go} {incr n} {
335
336 # Skip this IO error if it was specified with the "-exclude" option.
337 if {[info exists ::ioerropts(-exclude)]} {
338 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
339 }
340
341 # Delete the files test.db and test2.db, then execute the TCL and
342 # SQL (in that order) to prepare for the test case.
343 do_test $testname.$n.1 {
344 set ::sqlite_io_error_pending 0
345 catch {db close}
346 catch {file delete -force test.db}
347 catch {file delete -force test.db-journal}
348 catch {file delete -force test2.db}
349 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000350 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000351 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000352 if {[info exists ::ioerropts(-tclprep)]} {
353 eval $::ioerropts(-tclprep)
354 }
355 if {[info exists ::ioerropts(-sqlprep)]} {
356 execsql $::ioerropts(-sqlprep)
357 }
358 expr 0
359 } {0}
360
361 # Read the 'checksum' of the database.
362 if {$::ioerropts(-cksum)} {
363 set checksum [cksum]
364 }
365
366 # Set the Nth IO error to fail.
367 do_test $testname.$n.2 [subst {
368 set ::sqlite_io_error_pending $n
369 }] $n
370
371 # Create a single TCL script from the TCL and SQL specified
372 # as the body of the test.
373 set ::ioerrorbody {}
374 if {[info exists ::ioerropts(-tclbody)]} {
375 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
376 }
377 if {[info exists ::ioerropts(-sqlbody)]} {
378 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
379 }
380
381 # Execute the TCL Script created in the above block. If
382 # there are at least N IO operations performed by SQLite as
383 # a result of the script, the Nth will fail.
384 do_test $testname.$n.3 {
385 set r [catch $::ioerrorbody msg]
drh4ac285a2006-09-15 07:28:50 +0000386 # puts rc=[sqlite3_errcode $::DB]
drhe49f9822006-09-15 12:29:16 +0000387 set rc [sqlite3_errcode $::DB]
388 if {$::ioerropts(-erc)} {
389 # In extended result mode, all IOERRs are qualified
390 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
391 return $rc
392 }
393 } else {
394 # Not in extended result mode, no errors are qualified
395 if {[regexp {\+\d} $rc]} {
396 return $rc
397 }
398 }
danielk197732554c12005-01-22 03:39:39 +0000399 set ::go [expr {$::sqlite_io_error_pending<=0}]
drhc9ac5ca2005-11-04 22:03:30 +0000400 set s [expr $::sqlite_io_error_hit==0]
danielk1977f2fa8312006-01-24 13:09:33 +0000401 set ::sqlite_io_error_hit 0
danielk197732554c12005-01-22 03:39:39 +0000402 # puts "$::sqlite_io_error_pending $r $msg"
danielk1977f2fa8312006-01-24 13:09:33 +0000403 # puts "r=$r s=$s go=$::go msg=\"$msg\""
404 expr { ($s && !$r && !$::go) || (!$s && $r && $::go) }
danielk197732554c12005-01-22 03:39:39 +0000405 # expr {$::sqlite_io_error_pending>0 || $r!=0}
406 } {1}
407
408 # If an IO error occured, then the checksum of the database should
409 # be the same as before the script that caused the IO error was run.
410 if {$::go && $::ioerropts(-cksum)} {
411 do_test $testname.$n.4 {
412 catch {db close}
drha34c62d2006-01-06 22:11:20 +0000413 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +0000414 cksum
415 } $checksum
416 }
417
danielk1977c4da5b92006-01-21 12:08:54 +0000418 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +0000419 if {[info exists ::ioerropts(-cleanup)]} {
420 catch $::ioerropts(-cleanup)
421 }
danielk197732554c12005-01-22 03:39:39 +0000422 }
423 set ::sqlite_io_error_pending 0
424 unset ::ioerropts
425}
426
427# Return a checksum based on the contents of database 'db'.
428#
429proc cksum {{db db}} {
430 set txt [$db eval {
431 SELECT name, type, sql FROM sqlite_master order by name
432 }]\n
433 foreach tbl [$db eval {
434 SELECT name FROM sqlite_master WHERE type='table' order by name
435 }] {
436 append txt [$db eval "SELECT * FROM $tbl"]\n
437 }
438 foreach prag {default_synchronous default_cache_size} {
439 append txt $prag-[$db eval "PRAGMA $prag"]\n
440 }
441 set cksum [string length $txt]-[md5 $txt]
442 # puts $cksum-[file size test.db]
443 return $cksum
444}
445
446# Copy file $from into $to. This is used because some versions of
447# TCL for windows (notably the 8.4.1 binary package shipped with the
448# current mingw release) have a broken "file copy" command.
449#
450proc copy_file {from to} {
451 if {$::tcl_platform(platform)=="unix"} {
452 file copy -force $from $to
453 } else {
454 set f [open $from]
455 fconfigure $f -translation binary
456 set t [open $to w]
457 fconfigure $t -translation binary
458 puts -nonewline $t [read $f [file size $from]]
459 close $t
460 close $f
461 }
462}
463
danielk19772e588c72005-12-09 14:25:08 +0000464# This command checks for outstanding calls to sqliteMalloc() from within
465# the current thread. A list is returned with one entry for each outstanding
466# malloc. Each list entry is itself a list of 5 items, as follows:
467#
468# { <number-bytes> <file-name> <line-number> <test-case> <stack-dump> }
469#
470proc check_for_leaks {} {
471 set ret [list]
danielk1977aef0bf62005-12-30 16:28:01 +0000472 set cnt 0
danielk19772e588c72005-12-09 14:25:08 +0000473 foreach alloc [sqlite_malloc_outstanding] {
474 foreach {nBytes file iLine userstring backtrace} $alloc {}
475 set stack [list]
476 set skip 0
477
478 # The first command in this block will probably fail on windows. This
479 # means there will be no stack dump available.
danielk1977da184232006-01-05 11:34:32 +0000480 if {$cnt < 25 && $backtrace!=""} {
danielk1977aef0bf62005-12-30 16:28:01 +0000481 catch {
482 set stuff [eval "exec addr2line -e ./testfixture -f $backtrace"]
483 foreach {func line} $stuff {
484 if {$func != "??" || $line != "??:0"} {
485 regexp {.*/(.*)} $line dummy line
486 lappend stack "${func}() $line"
487 } else {
488 if {[lindex $stack end] != "..."} {
489 lappend stack "..."
490 }
danielk19772e588c72005-12-09 14:25:08 +0000491 }
492 }
493 }
danielk1977aef0bf62005-12-30 16:28:01 +0000494 incr cnt
danielk19772e588c72005-12-09 14:25:08 +0000495 }
496
497 if {!$skip} {
498 lappend ret [list $nBytes $file $iLine $userstring $stack]
499 }
500 }
501 return $ret
502}
503
504# Pretty print a report based on the return value of [check_for_leaks] to
505# stdout.
506proc pp_check_for_leaks {} {
507 set l [check_for_leaks]
508 set n 0
509 foreach leak $l {
510 foreach {nBytes file iLine userstring stack} $leak {}
511 puts "$nBytes bytes leaked at $file:$iLine ($userstring)"
512 foreach frame $stack {
513 puts " $frame"
514 }
515 incr n $nBytes
516 }
517 puts "Memory leaked: $n bytes in [llength $l] allocations"
518 puts ""
519}
520
danielk197745901d62004-11-10 15:27:38 +0000521# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
522# to non-zero, then set the global variable $AUTOVACUUM to 1.
523set AUTOVACUUM $sqlite_options(default_autovacuum)