xref: /sqlite-3.40.0/test/ioerr.test (revision c023e03e)
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.3 2003/04/25 15:37:59 drh Exp $
19
20set testdir [file dirname $argv0]
21source $testdir/tester.tcl
22
23set ::go 1
24for {set n 1} {$go} {incr n} {
25  do_test ioerr-1.$n.1 {
26    set ::sqlite_io_error_pending 0
27    db close
28    catch {file delete -force test.db}
29    catch {file delete -force test.db-journal}
30    sqlite db test.db
31    execsql {SELECT * FROM sqlite_master}
32  } {}
33  do_test ioerr-1.$n.2 [subst {
34    set ::sqlite_io_error_pending $n
35  }] $n
36  do_test ioerr-1.$n.3 {
37    set r [catch {db eval {
38      CREATE TABLE t1(a,b,c);
39      SELECT * FROM sqlite_master;
40      BEGIN TRANSACTION;
41      INSERT INTO t1 VALUES(1,2,3);
42      INSERT INTO t1 VALUES(4,5,6);
43      ROLLBACK;
44      SELECT * FROM t1;
45      BEGIN TRANSACTION;
46      INSERT INTO t1 VALUES(1,2,3);
47      INSERT INTO t1 VALUES(4,5,6);
48      COMMIT;
49      SELECT * FROM t1;
50      DELETE FROM t1 WHERE a<100;
51    }} msg]
52    # if {$r} {puts $msg}
53    set ::go [expr {$::sqlite_io_error_pending<=0}]
54    expr {$::sqlite_io_error_pending>0 || $r!=0}
55  } {1}
56}
57set ::sqlite_io_error_pending 0
58
59proc cksum {{db db}} {
60  set txt [$db eval {SELECT name, type, sql FROM sqlite_master}]\n
61  foreach tbl [$db eval {SELECT name FROM sqlite_master WHERE type='table'}] {
62    append txt [$db eval "SELECT * FROM $tbl"]\n
63  }
64  foreach prag {default_synchronous default_cache_size} {
65    append txt $prag-[$db eval "PRAGMA $prag"]\n
66  }
67  set cksum [string length $txt]-[md5 $txt]
68  # puts $cksum-[file size test.db]
69  return $cksum
70}
71
72set ::go 1
73for {set n 1} {$go} {incr n} {
74  do_test ioerr-2.$n.1 {
75    set ::sqlite_io_error_pending 0
76    db close
77    catch {file delete -force test.db}
78    catch {file delete -force test.db-journal}
79    sqlite db test.db
80    execsql {
81      BEGIN;
82      CREATE TABLE t1(a, b, c);
83      INSERT INTO t1 VALUES(1, randstr(5,50), randstr(5,50));
84      INSERT INTO t1 SELECT a+2, b||'-'||rowid, c||'-'||rowid FROM t1;
85      INSERT INTO t1 SELECT a+4, b||'-'||rowid, c||'-'||rowid FROM t1;
86      INSERT INTO t1 SELECT a+8, b||'-'||rowid, c||'-'||rowid FROM t1;
87      INSERT INTO t1 SELECT a+16, b||'-'||rowid, c||'-'||rowid FROM t1;
88      INSERT INTO t1 SELECT a+32, b||'-'||rowid, c||'-'||rowid FROM t1;
89      INSERT INTO t1 SELECT a+64, b||'-'||rowid, c||'-'||rowid FROM t1;
90      INSERT INTO t1 SELECT a+128, b||'-'||rowid, c||'-'||rowid FROM t1;
91      CREATE TABLE t2 AS SELECT * FROM t1;
92      CREATE TABLE t3 AS SELECT * FROM t1;
93      COMMIT;
94      DROP TABLE t2;
95    }
96    set ::cksum [cksum]
97    execsql {
98      SELECT name FROM sqlite_master WHERE type='table'
99    }
100  } {t1 t3}
101  do_test ioerr-2.$n.2 [subst {
102    set ::sqlite_io_error_pending $n
103  }] $n
104  do_test ioerr-2.$n.3 {
105    set r [catch {db eval {
106      VACUUM;
107    }} msg]
108    # puts "error_pending=$::sqlite_io_error_pending"
109    # if {$r} {puts $msg}
110    set ::go [expr {$::sqlite_io_error_pending<=0}]
111    expr {$::sqlite_io_error_pending>0 || $r!=0}
112    set ::sqlite_io_error_pending 0
113    db close
114    sqlite db test.db
115    cksum
116  } $cksum
117}
118set ::sqlite_io_error_pending 0
119
120finish_test
121