| 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 |
|
| 19 |
set testdir [file dirname $argv0] |
| 20 |
source $testdir/tester.tcl |
| 21 |
|
| 22 |
# This module uses hard-coded offsets which do not work if the reserved_bytes |
| 23 |
# value is nonzero. |
| 24 |
if {[nonzero_reserved_bytes]} {finish_test; return;} |
| 25 |
|
| 26 |
# These tests deal with corrupt database files |
| 27 |
# |
| 28 |
database_may_be_corrupt |
| 29 |
|
| 30 |
# We must have the page_size pragma for these tests to work. |
| 31 |
# |
| 32 |
ifcapable !pager_pragmas { |
| 33 |
finish_test |
| 34 |
return |
| 35 |
} |
| 36 |
|
| 37 |
# Create a simple, small database. |
| 38 |
# |
| 39 |
do_test corrupt6-1.1 { |
| 40 |
execsql { |
| 41 |
PRAGMA auto_vacuum=OFF; |
| 42 |
PRAGMA page_size=1024; |
| 43 |
CREATE TABLE t1(x); |
| 44 |
INSERT INTO t1(x) VALUES('varint32-01234567890123456789012345678901234567890123456789'); |
| 45 |
INSERT INTO t1(x) VALUES('varint32-01234567890123456789012345678901234567890123456789'); |
| 46 |
} |
| 47 |
file size test.db |
| 48 |
} [expr {1024*2}] |
| 49 |
|
| 50 |
# Verify that the file format is as we expect. The page size |
| 51 |
# should be 1024 bytes. |
| 52 |
# |
| 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 |
| 59 |
|
| 60 |
integrity_check corrupt6-1.4 |
| 61 |
|
| 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 |
| 70 |
|
| 71 |
# Verify simple query results as expected. |
| 72 |
do_test corrupt6-1.6 { |
| 73 |
db close |
| 74 |
sqlite3 db test.db |
| 75 |
catchsql { |
| 76 |
SELECT substr(x,1,8) FROM t1 |
| 77 |
} |
| 78 |
} [list 0 {varint32 varint32} ] |
| 79 |
integrity_check corrupt6-1.7 |
| 80 |
|
| 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 { |
| 85 |
db close |
| 86 |
hexio_write test.db 1923 8105 |
| 87 |
sqlite3 db test.db |
| 88 |
catchsql { |
| 89 |
SELECT substr(x,1,8) FROM t1 |
| 90 |
} |
| 91 |
} [list 1 {database disk image is malformed}] |
| 92 |
|
| 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 { |
| 97 |
db close |
| 98 |
hexio_write test.db 1923 8101 |
| 99 |
sqlite3 db test.db |
| 100 |
catchsql { |
| 101 |
SELECT substr(x,1,8) FROM t1 |
| 102 |
} |
| 103 |
} [list 1 {database disk image is malformed}] |
| 104 |
|
| 105 |
# Put value of record 1 / field 1 SerialTypeLen back. |
| 106 |
do_test corrupt6-1.8.3 { |
| 107 |
db close |
| 108 |
hexio_write test.db 1923 8103 |
| 109 |
sqlite3 db test.db |
| 110 |
catchsql { |
| 111 |
SELECT substr(x,1,8) FROM t1 |
| 112 |
} |
| 113 |
} [list 0 {varint32 varint32} ] |
| 114 |
integrity_check corrupt6-1.8.4 |
| 115 |
|
| 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 { |
| 120 |
db close |
| 121 |
hexio_write test.db 1987 8105 |
| 122 |
sqlite3 db test.db |
| 123 |
catchsql { |
| 124 |
SELECT substr(x,1,8) FROM t1 |
| 125 |
} |
| 126 |
} [list 1 {database disk image is malformed}] |
| 127 |
|
| 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 { |
| 132 |
db close |
| 133 |
hexio_write test.db 1987 8101 |
| 134 |
sqlite3 db test.db |
| 135 |
catchsql { |
| 136 |
SELECT substr(x,1,8) FROM t1 |
| 137 |
} |
| 138 |
} [list 1 {database disk image is malformed}] |
| 139 |
|
| 140 |
# Put value of record 1 / field 2 SerialTypeLen back. |
| 141 |
do_test corrupt6-1.9.3 { |
| 142 |
db close |
| 143 |
hexio_write test.db 1987 8103 |
| 144 |
sqlite3 db test.db |
| 145 |
catchsql { |
| 146 |
SELECT substr(x,1,8) FROM t1 |
| 147 |
} |
| 148 |
} [list 0 {varint32 varint32} ] |
| 149 |
integrity_check corrupt6-1.9.4 |
| 150 |
|
| 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 { |
| 155 |
db close |
| 156 |
hexio_write test.db 1923 FF7F |
| 157 |
sqlite3 db test.db |
| 158 |
catchsql { |
| 159 |
SELECT substr(x,1,8) FROM t1 |
| 160 |
} |
| 161 |
} [list 1 {database disk image is malformed}] |
| 162 |
|
| 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 { |
| 167 |
db close |
| 168 |
hexio_write test.db 1923 FFFF7F |
| 169 |
sqlite3 db test.db |
| 170 |
catchsql { |
| 171 |
SELECT substr(x,1,8) FROM t1 |
| 172 |
} |
| 173 |
} [list 1 {database disk image is malformed}] |
| 174 |
|
| 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 { |
| 179 |
db close |
| 180 |
hexio_write test.db 1923 FFFFFF7F |
| 181 |
sqlite3 db test.db |
| 182 |
catchsql { |
| 183 |
SELECT substr(x,1,8) FROM t1 |
| 184 |
} |
| 185 |
} [list 1 {database disk image is malformed}] |
| 186 |
|
| 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 { |
| 191 |
db close |
| 192 |
hexio_write test.db 1923 FFFFFFFF7F |
| 193 |
sqlite3 db test.db |
| 194 |
catchsql { |
| 195 |
SELECT substr(x,1,8) FROM t1 |
| 196 |
} |
| 197 |
} [list 1 {database disk image is malformed}] |
| 198 |
|
| 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 { |
| 203 |
db close |
| 204 |
hexio_write test.db 1923 FFFFFFFFFF7F |
| 205 |
sqlite3 db test.db |
| 206 |
catchsql { |
| 207 |
SELECT substr(x,1,8) FROM t1 |
| 208 |
} |
| 209 |
} [list 1 {database disk image is malformed}] |
| 210 |
|
| 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 { |
| 215 |
db close |
| 216 |
hexio_write test.db 1923 FFFFFFFFFFFF7F |
| 217 |
sqlite3 db test.db |
| 218 |
catchsql { |
| 219 |
SELECT substr(x,1,8) FROM t1 |
| 220 |
} |
| 221 |
} [list 1 {database disk image is malformed}] |
| 222 |
|
| 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 { |
| 227 |
db close |
| 228 |
hexio_write test.db 1923 FFFFFFFFFFFFFF7F |
| 229 |
sqlite3 db test.db |
| 230 |
catchsql { |
| 231 |
SELECT substr(x,1,8) FROM t1 |
| 232 |
} |
| 233 |
} [list 1 {database disk image is malformed}] |
| 234 |
|
| 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 { |
| 239 |
db close |
| 240 |
hexio_write test.db 1923 FFFFFFFFFFFFFFFF7F |
| 241 |
sqlite3 db test.db |
| 242 |
catchsql { |
| 243 |
SELECT substr(x,1,8) FROM t1 |
| 244 |
} |
| 245 |
} [list 1 {database disk image is malformed}] |
| 246 |
|
| 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 { |
| 251 |
db close |
| 252 |
hexio_write test.db 1923 FFFFFFFFFFFFFFFFFF7F |
| 253 |
sqlite3 db test.db |
| 254 |
catchsql { |
| 255 |
SELECT substr(x,1,8) FROM t1 |
| 256 |
} |
| 257 |
} [list 1 {database disk image is malformed}] |
| 258 |
|
| 259 |
finish_test |