xref: /sqlite-3.40.0/test/ioerr.test (revision 4abd5449)
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.35 2008/05/05 15:26:51 danielk1977 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 -erc 1 -ckrefcount 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] ? 4 : 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 -ckrefcount 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]?9:-1]]
78}
79
80do_ioerr_test ioerr-3 -ckrefcount true -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 -ckrefcount true -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
120ifcapable attach {
121  set ex ""
122  if {[string match [execsql {pragma auto_vacuum}] 1]} {
123    set ex [list 4 17]
124  }
125  do_ioerr_test ioerr-5 -ckrefcount true -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
135# Test IO errors when replaying two hot journals from a 2-file
136# transaction. This test only runs on UNIX.
137ifcapable crashtest&&attach {
138  if {![catch {sqlite3 -has_codec} r] && !$r} {
139    do_ioerr_test ioerr-6 -ckrefcount true -tclprep {
140      execsql {
141        ATTACH 'test2.db' as aux;
142        CREATE TABLE tx(a, b);
143        CREATE TABLE aux.ty(a, b);
144      }
145      set rc [crashsql -delay 2 -file test2.db-journal {
146        ATTACH 'test2.db' as aux;
147        PRAGMA cache_size = 10;
148        BEGIN;
149        CREATE TABLE aux.t2(a, b, c);
150        CREATE TABLE t1(a, b, c);
151        COMMIT;
152      }]
153      if {$rc!="1 {child process exited abnormally}"} {
154        error "Wrong error message: $rc"
155      }
156    } -sqlbody {
157      SELECT * FROM sqlite_master;
158      SELECT * FROM aux.sqlite_master;
159    }
160  }
161}
162
163# Test handling of IO errors that occur while rolling back hot journal
164# files.
165#
166# These tests can't be run on windows because the windows version of
167# SQLite holds a mandatory exclusive lock on journal files it has open.
168#
169if {$tcl_platform(platform)!="windows"} {
170  do_ioerr_test ioerr-7 -tclprep {
171    db close
172    sqlite3 db2 test2.db
173    db2 eval {
174      PRAGMA synchronous = 0;
175      CREATE TABLE t1(a, b);
176      INSERT INTO t1 VALUES(1, 2);
177      BEGIN;
178      INSERT INTO t1 VALUES(3, 4);
179    }
180    copy_file test2.db test.db
181    copy_file test2.db-journal test.db-journal
182    db2 close
183  } -tclbody {
184    sqlite3 db test.db
185    db eval {
186      SELECT * FROM t1;
187    }
188  } -exclude 1
189}
190
191# For test coverage:  Cause an I/O failure while trying to read a
192# short field (one that fits into a Mem buffer without mallocing
193# for space).
194#
195do_ioerr_test ioerr-8 -ckrefcount true -tclprep {
196  execsql {
197    CREATE TABLE t1(a,b,c);
198    INSERT INTO t1 VALUES(randstr(200,200), randstr(1000,1000), 2);
199  }
200  db close
201  sqlite3 db test.db
202} -sqlbody {
203  SELECT c FROM t1;
204}
205
206# For test coverage: Cause an IO error whilst reading the master-journal
207# name from a journal file.
208if {$tcl_platform(platform)=="unix"} {
209  do_ioerr_test ioerr-9 -ckrefcount true -tclprep {
210    execsql {
211      CREATE TABLE t1(a,b,c);
212      INSERT INTO t1 VALUES(randstr(200,200), randstr(1000,1000), 2);
213      BEGIN;
214      INSERT INTO t1 VALUES(randstr(200,200), randstr(1000,1000), 2);
215    }
216    copy_file test.db-journal test2.db-journal
217    execsql {
218      COMMIT;
219    }
220    copy_file test2.db-journal test.db-journal
221    set f [open test.db-journal a]
222    fconfigure $f -encoding binary
223    puts -nonewline $f "hello"
224    puts -nonewline $f "\x00\x00\x00\x05\x01\x02\x03\x04"
225    puts -nonewline $f "\xd9\xd5\x05\xf9\x20\xa1\x63\xd7"
226    close $f
227  } -sqlbody {
228    SELECT a FROM t1;
229  }
230}
231
232# For test coverage: Cause an IO error during statement playback (i.e.
233# a constraint).
234do_ioerr_test ioerr-10 -ckrefcount true -tclprep {
235  execsql {
236    BEGIN;
237    CREATE TABLE t1(a PRIMARY KEY, b);
238  }
239  for {set i 0} {$i < 500} {incr i} {
240    execsql {INSERT INTO t1 VALUES(:i, 'hello world');}
241  }
242  execsql {
243    COMMIT;
244  }
245} -tclbody {
246
247  catch {execsql {
248    BEGIN;
249    INSERT INTO t1 VALUES('abc', 123);
250    INSERT INTO t1 VALUES('def', 123);
251    INSERT INTO t1 VALUES('ghi', 123);
252    INSERT INTO t1 SELECT (a+500)%900, 'good string' FROM t1;
253  }} msg
254
255  if {$msg != "column a is not unique"} {
256    error $msg
257  }
258}
259
260# Assertion fault bug reported by alex dimitrov.
261#
262do_ioerr_test ioerr-11 -ckrefcount true -erc 1 -sqlprep {
263   CREATE TABLE A(Id INTEGER, Name TEXT);
264   INSERT INTO A(Id, Name) VALUES(1, 'Name');
265} -sqlbody {
266   UPDATE A SET Id = 2, Name = 'Name2' WHERE Id = 1;
267}
268
269# Test that an io error encountered in a sync() caused by a call to
270# sqlite3_release_memory() is handled Ok. Only try this if
271# memory-management is enabled.
272#
273ifcapable memorymanage {
274  do_ioerr_test memmanage-ioerr1 -ckrefcount true -sqlprep {
275    BEGIN;
276    CREATE TABLE t1(a, b, c);
277    INSERT INTO t1 VALUES(randstr(50,50), randstr(100,100), randstr(10,10));
278    INSERT INTO t1 SELECT randstr(50,50), randstr(9,9), randstr(90,90) FROM t1;
279    INSERT INTO t1 SELECT randstr(50,50), randstr(9,9), randstr(90,90) FROM t1;
280    INSERT INTO t1 SELECT randstr(50,50), randstr(9,9), randstr(90,90) FROM t1;
281    INSERT INTO t1 SELECT randstr(50,50), randstr(9,9), randstr(90,90) FROM t1;
282    INSERT INTO t1 SELECT randstr(50,50), randstr(9,9), randstr(90,90) FROM t1;
283  } -tclbody {
284    sqlite3_release_memory
285  } -sqlbody {
286    COMMIT;
287  }
288}
289
290finish_test
291