1# 2004 August 30 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. 15# 16# $Id: corrupt.test,v 1.1 2004/08/30 16:52:19 drh Exp $ 17 18set testdir [file dirname $argv0] 19source $testdir/tester.tcl 20 21# Construct a large database for testing. 22# 23do_test corrupt-1.1 { 24 execsql { 25 BEGIN; 26 CREATE TABLE t1(x); 27 INSERT INTO t1 VALUES(randstr(10,100)); 28 INSERT INTO t1 VALUES(randstr(10,100)); 29 INSERT INTO t1 VALUES(randstr(10,100)); 30 INSERT INTO t1 SELECT x || randstr(5,10) FROM t1; 31 INSERT INTO t1 SELECT x || randstr(5,10) FROM t1; 32 INSERT INTO t1 SELECT x || randstr(5,10) FROM t1; 33 INSERT INTO t1 SELECT x || randstr(5,10) FROM t1; 34 INSERT INTO t1 VALUES(randstr(2100,3000)); 35 INSERT INTO t1 SELECT x || randstr(5,10) FROM t1; 36 INSERT INTO t1 SELECT x || randstr(5,10) FROM t1; 37 INSERT INTO t1 SELECT x || randstr(5,10) FROM t1; 38 INSERT INTO t1 SELECT x || randstr(5,10) FROM t1; 39 CREATE INDEX t1i1 ON t1(x); 40 CREATE TABLE t2 AS SELECT * FROM t1; 41 DELETE FROM t2 WHERE rowid%5!=0; 42 COMMIT; 43 PRAGMA integrity_check; 44 } 45} {ok} 46 47# Copy a file 48# 49proc copy_file {from to} { 50 set f [open $from] 51 fconfigure $f -translation binary 52 set t [open $to w] 53 fconfigure $t -translation binary 54 puts -nonewline $t [read $f [file size $from]] 55 close $t 56 close $f 57} 58 59# Setup for the tests. 60copy_file test.db test.bu 61set fsize [file size test.db] 62set junk "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" 63while {[string length $junk]<256} {append junk $junk} 64set junk [string range $junk 0 255] 65 66 67for {set i [expr {1*256}]} {$i<$fsize-256} {incr i 256} { 68 set tn [expr {$i/256}] 69 db close 70 copy_file test.bu test.db 71 set fd [open test.db r+] 72 fconfigure $fd -translation binary 73 seek $fd $i 74 puts -nonewline $fd $junk 75 close $fd 76 sqlite3 db test.db 77 do_test corrupt-2.$tn.1 { 78 sqlite3 db test.db 79 catchsql {SELECT count(*) FROM sqlite_master} 80 set x {} 81 } {} 82 do_test corrupt-2.$tn.2 { 83 catchsql {SELECT count(*) FROM t1} 84 set x {} 85 } {} 86 do_test corrupt-2.$tn.3 { 87 catchsql {SELECT count(*) FROM t1 WHERE x>'abcdef'} 88 set x {} 89 } {} 90 do_test corrupt-2.$tn.4 { 91 catchsql {SELECT count(*) FROM t2} 92 set x {} 93 } {} 94 do_test corrupt-2.$tn.5 { 95 catchsql {CREATE TABLE t3 AS SELECT * FROM t1} 96 set x {} 97 } {} 98 do_test corrupt-2.$tn.6 { 99 catchsql {DROP TABLE t1} 100 set x {} 101 } {} 102if {$tn==724} btree_breakpoint 103 do_test corrupt-2.$tn.7 { 104 catchsql {PRAGMA integrity_check} 105 set x {} 106 } {} 107} 108 109finish_test 110