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 # We must have the page_size pragma for these tests to work.
29 ifcapable !pager_pragmas {
34 # Create a simple, small database.
36 do_test corrupt6-1.1 {
38 PRAGMA auto_vacuum=OFF;
39 PRAGMA page_size=1024;
41 INSERT INTO t1(x) VALUES('varint32-01234567890123456789012345678901234567890123456789');
42 INSERT INTO t1(x) VALUES('varint32-01234567890123456789012345678901234567890123456789');
47 # Verify that the file format is as we expect. The page size
48 # should be 1024 bytes.
50 do_test corrupt6-1.2 {
51 hexio_get_int [hexio_read test.db 16 2]
52 } 1024 ;# The page size is 1024
53 do_test corrupt6-1.3 {
54 hexio_get_int [hexio_read test.db 20 1]
55 } 0 ;# Unused bytes per page is 0
57 integrity_check corrupt6-1.4
59 # Verify SerialTypeLen for first field of two records as we expect.
60 # SerialTypeLen = (len*2+12) = 60*2+12 = 132
61 do_test corrupt6-1.5.1 {
62 hexio_read test.db 1923 2
63 } 8103 ;# First text field size is 81 03 == 131
64 do_test corrupt6-1.5.2 {
65 hexio_read test.db 1987 2
66 } 8103 ;# Second text field size is 81 03 == 131
68 # Verify simple query results as expected.
69 do_test corrupt6-1.6 {
73 SELECT substr(x,1,8) FROM t1
75 } [list 0 {varint32 varint32} ]
76 integrity_check corrupt6-1.7
78 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
79 # corruption is detected.
80 # Increase SerialTypeLen by 2.
81 do_test corrupt6-1.8.1 {
83 hexio_write test.db 1923 8105
86 SELECT substr(x,1,8) FROM t1
88 } [list 1 {database disk image is malformed}]
90 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
91 # corruption is detected.
92 # Decrease SerialTypeLen by 2.
93 do_test corrupt6-1.8.2 {
95 hexio_write test.db 1923 8101
98 SELECT substr(x,1,8) FROM t1
100 } [list 1 {database disk image is malformed}]
102 # Put value of record 1 / field 1 SerialTypeLen back.
103 do_test corrupt6-1.8.3 {
105 hexio_write test.db 1923 8103
108 SELECT substr(x,1,8) FROM t1
110 } [list 0 {varint32 varint32} ]
111 integrity_check corrupt6-1.8.4
113 # Adjust value of record 2 / field 1 SerialTypeLen and see if the
114 # corruption is detected.
115 # Increase SerialTypeLen by 2.
116 do_test corrupt6-1.9.1 {
118 hexio_write test.db 1987 8105
121 SELECT substr(x,1,8) FROM t1
123 } [list 1 {database disk image is malformed}]
125 # Adjust value of record 2 / field 2 SerialTypeLen and see if the
126 # corruption is detected.
127 # Decrease SerialTypeLen by 2.
128 do_test corrupt6-1.9.2 {
130 hexio_write test.db 1987 8101
133 SELECT substr(x,1,8) FROM t1
135 } [list 1 {database disk image is malformed}]
137 # Put value of record 1 / field 2 SerialTypeLen back.
138 do_test corrupt6-1.9.3 {
140 hexio_write test.db 1987 8103
143 SELECT substr(x,1,8) FROM t1
145 } [list 0 {varint32 varint32} ]
146 integrity_check corrupt6-1.9.4
148 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
149 # corruption is detected.
150 # Set SerialTypeLen to FF 7F (2 bytes)
151 do_test corrupt6-1.10.1 {
153 hexio_write test.db 1923 FF7F
156 SELECT substr(x,1,8) FROM t1
158 } [list 1 {database disk image is malformed}]
160 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
161 # corruption is detected.
162 # Set SerialTypeLen to FF FF 7F (3 bytes)
163 do_test corrupt6-1.10.2 {
165 hexio_write test.db 1923 FFFF7F
168 SELECT substr(x,1,8) FROM t1
170 } [list 1 {database disk image is malformed}]
172 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
173 # corruption is detected.
174 # Set SerialTypeLen to FF FF FF 7F (4 bytes)
175 do_test corrupt6-1.10.3 {
177 hexio_write test.db 1923 FFFFFF7F
180 SELECT substr(x,1,8) FROM t1
182 } [list 1 {database disk image is malformed}]
184 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
185 # corruption is detected.
186 # Set SerialTypeLen to FF FF FF FF 7F (5 bytes)
187 do_test corrupt6-1.10.4 {
189 hexio_write test.db 1923 FFFFFFFF7F
192 SELECT substr(x,1,8) FROM t1
194 } [list 1 {database disk image is malformed}]
196 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
197 # corruption is detected.
198 # Set SerialTypeLen to FF FF FF FF FF 7F (6 bytes, and overflows).
199 do_test corrupt6-1.10.5 {
201 hexio_write test.db 1923 FFFFFFFFFF7F
204 SELECT substr(x,1,8) FROM t1
206 } [list 1 {database disk image is malformed}]
208 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
209 # corruption is detected.
210 # Set SerialTypeLen to FF FF FF FF FF FF 7F (7 bytes, and overflows).
211 do_test corrupt6-1.10.6 {
213 hexio_write test.db 1923 FFFFFFFFFFFF7F
216 SELECT substr(x,1,8) FROM t1
218 } [list 1 {database disk image is malformed}]
220 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
221 # corruption is detected.
222 # Set SerialTypeLen to FF FF FF FF FF FF FF 7F (8 bytes, and overflows).
223 do_test corrupt6-1.10.7 {
225 hexio_write test.db 1923 FFFFFFFFFFFFFF7F
228 SELECT substr(x,1,8) FROM t1
230 } [list 1 {database disk image is malformed}]
232 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
233 # corruption is detected.
234 # Set SerialTypeLen to FF FF FF FF FF FF FF FF 7F (9 bytes, and overflows).
235 do_test corrupt6-1.10.8 {
237 hexio_write test.db 1923 FFFFFFFFFFFFFFFF7F
240 SELECT substr(x,1,8) FROM t1
242 } [list 1 {database disk image is malformed}]
244 # Adjust value of record 1 / field 1 SerialTypeLen and see if the
245 # corruption is detected.
246 # Set SerialTypeLen to FFFF FF FF FF FF FF FF FF 7F (10 bytes, and overflows).
247 do_test corrupt6-1.10.9 {
249 hexio_write test.db 1923 FFFFFFFFFFFFFFFFFF7F
252 SELECT substr(x,1,8) FROM t1
254 } [list 1 {database disk image is malformed}]