summaryrefslogtreecommitdiffstats
path: root/test/sysfault.test
blob: 4c3d34dbb95d7b1df6ee3dd000486e3274bcc84c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# 2011 March 28
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
source $testdir/lock_common.tcl
source $testdir/malloc_common.tcl

if {[llength [info commands test_syscall]]==0} {
  finish_test
  return
} 

set testprefix sysfault

set FAULTSIM(vfsfault-transient) [list             \
  -injectinstall   vfsfault_install                \
  -injectstart     vfsfault_injectstart_t          \
  -injectstop      vfsfault_injectstop             \
  -injecterrlist   {}                              \
  -injectuninstall {test_syscall uninstall}        \
]
set FAULTSIM(vfsfault-persistent) [list            \
  -injectinstall   vfsfault_install                \
  -injectstart     vfsfault_injectstart_p          \
  -injectstop      vfsfault_injectstop             \
  -injecterrlist   {}                              \
  -injectuninstall {test_syscall uninstall}        \
]

proc vfsfault_injectstart_t {iFail} { test_syscall fault $iFail 0 }
proc vfsfault_injectstart_p {iFail} { test_syscall fault $iFail 1 }
proc vfsfault_injectstop    {}      { test_syscall fault }

faultsim_save_and_close


set open_and_write_body {
  sqlite3 db test.db
  db eval {
    CREATE TABLE t1(a, b);
    INSERT INTO t1 VALUES(1, 2);
    PRAGMA journal_mode = WAL;
    INSERT INTO t1 VALUES(3, 4);
    SELECT * FROM t1;
    CREATE TEMP TABLE t2(x);
    INSERT INTO t2 VALUES('y');
  }
}

proc vfsfault_install {} { test_syscall install {open getcwd} }
do_faultsim_test 1 -faults vfsfault-* -prep {
  faultsim_restore
} -body $open_and_write_body -test {
  faultsim_test_result {0 {wal 1 2 3 4}}       \
    {1 {unable to open database file}}         \
    {1 {attempt to write a readonly database}}
}

#-------------------------------------------------------------------------
# Errors in the fstat() function when opening and writing a file. Cases
# where fstat() fails and sets errno to ENOMEM and EOVERFLOW are both
# tested. EOVERFLOW is interpreted as meaning that a file on disk is
# too large to be opened by the OS.
#
foreach {tn errno errlist} {
  1 ENOMEM       {{disk I/O error}}
  2 EOVERFLOW    {{disk I/O error} {large file support is disabled}}
} {
  proc vfsfault_install {} { test_syscall install fstat }
  set errs [list]
  foreach e $errlist { lappend errs [list 1 $e] }
  do_faultsim_test 1.2.$tn -faults vfsfault-* -prep {
    faultsim_restore
  } -body "
    test_syscall errno fstat $errno
    $open_and_write_body 
  " -test "
    faultsim_test_result {0 {wal 1 2 3 4}} $errs
  "
}

#-------------------------------------------------------------------------
# Various errors in locking functions. 
#
foreach vfs {unix unix-excl} {
  foreach {tn errno errlist} {
    1 EAGAIN       {{database is locked} {disk I/O error}}
    2 ETIMEDOUT    {{database is locked} {disk I/O error}}
    3 EBUSY        {{database is locked} {disk I/O error}}
    4 EINTR        {{database is locked} {disk I/O error}}
    5 ENOLCK       {{database is locked} {disk I/O error}}
    6 EACCES       {{database is locked} {disk I/O error}}
    7 EPERM        {{access permission denied} {disk I/O error}}
    8 EDEADLK      {{disk I/O error}}
    9 ENOMEM       {{disk I/O error}}
  } {
    proc vfsfault_install {} { test_syscall install fcntl }
    set errs [list]
    foreach e $errlist { lappend errs [list 1 $e] }
  
    set body [string map [list %VFS% $vfs] {
      sqlite3 db test.db
      db eval {
        CREATE TABLE t1(a, b);
        INSERT INTO t1 VALUES(1, 2);
      }
      set fd [open test.db-journal w]
      puts $fd "hello world"
      close $fd
      sqlite3 db test.db -vfs %VFS%
      db eval {
        SELECT * FROM t1;
      }
    }]
  
    do_faultsim_test 1.3.$vfs.$tn -faults vfsfault-* -prep {
      faultsim_restore
    } -body "
      test_syscall errno fcntl $errno
      $body
    " -test "
      faultsim_test_result {0 {1 2}} $errs
    "
  }
}

