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 loops in the B-Tree structure. A loop is formed in a B-Tree structure
16 # when there exists a page that is both an a descendent or ancestor of
19 # Also test that an SQLITE_CORRUPT error is returned if a B-Tree page
20 # contains a (corrupt) reference to a page greater than the configured
21 # maximum page number.
23 # $Id: corruptB.test,v 1.4 2009/07/21 19:25:24 danielk1977 Exp $
25 set testdir [file dirname $argv0]
26 source $testdir/tester.tcl
28 # Do not use a codec for tests in this file, as the database file is
29 # manipulated directly using tcl scripts (using the [hexio_write] command).
34 do_test corruptB-1.1 {
36 PRAGMA auto_vacuum = 1;
38 INSERT INTO t1 VALUES(randomblob(200));
39 INSERT INTO t1 SELECT randomblob(200) FROM t1;
40 INSERT INTO t1 SELECT randomblob(200) FROM t1;
41 INSERT INTO t1 SELECT randomblob(200) FROM t1;
42 INSERT INTO t1 SELECT randomblob(200) FROM t1;
43 INSERT INTO t1 SELECT randomblob(200) FROM t1;
45 expr {[file size test.db] > (1024*9)}
47 integrity_check corruptB-1.2
49 forcecopy test.db bak.db
51 # Set the right-child of a B-Tree rootpage to refer to the root-page itself.
53 do_test corruptB-1.3.1 {
54 set ::root [execsql {SELECT rootpage FROM sqlite_master}]
55 set ::offset [expr {($::root-1)*1024}]
56 hexio_write test.db [expr $offset+8] [hexio_render_int32 $::root]
58 do_test corruptB-1.3.2 {
60 catchsql { SELECT * FROM t1 }
61 } {1 {database disk image is malformed}}
63 # Set the left-child of a cell in a B-Tree rootpage to refer to the
66 do_test corruptB-1.4.1 {
68 forcecopy bak.db test.db
69 set cell_offset [hexio_get_int [hexio_read test.db [expr $offset+12] 2]]
70 hexio_write test.db [expr $offset+$cell_offset] [hexio_render_int32 $::root]
72 do_test corruptB-1.4.2 {
74 catchsql { SELECT * FROM t1 }
75 } {1 {database disk image is malformed}}
77 # Now grow the table B-Tree so that it is more than 2 levels high.
79 do_test corruptB-1.5.1 {
81 forcecopy bak.db test.db
84 INSERT INTO t1 SELECT randomblob(200) FROM t1;
85 INSERT INTO t1 SELECT randomblob(200) FROM t1;
86 INSERT INTO t1 SELECT randomblob(200) FROM t1;
87 INSERT INTO t1 SELECT randomblob(200) FROM t1;
88 INSERT INTO t1 SELECT randomblob(200) FROM t1;
89 INSERT INTO t1 SELECT randomblob(200) FROM t1;
90 INSERT INTO t1 SELECT randomblob(200) FROM t1;
94 forcecopy test.db bak.db
96 # Set the right-child pointer of the right-child of the root page to point
97 # back to the root page.
99 do_test corruptB-1.6.1 {
101 set iRightChild [hexio_get_int [hexio_read test.db [expr $offset+8] 4]]
102 set c_offset [expr ($iRightChild-1)*1024]
103 hexio_write test.db [expr $c_offset+8] [hexio_render_int32 $::root]
105 do_test corruptB-1.6.2 {
107 catchsql { SELECT * FROM t1 }
108 } {1 {database disk image is malformed}}
110 # Set the left-child pointer of a cell of the right-child of the root page to
111 # point back to the root page.
113 do_test corruptB-1.7.1 {
115 forcecopy bak.db test.db
116 set cell_offset [hexio_get_int [hexio_read test.db [expr $c_offset+12] 2]]
117 hexio_write test.db [expr $c_offset+$cell_offset] [hexio_render_int32 $::root]
119 do_test corruptB-1.7.2 {
121 catchsql { SELECT * FROM t1 }
122 } {1 {database disk image is malformed}}
124 do_test corruptB-1.8.1 {
126 set cell_offset [hexio_get_int [hexio_read test.db [expr $offset+12] 2]]
128 hexio_get_int [hexio_read test.db [expr $offset+$cell_offset] 4]
130 set c_offset [expr ($iLeftChild-1)*1024]
131 hexio_write test.db [expr $c_offset+8] [hexio_render_int32 $::root]
133 do_test corruptB-1.8.2 {
135 catchsql { SELECT * FROM t1 }
136 } {1 {database disk image is malformed}}
138 # Set the left-child pointer of a cell of the right-child of the root page to
139 # point back to the root page.
141 do_test corruptB-1.9.1 {
143 forcecopy bak.db test.db
144 set cell_offset [hexio_get_int [hexio_read test.db [expr $c_offset+12] 2]]
145 hexio_write test.db [expr $c_offset+$cell_offset] [hexio_render_int32 $::root]
147 do_test corruptB-1.9.2 {
149 catchsql { SELECT * FROM t1 }
150 } {1 {database disk image is malformed}}
152 #---------------------------------------------------------------------------
154 do_test corruptB-2.1.1 {
156 forcecopy bak.db test.db
157 hexio_write test.db [expr $offset+8] [hexio_render_int32 0x6FFFFFFF]
159 do_test corruptB-2.1.2 {
161 catchsql { SELECT * FROM t1 }
162 } {1 {database disk image is malformed}}
164 #---------------------------------------------------------------------------
166 # Corrupt the header-size field of a database record.
168 do_test corruptB-3.1.1 {
170 forcecopy bak.db test.db
172 set v [string repeat abcdefghij 200]
175 INSERT INTO t2 VALUES($v);
177 set t2_root [execsql {SELECT rootpage FROM sqlite_master WHERE name = 't2'}]
178 set iPage [expr ($t2_root-1)*1024]
179 set iCellarray [expr $iPage + 8]
180 set iRecord [hexio_get_int [hexio_read test.db $iCellarray 2]]
182 hexio_write test.db [expr $iPage+$iRecord+3] FF00
184 do_test corruptB-3.1.2 {
186 catchsql { SELECT * FROM t2 }
187 } {1 {database disk image is malformed}}