OLD | NEW |
| (Empty) |
1 # 2001 September 15 | |
2 # | |
3 # The author disclaims copyright to this source code. In place of | |
4 # a legal notice, here is a blessing: | |
5 # | |
6 # 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. | |
9 # | |
10 #*********************************************************************** | |
11 # This file implements some common TCL routines used for regression | |
12 # testing the SQLite library | |
13 # | |
14 # $Id: tester.tcl,v 1.143 2009/04/09 01:23:49 drh Exp $ | |
15 | |
16 #------------------------------------------------------------------------- | |
17 # The commands provided by the code in this file to help with creating | |
18 # test cases are as follows: | |
19 # | |
20 # Commands to manipulate the db and the file-system at a high level: | |
21 # | |
22 # is_relative_file | |
23 # test_pwd | |
24 # get_pwd | |
25 # copy_file FROM TO | |
26 # delete_file FILENAME | |
27 # drop_all_tables ?DB? | |
28 # forcecopy FROM TO | |
29 # forcedelete FILENAME | |
30 # | |
31 # Test the capability of the SQLite version built into the interpreter to | |
32 # determine if a specific test can be run: | |
33 # | |
34 # capable EXPR | |
35 # ifcapable EXPR | |
36 # | |
37 # Calulate checksums based on database contents: | |
38 # | |
39 # dbcksum DB DBNAME | |
40 # allcksum ?DB? | |
41 # cksum ?DB? | |
42 # | |
43 # Commands to execute/explain SQL statements: | |
44 # | |
45 # memdbsql SQL | |
46 # stepsql DB SQL | |
47 # execsql2 SQL | |
48 # explain_no_trace SQL | |
49 # explain SQL ?DB? | |
50 # catchsql SQL ?DB? | |
51 # execsql SQL ?DB? | |
52 # | |
53 # Commands to run test cases: | |
54 # | |
55 # do_ioerr_test TESTNAME ARGS... | |
56 # crashsql ARGS... | |
57 # integrity_check TESTNAME ?DB? | |
58 # verify_ex_errcode TESTNAME EXPECTED ?DB? | |
59 # do_test TESTNAME SCRIPT EXPECTED | |
60 # do_execsql_test TESTNAME SQL EXPECTED | |
61 # do_catchsql_test TESTNAME SQL EXPECTED | |
62 # do_timed_execsql_test TESTNAME SQL EXPECTED | |
63 # | |
64 # Commands providing a lower level interface to the global test counters: | |
65 # | |
66 # set_test_counter COUNTER ?VALUE? | |
67 # omit_test TESTNAME REASON ?APPEND? | |
68 # fail_test TESTNAME | |
69 # incr_ntest | |
70 # | |
71 # Command run at the end of each test file: | |
72 # | |
73 # finish_test | |
74 # | |
75 # Commands to help create test files that run with the "WAL" and other | |
76 # permutations (see file permutations.test): | |
77 # | |
78 # wal_is_wal_mode | |
79 # wal_set_journal_mode ?DB? | |
80 # wal_check_journal_mode TESTNAME?DB? | |
81 # permutation | |
82 # presql | |
83 # | |
84 # Command to test whether or not --verbose=1 was specified on the command | |
85 # line (returns 0 for not-verbose, 1 for verbose and 2 for "verbose in the | |
86 # output file only"). | |
87 # | |
88 # verbose | |
89 # | |
90 | |
91 # Set the precision of FP arithmatic used by the interpreter. And | |
92 # configure SQLite to take database file locks on the page that begins | |
93 # 64KB into the database file instead of the one 1GB in. This means | |
94 # the code that handles that special case can be tested without creating | |
95 # very large database files. | |
96 # | |
97 set tcl_precision 15 | |
98 sqlite3_test_control_pending_byte 0x0010000 | |
99 | |
100 | |
101 # If the pager codec is available, create a wrapper for the [sqlite3] | |
102 # command that appends "-key {xyzzy}" to the command line. i.e. this: | |
103 # | |
104 # sqlite3 db test.db | |
105 # | |
106 # becomes | |
107 # | |
108 # sqlite3 db test.db -key {xyzzy} | |
109 # | |
110 if {[info command sqlite_orig]==""} { | |
111 rename sqlite3 sqlite_orig | |
112 proc sqlite3 {args} { | |
113 if {[llength $args]>=2 && [string index [lindex $args 0] 0]!="-"} { | |
114 # This command is opening a new database connection. | |
115 # | |
116 if {[info exists ::G(perm:sqlite3_args)]} { | |
117 set args [concat $args $::G(perm:sqlite3_args)] | |
118 } | |
119 if {[sqlite_orig -has-codec] && ![info exists ::do_not_use_codec]} { | |
120 lappend args -key {xyzzy} | |
121 } | |
122 | |
123 set res [uplevel 1 sqlite_orig $args] | |
124 if {[info exists ::G(perm:presql)]} { | |
125 [lindex $args 0] eval $::G(perm:presql) | |
126 } | |
127 if {[info exists ::G(perm:dbconfig)]} { | |
128 set ::dbhandle [lindex $args 0] | |
129 uplevel #0 $::G(perm:dbconfig) | |
130 } | |
131 set res | |
132 } else { | |
133 # This command is not opening a new database connection. Pass the | |
134 # arguments through to the C implementation as the are. | |
135 # | |
136 uplevel 1 sqlite_orig $args | |
137 } | |
138 } | |
139 } | |
140 | |
141 proc getFileRetries {} { | |
142 if {![info exists ::G(file-retries)]} { | |
143 # | |
144 # NOTE: Return the default number of retries for [file] operations. A | |
145 # value of zero or less here means "disabled". | |
146 # | |
147 return [expr {$::tcl_platform(platform) eq "windows" ? 50 : 0}] | |
148 } | |
149 return $::G(file-retries) | |
150 } | |
151 | |
152 proc getFileRetryDelay {} { | |
153 if {![info exists ::G(file-retry-delay)]} { | |
154 # | |
155 # NOTE: Return the default number of milliseconds to wait when retrying | |
156 # failed [file] operations. A value of zero or less means "do not | |
157 # wait". | |
158 # | |
159 return 100; # TODO: Good default? | |
160 } | |
161 return $::G(file-retry-delay) | |
162 } | |
163 | |
164 # Return the string representing the name of the current directory. On | |
165 # Windows, the result is "normalized" to whatever our parent command shell | |
166 # is using to prevent case-mismatch issues. | |
167 # | |
168 proc get_pwd {} { | |
169 if {$::tcl_platform(platform) eq "windows"} { | |
170 # | |
171 # NOTE: Cannot use [file normalize] here because it would alter the | |
172 # case of the result to what Tcl considers canonical, which would | |
173 # defeat the purpose of this procedure. | |
174 # | |
175 return [string map [list \\ /] \ | |
176 [string trim [exec -- $::env(ComSpec) /c echo %CD%]]] | |
177 } else { | |
178 return [pwd] | |
179 } | |
180 } | |
181 | |
182 # Copy file $from into $to. This is used because some versions of | |
183 # TCL for windows (notably the 8.4.1 binary package shipped with the | |
184 # current mingw release) have a broken "file copy" command. | |
185 # | |
186 proc copy_file {from to} { | |
187 do_copy_file false $from $to | |
188 } | |
189 | |
190 proc forcecopy {from to} { | |
191 do_copy_file true $from $to | |
192 } | |
193 | |
194 proc do_copy_file {force from to} { | |
195 set nRetry [getFileRetries] ;# Maximum number of retries. | |
196 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying. | |
197 | |
198 # On windows, sometimes even a [file copy -force] can fail. The cause is | |
199 # usually "tag-alongs" - programs like anti-virus software, automatic backup | |
200 # tools and various explorer extensions that keep a file open a little longer | |
201 # than we expect, causing the delete to fail. | |
202 # | |
203 # The solution is to wait a short amount of time before retrying the copy. | |
204 # | |
205 if {$nRetry > 0} { | |
206 for {set i 0} {$i<$nRetry} {incr i} { | |
207 set rc [catch { | |
208 if {$force} { | |
209 file copy -force $from $to | |
210 } else { | |
211 file copy $from $to | |
212 } | |
213 } msg] | |
214 if {$rc==0} break | |
215 if {$nDelay > 0} { after $nDelay } | |
216 } | |
217 if {$rc} { error $msg } | |
218 } else { | |
219 if {$force} { | |
220 file copy -force $from $to | |
221 } else { | |
222 file copy $from $to | |
223 } | |
224 } | |
225 } | |
226 | |
227 # Check if a file name is relative | |
228 # | |
229 proc is_relative_file { file } { | |
230 return [expr {[file pathtype $file] != "absolute"}] | |
231 } | |
232 | |
233 # If the VFS supports using the current directory, returns [pwd]; | |
234 # otherwise, it returns only the provided suffix string (which is | |
235 # empty by default). | |
236 # | |
237 proc test_pwd { args } { | |
238 if {[llength $args] > 0} { | |
239 set suffix1 [lindex $args 0] | |
240 if {[llength $args] > 1} { | |
241 set suffix2 [lindex $args 1] | |
242 } else { | |
243 set suffix2 $suffix1 | |
244 } | |
245 } else { | |
246 set suffix1 ""; set suffix2 "" | |
247 } | |
248 ifcapable curdir { | |
249 return "[get_pwd]$suffix1" | |
250 } else { | |
251 return $suffix2 | |
252 } | |
253 } | |
254 | |
255 # Delete a file or directory | |
256 # | |
257 proc delete_file {args} { | |
258 do_delete_file false {*}$args | |
259 } | |
260 | |
261 proc forcedelete {args} { | |
262 do_delete_file true {*}$args | |
263 } | |
264 | |
265 proc do_delete_file {force args} { | |
266 set nRetry [getFileRetries] ;# Maximum number of retries. | |
267 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying. | |
268 | |
269 foreach filename $args { | |
270 # On windows, sometimes even a [file delete -force] can fail just after | |
271 # a file is closed. The cause is usually "tag-alongs" - programs like | |
272 # anti-virus software, automatic backup tools and various explorer | |
273 # extensions that keep a file open a little longer than we expect, causing | |
274 # the delete to fail. | |
275 # | |
276 # The solution is to wait a short amount of time before retrying the | |
277 # delete. | |
278 # | |
279 if {$nRetry > 0} { | |
280 for {set i 0} {$i<$nRetry} {incr i} { | |
281 set rc [catch { | |
282 if {$force} { | |
283 file delete -force $filename | |
284 } else { | |
285 file delete $filename | |
286 } | |
287 } msg] | |
288 if {$rc==0} break | |
289 if {$nDelay > 0} { after $nDelay } | |
290 } | |
291 if {$rc} { error $msg } | |
292 } else { | |
293 if {$force} { | |
294 file delete -force $filename | |
295 } else { | |
296 file delete $filename | |
297 } | |
298 } | |
299 } | |
300 } | |
301 | |
302 if {$::tcl_platform(platform) eq "windows"} { | |
303 proc do_remove_win32_dir {args} { | |
304 set nRetry [getFileRetries] ;# Maximum number of retries. | |
305 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying. | |
306 | |
307 foreach dirName $args { | |
308 # On windows, sometimes even a [remove_win32_dir] can fail just after | |
309 # a directory is emptied. The cause is usually "tag-alongs" - programs | |
310 # like anti-virus software, automatic backup tools and various explorer | |
311 # extensions that keep a file open a little longer than we expect, | |
312 # causing the delete to fail. | |
313 # | |
314 # The solution is to wait a short amount of time before retrying the | |
315 # removal. | |
316 # | |
317 if {$nRetry > 0} { | |
318 for {set i 0} {$i < $nRetry} {incr i} { | |
319 set rc [catch { | |
320 remove_win32_dir $dirName | |
321 } msg] | |
322 if {$rc == 0} break | |
323 if {$nDelay > 0} { after $nDelay } | |
324 } | |
325 if {$rc} { error $msg } | |
326 } else { | |
327 remove_win32_dir $dirName | |
328 } | |
329 } | |
330 } | |
331 | |
332 proc do_delete_win32_file {args} { | |
333 set nRetry [getFileRetries] ;# Maximum number of retries. | |
334 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying. | |
335 | |
336 foreach fileName $args { | |
337 # On windows, sometimes even a [delete_win32_file] can fail just after | |
338 # a file is closed. The cause is usually "tag-alongs" - programs like | |
339 # anti-virus software, automatic backup tools and various explorer | |
340 # extensions that keep a file open a little longer than we expect, | |
341 # causing the delete to fail. | |
342 # | |
343 # The solution is to wait a short amount of time before retrying the | |
344 # delete. | |
345 # | |
346 if {$nRetry > 0} { | |
347 for {set i 0} {$i < $nRetry} {incr i} { | |
348 set rc [catch { | |
349 delete_win32_file $fileName | |
350 } msg] | |
351 if {$rc == 0} break | |
352 if {$nDelay > 0} { after $nDelay } | |
353 } | |
354 if {$rc} { error $msg } | |
355 } else { | |
356 delete_win32_file $fileName | |
357 } | |
358 } | |
359 } | |
360 } | |
361 | |
362 proc execpresql {handle args} { | |
363 trace remove execution $handle enter [list execpresql $handle] | |
364 if {[info exists ::G(perm:presql)]} { | |
365 $handle eval $::G(perm:presql) | |
366 } | |
367 } | |
368 | |
369 # This command should be called after loading tester.tcl from within | |
370 # all test scripts that are incompatible with encryption codecs. | |
371 # | |
372 proc do_not_use_codec {} { | |
373 set ::do_not_use_codec 1 | |
374 reset_db | |
375 } | |
376 | |
377 # The following block only runs the first time this file is sourced. It | |
378 # does not run in slave interpreters (since the ::cmdlinearg array is | |
379 # populated before the test script is run in slave interpreters). | |
380 # | |
381 if {[info exists cmdlinearg]==0} { | |
382 | |
383 # Parse any options specified in the $argv array. This script accepts the | |
384 # following options: | |
385 # | |
386 # --pause | |
387 # --soft-heap-limit=NN | |
388 # --maxerror=NN | |
389 # --malloctrace=N | |
390 # --backtrace=N | |
391 # --binarylog=N | |
392 # --soak=N | |
393 # --file-retries=N | |
394 # --file-retry-delay=N | |
395 # --start=[$permutation:]$testfile | |
396 # --match=$pattern | |
397 # --verbose=$val | |
398 # --output=$filename | |
399 # --help | |
400 # | |
401 set cmdlinearg(soft-heap-limit) 0 | |
402 set cmdlinearg(maxerror) 1000 | |
403 set cmdlinearg(malloctrace) 0 | |
404 set cmdlinearg(backtrace) 10 | |
405 set cmdlinearg(binarylog) 0 | |
406 set cmdlinearg(soak) 0 | |
407 set cmdlinearg(file-retries) 0 | |
408 set cmdlinearg(file-retry-delay) 0 | |
409 set cmdlinearg(start) "" | |
410 set cmdlinearg(match) "" | |
411 set cmdlinearg(verbose) "" | |
412 set cmdlinearg(output) "" | |
413 | |
414 set leftover [list] | |
415 foreach a $argv { | |
416 switch -regexp -- $a { | |
417 {^-+pause$} { | |
418 # Wait for user input before continuing. This is to give the user an | |
419 # opportunity to connect profiling tools to the process. | |
420 puts -nonewline "Press RETURN to begin..." | |
421 flush stdout | |
422 gets stdin | |
423 } | |
424 {^-+soft-heap-limit=.+$} { | |
425 foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break | |
426 } | |
427 {^-+maxerror=.+$} { | |
428 foreach {dummy cmdlinearg(maxerror)} [split $a =] break | |
429 } | |
430 {^-+malloctrace=.+$} { | |
431 foreach {dummy cmdlinearg(malloctrace)} [split $a =] break | |
432 if {$cmdlinearg(malloctrace)} { | |
433 sqlite3_memdebug_log start | |
434 } | |
435 } | |
436 {^-+backtrace=.+$} { | |
437 foreach {dummy cmdlinearg(backtrace)} [split $a =] break | |
438 sqlite3_memdebug_backtrace $value | |
439 } | |
440 {^-+binarylog=.+$} { | |
441 foreach {dummy cmdlinearg(binarylog)} [split $a =] break | |
442 } | |
443 {^-+soak=.+$} { | |
444 foreach {dummy cmdlinearg(soak)} [split $a =] break | |
445 set ::G(issoak) $cmdlinearg(soak) | |
446 } | |
447 {^-+file-retries=.+$} { | |
448 foreach {dummy cmdlinearg(file-retries)} [split $a =] break | |
449 set ::G(file-retries) $cmdlinearg(file-retries) | |
450 } | |
451 {^-+file-retry-delay=.+$} { | |
452 foreach {dummy cmdlinearg(file-retry-delay)} [split $a =] break | |
453 set ::G(file-retry-delay) $cmdlinearg(file-retry-delay) | |
454 } | |
455 {^-+start=.+$} { | |
456 foreach {dummy cmdlinearg(start)} [split $a =] break | |
457 | |
458 set ::G(start:file) $cmdlinearg(start) | |
459 if {[regexp {(.*):(.*)} $cmdlinearg(start) -> s.perm s.file]} { | |
460 set ::G(start:permutation) ${s.perm} | |
461 set ::G(start:file) ${s.file} | |
462 } | |
463 if {$::G(start:file) == ""} {unset ::G(start:file)} | |
464 } | |
465 {^-+match=.+$} { | |
466 foreach {dummy cmdlinearg(match)} [split $a =] break | |
467 | |
468 set ::G(match) $cmdlinearg(match) | |
469 if {$::G(match) == ""} {unset ::G(match)} | |
470 } | |
471 | |
472 {^-+output=.+$} { | |
473 foreach {dummy cmdlinearg(output)} [split $a =] break | |
474 if {$cmdlinearg(verbose)==""} { | |
475 set cmdlinearg(verbose) 2 | |
476 } | |
477 } | |
478 {^-+verbose=.+$} { | |
479 foreach {dummy cmdlinearg(verbose)} [split $a =] break | |
480 if {$cmdlinearg(verbose)=="file"} { | |
481 set cmdlinearg(verbose) 2 | |
482 } elseif {[string is boolean -strict $cmdlinearg(verbose)]==0} { | |
483 error "option --verbose= must be set to a boolean or to \"file\"" | |
484 } | |
485 } | |
486 | |
487 default { | |
488 lappend leftover $a | |
489 } | |
490 } | |
491 } | |
492 set argv $leftover | |
493 | |
494 # Install the malloc layer used to inject OOM errors. And the 'automatic' | |
495 # extensions. This only needs to be done once for the process. | |
496 # | |
497 sqlite3_shutdown | |
498 install_malloc_faultsim 1 | |
499 sqlite3_initialize | |
500 autoinstall_test_functions | |
501 | |
502 # If the --binarylog option was specified, create the logging VFS. This | |
503 # call installs the new VFS as the default for all SQLite connections. | |
504 # | |
505 if {$cmdlinearg(binarylog)} { | |
506 vfslog new binarylog {} vfslog.bin | |
507 } | |
508 | |
509 # Set the backtrace depth, if malloc tracing is enabled. | |
510 # | |
511 if {$cmdlinearg(malloctrace)} { | |
512 sqlite3_memdebug_backtrace $cmdlinearg(backtrace) | |
513 } | |
514 | |
515 if {$cmdlinearg(output)!=""} { | |
516 puts "Copying output to file $cmdlinearg(output)" | |
517 set ::G(output_fd) [open $cmdlinearg(output) w] | |
518 fconfigure $::G(output_fd) -buffering line | |
519 } | |
520 | |
521 if {$cmdlinearg(verbose)==""} { | |
522 set cmdlinearg(verbose) 1 | |
523 } | |
524 } | |
525 | |
526 # Update the soft-heap-limit each time this script is run. In that | |
527 # way if an individual test file changes the soft-heap-limit, it | |
528 # will be reset at the start of the next test file. | |
529 # | |
530 sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit) | |
531 | |
532 # Create a test database | |
533 # | |
534 proc reset_db {} { | |
535 catch {db close} | |
536 forcedelete test.db | |
537 forcedelete test.db-journal | |
538 forcedelete test.db-wal | |
539 sqlite3 db ./test.db | |
540 set ::DB [sqlite3_connection_pointer db] | |
541 if {[info exists ::SETUP_SQL]} { | |
542 db eval $::SETUP_SQL | |
543 } | |
544 } | |
545 reset_db | |
546 | |
547 # Abort early if this script has been run before. | |
548 # | |
549 if {[info exists TC(count)]} return | |
550 | |
551 # Make sure memory statistics are enabled. | |
552 # | |
553 sqlite3_config_memstatus 1 | |
554 | |
555 # Initialize the test counters and set up commands to access them. | |
556 # Or, if this is a slave interpreter, set up aliases to write the | |
557 # counters in the parent interpreter. | |
558 # | |
559 if {0==[info exists ::SLAVE]} { | |
560 set TC(errors) 0 | |
561 set TC(count) 0 | |
562 set TC(fail_list) [list] | |
563 set TC(omit_list) [list] | |
564 set TC(warn_list) [list] | |
565 | |
566 proc set_test_counter {counter args} { | |
567 if {[llength $args]} { | |
568 set ::TC($counter) [lindex $args 0] | |
569 } | |
570 set ::TC($counter) | |
571 } | |
572 } | |
573 | |
574 # Record the fact that a sequence of tests were omitted. | |
575 # | |
576 proc omit_test {name reason {append 1}} { | |
577 set omitList [set_test_counter omit_list] | |
578 if {$append} { | |
579 lappend omitList [list $name $reason] | |
580 } | |
581 set_test_counter omit_list $omitList | |
582 } | |
583 | |
584 # Record the fact that a test failed. | |
585 # | |
586 proc fail_test {name} { | |
587 set f [set_test_counter fail_list] | |
588 lappend f $name | |
589 set_test_counter fail_list $f | |
590 set_test_counter errors [expr [set_test_counter errors] + 1] | |
591 | |
592 set nFail [set_test_counter errors] | |
593 if {$nFail>=$::cmdlinearg(maxerror)} { | |
594 output2 "*** Giving up..." | |
595 finalize_testing | |
596 } | |
597 } | |
598 | |
599 # Remember a warning message to be displayed at the conclusion of all testing | |
600 # | |
601 proc warning {msg {append 1}} { | |
602 output2 "Warning: $msg" | |
603 set warnList [set_test_counter warn_list] | |
604 if {$append} { | |
605 lappend warnList $msg | |
606 } | |
607 set_test_counter warn_list $warnList | |
608 } | |
609 | |
610 | |
611 # Increment the number of tests run | |
612 # | |
613 proc incr_ntest {} { | |
614 set_test_counter count [expr [set_test_counter count] + 1] | |
615 } | |
616 | |
617 # Return true if --verbose=1 was specified on the command line. Otherwise, | |
618 # return false. | |
619 # | |
620 proc verbose {} { | |
621 return $::cmdlinearg(verbose) | |
622 } | |
623 | |
624 # Use the following commands instead of [puts] for test output within | |
625 # this file. Test scripts can still use regular [puts], which is directed | |
626 # to stdout and, if one is open, the --output file. | |
627 # | |
628 # output1: output that should be printed if --verbose=1 was specified. | |
629 # output2: output that should be printed unconditionally. | |
630 # output2_if_no_verbose: output that should be printed only if --verbose=0. | |
631 # | |
632 proc output1 {args} { | |
633 set v [verbose] | |
634 if {$v==1} { | |
635 uplevel output2 $args | |
636 } elseif {$v==2} { | |
637 uplevel puts [lrange $args 0 end-1] $::G(output_fd) [lrange $args end end] | |
638 } | |
639 } | |
640 proc output2 {args} { | |
641 set nArg [llength $args] | |
642 uplevel puts $args | |
643 } | |
644 proc output2_if_no_verbose {args} { | |
645 set v [verbose] | |
646 if {$v==0} { | |
647 uplevel output2 $args | |
648 } elseif {$v==2} { | |
649 uplevel puts [lrange $args 0 end-1] stdout [lrange $args end end] | |
650 } | |
651 } | |
652 | |
653 # Override the [puts] command so that if no channel is explicitly | |
654 # specified the string is written to both stdout and to the file | |
655 # specified by "--output=", if any. | |
656 # | |
657 proc puts_override {args} { | |
658 set nArg [llength $args] | |
659 if {$nArg==1 || ($nArg==2 && [string first [lindex $args 0] -nonewline]==0)} { | |
660 uplevel puts_original $args | |
661 if {[info exists ::G(output_fd)]} { | |
662 uplevel puts [lrange $args 0 end-1] $::G(output_fd) [lrange $args end end] | |
663 } | |
664 } else { | |
665 # A channel was explicitly specified. | |
666 uplevel puts_original $args | |
667 } | |
668 } | |
669 rename puts puts_original | |
670 proc puts {args} { uplevel puts_override $args } | |
671 | |
672 | |
673 # Invoke the do_test procedure to run a single test | |
674 # | |
675 proc do_test {name cmd expected} { | |
676 global argv cmdlinearg | |
677 | |
678 fix_testname name | |
679 | |
680 sqlite3_memdebug_settitle $name | |
681 | |
682 # if {[llength $argv]==0} { | |
683 # set go 1 | |
684 # } else { | |
685 # set go 0 | |
686 # foreach pattern $argv { | |
687 # if {[string match $pattern $name]} { | |
688 # set go 1 | |
689 # break | |
690 # } | |
691 # } | |
692 # } | |
693 | |
694 if {[info exists ::G(perm:prefix)]} { | |
695 set name "$::G(perm:prefix)$name" | |
696 } | |
697 | |
698 incr_ntest | |
699 output1 -nonewline $name... | |
700 flush stdout | |
701 | |
702 if {![info exists ::G(match)] || [string match $::G(match) $name]} { | |
703 if {[catch {uplevel #0 "$cmd;\n"} result]} { | |
704 output2_if_no_verbose -nonewline $name... | |
705 output2 "\nError: $result" | |
706 fail_test $name | |
707 } else { | |
708 if {[regexp {^~?/.*/$} $expected]} { | |
709 # "expected" is of the form "/PATTERN/" then the result if correct if | |
710 # regular expression PATTERN matches the result. "~/PATTERN/" means | |
711 # the regular expression must not match. | |
712 if {[string index $expected 0]=="~"} { | |
713 set re [string range $expected 2 end-1] | |
714 if {[string index $re 0]=="*"} { | |
715 # If the regular expression begins with * then treat it as a glob in
stead | |
716 set ok [string match $re $result] | |
717 } else { | |
718 set re [string map {# {[-0-9.]+}} $re] | |
719 set ok [regexp $re $result] | |
720 } | |
721 set ok [expr {!$ok}] | |
722 } else { | |
723 set re [string range $expected 1 end-1] | |
724 if {[string index $re 0]=="*"} { | |
725 # If the regular expression begins with * then treat it as a glob in
stead | |
726 set ok [string match $re $result] | |
727 } else { | |
728 set re [string map {# {[-0-9.]+}} $re] | |
729 set ok [regexp $re $result] | |
730 } | |
731 } | |
732 } elseif {[regexp {^~?\*.*\*$} $expected]} { | |
733 # "expected" is of the form "*GLOB*" then the result if correct if | |
734 # glob pattern GLOB matches the result. "~/GLOB/" means | |
735 # the glob must not match. | |
736 if {[string index $expected 0]=="~"} { | |
737 set e [string range $expected 1 end] | |
738 set ok [expr {![string match $e $result]}] | |
739 } else { | |
740 set ok [string match $expected $result] | |
741 } | |
742 } else { | |
743 set ok [expr {[string compare $result $expected]==0}] | |
744 } | |
745 if {!$ok} { | |
746 # if {![info exists ::testprefix] || $::testprefix eq ""} { | |
747 # error "no test prefix" | |
748 # } | |
749 output1 "" | |
750 output2 "! $name expected: \[$expected\]\n! $name got: \[$result\]" | |
751 fail_test $name | |
752 } else { | |
753 output1 " Ok" | |
754 } | |
755 } | |
756 } else { | |
757 output1 " Omitted" | |
758 omit_test $name "pattern mismatch" 0 | |
759 } | |
760 flush stdout | |
761 } | |
762 | |
763 proc dumpbytes {s} { | |
764 set r "" | |
765 for {set i 0} {$i < [string length $s]} {incr i} { | |
766 if {$i > 0} {append r " "} | |
767 append r [format %02X [scan [string index $s $i] %c]] | |
768 } | |
769 return $r | |
770 } | |
771 | |
772 proc catchcmd {db {cmd ""}} { | |
773 global CLI | |
774 set out [open cmds.txt w] | |
775 puts $out $cmd | |
776 close $out | |
777 set line "exec $CLI $db < cmds.txt" | |
778 set rc [catch { eval $line } msg] | |
779 list $rc $msg | |
780 } | |
781 | |
782 proc catchcmdex {db {cmd ""}} { | |
783 global CLI | |
784 set out [open cmds.txt w] | |
785 fconfigure $out -encoding binary -translation binary | |
786 puts -nonewline $out $cmd | |
787 close $out | |
788 set line "exec -keepnewline -- $CLI $db < cmds.txt" | |
789 set chans [list stdin stdout stderr] | |
790 foreach chan $chans { | |
791 catch { | |
792 set modes($chan) [fconfigure $chan] | |
793 fconfigure $chan -encoding binary -translation binary -buffering none | |
794 } | |
795 } | |
796 set rc [catch { eval $line } msg] | |
797 foreach chan $chans { | |
798 catch { | |
799 eval fconfigure [list $chan] $modes($chan) | |
800 } | |
801 } | |
802 # puts [dumpbytes $msg] | |
803 list $rc $msg | |
804 } | |
805 | |
806 proc filepath_normalize {p} { | |
807 # test cases should be written to assume "unix"-like file paths | |
808 if {$::tcl_platform(platform)!="unix"} { | |
809 # lreverse*2 as a hack to remove any unneeded {} after the string map | |
810 lreverse [lreverse [string map {\\ /} [regsub -nocase -all {[a-z]:[/\\]+} $p
{/}]]] | |
811 } { | |
812 set p | |
813 } | |
814 } | |
815 proc do_filepath_test {name cmd expected} { | |
816 uplevel [list do_test $name [ | |
817 subst -nocommands { filepath_normalize [ $cmd ] } | |
818 ] [filepath_normalize $expected]] | |
819 } | |
820 | |
821 proc realnum_normalize {r} { | |
822 # different TCL versions display floating point values differently. | |
823 string map {1.#INF inf Inf inf .0e e} [regsub -all {(e[+-])0+} $r {\1}] | |
824 } | |
825 proc do_realnum_test {name cmd expected} { | |
826 uplevel [list do_test $name [ | |
827 subst -nocommands { realnum_normalize [ $cmd ] } | |
828 ] [realnum_normalize $expected]] | |
829 } | |
830 | |
831 proc fix_testname {varname} { | |
832 upvar $varname testname | |
833 if {[info exists ::testprefix] | |
834 && [string is digit [string range $testname 0 0]] | |
835 } { | |
836 set testname "${::testprefix}-$testname" | |
837 } | |
838 } | |
839 | |
840 proc do_execsql_test {testname sql {result {}}} { | |
841 fix_testname testname | |
842 uplevel do_test [list $testname] [list "execsql {$sql}"] [list [list {*}$resul
t]] | |
843 } | |
844 proc do_catchsql_test {testname sql result} { | |
845 fix_testname testname | |
846 uplevel do_test [list $testname] [list "catchsql {$sql}"] [list $result] | |
847 } | |
848 proc do_timed_execsql_test {testname sql {result {}}} { | |
849 fix_testname testname | |
850 uplevel do_test [list $testname] [list "execsql_timed {$sql}"]\ | |
851 [list [list {*}$result]] | |
852 } | |
853 proc do_eqp_test {name sql res} { | |
854 uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res] | |
855 } | |
856 | |
857 #------------------------------------------------------------------------- | |
858 # Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST | |
859 # | |
860 # Where switches are: | |
861 # | |
862 # -errorformat FMTSTRING | |
863 # -count | |
864 # -query SQL | |
865 # -tclquery TCL | |
866 # -repair TCL | |
867 # | |
868 proc do_select_tests {prefix args} { | |
869 | |
870 set testlist [lindex $args end] | |
871 set switches [lrange $args 0 end-1] | |
872 | |
873 set errfmt "" | |
874 set countonly 0 | |
875 set tclquery "" | |
876 set repair "" | |
877 | |
878 for {set i 0} {$i < [llength $switches]} {incr i} { | |
879 set s [lindex $switches $i] | |
880 set n [string length $s] | |
881 if {$n>=2 && [string equal -length $n $s "-query"]} { | |
882 set tclquery [list execsql [lindex $switches [incr i]]] | |
883 } elseif {$n>=2 && [string equal -length $n $s "-tclquery"]} { | |
884 set tclquery [lindex $switches [incr i]] | |
885 } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} { | |
886 set errfmt [lindex $switches [incr i]] | |
887 } elseif {$n>=2 && [string equal -length $n $s "-repair"]} { | |
888 set repair [lindex $switches [incr i]] | |
889 } elseif {$n>=2 && [string equal -length $n $s "-count"]} { | |
890 set countonly 1 | |
891 } else { | |
892 error "unknown switch: $s" | |
893 } | |
894 } | |
895 | |
896 if {$countonly && $errfmt!=""} { | |
897 error "Cannot use -count and -errorformat together" | |
898 } | |
899 set nTestlist [llength $testlist] | |
900 if {$nTestlist%3 || $nTestlist==0 } { | |
901 error "SELECT test list contains [llength $testlist] elements" | |
902 } | |
903 | |
904 eval $repair | |
905 foreach {tn sql res} $testlist { | |
906 if {$tclquery != ""} { | |
907 execsql $sql | |
908 uplevel do_test ${prefix}.$tn [list $tclquery] [list [list {*}$res]] | |
909 } elseif {$countonly} { | |
910 set nRow 0 | |
911 db eval $sql {incr nRow} | |
912 uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res] | |
913 } elseif {$errfmt==""} { | |
914 uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]] | |
915 } else { | |
916 set res [list 1 [string trim [format $errfmt {*}$res]]] | |
917 uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res] | |
918 } | |
919 eval $repair | |
920 } | |
921 | |
922 } | |
923 | |
924 proc delete_all_data {} { | |
925 db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} { | |
926 db eval "DELETE FROM '[string map {' ''} $t]'" | |
927 } | |
928 } | |
929 | |
930 # Run an SQL script. | |
931 # Return the number of microseconds per statement. | |
932 # | |
933 proc speed_trial {name numstmt units sql} { | |
934 output2 -nonewline [format {%-21.21s } $name...] | |
935 flush stdout | |
936 set speed [time {sqlite3_exec_nr db $sql}] | |
937 set tm [lindex $speed 0] | |
938 if {$tm == 0} { | |
939 set rate [format %20s "many"] | |
940 } else { | |
941 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]] | |
942 } | |
943 set u2 $units/s | |
944 output2 [format {%12d uS %s %s} $tm $rate $u2] | |
945 global total_time | |
946 set total_time [expr {$total_time+$tm}] | |
947 lappend ::speed_trial_times $name $tm | |
948 } | |
949 proc speed_trial_tcl {name numstmt units script} { | |
950 output2 -nonewline [format {%-21.21s } $name...] | |
951 flush stdout | |
952 set speed [time {eval $script}] | |
953 set tm [lindex $speed 0] | |
954 if {$tm == 0} { | |
955 set rate [format %20s "many"] | |
956 } else { | |
957 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]] | |
958 } | |
959 set u2 $units/s | |
960 output2 [format {%12d uS %s %s} $tm $rate $u2] | |
961 global total_time | |
962 set total_time [expr {$total_time+$tm}] | |
963 lappend ::speed_trial_times $name $tm | |
964 } | |
965 proc speed_trial_init {name} { | |
966 global total_time | |
967 set total_time 0 | |
968 set ::speed_trial_times [list] | |
969 sqlite3 versdb :memory: | |
970 set vers [versdb one {SELECT sqlite_source_id()}] | |
971 versdb close | |
972 output2 "SQLite $vers" | |
973 } | |
974 proc speed_trial_summary {name} { | |
975 global total_time | |
976 output2 [format {%-21.21s %12d uS TOTAL} $name $total_time] | |
977 | |
978 if { 0 } { | |
979 sqlite3 versdb :memory: | |
980 set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0] | |
981 versdb close | |
982 output2 "CREATE TABLE IF NOT EXISTS time(version, script, test, us);" | |
983 foreach {test us} $::speed_trial_times { | |
984 output2 "INSERT INTO time VALUES('$vers', '$name', '$test', $us);" | |
985 } | |
986 } | |
987 } | |
988 | |
989 # Run this routine last | |
990 # | |
991 proc finish_test {} { | |
992 catch {db close} | |
993 catch {db1 close} | |
994 catch {db2 close} | |
995 catch {db3 close} | |
996 if {0==[info exists ::SLAVE]} { finalize_testing } | |
997 } | |
998 proc finalize_testing {} { | |
999 global sqlite_open_file_count | |
1000 | |
1001 set omitList [set_test_counter omit_list] | |
1002 | |
1003 catch {db close} | |
1004 catch {db2 close} | |
1005 catch {db3 close} | |
1006 | |
1007 vfs_unlink_test | |
1008 sqlite3 db {} | |
1009 # sqlite3_clear_tsd_memdebug | |
1010 db close | |
1011 sqlite3_reset_auto_extension | |
1012 | |
1013 sqlite3_soft_heap_limit 0 | |
1014 set nTest [incr_ntest] | |
1015 set nErr [set_test_counter errors] | |
1016 | |
1017 set nKnown 0 | |
1018 if {[file readable known-problems.txt]} { | |
1019 set fd [open known-problems.txt] | |
1020 set content [read $fd] | |
1021 close $fd | |
1022 foreach x $content {set known_error($x) 1} | |
1023 foreach x [set_test_counter fail_list] { | |
1024 if {[info exists known_error($x)]} {incr nKnown} | |
1025 } | |
1026 } | |
1027 if {$nKnown>0} { | |
1028 output2 "[expr {$nErr-$nKnown}] new errors and $nKnown known errors\ | |
1029 out of $nTest tests" | |
1030 } else { | |
1031 output2 "$nErr errors out of $nTest tests" | |
1032 } | |
1033 if {$nErr>$nKnown} { | |
1034 output2 -nonewline "!Failures on these tests:" | |
1035 foreach x [set_test_counter fail_list] { | |
1036 if {![info exists known_error($x)]} {output2 -nonewline " $x"} | |
1037 } | |
1038 output2 "" | |
1039 } | |
1040 foreach warning [set_test_counter warn_list] { | |
1041 output2 "Warning: $warning" | |
1042 } | |
1043 run_thread_tests 1 | |
1044 if {[llength $omitList]>0} { | |
1045 output2 "Omitted test cases:" | |
1046 set prec {} | |
1047 foreach {rec} [lsort $omitList] { | |
1048 if {$rec==$prec} continue | |
1049 set prec $rec | |
1050 output2 [format {. %-12s %s} [lindex $rec 0] [lindex $rec 1]] | |
1051 } | |
1052 } | |
1053 if {$nErr>0 && ![working_64bit_int]} { | |
1054 output2 "******************************************************************" | |
1055 output2 "N.B.: The version of TCL that you used to build this test harness" | |
1056 output2 "is defective in that it does not support 64-bit integers. Some or" | |
1057 output2 "all of the test failures above might be a result from this defect" | |
1058 output2 "in your TCL build." | |
1059 output2 "******************************************************************" | |
1060 } | |
1061 if {$::cmdlinearg(binarylog)} { | |
1062 vfslog finalize binarylog | |
1063 } | |
1064 if {$sqlite_open_file_count} { | |
1065 output2 "$sqlite_open_file_count files were left open" | |
1066 incr nErr | |
1067 } | |
1068 if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 || | |
1069 [sqlite3_memory_used]>0} { | |
1070 output2 "Unfreed memory: [sqlite3_memory_used] bytes in\ | |
1071 [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations" | |
1072 incr nErr | |
1073 ifcapable memdebug||mem5||(mem3&&debug) { | |
1074 output2 "Writing unfreed memory log to \"./memleak.txt\"" | |
1075 sqlite3_memdebug_dump ./memleak.txt | |
1076 } | |
1077 } else { | |
1078 output2 "All memory allocations freed - no leaks" | |
1079 ifcapable memdebug||mem5 { | |
1080 sqlite3_memdebug_dump ./memusage.txt | |
1081 } | |
1082 } | |
1083 show_memstats | |
1084 output2 "Maximum memory usage: [sqlite3_memory_highwater 1] bytes" | |
1085 output2 "Current memory usage: [sqlite3_memory_highwater] bytes" | |
1086 if {[info commands sqlite3_memdebug_malloc_count] ne ""} { | |
1087 output2 "Number of malloc() : [sqlite3_memdebug_malloc_count] calls" | |
1088 } | |
1089 if {$::cmdlinearg(malloctrace)} { | |
1090 output2 "Writing mallocs.sql..." | |
1091 memdebug_log_sql | |
1092 sqlite3_memdebug_log stop | |
1093 sqlite3_memdebug_log clear | |
1094 | |
1095 if {[sqlite3_memory_used]>0} { | |
1096 output2 "Writing leaks.sql..." | |
1097 sqlite3_memdebug_log sync | |
1098 memdebug_log_sql leaks.sql | |
1099 } | |
1100 } | |
1101 foreach f [glob -nocomplain test.db-*-journal] { | |
1102 forcedelete $f | |
1103 } | |
1104 foreach f [glob -nocomplain test.db-mj*] { | |
1105 forcedelete $f | |
1106 } | |
1107 exit [expr {$nErr>0}] | |
1108 } | |
1109 | |
1110 # Display memory statistics for analysis and debugging purposes. | |
1111 # | |
1112 proc show_memstats {} { | |
1113 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0] | |
1114 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0] | |
1115 set val [format {now %10d max %10d max-size %10d} \ | |
1116 [lindex $x 1] [lindex $x 2] [lindex $y 2]] | |
1117 output1 "Memory used: $val" | |
1118 set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] | |
1119 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]] | |
1120 output1 "Allocation count: $val" | |
1121 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] | |
1122 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0] | |
1123 set val [format {now %10d max %10d max-size %10d} \ | |
1124 [lindex $x 1] [lindex $x 2] [lindex $y 2]] | |
1125 output1 "Page-cache used: $val" | |
1126 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] | |
1127 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]] | |
1128 output1 "Page-cache overflow: $val" | |
1129 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] | |
1130 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]] | |
1131 output1 "Scratch memory used: $val" | |
1132 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0] | |
1133 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0] | |
1134 set val [format {now %10d max %10d max-size %10d} \ | |
1135 [lindex $x 1] [lindex $x 2] [lindex $y 2]] | |
1136 output1 "Scratch overflow: $val" | |
1137 ifcapable yytrackmaxstackdepth { | |
1138 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0] | |
1139 set val [format { max %10d} [lindex $x 2]] | |
1140 output2 "Parser stack depth: $val" | |
1141 } | |
1142 } | |
1143 | |
1144 # A procedure to execute SQL | |
1145 # | |
1146 proc execsql {sql {db db}} { | |
1147 # puts "SQL = $sql" | |
1148 uplevel [list $db eval $sql] | |
1149 } | |
1150 proc execsql_timed {sql {db db}} { | |
1151 set tm [time { | |
1152 set x [uplevel [list $db eval $sql]] | |
1153 } 1] | |
1154 set tm [lindex $tm 0] | |
1155 output1 -nonewline " ([expr {$tm*0.001}]ms) " | |
1156 set x | |
1157 } | |
1158 | |
1159 # Execute SQL and catch exceptions. | |
1160 # | |
1161 proc catchsql {sql {db db}} { | |
1162 # puts "SQL = $sql" | |
1163 set r [catch [list uplevel [list $db eval $sql]] msg] | |
1164 lappend r $msg | |
1165 return $r | |
1166 } | |
1167 | |
1168 # Do an VDBE code dump on the SQL given | |
1169 # | |
1170 proc explain {sql {db db}} { | |
1171 output2 "" | |
1172 output2 "addr opcode p1 p2 p3 p4 p5 #" | |
1173 output2 "---- ------------ ------ ------ ------ --------------- -- -" | |
1174 $db eval "explain $sql" {} { | |
1175 output2 [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \ | |
1176 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment | |
1177 ] | |
1178 } | |
1179 } | |
1180 | |
1181 proc explain_i {sql {db db}} { | |
1182 output2 "" | |
1183 output2 "addr opcode p1 p2 p3 p4 p5 #" | |
1184 output2 "---- ------------ ------ ------ ------ ---------------- -- -" | |
1185 | |
1186 | |
1187 # Set up colors for the different opcodes. Scheme is as follows: | |
1188 # | |
1189 # Red: Opcodes that write to a b-tree. | |
1190 # Blue: Opcodes that reposition or seek a cursor. | |
1191 # Green: The ResultRow opcode. | |
1192 # | |
1193 if { [catch {fconfigure stdout -mode}]==0 } { | |
1194 set R "\033\[31;1m" ;# Red fg | |
1195 set G "\033\[32;1m" ;# Green fg | |
1196 set B "\033\[34;1m" ;# Red fg | |
1197 set D "\033\[39;0m" ;# Default fg | |
1198 } else { | |
1199 set R "" | |
1200 set G "" | |
1201 set B "" | |
1202 set D "" | |
1203 } | |
1204 foreach opcode { | |
1205 Seek SeekGe SeekGt SeekLe SeekLt NotFound Last Rewind | |
1206 NoConflict Next Prev VNext VPrev VFilter | |
1207 SorterSort SorterNext | |
1208 } { | |
1209 set color($opcode) $B | |
1210 } | |
1211 foreach opcode {ResultRow} { | |
1212 set color($opcode) $G | |
1213 } | |
1214 foreach opcode {IdxInsert Insert Delete IdxDelete} { | |
1215 set color($opcode) $R | |
1216 } | |
1217 | |
1218 set bSeenGoto 0 | |
1219 $db eval "explain $sql" {} { | |
1220 set x($addr) 0 | |
1221 set op($addr) $opcode | |
1222 | |
1223 if {$opcode == "Goto" && ($bSeenGoto==0 || ($p2 > $addr+10))} { | |
1224 set linebreak($p2) 1 | |
1225 set bSeenGoto 1 | |
1226 } | |
1227 | |
1228 if {$opcode=="Next" || $opcode=="Prev" | |
1229 || $opcode=="VNext" || $opcode=="VPrev" | |
1230 || $opcode=="SorterNext" | |
1231 } { | |
1232 for {set i $p2} {$i<$addr} {incr i} { | |
1233 incr x($i) 2 | |
1234 } | |
1235 } | |
1236 | |
1237 if {$opcode == "Goto" && $p2<$addr && $op($p2)=="Yield"} { | |
1238 for {set i [expr $p2+1]} {$i<$addr} {incr i} { | |
1239 incr x($i) 2 | |
1240 } | |
1241 } | |
1242 | |
1243 if {$opcode == "Halt" && $comment == "End of coroutine"} { | |
1244 set linebreak([expr $addr+1]) 1 | |
1245 } | |
1246 } | |
1247 | |
1248 $db eval "explain $sql" {} { | |
1249 if {[info exists linebreak($addr)]} { | |
1250 output2 "" | |
1251 } | |
1252 set I [string repeat " " $x($addr)] | |
1253 | |
1254 set col "" | |
1255 catch { set col $color($opcode) } | |
1256 | |
1257 output2 [format {%-4d %s%s%-12.12s%s %-6d %-6d %-6d % -17s %s %s} \ | |
1258 $addr $I $col $opcode $D $p1 $p2 $p3 $p4 $p5 $comment | |
1259 ] | |
1260 } | |
1261 output2 "---- ------------ ------ ------ ------ ---------------- -- -" | |
1262 } | |
1263 | |
1264 # Show the VDBE program for an SQL statement but omit the Trace | |
1265 # opcode at the beginning. This procedure can be used to prove | |
1266 # that different SQL statements generate exactly the same VDBE code. | |
1267 # | |
1268 proc explain_no_trace {sql} { | |
1269 set tr [db eval "EXPLAIN $sql"] | |
1270 return [lrange $tr 7 end] | |
1271 } | |
1272 | |
1273 # Another procedure to execute SQL. This one includes the field | |
1274 # names in the returned list. | |
1275 # | |
1276 proc execsql2 {sql} { | |
1277 set result {} | |
1278 db eval $sql data { | |
1279 foreach f $data(*) { | |
1280 lappend result $f $data($f) | |
1281 } | |
1282 } | |
1283 return $result | |
1284 } | |
1285 | |
1286 # Use a temporary in-memory database to execute SQL statements | |
1287 # | |
1288 proc memdbsql {sql} { | |
1289 sqlite3 memdb :memory: | |
1290 set result [memdb eval $sql] | |
1291 memdb close | |
1292 return $result | |
1293 } | |
1294 | |
1295 # Use the non-callback API to execute multiple SQL statements | |
1296 # | |
1297 proc stepsql {dbptr sql} { | |
1298 set sql [string trim $sql] | |
1299 set r 0 | |
1300 while {[string length $sql]>0} { | |
1301 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} { | |
1302 return [list 1 $vm] | |
1303 } | |
1304 set sql [string trim $sqltail] | |
1305 # while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} { | |
1306 # foreach v $VAL {lappend r $v} | |
1307 # } | |
1308 while {[sqlite3_step $vm]=="SQLITE_ROW"} { | |
1309 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} { | |
1310 lappend r [sqlite3_column_text $vm $i] | |
1311 } | |
1312 } | |
1313 if {[catch {sqlite3_finalize $vm} errmsg]} { | |
1314 return [list 1 $errmsg] | |
1315 } | |
1316 } | |
1317 return $r | |
1318 } | |
1319 | |
1320 # Do an integrity check of the entire database | |
1321 # | |
1322 proc integrity_check {name {db db}} { | |
1323 ifcapable integrityck { | |
1324 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok} | |
1325 } | |
1326 } | |
1327 | |
1328 # Check the extended error code | |
1329 # | |
1330 proc verify_ex_errcode {name expected {db db}} { | |
1331 do_test $name [list sqlite3_extended_errcode $db] $expected | |
1332 } | |
1333 | |
1334 | |
1335 # Return true if the SQL statement passed as the second argument uses a | |
1336 # statement transaction. | |
1337 # | |
1338 proc sql_uses_stmt {db sql} { | |
1339 set stmt [sqlite3_prepare $db $sql -1 dummy] | |
1340 set uses [uses_stmt_journal $stmt] | |
1341 sqlite3_finalize $stmt | |
1342 return $uses | |
1343 } | |
1344 | |
1345 proc fix_ifcapable_expr {expr} { | |
1346 set ret "" | |
1347 set state 0 | |
1348 for {set i 0} {$i < [string length $expr]} {incr i} { | |
1349 set char [string range $expr $i $i] | |
1350 set newstate [expr {[string is alnum $char] || $char eq "_"}] | |
1351 if {$newstate && !$state} { | |
1352 append ret {$::sqlite_options(} | |
1353 } | |
1354 if {!$newstate && $state} { | |
1355 append ret ) | |
1356 } | |
1357 append ret $char | |
1358 set state $newstate | |
1359 } | |
1360 if {$state} {append ret )} | |
1361 return $ret | |
1362 } | |
1363 | |
1364 # Returns non-zero if the capabilities are present; zero otherwise. | |
1365 # | |
1366 proc capable {expr} { | |
1367 set e [fix_ifcapable_expr $expr]; return [expr ($e)] | |
1368 } | |
1369 | |
1370 # Evaluate a boolean expression of capabilities. If true, execute the | |
1371 # code. Omit the code if false. | |
1372 # | |
1373 proc ifcapable {expr code {else ""} {elsecode ""}} { | |
1374 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2 | |
1375 set e2 [fix_ifcapable_expr $expr] | |
1376 if ($e2) { | |
1377 set c [catch {uplevel 1 $code} r] | |
1378 } else { | |
1379 set c [catch {uplevel 1 $elsecode} r] | |
1380 } | |
1381 return -code $c $r | |
1382 } | |
1383 | |
1384 # This proc execs a seperate process that crashes midway through executing | |
1385 # the SQL script $sql on database test.db. | |
1386 # | |
1387 # The crash occurs during a sync() of file $crashfile. When the crash | |
1388 # occurs a random subset of all unsynced writes made by the process are | |
1389 # written into the files on disk. Argument $crashdelay indicates the | |
1390 # number of file syncs to wait before crashing. | |
1391 # | |
1392 # The return value is a list of two elements. The first element is a | |
1393 # boolean, indicating whether or not the process actually crashed or | |
1394 # reported some other error. The second element in the returned list is the | |
1395 # error message. This is "child process exited abnormally" if the crash | |
1396 # occurred. | |
1397 # | |
1398 # crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql | |
1399 # | |
1400 proc crashsql {args} { | |
1401 | |
1402 set blocksize "" | |
1403 set crashdelay 1 | |
1404 set prngseed 0 | |
1405 set opendb { sqlite3 db test.db -vfs crash } | |
1406 set tclbody {} | |
1407 set crashfile "" | |
1408 set dc "" | |
1409 set sql [lindex $args end] | |
1410 | |
1411 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} { | |
1412 set z [lindex $args $ii] | |
1413 set n [string length $z] | |
1414 set z2 [lindex $args [expr $ii+1]] | |
1415 | |
1416 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \ | |
1417 elseif {$n>1 && [string first $z -opendb]==0} {set opendb $z2} \ | |
1418 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \ | |
1419 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \ | |
1420 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \ | |
1421 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \ | |
1422 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" }
\ | |
1423 else { error "Unrecognized option: $z" } | |
1424 } | |
1425 | |
1426 if {$crashfile eq ""} { | |
1427 error "Compulsory option -file missing" | |
1428 } | |
1429 | |
1430 # $crashfile gets compared to the native filename in | |
1431 # cfSync(), which can be different then what TCL uses by | |
1432 # default, so here we force it to the "nativename" format. | |
1433 set cfile [string map {\\ \\\\} [file nativename [file join [get_pwd] $crashfi
le]]] | |
1434 | |
1435 set f [open crash.tcl w] | |
1436 puts $f "sqlite3_crash_enable 1" | |
1437 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile" | |
1438 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte" | |
1439 | |
1440 # This block sets the cache size of the main database to 10 | |
1441 # pages. This is done in case the build is configured to omit | |
1442 # "PRAGMA cache_size". | |
1443 if {$opendb!=""} { | |
1444 puts $f $opendb | |
1445 puts $f {db eval {SELECT * FROM sqlite_master;}} | |
1446 puts $f {set bt [btree_from_db db]} | |
1447 puts $f {btree_set_cache_size $bt 10} | |
1448 } | |
1449 | |
1450 if {$prngseed} { | |
1451 set seed [expr {$prngseed%10007+1}] | |
1452 # puts seed=$seed | |
1453 puts $f "db eval {SELECT randomblob($seed)}" | |
1454 } | |
1455 | |
1456 if {[string length $tclbody]>0} { | |
1457 puts $f $tclbody | |
1458 } | |
1459 if {[string length $sql]>0} { | |
1460 puts $f "db eval {" | |
1461 puts $f "$sql" | |
1462 puts $f "}" | |
1463 } | |
1464 close $f | |
1465 set r [catch { | |
1466 exec [info nameofexec] crash.tcl >@stdout | |
1467 } msg] | |
1468 | |
1469 # Windows/ActiveState TCL returns a slightly different | |
1470 # error message. We map that to the expected message | |
1471 # so that we don't have to change all of the test | |
1472 # cases. | |
1473 if {$::tcl_platform(platform)=="windows"} { | |
1474 if {$msg=="child killed: unknown signal"} { | |
1475 set msg "child process exited abnormally" | |
1476 } | |
1477 } | |
1478 | |
1479 lappend r $msg | |
1480 } | |
1481 | |
1482 proc run_ioerr_prep {} { | |
1483 set ::sqlite_io_error_pending 0 | |
1484 catch {db close} | |
1485 catch {db2 close} | |
1486 catch {forcedelete test.db} | |
1487 catch {forcedelete test.db-journal} | |
1488 catch {forcedelete test2.db} | |
1489 catch {forcedelete test2.db-journal} | |
1490 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db] | |
1491 sqlite3_extended_result_codes $::DB $::ioerropts(-erc) | |
1492 if {[info exists ::ioerropts(-tclprep)]} { | |
1493 eval $::ioerropts(-tclprep) | |
1494 } | |
1495 if {[info exists ::ioerropts(-sqlprep)]} { | |
1496 execsql $::ioerropts(-sqlprep) | |
1497 } | |
1498 expr 0 | |
1499 } | |
1500 | |
1501 # Usage: do_ioerr_test <test number> <options...> | |
1502 # | |
1503 # This proc is used to implement test cases that check that IO errors | |
1504 # are correctly handled. The first argument, <test number>, is an integer | |
1505 # used to name the tests executed by this proc. Options are as follows: | |
1506 # | |
1507 # -tclprep TCL script to run to prepare test. | |
1508 # -sqlprep SQL script to run to prepare test. | |
1509 # -tclbody TCL script to run with IO error simulation. | |
1510 # -sqlbody TCL script to run with IO error simulation. | |
1511 # -exclude List of 'N' values not to test. | |
1512 # -erc Use extended result codes | |
1513 # -persist Make simulated I/O errors persistent | |
1514 # -start Value of 'N' to begin with (default 1) | |
1515 # | |
1516 # -cksum Boolean. If true, test that the database does | |
1517 # not change during the execution of the test case. | |
1518 # | |
1519 proc do_ioerr_test {testname args} { | |
1520 | |
1521 set ::ioerropts(-start) 1 | |
1522 set ::ioerropts(-cksum) 0 | |
1523 set ::ioerropts(-erc) 0 | |
1524 set ::ioerropts(-count) 100000000 | |
1525 set ::ioerropts(-persist) 1 | |
1526 set ::ioerropts(-ckrefcount) 0 | |
1527 set ::ioerropts(-restoreprng) 1 | |
1528 array set ::ioerropts $args | |
1529 | |
1530 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are | |
1531 # a couple of obscure IO errors that do not return them. | |
1532 set ::ioerropts(-erc) 0 | |
1533 | |
1534 # Create a single TCL script from the TCL and SQL specified | |
1535 # as the body of the test. | |
1536 set ::ioerrorbody {} | |
1537 if {[info exists ::ioerropts(-tclbody)]} { | |
1538 append ::ioerrorbody "$::ioerropts(-tclbody)\n" | |
1539 } | |
1540 if {[info exists ::ioerropts(-sqlbody)]} { | |
1541 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}" | |
1542 } | |
1543 | |
1544 save_prng_state | |
1545 if {$::ioerropts(-cksum)} { | |
1546 run_ioerr_prep | |
1547 eval $::ioerrorbody | |
1548 set ::goodcksum [cksum] | |
1549 } | |
1550 | |
1551 set ::go 1 | |
1552 #reset_prng_state | |
1553 for {set n $::ioerropts(-start)} {$::go} {incr n} { | |
1554 set ::TN $n | |
1555 incr ::ioerropts(-count) -1 | |
1556 if {$::ioerropts(-count)<0} break | |
1557 | |
1558 # Skip this IO error if it was specified with the "-exclude" option. | |
1559 if {[info exists ::ioerropts(-exclude)]} { | |
1560 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue | |
1561 } | |
1562 if {$::ioerropts(-restoreprng)} { | |
1563 restore_prng_state | |
1564 } | |
1565 | |
1566 # Delete the files test.db and test2.db, then execute the TCL and | |
1567 # SQL (in that order) to prepare for the test case. | |
1568 do_test $testname.$n.1 { | |
1569 run_ioerr_prep | |
1570 } {0} | |
1571 | |
1572 # Read the 'checksum' of the database. | |
1573 if {$::ioerropts(-cksum)} { | |
1574 set ::checksum [cksum] | |
1575 } | |
1576 | |
1577 # Set the Nth IO error to fail. | |
1578 do_test $testname.$n.2 [subst { | |
1579 set ::sqlite_io_error_persist $::ioerropts(-persist) | |
1580 set ::sqlite_io_error_pending $n | |
1581 }] $n | |
1582 | |
1583 # Execute the TCL script created for the body of this test. If | |
1584 # at least N IO operations performed by SQLite as a result of | |
1585 # the script, the Nth will fail. | |
1586 do_test $testname.$n.3 { | |
1587 set ::sqlite_io_error_hit 0 | |
1588 set ::sqlite_io_error_hardhit 0 | |
1589 set r [catch $::ioerrorbody msg] | |
1590 set ::errseen $r | |
1591 set rc [sqlite3_errcode $::DB] | |
1592 if {$::ioerropts(-erc)} { | |
1593 # If we are in extended result code mode, make sure all of the | |
1594 # IOERRs we get back really do have their extended code values. | |
1595 # If an extended result code is returned, the sqlite3_errcode | |
1596 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn | |
1597 # where nnnn is a number | |
1598 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} { | |
1599 return $rc | |
1600 } | |
1601 } else { | |
1602 # If we are not in extended result code mode, make sure no | |
1603 # extended error codes are returned. | |
1604 if {[regexp {\+\d} $rc]} { | |
1605 return $rc | |
1606 } | |
1607 } | |
1608 # The test repeats as long as $::go is non-zero. $::go starts out | |
1609 # as 1. When a test runs to completion without hitting an I/O | |
1610 # error, that means there is no point in continuing with this test | |
1611 # case so set $::go to zero. | |
1612 # | |
1613 if {$::sqlite_io_error_pending>0} { | |
1614 set ::go 0 | |
1615 set q 0 | |
1616 set ::sqlite_io_error_pending 0 | |
1617 } else { | |
1618 set q 1 | |
1619 } | |
1620 | |
1621 set s [expr $::sqlite_io_error_hit==0] | |
1622 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} { | |
1623 set r 1 | |
1624 } | |
1625 set ::sqlite_io_error_hit 0 | |
1626 | |
1627 # One of two things must have happened. either | |
1628 # 1. We never hit the IO error and the SQL returned OK | |
1629 # 2. An IO error was hit and the SQL failed | |
1630 # | |
1631 #puts "s=$s r=$r q=$q" | |
1632 expr { ($s && !$r && !$q) || (!$s && $r && $q) } | |
1633 } {1} | |
1634 | |
1635 set ::sqlite_io_error_hit 0 | |
1636 set ::sqlite_io_error_pending 0 | |
1637 | |
1638 # Check that no page references were leaked. There should be | |
1639 # a single reference if there is still an active transaction, | |
1640 # or zero otherwise. | |
1641 # | |
1642 # UPDATE: If the IO error occurs after a 'BEGIN' but before any | |
1643 # locks are established on database files (i.e. if the error | |
1644 # occurs while attempting to detect a hot-journal file), then | |
1645 # there may 0 page references and an active transaction according | |
1646 # to [sqlite3_get_autocommit]. | |
1647 # | |
1648 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} { | |
1649 do_test $testname.$n.4 { | |
1650 set bt [btree_from_db db] | |
1651 db_enter db | |
1652 array set stats [btree_pager_stats $bt] | |
1653 db_leave db | |
1654 set nRef $stats(ref) | |
1655 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)} | |
1656 } {1} | |
1657 } | |
1658 | |
1659 # If there is an open database handle and no open transaction, | |
1660 # and the pager is not running in exclusive-locking mode, | |
1661 # check that the pager is in "unlocked" state. Theoretically, | |
1662 # if a call to xUnlock() failed due to an IO error the underlying | |
1663 # file may still be locked. | |
1664 # | |
1665 ifcapable pragma { | |
1666 if { [info commands db] ne "" | |
1667 && $::ioerropts(-ckrefcount) | |
1668 && [db one {pragma locking_mode}] eq "normal" | |
1669 && [sqlite3_get_autocommit db] | |
1670 } { | |
1671 do_test $testname.$n.5 { | |
1672 set bt [btree_from_db db] | |
1673 db_enter db | |
1674 array set stats [btree_pager_stats $bt] | |
1675 db_leave db | |
1676 set stats(state) | |
1677 } 0 | |
1678 } | |
1679 } | |
1680 | |
1681 # If an IO error occurred, then the checksum of the database should | |
1682 # be the same as before the script that caused the IO error was run. | |
1683 # | |
1684 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} { | |
1685 do_test $testname.$n.6 { | |
1686 catch {db close} | |
1687 catch {db2 close} | |
1688 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db] | |
1689 set nowcksum [cksum] | |
1690 set res [expr {$nowcksum==$::checksum || $nowcksum==$::goodcksum}] | |
1691 if {$res==0} { | |
1692 output2 "now=$nowcksum" | |
1693 output2 "the=$::checksum" | |
1694 output2 "fwd=$::goodcksum" | |
1695 } | |
1696 set res | |
1697 } 1 | |
1698 } | |
1699 | |
1700 set ::sqlite_io_error_hardhit 0 | |
1701 set ::sqlite_io_error_pending 0 | |
1702 if {[info exists ::ioerropts(-cleanup)]} { | |
1703 catch $::ioerropts(-cleanup) | |
1704 } | |
1705 } | |
1706 set ::sqlite_io_error_pending 0 | |
1707 set ::sqlite_io_error_persist 0 | |
1708 unset ::ioerropts | |
1709 } | |
1710 | |
1711 # Return a checksum based on the contents of the main database associated | |
1712 # with connection $db | |
1713 # | |
1714 proc cksum {{db db}} { | |
1715 set txt [$db eval { | |
1716 SELECT name, type, sql FROM sqlite_master order by name | |
1717 }]\n | |
1718 foreach tbl [$db eval { | |
1719 SELECT name FROM sqlite_master WHERE type='table' order by name | |
1720 }] { | |
1721 append txt [$db eval "SELECT * FROM $tbl"]\n | |
1722 } | |
1723 foreach prag {default_synchronous default_cache_size} { | |
1724 append txt $prag-[$db eval "PRAGMA $prag"]\n | |
1725 } | |
1726 set cksum [string length $txt]-[md5 $txt] | |
1727 # puts $cksum-[file size test.db] | |
1728 return $cksum | |
1729 } | |
1730 | |
1731 # Generate a checksum based on the contents of the main and temp tables | |
1732 # database $db. If the checksum of two databases is the same, and the | |
1733 # integrity-check passes for both, the two databases are identical. | |
1734 # | |
1735 proc allcksum {{db db}} { | |
1736 set ret [list] | |
1737 ifcapable tempdb { | |
1738 set sql { | |
1739 SELECT name FROM sqlite_master WHERE type = 'table' UNION | |
1740 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION | |
1741 SELECT 'sqlite_master' UNION | |
1742 SELECT 'sqlite_temp_master' ORDER BY 1 | |
1743 } | |
1744 } else { | |
1745 set sql { | |
1746 SELECT name FROM sqlite_master WHERE type = 'table' UNION | |
1747 SELECT 'sqlite_master' ORDER BY 1 | |
1748 } | |
1749 } | |
1750 set tbllist [$db eval $sql] | |
1751 set txt {} | |
1752 foreach tbl $tbllist { | |
1753 append txt [$db eval "SELECT * FROM $tbl"] | |
1754 } | |
1755 foreach prag {default_cache_size} { | |
1756 append txt $prag-[$db eval "PRAGMA $prag"]\n | |
1757 } | |
1758 # puts txt=$txt | |
1759 return [md5 $txt] | |
1760 } | |
1761 | |
1762 # Generate a checksum based on the contents of a single database with | |
1763 # a database connection. The name of the database is $dbname. | |
1764 # Examples of $dbname are "temp" or "main". | |
1765 # | |
1766 proc dbcksum {db dbname} { | |
1767 if {$dbname=="temp"} { | |
1768 set master sqlite_temp_master | |
1769 } else { | |
1770 set master $dbname.sqlite_master | |
1771 } | |
1772 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"] | |
1773 set txt [$db eval "SELECT * FROM $master"]\n | |
1774 foreach tab $alltab { | |
1775 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n | |
1776 } | |
1777 return [md5 $txt] | |
1778 } | |
1779 | |
1780 proc memdebug_log_sql {{filename mallocs.sql}} { | |
1781 | |
1782 set data [sqlite3_memdebug_log dump] | |
1783 set nFrame [expr [llength [lindex $data 0]]-2] | |
1784 if {$nFrame < 0} { return "" } | |
1785 | |
1786 set database temp | |
1787 | |
1788 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);" | |
1789 | |
1790 set sql "" | |
1791 foreach e $data { | |
1792 set nCall [lindex $e 0] | |
1793 set nByte [lindex $e 1] | |
1794 set lStack [lrange $e 2 end] | |
1795 append sql "INSERT INTO ${database}.malloc VALUES" | |
1796 append sql "('test', $nCall, $nByte, '$lStack');\n" | |
1797 foreach f $lStack { | |
1798 set frames($f) 1 | |
1799 } | |
1800 } | |
1801 | |
1802 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n" | |
1803 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n" | |
1804 | |
1805 foreach f [array names frames] { | |
1806 set addr [format %x $f] | |
1807 set cmd "addr2line -e [info nameofexec] $addr" | |
1808 set line [eval exec $cmd] | |
1809 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n" | |
1810 | |
1811 set file [lindex [split $line :] 0] | |
1812 set files($file) 1 | |
1813 } | |
1814 | |
1815 foreach f [array names files] { | |
1816 set contents "" | |
1817 catch { | |
1818 set fd [open $f] | |
1819 set contents [read $fd] | |
1820 close $fd | |
1821 } | |
1822 set contents [string map {' ''} $contents] | |
1823 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n" | |
1824 } | |
1825 | |
1826 set fd [open $filename w] | |
1827 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;" | |
1828 close $fd | |
1829 } | |
1830 | |
1831 # Drop all tables in database [db] | |
1832 proc drop_all_tables {{db db}} { | |
1833 ifcapable trigger&&foreignkey { | |
1834 set pk [$db one "PRAGMA foreign_keys"] | |
1835 $db eval "PRAGMA foreign_keys = OFF" | |
1836 } | |
1837 foreach {idx name file} [db eval {PRAGMA database_list}] { | |
1838 if {$idx==1} { | |
1839 set master sqlite_temp_master | |
1840 } else { | |
1841 set master $name.sqlite_master | |
1842 } | |
1843 foreach {t type} [$db eval " | |
1844 SELECT name, type FROM $master | |
1845 WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X' | |
1846 "] { | |
1847 $db eval "DROP $type \"$t\"" | |
1848 } | |
1849 } | |
1850 ifcapable trigger&&foreignkey { | |
1851 $db eval "PRAGMA foreign_keys = $pk" | |
1852 } | |
1853 } | |
1854 | |
1855 #------------------------------------------------------------------------- | |
1856 # If a test script is executed with global variable $::G(perm:name) set to | |
1857 # "wal", then the tests are run in WAL mode. Otherwise, they should be run | |
1858 # in rollback mode. The following Tcl procs are used to make this less | |
1859 # intrusive: | |
1860 # | |
1861 # wal_set_journal_mode ?DB? | |
1862 # | |
1863 # If running a WAL test, execute "PRAGMA journal_mode = wal" using | |
1864 # connection handle DB. Otherwise, this command is a no-op. | |
1865 # | |
1866 # wal_check_journal_mode TESTNAME ?DB? | |
1867 # | |
1868 # If running a WAL test, execute a tests case that fails if the main | |
1869 # database for connection handle DB is not currently a WAL database. | |
1870 # Otherwise (if not running a WAL permutation) this is a no-op. | |
1871 # | |
1872 # wal_is_wal_mode | |
1873 # | |
1874 # Returns true if this test should be run in WAL mode. False otherwise. | |
1875 # | |
1876 proc wal_is_wal_mode {} { | |
1877 expr {[permutation] eq "wal"} | |
1878 } | |
1879 proc wal_set_journal_mode {{db db}} { | |
1880 if { [wal_is_wal_mode] } { | |
1881 $db eval "PRAGMA journal_mode = WAL" | |
1882 } | |
1883 } | |
1884 proc wal_check_journal_mode {testname {db db}} { | |
1885 if { [wal_is_wal_mode] } { | |
1886 $db eval { SELECT * FROM sqlite_master } | |
1887 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal} | |
1888 } | |
1889 } | |
1890 | |
1891 proc permutation {} { | |
1892 set perm "" | |
1893 catch {set perm $::G(perm:name)} | |
1894 set perm | |
1895 } | |
1896 proc presql {} { | |
1897 set presql "" | |
1898 catch {set presql $::G(perm:presql)} | |
1899 set presql | |
1900 } | |
1901 | |
1902 #------------------------------------------------------------------------- | |
1903 # | |
1904 proc slave_test_script {script} { | |
1905 | |
1906 # Create the interpreter used to run the test script. | |
1907 interp create tinterp | |
1908 | |
1909 # Populate some global variables that tester.tcl expects to see. | |
1910 foreach {var value} [list \ | |
1911 ::argv0 $::argv0 \ | |
1912 ::argv {} \ | |
1913 ::SLAVE 1 \ | |
1914 ] { | |
1915 interp eval tinterp [list set $var $value] | |
1916 } | |
1917 | |
1918 # If output is being copied into a file, share the file-descriptor with | |
1919 # the interpreter. | |
1920 if {[info exists ::G(output_fd)]} { | |
1921 interp share {} $::G(output_fd) tinterp | |
1922 } | |
1923 | |
1924 # The alias used to access the global test counters. | |
1925 tinterp alias set_test_counter set_test_counter | |
1926 | |
1927 # Set up the ::cmdlinearg array in the slave. | |
1928 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]] | |
1929 | |
1930 # Set up the ::G array in the slave. | |
1931 interp eval tinterp [list array set ::G [array get ::G]] | |
1932 | |
1933 # Load the various test interfaces implemented in C. | |
1934 load_testfixture_extensions tinterp | |
1935 | |
1936 # Run the test script. | |
1937 interp eval tinterp $script | |
1938 | |
1939 # Check if the interpreter call [run_thread_tests] | |
1940 if { [interp eval tinterp {info exists ::run_thread_tests_called}] } { | |
1941 set ::run_thread_tests_called 1 | |
1942 } | |
1943 | |
1944 # Delete the interpreter used to run the test script. | |
1945 interp delete tinterp | |
1946 } | |
1947 | |
1948 proc slave_test_file {zFile} { | |
1949 set tail [file tail $zFile] | |
1950 | |
1951 if {[info exists ::G(start:permutation)]} { | |
1952 if {[permutation] != $::G(start:permutation)} return | |
1953 unset ::G(start:permutation) | |
1954 } | |
1955 if {[info exists ::G(start:file)]} { | |
1956 if {$tail != $::G(start:file) && $tail!="$::G(start:file).test"} return | |
1957 unset ::G(start:file) | |
1958 } | |
1959 | |
1960 # Remember the value of the shared-cache setting. So that it is possible | |
1961 # to check afterwards that it was not modified by the test script. | |
1962 # | |
1963 ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] } | |
1964 | |
1965 # Run the test script in a slave interpreter. | |
1966 # | |
1967 unset -nocomplain ::run_thread_tests_called | |
1968 reset_prng_state | |
1969 set ::sqlite_open_file_count 0 | |
1970 set time [time { slave_test_script [list source $zFile] }] | |
1971 set ms [expr [lindex $time 0] / 1000] | |
1972 | |
1973 # Test that all files opened by the test script were closed. Omit this | |
1974 # if the test script has "thread" in its name. The open file counter | |
1975 # is not thread-safe. | |
1976 # | |
1977 if {[info exists ::run_thread_tests_called]==0} { | |
1978 do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0} | |
1979 } | |
1980 set ::sqlite_open_file_count 0 | |
1981 | |
1982 # Test that the global "shared-cache" setting was not altered by | |
1983 # the test script. | |
1984 # | |
1985 ifcapable shared_cache { | |
1986 set res [expr {[sqlite3_enable_shared_cache] == $scs}] | |
1987 do_test ${tail}-sharedcachesetting [list set {} $res] 1 | |
1988 } | |
1989 | |
1990 # Add some info to the output. | |
1991 # | |
1992 output2 "Time: $tail $ms ms" | |
1993 show_memstats | |
1994 } | |
1995 | |
1996 # Open a new connection on database test.db and execute the SQL script | |
1997 # supplied as an argument. Before returning, close the new conection and | |
1998 # restore the 4 byte fields starting at header offsets 28, 92 and 96 | |
1999 # to the values they held before the SQL was executed. This simulates | |
2000 # a write by a pre-3.7.0 client. | |
2001 # | |
2002 proc sql36231 {sql} { | |
2003 set B [hexio_read test.db 92 8] | |
2004 set A [hexio_read test.db 28 4] | |
2005 sqlite3 db36231 test.db | |
2006 catch { db36231 func a_string a_string } | |
2007 execsql $sql db36231 | |
2008 db36231 close | |
2009 hexio_write test.db 28 $A | |
2010 hexio_write test.db 92 $B | |
2011 return "" | |
2012 } | |
2013 | |
2014 proc db_save {} { | |
2015 foreach f [glob -nocomplain sv_test.db*] { forcedelete $f } | |
2016 foreach f [glob -nocomplain test.db*] { | |
2017 set f2 "sv_$f" | |
2018 forcecopy $f $f2 | |
2019 } | |
2020 } | |
2021 proc db_save_and_close {} { | |
2022 db_save | |
2023 catch { db close } | |
2024 return "" | |
2025 } | |
2026 proc db_restore {} { | |
2027 foreach f [glob -nocomplain test.db*] { forcedelete $f } | |
2028 foreach f2 [glob -nocomplain sv_test.db*] { | |
2029 set f [string range $f2 3 end] | |
2030 forcecopy $f2 $f | |
2031 } | |
2032 } | |
2033 proc db_restore_and_reopen {{dbfile test.db}} { | |
2034 catch { db close } | |
2035 db_restore | |
2036 sqlite3 db $dbfile | |
2037 } | |
2038 proc db_delete_and_reopen {{file test.db}} { | |
2039 catch { db close } | |
2040 foreach f [glob -nocomplain test.db*] { forcedelete $f } | |
2041 sqlite3 db $file | |
2042 } | |
2043 | |
2044 # Close any connections named [db], [db2] or [db3]. Then use sqlite3_config | |
2045 # to configure the size of the PAGECACHE allocation using the parameters | |
2046 # provided to this command. Save the old PAGECACHE parameters in a global | |
2047 # variable so that [test_restore_config_pagecache] can restore the previous | |
2048 # configuration. | |
2049 # | |
2050 # Before returning, reopen connection [db] on file test.db. | |
2051 # | |
2052 proc test_set_config_pagecache {sz nPg} { | |
2053 catch {db close} | |
2054 catch {db2 close} | |
2055 catch {db3 close} | |
2056 | |
2057 sqlite3_shutdown | |
2058 set ::old_pagecache_config [sqlite3_config_pagecache $sz $nPg] | |
2059 sqlite3_initialize | |
2060 autoinstall_test_functions | |
2061 reset_db | |
2062 } | |
2063 | |
2064 # Close any connections named [db], [db2] or [db3]. Then use sqlite3_config | |
2065 # to configure the size of the PAGECACHE allocation to the size saved in | |
2066 # the global variable by an earlier call to [test_set_config_pagecache]. | |
2067 # | |
2068 # Before returning, reopen connection [db] on file test.db. | |
2069 # | |
2070 proc test_restore_config_pagecache {} { | |
2071 catch {db close} | |
2072 catch {db2 close} | |
2073 catch {db3 close} | |
2074 | |
2075 sqlite3_shutdown | |
2076 eval sqlite3_config_pagecache $::old_pagecache_config | |
2077 unset ::old_pagecache_config | |
2078 sqlite3_initialize | |
2079 autoinstall_test_functions | |
2080 sqlite3 db test.db | |
2081 } | |
2082 | |
2083 # If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set | |
2084 # to non-zero, then set the global variable $AUTOVACUUM to 1. | |
2085 set AUTOVACUUM $sqlite_options(default_autovacuum) | |
2086 | |
2087 # Make sure the FTS enhanced query syntax is disabled. | |
2088 set sqlite_fts3_enable_parentheses 0 | |
2089 | |
2090 # During testing, assume that all database files are well-formed. The | |
2091 # few test cases that deliberately corrupt database files should rescind | |
2092 # this setting by invoking "database_can_be_corrupt" | |
2093 # | |
2094 database_never_corrupt | |
2095 | |
2096 source $testdir/thread_common.tcl | |
2097 source $testdir/malloc_common.tcl | |
OLD | NEW |