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