1df003d61Sdrh# 2013-08-01 2df003d61Sdrh# 3df003d61Sdrh# The author disclaims copyright to this source code. In place of 4df003d61Sdrh# a legal notice, here is a blessing: 5df003d61Sdrh# 6df003d61Sdrh# May you do good and not evil. 7df003d61Sdrh# May you find forgiveness for yourself and forgive others. 8df003d61Sdrh# May you share freely, never taking more than you give. 9df003d61Sdrh# 10df003d61Sdrh#*********************************************************************** 11df003d61Sdrh# 12df003d61Sdrh 13df003d61Sdrhset testdir [file dirname $argv0] 14df003d61Sdrhsource $testdir/tester.tcl 15df003d61Sdrhset testprefix corruptG 16df003d61Sdrh 17*af3906a7Sdrh# This module uses hard-coded offsets which do not work if the reserved_bytes 18*af3906a7Sdrh# value is nonzero. 19*af3906a7Sdrhif {[nonzero_reserved_bytes]} {finish_test; return;} 20df003d61Sdrh 2109fe6143Sdrh# These tests deal with corrupt database files 2209fe6143Sdrh# 2309fe6143Sdrhdatabase_may_be_corrupt 2409fe6143Sdrh 25df003d61Sdrh# Create a simple database with a single entry. Then corrupt the 26df003d61Sdrh# header-size varint on the index payload so that it maps into a 27df003d61Sdrh# negative number. Try to use the database. 28df003d61Sdrh# 29df003d61Sdrh 30df003d61Sdrhdo_execsql_test 1.1 { 31df003d61Sdrh PRAGMA page_size=512; 32df003d61Sdrh CREATE TABLE t1(a,b,c); 33f5601cacSdrh INSERT INTO t1(rowid,a,b,c) VALUES(52,'abc','xyz','123'); 34df003d61Sdrh CREATE INDEX t1abc ON t1(a,b,c); 35df003d61Sdrh} 36df003d61Sdrh 37c3c16cbdSdanset idxroot [db one {SELECT rootpage FROM sqlite_master WHERE name = 't1abc'}] 38c3c16cbdSdan 39df003d61Sdrh# Corrupt the file 40df003d61Sdrhdb close 41c3c16cbdSdanhexio_write test.db [expr {$idxroot*512 - 15}] 888080807f 42df003d61Sdrhsqlite3 db test.db 43df003d61Sdrh 44df003d61Sdrh# Try to use the file. 45df003d61Sdrhdo_test 1.2 { 46df003d61Sdrh catchsql { 47df003d61Sdrh SELECT c FROM t1 WHERE a>'abc'; 48df003d61Sdrh } 49a1f7c0a2Sdrh} {1 {database disk image is malformed}} 50df003d61Sdrhdo_test 1.3 { 51df003d61Sdrh catchsql { 52df003d61Sdrh PRAGMA integrity_check 53df003d61Sdrh } 54a1f7c0a2Sdrh} {1 {database disk image is malformed}} 55df003d61Sdrhdo_test 1.4 { 56df003d61Sdrh catchsql { 57df003d61Sdrh SELECT c FROM t1 ORDER BY a; 58df003d61Sdrh } 59df003d61Sdrh} {1 {database disk image is malformed}} 60df003d61Sdrh 61f5601cacSdrh# Corrupt the same file in a slightly different way. Make the record header 62f5601cacSdrh# sane, but corrupt one of the serial_type value to indicate a huge payload 63f5601cacSdrh# such that the payload begins in allocated space but overflows the buffer. 64f5601cacSdrh# 65f5601cacSdrhdb close 66c3c16cbdSdanhexio_write test.db [expr {$idxroot*512-15}] 0513ff7f01 67f5601cacSdrhsqlite3 db test.db 68f5601cacSdrh 69f5601cacSdrhdo_test 2.1 { 70f5601cacSdrh catchsql { 71af430063Sdrh SELECT rowid FROM t1 WHERE a='abc' and b='xyz123456789XYZ'; 72f5601cacSdrh } 73a1f7c0a2Sdrh} {1 {database disk image is malformed}} 74f5601cacSdrh 75df003d61Sdrhfinish_test 76