2008-10-31 14:57:40 +01:00
|
|
|
# 2004 August 30
|
|
|
|
#
|
|
|
|
# The author disclaims copyright to this source code. In place of
|
|
|
|
# a legal notice, here is a blessing:
|
|
|
|
#
|
|
|
|
# May you do good and not evil.
|
|
|
|
# May you find forgiveness for yourself and forgive others.
|
|
|
|
# May you share freely, never taking more than you give.
|
|
|
|
#
|
|
|
|
#***********************************************************************
|
|
|
|
# This file implements regression tests for SQLite library.
|
|
|
|
#
|
|
|
|
# This file implements tests to make sure SQLite does not crash or
|
|
|
|
# segfault if it sees a corrupt database file. It creates a base
|
|
|
|
# data base file, then tests that single byte corruptions in
|
|
|
|
# increasingly larger quantities are handled gracefully.
|
|
|
|
#
|
2008-11-11 21:51:50 +01:00
|
|
|
# $Id: corruptC.test,v 1.3 2008/11/11 20:51:51 shane Exp $
|
2008-10-31 14:57:40 +01:00
|
|
|
|
|
|
|
catch {file delete -force test.db test.db-journal test.bu}
|
|
|
|
|
|
|
|
set testdir [file dirname $argv0]
|
|
|
|
source $testdir/tester.tcl
|
|
|
|
|
2008-11-11 18:36:30 +01:00
|
|
|
# Set a uniform random seed
|
|
|
|
expr srand(0)
|
|
|
|
|
2008-10-31 14:57:40 +01:00
|
|
|
# Construct a compact, dense database for testing.
|
|
|
|
#
|
|
|
|
do_test corruptC-1.1 {
|
|
|
|
execsql {
|
|
|
|
BEGIN;
|
2008-11-11 21:51:50 +01:00
|
|
|
CREATE TABLE t1(x,y);
|
|
|
|
INSERT INTO t1 VALUES(1,1);
|
|
|
|
INSERT OR IGNORE INTO t1 SELECT x*2,y FROM t1;
|
|
|
|
INSERT OR IGNORE INTO t1 SELECT x*3,y FROM t1;
|
|
|
|
INSERT OR IGNORE INTO t1 SELECT x*5,y FROM t1;
|
|
|
|
INSERT OR IGNORE INTO t1 SELECT x*7,y FROM t1;
|
|
|
|
INSERT OR IGNORE INTO t1 SELECT x*11,y FROM t1;
|
|
|
|
INSERT OR IGNORE INTO t1 SELECT x*13,y FROM t1;
|
2008-10-31 14:57:40 +01:00
|
|
|
CREATE INDEX t1i1 ON t1(x);
|
2008-11-11 21:51:50 +01:00
|
|
|
CREATE TABLE t2 AS SELECT x,2 FROM t1 WHERE rowid%5!=0;
|
2008-10-31 14:57:40 +01:00
|
|
|
COMMIT;
|
|
|
|
}
|
|
|
|
} {}
|
|
|
|
|
|
|
|
ifcapable {integrityck} {
|
|
|
|
integrity_check corruptC-1.2
|
|
|
|
}
|
|
|
|
|
|
|
|
# Generate random integer
|
|
|
|
#
|
|
|
|
proc random {range} {
|
|
|
|
return [expr {round(rand()*$range)}]
|
|
|
|
}
|
|
|
|
|
|
|
|
# Copy file $from into $to
|
|
|
|
#
|
|
|
|
proc copy_file {from to} {
|
|
|
|
set f [open $from]
|
|
|
|
fconfigure $f -translation binary
|
|
|
|
set t [open $to w]
|
|
|
|
fconfigure $t -translation binary
|
|
|
|
puts -nonewline $t [read $f [file size $from]]
|
|
|
|
close $t
|
|
|
|
close $f
|
|
|
|
}
|
|
|
|
|
|
|
|
# Setup for the tests. Make a backup copy of the good database in test.bu.
|
|
|
|
#
|
2008-11-11 21:51:50 +01:00
|
|
|
db close
|
2008-10-31 14:57:40 +01:00
|
|
|
copy_file test.db test.bu
|
2008-11-11 21:51:50 +01:00
|
|
|
sqlite3 db test.db
|
2008-10-31 14:57:40 +01:00
|
|
|
set fsize [file size test.db]
|
|
|
|
|
2008-11-11 18:36:30 +01:00
|
|
|
#
|
|
|
|
# first test some specific corruption tests found from earlier runs
|
|
|
|
#
|
|
|
|
|
|
|
|
# test that a corrupt content offset size is handled (seed 5577)
|
|
|
|
do_test corruptC-2.1 {
|
|
|
|
db close
|
|
|
|
copy_file test.bu test.db
|
|
|
|
|
|
|
|
# insert corrupt byte(s)
|
2008-11-11 21:51:50 +01:00
|
|
|
hexio_write test.db 2053 [format %02x 0x04]
|
2008-11-11 18:36:30 +01:00
|
|
|
|
|
|
|
sqlite3 db test.db
|
|
|
|
catchsql {PRAGMA integrity_check}
|
|
|
|
} {0 {{*** in database main ***
|
|
|
|
Corruption detected in header on page 3
|
|
|
|
Multiple uses for byte 604 of page 3}}}
|
|
|
|
|
2008-11-11 21:51:50 +01:00
|
|
|
# test that a corrupt content offset size is handled (seed 5649)
|
|
|
|
do_test corruptC-2.2 {
|
|
|
|
db close
|
|
|
|
copy_file test.bu test.db
|
|
|
|
|
|
|
|
# insert corrupt byte(s)
|
|
|
|
hexio_write test.db 27 [format %02x 0x08]
|
|
|
|
hexio_write test.db 233 [format %02x 0x6a]
|
|
|
|
hexio_write test.db 328 [format %02x 0x67]
|
|
|
|
hexio_write test.db 750 [format %02x 0x1f]
|
|
|
|
hexio_write test.db 1132 [format %02x 0x52]
|
|
|
|
hexio_write test.db 1133 [format %02x 0x84]
|
|
|
|
hexio_write test.db 1220 [format %02x 0x01]
|
|
|
|
hexio_write test.db 3688 [format %02x 0xc1]
|
|
|
|
hexio_write test.db 3714 [format %02x 0x58]
|
|
|
|
hexio_write test.db 3746 [format %02x 0x9a]
|
|
|
|
|
|
|
|
sqlite3 db test.db
|
|
|
|
catchsql {UPDATE t1 SET y=1}
|
|
|
|
} {0 {}}
|
2008-11-11 18:36:30 +01:00
|
|
|
|
|
|
|
#
|
|
|
|
# now test for a series of quasi-random seeds
|
|
|
|
#
|
|
|
|
for {set tn 0} {$tn<=1024} {incr tn 1} {
|
|
|
|
|
|
|
|
# Set a quasi-random random seed
|
|
|
|
expr srand($tn)
|
2008-10-31 14:57:40 +01:00
|
|
|
|
|
|
|
# setup for test
|
|
|
|
db close
|
|
|
|
copy_file test.bu test.db
|
2008-11-11 21:51:50 +01:00
|
|
|
sqlite3 db test.db
|
2008-10-31 14:57:40 +01:00
|
|
|
|
|
|
|
# Seek to a random location in the file, and write a random single byte
|
|
|
|
# value. Then do various operations on the file to make sure that
|
|
|
|
# the database engine can handle the corruption gracefully.
|
|
|
|
#
|
|
|
|
set last 0
|
|
|
|
for {set i 1} {$i<=1024 && !$last} {incr i 1} {
|
|
|
|
|
|
|
|
# insert random byte at random location
|
2008-11-11 21:51:50 +01:00
|
|
|
db close
|
2008-11-11 18:36:30 +01:00
|
|
|
hexio_write test.db [random $fsize] [format %02x [random 255]]
|
2008-11-11 21:51:50 +01:00
|
|
|
sqlite3 db test.db
|
2008-10-31 14:57:40 +01:00
|
|
|
|
|
|
|
# do a few random operations to make sure that if
|
|
|
|
# they error, they error gracefully instead of crashing.
|
2008-11-11 18:36:30 +01:00
|
|
|
do_test corruptC-3.$tn.$i.1 {
|
2008-10-31 14:57:40 +01:00
|
|
|
catchsql {SELECT count(*) FROM sqlite_master}
|
|
|
|
set x {}
|
|
|
|
} {}
|
2008-11-11 18:36:30 +01:00
|
|
|
do_test corruptC-3.$tn.$i.2 {
|
2008-10-31 14:57:40 +01:00
|
|
|
catchsql {SELECT count(*) FROM t1}
|
|
|
|
set x {}
|
|
|
|
} {}
|
2008-11-11 18:36:30 +01:00
|
|
|
do_test corruptC-3.$tn.$i.3 {
|
2008-10-31 14:57:40 +01:00
|
|
|
catchsql {SELECT count(*) FROM t1 WHERE x>13}
|
|
|
|
set x {}
|
|
|
|
} {}
|
2008-11-11 18:36:30 +01:00
|
|
|
do_test corruptC-3.$tn.$i.4 {
|
2008-10-31 14:57:40 +01:00
|
|
|
catchsql {SELECT count(*) FROM t2}
|
|
|
|
set x {}
|
|
|
|
} {}
|
2008-11-11 18:36:30 +01:00
|
|
|
do_test corruptC-3.$tn.$i.5 {
|
2008-10-31 14:57:40 +01:00
|
|
|
catchsql {SELECT count(*) FROM t2 WHERE x<13}
|
|
|
|
set x {}
|
|
|
|
} {}
|
2008-11-11 21:51:50 +01:00
|
|
|
do_test corruptC-3.$tn.$i.6 {
|
|
|
|
catchsql {UPDATE t1 SET y=1}
|
|
|
|
set x {}
|
|
|
|
} {}
|
|
|
|
do_test corruptC-3.$tn.$i.7 {
|
|
|
|
catchsql {UPDATE t2 SET y=2}
|
|
|
|
set x {}
|
|
|
|
} {}
|
2008-10-31 14:57:40 +01:00
|
|
|
|
|
|
|
# check the integrity of the database.
|
|
|
|
# once the corruption is detected, we can stop.
|
|
|
|
ifcapable {integrityck} {
|
|
|
|
set res [ catchsql {PRAGMA integrity_check} ]
|
|
|
|
set ans [lindex $res 1]
|
|
|
|
if { [ string compare $ans "ok" ] != 0 } {
|
|
|
|
set last -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# if we are not capable of doing an integrity check,
|
|
|
|
# stop after corrupting 5 bytes.
|
|
|
|
ifcapable {!integrityck} {
|
|
|
|
if { $i > 5 } {
|
|
|
|
set last -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check that no page references were leaked.
|
2008-11-11 21:51:50 +01:00
|
|
|
do_test corruptC-3.$tn.$i.8 {
|
2008-10-31 14:57:40 +01:00
|
|
|
set bt [btree_from_db db]
|
|
|
|
db_enter db
|
|
|
|
array set stats [btree_pager_stats $bt]
|
|
|
|
db_leave db
|
|
|
|
set stats(ref)
|
|
|
|
} {0}
|
|
|
|
}
|
|
|
|
# end for i
|
|
|
|
|
|
|
|
}
|
|
|
|
# end for tn
|
|
|
|
|
|
|
|
finish_test
|