blob: 2defff85bf9d42d93f60a25b74acfe2f8e6fb14a [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#
danielk197732554c12005-01-22 03:39:39 +000014# $Id: tester.tcl,v 1.46 2005/01/22 03:39:39 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
44
drh9eb9e262004-02-11 02:18:05 +000045# Use the pager codec if it is available
46#
drhef4ac8f2004-06-19 00:16:31 +000047if {[sqlite3 -has-codec] && [info command sqlite_orig]==""} {
48 rename sqlite3 sqlite_orig
49 proc sqlite3 {args} {
drh9eb9e262004-02-11 02:18:05 +000050 if {[llength $args]==2 && [string index [lindex $args 0] 0]!="-"} {
51 lappend args -key {xyzzy}
52 }
53 uplevel 1 sqlite_orig $args
54 }
55}
56
57
drhbec2bf42000-05-29 23:48:22 +000058# Create a test database
59#
drh254cba22001-09-20 01:44:42 +000060catch {db close}
61file delete -force test.db
62file delete -force test.db-journal
drhef4ac8f2004-06-19 00:16:31 +000063sqlite3 db ./test.db
drhcd61c282002-03-06 22:01:34 +000064if {[info exists ::SETUP_SQL]} {
65 db eval $::SETUP_SQL
66}
drhbec2bf42000-05-29 23:48:22 +000067
68# Abort early if this script has been run before.
69#
70if {[info exists nTest]} return
71
72# Set the test counters to zero
73#
drh348784e2000-05-29 20:41:49 +000074set nErr 0
75set nTest 0
drhdb25e382001-03-15 18:21:22 +000076set nProb 0
drh767c2002000-10-19 14:10:08 +000077set skip_test 0
drha1b351a2001-09-14 16:42:12 +000078set failList {}
drhd3d39e92004-05-20 22:16:29 +000079set maxErr 1000
drh348784e2000-05-29 20:41:49 +000080
81# Invoke the do_test procedure to run a single test
82#
83proc do_test {name cmd expected} {
drhd3d39e92004-05-20 22:16:29 +000084 global argv nErr nTest skip_test maxErr
drh767c2002000-10-19 14:10:08 +000085 if {$skip_test} {
86 set skip_test 0
87 return
88 }
89 if {[llength $argv]==0} {
drh348784e2000-05-29 20:41:49 +000090 set go 1
91 } else {
92 set go 0
93 foreach pattern $argv {
94 if {[string match $pattern $name]} {
95 set go 1
96 break
97 }
98 }
99 }
100 if {!$go} return
101 incr nTest
drh5edc3122001-09-13 21:53:09 +0000102 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000103 flush stdout
104 if {[catch {uplevel #0 "$cmd;\n"} result]} {
105 puts "\nError: $result"
106 incr nErr
drha1b351a2001-09-14 16:42:12 +0000107 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000108 if {$nErr>$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000109 } elseif {[string compare $result $expected]} {
110 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
111 incr nErr
drha1b351a2001-09-14 16:42:12 +0000112 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000113 if {$nErr>=$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000114 } else {
115 puts " Ok"
116 }
117}
118
drhdaffd0e2001-04-11 14:28:42 +0000119# The procedure uses the special "sqlite_malloc_stat" command
drhc89b91b2005-01-03 01:32:59 +0000120# (which is only available if SQLite is compiled with -DSQLITE_DEBUG=1)
drh8c82b352000-12-10 18:23:50 +0000121# to see how many malloc()s have not been free()ed. The number
122# of surplus malloc()s is stored in the global variable $::Leak.
123# If the value in $::Leak grows, it may mean there is a memory leak
124# in the library.
125#
126proc memleak_check {} {
drhdaffd0e2001-04-11 14:28:42 +0000127 if {[info command sqlite_malloc_stat]!=""} {
128 set r [sqlite_malloc_stat]
129 set ::Leak [expr {[lindex $r 0]-[lindex $r 1]}]
130 }
drh8c82b352000-12-10 18:23:50 +0000131}
132
drh348784e2000-05-29 20:41:49 +0000133# Run this routine last
134#
135proc finish_test {} {
drha1b351a2001-09-14 16:42:12 +0000136 finalize_testing
137}
138proc finalize_testing {} {
drh94e92032003-02-16 22:21:32 +0000139 global nTest nErr nProb sqlite_open_file_count
drha1b351a2001-09-14 16:42:12 +0000140 if {$nErr==0} memleak_check
drh6e142f52000-06-08 13:36:40 +0000141 catch {db close}
drh348784e2000-05-29 20:41:49 +0000142 puts "$nErr errors out of $nTest tests"
drha1b351a2001-09-14 16:42:12 +0000143 puts "Failures on these tests: $::failList"
drhdb25e382001-03-15 18:21:22 +0000144 if {$nProb>0} {
145 puts "$nProb probabilistic tests also failed, but this does"
146 puts "not necessarily indicate a malfunction."
147 }
drh3aac2dd2004-04-26 14:10:20 +0000148 if 0 {
drh94e92032003-02-16 22:21:32 +0000149 if {$sqlite_open_file_count} {
150 puts "$sqlite_open_file_count files were left open"
151 incr nErr
152 }
drh3aac2dd2004-04-26 14:10:20 +0000153 }
drhdb25e382001-03-15 18:21:22 +0000154 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000155}
156
drh348784e2000-05-29 20:41:49 +0000157# A procedure to execute SQL
158#
drhc4a3c772001-04-04 11:48:57 +0000159proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000160 # puts "SQL = $sql"
drhc4a3c772001-04-04 11:48:57 +0000161 return [$db eval $sql]
drh348784e2000-05-29 20:41:49 +0000162}
drh3aadb2e2000-05-31 17:59:25 +0000163
drhadbca9c2001-09-27 15:11:53 +0000164# Execute SQL and catch exceptions.
165#
166proc catchsql {sql {db db}} {
167 # puts "SQL = $sql"
168 set r [catch {$db eval $sql} msg]
169 lappend r $msg
170 return $r
171}
172
drh04096482001-11-09 22:41:44 +0000173# Do an VDBE code dump on the SQL given
174#
175proc explain {sql {db db}} {
176 puts ""
177 puts "addr opcode p1 p2 p3 "
178 puts "---- ------------ ------ ------ ---------------"
179 $db eval "explain $sql" {} {
180 puts [format {%-4d %-12.12s %-6d %-6d %s} $addr $opcode $p1 $p2 $p3]
181 }
182}
183
drh3aadb2e2000-05-31 17:59:25 +0000184# Another procedure to execute SQL. This one includes the field
185# names in the returned list.
186#
187proc execsql2 {sql} {
188 set result {}
189 db eval $sql data {
190 foreach f $data(*) {
191 lappend result $f $data($f)
192 }
193 }
194 return $result
195}
drh17a68932001-01-31 13:28:08 +0000196
drhdde85d92003-03-01 19:45:34 +0000197# Use the non-callback API to execute multiple SQL statements
198#
199proc stepsql {dbptr sql} {
200 set sql [string trim $sql]
201 set r 0
202 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000203 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000204 return [list 1 $vm]
205 }
206 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000207# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
208# foreach v $VAL {lappend r $v}
209# }
210 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
211 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
212 lappend r [sqlite3_column_text $vm $i]
213 }
drhdde85d92003-03-01 19:45:34 +0000214 }
danielk1977106bb232004-05-21 10:08:53 +0000215 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000216 return [list 1 $errmsg]
217 }
218 }
219 return $r
220}
221
drh17a68932001-01-31 13:28:08 +0000222# Delete a file or directory
223#
224proc forcedelete {filename} {
225 if {[catch {file delete -force $filename}]} {
226 exec rm -rf $filename
227 }
228}
drh21504322002-06-25 13:16:02 +0000229
230# Do an integrity check of the entire database
231#
232proc integrity_check {name} {
drh27d258a2004-10-30 20:23:09 +0000233 ifcapable integrityck {
234 do_test $name {
235 execsql {PRAGMA integrity_check}
236 } {ok}
237 }
238}
239
240# Evaluate a boolean expression of capabilities. If true, execute the
241# code. Omit the code if false.
242#
danielk19773e8c37e2005-01-21 03:12:14 +0000243proc ifcapable {expr code {else ""} {elsecode ""}} {
drh5436dc22004-11-14 04:04:17 +0000244 regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
danielk19773e8c37e2005-01-21 03:12:14 +0000245 if ($e2) {
246 set c [catch {uplevel 1 $code} r]
247 } else {
248 set c [catch {uplevel 1 $elsecode} r]
249 }
250 return -code $c $r
drh21504322002-06-25 13:16:02 +0000251}
danielk197745901d62004-11-10 15:27:38 +0000252
danielk1977aca790a2005-01-13 11:07:52 +0000253# This proc execs a seperate process that crashes midway through executing
254# the SQL script $sql on database test.db.
255#
256# The crash occurs during a sync() of file $crashfile. When the crash
257# occurs a random subset of all unsynced writes made by the process are
258# written into the files on disk. Argument $crashdelay indicates the
259# number of file syncs to wait before crashing.
260#
261# The return value is a list of two elements. The first element is a
262# boolean, indicating whether or not the process actually crashed or
263# reported some other error. The second element in the returned list is the
264# error message. This is "child process exited abnormally" if the crash
265# occured.
266#
267proc crashsql {crashdelay crashfile sql} {
268 if {$::tcl_platform(platform)!="unix"} {
269 error "crashsql should only be used on unix"
270 }
271 set cfile [file join [pwd] $crashfile]
272
273 set f [open crash.tcl w]
274 puts $f "sqlite3_crashparams $crashdelay $cfile"
275 puts $f "sqlite3 db test.db"
276 puts $f "db eval {pragma cache_size = 10}"
277 puts $f "db eval {"
278 puts $f "$sql"
279 puts $f "}"
280 close $f
281
282 set r [catch {
283 exec [file join . crashtest] crash.tcl >@stdout
284 } msg]
285 lappend r $msg
286}
287
danielk197732554c12005-01-22 03:39:39 +0000288# Usage: do_ioerr_test <test number> <options...>
289#
290# This proc is used to implement test cases that check that IO errors
291# are correctly handled. The first argument, <test number>, is an integer
292# used to name the tests executed by this proc. Options are as follows:
293#
294# -tclprep TCL script to run to prepare test.
295# -sqlprep SQL script to run to prepare test.
296# -tclbody TCL script to run with IO error simulation.
297# -sqlbody TCL script to run with IO error simulation.
298# -exclude List of 'N' values not to test.
299# -start Value of 'N' to begin with (default 1)
300#
301# -cksum Boolean. If true, test that the database does
302# not change during the execution of the test case.
303#
304proc do_ioerr_test {testname args} {
305
306 if {$testname=="ioerr-2"} {
307 breakpoint
308 }
309 set ::ioerropts(-start) 1
310 set ::ioerropts(-cksum) 0
311
312 array set ::ioerropts $args
313
314 set ::go 1
315 for {set n $::ioerropts(-start)} {$::go} {incr n} {
316
317 # Skip this IO error if it was specified with the "-exclude" option.
318 if {[info exists ::ioerropts(-exclude)]} {
319 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
320 }
321
322 # Delete the files test.db and test2.db, then execute the TCL and
323 # SQL (in that order) to prepare for the test case.
324 do_test $testname.$n.1 {
325 set ::sqlite_io_error_pending 0
326 catch {db close}
327 catch {file delete -force test.db}
328 catch {file delete -force test.db-journal}
329 catch {file delete -force test2.db}
330 catch {file delete -force test2.db-journal}
331 set ::DB [sqlite3 db test.db]
332 if {[info exists ::ioerropts(-tclprep)]} {
333 eval $::ioerropts(-tclprep)
334 }
335 if {[info exists ::ioerropts(-sqlprep)]} {
336 execsql $::ioerropts(-sqlprep)
337 }
338 expr 0
339 } {0}
340
341 # Read the 'checksum' of the database.
342 if {$::ioerropts(-cksum)} {
343 set checksum [cksum]
344 }
345
346 # Set the Nth IO error to fail.
347 do_test $testname.$n.2 [subst {
348 set ::sqlite_io_error_pending $n
349 }] $n
350
351 # Create a single TCL script from the TCL and SQL specified
352 # as the body of the test.
353 set ::ioerrorbody {}
354 if {[info exists ::ioerropts(-tclbody)]} {
355 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
356 }
357 if {[info exists ::ioerropts(-sqlbody)]} {
358 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
359 }
360
361 # Execute the TCL Script created in the above block. If
362 # there are at least N IO operations performed by SQLite as
363 # a result of the script, the Nth will fail.
364 do_test $testname.$n.3 {
365 set r [catch $::ioerrorbody msg]
366 set ::go [expr {$::sqlite_io_error_pending<=0}]
367 set s [expr $::sqlite_io_error_pending>0]
368 # puts "$::sqlite_io_error_pending $r $msg"
369 expr { ($s && !$r) || (!$s && $r) }
370 # expr {$::sqlite_io_error_pending>0 || $r!=0}
371 } {1}
372
373 # If an IO error occured, then the checksum of the database should
374 # be the same as before the script that caused the IO error was run.
375 if {$::go && $::ioerropts(-cksum)} {
376 do_test $testname.$n.4 {
377 catch {db close}
378 set ::DB [sqlite3 db test.db]
379 cksum
380 } $checksum
381 }
382
383 }
384 set ::sqlite_io_error_pending 0
385 unset ::ioerropts
386}
387
388# Return a checksum based on the contents of database 'db'.
389#
390proc cksum {{db db}} {
391 set txt [$db eval {
392 SELECT name, type, sql FROM sqlite_master order by name
393 }]\n
394 foreach tbl [$db eval {
395 SELECT name FROM sqlite_master WHERE type='table' order by name
396 }] {
397 append txt [$db eval "SELECT * FROM $tbl"]\n
398 }
399 foreach prag {default_synchronous default_cache_size} {
400 append txt $prag-[$db eval "PRAGMA $prag"]\n
401 }
402 set cksum [string length $txt]-[md5 $txt]
403 # puts $cksum-[file size test.db]
404 return $cksum
405}
406
407# Copy file $from into $to. This is used because some versions of
408# TCL for windows (notably the 8.4.1 binary package shipped with the
409# current mingw release) have a broken "file copy" command.
410#
411proc copy_file {from to} {
412 if {$::tcl_platform(platform)=="unix"} {
413 file copy -force $from $to
414 } else {
415 set f [open $from]
416 fconfigure $f -translation binary
417 set t [open $to w]
418 fconfigure $t -translation binary
419 puts -nonewline $t [read $f [file size $from]]
420 close $t
421 close $f
422 }
423}
424
danielk197745901d62004-11-10 15:27:38 +0000425# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
426# to non-zero, then set the global variable $AUTOVACUUM to 1.
427set AUTOVACUUM $sqlite_options(default_autovacuum)