xref: /sqlite-3.40.0/test/ioerr.test (revision 4dcbdbff)
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.20 2005/03/10 12:58:22 drh Exp $
19
20set testdir [file dirname $argv0]
21source $testdir/tester.tcl
22
23# If SQLITE_DEFAULT_AUTOVACUUM is set to true, then a simulated IO error
24# on the 8th IO operation in the SQL script below doesn't report an error.
25#
26# This is because the 8th IO call attempts to read page 2 of the database
27# file when the file on disk is only 1 page. The pager layer detects that
28# this has happened and suppresses the error returned by the OS layer.
29#
30do_ioerr_test ioerr-1 -sqlprep {
31  SELECT * FROM sqlite_master;
32} -sqlbody {
33  CREATE TABLE t1(a,b,c);
34  SELECT * FROM sqlite_master;
35  BEGIN TRANSACTION;
36  INSERT INTO t1 VALUES(1,2,3);
37  INSERT INTO t1 VALUES(4,5,6);
38  ROLLBACK;
39  SELECT * FROM t1;
40  BEGIN TRANSACTION;
41  INSERT INTO t1 VALUES(1,2,3);
42  INSERT INTO t1 VALUES(4,5,6);
43  COMMIT;
44  SELECT * FROM t1;
45  DELETE FROM t1 WHERE a<100;
46} -exclude [expr [string match [execsql {pragma auto_vacuum}] 1] ? 8 : 0]
47
48# Test for IO errors during a VACUUM.
49#
50# The first IO call is excluded from the test. This call attempts to read
51# the file-header of the temporary database used by VACUUM. Since the
52# database doesn't exist at that point, the IO error is not detected.
53#
54# Additionally, if auto-vacuum is enabled, the 12th IO error is not
55# detected. Same reason as the 8th in the test case above.
56#
57ifcapable vacuum {
58  do_ioerr_test ioerr-2 -cksum true -sqlprep {
59    BEGIN;
60    CREATE TABLE t1(a, b, c);
61    INSERT INTO t1 VALUES(1, randstr(50,50), randstr(50,50));
62    INSERT INTO t1 SELECT a+2, b||'-'||rowid, c||'-'||rowid FROM t1;
63    INSERT INTO t1 SELECT a+4, b||'-'||rowid, c||'-'||rowid FROM t1;
64    INSERT INTO t1 SELECT a+8, b||'-'||rowid, c||'-'||rowid FROM t1;
65    INSERT INTO t1 SELECT a+16, b||'-'||rowid, c||'-'||rowid FROM t1;
66    INSERT INTO t1 SELECT a+32, b||'-'||rowid, c||'-'||rowid FROM t1;
67    INSERT INTO t1 SELECT a+64, b||'-'||rowid, c||'-'||rowid FROM t1;
68    INSERT INTO t1 SELECT a+128, b||'-'||rowid, c||'-'||rowid FROM t1;
69    INSERT INTO t1 VALUES(1, randstr(600,600), randstr(600,600));
70    CREATE TABLE t2 AS SELECT * FROM t1;
71    CREATE TABLE t3 AS SELECT * FROM t1;
72    COMMIT;
73    DROP TABLE t2;
74  } -sqlbody {
75    VACUUM;
76  } -exclude [list \
77      1 [expr [string match [execsql {pragma auto_vacuum}] 1]?12:-1]]
78}
79
80do_ioerr_test ioerr-3 -tclprep {
81  execsql {
82    PRAGMA cache_size = 10;
83    BEGIN;
84    CREATE TABLE abc(a);
85    INSERT INTO abc VALUES(randstr(1500,1500)); -- Page 4 is overflow
86  }
87  for {set i 0} {$i<150} {incr i} {
88    execsql {
89      INSERT INTO abc VALUES(randstr(100,100));
90    }
91  }
92  execsql COMMIT
93} -sqlbody {
94  CREATE TABLE abc2(a);
95  BEGIN;
96  DELETE FROM abc WHERE length(a)>100;
97  UPDATE abc SET a = randstr(90,90);
98  COMMIT;
99  CREATE TABLE abc3(a);
100}
101
102# Test IO errors that can occur retrieving a record header that flows over
103# onto an overflow page.
104do_ioerr_test ioerr-4 -tclprep {
105  set sql "CREATE TABLE abc(a1"
106  for {set i 2} {$i<1300} {incr i} {
107    append sql ", a$i"
108  }
109  append sql ");"
110  execsql $sql
111  execsql {INSERT INTO abc (a1) VALUES(NULL)}
112} -sqlbody {
113 SELECT * FROM abc;
114}
115
116# Test IO errors that may occur during a multi-file commit.
117#
118# Tests 8 and 17 are excluded when auto-vacuum is enabled for the same
119# reason as in test cases ioerr-1.XXX
120set ex ""
121if {[string match [execsql {pragma auto_vacuum}] 1]} {
122  set ex [list 8 17]
123}
124do_ioerr_test ioerr-5 -sqlprep {
125  ATTACH 'test2.db' AS test2;
126} -sqlbody {
127  BEGIN;
128  CREATE TABLE t1(a,b,c);
129  CREATE TABLE test2.t2(a,b,c);
130  COMMIT;
131} -exclude $ex
132
133# Test IO errors when replaying two hot journals from a 2-file
134# transaction. This test only runs on UNIX.
135if {$tcl_platform(platform)=="unix" && [file exists ./crashtest]
136     && ![catch {sqlite3 -has-codec} r] && !$r} {
137  do_ioerr_test ioerr-6 -tclprep {
138    execsql {
139      ATTACH 'test2.db' as aux;
140      CREATE TABLE tx(a, b);
141      CREATE TABLE aux.ty(a, b);
142    }
143    set rc [crashsql 2 test2.db-journal {
144      ATTACH 'test2.db' as aux;
145      PRAGMA cache_size = 10;
146      BEGIN;
147      CREATE TABLE aux.t2(a, b, c);
148      CREATE TABLE t1(a, b, c);
149      COMMIT;
150    }]
151    if {$rc!="1 {child process exited abnormally}"} {
152      error "Wrong error message: $rc"
153    }
154  } -sqlbody {
155    SELECT * FROM sqlite_master;
156    SELECT * FROM aux.sqlite_master;
157  }
158}
159
160# Test handling of IO errors that occur while rolling back hot journal
161# files.
162#
163# These tests can't be run on windows because the windows version of
164# SQLite holds a mandatory exclusive lock on journal files it has open.
165#
166if {$tcl_platform(platform)!="windows"} {
167  do_ioerr_test ioerr-7 -tclprep {
168    db close
169    sqlite3 db2 test2.db
170    db2 eval {
171      PRAGMA synchronous = 0;
172      CREATE TABLE t1(a, b);
173      INSERT INTO t1 VALUES(1, 2);
174      BEGIN;
175      INSERT INTO t1 VALUES(3, 4);
176    }
177    copy_file test2.db test.db
178    copy_file test2.db-journal test.db-journal
179    db2 close
180  } -tclbody {
181    sqlite3 db test.db
182    db eval {
183      SELECT * FROM t1;
184    }
185  } -exclude 1
186}
187
188finish_test
189