AryaWu/sqlite
0
1# 2013-08-012#3# The author disclaims copyright to this source code. In place of4# 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#12 13set testdir [file dirname $argv0]14source $testdir/tester.tcl15set testprefix corruptG16 17# This module uses hard-coded offsets which do not work if the reserved_bytes18# value is nonzero.19if {[nonzero_reserved_bytes]} {finish_test; return;}20 21# These tests deal with corrupt database files22#23database_may_be_corrupt24 25# Create a simple database with a single entry. Then corrupt the26# header-size varint on the index payload so that it maps into a27# negative number. Try to use the database.28#29 30do_execsql_test 1.1 {31 PRAGMA page_size=512;32 CREATE TABLE t1(a,b,c);33 INSERT INTO t1(rowid,a,b,c) VALUES(52,'abc','xyz','123');34 CREATE INDEX t1abc ON t1(a,b,c);35}36 37set idxroot [db one {SELECT rootpage FROM sqlite_master WHERE name = 't1abc'}]38 39# Corrupt the file40db close41hexio_write test.db [expr {$idxroot*512 - 15}] 888080807f42sqlite3 db test.db43 44# Try to use the file.45do_test 1.2 {46 catchsql {47 SELECT c FROM t1 WHERE a>'abc';48 }49} {1 {database disk image is malformed}}50do_test 1.3 {51 catchsql {52 PRAGMA integrity_check53 }54} {1 {database disk image is malformed}}55do_test 1.4 {56 catchsql {57 SELECT c FROM t1 ORDER BY a;58 }59} {1 {database disk image is malformed}}60 61# Corrupt the same file in a slightly different way. Make the record header62# sane, but corrupt one of the serial_type value to indicate a huge payload63# such that the payload begins in allocated space but overflows the buffer.64#65db close66hexio_write test.db [expr {$idxroot*512-15}] 0513ff7f0167sqlite3 db test.db68 69do_test 2.1 {70 catchsql {71 SELECT rowid FROM t1 WHERE a='abc' and b='xyz123456789XYZ';72 }73} {1 {database disk image is malformed}}74 75finish_test76 