#-------------------------------------------------------------------------
# Check that a single EINTR error does not affect processing.
#
proc vfsfault_install {} { 
  test_syscall reset
  test_syscall install {open ftruncate close read pread pread64 write fallocate}
}

forcedelete test.db test.db2
sqlite3 db test.db
do_test 2.setup {
  execsql {
    CREATE TABLE t1(a, b, c, PRIMARY KEY(a));
    INSERT INTO t1 VALUES('abc', 'def', 'ghi');
    ATTACH 'test.db2' AS 'aux';
    CREATE TABLE aux.t2(x);
    INSERT INTO t2 VALUES(1);
  }
  faultsim_save_and_close
} {}

do_faultsim_test 2.1 -faults vfsfault-transient -prep {
  catch { db close }
  faultsim_restore
} -body {
  test_syscall errno open      EINTR
  test_syscall errno ftruncate EINTR
  test_syscall errno close     EINTR
  test_syscall errno read      EINTR
  test_syscall errno pread     EINTR
  test_syscall errno pread64   EINTR
  test_syscall errno write     EINTR
  test_syscall errno fallocate EINTR

  sqlite3 db test.db
  file_control_chunksize_test db main 8192

  set res [db eval {
    ATTACH 'test.db2' AS 'aux';
    SELECT * FROM t1;
    PRAGMA journal_mode = truncate;
    BEGIN;
      INSERT INTO t1 VALUES('jkl', 'mno', 'pqr');
      INSERT INTO t1 VALUES(randomblob(10000), 0, 0);
      UPDATE t2 SET x = 2;
    COMMIT;
    DELETE FROM t1 WHERE length(a)>3;
    SELECT * FROM t1;
    SELECT * FROM t2;
  }]
  db close
  set res
} -test {
  faultsim_test_result {0 {abc def ghi truncate abc def ghi jkl mno pqr 2}}
}

do_faultsim_test 2.2 -faults vfsfault-* -prep {
  catch { db close }
  faultsim_restore
} -body {
  sqlite3 db test.db
  set res [db eval {
    ATTACH 'test.db2' AS 'aux';
    SELECT * FROM t1;
    PRAGMA journal_mode = truncate;
    BEGIN;
      INSERT INTO t1 VALUES('jkl', 'mno', 'pqr');
      UPDATE t2 SET x = 2;
    COMMIT;
    SELECT * FROM t1;
    SELECT * FROM t2;
  }]
  db close
  set res
} -test {
  faultsim_test_result {0 {abc def ghi truncate abc def ghi jkl mno pqr 2}} \
    {1 {unable to open database file}}                                      \
    {1 {unable to open database: test.db2}}                                 \
    {1 {attempt to write a readonly database}}                              \
    {1 {disk I/O error}}                                                  
}

#-------------------------------------------------------------------------

proc vfsfault_install {} { 
  test_syscall reset
  test_syscall install {fstat fallocate}
}
do_faultsim_test 3 -faults vfsfault-* -prep {
  faultsim_delete_and_reopen
  file_control_chunksize_test db main 8192
  execsql {
    PRAGMA synchronous=OFF;
    CREATE TABLE t1(a, b);
    BEGIN;
      SELECT * FROM t1;
  }
} -body {
  test_syscall errno fstat     EIO
  test_syscall errno fallocate EIO

  execsql {
    INSERT INTO t1 VALUES(randomblob(10000), randomblob(10000));
    SELECT length(a) + length(b) FROM t1;
    COMMIT;
  }
} -test {
  faultsim_test_result {0 20000}
}

#-------------------------------------------------------------------------
# Test errors in mmap().
#
proc vfsfault_install {} { 
  test_syscall reset
  test_syscall install {mmap}
}

faultsim_delete_and_reopen
execsql {
  CREATE TABLE t1(a, b);
  INSERT INTO t1 VALUES(1, 2);
}
faultsim_save_and_close

do_faultsim_test 4 -faults vfsfault-* -prep {
  faultsim_restore_and_reopen
  file_control_chunksize_test db main 8192
  execsql { 
    PRAGMA mmap_size = 1000000;
  }
} -body {
  test_syscall errno mmap     EACCES

  execsql {
    SELECT * FROM t1;
  }
} -test {
  faultsim_test_result {0 {1 2}} {1 {disk I/O error}}
}

finish_test