xref: /sqlite-3.40.0/test/wal4.test (revision fda06bef)
132f29643Sdrh# 2010 July 1
232f29643Sdrh#
332f29643Sdrh# The author disclaims copyright to this source code.  In place of
432f29643Sdrh# a legal notice, here is a blessing:
532f29643Sdrh#
632f29643Sdrh#    May you do good and not evil.
732f29643Sdrh#    May you find forgiveness for yourself and forgive others.
832f29643Sdrh#    May you share freely, never taking more than you give.
932f29643Sdrh#
1032f29643Sdrh#***********************************************************************
1132f29643Sdrh# Verify that an empty database and a non-empty WAL file do not
1232f29643Sdrh# result in database corruption
1332f29643Sdrh#
1432f29643Sdrh
1532f29643Sdrhset testdir [file dirname $argv0]
1632f29643Sdrhsource $testdir/tester.tcl
1732f29643Sdrhsource $testdir/malloc_common.tcl
1832f29643Sdrhifcapable !wal {finish_test ; return }
1932f29643Sdrh
2032f29643Sdrhdo_test wal4-1.1 {
21250ea1adSdan  execsql {
2232f29643Sdrh    PRAGMA journal_mode=WAL;
2332f29643Sdrh    CREATE TABLE t1(x);
2432f29643Sdrh    INSERT INTO t1 VALUES(1);
2532f29643Sdrh    INSERT INTO t1 VALUES(2);
2632f29643Sdrh    SELECT x FROM t1 ORDER BY x;
2732f29643Sdrh  }
2832f29643Sdrh} {wal 1 2}
29250ea1adSdan
3032f29643Sdrhdo_test wal4-1.2 {
31250ea1adSdan  # Save a copy of the file-system containing the wal and wal-index files
32250ea1adSdan  # only (no database file).
33250ea1adSdan  faultsim_save_and_close
34*fda06befSmistachkin  forcedelete sv_test.db
35250ea1adSdan} {}
36250ea1adSdan
37250ea1adSdando_test wal4-1.3 {
38250ea1adSdan  faultsim_restore_and_reopen
39250ea1adSdan  catchsql { SELECT * FROM t1 }
4032f29643Sdrh} {1 {no such table: t1}}
4132f29643Sdrh
42250ea1adSdando_faultsim_test wal4-2 -prep {
43250ea1adSdan  faultsim_restore_and_reopen
44250ea1adSdan} -body {
45250ea1adSdan  execsql { SELECT name FROM sqlite_master }
46250ea1adSdan} -test {
47250ea1adSdan  # Result should be zero rows (empty db file).
48250ea1adSdan  #
49250ea1adSdan  faultsim_test_result {0 {}}
5032f29643Sdrh
51250ea1adSdan  # If the SELECT finished successfully, the WAL file should have been
52250ea1adSdan  # deleted. In no case should the database file have been written, so
53250ea1adSdan  # it should still be zero bytes in size regardless of whether or not
54250ea1adSdan  # a fault was injected. Test these assertions:
55250ea1adSdan  #
56250ea1adSdan  if { $testrc==0 && [file exists test.db-wal] } {
57250ea1adSdan    error "Wal file was not deleted"
58250ea1adSdan  }
59250ea1adSdan  if { [file size test.db]!=0 } {
60250ea1adSdan    error "Db file grew to [file size test.db] bytes"
61250ea1adSdan  }
62250ea1adSdan}
6332f29643Sdrh
6432f29643Sdrhfinish_test
65