1*1da57d55SToomas Soome# 2c5c4113dSnw141292# 2001 October 12 3c5c4113dSnw141292# 4c5c4113dSnw141292# The author disclaims copyright to this source code. In place of 5c5c4113dSnw141292# a legal notice, here is a blessing: 6c5c4113dSnw141292# 7c5c4113dSnw141292# May you do good and not evil. 8c5c4113dSnw141292# May you find forgiveness for yourself and forgive others. 9c5c4113dSnw141292# May you share freely, never taking more than you give. 10c5c4113dSnw141292# 11c5c4113dSnw141292#*********************************************************************** 12c5c4113dSnw141292# This file implements regression tests for SQLite library. The 13c5c4113dSnw141292# focus of this file is testing for correct handling of I/O errors 14c5c4113dSnw141292# such as writes failing because the disk is full. 15c5c4113dSnw141292# 16c5c4113dSnw141292# The tests in this file use special facilities that are only 17c5c4113dSnw141292# available in the SQLite test fixture. 18c5c4113dSnw141292# 19c5c4113dSnw141292# $Id: ioerr.test,v 1.3 2003/04/25 15:37:59 drh Exp $ 20c5c4113dSnw141292 21c5c4113dSnw141292set testdir [file dirname $argv0] 22c5c4113dSnw141292source $testdir/tester.tcl 23c5c4113dSnw141292 24c5c4113dSnw141292set ::go 1 25c5c4113dSnw141292for {set n 1} {$go} {incr n} { 26c5c4113dSnw141292 do_test ioerr-1.$n.1 { 27c5c4113dSnw141292 set ::sqlite_io_error_pending 0 28c5c4113dSnw141292 db close 29c5c4113dSnw141292 catch {file delete -force test.db} 30c5c4113dSnw141292 catch {file delete -force test.db-journal} 31c5c4113dSnw141292 sqlite db test.db 32c5c4113dSnw141292 execsql {SELECT * FROM sqlite_master} 33c5c4113dSnw141292 } {} 34c5c4113dSnw141292 do_test ioerr-1.$n.2 [subst { 35c5c4113dSnw141292 set ::sqlite_io_error_pending $n 36c5c4113dSnw141292 }] $n 37c5c4113dSnw141292 do_test ioerr-1.$n.3 { 38c5c4113dSnw141292 set r [catch {db eval { 39c5c4113dSnw141292 CREATE TABLE t1(a,b,c); 40c5c4113dSnw141292 SELECT * FROM sqlite_master; 41c5c4113dSnw141292 BEGIN TRANSACTION; 42c5c4113dSnw141292 INSERT INTO t1 VALUES(1,2,3); 43c5c4113dSnw141292 INSERT INTO t1 VALUES(4,5,6); 44c5c4113dSnw141292 ROLLBACK; 45c5c4113dSnw141292 SELECT * FROM t1; 46c5c4113dSnw141292 BEGIN TRANSACTION; 47c5c4113dSnw141292 INSERT INTO t1 VALUES(1,2,3); 48c5c4113dSnw141292 INSERT INTO t1 VALUES(4,5,6); 49c5c4113dSnw141292 COMMIT; 50c5c4113dSnw141292 SELECT * FROM t1; 51c5c4113dSnw141292 DELETE FROM t1 WHERE a<100; 52c5c4113dSnw141292 }} msg] 53c5c4113dSnw141292 # if {$r} {puts $msg} 54c5c4113dSnw141292 set ::go [expr {$::sqlite_io_error_pending<=0}] 55c5c4113dSnw141292 expr {$::sqlite_io_error_pending>0 || $r!=0} 56c5c4113dSnw141292 } {1} 57c5c4113dSnw141292} 58c5c4113dSnw141292set ::sqlite_io_error_pending 0 59c5c4113dSnw141292 60c5c4113dSnw141292proc cksum {{db db}} { 61c5c4113dSnw141292 set txt [$db eval {SELECT name, type, sql FROM sqlite_master}]\n 62c5c4113dSnw141292 foreach tbl [$db eval {SELECT name FROM sqlite_master WHERE type='table'}] { 63c5c4113dSnw141292 append txt [$db eval "SELECT * FROM $tbl"]\n 64c5c4113dSnw141292 } 65c5c4113dSnw141292 foreach prag {default_synchronous default_cache_size} { 66c5c4113dSnw141292 append txt $prag-[$db eval "PRAGMA $prag"]\n 67c5c4113dSnw141292 } 68c5c4113dSnw141292 set cksum [string length $txt]-[md5 $txt] 69c5c4113dSnw141292 # puts $cksum-[file size test.db] 70c5c4113dSnw141292 return $cksum 71c5c4113dSnw141292} 72c5c4113dSnw141292 73c5c4113dSnw141292set ::go 1 74c5c4113dSnw141292for {set n 1} {$go} {incr n} { 75c5c4113dSnw141292 do_test ioerr-2.$n.1 { 76c5c4113dSnw141292 set ::sqlite_io_error_pending 0 77c5c4113dSnw141292 db close 78c5c4113dSnw141292 catch {file delete -force test.db} 79c5c4113dSnw141292 catch {file delete -force test.db-journal} 80c5c4113dSnw141292 sqlite db test.db 81c5c4113dSnw141292 execsql { 82c5c4113dSnw141292 BEGIN; 83c5c4113dSnw141292 CREATE TABLE t1(a, b, c); 84c5c4113dSnw141292 INSERT INTO t1 VALUES(1, randstr(5,50), randstr(5,50)); 85c5c4113dSnw141292 INSERT INTO t1 SELECT a+2, b||'-'||rowid, c||'-'||rowid FROM t1; 86c5c4113dSnw141292 INSERT INTO t1 SELECT a+4, b||'-'||rowid, c||'-'||rowid FROM t1; 87c5c4113dSnw141292 INSERT INTO t1 SELECT a+8, b||'-'||rowid, c||'-'||rowid FROM t1; 88c5c4113dSnw141292 INSERT INTO t1 SELECT a+16, b||'-'||rowid, c||'-'||rowid FROM t1; 89c5c4113dSnw141292 INSERT INTO t1 SELECT a+32, b||'-'||rowid, c||'-'||rowid FROM t1; 90c5c4113dSnw141292 INSERT INTO t1 SELECT a+64, b||'-'||rowid, c||'-'||rowid FROM t1; 91c5c4113dSnw141292 INSERT INTO t1 SELECT a+128, b||'-'||rowid, c||'-'||rowid FROM t1; 92c5c4113dSnw141292 CREATE TABLE t2 AS SELECT * FROM t1; 93c5c4113dSnw141292 CREATE TABLE t3 AS SELECT * FROM t1; 94c5c4113dSnw141292 COMMIT; 95c5c4113dSnw141292 DROP TABLE t2; 96c5c4113dSnw141292 } 97c5c4113dSnw141292 set ::cksum [cksum] 98c5c4113dSnw141292 execsql { 99c5c4113dSnw141292 SELECT name FROM sqlite_master WHERE type='table' 100c5c4113dSnw141292 } 101c5c4113dSnw141292 } {t1 t3} 102c5c4113dSnw141292 do_test ioerr-2.$n.2 [subst { 103c5c4113dSnw141292 set ::sqlite_io_error_pending $n 104c5c4113dSnw141292 }] $n 105c5c4113dSnw141292 do_test ioerr-2.$n.3 { 106c5c4113dSnw141292 set r [catch {db eval { 107c5c4113dSnw141292 VACUUM; 108c5c4113dSnw141292 }} msg] 109c5c4113dSnw141292 # puts "error_pending=$::sqlite_io_error_pending" 110c5c4113dSnw141292 # if {$r} {puts $msg} 111c5c4113dSnw141292 set ::go [expr {$::sqlite_io_error_pending<=0}] 112c5c4113dSnw141292 expr {$::sqlite_io_error_pending>0 || $r!=0} 113c5c4113dSnw141292 set ::sqlite_io_error_pending 0 114c5c4113dSnw141292 db close 115c5c4113dSnw141292 sqlite db test.db 116c5c4113dSnw141292 cksum 117c5c4113dSnw141292 } $cksum 118c5c4113dSnw141292} 119c5c4113dSnw141292set ::sqlite_io_error_pending 0 120c5c4113dSnw141292 121c5c4113dSnw141292finish_test 122