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
26 while {[string length $str] < $n} {
27 append str [expr rand()]
29 return [string range $str 0 $n]
32 # Return a randomly generated SQL literal.
35 switch -- [expr int(rand()*5)] {
40 return [expr int(rand()*1024)]
46 set n [expr int(rand()*2500)]
47 return "'[rstring $n]'"
50 set n [expr int(rand()*2500)]
51 return "CAST('[rstring $n]' AS BLOB)"
57 set cksum [execsql { SELECT md5sum(a, b, c) FROM t1 }]
58 append cksum [execsql { SELECT md5sum(d, e, f) FROM t2 }]
62 # Modify a single byte in the file 'test.db' using tcl IO commands. The
63 # argument value, which must be an integer, determines both the offset of
64 # the byte that is modified, and the value that it is set to. The lower
65 # 8 bits of iMod determine the new byte value. The offset of the byte
66 # modified is the value of ($iMod >> 8).
68 # The return value is the iMod value required to restore the file
69 # to its original state. The command:
71 # modify_database [modify_database $x]
73 # leaves the file in the same state as it was in at the start of the
74 # command (assuming that the file is at least ($x>>8) bytes in size).
76 proc modify_database {iMod} {
77 set blob [binary format c [expr {$iMod&0xFF}]]
78 set offset [expr {$iMod>>8}]
80 set fd [open test.db r+]
81 fconfigure $fd -encoding binary -translation binary
83 set old_blob [read $fd 1]
85 puts -nonewline $fd $blob
88 binary scan $old_blob c iOld
89 return [expr {($offset<<8) + ($iOld&0xFF)}]
92 proc purge_pcache {} {
93 ifcapable !memorymanage {
97 sqlite3_release_memory 10000000
99 if {[lindex [pcache_stats] 1] != 0} {
100 error "purge_pcache failed: [pcache_stats]"
104 # This block creates a database to work with.
109 CREATE TABLE t1(a, b, c);
110 CREATE TABLE t2(d, e, f);
111 CREATE INDEX i1 ON t1(a, b, c);
112 CREATE INDEX i2 ON t2(d, e, f);
114 for {set i 0} {$i < 50} {incr i} {
115 execsql "INSERT INTO t1 VALUES([rvalue], [rvalue], [rvalue])"
116 execsql "INSERT INTO t2 VALUES([rvalue], [rvalue], [rvalue])"
121 set ::cksum [db_checksum]
126 for {set ii 0} {$ii < 5000} {incr ii} {
129 # Randomly modify a single byte of the database file somewhere within
130 # the first 100KB of the file.
131 set iNew [expr int(rand()*5*1024*256)]
132 set iOld [modify_database $iNew]
136 {SELECT * FROM t2 ORDER BY d}
139 {SELECT * FROM t1 ORDER BY a}
140 {SELECT * FROM t1 WHERE a = (SELECT a FROM t1 WHERE rowid=25)}
141 {SELECT * FROM t2 WHERE d = (SELECT d FROM t2 WHERE rowid=1)}
142 {SELECT * FROM t2 WHERE d = (SELECT d FROM t2 WHERE rowid=50)}
143 {PRAGMA integrity_check}
145 do_test fuzz3-$ii.$iNew.[incr iTest] {
146 foreach {rc msg} [catchsql $sql] {}
148 || $msg eq "database or disk is full"
149 || $msg eq "database disk image is malformed"
150 || $msg eq "file is encrypted or is not a database"
151 || [string match "malformed database schema*" $msg]
159 # Restore the original database file content. Test that the correct
160 # checksum is now returned.
163 modify_database $iOld
164 do_test fuzz3-$ii.$iNew.[incr iTest] {