3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library.
13 # $Id: misc7.test,v 1.29 2009/07/16 18:21:18 drh Exp $
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
19 if {[clang_sanitize_address]==0} {
20 do_test misc7-1-misuse {
33 # Try to open a directory:
42 } {1 {unable to open database file}}
44 # Try to open a file with a directory where its journal file should be.
46 if {[atomic_batch_write test.db]==0} {
49 file mkdir mydir-journal
52 CREATE TABLE abc(a, b, c);
54 } {1 {unable to open database file}}
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
65 proc use_up_files {} {
68 while 1 { lappend ret [open test.db] }
73 proc do_fileopen_test {prefix sql} {
74 set fd_list [use_up_files]
80 do_test ${prefix}.${::n} {
85 if {$rc == 0} {set ::go 0}
87 expr {$rc == 0 || ($rc == 1 && [string first unable $msg]==0)}
90 close [lindex $fd_list 0]
91 set fd_list [lrange $fd_list 1 end]
100 execsql { CREATE TABLE abc(a PRIMARY KEY, b, c); }
103 if {$tcl_platform(platform)!="windows"} {
104 do_fileopen_test misc7-6.1 {
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;
112 do_fileopen_test misc7-6.2 {
113 PRAGMA temp.cache_size = 1000;
118 # End of tests for out-of-file-descriptors condition.
119 #--------------------------------------------------------------------
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;
130 #--------------------------------------------------------------------
131 # Test that the sqlite3_busy_timeout call seems to delay approximately
132 # the right amount of time.
136 sqlite3_busy_timeout [sqlite3_connection_pointer db] 2000
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.
146 set result [catchsql {
147 SELECT * FROM sqlite_master;
150 set delay [lindex $tm 0] ;# In microseconds
151 lappend result [expr {$delay>1500000 && $delay<4000000}]
152 } {1 {database is locked} 1}
155 #--------------------------------------------------------------------
156 # Test that nothing goes horribly wrong when attaching a database
157 # after the omit_readlock pragma has been exercised.
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.
167 forcedelete test2.db-journal
169 PRAGMA omit_readlock = 1;
170 ATTACH 'test2.db' AS aux;
171 CREATE TABLE aux.hello(world);
172 SELECT name FROM aux.sqlite_master;
182 sqlite3 db test.db -readonly 1
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;
193 set ::DB [sqlite3_connection_pointer db]
197 # Test the UTF-16 version of the "out of memory" message (used when
198 # malloc fails during sqlite3_open() ).
202 encoding convertfrom unicode [sqlite3_errmsg16 0x00000000]
209 FROM (SELECT name+1 AS one FROM sqlite_master LIMIT 1 OFFSET 1)
210 WHERE one LIKE 'hello%';
214 #--------------------------------------------------------------------
215 # Improve coverage for vtab code.
218 # Run some debug code to improve reported coverage
221 # set sqlite_where_trace 1
223 register_echo_module [sqlite3_connection_pointer db]
225 CREATE VIRTUAL TABLE t1 USING echo(abc);
226 SELECT a FROM t1 WHERE a = 1 ORDER BY b;
229 set sqlite_where_trace 0
231 # Specify an ORDER BY clause that cannot be indexed.
234 SELECT t1.a, t2.a FROM t1, t1 AS t2 ORDER BY 2 LIMIT 1;
238 # The whole point of this is to test an error code other than
239 # SQLITE_NOMEM from the vtab xBestIndex callback.
241 do_ioerr_test misc7-12 -tclprep {
243 register_echo_module [sqlite3_connection_pointer db2]
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);
251 register_echo_module [sqlite3_connection_pointer db]
252 execsql {SELECT * FROM t1 WHERE a = 1;}
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).
260 register_echo_module [sqlite3_connection_pointer db]
261 set ::echo_module_cost 2.0e+99
262 execsql {SELECT * FROM t1 WHERE a = 1;}
264 unset ::echo_module_cost
269 forcedelete test.db-journal
273 do_execsql_test misc7-14.0 {
274 CREATE TABLE abc(a PRIMARY KEY, b, c);
276 do_eqp_test misc7-14.1 {
277 SELECT * FROM abc AS t2 WHERE rowid = 1;
280 `--SEARCH TABLE abc AS t2 USING INTEGER PRIMARY KEY (rowid=?)
282 do_eqp_test misc7-14.2 {
283 SELECT * FROM abc AS t2 WHERE a = 1;
286 `--SEARCH TABLE abc AS t2 USING INDEX sqlite_autoindex_abc_1 (a=?)
288 do_eqp_test misc7-14.3 {
289 SELECT * FROM abc AS t2 ORDER BY a;
292 `--SCAN TABLE abc AS t2 USING INDEX sqlite_autoindex_abc_1
298 forcedelete test.db-journal
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
308 PRAGMA cache_size = 10;
310 CREATE TABLE abc(a PRIMARY KEY, b, c);
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;
331 expr {[file size test.db]>10240}
335 DELETE FROM abc WHERE rowid > 12;
336 INSERT INTO abc SELECT
337 randstr(100,100), randstr(100,100), randstr(100,100) FROM abc;
343 forcedelete test.db-journal
346 do_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;
357 SET b = 'hello world'
358 WHERE rowid >= (SELECT max(rowid)-1 FROM t3);
360 set rc [catch {db eval {
362 PRAGMA cache_size = 10;
363 INSERT INTO t3 VALUES( randstr(100, 100), randstr(100, 100) );
368 if {!$rc || ($rc && [string first "UNIQUE" $msg]==0)} {
379 SELECT count(*) FROM t3;
383 #----------------------------------------------------------------------
384 # Test the situation where a hot-journal is discovered but write-access
385 # to it is denied. This should return SQLITE_BUSY.
387 # These tests do not work on windows due to restrictions in the
388 # windows file system.
390 if {$tcl_platform(platform)!="windows"} {
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.
396 file attributes test.db -permissions rw-r--r--
397 if {[file attributes test.db -permissions]==0644} {
402 DELETE FROM t3 WHERE (oid%3)==0;
404 forcecopy test.db bak.db
405 forcecopy test.db-journal bak.db-journal
411 forcecopy bak.db test.db
412 forcecopy bak.db-journal test.db-journal
415 catch {file attributes test.db-journal -permissions r--------}
416 catch {file attributes test.db-journal -readonly 1}
418 SELECT count(*) FROM t3;
420 } {1 {unable to open database file}}
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
426 catch {file attributes test.db-journal -readonly 0}
427 catch {file attributes test.db-journal -permissions rw-------}
429 SELECT count(*) FROM t3;
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
437 sqlite3_db_config db DEFENSIVE 0
439 pragma writable_schema = true;
441 SET rootpage = $pending_byte_page
442 WHERE type = 'table' AND name = 't3';
445 SELECT rootpage FROM sqlite_master WHERE type = 'table' AND name = 't3';
447 } $::pending_byte_page
453 SELECT count(*) FROM t3;
455 } {1 {database disk image is malformed}}
463 CREATE TABLE table_1 (col_10);
464 CREATE TABLE table_2 (
465 col_1, col_2, col_3, col_4, col_5,
466 col_6, col_7, col_8, col_9, col_10
470 (SELECT table_1.col_10 AS col_10 FROM table_1) a,
471 (SELECT table_1.col_10, table_2.col_9 AS qcol_9
472 FROM table_1, table_2
473 GROUP BY table_1.col_10, qcol_9);
477 # Testing boundary conditions on sqlite3_status()
483 sqlite3_status 1000 0
487 # sqlite3_global_recover() is a no-op. But we might as well test it
488 # if only to get the test coverage.
491 sqlite3_global_recover
494 # Try to open a really long file name.
497 set zFile [file join [get_pwd] "[string repeat abcde 104].db"]
498 set rc [catch {sqlite3 db2 $zFile} msg]
500 } {1 {unable to open database file}}
502 # Try to do hot-journal rollback with a read-only connection. The
503 # error code should be SQLITE_READONLY_ROLLBACK.
507 forcedelete test.db copy.db-journal
510 CREATE TABLE t1(a, b);
511 INSERT INTO t1 VALUES(1, 2);
512 INSERT INTO t1 VALUES(3, 4);
515 sqlite3 db test.db -readonly 1
517 INSERT INTO t1 VALUES(5, 6);
519 } {1 {attempt to write a readonly database}}
520 do_test misc7-22.2 { execsql { SELECT * FROM t1 } } {1 2 3 4}
522 set fd [open test.db-journal w]
523 puts $fd [string repeat abc 1000]
525 catchsql { SELECT * FROM t1 }
526 } {1 {attempt to write a readonly database}}
528 sqlite3_extended_errcode db
529 } SQLITE_READONLY_ROLLBACK
533 if {$::tcl_platform(platform)=="unix"
534 && [atomic_batch_write test.db]==0
537 do_execsql_test 23.0 {
538 CREATE TABLE t1(x, y);
539 INSERT INTO t1 VALUES(1, 2);
546 forcecopy test.db tst/test.db
547 file attributes tst -permissions r-xr-xr-x
550 sqlite3 db tst/test.db
551 do_execsql_test 23.2 {
555 do_catchsql_test 23.3 {
556 INSERT INTO t1 VALUES(3, 4);
557 } {1 {attempt to write a readonly database}}
560 sqlite3_extended_errcode db
561 } {SQLITE_READONLY_DIRECTORY}