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 corruption in the form of duplicate entries on the freelist.
17 # $Id: corrupt9.test,v 1.3 2009/06/04 02:47:04 shane 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 {
38 # Return the offset to the first (trunk) page of the freelist. Return
39 # zero of the freelist is empty.
41 proc freelist_trunk_offset {filename} {
42 if {[hexio_read $filename 36 4]==0} {return 0}
43 set pgno [hexio_get_int [hexio_read $filename 32 4]]
44 return [expr {($pgno-1)*[hexio_get_int [hexio_read $filename 16 2]]}]
47 # This procedure looks at the first trunk page of the freelist and
48 # corrupts that page by overwriting up to N entries with duplicates
51 proc corrupt_freelist {filename N} {
52 set offset [freelist_trunk_offset $filename]
53 if {$offset==0} {error "Freelist is empty"}
54 set cnt [hexio_get_int [hexio_read $filename [expr {$offset+4}] 4]]
55 set pgno [hexio_read $filename [expr {$offset+8}] 4]
56 for {set i 12} {$N>0 && $i<8+4*$cnt} {incr i 4; incr N -1} {
57 hexio_write $filename [expr {$offset+$i}] $pgno
61 # Create a database to work with. Make sure there are plenty of
62 # entries on the freelist.
64 do_test corrupt9-1.1 {
66 PRAGMA auto_vacuum=NONE;
67 PRAGMA page_size=1024;
69 INSERT INTO t1(x) VALUES(1);
70 INSERT INTO t1(x) VALUES(2);
71 INSERT INTO t1(x) SELECT x+2 FROM t1;
72 INSERT INTO t1(x) SELECT x+4 FROM t1;
73 INSERT INTO t1(x) SELECT x+8 FROM t1;
74 INSERT INTO t1(x) SELECT x+16 FROM t1;
75 INSERT INTO t1(x) SELECT x+32 FROM t1;
76 INSERT INTO t1(x) SELECT x+64 FROM t1;
77 INSERT INTO t1(x) SELECT x+128 FROM t1;
78 INSERT INTO t1(x) SELECT x+256 FROM t1;
80 INSERT INTO t2 SELECT x, x*x FROM t1;
81 CREATE INDEX i1 ON t1(x);
82 CREATE INDEX i2 ON t2(b,a);
85 expr {[file size test.db]>1024*24}
87 integrity_check corrupt9-1.2
89 # Corrupt the freelist by adding duplicate entries to the freelist.
90 # Make sure the corruption is detected.
93 forcecopy test.db test.db-template
95 corrupt_freelist test.db 1
97 do_test corrupt9-2.1 {
98 set x [db eval {PRAGMA integrity_check}]
101 do_test corrupt9-2.2 {
103 CREATE INDEX i2 ON t2(b,a);
106 } {1 {database disk image is malformed}}
110 forcecopy test.db-template test.db
111 corrupt_freelist test.db 2
113 do_test corrupt9-3.1 {
114 set x [db eval {PRAGMA integrity_check}]
117 do_test corrupt9-3.2 {
119 CREATE INDEX i2 ON t2(b,a);
122 } {1 {database disk image is malformed}}
125 forcecopy test.db-template test.db
126 corrupt_freelist test.db 3
128 do_test corrupt9-4.1 {
129 set x [db eval {PRAGMA integrity_check}]
132 do_test corrupt9-4.2 {
134 CREATE INDEX i2 ON t2(b,a);
137 } {1 {database disk image is malformed}}