blob: 20ec60865a6567672388e9f8ad40b2258d48596b [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#
danielk1977374177e2008-05-08 15:58:06 +000014# $Id: tester.tcl,v 1.120 2008/05/08 15:58:06 danielk1977 Exp $
drhfbc3eab2001-04-06 16:13:42 +000015
drh8359d8c2008-03-29 11:00:54 +000016#
17# What for user input before continuing. This gives an opportunity
18# to connect profiling tools to the process.
19#
20for {set i 0} {$i<[llength $argv]} {incr i} {
21 if {[regexp {^-+pause$} [lindex $argv $i] all value]} {
22 puts -nonewline "Press RETURN to begin..."
23 flush stdout
24 gets stdin
25 set argv [lreplace $argv $i $i]
26 }
27}
drh348784e2000-05-29 20:41:49 +000028
drh92febd92004-08-20 18:34:20 +000029set tcl_precision 15
drh49285702005-09-17 15:20:26 +000030set sqlite_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +000031
drh3a7fb7c2007-08-10 16:41:08 +000032#
33# Check the command-line arguments for a default soft-heap-limit.
34# Store this default value in the global variable ::soft_limit and
35# update the soft-heap-limit each time this script is run. In that
36# way if an individual test file changes the soft-heap-limit, it
37# will be reset at the start of the next test file.
38#
39if {![info exists soft_limit]} {
40 set soft_limit 0
41 for {set i 0} {$i<[llength $argv]} {incr i} {
42 if {[regexp {^--soft-heap-limit=(.+)$} [lindex $argv $i] all value]} {
43 if {$value!="off"} {
44 set soft_limit $value
45 }
46 set argv [lreplace $argv $i $i]
47 }
48 }
49}
50sqlite3_soft_heap_limit $soft_limit
51
drh4a50aac2007-08-23 02:47:53 +000052#
53# Check the command-line arguments to set the memory debugger
54# backtrace depth.
55#
56# See the sqlite3_memdebug_backtrace() function in mem2.c or
57# test_malloc.c for additional information.
58#
59for {set i 0} {$i<[llength $argv]} {incr i} {
danielk197735754ac2008-03-21 17:29:37 +000060 if {[lindex $argv $i] eq "--malloctrace"} {
61 set argv [lreplace $argv $i $i]
danielk19775f096132008-03-28 15:44:09 +000062 sqlite3_memdebug_backtrace 10
danielk197735754ac2008-03-21 17:29:37 +000063 sqlite3_memdebug_log start
danielk197735754ac2008-03-21 17:29:37 +000064 set tester_do_malloctrace 1
65 }
66}
67for {set i 0} {$i<[llength $argv]} {incr i} {
drh4a50aac2007-08-23 02:47:53 +000068 if {[regexp {^--backtrace=(\d+)$} [lindex $argv $i] all value]} {
69 sqlite3_memdebug_backtrace $value
70 set argv [lreplace $argv $i $i]
71 }
72}
73
danielk19771e21fd52008-04-10 17:27:38 +000074
75proc ostrace_call {zCall nClick zFile i32 i64} {
76 set s "INSERT INTO ostrace VALUES( '$zCall', $nClick, '$zFile', $i32, $i64);"
77 puts $::ostrace_fd $s
78}
79
80for {set i 0} {$i<[llength $argv]} {incr i} {
81 if {[lindex $argv $i] eq "--ossummary" || [lindex $argv $i] eq "--ostrace"} {
82 sqlite3_instvfs create -default ostrace
83 set tester_do_ostrace 1
84 set ostrace_fd [open ostrace.sql w]
85 puts $ostrace_fd "BEGIN;"
86 if {[lindex $argv $i] eq "--ostrace"} {
87 set s "CREATE TABLE ostrace"
88 append s "(method TEXT, clicks INT, file TEXT, i32 INT, i64 INT);"
89 puts $ostrace_fd $s
90 sqlite3_instvfs configure ostrace ostrace_call
danielk1977374177e2008-05-08 15:58:06 +000091 sqlite3_instvfs configure ostrace ostrace_call
danielk19771e21fd52008-04-10 17:27:38 +000092 }
93 set argv [lreplace $argv $i $i]
94 }
danielk1977374177e2008-05-08 15:58:06 +000095 if {[lindex $argv $i] eq "--binarylog"} {
96 set tester_do_binarylog 1
97 sqlite3_instvfs binarylog -default binarylog ostrace.bin
98 set argv [lreplace $argv $i $i]
99 }
danielk19771e21fd52008-04-10 17:27:38 +0000100}
101
drh6a288a32008-01-07 19:20:24 +0000102#
103# Check the command-line arguments to set the maximum number of
104# errors tolerated before halting.
105#
106if {![info exists maxErr]} {
107 set maxErr 1000
108}
109for {set i 0} {$i<[llength $argv]} {incr i} {
110 if {[regexp {^--maxerror=(\d+)$} [lindex $argv $i] all maxErr]} {
111 set argv [lreplace $argv $i $i]
112 }
113}
114#puts "Max error = $maxErr"
115
drh4a50aac2007-08-23 02:47:53 +0000116
drh9eb9e262004-02-11 02:18:05 +0000117# Use the pager codec if it is available
118#
drhef4ac8f2004-06-19 00:16:31 +0000119if {[sqlite3 -has-codec] && [info command sqlite_orig]==""} {
120 rename sqlite3 sqlite_orig
121 proc sqlite3 {args} {
drh9eb9e262004-02-11 02:18:05 +0000122 if {[llength $args]==2 && [string index [lindex $args 0] 0]!="-"} {
123 lappend args -key {xyzzy}
124 }
125 uplevel 1 sqlite_orig $args
126 }
127}
128
129
drhbec2bf42000-05-29 23:48:22 +0000130# Create a test database
131#
drh254cba22001-09-20 01:44:42 +0000132catch {db close}
133file delete -force test.db
134file delete -force test.db-journal
drhdddca282006-01-03 00:33:50 +0000135sqlite3 db ./test.db
136set ::DB [sqlite3_connection_pointer db]
drhcd61c282002-03-06 22:01:34 +0000137if {[info exists ::SETUP_SQL]} {
138 db eval $::SETUP_SQL
139}
drhbec2bf42000-05-29 23:48:22 +0000140
141# Abort early if this script has been run before.
142#
143if {[info exists nTest]} return
144
145# Set the test counters to zero
146#
drh348784e2000-05-29 20:41:49 +0000147set nErr 0
148set nTest 0
drh767c2002000-10-19 14:10:08 +0000149set skip_test 0
drha1b351a2001-09-14 16:42:12 +0000150set failList {}
drh521cc842008-04-15 02:36:33 +0000151set omitList {}
drhb62c3352006-11-23 09:39:16 +0000152if {![info exists speedTest]} {
153 set speedTest 0
154}
drh348784e2000-05-29 20:41:49 +0000155
drh521cc842008-04-15 02:36:33 +0000156# Record the fact that a sequence of tests were omitted.
157#
158proc omit_test {name reason} {
159 global omitList
160 lappend omitList [list $name $reason]
161}
162
drh348784e2000-05-29 20:41:49 +0000163# Invoke the do_test procedure to run a single test
164#
165proc do_test {name cmd expected} {
drhd3d39e92004-05-20 22:16:29 +0000166 global argv nErr nTest skip_test maxErr
drh4a50aac2007-08-23 02:47:53 +0000167 sqlite3_memdebug_settitle $name
danielk1977374177e2008-05-08 15:58:06 +0000168 if {$::tester_do_binarylog} {
169 sqlite3_instvfs marker binarylog "Start of $name"
170 }
drh767c2002000-10-19 14:10:08 +0000171 if {$skip_test} {
172 set skip_test 0
173 return
174 }
175 if {[llength $argv]==0} {
drh348784e2000-05-29 20:41:49 +0000176 set go 1
177 } else {
178 set go 0
179 foreach pattern $argv {
180 if {[string match $pattern $name]} {
181 set go 1
182 break
183 }
184 }
185 }
186 if {!$go} return
187 incr nTest
drh5edc3122001-09-13 21:53:09 +0000188 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000189 flush stdout
190 if {[catch {uplevel #0 "$cmd;\n"} result]} {
191 puts "\nError: $result"
192 incr nErr
drha1b351a2001-09-14 16:42:12 +0000193 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000194 if {$nErr>$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000195 } elseif {[string compare $result $expected]} {
196 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
197 incr nErr
drha1b351a2001-09-14 16:42:12 +0000198 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000199 if {$nErr>=$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000200 } else {
201 puts " Ok"
202 }
drh6558db82007-04-13 03:23:21 +0000203 flush stdout
danielk1977374177e2008-05-08 15:58:06 +0000204 if {$::tester_do_binarylog} {
205 sqlite3_instvfs marker binarylog "End of $name"
206 }
drh348784e2000-05-29 20:41:49 +0000207}
208
drhb62c3352006-11-23 09:39:16 +0000209# Run an SQL script.
210# Return the number of microseconds per statement.
211#
drh3590f152006-11-23 21:09:10 +0000212proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000213 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000214 flush stdout
215 set speed [time {sqlite3_exec_nr db $sql}]
216 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000217 if {$tm == 0} {
218 set rate [format %20s "many"]
219 } else {
220 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
221 }
drh3590f152006-11-23 21:09:10 +0000222 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000223 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000224 global total_time
225 set total_time [expr {$total_time+$tm}]
226}
drh45c236d2008-03-22 01:08:00 +0000227proc speed_trial_tcl {name numstmt units script} {
228 puts -nonewline [format {%-21.21s } $name...]
229 flush stdout
230 set speed [time {eval $script}]
231 set tm [lindex $speed 0]
232 if {$tm == 0} {
233 set rate [format %20s "many"]
234 } else {
235 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
236 }
237 set u2 $units/s
238 puts [format {%12d uS %s %s} $tm $rate $u2]
239 global total_time
240 set total_time [expr {$total_time+$tm}]
241}
drhdd924312007-03-31 22:34:16 +0000242proc speed_trial_init {name} {
243 global total_time
244 set total_time 0
245}
246proc speed_trial_summary {name} {
247 global total_time
248 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
drhb62c3352006-11-23 09:39:16 +0000249}
250
drh348784e2000-05-29 20:41:49 +0000251# Run this routine last
252#
253proc finish_test {} {
drha1b351a2001-09-14 16:42:12 +0000254 finalize_testing
255}
256proc finalize_testing {} {
drh521cc842008-04-15 02:36:33 +0000257 global nTest nErr sqlite_open_file_count omitList
danielk19772e588c72005-12-09 14:25:08 +0000258
drh6e142f52000-06-08 13:36:40 +0000259 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000260 catch {db2 close}
261 catch {db3 close}
262
drh9bc54492007-10-23 14:49:59 +0000263 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000264 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000265 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000266 db close
drh984bfaa2008-03-19 16:08:53 +0000267 sqlite3_reset_auto_extension
drh3a7fb7c2007-08-10 16:41:08 +0000268 set heaplimit [sqlite3_soft_heap_limit]
269 if {$heaplimit!=$::soft_limit} {
270 puts "soft-heap-limit changed by this script\
271 from $::soft_limit to $heaplimit"
272 } elseif {$heaplimit!="" && $heaplimit>0} {
273 puts "soft-heap-limit set to $heaplimit"
274 }
275 sqlite3_soft_heap_limit 0
drhb4bc7052006-01-11 23:40:33 +0000276 incr nTest
drh348784e2000-05-29 20:41:49 +0000277 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000278 if {$nErr>0} {
279 puts "Failures on these tests: $::failList"
280 }
drh521cc842008-04-15 02:36:33 +0000281 if {[llength $omitList]>0} {
282 puts "Omitted test cases:"
283 set prec {}
284 foreach {rec} [lsort $omitList] {
285 if {$rec==$prec} continue
286 set prec $rec
287 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
288 }
289 }
drh80788d82006-09-02 14:50:23 +0000290 if {$nErr>0 && ![working_64bit_int]} {
291 puts "******************************************************************"
292 puts "N.B.: The version of TCL that you used to build this test harness"
293 puts "is defective in that it does not support 64-bit integers. Some or"
294 puts "all of the test failures above might be a result from this defect"
295 puts "in your TCL build."
296 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000297 }
danielk1977374177e2008-05-08 15:58:06 +0000298 if {[info exists ::tester_do_binarylog]} {
299 sqlite3_instvfs destroy binarylog
300 }
drh94e92032003-02-16 22:21:32 +0000301 if {$sqlite_open_file_count} {
302 puts "$sqlite_open_file_count files were left open"
303 incr nErr
304 }
danielk19771e21fd52008-04-10 17:27:38 +0000305 if {[info exists ::tester_do_ostrace]} {
306 puts "Writing ostrace.sql..."
307 set fd $::ostrace_fd
308
309 puts -nonewline $fd "CREATE TABLE ossummary"
310 puts $fd "(method TEXT, clicks INTEGER, count INTEGER);"
311 foreach row [sqlite3_instvfs report ostrace] {
312 foreach {method count clicks} $row break
313 puts $fd "INSERT INTO ossummary VALUES('$method', $clicks, $count);"
314 }
315 puts $fd "COMMIT;"
316 close $fd
317 sqlite3_instvfs destroy ostrace
318 }
drhf3a65f72007-08-22 20:18:21 +0000319 if {[sqlite3_memory_used]>0} {
320 puts "Unfreed memory: [sqlite3_memory_used] bytes"
321 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000322 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000323 puts "Writing unfreed memory log to \"./memleak.txt\""
324 sqlite3_memdebug_dump ./memleak.txt
325 }
drhf3a65f72007-08-22 20:18:21 +0000326 } else {
327 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000328 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000329 sqlite3_memdebug_dump ./memusage.txt
330 }
drhf3a65f72007-08-22 20:18:21 +0000331 }
drh91fd4d42008-01-19 20:11:25 +0000332 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
333 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000334 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
335 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
336 }
danielk197735754ac2008-03-21 17:29:37 +0000337 if {[info exists ::tester_do_malloctrace]} {
338 puts "Writing mallocs.sql..."
339 memdebug_log_sql
340 sqlite3_memdebug_log stop
341 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000342
343 if {[sqlite3_memory_used]>0} {
344 puts "Writing leaks.sql..."
345 sqlite3_memdebug_log sync
346 memdebug_log_sql leaks.sql
347 }
danielk197735754ac2008-03-21 17:29:37 +0000348 }
drhd9910fe2006-10-04 11:55:49 +0000349 foreach f [glob -nocomplain test.db-*-journal] {
350 file delete -force $f
351 }
352 foreach f [glob -nocomplain test.db-mj*] {
353 file delete -force $f
354 }
drhdb25e382001-03-15 18:21:22 +0000355 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000356}
357
drh348784e2000-05-29 20:41:49 +0000358# A procedure to execute SQL
359#
drhc4a3c772001-04-04 11:48:57 +0000360proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000361 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000362 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000363}
drh3aadb2e2000-05-31 17:59:25 +0000364
drhadbca9c2001-09-27 15:11:53 +0000365# Execute SQL and catch exceptions.
366#
367proc catchsql {sql {db db}} {
368 # puts "SQL = $sql"
369 set r [catch {$db eval $sql} msg]
370 lappend r $msg
371 return $r
372}
373
drh04096482001-11-09 22:41:44 +0000374# Do an VDBE code dump on the SQL given
375#
376proc explain {sql {db db}} {
377 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000378 puts "addr opcode p1 p2 p3 p4 p5 #"
379 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000380 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000381 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
382 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
383 ]
drh04096482001-11-09 22:41:44 +0000384 }
385}
386
drhdafc0ce2008-04-17 19:14:02 +0000387# Show the VDBE program for an SQL statement but omit the Trace
388# opcode at the beginning. This procedure can be used to prove
389# that different SQL statements generate exactly the same VDBE code.
390#
391proc explain_no_trace {sql} {
392 set tr [db eval "EXPLAIN $sql"]
393 return [lrange $tr 7 end]
394}
395
drh3aadb2e2000-05-31 17:59:25 +0000396# Another procedure to execute SQL. This one includes the field
397# names in the returned list.
398#
399proc execsql2 {sql} {
400 set result {}
401 db eval $sql data {
402 foreach f $data(*) {
403 lappend result $f $data($f)
404 }
405 }
406 return $result
407}
drh17a68932001-01-31 13:28:08 +0000408
drhdde85d92003-03-01 19:45:34 +0000409# Use the non-callback API to execute multiple SQL statements
410#
411proc stepsql {dbptr sql} {
412 set sql [string trim $sql]
413 set r 0
414 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000415 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000416 return [list 1 $vm]
417 }
418 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000419# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
420# foreach v $VAL {lappend r $v}
421# }
422 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
423 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
424 lappend r [sqlite3_column_text $vm $i]
425 }
drhdde85d92003-03-01 19:45:34 +0000426 }
danielk1977106bb232004-05-21 10:08:53 +0000427 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000428 return [list 1 $errmsg]
429 }
430 }
431 return $r
432}
433
drh17a68932001-01-31 13:28:08 +0000434# Delete a file or directory
435#
436proc forcedelete {filename} {
437 if {[catch {file delete -force $filename}]} {
438 exec rm -rf $filename
439 }
440}
drh21504322002-06-25 13:16:02 +0000441
442# Do an integrity check of the entire database
443#
444proc integrity_check {name} {
drh27d258a2004-10-30 20:23:09 +0000445 ifcapable integrityck {
446 do_test $name {
447 execsql {PRAGMA integrity_check}
448 } {ok}
449 }
450}
451
danielk1977db4e8862008-01-16 08:24:46 +0000452proc fix_ifcapable_expr {expr} {
453 set ret ""
454 set state 0
455 for {set i 0} {$i < [string length $expr]} {incr i} {
456 set char [string range $expr $i $i]
457 set newstate [expr {[string is alnum $char] || $char eq "_"}]
458 if {$newstate && !$state} {
459 append ret {$::sqlite_options(}
460 }
461 if {!$newstate && $state} {
462 append ret )
463 }
464 append ret $char
465 set state $newstate
466 }
467 if {$state} {append ret )}
468 return $ret
469}
470
drh27d258a2004-10-30 20:23:09 +0000471# Evaluate a boolean expression of capabilities. If true, execute the
472# code. Omit the code if false.
473#
danielk19773e8c37e2005-01-21 03:12:14 +0000474proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +0000475 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
476 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +0000477 if ($e2) {
478 set c [catch {uplevel 1 $code} r]
479 } else {
480 set c [catch {uplevel 1 $elsecode} r]
481 }
482 return -code $c $r
drh21504322002-06-25 13:16:02 +0000483}
danielk197745901d62004-11-10 15:27:38 +0000484
danielk1977aca790a2005-01-13 11:07:52 +0000485# This proc execs a seperate process that crashes midway through executing
486# the SQL script $sql on database test.db.
487#
488# The crash occurs during a sync() of file $crashfile. When the crash
489# occurs a random subset of all unsynced writes made by the process are
490# written into the files on disk. Argument $crashdelay indicates the
491# number of file syncs to wait before crashing.
492#
493# The return value is a list of two elements. The first element is a
494# boolean, indicating whether or not the process actually crashed or
495# reported some other error. The second element in the returned list is the
496# error message. This is "child process exited abnormally" if the crash
497# occured.
498#
danielk1977f8940ae2007-08-23 11:07:10 +0000499# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +0000500#
501proc crashsql {args} {
danielk1977aca790a2005-01-13 11:07:52 +0000502 if {$::tcl_platform(platform)!="unix"} {
503 error "crashsql should only be used on unix"
504 }
danielk197759a33f92007-03-17 10:26:59 +0000505
506 set blocksize ""
507 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +0000508 set prngseed 0
drhf8587402008-01-08 16:03:49 +0000509 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +0000510 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +0000511 set dc ""
danielk197759a33f92007-03-17 10:26:59 +0000512 set sql [lindex $args end]
513
514 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
515 set z [lindex $args $ii]
516 set n [string length $z]
517 set z2 [lindex $args [expr $ii+1]]
518
519 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +0000520 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +0000521 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +0000522 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000523 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +0000524 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +0000525 else { error "Unrecognized option: $z" }
526 }
527
528 if {$crashfile eq ""} {
529 error "Compulsory option -file missing"
530 }
531
danielk1977aca790a2005-01-13 11:07:52 +0000532 set cfile [file join [pwd] $crashfile]
533
534 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +0000535 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +0000536 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
danielk1977933bbd62007-03-17 07:22:42 +0000537 puts $f "set sqlite_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +0000538 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +0000539
540 # This block sets the cache size of the main database to 10
541 # pages. This is done in case the build is configured to omit
542 # "PRAGMA cache_size".
543 puts $f {db eval {SELECT * FROM sqlite_master;}}
544 puts $f {set bt [btree_from_db db]}
545 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +0000546 if {$prngseed} {
547 set seed [expr {$prngseed%10007+1}]
548 # puts seed=$seed
549 puts $f "db eval {SELECT randomblob($seed)}"
550 }
danielk1977933bbd62007-03-17 07:22:42 +0000551
drhf8587402008-01-08 16:03:49 +0000552 if {[string length $tclbody]>0} {
553 puts $f $tclbody
554 }
555 if {[string length $sql]>0} {
556 puts $f "db eval {"
557 puts $f "$sql"
558 puts $f "}"
559 }
danielk1977aca790a2005-01-13 11:07:52 +0000560 close $f
561
562 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000563 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000564 } msg]
565 lappend r $msg
566}
567
danielk197732554c12005-01-22 03:39:39 +0000568# Usage: do_ioerr_test <test number> <options...>
569#
570# This proc is used to implement test cases that check that IO errors
571# are correctly handled. The first argument, <test number>, is an integer
572# used to name the tests executed by this proc. Options are as follows:
573#
574# -tclprep TCL script to run to prepare test.
575# -sqlprep SQL script to run to prepare test.
576# -tclbody TCL script to run with IO error simulation.
577# -sqlbody TCL script to run with IO error simulation.
578# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000579# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +0000580# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +0000581# -start Value of 'N' to begin with (default 1)
582#
583# -cksum Boolean. If true, test that the database does
584# not change during the execution of the test case.
585#
586proc do_ioerr_test {testname args} {
587
danielk197732554c12005-01-22 03:39:39 +0000588 set ::ioerropts(-start) 1
589 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000590 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +0000591 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +0000592 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +0000593 set ::ioerropts(-ckrefcount) 0
danielk197732554c12005-01-22 03:39:39 +0000594 array set ::ioerropts $args
595
596 set ::go 1
drh93aed5a2008-01-16 17:46:38 +0000597 #reset_prng_state
598 save_prng_state
danielk197732554c12005-01-22 03:39:39 +0000599 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +0000600 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +0000601 incr ::ioerropts(-count) -1
602 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +0000603
604 # Skip this IO error if it was specified with the "-exclude" option.
605 if {[info exists ::ioerropts(-exclude)]} {
606 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
607 }
drh93aed5a2008-01-16 17:46:38 +0000608 restore_prng_state
danielk197732554c12005-01-22 03:39:39 +0000609
610 # Delete the files test.db and test2.db, then execute the TCL and
611 # SQL (in that order) to prepare for the test case.
612 do_test $testname.$n.1 {
613 set ::sqlite_io_error_pending 0
614 catch {db close}
615 catch {file delete -force test.db}
616 catch {file delete -force test.db-journal}
617 catch {file delete -force test2.db}
618 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000619 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000620 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000621 if {[info exists ::ioerropts(-tclprep)]} {
622 eval $::ioerropts(-tclprep)
623 }
624 if {[info exists ::ioerropts(-sqlprep)]} {
625 execsql $::ioerropts(-sqlprep)
626 }
627 expr 0
628 } {0}
629
630 # Read the 'checksum' of the database.
631 if {$::ioerropts(-cksum)} {
632 set checksum [cksum]
633 }
drh93aed5a2008-01-16 17:46:38 +0000634
danielk197732554c12005-01-22 03:39:39 +0000635 # Set the Nth IO error to fail.
636 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +0000637 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +0000638 set ::sqlite_io_error_pending $n
639 }] $n
640
641 # Create a single TCL script from the TCL and SQL specified
642 # as the body of the test.
643 set ::ioerrorbody {}
644 if {[info exists ::ioerropts(-tclbody)]} {
645 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
646 }
647 if {[info exists ::ioerropts(-sqlbody)]} {
648 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
649 }
650
651 # Execute the TCL Script created in the above block. If
652 # there are at least N IO operations performed by SQLite as
653 # a result of the script, the Nth will fail.
654 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +0000655 set ::sqlite_io_error_hit 0
656 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +0000657 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +0000658 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +0000659 set rc [sqlite3_errcode $::DB]
660 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +0000661 # If we are in extended result code mode, make sure all of the
662 # IOERRs we get back really do have their extended code values.
663 # If an extended result code is returned, the sqlite3_errcode
664 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
665 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +0000666 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
667 return $rc
668 }
669 } else {
drh5f7b5bf2007-04-19 12:30:54 +0000670 # If we are not in extended result code mode, make sure no
671 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +0000672 if {[regexp {\+\d} $rc]} {
673 return $rc
674 }
675 }
drh93aed5a2008-01-16 17:46:38 +0000676 # The test repeats as long as $::go is non-zero. $::go starts out
677 # as 1. When a test runs to completion without hitting an I/O
678 # error, that means there is no point in continuing with this test
679 # case so set $::go to zero.
680 #
681 if {$::sqlite_io_error_pending>0} {
682 set ::go 0
683 set q 0
684 set ::sqlite_io_error_pending 0
685 } else {
686 set q 1
687 }
688
drhc9ac5ca2005-11-04 22:03:30 +0000689 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +0000690 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
691 set r 1
692 }
danielk1977f2fa8312006-01-24 13:09:33 +0000693 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +0000694
695 # One of two things must have happened. either
696 # 1. We never hit the IO error and the SQL returned OK
697 # 2. An IO error was hit and the SQL failed
698 #
drh93aed5a2008-01-16 17:46:38 +0000699 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +0000700 } {1}
701
danielk197752b472a2008-05-05 16:23:55 +0000702 # Check that no page references were leaked. There should be
703 # a single reference if there is still an active transaction,
704 # or zero otherwise.
705 #
danielk19774abd5442008-05-05 15:26:50 +0000706 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +0000707 do_test $testname.$n.4 {
708 set bt [btree_from_db db]
709 db_enter db
710 array set stats [btree_pager_stats $bt]
711 db_leave db
712 set stats(ref)
713 } [expr {[sqlite3_get_autocommit db]?0:1}]
714 }
715
danielk197752b472a2008-05-05 16:23:55 +0000716 # If there is an open database handle and no open transaction,
717 # and the pager is not running in exclusive-locking mode,
718 # check that the pager is in "unlocked" state. Theoretically,
719 # if a call to xUnlock() failed due to an IO error the underlying
720 # file may still be locked.
721 #
722 ifcapable pragma {
723 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +0000724 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +0000725 && [db one {pragma locking_mode}] eq "normal"
726 && [sqlite3_get_autocommit db]
727 } {
728 do_test $testname.$n.5 {
729 set bt [btree_from_db db]
730 db_enter db
731 array set stats [btree_pager_stats $bt]
732 db_leave db
733 set stats(state)
734 } 0
735 }
736 }
737
danielk197732554c12005-01-22 03:39:39 +0000738 # If an IO error occured, then the checksum of the database should
739 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +0000740 #
drh1aa5af12008-03-07 19:51:14 +0000741 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +0000742 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +0000743 catch {db close}
drha34c62d2006-01-06 22:11:20 +0000744 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +0000745 cksum
746 } $checksum
747 }
748
drh1aa5af12008-03-07 19:51:14 +0000749 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +0000750 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +0000751 if {[info exists ::ioerropts(-cleanup)]} {
752 catch $::ioerropts(-cleanup)
753 }
danielk197732554c12005-01-22 03:39:39 +0000754 }
755 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +0000756 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +0000757 unset ::ioerropts
758}
759
drh93aed5a2008-01-16 17:46:38 +0000760# Return a checksum based on the contents of the main database associated
761# with connection $db
danielk197732554c12005-01-22 03:39:39 +0000762#
763proc cksum {{db db}} {
764 set txt [$db eval {
765 SELECT name, type, sql FROM sqlite_master order by name
766 }]\n
767 foreach tbl [$db eval {
768 SELECT name FROM sqlite_master WHERE type='table' order by name
769 }] {
770 append txt [$db eval "SELECT * FROM $tbl"]\n
771 }
772 foreach prag {default_synchronous default_cache_size} {
773 append txt $prag-[$db eval "PRAGMA $prag"]\n
774 }
775 set cksum [string length $txt]-[md5 $txt]
776 # puts $cksum-[file size test.db]
777 return $cksum
778}
779
drh93aed5a2008-01-16 17:46:38 +0000780# Generate a checksum based on the contents of the main and temp tables
781# database $db. If the checksum of two databases is the same, and the
782# integrity-check passes for both, the two databases are identical.
783#
drh1db639c2008-01-17 02:36:28 +0000784proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +0000785 set ret [list]
786 ifcapable tempdb {
787 set sql {
788 SELECT name FROM sqlite_master WHERE type = 'table' UNION
789 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
790 SELECT 'sqlite_master' UNION
791 SELECT 'sqlite_temp_master' ORDER BY 1
792 }
793 } else {
794 set sql {
795 SELECT name FROM sqlite_master WHERE type = 'table' UNION
796 SELECT 'sqlite_master' ORDER BY 1
797 }
798 }
799 set tbllist [$db eval $sql]
800 set txt {}
801 foreach tbl $tbllist {
802 append txt [$db eval "SELECT * FROM $tbl"]
803 }
804 foreach prag {default_cache_size} {
805 append txt $prag-[$db eval "PRAGMA $prag"]\n
806 }
807 # puts txt=$txt
808 return [md5 $txt]
809}
810
danielk197735754ac2008-03-21 17:29:37 +0000811proc memdebug_log_sql {{filename mallocs.sql}} {
812
danielk19776f332c12008-03-21 14:22:44 +0000813 set data [sqlite3_memdebug_log dump]
814 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +0000815 if {$nFrame < 0} { return "" }
816
danielk197735754ac2008-03-21 17:29:37 +0000817 set database temp
818
danielk19776f332c12008-03-21 14:22:44 +0000819 set tbl "CREATE TABLE ${database}.malloc(nCall, nByte"
820 for {set ii 1} {$ii <= $nFrame} {incr ii} {
821 append tbl ", f${ii}"
822 }
823 append tbl ");\n"
824
825 set sql ""
826 foreach e $data {
827 append sql "INSERT INTO ${database}.malloc VALUES([join $e ,]);\n"
828 foreach f [lrange $e 2 end] {
829 set frames($f) 1
830 }
831 }
832
833 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +0000834 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +0000835
836 foreach f [array names frames] {
837 set addr [format %x $f]
838 set cmd "addr2line -e [info nameofexec] $addr"
839 set line [eval exec $cmd]
840 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +0000841
842 set file [lindex [split $line :] 0]
843 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +0000844 }
845
danielk197735754ac2008-03-21 17:29:37 +0000846 foreach f [array names files] {
847 set contents ""
848 catch {
849 set fd [open $f]
850 set contents [read $fd]
851 close $fd
danielk19776f332c12008-03-21 14:22:44 +0000852 }
danielk197735754ac2008-03-21 17:29:37 +0000853 set contents [string map {' ''} $contents]
854 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +0000855 }
danielk19776f332c12008-03-21 14:22:44 +0000856
danielk197735754ac2008-03-21 17:29:37 +0000857 set fd [open $filename w]
858 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
859 close $fd
danielk19776f332c12008-03-21 14:22:44 +0000860}
drh93aed5a2008-01-16 17:46:38 +0000861
danielk197732554c12005-01-22 03:39:39 +0000862# Copy file $from into $to. This is used because some versions of
863# TCL for windows (notably the 8.4.1 binary package shipped with the
864# current mingw release) have a broken "file copy" command.
865#
866proc copy_file {from to} {
867 if {$::tcl_platform(platform)=="unix"} {
868 file copy -force $from $to
869 } else {
870 set f [open $from]
871 fconfigure $f -translation binary
872 set t [open $to w]
873 fconfigure $t -translation binary
874 puts -nonewline $t [read $f [file size $from]]
875 close $t
876 close $f
877 }
878}
879
danielk197745901d62004-11-10 15:27:38 +0000880# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
881# to non-zero, then set the global variable $AUTOVACUUM to 1.
882set AUTOVACUUM $sqlite_options(default_autovacuum)