blob: c74f6c2ffa27c0461deab93f502c15f10808a9fa [file] [log] [blame]
danielk197706f52cb2007-03-17 10:28:04 +00001# 2001 September 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# This file implements regression tests for SQLite library.
12#
13# The focus of this file is testing the ability of the database to
14# uses its rollback journal to recover intact (no database corruption)
15# from a power failure during the middle of a COMMIT. Even more
16# specifically, the tests in this file verify this functionality
17# for storage mediums with various sector sizes.
18#
danielk19778c200142008-08-25 07:12:28 +000019# $Id: crash2.test,v 1.6 2008/08/25 07:12:29 danielk1977 Exp $
danielk197706f52cb2007-03-17 10:28:04 +000020
21set testdir [file dirname $argv0]
22source $testdir/tester.tcl
23
24ifcapable !crashtest {
25 finish_test
26 return
27}
28
danielk19779663b8f2007-08-24 11:52:28 +000029db close
30
danielk197706f52cb2007-03-17 10:28:04 +000031# This test is designed to check that the crash-test infrastructure
32# can create files that do not consist of an integer number of
33# simulated disk blocks (i.e. 3KB file using 2KB disk blocks).
34#
35do_test crash2-1.1 {
36 crashsql -delay 500 -file test.db -blocksize 2048 {
drh1e9daa62007-04-06 21:42:22 +000037 PRAGMA auto_vacuum=OFF;
danielk19779663b8f2007-08-24 11:52:28 +000038 PRAGMA page_size=1024;
danielk197706f52cb2007-03-17 10:28:04 +000039 BEGIN;
40 CREATE TABLE abc AS SELECT 1 AS a, 2 AS b, 3 AS c;
41 CREATE TABLE def AS SELECT 1 AS d, 2 AS e, 3 AS f;
42 COMMIT;
43 }
44 file size test.db
45} {3072}
46
47for {set ii 0} {$ii < 5} {incr ii} {
48
49 # Simple test using the database created above: Create a new
50 # table so that page 1 and page 4 are modified. Using a
51 # block-size of 2048 and page-size of 1024, this means
52 # pages 2 and 3 must also be saved in the journal to avoid
53 # risking corruption.
54 #
55 # The loop is so that this test can be run with a couple
56 # of different seeds for the random number generator.
57 #
58 do_test crash2-1.2.$ii {
danielk19779663b8f2007-08-24 11:52:28 +000059 crashsql -file test.db -blocksize 2048 [subst {
danielk197706f52cb2007-03-17 10:28:04 +000060 [string repeat {SELECT random();} $ii]
61 CREATE TABLE hij(h, i, j);
danielk19779663b8f2007-08-24 11:52:28 +000062 }]
63 sqlite3 db test.db
danielk197706f52cb2007-03-17 10:28:04 +000064 db eval {PRAGMA integrity_check}
65 } {ok}
66}
67
danielk1977a8553142007-03-19 15:04:54 +000068proc signature {} {
69 return [db eval {SELECT count(*), md5sum(a), md5sum(b), md5sum(c) FROM abc}]
70}
71
72# Test case for crashing during journal sync with simulated
73# sector-size values from 1024 to 8192.
74#
75do_test crash2-2.0 {
76 execsql BEGIN
77 for {set n 0} {$n < 1000} {incr n} {
78 execsql "INSERT INTO abc VALUES($n, [expr 2*$n], [expr 3*$n])"
79 }
80 execsql {
81 INSERT INTO abc SELECT * FROM abc;
82 INSERT INTO abc SELECT * FROM abc;
83 INSERT INTO abc SELECT * FROM abc;
84 INSERT INTO abc SELECT * FROM abc;
85 INSERT INTO abc SELECT * FROM abc;
86 }
87 execsql COMMIT
88 expr ([file size test.db] / 1024) > 450
89} {1}
90for {set i 1} {$i < 30} {incr i} {
91 set sig [signature]
92 set sector [expr 1024 * 1<<($i%4)]
danielk19779663b8f2007-08-24 11:52:28 +000093 db close
94 do_test crash2-2.$i.1 {
danielk1977a8553142007-03-19 15:04:54 +000095 crashsql -blocksize $sector -delay [expr $i%5 + 1] -file test.db-journal "
danielk19778c200142008-08-25 07:12:28 +000096 PRAGMA temp_store = memory;
danielk1977a8553142007-03-19 15:04:54 +000097 BEGIN;
98 SELECT random() FROM abc LIMIT $i;
99 INSERT INTO abc SELECT randstr(10,10), 0, 0 FROM abc WHERE random()%2==0;
100 DELETE FROM abc WHERE random()%2!=0;
101 COMMIT;
102 "
103 } {1 {child process exited abnormally}}
danielk19779663b8f2007-08-24 11:52:28 +0000104 do_test crash2-2.$i.2 {
105 sqlite3 db test.db
danielk1977a8553142007-03-19 15:04:54 +0000106 signature
107 } $sig
108}
109
110
111# Test case for crashing during database sync with simulated
112# sector-size values from 1024 to 8192.
113#
114for {set i 1} {$i < 10} {incr i} {
115 set sig [signature]
116 set sector [expr 1024 * 1<<($i%4)]
danielk19779663b8f2007-08-24 11:52:28 +0000117 db close
118 do_test crash2-3.$i.1 {
danielk1977a8553142007-03-19 15:04:54 +0000119 crashsql -blocksize $sector -file test.db "
120 BEGIN;
121 SELECT random() FROM abc LIMIT $i;
122 INSERT INTO abc SELECT randstr(10,10), 0, 0 FROM abc WHERE random()%2==0;
123 DELETE FROM abc WHERE random()%2!=0;
124 COMMIT;
125 "
126 } {1 {child process exited abnormally}}
danielk19779663b8f2007-08-24 11:52:28 +0000127 do_test crash2-3.$i.2 {
128 sqlite3 db test.db
danielk1977a8553142007-03-19 15:04:54 +0000129 signature
130 } $sig
131}
132
danielk197706f52cb2007-03-17 10:28:04 +0000133finish_test