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
18 if {[clang_sanitize_address]==0} {
19 do_test misc7-1-misuse {
32 # Try to open a directory:
41 } {1 {unable to open database file}}
43 # Try to open a file with a directory where its journal file should be.
47 file mkdir mydir-journal
50 CREATE TABLE abc(a, b, c);
52 } {1 {unable to open database file}}
55 #--------------------------------------------------------------------
56 # The following tests, misc7-6.* test the libraries behaviour when
57 # it cannot open a file. To force this condition, we use up all the
58 # file-descriptors before running sqlite. This probably only works
62 proc use_up_files {} {
65 while 1 { lappend ret [open test.db] }
70 proc do_fileopen_test {prefix sql} {
71 set fd_list [use_up_files]
77 do_test ${prefix}.${::n} {
82 if {$rc == 0} {set ::go 0}
84 expr {$rc == 0 || ($rc == 1 && [string first unable $msg]==0)}
87 close [lindex $fd_list 0]
88 set fd_list [lrange $fd_list 1 end]
97 execsql { CREATE TABLE abc(a PRIMARY KEY, b, c); }
100 if {$tcl_platform(platform)!="windows"} {
101 do_fileopen_test misc7-6.1 {
103 INSERT INTO abc VALUES(1, 2, 3);
104 INSERT INTO abc VALUES(2, 3, 4);
105 INSERT INTO abc SELECT a+2, b, c FROM abc;
109 do_fileopen_test misc7-6.2 {
110 PRAGMA temp.cache_size = 1000;
115 # End of tests for out-of-file-descriptors condition.
116 #--------------------------------------------------------------------
121 INSERT INTO abc VALUES(1, 2, 3);
122 INSERT INTO abc VALUES(2, 3, 4);
123 INSERT INTO abc SELECT a+2, b, c FROM abc;
127 #--------------------------------------------------------------------
128 # Test that the sqlite3_busy_timeout call seems to delay approximately
129 # the right amount of time.
133 sqlite3_busy_timeout [sqlite3_connection_pointer db] 2000
138 # Now db2 has an exclusive lock on the database file, and db has
139 # a busy-timeout of 2000 milliseconds. So check that trying to
140 # access the database using connection db delays for at least 1500 ms.
143 set result [catchsql {
144 SELECT * FROM sqlite_master;
147 set delay [lindex $tm 0] ;# In microseconds
148 lappend result [expr {$delay>1500000 && $delay<4000000}]
149 } {1 {database is locked} 1}
152 #--------------------------------------------------------------------
153 # Test that nothing goes horribly wrong when attaching a database
154 # after the omit_readlock pragma has been exercised.
156 # Note: The PRAGMA omit_readlock was an early hack to disable the
157 # fcntl() calls for read-only databases so that read-only databases could
158 # be read on broken NFS systems. That pragma has now been removed.
159 # (Use the unix-none VFS as a replacement, if needed.) But these tests
160 # do not really depend on omit_readlock, so we left them in place.
164 forcedelete test2.db-journal
166 PRAGMA omit_readlock = 1;
167 ATTACH 'test2.db' AS aux;
168 CREATE TABLE aux.hello(world);
169 SELECT name FROM aux.sqlite_master;
179 sqlite3 db test.db -readonly 1
181 PRAGMA omit_readlock = 1;
182 ATTACH 'test2.db' AS aux;
183 SELECT name FROM aux.sqlite_master;
184 SELECT name FROM aux.sqlite_master;
190 set ::DB [sqlite3_connection_pointer db]
194 # Test the UTF-16 version of the "out of memory" message (used when
195 # malloc fails during sqlite3_open() ).
199 encoding convertfrom unicode [sqlite3_errmsg16 0x00000000]
206 FROM (SELECT name+1 AS one FROM sqlite_master LIMIT 1 OFFSET 1)
207 WHERE one LIKE 'hello%';
211 #--------------------------------------------------------------------
212 # Improve coverage for vtab code.
215 # Run some debug code to improve reported coverage
218 # set sqlite_where_trace 1
220 register_echo_module [sqlite3_connection_pointer db]
222 CREATE VIRTUAL TABLE t1 USING echo(abc);
223 SELECT a FROM t1 WHERE a = 1 ORDER BY b;
226 set sqlite_where_trace 0
228 # Specify an ORDER BY clause that cannot be indexed.
231 SELECT t1.a, t2.a FROM t1, t1 AS t2 ORDER BY 2 LIMIT 1;
235 # The whole point of this is to test an error code other than
236 # SQLITE_NOMEM from the vtab xBestIndex callback.
238 do_ioerr_test misc7-12 -tclprep {
240 register_echo_module [sqlite3_connection_pointer db2]
242 CREATE TABLE abc(a PRIMARY KEY, b, c);
243 INSERT INTO abc VALUES(1, 2, 3);
244 CREATE VIRTUAL TABLE t1 USING echo(abc);
248 register_echo_module [sqlite3_connection_pointer db]
249 execsql {SELECT * FROM t1 WHERE a = 1;}
252 # The case where the virtual table module returns a very large number
253 # as the cost of a scan (greater than SQLITE_BIG_DOUBLE in the code).
257 register_echo_module [sqlite3_connection_pointer db]
258 set ::echo_module_cost 2.0e+99
259 execsql {SELECT * FROM t1 WHERE a = 1;}
261 unset ::echo_module_cost
266 forcedelete test.db-journal
270 do_execsql_test misc7-14.1 {
271 CREATE TABLE abc(a PRIMARY KEY, b, c);
272 EXPLAIN QUERY PLAN SELECT * FROM abc AS t2 WHERE rowid = 1;
274 0 0 0 {SEARCH TABLE abc AS t2 USING INTEGER PRIMARY KEY (rowid=?)}
276 do_execsql_test misc7-14.2 {
277 EXPLAIN QUERY PLAN SELECT * FROM abc AS t2 WHERE a = 1;
279 {SEARCH TABLE abc AS t2 USING INDEX sqlite_autoindex_abc_1 (a=?)}
281 do_execsql_test misc7-14.3 {
282 EXPLAIN QUERY PLAN SELECT * FROM abc AS t2 ORDER BY a;
284 {SCAN TABLE abc AS t2 USING INDEX sqlite_autoindex_abc_1}
290 forcedelete test.db-journal
293 #--------------------------------------------------------------------
294 # This is all to force the pager_remove_from_stmt_list() function
295 # (inside pager.c) to remove a pager from the middle of the
300 PRAGMA cache_size = 10;
302 CREATE TABLE abc(a PRIMARY KEY, b, c);
304 VALUES(randstr(100,100), randstr(100,100), randstr(100,100));
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 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;
323 expr {[file size test.db]>10240}
327 DELETE FROM abc WHERE rowid > 12;
328 INSERT INTO abc SELECT
329 randstr(100,100), randstr(100,100), randstr(100,100) FROM abc;
335 forcedelete test.db-journal
338 do_ioerr_test misc7-16 -sqlprep {
339 PRAGMA cache_size = 10;
340 PRAGMA default_cache_size = 10;
341 CREATE TABLE t3(a, b, UNIQUE(a, b));
342 INSERT INTO t3 VALUES( randstr(100, 100), randstr(100, 100) );
343 INSERT INTO t3 SELECT randstr(100, 100), randstr(100, 100) FROM t3;
344 INSERT INTO t3 SELECT randstr(100, 100), randstr(100, 100) FROM t3;
345 INSERT INTO t3 SELECT randstr(100, 100), randstr(100, 100) FROM t3;
346 INSERT INTO t3 SELECT randstr(100, 100), randstr(100, 100) FROM t3;
347 INSERT INTO t3 SELECT randstr(100, 100), randstr(100, 100) FROM t3;
349 SET b = 'hello world'
350 WHERE rowid >= (SELECT max(rowid)-1 FROM t3);
352 set rc [catch {db eval {
354 PRAGMA cache_size = 10;
355 INSERT INTO t3 VALUES( randstr(100, 100), randstr(100, 100) );
360 if {!$rc || ($rc && [string first "UNIQUE" $msg]==0)} {
371 SELECT count(*) FROM t3;
375 #----------------------------------------------------------------------
376 # Test the situation where a hot-journal is discovered but write-access
377 # to it is denied. This should return SQLITE_BUSY.
379 # These tests do not work on windows due to restrictions in the
380 # windows file system.
382 if {$tcl_platform(platform)!="windows"} {
384 # Some network filesystems (ex: AFP) do not support setting read-only
385 # permissions. Only run these tests if full unix permission setting
386 # capabilities are supported.
388 file attributes test.db -permissions rw-r--r--
389 if {[file attributes test.db -permissions]==0644} {
394 DELETE FROM t3 WHERE (oid%3)==0;
396 forcecopy test.db bak.db
397 forcecopy test.db-journal bak.db-journal
403 forcecopy bak.db test.db
404 forcecopy bak.db-journal test.db-journal
407 catch {file attributes test.db-journal -permissions r--------}
408 catch {file attributes test.db-journal -readonly 1}
410 SELECT count(*) FROM t3;
412 } {1 {unable to open database file}}
414 # Note that the -readonly flag must be cleared before the -permissions
415 # are set. Otherwise, when using tcl 8.5 on mac, the fact that the
416 # -readonly flag is set causes the attempt to set the permissions
418 catch {file attributes test.db-journal -readonly 0}
419 catch {file attributes test.db-journal -permissions rw-------}
421 SELECT count(*) FROM t3;
425 # sqlite3_test_control_pending_page [expr ($::sqlite_pending_byte / 1024) + 1]
426 set ::pending_byte_page [expr ($::sqlite_pending_byte / 1024) + 1]
427 sqlite3_test_control_pending_byte $::sqlite_pending_byte
430 pragma writable_schema = true;
432 SET rootpage = $pending_byte_page
433 WHERE type = 'table' AND name = 't3';
436 SELECT rootpage FROM sqlite_master WHERE type = 'table' AND name = 't3';
438 } $::pending_byte_page
444 SELECT count(*) FROM t3;
446 } {1 {database disk image is malformed}}
454 CREATE TABLE table_1 (col_10);
455 CREATE TABLE table_2 (
456 col_1, col_2, col_3, col_4, col_5,
457 col_6, col_7, col_8, col_9, col_10
461 (SELECT table_1.col_10 AS col_10 FROM table_1) a,
462 (SELECT table_1.col_10, table_2.col_9 AS qcol_9
463 FROM table_1, table_2
464 GROUP BY table_1.col_10, qcol_9);
468 # Testing boundary conditions on sqlite3_status()
474 sqlite3_status 1000 0
478 # sqlite3_global_recover() is a no-op. But we might as well test it
479 # if only to get the test coverage.
482 sqlite3_global_recover
485 # Try to open a really long file name.
488 set zFile [file join [get_pwd] "[string repeat abcde 104].db"]
489 set rc [catch {sqlite3 db2 $zFile} msg]
491 } {1 {unable to open database file}}
493 # Try to do hot-journal rollback with a read-only connection. The
494 # error code should be SQLITE_READONLY_ROLLBACK.
498 forcedelete test.db copy.db-journal
501 CREATE TABLE t1(a, b);
502 INSERT INTO t1 VALUES(1, 2);
503 INSERT INTO t1 VALUES(3, 4);
506 sqlite3 db test.db -readonly 1
508 INSERT INTO t1 VALUES(5, 6);
510 } {1 {attempt to write a readonly database}}
511 do_test misc7-22.2 { execsql { SELECT * FROM t1 } } {1 2 3 4}
513 set fd [open test.db-journal w]
514 puts $fd [string repeat abc 1000]
516 catchsql { SELECT * FROM t1 }
517 } {1 {attempt to write a readonly database}}
519 sqlite3_extended_errcode db
520 } SQLITE_READONLY_ROLLBACK