blob: 521e76b727bc9c87b84351671f661ccd8c89353d [file] [log] [blame]
danb0ac3e32010-06-16 10:55:42 +00001# 2010 June 15
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
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.
9#
10#***********************************************************************
11#
12
13set testdir [file dirname $argv0]
14source $testdir/tester.tcl
15source $testdir/lock_common.tcl
16source $testdir/malloc_common.tcl
17
danb3f43512010-07-03 16:37:45 +000018if {[permutation] == "inmemory_journal"} {
19 finish_test
20 return
21}
22
danb0ac3e32010-06-16 10:55:42 +000023set a_string_counter 1
24proc a_string {n} {
25 global a_string_counter
26 incr a_string_counter
27 string range [string repeat "${a_string_counter}." $n] 1 $n
28}
29db func a_string a_string
30
dan5653e4d2010-08-12 11:25:47 +000031if 1 {
32
danb0ac3e32010-06-16 10:55:42 +000033#-------------------------------------------------------------------------
34# Test fault-injection while rolling back a hot-journal file.
35#
36do_test pagerfault-1-pre1 {
37 execsql {
38 PRAGMA journal_mode = DELETE;
39 PRAGMA cache_size = 10;
40 CREATE TABLE t1(a UNIQUE, b UNIQUE);
41 INSERT INTO t1 VALUES(a_string(200), a_string(300));
42 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
43 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
44 BEGIN;
45 INSERT INTO t1 SELECT a_string(201), a_string(301) FROM t1;
46 INSERT INTO t1 SELECT a_string(202), a_string(302) FROM t1;
47 INSERT INTO t1 SELECT a_string(203), a_string(303) FROM t1;
48 INSERT INTO t1 SELECT a_string(204), a_string(304) FROM t1;
49 }
50 faultsim_save_and_close
51} {}
52do_faultsim_test pagerfault-1 -prep {
53 faultsim_restore_and_reopen
54} -body {
55 execsql { SELECT count(*) FROM t1 }
56} -test {
dan22b328b2010-08-11 18:56:45 +000057 faultsim_test_result {0 4}
danb0ac3e32010-06-16 10:55:42 +000058 faultsim_integrity_check
59 if {[db one { SELECT count(*) FROM t1 }] != 4} {
60 error "Database content appears incorrect"
61 }
62}
63
64#-------------------------------------------------------------------------
dande4996e2010-06-19 11:30:41 +000065# Test fault-injection while rolling back a hot-journal file with a
66# page-size different from the current value stored on page 1 of the
67# database file.
68#
69do_test pagerfault-2-pre1 {
70 testvfs tv -default 1
71 tv filter xSync
72 tv script xSyncCb
73 proc xSyncCb {filename args} {
74 if {[string match *journal filename]==0} faultsim_save
75 }
76 faultsim_delete_and_reopen
77 execsql {
78 PRAGMA page_size = 4096;
79 BEGIN;
80 CREATE TABLE abc(a, b, c);
81 INSERT INTO abc VALUES('o', 't', 't');
82 INSERT INTO abc VALUES('f', 'f', 's');
83 INSERT INTO abc SELECT * FROM abc; -- 4
84 INSERT INTO abc SELECT * FROM abc; -- 8
85 INSERT INTO abc SELECT * FROM abc; -- 16
86 INSERT INTO abc SELECT * FROM abc; -- 32
87 INSERT INTO abc SELECT * FROM abc; -- 64
88 INSERT INTO abc SELECT * FROM abc; -- 128
89 INSERT INTO abc SELECT * FROM abc; -- 256
90 COMMIT;
91 PRAGMA page_size = 1024;
92 VACUUM;
93 }
94 db close
95 tv delete
96} {}
97do_faultsim_test pagerfault-2 -prep {
98 faultsim_restore_and_reopen
99} -body {
100 execsql { SELECT * FROM abc }
101} -test {
102 set answer [split [string repeat "ottffs" 128] ""]
103 faultsim_test_result [list 0 $answer]
104 faultsim_integrity_check
105 set res [db eval { SELECT * FROM abc }]
106 if {$res != $answer} { error "Database content appears incorrect ($res)" }
danec6ffc12010-06-24 19:16:06 +0000107}
dande4996e2010-06-19 11:30:41 +0000108
109#-------------------------------------------------------------------------
danb0ac3e32010-06-16 10:55:42 +0000110# Test fault-injection while rolling back hot-journals that were created
111# as part of a multi-file transaction.
112#
dande4996e2010-06-19 11:30:41 +0000113do_test pagerfault-3-pre1 {
danb0ac3e32010-06-16 10:55:42 +0000114 testvfs tstvfs -default 1
115 tstvfs filter xDelete
116 tstvfs script xDeleteCallback
117
118 proc xDeleteCallback {method file args} {
119 set file [file tail $file]
120 if { [string match *mj* $file] } { faultsim_save }
121 }
122
123 faultsim_delete_and_reopen
124 db func a_string a_string
125
126 execsql {
127 ATTACH 'test.db2' AS aux;
128 PRAGMA journal_mode = DELETE;
129 PRAGMA main.cache_size = 10;
130 PRAGMA aux.cache_size = 10;
131
132 CREATE TABLE t1(a UNIQUE, b UNIQUE);
133 CREATE TABLE aux.t2(a UNIQUE, b UNIQUE);
134 INSERT INTO t1 VALUES(a_string(200), a_string(300));
135 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
136 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
137 INSERT INTO t2 SELECT * FROM t1;
138
139 BEGIN;
140 INSERT INTO t1 SELECT a_string(201), a_string(301) FROM t1;
141 INSERT INTO t1 SELECT a_string(202), a_string(302) FROM t1;
142 INSERT INTO t1 SELECT a_string(203), a_string(303) FROM t1;
143 INSERT INTO t1 SELECT a_string(204), a_string(304) FROM t1;
144 REPLACE INTO t2 SELECT * FROM t1;
145 COMMIT;
146 }
147
148 db close
149 tstvfs delete
150} {}
danec6ffc12010-06-24 19:16:06 +0000151do_faultsim_test pagerfault-3 -prep {
danb0ac3e32010-06-16 10:55:42 +0000152 faultsim_restore_and_reopen
153} -body {
154 execsql {
155 ATTACH 'test.db2' AS aux;
156 SELECT count(*) FROM t2;
157 SELECT count(*) FROM t1;
158 }
159} -test {
160 faultsim_test_result {0 {4 4}} {1 {unable to open database: test.db2}}
161 faultsim_integrity_check
danb0ac3e32010-06-16 10:55:42 +0000162 catchsql { ATTACH 'test.db2' AS aux }
163 if {[db one { SELECT count(*) FROM t1 }] != 4
164 || [db one { SELECT count(*) FROM t2 }] != 4
165 } {
166 error "Database content appears incorrect"
167 }
168}
169
dande4996e2010-06-19 11:30:41 +0000170#-------------------------------------------------------------------------
171# Test fault-injection as part of a vanilla, no-transaction, INSERT
172# statement.
173#
174do_faultsim_test pagerfault-4 -prep {
175 faultsim_delete_and_reopen
176} -body {
177 execsql {
178 CREATE TABLE x(y);
179 INSERT INTO x VALUES('z');
180 SELECT * FROM x;
181 }
182} -test {
183 faultsim_test_result {0 z}
184 faultsim_integrity_check
185}
186
187#-------------------------------------------------------------------------
188# Test fault-injection as part of a commit when using journal_mode=PERSIST.
dan146ed782010-06-19 17:26:37 +0000189# Three different cases:
190#
191# pagerfault-5.1: With no journal_size_limit configured.
192# pagerfault-5.2: With a journal_size_limit configured.
193# pagerfault-5.4: Multi-file transaction. One connection has a
194# journal_size_limit of 0, the other has no limit.
dande4996e2010-06-19 11:30:41 +0000195#
196do_test pagerfault-5-pre1 {
197 faultsim_delete_and_reopen
198 db func a_string a_string
199 execsql {
200 CREATE TABLE t1(a UNIQUE, b UNIQUE);
201 INSERT INTO t1 VALUES(a_string(200), a_string(300));
202 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
203 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
204 }
205 faultsim_save_and_close
206} {}
207do_faultsim_test pagerfault-5.1 -prep {
208 faultsim_restore_and_reopen
209 db func a_string a_string
210 execsql { PRAGMA journal_mode = PERSIST }
211} -body {
212 execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
213} -test {
214 faultsim_test_result {0 {}}
215 faultsim_integrity_check
216}
217do_faultsim_test pagerfault-5.2 -prep {
218 faultsim_restore_and_reopen
219 db func a_string a_string
220 execsql {
221 PRAGMA journal_mode = PERSIST;
222 PRAGMA journal_size_limit = 2048;
223 }
224} -body {
225 execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
226} -test {
227 faultsim_test_result {0 {}}
228 faultsim_integrity_check
229}
danec6ffc12010-06-24 19:16:06 +0000230do_faultsim_test pagerfault-5.3 -faults oom-transient -prep {
dande4996e2010-06-19 11:30:41 +0000231 faultsim_restore_and_reopen
232 db func a_string a_string
233 file delete -force test2.db test2.db-journal test2.db-wal
234 execsql {
235 PRAGMA journal_mode = PERSIST;
236 ATTACH 'test2.db' AS aux;
237 PRAGMA aux.journal_mode = PERSIST;
238 PRAGMA aux.journal_size_limit = 0;
239 }
240} -body {
241 execsql {
242 BEGIN;
243 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
244 CREATE TABLE aux.t2 AS SELECT * FROM t1;
245 COMMIT;
246 }
247} -test {
248 faultsim_test_result {0 {}}
danec6ffc12010-06-24 19:16:06 +0000249
danf43d7fc2010-07-03 10:00:00 +0000250 catchsql { COMMIT }
251 catchsql { ROLLBACK }
252
253 faultsim_integrity_check
danec6ffc12010-06-24 19:16:06 +0000254 set res ""
255 set rc [catch { set res [db one { PRAGMA aux.integrity_check }] }]
256 if {$rc!=0 || $res != "ok"} {error "integrity-check problem:$rc $res"}
dande4996e2010-06-19 11:30:41 +0000257}
258
dan153eda02010-06-21 07:45:47 +0000259#-------------------------------------------------------------------------
260# Test fault-injection as part of a commit when using
261# journal_mode=TRUNCATE.
262#
263do_test pagerfault-6-pre1 {
264 faultsim_delete_and_reopen
265 db func a_string a_string
266 execsql {
267 CREATE TABLE t1(a UNIQUE, b UNIQUE);
268 INSERT INTO t1 VALUES(a_string(200), a_string(300));
269 }
270 faultsim_save_and_close
271} {}
danf9b44192010-06-25 19:09:48 +0000272
dan153eda02010-06-21 07:45:47 +0000273do_faultsim_test pagerfault-6.1 -prep {
274 faultsim_restore_and_reopen
275 db func a_string a_string
276 execsql { PRAGMA journal_mode = TRUNCATE }
277} -body {
278 execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
danf9b44192010-06-25 19:09:48 +0000279 execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
280} -test {
281 faultsim_test_result {0 {}}
282 faultsim_integrity_check
283}
284
285# The unix vfs xAccess() method considers a file zero bytes in size to
286# "not exist". This proc overrides that behaviour so that a zero length
287# file is considered to exist.
288#
289proc xAccess {method filename op args} {
290 if {$op != "SQLITE_ACCESS_EXISTS"} { return "" }
291 return [file exists $filename]
292}
293do_faultsim_test pagerfault-6.2 -faults cantopen-* -prep {
294 shmfault filter xAccess
295 shmfault script xAccess
296
297 faultsim_restore_and_reopen
298 db func a_string a_string
299 execsql { PRAGMA journal_mode = TRUNCATE }
300} -body {
301 execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
302 execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
dan153eda02010-06-21 07:45:47 +0000303} -test {
304 faultsim_test_result {0 {}}
305 faultsim_integrity_check
306}
307
dan146ed782010-06-19 17:26:37 +0000308# The following was an attempt to get a bitvec malloc to fail. Didn't work.
309#
310# do_test pagerfault-6-pre1 {
311# faultsim_delete_and_reopen
312# execsql {
313# CREATE TABLE t1(x, y, UNIQUE(x, y));
314# INSERT INTO t1 VALUES(1, randomblob(1501));
315# INSERT INTO t1 VALUES(2, randomblob(1502));
316# INSERT INTO t1 VALUES(3, randomblob(1503));
317# INSERT INTO t1 VALUES(4, randomblob(1504));
318# INSERT INTO t1
319# SELECT x, randomblob(1500+oid+(SELECT max(oid) FROM t1)) FROM t1;
320# INSERT INTO t1
321# SELECT x, randomblob(1500+oid+(SELECT max(oid) FROM t1)) FROM t1;
322# INSERT INTO t1
323# SELECT x, randomblob(1500+oid+(SELECT max(oid) FROM t1)) FROM t1;
324# INSERT INTO t1
325# SELECT x, randomblob(1500+oid+(SELECT max(oid) FROM t1)) FROM t1;
326# }
327# faultsim_save_and_close
328# } {}
329# do_faultsim_test pagerfault-6 -prep {
330# faultsim_restore_and_reopen
331# } -body {
332# execsql {
333# BEGIN;
334# UPDATE t1 SET x=x+4 WHERE x=1;
335# SAVEPOINT one;
336# UPDATE t1 SET x=x+4 WHERE x=2;
337# SAVEPOINT three;
338# UPDATE t1 SET x=x+4 WHERE x=3;
339# SAVEPOINT four;
340# UPDATE t1 SET x=x+4 WHERE x=4;
341# RELEASE three;
342# COMMIT;
343# SELECT DISTINCT x FROM t1;
344# }
345# } -test {
346# faultsim_test_result {0 {5 6 7 8}}
347# faultsim_integrity_check
348# }
dan346e4262010-06-23 19:27:36 +0000349#
dandca321a2010-06-24 10:50:17 +0000350
351# This is designed to provoke a special case in the pager code:
352#
353# If an error (specifically, a FULL or IOERR error) occurs while writing a
354# dirty page to the file-system in order to free up memory, the pager enters
355# the "error state". An IO error causes SQLite to roll back the current
356# transaction (exiting the error state). A FULL error, however, may only
357# rollback the current statement.
358#
359# This block tests that nothing goes wrong if a FULL error occurs while
360# writing a dirty page out to free memory from within a statement that has
361# opened a statement transaction.
362#
dan346e4262010-06-23 19:27:36 +0000363do_test pagerfault-7-pre1 {
364 faultsim_delete_and_reopen
365 execsql {
366 CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
367 BEGIN;
368 INSERT INTO t2 VALUES(NULL, randomblob(1500));
369 INSERT INTO t2 VALUES(NULL, randomblob(1500));
370 INSERT INTO t2 SELECT NULL, randomblob(1500) FROM t2; -- 4
371 INSERT INTO t2 SELECT NULL, randomblob(1500) FROM t2; -- 8
372 INSERT INTO t2 SELECT NULL, randomblob(1500) FROM t2; -- 16
373 INSERT INTO t2 SELECT NULL, randomblob(1500) FROM t2; -- 32
374 INSERT INTO t2 SELECT NULL, randomblob(1500) FROM t2; -- 64
375 COMMIT;
376 CREATE TABLE t1(a PRIMARY KEY, b);
377 INSERT INTO t1 SELECT * FROM t2;
378 DROP TABLE t2;
379 }
380 faultsim_save_and_close
381} {}
danec6ffc12010-06-24 19:16:06 +0000382do_faultsim_test pagerfault-7 -prep {
dan346e4262010-06-23 19:27:36 +0000383 faultsim_restore_and_reopen
384 execsql {
385 PRAGMA cache_size = 10;
386 BEGIN;
387 UPDATE t1 SET b = randomblob(1500);
388 }
389} -body {
390 execsql { UPDATE t1 SET a = 65, b = randomblob(1500) WHERE (a+1)>200 }
391 execsql COMMIT
392} -test {
393 faultsim_test_result {0 {}}
394 faultsim_integrity_check
395}
dan146ed782010-06-19 17:26:37 +0000396
dandca321a2010-06-24 10:50:17 +0000397do_test pagerfault-8-pre1 {
398 faultsim_delete_and_reopen
399 execsql {
400 PRAGMA auto_vacuum = 1;
401 CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
402 BEGIN;
403 INSERT INTO t1 VALUES(NULL, randomblob(1500));
404 INSERT INTO t1 VALUES(NULL, randomblob(1500));
405 INSERT INTO t1 SELECT NULL, randomblob(1500) FROM t1; -- 4
406 INSERT INTO t1 SELECT NULL, randomblob(1500) FROM t1; -- 8
407 INSERT INTO t1 SELECT NULL, randomblob(1500) FROM t1; -- 16
408 INSERT INTO t1 SELECT NULL, randomblob(1500) FROM t1; -- 32
409 INSERT INTO t1 SELECT NULL, randomblob(1500) FROM t1; -- 64
410 COMMIT;
411 }
412 faultsim_save_and_close
413 set filesize [file size test.db]
414 set {} {}
415} {}
416do_test pagerfault-8-pre2 {
417 faultsim_restore_and_reopen
418 execsql { DELETE FROM t1 WHERE a>32 }
419 expr {[file size test.db] < $filesize}
420} {1}
dandca321a2010-06-24 10:50:17 +0000421do_faultsim_test pagerfault-8 -prep {
422 faultsim_restore_and_reopen
423 execsql {
424 BEGIN;
425 DELETE FROM t1 WHERE a>32;
426 }
427} -body {
428 execsql COMMIT
429} -test {
430 faultsim_test_result {0 {}}
431 faultsim_integrity_check
432}
433
dan273f3f02010-06-26 15:42:33 +0000434#-------------------------------------------------------------------------
435# This test case is specially designed so that during a savepoint
436# rollback, a new cache entry must be allocated (see comments surrounding
437# the call to sqlite3PagerAcquire() from within pager_playback_one_page()
438# for details). Test the effects of injecting an OOM at this point.
439#
dan0a6052e2010-06-24 13:24:26 +0000440do_test pagerfault-9-pre1 {
441 faultsim_delete_and_reopen
442 execsql {
443 PRAGMA auto_vacuum = incremental;
444 CREATE TABLE t1(x);
445 CREATE TABLE t2(y);
446 CREATE TABLE t3(z);
447
448 INSERT INTO t1 VALUES(randomblob(900));
449 INSERT INTO t1 VALUES(randomblob(900));
450 DELETE FROM t1;
451 }
452 faultsim_save_and_close
453} {}
dan273f3f02010-06-26 15:42:33 +0000454do_faultsim_test pagerfault-9.1 -prep {
dan0a6052e2010-06-24 13:24:26 +0000455 faultsim_restore_and_reopen
456 execsql {
457 BEGIN;
458 INSERT INTO t1 VALUES(randomblob(900));
459 INSERT INTO t1 VALUES(randomblob(900));
460 DROP TABLE t3;
461 DROP TABLE t2;
462 SAVEPOINT abc;
463 PRAGMA incremental_vacuum;
464 }
465} -body {
466 execsql {
467 ROLLBACK TO abc;
468 COMMIT;
469 PRAGMA freelist_count
470 }
471} -test {
472 faultsim_test_result {0 2}
473 faultsim_integrity_check
474
475 set sl [db one { SELECT COALESCE(sum(length(x)), 'null') FROM t1 }]
476 if {$sl!="null" && $sl!=1800} {
477 error "Content looks no good... ($sl)"
478 }
479}
480
dan273f3f02010-06-26 15:42:33 +0000481#-------------------------------------------------------------------------
482# Test fault injection with a temporary database file.
483#
danc8ce3972010-06-29 10:30:23 +0000484foreach v {a b} {
485 do_faultsim_test pagerfault-10$v -prep {
486 sqlite3 db ""
487 db func a_string a_string;
488 execsql {
489 PRAGMA cache_size = 10;
490 BEGIN;
491 CREATE TABLE xx(a, b, UNIQUE(a, b));
492 INSERT INTO xx VALUES(a_string(200), a_string(200));
493 INSERT INTO xx SELECT a_string(200), a_string(200) FROM xx;
494 INSERT INTO xx SELECT a_string(200), a_string(200) FROM xx;
495 INSERT INTO xx SELECT a_string(200), a_string(200) FROM xx;
496 INSERT INTO xx SELECT a_string(200), a_string(200) FROM xx;
497 COMMIT;
498 }
499 } -body {
500 execsql { UPDATE xx SET a = a_string(300) }
501 } -test {
502 faultsim_test_result {0 {}}
503 if {$::v == "b"} { execsql { PRAGMA journal_mode = TRUNCATE } }
504 faultsim_integrity_check
505 faultsim_integrity_check
dan273f3f02010-06-26 15:42:33 +0000506 }
dan273f3f02010-06-26 15:42:33 +0000507}
508
dan273f3f02010-06-26 15:42:33 +0000509#-------------------------------------------------------------------------
510# Test fault injection with transaction savepoints (savepoints created
511# when a SAVEPOINT command is executed outside of any other savepoint
512# or transaction context).
513#
514do_test pagerfault-9-pre1 {
515 faultsim_delete_and_reopen
516 db func a_string a_string;
517 execsql {
518 PRAGMA auto_vacuum = on;
519 CREATE TABLE t1(x UNIQUE);
520 CREATE TABLE t2(y UNIQUE);
521 CREATE TABLE t3(z UNIQUE);
522 BEGIN;
523 INSERT INTO t1 VALUES(a_string(202));
524 INSERT INTO t2 VALUES(a_string(203));
525 INSERT INTO t3 VALUES(a_string(204));
526 INSERT INTO t1 SELECT a_string(202) FROM t1;
527 INSERT INTO t1 SELECT a_string(203) FROM t1;
528 INSERT INTO t1 SELECT a_string(204) FROM t1;
529 INSERT INTO t1 SELECT a_string(205) FROM t1;
530 INSERT INTO t2 SELECT a_string(length(x)) FROM t1;
531 INSERT INTO t3 SELECT a_string(length(x)) FROM t1;
532 COMMIT;
533 }
534 faultsim_save_and_close
535} {}
536do_faultsim_test pagerfault-11 -prep {
537 faultsim_restore_and_reopen
538 execsql { PRAGMA cache_size = 10 }
539} -body {
540 execsql {
541 SAVEPOINT trans;
542 UPDATE t2 SET y = y||'2';
543 INSERT INTO t3 SELECT * FROM t2;
544 DELETE FROM t1;
545 ROLLBACK TO trans;
546 UPDATE t1 SET x = x||'3';
547 INSERT INTO t2 SELECT * FROM t1;
548 DELETE FROM t3;
549 RELEASE trans;
550 }
551} -test {
552 faultsim_test_result {0 {}}
553 faultsim_integrity_check
554}
555
danc396d4a2010-07-02 11:27:43 +0000556
danc8ce3972010-06-29 10:30:23 +0000557#-------------------------------------------------------------------------
558# Test fault injection when writing to a database file that resides on
559# a file-system with a sector-size larger than the database page-size.
560#
dand3533312010-06-28 19:04:02 +0000561do_test pagerfault-12-pre1 {
562 testvfs ss_layer -default 1
563 ss_layer sectorsize 4096
564 faultsim_delete_and_reopen
565 db func a_string a_string;
566
567 execsql {
568 PRAGMA page_size = 1024;
569 PRAGMA journal_mode = PERSIST;
570 PRAGMA cache_size = 10;
571 BEGIN;
572 CREATE TABLE t1(x, y UNIQUE);
573 INSERT INTO t1 VALUES(a_string(333), a_string(444));
574 INSERT INTO t1 SELECT a_string(333+rowid), a_string(444+rowid) FROM t1;
575 INSERT INTO t1 SELECT a_string(333+rowid), a_string(444+rowid) FROM t1;
576 INSERT INTO t1 SELECT a_string(333+rowid), a_string(444+rowid) FROM t1;
577 INSERT INTO t1 SELECT a_string(333+rowid), a_string(444+rowid) FROM t1;
578 INSERT INTO t1 SELECT a_string(44), a_string(55) FROM t1 LIMIT 13;
579 COMMIT;
580 }
581 faultsim_save_and_close
582} {}
danc396d4a2010-07-02 11:27:43 +0000583
584do_faultsim_test pagerfault-12a -prep {
dand3533312010-06-28 19:04:02 +0000585 faultsim_restore_and_reopen
586 execsql { PRAGMA cache_size = 10 }
587 db func a_string a_string;
588} -body {
589 execsql {
590 UPDATE t1 SET x = a_string(length(x)), y = a_string(length(y));
591 }
592} -test {
593 faultsim_test_result {0 {}}
594 faultsim_integrity_check
595}
596
danc396d4a2010-07-02 11:27:43 +0000597do_test pagerfault-12-pre2 {
598 faultsim_restore_and_reopen
599 execsql {
600 CREATE TABLE t2 AS SELECT * FROM t1 LIMIT 10;
601 }
602 faultsim_save_and_close
603} {}
604do_faultsim_test pagerfault-12b -prep {
605 faultsim_restore_and_reopen
606 db func a_string a_string;
607 execsql { SELECT * FROM t1 }
608} -body {
609 set sql(1) { UPDATE t2 SET x = a_string(280) }
610 set sql(2) { UPDATE t1 SET x = a_string(280) WHERE rowid = 5 }
611
612 db eval { SELECT rowid FROM t1 LIMIT 2 } { db eval $sql($rowid) }
613
614} -test {
615 faultsim_test_result {0 {}}
616 faultsim_integrity_check
617}
618
619catch { db close }
620ss_layer delete
621
dand3533312010-06-28 19:04:02 +0000622
danc8ce3972010-06-29 10:30:23 +0000623#-------------------------------------------------------------------------
danba3cbf32010-06-30 04:29:03 +0000624# Test fault injection when SQLite opens a database where the size of the
625# database file is zero bytes but the accompanying journal file is larger
626# than that. In this scenario SQLite should delete the journal file
627# without rolling it back, even if it is in all other respects a valid
628# hot-journal file.
danc8ce3972010-06-29 10:30:23 +0000629#
630do_test pagerfault-13-pre1 {
631 faultsim_delete_and_reopen
632 db func a_string a_string;
633 execsql {
634 PRAGMA journal_mode = PERSIST;
635 BEGIN;
636 CREATE TABLE t1(x, y UNIQUE);
637 INSERT INTO t1 VALUES(a_string(333), a_string(444));
638 COMMIT;
639 }
640 db close
641 file delete -force test.db
642 faultsim_save
643} {}
644do_faultsim_test pagerfault-13 -prep {
645 faultsim_restore_and_reopen
646} -body {
647 execsql { CREATE TABLE xx(a, b) }
648} -test {
649 faultsim_test_result {0 {}}
650}
651
danba3cbf32010-06-30 04:29:03 +0000652#---------------------------------------------------------------------------
653# Test fault injection into a small backup operation.
654#
655do_test pagerfault-14-pre1 {
656 faultsim_delete_and_reopen
657 db func a_string a_string;
658 execsql {
659 PRAGMA journal_mode = PERSIST;
660 ATTACH 'test.db2' AS two;
661 BEGIN;
662 CREATE TABLE t1(x, y UNIQUE);
663 CREATE TABLE two.t2(x, y UNIQUE);
664 INSERT INTO t1 VALUES(a_string(333), a_string(444));
665 INSERT INTO t2 VALUES(a_string(333), a_string(444));
666 COMMIT;
667 }
668 faultsim_save_and_close
669} {}
dan6b63ab42010-06-30 10:36:18 +0000670
671do_faultsim_test pagerfault-14a -prep {
danba3cbf32010-06-30 04:29:03 +0000672 faultsim_restore_and_reopen
673} -body {
674 if {[catch {db backup test.db2} msg]} { error [regsub {.*: } $msg {}] }
675} -test {
676 faultsim_test_result {0 {}} {1 {}} {1 {SQL logic error or missing database}}
danc8ce3972010-06-29 10:30:23 +0000677}
dan6b63ab42010-06-30 10:36:18 +0000678do_faultsim_test pagerfault-14b -prep {
shanehcaace922010-07-08 13:33:47 +0000679 catch { db2 close }
dan6b63ab42010-06-30 10:36:18 +0000680 faultsim_restore_and_reopen
681 sqlite3 db2 ""
682 db2 eval { PRAGMA page_size = 4096; CREATE TABLE xx(a) }
683} -body {
684 sqlite3_backup B db2 main db main
685 B step 200
686 set rc [B finish]
687 if {[string match SQLITE_IOERR_* $rc]} {set rc SQLITE_IOERR}
688 if {$rc != "SQLITE_OK"} { error [sqlite3_test_errstr $rc] }
689 set {} {}
690} -test {
691 faultsim_test_result {0 {}}
692}
dand0b0d4d2010-07-01 19:01:56 +0000693do_faultsim_test pagerfault-14c -prep {
shanehcaace922010-07-08 13:33:47 +0000694 catch { db2 close }
dand0b0d4d2010-07-01 19:01:56 +0000695 faultsim_restore_and_reopen
696 sqlite3 db2 test.db2
697 db2 eval {
698 PRAGMA synchronous = off;
699 PRAGMA page_size = 4096;
700 CREATE TABLE xx(a);
701 }
702} -body {
703 sqlite3_backup B db2 main db main
704 B step 200
705 set rc [B finish]
706 if {[string match SQLITE_IOERR_* $rc]} {set rc SQLITE_IOERR}
707 if {$rc != "SQLITE_OK"} { error [sqlite3_test_errstr $rc] }
708 set {} {}
709} -test {
710 faultsim_test_result {0 {}}
711}
danc8ce3972010-06-29 10:30:23 +0000712
danba3cbf32010-06-30 04:29:03 +0000713do_test pagerfault-15-pre1 {
714 faultsim_delete_and_reopen
715 db func a_string a_string;
716 execsql {
717 BEGIN;
718 CREATE TABLE t1(x, y UNIQUE);
719 INSERT INTO t1 VALUES(a_string(11), a_string(22));
720 INSERT INTO t1 VALUES(a_string(11), a_string(22));
721 COMMIT;
722 }
723 faultsim_save_and_close
724} {}
725do_faultsim_test pagerfault-15 -prep {
726 faultsim_restore_and_reopen
727 db func a_string a_string;
728} -body {
729 db eval { SELECT * FROM t1 LIMIT 1 } {
730 execsql {
731 BEGIN; INSERT INTO t1 VALUES(a_string(333), a_string(555)); COMMIT;
732 BEGIN; INSERT INTO t1 VALUES(a_string(333), a_string(555)); COMMIT;
733 }
734 }
735} -test {
736 faultsim_test_result {0 {}}
737 faultsim_integrity_check
738}
739
danba3cbf32010-06-30 04:29:03 +0000740
741do_test pagerfault-16-pre1 {
742 faultsim_delete_and_reopen
743 execsql { CREATE TABLE t1(x, y UNIQUE) }
744 faultsim_save_and_close
745} {}
746do_faultsim_test pagerfault-16 -prep {
747 faultsim_restore_and_reopen
748} -body {
749 execsql {
750 PRAGMA locking_mode = exclusive;
751 PRAGMA journal_mode = wal;
752 INSERT INTO t1 VALUES(1, 2);
753 INSERT INTO t1 VALUES(3, 4);
754 PRAGMA journal_mode = delete;
755 INSERT INTO t1 VALUES(4, 5);
756 PRAGMA journal_mode = wal;
757 INSERT INTO t1 VALUES(6, 7);
758 PRAGMA journal_mode = persist;
759 INSERT INTO t1 VALUES(8, 9);
760 }
761} -test {
762 faultsim_test_result {0 {exclusive wal delete wal persist}}
763 faultsim_integrity_check
764}
765
766
dan89ccf442010-07-01 15:09:47 +0000767#-------------------------------------------------------------------------
768# Test fault injection while changing into and out of WAL mode.
769#
770do_test pagerfault-17-pre1 {
771 faultsim_delete_and_reopen
772 execsql {
773 CREATE TABLE t1(a PRIMARY KEY, b);
774 INSERT INTO t1 VALUES(1862, 'Botha');
775 INSERT INTO t1 VALUES(1870, 'Smuts');
776 INSERT INTO t1 VALUES(1866, 'Hertzog');
777 }
778 faultsim_save_and_close
779} {}
780do_faultsim_test pagerfault-17a -prep {
781 faultsim_restore_and_reopen
782} -body {
783 execsql {
784 PRAGMA journal_mode = wal;
785 PRAGMA journal_mode = delete;
786 }
787} -test {
788 faultsim_test_result {0 {wal delete}}
789 faultsim_integrity_check
790}
791do_faultsim_test pagerfault-17b -prep {
792 faultsim_restore_and_reopen
793 execsql { PRAGMA synchronous = OFF }
794} -body {
795 execsql {
796 PRAGMA journal_mode = wal;
797 INSERT INTO t1 VALUES(22, 'Clarke');
798 PRAGMA journal_mode = delete;
799 }
800} -test {
801 faultsim_test_result {0 {wal delete}}
802 faultsim_integrity_check
803}
804do_faultsim_test pagerfault-17c -prep {
805 faultsim_restore_and_reopen
806 execsql {
807 PRAGMA locking_mode = exclusive;
808 PRAGMA journal_mode = wal;
809 }
810} -body {
811 execsql { PRAGMA journal_mode = delete }
812} -test {
813 faultsim_test_result {0 delete}
814 faultsim_integrity_check
815}
816do_faultsim_test pagerfault-17d -prep {
shanehcaace922010-07-08 13:33:47 +0000817 catch { db2 close }
dan89ccf442010-07-01 15:09:47 +0000818 faultsim_restore_and_reopen
819 sqlite3 db2 test.db
820 execsql { PRAGMA journal_mode = delete }
821 execsql { PRAGMA journal_mode = wal }
822 execsql { INSERT INTO t1 VALUES(99, 'Bradman') } db2
823} -body {
824 execsql { PRAGMA journal_mode = delete }
825} -test {
826 faultsim_test_result {1 {database is locked}}
827 faultsim_integrity_check
828}
829do_faultsim_test pagerfault-17e -prep {
shanehcaace922010-07-08 13:33:47 +0000830 catch { db2 close }
dan89ccf442010-07-01 15:09:47 +0000831 faultsim_restore_and_reopen
832 sqlite3 db2 test.db
833 execsql { PRAGMA journal_mode = delete }
834 execsql { PRAGMA journal_mode = wal }
835 set ::chan [launch_testfixture]
836 testfixture $::chan {
837 sqlite3 db test.db
838 db eval { INSERT INTO t1 VALUES(101, 'Latham') }
839 }
840 catch { testfixture $::chan sqlite_abort }
841 catch { close $::chan }
842} -body {
843 execsql { PRAGMA journal_mode = delete }
844} -test {
845 faultsim_test_result {0 delete}
846 faultsim_integrity_check
847}
848
849#-------------------------------------------------------------------------
850# Test fault-injection when changing from journal_mode=persist to
851# journal_mode=delete (this involves deleting the journal file).
852#
853do_test pagerfault-18-pre1 {
854 faultsim_delete_and_reopen
855 execsql {
856 CREATE TABLE qq(x);
857 INSERT INTO qq VALUES('Herbert');
858 INSERT INTO qq VALUES('Macalister');
859 INSERT INTO qq VALUES('Mackenzie');
860 INSERT INTO qq VALUES('Lilley');
861 INSERT INTO qq VALUES('Palmer');
862 }
863 faultsim_save_and_close
864} {}
865do_faultsim_test pagerfault-18 -prep {
866 faultsim_restore_and_reopen
867 execsql {
868 PRAGMA journal_mode = PERSIST;
869 INSERT INTO qq VALUES('Beatty');
870 }
871} -body {
872 execsql { PRAGMA journal_mode = delete }
873} -test {
874 faultsim_test_result {0 delete}
875 faultsim_integrity_check
876}
877
dan89ccf442010-07-01 15:09:47 +0000878do_faultsim_test pagerfault-19a -prep {
879 sqlite3 db :memory:
880 db func a_string a_string
881 execsql {
882 PRAGMA auto_vacuum = FULL;
883 BEGIN;
884 CREATE TABLE t1(a, b);
885 INSERT INTO t1 VALUES(a_string(5000), a_string(6000));
886 COMMIT;
887 }
888} -body {
889 execsql {
890 CREATE TABLE t2(a, b);
891 INSERT INTO t2 SELECT * FROM t1;
892 DELETE FROM t1;
893 }
894} -test {
895 faultsim_test_result {0 {}}
896}
897
898do_test pagerfault-19-pre1 {
899 faultsim_delete_and_reopen
900 execsql {
901 PRAGMA auto_vacuum = FULL;
902 CREATE TABLE t1(x); INSERT INTO t1 VALUES(1);
903 CREATE TABLE t2(x); INSERT INTO t2 VALUES(2);
904 CREATE TABLE t3(x); INSERT INTO t3 VALUES(3);
905 CREATE TABLE t4(x); INSERT INTO t4 VALUES(4);
906 CREATE TABLE t5(x); INSERT INTO t5 VALUES(5);
907 CREATE TABLE t6(x); INSERT INTO t6 VALUES(6);
908 }
909 faultsim_save_and_close
910} {}
911do_faultsim_test pagerfault-19b -prep {
912 faultsim_restore_and_reopen
913} -body {
914 execsql {
915 BEGIN;
916 UPDATE t4 SET x = x+1;
917 UPDATE t6 SET x = x+1;
918 SAVEPOINT one;
919 UPDATE t3 SET x = x+1;
920 SAVEPOINT two;
921 DROP TABLE t2;
922 ROLLBACK TO one;
923 COMMIT;
924 SELECT * FROM t3;
925 SELECT * FROM t4;
926 SELECT * FROM t6;
927 }
928} -test {
929 faultsim_test_result {0 {3 5 7}}
930}
931
dand0b0d4d2010-07-01 19:01:56 +0000932#-------------------------------------------------------------------------
933# This tests fault-injection in a special case in the auto-vacuum code.
934#
dan89ccf442010-07-01 15:09:47 +0000935do_test pagerfault-20-pre1 {
936 faultsim_delete_and_reopen
dan89ccf442010-07-01 15:09:47 +0000937 execsql {
dand0b0d4d2010-07-01 19:01:56 +0000938 PRAGMA cache_size = 10;
939 PRAGMA auto_vacuum = FULL;
940 CREATE TABLE t0(a, b);
dan89ccf442010-07-01 15:09:47 +0000941 }
942 faultsim_save_and_close
943} {}
944do_faultsim_test pagerfault-20 -prep {
945 faultsim_restore_and_reopen
dan89ccf442010-07-01 15:09:47 +0000946} -body {
947 execsql {
948 BEGIN;
dand0b0d4d2010-07-01 19:01:56 +0000949 CREATE TABLE t1(a, b);
950 CREATE TABLE t2(a, b);
951 DROP TABLE t1;
dan89ccf442010-07-01 15:09:47 +0000952 COMMIT;
953 }
954} -test {
955 faultsim_test_result {0 {}}
dan89ccf442010-07-01 15:09:47 +0000956}
dan273f3f02010-06-26 15:42:33 +0000957
dand0b0d4d2010-07-01 19:01:56 +0000958do_test pagerfault-21-pre1 {
959 faultsim_delete_and_reopen
960 execsql {
961 PRAGMA cache_size = 10;
962 CREATE TABLE t0(a PRIMARY KEY, b);
963 INSERT INTO t0 VALUES(1, 2);
964 }
965 faultsim_save_and_close
966} {}
967do_faultsim_test pagerfault-21 -prep {
968 faultsim_restore_and_reopen
969} -body {
970 db eval { SELECT * FROM t0 LIMIT 1 } {
971 db eval { INSERT INTO t0 SELECT a+1, b FROM t0 }
972 db eval { INSERT INTO t0 SELECT a+2, b FROM t0 }
973 }
974} -test {
975 faultsim_test_result {0 {}}
976}
977
dand0b0d4d2010-07-01 19:01:56 +0000978
dan9d08d642010-07-14 08:20:35 +0000979#-------------------------------------------------------------------------
980# Test fault-injection and rollback when the nReserve header value
981# is non-zero.
982#
983do_test pagerfault-21-pre1 {
984 faultsim_delete_and_reopen
985 execsql {
986 PRAGMA page_size = 1024;
987 PRAGMA journal_mode = WAL;
988 PRAGMA journal_mode = DELETE;
989 }
990 db close
991 hexio_write test.db 20 10
992 hexio_write test.db 105 03F0
993 sqlite3 db test.db
994 db func a_string a_string
995 execsql {
996 CREATE TABLE t0(a PRIMARY KEY, b UNIQUE);
997 INSERT INTO t0 VALUES(a_string(222), a_string(333));
998 INSERT INTO t0 VALUES(a_string(223), a_string(334));
999 INSERT INTO t0 VALUES(a_string(224), a_string(335));
1000 INSERT INTO t0 VALUES(a_string(225), a_string(336));
1001 }
1002 faultsim_save_and_close
1003} {}
1004
1005do_faultsim_test pagerfault-21 -prep {
1006 faultsim_restore_and_reopen
1007} -body {
1008 execsql { INSERT INTO t0 SELECT a||'x', b||'x' FROM t0 }
1009} -test {
1010 faultsim_test_result {0 {}}
1011 faultsim_integrity_check
1012}
1013ifcapable crashtest {
1014 faultsim_delete_and_reopen
1015 execsql {
1016 PRAGMA page_size = 1024;
1017 PRAGMA journal_mode = WAL;
1018 PRAGMA journal_mode = DELETE;
1019 }
1020 db close
1021 hexio_write test.db 20 10
1022 hexio_write test.db 105 03F0
1023
1024 sqlite3 db test.db
1025 db func a_string a_string
1026 execsql {
1027 CREATE TABLE t0(a PRIMARY KEY, b UNIQUE);
1028 INSERT INTO t0 VALUES(a_string(222), a_string(333));
1029 INSERT INTO t0 VALUES(a_string(223), a_string(334));
1030 }
1031 faultsim_save_and_close
1032
1033 for {set iTest 1} {$iTest<50} {incr iTest} {
1034 do_test pagerfault-21.crash.$iTest.1 {
1035 crashsql -delay 1 -file test.db -seed $iTest {
1036 BEGIN;
1037 CREATE TABLE t1(a PRIMARY KEY, b UNIQUE);
1038 INSERT INTO t1 SELECT a, b FROM t0;
1039 COMMIT;
1040 }
1041 } {1 {child process exited abnormally}}
1042 do_test pagerfault-22.$iTest.2 {
1043 sqlite3 db test.db
1044 execsql { PRAGMA integrity_check }
1045 } {ok}
1046 db close
1047 }
1048}
1049
dan59257dc2010-08-04 11:34:31 +00001050
1051#-------------------------------------------------------------------------
1052# When a 3.7.0 client opens a write-transaction on a database file that
1053# has been appended to or truncated by a pre-370 client, it updates
1054# the db-size in the file header immediately. This test case provokes
1055# errors during that operation.
1056#
1057do_test pagerfault-22-pre1 {
1058 faultsim_delete_and_reopen
1059 db func a_string a_string
1060 execsql {
1061 PRAGMA page_size = 1024;
1062 PRAGMA auto_vacuum = 0;
1063 CREATE TABLE t1(a);
1064 CREATE INDEX i1 ON t1(a);
1065 INSERT INTO t1 VALUES(a_string(3000));
1066 CREATE TABLE t2(a);
1067 INSERT INTO t2 VALUES(1);
1068 }
1069 db close
1070 sql36231 { INSERT INTO t1 VALUES(a_string(3000)) }
1071 faultsim_save_and_close
1072} {}
1073do_faultsim_test pagerfault-22 -prep {
1074 faultsim_restore_and_reopen
1075} -body {
1076 execsql { INSERT INTO t2 VALUES(2) }
1077 execsql { SELECT * FROM t2 }
1078} -test {
1079 faultsim_test_result {0 {1 2}}
1080 faultsim_integrity_check
1081}
1082
dan22b328b2010-08-11 18:56:45 +00001083#-------------------------------------------------------------------------
1084# Provoke an OOM error during a commit of multi-file transaction. One of
1085# the databases written during the transaction is an in-memory database.
1086# This test causes rollback of the in-memory database after CommitPhaseOne()
1087# has successfully returned. i.e. the series of calls for the aborted commit
1088# is:
1089#
1090# PagerCommitPhaseOne(<in-memory-db>) -> SQLITE_OK
1091# PagerCommitPhaseOne(<file-db>) -> SQLITE_IOERR
1092# PagerRollback(<in-memory-db>)
1093# PagerRollback(<file-db>)
1094#
1095do_faultsim_test pagerfault-23 -prep {
1096 foreach f [glob -nocomplain test.db*] { file delete -force $f }
1097 sqlite3 db :memory:
1098 db eval {
1099 ATTACH 'test.db2' AS aux;
1100 CREATE TABLE t1(a, b);
1101 CREATE TABLE aux.t2(a, b);
1102 }
1103} -body {
1104 execsql {
1105 BEGIN;
1106 INSERT INTO t1 VALUES(1,2);
1107 INSERT INTO t2 VALUES(3,4);
1108 COMMIT;
1109 }
1110} -test {
1111 faultsim_test_result {0 {}}
1112 faultsim_integrity_check
1113}
1114
1115do_faultsim_test pagerfault-24 -prep {
1116 faultsim_delete_and_reopen
1117 db eval { PRAGMA temp_store = file }
1118 execsql { CREATE TABLE x(a, b) }
1119} -body {
1120 execsql { CREATE TEMP TABLE t1(a, b) }
1121} -test {
dan5653e4d2010-08-12 11:25:47 +00001122 faultsim_test_result {0 {}} \
1123 {1 {unable to open a temporary database file for storing temporary tables}}
dan22b328b2010-08-11 18:56:45 +00001124 set ic [db eval { PRAGMA temp.integrity_check }]
1125 if {$ic != "ok"} { error "Integrity check: $ic" }
1126}
1127
dan5653e4d2010-08-12 11:25:47 +00001128}
1129
1130proc lockrows {n} {
1131 if {$n==0} { return "" }
1132 db eval { SELECT * FROM t1 WHERE oid = $n } {
1133 return [lockrows [expr {$n-1}]]
1134 }
1135}
1136
1137do_test pagerfault-25-pre1 {
1138 faultsim_delete_and_reopen
1139 db func a_string a_string
1140 execsql {
1141 PRAGMA page_size = 1024;
1142 PRAGMA auto_vacuum = 0;
1143 CREATE TABLE t1(a);
1144 INSERT INTO t1 VALUES(a_string(500));
1145 INSERT INTO t1 SELECT a_string(500) FROM t1;
1146 INSERT INTO t1 SELECT a_string(500) FROM t1;
1147 INSERT INTO t1 SELECT a_string(500) FROM t1;
1148 INSERT INTO t1 SELECT a_string(500) FROM t1;
1149 INSERT INTO t1 SELECT a_string(500) FROM t1;
1150 }
1151 faultsim_save_and_close
1152} {}
1153do_faultsim_test pagerfault-25 -faults full -prep {
1154 faultsim_restore_and_reopen
1155 db func a_string a_string
1156 set ::channel [db incrblob -readonly t1 a 1]
1157 execsql {
1158 PRAGMA cache_size = 10;
1159 BEGIN;
1160 INSERT INTO t1 VALUES(a_string(3000));
1161 INSERT INTO t1 VALUES(a_string(3000));
1162 }
1163} -body {
1164 lockrows 30
1165} -test {
1166 catch { lockrows 30 }
1167 close $::channel
1168 faultsim_test_result {0 {}}
1169}
dan22b328b2010-08-11 18:56:45 +00001170
1171
danb0ac3e32010-06-16 10:55:42 +00001172finish_test