| OLD | NEW |
| (Empty) |
| 1 # 2008 July 9 | |
| 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 corruption in the form of duplicate entries on the freelist. | |
| 16 # | |
| 17 # $Id: corrupt9.test,v 1.3 2009/06/04 02:47:04 shane Exp $ | |
| 18 | |
| 19 set testdir [file dirname $argv0] | |
| 20 source $testdir/tester.tcl | |
| 21 | |
| 22 # We must have the page_size pragma for these tests to work. | |
| 23 # | |
| 24 ifcapable !pager_pragmas { | |
| 25 finish_test | |
| 26 return | |
| 27 } | |
| 28 | |
| 29 # Return the offset to the first (trunk) page of the freelist. Return | |
| 30 # zero of the freelist is empty. | |
| 31 # | |
| 32 proc freelist_trunk_offset {filename} { | |
| 33 if {[hexio_read $filename 36 4]==0} {return 0} | |
| 34 set pgno [hexio_get_int [hexio_read $filename 32 4]] | |
| 35 return [expr {($pgno-1)*[hexio_get_int [hexio_read $filename 16 2]]}] | |
| 36 } | |
| 37 | |
| 38 # This procedure looks at the first trunk page of the freelist and | |
| 39 # corrupts that page by overwriting up to N entries with duplicates | |
| 40 # of the first entry. | |
| 41 # | |
| 42 proc corrupt_freelist {filename N} { | |
| 43 set offset [freelist_trunk_offset $filename] | |
| 44 if {$offset==0} {error "Freelist is empty"} | |
| 45 set cnt [hexio_get_int [hexio_read $filename [expr {$offset+4}] 4]] | |
| 46 set pgno [hexio_read $filename [expr {$offset+8}] 4] | |
| 47 for {set i 12} {$N>0 && $i<8+4*$cnt} {incr i 4; incr N -1} { | |
| 48 hexio_write $filename [expr {$offset+$i}] $pgno | |
| 49 } | |
| 50 } | |
| 51 | |
| 52 # Create a database to work with. Make sure there are plenty of | |
| 53 # entries on the freelist. | |
| 54 # | |
| 55 do_test corrupt9-1.1 { | |
| 56 execsql { | |
| 57 PRAGMA auto_vacuum=NONE; | |
| 58 PRAGMA page_size=1024; | |
| 59 CREATE TABLE t1(x); | |
| 60 INSERT INTO t1(x) VALUES(1); | |
| 61 INSERT INTO t1(x) VALUES(2); | |
| 62 INSERT INTO t1(x) SELECT x+2 FROM t1; | |
| 63 INSERT INTO t1(x) SELECT x+4 FROM t1; | |
| 64 INSERT INTO t1(x) SELECT x+8 FROM t1; | |
| 65 INSERT INTO t1(x) SELECT x+16 FROM t1; | |
| 66 INSERT INTO t1(x) SELECT x+32 FROM t1; | |
| 67 INSERT INTO t1(x) SELECT x+64 FROM t1; | |
| 68 INSERT INTO t1(x) SELECT x+128 FROM t1; | |
| 69 INSERT INTO t1(x) SELECT x+256 FROM t1; | |
| 70 CREATE TABLE t2(a,b); | |
| 71 INSERT INTO t2 SELECT x, x*x FROM t1; | |
| 72 CREATE INDEX i1 ON t1(x); | |
| 73 CREATE INDEX i2 ON t2(b,a); | |
| 74 DROP INDEX i2; | |
| 75 } | |
| 76 expr {[file size test.db]>1024*24} | |
| 77 } {1} | |
| 78 integrity_check corrupt9-1.2 | |
| 79 | |
| 80 # Corrupt the freelist by adding duplicate entries to the freelist. | |
| 81 # Make sure the corruption is detected. | |
| 82 # | |
| 83 db close | |
| 84 file copy -force test.db test.db-template | |
| 85 | |
| 86 corrupt_freelist test.db 1 | |
| 87 sqlite3 db test.db | |
| 88 do_test corrupt9-2.1 { | |
| 89 set x [db eval {PRAGMA integrity_check}] | |
| 90 expr {$x!="ok"} | |
| 91 } {1} | |
| 92 do_test corrupt9-2.2 { | |
| 93 catchsql { | |
| 94 CREATE INDEX i2 ON t2(b,a); | |
| 95 REINDEX; | |
| 96 } | |
| 97 } {1 {database disk image is malformed}} | |
| 98 | |
| 99 | |
| 100 db close | |
| 101 file copy -force test.db-template test.db | |
| 102 corrupt_freelist test.db 2 | |
| 103 sqlite3 db test.db | |
| 104 do_test corrupt9-3.1 { | |
| 105 set x [db eval {PRAGMA integrity_check}] | |
| 106 expr {$x!="ok"} | |
| 107 } {1} | |
| 108 do_test corrupt9-3.2 { | |
| 109 catchsql { | |
| 110 CREATE INDEX i2 ON t2(b,a); | |
| 111 REINDEX; | |
| 112 } | |
| 113 } {1 {database disk image is malformed}} | |
| 114 | |
| 115 db close | |
| 116 file copy -force test.db-template test.db | |
| 117 corrupt_freelist test.db 3 | |
| 118 sqlite3 db test.db | |
| 119 do_test corrupt9-4.1 { | |
| 120 set x [db eval {PRAGMA integrity_check}] | |
| 121 expr {$x!="ok"} | |
| 122 } {1} | |
| 123 do_test corrupt9-4.2 { | |
| 124 catchsql { | |
| 125 CREATE INDEX i2 ON t2(b,a); | |
| 126 REINDEX; | |
| 127 } | |
| 128 } {1 {database disk image is malformed}} | |
| 129 | |
| 130 | |
| 131 finish_test | |
| OLD | NEW |