115926590Sdrh# 2007 April 6 215926590Sdrh# 315926590Sdrh# The author disclaims copyright to this source code. In place of 415926590Sdrh# a legal notice, here is a blessing: 515926590Sdrh# 615926590Sdrh# May you do good and not evil. 715926590Sdrh# May you find forgiveness for yourself and forgive others. 815926590Sdrh# May you share freely, never taking more than you give. 915926590Sdrh# 1015926590Sdrh#*********************************************************************** 1115926590Sdrh# This file implements regression tests for SQLite library. 1215926590Sdrh# 1315926590Sdrh# This file implements tests to make sure SQLite does not crash or 1415926590Sdrh# segfault if it sees a corrupt database file. 1515926590Sdrh# 161e9daa6aSdrh# $Id: corrupt3.test,v 1.2 2007/04/06 21:42:22 drh Exp $ 1715926590Sdrh 1815926590Sdrhset testdir [file dirname $argv0] 1915926590Sdrhsource $testdir/tester.tcl 2015926590Sdrh 21af3906a7Sdrh# This module uses hard-coded offsets which do not work if the reserved_bytes 22af3906a7Sdrh# value is nonzero. 23af3906a7Sdrhif {[nonzero_reserved_bytes]} {finish_test; return;} 2468928b6cSdan 2509fe6143Sdrh# These tests deal with corrupt database files 2609fe6143Sdrh# 2709fe6143Sdrhdatabase_may_be_corrupt 2809fe6143Sdrh 2915926590Sdrh# We must have the page_size pragma for these tests to work. 3015926590Sdrh# 31f4ba1093Sdanifcapable !pager_pragmas||direct_read { 3215926590Sdrh finish_test 3315926590Sdrh return 3415926590Sdrh} 3515926590Sdrh 3615926590Sdrh# Create a database with an overflow page. 3715926590Sdrh# 3815926590Sdrhdo_test corrupt3-1.1 { 3915926590Sdrh set bigstring [string repeat 0123456789 200] 4015926590Sdrh execsql { 411e9daa6aSdrh PRAGMA auto_vacuum=OFF; 4215926590Sdrh PRAGMA page_size=1024; 4315926590Sdrh CREATE TABLE t1(x); 4415926590Sdrh INSERT INTO t1 VALUES($bigstring); 4515926590Sdrh } 4615926590Sdrh file size test.db 4715926590Sdrh} [expr {1024*3}] 4815926590Sdrh 4915926590Sdrh# Verify that the file format is as we expect. The page size 5015926590Sdrh# should be 1024 bytes. The only record should have a single 5115926590Sdrh# overflow page. The overflow page is page 3. The pointer to 5215926590Sdrh# the overflow page is on the last 4 bytes of page 2. 5315926590Sdrh# 5415926590Sdrhdo_test corrupt3-1.2 { 5515926590Sdrh hexio_get_int [hexio_read test.db 16 2] 5615926590Sdrh} 1024 ;# The page size is 1024 5715926590Sdrhdo_test corrupt3-1.3 { 5815926590Sdrh hexio_get_int [hexio_read test.db 20 1] 5915926590Sdrh} 0 ;# Unused bytes per page is 0 6015926590Sdrhdo_test corrupt3-1.4 { 6115926590Sdrh hexio_get_int [hexio_read test.db 2044 4] 6215926590Sdrh} 3 ;# Overflow page is 3 6315926590Sdrhdo_test corrupt3-1.5 { 6415926590Sdrh hexio_get_int [hexio_read test.db 2048 4] 6515926590Sdrh} 0 ;# First chained overflow is 0 6615926590Sdrh 6715926590Sdrhintegrity_check corrupt3-1.6 6815926590Sdrh 6915926590Sdrh# Make the overflow chain loop back on itself. See if the 7091d5866eSdrh# corruption is detected. 7115926590Sdrh# 7215926590Sdrhdo_test corrupt3-1.7 { 7315926590Sdrh db close 7415926590Sdrh hexio_write test.db 2048 [hexio_render_int32 3] 7515926590Sdrh sqlite3 db test.db 7615926590Sdrh catchsql { 7715926590Sdrh SELECT x FROM t1 7815926590Sdrh } 7915926590Sdrh} [list 0 $bigstring] 8091d5866eSdrhdo_test corrupt3-1.8 { 8191d5866eSdrh catchsql { 8291d5866eSdrh PRAGMA integrity_check 8391d5866eSdrh } 8491d5866eSdrh} {0 {{*** in database main *** 8591d5866eSdrhOn tree page 2 cell 0: 2nd reference to page 3}}} 8615926590Sdrh 8715926590Sdrh# Change the pointer for the first page of the overflow 8815926590Sdrh# change to be a non-existant page. 8915926590Sdrh# 9015926590Sdrhdo_test corrupt3-1.9 { 9115926590Sdrh db close 9215926590Sdrh hexio_write test.db 2044 [hexio_render_int32 4] 9315926590Sdrh sqlite3 db test.db 9415926590Sdrh catchsql { 9515926590Sdrh SELECT substr(x,1,10) FROM t1 9615926590Sdrh } 97*584e8b76Sdrh} [list 1 {database disk image is malformed}] 9815926590Sdrhdo_test corrupt3-1.10 { 9915926590Sdrh catchsql { 10015926590Sdrh PRAGMA integrity_check 10115926590Sdrh } 10215926590Sdrh} {0 {{*** in database main *** 10315926590SdrhOn tree page 2 cell 0: invalid page number 4 10415926590SdrhPage 3 is never used}}} 10515926590Sdrhdo_test corrupt3-1.11 { 10615926590Sdrh db close 10715926590Sdrh hexio_write test.db 2044 [hexio_render_int32 0] 10815926590Sdrh sqlite3 db test.db 10915926590Sdrh catchsql { 11015926590Sdrh SELECT substr(x,1,10) FROM t1 11115926590Sdrh } 11215926590Sdrh} [list 1 {database disk image is malformed}] 11315926590Sdrhdo_test corrupt3-1.12 { 11415926590Sdrh catchsql { 11515926590Sdrh PRAGMA integrity_check 11615926590Sdrh } 11715926590Sdrh} {0 {{*** in database main *** 11891d5866eSdrhOn tree page 2 cell 0: overflow list length is 0 but should be 1 11915926590SdrhPage 3 is never used}}} 12015926590Sdrh 12115926590Sdrhfinish_test 122