1# 2007 April 6 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: corrupt3.test,v 1.2 2007/04/06 21:42:22 drh Exp $ 17 18set testdir [file dirname $argv0] 19source $testdir/tester.tcl 20 21# Do not use a codec for tests in this file, as the database file is 22# manipulated directly using tcl scripts (using the [hexio_write] command). 23# 24do_not_use_codec 25 26# We must have the page_size pragma for these tests to work. 27# 28ifcapable !pager_pragmas { 29 finish_test 30 return 31} 32 33# Create a database with an overflow page. 34# 35do_test corrupt3-1.1 { 36 set bigstring [string repeat 0123456789 200] 37 execsql { 38 PRAGMA auto_vacuum=OFF; 39 PRAGMA page_size=1024; 40 CREATE TABLE t1(x); 41 INSERT INTO t1 VALUES($bigstring); 42 } 43 file size test.db 44} [expr {1024*3}] 45 46# Verify that the file format is as we expect. The page size 47# should be 1024 bytes. The only record should have a single 48# overflow page. The overflow page is page 3. The pointer to 49# the overflow page is on the last 4 bytes of page 2. 50# 51do_test corrupt3-1.2 { 52 hexio_get_int [hexio_read test.db 16 2] 53} 1024 ;# The page size is 1024 54do_test corrupt3-1.3 { 55 hexio_get_int [hexio_read test.db 20 1] 56} 0 ;# Unused bytes per page is 0 57do_test corrupt3-1.4 { 58 hexio_get_int [hexio_read test.db 2044 4] 59} 3 ;# Overflow page is 3 60do_test corrupt3-1.5 { 61 hexio_get_int [hexio_read test.db 2048 4] 62} 0 ;# First chained overflow is 0 63 64integrity_check corrupt3-1.6 65 66# Make the overflow chain loop back on itself. See if the 67# corruption is detected. (Actually, the last pointer in 68# an overflow chain is ignored, so this is not an error.) 69# 70do_test corrupt3-1.7 { 71 db close 72 hexio_write test.db 2048 [hexio_render_int32 3] 73 sqlite3 db test.db 74 catchsql { 75 SELECT x FROM t1 76 } 77} [list 0 $bigstring] 78integrity_check corrupt3-1.8 79 80# Change the pointer for the first page of the overflow 81# change to be a non-existant page. 82# 83do_test corrupt3-1.9 { 84 db close 85 hexio_write test.db 2044 [hexio_render_int32 4] 86 sqlite3 db test.db 87 catchsql { 88 SELECT substr(x,1,10) FROM t1 89 } 90} [list 0 0123456789] 91do_test corrupt3-1.10 { 92 catchsql { 93 PRAGMA integrity_check 94 } 95} {0 {{*** in database main *** 96On tree page 2 cell 0: invalid page number 4 97Page 3 is never used}}} 98do_test corrupt3-1.11 { 99 db close 100 hexio_write test.db 2044 [hexio_render_int32 0] 101 sqlite3 db test.db 102 catchsql { 103 SELECT substr(x,1,10) FROM t1 104 } 105} [list 1 {database disk image is malformed}] 106do_test corrupt3-1.12 { 107 catchsql { 108 PRAGMA integrity_check 109 } 110} {0 {{*** in database main *** 111On tree page 2 cell 0: 1 of 1 pages missing from overflow list starting at 0 112Page 3 is never used}}} 113 114finish_test 115