wasCSharpSQLite – Blame information for rev 4

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 # 2007 May 10
2 #
3 # The author disclaims copyright to this source code. In place of
4 # 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 # 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
14 # bytes.
15 #
16 # $Id: fuzz3.test,v 1.3 2009/01/05 17:19:03 drh Exp $
17  
18 set testdir [file dirname $argv0]
19 source $testdir/tester.tcl
20  
21 # Do not use a codec for tests in this file, as the database file is
22 # manipulated directly using tcl scripts. See proc [set_file_format].
23 #
24 do_not_use_codec
25  
26 expr srand(123)
27  
28 proc rstring {n} {
29 set str s
30 while {[string length $str] < $n} {
31 append str [expr rand()]
32 }
33 return [string range $str 0 $n]
34 }
35  
36 # Return a randomly generated SQL literal.
37 #
38 proc rvalue {} {
39 switch -- [expr int(rand()*5)] {
40  
41 return NULL
42 }
43 1 { # Integer value.
44 return [expr int(rand()*1024)]
45 }
46 2 { # Real value.
47 return [expr rand()]
48 }
49 3 { # String value.
50 set n [expr int(rand()*2500)]
51 return "'[rstring $n]'"
52 }
53 4 { # Blob value.
54 set n [expr int(rand()*2500)]
55 return "CAST('[rstring $n]' AS BLOB)"
56 }
57 }
58 }
59  
60 proc db_checksum {} {
61 set cksum [execsql { SELECT md5sum(a, b, c) FROM t1 }]
62 append cksum [execsql { SELECT md5sum(d, e, f) FROM t2 }]
63 set cksum
64 }
65  
66 # Modify a single byte in the file 'test.db' using tcl IO commands. The
67 # argument value, which must be an integer, determines both the offset of
68 # the byte that is modified, and the value that it is set to. The lower
69 # 8 bits of iMod determine the new byte value. The offset of the byte
70 # modified is the value of ($iMod >> 8).
71 #
72 # The return value is the iMod value required to restore the file
73 # to its original state. The command:
74 #
75 # modify_database [modify_database $x]
76 #
77 # leaves the file in the same state as it was in at the start of the
78 # command (assuming that the file is at least ($x>>8) bytes in size).
79 #
80 proc modify_database {iMod} {
81 set blob [binary format c [expr {$iMod&0xFF}]]
82 set offset [expr {$iMod>>8}]
83  
84 set fd [open test.db r+]
85 fconfigure $fd -encoding binary -translation binary
86 seek $fd $offset
87 set old_blob [read $fd 1]
88 seek $fd $offset
89 puts -nonewline $fd $blob
90 close $fd
91  
92 binary scan $old_blob c iOld
93 return [expr {($offset<<8) + ($iOld&0xFF)}]
94 }
95  
96 proc purge_pcache {} {
97 ifcapable !memorymanage {
98 db close
99 sqlite3 db test.db
100 } else {
101 sqlite3_release_memory 10000000
102 }
103 if {[lindex [pcache_stats] 1] != 0} {
104 error "purge_pcache failed: [pcache_stats]"
105 }
106 }
107  
108 # This block creates a database to work with.
109 #
110 do_test fuzz3-1 {
111 execsql {
112 BEGIN;
113 CREATE TABLE t1(a, b, c);
114 CREATE TABLE t2(d, e, f);
115 CREATE INDEX i1 ON t1(a, b, c);
116 CREATE INDEX i2 ON t2(d, e, f);
117 }
118 for {set i 0} {$i < 50} {incr i} {
119 execsql "INSERT INTO t1 VALUES([rvalue], [rvalue], [rvalue])"
120 execsql "INSERT INTO t2 VALUES([rvalue], [rvalue], [rvalue])"
121 }
122 execsql COMMIT
123 } {}
124  
125 set ::cksum [db_checksum]
126 do_test fuzz3-2 {
127 db_checksum
128 } $::cksum
129  
130 for {set ii 0} {$ii < 5000} {incr ii} {
131 purge_pcache
132  
133 # Randomly modify a single byte of the database file somewhere within
134 # the first 100KB of the file.
135 set iNew [expr int(rand()*5*1024*256)]
136 set iOld [modify_database $iNew]
137  
138 set iTest 0
139 foreach sql {
140 {SELECT * FROM t2 ORDER BY d}
141 {SELECT * FROM t1}
142 {SELECT * FROM t2}
143 {SELECT * FROM t1 ORDER BY a}
144 {SELECT * FROM t1 WHERE a = (SELECT a FROM t1 WHERE rowid=25)}
145 {SELECT * FROM t2 WHERE d = (SELECT d FROM t2 WHERE rowid=1)}
146 {SELECT * FROM t2 WHERE d = (SELECT d FROM t2 WHERE rowid=50)}
147 {PRAGMA integrity_check}
148 } {
149 do_test fuzz3-$ii.$iNew.[incr iTest] {
150 foreach {rc msg} [catchsql $sql] {}
151 if {$rc == 0
152 || $msg eq "database or disk is full"
153 || $msg eq "database disk image is malformed"
154 || $msg eq "file is encrypted or is not a database"
155 || [string match "malformed database schema*" $msg]
156 } {
157 set msg ok
158 }
159 set msg
160 } {ok}
161 }
162  
163 # Restore the original database file content. Test that the correct
164 # checksum is now returned.
165 #
166 purge_pcache
167 modify_database $iOld
168 do_test fuzz3-$ii.$iNew.[incr iTest] {
169 db_checksum
170 } $::cksum
171 }
172  
173 finish_test