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 # This module uses hard-coded offsets which do not work if the reserved_bytes
24 if {[nonzero_reserved_bytes]} {finish_test; return;}
26 # These tests deal with corrupt database files
28 database_may_be_corrupt
30 # We must have the page_size pragma for these tests to work.
32 ifcapable !pager_pragmas {
37 # Create a simple, small database.
39 do_test corrupt6-1.1 {
41 PRAGMA auto_vacuum=OFF;
42 PRAGMA page_size=1024;
44 INSERT INTO t1(x) VALUES('varint32-01234567890123456789012345678901234567890123456789');
45 INSERT INTO t1(x) VALUES('varint32-01234567890123456789012345678901234567890123456789');
50 # Verify that the file format is as we expect. The page size
51 # should be 1024 bytes.
53 do_test corrupt6-1.2 {
54 hexio_get_int [hexio_read test.db 16 2]
55 } 1024 ;# The page size is 1024
56 do_test corrupt6-1.3 {
57 hexio_get_int [hexio_read test.db 20 1]
58 } 0 ;# Unused bytes per page is 0
60 integrity_check corrupt6-1.4
62 # Verify SerialTypeLen for first field of two records as we expect.
63 # SerialTypeLen = (len*2+12) = 60*2+12 = 132
64 do_test corrupt6-1.5.1 {
65 hexio_read test.db 1923 2
66 } 8103 ;# First text field size is 81 03 == 131
67 do_test corrupt6-1.5.2 {
68 hexio_read test.db 1987 2
69 } 8103 ;# Second text field size is 81 03 == 131
71 # Verify simple query results as expected.
72 do_test corrupt6-1.6 {
76 SELECT substr(x,1,8) FROM t1
78 } [list 0 {varint32 varint32} ]
79 integrity_check corrupt6-1.7
81 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
82 # corruption is detected.
83 # Increase SerialTypeLen by 2.
84 do_test corrupt6-1.8.1 {
86 hexio_write test.db 1923 8105
89 SELECT substr(x,1,8) FROM t1
91 } [list 1 {database disk image is malformed}]
93 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
94 # corruption is detected.
95 # Decrease SerialTypeLen by 2.
96 do_test corrupt6-1.8.2 {
98 hexio_write test.db 1923 8101
101 SELECT substr(x,1,8) FROM t1
103 } [list 1 {database disk image is malformed}]
105 # Put value of record 1 / field 1 SerialTypeLen back.
106 do_test corrupt6-1.8.3 {
108 hexio_write test.db 1923 8103
111 SELECT substr(x,1,8) FROM t1
113 } [list 0 {varint32 varint32} ]
114 integrity_check corrupt6-1.8.4
116 # Adjust value of record 2 / field 1 SerialTypeLen and see if the
117 # corruption is detected.
118 # Increase SerialTypeLen by 2.
119 do_test corrupt6-1.9.1 {
121 hexio_write test.db 1987 8105
124 SELECT substr(x,1,8) FROM t1
126 } [list 1 {database disk image is malformed}]
128 # Adjust value of record 2 / field 2 SerialTypeLen and see if the
129 # corruption is detected.
130 # Decrease SerialTypeLen by 2.
131 do_test corrupt6-1.9.2 {
133 hexio_write test.db 1987 8101
136 SELECT substr(x,1,8) FROM t1
138 } [list 1 {database disk image is malformed}]
140 # Put value of record 1 / field 2 SerialTypeLen back.
141 do_test corrupt6-1.9.3 {
143 hexio_write test.db 1987 8103
146 SELECT substr(x,1,8) FROM t1
148 } [list 0 {varint32 varint32} ]
149 integrity_check corrupt6-1.9.4
151 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
152 # corruption is detected.
153 # Set SerialTypeLen to FF 7F (2 bytes)
154 do_test corrupt6-1.10.1 {
156 hexio_write test.db 1923 FF7F
159 SELECT substr(x,1,8) FROM t1
161 } [list 1 {database disk image is malformed}]
163 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
164 # corruption is detected.
165 # Set SerialTypeLen to FF FF 7F (3 bytes)
166 do_test corrupt6-1.10.2 {
168 hexio_write test.db 1923 FFFF7F
171 SELECT substr(x,1,8) FROM t1
173 } [list 1 {database disk image is malformed}]
175 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
176 # corruption is detected.
177 # Set SerialTypeLen to FF FF FF 7F (4 bytes)
178 do_test corrupt6-1.10.3 {
180 hexio_write test.db 1923 FFFFFF7F
183 SELECT substr(x,1,8) FROM t1
185 } [list 1 {database disk image is malformed}]
187 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
188 # corruption is detected.
189 # Set SerialTypeLen to FF FF FF FF 7F (5 bytes)
190 do_test corrupt6-1.10.4 {
192 hexio_write test.db 1923 FFFFFFFF7F
195 SELECT substr(x,1,8) FROM t1
197 } [list 1 {database disk image is malformed}]
199 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
200 # corruption is detected.
201 # Set SerialTypeLen to FF FF FF FF FF 7F (6 bytes, and overflows).
202 do_test corrupt6-1.10.5 {
204 hexio_write test.db 1923 FFFFFFFFFF7F
207 SELECT substr(x,1,8) FROM t1
209 } [list 1 {database disk image is malformed}]
211 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
212 # corruption is detected.
213 # Set SerialTypeLen to FF FF FF FF FF FF 7F (7 bytes, and overflows).
214 do_test corrupt6-1.10.6 {
216 hexio_write test.db 1923 FFFFFFFFFFFF7F
219 SELECT substr(x,1,8) FROM t1
221 } [list 1 {database disk image is malformed}]
223 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
224 # corruption is detected.
225 # Set SerialTypeLen to FF FF FF FF FF FF FF 7F (8 bytes, and overflows).
226 do_test corrupt6-1.10.7 {
228 hexio_write test.db 1923 FFFFFFFFFFFFFF7F
231 SELECT substr(x,1,8) FROM t1
233 } [list 1 {database disk image is malformed}]
235 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
236 # corruption is detected.
237 # Set SerialTypeLen to FF FF FF FF FF FF FF FF 7F (9 bytes, and overflows).
238 do_test corrupt6-1.10.8 {
240 hexio_write test.db 1923 FFFFFFFFFFFFFFFF7F
243 SELECT substr(x,1,8) FROM t1
245 } [list 1 {database disk image is malformed}]
247 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
248 # corruption is detected.
249 # Set SerialTypeLen to FFFF FF FF FF FF FF FF FF 7F (10 bytes, and overflows).
250 do_test corrupt6-1.10.9 {
252 hexio_write test.db 1923 FFFFFFFFFFFFFFFFFF7F
255 SELECT substr(x,1,8) FROM t1
257 } [list 1 {database disk image is malformed}]