blob: 09b86b00f06f0958c704cd3c030f36bddac7a9fd [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#
danielk1977dbdc4d42008-03-28 07:42:53 +000014# $Id: tester.tcl,v 1.111 2008/03/28 07:42:54 danielk1977 Exp $
drhfbc3eab2001-04-06 16:13:42 +000015
drh348784e2000-05-29 20:41:49 +000016
drh92febd92004-08-20 18:34:20 +000017set tcl_precision 15
drh49285702005-09-17 15:20:26 +000018set sqlite_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +000019
drh3a7fb7c2007-08-10 16:41:08 +000020#
21# Check the command-line arguments for a default soft-heap-limit.
22# Store this default value in the global variable ::soft_limit and
23# update the soft-heap-limit each time this script is run. In that
24# way if an individual test file changes the soft-heap-limit, it
25# will be reset at the start of the next test file.
26#
27if {![info exists soft_limit]} {
28 set soft_limit 0
29 for {set i 0} {$i<[llength $argv]} {incr i} {
30 if {[regexp {^--soft-heap-limit=(.+)$} [lindex $argv $i] all value]} {
31 if {$value!="off"} {
32 set soft_limit $value
33 }
34 set argv [lreplace $argv $i $i]
35 }
36 }
37}
38sqlite3_soft_heap_limit $soft_limit
39
drh4a50aac2007-08-23 02:47:53 +000040#
41# Check the command-line arguments to set the memory debugger
42# backtrace depth.
43#
44# See the sqlite3_memdebug_backtrace() function in mem2.c or
45# test_malloc.c for additional information.
46#
47for {set i 0} {$i<[llength $argv]} {incr i} {
danielk197735754ac2008-03-21 17:29:37 +000048 if {[lindex $argv $i] eq "--malloctrace"} {
49 set argv [lreplace $argv $i $i]
50 sqlite3_memdebug_backtrace 5
51 sqlite3_memdebug_log start
52 set argv [lreplace $argv $i $i]
53 set tester_do_malloctrace 1
54 }
55}
56for {set i 0} {$i<[llength $argv]} {incr i} {
drh4a50aac2007-08-23 02:47:53 +000057 if {[regexp {^--backtrace=(\d+)$} [lindex $argv $i] all value]} {
58 sqlite3_memdebug_backtrace $value
59 set argv [lreplace $argv $i $i]
60 }
61}
62
drh6a288a32008-01-07 19:20:24 +000063#
64# Check the command-line arguments to set the maximum number of
65# errors tolerated before halting.
66#
67if {![info exists maxErr]} {
68 set maxErr 1000
69}
70for {set i 0} {$i<[llength $argv]} {incr i} {
71 if {[regexp {^--maxerror=(\d+)$} [lindex $argv $i] all maxErr]} {
72 set argv [lreplace $argv $i $i]
73 }
74}
75#puts "Max error = $maxErr"
76
drh4a50aac2007-08-23 02:47:53 +000077
drh9eb9e262004-02-11 02:18:05 +000078# Use the pager codec if it is available
79#
drhef4ac8f2004-06-19 00:16:31 +000080if {[sqlite3 -has-codec] && [info command sqlite_orig]==""} {
81 rename sqlite3 sqlite_orig
82 proc sqlite3 {args} {
drh9eb9e262004-02-11 02:18:05 +000083 if {[llength $args]==2 && [string index [lindex $args 0] 0]!="-"} {
84 lappend args -key {xyzzy}
85 }
86 uplevel 1 sqlite_orig $args
87 }
88}
89
90
drhbec2bf42000-05-29 23:48:22 +000091# Create a test database
92#
drh254cba22001-09-20 01:44:42 +000093catch {db close}
94file delete -force test.db
95file delete -force test.db-journal
drhdddca282006-01-03 00:33:50 +000096sqlite3 db ./test.db
97set ::DB [sqlite3_connection_pointer db]
drhcd61c282002-03-06 22:01:34 +000098if {[info exists ::SETUP_SQL]} {
99 db eval $::SETUP_SQL
100}
drhbec2bf42000-05-29 23:48:22 +0000101
102# Abort early if this script has been run before.
103#
104if {[info exists nTest]} return
105
106# Set the test counters to zero
107#
drh348784e2000-05-29 20:41:49 +0000108set nErr 0
109set nTest 0
drh767c2002000-10-19 14:10:08 +0000110set skip_test 0
drha1b351a2001-09-14 16:42:12 +0000111set failList {}
drhb62c3352006-11-23 09:39:16 +0000112if {![info exists speedTest]} {
113 set speedTest 0
114}
drh348784e2000-05-29 20:41:49 +0000115
116# Invoke the do_test procedure to run a single test
117#
118proc do_test {name cmd expected} {
drhd3d39e92004-05-20 22:16:29 +0000119 global argv nErr nTest skip_test maxErr
drh4a50aac2007-08-23 02:47:53 +0000120 sqlite3_memdebug_settitle $name
drh767c2002000-10-19 14:10:08 +0000121 if {$skip_test} {
122 set skip_test 0
123 return
124 }
125 if {[llength $argv]==0} {
drh348784e2000-05-29 20:41:49 +0000126 set go 1
127 } else {
128 set go 0
129 foreach pattern $argv {
130 if {[string match $pattern $name]} {
131 set go 1
132 break
133 }
134 }
135 }
136 if {!$go} return
137 incr nTest
drh5edc3122001-09-13 21:53:09 +0000138 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000139 flush stdout
140 if {[catch {uplevel #0 "$cmd;\n"} result]} {
141 puts "\nError: $result"
142 incr nErr
drha1b351a2001-09-14 16:42:12 +0000143 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000144 if {$nErr>$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000145 } elseif {[string compare $result $expected]} {
146 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
147 incr nErr
drha1b351a2001-09-14 16:42:12 +0000148 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000149 if {$nErr>=$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000150 } else {
151 puts " Ok"
152 }
drh6558db82007-04-13 03:23:21 +0000153 flush stdout
drh348784e2000-05-29 20:41:49 +0000154}
155
drhb62c3352006-11-23 09:39:16 +0000156# Run an SQL script.
157# Return the number of microseconds per statement.
158#
drh3590f152006-11-23 21:09:10 +0000159proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000160 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000161 flush stdout
162 set speed [time {sqlite3_exec_nr db $sql}]
163 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000164 if {$tm == 0} {
165 set rate [format %20s "many"]
166 } else {
167 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
168 }
drh3590f152006-11-23 21:09:10 +0000169 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000170 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000171 global total_time
172 set total_time [expr {$total_time+$tm}]
173}
drh45c236d2008-03-22 01:08:00 +0000174proc speed_trial_tcl {name numstmt units script} {
175 puts -nonewline [format {%-21.21s } $name...]
176 flush stdout
177 set speed [time {eval $script}]
178 set tm [lindex $speed 0]
179 if {$tm == 0} {
180 set rate [format %20s "many"]
181 } else {
182 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
183 }
184 set u2 $units/s
185 puts [format {%12d uS %s %s} $tm $rate $u2]
186 global total_time
187 set total_time [expr {$total_time+$tm}]
188}
drhdd924312007-03-31 22:34:16 +0000189proc speed_trial_init {name} {
190 global total_time
191 set total_time 0
192}
193proc speed_trial_summary {name} {
194 global total_time
195 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
drhb62c3352006-11-23 09:39:16 +0000196}
197
drh348784e2000-05-29 20:41:49 +0000198# Run this routine last
199#
200proc finish_test {} {
drha1b351a2001-09-14 16:42:12 +0000201 finalize_testing
202}
203proc finalize_testing {} {
drh80788d82006-09-02 14:50:23 +0000204 global nTest nErr sqlite_open_file_count
danielk19772e588c72005-12-09 14:25:08 +0000205
drh6e142f52000-06-08 13:36:40 +0000206 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000207 catch {db2 close}
208 catch {db3 close}
209
drh9bc54492007-10-23 14:49:59 +0000210 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000211 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000212 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000213 db close
drh984bfaa2008-03-19 16:08:53 +0000214 sqlite3_reset_auto_extension
drh3a7fb7c2007-08-10 16:41:08 +0000215 set heaplimit [sqlite3_soft_heap_limit]
216 if {$heaplimit!=$::soft_limit} {
217 puts "soft-heap-limit changed by this script\
218 from $::soft_limit to $heaplimit"
219 } elseif {$heaplimit!="" && $heaplimit>0} {
220 puts "soft-heap-limit set to $heaplimit"
221 }
222 sqlite3_soft_heap_limit 0
drhb4bc7052006-01-11 23:40:33 +0000223 incr nTest
drh348784e2000-05-29 20:41:49 +0000224 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000225 if {$nErr>0} {
226 puts "Failures on these tests: $::failList"
227 }
drh80788d82006-09-02 14:50:23 +0000228 if {$nErr>0 && ![working_64bit_int]} {
229 puts "******************************************************************"
230 puts "N.B.: The version of TCL that you used to build this test harness"
231 puts "is defective in that it does not support 64-bit integers. Some or"
232 puts "all of the test failures above might be a result from this defect"
233 puts "in your TCL build."
234 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000235 }
drh94e92032003-02-16 22:21:32 +0000236 if {$sqlite_open_file_count} {
237 puts "$sqlite_open_file_count files were left open"
238 incr nErr
239 }
drhf3a65f72007-08-22 20:18:21 +0000240 if {[sqlite3_memory_used]>0} {
241 puts "Unfreed memory: [sqlite3_memory_used] bytes"
242 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000243 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000244 puts "Writing unfreed memory log to \"./memleak.txt\""
245 sqlite3_memdebug_dump ./memleak.txt
246 }
drhf3a65f72007-08-22 20:18:21 +0000247 } else {
248 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000249 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000250 sqlite3_memdebug_dump ./memusage.txt
251 }
drhf3a65f72007-08-22 20:18:21 +0000252 }
drh91fd4d42008-01-19 20:11:25 +0000253 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
254 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000255 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
256 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
257 }
danielk197735754ac2008-03-21 17:29:37 +0000258 if {[info exists ::tester_do_malloctrace]} {
259 puts "Writing mallocs.sql..."
260 memdebug_log_sql
261 sqlite3_memdebug_log stop
262 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000263
264 if {[sqlite3_memory_used]>0} {
265 puts "Writing leaks.sql..."
266 sqlite3_memdebug_log sync
267 memdebug_log_sql leaks.sql
268 }
danielk197735754ac2008-03-21 17:29:37 +0000269 }
drhd9910fe2006-10-04 11:55:49 +0000270 foreach f [glob -nocomplain test.db-*-journal] {
271 file delete -force $f
272 }
273 foreach f [glob -nocomplain test.db-mj*] {
274 file delete -force $f
275 }
drhdb25e382001-03-15 18:21:22 +0000276 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000277}
278
drh348784e2000-05-29 20:41:49 +0000279# A procedure to execute SQL
280#
drhc4a3c772001-04-04 11:48:57 +0000281proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000282 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000283 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000284}
drh3aadb2e2000-05-31 17:59:25 +0000285
drhadbca9c2001-09-27 15:11:53 +0000286# Execute SQL and catch exceptions.
287#
288proc catchsql {sql {db db}} {
289 # puts "SQL = $sql"
290 set r [catch {$db eval $sql} msg]
291 lappend r $msg
292 return $r
293}
294
drh04096482001-11-09 22:41:44 +0000295# Do an VDBE code dump on the SQL given
296#
297proc explain {sql {db db}} {
298 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000299 puts "addr opcode p1 p2 p3 p4 p5 #"
300 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000301 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000302 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
303 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
304 ]
drh04096482001-11-09 22:41:44 +0000305 }
306}
307
drh3aadb2e2000-05-31 17:59:25 +0000308# Another procedure to execute SQL. This one includes the field
309# names in the returned list.
310#
311proc execsql2 {sql} {
312 set result {}
313 db eval $sql data {
314 foreach f $data(*) {
315 lappend result $f $data($f)
316 }
317 }
318 return $result
319}
drh17a68932001-01-31 13:28:08 +0000320
drhdde85d92003-03-01 19:45:34 +0000321# Use the non-callback API to execute multiple SQL statements
322#
323proc stepsql {dbptr sql} {
324 set sql [string trim $sql]
325 set r 0
326 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000327 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000328 return [list 1 $vm]
329 }
330 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000331# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
332# foreach v $VAL {lappend r $v}
333# }
334 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
335 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
336 lappend r [sqlite3_column_text $vm $i]
337 }
drhdde85d92003-03-01 19:45:34 +0000338 }
danielk1977106bb232004-05-21 10:08:53 +0000339 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000340 return [list 1 $errmsg]
341 }
342 }
343 return $r
344}
345
drh17a68932001-01-31 13:28:08 +0000346# Delete a file or directory
347#
348proc forcedelete {filename} {
349 if {[catch {file delete -force $filename}]} {
350 exec rm -rf $filename
351 }
352}
drh21504322002-06-25 13:16:02 +0000353
354# Do an integrity check of the entire database
355#
356proc integrity_check {name} {
drh27d258a2004-10-30 20:23:09 +0000357 ifcapable integrityck {
358 do_test $name {
359 execsql {PRAGMA integrity_check}
360 } {ok}
361 }
362}
363
danielk1977db4e8862008-01-16 08:24:46 +0000364proc fix_ifcapable_expr {expr} {
365 set ret ""
366 set state 0
367 for {set i 0} {$i < [string length $expr]} {incr i} {
368 set char [string range $expr $i $i]
369 set newstate [expr {[string is alnum $char] || $char eq "_"}]
370 if {$newstate && !$state} {
371 append ret {$::sqlite_options(}
372 }
373 if {!$newstate && $state} {
374 append ret )
375 }
376 append ret $char
377 set state $newstate
378 }
379 if {$state} {append ret )}
380 return $ret
381}
382
drh27d258a2004-10-30 20:23:09 +0000383# Evaluate a boolean expression of capabilities. If true, execute the
384# code. Omit the code if false.
385#
danielk19773e8c37e2005-01-21 03:12:14 +0000386proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +0000387 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
388 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +0000389 if ($e2) {
390 set c [catch {uplevel 1 $code} r]
391 } else {
392 set c [catch {uplevel 1 $elsecode} r]
393 }
394 return -code $c $r
drh21504322002-06-25 13:16:02 +0000395}
danielk197745901d62004-11-10 15:27:38 +0000396
danielk1977aca790a2005-01-13 11:07:52 +0000397# This proc execs a seperate process that crashes midway through executing
398# the SQL script $sql on database test.db.
399#
400# The crash occurs during a sync() of file $crashfile. When the crash
401# occurs a random subset of all unsynced writes made by the process are
402# written into the files on disk. Argument $crashdelay indicates the
403# number of file syncs to wait before crashing.
404#
405# The return value is a list of two elements. The first element is a
406# boolean, indicating whether or not the process actually crashed or
407# reported some other error. The second element in the returned list is the
408# error message. This is "child process exited abnormally" if the crash
409# occured.
410#
danielk1977f8940ae2007-08-23 11:07:10 +0000411# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +0000412#
413proc crashsql {args} {
danielk1977aca790a2005-01-13 11:07:52 +0000414 if {$::tcl_platform(platform)!="unix"} {
415 error "crashsql should only be used on unix"
416 }
danielk197759a33f92007-03-17 10:26:59 +0000417
418 set blocksize ""
419 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +0000420 set prngseed 0
drhf8587402008-01-08 16:03:49 +0000421 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +0000422 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +0000423 set dc ""
danielk197759a33f92007-03-17 10:26:59 +0000424 set sql [lindex $args end]
425
426 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
427 set z [lindex $args $ii]
428 set n [string length $z]
429 set z2 [lindex $args [expr $ii+1]]
430
431 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +0000432 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +0000433 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +0000434 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000435 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +0000436 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +0000437 else { error "Unrecognized option: $z" }
438 }
439
440 if {$crashfile eq ""} {
441 error "Compulsory option -file missing"
442 }
443
danielk1977aca790a2005-01-13 11:07:52 +0000444 set cfile [file join [pwd] $crashfile]
445
446 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +0000447 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +0000448 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
danielk1977933bbd62007-03-17 07:22:42 +0000449 puts $f "set sqlite_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +0000450 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +0000451
452 # This block sets the cache size of the main database to 10
453 # pages. This is done in case the build is configured to omit
454 # "PRAGMA cache_size".
455 puts $f {db eval {SELECT * FROM sqlite_master;}}
456 puts $f {set bt [btree_from_db db]}
457 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +0000458 if {$prngseed} {
459 set seed [expr {$prngseed%10007+1}]
460 # puts seed=$seed
461 puts $f "db eval {SELECT randomblob($seed)}"
462 }
danielk1977933bbd62007-03-17 07:22:42 +0000463
drhf8587402008-01-08 16:03:49 +0000464 if {[string length $tclbody]>0} {
465 puts $f $tclbody
466 }
467 if {[string length $sql]>0} {
468 puts $f "db eval {"
469 puts $f "$sql"
470 puts $f "}"
471 }
danielk1977aca790a2005-01-13 11:07:52 +0000472 close $f
473
474 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000475 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000476 } msg]
477 lappend r $msg
478}
479
danielk197732554c12005-01-22 03:39:39 +0000480# Usage: do_ioerr_test <test number> <options...>
481#
482# This proc is used to implement test cases that check that IO errors
483# are correctly handled. The first argument, <test number>, is an integer
484# used to name the tests executed by this proc. Options are as follows:
485#
486# -tclprep TCL script to run to prepare test.
487# -sqlprep SQL script to run to prepare test.
488# -tclbody TCL script to run with IO error simulation.
489# -sqlbody TCL script to run with IO error simulation.
490# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000491# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +0000492# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +0000493# -start Value of 'N' to begin with (default 1)
494#
495# -cksum Boolean. If true, test that the database does
496# not change during the execution of the test case.
497#
498proc do_ioerr_test {testname args} {
499
danielk197732554c12005-01-22 03:39:39 +0000500 set ::ioerropts(-start) 1
501 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000502 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +0000503 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +0000504 set ::ioerropts(-persist) 1
danielk197732554c12005-01-22 03:39:39 +0000505 array set ::ioerropts $args
506
507 set ::go 1
drh93aed5a2008-01-16 17:46:38 +0000508 #reset_prng_state
509 save_prng_state
danielk197732554c12005-01-22 03:39:39 +0000510 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +0000511 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +0000512 incr ::ioerropts(-count) -1
513 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +0000514
515 # Skip this IO error if it was specified with the "-exclude" option.
516 if {[info exists ::ioerropts(-exclude)]} {
517 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
518 }
drh93aed5a2008-01-16 17:46:38 +0000519 restore_prng_state
danielk197732554c12005-01-22 03:39:39 +0000520
521 # Delete the files test.db and test2.db, then execute the TCL and
522 # SQL (in that order) to prepare for the test case.
523 do_test $testname.$n.1 {
524 set ::sqlite_io_error_pending 0
525 catch {db close}
526 catch {file delete -force test.db}
527 catch {file delete -force test.db-journal}
528 catch {file delete -force test2.db}
529 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000530 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000531 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000532 if {[info exists ::ioerropts(-tclprep)]} {
533 eval $::ioerropts(-tclprep)
534 }
535 if {[info exists ::ioerropts(-sqlprep)]} {
536 execsql $::ioerropts(-sqlprep)
537 }
538 expr 0
539 } {0}
540
541 # Read the 'checksum' of the database.
542 if {$::ioerropts(-cksum)} {
543 set checksum [cksum]
544 }
drh93aed5a2008-01-16 17:46:38 +0000545
danielk197732554c12005-01-22 03:39:39 +0000546 # Set the Nth IO error to fail.
547 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +0000548 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +0000549 set ::sqlite_io_error_pending $n
550 }] $n
551
552 # Create a single TCL script from the TCL and SQL specified
553 # as the body of the test.
554 set ::ioerrorbody {}
555 if {[info exists ::ioerropts(-tclbody)]} {
556 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
557 }
558 if {[info exists ::ioerropts(-sqlbody)]} {
559 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
560 }
561
562 # Execute the TCL Script created in the above block. If
563 # there are at least N IO operations performed by SQLite as
564 # a result of the script, the Nth will fail.
565 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +0000566 set ::sqlite_io_error_hit 0
567 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +0000568 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +0000569 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +0000570 set rc [sqlite3_errcode $::DB]
571 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +0000572 # If we are in extended result code mode, make sure all of the
573 # IOERRs we get back really do have their extended code values.
574 # If an extended result code is returned, the sqlite3_errcode
575 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
576 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +0000577 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
578 return $rc
579 }
580 } else {
drh5f7b5bf2007-04-19 12:30:54 +0000581 # If we are not in extended result code mode, make sure no
582 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +0000583 if {[regexp {\+\d} $rc]} {
584 return $rc
585 }
586 }
drh93aed5a2008-01-16 17:46:38 +0000587 # The test repeats as long as $::go is non-zero. $::go starts out
588 # as 1. When a test runs to completion without hitting an I/O
589 # error, that means there is no point in continuing with this test
590 # case so set $::go to zero.
591 #
592 if {$::sqlite_io_error_pending>0} {
593 set ::go 0
594 set q 0
595 set ::sqlite_io_error_pending 0
596 } else {
597 set q 1
598 }
599
drhc9ac5ca2005-11-04 22:03:30 +0000600 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +0000601 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
602 set r 1
603 }
danielk1977f2fa8312006-01-24 13:09:33 +0000604 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +0000605
606 # One of two things must have happened. either
607 # 1. We never hit the IO error and the SQL returned OK
608 # 2. An IO error was hit and the SQL failed
609 #
drh93aed5a2008-01-16 17:46:38 +0000610 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +0000611 } {1}
612
613 # If an IO error occured, then the checksum of the database should
614 # be the same as before the script that caused the IO error was run.
drh1aa5af12008-03-07 19:51:14 +0000615 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk197732554c12005-01-22 03:39:39 +0000616 do_test $testname.$n.4 {
617 catch {db close}
drha34c62d2006-01-06 22:11:20 +0000618 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +0000619 cksum
620 } $checksum
621 }
622
drh1aa5af12008-03-07 19:51:14 +0000623 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +0000624 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +0000625 if {[info exists ::ioerropts(-cleanup)]} {
626 catch $::ioerropts(-cleanup)
627 }
danielk197732554c12005-01-22 03:39:39 +0000628 }
629 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +0000630 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +0000631 unset ::ioerropts
632}
633
drh93aed5a2008-01-16 17:46:38 +0000634# Return a checksum based on the contents of the main database associated
635# with connection $db
danielk197732554c12005-01-22 03:39:39 +0000636#
637proc cksum {{db db}} {
638 set txt [$db eval {
639 SELECT name, type, sql FROM sqlite_master order by name
640 }]\n
641 foreach tbl [$db eval {
642 SELECT name FROM sqlite_master WHERE type='table' order by name
643 }] {
644 append txt [$db eval "SELECT * FROM $tbl"]\n
645 }
646 foreach prag {default_synchronous default_cache_size} {
647 append txt $prag-[$db eval "PRAGMA $prag"]\n
648 }
649 set cksum [string length $txt]-[md5 $txt]
650 # puts $cksum-[file size test.db]
651 return $cksum
652}
653
drh93aed5a2008-01-16 17:46:38 +0000654# Generate a checksum based on the contents of the main and temp tables
655# database $db. If the checksum of two databases is the same, and the
656# integrity-check passes for both, the two databases are identical.
657#
drh1db639c2008-01-17 02:36:28 +0000658proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +0000659 set ret [list]
660 ifcapable tempdb {
661 set sql {
662 SELECT name FROM sqlite_master WHERE type = 'table' UNION
663 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
664 SELECT 'sqlite_master' UNION
665 SELECT 'sqlite_temp_master' ORDER BY 1
666 }
667 } else {
668 set sql {
669 SELECT name FROM sqlite_master WHERE type = 'table' UNION
670 SELECT 'sqlite_master' ORDER BY 1
671 }
672 }
673 set tbllist [$db eval $sql]
674 set txt {}
675 foreach tbl $tbllist {
676 append txt [$db eval "SELECT * FROM $tbl"]
677 }
678 foreach prag {default_cache_size} {
679 append txt $prag-[$db eval "PRAGMA $prag"]\n
680 }
681 # puts txt=$txt
682 return [md5 $txt]
683}
684
danielk197735754ac2008-03-21 17:29:37 +0000685proc memdebug_log_sql {{filename mallocs.sql}} {
686
danielk19776f332c12008-03-21 14:22:44 +0000687 set data [sqlite3_memdebug_log dump]
688 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +0000689 if {$nFrame < 0} { return "" }
690
danielk197735754ac2008-03-21 17:29:37 +0000691 set database temp
692
danielk19776f332c12008-03-21 14:22:44 +0000693 set tbl "CREATE TABLE ${database}.malloc(nCall, nByte"
694 for {set ii 1} {$ii <= $nFrame} {incr ii} {
695 append tbl ", f${ii}"
696 }
697 append tbl ");\n"
698
699 set sql ""
700 foreach e $data {
701 append sql "INSERT INTO ${database}.malloc VALUES([join $e ,]);\n"
702 foreach f [lrange $e 2 end] {
703 set frames($f) 1
704 }
705 }
706
707 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +0000708 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +0000709
710 foreach f [array names frames] {
711 set addr [format %x $f]
712 set cmd "addr2line -e [info nameofexec] $addr"
713 set line [eval exec $cmd]
714 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +0000715
716 set file [lindex [split $line :] 0]
717 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +0000718 }
719
danielk197735754ac2008-03-21 17:29:37 +0000720 foreach f [array names files] {
721 set contents ""
722 catch {
723 set fd [open $f]
724 set contents [read $fd]
725 close $fd
danielk19776f332c12008-03-21 14:22:44 +0000726 }
danielk197735754ac2008-03-21 17:29:37 +0000727 set contents [string map {' ''} $contents]
728 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +0000729 }
danielk19776f332c12008-03-21 14:22:44 +0000730
danielk197735754ac2008-03-21 17:29:37 +0000731 set fd [open $filename w]
732 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
733 close $fd
danielk19776f332c12008-03-21 14:22:44 +0000734}
drh93aed5a2008-01-16 17:46:38 +0000735
danielk197732554c12005-01-22 03:39:39 +0000736# Copy file $from into $to. This is used because some versions of
737# TCL for windows (notably the 8.4.1 binary package shipped with the
738# current mingw release) have a broken "file copy" command.
739#
740proc copy_file {from to} {
741 if {$::tcl_platform(platform)=="unix"} {
742 file copy -force $from $to
743 } else {
744 set f [open $from]
745 fconfigure $f -translation binary
746 set t [open $to w]
747 fconfigure $t -translation binary
748 puts -nonewline $t [read $f [file size $from]]
749 close $t
750 close $f
751 }
752}
753
danielk197745901d62004-11-10 15:27:38 +0000754# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
755# to non-zero, then set the global variable $AUTOVACUUM to 1.
756set AUTOVACUUM $sqlite_options(default_autovacuum)