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 if {![info exists testdir]} {
14 set testdir [file join [file dirname [info script]] .. .. test]
16 source $testdir/tester.tcl
17 source $testdir/malloc_common.tcl
18 set ::testprefix rbufault
20 proc copy_if_exists {src target} {
21 if {[file exists $src]} {
22 forcecopy $src $target
28 foreach {tn2 setup sql expect} {
30 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
31 CREATE INDEX t1cb ON t1(c, b);
32 INSERT INTO t1 VALUES(1, 1, 1);
33 INSERT INTO t1 VALUES(2, 2, 2);
34 INSERT INTO t1 VALUES(3, 3, 3);
36 CREATE TABLE rbu.data_t1(a, b, c, rbu_control);
37 INSERT INTO data_t1 VALUES(2, NULL, NULL, 1);
38 INSERT INTO data_t1 VALUES(3, 'three', NULL, '.x.');
39 INSERT INTO data_t1 VALUES(4, 4, 4, 0);
42 } {1 1 1 3 three 3 4 4 4}
45 CREATE TABLE t2(a PRIMARY KEY, b, c) WITHOUT ROWID;
46 CREATE INDEX t2cb ON t2(c, b);
47 INSERT INTO t2 VALUES('a', 'a', 'a');
48 INSERT INTO t2 VALUES('b', 'b', 'b');
49 INSERT INTO t2 VALUES('c', 'c', 'c');
51 CREATE TABLE rbu.data_t2(a, b, c, rbu_control);
52 INSERT INTO data_t2 VALUES('b', NULL, NULL, 1);
53 INSERT INTO data_t2 VALUES('c', 'see', NULL, '.x.');
54 INSERT INTO data_t2 VALUES('d', 'd', 'd', 0);
57 } {a a a c see c d d d}
60 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
61 CREATE TABLE t2(a PRIMARY KEY, b, c) WITHOUT ROWID;
62 CREATE INDEX t1cb ON t1(c, b);
63 CREATE INDEX t2cb ON t2(c, b);
65 CREATE TABLE rbu.data_t1(a, b, c, rbu_control);
66 CREATE TABLE rbu.data_t2(a, b, c, rbu_control);
67 INSERT INTO data_t1 VALUES(1, 2, 3, 0);
68 INSERT INTO data_t2 VALUES(4, 5, 6, 0);
70 SELECT * FROM t1 UNION ALL SELECT * FROM t2
74 CREATE TABLE t1(a PRIMARY KEY, b, c);
75 CREATE INDEX t1c ON t1(c);
76 INSERT INTO t1 VALUES('A', 'B', 'C');
77 INSERT INTO t1 VALUES('D', 'E', 'F');
79 CREATE TABLE rbu.data_t1(a, b, c, rbu_control);
80 INSERT INTO data_t1 VALUES('D', NULL, NULL, 1);
81 INSERT INTO data_t1 VALUES('A', 'Z', NULL, '.x.');
82 INSERT INTO data_t1 VALUES('G', 'H', 'I', 0);
84 SELECT * FROM t1 ORDER BY a;
88 CREATE TABLE t1(a, b, c);
89 CREATE INDEX t1c ON t1(c, b);
91 CREATE TABLE rbu.data_t1(a, b, c, rbu_rowid, rbu_control);
92 INSERT INTO data_t1 VALUES('a', 'b', 'c', 1, 0);
93 INSERT INTO data_t1 VALUES('d', 'e', 'f', '2', 0);
95 SELECT * FROM t1 ORDER BY a;
100 forcedelete rbu.db test.db
103 PRAGMA encoding = utf16;
104 ATTACH 'rbu.db' AS rbu;
109 forcecopy test.db test.db.bak
110 forcecopy rbu.db rbu.db.bak
112 foreach {tn f reslist} {
115 {1 {SQLITE_NOMEM - out of memory}}
117 {1 SQLITE_IOERR_NOMEM}
118 {1 {SQLITE_NOMEM - unable to open a temporary database file for storing temporary tables}}
123 {1 {SQLITE_IOERR - disk I/O error}}
125 {1 SQLITE_IOERR_WRITE}
126 {1 SQLITE_IOERR_READ}
127 {1 SQLITE_IOERR_FSYNC}
128 {1 {SQLITE_ERROR - SQL logic error}}
129 {1 {SQLITE_ERROR - unable to open database: rbu.db}}
130 {1 {SQLITE_IOERR - unable to open database: rbu.db}}
135 {1 {SQLITE_IOERR - disk I/O error}}
142 set lookaside_config [sqlite3_config_lookaside 0 0]
144 autoinstall_test_functions
146 do_faultsim_test 2.$tn2 -faults $::f -prep {
148 forcedelete test.db-journal test.db-wal rbu.db-journal rbu.db-wal
149 forcecopy test.db.bak test.db
150 forcecopy rbu.db.bak rbu.db
152 sqlite3rbu rbu test.db rbu.db
153 while {[rbu step]=="SQLITE_OK"} {}
156 faultsim_test_result {*}$::reslist
159 faultsim_integrity_check
160 set res [db eval $::sql]
161 if {$res != [list {*}$::expect]} {
165 error "data not as expected!"
172 sqlite3_config_lookaside {*}$lookaside_config
174 autoinstall_test_functions
177 for {set iStep 0} {$iStep<=21} {incr iStep} {
179 forcedelete test.db-journal test.db-wal rbu.db-journal rbu.db-wal
181 copy_if_exists test.db.bak test.db
182 copy_if_exists rbu.db.bak rbu.db
184 sqlite3rbu rbu test.db rbu.db
185 for {set x 0} {$x < $::iStep} {incr x} { rbu step }
188 # sqlite3 x rbu.db ; puts "XYZ [x eval { SELECT * FROM rbu_state } ]" ; x close
190 copy_if_exists test.db test.db.bak.2
191 copy_if_exists test.db-wal test.db.bak.2-wal
192 copy_if_exists test.db-oal test.db.bak.2-oal
193 copy_if_exists rbu.db rbu.db.bak.2
195 do_faultsim_test 3.$tn.$iStep -faults $::f -prep {
197 forcedelete test.db-journal test.db-wal rbu.db-journal rbu.db-wal
198 copy_if_exists test.db.bak.2 test.db
199 copy_if_exists test.db.bak.2-wal test.db-wal
200 copy_if_exists test.db.bak.2-oal test.db-oal
201 copy_if_exists rbu.db.bak.2 rbu.db
203 sqlite3rbu rbu test.db rbu.db
208 if {$testresult=="SQLITE_OK"} {set testresult "SQLITE_DONE"}
209 faultsim_test_result {*}$::reslist
212 # No error occurred. If the RBU has not already been fully applied,
213 # apply the rest of it now. Then ensure that the final state of the
214 # target db is as expected. And that "PRAGMA integrity_check"
216 sqlite3rbu rbu test.db rbu.db
217 while {[rbu step] == "SQLITE_OK"} {}
221 faultsim_integrity_check
223 set res [db eval $::sql]
224 if {$res != [list {*}$::expect]} {
227 puts "exp: $::expect"
228 error "data not as expected!"