3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The focus
12 # of this file is checking the libraries response to subtly corrupting
13 # the database file by changing the values of pseudo-randomly selected
16 # $Id: fuzz3.test,v 1.3 2009/01/05 17:19:03 drh Exp $
18 set testdir [file dirname $argv0]
19 source $testdir/tester.tcl
21 # These tests deal with corrupt database files
23 database_may_be_corrupt
29 while {[string length $str] < $n} {
30 append str [expr rand()]
32 return [string range $str 0 $n]
35 # Return a randomly generated SQL literal.
38 switch -- [expr int(rand()*5)] {
43 return [expr int(rand()*1024)]
49 set n [expr int(rand()*2500)]
50 return "'[rstring $n]'"
53 set n [expr int(rand()*2500)]
54 return "CAST('[rstring $n]' AS BLOB)"
60 set cksum [execsql { SELECT md5sum(a, b, c) FROM t1 }]
61 append cksum [execsql { SELECT md5sum(d, e, f) FROM t2 }]
65 # Modify a single byte in the file 'test.db' using tcl IO commands. The
66 # argument value, which must be an integer, determines both the offset of
67 # the byte that is modified, and the value that it is set to. The lower
68 # 8 bits of iMod determine the new byte value. The offset of the byte
69 # modified is the value of ($iMod >> 8).
71 # The return value is the iMod value required to restore the file
72 # to its original state. The command:
74 # modify_database [modify_database $x]
76 # leaves the file in the same state as it was in at the start of the
77 # command (assuming that the file is at least ($x>>8) bytes in size).
79 proc modify_database {iMod} {
80 set blob [binary format c [expr {$iMod&0xFF}]]
81 set offset [expr {$iMod>>8}]
83 set fd [open test.db r+]
84 fconfigure $fd -encoding binary -translation binary
86 set old_blob [read $fd 1]
88 puts -nonewline $fd $blob
91 binary scan $old_blob c iOld
92 return [expr {($offset<<8) + ($iOld&0xFF)}]
95 proc purge_pcache {} {
96 ifcapable !memorymanage {
100 sqlite3_release_memory 10000000
102 if {[lindex [pcache_stats] 1] != 0} {
103 error "purge_pcache failed: [pcache_stats]"
107 # This block creates a database to work with.
112 CREATE TABLE t1(a, b, c);
113 CREATE TABLE t2(d, e, f);
114 CREATE INDEX i1 ON t1(a, b, c);
115 CREATE INDEX i2 ON t2(d, e, f);
117 for {set i 0} {$i < 50} {incr i} {
118 execsql "INSERT INTO t1 VALUES([rvalue], [rvalue], [rvalue])"
119 execsql "INSERT INTO t2 VALUES([rvalue], [rvalue], [rvalue])"
124 set ::cksum [db_checksum]
129 for {set ii 0} {$ii < 5000} {incr ii} {
132 # Randomly modify a single byte of the database file somewhere within
133 # the first 100KB of the file.
134 set iNew [expr int(rand()*5*1024*256)]
135 set iOld [modify_database $iNew]
139 {SELECT * FROM t2 ORDER BY d}
142 {SELECT * FROM t1 ORDER BY a}
143 {SELECT * FROM t1 WHERE a = (SELECT a FROM t1 WHERE rowid=25)}
144 {SELECT * FROM t2 WHERE d = (SELECT d FROM t2 WHERE rowid=1)}
145 {SELECT * FROM t2 WHERE d = (SELECT d FROM t2 WHERE rowid=50)}
146 {PRAGMA integrity_check}
148 do_test fuzz3-$ii.$iNew.[incr iTest] {
149 foreach {rc msg} [catchsql $sql] {}
151 || $msg eq "database or disk is full"
152 || $msg eq "database disk image is malformed"
153 || $msg eq "file is encrypted or is not a database"
154 || [string match "malformed database schema*" $msg]
162 # Restore the original database file content. Test that the correct
163 # checksum is now returned.
166 modify_database $iOld
167 do_test fuzz3-$ii.$iNew.[incr iTest] {