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