Add fuzz3.test. For testing the library's response to corrupted database files. (CVS 5685)

FossilOrigin-Name: 7fd4dd95791974b0ef6cf30a186b32df25ee2211
This commit is contained in:
danielk1977 2008-09-09 18:28:06 +00:00
parent e3602be8fd
commit f2fb044bca
5 changed files with 183 additions and 11 deletions

View File

@ -1,5 +1,5 @@
C Calling\ssqlite3_create_function\swith\snArg==(-1)\sdoes\snot\soverride\sprior\ncalls\son\sthe\ssame\sfunction\sname\swith\snArg>=0.\s\sTicket\s#3345.\s\sAdd\sthe\nnew\s-argcount\soption\sto\sthe\s"function"\smethod\sin\sthe\sTCL\sinterface.\s(CVS\s5684)
D 2008-09-09T12:31:34
C Add\sfuzz3.test.\sFor\stesting\sthe\slibrary's\sresponse\sto\scorrupted\sdatabase\sfiles.\s(CVS\s5685)
D 2008-09-09T18:28:07
F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0
F Makefile.in d15a7ebfe5e057a72a49805ffb302dbb601c8329
F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
@ -203,7 +203,7 @@ F src/where.c a9958b26cc87ea1446fbe6d004a7959b8d5d75c1
F tclinstaller.tcl 4356d9d94d2b5ed5e68f9f0c80c4df3048dd7617
F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2
F test/alias.test c321c114a8a31a33e3cbda910fa39949f5d9dcb2
F test/all.test 89e09ed0074083ac6f208dc3243429e8f89efb69
F test/all.test e15e9df483e1a72ac0c9d6cad216c4c0bdc1c1c9
F test/alter.test 6353aae6839e486c9b7d8f73b1f4a1e98e57332c
F test/alter2.test dd55146e812622c8fc51fd2216bcd8dca8880752
F test/alter3.test 25b95a136708f22b87184fa6a4309eea03d65153
@ -353,6 +353,7 @@ F test/fts3near.test 2d4dadcaac5025ab65bb87e66c45f39e92966194
F test/func.test 628dc9b321fc66dd6d055fca6525e157004744e1
F test/fuzz.test 62fc19dd36a427777fd671b569df07166548628a
F test/fuzz2.test ea38692ce2da99ad79fe0be5eb1a452c1c4d37bb
F test/fuzz3.test 5fa227c60207a18077d629144a178dd83a4ec8b9
F test/fuzz_common.tcl ff4bc2dfc465f6878f8e2d819620914365382731
F test/fuzz_malloc.test 4eca9d345f06d5b0b0105f7a2ef9e7f22658827b
F test/hook.test e17d4ed2843ba4ef9b234aa63e6f056bf88f9a19
@ -456,7 +457,7 @@ F test/pragma2.test 5364893491b9231dd170e3459bfc2e2342658b47
F test/printf.test 262a5acd3158f788e9bdf7f18d718f3af32ff6ef
F test/progress.test 5b075c3c790c7b2a61419bc199db87aaf48b8301 x
F test/ptrchng.test ef1aa72d6cf35a2bbd0869a649b744e9d84977fc
F test/quick.test ab9399a71983f67113c04bab331edb59b950e2d6
F test/quick.test bbaa1fbdac766cab2219d5599e9cd5bc2614d01e
F test/quote.test 215897dbe8de1a6f701265836d6601cc6ed103e6
F test/rdonly.test bd054831f8a3078e765a0657e247182486f0cb47
F test/reindex.test 44edd3966b474468b823d481eafef0c305022254
@ -633,7 +634,7 @@ F tool/speedtest16.c c8a9c793df96db7e4933f0852abb7a03d48f2e81
F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
F tool/speedtest8.c 1dbced29de5f59ba2ebf877edcadf171540374d1
F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
P a6dee85b823355cb381163c93c8366aa4395ae6a
R ef041795a1f812a73913c8f5ffd83ed9
U drh
Z ef224dd97bd8e2824d90a691c3098d84
P 5aa5b8044a14f59559c1839dc0799b0d2f990809
R dc12ffc18ad6820a14094d33496f85b8
U danielk1977
Z bd924448786b0f6a62ff458ca7bca6c4

View File

@ -1 +1 @@
5aa5b8044a14f59559c1839dc0799b0d2f990809
7fd4dd95791974b0ef6cf30a186b32df25ee2211

View File

@ -10,7 +10,7 @@
#***********************************************************************
# This file runs all tests.
#
# $Id: all.test,v 1.57 2008/07/14 15:11:20 drh Exp $
# $Id: all.test,v 1.58 2008/09/09 18:28:07 danielk1977 Exp $
set testdir [file dirname $argv0]
source $testdir/tester.tcl
@ -64,6 +64,7 @@ lappend EXCLUDE misuse.test ;# Run seperately later.
lappend EXCLUDE memleak.test ;# Alternate test driver script
lappend EXCLUDE permutations.test ;# Run seperately later.
lappend EXCLUDE fuzz.test
lappend EXCLUDE fuzz3.test
lappend EXCLUDE soak.test ;# Takes a very long time (default 1 hr)
lappend EXCLUDE fts3.test ;# Wrapper for muliple fts3*.tests
lappend EXCLUDE mallocAll.test ;# Wrapper for running all malloc tests

