xref: /sqlite-3.40.0/test/ioerr.test (revision f2fa8310)
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.25 2006/01/24 13:09:33 danielk1977 Exp $
19
20set testdir [file dirname $argv0]
21source $testdir/tester.tcl
22
23
24# If SQLITE_DEFAULT_AUTOVACUUM is set to true, then a simulated IO error
25# on the 8th IO operation in the SQL script below doesn't report an error.
26#
27# This is because the 8th IO call attempts to read page 2 of the database
28# file when the file on disk is only 1 page. The pager layer detects that
29# this has happened and suppresses the error returned by the OS layer.
30#
31do_ioerr_test ioerr-1 -sqlprep {
32  SELECT * FROM sqlite_master;
33} -sqlbody {
34  CREATE TABLE t1(a,b,c);
35  SELECT * FROM sqlite_master;
36  BEGIN TRANSACTION;
37  INSERT INTO t1 VALUES(1,2,3);
38  INSERT INTO t1 VALUES(4,5,6);
39  ROLLBACK;
40  SELECT * FROM t1;
41  BEGIN TRANSACTION;
42  INSERT INTO t1 VALUES(1,2,3);
43  INSERT INTO t1 VALUES(4,5,6);
44  COMMIT;
45  SELECT * FROM t1;
46  DELETE FROM t1 WHERE a<100;
47} -exclude [expr [string match [execsql {pragma auto_vacuum}] 1] ? 4 : 0]
48
49# Test for IO errors during a VACUUM.
50#
51# The first IO call is excluded from the test. This call attempts to read
52# the file-header of the temporary database used by VACUUM. Since the
53# database doesn't exist at that point, the IO error is not detected.
54#
55# Additionally, if auto-vacuum is enabled, the 12th IO error is not
56# detected. Same reason as the 8th in the test case above.
57#
58ifcapable vacuum {
59  do_ioerr_test ioerr-2 -cksum true -sqlprep {
60    BEGIN;
61    CREATE TABLE t1(a, b, c);
62    INSERT INTO t1 VALUES(1, randstr(50,50), randstr(50,50));
63    INSERT INTO t1 SELECT a+2, b||'-'||rowid, c||'-'||rowid FROM t1;
64    INSERT INTO t1 SELECT a+4, b||'-'||rowid, c||'-'||rowid FROM t1;
65    INSERT INTO t1 SELECT a+8, b||'-'||rowid, c||'-'||rowid FROM t1;
66    INSERT INTO t1 SELECT a+16, b||'-'||rowid, c||'-'||rowid FROM t1;
67    INSERT INTO t1 SELECT a+32, b||'-'||rowid, c||'-'||rowid FROM t1;
68    INSERT INTO t1 SELECT a+64, b||'-'||rowid, c||'-'||rowid FROM t1;
69    INSERT INTO t1 SELECT a+128, b||'-'||rowid, c||'-'||rowid FROM t1;
70    INSERT INTO t1 VALUES(1, randstr(600,600), randstr(600,600));
71    CREATE TABLE t2 AS SELECT * FROM t1;
72    CREATE TABLE t3 AS SELECT * FROM t1;
73    COMMIT;
74    DROP TABLE t2;
75  } -sqlbody {
76    VACUUM;
77  } -exclude [list \
78      1 [expr [string match [execsql {pragma auto_vacuum}] 1]?9:-1]]
79}
80
81do_ioerr_test ioerr-3 -tclprep {
82  execsql {
83    PRAGMA cache_size = 10;
84    BEGIN;
85    CREATE TABLE abc(a);
86    INSERT INTO abc VALUES(randstr(1500,1500)); -- Page 4 is overflow
87  }
88  for {set i 0} {$i<150} {incr i} {
89    execsql {
90      INSERT INTO abc VALUES(randstr(100,100));
91    }
92  }
93  execsql COMMIT
94} -sqlbody {
95  CREATE TABLE abc2(a);
96  BEGIN;
97  DELETE FROM abc WHERE length(a)>100;
98  UPDATE abc SET a = randstr(90,90);
99  COMMIT;
100  CREATE TABLE abc3(a);
101}
102
103# Test IO errors that can occur retrieving a record header that flows over
104# onto an overflow page.
105do_ioerr_test ioerr-4 -tclprep {
106  set sql "CREATE TABLE abc(a1"
107  for {set i 2} {$i<1300} {incr i} {
108    append sql ", a$i"
109  }
110  append sql ");"
111  execsql $sql
112  execsql {INSERT INTO abc (a1) VALUES(NULL)}
113} -sqlbody {
114 SELECT * FROM abc;
115}
116
117# Test IO errors that may occur during a multi-file commit.
118#
119# Tests 8 and 17 are excluded when auto-vacuum is enabled for the same
120# reason as in test cases ioerr-1.XXX
121set ex ""
122if {[string match [execsql {pragma auto_vacuum}] 1]} {
123  set ex [list 4 17]
124}
125do_ioerr_test ioerr-5 -sqlprep {
126  ATTACH 'test2.db' AS test2;
127} -sqlbody {
128  BEGIN;
129  CREATE TABLE t1(a,b,c);
130  CREATE TABLE test2.t2(a,b,c);
131  COMMIT;
132} -exclude $ex
133
134# Test IO errors when replaying two hot journals from a 2-file
135# transaction. This test only runs on UNIX.
136ifcapable crashtest {
137  if {![catch {sqlite3 -has_codec} r] && !$r} {
138    do_ioerr_test ioerr-6 -tclprep {
139      execsql {
140        ATTACH 'test2.db' as aux;
141        CREATE TABLE tx(a, b);
142        CREATE TABLE aux.ty(a, b);
143      }
144      set rc [crashsql 2 test2.db-journal {
145        ATTACH 'test2.db' as aux;
146        PRAGMA cache_size = 10;
147        BEGIN;
148        CREATE TABLE aux.t2(a, b, c);
149        CREATE TABLE t1(a, b, c);
150        COMMIT;
151      }]
152      if {$rc!="1 {child process exited abnormally}"} {
153        error "Wrong error message: $rc"
154      }
155    } -sqlbody {
156      SELECT * FROM sqlite_master;
157      SELECT * FROM aux.sqlite_master;
158    }
159  }
160}
161
162# Test handling of IO errors that occur while rolling back hot journal
163# files.
164#
165# These tests can't be run on windows because the windows version of
166# SQLite holds a mandatory exclusive lock on journal files it has open.
167#
168if {$tcl_platform(platform)!="windows"} {
169  do_ioerr_test ioerr-7 -tclprep {
170    db close
171    sqlite3 db2 test2.db
172    db2 eval {
173      PRAGMA synchronous = 0;
174      CREATE TABLE t1(a, b);
175      INSERT INTO t1 VALUES(1, 2);
176      BEGIN;
177      INSERT INTO t1 VALUES(3, 4);
178    }
179    copy_file test2.db test.db
180    copy_file test2.db-journal test.db-journal
181    db2 close
182  } -tclbody {
183    sqlite3 db test.db
184    db eval {
185      SELECT * FROM t1;
186    }
187  } -exclude 1
188}
189
190# For test coverage:  Cause an I/O failure while trying to read a
191# short field (one that fits into a Mem buffer without mallocing
192# for space).
193#
194do_ioerr_test ioerr-8 -tclprep {
195  execsql {
196    CREATE TABLE t1(a,b,c);
197    INSERT INTO t1 VALUES(randstr(200,200), randstr(1000,1000), 2);
198  }
199  db close
200  sqlite3 db test.db
201} -sqlbody {
202  SELECT c FROM t1;
203}
204
205# For test coverage: Cause an IO error whilst reading the master-journal
206# name from a journal file.
207if {$tcl_platform(platform)=="unix"} {
208  do_ioerr_test ioerr-9 -tclprep {
209    execsql {
210      CREATE TABLE t1(a,b,c);
211      INSERT INTO t1 VALUES(randstr(200,200), randstr(1000,1000), 2);
212      BEGIN;
213      INSERT INTO t1 VALUES(randstr(200,200), randstr(1000,1000), 2);
214    }
215    copy_file test.db-journal test2.db-journal
216    execsql {
217      COMMIT;
218    }
219    copy_file test2.db-journal test.db-journal
220    set f [open test.db-journal a]
221    fconfigure $f -encoding binary
222    puts -nonewline $f "hello"
223    puts -nonewline $f "\x00\x00\x00\x05\x01\x02\x03\x04"
224    puts -nonewline $f "\xd9\xd5\x05\xf9\x20\xa1\x63\xd7"
225    close $f
226  } -sqlbody {
227    SELECT a FROM t1;
228  }
229}
230
231# For test coverage: Cause an IO error during statement playback (i.e.
232# a constraint).
233do_ioerr_test ioerr-10 -tclprep {
234  execsql {
235    BEGIN;
236    CREATE TABLE t1(a PRIMARY KEY, b);
237  }
238  for {set i 0} {$i < 500} {incr i} {
239    execsql {INSERT INTO t1 VALUES($i, 'hello world');}
240  }
241  execsql {
242    COMMIT;
243  }
244} -tclbody {
245
246  catch {execsql {
247    BEGIN;
248    INSERT INTO t1 VALUES('abc', 123);
249    INSERT INTO t1 VALUES('def', 123);
250    INSERT INTO t1 VALUES('ghi', 123);
251    INSERT INTO t1 SELECT (a+500)%900, 'good string' FROM t1;
252  }} msg
253
254  if {$msg != "column a is not unique"} {
255    error $msg
256  }
257}
258
259finish_test
260