drh | 32f2964 | 2010-07-01 19:45:34 +0000 | [diff] [blame] | 1 | # 2010 July 1 |
| 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 | # Verify that an empty database and a non-empty WAL file do not |
| 12 | # result in database corruption |
| 13 | # |
| 14 | |
| 15 | set testdir [file dirname $argv0] |
| 16 | source $testdir/tester.tcl |
| 17 | source $testdir/malloc_common.tcl |
| 18 | ifcapable !wal {finish_test ; return } |
| 19 | |
| 20 | do_test wal4-1.1 { |
dan | 250ea1a | 2010-07-16 11:10:25 +0000 | [diff] [blame] | 21 | execsql { |
drh | 32f2964 | 2010-07-01 19:45:34 +0000 | [diff] [blame] | 22 | PRAGMA journal_mode=WAL; |
| 23 | CREATE TABLE t1(x); |
| 24 | INSERT INTO t1 VALUES(1); |
| 25 | INSERT INTO t1 VALUES(2); |
| 26 | SELECT x FROM t1 ORDER BY x; |
| 27 | } |
| 28 | } {wal 1 2} |
dan | 250ea1a | 2010-07-16 11:10:25 +0000 | [diff] [blame] | 29 | |
drh | 32f2964 | 2010-07-01 19:45:34 +0000 | [diff] [blame] | 30 | do_test wal4-1.2 { |
dan | 250ea1a | 2010-07-16 11:10:25 +0000 | [diff] [blame] | 31 | # Save a copy of the file-system containing the wal and wal-index files |
| 32 | # only (no database file). |
| 33 | faultsim_save_and_close |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 34 | forcedelete sv_test.db |
dan | 250ea1a | 2010-07-16 11:10:25 +0000 | [diff] [blame] | 35 | } {} |
| 36 | |
| 37 | do_test wal4-1.3 { |
| 38 | faultsim_restore_and_reopen |
| 39 | catchsql { SELECT * FROM t1 } |
drh | 32f2964 | 2010-07-01 19:45:34 +0000 | [diff] [blame] | 40 | } {1 {no such table: t1}} |
| 41 | |
dan | 250ea1a | 2010-07-16 11:10:25 +0000 | [diff] [blame] | 42 | do_faultsim_test wal4-2 -prep { |
| 43 | faultsim_restore_and_reopen |
| 44 | } -body { |
| 45 | execsql { SELECT name FROM sqlite_master } |
| 46 | } -test { |
| 47 | # Result should be zero rows (empty db file). |
| 48 | # |
| 49 | faultsim_test_result {0 {}} |
drh | 32f2964 | 2010-07-01 19:45:34 +0000 | [diff] [blame] | 50 | |
dan | 250ea1a | 2010-07-16 11:10:25 +0000 | [diff] [blame] | 51 | # If the SELECT finished successfully, the WAL file should have been |
| 52 | # deleted. In no case should the database file have been written, so |
| 53 | # it should still be zero bytes in size regardless of whether or not |
| 54 | # a fault was injected. Test these assertions: |
| 55 | # |
| 56 | if { $testrc==0 && [file exists test.db-wal] } { |
| 57 | error "Wal file was not deleted" |
| 58 | } |
| 59 | if { [file size test.db]!=0 } { |
| 60 | error "Db file grew to [file size test.db] bytes" |
| 61 | } |
| 62 | } |
drh | 32f2964 | 2010-07-01 19:45:34 +0000 | [diff] [blame] | 63 | |
| 64 | finish_test |