169
test/fuzz3.test Normal file
View File

@ -0,0 +1,169 @@
# 2007 May 10
#
# 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. The focus
# of this file is checking the libraries response to subtly corrupting
# the database file by changing the values of pseudo-randomly selected
# bytes.
#
# $Id: fuzz3.test,v 1.1 2008/09/09 18:28:07 danielk1977 Exp $
set testdir [file dirname $argv0]
source $testdir/tester.tcl
expr srand(123)
proc rstring {n} {
set str s
while {[string length $str] < $n} {
append str [expr rand()]
}
return [string range $str 0 $n]
}
# Return a randomly generated SQL literal.
#
proc rvalue {} {
switch -- [expr int(rand()*5)] {
0 { # SQL NULL value.
return NULL
}
1 { # Integer value.
return [expr int(rand()*1024)]
}
2 { # Real value.
return [expr rand()]
}
3 { # String value.
set n [expr int(rand()*2500)]
return "'[rstring $n]'"
}
4 { # Blob value.
set n [expr int(rand()*2500)]
return "CAST('[rstring $n]' AS BLOB)"
}
}
}
proc db_checksum {} {
set cksum [execsql { SELECT md5sum(a, b, c) FROM t1 }]
append cksum [execsql { SELECT md5sum(d, e, f) FROM t2 }]
set cksum
}
# Modify a single byte in the file 'test.db' using tcl IO commands. The
# argument value, which must be an integer, determines both the offset of
# the byte that is modified, and the value that it is set to. The lower
# 8 bits of iMod determine the new byte value. The offset of the byte
# modified is the value of ($iMod >> 8).
#
# The return value is the iMod value required to restore the file
# to its original state. The command:
#
# modify_database [modify_database $x]
#
# leaves the file in the same state as it was in at the start of the
# command (assuming that the file is at least ($x>>8) bytes in size).
#
proc modify_database {iMod} {
set blob [binary format c [expr {$iMod&0xFF}]]
set offset [expr {$iMod>>8}]
set fd [open test.db r+]
fconfigure $fd -encoding binary -translation binary
seek $fd $offset
set old_blob [read $fd 1]
seek $fd $offset
puts -nonewline $fd $blob
close $fd
binary scan $old_blob c iOld
return [expr {($offset<<8) + ($iOld&0xFF)}]
}
proc purge_pcache {} {
ifcapable !memorymanage {
db close
sqlite3 db test.db
} else {
sqlite3_release_memory 10000000
}
if {[lindex [pcache_stats] 1] != 0} {
error "purge_pcache failed: [pcache_stats]"
}
}
# This block creates a database to work with.
#
do_test fuzz3-1 {
execsql {
BEGIN;
CREATE TABLE t1(a, b, c);
CREATE TABLE t2(d, e, f);
CREATE INDEX i1 ON t1(a, b, c);
CREATE INDEX i2 ON t2(d, e, f);
}
for {set i 0} {$i < 50} {incr i} {
execsql "INSERT INTO t1 VALUES([rvalue], [rvalue], [rvalue])"
execsql "INSERT INTO t2 VALUES([rvalue], [rvalue], [rvalue])"
}
execsql COMMIT
} {}
set ::cksum [db_checksum]
do_test fuzz3-2 {
db_checksum
} $::cksum
for {set ii 0} {$ii < 10000} {incr ii} {
purge_pcache
# Randomly modify a single byte of the database file somewhere within
# the first 100KB of the file.
set iNew [expr int(rand()*5*1024*256)]
set iOld [modify_database $iNew]
set iTest 2
foreach sql {
{SELECT * FROM t1}
{SELECT * FROM t2}
{SELECT * FROM t1 ORDER BY a}
{SELECT * FROM t2 ORDER BY d}
{SELECT * FROM t1 WHERE a = (SELECT a FROM t1 WHERE rowid=25)}
{SELECT * FROM t2 WHERE d = (SELECT d FROM t2 WHERE rowid=1)}
{SELECT * FROM t2 WHERE d = (SELECT d FROM t2 WHERE rowid=50)}
{PRAGMA integrity_check}
} {
do_test fuzz3-$ii.$iNew.[incr iTest] {
foreach {rc msg} [catchsql $sql] {}
if {$rc == 0
|| $msg eq "database disk image is malformed"
|| $msg eq "file is encrypted or is not a database"
|| [string match "malformed database schema*" $msg]
} {
set msg ok
}
set msg
} {ok}
}
# Restore the original database file content. Test that the correct
# checksum is now returned.
#
purge_pcache
modify_database $iOld
do_test fuzz3-$ii.$iNew.[incr iTest] {
db_checksum
} $::cksum
}
finish_test

View File

@ -6,7 +6,7 @@
#***********************************************************************
# This file runs all tests.
#
# $Id: quick.test,v 1.86 2008/08/28 08:31:48 danielk1977 Exp $
# $Id: quick.test,v 1.87 2008/09/09 18:28:07 danielk1977 Exp $
proc lshift {lvar} {
upvar $lvar l
@ -54,6 +54,7 @@ set EXCLUDE {
delete3.test
fts3.test
fuzz.test
fuzz3.test
fuzz_malloc.test
in2.test
loadext.test