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 #***********************************************************************
11 # This file implements regression tests for SQLite library. The focus
12 # of this file is the sqlite3_snapshot_xxx() APIs.
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
17 ifcapable !snapshot {finish_test; return}
18 set testprefix snapshot_fault
20 #-------------------------------------------------------------------------
21 # Check that an sqlite3_snapshot_open() client cannot be tricked into
22 # reading a corrupt snapshot even if a second client fails while
23 # checkpointing the db.
25 do_faultsim_test 1.0 -prep {
26 faultsim_delete_and_reopen
29 CREATE TABLE t1(a, b UNIQUE, c UNIQUE);
30 INSERT INTO t1 VALUES(1, randomblob(500), randomblob(500));
31 INSERT INTO t1 VALUES(2, randomblob(500), randomblob(500));
32 PRAGMA journal_mode = wal;
33 INSERT INTO t1 VALUES(3, randomblob(500), randomblob(500));
37 set ::snapshot [sqlite3_snapshot_get db2 main]
40 UPDATE t1 SET b=randomblob(501), c=randomblob(501) WHERE a=1;
41 INSERT INTO t1 VALUES(4, randomblob(500), randomblob(500));
42 INSERT INTO t1 VALUES(5, randomblob(500), randomblob(500));
43 INSERT INTO t1 VALUES(6, randomblob(500), randomblob(500));
46 db eval { PRAGMA wal_checkpoint }
49 if {[catch { sqlite3_snapshot_open db2 main $::snapshot } msg]} {
50 if {$msg != "SQLITE_ERROR_SNAPSHOT" && $msg != "SQLITE_BUSY"} {
56 PRAGMA integrity_check;
58 if {$res != "1 2 3 ok"} { error "res is $res" }
61 sqlite3_snapshot_free $::snapshot
64 #-------------------------------------------------------------------------
65 # This test is similar to the previous one. Except, after the
66 # "PRAGMA wal_checkpoint" command fails the db is closed and reopened
67 # so as to require wal file recovery. It should not be possible to open
68 # a snapshot that is part of the body of a recovered wal file.
70 do_faultsim_test 2.0 -prep {
71 faultsim_delete_and_reopen
73 CREATE TABLE t1(a, b UNIQUE, c UNIQUE);
74 INSERT INTO t1 VALUES(1, randomblob(500), randomblob(500));
75 INSERT INTO t1 VALUES(2, randomblob(500), randomblob(500));
76 PRAGMA journal_mode = wal;
77 INSERT INTO t1 VALUES(3, randomblob(500), randomblob(500));
81 set ::snapshot [sqlite3_snapshot_get db main]
85 UPDATE t1 SET b=randomblob(501), c=randomblob(501) WHERE a=1;
86 INSERT INTO t1 VALUES(4, randomblob(500), randomblob(500));
87 INSERT INTO t1 VALUES(5, randomblob(500), randomblob(500));
88 INSERT INTO t1 VALUES(6, randomblob(500), randomblob(500));
91 db eval { PRAGMA wal_checkpoint }
97 db eval { SELECT * FROM t1 }
100 if {[catch { sqlite3_snapshot_open db main $::snapshot } msg]} {
101 if {$msg != "SQLITE_ERROR_SNAPSHOT" && $msg != "SQLITE_BUSY"} {
102 error "error is $msg"
105 # This branch should actually never be taken. But it was useful in
106 # determining whether or not this test was actually working (by
107 # running a modified version of SQLite that allowed snapshots to be
108 # opened following a recovery).
109 error "TEST HAS FAILED"
113 PRAGMA integrity_check;
115 if {$res != "1 2 3 ok"} { error "res is $res" }
118 sqlite3_snapshot_free $::snapshot
121 #-------------------------------------------------------------------------
122 # Test the handling of faults that occur within sqlite3_snapshot_open().
124 do_faultsim_test 3.0 -prep {
125 faultsim_delete_and_reopen
127 CREATE TABLE t1(a, b UNIQUE, c UNIQUE);
128 INSERT INTO t1 VALUES(1, randomblob(500), randomblob(500));
129 INSERT INTO t1 VALUES(2, randomblob(500), randomblob(500));
130 PRAGMA journal_mode = wal;
131 INSERT INTO t1 VALUES(3, randomblob(500), randomblob(500));
135 set ::snapshot [sqlite3_snapshot_get db main]
138 UPDATE t1 SET b=randomblob(501), c=randomblob(501) WHERE a=1;
139 INSERT INTO t1 VALUES(4, randomblob(500), randomblob(500));
140 INSERT INTO t1 VALUES(5, randomblob(500), randomblob(500));
141 INSERT INTO t1 VALUES(6, randomblob(500), randomblob(500));
145 if { [catch { sqlite3_snapshot_open db main $::snapshot } msg] } {
149 faultsim_test_result {0 {}} {1 SQLITE_IOERR} {1 SQLITE_NOMEM} \
150 {1 SQLITE_IOERR_NOMEM} {1 SQLITE_IOERR_READ}
154 PRAGMA integrity_check;
156 if {$res != "1 2 3 ok"} { error "res is $res" }
159 sqlite3_snapshot_free $::snapshot
162 #-------------------------------------------------------------------------
163 # Test the handling of faults that occur within sqlite3_snapshot_recover().
166 do_execsql_test 4.0 {
167 PRAGMA journal_mode = wal;
168 CREATE TABLE t1(zzz);
169 INSERT INTO t1 VALUES('abc');
170 INSERT INTO t1 VALUES('def');
172 faultsim_save_and_close
175 faultsim_restore_and_reopen
176 db eval { SELECT * FROM sqlite_master }
177 sqlite3_snapshot_recover db main
181 do_faultsim_test 4.0 -faults oom* -prep {
182 faultsim_restore_and_reopen
183 db eval { SELECT * FROM sqlite_master }
185 sqlite3_snapshot_recover db main
187 faultsim_test_result {0 {}} {1 SQLITE_NOMEM} {1 SQLITE_IOERR_NOMEM}
190 # The following test cases contrive to call sqlite3_snapshot_recover()
191 # before all pages of the *-shm file have been mapped. This tests an
192 # extra branch of error handling logic in snapshot_recover().
195 do_execsql_test 4.1.0 {
196 PRAGMA page_size = 512;
197 PRAGMA journal_mode = wal;
198 PRAGMA wal_autocheckpoint = 0;
199 CREATE TABLE t1(zzz);
200 INSERT INTO t1 VALUES(randomblob( 500 * 9500 ));
201 PRAGMA user_version = 211;
205 list [file size test.db-shm] [file size test.db]
208 faultsim_save_and_close
209 do_faultsim_test 4.1 -faults shm* -prep {
212 faultsim_restore_and_reopen
214 db2 eval { SELECT * FROM sqlite_master }
216 sqlite3_snapshot_get_blob db main
219 sqlite3_snapshot_recover db main
221 faultsim_test_result {0 {}} {1 SQLITE_IOERR}