xref: /sqlite-3.40.0/test/corrupt6.test (revision a3fdec71)
1# 2008 May 6
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.
12#
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.
16#
17# $Id: corrupt6.test,v 1.2 2008/05/19 15:37:10 shane Exp $
18
19set testdir [file dirname $argv0]
20source $testdir/tester.tcl
21
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).
24#
25do_not_use_codec
26
27# These tests deal with corrupt database files
28#
29database_may_be_corrupt
30
31# We must have the page_size pragma for these tests to work.
32#
33ifcapable !pager_pragmas {
34  finish_test
35  return
36}
37
38# Create a simple, small database.
39#
40do_test corrupt6-1.1 {
41  execsql {
42    PRAGMA auto_vacuum=OFF;
43    PRAGMA page_size=1024;
44    CREATE TABLE t1(x);
45    INSERT INTO t1(x) VALUES('varint32-01234567890123456789012345678901234567890123456789');
46    INSERT INTO t1(x) VALUES('varint32-01234567890123456789012345678901234567890123456789');
47  }
48  file size test.db
49} [expr {1024*2}]
50
51# Verify that the file format is as we expect.  The page size
52# should be 1024 bytes.
53#
54do_test corrupt6-1.2 {
55  hexio_get_int [hexio_read test.db 16 2]
56} 1024   ;# The page size is 1024
57do_test corrupt6-1.3 {
58  hexio_get_int [hexio_read test.db 20 1]
59} 0      ;# Unused bytes per page is 0
60
61integrity_check corrupt6-1.4
62
63# Verify SerialTypeLen for first field of two records as we expect.
64# SerialTypeLen = (len*2+12) = 60*2+12 = 132
65do_test corrupt6-1.5.1 {
66  hexio_read test.db 1923 2
67} 8103      ;# First text field size is 81 03 == 131
68do_test corrupt6-1.5.2 {
69  hexio_read test.db 1987 2
70} 8103      ;# Second text field size is 81 03 == 131
71
72# Verify simple query results as expected.
73do_test corrupt6-1.6 {
74  db close
75  sqlite3 db test.db
76  catchsql {
77    SELECT substr(x,1,8) FROM t1
78  }
79} [list 0 {varint32 varint32} ]
80integrity_check corrupt6-1.7
81
82# Adjust value of record 1 / field 1 SerialTypeLen and see if the
83# corruption is detected.
84# Increase SerialTypeLen by 2.
85do_test corrupt6-1.8.1 {
86  db close
87  hexio_write test.db 1923 8105
88  sqlite3 db test.db
89  catchsql {
90    SELECT substr(x,1,8) FROM t1
91  }
92} [list 1 {database disk image is malformed}]
93
94# Adjust value of record 1 / field 1 SerialTypeLen and see if the
95# corruption is detected.
96# Decrease SerialTypeLen by 2.
97do_test corrupt6-1.8.2 {
98  db close
99  hexio_write test.db 1923 8101
100  sqlite3 db test.db
101  catchsql {
102    SELECT substr(x,1,8) FROM t1
103  }
104} [list 1 {database disk image is malformed}]
105
106# Put value of record 1 / field 1 SerialTypeLen back.
107do_test corrupt6-1.8.3 {
108  db close
109  hexio_write test.db 1923 8103
110  sqlite3 db test.db
111  catchsql {
112    SELECT substr(x,1,8) FROM t1
113  }
114} [list 0 {varint32 varint32} ]
115integrity_check corrupt6-1.8.4
116
117# Adjust value of record 2 / field 1 SerialTypeLen and see if the
118# corruption is detected.
119# Increase SerialTypeLen by 2.
120do_test corrupt6-1.9.1 {
121  db close
122  hexio_write test.db 1987 8105
123  sqlite3 db test.db
124  catchsql {
125    SELECT substr(x,1,8) FROM t1
126  }
127} [list 1 {database disk image is malformed}]
128
129# Adjust value of record 2 / field 2 SerialTypeLen and see if the
130# corruption is detected.
131# Decrease SerialTypeLen by 2.
132do_test corrupt6-1.9.2 {
133  db close
134  hexio_write test.db 1987 8101
135  sqlite3 db test.db
136  catchsql {
137    SELECT substr(x,1,8) FROM t1
138  }
139} [list 1 {database disk image is malformed}]
140
141# Put value of record 1 / field 2 SerialTypeLen back.
142do_test corrupt6-1.9.3 {
143  db close
144  hexio_write test.db 1987 8103
145  sqlite3 db test.db
146  catchsql {
147    SELECT substr(x,1,8) FROM t1
148  }
149} [list 0 {varint32 varint32} ]
150integrity_check corrupt6-1.9.4
151
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)
155do_test corrupt6-1.10.1 {
156  db close
157  hexio_write test.db 1923 FF7F
158  sqlite3 db test.db
159  catchsql {
160    SELECT substr(x,1,8) FROM t1
161  }
162} [list 1 {database disk image is malformed}]
163
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)
167do_test corrupt6-1.10.2 {
168  db close
169  hexio_write test.db 1923 FFFF7F
170  sqlite3 db test.db
171  catchsql {
172    SELECT substr(x,1,8) FROM t1
173  }
174} [list 1 {database disk image is malformed}]
175
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)
179do_test corrupt6-1.10.3 {
180  db close
181  hexio_write test.db 1923 FFFFFF7F
182  sqlite3 db test.db
183  catchsql {
184    SELECT substr(x,1,8) FROM t1
185  }
186} [list 1 {database disk image is malformed}]
187
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)
191do_test corrupt6-1.10.4 {
192  db close
193  hexio_write test.db 1923 FFFFFFFF7F
194  sqlite3 db test.db
195  catchsql {
196    SELECT substr(x,1,8) FROM t1
197  }
198} [list 1 {database disk image is malformed}]
199
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).
203do_test corrupt6-1.10.5 {
204  db close
205  hexio_write test.db 1923 FFFFFFFFFF7F
206  sqlite3 db test.db
207  catchsql {
208    SELECT substr(x,1,8) FROM t1
209  }
210} [list 1 {database disk image is malformed}]
211
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).
215do_test corrupt6-1.10.6 {
216  db close
217  hexio_write test.db 1923 FFFFFFFFFFFF7F
218  sqlite3 db test.db
219  catchsql {
220    SELECT substr(x,1,8) FROM t1
221  }
222} [list 1 {database disk image is malformed}]
223
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).
227do_test corrupt6-1.10.7 {
228  db close
229  hexio_write test.db 1923 FFFFFFFFFFFFFF7F
230  sqlite3 db test.db
231  catchsql {
232    SELECT substr(x,1,8) FROM t1
233  }
234} [list 1 {database disk image is malformed}]
235
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).
239do_test corrupt6-1.10.8 {
240  db close
241  hexio_write test.db 1923 FFFFFFFFFFFFFFFF7F
242  sqlite3 db test.db
243  catchsql {
244    SELECT substr(x,1,8) FROM t1
245  }
246} [list 1 {database disk image is malformed}]
247
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).
251do_test corrupt6-1.10.9 {
252  db close
253  hexio_write test.db 1923 FFFFFFFFFFFFFFFFFF7F
254  sqlite3 db test.db
255  catchsql {
256    SELECT substr(x,1,8) FROM t1
257  }
258} [list 1 {database disk image is malformed}]
259
260finish_test
261