sl@0: # 2001 October 12 sl@0: # sl@0: # Portions Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiaries. All rights reserved. sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # This file implements regression tests for SQLite library. The sl@0: # focus of this file is testing for correct handling of I/O errors sl@0: # such as writes failing because the disk is full. sl@0: # sl@0: # The tests in this file use special facilities that are only sl@0: # available in the SQLite test fixture. sl@0: # sl@0: # $Id: ioerr.test,v 1.41 2008/07/12 14:52:20 drh Exp $ sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: # If SQLITE_DEFAULT_AUTOVACUUM is set to true, then a simulated IO error sl@0: # on the 8th IO operation in the SQL script below doesn't report an error. sl@0: # sl@0: # This is because the 8th IO call attempts to read page 2 of the database sl@0: # file when the file on disk is only 1 page. The pager layer detects that sl@0: # this has happened and suppresses the error returned by the OS layer. sl@0: # sl@0: do_ioerr_test ioerr-1 -erc 1 -ckrefcount 1 -sqlprep { sl@0: SELECT * FROM sqlite_master; sl@0: } -sqlbody { sl@0: CREATE TABLE t1(a,b,c); sl@0: SELECT * FROM sqlite_master; sl@0: BEGIN TRANSACTION; sl@0: INSERT INTO t1 VALUES(1,2,3); sl@0: INSERT INTO t1 VALUES(4,5,6); sl@0: ROLLBACK; sl@0: SELECT * FROM t1; sl@0: BEGIN TRANSACTION; sl@0: INSERT INTO t1 VALUES(1,2,3); sl@0: INSERT INTO t1 VALUES(4,5,6); sl@0: COMMIT; sl@0: SELECT * FROM t1; sl@0: DELETE FROM t1 WHERE a<100; sl@0: } -exclude [expr [string match [execsql {pragma auto_vacuum}] 1] ? 4 : 0] sl@0: sl@0: # Test for IO errors during a VACUUM. sl@0: # sl@0: # The first IO call is excluded from the test. This call attempts to read sl@0: # the file-header of the temporary database used by VACUUM. Since the sl@0: # database doesn't exist at that point, the IO error is not detected. sl@0: # sl@0: # Additionally, if auto-vacuum is enabled, the 12th IO error is not sl@0: # detected. Same reason as the 8th in the test case above. sl@0: # sl@0: ifcapable vacuum { sl@0: do_ioerr_test ioerr-2 -cksum true -ckrefcount true -sqlprep { sl@0: BEGIN; sl@0: CREATE TABLE t1(a, b, c); sl@0: INSERT INTO t1 VALUES(1, randstr(50,50), randstr(50,50)); sl@0: INSERT INTO t1 SELECT a+2, b||'-'||rowid, c||'-'||rowid FROM t1; sl@0: INSERT INTO t1 SELECT a+4, b||'-'||rowid, c||'-'||rowid FROM t1; sl@0: INSERT INTO t1 SELECT a+8, b||'-'||rowid, c||'-'||rowid FROM t1; sl@0: INSERT INTO t1 SELECT a+16, b||'-'||rowid, c||'-'||rowid FROM t1; sl@0: INSERT INTO t1 SELECT a+32, b||'-'||rowid, c||'-'||rowid FROM t1; sl@0: INSERT INTO t1 SELECT a+64, b||'-'||rowid, c||'-'||rowid FROM t1; sl@0: INSERT INTO t1 SELECT a+128, b||'-'||rowid, c||'-'||rowid FROM t1; sl@0: INSERT INTO t1 VALUES(1, randstr(600,600), randstr(600,600)); sl@0: CREATE TABLE t2 AS SELECT * FROM t1; sl@0: CREATE TABLE t3 AS SELECT * FROM t1; sl@0: COMMIT; sl@0: DROP TABLE t2; sl@0: } -sqlbody { sl@0: VACUUM; sl@0: } -exclude [list \ sl@0: 1 [expr [string match [execsql {pragma auto_vacuum}] 1]?9:-1]] sl@0: } sl@0: sl@0: do_ioerr_test ioerr-3 -ckrefcount true -tclprep { sl@0: execsql { sl@0: PRAGMA cache_size = 10; sl@0: BEGIN; sl@0: CREATE TABLE abc(a); sl@0: INSERT INTO abc VALUES(randstr(1500,1500)); -- Page 4 is overflow sl@0: } sl@0: for {set i 0} {$i<150} {incr i} { sl@0: execsql { sl@0: INSERT INTO abc VALUES(randstr(100,100)); sl@0: } sl@0: } sl@0: execsql COMMIT sl@0: } -sqlbody { sl@0: CREATE TABLE abc2(a); sl@0: BEGIN; sl@0: DELETE FROM abc WHERE length(a)>100; sl@0: UPDATE abc SET a = randstr(90,90); sl@0: COMMIT; sl@0: CREATE TABLE abc3(a); sl@0: } sl@0: sl@0: # Test IO errors that can occur retrieving a record header that flows over sl@0: # onto an overflow page. sl@0: do_ioerr_test ioerr-4 -ckrefcount true -tclprep { sl@0: set sql "CREATE TABLE abc(a1" sl@0: for {set i 2} {$i<1300} {incr i} { sl@0: append sql ", a$i" sl@0: } sl@0: append sql ");" sl@0: execsql $sql sl@0: execsql {INSERT INTO abc (a1) VALUES(NULL)} sl@0: } -sqlbody { sl@0: SELECT * FROM abc; sl@0: } sl@0: sl@0: sl@0: # Test IO errors that may occur during a multi-file commit. sl@0: # sl@0: # Tests 8 and 17 are excluded when auto-vacuum is enabled for the same sl@0: # reason as in test cases ioerr-1.XXX sl@0: ifcapable attach { sl@0: set ex "" sl@0: if {[string match [execsql {pragma auto_vacuum}] 1]} { sl@0: set ex [list 4 17] sl@0: } sl@0: do_ioerr_test ioerr-5 -restoreprng 0 -ckrefcount true -sqlprep { sl@0: ATTACH 'test2.db' AS test2; sl@0: } -sqlbody { sl@0: BEGIN; sl@0: CREATE TABLE t1(a,b,c); sl@0: CREATE TABLE test2.t2(a,b,c); sl@0: COMMIT; sl@0: } -exclude $ex sl@0: } sl@0: sl@0: # Test IO errors when replaying two hot journals from a 2-file sl@0: # transaction. This test only runs on UNIX. sl@0: ifcapable crashtest&&attach { sl@0: if {![catch {sqlite3 -has_codec} r] && !$r} { sl@0: do_ioerr_test ioerr-6 -ckrefcount true -tclprep { sl@0: execsql { sl@0: ATTACH 'test2.db' as aux; sl@0: CREATE TABLE tx(a, b); sl@0: CREATE TABLE aux.ty(a, b); sl@0: } sl@0: set rc [crashsql -delay 2 -file test2.db-journal { sl@0: ATTACH 'test2.db' as aux; sl@0: PRAGMA cache_size = 10; sl@0: BEGIN; sl@0: CREATE TABLE aux.t2(a, b, c); sl@0: CREATE TABLE t1(a, b, c); sl@0: COMMIT; sl@0: }] sl@0: if {$rc!="1 {child process exited abnormally}"} { sl@0: error "Wrong error message: $rc" sl@0: } sl@0: } -sqlbody { sl@0: SELECT * FROM sqlite_master; sl@0: SELECT * FROM aux.sqlite_master; sl@0: } sl@0: } sl@0: } sl@0: sl@0: # Test handling of IO errors that occur while rolling back hot journal sl@0: # files. sl@0: # sl@0: # These tests can't be run on windows because the windows version of sl@0: # SQLite holds a mandatory exclusive lock on journal files it has open. sl@0: # sl@0: if {$tcl_platform(platform)!="windows" && $tcl_platform(platform)!="symbian"} { sl@0: do_ioerr_test ioerr-7 -tclprep { sl@0: db close sl@0: sqlite3 db2 test2.db sl@0: db2 eval { sl@0: PRAGMA synchronous = 0; sl@0: CREATE TABLE t1(a, b); sl@0: INSERT INTO t1 VALUES(1, 2); sl@0: BEGIN; sl@0: INSERT INTO t1 VALUES(3, 4); sl@0: } sl@0: copy_file test2.db test.db sl@0: copy_file test2.db-journal test.db-journal sl@0: db2 close sl@0: } -tclbody { sl@0: sqlite3 db test.db sl@0: db eval { sl@0: SELECT * FROM t1; sl@0: } sl@0: } -exclude 1 sl@0: } sl@0: sl@0: # For test coverage: Cause an I/O failure while trying to read a sl@0: # short field (one that fits into a Mem buffer without mallocing sl@0: # for space). sl@0: # sl@0: do_ioerr_test ioerr-8 -ckrefcount true -tclprep { sl@0: execsql { sl@0: CREATE TABLE t1(a,b,c); sl@0: INSERT INTO t1 VALUES(randstr(200,200), randstr(1000,1000), 2); sl@0: } sl@0: db close sl@0: sqlite3 db test.db sl@0: } -sqlbody { sl@0: SELECT c FROM t1; sl@0: } sl@0: sl@0: # For test coverage: Cause an IO error whilst reading the master-journal sl@0: # name from a journal file. sl@0: if {$tcl_platform(platform)=="unix"} { sl@0: do_ioerr_test ioerr-9 -ckrefcount true -tclprep { sl@0: execsql { sl@0: CREATE TABLE t1(a,b,c); sl@0: INSERT INTO t1 VALUES(randstr(200,200), randstr(1000,1000), 2); sl@0: BEGIN; sl@0: INSERT INTO t1 VALUES(randstr(200,200), randstr(1000,1000), 2); sl@0: } sl@0: copy_file test.db-journal test2.db-journal sl@0: execsql { sl@0: COMMIT; sl@0: } sl@0: copy_file test2.db-journal test.db-journal sl@0: set f [open test.db-journal a] sl@0: fconfigure $f -encoding binary sl@0: puts -nonewline $f "hello" sl@0: puts -nonewline $f "\x00\x00\x00\x05\x01\x02\x03\x04" sl@0: puts -nonewline $f "\xd9\xd5\x05\xf9\x20\xa1\x63\xd7" sl@0: close $f sl@0: } -sqlbody { sl@0: SELECT a FROM t1; sl@0: } sl@0: } sl@0: sl@0: # For test coverage: Cause an IO error during statement playback (i.e. sl@0: # a constraint). sl@0: do_ioerr_test ioerr-10 -ckrefcount true -tclprep { sl@0: execsql { sl@0: BEGIN; sl@0: CREATE TABLE t1(a PRIMARY KEY, b); sl@0: } sl@0: for {set i 0} {$i < 500} {incr i} { sl@0: execsql {INSERT INTO t1 VALUES(:i, 'hello world');} sl@0: } sl@0: execsql { sl@0: COMMIT; sl@0: } sl@0: } -tclbody { sl@0: sl@0: catch {execsql { sl@0: BEGIN; sl@0: INSERT INTO t1 VALUES('abc', 123); sl@0: INSERT INTO t1 VALUES('def', 123); sl@0: INSERT INTO t1 VALUES('ghi', 123); sl@0: INSERT INTO t1 SELECT (a+500)%900, 'good string' FROM t1; sl@0: }} msg sl@0: sl@0: if {$msg != "column a is not unique"} { sl@0: error $msg sl@0: } sl@0: } sl@0: sl@0: # Assertion fault bug reported by alex dimitrov. sl@0: # sl@0: do_ioerr_test ioerr-11 -ckrefcount true -erc 1 -sqlprep { sl@0: CREATE TABLE A(Id INTEGER, Name TEXT); sl@0: INSERT INTO A(Id, Name) VALUES(1, 'Name'); sl@0: } -sqlbody { sl@0: UPDATE A SET Id = 2, Name = 'Name2' WHERE Id = 1; sl@0: } sl@0: sl@0: # Test that an io error encountered in a sync() caused by a call to sl@0: # sqlite3_release_memory() is handled Ok. Only try this if sl@0: # memory-management is enabled. sl@0: # sl@0: ifcapable memorymanage { sl@0: do_ioerr_test memmanage-ioerr1 -ckrefcount true -sqlprep { sl@0: BEGIN; sl@0: CREATE TABLE t1(a, b, c); sl@0: INSERT INTO t1 VALUES(randstr(50,50), randstr(100,100), randstr(10,10)); sl@0: INSERT INTO t1 SELECT randstr(50,50), randstr(9,9), randstr(90,90) FROM t1; sl@0: INSERT INTO t1 SELECT randstr(50,50), randstr(9,9), randstr(90,90) FROM t1; sl@0: INSERT INTO t1 SELECT randstr(50,50), randstr(9,9), randstr(90,90) FROM t1; sl@0: INSERT INTO t1 SELECT randstr(50,50), randstr(9,9), randstr(90,90) FROM t1; sl@0: INSERT INTO t1 SELECT randstr(50,50), randstr(9,9), randstr(90,90) FROM t1; sl@0: } -tclbody { sl@0: sqlite3_release_memory sl@0: } -sqlbody { sl@0: COMMIT; sl@0: } sl@0: } sl@0: sl@0: ifcapable pager_pragmas&&autovacuum { sl@0: do_ioerr_test ioerr-12 -ckrefcount true -erc 1 -sqlprep { sl@0: PRAGMA page_size = 512; sl@0: PRAGMA auto_vacuum = incremental; sl@0: CREATE TABLE t1(x); sl@0: INSERT INTO t1 VALUES( randomblob(1 * (512-4)) ); sl@0: INSERT INTO t1 VALUES( randomblob(110 * (512-4)) ); sl@0: INSERT INTO t1 VALUES( randomblob(2 * (512-4)) ); sl@0: INSERT INTO t1 VALUES( randomblob(110 * (512-4)) ); sl@0: INSERT INTO t1 VALUES( randomblob(3 * (512-4)) ); sl@0: DELETE FROM t1 WHERE rowid = 3; sl@0: PRAGMA incremental_vacuum = 2; sl@0: DELETE FROM t1 WHERE rowid = 1; sl@0: } -sqlbody { sl@0: PRAGMA incremental_vacuum = 1; sl@0: } sl@0: } sl@0: sl@0: # Usually, after a new page is allocated from the end of the file, it does sl@0: # not need to be written to the journal. The exception is when the new page sl@0: # shares its sector with an existing page that does need to be journalled. sl@0: # This test case provokes this condition to test for the sake of coverage sl@0: # that an IO error while journalling the coresident page is handled correctly. sl@0: # sl@0: sqlite3_simulate_device -char {} -sectorsize 2048 sl@0: do_ioerr_test ioerr-12 -ckrefcount true -erc 1 -tclprep { sl@0: db close sl@0: sqlite3 db test.db -vfs devsym sl@0: sl@0: # Create a test database. Page 2 is the root page of table t1. The only sl@0: # row inserted into t1 has an overflow page - page 3. Page 3 will be sl@0: # coresident on the 2048 byte sector with the next page to be allocated. sl@0: # sl@0: db eval { PRAGMA page_size = 1024 } sl@0: db eval { CREATE TABLE t1(x) } sl@0: db eval { INSERT INTO t1 VALUES(randomblob(1100)); } sl@0: } -tclbody { sl@0: db eval { INSERT INTO t1 VALUES(randomblob(2000)); } sl@0: } sl@0: sqlite3_simulate_device -char {} -sectorsize 0 sl@0: catch {db close} sl@0: sl@0: do_ioerr_test ioerr-13 -ckrefcount true -erc 1 -sqlprep { sl@0: PRAGMA auto_vacuum = incremental; sl@0: CREATE TABLE t1(x); sl@0: CREATE TABLE t2(x); sl@0: INSERT INTO t2 VALUES(randomblob(1500)); sl@0: INSERT INTO t2 SELECT randomblob(1500) FROM t2; sl@0: INSERT INTO t2 SELECT randomblob(1500) FROM t2; sl@0: INSERT INTO t2 SELECT randomblob(1500) FROM t2; sl@0: INSERT INTO t2 SELECT randomblob(1500) FROM t2; sl@0: INSERT INTO t2 SELECT randomblob(1500) FROM t2; sl@0: INSERT INTO t2 SELECT randomblob(1500) FROM t2; sl@0: INSERT INTO t2 SELECT randomblob(1500) FROM t2; sl@0: INSERT INTO t2 SELECT randomblob(1500) FROM t2; sl@0: INSERT INTO t1 VALUES(randomblob(20)); sl@0: INSERT INTO t1 SELECT x FROM t1; sl@0: INSERT INTO t1 SELECT x FROM t1; sl@0: INSERT INTO t1 SELECT x FROM t1; sl@0: INSERT INTO t1 SELECT x FROM t1; sl@0: INSERT INTO t1 SELECT x FROM t1; sl@0: INSERT INTO t1 SELECT x FROM t1; /* 64 entries in t1 */ sl@0: INSERT INTO t1 SELECT x FROM t1 LIMIT 14; /* 78 entries in t1 */ sl@0: DELETE FROM t2 WHERE rowid = 3; sl@0: } -sqlbody { sl@0: -- This statement uses the balance_quick() optimization. The new page sl@0: -- is appended to the database file. But the overflow page used by sl@0: -- the new record will be positioned near the start of the database sl@0: -- file, in the gap left by the "DELETE FROM t2 WHERE rowid=3" statement sl@0: -- above. sl@0: -- sl@0: -- The point of this is that the statement wil need to update two pointer sl@0: -- map pages. Which introduces another opportunity for an IO error. sl@0: -- sl@0: INSERT INTO t1 VALUES(randomblob(2000)); sl@0: } sl@0: sl@0: do_ioerr_test ioerr-14 -ckrefcount true -erc 1 -sqlprep { sl@0: PRAGMA auto_vacuum = incremental; sl@0: CREATE TABLE t1(x); sl@0: CREATE TABLE t2(x); sl@0: INSERT INTO t2 VALUES(randomblob(1500)); sl@0: INSERT INTO t2 SELECT randomblob(1500) FROM t2; sl@0: INSERT INTO t2 SELECT randomblob(1500) FROM t2; sl@0: INSERT INTO t2 SELECT randomblob(1500) FROM t2; sl@0: INSERT INTO t2 SELECT randomblob(1500) FROM t2; sl@0: INSERT INTO t2 SELECT randomblob(1500) FROM t2; sl@0: INSERT INTO t2 SELECT randomblob(1500) FROM t2; sl@0: INSERT INTO t2 SELECT randomblob(1500) FROM t2; sl@0: INSERT INTO t2 SELECT randomblob(1500) FROM t2; sl@0: sl@0: -- This statement inserts a row into t1 with an overflow page at the sl@0: -- end of the file. A long way from its parent (the root of t1). sl@0: INSERT INTO t1 VALUES(randomblob(1500)); sl@0: DELETE FROM t2 WHERE rowid<10; sl@0: } -sqlbody { sl@0: -- This transaction will cause the root-page of table t1 to divide sl@0: -- (by calling balance_deeper()). When it does, the "parent" page of the sl@0: -- overflow page inserted in the -sqlprep block above will change and sl@0: -- the corresponding pointer map page be updated. This test case attempts sl@0: -- to cause an IO error during the pointer map page update. sl@0: -- sl@0: BEGIN; sl@0: INSERT INTO t1 VALUES(randomblob(100)); sl@0: INSERT INTO t1 VALUES(randomblob(100)); sl@0: INSERT INTO t1 VALUES(randomblob(100)); sl@0: INSERT INTO t1 VALUES(randomblob(100)); sl@0: INSERT INTO t1 VALUES(randomblob(100)); sl@0: INSERT INTO t1 VALUES(randomblob(100)); sl@0: INSERT INTO t1 VALUES(randomblob(100)); sl@0: INSERT INTO t1 VALUES(randomblob(100)); sl@0: INSERT INTO t1 VALUES(randomblob(100)); sl@0: INSERT INTO t1 VALUES(randomblob(100)); sl@0: COMMIT; sl@0: } sl@0: sl@0: finish_test