AryaWu/sqlite
0
1# 2008 May 62#3# The author disclaims copyright to this source code. In place of4# 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 or14# segfault if it sees a corrupt database file. It specifically focuses15# 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.tcl21 22# This module uses hard-coded offsets which do not work if the reserved_bytes23# value is nonzero.24if {[nonzero_reserved_bytes]} {finish_test; return;}25 26# These tests deal with corrupt database files27#28database_may_be_corrupt29 30# We must have the page_size pragma for these tests to work.31#32ifcapable !pager_pragmas {33 finish_test34 return35}36 37# Create a simple, small database.38#39do_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.db48} [expr {1024*2}]49 50# Verify that the file format is as we expect. The page size51# should be 1024 bytes.52#53do_test corrupt6-1.2 {54 hexio_get_int [hexio_read test.db 16 2]55} 1024 ;# The page size is 102456do_test corrupt6-1.3 {57 hexio_get_int [hexio_read test.db 20 1]58} 0 ;# Unused bytes per page is 059 60integrity_check corrupt6-1.461 62# Verify SerialTypeLen for first field of two records as we expect.63# SerialTypeLen = (len*2+12) = 60*2+12 = 13264do_test corrupt6-1.5.1 {65 hexio_read test.db 1923 266} 8103 ;# First text field size is 81 03 == 13167do_test corrupt6-1.5.2 {68 hexio_read test.db 1987 269} 8103 ;# Second text field size is 81 03 == 13170 71# Verify simple query results as expected.72do_test corrupt6-1.6 {73 db close74 sqlite3 db test.db75 catchsql {76 SELECT substr(x,1,8) FROM t177 }78} [list 0 {varint32 varint32} ]79integrity_check corrupt6-1.780 81# Adjust value of record 1 / field 1 SerialTypeLen and see if the82# corruption is detected.83# Increase SerialTypeLen by 2.84do_test corrupt6-1.8.1 {85 db close86 hexio_write test.db 1923 810587 sqlite3 db test.db88 catchsql {89 SELECT substr(x,1,8) FROM t190 }91} [list 1 {database disk image is malformed}]92 93# Adjust value of record 1 / field 1 SerialTypeLen and see if the94# corruption is detected.95# Decrease SerialTypeLen by 2.96do_test corrupt6-1.8.2 {97 db close98 hexio_write test.db 1923 810199 sqlite3 db test.db100 catchsql {101 SELECT substr(x,1,8) FROM t1102 }103} [list 1 {database disk image is malformed}]104 105# Put value of record 1 / field 1 SerialTypeLen back.106do_test corrupt6-1.8.3 {107 db close108 hexio_write test.db 1923 8103109 sqlite3 db test.db110 catchsql {111 SELECT substr(x,1,8) FROM t1112 }113} [list 0 {varint32 varint32} ]114integrity_check corrupt6-1.8.4115 116# Adjust value of record 2 / field 1 SerialTypeLen and see if the117# corruption is detected.118# Increase SerialTypeLen by 2.119do_test corrupt6-1.9.1 {120 db close121 hexio_write test.db 1987 8105122 sqlite3 db test.db123 catchsql {124 SELECT substr(x,1,8) FROM t1125 }126} [list 1 {database disk image is malformed}]127 128# Adjust value of record 2 / field 2 SerialTypeLen and see if the129# corruption is detected.130# Decrease SerialTypeLen by 2.131do_test corrupt6-1.9.2 {132 db close133 hexio_write test.db 1987 8101134 sqlite3 db test.db135 catchsql {136 SELECT substr(x,1,8) FROM t1137 }138} [list 1 {database disk image is malformed}]139 140# Put value of record 1 / field 2 SerialTypeLen back.141do_test corrupt6-1.9.3 {142 db close143 hexio_write test.db 1987 8103144 sqlite3 db test.db145 catchsql {146 SELECT substr(x,1,8) FROM t1147 }148} [list 0 {varint32 varint32} ]149integrity_check corrupt6-1.9.4150 151# Adjust value of record 1 / field 1 SerialTypeLen and see if the152# corruption is detected.153# Set SerialTypeLen to FF 7F (2 bytes)154do_test corrupt6-1.10.1 {155 db close156 hexio_write test.db 1923 FF7F157 sqlite3 db test.db158 catchsql {159 SELECT substr(x,1,8) FROM t1160 }161} [list 1 {database disk image is malformed}]162 163# Adjust value of record 1 / field 1 SerialTypeLen and see if the164# corruption is detected.165# Set SerialTypeLen to FF FF 7F (3 bytes)166do_test corrupt6-1.10.2 {167 db close168 hexio_write test.db 1923 FFFF7F169 sqlite3 db test.db170 catchsql {171 SELECT substr(x,1,8) FROM t1172 }173} [list 1 {database disk image is malformed}]174 175# Adjust value of record 1 / field 1 SerialTypeLen and see if the176# corruption is detected.177# Set SerialTypeLen to FF FF FF 7F (4 bytes)178do_test corrupt6-1.10.3 {179 db close180 hexio_write test.db 1923 FFFFFF7F181 sqlite3 db test.db182 catchsql {183 SELECT substr(x,1,8) FROM t1184 }185} [list 1 {database disk image is malformed}]186 187# Adjust value of record 1 / field 1 SerialTypeLen and see if the188# corruption is detected.189# Set SerialTypeLen to FF FF FF FF 7F (5 bytes)190do_test corrupt6-1.10.4 {191 db close192 hexio_write test.db 1923 FFFFFFFF7F193 sqlite3 db test.db194 catchsql {195 SELECT substr(x,1,8) FROM t1196 }197} [list 1 {database disk image is malformed}]198 199# Adjust value of record 1 / field 1 SerialTypeLen and see if the200# corruption is detected.201# Set SerialTypeLen to FF FF FF FF FF 7F (6 bytes, and overflows).202do_test corrupt6-1.10.5 {203 db close204 hexio_write test.db 1923 FFFFFFFFFF7F205 sqlite3 db test.db206 catchsql {207 SELECT substr(x,1,8) FROM t1208 }209} [list 1 {database disk image is malformed}]210 211# Adjust value of record 1 / field 1 SerialTypeLen and see if the212# corruption is detected.213# Set SerialTypeLen to FF FF FF FF FF FF 7F (7 bytes, and overflows).214do_test corrupt6-1.10.6 {215 db close216 hexio_write test.db 1923 FFFFFFFFFFFF7F217 sqlite3 db test.db218 catchsql {219 SELECT substr(x,1,8) FROM t1220 }221} [list 1 {database disk image is malformed}]222 223# Adjust value of record 1 / field 1 SerialTypeLen and see if the224# corruption is detected.225# Set SerialTypeLen to FF FF FF FF FF FF FF 7F (8 bytes, and overflows).226do_test corrupt6-1.10.7 {227 db close228 hexio_write test.db 1923 FFFFFFFFFFFFFF7F229 sqlite3 db test.db230 catchsql {231 SELECT substr(x,1,8) FROM t1232 }233} [list 1 {database disk image is malformed}]234 235# Adjust value of record 1 / field 1 SerialTypeLen and see if the236# corruption is detected.237# Set SerialTypeLen to FF FF FF FF FF FF FF FF 7F (9 bytes, and overflows).238do_test corrupt6-1.10.8 {239 db close240 hexio_write test.db 1923 FFFFFFFFFFFFFFFF7F241 sqlite3 db test.db242 catchsql {243 SELECT substr(x,1,8) FROM t1244 }245} [list 1 {database disk image is malformed}]246 247# Adjust value of record 1 / field 1 SerialTypeLen and see if the248# corruption is detected.249# Set SerialTypeLen to FFFF FF FF FF FF FF FF FF 7F (10 bytes, and overflows).250do_test corrupt6-1.10.9 {251 db close252 hexio_write test.db 1923 FFFFFFFFFFFFFFFFFF7F253 sqlite3 db test.db254 catchsql {255 SELECT substr(x,1,8) FROM t1256 }257} [list 1 {database disk image is malformed}]258 259finish_test260 