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