3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library.
13 # This file implements tests to make sure SQLite does not crash or
14 # segfault if it sees a corrupt database file. It specifically focuses
15 # on corrupt pointer map pages.
17 # $Id: corrupt8.test,v 1.2 2008/07/11 03:34:10 drh Exp $
19 set testdir [file dirname $argv0]
20 source $testdir/tester.tcl
22 # Do not use a codec for tests in this file, as the database file is
23 # manipulated directly using tcl scripts (using the [hexio_write] command).
27 # These tests deal with corrupt database files
29 database_may_be_corrupt
31 # We must have the page_size pragma for these tests to work.
33 ifcapable !pager_pragmas||!autovacuum {
38 # Create a database to work with.
40 do_test corrupt8-1.1 {
43 PRAGMA page_size=1024;
45 INSERT INTO t1(x) VALUES(1);
46 INSERT INTO t1(x) VALUES(2);
47 INSERT INTO t1(x) SELECT x+2 FROM t1;
48 INSERT INTO t1(x) SELECT x+4 FROM t1;
49 INSERT INTO t1(x) SELECT x+8 FROM t1;
50 INSERT INTO t1(x) SELECT x+16 FROM t1;
51 INSERT INTO t1(x) SELECT x+32 FROM t1;
52 INSERT INTO t1(x) SELECT x+64 FROM t1;
53 INSERT INTO t1(x) SELECT x+128 FROM t1;
54 INSERT INTO t1(x) SELECT x+256 FROM t1;
56 INSERT INTO t2 SELECT x, x*x FROM t1;
58 expr {[file size test.db]>1024*12}
60 integrity_check corrupt8-1.2
62 # Loop through each ptrmap entry. Corrupt the entry and make sure the
63 # corruption is detected by the integrity_check.
65 for {set i 1024} {$i<2048} {incr i 5} {
66 set oldval [hexio_read test.db $i 1]
68 hexio_write test.db $i 00
69 do_test corrupt8-2.$i.0 {
72 set x [db eval {PRAGMA integrity_check}]
75 for {set k 1} {$k<=5} {incr k} {
76 if {$k==$oldval} continue
77 hexio_write test.db $i 0$k
78 do_test corrupt8-2.$i.$k {
81 set x [db eval {PRAGMA integrity_check}]
85 hexio_write test.db $i 06
86 do_test corrupt8-2.$i.6 {
89 set x [db eval {PRAGMA integrity_check}]
92 hexio_write test.db $i $oldval
94 set i2 [expr {$i+1+$i%4}]
95 set oldval [hexio_read test.db $i2 1]
96 hexio_write test.db $i2 [format %02x [expr {($oldval+1)&0xff}]]
97 do_test corrupt8-2.$i.7 {
100 set x [db eval {PRAGMA integrity_check}]
103 hexio_write test.db $i2 $oldval