blob: b812782e9a05d314fb3143867bb564e5b82f6aa1 [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#
drh8359d8c2008-03-29 11:00:54 +000014# $Id: tester.tcl,v 1.113 2008/03/29 11:00:55 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
64 set argv [lreplace $argv $i $i]
65 set tester_do_malloctrace 1
66 }
67}
68for {set i 0} {$i<[llength $argv]} {incr i} {
drh4a50aac2007-08-23 02:47:53 +000069 if {[regexp {^--backtrace=(\d+)$} [lindex $argv $i] all value]} {
70 sqlite3_memdebug_backtrace $value
71 set argv [lreplace $argv $i $i]
72 }
73}
74
drh6a288a32008-01-07 19:20:24 +000075#
76# Check the command-line arguments to set the maximum number of
77# errors tolerated before halting.
78#
79if {![info exists maxErr]} {
80 set maxErr 1000
81}
82for {set i 0} {$i<[llength $argv]} {incr i} {
83 if {[regexp {^--maxerror=(\d+)$} [lindex $argv $i] all maxErr]} {
84 set argv [lreplace $argv $i $i]
85 }
86}
87#puts "Max error = $maxErr"
88
drh4a50aac2007-08-23 02:47:53 +000089
drh9eb9e262004-02-11 02:18:05 +000090# Use the pager codec if it is available
91#
drhef4ac8f2004-06-19 00:16:31 +000092if {[sqlite3 -has-codec] && [info command sqlite_orig]==""} {
93 rename sqlite3 sqlite_orig
94 proc sqlite3 {args} {
drh9eb9e262004-02-11 02:18:05 +000095 if {[llength $args]==2 && [string index [lindex $args 0] 0]!="-"} {
96 lappend args -key {xyzzy}
97 }
98 uplevel 1 sqlite_orig $args
99 }
100}
101
102
drhbec2bf42000-05-29 23:48:22 +0000103# Create a test database
104#
drh254cba22001-09-20 01:44:42 +0000105catch {db close}
106file delete -force test.db
107file delete -force test.db-journal
drhdddca282006-01-03 00:33:50 +0000108sqlite3 db ./test.db
109set ::DB [sqlite3_connection_pointer db]
drhcd61c282002-03-06 22:01:34 +0000110if {[info exists ::SETUP_SQL]} {
111 db eval $::SETUP_SQL
112}
drhbec2bf42000-05-29 23:48:22 +0000113
114# Abort early if this script has been run before.
115#
116if {[info exists nTest]} return
117
118# Set the test counters to zero
119#
drh348784e2000-05-29 20:41:49 +0000120set nErr 0
121set nTest 0
drh767c2002000-10-19 14:10:08 +0000122set skip_test 0
drha1b351a2001-09-14 16:42:12 +0000123set failList {}
drhb62c3352006-11-23 09:39:16 +0000124if {![info exists speedTest]} {
125 set speedTest 0
126}
drh348784e2000-05-29 20:41:49 +0000127
128# Invoke the do_test procedure to run a single test
129#
130proc do_test {name cmd expected} {
drhd3d39e92004-05-20 22:16:29 +0000131 global argv nErr nTest skip_test maxErr
drh4a50aac2007-08-23 02:47:53 +0000132 sqlite3_memdebug_settitle $name
drh767c2002000-10-19 14:10:08 +0000133 if {$skip_test} {
134 set skip_test 0
135 return
136 }
137 if {[llength $argv]==0} {
drh348784e2000-05-29 20:41:49 +0000138 set go 1
139 } else {
140 set go 0
141 foreach pattern $argv {
142 if {[string match $pattern $name]} {
143 set go 1
144 break
145 }
146 }
147 }
148 if {!$go} return
149 incr nTest
drh5edc3122001-09-13 21:53:09 +0000150 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000151 flush stdout
152 if {[catch {uplevel #0 "$cmd;\n"} result]} {
153 puts "\nError: $result"
154 incr nErr
drha1b351a2001-09-14 16:42:12 +0000155 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000156 if {$nErr>$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000157 } elseif {[string compare $result $expected]} {
158 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
159 incr nErr
drha1b351a2001-09-14 16:42:12 +0000160 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000161 if {$nErr>=$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000162 } else {
163 puts " Ok"
164 }
drh6558db82007-04-13 03:23:21 +0000165 flush stdout
drh348784e2000-05-29 20:41:49 +0000166}
167
drhb62c3352006-11-23 09:39:16 +0000168# Run an SQL script.
169# Return the number of microseconds per statement.
170#
drh3590f152006-11-23 21:09:10 +0000171proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000172 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000173 flush stdout
174 set speed [time {sqlite3_exec_nr db $sql}]
175 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000176 if {$tm == 0} {
177 set rate [format %20s "many"]
178 } else {
179 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
180 }
drh3590f152006-11-23 21:09:10 +0000181 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000182 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000183 global total_time
184 set total_time [expr {$total_time+$tm}]
185}
drh45c236d2008-03-22 01:08:00 +0000186proc speed_trial_tcl {name numstmt units script} {
187 puts -nonewline [format {%-21.21s } $name...]
188 flush stdout
189 set speed [time {eval $script}]
190 set tm [lindex $speed 0]
191 if {$tm == 0} {
192 set rate [format %20s "many"]
193 } else {
194 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
195 }
196 set u2 $units/s
197 puts [format {%12d uS %s %s} $tm $rate $u2]
198 global total_time
199 set total_time [expr {$total_time+$tm}]
200}
drhdd924312007-03-31 22:34:16 +0000201proc speed_trial_init {name} {
202 global total_time
203 set total_time 0
204}
205proc speed_trial_summary {name} {
206 global total_time
207 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
drhb62c3352006-11-23 09:39:16 +0000208}
209
drh348784e2000-05-29 20:41:49 +0000210# Run this routine last
211#
212proc finish_test {} {
drha1b351a2001-09-14 16:42:12 +0000213 finalize_testing
214}
215proc finalize_testing {} {
drh80788d82006-09-02 14:50:23 +0000216 global nTest nErr sqlite_open_file_count
danielk19772e588c72005-12-09 14:25:08 +0000217
drh6e142f52000-06-08 13:36:40 +0000218 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000219 catch {db2 close}
220 catch {db3 close}
221
drh9bc54492007-10-23 14:49:59 +0000222 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000223 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000224 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000225 db close
drh984bfaa2008-03-19 16:08:53 +0000226 sqlite3_reset_auto_extension
drh3a7fb7c2007-08-10 16:41:08 +0000227 set heaplimit [sqlite3_soft_heap_limit]
228 if {$heaplimit!=$::soft_limit} {
229 puts "soft-heap-limit changed by this script\
230 from $::soft_limit to $heaplimit"
231 } elseif {$heaplimit!="" && $heaplimit>0} {
232 puts "soft-heap-limit set to $heaplimit"
233 }
234 sqlite3_soft_heap_limit 0
drhb4bc7052006-01-11 23:40:33 +0000235 incr nTest
drh348784e2000-05-29 20:41:49 +0000236 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000237 if {$nErr>0} {
238 puts "Failures on these tests: $::failList"
239 }
drh80788d82006-09-02 14:50:23 +0000240 if {$nErr>0 && ![working_64bit_int]} {
241 puts "******************************************************************"
242 puts "N.B.: The version of TCL that you used to build this test harness"
243 puts "is defective in that it does not support 64-bit integers. Some or"
244 puts "all of the test failures above might be a result from this defect"
245 puts "in your TCL build."
246 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000247 }
drh94e92032003-02-16 22:21:32 +0000248 if {$sqlite_open_file_count} {
249 puts "$sqlite_open_file_count files were left open"
250 incr nErr
251 }
drhf3a65f72007-08-22 20:18:21 +0000252 if {[sqlite3_memory_used]>0} {
253 puts "Unfreed memory: [sqlite3_memory_used] bytes"
254 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000255 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000256 puts "Writing unfreed memory log to \"./memleak.txt\""
257 sqlite3_memdebug_dump ./memleak.txt
258 }
drhf3a65f72007-08-22 20:18:21 +0000259 } else {
260 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000261 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000262 sqlite3_memdebug_dump ./memusage.txt
263 }
drhf3a65f72007-08-22 20:18:21 +0000264 }
drh91fd4d42008-01-19 20:11:25 +0000265 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
266 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000267 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
268 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
269 }
danielk197735754ac2008-03-21 17:29:37 +0000270 if {[info exists ::tester_do_malloctrace]} {
271 puts "Writing mallocs.sql..."
272 memdebug_log_sql
273 sqlite3_memdebug_log stop
274 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000275
276 if {[sqlite3_memory_used]>0} {
277 puts "Writing leaks.sql..."
278 sqlite3_memdebug_log sync
279 memdebug_log_sql leaks.sql
280 }
danielk197735754ac2008-03-21 17:29:37 +0000281 }
drhd9910fe2006-10-04 11:55:49 +0000282 foreach f [glob -nocomplain test.db-*-journal] {
283 file delete -force $f
284 }
285 foreach f [glob -nocomplain test.db-mj*] {
286 file delete -force $f
287 }
drhdb25e382001-03-15 18:21:22 +0000288 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000289}
290
drh348784e2000-05-29 20:41:49 +0000291# A procedure to execute SQL
292#
drhc4a3c772001-04-04 11:48:57 +0000293proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000294 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000295 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000296}
drh3aadb2e2000-05-31 17:59:25 +0000297
drhadbca9c2001-09-27 15:11:53 +0000298# Execute SQL and catch exceptions.
299#
300proc catchsql {sql {db db}} {
301 # puts "SQL = $sql"
302 set r [catch {$db eval $sql} msg]
303 lappend r $msg
304 return $r
305}
306
drh04096482001-11-09 22:41:44 +0000307# Do an VDBE code dump on the SQL given
308#
309proc explain {sql {db db}} {
310 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000311 puts "addr opcode p1 p2 p3 p4 p5 #"
312 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000313 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000314 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
315 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
316 ]
drh04096482001-11-09 22:41:44 +0000317 }
318}
319
drh3aadb2e2000-05-31 17:59:25 +0000320# Another procedure to execute SQL. This one includes the field
321# names in the returned list.
322#
323proc execsql2 {sql} {
324 set result {}
325 db eval $sql data {
326 foreach f $data(*) {
327 lappend result $f $data($f)
328 }
329 }
330 return $result
331}
drh17a68932001-01-31 13:28:08 +0000332
drhdde85d92003-03-01 19:45:34 +0000333# Use the non-callback API to execute multiple SQL statements
334#
335proc stepsql {dbptr sql} {
336 set sql [string trim $sql]
337 set r 0
338 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000339 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000340 return [list 1 $vm]
341 }
342 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000343# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
344# foreach v $VAL {lappend r $v}
345# }
346 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
347 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
348 lappend r [sqlite3_column_text $vm $i]
349 }
drhdde85d92003-03-01 19:45:34 +0000350 }
danielk1977106bb232004-05-21 10:08:53 +0000351 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000352 return [list 1 $errmsg]
353 }
354 }
355 return $r
356}
357
drh17a68932001-01-31 13:28:08 +0000358# Delete a file or directory
359#
360proc forcedelete {filename} {
361 if {[catch {file delete -force $filename}]} {
362 exec rm -rf $filename
363 }
364}
drh21504322002-06-25 13:16:02 +0000365
366# Do an integrity check of the entire database
367#
368proc integrity_check {name} {
drh27d258a2004-10-30 20:23:09 +0000369 ifcapable integrityck {
370 do_test $name {
371 execsql {PRAGMA integrity_check}
372 } {ok}
373 }
374}
375
danielk1977db4e8862008-01-16 08:24:46 +0000376proc fix_ifcapable_expr {expr} {
377 set ret ""
378 set state 0
379 for {set i 0} {$i < [string length $expr]} {incr i} {
380 set char [string range $expr $i $i]
381 set newstate [expr {[string is alnum $char] || $char eq "_"}]
382 if {$newstate && !$state} {
383 append ret {$::sqlite_options(}
384 }
385 if {!$newstate && $state} {
386 append ret )
387 }
388 append ret $char
389 set state $newstate
390 }
391 if {$state} {append ret )}
392 return $ret
393}
394
drh27d258a2004-10-30 20:23:09 +0000395# Evaluate a boolean expression of capabilities. If true, execute the
396# code. Omit the code if false.
397#
danielk19773e8c37e2005-01-21 03:12:14 +0000398proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +0000399 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
400 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +0000401 if ($e2) {
402 set c [catch {uplevel 1 $code} r]
403 } else {
404 set c [catch {uplevel 1 $elsecode} r]
405 }
406 return -code $c $r
drh21504322002-06-25 13:16:02 +0000407}
danielk197745901d62004-11-10 15:27:38 +0000408
danielk1977aca790a2005-01-13 11:07:52 +0000409# This proc execs a seperate process that crashes midway through executing
410# the SQL script $sql on database test.db.
411#
412# The crash occurs during a sync() of file $crashfile. When the crash
413# occurs a random subset of all unsynced writes made by the process are
414# written into the files on disk. Argument $crashdelay indicates the
415# number of file syncs to wait before crashing.
416#
417# The return value is a list of two elements. The first element is a
418# boolean, indicating whether or not the process actually crashed or
419# reported some other error. The second element in the returned list is the
420# error message. This is "child process exited abnormally" if the crash
421# occured.
422#
danielk1977f8940ae2007-08-23 11:07:10 +0000423# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +0000424#
425proc crashsql {args} {
danielk1977aca790a2005-01-13 11:07:52 +0000426 if {$::tcl_platform(platform)!="unix"} {
427 error "crashsql should only be used on unix"
428 }
danielk197759a33f92007-03-17 10:26:59 +0000429
430 set blocksize ""
431 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +0000432 set prngseed 0
drhf8587402008-01-08 16:03:49 +0000433 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +0000434 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +0000435 set dc ""
danielk197759a33f92007-03-17 10:26:59 +0000436 set sql [lindex $args end]
437
438 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
439 set z [lindex $args $ii]
440 set n [string length $z]
441 set z2 [lindex $args [expr $ii+1]]
442
443 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +0000444 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +0000445 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +0000446 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000447 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +0000448 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +0000449 else { error "Unrecognized option: $z" }
450 }
451
452 if {$crashfile eq ""} {
453 error "Compulsory option -file missing"
454 }
455
danielk1977aca790a2005-01-13 11:07:52 +0000456 set cfile [file join [pwd] $crashfile]
457
458 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +0000459 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +0000460 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
danielk1977933bbd62007-03-17 07:22:42 +0000461 puts $f "set sqlite_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +0000462 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +0000463
464 # This block sets the cache size of the main database to 10
465 # pages. This is done in case the build is configured to omit
466 # "PRAGMA cache_size".
467 puts $f {db eval {SELECT * FROM sqlite_master;}}
468 puts $f {set bt [btree_from_db db]}
469 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +0000470 if {$prngseed} {
471 set seed [expr {$prngseed%10007+1}]
472 # puts seed=$seed
473 puts $f "db eval {SELECT randomblob($seed)}"
474 }
danielk1977933bbd62007-03-17 07:22:42 +0000475
drhf8587402008-01-08 16:03:49 +0000476 if {[string length $tclbody]>0} {
477 puts $f $tclbody
478 }
479 if {[string length $sql]>0} {
480 puts $f "db eval {"
481 puts $f "$sql"
482 puts $f "}"
483 }
danielk1977aca790a2005-01-13 11:07:52 +0000484 close $f
485
486 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000487 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000488 } msg]
489 lappend r $msg
490}
491
danielk197732554c12005-01-22 03:39:39 +0000492# Usage: do_ioerr_test <test number> <options...>
493#
494# This proc is used to implement test cases that check that IO errors
495# are correctly handled. The first argument, <test number>, is an integer
496# used to name the tests executed by this proc. Options are as follows:
497#
498# -tclprep TCL script to run to prepare test.
499# -sqlprep SQL script to run to prepare test.
500# -tclbody TCL script to run with IO error simulation.
501# -sqlbody TCL script to run with IO error simulation.
502# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000503# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +0000504# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +0000505# -start Value of 'N' to begin with (default 1)
506#
507# -cksum Boolean. If true, test that the database does
508# not change during the execution of the test case.
509#
510proc do_ioerr_test {testname args} {
511
danielk197732554c12005-01-22 03:39:39 +0000512 set ::ioerropts(-start) 1
513 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000514 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +0000515 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +0000516 set ::ioerropts(-persist) 1
danielk197732554c12005-01-22 03:39:39 +0000517 array set ::ioerropts $args
518
519 set ::go 1
drh93aed5a2008-01-16 17:46:38 +0000520 #reset_prng_state
521 save_prng_state
danielk197732554c12005-01-22 03:39:39 +0000522 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +0000523 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +0000524 incr ::ioerropts(-count) -1
525 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +0000526
527 # Skip this IO error if it was specified with the "-exclude" option.
528 if {[info exists ::ioerropts(-exclude)]} {
529 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
530 }
drh93aed5a2008-01-16 17:46:38 +0000531 restore_prng_state
danielk197732554c12005-01-22 03:39:39 +0000532
533 # Delete the files test.db and test2.db, then execute the TCL and
534 # SQL (in that order) to prepare for the test case.
535 do_test $testname.$n.1 {
536 set ::sqlite_io_error_pending 0
537 catch {db close}
538 catch {file delete -force test.db}
539 catch {file delete -force test.db-journal}
540 catch {file delete -force test2.db}
541 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000542 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000543 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000544 if {[info exists ::ioerropts(-tclprep)]} {
545 eval $::ioerropts(-tclprep)
546 }
547 if {[info exists ::ioerropts(-sqlprep)]} {
548 execsql $::ioerropts(-sqlprep)
549 }
550 expr 0
551 } {0}
552
553 # Read the 'checksum' of the database.
554 if {$::ioerropts(-cksum)} {
555 set checksum [cksum]
556 }
drh93aed5a2008-01-16 17:46:38 +0000557
danielk197732554c12005-01-22 03:39:39 +0000558 # Set the Nth IO error to fail.
559 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +0000560 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +0000561 set ::sqlite_io_error_pending $n
562 }] $n
563
564 # Create a single TCL script from the TCL and SQL specified
565 # as the body of the test.
566 set ::ioerrorbody {}
567 if {[info exists ::ioerropts(-tclbody)]} {
568 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
569 }
570 if {[info exists ::ioerropts(-sqlbody)]} {
571 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
572 }
573
574 # Execute the TCL Script created in the above block. If
575 # there are at least N IO operations performed by SQLite as
576 # a result of the script, the Nth will fail.
577 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +0000578 set ::sqlite_io_error_hit 0
579 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +0000580 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +0000581 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +0000582 set rc [sqlite3_errcode $::DB]
583 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +0000584 # If we are in extended result code mode, make sure all of the
585 # IOERRs we get back really do have their extended code values.
586 # If an extended result code is returned, the sqlite3_errcode
587 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
588 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +0000589 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
590 return $rc
591 }
592 } else {
drh5f7b5bf2007-04-19 12:30:54 +0000593 # If we are not in extended result code mode, make sure no
594 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +0000595 if {[regexp {\+\d} $rc]} {
596 return $rc
597 }
598 }
drh93aed5a2008-01-16 17:46:38 +0000599 # The test repeats as long as $::go is non-zero. $::go starts out
600 # as 1. When a test runs to completion without hitting an I/O
601 # error, that means there is no point in continuing with this test
602 # case so set $::go to zero.
603 #
604 if {$::sqlite_io_error_pending>0} {
605 set ::go 0
606 set q 0
607 set ::sqlite_io_error_pending 0
608 } else {
609 set q 1
610 }
611
drhc9ac5ca2005-11-04 22:03:30 +0000612 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +0000613 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
614 set r 1
615 }
danielk1977f2fa8312006-01-24 13:09:33 +0000616 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +0000617
618 # One of two things must have happened. either
619 # 1. We never hit the IO error and the SQL returned OK
620 # 2. An IO error was hit and the SQL failed
621 #
drh93aed5a2008-01-16 17:46:38 +0000622 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +0000623 } {1}
624
625 # If an IO error occured, then the checksum of the database should
626 # be the same as before the script that caused the IO error was run.
drh1aa5af12008-03-07 19:51:14 +0000627 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk197732554c12005-01-22 03:39:39 +0000628 do_test $testname.$n.4 {
629 catch {db close}
drha34c62d2006-01-06 22:11:20 +0000630 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +0000631 cksum
632 } $checksum
633 }
634
drh1aa5af12008-03-07 19:51:14 +0000635 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +0000636 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +0000637 if {[info exists ::ioerropts(-cleanup)]} {
638 catch $::ioerropts(-cleanup)
639 }
danielk197732554c12005-01-22 03:39:39 +0000640 }
641 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +0000642 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +0000643 unset ::ioerropts
644}
645
drh93aed5a2008-01-16 17:46:38 +0000646# Return a checksum based on the contents of the main database associated
647# with connection $db
danielk197732554c12005-01-22 03:39:39 +0000648#
649proc cksum {{db db}} {
650 set txt [$db eval {
651 SELECT name, type, sql FROM sqlite_master order by name
652 }]\n
653 foreach tbl [$db eval {
654 SELECT name FROM sqlite_master WHERE type='table' order by name
655 }] {
656 append txt [$db eval "SELECT * FROM $tbl"]\n
657 }
658 foreach prag {default_synchronous default_cache_size} {
659 append txt $prag-[$db eval "PRAGMA $prag"]\n
660 }
661 set cksum [string length $txt]-[md5 $txt]
662 # puts $cksum-[file size test.db]
663 return $cksum
664}
665
drh93aed5a2008-01-16 17:46:38 +0000666# Generate a checksum based on the contents of the main and temp tables
667# database $db. If the checksum of two databases is the same, and the
668# integrity-check passes for both, the two databases are identical.
669#
drh1db639c2008-01-17 02:36:28 +0000670proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +0000671 set ret [list]
672 ifcapable tempdb {
673 set sql {
674 SELECT name FROM sqlite_master WHERE type = 'table' UNION
675 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
676 SELECT 'sqlite_master' UNION
677 SELECT 'sqlite_temp_master' ORDER BY 1
678 }
679 } else {
680 set sql {
681 SELECT name FROM sqlite_master WHERE type = 'table' UNION
682 SELECT 'sqlite_master' ORDER BY 1
683 }
684 }
685 set tbllist [$db eval $sql]
686 set txt {}
687 foreach tbl $tbllist {
688 append txt [$db eval "SELECT * FROM $tbl"]
689 }
690 foreach prag {default_cache_size} {
691 append txt $prag-[$db eval "PRAGMA $prag"]\n
692 }
693 # puts txt=$txt
694 return [md5 $txt]
695}
696
danielk197735754ac2008-03-21 17:29:37 +0000697proc memdebug_log_sql {{filename mallocs.sql}} {
698
danielk19776f332c12008-03-21 14:22:44 +0000699 set data [sqlite3_memdebug_log dump]
700 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +0000701 if {$nFrame < 0} { return "" }
702
danielk197735754ac2008-03-21 17:29:37 +0000703 set database temp
704
danielk19776f332c12008-03-21 14:22:44 +0000705 set tbl "CREATE TABLE ${database}.malloc(nCall, nByte"
706 for {set ii 1} {$ii <= $nFrame} {incr ii} {
707 append tbl ", f${ii}"
708 }
709 append tbl ");\n"
710
711 set sql ""
712 foreach e $data {
713 append sql "INSERT INTO ${database}.malloc VALUES([join $e ,]);\n"
714 foreach f [lrange $e 2 end] {
715 set frames($f) 1
716 }
717 }
718
719 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +0000720 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +0000721
722 foreach f [array names frames] {
723 set addr [format %x $f]
724 set cmd "addr2line -e [info nameofexec] $addr"
725 set line [eval exec $cmd]
726 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +0000727
728 set file [lindex [split $line :] 0]
729 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +0000730 }
731
danielk197735754ac2008-03-21 17:29:37 +0000732 foreach f [array names files] {
733 set contents ""
734 catch {
735 set fd [open $f]
736 set contents [read $fd]
737 close $fd
danielk19776f332c12008-03-21 14:22:44 +0000738 }
danielk197735754ac2008-03-21 17:29:37 +0000739 set contents [string map {' ''} $contents]
740 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +0000741 }
danielk19776f332c12008-03-21 14:22:44 +0000742
danielk197735754ac2008-03-21 17:29:37 +0000743 set fd [open $filename w]
744 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
745 close $fd
danielk19776f332c12008-03-21 14:22:44 +0000746}
drh93aed5a2008-01-16 17:46:38 +0000747
danielk197732554c12005-01-22 03:39:39 +0000748# Copy file $from into $to. This is used because some versions of
749# TCL for windows (notably the 8.4.1 binary package shipped with the
750# current mingw release) have a broken "file copy" command.
751#
752proc copy_file {from to} {
753 if {$::tcl_platform(platform)=="unix"} {
754 file copy -force $from $to
755 } else {
756 set f [open $from]
757 fconfigure $f -translation binary
758 set t [open $to w]
759 fconfigure $t -translation binary
760 puts -nonewline $t [read $f [file size $from]]
761 close $t
762 close $f
763 }
764}
765
danielk197745901d62004-11-10 15:27:38 +0000766# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
767# to non-zero, then set the global variable $AUTOVACUUM to 1.
768set AUTOVACUUM $sqlite_options(default_autovacuum)