sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 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 | # drop_all_indexes ?DB? |
| 29 | # forcecopy FROM TO |
| 30 | # forcedelete FILENAME |
| 31 | # |
| 32 | # Test the capability of the SQLite version built into the interpreter to |
| 33 | # determine if a specific test can be run: |
| 34 | # |
| 35 | # capable EXPR |
| 36 | # ifcapable EXPR |
| 37 | # |
| 38 | # Calulate checksums based on database contents: |
| 39 | # |
| 40 | # dbcksum DB DBNAME |
| 41 | # allcksum ?DB? |
| 42 | # cksum ?DB? |
| 43 | # |
| 44 | # Commands to execute/explain SQL statements: |
| 45 | # |
| 46 | # memdbsql SQL |
| 47 | # stepsql DB SQL |
| 48 | # execsql2 SQL |
| 49 | # explain_no_trace SQL |
| 50 | # explain SQL ?DB? |
| 51 | # catchsql SQL ?DB? |
| 52 | # execsql SQL ?DB? |
| 53 | # |
| 54 | # Commands to run test cases: |
| 55 | # |
| 56 | # do_ioerr_test TESTNAME ARGS... |
| 57 | # crashsql ARGS... |
| 58 | # integrity_check TESTNAME ?DB? |
| 59 | # verify_ex_errcode TESTNAME EXPECTED ?DB? |
| 60 | # do_test TESTNAME SCRIPT EXPECTED |
| 61 | # do_execsql_test TESTNAME SQL EXPECTED |
| 62 | # do_catchsql_test TESTNAME SQL EXPECTED |
| 63 | # do_timed_execsql_test TESTNAME SQL EXPECTED |
| 64 | # |
| 65 | # Commands providing a lower level interface to the global test counters: |
| 66 | # |
| 67 | # set_test_counter COUNTER ?VALUE? |
| 68 | # omit_test TESTNAME REASON ?APPEND? |
| 69 | # fail_test TESTNAME |
| 70 | # incr_ntest |
| 71 | # |
| 72 | # Command run at the end of each test file: |
| 73 | # |
| 74 | # finish_test |
| 75 | # |
| 76 | # Commands to help create test files that run with the "WAL" and other |
| 77 | # permutations (see file permutations.test): |
| 78 | # |
| 79 | # wal_is_wal_mode |
| 80 | # wal_set_journal_mode ?DB? |
| 81 | # wal_check_journal_mode TESTNAME?DB? |
| 82 | # permutation |
| 83 | # presql |
| 84 | # |
| 85 | # Command to test whether or not --verbose=1 was specified on the command |
| 86 | # line (returns 0 for not-verbose, 1 for verbose and 2 for "verbose in the |
| 87 | # output file only"). |
| 88 | # |
| 89 | # verbose |
| 90 | # |
| 91 | |
| 92 | # Only run this script once. If sourced a second time, make it a no-op |
| 93 | if {[info exists ::tester_tcl_has_run]} return |
| 94 | |
| 95 | # Set the precision of FP arithmatic used by the interpreter. And |
| 96 | # configure SQLite to take database file locks on the page that begins |
| 97 | # 64KB into the database file instead of the one 1GB in. This means |
| 98 | # the code that handles that special case can be tested without creating |
| 99 | # very large database files. |
| 100 | # |
| 101 | set tcl_precision 15 |
| 102 | sqlite3_test_control_pending_byte 0x0010000 |
| 103 | |
| 104 | |
| 105 | # If the pager codec is available, create a wrapper for the [sqlite3] |
| 106 | # command that appends "-key {xyzzy}" to the command line. i.e. this: |
| 107 | # |
| 108 | # sqlite3 db test.db |
| 109 | # |
| 110 | # becomes |
| 111 | # |
| 112 | # sqlite3 db test.db -key {xyzzy} |
| 113 | # |
| 114 | if {[info command sqlite_orig]==""} { |
| 115 | rename sqlite3 sqlite_orig |
| 116 | proc sqlite3 {args} { |
| 117 | if {[llength $args]>=2 && [string index [lindex $args 0] 0]!="-"} { |
| 118 | # This command is opening a new database connection. |
| 119 | # |
| 120 | if {[info exists ::G(perm:sqlite3_args)]} { |
| 121 | set args [concat $args $::G(perm:sqlite3_args)] |
| 122 | } |
| 123 | if {[sqlite_orig -has-codec] && ![info exists ::do_not_use_codec]} { |
| 124 | lappend args -key {xyzzy} |
| 125 | } |
| 126 | |
| 127 | set res [uplevel 1 sqlite_orig $args] |
| 128 | if {[info exists ::G(perm:presql)]} { |
| 129 | [lindex $args 0] eval $::G(perm:presql) |
| 130 | } |
| 131 | if {[info exists ::G(perm:dbconfig)]} { |
| 132 | set ::dbhandle [lindex $args 0] |
| 133 | uplevel #0 $::G(perm:dbconfig) |
| 134 | } |
| 135 | [lindex $args 0] cache size 3 |
| 136 | set res |
| 137 | } else { |
| 138 | # This command is not opening a new database connection. Pass the |
| 139 | # arguments through to the C implementation as the are. |
| 140 | # |
| 141 | uplevel 1 sqlite_orig $args |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | proc getFileRetries {} { |
| 147 | if {![info exists ::G(file-retries)]} { |
| 148 | # |
| 149 | # NOTE: Return the default number of retries for [file] operations. A |
| 150 | # value of zero or less here means "disabled". |
| 151 | # |
| 152 | return [expr {$::tcl_platform(platform) eq "windows" ? 50 : 0}] |
| 153 | } |
| 154 | return $::G(file-retries) |
| 155 | } |
| 156 | |
| 157 | proc getFileRetryDelay {} { |
| 158 | if {![info exists ::G(file-retry-delay)]} { |
| 159 | # |
| 160 | # NOTE: Return the default number of milliseconds to wait when retrying |
| 161 | # failed [file] operations. A value of zero or less means "do not |
| 162 | # wait". |
| 163 | # |
| 164 | return 100; # TODO: Good default? |
| 165 | } |
| 166 | return $::G(file-retry-delay) |
| 167 | } |
| 168 | |
| 169 | # Return the string representing the name of the current directory. On |
| 170 | # Windows, the result is "normalized" to whatever our parent command shell |
| 171 | # is using to prevent case-mismatch issues. |
| 172 | # |
| 173 | proc get_pwd {} { |
| 174 | if {$::tcl_platform(platform) eq "windows"} { |
| 175 | # |
| 176 | # NOTE: Cannot use [file normalize] here because it would alter the |
| 177 | # case of the result to what Tcl considers canonical, which would |
| 178 | # defeat the purpose of this procedure. |
| 179 | # |
| 180 | if {[info exists ::env(ComSpec)]} { |
| 181 | set comSpec $::env(ComSpec) |
| 182 | } else { |
| 183 | # NOTE: Hard-code the typical default value. |
| 184 | set comSpec {C:\Windows\system32\cmd.exe} |
| 185 | } |
| 186 | return [string map [list \\ /] \ |
| 187 | [string trim [exec -- $comSpec /c CD]]] |
| 188 | } else { |
| 189 | return [pwd] |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | # Copy file $from into $to. This is used because some versions of |
| 194 | # TCL for windows (notably the 8.4.1 binary package shipped with the |
| 195 | # current mingw release) have a broken "file copy" command. |
| 196 | # |
| 197 | proc copy_file {from to} { |
| 198 | do_copy_file false $from $to |
| 199 | } |
| 200 | |
| 201 | proc forcecopy {from to} { |
| 202 | do_copy_file true $from $to |
| 203 | } |
| 204 | |
| 205 | proc do_copy_file {force from to} { |
| 206 | set nRetry [getFileRetries] ;# Maximum number of retries. |
| 207 | set nDelay [getFileRetryDelay] ;# Delay in ms before retrying. |
| 208 | |
| 209 | # On windows, sometimes even a [file copy -force] can fail. The cause is |
| 210 | # usually "tag-alongs" - programs like anti-virus software, automatic backup |
| 211 | # tools and various explorer extensions that keep a file open a little longer |
| 212 | # than we expect, causing the delete to fail. |
| 213 | # |
| 214 | # The solution is to wait a short amount of time before retrying the copy. |
| 215 | # |
| 216 | if {$nRetry > 0} { |
| 217 | for {set i 0} {$i<$nRetry} {incr i} { |
| 218 | set rc [catch { |
| 219 | if {$force} { |
| 220 | file copy -force $from $to |
| 221 | } else { |
| 222 | file copy $from $to |
| 223 | } |
| 224 | } msg] |
| 225 | if {$rc==0} break |
| 226 | if {$nDelay > 0} { after $nDelay } |
| 227 | } |
| 228 | if {$rc} { error $msg } |
| 229 | } else { |
| 230 | if {$force} { |
| 231 | file copy -force $from $to |
| 232 | } else { |
| 233 | file copy $from $to |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | # Check if a file name is relative |
| 239 | # |
| 240 | proc is_relative_file { file } { |
| 241 | return [expr {[file pathtype $file] != "absolute"}] |
| 242 | } |
| 243 | |
| 244 | # If the VFS supports using the current directory, returns [pwd]; |
| 245 | # otherwise, it returns only the provided suffix string (which is |
| 246 | # empty by default). |
| 247 | # |
| 248 | proc test_pwd { args } { |
| 249 | if {[llength $args] > 0} { |
| 250 | set suffix1 [lindex $args 0] |
| 251 | if {[llength $args] > 1} { |
| 252 | set suffix2 [lindex $args 1] |
| 253 | } else { |
| 254 | set suffix2 $suffix1 |
| 255 | } |
| 256 | } else { |
| 257 | set suffix1 ""; set suffix2 "" |
| 258 | } |
| 259 | ifcapable curdir { |
| 260 | return "[get_pwd]$suffix1" |
| 261 | } else { |
| 262 | return $suffix2 |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | # Delete a file or directory |
| 267 | # |
| 268 | proc delete_file {args} { |
| 269 | do_delete_file false {*}$args |
| 270 | } |
| 271 | |
| 272 | proc forcedelete {args} { |
| 273 | do_delete_file true {*}$args |
| 274 | } |
| 275 | |
| 276 | proc do_delete_file {force args} { |
| 277 | set nRetry [getFileRetries] ;# Maximum number of retries. |
| 278 | set nDelay [getFileRetryDelay] ;# Delay in ms before retrying. |
| 279 | |
| 280 | foreach filename $args { |
| 281 | # On windows, sometimes even a [file delete -force] can fail just after |
| 282 | # a file is closed. The cause is usually "tag-alongs" - programs like |
| 283 | # anti-virus software, automatic backup tools and various explorer |
| 284 | # extensions that keep a file open a little longer than we expect, causing |
| 285 | # the delete to fail. |
| 286 | # |
| 287 | # The solution is to wait a short amount of time before retrying the |
| 288 | # delete. |
| 289 | # |
| 290 | if {$nRetry > 0} { |
| 291 | for {set i 0} {$i<$nRetry} {incr i} { |
| 292 | set rc [catch { |
| 293 | if {$force} { |
| 294 | file delete -force -- $filename |
| 295 | } else { |
| 296 | file delete -- $filename |
| 297 | } |
| 298 | } msg] |
| 299 | if {$rc==0} break |
| 300 | if {$nDelay > 0} { after $nDelay } |
| 301 | } |
| 302 | if {$rc} { error $msg } |
| 303 | } else { |
| 304 | if {$force} { |
| 305 | file delete -force -- $filename |
| 306 | } else { |
| 307 | file delete -- $filename |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | proc execpresql {handle args} { |
| 314 | trace remove execution $handle enter [list execpresql $handle] |
| 315 | if {[info exists ::G(perm:presql)]} { |
| 316 | $handle eval $::G(perm:presql) |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | # This command should be called after loading tester.tcl from within |
| 321 | # all test scripts that are incompatible with encryption codecs. |
| 322 | # |
| 323 | proc do_not_use_codec {} { |
| 324 | set ::do_not_use_codec 1 |
| 325 | reset_db |
| 326 | } |
| 327 | unset -nocomplain do_not_use_codec |
| 328 | |
| 329 | # Return true if the "reserved_bytes" integer on database files is non-zero. |
| 330 | # |
| 331 | proc nonzero_reserved_bytes {} { |
| 332 | return [sqlite3 -has-codec] |
| 333 | } |
| 334 | |
| 335 | # Print a HELP message and exit |
| 336 | # |
| 337 | proc print_help_and_quit {} { |
| 338 | puts {Options: |
| 339 | --pause Wait for user input before continuing |
| 340 | --soft-heap-limit=N Set the soft-heap-limit to N |
| 341 | --hard-heap-limit=N Set the hard-heap-limit to N |
| 342 | --maxerror=N Quit after N errors |
| 343 | --verbose=(0|1) Control the amount of output. Default '1' |
| 344 | --output=FILE set --verbose=2 and output to FILE. Implies -q |
| 345 | -q Shorthand for --verbose=0 |
| 346 | --help This message |
| 347 | } |
| 348 | exit 1 |
| 349 | } |
| 350 | |
| 351 | # The following block only runs the first time this file is sourced. It |
| 352 | # does not run in slave interpreters (since the ::cmdlinearg array is |
| 353 | # populated before the test script is run in slave interpreters). |
| 354 | # |
| 355 | if {[info exists cmdlinearg]==0} { |
| 356 | |
| 357 | # Parse any options specified in the $argv array. This script accepts the |
| 358 | # following options: |
| 359 | # |
| 360 | # --pause |
| 361 | # --soft-heap-limit=NN |
| 362 | # --hard-heap-limit=NN |
| 363 | # --maxerror=NN |
| 364 | # --malloctrace=N |
| 365 | # --backtrace=N |
| 366 | # --binarylog=N |
| 367 | # --soak=N |
| 368 | # --file-retries=N |
| 369 | # --file-retry-delay=N |
| 370 | # --start=[$permutation:]$testfile |
| 371 | # --match=$pattern |
| 372 | # --verbose=$val |
| 373 | # --output=$filename |
| 374 | # -q Reduce output |
| 375 | # --testdir=$dir Run tests in subdirectory $dir |
| 376 | # --help |
| 377 | # |
| 378 | set cmdlinearg(soft-heap-limit) 0 |
| 379 | set cmdlinearg(hard-heap-limit) 0 |
| 380 | set cmdlinearg(maxerror) 1000 |
| 381 | set cmdlinearg(malloctrace) 0 |
| 382 | set cmdlinearg(backtrace) 10 |
| 383 | set cmdlinearg(binarylog) 0 |
| 384 | set cmdlinearg(soak) 0 |
| 385 | set cmdlinearg(file-retries) 0 |
| 386 | set cmdlinearg(file-retry-delay) 0 |
| 387 | set cmdlinearg(start) "" |
| 388 | set cmdlinearg(match) "" |
| 389 | set cmdlinearg(verbose) "" |
| 390 | set cmdlinearg(output) "" |
| 391 | set cmdlinearg(testdir) "testdir" |
| 392 | |
| 393 | set leftover [list] |
| 394 | foreach a $argv { |
| 395 | switch -regexp -- $a { |
| 396 | {^-+pause$} { |
| 397 | # Wait for user input before continuing. This is to give the user an |
| 398 | # opportunity to connect profiling tools to the process. |
| 399 | puts -nonewline "Press RETURN to begin..." |
| 400 | flush stdout |
| 401 | gets stdin |
| 402 | } |
| 403 | {^-+soft-heap-limit=.+$} { |
| 404 | foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break |
| 405 | } |
| 406 | {^-+hard-heap-limit=.+$} { |
| 407 | foreach {dummy cmdlinearg(hard-heap-limit)} [split $a =] break |
| 408 | } |
| 409 | {^-+maxerror=.+$} { |
| 410 | foreach {dummy cmdlinearg(maxerror)} [split $a =] break |
| 411 | } |
| 412 | {^-+malloctrace=.+$} { |
| 413 | foreach {dummy cmdlinearg(malloctrace)} [split $a =] break |
| 414 | if {$cmdlinearg(malloctrace)} { |
| 415 | if {0==$::sqlite_options(memdebug)} { |
| 416 | set err "Error: --malloctrace=1 requires an SQLITE_MEMDEBUG build" |
| 417 | puts stderr $err |
| 418 | exit 1 |
| 419 | } |
| 420 | sqlite3_memdebug_log start |
| 421 | } |
| 422 | } |
| 423 | {^-+backtrace=.+$} { |
| 424 | foreach {dummy cmdlinearg(backtrace)} [split $a =] break |
| 425 | sqlite3_memdebug_backtrace $cmdlinearg(backtrace) |
| 426 | } |
| 427 | {^-+binarylog=.+$} { |
| 428 | foreach {dummy cmdlinearg(binarylog)} [split $a =] break |
| 429 | set cmdlinearg(binarylog) [file normalize $cmdlinearg(binarylog)] |
| 430 | } |
| 431 | {^-+soak=.+$} { |
| 432 | foreach {dummy cmdlinearg(soak)} [split $a =] break |
| 433 | set ::G(issoak) $cmdlinearg(soak) |
| 434 | } |
| 435 | {^-+file-retries=.+$} { |
| 436 | foreach {dummy cmdlinearg(file-retries)} [split $a =] break |
| 437 | set ::G(file-retries) $cmdlinearg(file-retries) |
| 438 | } |
| 439 | {^-+file-retry-delay=.+$} { |
| 440 | foreach {dummy cmdlinearg(file-retry-delay)} [split $a =] break |
| 441 | set ::G(file-retry-delay) $cmdlinearg(file-retry-delay) |
| 442 | } |
| 443 | {^-+start=.+$} { |
| 444 | foreach {dummy cmdlinearg(start)} [split $a =] break |
| 445 | |
| 446 | set ::G(start:file) $cmdlinearg(start) |
| 447 | if {[regexp {(.*):(.*)} $cmdlinearg(start) -> s.perm s.file]} { |
| 448 | set ::G(start:permutation) ${s.perm} |
| 449 | set ::G(start:file) ${s.file} |
| 450 | } |
| 451 | if {$::G(start:file) == ""} {unset ::G(start:file)} |
| 452 | } |
| 453 | {^-+match=.+$} { |
| 454 | foreach {dummy cmdlinearg(match)} [split $a =] break |
| 455 | |
| 456 | set ::G(match) $cmdlinearg(match) |
| 457 | if {$::G(match) == ""} {unset ::G(match)} |
| 458 | } |
| 459 | |
| 460 | {^-+output=.+$} { |
| 461 | foreach {dummy cmdlinearg(output)} [split $a =] break |
| 462 | set cmdlinearg(output) [file normalize $cmdlinearg(output)] |
| 463 | if {$cmdlinearg(verbose)==""} { |
| 464 | set cmdlinearg(verbose) 2 |
| 465 | } |
| 466 | } |
| 467 | {^-+verbose=.+$} { |
| 468 | foreach {dummy cmdlinearg(verbose)} [split $a =] break |
| 469 | if {$cmdlinearg(verbose)=="file"} { |
| 470 | set cmdlinearg(verbose) 2 |
| 471 | } elseif {[string is boolean -strict $cmdlinearg(verbose)]==0} { |
| 472 | error "option --verbose= must be set to a boolean or to \"file\"" |
| 473 | } |
| 474 | } |
| 475 | {^-+testdir=.*$} { |
| 476 | foreach {dummy cmdlinearg(testdir)} [split $a =] break |
| 477 | } |
| 478 | {.*help.*} { |
| 479 | print_help_and_quit |
| 480 | } |
| 481 | {^-q$} { |
| 482 | set cmdlinearg(output) test-out.txt |
| 483 | set cmdlinearg(verbose) 2 |
| 484 | } |
| 485 | |
| 486 | default { |
| 487 | if {[file tail $a]==$a} { |
| 488 | lappend leftover $a |
| 489 | } else { |
| 490 | lappend leftover [file normalize $a] |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | unset -nocomplain a |
| 496 | set testdir [file normalize $testdir] |
| 497 | set cmdlinearg(TESTFIXTURE_HOME) [file dirname [info nameofexec]] |
| 498 | set cmdlinearg(INFO_SCRIPT) [file normalize [info script]] |
| 499 | set argv0 [file normalize $argv0] |
| 500 | if {$cmdlinearg(testdir)!=""} { |
| 501 | file mkdir $cmdlinearg(testdir) |
| 502 | cd $cmdlinearg(testdir) |
| 503 | } |
| 504 | set argv $leftover |
| 505 | |
| 506 | # Install the malloc layer used to inject OOM errors. And the 'automatic' |
| 507 | # extensions. This only needs to be done once for the process. |
| 508 | # |
| 509 | sqlite3_shutdown |
| 510 | install_malloc_faultsim 1 |
| 511 | sqlite3_initialize |
| 512 | autoinstall_test_functions |
| 513 | |
| 514 | # If the --binarylog option was specified, create the logging VFS. This |
| 515 | # call installs the new VFS as the default for all SQLite connections. |
| 516 | # |
| 517 | if {$cmdlinearg(binarylog)} { |
| 518 | vfslog new binarylog {} vfslog.bin |
| 519 | } |
| 520 | |
| 521 | # Set the backtrace depth, if malloc tracing is enabled. |
| 522 | # |
| 523 | if {$cmdlinearg(malloctrace)} { |
| 524 | sqlite3_memdebug_backtrace $cmdlinearg(backtrace) |
| 525 | } |
| 526 | |
| 527 | if {$cmdlinearg(output)!=""} { |
| 528 | puts "Copying output to file $cmdlinearg(output)" |
| 529 | set ::G(output_fd) [open $cmdlinearg(output) w] |
| 530 | fconfigure $::G(output_fd) -buffering line |
| 531 | } |
| 532 | |
| 533 | if {$cmdlinearg(verbose)==""} { |
| 534 | set cmdlinearg(verbose) 1 |
| 535 | } |
| 536 | |
| 537 | if {[info commands vdbe_coverage]!=""} { |
| 538 | vdbe_coverage start |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | # Update the soft-heap-limit each time this script is run. In that |
| 543 | # way if an individual test file changes the soft-heap-limit, it |
| 544 | # will be reset at the start of the next test file. |
| 545 | # |
| 546 | sqlite3_soft_heap_limit64 $cmdlinearg(soft-heap-limit) |
| 547 | sqlite3_hard_heap_limit64 $cmdlinearg(hard-heap-limit) |
| 548 | |
| 549 | # Create a test database |
| 550 | # |
| 551 | proc reset_db {} { |
| 552 | catch {db close} |
| 553 | forcedelete test.db |
| 554 | forcedelete test.db-journal |
| 555 | forcedelete test.db-wal |
| 556 | sqlite3 db ./test.db |
| 557 | set ::DB [sqlite3_connection_pointer db] |
| 558 | if {[info exists ::SETUP_SQL]} { |
| 559 | db eval $::SETUP_SQL |
| 560 | } |
| 561 | } |
| 562 | reset_db |
| 563 | |
| 564 | # Abort early if this script has been run before. |
| 565 | # |
| 566 | if {[info exists TC(count)]} return |
| 567 | |
| 568 | # Make sure memory statistics are enabled. |
| 569 | # |
| 570 | sqlite3_config_memstatus 1 |
| 571 | |
| 572 | # Initialize the test counters and set up commands to access them. |
| 573 | # Or, if this is a slave interpreter, set up aliases to write the |
| 574 | # counters in the parent interpreter. |
| 575 | # |
| 576 | if {0==[info exists ::SLAVE]} { |
| 577 | set TC(errors) 0 |
| 578 | set TC(count) 0 |
| 579 | set TC(fail_list) [list] |
| 580 | set TC(omit_list) [list] |
| 581 | set TC(warn_list) [list] |
| 582 | |
| 583 | proc set_test_counter {counter args} { |
| 584 | if {[llength $args]} { |
| 585 | set ::TC($counter) [lindex $args 0] |
| 586 | } |
| 587 | set ::TC($counter) |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | # Record the fact that a sequence of tests were omitted. |
| 592 | # |
| 593 | proc omit_test {name reason {append 1}} { |
| 594 | set omitList [set_test_counter omit_list] |
| 595 | if {$append} { |
| 596 | lappend omitList [list $name $reason] |
| 597 | } |
| 598 | set_test_counter omit_list $omitList |
| 599 | } |
| 600 | |
| 601 | # Record the fact that a test failed. |
| 602 | # |
| 603 | proc fail_test {name} { |
| 604 | set f [set_test_counter fail_list] |
| 605 | lappend f $name |
| 606 | set_test_counter fail_list $f |
| 607 | set_test_counter errors [expr [set_test_counter errors] + 1] |
| 608 | |
| 609 | set nFail [set_test_counter errors] |
| 610 | if {$nFail>=$::cmdlinearg(maxerror)} { |
| 611 | output2 "*** Giving up..." |
| 612 | finalize_testing |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | # Remember a warning message to be displayed at the conclusion of all testing |
| 617 | # |
| 618 | proc warning {msg {append 1}} { |
| 619 | output2 "Warning: $msg" |
| 620 | set warnList [set_test_counter warn_list] |
| 621 | if {$append} { |
| 622 | lappend warnList $msg |
| 623 | } |
| 624 | set_test_counter warn_list $warnList |
| 625 | } |
| 626 | |
| 627 | |
| 628 | # Increment the number of tests run |
| 629 | # |
| 630 | proc incr_ntest {} { |
| 631 | set_test_counter count [expr [set_test_counter count] + 1] |
| 632 | } |
| 633 | |
| 634 | # Return true if --verbose=1 was specified on the command line. Otherwise, |
| 635 | # return false. |
| 636 | # |
| 637 | proc verbose {} { |
| 638 | return $::cmdlinearg(verbose) |
| 639 | } |
| 640 | |
| 641 | # Use the following commands instead of [puts] for test output within |
| 642 | # this file. Test scripts can still use regular [puts], which is directed |
| 643 | # to stdout and, if one is open, the --output file. |
| 644 | # |
| 645 | # output1: output that should be printed if --verbose=1 was specified. |
| 646 | # output2: output that should be printed unconditionally. |
| 647 | # output2_if_no_verbose: output that should be printed only if --verbose=0. |
| 648 | # |
| 649 | proc output1 {args} { |
| 650 | set v [verbose] |
| 651 | if {$v==1} { |
| 652 | uplevel output2 $args |
| 653 | } elseif {$v==2} { |
| 654 | uplevel puts [lrange $args 0 end-1] $::G(output_fd) [lrange $args end end] |
| 655 | } |
| 656 | } |
| 657 | proc output2 {args} { |
| 658 | set nArg [llength $args] |
| 659 | uplevel puts $args |
| 660 | } |
| 661 | proc output2_if_no_verbose {args} { |
| 662 | set v [verbose] |
| 663 | if {$v==0} { |
| 664 | uplevel output2 $args |
| 665 | } elseif {$v==2} { |
| 666 | uplevel puts [lrange $args 0 end-1] stdout [lrange $args end end] |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | # Override the [puts] command so that if no channel is explicitly |
| 671 | # specified the string is written to both stdout and to the file |
| 672 | # specified by "--output=", if any. |
| 673 | # |
| 674 | proc puts_override {args} { |
| 675 | set nArg [llength $args] |
| 676 | if {$nArg==1 || ($nArg==2 && [string first [lindex $args 0] -nonewline]==0)} { |
| 677 | uplevel puts_original $args |
| 678 | if {[info exists ::G(output_fd)]} { |
| 679 | uplevel puts [lrange $args 0 end-1] $::G(output_fd) [lrange $args end end] |
| 680 | } |
| 681 | } else { |
| 682 | # A channel was explicitly specified. |
| 683 | uplevel puts_original $args |
| 684 | } |
| 685 | } |
| 686 | rename puts puts_original |
| 687 | proc puts {args} { uplevel puts_override $args } |
| 688 | |
| 689 | |
| 690 | # Invoke the do_test procedure to run a single test |
| 691 | # |
| 692 | # The $expected parameter is the expected result. The result is the return |
| 693 | # value from the last TCL command in $cmd. |
| 694 | # |
| 695 | # Normally, $expected must match exactly. But if $expected is of the form |
| 696 | # "/regexp/" then regular expression matching is used. If $expected is |
| 697 | # "~/regexp/" then the regular expression must NOT match. If $expected is |
| 698 | # of the form "#/value-list/" then each term in value-list must be numeric |
| 699 | # and must approximately match the corresponding numeric term in $result. |
| 700 | # Values must match within 10%. Or if the $expected term is A..B then the |
| 701 | # $result term must be in between A and B. |
| 702 | # |
| 703 | proc do_test {name cmd expected} { |
| 704 | global argv cmdlinearg |
| 705 | |
| 706 | fix_testname name |
| 707 | |
| 708 | sqlite3_memdebug_settitle $name |
| 709 | |
| 710 | # if {[llength $argv]==0} { |
| 711 | # set go 1 |
| 712 | # } else { |
| 713 | # set go 0 |
| 714 | # foreach pattern $argv { |
| 715 | # if {[string match $pattern $name]} { |
| 716 | # set go 1 |
| 717 | # break |
| 718 | # } |
| 719 | # } |
| 720 | # } |
| 721 | |
| 722 | if {[info exists ::G(perm:prefix)]} { |
| 723 | set name "$::G(perm:prefix)$name" |
| 724 | } |
| 725 | |
| 726 | incr_ntest |
| 727 | output1 -nonewline $name... |
| 728 | flush stdout |
| 729 | |
| 730 | if {![info exists ::G(match)] || [string match $::G(match) $name]} { |
| 731 | if {[catch {uplevel #0 "$cmd;\n"} result]} { |
| 732 | output2_if_no_verbose -nonewline $name... |
| 733 | output2 "\nError: $result" |
| 734 | fail_test $name |
| 735 | } else { |
| 736 | if {[permutation]=="maindbname"} { |
| 737 | set result [string map [list [string tolower ICECUBE] main] $result] |
| 738 | } |
| 739 | if {[regexp {^[~#]?/.*/$} $expected]} { |
| 740 | # "expected" is of the form "/PATTERN/" then the result if correct if |
| 741 | # regular expression PATTERN matches the result. "~/PATTERN/" means |
| 742 | # the regular expression must not match. |
| 743 | if {[string index $expected 0]=="~"} { |
| 744 | set re [string range $expected 2 end-1] |
| 745 | if {[string index $re 0]=="*"} { |
| 746 | # If the regular expression begins with * then treat it as a glob instead |
| 747 | set ok [string match $re $result] |
| 748 | } else { |
| 749 | set re [string map {# {[-0-9.]+}} $re] |
| 750 | set ok [regexp $re $result] |
| 751 | } |
| 752 | set ok [expr {!$ok}] |
| 753 | } elseif {[string index $expected 0]=="#"} { |
| 754 | # Numeric range value comparison. Each term of the $result is matched |
| 755 | # against one term of $expect. Both $result and $expected terms must be |
| 756 | # numeric. The values must match within 10%. Or if $expected is of the |
| 757 | # form A..B then the $result term must be between A and B. |
| 758 | set e2 [string range $expected 2 end-1] |
| 759 | foreach i $result j $e2 { |
| 760 | if {[regexp {^(-?\d+)\.\.(-?\d)$} $j all A B]} { |
| 761 | set ok [expr {$i+0>=$A && $i+0<=$B}] |
| 762 | } else { |
| 763 | set ok [expr {$i+0>=0.9*$j && $i+0<=1.1*$j}] |
| 764 | } |
| 765 | if {!$ok} break |
| 766 | } |
| 767 | if {$ok && [llength $result]!=[llength $e2]} {set ok 0} |
| 768 | } else { |
| 769 | set re [string range $expected 1 end-1] |
| 770 | if {[string index $re 0]=="*"} { |
| 771 | # If the regular expression begins with * then treat it as a glob instead |
| 772 | set ok [string match $re $result] |
| 773 | } else { |
| 774 | set re [string map {# {[-0-9.]+}} $re] |
| 775 | set ok [regexp $re $result] |
| 776 | } |
| 777 | } |
| 778 | } elseif {[regexp {^~?\*.*\*$} $expected]} { |
| 779 | # "expected" is of the form "*GLOB*" then the result if correct if |
| 780 | # glob pattern GLOB matches the result. "~/GLOB/" means |
| 781 | # the glob must not match. |
| 782 | if {[string index $expected 0]=="~"} { |
| 783 | set e [string range $expected 1 end] |
| 784 | set ok [expr {![string match $e $result]}] |
| 785 | } else { |
| 786 | set ok [string match $expected $result] |
| 787 | } |
| 788 | } else { |
| 789 | set ok [expr {[string compare $result $expected]==0}] |
| 790 | if {!$ok} { |
| 791 | set ok [fpnum_compare $result $expected] |
| 792 | } |
| 793 | } |
| 794 | if {!$ok} { |
| 795 | # if {![info exists ::testprefix] || $::testprefix eq ""} { |
| 796 | # error "no test prefix" |
| 797 | # } |
| 798 | output1 "" |
| 799 | output2 "! $name expected: \[$expected\]\n! $name got: \[$result\]" |
| 800 | fail_test $name |
| 801 | } else { |
| 802 | output1 " Ok" |
| 803 | } |
| 804 | } |
| 805 | } else { |
| 806 | output1 " Omitted" |
| 807 | omit_test $name "pattern mismatch" 0 |
| 808 | } |
| 809 | flush stdout |
| 810 | } |
| 811 | |
| 812 | # Like do_test except the test is not run in a slave interpreter |
| 813 | # on Windows because of issues with ANSI and UTF8 I/O on Win11. |
| 814 | # |
| 815 | proc do_test_with_ansi_output {name cmd expected} { |
| 816 | if {![info exists ::SLAVE] || $::tcl_platform(platform) ne "windows"} { |
| 817 | uplevel 1 [list do_test $name $cmd $expected] |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | proc dumpbytes {s} { |
| 822 | set r "" |
| 823 | for {set i 0} {$i < [string length $s]} {incr i} { |
| 824 | if {$i > 0} {append r " "} |
| 825 | append r [format %02X [scan [string index $s $i] %c]] |
| 826 | } |
| 827 | return $r |
| 828 | } |
| 829 | |
| 830 | proc catchcmd {db {cmd ""}} { |
| 831 | global CLI |
| 832 | set out [open cmds.txt w] |
| 833 | puts $out $cmd |
| 834 | close $out |
| 835 | set line "exec $CLI $db < cmds.txt" |
| 836 | set rc [catch { eval $line } msg] |
| 837 | list $rc $msg |
| 838 | } |
| 839 | proc catchsafecmd {db {cmd ""}} { |
| 840 | global CLI |
| 841 | set out [open cmds.txt w] |
| 842 | puts $out $cmd |
| 843 | close $out |
| 844 | set line "exec $CLI -safe $db < cmds.txt" |
| 845 | set rc [catch { eval $line } msg] |
| 846 | list $rc $msg |
| 847 | } |
| 848 | |
| 849 | proc catchcmdex {db {cmd ""}} { |
| 850 | global CLI |
| 851 | set out [open cmds.txt w] |
| 852 | fconfigure $out -translation binary |
| 853 | puts -nonewline $out $cmd |
| 854 | close $out |
| 855 | set line "exec -keepnewline -- $CLI $db < cmds.txt" |
| 856 | set chans [list stdin stdout stderr] |
| 857 | foreach chan $chans { |
| 858 | catch { |
| 859 | set modes($chan) [fconfigure $chan] |
| 860 | fconfigure $chan -translation binary -buffering none |
| 861 | } |
| 862 | } |
| 863 | set rc [catch { eval $line } msg] |
| 864 | foreach chan $chans { |
| 865 | catch { |
| 866 | eval fconfigure [list $chan] $modes($chan) |
| 867 | } |
| 868 | } |
| 869 | # puts [dumpbytes $msg] |
| 870 | list $rc $msg |
| 871 | } |
| 872 | |
| 873 | proc filepath_normalize {p} { |
| 874 | # test cases should be written to assume "unix"-like file paths |
| 875 | if {$::tcl_platform(platform) ne "unix"} { |
| 876 | string map [list \\ / \{/ / .db\} .db] \ |
| 877 | [regsub -nocase -all {[a-z]:[/\\]+} $p {/}] |
| 878 | } { |
| 879 | set p |
| 880 | } |
| 881 | } |
| 882 | proc do_filepath_test {name cmd expected} { |
| 883 | uplevel [list do_test $name [ |
| 884 | subst -nocommands { filepath_normalize [ $cmd ] } |
| 885 | ] [filepath_normalize $expected]] |
| 886 | } |
| 887 | |
| 888 | proc realnum_normalize {r} { |
| 889 | # different TCL versions display floating point values differently. |
| 890 | string map {1.#INF inf Inf inf .0e e} [regsub -all {(e[+-])0+} $r {\1}] |
| 891 | } |
| 892 | proc do_realnum_test {name cmd expected} { |
| 893 | uplevel [list do_test $name [ |
| 894 | subst -nocommands { realnum_normalize [ $cmd ] } |
| 895 | ] [realnum_normalize $expected]] |
| 896 | } |
| 897 | |
| 898 | proc fix_testname {varname} { |
| 899 | upvar $varname testname |
| 900 | if {[info exists ::testprefix] |
| 901 | && [string is digit [string range $testname 0 0]] |
| 902 | } { |
| 903 | set testname "${::testprefix}-$testname" |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | proc normalize_list {L} { |
| 908 | set L2 [list] |
| 909 | foreach l $L {lappend L2 $l} |
| 910 | set L2 |
| 911 | } |
| 912 | |
| 913 | # Run SQL and verify that the number of "vmsteps" required is greater |
| 914 | # than or less than some constant. |
| 915 | # |
| 916 | proc do_vmstep_test {tn sql nstep {res {}}} { |
| 917 | uplevel [list do_execsql_test $tn.0 $sql $res] |
| 918 | |
| 919 | set vmstep [db status vmstep] |
| 920 | if {[string range $nstep 0 0]=="+"} { |
| 921 | set body "if {$vmstep<$nstep} { |
| 922 | error \"got $vmstep, expected more than [string range $nstep 1 end]\" |
| 923 | }" |
| 924 | } else { |
| 925 | set body "if {$vmstep>$nstep} { |
| 926 | error \"got $vmstep, expected less than $nstep\" |
| 927 | }" |
| 928 | } |
| 929 | |
| 930 | # set name "$tn.vmstep=$vmstep,expect=$nstep" |
| 931 | set name "$tn.1" |
| 932 | uplevel [list do_test $name $body {}] |
| 933 | } |
| 934 | |
| 935 | |
| 936 | # Either: |
| 937 | # |
| 938 | # do_execsql_test TESTNAME SQL ?RES? |
| 939 | # do_execsql_test -db DB TESTNAME SQL ?RES? |
| 940 | # |
| 941 | proc do_execsql_test {args} { |
| 942 | set db db |
| 943 | if {[lindex $args 0]=="-db"} { |
| 944 | set db [lindex $args 1] |
| 945 | set args [lrange $args 2 end] |
| 946 | } |
| 947 | |
| 948 | if {[llength $args]==2} { |
| 949 | foreach {testname sql} $args {} |
| 950 | set result "" |
| 951 | } elseif {[llength $args]==3} { |
| 952 | foreach {testname sql result} $args {} |
| 953 | |
| 954 | # With some versions of Tcl on windows, if $result is all whitespace but |
| 955 | # contains some CR/LF characters, the [list {*}$result] below returns a |
| 956 | # copy of $result instead of a zero length string. Not clear exactly why |
| 957 | # this is. The following is a workaround. |
| 958 | if {[llength $result]==0} { set result "" } |
| 959 | } else { |
| 960 | error [string trim { |
| 961 | wrong # args: should be "do_execsql_test ?-db DB? testname sql ?result?" |
| 962 | }] |
| 963 | } |
| 964 | |
| 965 | fix_testname testname |
| 966 | |
| 967 | uplevel do_test \ |
| 968 | [list $testname] \ |
| 969 | [list "execsql {$sql} $db"] \ |
| 970 | [list [list {*}$result]] |
| 971 | } |
| 972 | |
| 973 | proc do_catchsql_test {testname sql result} { |
| 974 | fix_testname testname |
| 975 | uplevel do_test [list $testname] [list "catchsql {$sql}"] [list $result] |
| 976 | } |
| 977 | proc do_timed_execsql_test {testname sql {result {}}} { |
| 978 | fix_testname testname |
| 979 | uplevel do_test [list $testname] [list "execsql_timed {$sql}"]\ |
| 980 | [list [list {*}$result]] |
| 981 | } |
| 982 | |
| 983 | # Run an EXPLAIN QUERY PLAN $sql in database "db". Then rewrite the output |
| 984 | # as an ASCII-art graph and return a string that is that graph. |
| 985 | # |
| 986 | # Hexadecimal literals in the output text are converted into "xxxxxx" since those |
| 987 | # literals are pointer values that might very from one run of the test to the |
| 988 | # next, yet we want the output to be consistent. |
| 989 | # |
| 990 | proc query_plan_graph {sql} { |
| 991 | db eval "EXPLAIN QUERY PLAN $sql" { |
| 992 | set dx($id) $detail |
| 993 | lappend cx($parent) $id |
| 994 | } |
| 995 | set a "\n QUERY PLAN\n" |
| 996 | append a [append_graph " " dx cx 0] |
| 997 | regsub -all {SUBQUERY 0x[A-F0-9]+\y} $a {SUBQUERY xxxxxx} a |
| 998 | regsub -all {(MATERIALIZE|CO-ROUTINE|SUBQUERY) \d+\y} $a {\1 xxxxxx} a |
| 999 | regsub -all {\((join|subquery)-\d+\)} $a {(\1-xxxxxx)} a |
| 1000 | return $a |
| 1001 | } |
| 1002 | |
| 1003 | # Helper routine for [query_plan_graph SQL]: |
| 1004 | # |
| 1005 | # Output rows of the graph that are children of $level. |
| 1006 | # |
| 1007 | # prefix: Prepend to every output line |
| 1008 | # |
| 1009 | # dxname: Name of an array variable that stores text describe |
| 1010 | # The description for $id is $dx($id) |
| 1011 | # |
| 1012 | # cxname: Name of an array variable holding children of item. |
| 1013 | # Children of $id are $cx($id) |
| 1014 | # |
| 1015 | # level: Render all lines that are children of $level |
| 1016 | # |
| 1017 | proc append_graph {prefix dxname cxname level} { |
| 1018 | upvar $dxname dx $cxname cx |
| 1019 | set a "" |
| 1020 | set x $cx($level) |
| 1021 | set n [llength $x] |
| 1022 | for {set i 0} {$i<$n} {incr i} { |
| 1023 | set id [lindex $x $i] |
| 1024 | if {$i==$n-1} { |
| 1025 | set p1 "`--" |
| 1026 | set p2 " " |
| 1027 | } else { |
| 1028 | set p1 "|--" |
| 1029 | set p2 "| " |
| 1030 | } |
| 1031 | append a $prefix$p1$dx($id)\n |
| 1032 | if {[info exists cx($id)]} { |
| 1033 | append a [append_graph "$prefix$p2" dx cx $id] |
| 1034 | } |
| 1035 | } |
| 1036 | return $a |
| 1037 | } |
| 1038 | |
| 1039 | # Do an EXPLAIN QUERY PLAN test on input $sql with expected results $res |
| 1040 | # |
| 1041 | # If $res begins with a "\s+QUERY PLAN\n" then it is assumed to be the |
| 1042 | # complete graph which must match the output of [query_plan_graph $sql] |
| 1043 | # exactly. |
| 1044 | # |
| 1045 | # If $res does not begin with "\s+QUERY PLAN\n" then take it is a string |
| 1046 | # that must be found somewhere in the query plan output. |
| 1047 | # |
| 1048 | proc do_eqp_test {name sql res} { |
| 1049 | if {[regexp {^\s+QUERY PLAN\n} $res]} { |
| 1050 | |
| 1051 | set query_plan [query_plan_graph $sql] |
| 1052 | |
| 1053 | if {[list {*}$query_plan]==[list {*}$res]} { |
| 1054 | uplevel [list do_test $name [list set {} ok] ok] |
| 1055 | } else { |
| 1056 | uplevel [list \ |
| 1057 | do_test $name [list query_plan_graph $sql] $res |
| 1058 | ] |
| 1059 | } |
| 1060 | } else { |
| 1061 | if {[string index $res 0]!="/"} { |
| 1062 | set res "/*$res*/" |
| 1063 | } |
| 1064 | uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res] |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | # Do both an eqp_test and an execsql_test on the same SQL. |
| 1069 | # |
| 1070 | proc do_eqp_execsql_test {name sql res1 res2} { |
| 1071 | if {[regexp {^\s+QUERY PLAN\n} $res1]} { |
| 1072 | |
| 1073 | set query_plan [query_plan_graph $sql] |
| 1074 | |
| 1075 | if {[list {*}$query_plan]==[list {*}$res1]} { |
| 1076 | uplevel [list do_test ${name}a [list set {} ok] ok] |
| 1077 | } else { |
| 1078 | uplevel [list \ |
| 1079 | do_test ${name}a [list query_plan_graph $sql] $res1 |
| 1080 | ] |
| 1081 | } |
| 1082 | } else { |
| 1083 | if {[string index $res 0]!="/"} { |
| 1084 | set res1 "/*$res1*/" |
| 1085 | } |
| 1086 | uplevel do_execsql_test ${name}a [list "EXPLAIN QUERY PLAN $sql"] [list $res1] |
| 1087 | } |
| 1088 | uplevel do_execsql_test ${name}b [list $sql] [list $res2] |
| 1089 | } |
| 1090 | |
| 1091 | |
| 1092 | #------------------------------------------------------------------------- |
| 1093 | # Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST |
| 1094 | # |
| 1095 | # Where switches are: |
| 1096 | # |
| 1097 | # -errorformat FMTSTRING |
| 1098 | # -count |
| 1099 | # -query SQL |
| 1100 | # -tclquery TCL |
| 1101 | # -repair TCL |
| 1102 | # |
| 1103 | proc do_select_tests {prefix args} { |
| 1104 | |
| 1105 | set testlist [lindex $args end] |
| 1106 | set switches [lrange $args 0 end-1] |
| 1107 | |
| 1108 | set errfmt "" |
| 1109 | set countonly 0 |
| 1110 | set tclquery "" |
| 1111 | set repair "" |
| 1112 | |
| 1113 | for {set i 0} {$i < [llength $switches]} {incr i} { |
| 1114 | set s [lindex $switches $i] |
| 1115 | set n [string length $s] |
| 1116 | if {$n>=2 && [string equal -length $n $s "-query"]} { |
| 1117 | set tclquery [list execsql [lindex $switches [incr i]]] |
| 1118 | } elseif {$n>=2 && [string equal -length $n $s "-tclquery"]} { |
| 1119 | set tclquery [lindex $switches [incr i]] |
| 1120 | } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} { |
| 1121 | set errfmt [lindex $switches [incr i]] |
| 1122 | } elseif {$n>=2 && [string equal -length $n $s "-repair"]} { |
| 1123 | set repair [lindex $switches [incr i]] |
| 1124 | } elseif {$n>=2 && [string equal -length $n $s "-count"]} { |
| 1125 | set countonly 1 |
| 1126 | } else { |
| 1127 | error "unknown switch: $s" |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | if {$countonly && $errfmt!=""} { |
| 1132 | error "Cannot use -count and -errorformat together" |
| 1133 | } |
| 1134 | set nTestlist [llength $testlist] |
| 1135 | if {$nTestlist%3 || $nTestlist==0 } { |
| 1136 | error "SELECT test list contains [llength $testlist] elements" |
| 1137 | } |
| 1138 | |
| 1139 | eval $repair |
| 1140 | foreach {tn sql res} $testlist { |
| 1141 | if {$tclquery != ""} { |
| 1142 | execsql $sql |
| 1143 | uplevel do_test ${prefix}.$tn [list $tclquery] [list [list {*}$res]] |
| 1144 | } elseif {$countonly} { |
| 1145 | set nRow 0 |
| 1146 | db eval $sql {incr nRow} |
| 1147 | uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res] |
| 1148 | } elseif {$errfmt==""} { |
| 1149 | uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]] |
| 1150 | } else { |
| 1151 | set res [list 1 [string trim [format $errfmt {*}$res]]] |
| 1152 | uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res] |
| 1153 | } |
| 1154 | eval $repair |
| 1155 | } |
| 1156 | |
| 1157 | } |
| 1158 | |
| 1159 | proc delete_all_data {} { |
| 1160 | db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} { |
| 1161 | db eval "DELETE FROM '[string map {' ''} $t]'" |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | # Run an SQL script. |
| 1166 | # Return the number of microseconds per statement. |
| 1167 | # |
| 1168 | proc speed_trial {name numstmt units sql} { |
| 1169 | output2 -nonewline [format {%-21.21s } $name...] |
| 1170 | flush stdout |
| 1171 | set speed [time {sqlite3_exec_nr db $sql}] |
| 1172 | set tm [lindex $speed 0] |
| 1173 | if {$tm == 0} { |
| 1174 | set rate [format %20s "many"] |
| 1175 | } else { |
| 1176 | set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]] |
| 1177 | } |
| 1178 | set u2 $units/s |
| 1179 | output2 [format {%12d uS %s %s} $tm $rate $u2] |
| 1180 | global total_time |
| 1181 | set total_time [expr {$total_time+$tm}] |
| 1182 | lappend ::speed_trial_times $name $tm |
| 1183 | } |
| 1184 | proc speed_trial_tcl {name numstmt units script} { |
| 1185 | output2 -nonewline [format {%-21.21s } $name...] |
| 1186 | flush stdout |
| 1187 | set speed [time {eval $script}] |
| 1188 | set tm [lindex $speed 0] |
| 1189 | if {$tm == 0} { |
| 1190 | set rate [format %20s "many"] |
| 1191 | } else { |
| 1192 | set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]] |
| 1193 | } |
| 1194 | set u2 $units/s |
| 1195 | output2 [format {%12d uS %s %s} $tm $rate $u2] |
| 1196 | global total_time |
| 1197 | set total_time [expr {$total_time+$tm}] |
| 1198 | lappend ::speed_trial_times $name $tm |
| 1199 | } |
| 1200 | proc speed_trial_init {name} { |
| 1201 | global total_time |
| 1202 | set total_time 0 |
| 1203 | set ::speed_trial_times [list] |
| 1204 | sqlite3 versdb :memory: |
| 1205 | set vers [versdb one {SELECT sqlite_source_id()}] |
| 1206 | versdb close |
| 1207 | output2 "SQLite $vers" |
| 1208 | } |
| 1209 | proc speed_trial_summary {name} { |
| 1210 | global total_time |
| 1211 | output2 [format {%-21.21s %12d uS TOTAL} $name $total_time] |
| 1212 | |
| 1213 | if { 0 } { |
| 1214 | sqlite3 versdb :memory: |
| 1215 | set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0] |
| 1216 | versdb close |
| 1217 | output2 "CREATE TABLE IF NOT EXISTS time(version, script, test, us);" |
| 1218 | foreach {test us} $::speed_trial_times { |
| 1219 | output2 "INSERT INTO time VALUES('$vers', '$name', '$test', $us);" |
| 1220 | } |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | # Clear out left-over configuration setup from the end of a test |
| 1225 | # |
| 1226 | proc finish_test_precleanup {} { |
| 1227 | catch {db1 close} |
| 1228 | catch {db2 close} |
| 1229 | catch {db3 close} |
| 1230 | catch {unregister_devsim} |
| 1231 | catch {unregister_jt_vfs} |
| 1232 | catch {unregister_demovfs} |
| 1233 | } |
| 1234 | |
| 1235 | # Run this routine last |
| 1236 | # |
| 1237 | proc finish_test {} { |
| 1238 | global argv |
| 1239 | finish_test_precleanup |
| 1240 | if {[llength $argv]>0} { |
| 1241 | # If additional test scripts are specified on the command-line, |
| 1242 | # run them also, before quitting. |
| 1243 | proc finish_test {} { |
| 1244 | finish_test_precleanup |
| 1245 | return |
| 1246 | } |
| 1247 | foreach extra $argv { |
| 1248 | puts "Running \"$extra\"" |
| 1249 | db_delete_and_reopen |
| 1250 | uplevel #0 source $extra |
| 1251 | } |
| 1252 | } |
| 1253 | catch {db close} |
| 1254 | if {0==[info exists ::SLAVE]} { finalize_testing } |
| 1255 | } |
| 1256 | proc finalize_testing {} { |
| 1257 | global sqlite_open_file_count |
| 1258 | |
| 1259 | set omitList [set_test_counter omit_list] |
| 1260 | |
| 1261 | catch {db close} |
| 1262 | catch {db2 close} |
| 1263 | catch {db3 close} |
| 1264 | |
| 1265 | vfs_unlink_test |
| 1266 | sqlite3 db {} |
| 1267 | # sqlite3_clear_tsd_memdebug |
| 1268 | db close |
| 1269 | sqlite3_reset_auto_extension |
| 1270 | |
| 1271 | sqlite3_soft_heap_limit64 0 |
| 1272 | sqlite3_hard_heap_limit64 0 |
| 1273 | set nTest [incr_ntest] |
| 1274 | set nErr [set_test_counter errors] |
| 1275 | |
| 1276 | set nKnown 0 |
| 1277 | if {[file readable known-problems.txt]} { |
| 1278 | set fd [open known-problems.txt] |
| 1279 | set content [read $fd] |
| 1280 | close $fd |
| 1281 | foreach x $content {set known_error($x) 1} |
| 1282 | foreach x [set_test_counter fail_list] { |
| 1283 | if {[info exists known_error($x)]} {incr nKnown} |
| 1284 | } |
| 1285 | } |
| 1286 | if {$nKnown>0} { |
| 1287 | output2 "[expr {$nErr-$nKnown}] new errors and $nKnown known errors\ |
| 1288 | out of $nTest tests" |
| 1289 | } else { |
| 1290 | set cpuinfo {} |
| 1291 | if {[catch {exec hostname} hname]==0} { |
| 1292 | regsub {\.local$} $hname {} hname |
| 1293 | set cpuinfo [string trim $hname] |
| 1294 | } |
| 1295 | append cpuinfo " $::tcl_platform(os)" |
| 1296 | append cpuinfo " [expr {$::tcl_platform(pointerSize)*8}]-bit" |
| 1297 | if {[string match big* $::tcl_platform(byteOrder)]} { |
| 1298 | append cpuinfo " [string map {E -e} $::tcl_platform(byteOrder)]" |
| 1299 | } |
| 1300 | output2 "SQLite [sqlite3 -sourceid]" |
| 1301 | output2 "$nErr errors out of $nTest tests on $cpuinfo" |
| 1302 | } |
| 1303 | if {$nErr>$nKnown} { |
| 1304 | output2 -nonewline "!Failures on these tests:" |
| 1305 | foreach x [set_test_counter fail_list] { |
| 1306 | if {![info exists known_error($x)]} {output2 -nonewline " $x"} |
| 1307 | } |
| 1308 | output2 "" |
| 1309 | } |
| 1310 | foreach warning [set_test_counter warn_list] { |
| 1311 | output2 "Warning: $warning" |
| 1312 | } |
| 1313 | run_thread_tests 1 |
| 1314 | if {[llength $omitList]>0} { |
| 1315 | output2 "Omitted test cases:" |
| 1316 | set prec {} |
| 1317 | foreach {rec} [lsort $omitList] { |
| 1318 | if {$rec==$prec} continue |
| 1319 | set prec $rec |
| 1320 | output2 [format {. %-12s %s} [lindex $rec 0] [lindex $rec 1]] |
| 1321 | } |
| 1322 | } |
| 1323 | if {$nErr>0 && ![working_64bit_int]} { |
| 1324 | output2 "******************************************************************" |
| 1325 | output2 "N.B.: The version of TCL that you used to build this test harness" |
| 1326 | output2 "is defective in that it does not support 64-bit integers. Some or" |
| 1327 | output2 "all of the test failures above might be a result from this defect" |
| 1328 | output2 "in your TCL build." |
| 1329 | output2 "******************************************************************" |
| 1330 | } |
| 1331 | if {$::cmdlinearg(binarylog)} { |
| 1332 | vfslog finalize binarylog |
| 1333 | } |
| 1334 | if {[info exists ::run_thread_tests_called]==0} { |
| 1335 | if {$sqlite_open_file_count} { |
| 1336 | output2 "$sqlite_open_file_count files were left open" |
| 1337 | incr nErr |
| 1338 | } |
| 1339 | } |
| 1340 | if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 || |
| 1341 | [sqlite3_memory_used]>0} { |
| 1342 | output2 "Unfreed memory: [sqlite3_memory_used] bytes in\ |
| 1343 | [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations" |
| 1344 | incr nErr |
| 1345 | ifcapable mem5||(mem3&&debug) { |
| 1346 | output2 "Writing unfreed memory log to \"./memleak.txt\"" |
| 1347 | sqlite3_memdebug_dump ./memleak.txt |
| 1348 | } |
| 1349 | } else { |
| 1350 | output2 "All memory allocations freed - no leaks" |
| 1351 | ifcapable mem5 { |
| 1352 | sqlite3_memdebug_dump ./memusage.txt |
| 1353 | } |
| 1354 | } |
| 1355 | show_memstats |
| 1356 | output2 "Maximum memory usage: [sqlite3_memory_highwater 1] bytes" |
| 1357 | output2 "Current memory usage: [sqlite3_memory_highwater] bytes" |
| 1358 | if {[info commands sqlite3_memdebug_malloc_count] ne ""} { |
| 1359 | output2 "Number of malloc() : [sqlite3_memdebug_malloc_count] calls" |
| 1360 | } |
| 1361 | if {$::cmdlinearg(malloctrace)} { |
| 1362 | output2 "Writing mallocs.tcl..." |
| 1363 | memdebug_log_sql mallocs.tcl |
| 1364 | sqlite3_memdebug_log stop |
| 1365 | sqlite3_memdebug_log clear |
| 1366 | if {[sqlite3_memory_used]>0} { |
| 1367 | output2 "Writing leaks.tcl..." |
| 1368 | sqlite3_memdebug_log sync |
| 1369 | memdebug_log_sql leaks.tcl |
| 1370 | } |
| 1371 | } |
| 1372 | if {[info commands vdbe_coverage]!=""} { |
| 1373 | vdbe_coverage_report |
| 1374 | } |
| 1375 | foreach f [glob -nocomplain test.db-*-journal] { |
| 1376 | forcedelete $f |
| 1377 | } |
| 1378 | foreach f [glob -nocomplain test.db-mj*] { |
| 1379 | forcedelete $f |
| 1380 | } |
| 1381 | exit [expr {$nErr>0}] |
| 1382 | } |
| 1383 | |
| 1384 | proc vdbe_coverage_report {} { |
| 1385 | puts "Writing vdbe coverage report to vdbe_coverage.txt" |
| 1386 | set lSrc [list] |
| 1387 | set iLine 0 |
| 1388 | if {[file exists ../sqlite3.c]} { |
| 1389 | set fd [open ../sqlite3.c] |
| 1390 | set iLine |
| 1391 | while { ![eof $fd] } { |
| 1392 | set line [gets $fd] |
| 1393 | incr iLine |
| 1394 | if {[regexp {^/\** Begin file (.*\.c) \**/} $line -> file]} { |
| 1395 | lappend lSrc [list $iLine $file] |
| 1396 | } |
| 1397 | } |
| 1398 | close $fd |
| 1399 | } |
| 1400 | set fd [open vdbe_coverage.txt w] |
| 1401 | foreach miss [vdbe_coverage report] { |
| 1402 | foreach {line branch never} $miss {} |
| 1403 | set nextfile "" |
| 1404 | while {[llength $lSrc]>0 && [lindex $lSrc 0 0] < $line} { |
| 1405 | set nextfile [lindex $lSrc 0 1] |
| 1406 | set lSrc [lrange $lSrc 1 end] |
| 1407 | } |
| 1408 | if {$nextfile != ""} { |
| 1409 | puts $fd "" |
| 1410 | puts $fd "### $nextfile ###" |
| 1411 | } |
| 1412 | puts $fd "Vdbe branch $line: never $never (path $branch)" |
| 1413 | } |
| 1414 | close $fd |
| 1415 | } |
| 1416 | |
| 1417 | # Display memory statistics for analysis and debugging purposes. |
| 1418 | # |
| 1419 | proc show_memstats {} { |
| 1420 | set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0] |
| 1421 | set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0] |
| 1422 | set val [format {now %10d max %10d max-size %10d} \ |
| 1423 | [lindex $x 1] [lindex $x 2] [lindex $y 2]] |
| 1424 | output1 "Memory used: $val" |
| 1425 | set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] |
| 1426 | set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]] |
| 1427 | output1 "Allocation count: $val" |
| 1428 | set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] |
| 1429 | set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0] |
| 1430 | set val [format {now %10d max %10d max-size %10d} \ |
| 1431 | [lindex $x 1] [lindex $x 2] [lindex $y 2]] |
| 1432 | output1 "Page-cache used: $val" |
| 1433 | set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] |
| 1434 | set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]] |
| 1435 | output1 "Page-cache overflow: $val" |
| 1436 | ifcapable yytrackmaxstackdepth { |
| 1437 | set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0] |
| 1438 | set val [format { max %10d} [lindex $x 2]] |
| 1439 | output2 "Parser stack depth: $val" |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | # A procedure to execute SQL |
| 1444 | # |
| 1445 | proc execsql {sql {db db}} { |
| 1446 | # puts "SQL = $sql" |
| 1447 | uplevel [list $db eval $sql] |
| 1448 | } |
| 1449 | proc execsql_timed {sql {db db}} { |
| 1450 | set tm [time { |
| 1451 | set x [uplevel [list $db eval $sql]] |
| 1452 | } 1] |
| 1453 | set tm [lindex $tm 0] |
| 1454 | output1 -nonewline " ([expr {$tm*0.001}]ms) " |
| 1455 | set x |
| 1456 | } |
| 1457 | |
| 1458 | # Execute SQL and catch exceptions. |
| 1459 | # |
| 1460 | proc catchsql {sql {db db}} { |
| 1461 | # puts "SQL = $sql" |
| 1462 | set r [catch [list uplevel [list $db eval $sql]] msg] |
| 1463 | lappend r $msg |
| 1464 | return $r |
| 1465 | } |
| 1466 | |
| 1467 | # Do an VDBE code dump on the SQL given |
| 1468 | # |
| 1469 | proc explain {sql {db db}} { |
| 1470 | output2 "" |
| 1471 | output2 "addr opcode p1 p2 p3 p4 p5 #" |
| 1472 | output2 "---- ------------ ------ ------ ------ --------------- -- -" |
| 1473 | $db eval "explain $sql" {} { |
| 1474 | output2 [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \ |
| 1475 | $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment |
| 1476 | ] |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | proc explain_i {sql {db db}} { |
| 1481 | output2 "" |
| 1482 | output2 "addr opcode p1 p2 p3 p4 p5 #" |
| 1483 | output2 "---- ------------ ------ ------ ------ ---------------- -- -" |
| 1484 | |
| 1485 | |
| 1486 | # Set up colors for the different opcodes. Scheme is as follows: |
| 1487 | # |
| 1488 | # Red: Opcodes that write to a b-tree. |
| 1489 | # Blue: Opcodes that reposition or seek a cursor. |
| 1490 | # Green: The ResultRow opcode. |
| 1491 | # |
| 1492 | if { [catch {fconfigure stdout -mode}]==0 } { |
| 1493 | set R "\033\[31;1m" ;# Red fg |
| 1494 | set G "\033\[32;1m" ;# Green fg |
| 1495 | set B "\033\[34;1m" ;# Red fg |
| 1496 | set D "\033\[39;0m" ;# Default fg |
| 1497 | } else { |
| 1498 | set R "" |
| 1499 | set G "" |
| 1500 | set B "" |
| 1501 | set D "" |
| 1502 | } |
| 1503 | foreach opcode { |
| 1504 | Seek SeekGE SeekGT SeekLE SeekLT NotFound Last Rewind |
| 1505 | NoConflict Next Prev VNext VPrev VFilter |
| 1506 | SorterSort SorterNext NextIfOpen |
| 1507 | } { |
| 1508 | set color($opcode) $B |
| 1509 | } |
| 1510 | foreach opcode {ResultRow} { |
| 1511 | set color($opcode) $G |
| 1512 | } |
| 1513 | foreach opcode {IdxInsert Insert Delete IdxDelete} { |
| 1514 | set color($opcode) $R |
| 1515 | } |
| 1516 | |
| 1517 | set bSeenGoto 0 |
| 1518 | $db eval "explain $sql" {} { |
| 1519 | set x($addr) 0 |
| 1520 | set op($addr) $opcode |
| 1521 | |
| 1522 | if {$opcode == "Goto" && ($bSeenGoto==0 || ($p2 > $addr+10))} { |
| 1523 | set linebreak($p2) 1 |
| 1524 | set bSeenGoto 1 |
| 1525 | } |
| 1526 | |
| 1527 | if {$opcode=="Once"} { |
| 1528 | for {set i $addr} {$i<$p2} {incr i} { |
| 1529 | set star($i) $addr |
| 1530 | } |
| 1531 | } |
| 1532 | |
| 1533 | if {$opcode=="Next" || $opcode=="Prev" |
| 1534 | || $opcode=="VNext" || $opcode=="VPrev" |
| 1535 | || $opcode=="SorterNext" || $opcode=="NextIfOpen" |
| 1536 | } { |
| 1537 | for {set i $p2} {$i<$addr} {incr i} { |
| 1538 | incr x($i) 2 |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | if {$opcode == "Goto" && $p2<$addr && $op($p2)=="Yield"} { |
| 1543 | for {set i [expr $p2+1]} {$i<$addr} {incr i} { |
| 1544 | incr x($i) 2 |
| 1545 | } |
| 1546 | } |
| 1547 | |
| 1548 | if {$opcode == "Halt" && $comment == "End of coroutine"} { |
| 1549 | set linebreak([expr $addr+1]) 1 |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | $db eval "explain $sql" {} { |
| 1554 | if {[info exists linebreak($addr)]} { |
| 1555 | output2 "" |
| 1556 | } |
| 1557 | set I [string repeat " " $x($addr)] |
| 1558 | |
| 1559 | if {[info exists star($addr)]} { |
| 1560 | set ii [expr $x($star($addr))] |
| 1561 | append I " " |
| 1562 | set I [string replace $I $ii $ii *] |
| 1563 | } |
| 1564 | |
| 1565 | set col "" |
| 1566 | catch { set col $color($opcode) } |
| 1567 | |
| 1568 | output2 [format {%-4d %s%s%-12.12s%s %-6d %-6d %-6d % -17s %s %s} \ |
| 1569 | $addr $I $col $opcode $D $p1 $p2 $p3 $p4 $p5 $comment |
| 1570 | ] |
| 1571 | } |
| 1572 | output2 "---- ------------ ------ ------ ------ ---------------- -- -" |
| 1573 | } |
| 1574 | |
| 1575 | proc execsql_pp {sql {db db}} { |
| 1576 | set nCol 0 |
| 1577 | $db eval $sql A { |
| 1578 | if {$nCol==0} { |
| 1579 | set nCol [llength $A(*)] |
| 1580 | foreach c $A(*) { |
| 1581 | set aWidth($c) [string length $c] |
| 1582 | lappend data $c |
| 1583 | } |
| 1584 | } |
| 1585 | foreach c $A(*) { |
| 1586 | set n [string length $A($c)] |
| 1587 | if {$n > $aWidth($c)} { |
| 1588 | set aWidth($c) $n |
| 1589 | } |
| 1590 | lappend data $A($c) |
| 1591 | } |
| 1592 | } |
| 1593 | if {$nCol>0} { |
| 1594 | set nTotal 0 |
| 1595 | foreach e [array names aWidth] { incr nTotal $aWidth($e) } |
| 1596 | incr nTotal [expr ($nCol-1) * 3] |
| 1597 | incr nTotal 4 |
| 1598 | |
| 1599 | set fmt "" |
| 1600 | foreach c $A(*) { |
| 1601 | lappend fmt "% -$aWidth($c)s" |
| 1602 | } |
| 1603 | set fmt "| [join $fmt { | }] |" |
| 1604 | |
| 1605 | puts [string repeat - $nTotal] |
| 1606 | for {set i 0} {$i < [llength $data]} {incr i $nCol} { |
| 1607 | set vals [lrange $data $i [expr $i+$nCol-1]] |
| 1608 | puts [format $fmt {*}$vals] |
| 1609 | if {$i==0} { puts [string repeat - $nTotal] } |
| 1610 | } |
| 1611 | puts [string repeat - $nTotal] |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | |
| 1616 | # Show the VDBE program for an SQL statement but omit the Trace |
| 1617 | # opcode at the beginning. This procedure can be used to prove |
| 1618 | # that different SQL statements generate exactly the same VDBE code. |
| 1619 | # |
| 1620 | proc explain_no_trace {sql} { |
| 1621 | set tr [db eval "EXPLAIN $sql"] |
| 1622 | return [lrange $tr 7 end] |
| 1623 | } |
| 1624 | |
| 1625 | # Another procedure to execute SQL. This one includes the field |
| 1626 | # names in the returned list. |
| 1627 | # |
| 1628 | proc execsql2 {sql} { |
| 1629 | set result {} |
| 1630 | db eval $sql data { |
| 1631 | foreach f $data(*) { |
| 1632 | lappend result $f $data($f) |
| 1633 | } |
| 1634 | } |
| 1635 | return $result |
| 1636 | } |
| 1637 | |
| 1638 | # Use a temporary in-memory database to execute SQL statements |
| 1639 | # |
| 1640 | proc memdbsql {sql} { |
| 1641 | sqlite3 memdb :memory: |
| 1642 | set result [memdb eval $sql] |
| 1643 | memdb close |
| 1644 | return $result |
| 1645 | } |
| 1646 | |
| 1647 | # Use the non-callback API to execute multiple SQL statements |
| 1648 | # |
| 1649 | proc stepsql {dbptr sql} { |
| 1650 | set sql [string trim $sql] |
| 1651 | set r 0 |
| 1652 | while {[string length $sql]>0} { |
| 1653 | if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} { |
| 1654 | return [list 1 $vm] |
| 1655 | } |
| 1656 | set sql [string trim $sqltail] |
| 1657 | # while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} { |
| 1658 | # foreach v $VAL {lappend r $v} |
| 1659 | # } |
| 1660 | while {[sqlite3_step $vm]=="SQLITE_ROW"} { |
| 1661 | for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} { |
| 1662 | lappend r [sqlite3_column_text $vm $i] |
| 1663 | } |
| 1664 | } |
| 1665 | if {[catch {sqlite3_finalize $vm} errmsg]} { |
| 1666 | return [list 1 $errmsg] |
| 1667 | } |
| 1668 | } |
| 1669 | return $r |
| 1670 | } |
| 1671 | |
| 1672 | # Do an integrity check of the entire database |
| 1673 | # |
| 1674 | proc integrity_check {name {db db}} { |
| 1675 | ifcapable integrityck { |
| 1676 | do_test $name [list execsql {PRAGMA integrity_check} $db] {ok} |
| 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | # Check the extended error code |
| 1681 | # |
| 1682 | proc verify_ex_errcode {name expected {db db}} { |
| 1683 | do_test $name [list sqlite3_extended_errcode $db] $expected |
| 1684 | } |
| 1685 | |
| 1686 | |
| 1687 | # Return true if the SQL statement passed as the second argument uses a |
| 1688 | # statement transaction. |
| 1689 | # |
| 1690 | proc sql_uses_stmt {db sql} { |
| 1691 | set stmt [sqlite3_prepare $db $sql -1 dummy] |
| 1692 | set uses [uses_stmt_journal $stmt] |
| 1693 | sqlite3_finalize $stmt |
| 1694 | return $uses |
| 1695 | } |
| 1696 | |
| 1697 | proc fix_ifcapable_expr {expr} { |
| 1698 | set ret "" |
| 1699 | set state 0 |
| 1700 | for {set i 0} {$i < [string length $expr]} {incr i} { |
| 1701 | set char [string range $expr $i $i] |
| 1702 | set newstate [expr {[string is alnum $char] || $char eq "_"}] |
| 1703 | if {$newstate && !$state} { |
| 1704 | append ret {$::sqlite_options(} |
| 1705 | } |
| 1706 | if {!$newstate && $state} { |
| 1707 | append ret ) |
| 1708 | } |
| 1709 | append ret $char |
| 1710 | set state $newstate |
| 1711 | } |
| 1712 | if {$state} {append ret )} |
| 1713 | return $ret |
| 1714 | } |
| 1715 | |
| 1716 | # Returns non-zero if the capabilities are present; zero otherwise. |
| 1717 | # |
| 1718 | proc capable {expr} { |
| 1719 | set e [fix_ifcapable_expr $expr]; return [expr ($e)] |
| 1720 | } |
| 1721 | |
| 1722 | # Evaluate a boolean expression of capabilities. If true, execute the |
| 1723 | # code. Omit the code if false. |
| 1724 | # |
| 1725 | proc ifcapable {expr code {else ""} {elsecode ""}} { |
| 1726 | #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2 |
| 1727 | set e2 [fix_ifcapable_expr $expr] |
| 1728 | if ($e2) { |
| 1729 | set c [catch {uplevel 1 $code} r] |
| 1730 | } else { |
| 1731 | set c [catch {uplevel 1 $elsecode} r] |
| 1732 | } |
| 1733 | return -code $c $r |
| 1734 | } |
| 1735 | |
| 1736 | # This proc execs a separate process that crashes midway through executing |
| 1737 | # the SQL script $sql on database test.db. |
| 1738 | # |
| 1739 | # The crash occurs during a sync() of file $crashfile. When the crash |
| 1740 | # occurs a random subset of all unsynced writes made by the process are |
| 1741 | # written into the files on disk. Argument $crashdelay indicates the |
| 1742 | # number of file syncs to wait before crashing. |
| 1743 | # |
| 1744 | # The return value is a list of two elements. The first element is a |
| 1745 | # boolean, indicating whether or not the process actually crashed or |
| 1746 | # reported some other error. The second element in the returned list is the |
| 1747 | # error message. This is "child process exited abnormally" if the crash |
| 1748 | # occurred. |
| 1749 | # |
| 1750 | # crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql |
| 1751 | # |
| 1752 | proc crashsql {args} { |
| 1753 | |
| 1754 | set blocksize "" |
| 1755 | set crashdelay 1 |
| 1756 | set prngseed 0 |
| 1757 | set opendb { sqlite3 db test.db -vfs crash } |
| 1758 | set tclbody {} |
| 1759 | set crashfile "" |
| 1760 | set dc "" |
| 1761 | set dfltvfs 0 |
| 1762 | set sql [lindex $args end] |
| 1763 | |
| 1764 | for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} { |
| 1765 | set z [lindex $args $ii] |
| 1766 | set n [string length $z] |
| 1767 | set z2 [lindex $args [expr $ii+1]] |
| 1768 | |
| 1769 | if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \ |
| 1770 | elseif {$n>1 && [string first $z -opendb]==0} {set opendb $z2} \ |
| 1771 | elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \ |
| 1772 | elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \ |
| 1773 | elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \ |
| 1774 | elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \ |
| 1775 | elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" }\ |
| 1776 | elseif {$n>1 && [string first $z -dfltvfs]==0} {set dfltvfs $z2 }\ |
| 1777 | else { error "Unrecognized option: $z" } |
| 1778 | } |
| 1779 | |
| 1780 | if {$crashfile eq ""} { |
| 1781 | error "Compulsory option -file missing" |
| 1782 | } |
| 1783 | |
| 1784 | # $crashfile gets compared to the native filename in |
| 1785 | # cfSync(), which can be different then what TCL uses by |
| 1786 | # default, so here we force it to the "nativename" format. |
| 1787 | set cfile [string map {\\ \\\\} [file nativename [file join [get_pwd] $crashfile]]] |
| 1788 | |
| 1789 | set f [open crash.tcl w] |
| 1790 | puts $f "sqlite3_initialize ; sqlite3_shutdown" |
| 1791 | puts $f "catch { install_malloc_faultsim 1 }" |
| 1792 | puts $f "sqlite3_crash_enable 1 $dfltvfs" |
| 1793 | puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile" |
| 1794 | puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte" |
| 1795 | puts $f "autoinstall_test_functions" |
| 1796 | |
| 1797 | # This block sets the cache size of the main database to 10 |
| 1798 | # pages. This is done in case the build is configured to omit |
| 1799 | # "PRAGMA cache_size". |
| 1800 | if {$opendb!=""} { |
| 1801 | puts $f $opendb |
| 1802 | puts $f {db eval {SELECT * FROM sqlite_master;}} |
| 1803 | puts $f {set bt [btree_from_db db]} |
| 1804 | puts $f {btree_set_cache_size $bt 10} |
| 1805 | } |
| 1806 | |
| 1807 | if {$prngseed} { |
| 1808 | set seed [expr {$prngseed%10007+1}] |
| 1809 | # puts seed=$seed |
| 1810 | puts $f "db eval {SELECT randomblob($seed)}" |
| 1811 | } |
| 1812 | |
| 1813 | if {[string length $tclbody]>0} { |
| 1814 | puts $f $tclbody |
| 1815 | } |
| 1816 | if {[string length $sql]>0} { |
| 1817 | puts $f "db eval {" |
| 1818 | puts $f "$sql" |
| 1819 | puts $f "}" |
| 1820 | } |
| 1821 | close $f |
| 1822 | set r [catch { |
| 1823 | exec [info nameofexec] crash.tcl >@stdout 2>@stdout |
| 1824 | } msg] |
| 1825 | |
| 1826 | # Windows/ActiveState TCL returns a slightly different |
| 1827 | # error message. We map that to the expected message |
| 1828 | # so that we don't have to change all of the test |
| 1829 | # cases. |
| 1830 | if {$::tcl_platform(platform) eq "windows"} { |
| 1831 | if {$msg=="child killed: unknown signal"} { |
| 1832 | set msg "child process exited abnormally" |
| 1833 | } |
| 1834 | } |
| 1835 | if {$r && [string match {*ERROR: LeakSanitizer*} $msg]} { |
| 1836 | set msg "child process exited abnormally" |
| 1837 | } |
| 1838 | |
| 1839 | lappend r $msg |
| 1840 | } |
| 1841 | |
| 1842 | # crash_on_write ?-devchar DEVCHAR? CRASHDELAY SQL |
| 1843 | # |
| 1844 | proc crash_on_write {args} { |
| 1845 | |
| 1846 | set nArg [llength $args] |
| 1847 | if {$nArg<2 || $nArg%2} { |
| 1848 | error "bad args: $args" |
| 1849 | } |
| 1850 | set zSql [lindex $args end] |
| 1851 | set nDelay [lindex $args end-1] |
| 1852 | |
| 1853 | set devchar {} |
| 1854 | for {set ii 0} {$ii < $nArg-2} {incr ii 2} { |
| 1855 | set opt [lindex $args $ii] |
| 1856 | switch -- [lindex $args $ii] { |
| 1857 | -devchar { |
| 1858 | set devchar [lindex $args [expr $ii+1]] |
| 1859 | } |
| 1860 | |
| 1861 | default { error "unrecognized option: $opt" } |
| 1862 | } |
| 1863 | } |
| 1864 | |
| 1865 | set f [open crash.tcl w] |
| 1866 | puts $f "sqlite3_crash_on_write $nDelay" |
| 1867 | puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte" |
| 1868 | puts $f "sqlite3 db test.db -vfs writecrash" |
| 1869 | puts $f "db eval {$zSql}" |
| 1870 | puts $f "set {} {}" |
| 1871 | |
| 1872 | close $f |
| 1873 | set r [catch { |
| 1874 | exec [info nameofexec] crash.tcl >@stdout |
| 1875 | } msg] |
| 1876 | |
| 1877 | # Windows/ActiveState TCL returns a slightly different |
| 1878 | # error message. We map that to the expected message |
| 1879 | # so that we don't have to change all of the test |
| 1880 | # cases. |
| 1881 | if {$::tcl_platform(platform) eq "windows"} { |
| 1882 | if {$msg=="child killed: unknown signal"} { |
| 1883 | set msg "child process exited abnormally" |
| 1884 | } |
| 1885 | } |
| 1886 | |
| 1887 | lappend r $msg |
| 1888 | } |
| 1889 | |
| 1890 | proc run_ioerr_prep {} { |
| 1891 | set ::sqlite_io_error_pending 0 |
| 1892 | catch {db close} |
| 1893 | catch {db2 close} |
| 1894 | catch {forcedelete test.db} |
| 1895 | catch {forcedelete test.db-journal} |
| 1896 | catch {forcedelete test2.db} |
| 1897 | catch {forcedelete test2.db-journal} |
| 1898 | set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db] |
| 1899 | sqlite3_extended_result_codes $::DB $::ioerropts(-erc) |
| 1900 | if {[info exists ::ioerropts(-tclprep)]} { |
| 1901 | eval $::ioerropts(-tclprep) |
| 1902 | } |
| 1903 | if {[info exists ::ioerropts(-sqlprep)]} { |
| 1904 | execsql $::ioerropts(-sqlprep) |
| 1905 | } |
| 1906 | expr 0 |
| 1907 | } |
| 1908 | |
| 1909 | # Usage: do_ioerr_test <test number> <options...> |
| 1910 | # |
| 1911 | # This proc is used to implement test cases that check that IO errors |
| 1912 | # are correctly handled. The first argument, <test number>, is an integer |
| 1913 | # used to name the tests executed by this proc. Options are as follows: |
| 1914 | # |
| 1915 | # -tclprep TCL script to run to prepare test. |
| 1916 | # -sqlprep SQL script to run to prepare test. |
| 1917 | # -tclbody TCL script to run with IO error simulation. |
| 1918 | # -sqlbody TCL script to run with IO error simulation. |
| 1919 | # -exclude List of 'N' values not to test. |
| 1920 | # -erc Use extended result codes |
| 1921 | # -persist Make simulated I/O errors persistent |
| 1922 | # -start Value of 'N' to begin with (default 1) |
| 1923 | # |
| 1924 | # -cksum Boolean. If true, test that the database does |
| 1925 | # not change during the execution of the test case. |
| 1926 | # |
| 1927 | proc do_ioerr_test {testname args} { |
| 1928 | |
| 1929 | set ::ioerropts(-start) 1 |
| 1930 | set ::ioerropts(-cksum) 0 |
| 1931 | set ::ioerropts(-erc) 0 |
| 1932 | set ::ioerropts(-count) 100000000 |
| 1933 | set ::ioerropts(-persist) 1 |
| 1934 | set ::ioerropts(-ckrefcount) 0 |
| 1935 | set ::ioerropts(-restoreprng) 1 |
| 1936 | array set ::ioerropts $args |
| 1937 | |
| 1938 | # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are |
| 1939 | # a couple of obscure IO errors that do not return them. |
| 1940 | set ::ioerropts(-erc) 0 |
| 1941 | |
| 1942 | # Create a single TCL script from the TCL and SQL specified |
| 1943 | # as the body of the test. |
| 1944 | set ::ioerrorbody {} |
| 1945 | if {[info exists ::ioerropts(-tclbody)]} { |
| 1946 | append ::ioerrorbody "$::ioerropts(-tclbody)\n" |
| 1947 | } |
| 1948 | if {[info exists ::ioerropts(-sqlbody)]} { |
| 1949 | append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}" |
| 1950 | } |
| 1951 | |
| 1952 | save_prng_state |
| 1953 | if {$::ioerropts(-cksum)} { |
| 1954 | run_ioerr_prep |
| 1955 | eval $::ioerrorbody |
| 1956 | set ::goodcksum [cksum] |
| 1957 | } |
| 1958 | |
| 1959 | set ::go 1 |
| 1960 | #reset_prng_state |
| 1961 | for {set n $::ioerropts(-start)} {$::go} {incr n} { |
| 1962 | set ::TN $n |
| 1963 | incr ::ioerropts(-count) -1 |
| 1964 | if {$::ioerropts(-count)<0} break |
| 1965 | |
| 1966 | # Skip this IO error if it was specified with the "-exclude" option. |
| 1967 | if {[info exists ::ioerropts(-exclude)]} { |
| 1968 | if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue |
| 1969 | } |
| 1970 | if {$::ioerropts(-restoreprng)} { |
| 1971 | restore_prng_state |
| 1972 | } |
| 1973 | |
| 1974 | # Delete the files test.db and test2.db, then execute the TCL and |
| 1975 | # SQL (in that order) to prepare for the test case. |
| 1976 | do_test $testname.$n.1 { |
| 1977 | run_ioerr_prep |
| 1978 | } {0} |
| 1979 | |
| 1980 | # Read the 'checksum' of the database. |
| 1981 | if {$::ioerropts(-cksum)} { |
| 1982 | set ::checksum [cksum] |
| 1983 | } |
| 1984 | |
| 1985 | # Set the Nth IO error to fail. |
| 1986 | do_test $testname.$n.2 [subst { |
| 1987 | set ::sqlite_io_error_persist $::ioerropts(-persist) |
| 1988 | set ::sqlite_io_error_pending $n |
| 1989 | }] $n |
| 1990 | |
| 1991 | # Execute the TCL script created for the body of this test. If |
| 1992 | # at least N IO operations performed by SQLite as a result of |
| 1993 | # the script, the Nth will fail. |
| 1994 | do_test $testname.$n.3 { |
| 1995 | set ::sqlite_io_error_hit 0 |
| 1996 | set ::sqlite_io_error_hardhit 0 |
| 1997 | set r [catch $::ioerrorbody msg] |
| 1998 | set ::errseen $r |
| 1999 | if {[info commands db]!=""} { |
| 2000 | set rc [sqlite3_errcode db] |
| 2001 | if {$::ioerropts(-erc)} { |
| 2002 | # If we are in extended result code mode, make sure all of the |
| 2003 | # IOERRs we get back really do have their extended code values. |
| 2004 | # If an extended result code is returned, the sqlite3_errcode |
| 2005 | # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn |
| 2006 | # where nnnn is a number |
| 2007 | if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} { |
| 2008 | return $rc |
| 2009 | } |
| 2010 | } else { |
| 2011 | # If we are not in extended result code mode, make sure no |
| 2012 | # extended error codes are returned. |
| 2013 | if {[regexp {\+\d} $rc]} { |
| 2014 | return $rc |
| 2015 | } |
| 2016 | } |
| 2017 | } |
| 2018 | # The test repeats as long as $::go is non-zero. $::go starts out |
| 2019 | # as 1. When a test runs to completion without hitting an I/O |
| 2020 | # error, that means there is no point in continuing with this test |
| 2021 | # case so set $::go to zero. |
| 2022 | # |
| 2023 | if {$::sqlite_io_error_pending>0} { |
| 2024 | set ::go 0 |
| 2025 | set q 0 |
| 2026 | set ::sqlite_io_error_pending 0 |
| 2027 | } else { |
| 2028 | set q 1 |
| 2029 | } |
| 2030 | |
| 2031 | set s [expr $::sqlite_io_error_hit==0] |
| 2032 | if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} { |
| 2033 | set r 1 |
| 2034 | } |
| 2035 | set ::sqlite_io_error_hit 0 |
| 2036 | |
| 2037 | # One of two things must have happened. either |
| 2038 | # 1. We never hit the IO error and the SQL returned OK |
| 2039 | # 2. An IO error was hit and the SQL failed |
| 2040 | # |
| 2041 | #puts "s=$s r=$r q=$q" |
| 2042 | expr { ($s && !$r && !$q) || (!$s && $r && $q) } |
| 2043 | } {1} |
| 2044 | |
| 2045 | set ::sqlite_io_error_hit 0 |
| 2046 | set ::sqlite_io_error_pending 0 |
| 2047 | |
| 2048 | # Check that no page references were leaked. There should be |
| 2049 | # a single reference if there is still an active transaction, |
| 2050 | # or zero otherwise. |
| 2051 | # |
| 2052 | # UPDATE: If the IO error occurs after a 'BEGIN' but before any |
| 2053 | # locks are established on database files (i.e. if the error |
| 2054 | # occurs while attempting to detect a hot-journal file), then |
| 2055 | # there may 0 page references and an active transaction according |
| 2056 | # to [sqlite3_get_autocommit]. |
| 2057 | # |
| 2058 | if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} { |
| 2059 | do_test $testname.$n.4 { |
| 2060 | set bt [btree_from_db db] |
| 2061 | db_enter db |
| 2062 | array set stats [btree_pager_stats $bt] |
| 2063 | db_leave db |
| 2064 | set nRef $stats(ref) |
| 2065 | expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)} |
| 2066 | } {1} |
| 2067 | } |
| 2068 | |
| 2069 | # If there is an open database handle and no open transaction, |
| 2070 | # and the pager is not running in exclusive-locking mode, |
| 2071 | # check that the pager is in "unlocked" state. Theoretically, |
| 2072 | # if a call to xUnlock() failed due to an IO error the underlying |
| 2073 | # file may still be locked. |
| 2074 | # |
| 2075 | ifcapable pragma { |
| 2076 | if { [info commands db] ne "" |
| 2077 | && $::ioerropts(-ckrefcount) |
| 2078 | && [db one {pragma locking_mode}] eq "normal" |
| 2079 | && [sqlite3_get_autocommit db] |
| 2080 | } { |
| 2081 | do_test $testname.$n.5 { |
| 2082 | set bt [btree_from_db db] |
| 2083 | db_enter db |
| 2084 | array set stats [btree_pager_stats $bt] |
| 2085 | db_leave db |
| 2086 | set stats(state) |
| 2087 | } 0 |
| 2088 | } |
| 2089 | } |
| 2090 | |
| 2091 | # If an IO error occurred, then the checksum of the database should |
| 2092 | # be the same as before the script that caused the IO error was run. |
| 2093 | # |
| 2094 | if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} { |
| 2095 | do_test $testname.$n.6 { |
| 2096 | catch {db close} |
| 2097 | catch {db2 close} |
| 2098 | set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db] |
| 2099 | set nowcksum [cksum] |
| 2100 | set res [expr {$nowcksum==$::checksum || $nowcksum==$::goodcksum}] |
| 2101 | if {$res==0} { |
| 2102 | output2 "now=$nowcksum" |
| 2103 | output2 "the=$::checksum" |
| 2104 | output2 "fwd=$::goodcksum" |
| 2105 | } |
| 2106 | set res |
| 2107 | } 1 |
| 2108 | } |
| 2109 | |
| 2110 | set ::sqlite_io_error_hardhit 0 |
| 2111 | set ::sqlite_io_error_pending 0 |
| 2112 | if {[info exists ::ioerropts(-cleanup)]} { |
| 2113 | catch $::ioerropts(-cleanup) |
| 2114 | } |
| 2115 | } |
| 2116 | set ::sqlite_io_error_pending 0 |
| 2117 | set ::sqlite_io_error_persist 0 |
| 2118 | unset ::ioerropts |
| 2119 | } |
| 2120 | |
| 2121 | # Return a checksum based on the contents of the main database associated |
| 2122 | # with connection $db |
| 2123 | # |
| 2124 | proc cksum {{db db}} { |
| 2125 | set txt [$db eval { |
| 2126 | SELECT name, type, sql FROM sqlite_master order by name |
| 2127 | }]\n |
| 2128 | foreach tbl [$db eval { |
| 2129 | SELECT name FROM sqlite_master WHERE type='table' order by name |
| 2130 | }] { |
| 2131 | append txt [$db eval "SELECT * FROM $tbl"]\n |
| 2132 | } |
| 2133 | foreach prag {default_synchronous default_cache_size} { |
| 2134 | append txt $prag-[$db eval "PRAGMA $prag"]\n |
| 2135 | } |
| 2136 | set cksum [string length $txt]-[md5 $txt] |
| 2137 | # puts $cksum-[file size test.db] |
| 2138 | return $cksum |
| 2139 | } |
| 2140 | |
| 2141 | # Generate a checksum based on the contents of the main and temp tables |
| 2142 | # database $db. If the checksum of two databases is the same, and the |
| 2143 | # integrity-check passes for both, the two databases are identical. |
| 2144 | # |
| 2145 | proc allcksum {{db db}} { |
| 2146 | set ret [list] |
| 2147 | ifcapable tempdb { |
| 2148 | set sql { |
| 2149 | SELECT name FROM sqlite_master WHERE type = 'table' UNION |
| 2150 | SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION |
| 2151 | SELECT 'sqlite_master' UNION |
| 2152 | SELECT 'sqlite_temp_master' ORDER BY 1 |
| 2153 | } |
| 2154 | } else { |
| 2155 | set sql { |
| 2156 | SELECT name FROM sqlite_master WHERE type = 'table' UNION |
| 2157 | SELECT 'sqlite_master' ORDER BY 1 |
| 2158 | } |
| 2159 | } |
| 2160 | set tbllist [$db eval $sql] |
| 2161 | set txt {} |
| 2162 | foreach tbl $tbllist { |
| 2163 | append txt [$db eval "SELECT * FROM $tbl"] |
| 2164 | } |
| 2165 | foreach prag {default_cache_size} { |
| 2166 | append txt $prag-[$db eval "PRAGMA $prag"]\n |
| 2167 | } |
| 2168 | # puts txt=$txt |
| 2169 | return [md5 $txt] |
| 2170 | } |
| 2171 | |
| 2172 | # Generate a checksum based on the contents of a single database with |
| 2173 | # a database connection. The name of the database is $dbname. |
| 2174 | # Examples of $dbname are "temp" or "main". |
| 2175 | # |
| 2176 | proc dbcksum {db dbname} { |
| 2177 | if {$dbname=="temp"} { |
| 2178 | set master sqlite_temp_master |
| 2179 | } else { |
| 2180 | set master $dbname.sqlite_master |
| 2181 | } |
| 2182 | set alltab [$db eval "SELECT name FROM $master WHERE type='table'"] |
| 2183 | set txt [$db eval "SELECT * FROM $master"]\n |
| 2184 | foreach tab $alltab { |
| 2185 | append txt [$db eval "SELECT * FROM $dbname.$tab"]\n |
| 2186 | } |
| 2187 | return [md5 $txt] |
| 2188 | } |
| 2189 | |
| 2190 | proc memdebug_log_sql {filename} { |
| 2191 | |
| 2192 | set data [sqlite3_memdebug_log dump] |
| 2193 | set nFrame [expr [llength [lindex $data 0]]-2] |
| 2194 | if {$nFrame < 0} { return "" } |
| 2195 | |
| 2196 | set database temp |
| 2197 | |
| 2198 | set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);" |
| 2199 | |
| 2200 | set sql "" |
| 2201 | foreach e $data { |
| 2202 | set nCall [lindex $e 0] |
| 2203 | set nByte [lindex $e 1] |
| 2204 | set lStack [lrange $e 2 end] |
| 2205 | append sql "INSERT INTO ${database}.malloc VALUES" |
| 2206 | append sql "('test', $nCall, $nByte, '$lStack');\n" |
| 2207 | foreach f $lStack { |
| 2208 | set frames($f) 1 |
| 2209 | } |
| 2210 | } |
| 2211 | |
| 2212 | set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n" |
| 2213 | set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n" |
| 2214 | |
| 2215 | set pid [pid] |
| 2216 | |
| 2217 | foreach f [array names frames] { |
| 2218 | set addr [format %x $f] |
| 2219 | set cmd "eu-addr2line --pid=$pid $addr" |
| 2220 | set line [eval exec $cmd] |
| 2221 | append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n" |
| 2222 | |
| 2223 | set file [lindex [split $line :] 0] |
| 2224 | set files($file) 1 |
| 2225 | } |
| 2226 | |
| 2227 | foreach f [array names files] { |
| 2228 | set contents "" |
| 2229 | catch { |
| 2230 | set fd [open $f] |
| 2231 | set contents [read $fd] |
| 2232 | close $fd |
| 2233 | } |
| 2234 | set contents [string map {' ''} $contents] |
| 2235 | append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n" |
| 2236 | } |
| 2237 | |
| 2238 | set escaped "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;" |
| 2239 | set escaped [string map [list "{" "\\{" "}" "\\}" "\\" "\\\\"] $escaped] |
| 2240 | |
| 2241 | set fd [open $filename w] |
| 2242 | puts $fd "set BUILTIN {" |
| 2243 | puts $fd $escaped |
| 2244 | puts $fd "}" |
| 2245 | puts $fd {set BUILTIN [string map [list "\\{" "{" "\\}" "}" "\\\\" "\\"] $BUILTIN]} |
| 2246 | set mtv [open $::testdir/malloctraceviewer.tcl] |
| 2247 | set txt [read $mtv] |
| 2248 | close $mtv |
| 2249 | puts $fd $txt |
| 2250 | close $fd |
| 2251 | } |
| 2252 | |
| 2253 | # Drop all tables in database [db] |
| 2254 | proc drop_all_tables {{db db}} { |
| 2255 | ifcapable trigger&&foreignkey { |
| 2256 | set pk [$db one "PRAGMA foreign_keys"] |
| 2257 | $db eval "PRAGMA foreign_keys = OFF" |
| 2258 | } |
| 2259 | foreach {idx name file} [db eval {PRAGMA database_list}] { |
| 2260 | if {$idx==1} { |
| 2261 | set master sqlite_temp_master |
| 2262 | } else { |
| 2263 | set master $name.sqlite_master |
| 2264 | } |
| 2265 | foreach {t type} [$db eval " |
| 2266 | SELECT name, type FROM $master |
| 2267 | WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X' |
| 2268 | "] { |
| 2269 | $db eval "DROP $type \"$t\"" |
| 2270 | } |
| 2271 | } |
| 2272 | ifcapable trigger&&foreignkey { |
| 2273 | $db eval "PRAGMA foreign_keys = $pk" |
| 2274 | } |
| 2275 | } |
| 2276 | |
| 2277 | # Drop all auxiliary indexes from the main database opened by handle [db]. |
| 2278 | # |
| 2279 | proc drop_all_indexes {{db db}} { |
| 2280 | set L [$db eval { |
| 2281 | SELECT name FROM sqlite_master WHERE type='index' AND sql LIKE 'create%' |
| 2282 | }] |
| 2283 | foreach idx $L { $db eval "DROP INDEX $idx" } |
| 2284 | } |
| 2285 | |
| 2286 | |
| 2287 | #------------------------------------------------------------------------- |
| 2288 | # If a test script is executed with global variable $::G(perm:name) set to |
| 2289 | # "wal", then the tests are run in WAL mode. Otherwise, they should be run |
| 2290 | # in rollback mode. The following Tcl procs are used to make this less |
| 2291 | # intrusive: |
| 2292 | # |
| 2293 | # wal_set_journal_mode ?DB? |
| 2294 | # |
| 2295 | # If running a WAL test, execute "PRAGMA journal_mode = wal" using |
| 2296 | # connection handle DB. Otherwise, this command is a no-op. |
| 2297 | # |
| 2298 | # wal_check_journal_mode TESTNAME ?DB? |
| 2299 | # |
| 2300 | # If running a WAL test, execute a tests case that fails if the main |
| 2301 | # database for connection handle DB is not currently a WAL database. |
| 2302 | # Otherwise (if not running a WAL permutation) this is a no-op. |
| 2303 | # |
| 2304 | # wal_is_wal_mode |
| 2305 | # |
| 2306 | # Returns true if this test should be run in WAL mode. False otherwise. |
| 2307 | # |
| 2308 | proc wal_is_wal_mode {} { |
| 2309 | expr {[permutation] eq "wal"} |
| 2310 | } |
| 2311 | proc wal_set_journal_mode {{db db}} { |
| 2312 | if { [wal_is_wal_mode] } { |
| 2313 | $db eval "PRAGMA journal_mode = WAL" |
| 2314 | } |
| 2315 | } |
| 2316 | proc wal_check_journal_mode {testname {db db}} { |
| 2317 | if { [wal_is_wal_mode] } { |
| 2318 | $db eval { SELECT * FROM sqlite_master } |
| 2319 | do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal} |
| 2320 | } |
| 2321 | } |
| 2322 | |
| 2323 | proc wal_is_capable {} { |
| 2324 | ifcapable !wal { return 0 } |
| 2325 | if {[permutation]=="journaltest"} { return 0 } |
| 2326 | return 1 |
| 2327 | } |
| 2328 | |
| 2329 | proc permutation {} { |
| 2330 | set perm "" |
| 2331 | catch {set perm $::G(perm:name)} |
| 2332 | set perm |
| 2333 | } |
| 2334 | proc presql {} { |
| 2335 | set presql "" |
| 2336 | catch {set presql $::G(perm:presql)} |
| 2337 | set presql |
| 2338 | } |
| 2339 | |
| 2340 | proc isquick {} { |
| 2341 | set ret 0 |
| 2342 | catch {set ret $::G(isquick)} |
| 2343 | set ret |
| 2344 | } |
| 2345 | |
| 2346 | #------------------------------------------------------------------------- |
| 2347 | # |
| 2348 | proc slave_test_script {script} { |
| 2349 | |
| 2350 | # Create the interpreter used to run the test script. |
| 2351 | interp create tinterp |
| 2352 | |
| 2353 | # Populate some global variables that tester.tcl expects to see. |
| 2354 | foreach {var value} [list \ |
| 2355 | ::argv0 $::argv0 \ |
| 2356 | ::argv {} \ |
| 2357 | ::SLAVE 1 \ |
| 2358 | ] { |
| 2359 | interp eval tinterp [list set $var $value] |
| 2360 | } |
| 2361 | |
| 2362 | # If output is being copied into a file, share the file-descriptor with |
| 2363 | # the interpreter. |
| 2364 | if {[info exists ::G(output_fd)]} { |
| 2365 | interp share {} $::G(output_fd) tinterp |
| 2366 | } |
| 2367 | |
| 2368 | # The alias used to access the global test counters. |
| 2369 | tinterp alias set_test_counter set_test_counter |
| 2370 | |
| 2371 | # Set up the ::cmdlinearg array in the slave. |
| 2372 | interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]] |
| 2373 | |
| 2374 | # Set up the ::G array in the slave. |
| 2375 | interp eval tinterp [list array set ::G [array get ::G]] |
| 2376 | |
| 2377 | # Load the various test interfaces implemented in C. |
| 2378 | load_testfixture_extensions tinterp |
| 2379 | |
| 2380 | # Run the test script. |
| 2381 | interp eval tinterp $script |
| 2382 | |
| 2383 | # Check if the interpreter call [run_thread_tests] |
| 2384 | if { [interp eval tinterp {info exists ::run_thread_tests_called}] } { |
| 2385 | set ::run_thread_tests_called 1 |
| 2386 | } |
| 2387 | |
| 2388 | # Delete the interpreter used to run the test script. |
| 2389 | interp delete tinterp |
| 2390 | } |
| 2391 | |
| 2392 | proc slave_test_file {zFile} { |
| 2393 | set tail [file tail $zFile] |
| 2394 | |
| 2395 | if {[info exists ::G(start:permutation)]} { |
| 2396 | if {[permutation] != $::G(start:permutation)} return |
| 2397 | unset ::G(start:permutation) |
| 2398 | } |
| 2399 | if {[info exists ::G(start:file)]} { |
| 2400 | if {$tail != $::G(start:file) && $tail!="$::G(start:file).test"} return |
| 2401 | unset ::G(start:file) |
| 2402 | } |
| 2403 | |
| 2404 | # Remember the value of the shared-cache setting. So that it is possible |
| 2405 | # to check afterwards that it was not modified by the test script. |
| 2406 | # |
| 2407 | ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] } |
| 2408 | |
| 2409 | # Run the test script in a slave interpreter. |
| 2410 | # |
| 2411 | unset -nocomplain ::run_thread_tests_called |
| 2412 | reset_prng_state |
| 2413 | set ::sqlite_open_file_count 0 |
| 2414 | set time [time { slave_test_script [list source $zFile] }] |
| 2415 | set ms [expr [lindex $time 0] / 1000] |
| 2416 | |
| 2417 | # Test that all files opened by the test script were closed. Omit this |
| 2418 | # if the test script has "thread" in its name. The open file counter |
| 2419 | # is not thread-safe. |
| 2420 | # |
| 2421 | if {[info exists ::run_thread_tests_called]==0} { |
| 2422 | do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0} |
| 2423 | } |
| 2424 | set ::sqlite_open_file_count 0 |
| 2425 | |
| 2426 | # Test that the global "shared-cache" setting was not altered by |
| 2427 | # the test script. |
| 2428 | # |
| 2429 | ifcapable shared_cache { |
| 2430 | set res [expr {[sqlite3_enable_shared_cache] == $scs}] |
| 2431 | do_test ${tail}-sharedcachesetting [list set {} $res] 1 |
| 2432 | } |
| 2433 | |
| 2434 | # Add some info to the output. |
| 2435 | # |
| 2436 | output2 "Time: $tail $ms ms" |
| 2437 | show_memstats |
| 2438 | } |
| 2439 | |
| 2440 | # Open a new connection on database test.db and execute the SQL script |
| 2441 | # supplied as an argument. Before returning, close the new conection and |
| 2442 | # restore the 4 byte fields starting at header offsets 28, 92 and 96 |
| 2443 | # to the values they held before the SQL was executed. This simulates |
| 2444 | # a write by a pre-3.7.0 client. |
| 2445 | # |
| 2446 | proc sql36231 {sql} { |
| 2447 | set B [hexio_read test.db 92 8] |
| 2448 | set A [hexio_read test.db 28 4] |
| 2449 | sqlite3 db36231 test.db |
| 2450 | catch { db36231 func a_string a_string } |
| 2451 | execsql $sql db36231 |
| 2452 | db36231 close |
| 2453 | hexio_write test.db 28 $A |
| 2454 | hexio_write test.db 92 $B |
| 2455 | return "" |
| 2456 | } |
| 2457 | |
| 2458 | proc db_save {} { |
| 2459 | foreach f [glob -nocomplain sv_test.db*] { forcedelete $f } |
| 2460 | foreach f [glob -nocomplain test.db*] { |
| 2461 | set f2 "sv_$f" |
| 2462 | forcecopy $f $f2 |
| 2463 | } |
| 2464 | } |
| 2465 | proc db_save_and_close {} { |
| 2466 | db_save |
| 2467 | catch { db close } |
| 2468 | return "" |
| 2469 | } |
| 2470 | proc db_restore {} { |
| 2471 | foreach f [glob -nocomplain test.db*] { forcedelete $f } |
| 2472 | foreach f2 [glob -nocomplain sv_test.db*] { |
| 2473 | set f [string range $f2 3 end] |
| 2474 | forcecopy $f2 $f |
| 2475 | } |
| 2476 | } |
| 2477 | proc db_restore_and_reopen {{dbfile test.db}} { |
| 2478 | catch { db close } |
| 2479 | db_restore |
| 2480 | sqlite3 db $dbfile |
| 2481 | } |
| 2482 | proc db_delete_and_reopen {{file test.db}} { |
| 2483 | catch { db close } |
| 2484 | foreach f [glob -nocomplain test.db*] { forcedelete $f } |
| 2485 | sqlite3 db $file |
| 2486 | } |
| 2487 | |
| 2488 | # Close any connections named [db], [db2] or [db3]. Then use sqlite3_config |
| 2489 | # to configure the size of the PAGECACHE allocation using the parameters |
| 2490 | # provided to this command. Save the old PAGECACHE parameters in a global |
| 2491 | # variable so that [test_restore_config_pagecache] can restore the previous |
| 2492 | # configuration. |
| 2493 | # |
| 2494 | # Before returning, reopen connection [db] on file test.db. |
| 2495 | # |
| 2496 | proc test_set_config_pagecache {sz nPg} { |
| 2497 | catch {db close} |
| 2498 | catch {db2 close} |
| 2499 | catch {db3 close} |
| 2500 | |
| 2501 | sqlite3_shutdown |
| 2502 | set ::old_pagecache_config [sqlite3_config_pagecache $sz $nPg] |
| 2503 | sqlite3_initialize |
| 2504 | autoinstall_test_functions |
| 2505 | reset_db |
| 2506 | } |
| 2507 | |
| 2508 | # Close any connections named [db], [db2] or [db3]. Then use sqlite3_config |
| 2509 | # to configure the size of the PAGECACHE allocation to the size saved in |
| 2510 | # the global variable by an earlier call to [test_set_config_pagecache]. |
| 2511 | # |
| 2512 | # Before returning, reopen connection [db] on file test.db. |
| 2513 | # |
| 2514 | proc test_restore_config_pagecache {} { |
| 2515 | catch {db close} |
| 2516 | catch {db2 close} |
| 2517 | catch {db3 close} |
| 2518 | |
| 2519 | sqlite3_shutdown |
| 2520 | if {[info exists ::old_pagecache_config]} { |
| 2521 | eval sqlite3_config_pagecache $::old_pagecache_config |
| 2522 | unset ::old_pagecache_config |
| 2523 | } |
| 2524 | sqlite3_initialize |
| 2525 | autoinstall_test_functions |
| 2526 | sqlite3 db test.db |
| 2527 | } |
| 2528 | |
| 2529 | proc test_binary_name {nm} { |
| 2530 | if {$::tcl_platform(platform) eq "windows"} { |
| 2531 | set ret "$nm.exe" |
| 2532 | } else { |
| 2533 | set ret $nm |
| 2534 | } |
| 2535 | file normalize [file join $::cmdlinearg(TESTFIXTURE_HOME) $ret] |
| 2536 | } |
| 2537 | |
| 2538 | proc test_find_binary {nm} { |
| 2539 | set ret [test_binary_name $nm] |
| 2540 | if {![file executable $ret]} { |
| 2541 | finish_test |
| 2542 | return "" |
| 2543 | } |
| 2544 | return $ret |
| 2545 | } |
| 2546 | |
| 2547 | # Find the name of the 'shell' executable (e.g. "sqlite3.exe") to use for |
| 2548 | # the tests in shell*.test. If no such executable can be found, invoke |
| 2549 | # [finish_test ; return] in the callers context. |
| 2550 | # |
| 2551 | proc test_find_cli {} { |
| 2552 | set prog [test_find_binary sqlite3] |
| 2553 | if {$prog==""} { return -code return } |
| 2554 | return $prog |
| 2555 | } |
| 2556 | |
| 2557 | # Find invocation of the 'shell' executable (e.g. "sqlite3.exe") to use |
| 2558 | # for the tests in shell*.test with optional valgrind prefix when the |
| 2559 | # environment variable SQLITE_CLI_VALGRIND_OPT is set. The set value |
| 2560 | # operates as follows: |
| 2561 | # empty or 0 => no valgrind prefix; |
| 2562 | # 1 => valgrind options for memory leak check; |
| 2563 | # other => use value as valgrind options. |
| 2564 | # If shell not found, invoke [finish_test ; return] in callers context. |
| 2565 | # |
| 2566 | proc test_cli_invocation {} { |
| 2567 | set prog [test_find_binary sqlite3] |
| 2568 | if {$prog==""} { return -code return } |
| 2569 | set vgrun [expr {[permutation]=="valgrind"}] |
| 2570 | if {$vgrun || [info exists ::env(SQLITE_CLI_VALGRIND_OPT)]} { |
| 2571 | if {$vgrun} { |
| 2572 | set vgo "--quiet" |
| 2573 | } else { |
| 2574 | set vgo $::env(SQLITE_CLI_VALGRIND_OPT) |
| 2575 | } |
| 2576 | if {$vgo == 0 || $vgo eq ""} { |
| 2577 | return $prog |
| 2578 | } elseif {$vgo == 1} { |
| 2579 | return "valgrind --quiet --leak-check=yes $prog" |
| 2580 | } else { |
| 2581 | return "valgrind $vgo $prog" |
| 2582 | } |
| 2583 | } else { |
| 2584 | return $prog |
| 2585 | } |
| 2586 | } |
| 2587 | |
| 2588 | # Find the name of the 'sqldiff' executable (e.g. "sqlite3.exe") to use for |
| 2589 | # the tests in sqldiff tests. If no such executable can be found, invoke |
| 2590 | # [finish_test ; return] in the callers context. |
| 2591 | # |
| 2592 | proc test_find_sqldiff {} { |
| 2593 | set prog [test_find_binary sqldiff] |
| 2594 | if {$prog==""} { return -code return } |
| 2595 | return $prog |
| 2596 | } |
| 2597 | |
| 2598 | # Call sqlite3_expanded_sql() on all statements associated with database |
| 2599 | # connection $db. This sometimes finds use-after-free bugs if run with |
| 2600 | # valgrind or address-sanitizer. |
| 2601 | proc expand_all_sql {db} { |
| 2602 | set stmt "" |
| 2603 | while {[set stmt [sqlite3_next_stmt $db $stmt]]!=""} { |
| 2604 | sqlite3_expanded_sql $stmt |
| 2605 | } |
| 2606 | } |
| 2607 | |
| 2608 | |
| 2609 | # If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set |
| 2610 | # to non-zero, then set the global variable $AUTOVACUUM to 1. |
| 2611 | set AUTOVACUUM $sqlite_options(default_autovacuum) |
| 2612 | |
| 2613 | # Make sure the FTS enhanced query syntax is disabled. |
| 2614 | set sqlite_fts3_enable_parentheses 0 |
| 2615 | |
| 2616 | # During testing, assume that all database files are well-formed. The |
| 2617 | # few test cases that deliberately corrupt database files should rescind |
| 2618 | # this setting by invoking "database_can_be_corrupt" |
| 2619 | # |
| 2620 | database_never_corrupt |
| 2621 | extra_schema_checks 1 |
| 2622 | |
| 2623 | source $testdir/thread_common.tcl |
| 2624 | source $testdir/malloc_common.tcl |
| 2625 | |
| 2626 | set tester_tcl_has_run 1 |