| OLD | NEW |
| (Empty) |
| 1 # 2008 Sep 10 | |
| 2 # | |
| 3 # The author disclaims copyright to this source code. In place of | |
| 4 # a legal notice, here is a blessing: | |
| 5 # | |
| 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. | |
| 9 # | |
| 10 #*********************************************************************** | |
| 11 # This file implements regression tests for SQLite library. | |
| 12 # | |
| 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 | |
| 17 # itself. | |
| 18 # | |
| 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. | |
| 22 # | |
| 23 # $Id: corruptB.test,v 1.4 2009/07/21 19:25:24 danielk1977 Exp $ | |
| 24 | |
| 25 set testdir [file dirname $argv0] | |
| 26 source $testdir/tester.tcl | |
| 27 | |
| 28 | |
| 29 do_test corruptB-1.1 { | |
| 30 execsql { | |
| 31 PRAGMA auto_vacuum = 1; | |
| 32 CREATE TABLE t1(x); | |
| 33 INSERT INTO t1 VALUES(randomblob(200)); | |
| 34 INSERT INTO t1 SELECT randomblob(200) FROM t1; | |
| 35 INSERT INTO t1 SELECT randomblob(200) FROM t1; | |
| 36 INSERT INTO t1 SELECT randomblob(200) FROM t1; | |
| 37 INSERT INTO t1 SELECT randomblob(200) FROM t1; | |
| 38 INSERT INTO t1 SELECT randomblob(200) FROM t1; | |
| 39 } | |
| 40 expr {[file size test.db] > (1024*9)} | |
| 41 } {1} | |
| 42 integrity_check corruptB-1.2 | |
| 43 | |
| 44 file copy -force test.db bak.db | |
| 45 | |
| 46 # Set the right-child of a B-Tree rootpage to refer to the root-page itself. | |
| 47 # | |
| 48 do_test corruptB-1.3.1 { | |
| 49 set ::root [execsql {SELECT rootpage FROM sqlite_master}] | |
| 50 set ::offset [expr {($::root-1)*1024}] | |
| 51 hexio_write test.db [expr $offset+8] [hexio_render_int32 $::root] | |
| 52 } {4} | |
| 53 do_test corruptB-1.3.2 { | |
| 54 sqlite3 db test.db | |
| 55 catchsql { SELECT * FROM t1 } | |
| 56 } {1 {database disk image is malformed}} | |
| 57 | |
| 58 # Set the left-child of a cell in a B-Tree rootpage to refer to the | |
| 59 # root-page itself. | |
| 60 # | |
| 61 do_test corruptB-1.4.1 { | |
| 62 db close | |
| 63 file copy -force bak.db test.db | |
| 64 set cell_offset [hexio_get_int [hexio_read test.db [expr $offset+12] 2]] | |
| 65 hexio_write test.db [expr $offset+$cell_offset] [hexio_render_int32 $::root] | |
| 66 } {4} | |
| 67 do_test corruptB-1.4.2 { | |
| 68 sqlite3 db test.db | |
| 69 catchsql { SELECT * FROM t1 } | |
| 70 } {1 {database disk image is malformed}} | |
| 71 | |
| 72 # Now grow the table B-Tree so that it is more than 2 levels high. | |
| 73 # | |
| 74 do_test corruptB-1.5.1 { | |
| 75 db close | |
| 76 file copy -force bak.db test.db | |
| 77 sqlite3 db test.db | |
| 78 execsql { | |
| 79 INSERT INTO t1 SELECT randomblob(200) FROM t1; | |
| 80 INSERT INTO t1 SELECT randomblob(200) FROM t1; | |
| 81 INSERT INTO t1 SELECT randomblob(200) FROM t1; | |
| 82 INSERT INTO t1 SELECT randomblob(200) FROM t1; | |
| 83 INSERT INTO t1 SELECT randomblob(200) FROM t1; | |
| 84 INSERT INTO t1 SELECT randomblob(200) FROM t1; | |
| 85 INSERT INTO t1 SELECT randomblob(200) FROM t1; | |
| 86 } | |
| 87 } {} | |
| 88 | |
| 89 file copy -force test.db bak.db | |
| 90 | |
| 91 # Set the right-child pointer of the right-child of the root page to point | |
| 92 # back to the root page. | |
| 93 # | |
| 94 do_test corruptB-1.6.1 { | |
| 95 db close | |
| 96 set iRightChild [hexio_get_int [hexio_read test.db [expr $offset+8] 4]] | |
| 97 set c_offset [expr ($iRightChild-1)*1024] | |
| 98 hexio_write test.db [expr $c_offset+8] [hexio_render_int32 $::root] | |
| 99 } {4} | |
| 100 do_test corruptB-1.6.2 { | |
| 101 sqlite3 db test.db | |
| 102 catchsql { SELECT * FROM t1 } | |
| 103 } {1 {database disk image is malformed}} | |
| 104 | |
| 105 # Set the left-child pointer of a cell of the right-child of the root page to | |
| 106 # point back to the root page. | |
| 107 # | |
| 108 do_test corruptB-1.7.1 { | |
| 109 db close | |
| 110 file copy -force bak.db test.db | |
| 111 set cell_offset [hexio_get_int [hexio_read test.db [expr $c_offset+12] 2]] | |
| 112 hexio_write test.db [expr $c_offset+$cell_offset] [hexio_render_int32 $::root] | |
| 113 } {4} | |
| 114 do_test corruptB-1.7.2 { | |
| 115 sqlite3 db test.db | |
| 116 catchsql { SELECT * FROM t1 } | |
| 117 } {1 {database disk image is malformed}} | |
| 118 | |
| 119 do_test corruptB-1.8.1 { | |
| 120 db close | |
| 121 set cell_offset [hexio_get_int [hexio_read test.db [expr $offset+12] 2]] | |
| 122 set iLeftChild [ | |
| 123 hexio_get_int [hexio_read test.db [expr $offset+$cell_offset] 4] | |
| 124 ] | |
| 125 set c_offset [expr ($iLeftChild-1)*1024] | |
| 126 hexio_write test.db [expr $c_offset+8] [hexio_render_int32 $::root] | |
| 127 } {4} | |
| 128 do_test corruptB-1.8.2 { | |
| 129 sqlite3 db test.db | |
| 130 catchsql { SELECT * FROM t1 } | |
| 131 } {1 {database disk image is malformed}} | |
| 132 | |
| 133 # Set the left-child pointer of a cell of the right-child of the root page to | |
| 134 # point back to the root page. | |
| 135 # | |
| 136 do_test corruptB-1.9.1 { | |
| 137 db close | |
| 138 file copy -force bak.db test.db | |
| 139 set cell_offset [hexio_get_int [hexio_read test.db [expr $c_offset+12] 2]] | |
| 140 hexio_write test.db [expr $c_offset+$cell_offset] [hexio_render_int32 $::root] | |
| 141 } {4} | |
| 142 do_test corruptB-1.9.2 { | |
| 143 sqlite3 db test.db | |
| 144 catchsql { SELECT * FROM t1 } | |
| 145 } {1 {database disk image is malformed}} | |
| 146 | |
| 147 #--------------------------------------------------------------------------- | |
| 148 | |
| 149 do_test corruptB-2.1.1 { | |
| 150 db close | |
| 151 file copy -force bak.db test.db | |
| 152 hexio_write test.db [expr $offset+8] [hexio_render_int32 0x6FFFFFFF] | |
| 153 } {4} | |
| 154 do_test corruptB-2.1.2 { | |
| 155 sqlite3 db test.db | |
| 156 catchsql { SELECT * FROM t1 } | |
| 157 } {1 {database or disk is full}} | |
| 158 | |
| 159 #--------------------------------------------------------------------------- | |
| 160 | |
| 161 # Corrupt the header-size field of a database record. | |
| 162 # | |
| 163 do_test corruptB-3.1.1 { | |
| 164 db close | |
| 165 file copy -force bak.db test.db | |
| 166 sqlite3 db test.db | |
| 167 set v [string repeat abcdefghij 200] | |
| 168 execsql { | |
| 169 CREATE TABLE t2(a); | |
| 170 INSERT INTO t2 VALUES($v); | |
| 171 } | |
| 172 set t2_root [execsql {SELECT rootpage FROM sqlite_master WHERE name = 't2'}] | |
| 173 set iPage [expr ($t2_root-1)*1024] | |
| 174 set iCellarray [expr $iPage + 8] | |
| 175 set iRecord [hexio_get_int [hexio_read test.db $iCellarray 2]] | |
| 176 db close | |
| 177 hexio_write test.db [expr $iPage+$iRecord+3] FF00 | |
| 178 } {2} | |
| 179 do_test corruptB-3.1.2 { | |
| 180 sqlite3 db test.db | |
| 181 catchsql { SELECT * FROM t2 } | |
| 182 } {1 {database disk image is malformed}} | |
| 183 | |
| 184 finish_test | |
| OLD | NEW |