1# 2006 September 4 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 regression tests for SQLite library. 12# 13# $Id: misc7.test,v 1.29 2009/07/16 18:21:18 drh Exp $ 14 15set testdir [file dirname $argv0] 16source $testdir/tester.tcl 17set testprefix misc7 18 19if {[clang_sanitize_address]==0} { 20 do_test misc7-1-misuse { 21 c_misuse_test 22 } {} 23} 24 25do_test misc7-2 { 26 c_realloc_test 27} {} 28 29do_test misc7-3 { 30 c_collation_test 31} {} 32 33# Try to open a directory: 34# 35do_test misc7-4 { 36 delete_file mydir 37 file mkdir mydir 38 set rc [catch { 39 sqlite3 db2 ./mydir 40 } msg] 41 list $rc $msg 42} {1 {unable to open database file}} 43 44# Try to open a file with a directory where its journal file should be. 45# 46if {[atomic_batch_write test.db]==0} { 47 do_test misc7-5 { 48 delete_file mydir 49 file mkdir mydir-journal 50 sqlite3 db2 ./mydir 51 catchsql { 52 CREATE TABLE abc(a, b, c); 53 } db2 54 } {1 {unable to open database file}} 55 db2 close 56} 57 58#-------------------------------------------------------------------- 59# The following tests, misc7-6.* test the libraries behaviour when 60# it cannot open a file. To force this condition, we use up all the 61# file-descriptors before running sqlite. This probably only works 62# on unix. 63# 64 65proc use_up_files {} { 66 set ret [list] 67 catch { 68 while 1 { lappend ret [open test.db] } 69 } 70 return $ret 71} 72 73proc do_fileopen_test {prefix sql} { 74 set fd_list [use_up_files] 75 set ::go 1 76 set ::n 1 77 set ::sql $sql 78 while {$::go} { 79 catch {db close} 80 do_test ${prefix}.${::n} { 81 set rc [catch { 82 sqlite db test.db 83 db eval $::sql 84 } msg] 85 if {$rc == 0} {set ::go 0} 86 87 expr {$rc == 0 || ($rc == 1 && [string first unable $msg]==0)} 88 } 1 89 90 close [lindex $fd_list 0] 91 set fd_list [lrange $fd_list 1 end] 92 incr ::n 93 } 94 foreach fd $fd_list { 95 close $fd 96 } 97 db close 98} 99 100execsql { CREATE TABLE abc(a PRIMARY KEY, b, c); } 101db close 102 103if {$tcl_platform(platform)!="windows"} { 104 do_fileopen_test misc7-6.1 { 105 BEGIN; 106 INSERT INTO abc VALUES(1, 2, 3); 107 INSERT INTO abc VALUES(2, 3, 4); 108 INSERT INTO abc SELECT a+2, b, c FROM abc; 109 COMMIT; 110 } 111 112 do_fileopen_test misc7-6.2 { 113 PRAGMA temp.cache_size = 1000; 114 } 115} 116 117# 118# End of tests for out-of-file-descriptors condition. 119#-------------------------------------------------------------------- 120 121sqlite3 db test.db 122execsql { 123 DELETE FROM abc; 124 INSERT INTO abc VALUES(1, 2, 3); 125 INSERT INTO abc VALUES(2, 3, 4); 126 INSERT INTO abc SELECT a+2, b, c FROM abc; 127} 128 129 130#-------------------------------------------------------------------- 131# Test that the sqlite3_busy_timeout call seems to delay approximately 132# the right amount of time. 133# 134do_test misc7-7.0 { 135 sqlite3 db2 test.db 136 sqlite3_busy_timeout [sqlite3_connection_pointer db] 2000 137 execsql { 138 BEGIN EXCLUSIVE; 139 } db2 140 141 # Now db2 has an exclusive lock on the database file, and db has 142 # a busy-timeout of 2000 milliseconds. So check that trying to 143 # access the database using connection db delays for at least 1500 ms. 144 # 145 set tm [time { 146 set result [catchsql { 147 SELECT * FROM sqlite_master; 148 } db] 149 }] 150 set delay [lindex $tm 0] ;# In microseconds 151 lappend result [expr {$delay>1500000 && $delay<4000000}] 152} {1 {database is locked} 1} 153db2 close 154 155#-------------------------------------------------------------------- 156# Test that nothing goes horribly wrong when attaching a database 157# after the omit_readlock pragma has been exercised. 158# 159# Note: The PRAGMA omit_readlock was an early hack to disable the 160# fcntl() calls for read-only databases so that read-only databases could 161# be read on broken NFS systems. That pragma has now been removed. 162# (Use the unix-none VFS as a replacement, if needed.) But these tests 163# do not really depend on omit_readlock, so we left them in place. 164# 165do_test misc7-7.1 { 166 forcedelete test2.db 167 forcedelete test2.db-journal 168 execsql { 169 PRAGMA omit_readlock = 1; 170 ATTACH 'test2.db' AS aux; 171 CREATE TABLE aux.hello(world); 172 SELECT name FROM aux.sqlite_master; 173 } 174} {hello} 175do_test misc7-7.2 { 176 execsql { 177 DETACH aux; 178 } 179} {} 180do_test misc7-7.3 { 181 db close 182 sqlite3 db test.db -readonly 1 183 execsql { 184 PRAGMA omit_readlock = 1; 185 ATTACH 'test2.db' AS aux; 186 SELECT name FROM aux.sqlite_master; 187 SELECT name FROM aux.sqlite_master; 188 } 189} {hello hello} 190do_test misc7-7.3 { 191 db close 192 sqlite3 db test.db 193 set ::DB [sqlite3_connection_pointer db] 194 list 195} {} 196 197# Test the UTF-16 version of the "out of memory" message (used when 198# malloc fails during sqlite3_open() ). 199# 200ifcapable utf16 { 201 do_test misc7-8 { 202 encoding convertfrom unicode [sqlite3_errmsg16 0x00000000] 203 } {out of memory} 204} 205 206do_test misc7-9 { 207 execsql { 208 SELECT * 209 FROM (SELECT name+1 AS one FROM sqlite_master LIMIT 1 OFFSET 1) 210 WHERE one LIKE 'hello%'; 211 } 212} {} 213 214#-------------------------------------------------------------------- 215# Improve coverage for vtab code. 216# 217ifcapable vtab { 218 # Run some debug code to improve reported coverage 219 # 220 221 # set sqlite_where_trace 1 222 do_test misc7-10 { 223 register_echo_module [sqlite3_connection_pointer db] 224 execsql { 225 CREATE VIRTUAL TABLE t1 USING echo(abc); 226 SELECT a FROM t1 WHERE a = 1 ORDER BY b; 227 } 228 } {1} 229 set sqlite_where_trace 0 230 231 # Specify an ORDER BY clause that cannot be indexed. 232 do_test misc7-11 { 233 execsql { 234 SELECT t1.a, t2.a FROM t1, t1 AS t2 ORDER BY 2 LIMIT 1; 235 } 236 } {1 1} 237 238 # The whole point of this is to test an error code other than 239 # SQLITE_NOMEM from the vtab xBestIndex callback. 240 # 241 do_ioerr_test misc7-12 -tclprep { 242 sqlite3 db2 test.db 243 register_echo_module [sqlite3_connection_pointer db2] 244 db2 eval { 245 CREATE TABLE abc(a PRIMARY KEY, b, c); 246 INSERT INTO abc VALUES(1, 2, 3); 247 CREATE VIRTUAL TABLE t1 USING echo(abc); 248 } 249 db2 close 250 } -tclbody { 251 register_echo_module [sqlite3_connection_pointer db] 252 execsql {SELECT * FROM t1 WHERE a = 1;} 253 } 254 255 # The case where the virtual table module returns a very large number 256 # as the cost of a scan (greater than SQLITE_BIG_DOUBLE in the code). 257 # 258 do_test misc7-13 { 259 sqlite3 db test.db 260 register_echo_module [sqlite3_connection_pointer db] 261 set ::echo_module_cost 2.0e+99 262 execsql {SELECT * FROM t1 WHERE a = 1;} 263 } {1 2 3} 264 unset ::echo_module_cost 265} 266 267db close 268forcedelete test.db 269forcedelete test.db-journal 270sqlite3 db test.db 271 272ifcapable explain { 273 do_execsql_test misc7-14.0 { 274 CREATE TABLE abc(a PRIMARY KEY, b, c); 275 } 276 do_eqp_test misc7-14.1 { 277 SELECT * FROM abc AS t2 WHERE rowid = 1; 278 } { 279 QUERY PLAN 280 `--SEARCH TABLE abc AS t2 USING INTEGER PRIMARY KEY (rowid=?) 281} 282 do_eqp_test misc7-14.2 { 283 SELECT * FROM abc AS t2 WHERE a = 1; 284} { 285 QUERY PLAN 286 `--SEARCH TABLE abc AS t2 USING INDEX sqlite_autoindex_abc_1 (a=?) 287} 288 do_eqp_test misc7-14.3 { 289 SELECT * FROM abc AS t2 ORDER BY a; 290 } { 291 QUERY PLAN 292 `--SCAN TABLE abc AS t2 USING INDEX sqlite_autoindex_abc_1 293} 294} 295 296db close 297forcedelete test.db 298forcedelete test.db-journal 299sqlite3 db test.db 300 301#-------------------------------------------------------------------- 302# This is all to force the pager_remove_from_stmt_list() function 303# (inside pager.c) to remove a pager from the middle of the 304# statement-list. 305# 306do_test misc7-15.1 { 307 execsql { 308 PRAGMA cache_size = 10; 309 BEGIN; 310 CREATE TABLE abc(a PRIMARY KEY, b, c); 311 INSERT INTO abc 312 VALUES(randstr(100,100), randstr(100,100), randstr(100,100)); 313 INSERT INTO abc SELECT 314 randstr(100,100), randstr(100,100), randstr(100,100) FROM abc; 315 INSERT INTO abc SELECT 316 randstr(100,100), randstr(100,100), randstr(100,100) FROM abc; 317 INSERT INTO abc SELECT 318 randstr(100,100), randstr(100,100), randstr(100,100) FROM abc; 319 INSERT INTO abc SELECT 320 randstr(100,100), randstr(100,100), randstr(100,100) FROM abc; 321 INSERT INTO abc SELECT 322 randstr(100,100), randstr(100,100), randstr(100,100) FROM abc; 323 INSERT INTO abc SELECT 324 randstr(100,100), randstr(100,100), randstr(100,100) FROM abc; 325 INSERT INTO abc SELECT 326 randstr(100,100), randstr(100,100), randstr(100,100) FROM abc; 327 INSERT INTO abc SELECT 328 randstr(100,100), randstr(100,100), randstr(100,100) FROM abc; 329 COMMIT; 330 } 331 expr {[file size test.db]>10240} 332} {1} 333do_test misc7-15.2 { 334 execsql { 335 DELETE FROM abc WHERE rowid > 12; 336 INSERT INTO abc SELECT 337 randstr(100,100), randstr(100,100), randstr(100,100) FROM abc; 338 } 339} {} 340 341db close 342forcedelete test.db 343forcedelete test.db-journal 344sqlite3 db test.db 345 346do_ioerr_test misc7-16 -sqlprep { 347 PRAGMA cache_size = 10; 348 PRAGMA default_cache_size = 10; 349 CREATE TABLE t3(a, b, UNIQUE(a, b)); 350 INSERT INTO t3 VALUES( randstr(100, 100), randstr(100, 100) ); 351 INSERT INTO t3 SELECT randstr(100, 100), randstr(100, 100) FROM t3; 352 INSERT INTO t3 SELECT randstr(100, 100), randstr(100, 100) FROM t3; 353 INSERT INTO t3 SELECT randstr(100, 100), randstr(100, 100) FROM t3; 354 INSERT INTO t3 SELECT randstr(100, 100), randstr(100, 100) FROM t3; 355 INSERT INTO t3 SELECT randstr(100, 100), randstr(100, 100) FROM t3; 356 UPDATE t3 357 SET b = 'hello world' 358 WHERE rowid >= (SELECT max(rowid)-1 FROM t3); 359} -tclbody { 360 set rc [catch {db eval { 361 BEGIN; 362 PRAGMA cache_size = 10; 363 INSERT INTO t3 VALUES( randstr(100, 100), randstr(100, 100) ); 364 UPDATE t3 SET a = b; 365 COMMIT; 366 }} msg] 367 368 if {!$rc || ($rc && [string first "UNIQUE" $msg]==0)} { 369 set msg 370 } else { 371 error $msg 372 } 373} 374 375sqlite3 db test.db 376 377do_test misc7-16.X { 378 execsql { 379 SELECT count(*) FROM t3; 380 } 381} {32} 382 383#---------------------------------------------------------------------- 384# Test the situation where a hot-journal is discovered but write-access 385# to it is denied. This should return SQLITE_BUSY. 386# 387# These tests do not work on windows due to restrictions in the 388# windows file system. 389# 390if {$tcl_platform(platform)!="windows"} { 391 392 # Some network filesystems (ex: AFP) do not support setting read-only 393 # permissions. Only run these tests if full unix permission setting 394 # capabilities are supported. 395 # 396 file attributes test.db -permissions rw-r--r-- 397 if {[file attributes test.db -permissions]==0644} { 398 399 do_test misc7-17.1 { 400 execsql { 401 BEGIN; 402 DELETE FROM t3 WHERE (oid%3)==0; 403 } 404 forcecopy test.db bak.db 405 forcecopy test.db-journal bak.db-journal 406 execsql { 407 COMMIT; 408 } 409 410 db close 411 forcecopy bak.db test.db 412 forcecopy bak.db-journal test.db-journal 413 sqlite3 db test.db 414 415 catch {file attributes test.db-journal -permissions r--------} 416 catch {file attributes test.db-journal -readonly 1} 417 catchsql { 418 SELECT count(*) FROM t3; 419 } 420 } {1 {unable to open database file}} 421 do_test misc7-17.2 { 422 # Note that the -readonly flag must be cleared before the -permissions 423 # are set. Otherwise, when using tcl 8.5 on mac, the fact that the 424 # -readonly flag is set causes the attempt to set the permissions 425 # to fail. 426 catch {file attributes test.db-journal -readonly 0} 427 catch {file attributes test.db-journal -permissions rw-------} 428 catchsql { 429 SELECT count(*) FROM t3; 430 } 431 } {0 32} 432 433 # sqlite3_test_control_pending_page [expr ($::sqlite_pending_byte / 1024) + 1] 434 set ::pending_byte_page [expr ($::sqlite_pending_byte / 1024) + 1] 435 sqlite3_test_control_pending_byte $::sqlite_pending_byte 436 do_test misc7-17.3 { 437 db eval { 438 pragma writable_schema = true; 439 UPDATE sqlite_master 440 SET rootpage = $pending_byte_page 441 WHERE type = 'table' AND name = 't3'; 442 } 443 execsql { 444 SELECT rootpage FROM sqlite_master WHERE type = 'table' AND name = 't3'; 445 } 446 } $::pending_byte_page 447 448 do_test misc7-17.4 { 449 db close 450 sqlite3 db test.db 451 catchsql { 452 SELECT count(*) FROM t3; 453 } 454 } {1 {database disk image is malformed}} 455 } 456} 457 458# Ticket #2470 459# 460do_test misc7-18.1 { 461 execsql { 462 CREATE TABLE table_1 (col_10); 463 CREATE TABLE table_2 ( 464 col_1, col_2, col_3, col_4, col_5, 465 col_6, col_7, col_8, col_9, col_10 466 ); 467 SELECT a.col_10 468 FROM 469 (SELECT table_1.col_10 AS col_10 FROM table_1) a, 470 (SELECT table_1.col_10, table_2.col_9 AS qcol_9 471 FROM table_1, table_2 472 GROUP BY table_1.col_10, qcol_9); 473 } 474} {} 475 476# Testing boundary conditions on sqlite3_status() 477# 478do_test misc7-19.1 { 479 sqlite3_status -1 0 480} {21 0 0} 481do_test misc7-19.2 { 482 sqlite3_status 1000 0 483} {21 0 0} 484 485 486# sqlite3_global_recover() is a no-op. But we might as well test it 487# if only to get the test coverage. 488# 489do_test misc7-20.1 { 490 sqlite3_global_recover 491} {SQLITE_OK} 492 493# Try to open a really long file name. 494# 495do_test misc7-21.1 { 496 set zFile [file join [get_pwd] "[string repeat abcde 104].db"] 497 set rc [catch {sqlite3 db2 $zFile} msg] 498 list $rc $msg 499} {1 {unable to open database file}} 500 501# Try to do hot-journal rollback with a read-only connection. The 502# error code should be SQLITE_READONLY_ROLLBACK. 503# 504do_test misc7-22.1 { 505 db close 506 forcedelete test.db copy.db-journal 507 sqlite3 db test.db 508 execsql { 509 CREATE TABLE t1(a, b); 510 INSERT INTO t1 VALUES(1, 2); 511 INSERT INTO t1 VALUES(3, 4); 512 } 513 db close 514 sqlite3 db test.db -readonly 1 515 catchsql { 516 INSERT INTO t1 VALUES(5, 6); 517 } 518} {1 {attempt to write a readonly database}} 519do_test misc7-22.2 { execsql { SELECT * FROM t1 } } {1 2 3 4} 520do_test misc7-22.3 { 521 set fd [open test.db-journal w] 522 puts $fd [string repeat abc 1000] 523 close $fd 524 catchsql { SELECT * FROM t1 } 525} {1 {attempt to write a readonly database}} 526do_test misc7-22.4 { 527 sqlite3_extended_errcode db 528} SQLITE_READONLY_ROLLBACK 529catch { db close } 530forcedelete test.db 531 532if {$::tcl_platform(platform)=="unix" 533 && [atomic_batch_write test.db]==0 534} { 535 reset_db 536 do_execsql_test 23.0 { 537 CREATE TABLE t1(x, y); 538 INSERT INTO t1 VALUES(1, 2); 539 } 540 541 do_test 23.1 { 542 db close 543 forcedelete tst 544 file mkdir tst 545 forcecopy test.db tst/test.db 546 file attributes tst -permissions r-xr-xr-x 547 } {} 548 549 sqlite3 db tst/test.db 550 do_execsql_test 23.2 { 551 SELECT * FROM t1; 552 } {1 2} 553 554 do_catchsql_test 23.3 { 555 INSERT INTO t1 VALUES(3, 4); 556 } {1 {attempt to write a readonly database}} 557 558 do_test 23.4 { 559 sqlite3_extended_errcode db 560 } {SQLITE_READONLY_DIRECTORY} 561 562 do_test 23.5 { 563 db close 564 forcedelete tst 565 } {} 566} 567 568finish_test 569