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 # We must have the page_size pragma for these tests to work.
29 ifcapable !pager_pragmas||!autovacuum {
34 # Create a database to work with.
36 do_test corrupt8-1.1 {
39 PRAGMA page_size=1024;
41 INSERT INTO t1(x) VALUES(1);
42 INSERT INTO t1(x) VALUES(2);
43 INSERT INTO t1(x) SELECT x+2 FROM t1;
44 INSERT INTO t1(x) SELECT x+4 FROM t1;
45 INSERT INTO t1(x) SELECT x+8 FROM t1;
46 INSERT INTO t1(x) SELECT x+16 FROM t1;
47 INSERT INTO t1(x) SELECT x+32 FROM t1;
48 INSERT INTO t1(x) SELECT x+64 FROM t1;
49 INSERT INTO t1(x) SELECT x+128 FROM t1;
50 INSERT INTO t1(x) SELECT x+256 FROM t1;
52 INSERT INTO t2 SELECT x, x*x FROM t1;
54 expr {[file size test.db]>1024*12}
56 integrity_check corrupt8-1.2
58 # Loop through each ptrmap entry. Corrupt the entry and make sure the
59 # corruption is detected by the integrity_check.
61 for {set i 1024} {$i<2048} {incr i 5} {
62 set oldval [hexio_read test.db $i 1]
64 hexio_write test.db $i 00
65 do_test corrupt8-2.$i.0 {
68 set x [db eval {PRAGMA integrity_check}]
71 for {set k 1} {$k<=5} {incr k} {
72 if {$k==$oldval} continue
73 hexio_write test.db $i 0$k
74 do_test corrupt8-2.$i.$k {
77 set x [db eval {PRAGMA integrity_check}]
81 hexio_write test.db $i 06
82 do_test corrupt8-2.$i.6 {
85 set x [db eval {PRAGMA integrity_check}]
88 hexio_write test.db $i $oldval
90 set i2 [expr {$i+1+$i%4}]
91 set oldval [hexio_read test.db $i2 1]
92 hexio_write test.db $i2 [format %02x [expr {($oldval+1)&0xff}]]
93 do_test corrupt8-2.$i.7 {
96 set x [db eval {PRAGMA integrity_check}]
99 hexio_write test.db $i2 $oldval