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.
13 # This file implements tests to make sure SQLite does not crash or
14 # segfault if it sees a corrupt database file. It specifically focuses
15 # on corrupt SerialTypeLen values.
17 # $Id: corrupt6.test,v 1.2 2008/05/19 15:37:10 shane Exp $
19 set testdir [file dirname $argv0]
20 source $testdir/tester.tcl
22 # Do not use a codec for tests in this file, as the database file is
23 # manipulated directly using tcl scripts (using the [hexio_write] command).
27 # These tests deal with corrupt database files
29 database_may_be_corrupt
31 # We must have the page_size pragma for these tests to work.
33 ifcapable !pager_pragmas {
38 # Create a simple, small database.
40 do_test corrupt6-1.1 {
42 PRAGMA auto_vacuum=OFF;
43 PRAGMA page_size=1024;
45 INSERT INTO t1(x) VALUES('varint32-01234567890123456789012345678901234567890123456789');
46 INSERT INTO t1(x) VALUES('varint32-01234567890123456789012345678901234567890123456789');
51 # Verify that the file format is as we expect. The page size
52 # should be 1024 bytes.
54 do_test corrupt6-1.2 {
55 hexio_get_int [hexio_read test.db 16 2]
56 } 1024 ;# The page size is 1024
57 do_test corrupt6-1.3 {
58 hexio_get_int [hexio_read test.db 20 1]
59 } 0 ;# Unused bytes per page is 0
61 integrity_check corrupt6-1.4
63 # Verify SerialTypeLen for first field of two records as we expect.
64 # SerialTypeLen = (len*2+12) = 60*2+12 = 132
65 do_test corrupt6-1.5.1 {
66 hexio_read test.db 1923 2
67 } 8103 ;# First text field size is 81 03 == 131
68 do_test corrupt6-1.5.2 {
69 hexio_read test.db 1987 2
70 } 8103 ;# Second text field size is 81 03 == 131
72 # Verify simple query results as expected.
73 do_test corrupt6-1.6 {
77 SELECT substr(x,1,8) FROM t1
79 } [list 0 {varint32 varint32} ]
80 integrity_check corrupt6-1.7
82 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
83 # corruption is detected.
84 # Increase SerialTypeLen by 2.
85 do_test corrupt6-1.8.1 {
87 hexio_write test.db 1923 8105
90 SELECT substr(x,1,8) FROM t1
92 } [list 1 {database disk image is malformed}]
94 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
95 # corruption is detected.
96 # Decrease SerialTypeLen by 2.
97 do_test corrupt6-1.8.2 {
99 hexio_write test.db 1923 8101
102 SELECT substr(x,1,8) FROM t1
104 } [list 1 {database disk image is malformed}]
106 # Put value of record 1 / field 1 SerialTypeLen back.
107 do_test corrupt6-1.8.3 {
109 hexio_write test.db 1923 8103
112 SELECT substr(x,1,8) FROM t1
114 } [list 0 {varint32 varint32} ]
115 integrity_check corrupt6-1.8.4
117 # Adjust value of record 2 / field 1 SerialTypeLen and see if the
118 # corruption is detected.
119 # Increase SerialTypeLen by 2.
120 do_test corrupt6-1.9.1 {
122 hexio_write test.db 1987 8105
125 SELECT substr(x,1,8) FROM t1
127 } [list 1 {database disk image is malformed}]
129 # Adjust value of record 2 / field 2 SerialTypeLen and see if the
130 # corruption is detected.
131 # Decrease SerialTypeLen by 2.
132 do_test corrupt6-1.9.2 {
134 hexio_write test.db 1987 8101
137 SELECT substr(x,1,8) FROM t1
139 } [list 1 {database disk image is malformed}]
141 # Put value of record 1 / field 2 SerialTypeLen back.
142 do_test corrupt6-1.9.3 {
144 hexio_write test.db 1987 8103
147 SELECT substr(x,1,8) FROM t1
149 } [list 0 {varint32 varint32} ]
150 integrity_check corrupt6-1.9.4
152 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
153 # corruption is detected.
154 # Set SerialTypeLen to FF 7F (2 bytes)
155 do_test corrupt6-1.10.1 {
157 hexio_write test.db 1923 FF7F
160 SELECT substr(x,1,8) FROM t1
162 } [list 1 {database disk image is malformed}]
164 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
165 # corruption is detected.
166 # Set SerialTypeLen to FF FF 7F (3 bytes)
167 do_test corrupt6-1.10.2 {
169 hexio_write test.db 1923 FFFF7F
172 SELECT substr(x,1,8) FROM t1
174 } [list 1 {database disk image is malformed}]
176 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
177 # corruption is detected.
178 # Set SerialTypeLen to FF FF FF 7F (4 bytes)
179 do_test corrupt6-1.10.3 {
181 hexio_write test.db 1923 FFFFFF7F
184 SELECT substr(x,1,8) FROM t1
186 } [list 1 {database disk image is malformed}]
188 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
189 # corruption is detected.
190 # Set SerialTypeLen to FF FF FF FF 7F (5 bytes)
191 do_test corrupt6-1.10.4 {
193 hexio_write test.db 1923 FFFFFFFF7F
196 SELECT substr(x,1,8) FROM t1
198 } [list 1 {database disk image is malformed}]
200 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
201 # corruption is detected.
202 # Set SerialTypeLen to FF FF FF FF FF 7F (6 bytes, and overflows).
203 do_test corrupt6-1.10.5 {
205 hexio_write test.db 1923 FFFFFFFFFF7F
208 SELECT substr(x,1,8) FROM t1
210 } [list 1 {database disk image is malformed}]
212 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
213 # corruption is detected.
214 # Set SerialTypeLen to FF FF FF FF FF FF 7F (7 bytes, and overflows).
215 do_test corrupt6-1.10.6 {
217 hexio_write test.db 1923 FFFFFFFFFFFF7F
220 SELECT substr(x,1,8) FROM t1
222 } [list 1 {database disk image is malformed}]
224 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
225 # corruption is detected.
226 # Set SerialTypeLen to FF FF FF FF FF FF FF 7F (8 bytes, and overflows).
227 do_test corrupt6-1.10.7 {
229 hexio_write test.db 1923 FFFFFFFFFFFFFF7F
232 SELECT substr(x,1,8) FROM t1
234 } [list 1 {database disk image is malformed}]
236 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
237 # corruption is detected.
238 # Set SerialTypeLen to FF FF FF FF FF FF FF FF 7F (9 bytes, and overflows).
239 do_test corrupt6-1.10.8 {
241 hexio_write test.db 1923 FFFFFFFFFFFFFFFF7F
244 SELECT substr(x,1,8) FROM t1
246 } [list 1 {database disk image is malformed}]
248 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
249 # corruption is detected.
250 # Set SerialTypeLen to FFFF FF FF FF FF FF FF FF 7F (10 bytes, and overflows).
251 do_test corrupt6-1.10.9 {
253 hexio_write test.db 1923 FFFFFFFFFFFFFFFFFF7F
256 SELECT substr(x,1,8) FROM t1
258 } [list 1 {database disk image is malformed}]