xref: /sqlite-3.40.0/test/crash2.test (revision 8c20014a)
106f52cb9Sdanielk1977# 2001 September 15
206f52cb9Sdanielk1977#
306f52cb9Sdanielk1977# The author disclaims copyright to this source code.  In place of
406f52cb9Sdanielk1977# a legal notice, here is a blessing:
506f52cb9Sdanielk1977#
606f52cb9Sdanielk1977#    May you do good and not evil.
706f52cb9Sdanielk1977#    May you find forgiveness for yourself and forgive others.
806f52cb9Sdanielk1977#    May you share freely, never taking more than you give.
906f52cb9Sdanielk1977#
1006f52cb9Sdanielk1977#***********************************************************************
1106f52cb9Sdanielk1977# This file implements regression tests for SQLite library.
1206f52cb9Sdanielk1977#
1306f52cb9Sdanielk1977# The focus of this file is testing the ability of the database to
1406f52cb9Sdanielk1977# uses its rollback journal to recover intact (no database corruption)
1506f52cb9Sdanielk1977# from a power failure during the middle of a COMMIT. Even more
1606f52cb9Sdanielk1977# specifically, the tests in this file verify this functionality
1706f52cb9Sdanielk1977# for storage mediums with various sector sizes.
1806f52cb9Sdanielk1977#
19*8c20014aSdanielk1977# $Id: crash2.test,v 1.6 2008/08/25 07:12:29 danielk1977 Exp $
2006f52cb9Sdanielk1977
2106f52cb9Sdanielk1977set testdir [file dirname $argv0]
2206f52cb9Sdanielk1977source $testdir/tester.tcl
2306f52cb9Sdanielk1977
2406f52cb9Sdanielk1977ifcapable !crashtest {
2506f52cb9Sdanielk1977  finish_test
2606f52cb9Sdanielk1977  return
2706f52cb9Sdanielk1977}
2806f52cb9Sdanielk1977
299663b8f9Sdanielk1977db close
309663b8f9Sdanielk1977
3106f52cb9Sdanielk1977# This test is designed to check that the crash-test infrastructure
3206f52cb9Sdanielk1977# can create files that do not consist of an integer number of
3306f52cb9Sdanielk1977# simulated disk blocks (i.e. 3KB file using 2KB disk blocks).
3406f52cb9Sdanielk1977#
3506f52cb9Sdanielk1977do_test crash2-1.1 {
3606f52cb9Sdanielk1977  crashsql -delay 500 -file test.db -blocksize 2048 {
371e9daa6aSdrh    PRAGMA auto_vacuum=OFF;
389663b8f9Sdanielk1977    PRAGMA page_size=1024;
3906f52cb9Sdanielk1977    BEGIN;
4006f52cb9Sdanielk1977    CREATE TABLE abc AS SELECT 1 AS a, 2 AS b, 3 AS c;
4106f52cb9Sdanielk1977    CREATE TABLE def AS SELECT 1 AS d, 2 AS e, 3 AS f;
4206f52cb9Sdanielk1977    COMMIT;
4306f52cb9Sdanielk1977  }
4406f52cb9Sdanielk1977  file size test.db
4506f52cb9Sdanielk1977} {3072}
4606f52cb9Sdanielk1977
4706f52cb9Sdanielk1977for {set ii 0} {$ii < 5} {incr ii} {
4806f52cb9Sdanielk1977
4906f52cb9Sdanielk1977  # Simple test using the database created above: Create a new
5006f52cb9Sdanielk1977  # table so that page 1 and page 4 are modified. Using a
5106f52cb9Sdanielk1977  # block-size of 2048 and page-size of 1024, this means
5206f52cb9Sdanielk1977  # pages 2 and 3 must also be saved in the journal to avoid
5306f52cb9Sdanielk1977  # risking corruption.
5406f52cb9Sdanielk1977  #
5506f52cb9Sdanielk1977  # The loop is so that this test can be run with a couple
5606f52cb9Sdanielk1977  # of different seeds for the random number generator.
5706f52cb9Sdanielk1977  #
5806f52cb9Sdanielk1977  do_test crash2-1.2.$ii {
599663b8f9Sdanielk1977    crashsql -file test.db -blocksize 2048 [subst {
6006f52cb9Sdanielk1977      [string repeat {SELECT random();} $ii]
6106f52cb9Sdanielk1977      CREATE TABLE hij(h, i, j);
629663b8f9Sdanielk1977    }]
639663b8f9Sdanielk1977    sqlite3 db test.db
6406f52cb9Sdanielk1977    db eval {PRAGMA integrity_check}
6506f52cb9Sdanielk1977  } {ok}
6606f52cb9Sdanielk1977}
6706f52cb9Sdanielk1977
68a8553141Sdanielk1977proc signature {} {
69a8553141Sdanielk1977  return [db eval {SELECT count(*), md5sum(a), md5sum(b), md5sum(c) FROM abc}]
70a8553141Sdanielk1977}
71a8553141Sdanielk1977
72a8553141Sdanielk1977# Test case for crashing during journal sync with simulated
73a8553141Sdanielk1977# sector-size values from 1024 to 8192.
74a8553141Sdanielk1977#
75a8553141Sdanielk1977do_test crash2-2.0 {
76a8553141Sdanielk1977  execsql BEGIN
77a8553141Sdanielk1977  for {set n 0} {$n < 1000} {incr n} {
78a8553141Sdanielk1977    execsql "INSERT INTO abc VALUES($n, [expr 2*$n], [expr 3*$n])"
79a8553141Sdanielk1977  }
80a8553141Sdanielk1977  execsql {
81a8553141Sdanielk1977    INSERT INTO abc SELECT * FROM abc;
82a8553141Sdanielk1977    INSERT INTO abc SELECT * FROM abc;
83a8553141Sdanielk1977    INSERT INTO abc SELECT * FROM abc;
84a8553141Sdanielk1977    INSERT INTO abc SELECT * FROM abc;
85a8553141Sdanielk1977    INSERT INTO abc SELECT * FROM abc;
86a8553141Sdanielk1977  }
87a8553141Sdanielk1977  execsql COMMIT
88a8553141Sdanielk1977  expr ([file size test.db] / 1024) > 450
89a8553141Sdanielk1977} {1}
90a8553141Sdanielk1977for {set i 1} {$i < 30} {incr i} {
91a8553141Sdanielk1977  set sig [signature]
92a8553141Sdanielk1977  set sector [expr 1024 * 1<<($i%4)]
939663b8f9Sdanielk1977  db close
949663b8f9Sdanielk1977  do_test crash2-2.$i.1 {
95a8553141Sdanielk1977     crashsql -blocksize $sector -delay [expr $i%5 + 1] -file test.db-journal "
96*8c20014aSdanielk1977       PRAGMA temp_store = memory;
97a8553141Sdanielk1977       BEGIN;
98a8553141Sdanielk1977       SELECT random() FROM abc LIMIT $i;
99a8553141Sdanielk1977       INSERT INTO abc SELECT randstr(10,10), 0, 0 FROM abc WHERE random()%2==0;
100a8553141Sdanielk1977       DELETE FROM abc WHERE random()%2!=0;
101a8553141Sdanielk1977       COMMIT;
102a8553141Sdanielk1977     "
103a8553141Sdanielk1977  } {1 {child process exited abnormally}}
1049663b8f9Sdanielk1977  do_test crash2-2.$i.2 {
1059663b8f9Sdanielk1977    sqlite3 db test.db
106a8553141Sdanielk1977    signature
107a8553141Sdanielk1977  } $sig
108a8553141Sdanielk1977}
109a8553141Sdanielk1977
110a8553141Sdanielk1977
111a8553141Sdanielk1977# Test case for crashing during database sync with simulated
112a8553141Sdanielk1977# sector-size values from 1024 to 8192.
113a8553141Sdanielk1977#
114a8553141Sdanielk1977for {set i 1} {$i < 10} {incr i} {
115a8553141Sdanielk1977  set sig [signature]
116a8553141Sdanielk1977  set sector [expr 1024 * 1<<($i%4)]
1179663b8f9Sdanielk1977  db close
1189663b8f9Sdanielk1977  do_test crash2-3.$i.1 {
119a8553141Sdanielk1977     crashsql -blocksize $sector -file test.db "
120a8553141Sdanielk1977       BEGIN;
121a8553141Sdanielk1977       SELECT random() FROM abc LIMIT $i;
122a8553141Sdanielk1977       INSERT INTO abc SELECT randstr(10,10), 0, 0 FROM abc WHERE random()%2==0;
123a8553141Sdanielk1977       DELETE FROM abc WHERE random()%2!=0;
124a8553141Sdanielk1977       COMMIT;
125a8553141Sdanielk1977     "
126a8553141Sdanielk1977  } {1 {child process exited abnormally}}
1279663b8f9Sdanielk1977  do_test crash2-3.$i.2 {
1289663b8f9Sdanielk1977    sqlite3 db test.db
129a8553141Sdanielk1977    signature
130a8553141Sdanielk1977  } $sig
131a8553141Sdanielk1977}
132a8553141Sdanielk1977
13306f52cb9Sdanielk1977finish_test
134