sl@0: # 2001 September 15 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # This file implements regression tests for SQLite library. sl@0: # sl@0: # The focus of this file is testing the ability of the database to sl@0: # uses its rollback journal to recover intact (no database corruption) sl@0: # from a power failure during the middle of a COMMIT. Even more sl@0: # specifically, the tests in this file verify this functionality sl@0: # for storage mediums with various sector sizes. sl@0: # sl@0: # $Id: crash2.test,v 1.6 2008/08/25 07:12:29 danielk1977 Exp $ sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: ifcapable !crashtest { sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: db close sl@0: sl@0: # This test is designed to check that the crash-test infrastructure sl@0: # can create files that do not consist of an integer number of sl@0: # simulated disk blocks (i.e. 3KB file using 2KB disk blocks). sl@0: # sl@0: do_test crash2-1.1 { sl@0: crashsql -delay 500 -file test.db -blocksize 2048 { sl@0: PRAGMA auto_vacuum=OFF; sl@0: PRAGMA page_size=1024; sl@0: BEGIN; sl@0: CREATE TABLE abc AS SELECT 1 AS a, 2 AS b, 3 AS c; sl@0: CREATE TABLE def AS SELECT 1 AS d, 2 AS e, 3 AS f; sl@0: COMMIT; sl@0: } sl@0: file size test.db sl@0: } {3072} sl@0: sl@0: for {set ii 0} {$ii < 5} {incr ii} { sl@0: sl@0: # Simple test using the database created above: Create a new sl@0: # table so that page 1 and page 4 are modified. Using a sl@0: # block-size of 2048 and page-size of 1024, this means sl@0: # pages 2 and 3 must also be saved in the journal to avoid sl@0: # risking corruption. sl@0: # sl@0: # The loop is so that this test can be run with a couple sl@0: # of different seeds for the random number generator. sl@0: # sl@0: do_test crash2-1.2.$ii { sl@0: crashsql -file test.db -blocksize 2048 [subst { sl@0: [string repeat {SELECT random();} $ii] sl@0: CREATE TABLE hij(h, i, j); sl@0: }] sl@0: sqlite3 db test.db sl@0: db eval {PRAGMA integrity_check} sl@0: } {ok} sl@0: } sl@0: sl@0: proc signature {} { sl@0: return [db eval {SELECT count(*), md5sum(a), md5sum(b), md5sum(c) FROM abc}] sl@0: } sl@0: sl@0: # Test case for crashing during journal sync with simulated sl@0: # sector-size values from 1024 to 8192. sl@0: # sl@0: do_test crash2-2.0 { sl@0: execsql BEGIN sl@0: for {set n 0} {$n < 1000} {incr n} { sl@0: execsql "INSERT INTO abc VALUES($n, [expr 2*$n], [expr 3*$n])" sl@0: } sl@0: execsql { sl@0: INSERT INTO abc SELECT * FROM abc; sl@0: INSERT INTO abc SELECT * FROM abc; sl@0: INSERT INTO abc SELECT * FROM abc; sl@0: INSERT INTO abc SELECT * FROM abc; sl@0: INSERT INTO abc SELECT * FROM abc; sl@0: } sl@0: execsql COMMIT sl@0: expr ([file size test.db] / 1024) > 450 sl@0: } {1} sl@0: for {set i 1} {$i < 30} {incr i} { sl@0: set sig [signature] sl@0: set sector [expr 1024 * 1<<($i%4)] sl@0: db close sl@0: do_test crash2-2.$i.1 { sl@0: crashsql -blocksize $sector -delay [expr $i%5 + 1] -file test.db-journal " sl@0: PRAGMA temp_store = memory; sl@0: BEGIN; sl@0: SELECT random() FROM abc LIMIT $i; sl@0: INSERT INTO abc SELECT randstr(10,10), 0, 0 FROM abc WHERE random()%2==0; sl@0: DELETE FROM abc WHERE random()%2!=0; sl@0: COMMIT; sl@0: " sl@0: } {1 {child process exited abnormally}} sl@0: do_test crash2-2.$i.2 { sl@0: sqlite3 db test.db sl@0: signature sl@0: } $sig sl@0: } sl@0: sl@0: sl@0: # Test case for crashing during database sync with simulated sl@0: # sector-size values from 1024 to 8192. sl@0: # sl@0: for {set i 1} {$i < 10} {incr i} { sl@0: set sig [signature] sl@0: set sector [expr 1024 * 1<<($i%4)] sl@0: db close sl@0: do_test crash2-3.$i.1 { sl@0: crashsql -blocksize $sector -file test.db " sl@0: BEGIN; sl@0: SELECT random() FROM abc LIMIT $i; sl@0: INSERT INTO abc SELECT randstr(10,10), 0, 0 FROM abc WHERE random()%2==0; sl@0: DELETE FROM abc WHERE random()%2!=0; sl@0: COMMIT; sl@0: " sl@0: } {1 {child process exited abnormally}} sl@0: do_test crash2-3.$i.2 { sl@0: sqlite3 db test.db sl@0: signature sl@0: } $sig sl@0: } sl@0: sl@0: finish_test