3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
13 set testdir [file dirname $argv0]
14 source $testdir/tester.tcl
15 source $testdir/lock_common.tcl
16 source $testdir/malloc_common.tcl
18 if {[llength [info commands test_syscall]]==0} {
23 set testprefix sysfault
25 set FAULTSIM(vfsfault-transient) [list \
26 -injectinstall vfsfault_install \
27 -injectstart vfsfault_injectstart_t \
28 -injectstop vfsfault_injectstop \
30 -injectuninstall {test_syscall uninstall} \
32 set FAULTSIM(vfsfault-persistent) [list \
33 -injectinstall vfsfault_install \
34 -injectstart vfsfault_injectstart_p \
35 -injectstop vfsfault_injectstop \
37 -injectuninstall {test_syscall uninstall} \
40 proc vfsfault_injectstart_t {iFail} { test_syscall fault $iFail 0 }
41 proc vfsfault_injectstart_p {iFail} { test_syscall fault $iFail 1 }
42 proc vfsfault_injectstop {} { test_syscall fault }
44 faultsim_save_and_close
47 set open_and_write_body {
50 CREATE TABLE t1(a, b);
51 INSERT INTO t1 VALUES(1, 2);
52 PRAGMA journal_mode = WAL;
53 INSERT INTO t1 VALUES(3, 4);
55 CREATE TEMP TABLE t2(x);
56 INSERT INTO t2 VALUES('y');
60 proc vfsfault_install {} { test_syscall install {open getcwd} }
61 do_faultsim_test 1 -faults vfsfault-* -prep {
63 } -body $open_and_write_body -test {
64 faultsim_test_result {0 {wal 1 2 3 4}} \
65 {1 {unable to open database file}} \
66 {1 {attempt to write a readonly database}}
69 #-------------------------------------------------------------------------
70 # Errors in the fstat() function when opening and writing a file. Cases
71 # where fstat() fails and sets errno to ENOMEM and EOVERFLOW are both
72 # tested. EOVERFLOW is interpreted as meaning that a file on disk is
73 # too large to be opened by the OS.
75 foreach {tn errno errlist} {
76 1 ENOMEM {{disk I/O error}}
77 2 EOVERFLOW {{disk I/O error} {large file support is disabled}}
79 proc vfsfault_install {} { test_syscall install fstat }
81 foreach e $errlist { lappend errs [list 1 $e] }
82 do_faultsim_test 1.2.$tn -faults vfsfault-* -prep {
85 test_syscall errno fstat $errno
88 faultsim_test_result {0 {wal 1 2 3 4}} $errs
92 #-------------------------------------------------------------------------
93 # Various errors in locking functions.
95 foreach vfs {unix unix-excl} {
96 foreach {tn errno errlist} {
97 1 EAGAIN {{database is locked} {disk I/O error}}
98 2 ETIMEDOUT {{database is locked} {disk I/O error}}
99 3 EBUSY {{database is locked} {disk I/O error}}
100 4 EINTR {{database is locked} {disk I/O error}}
101 5 ENOLCK {{database is locked} {disk I/O error}}
102 6 EACCES {{database is locked} {disk I/O error}}
103 7 EPERM {{access permission denied} {disk I/O error}}
104 8 EDEADLK {{disk I/O error}}
105 9 ENOMEM {{disk I/O error}}
107 proc vfsfault_install {} { test_syscall install fcntl }
109 foreach e $errlist { lappend errs [list 1 $e] }
111 set body [string map [list %VFS% $vfs] {
114 CREATE TABLE t1(a, b);
115 INSERT INTO t1 VALUES(1, 2);
117 set fd [open test.db-journal w]
118 puts $fd "hello world"
120 sqlite3 db test.db -vfs %VFS%
126 do_faultsim_test 1.3.$vfs.$tn -faults vfsfault-* -prep {
129 test_syscall errno fcntl $errno
132 faultsim_test_result {0 {1 2}} $errs
137 #-------------------------------------------------------------------------
138 # Check that a single EINTR error does not affect processing.
140 proc vfsfault_install {} {
142 test_syscall install {open ftruncate close read pread pread64 write fallocate}
145 forcedelete test.db test.db2
149 CREATE TABLE t1(a, b, c, PRIMARY KEY(a));
150 INSERT INTO t1 VALUES('abc', 'def', 'ghi');
151 ATTACH 'test.db2' AS 'aux';
152 CREATE TABLE aux.t2(x);
153 INSERT INTO t2 VALUES(1);
155 faultsim_save_and_close
158 do_faultsim_test 2.1 -faults vfsfault-transient -prep {
162 test_syscall errno open EINTR
163 test_syscall errno ftruncate EINTR
164 test_syscall errno close EINTR
165 test_syscall errno read EINTR
166 test_syscall errno pread EINTR
167 test_syscall errno pread64 EINTR
168 test_syscall errno write EINTR
169 test_syscall errno fallocate EINTR
172 file_control_chunksize_test db main 8192
175 ATTACH 'test.db2' AS 'aux';
177 PRAGMA journal_mode = truncate;
179 INSERT INTO t1 VALUES('jkl', 'mno', 'pqr');
180 INSERT INTO t1 VALUES(randomblob(10000), 0, 0);
183 DELETE FROM t1 WHERE length(a)>3;
190 faultsim_test_result {0 {abc def ghi truncate abc def ghi jkl mno pqr 2}}
193 do_faultsim_test 2.2 -faults vfsfault-* -prep {
199 ATTACH 'test.db2' AS 'aux';
201 PRAGMA journal_mode = truncate;
203 INSERT INTO t1 VALUES('jkl', 'mno', 'pqr');
212 faultsim_test_result {0 {abc def ghi truncate abc def ghi jkl mno pqr 2}} \
213 {1 {unable to open database file}} \
214 {1 {unable to open database: test.db2}} \
215 {1 {attempt to write a readonly database}} \
219 #-------------------------------------------------------------------------
221 proc vfsfault_install {} {
223 test_syscall install {fstat fallocate}
225 do_faultsim_test 3 -faults vfsfault-* -prep {
226 faultsim_delete_and_reopen
227 file_control_chunksize_test db main 8192
229 PRAGMA synchronous=OFF;
230 CREATE TABLE t1(a, b);
235 test_syscall errno fstat EIO
236 test_syscall errno fallocate EIO
239 INSERT INTO t1 VALUES(randomblob(10000), randomblob(10000));
240 SELECT length(a) + length(b) FROM t1;
244 faultsim_test_result {0 20000}
247 #-------------------------------------------------------------------------
248 # Test errors in mmap().
250 proc vfsfault_install {} {
252 test_syscall install {mmap}
255 faultsim_delete_and_reopen
257 CREATE TABLE t1(a, b);
258 INSERT INTO t1 VALUES(1, 2);
260 faultsim_save_and_close
262 do_faultsim_test 4 -faults vfsfault-* -prep {
263 faultsim_restore_and_reopen
264 file_control_chunksize_test db main 8192
266 PRAGMA mmap_size = 1000000;
269 test_syscall errno mmap EACCES
275 faultsim_test_result {0 {1 2}} {1 {disk I/O error}}