1# 2001 October 12 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. The 12# focus of this file is testing for correct handling of I/O errors 13# such as writes failing because the disk is full. 14# 15# The tests in this file use special facilities that are only 16# available in the SQLite test fixture. 17# 18# $Id: ioerr.test,v 1.29 2007/01/04 14:58:14 drh Exp $ 19 20set testdir [file dirname $argv0] 21source $testdir/tester.tcl 22 23 24# If SQLITE_DEFAULT_AUTOVACUUM is set to true, then a simulated IO error 25# on the 8th IO operation in the SQL script below doesn't report an error. 26# 27# This is because the 8th IO call attempts to read page 2 of the database 28# file when the file on disk is only 1 page. The pager layer detects that 29# this has happened and suppresses the error returned by the OS layer. 30# 31do_ioerr_test ioerr-1 -erc 1 -sqlprep { 32 SELECT * FROM sqlite_master; 33} -sqlbody { 34 CREATE TABLE t1(a,b,c); 35 SELECT * FROM sqlite_master; 36 BEGIN TRANSACTION; 37 INSERT INTO t1 VALUES(1,2,3); 38 INSERT INTO t1 VALUES(4,5,6); 39 ROLLBACK; 40 SELECT * FROM t1; 41 BEGIN TRANSACTION; 42 INSERT INTO t1 VALUES(1,2,3); 43 INSERT INTO t1 VALUES(4,5,6); 44 COMMIT; 45 SELECT * FROM t1; 46 DELETE FROM t1 WHERE a<100; 47} -exclude [expr [string match [execsql {pragma auto_vacuum}] 1] ? 4 : 0] 48 49finish_test 50return 51 52# Test for IO errors during a VACUUM. 53# 54# The first IO call is excluded from the test. This call attempts to read 55# the file-header of the temporary database used by VACUUM. Since the 56# database doesn't exist at that point, the IO error is not detected. 57# 58# Additionally, if auto-vacuum is enabled, the 12th IO error is not 59# detected. Same reason as the 8th in the test case above. 60# 61ifcapable vacuum { 62 do_ioerr_test ioerr-2 -cksum true -sqlprep { 63 BEGIN; 64 CREATE TABLE t1(a, b, c); 65 INSERT INTO t1 VALUES(1, randstr(50,50), randstr(50,50)); 66 INSERT INTO t1 SELECT a+2, b||'-'||rowid, c||'-'||rowid FROM t1; 67 INSERT INTO t1 SELECT a+4, b||'-'||rowid, c||'-'||rowid FROM t1; 68 INSERT INTO t1 SELECT a+8, b||'-'||rowid, c||'-'||rowid FROM t1; 69 INSERT INTO t1 SELECT a+16, b||'-'||rowid, c||'-'||rowid FROM t1; 70 INSERT INTO t1 SELECT a+32, b||'-'||rowid, c||'-'||rowid FROM t1; 71 INSERT INTO t1 SELECT a+64, b||'-'||rowid, c||'-'||rowid FROM t1; 72 INSERT INTO t1 SELECT a+128, b||'-'||rowid, c||'-'||rowid FROM t1; 73 INSERT INTO t1 VALUES(1, randstr(600,600), randstr(600,600)); 74 CREATE TABLE t2 AS SELECT * FROM t1; 75 CREATE TABLE t3 AS SELECT * FROM t1; 76 COMMIT; 77 DROP TABLE t2; 78 } -sqlbody { 79 VACUUM; 80 } -exclude [list \ 81 1 [expr [string match [execsql {pragma auto_vacuum}] 1]?9:-1]] 82} 83 84do_ioerr_test ioerr-3 -tclprep { 85 execsql { 86 PRAGMA cache_size = 10; 87 BEGIN; 88 CREATE TABLE abc(a); 89 INSERT INTO abc VALUES(randstr(1500,1500)); -- Page 4 is overflow 90 } 91 for {set i 0} {$i<150} {incr i} { 92 execsql { 93 INSERT INTO abc VALUES(randstr(100,100)); 94 } 95 } 96 execsql COMMIT 97} -sqlbody { 98 CREATE TABLE abc2(a); 99 BEGIN; 100 DELETE FROM abc WHERE length(a)>100; 101 UPDATE abc SET a = randstr(90,90); 102 COMMIT; 103 CREATE TABLE abc3(a); 104} 105 106# Test IO errors that can occur retrieving a record header that flows over 107# onto an overflow page. 108do_ioerr_test ioerr-4 -tclprep { 109 set sql "CREATE TABLE abc(a1" 110 for {set i 2} {$i<1300} {incr i} { 111 append sql ", a$i" 112 } 113 append sql ");" 114 execsql $sql 115 execsql {INSERT INTO abc (a1) VALUES(NULL)} 116} -sqlbody { 117 SELECT * FROM abc; 118} 119 120# Test IO errors that may occur during a multi-file commit. 121# 122# Tests 8 and 17 are excluded when auto-vacuum is enabled for the same 123# reason as in test cases ioerr-1.XXX 124set ex "" 125if {[string match [execsql {pragma auto_vacuum}] 1]} { 126 set ex [list 4 17] 127} 128do_ioerr_test ioerr-5 -sqlprep { 129 ATTACH 'test2.db' AS test2; 130} -sqlbody { 131 BEGIN; 132 CREATE TABLE t1(a,b,c); 133 CREATE TABLE test2.t2(a,b,c); 134 COMMIT; 135} -exclude $ex 136 137# Test IO errors when replaying two hot journals from a 2-file 138# transaction. This test only runs on UNIX. 139ifcapable crashtest { 140 if {![catch {sqlite3 -has_codec} r] && !$r} { 141 do_ioerr_test ioerr-6 -tclprep { 142 execsql { 143 ATTACH 'test2.db' as aux; 144 CREATE TABLE tx(a, b); 145 CREATE TABLE aux.ty(a, b); 146 } 147 set rc [crashsql 2 test2.db-journal { 148 ATTACH 'test2.db' as aux; 149 PRAGMA cache_size = 10; 150 BEGIN; 151 CREATE TABLE aux.t2(a, b, c); 152 CREATE TABLE t1(a, b, c); 153 COMMIT; 154 }] 155 if {$rc!="1 {child process exited abnormally}"} { 156 error "Wrong error message: $rc" 157 } 158 } -sqlbody { 159 SELECT * FROM sqlite_master; 160 SELECT * FROM aux.sqlite_master; 161 } 162 } 163} 164 165# Test handling of IO errors that occur while rolling back hot journal 166# files. 167# 168# These tests can't be run on windows because the windows version of 169# SQLite holds a mandatory exclusive lock on journal files it has open. 170# 171btree_breakpoint 172if {$tcl_platform(platform)!="windows"} { 173 do_ioerr_test ioerr-7 -tclprep { 174 db close 175 sqlite3 db2 test2.db 176 db2 eval { 177 PRAGMA synchronous = 0; 178 CREATE TABLE t1(a, b); 179 INSERT INTO t1 VALUES(1, 2); 180 BEGIN; 181 INSERT INTO t1 VALUES(3, 4); 182 } 183 copy_file test2.db test.db 184 copy_file test2.db-journal test.db-journal 185 db2 close 186 } -tclbody { 187 sqlite3 db test.db 188 db eval { 189 SELECT * FROM t1; 190 } 191 } -exclude 1 192} 193 194# For test coverage: Cause an I/O failure while trying to read a 195# short field (one that fits into a Mem buffer without mallocing 196# for space). 197# 198do_ioerr_test ioerr-8 -tclprep { 199 execsql { 200 CREATE TABLE t1(a,b,c); 201 INSERT INTO t1 VALUES(randstr(200,200), randstr(1000,1000), 2); 202 } 203 db close 204 sqlite3 db test.db 205} -sqlbody { 206 SELECT c FROM t1; 207} 208 209# For test coverage: Cause an IO error whilst reading the master-journal 210# name from a journal file. 211if {$tcl_platform(platform)=="unix"} { 212 do_ioerr_test ioerr-9 -tclprep { 213 execsql { 214 CREATE TABLE t1(a,b,c); 215 INSERT INTO t1 VALUES(randstr(200,200), randstr(1000,1000), 2); 216 BEGIN; 217 INSERT INTO t1 VALUES(randstr(200,200), randstr(1000,1000), 2); 218 } 219 copy_file test.db-journal test2.db-journal 220 execsql { 221 COMMIT; 222 } 223 copy_file test2.db-journal test.db-journal 224 set f [open test.db-journal a] 225 fconfigure $f -encoding binary 226 puts -nonewline $f "hello" 227 puts -nonewline $f "\x00\x00\x00\x05\x01\x02\x03\x04" 228 puts -nonewline $f "\xd9\xd5\x05\xf9\x20\xa1\x63\xd7" 229 close $f 230 } -sqlbody { 231 SELECT a FROM t1; 232 } 233} 234 235# For test coverage: Cause an IO error during statement playback (i.e. 236# a constraint). 237do_ioerr_test ioerr-10 -tclprep { 238 execsql { 239 BEGIN; 240 CREATE TABLE t1(a PRIMARY KEY, b); 241 } 242 for {set i 0} {$i < 500} {incr i} { 243 execsql {INSERT INTO t1 VALUES(:i, 'hello world');} 244 } 245 execsql { 246 COMMIT; 247 } 248} -tclbody { 249 250 catch {execsql { 251 BEGIN; 252 INSERT INTO t1 VALUES('abc', 123); 253 INSERT INTO t1 VALUES('def', 123); 254 INSERT INTO t1 VALUES('ghi', 123); 255 INSERT INTO t1 SELECT (a+500)%900, 'good string' FROM t1; 256 }} msg 257 258 if {$msg != "column a is not unique"} { 259 error $msg 260 } 261} 262 263finish_test 264