blob: e0c2a50c9aaedc93b51996296b933ccf04b0aadd [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#
danielk1977a7a8e142008-02-13 18:25:27 +000014# $Id: tester.tcl,v 1.104 2008/02/13 18:25:27 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} {
48 if {[regexp {^--backtrace=(\d+)$} [lindex $argv $i] all value]} {
49 sqlite3_memdebug_backtrace $value
50 set argv [lreplace $argv $i $i]
51 }
52}
53
drh6a288a32008-01-07 19:20:24 +000054#
55# Check the command-line arguments to set the maximum number of
56# errors tolerated before halting.
57#
58if {![info exists maxErr]} {
59 set maxErr 1000
60}
61for {set i 0} {$i<[llength $argv]} {incr i} {
62 if {[regexp {^--maxerror=(\d+)$} [lindex $argv $i] all maxErr]} {
63 set argv [lreplace $argv $i $i]
64 }
65}
66#puts "Max error = $maxErr"
67
drh4a50aac2007-08-23 02:47:53 +000068
drh9eb9e262004-02-11 02:18:05 +000069# Use the pager codec if it is available
70#
drhef4ac8f2004-06-19 00:16:31 +000071if {[sqlite3 -has-codec] && [info command sqlite_orig]==""} {
72 rename sqlite3 sqlite_orig
73 proc sqlite3 {args} {
drh9eb9e262004-02-11 02:18:05 +000074 if {[llength $args]==2 && [string index [lindex $args 0] 0]!="-"} {
75 lappend args -key {xyzzy}
76 }
77 uplevel 1 sqlite_orig $args
78 }
79}
80
81
drhbec2bf42000-05-29 23:48:22 +000082# Create a test database
83#
drh254cba22001-09-20 01:44:42 +000084catch {db close}
85file delete -force test.db
86file delete -force test.db-journal
drhdddca282006-01-03 00:33:50 +000087sqlite3 db ./test.db
88set ::DB [sqlite3_connection_pointer db]
drhcd61c282002-03-06 22:01:34 +000089if {[info exists ::SETUP_SQL]} {
90 db eval $::SETUP_SQL
91}
drhbec2bf42000-05-29 23:48:22 +000092
93# Abort early if this script has been run before.
94#
95if {[info exists nTest]} return
96
97# Set the test counters to zero
98#
drh348784e2000-05-29 20:41:49 +000099set nErr 0
100set nTest 0
drh767c2002000-10-19 14:10:08 +0000101set skip_test 0
drha1b351a2001-09-14 16:42:12 +0000102set failList {}
drhb62c3352006-11-23 09:39:16 +0000103if {![info exists speedTest]} {
104 set speedTest 0
105}
drh348784e2000-05-29 20:41:49 +0000106
107# Invoke the do_test procedure to run a single test
108#
109proc do_test {name cmd expected} {
drhd3d39e92004-05-20 22:16:29 +0000110 global argv nErr nTest skip_test maxErr
drh4a50aac2007-08-23 02:47:53 +0000111 sqlite3_memdebug_settitle $name
drh767c2002000-10-19 14:10:08 +0000112 if {$skip_test} {
113 set skip_test 0
114 return
115 }
116 if {[llength $argv]==0} {
drh348784e2000-05-29 20:41:49 +0000117 set go 1
118 } else {
119 set go 0
120 foreach pattern $argv {
121 if {[string match $pattern $name]} {
122 set go 1
123 break
124 }
125 }
126 }
127 if {!$go} return
128 incr nTest
drh5edc3122001-09-13 21:53:09 +0000129 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000130 flush stdout
131 if {[catch {uplevel #0 "$cmd;\n"} result]} {
132 puts "\nError: $result"
133 incr nErr
drha1b351a2001-09-14 16:42:12 +0000134 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000135 if {$nErr>$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000136 } elseif {[string compare $result $expected]} {
137 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
138 incr nErr
drha1b351a2001-09-14 16:42:12 +0000139 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000140 if {$nErr>=$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000141 } else {
142 puts " Ok"
143 }
drh6558db82007-04-13 03:23:21 +0000144 flush stdout
drh348784e2000-05-29 20:41:49 +0000145}
146
drhb62c3352006-11-23 09:39:16 +0000147# Run an SQL script.
148# Return the number of microseconds per statement.
149#
drh3590f152006-11-23 21:09:10 +0000150proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000151 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000152 flush stdout
153 set speed [time {sqlite3_exec_nr db $sql}]
154 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000155 if {$tm == 0} {
156 set rate [format %20s "many"]
157 } else {
158 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
159 }
drh3590f152006-11-23 21:09:10 +0000160 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000161 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000162 global total_time
163 set total_time [expr {$total_time+$tm}]
164}
165proc speed_trial_init {name} {
166 global total_time
167 set total_time 0
168}
169proc speed_trial_summary {name} {
170 global total_time
171 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
drhb62c3352006-11-23 09:39:16 +0000172}
173
drh348784e2000-05-29 20:41:49 +0000174# Run this routine last
175#
176proc finish_test {} {
drha1b351a2001-09-14 16:42:12 +0000177 finalize_testing
178}
179proc finalize_testing {} {
drh80788d82006-09-02 14:50:23 +0000180 global nTest nErr sqlite_open_file_count
danielk19772e588c72005-12-09 14:25:08 +0000181
drh6e142f52000-06-08 13:36:40 +0000182 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000183 catch {db2 close}
184 catch {db3 close}
185
drh9bc54492007-10-23 14:49:59 +0000186 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000187 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000188 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000189 db close
drh3a7fb7c2007-08-10 16:41:08 +0000190 set heaplimit [sqlite3_soft_heap_limit]
191 if {$heaplimit!=$::soft_limit} {
192 puts "soft-heap-limit changed by this script\
193 from $::soft_limit to $heaplimit"
194 } elseif {$heaplimit!="" && $heaplimit>0} {
195 puts "soft-heap-limit set to $heaplimit"
196 }
197 sqlite3_soft_heap_limit 0
drhb4bc7052006-01-11 23:40:33 +0000198 incr nTest
drh348784e2000-05-29 20:41:49 +0000199 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000200 if {$nErr>0} {
201 puts "Failures on these tests: $::failList"
202 }
drh80788d82006-09-02 14:50:23 +0000203 if {$nErr>0 && ![working_64bit_int]} {
204 puts "******************************************************************"
205 puts "N.B.: The version of TCL that you used to build this test harness"
206 puts "is defective in that it does not support 64-bit integers. Some or"
207 puts "all of the test failures above might be a result from this defect"
208 puts "in your TCL build."
209 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000210 }
drh94e92032003-02-16 22:21:32 +0000211 if {$sqlite_open_file_count} {
212 puts "$sqlite_open_file_count files were left open"
213 incr nErr
214 }
drhf3a65f72007-08-22 20:18:21 +0000215 if {[sqlite3_memory_used]>0} {
216 puts "Unfreed memory: [sqlite3_memory_used] bytes"
217 incr nErr
drha4e5d582007-10-20 15:41:57 +0000218 ifcapable memdebug||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000219 puts "Writing unfreed memory log to \"./memleak.txt\""
220 sqlite3_memdebug_dump ./memleak.txt
221 }
drhf3a65f72007-08-22 20:18:21 +0000222 } else {
223 puts "All memory allocations freed - no leaks"
drhd2bb3272007-10-15 19:34:32 +0000224 ifcapable memdebug {
225 sqlite3_memdebug_dump ./memusage.txt
226 }
drhf3a65f72007-08-22 20:18:21 +0000227 }
drh91fd4d42008-01-19 20:11:25 +0000228 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
229 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000230 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
231 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
232 }
drhd9910fe2006-10-04 11:55:49 +0000233 foreach f [glob -nocomplain test.db-*-journal] {
234 file delete -force $f
235 }
236 foreach f [glob -nocomplain test.db-mj*] {
237 file delete -force $f
238 }
drhdb25e382001-03-15 18:21:22 +0000239 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000240}
241
drh348784e2000-05-29 20:41:49 +0000242# A procedure to execute SQL
243#
drhc4a3c772001-04-04 11:48:57 +0000244proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000245 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000246 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000247}
drh3aadb2e2000-05-31 17:59:25 +0000248
drhadbca9c2001-09-27 15:11:53 +0000249# Execute SQL and catch exceptions.
250#
251proc catchsql {sql {db db}} {
252 # puts "SQL = $sql"
253 set r [catch {$db eval $sql} msg]
254 lappend r $msg
255 return $r
256}
257
drh04096482001-11-09 22:41:44 +0000258# Do an VDBE code dump on the SQL given
259#
260proc explain {sql {db db}} {
261 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000262 puts "addr opcode p1 p2 p3 p4 p5 #"
263 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000264 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000265 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
266 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
267 ]
drh04096482001-11-09 22:41:44 +0000268 }
269}
270
drh3aadb2e2000-05-31 17:59:25 +0000271# Another procedure to execute SQL. This one includes the field
272# names in the returned list.
273#
274proc execsql2 {sql} {
275 set result {}
276 db eval $sql data {
277 foreach f $data(*) {
278 lappend result $f $data($f)
279 }
280 }
281 return $result
282}
drh17a68932001-01-31 13:28:08 +0000283
drhdde85d92003-03-01 19:45:34 +0000284# Use the non-callback API to execute multiple SQL statements
285#
286proc stepsql {dbptr sql} {
287 set sql [string trim $sql]
288 set r 0
289 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000290 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000291 return [list 1 $vm]
292 }
293 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000294# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
295# foreach v $VAL {lappend r $v}
296# }
297 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
298 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
299 lappend r [sqlite3_column_text $vm $i]
300 }
drhdde85d92003-03-01 19:45:34 +0000301 }
danielk1977106bb232004-05-21 10:08:53 +0000302 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000303 return [list 1 $errmsg]
304 }
305 }
306 return $r
307}
308
drh17a68932001-01-31 13:28:08 +0000309# Delete a file or directory
310#
311proc forcedelete {filename} {
312 if {[catch {file delete -force $filename}]} {
313 exec rm -rf $filename
314 }
315}
drh21504322002-06-25 13:16:02 +0000316
317# Do an integrity check of the entire database
318#
319proc integrity_check {name} {
drh27d258a2004-10-30 20:23:09 +0000320 ifcapable integrityck {
321 do_test $name {
322 execsql {PRAGMA integrity_check}
323 } {ok}
324 }
325}
326
danielk1977db4e8862008-01-16 08:24:46 +0000327proc fix_ifcapable_expr {expr} {
328 set ret ""
329 set state 0
330 for {set i 0} {$i < [string length $expr]} {incr i} {
331 set char [string range $expr $i $i]
332 set newstate [expr {[string is alnum $char] || $char eq "_"}]
333 if {$newstate && !$state} {
334 append ret {$::sqlite_options(}
335 }
336 if {!$newstate && $state} {
337 append ret )
338 }
339 append ret $char
340 set state $newstate
341 }
342 if {$state} {append ret )}
343 return $ret
344}
345
drh27d258a2004-10-30 20:23:09 +0000346# Evaluate a boolean expression of capabilities. If true, execute the
347# code. Omit the code if false.
348#
danielk19773e8c37e2005-01-21 03:12:14 +0000349proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +0000350 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
351 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +0000352 if ($e2) {
353 set c [catch {uplevel 1 $code} r]
354 } else {
355 set c [catch {uplevel 1 $elsecode} r]
356 }
357 return -code $c $r
drh21504322002-06-25 13:16:02 +0000358}
danielk197745901d62004-11-10 15:27:38 +0000359
danielk1977aca790a2005-01-13 11:07:52 +0000360# This proc execs a seperate process that crashes midway through executing
361# the SQL script $sql on database test.db.
362#
363# The crash occurs during a sync() of file $crashfile. When the crash
364# occurs a random subset of all unsynced writes made by the process are
365# written into the files on disk. Argument $crashdelay indicates the
366# number of file syncs to wait before crashing.
367#
368# The return value is a list of two elements. The first element is a
369# boolean, indicating whether or not the process actually crashed or
370# reported some other error. The second element in the returned list is the
371# error message. This is "child process exited abnormally" if the crash
372# occured.
373#
danielk1977f8940ae2007-08-23 11:07:10 +0000374# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +0000375#
376proc crashsql {args} {
danielk1977aca790a2005-01-13 11:07:52 +0000377 if {$::tcl_platform(platform)!="unix"} {
378 error "crashsql should only be used on unix"
379 }
danielk197759a33f92007-03-17 10:26:59 +0000380
381 set blocksize ""
382 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +0000383 set prngseed 0
drhf8587402008-01-08 16:03:49 +0000384 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +0000385 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +0000386 set dc ""
danielk197759a33f92007-03-17 10:26:59 +0000387 set sql [lindex $args end]
388
389 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
390 set z [lindex $args $ii]
391 set n [string length $z]
392 set z2 [lindex $args [expr $ii+1]]
393
394 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +0000395 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +0000396 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +0000397 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000398 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +0000399 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +0000400 else { error "Unrecognized option: $z" }
401 }
402
403 if {$crashfile eq ""} {
404 error "Compulsory option -file missing"
405 }
406
danielk1977aca790a2005-01-13 11:07:52 +0000407 set cfile [file join [pwd] $crashfile]
408
409 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +0000410 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +0000411 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
danielk1977933bbd62007-03-17 07:22:42 +0000412 puts $f "set sqlite_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +0000413 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +0000414
415 # This block sets the cache size of the main database to 10
416 # pages. This is done in case the build is configured to omit
417 # "PRAGMA cache_size".
418 puts $f {db eval {SELECT * FROM sqlite_master;}}
419 puts $f {set bt [btree_from_db db]}
420 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +0000421 if {$prngseed} {
422 set seed [expr {$prngseed%10007+1}]
423 # puts seed=$seed
424 puts $f "db eval {SELECT randomblob($seed)}"
425 }
danielk1977933bbd62007-03-17 07:22:42 +0000426
drhf8587402008-01-08 16:03:49 +0000427 if {[string length $tclbody]>0} {
428 puts $f $tclbody
429 }
430 if {[string length $sql]>0} {
431 puts $f "db eval {"
432 puts $f "$sql"
433 puts $f "}"
434 }
danielk1977aca790a2005-01-13 11:07:52 +0000435 close $f
436
437 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000438 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000439 } msg]
440 lappend r $msg
441}
442
danielk197732554c12005-01-22 03:39:39 +0000443# Usage: do_ioerr_test <test number> <options...>
444#
445# This proc is used to implement test cases that check that IO errors
446# are correctly handled. The first argument, <test number>, is an integer
447# used to name the tests executed by this proc. Options are as follows:
448#
449# -tclprep TCL script to run to prepare test.
450# -sqlprep SQL script to run to prepare test.
451# -tclbody TCL script to run with IO error simulation.
452# -sqlbody TCL script to run with IO error simulation.
453# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000454# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +0000455# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +0000456# -start Value of 'N' to begin with (default 1)
457#
458# -cksum Boolean. If true, test that the database does
459# not change during the execution of the test case.
460#
461proc do_ioerr_test {testname args} {
462
danielk197732554c12005-01-22 03:39:39 +0000463 set ::ioerropts(-start) 1
464 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000465 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +0000466 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +0000467 set ::ioerropts(-persist) 1
danielk197732554c12005-01-22 03:39:39 +0000468 array set ::ioerropts $args
469
470 set ::go 1
drh93aed5a2008-01-16 17:46:38 +0000471 #reset_prng_state
472 save_prng_state
danielk197732554c12005-01-22 03:39:39 +0000473 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +0000474 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +0000475 incr ::ioerropts(-count) -1
476 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +0000477
478 # Skip this IO error if it was specified with the "-exclude" option.
479 if {[info exists ::ioerropts(-exclude)]} {
480 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
481 }
drh93aed5a2008-01-16 17:46:38 +0000482 restore_prng_state
danielk197732554c12005-01-22 03:39:39 +0000483
484 # Delete the files test.db and test2.db, then execute the TCL and
485 # SQL (in that order) to prepare for the test case.
486 do_test $testname.$n.1 {
487 set ::sqlite_io_error_pending 0
488 catch {db close}
489 catch {file delete -force test.db}
490 catch {file delete -force test.db-journal}
491 catch {file delete -force test2.db}
492 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000493 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000494 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000495 if {[info exists ::ioerropts(-tclprep)]} {
496 eval $::ioerropts(-tclprep)
497 }
498 if {[info exists ::ioerropts(-sqlprep)]} {
499 execsql $::ioerropts(-sqlprep)
500 }
501 expr 0
502 } {0}
503
504 # Read the 'checksum' of the database.
505 if {$::ioerropts(-cksum)} {
506 set checksum [cksum]
507 }
drh93aed5a2008-01-16 17:46:38 +0000508
danielk197732554c12005-01-22 03:39:39 +0000509 # Set the Nth IO error to fail.
510 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +0000511 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +0000512 set ::sqlite_io_error_pending $n
513 }] $n
514
515 # Create a single TCL script from the TCL and SQL specified
516 # as the body of the test.
517 set ::ioerrorbody {}
518 if {[info exists ::ioerropts(-tclbody)]} {
519 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
520 }
521 if {[info exists ::ioerropts(-sqlbody)]} {
522 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
523 }
524
525 # Execute the TCL Script created in the above block. If
526 # there are at least N IO operations performed by SQLite as
527 # a result of the script, the Nth will fail.
528 do_test $testname.$n.3 {
529 set r [catch $::ioerrorbody msg]
drhe49f9822006-09-15 12:29:16 +0000530 set rc [sqlite3_errcode $::DB]
531 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +0000532 # If we are in extended result code mode, make sure all of the
533 # IOERRs we get back really do have their extended code values.
534 # If an extended result code is returned, the sqlite3_errcode
535 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
536 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +0000537 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
538 return $rc
539 }
540 } else {
drh5f7b5bf2007-04-19 12:30:54 +0000541 # If we are not in extended result code mode, make sure no
542 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +0000543 if {[regexp {\+\d} $rc]} {
544 return $rc
545 }
546 }
drh93aed5a2008-01-16 17:46:38 +0000547 # The test repeats as long as $::go is non-zero. $::go starts out
548 # as 1. When a test runs to completion without hitting an I/O
549 # error, that means there is no point in continuing with this test
550 # case so set $::go to zero.
551 #
552 if {$::sqlite_io_error_pending>0} {
553 set ::go 0
554 set q 0
555 set ::sqlite_io_error_pending 0
556 } else {
557 set q 1
558 }
559
drhc9ac5ca2005-11-04 22:03:30 +0000560 set s [expr $::sqlite_io_error_hit==0]
danielk1977f2fa8312006-01-24 13:09:33 +0000561 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +0000562
563 # One of two things must have happened. either
564 # 1. We never hit the IO error and the SQL returned OK
565 # 2. An IO error was hit and the SQL failed
566 #
drh93aed5a2008-01-16 17:46:38 +0000567 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +0000568 } {1}
569
570 # If an IO error occured, then the checksum of the database should
571 # be the same as before the script that caused the IO error was run.
572 if {$::go && $::ioerropts(-cksum)} {
573 do_test $testname.$n.4 {
574 catch {db close}
drha34c62d2006-01-06 22:11:20 +0000575 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +0000576 cksum
577 } $checksum
578 }
579
danielk1977c4da5b92006-01-21 12:08:54 +0000580 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +0000581 if {[info exists ::ioerropts(-cleanup)]} {
582 catch $::ioerropts(-cleanup)
583 }
danielk197732554c12005-01-22 03:39:39 +0000584 }
585 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +0000586 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +0000587 unset ::ioerropts
588}
589
drh93aed5a2008-01-16 17:46:38 +0000590# Return a checksum based on the contents of the main database associated
591# with connection $db
danielk197732554c12005-01-22 03:39:39 +0000592#
593proc cksum {{db db}} {
594 set txt [$db eval {
595 SELECT name, type, sql FROM sqlite_master order by name
596 }]\n
597 foreach tbl [$db eval {
598 SELECT name FROM sqlite_master WHERE type='table' order by name
599 }] {
600 append txt [$db eval "SELECT * FROM $tbl"]\n
601 }
602 foreach prag {default_synchronous default_cache_size} {
603 append txt $prag-[$db eval "PRAGMA $prag"]\n
604 }
605 set cksum [string length $txt]-[md5 $txt]
606 # puts $cksum-[file size test.db]
607 return $cksum
608}
609
drh93aed5a2008-01-16 17:46:38 +0000610# Generate a checksum based on the contents of the main and temp tables
611# database $db. If the checksum of two databases is the same, and the
612# integrity-check passes for both, the two databases are identical.
613#
drh1db639c2008-01-17 02:36:28 +0000614proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +0000615 set ret [list]
616 ifcapable tempdb {
617 set sql {
618 SELECT name FROM sqlite_master WHERE type = 'table' UNION
619 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
620 SELECT 'sqlite_master' UNION
621 SELECT 'sqlite_temp_master' ORDER BY 1
622 }
623 } else {
624 set sql {
625 SELECT name FROM sqlite_master WHERE type = 'table' UNION
626 SELECT 'sqlite_master' ORDER BY 1
627 }
628 }
629 set tbllist [$db eval $sql]
630 set txt {}
631 foreach tbl $tbllist {
632 append txt [$db eval "SELECT * FROM $tbl"]
633 }
634 foreach prag {default_cache_size} {
635 append txt $prag-[$db eval "PRAGMA $prag"]\n
636 }
637 # puts txt=$txt
638 return [md5 $txt]
639}
640
641
642
danielk197732554c12005-01-22 03:39:39 +0000643# Copy file $from into $to. This is used because some versions of
644# TCL for windows (notably the 8.4.1 binary package shipped with the
645# current mingw release) have a broken "file copy" command.
646#
647proc copy_file {from to} {
648 if {$::tcl_platform(platform)=="unix"} {
649 file copy -force $from $to
650 } else {
651 set f [open $from]
652 fconfigure $f -translation binary
653 set t [open $to w]
654 fconfigure $t -translation binary
655 puts -nonewline $t [read $f [file size $from]]
656 close $t
657 close $f
658 }
659}
660
danielk197745901d62004-11-10 15:27:38 +0000661# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
662# to non-zero, then set the global variable $AUTOVACUUM to 1.
663set AUTOVACUUM $sqlite_options(default_autovacuum)