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
|
# 2015 December 10
#
# 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.
#
#***********************************************************************
# This file implements regression tests for SQLite library. The focus
# of this file is the sqlite3_snapshot_xxx() APIs.
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
ifcapable !snapshot {finish_test; return}
set testprefix snapshot_fault
#-------------------------------------------------------------------------
# Check that an sqlite3_snapshot_open() client cannot be tricked into
# reading a corrupt snapshot even if a second client fails while
# checkpointing the db.
#
do_faultsim_test 1.0 -prep {
catch { db2 close }
faultsim_delete_and_reopen
sqlite3 db2 test.db
db2 eval {
CREATE TABLE t1(a, b UNIQUE, c UNIQUE);
INSERT INTO t1 VALUES(1, randomblob(500), randomblob(500));
INSERT INTO t1 VALUES(2, randomblob(500), randomblob(500));
PRAGMA journal_mode = wal;
INSERT INTO t1 VALUES(3, randomblob(500), randomblob(500));
BEGIN;
SELECT a FROM t1;
}
set ::snapshot [sqlite3_snapshot_get db2 main]
db2 eval COMMIT
db2 eval {
UPDATE t1 SET b=randomblob(501), c=randomblob(501) WHERE a=1;
INSERT INTO t1 VALUES(4, randomblob(500), randomblob(500));
INSERT INTO t1 VALUES(5, randomblob(500), randomblob(500));
INSERT INTO t1 VALUES(6, randomblob(500), randomblob(500));
}
} -body {
db eval { PRAGMA wal_checkpoint }
} -test {
db2 eval BEGIN
if {[catch { sqlite3_snapshot_open db2 main $::snapshot } msg]} {
if {$msg != "SQLITE_ERROR_SNAPSHOT" && $msg != "SQLITE_BUSY"} {
error "error is $msg"
}
} else {
set res [db2 eval {
SELECT a FROM t1;
PRAGMA integrity_check;
}]
if {$res != "1 2 3 ok"} { error "res is $res" }
}
sqlite3_snapshot_free $::snapshot
}
#-------------------------------------------------------------------------
# This test is similar to the previous one. Except, after the
# "PRAGMA wal_checkpoint" command fails the db is closed and reopened
# so as to require wal file recovery. It should not be possible to open
# a snapshot that is part of the body of a recovered wal file.
#
do_faultsim_test 2.0 -prep {
faultsim_delete_and_reopen
db eval {
CREATE TABLE t1(a, b UNIQUE, c UNIQUE);
INSERT INTO t1 VALUES(1, randomblob(500), randomblob(500));
INSERT INTO t1 VALUES(2, randomblob(500), randomblob(500));
PRAGMA journal_mode = wal;
INSERT INTO t1 VALUES(3, randomblob(500), randomblob(500));
BEGIN;
SELECT a FROM t1;
}
set ::snapshot [sqlite3_snapshot_get db main]
db eval COMMIT
db eval {
UPDATE t1 SET b=randomblob(501), c=randomblob(501) WHERE a=1;
INSERT INTO t1 VALUES(4, randomblob(500), randomblob(500));
INSERT INTO t1 VALUES(5, randomblob(500), randomblob(500));
INSERT INTO t1 VALUES(6, randomblob(500), randomblob(500));
}
} -body {
db eval { PRAGMA wal_checkpoint }
} -test {
db_save
db close
db_restore_and_reopen
db eval { SELECT * FROM t1 }
db eval BEGIN
if {[catch { sqlite3_snapshot_open db main $::snapshot } msg]} {
if {$msg != "SQLITE_ERROR_SNAPSHOT" && $msg != "SQLITE_BUSY"} {
error "error is $msg"
}
} else {
# This branch should actually never be taken. But it was useful in
# determining whether or not this test was actually working (by
# running a modified version of SQLite that allowed snapshots to be
# opened following a recovery).
error "TEST HAS FAILED"
set res [db eval {
SELECT a FROM t1;
PRAGMA integrity_check;
}]
if {$res != "1 2 3 ok"} { error "res is $res" }
}
sqlite3_snapshot_free $::snapshot
}
#-------------------------------------------------------------------------
# Test the handling of faults that occur within sqlite3_snapshot_open().
#
do_faultsim_test 3.0 -prep {
faultsim_delete_and_reopen
db eval {
CREATE TABLE t1(a, b UNIQUE, c UNIQUE);
INSERT INTO t1 VALUES(1, randomblob(500), randomblob(500));
INSERT INTO t1 VALUES(2, randomblob(500), randomblob(500));
PRAGMA journal_mode = wal;
INSERT INTO t1 VALUES(3, randomblob(500), randomblob(500));
BEGIN;
SELECT a FROM t1;
}
set ::snapshot [sqlite3_snapshot_get db main]
db eval COMMIT
db eval {
UPDATE t1 SET b=randomblob(501), c=randomblob(501) WHERE a=1;
INSERT INTO t1 VALUES(4, randomblob(500), randomblob(500));
INSERT INTO t1 VALUES(5, randomblob(500), randomblob(500));
INSERT INTO t1 VALUES(6, randomblob(500), randomblob(500));
BEGIN;
}
} -body {
if { [catch { sqlite3_snapshot_open db main $::snapshot } msg] } {
error $msg
}
} -test {
faultsim_test_result {0 {}} {1 SQLITE_IOERR} {1 SQLITE_NOMEM} \
{1 SQLITE_IOERR_NOMEM} {1 SQLITE_IOERR_READ}
if {$testrc==0} {
set res [db eval {
SELECT a FROM t1;
PRAGMA integrity_check;
}]
if {$res != "1 2 3 ok"} { error "res is $res" }
}
sqlite3_snapshot_free $::snapshot
}
#-------------------------------------------------------------------------
# Test the handling of faults that occur within sqlite3_snapshot_recover().
#
reset_db
do_execsql_test 4.0 {
PRAGMA journal_mode = wal;
CREATE TABLE t1(zzz);
INSERT INTO t1 VALUES('abc');
INSERT INTO t1 VALUES('def');
} {wal}
faultsim_save_and_close
do_test 4.0.1 {
faultsim_restore_and_reopen
db eval { SELECT * FROM sqlite_master }
sqlite3_snapshot_recover db main
} {}
db close
do_faultsim_test 4.0 -faults oom* -prep {
faultsim_restore_and_reopen
db eval { SELECT * FROM sqlite_master }
} -body {
sqlite3_snapshot_recover db main
} -test {
faultsim_test_result {0 {}} {1 SQLITE_NOMEM} {1 SQLITE_IOERR_NOMEM}
}
# The following test cases contrive to call sqlite3_snapshot_recover()
# before all pages of the *-shm file have been mapped. This tests an
# extra branch of error handling logic in snapshot_recover().
#
reset_db
do_execsql_test 4.1.0 {
PRAGMA page_size = 512;
PRAGMA journal_mode = wal;
PRAGMA wal_autocheckpoint = 0;
CREATE TABLE t1(zzz);
INSERT INTO t1 VALUES(randomblob( 500 * 9500 ));
PRAGMA user_version = 211;
} {wal 0}
do_test 4.1.1 {
list [file size test.db-shm] [file size test.db]
} {98304 512}
faultsim_save_and_close
do_faultsim_test 4.1 -faults shm* -prep {
catch { db2 close }
catch { db close }
faultsim_restore_and_reopen
sqlite3 db2 test.db
db2 eval { SELECT * FROM sqlite_master }
db eval BEGIN
sqlite3_snapshot_get_blob db main
db eval COMMIT
} -body {
sqlite3_snapshot_recover db main
} -test {
faultsim_test_result {0 {}} {1 SQLITE_IOERR}
}
#-------------------------------------------------------------------------
# Test the handling of faults that occur within sqlite3_snapshot_get().
#
reset_db
do_execsql_test 5.0 {
PRAGMA page_size = 512;
PRAGMA journal_mode = wal;
PRAGMA wal_autocheckpoint = 0;
CREATE TABLE t1(zzz);
INSERT INTO t1 VALUES(randomblob( 5000 ));
PRAGMA user_version = 211;
} {wal 0}
faultsim_save_and_close
do_faultsim_test 5 -prep {
faultsim_restore_and_reopen
execsql { SELECT count(*) FROM sqlite_master }
execsql BEGIN
} -body {
sqlite3_snapshot_get_blob db main
set {} {}
} -test {
execsql END
faultsim_test_result {0 {}} {1 SQLITE_IOERR} {1 SQLITE_NOMEM}
}
finish_test
|