blob: 47cb1a77e4d3844979a027f0458f71e70f18cf9e [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#
drhdafc0ce2008-04-17 19:14:02 +000014# $Id: tester.tcl,v 1.116 2008/04/17 19:14:02 drh 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
91 }
92 set argv [lreplace $argv $i $i]
93 }
94}
95
drh6a288a32008-01-07 19:20:24 +000096#
97# Check the command-line arguments to set the maximum number of
98# errors tolerated before halting.
99#
100if {![info exists maxErr]} {
101 set maxErr 1000
102}
103for {set i 0} {$i<[llength $argv]} {incr i} {
104 if {[regexp {^--maxerror=(\d+)$} [lindex $argv $i] all maxErr]} {
105 set argv [lreplace $argv $i $i]
106 }
107}
108#puts "Max error = $maxErr"
109
drh4a50aac2007-08-23 02:47:53 +0000110
drh9eb9e262004-02-11 02:18:05 +0000111# Use the pager codec if it is available
112#
drhef4ac8f2004-06-19 00:16:31 +0000113if {[sqlite3 -has-codec] && [info command sqlite_orig]==""} {
114 rename sqlite3 sqlite_orig
115 proc sqlite3 {args} {
drh9eb9e262004-02-11 02:18:05 +0000116 if {[llength $args]==2 && [string index [lindex $args 0] 0]!="-"} {
117 lappend args -key {xyzzy}
118 }
119 uplevel 1 sqlite_orig $args
120 }
121}
122
123
drhbec2bf42000-05-29 23:48:22 +0000124# Create a test database
125#
drh254cba22001-09-20 01:44:42 +0000126catch {db close}
127file delete -force test.db
128file delete -force test.db-journal
drhdddca282006-01-03 00:33:50 +0000129sqlite3 db ./test.db
130set ::DB [sqlite3_connection_pointer db]
drhcd61c282002-03-06 22:01:34 +0000131if {[info exists ::SETUP_SQL]} {
132 db eval $::SETUP_SQL
133}
drhbec2bf42000-05-29 23:48:22 +0000134
135# Abort early if this script has been run before.
136#
137if {[info exists nTest]} return
138
139# Set the test counters to zero
140#
drh348784e2000-05-29 20:41:49 +0000141set nErr 0
142set nTest 0
drh767c2002000-10-19 14:10:08 +0000143set skip_test 0
drha1b351a2001-09-14 16:42:12 +0000144set failList {}
drh521cc842008-04-15 02:36:33 +0000145set omitList {}
drhb62c3352006-11-23 09:39:16 +0000146if {![info exists speedTest]} {
147 set speedTest 0
148}
drh348784e2000-05-29 20:41:49 +0000149
drh521cc842008-04-15 02:36:33 +0000150# Record the fact that a sequence of tests were omitted.
151#
152proc omit_test {name reason} {
153 global omitList
154 lappend omitList [list $name $reason]
155}
156
drh348784e2000-05-29 20:41:49 +0000157# Invoke the do_test procedure to run a single test
158#
159proc do_test {name cmd expected} {
drhd3d39e92004-05-20 22:16:29 +0000160 global argv nErr nTest skip_test maxErr
drh4a50aac2007-08-23 02:47:53 +0000161 sqlite3_memdebug_settitle $name
drh767c2002000-10-19 14:10:08 +0000162 if {$skip_test} {
163 set skip_test 0
164 return
165 }
166 if {[llength $argv]==0} {
drh348784e2000-05-29 20:41:49 +0000167 set go 1
168 } else {
169 set go 0
170 foreach pattern $argv {
171 if {[string match $pattern $name]} {
172 set go 1
173 break
174 }
175 }
176 }
177 if {!$go} return
178 incr nTest
drh5edc3122001-09-13 21:53:09 +0000179 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000180 flush stdout
181 if {[catch {uplevel #0 "$cmd;\n"} result]} {
182 puts "\nError: $result"
183 incr nErr
drha1b351a2001-09-14 16:42:12 +0000184 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000185 if {$nErr>$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000186 } elseif {[string compare $result $expected]} {
187 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
188 incr nErr
drha1b351a2001-09-14 16:42:12 +0000189 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000190 if {$nErr>=$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000191 } else {
192 puts " Ok"
193 }
drh6558db82007-04-13 03:23:21 +0000194 flush stdout
drh348784e2000-05-29 20:41:49 +0000195}
196
drhb62c3352006-11-23 09:39:16 +0000197# Run an SQL script.
198# Return the number of microseconds per statement.
199#
drh3590f152006-11-23 21:09:10 +0000200proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000201 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000202 flush stdout
203 set speed [time {sqlite3_exec_nr db $sql}]
204 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000205 if {$tm == 0} {
206 set rate [format %20s "many"]
207 } else {
208 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
209 }
drh3590f152006-11-23 21:09:10 +0000210 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000211 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000212 global total_time
213 set total_time [expr {$total_time+$tm}]
214}
drh45c236d2008-03-22 01:08:00 +0000215proc speed_trial_tcl {name numstmt units script} {
216 puts -nonewline [format {%-21.21s } $name...]
217 flush stdout
218 set speed [time {eval $script}]
219 set tm [lindex $speed 0]
220 if {$tm == 0} {
221 set rate [format %20s "many"]
222 } else {
223 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
224 }
225 set u2 $units/s
226 puts [format {%12d uS %s %s} $tm $rate $u2]
227 global total_time
228 set total_time [expr {$total_time+$tm}]
229}
drhdd924312007-03-31 22:34:16 +0000230proc speed_trial_init {name} {
231 global total_time
232 set total_time 0
233}
234proc speed_trial_summary {name} {
235 global total_time
236 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
drhb62c3352006-11-23 09:39:16 +0000237}
238
drh348784e2000-05-29 20:41:49 +0000239# Run this routine last
240#
241proc finish_test {} {
drha1b351a2001-09-14 16:42:12 +0000242 finalize_testing
243}
244proc finalize_testing {} {
drh521cc842008-04-15 02:36:33 +0000245 global nTest nErr sqlite_open_file_count omitList
danielk19772e588c72005-12-09 14:25:08 +0000246
drh6e142f52000-06-08 13:36:40 +0000247 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000248 catch {db2 close}
249 catch {db3 close}
250
drh9bc54492007-10-23 14:49:59 +0000251 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000252 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000253 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000254 db close
drh984bfaa2008-03-19 16:08:53 +0000255 sqlite3_reset_auto_extension
drh3a7fb7c2007-08-10 16:41:08 +0000256 set heaplimit [sqlite3_soft_heap_limit]
257 if {$heaplimit!=$::soft_limit} {
258 puts "soft-heap-limit changed by this script\
259 from $::soft_limit to $heaplimit"
260 } elseif {$heaplimit!="" && $heaplimit>0} {
261 puts "soft-heap-limit set to $heaplimit"
262 }
263 sqlite3_soft_heap_limit 0
drhb4bc7052006-01-11 23:40:33 +0000264 incr nTest
drh348784e2000-05-29 20:41:49 +0000265 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000266 if {$nErr>0} {
267 puts "Failures on these tests: $::failList"
268 }
drh521cc842008-04-15 02:36:33 +0000269 if {[llength $omitList]>0} {
270 puts "Omitted test cases:"
271 set prec {}
272 foreach {rec} [lsort $omitList] {
273 if {$rec==$prec} continue
274 set prec $rec
275 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
276 }
277 }
drh80788d82006-09-02 14:50:23 +0000278 if {$nErr>0 && ![working_64bit_int]} {
279 puts "******************************************************************"
280 puts "N.B.: The version of TCL that you used to build this test harness"
281 puts "is defective in that it does not support 64-bit integers. Some or"
282 puts "all of the test failures above might be a result from this defect"
283 puts "in your TCL build."
284 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000285 }
drh94e92032003-02-16 22:21:32 +0000286 if {$sqlite_open_file_count} {
287 puts "$sqlite_open_file_count files were left open"
288 incr nErr
289 }
danielk19771e21fd52008-04-10 17:27:38 +0000290 if {[info exists ::tester_do_ostrace]} {
291 puts "Writing ostrace.sql..."
292 set fd $::ostrace_fd
293
294 puts -nonewline $fd "CREATE TABLE ossummary"
295 puts $fd "(method TEXT, clicks INTEGER, count INTEGER);"
296 foreach row [sqlite3_instvfs report ostrace] {
297 foreach {method count clicks} $row break
298 puts $fd "INSERT INTO ossummary VALUES('$method', $clicks, $count);"
299 }
300 puts $fd "COMMIT;"
301 close $fd
302 sqlite3_instvfs destroy ostrace
303 }
drhf3a65f72007-08-22 20:18:21 +0000304 if {[sqlite3_memory_used]>0} {
305 puts "Unfreed memory: [sqlite3_memory_used] bytes"
306 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000307 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000308 puts "Writing unfreed memory log to \"./memleak.txt\""
309 sqlite3_memdebug_dump ./memleak.txt
310 }
drhf3a65f72007-08-22 20:18:21 +0000311 } else {
312 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000313 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000314 sqlite3_memdebug_dump ./memusage.txt
315 }
drhf3a65f72007-08-22 20:18:21 +0000316 }
drh91fd4d42008-01-19 20:11:25 +0000317 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
318 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000319 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
320 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
321 }
danielk197735754ac2008-03-21 17:29:37 +0000322 if {[info exists ::tester_do_malloctrace]} {
323 puts "Writing mallocs.sql..."
324 memdebug_log_sql
325 sqlite3_memdebug_log stop
326 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000327
328 if {[sqlite3_memory_used]>0} {
329 puts "Writing leaks.sql..."
330 sqlite3_memdebug_log sync
331 memdebug_log_sql leaks.sql
332 }
danielk197735754ac2008-03-21 17:29:37 +0000333 }
drhd9910fe2006-10-04 11:55:49 +0000334 foreach f [glob -nocomplain test.db-*-journal] {
335 file delete -force $f
336 }
337 foreach f [glob -nocomplain test.db-mj*] {
338 file delete -force $f
339 }
drhdb25e382001-03-15 18:21:22 +0000340 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000341}
342
drh348784e2000-05-29 20:41:49 +0000343# A procedure to execute SQL
344#
drhc4a3c772001-04-04 11:48:57 +0000345proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000346 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000347 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000348}
drh3aadb2e2000-05-31 17:59:25 +0000349
drhadbca9c2001-09-27 15:11:53 +0000350# Execute SQL and catch exceptions.
351#
352proc catchsql {sql {db db}} {
353 # puts "SQL = $sql"
354 set r [catch {$db eval $sql} msg]
355 lappend r $msg
356 return $r
357}
358
drh04096482001-11-09 22:41:44 +0000359# Do an VDBE code dump on the SQL given
360#
361proc explain {sql {db db}} {
362 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000363 puts "addr opcode p1 p2 p3 p4 p5 #"
364 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000365 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000366 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
367 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
368 ]
drh04096482001-11-09 22:41:44 +0000369 }
370}
371
drhdafc0ce2008-04-17 19:14:02 +0000372# Show the VDBE program for an SQL statement but omit the Trace
373# opcode at the beginning. This procedure can be used to prove
374# that different SQL statements generate exactly the same VDBE code.
375#
376proc explain_no_trace {sql} {
377 set tr [db eval "EXPLAIN $sql"]
378 return [lrange $tr 7 end]
379}
380
drh3aadb2e2000-05-31 17:59:25 +0000381# Another procedure to execute SQL. This one includes the field
382# names in the returned list.
383#
384proc execsql2 {sql} {
385 set result {}
386 db eval $sql data {
387 foreach f $data(*) {
388 lappend result $f $data($f)
389 }
390 }
391 return $result
392}
drh17a68932001-01-31 13:28:08 +0000393
drhdde85d92003-03-01 19:45:34 +0000394# Use the non-callback API to execute multiple SQL statements
395#
396proc stepsql {dbptr sql} {
397 set sql [string trim $sql]
398 set r 0
399 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000400 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000401 return [list 1 $vm]
402 }
403 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000404# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
405# foreach v $VAL {lappend r $v}
406# }
407 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
408 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
409 lappend r [sqlite3_column_text $vm $i]
410 }
drhdde85d92003-03-01 19:45:34 +0000411 }
danielk1977106bb232004-05-21 10:08:53 +0000412 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000413 return [list 1 $errmsg]
414 }
415 }
416 return $r
417}
418
drh17a68932001-01-31 13:28:08 +0000419# Delete a file or directory
420#
421proc forcedelete {filename} {
422 if {[catch {file delete -force $filename}]} {
423 exec rm -rf $filename
424 }
425}
drh21504322002-06-25 13:16:02 +0000426
427# Do an integrity check of the entire database
428#
429proc integrity_check {name} {
drh27d258a2004-10-30 20:23:09 +0000430 ifcapable integrityck {
431 do_test $name {
432 execsql {PRAGMA integrity_check}
433 } {ok}
434 }
435}
436
danielk1977db4e8862008-01-16 08:24:46 +0000437proc fix_ifcapable_expr {expr} {
438 set ret ""
439 set state 0
440 for {set i 0} {$i < [string length $expr]} {incr i} {
441 set char [string range $expr $i $i]
442 set newstate [expr {[string is alnum $char] || $char eq "_"}]
443 if {$newstate && !$state} {
444 append ret {$::sqlite_options(}
445 }
446 if {!$newstate && $state} {
447 append ret )
448 }
449 append ret $char
450 set state $newstate
451 }
452 if {$state} {append ret )}
453 return $ret
454}
455
drh27d258a2004-10-30 20:23:09 +0000456# Evaluate a boolean expression of capabilities. If true, execute the
457# code. Omit the code if false.
458#
danielk19773e8c37e2005-01-21 03:12:14 +0000459proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +0000460 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
461 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +0000462 if ($e2) {
463 set c [catch {uplevel 1 $code} r]
464 } else {
465 set c [catch {uplevel 1 $elsecode} r]
466 }
467 return -code $c $r
drh21504322002-06-25 13:16:02 +0000468}
danielk197745901d62004-11-10 15:27:38 +0000469
danielk1977aca790a2005-01-13 11:07:52 +0000470# This proc execs a seperate process that crashes midway through executing
471# the SQL script $sql on database test.db.
472#
473# The crash occurs during a sync() of file $crashfile. When the crash
474# occurs a random subset of all unsynced writes made by the process are
475# written into the files on disk. Argument $crashdelay indicates the
476# number of file syncs to wait before crashing.
477#
478# The return value is a list of two elements. The first element is a
479# boolean, indicating whether or not the process actually crashed or
480# reported some other error. The second element in the returned list is the
481# error message. This is "child process exited abnormally" if the crash
482# occured.
483#
danielk1977f8940ae2007-08-23 11:07:10 +0000484# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +0000485#
486proc crashsql {args} {
danielk1977aca790a2005-01-13 11:07:52 +0000487 if {$::tcl_platform(platform)!="unix"} {
488 error "crashsql should only be used on unix"
489 }
danielk197759a33f92007-03-17 10:26:59 +0000490
491 set blocksize ""
492 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +0000493 set prngseed 0
drhf8587402008-01-08 16:03:49 +0000494 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +0000495 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +0000496 set dc ""
danielk197759a33f92007-03-17 10:26:59 +0000497 set sql [lindex $args end]
498
499 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
500 set z [lindex $args $ii]
501 set n [string length $z]
502 set z2 [lindex $args [expr $ii+1]]
503
504 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +0000505 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +0000506 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +0000507 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000508 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +0000509 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +0000510 else { error "Unrecognized option: $z" }
511 }
512
513 if {$crashfile eq ""} {
514 error "Compulsory option -file missing"
515 }
516
danielk1977aca790a2005-01-13 11:07:52 +0000517 set cfile [file join [pwd] $crashfile]
518
519 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +0000520 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +0000521 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
danielk1977933bbd62007-03-17 07:22:42 +0000522 puts $f "set sqlite_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +0000523 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +0000524
525 # This block sets the cache size of the main database to 10
526 # pages. This is done in case the build is configured to omit
527 # "PRAGMA cache_size".
528 puts $f {db eval {SELECT * FROM sqlite_master;}}
529 puts $f {set bt [btree_from_db db]}
530 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +0000531 if {$prngseed} {
532 set seed [expr {$prngseed%10007+1}]
533 # puts seed=$seed
534 puts $f "db eval {SELECT randomblob($seed)}"
535 }
danielk1977933bbd62007-03-17 07:22:42 +0000536
drhf8587402008-01-08 16:03:49 +0000537 if {[string length $tclbody]>0} {
538 puts $f $tclbody
539 }
540 if {[string length $sql]>0} {
541 puts $f "db eval {"
542 puts $f "$sql"
543 puts $f "}"
544 }
danielk1977aca790a2005-01-13 11:07:52 +0000545 close $f
546
547 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000548 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000549 } msg]
550 lappend r $msg
551}
552
danielk197732554c12005-01-22 03:39:39 +0000553# Usage: do_ioerr_test <test number> <options...>
554#
555# This proc is used to implement test cases that check that IO errors
556# are correctly handled. The first argument, <test number>, is an integer
557# used to name the tests executed by this proc. Options are as follows:
558#
559# -tclprep TCL script to run to prepare test.
560# -sqlprep SQL script to run to prepare test.
561# -tclbody TCL script to run with IO error simulation.
562# -sqlbody TCL script to run with IO error simulation.
563# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000564# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +0000565# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +0000566# -start Value of 'N' to begin with (default 1)
567#
568# -cksum Boolean. If true, test that the database does
569# not change during the execution of the test case.
570#
571proc do_ioerr_test {testname args} {
572
danielk197732554c12005-01-22 03:39:39 +0000573 set ::ioerropts(-start) 1
574 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000575 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +0000576 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +0000577 set ::ioerropts(-persist) 1
danielk197732554c12005-01-22 03:39:39 +0000578 array set ::ioerropts $args
579
580 set ::go 1
drh93aed5a2008-01-16 17:46:38 +0000581 #reset_prng_state
582 save_prng_state
danielk197732554c12005-01-22 03:39:39 +0000583 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +0000584 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +0000585 incr ::ioerropts(-count) -1
586 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +0000587
588 # Skip this IO error if it was specified with the "-exclude" option.
589 if {[info exists ::ioerropts(-exclude)]} {
590 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
591 }
drh93aed5a2008-01-16 17:46:38 +0000592 restore_prng_state
danielk197732554c12005-01-22 03:39:39 +0000593
594 # Delete the files test.db and test2.db, then execute the TCL and
595 # SQL (in that order) to prepare for the test case.
596 do_test $testname.$n.1 {
597 set ::sqlite_io_error_pending 0
598 catch {db close}
599 catch {file delete -force test.db}
600 catch {file delete -force test.db-journal}
601 catch {file delete -force test2.db}
602 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000603 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000604 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000605 if {[info exists ::ioerropts(-tclprep)]} {
606 eval $::ioerropts(-tclprep)
607 }
608 if {[info exists ::ioerropts(-sqlprep)]} {
609 execsql $::ioerropts(-sqlprep)
610 }
611 expr 0
612 } {0}
613
614 # Read the 'checksum' of the database.
615 if {$::ioerropts(-cksum)} {
616 set checksum [cksum]
617 }
drh93aed5a2008-01-16 17:46:38 +0000618
danielk197732554c12005-01-22 03:39:39 +0000619 # Set the Nth IO error to fail.
620 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +0000621 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +0000622 set ::sqlite_io_error_pending $n
623 }] $n
624
625 # Create a single TCL script from the TCL and SQL specified
626 # as the body of the test.
627 set ::ioerrorbody {}
628 if {[info exists ::ioerropts(-tclbody)]} {
629 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
630 }
631 if {[info exists ::ioerropts(-sqlbody)]} {
632 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
633 }
634
635 # Execute the TCL Script created in the above block. If
636 # there are at least N IO operations performed by SQLite as
637 # a result of the script, the Nth will fail.
638 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +0000639 set ::sqlite_io_error_hit 0
640 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +0000641 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +0000642 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +0000643 set rc [sqlite3_errcode $::DB]
644 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +0000645 # If we are in extended result code mode, make sure all of the
646 # IOERRs we get back really do have their extended code values.
647 # If an extended result code is returned, the sqlite3_errcode
648 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
649 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +0000650 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
651 return $rc
652 }
653 } else {
drh5f7b5bf2007-04-19 12:30:54 +0000654 # If we are not in extended result code mode, make sure no
655 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +0000656 if {[regexp {\+\d} $rc]} {
657 return $rc
658 }
659 }
drh93aed5a2008-01-16 17:46:38 +0000660 # The test repeats as long as $::go is non-zero. $::go starts out
661 # as 1. When a test runs to completion without hitting an I/O
662 # error, that means there is no point in continuing with this test
663 # case so set $::go to zero.
664 #
665 if {$::sqlite_io_error_pending>0} {
666 set ::go 0
667 set q 0
668 set ::sqlite_io_error_pending 0
669 } else {
670 set q 1
671 }
672
drhc9ac5ca2005-11-04 22:03:30 +0000673 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +0000674 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
675 set r 1
676 }
danielk1977f2fa8312006-01-24 13:09:33 +0000677 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +0000678
679 # One of two things must have happened. either
680 # 1. We never hit the IO error and the SQL returned OK
681 # 2. An IO error was hit and the SQL failed
682 #
drh93aed5a2008-01-16 17:46:38 +0000683 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +0000684 } {1}
685
686 # If an IO error occured, then the checksum of the database should
687 # be the same as before the script that caused the IO error was run.
drh1aa5af12008-03-07 19:51:14 +0000688 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk197732554c12005-01-22 03:39:39 +0000689 do_test $testname.$n.4 {
690 catch {db close}
drha34c62d2006-01-06 22:11:20 +0000691 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +0000692 cksum
693 } $checksum
694 }
695
drh1aa5af12008-03-07 19:51:14 +0000696 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +0000697 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +0000698 if {[info exists ::ioerropts(-cleanup)]} {
699 catch $::ioerropts(-cleanup)
700 }
danielk197732554c12005-01-22 03:39:39 +0000701 }
702 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +0000703 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +0000704 unset ::ioerropts
705}
706
drh93aed5a2008-01-16 17:46:38 +0000707# Return a checksum based on the contents of the main database associated
708# with connection $db
danielk197732554c12005-01-22 03:39:39 +0000709#
710proc cksum {{db db}} {
711 set txt [$db eval {
712 SELECT name, type, sql FROM sqlite_master order by name
713 }]\n
714 foreach tbl [$db eval {
715 SELECT name FROM sqlite_master WHERE type='table' order by name
716 }] {
717 append txt [$db eval "SELECT * FROM $tbl"]\n
718 }
719 foreach prag {default_synchronous default_cache_size} {
720 append txt $prag-[$db eval "PRAGMA $prag"]\n
721 }
722 set cksum [string length $txt]-[md5 $txt]
723 # puts $cksum-[file size test.db]
724 return $cksum
725}
726
drh93aed5a2008-01-16 17:46:38 +0000727# Generate a checksum based on the contents of the main and temp tables
728# database $db. If the checksum of two databases is the same, and the
729# integrity-check passes for both, the two databases are identical.
730#
drh1db639c2008-01-17 02:36:28 +0000731proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +0000732 set ret [list]
733 ifcapable tempdb {
734 set sql {
735 SELECT name FROM sqlite_master WHERE type = 'table' UNION
736 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
737 SELECT 'sqlite_master' UNION
738 SELECT 'sqlite_temp_master' ORDER BY 1
739 }
740 } else {
741 set sql {
742 SELECT name FROM sqlite_master WHERE type = 'table' UNION
743 SELECT 'sqlite_master' ORDER BY 1
744 }
745 }
746 set tbllist [$db eval $sql]
747 set txt {}
748 foreach tbl $tbllist {
749 append txt [$db eval "SELECT * FROM $tbl"]
750 }
751 foreach prag {default_cache_size} {
752 append txt $prag-[$db eval "PRAGMA $prag"]\n
753 }
754 # puts txt=$txt
755 return [md5 $txt]
756}
757
danielk197735754ac2008-03-21 17:29:37 +0000758proc memdebug_log_sql {{filename mallocs.sql}} {
759
danielk19776f332c12008-03-21 14:22:44 +0000760 set data [sqlite3_memdebug_log dump]
761 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +0000762 if {$nFrame < 0} { return "" }
763
danielk197735754ac2008-03-21 17:29:37 +0000764 set database temp
765
danielk19776f332c12008-03-21 14:22:44 +0000766 set tbl "CREATE TABLE ${database}.malloc(nCall, nByte"
767 for {set ii 1} {$ii <= $nFrame} {incr ii} {
768 append tbl ", f${ii}"
769 }
770 append tbl ");\n"
771
772 set sql ""
773 foreach e $data {
774 append sql "INSERT INTO ${database}.malloc VALUES([join $e ,]);\n"
775 foreach f [lrange $e 2 end] {
776 set frames($f) 1
777 }
778 }
779
780 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +0000781 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +0000782
783 foreach f [array names frames] {
784 set addr [format %x $f]
785 set cmd "addr2line -e [info nameofexec] $addr"
786 set line [eval exec $cmd]
787 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +0000788
789 set file [lindex [split $line :] 0]
790 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +0000791 }
792
danielk197735754ac2008-03-21 17:29:37 +0000793 foreach f [array names files] {
794 set contents ""
795 catch {
796 set fd [open $f]
797 set contents [read $fd]
798 close $fd
danielk19776f332c12008-03-21 14:22:44 +0000799 }
danielk197735754ac2008-03-21 17:29:37 +0000800 set contents [string map {' ''} $contents]
801 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +0000802 }
danielk19776f332c12008-03-21 14:22:44 +0000803
danielk197735754ac2008-03-21 17:29:37 +0000804 set fd [open $filename w]
805 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
806 close $fd
danielk19776f332c12008-03-21 14:22:44 +0000807}
drh93aed5a2008-01-16 17:46:38 +0000808
danielk197732554c12005-01-22 03:39:39 +0000809# Copy file $from into $to. This is used because some versions of
810# TCL for windows (notably the 8.4.1 binary package shipped with the
811# current mingw release) have a broken "file copy" command.
812#
813proc copy_file {from to} {
814 if {$::tcl_platform(platform)=="unix"} {
815 file copy -force $from $to
816 } else {
817 set f [open $from]
818 fconfigure $f -translation binary
819 set t [open $to w]
820 fconfigure $t -translation binary
821 puts -nonewline $t [read $f [file size $from]]
822 close $t
823 close $f
824 }
825}
826
danielk197745901d62004-11-10 15:27:38 +0000827# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
828# to non-zero, then set the global variable $AUTOVACUUM to 1.
829set AUTOVACUUM $sqlite_options(default_autovacuum)