blob: 39f97d505622edfe9444258a91d17f48f623197f [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
danb3f43512010-07-03 16:37:45 +000031
dan273f3f02010-06-26 15:42:33 +000032if 1 {
33
danb0ac3e32010-06-16 10:55:42 +000034#-------------------------------------------------------------------------
35# Test fault-injection while rolling back a hot-journal file.
36#
37do_test pagerfault-1-pre1 {
38 execsql {
39 PRAGMA journal_mode = DELETE;
40 PRAGMA cache_size = 10;
41 CREATE TABLE t1(a UNIQUE, b UNIQUE);
42 INSERT INTO t1 VALUES(a_string(200), a_string(300));
43 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
44 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
45 BEGIN;
46 INSERT INTO t1 SELECT a_string(201), a_string(301) FROM t1;
47 INSERT INTO t1 SELECT a_string(202), a_string(302) FROM t1;
48 INSERT INTO t1 SELECT a_string(203), a_string(303) FROM t1;
49 INSERT INTO t1 SELECT a_string(204), a_string(304) FROM t1;
50 }
51 faultsim_save_and_close
52} {}
53do_faultsim_test pagerfault-1 -prep {
54 faultsim_restore_and_reopen
55} -body {
56 execsql { SELECT count(*) FROM t1 }
57} -test {
58 faultsim_test_result {0 4}
59 faultsim_integrity_check
60 if {[db one { SELECT count(*) FROM t1 }] != 4} {
61 error "Database content appears incorrect"
62 }
63}
64
65#-------------------------------------------------------------------------
dande4996e2010-06-19 11:30:41 +000066# Test fault-injection while rolling back a hot-journal file with a
67# page-size different from the current value stored on page 1 of the
68# database file.
69#
70do_test pagerfault-2-pre1 {
71 testvfs tv -default 1
72 tv filter xSync
73 tv script xSyncCb
74 proc xSyncCb {filename args} {
75 if {[string match *journal filename]==0} faultsim_save
76 }
77 faultsim_delete_and_reopen
78 execsql {
79 PRAGMA page_size = 4096;
80 BEGIN;
81 CREATE TABLE abc(a, b, c);
82 INSERT INTO abc VALUES('o', 't', 't');
83 INSERT INTO abc VALUES('f', 'f', 's');
84 INSERT INTO abc SELECT * FROM abc; -- 4
85 INSERT INTO abc SELECT * FROM abc; -- 8
86 INSERT INTO abc SELECT * FROM abc; -- 16
87 INSERT INTO abc SELECT * FROM abc; -- 32
88 INSERT INTO abc SELECT * FROM abc; -- 64
89 INSERT INTO abc SELECT * FROM abc; -- 128
90 INSERT INTO abc SELECT * FROM abc; -- 256
91 COMMIT;
92 PRAGMA page_size = 1024;
93 VACUUM;
94 }
95 db close
96 tv delete
97} {}
98do_faultsim_test pagerfault-2 -prep {
99 faultsim_restore_and_reopen
100} -body {
101 execsql { SELECT * FROM abc }
102} -test {
103 set answer [split [string repeat "ottffs" 128] ""]
104 faultsim_test_result [list 0 $answer]
105 faultsim_integrity_check
106 set res [db eval { SELECT * FROM abc }]
107 if {$res != $answer} { error "Database content appears incorrect ($res)" }
danec6ffc12010-06-24 19:16:06 +0000108}
dande4996e2010-06-19 11:30:41 +0000109
110#-------------------------------------------------------------------------
danb0ac3e32010-06-16 10:55:42 +0000111# Test fault-injection while rolling back hot-journals that were created
112# as part of a multi-file transaction.
113#
dande4996e2010-06-19 11:30:41 +0000114do_test pagerfault-3-pre1 {
danb0ac3e32010-06-16 10:55:42 +0000115 testvfs tstvfs -default 1
116 tstvfs filter xDelete
117 tstvfs script xDeleteCallback
118
119 proc xDeleteCallback {method file args} {
120 set file [file tail $file]
121 if { [string match *mj* $file] } { faultsim_save }
122 }
123
124 faultsim_delete_and_reopen
125 db func a_string a_string
126
127 execsql {
128 ATTACH 'test.db2' AS aux;
129 PRAGMA journal_mode = DELETE;
130 PRAGMA main.cache_size = 10;
131 PRAGMA aux.cache_size = 10;
132
133 CREATE TABLE t1(a UNIQUE, b UNIQUE);
134 CREATE TABLE aux.t2(a UNIQUE, b UNIQUE);
135 INSERT INTO t1 VALUES(a_string(200), a_string(300));
136 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
137 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
138 INSERT INTO t2 SELECT * FROM t1;
139
140 BEGIN;
141 INSERT INTO t1 SELECT a_string(201), a_string(301) FROM t1;
142 INSERT INTO t1 SELECT a_string(202), a_string(302) FROM t1;
143 INSERT INTO t1 SELECT a_string(203), a_string(303) FROM t1;
144 INSERT INTO t1 SELECT a_string(204), a_string(304) FROM t1;
145 REPLACE INTO t2 SELECT * FROM t1;
146 COMMIT;
147 }
148
149 db close
150 tstvfs delete
151} {}
danec6ffc12010-06-24 19:16:06 +0000152do_faultsim_test pagerfault-3 -prep {
danb0ac3e32010-06-16 10:55:42 +0000153 faultsim_restore_and_reopen
154} -body {
155 execsql {
156 ATTACH 'test.db2' AS aux;
157 SELECT count(*) FROM t2;
158 SELECT count(*) FROM t1;
159 }
160} -test {
161 faultsim_test_result {0 {4 4}} {1 {unable to open database: test.db2}}
162 faultsim_integrity_check
danb0ac3e32010-06-16 10:55:42 +0000163 catchsql { ATTACH 'test.db2' AS aux }
164 if {[db one { SELECT count(*) FROM t1 }] != 4
165 || [db one { SELECT count(*) FROM t2 }] != 4
166 } {
167 error "Database content appears incorrect"
168 }
169}
170
dande4996e2010-06-19 11:30:41 +0000171#-------------------------------------------------------------------------
172# Test fault-injection as part of a vanilla, no-transaction, INSERT
173# statement.
174#
175do_faultsim_test pagerfault-4 -prep {
176 faultsim_delete_and_reopen
177} -body {
178 execsql {
179 CREATE TABLE x(y);
180 INSERT INTO x VALUES('z');
181 SELECT * FROM x;
182 }
183} -test {
184 faultsim_test_result {0 z}
185 faultsim_integrity_check
186}
187
188#-------------------------------------------------------------------------
189# Test fault-injection as part of a commit when using journal_mode=PERSIST.
dan146ed782010-06-19 17:26:37 +0000190# Three different cases:
191#
192# pagerfault-5.1: With no journal_size_limit configured.
193# pagerfault-5.2: With a journal_size_limit configured.
194# pagerfault-5.4: Multi-file transaction. One connection has a
195# journal_size_limit of 0, the other has no limit.
dande4996e2010-06-19 11:30:41 +0000196#
197do_test pagerfault-5-pre1 {
198 faultsim_delete_and_reopen
199 db func a_string a_string
200 execsql {
201 CREATE TABLE t1(a UNIQUE, b UNIQUE);
202 INSERT INTO t1 VALUES(a_string(200), a_string(300));
203 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
204 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
205 }
206 faultsim_save_and_close
207} {}
208do_faultsim_test pagerfault-5.1 -prep {
209 faultsim_restore_and_reopen
210 db func a_string a_string
211 execsql { PRAGMA journal_mode = PERSIST }
212} -body {
213 execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
214} -test {
215 faultsim_test_result {0 {}}
216 faultsim_integrity_check
217}
218do_faultsim_test pagerfault-5.2 -prep {
219 faultsim_restore_and_reopen
220 db func a_string a_string
221 execsql {
222 PRAGMA journal_mode = PERSIST;
223 PRAGMA journal_size_limit = 2048;
224 }
225} -body {
226 execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
227} -test {
228 faultsim_test_result {0 {}}
229 faultsim_integrity_check
230}
danec6ffc12010-06-24 19:16:06 +0000231do_faultsim_test pagerfault-5.3 -faults oom-transient -prep {
dande4996e2010-06-19 11:30:41 +0000232 faultsim_restore_and_reopen
233 db func a_string a_string
234 file delete -force test2.db test2.db-journal test2.db-wal
235 execsql {
236 PRAGMA journal_mode = PERSIST;
237 ATTACH 'test2.db' AS aux;
238 PRAGMA aux.journal_mode = PERSIST;
239 PRAGMA aux.journal_size_limit = 0;
240 }
241} -body {
242 execsql {
243 BEGIN;
244 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
245 CREATE TABLE aux.t2 AS SELECT * FROM t1;
246 COMMIT;
247 }
248} -test {
249 faultsim_test_result {0 {}}
danec6ffc12010-06-24 19:16:06 +0000250
danf43d7fc2010-07-03 10:00:00 +0000251 catchsql { COMMIT }
252 catchsql { ROLLBACK }
253
254 faultsim_integrity_check
danec6ffc12010-06-24 19:16:06 +0000255 set res ""
256 set rc [catch { set res [db one { PRAGMA aux.integrity_check }] }]
257 if {$rc!=0 || $res != "ok"} {error "integrity-check problem:$rc $res"}
dande4996e2010-06-19 11:30:41 +0000258}
259
dan153eda02010-06-21 07:45:47 +0000260#-------------------------------------------------------------------------
261# Test fault-injection as part of a commit when using
262# journal_mode=TRUNCATE.
263#
264do_test pagerfault-6-pre1 {
265 faultsim_delete_and_reopen
266 db func a_string a_string
267 execsql {
268 CREATE TABLE t1(a UNIQUE, b UNIQUE);
269 INSERT INTO t1 VALUES(a_string(200), a_string(300));
270 }
271 faultsim_save_and_close
272} {}
danf9b44192010-06-25 19:09:48 +0000273
dan153eda02010-06-21 07:45:47 +0000274do_faultsim_test pagerfault-6.1 -prep {
275 faultsim_restore_and_reopen
276 db func a_string a_string
277 execsql { PRAGMA journal_mode = TRUNCATE }
278} -body {
279 execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
danf9b44192010-06-25 19:09:48 +0000280 execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
281} -test {
282 faultsim_test_result {0 {}}
283 faultsim_integrity_check
284}
285
286# The unix vfs xAccess() method considers a file zero bytes in size to
287# "not exist". This proc overrides that behaviour so that a zero length
288# file is considered to exist.
289#
290proc xAccess {method filename op args} {
291 if {$op != "SQLITE_ACCESS_EXISTS"} { return "" }
292 return [file exists $filename]
293}
294do_faultsim_test pagerfault-6.2 -faults cantopen-* -prep {
295 shmfault filter xAccess
296 shmfault script xAccess
297
298 faultsim_restore_and_reopen
299 db func a_string a_string
300 execsql { PRAGMA journal_mode = TRUNCATE }
301} -body {
302 execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
303 execsql { INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1 }
dan153eda02010-06-21 07:45:47 +0000304} -test {
305 faultsim_test_result {0 {}}
306 faultsim_integrity_check
307}
308
dan146ed782010-06-19 17:26:37 +0000309# The following was an attempt to get a bitvec malloc to fail. Didn't work.
310#
311# do_test pagerfault-6-pre1 {
312# faultsim_delete_and_reopen
313# execsql {
314# CREATE TABLE t1(x, y, UNIQUE(x, y));
315# INSERT INTO t1 VALUES(1, randomblob(1501));
316# INSERT INTO t1 VALUES(2, randomblob(1502));
317# INSERT INTO t1 VALUES(3, randomblob(1503));
318# INSERT INTO t1 VALUES(4, randomblob(1504));
319# INSERT INTO t1
320# SELECT x, randomblob(1500+oid+(SELECT max(oid) FROM t1)) FROM t1;
321# INSERT INTO t1
322# SELECT x, randomblob(1500+oid+(SELECT max(oid) FROM t1)) FROM t1;
323# INSERT INTO t1
324# SELECT x, randomblob(1500+oid+(SELECT max(oid) FROM t1)) FROM t1;
325# INSERT INTO t1
326# SELECT x, randomblob(1500+oid+(SELECT max(oid) FROM t1)) FROM t1;
327# }
328# faultsim_save_and_close
329# } {}
330# do_faultsim_test pagerfault-6 -prep {
331# faultsim_restore_and_reopen
332# } -body {
333# execsql {
334# BEGIN;
335# UPDATE t1 SET x=x+4 WHERE x=1;
336# SAVEPOINT one;
337# UPDATE t1 SET x=x+4 WHERE x=2;
338# SAVEPOINT three;
339# UPDATE t1 SET x=x+4 WHERE x=3;
340# SAVEPOINT four;
341# UPDATE t1 SET x=x+4 WHERE x=4;
342# RELEASE three;
343# COMMIT;
344# SELECT DISTINCT x FROM t1;
345# }
346# } -test {
347# faultsim_test_result {0 {5 6 7 8}}
348# faultsim_integrity_check
349# }
dan346e4262010-06-23 19:27:36 +0000350#
dandca321a2010-06-24 10:50:17 +0000351
352# This is designed to provoke a special case in the pager code:
353#
354# If an error (specifically, a FULL or IOERR error) occurs while writing a
355# dirty page to the file-system in order to free up memory, the pager enters
356# the "error state". An IO error causes SQLite to roll back the current
357# transaction (exiting the error state). A FULL error, however, may only
358# rollback the current statement.
359#
360# This block tests that nothing goes wrong if a FULL error occurs while
361# writing a dirty page out to free memory from within a statement that has
362# opened a statement transaction.
363#
dan346e4262010-06-23 19:27:36 +0000364do_test pagerfault-7-pre1 {
365 faultsim_delete_and_reopen
366 execsql {
367 CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
368 BEGIN;
369 INSERT INTO t2 VALUES(NULL, randomblob(1500));
370 INSERT INTO t2 VALUES(NULL, randomblob(1500));
371 INSERT INTO t2 SELECT NULL, randomblob(1500) FROM t2; -- 4
372 INSERT INTO t2 SELECT NULL, randomblob(1500) FROM t2; -- 8
373 INSERT INTO t2 SELECT NULL, randomblob(1500) FROM t2; -- 16
374 INSERT INTO t2 SELECT NULL, randomblob(1500) FROM t2; -- 32
375 INSERT INTO t2 SELECT NULL, randomblob(1500) FROM t2; -- 64
376 COMMIT;
377 CREATE TABLE t1(a PRIMARY KEY, b);
378 INSERT INTO t1 SELECT * FROM t2;
379 DROP TABLE t2;
380 }
381 faultsim_save_and_close
382} {}
danec6ffc12010-06-24 19:16:06 +0000383do_faultsim_test pagerfault-7 -prep {
dan346e4262010-06-23 19:27:36 +0000384 faultsim_restore_and_reopen
385 execsql {
386 PRAGMA cache_size = 10;
387 BEGIN;
388 UPDATE t1 SET b = randomblob(1500);
389 }
390} -body {
391 execsql { UPDATE t1 SET a = 65, b = randomblob(1500) WHERE (a+1)>200 }
392 execsql COMMIT
393} -test {
394 faultsim_test_result {0 {}}
395 faultsim_integrity_check
396}
dan146ed782010-06-19 17:26:37 +0000397
dandca321a2010-06-24 10:50:17 +0000398do_test pagerfault-8-pre1 {
399 faultsim_delete_and_reopen
400 execsql {
401 PRAGMA auto_vacuum = 1;
402 CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
403 BEGIN;
404 INSERT INTO t1 VALUES(NULL, randomblob(1500));
405 INSERT INTO t1 VALUES(NULL, randomblob(1500));
406 INSERT INTO t1 SELECT NULL, randomblob(1500) FROM t1; -- 4
407 INSERT INTO t1 SELECT NULL, randomblob(1500) FROM t1; -- 8
408 INSERT INTO t1 SELECT NULL, randomblob(1500) FROM t1; -- 16
409 INSERT INTO t1 SELECT NULL, randomblob(1500) FROM t1; -- 32
410 INSERT INTO t1 SELECT NULL, randomblob(1500) FROM t1; -- 64
411 COMMIT;
412 }
413 faultsim_save_and_close
414 set filesize [file size test.db]
415 set {} {}
416} {}
417do_test pagerfault-8-pre2 {
418 faultsim_restore_and_reopen
419 execsql { DELETE FROM t1 WHERE a>32 }
420 expr {[file size test.db] < $filesize}
421} {1}
dandca321a2010-06-24 10:50:17 +0000422do_faultsim_test pagerfault-8 -prep {
423 faultsim_restore_and_reopen
424 execsql {
425 BEGIN;
426 DELETE FROM t1 WHERE a>32;
427 }
428} -body {
429 execsql COMMIT
430} -test {
431 faultsim_test_result {0 {}}
432 faultsim_integrity_check
433}
434
dan273f3f02010-06-26 15:42:33 +0000435#-------------------------------------------------------------------------
436# This test case is specially designed so that during a savepoint
437# rollback, a new cache entry must be allocated (see comments surrounding
438# the call to sqlite3PagerAcquire() from within pager_playback_one_page()
439# for details). Test the effects of injecting an OOM at this point.
440#
dan0a6052e2010-06-24 13:24:26 +0000441do_test pagerfault-9-pre1 {
442 faultsim_delete_and_reopen
443 execsql {
444 PRAGMA auto_vacuum = incremental;
445 CREATE TABLE t1(x);
446 CREATE TABLE t2(y);
447 CREATE TABLE t3(z);
448
449 INSERT INTO t1 VALUES(randomblob(900));
450 INSERT INTO t1 VALUES(randomblob(900));
451 DELETE FROM t1;
452 }
453 faultsim_save_and_close
454} {}
dan273f3f02010-06-26 15:42:33 +0000455do_faultsim_test pagerfault-9.1 -prep {
dan0a6052e2010-06-24 13:24:26 +0000456 faultsim_restore_and_reopen
457 execsql {
458 BEGIN;
459 INSERT INTO t1 VALUES(randomblob(900));
460 INSERT INTO t1 VALUES(randomblob(900));
461 DROP TABLE t3;
462 DROP TABLE t2;
463 SAVEPOINT abc;
464 PRAGMA incremental_vacuum;
465 }
466} -body {
467 execsql {
468 ROLLBACK TO abc;
469 COMMIT;
470 PRAGMA freelist_count
471 }
472} -test {
473 faultsim_test_result {0 2}
474 faultsim_integrity_check
475
476 set sl [db one { SELECT COALESCE(sum(length(x)), 'null') FROM t1 }]
477 if {$sl!="null" && $sl!=1800} {
478 error "Content looks no good... ($sl)"
479 }
480}
481
dan273f3f02010-06-26 15:42:33 +0000482#-------------------------------------------------------------------------
483# Test fault injection with a temporary database file.
484#
danc8ce3972010-06-29 10:30:23 +0000485foreach v {a b} {
486 do_faultsim_test pagerfault-10$v -prep {
487 sqlite3 db ""
488 db func a_string a_string;
489 execsql {
490 PRAGMA cache_size = 10;
491 BEGIN;
492 CREATE TABLE xx(a, b, UNIQUE(a, b));
493 INSERT INTO xx VALUES(a_string(200), a_string(200));
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 INSERT INTO xx SELECT a_string(200), a_string(200) FROM xx;
498 COMMIT;
499 }
500 } -body {
501 execsql { UPDATE xx SET a = a_string(300) }
502 } -test {
503 faultsim_test_result {0 {}}
504 if {$::v == "b"} { execsql { PRAGMA journal_mode = TRUNCATE } }
505 faultsim_integrity_check
506 faultsim_integrity_check
dan273f3f02010-06-26 15:42:33 +0000507 }
dan273f3f02010-06-26 15:42:33 +0000508}
509
dan273f3f02010-06-26 15:42:33 +0000510#-------------------------------------------------------------------------
511# Test fault injection with transaction savepoints (savepoints created
512# when a SAVEPOINT command is executed outside of any other savepoint
513# or transaction context).
514#
515do_test pagerfault-9-pre1 {
516 faultsim_delete_and_reopen
517 db func a_string a_string;
518 execsql {
519 PRAGMA auto_vacuum = on;
520 CREATE TABLE t1(x UNIQUE);
521 CREATE TABLE t2(y UNIQUE);
522 CREATE TABLE t3(z UNIQUE);
523 BEGIN;
524 INSERT INTO t1 VALUES(a_string(202));
525 INSERT INTO t2 VALUES(a_string(203));
526 INSERT INTO t3 VALUES(a_string(204));
527 INSERT INTO t1 SELECT a_string(202) FROM t1;
528 INSERT INTO t1 SELECT a_string(203) FROM t1;
529 INSERT INTO t1 SELECT a_string(204) FROM t1;
530 INSERT INTO t1 SELECT a_string(205) FROM t1;
531 INSERT INTO t2 SELECT a_string(length(x)) FROM t1;
532 INSERT INTO t3 SELECT a_string(length(x)) FROM t1;
533 COMMIT;
534 }
535 faultsim_save_and_close
536} {}
537do_faultsim_test pagerfault-11 -prep {
538 faultsim_restore_and_reopen
539 execsql { PRAGMA cache_size = 10 }
540} -body {
541 execsql {
542 SAVEPOINT trans;
543 UPDATE t2 SET y = y||'2';
544 INSERT INTO t3 SELECT * FROM t2;
545 DELETE FROM t1;
546 ROLLBACK TO trans;
547 UPDATE t1 SET x = x||'3';
548 INSERT INTO t2 SELECT * FROM t1;
549 DELETE FROM t3;
550 RELEASE trans;
551 }
552} -test {
553 faultsim_test_result {0 {}}
554 faultsim_integrity_check
555}
556
danc396d4a2010-07-02 11:27:43 +0000557}
558
559
danc8ce3972010-06-29 10:30:23 +0000560#-------------------------------------------------------------------------
561# Test fault injection when writing to a database file that resides on
562# a file-system with a sector-size larger than the database page-size.
563#
dand3533312010-06-28 19:04:02 +0000564do_test pagerfault-12-pre1 {
565 testvfs ss_layer -default 1
566 ss_layer sectorsize 4096
567 faultsim_delete_and_reopen
568 db func a_string a_string;
569
570 execsql {
571 PRAGMA page_size = 1024;
572 PRAGMA journal_mode = PERSIST;
573 PRAGMA cache_size = 10;
574 BEGIN;
575 CREATE TABLE t1(x, y UNIQUE);
576 INSERT INTO t1 VALUES(a_string(333), a_string(444));
577 INSERT INTO t1 SELECT a_string(333+rowid), a_string(444+rowid) FROM t1;
578 INSERT INTO t1 SELECT a_string(333+rowid), a_string(444+rowid) FROM t1;
579 INSERT INTO t1 SELECT a_string(333+rowid), a_string(444+rowid) FROM t1;
580 INSERT INTO t1 SELECT a_string(333+rowid), a_string(444+rowid) FROM t1;
581 INSERT INTO t1 SELECT a_string(44), a_string(55) FROM t1 LIMIT 13;
582 COMMIT;
583 }
584 faultsim_save_and_close
585} {}
danc396d4a2010-07-02 11:27:43 +0000586
587do_faultsim_test pagerfault-12a -prep {
dand3533312010-06-28 19:04:02 +0000588 faultsim_restore_and_reopen
589 execsql { PRAGMA cache_size = 10 }
590 db func a_string a_string;
591} -body {
592 execsql {
593 UPDATE t1 SET x = a_string(length(x)), y = a_string(length(y));
594 }
595} -test {
596 faultsim_test_result {0 {}}
597 faultsim_integrity_check
598}
599
danc396d4a2010-07-02 11:27:43 +0000600do_test pagerfault-12-pre2 {
601 faultsim_restore_and_reopen
602 execsql {
603 CREATE TABLE t2 AS SELECT * FROM t1 LIMIT 10;
604 }
605 faultsim_save_and_close
606} {}
607do_faultsim_test pagerfault-12b -prep {
608 faultsim_restore_and_reopen
609 db func a_string a_string;
610 execsql { SELECT * FROM t1 }
611} -body {
612 set sql(1) { UPDATE t2 SET x = a_string(280) }
613 set sql(2) { UPDATE t1 SET x = a_string(280) WHERE rowid = 5 }
614
615 db eval { SELECT rowid FROM t1 LIMIT 2 } { db eval $sql($rowid) }
616
617} -test {
618 faultsim_test_result {0 {}}
619 faultsim_integrity_check
620}
621
622catch { db close }
623ss_layer delete
624
dand3533312010-06-28 19:04:02 +0000625
danc8ce3972010-06-29 10:30:23 +0000626#-------------------------------------------------------------------------
danba3cbf32010-06-30 04:29:03 +0000627# Test fault injection when SQLite opens a database where the size of the
628# database file is zero bytes but the accompanying journal file is larger
629# than that. In this scenario SQLite should delete the journal file
630# without rolling it back, even if it is in all other respects a valid
631# hot-journal file.
danc8ce3972010-06-29 10:30:23 +0000632#
633do_test pagerfault-13-pre1 {
634 faultsim_delete_and_reopen
635 db func a_string a_string;
636 execsql {
637 PRAGMA journal_mode = PERSIST;
638 BEGIN;
639 CREATE TABLE t1(x, y UNIQUE);
640 INSERT INTO t1 VALUES(a_string(333), a_string(444));
641 COMMIT;
642 }
643 db close
644 file delete -force test.db
645 faultsim_save
646} {}
647do_faultsim_test pagerfault-13 -prep {
648 faultsim_restore_and_reopen
649} -body {
650 execsql { CREATE TABLE xx(a, b) }
651} -test {
652 faultsim_test_result {0 {}}
653}
654
danba3cbf32010-06-30 04:29:03 +0000655#---------------------------------------------------------------------------
656# Test fault injection into a small backup operation.
657#
658do_test pagerfault-14-pre1 {
659 faultsim_delete_and_reopen
660 db func a_string a_string;
661 execsql {
662 PRAGMA journal_mode = PERSIST;
663 ATTACH 'test.db2' AS two;
664 BEGIN;
665 CREATE TABLE t1(x, y UNIQUE);
666 CREATE TABLE two.t2(x, y UNIQUE);
667 INSERT INTO t1 VALUES(a_string(333), a_string(444));
668 INSERT INTO t2 VALUES(a_string(333), a_string(444));
669 COMMIT;
670 }
671 faultsim_save_and_close
672} {}
dan6b63ab42010-06-30 10:36:18 +0000673
674do_faultsim_test pagerfault-14a -prep {
danba3cbf32010-06-30 04:29:03 +0000675 faultsim_restore_and_reopen
676} -body {
677 if {[catch {db backup test.db2} msg]} { error [regsub {.*: } $msg {}] }
678} -test {
679 faultsim_test_result {0 {}} {1 {}} {1 {SQL logic error or missing database}}
danc8ce3972010-06-29 10:30:23 +0000680}
dan6b63ab42010-06-30 10:36:18 +0000681do_faultsim_test pagerfault-14b -prep {
682 faultsim_restore_and_reopen
683 sqlite3 db2 ""
684 db2 eval { PRAGMA page_size = 4096; CREATE TABLE xx(a) }
685} -body {
686 sqlite3_backup B db2 main db main
687 B step 200
688 set rc [B finish]
689 if {[string match SQLITE_IOERR_* $rc]} {set rc SQLITE_IOERR}
690 if {$rc != "SQLITE_OK"} { error [sqlite3_test_errstr $rc] }
691 set {} {}
692} -test {
693 faultsim_test_result {0 {}}
694}
dand0b0d4d2010-07-01 19:01:56 +0000695do_faultsim_test pagerfault-14c -prep {
696 faultsim_restore_and_reopen
697 sqlite3 db2 test.db2
698 db2 eval {
699 PRAGMA synchronous = off;
700 PRAGMA page_size = 4096;
701 CREATE TABLE xx(a);
702 }
703} -body {
704 sqlite3_backup B db2 main db main
705 B step 200
706 set rc [B finish]
707 if {[string match SQLITE_IOERR_* $rc]} {set rc SQLITE_IOERR}
708 if {$rc != "SQLITE_OK"} { error [sqlite3_test_errstr $rc] }
709 set {} {}
710} -test {
711 faultsim_test_result {0 {}}
712}
danc8ce3972010-06-29 10:30:23 +0000713
danba3cbf32010-06-30 04:29:03 +0000714do_test pagerfault-15-pre1 {
715 faultsim_delete_and_reopen
716 db func a_string a_string;
717 execsql {
718 BEGIN;
719 CREATE TABLE t1(x, y UNIQUE);
720 INSERT INTO t1 VALUES(a_string(11), a_string(22));
721 INSERT INTO t1 VALUES(a_string(11), a_string(22));
722 COMMIT;
723 }
724 faultsim_save_and_close
725} {}
726do_faultsim_test pagerfault-15 -prep {
727 faultsim_restore_and_reopen
728 db func a_string a_string;
729} -body {
730 db eval { SELECT * FROM t1 LIMIT 1 } {
731 execsql {
732 BEGIN; INSERT INTO t1 VALUES(a_string(333), a_string(555)); COMMIT;
733 BEGIN; INSERT INTO t1 VALUES(a_string(333), a_string(555)); COMMIT;
734 }
735 }
736} -test {
737 faultsim_test_result {0 {}}
738 faultsim_integrity_check
739}
740
danba3cbf32010-06-30 04:29:03 +0000741
742do_test pagerfault-16-pre1 {
743 faultsim_delete_and_reopen
744 execsql { CREATE TABLE t1(x, y UNIQUE) }
745 faultsim_save_and_close
746} {}
747do_faultsim_test pagerfault-16 -prep {
748 faultsim_restore_and_reopen
749} -body {
750 execsql {
751 PRAGMA locking_mode = exclusive;
752 PRAGMA journal_mode = wal;
753 INSERT INTO t1 VALUES(1, 2);
754 INSERT INTO t1 VALUES(3, 4);
755 PRAGMA journal_mode = delete;
756 INSERT INTO t1 VALUES(4, 5);
757 PRAGMA journal_mode = wal;
758 INSERT INTO t1 VALUES(6, 7);
759 PRAGMA journal_mode = persist;
760 INSERT INTO t1 VALUES(8, 9);
761 }
762} -test {
763 faultsim_test_result {0 {exclusive wal delete wal persist}}
764 faultsim_integrity_check
765}
766
767
dan89ccf442010-07-01 15:09:47 +0000768#-------------------------------------------------------------------------
769# Test fault injection while changing into and out of WAL mode.
770#
771do_test pagerfault-17-pre1 {
772 faultsim_delete_and_reopen
773 execsql {
774 CREATE TABLE t1(a PRIMARY KEY, b);
775 INSERT INTO t1 VALUES(1862, 'Botha');
776 INSERT INTO t1 VALUES(1870, 'Smuts');
777 INSERT INTO t1 VALUES(1866, 'Hertzog');
778 }
779 faultsim_save_and_close
780} {}
781do_faultsim_test pagerfault-17a -prep {
782 faultsim_restore_and_reopen
783} -body {
784 execsql {
785 PRAGMA journal_mode = wal;
786 PRAGMA journal_mode = delete;
787 }
788} -test {
789 faultsim_test_result {0 {wal delete}}
790 faultsim_integrity_check
791}
792do_faultsim_test pagerfault-17b -prep {
793 faultsim_restore_and_reopen
794 execsql { PRAGMA synchronous = OFF }
795} -body {
796 execsql {
797 PRAGMA journal_mode = wal;
798 INSERT INTO t1 VALUES(22, 'Clarke');
799 PRAGMA journal_mode = delete;
800 }
801} -test {
802 faultsim_test_result {0 {wal delete}}
803 faultsim_integrity_check
804}
805do_faultsim_test pagerfault-17c -prep {
806 faultsim_restore_and_reopen
807 execsql {
808 PRAGMA locking_mode = exclusive;
809 PRAGMA journal_mode = wal;
810 }
811} -body {
812 execsql { PRAGMA journal_mode = delete }
813} -test {
814 faultsim_test_result {0 delete}
815 faultsim_integrity_check
816}
817do_faultsim_test pagerfault-17d -prep {
818 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 {
830 faultsim_restore_and_reopen
831 sqlite3 db2 test.db
832 execsql { PRAGMA journal_mode = delete }
833 execsql { PRAGMA journal_mode = wal }
834 set ::chan [launch_testfixture]
835 testfixture $::chan {
836 sqlite3 db test.db
837 db eval { INSERT INTO t1 VALUES(101, 'Latham') }
838 }
839 catch { testfixture $::chan sqlite_abort }
840 catch { close $::chan }
841} -body {
842 execsql { PRAGMA journal_mode = delete }
843} -test {
844 faultsim_test_result {0 delete}
845 faultsim_integrity_check
846}
847
848#-------------------------------------------------------------------------
849# Test fault-injection when changing from journal_mode=persist to
850# journal_mode=delete (this involves deleting the journal file).
851#
852do_test pagerfault-18-pre1 {
853 faultsim_delete_and_reopen
854 execsql {
855 CREATE TABLE qq(x);
856 INSERT INTO qq VALUES('Herbert');
857 INSERT INTO qq VALUES('Macalister');
858 INSERT INTO qq VALUES('Mackenzie');
859 INSERT INTO qq VALUES('Lilley');
860 INSERT INTO qq VALUES('Palmer');
861 }
862 faultsim_save_and_close
863} {}
864do_faultsim_test pagerfault-18 -prep {
865 faultsim_restore_and_reopen
866 execsql {
867 PRAGMA journal_mode = PERSIST;
868 INSERT INTO qq VALUES('Beatty');
869 }
870} -body {
871 execsql { PRAGMA journal_mode = delete }
872} -test {
873 faultsim_test_result {0 delete}
874 faultsim_integrity_check
875}
876
dan89ccf442010-07-01 15:09:47 +0000877do_faultsim_test pagerfault-19a -prep {
878 sqlite3 db :memory:
879 db func a_string a_string
880 execsql {
881 PRAGMA auto_vacuum = FULL;
882 BEGIN;
883 CREATE TABLE t1(a, b);
884 INSERT INTO t1 VALUES(a_string(5000), a_string(6000));
885 COMMIT;
886 }
887} -body {
888 execsql {
889 CREATE TABLE t2(a, b);
890 INSERT INTO t2 SELECT * FROM t1;
891 DELETE FROM t1;
892 }
893} -test {
894 faultsim_test_result {0 {}}
895}
896
897do_test pagerfault-19-pre1 {
898 faultsim_delete_and_reopen
899 execsql {
900 PRAGMA auto_vacuum = FULL;
901 CREATE TABLE t1(x); INSERT INTO t1 VALUES(1);
902 CREATE TABLE t2(x); INSERT INTO t2 VALUES(2);
903 CREATE TABLE t3(x); INSERT INTO t3 VALUES(3);
904 CREATE TABLE t4(x); INSERT INTO t4 VALUES(4);
905 CREATE TABLE t5(x); INSERT INTO t5 VALUES(5);
906 CREATE TABLE t6(x); INSERT INTO t6 VALUES(6);
907 }
908 faultsim_save_and_close
909} {}
910do_faultsim_test pagerfault-19b -prep {
911 faultsim_restore_and_reopen
912} -body {
913 execsql {
914 BEGIN;
915 UPDATE t4 SET x = x+1;
916 UPDATE t6 SET x = x+1;
917 SAVEPOINT one;
918 UPDATE t3 SET x = x+1;
919 SAVEPOINT two;
920 DROP TABLE t2;
921 ROLLBACK TO one;
922 COMMIT;
923 SELECT * FROM t3;
924 SELECT * FROM t4;
925 SELECT * FROM t6;
926 }
927} -test {
928 faultsim_test_result {0 {3 5 7}}
929}
930
dand0b0d4d2010-07-01 19:01:56 +0000931#-------------------------------------------------------------------------
932# This tests fault-injection in a special case in the auto-vacuum code.
933#
dan89ccf442010-07-01 15:09:47 +0000934do_test pagerfault-20-pre1 {
935 faultsim_delete_and_reopen
dan89ccf442010-07-01 15:09:47 +0000936 execsql {
dand0b0d4d2010-07-01 19:01:56 +0000937 PRAGMA cache_size = 10;
938 PRAGMA auto_vacuum = FULL;
939 CREATE TABLE t0(a, b);
dan89ccf442010-07-01 15:09:47 +0000940 }
941 faultsim_save_and_close
942} {}
943do_faultsim_test pagerfault-20 -prep {
944 faultsim_restore_and_reopen
dan89ccf442010-07-01 15:09:47 +0000945} -body {
946 execsql {
947 BEGIN;
dand0b0d4d2010-07-01 19:01:56 +0000948 CREATE TABLE t1(a, b);
949 CREATE TABLE t2(a, b);
950 DROP TABLE t1;
dan89ccf442010-07-01 15:09:47 +0000951 COMMIT;
952 }
953} -test {
954 faultsim_test_result {0 {}}
dan89ccf442010-07-01 15:09:47 +0000955}
dan273f3f02010-06-26 15:42:33 +0000956
dand0b0d4d2010-07-01 19:01:56 +0000957do_test pagerfault-21-pre1 {
958 faultsim_delete_and_reopen
959 execsql {
960 PRAGMA cache_size = 10;
961 CREATE TABLE t0(a PRIMARY KEY, b);
962 INSERT INTO t0 VALUES(1, 2);
963 }
964 faultsim_save_and_close
965} {}
966do_faultsim_test pagerfault-21 -prep {
967 faultsim_restore_and_reopen
968} -body {
969 db eval { SELECT * FROM t0 LIMIT 1 } {
970 db eval { INSERT INTO t0 SELECT a+1, b FROM t0 }
971 db eval { INSERT INTO t0 SELECT a+2, b FROM t0 }
972 }
973} -test {
974 faultsim_test_result {0 {}}
975}
976
dand0b0d4d2010-07-01 19:01:56 +0000977
danb0ac3e32010-06-16 10:55:42 +0000978finish_test