12ca8bc08Sshane# 2008 May 6 22ca8bc08Sshane# 32ca8bc08Sshane# The author disclaims copyright to this source code. In place of 42ca8bc08Sshane# a legal notice, here is a blessing: 52ca8bc08Sshane# 62ca8bc08Sshane# May you do good and not evil. 72ca8bc08Sshane# May you find forgiveness for yourself and forgive others. 82ca8bc08Sshane# May you share freely, never taking more than you give. 92ca8bc08Sshane# 102ca8bc08Sshane#*********************************************************************** 112ca8bc08Sshane# This file implements regression tests for SQLite library. 122ca8bc08Sshane# 132ca8bc08Sshane# This file implements tests to make sure SQLite does not crash or 142ca8bc08Sshane# segfault if it sees a corrupt database file. It specifically focuses 152ca8bc08Sshane# on corrupt SerialTypeLen values. 162ca8bc08Sshane# 178ef42631Sshane# $Id: corrupt6.test,v 1.2 2008/05/19 15:37:10 shane Exp $ 182ca8bc08Sshane 192ca8bc08Sshaneset testdir [file dirname $argv0] 202ca8bc08Sshanesource $testdir/tester.tcl 212ca8bc08Sshane 22*af3906a7Sdrh# This module uses hard-coded offsets which do not work if the reserved_bytes 23*af3906a7Sdrh# value is nonzero. 24*af3906a7Sdrhif {[nonzero_reserved_bytes]} {finish_test; return;} 2568928b6cSdan 2609fe6143Sdrh# These tests deal with corrupt database files 2709fe6143Sdrh# 2809fe6143Sdrhdatabase_may_be_corrupt 2909fe6143Sdrh 302ca8bc08Sshane# We must have the page_size pragma for these tests to work. 312ca8bc08Sshane# 322ca8bc08Sshaneifcapable !pager_pragmas { 332ca8bc08Sshane finish_test 342ca8bc08Sshane return 352ca8bc08Sshane} 362ca8bc08Sshane 372ca8bc08Sshane# Create a simple, small database. 382ca8bc08Sshane# 392ca8bc08Sshanedo_test corrupt6-1.1 { 402ca8bc08Sshane execsql { 412ca8bc08Sshane PRAGMA auto_vacuum=OFF; 422ca8bc08Sshane PRAGMA page_size=1024; 432ca8bc08Sshane CREATE TABLE t1(x); 442ca8bc08Sshane INSERT INTO t1(x) VALUES('varint32-01234567890123456789012345678901234567890123456789'); 452ca8bc08Sshane INSERT INTO t1(x) VALUES('varint32-01234567890123456789012345678901234567890123456789'); 462ca8bc08Sshane } 472ca8bc08Sshane file size test.db 482ca8bc08Sshane} [expr {1024*2}] 492ca8bc08Sshane 502ca8bc08Sshane# Verify that the file format is as we expect. The page size 512ca8bc08Sshane# should be 1024 bytes. 522ca8bc08Sshane# 532ca8bc08Sshanedo_test corrupt6-1.2 { 542ca8bc08Sshane hexio_get_int [hexio_read test.db 16 2] 552ca8bc08Sshane} 1024 ;# The page size is 1024 562ca8bc08Sshanedo_test corrupt6-1.3 { 572ca8bc08Sshane hexio_get_int [hexio_read test.db 20 1] 582ca8bc08Sshane} 0 ;# Unused bytes per page is 0 592ca8bc08Sshane 602ca8bc08Sshaneintegrity_check corrupt6-1.4 612ca8bc08Sshane 622ca8bc08Sshane# Verify SerialTypeLen for first field of two records as we expect. 632ca8bc08Sshane# SerialTypeLen = (len*2+12) = 60*2+12 = 132 642ca8bc08Sshanedo_test corrupt6-1.5.1 { 652ca8bc08Sshane hexio_read test.db 1923 2 668ef42631Sshane} 8103 ;# First text field size is 81 03 == 131 672ca8bc08Sshanedo_test corrupt6-1.5.2 { 682ca8bc08Sshane hexio_read test.db 1987 2 698ef42631Sshane} 8103 ;# Second text field size is 81 03 == 131 702ca8bc08Sshane 712ca8bc08Sshane# Verify simple query results as expected. 722ca8bc08Sshanedo_test corrupt6-1.6 { 732ca8bc08Sshane db close 742ca8bc08Sshane sqlite3 db test.db 752ca8bc08Sshane catchsql { 762ca8bc08Sshane SELECT substr(x,1,8) FROM t1 772ca8bc08Sshane } 782ca8bc08Sshane} [list 0 {varint32 varint32} ] 792ca8bc08Sshaneintegrity_check corrupt6-1.7 802ca8bc08Sshane 812ca8bc08Sshane# Adjust value of record 1 / field 1 SerialTypeLen and see if the 822ca8bc08Sshane# corruption is detected. 832ca8bc08Sshane# Increase SerialTypeLen by 2. 842ca8bc08Sshanedo_test corrupt6-1.8.1 { 852ca8bc08Sshane db close 862ca8bc08Sshane hexio_write test.db 1923 8105 872ca8bc08Sshane sqlite3 db test.db 882ca8bc08Sshane catchsql { 892ca8bc08Sshane SELECT substr(x,1,8) FROM t1 902ca8bc08Sshane } 912ca8bc08Sshane} [list 1 {database disk image is malformed}] 922ca8bc08Sshane 932ca8bc08Sshane# Adjust value of record 1 / field 1 SerialTypeLen and see if the 942ca8bc08Sshane# corruption is detected. 952ca8bc08Sshane# Decrease SerialTypeLen by 2. 962ca8bc08Sshanedo_test corrupt6-1.8.2 { 972ca8bc08Sshane db close 982ca8bc08Sshane hexio_write test.db 1923 8101 992ca8bc08Sshane sqlite3 db test.db 1002ca8bc08Sshane catchsql { 1012ca8bc08Sshane SELECT substr(x,1,8) FROM t1 1022ca8bc08Sshane } 1032ca8bc08Sshane} [list 1 {database disk image is malformed}] 1042ca8bc08Sshane 1052ca8bc08Sshane# Put value of record 1 / field 1 SerialTypeLen back. 1062ca8bc08Sshanedo_test corrupt6-1.8.3 { 1072ca8bc08Sshane db close 1082ca8bc08Sshane hexio_write test.db 1923 8103 1092ca8bc08Sshane sqlite3 db test.db 1102ca8bc08Sshane catchsql { 1112ca8bc08Sshane SELECT substr(x,1,8) FROM t1 1122ca8bc08Sshane } 1132ca8bc08Sshane} [list 0 {varint32 varint32} ] 1142ca8bc08Sshaneintegrity_check corrupt6-1.8.4 1152ca8bc08Sshane 1162ca8bc08Sshane# Adjust value of record 2 / field 1 SerialTypeLen and see if the 1172ca8bc08Sshane# corruption is detected. 1182ca8bc08Sshane# Increase SerialTypeLen by 2. 1192ca8bc08Sshanedo_test corrupt6-1.9.1 { 1202ca8bc08Sshane db close 1212ca8bc08Sshane hexio_write test.db 1987 8105 1222ca8bc08Sshane sqlite3 db test.db 1232ca8bc08Sshane catchsql { 1242ca8bc08Sshane SELECT substr(x,1,8) FROM t1 1252ca8bc08Sshane } 1262ca8bc08Sshane} [list 1 {database disk image is malformed}] 1272ca8bc08Sshane 1282ca8bc08Sshane# Adjust value of record 2 / field 2 SerialTypeLen and see if the 1292ca8bc08Sshane# corruption is detected. 1302ca8bc08Sshane# Decrease SerialTypeLen by 2. 1312ca8bc08Sshanedo_test corrupt6-1.9.2 { 1322ca8bc08Sshane db close 1332ca8bc08Sshane hexio_write test.db 1987 8101 1342ca8bc08Sshane sqlite3 db test.db 1352ca8bc08Sshane catchsql { 1362ca8bc08Sshane SELECT substr(x,1,8) FROM t1 1372ca8bc08Sshane } 1382ca8bc08Sshane} [list 1 {database disk image is malformed}] 1392ca8bc08Sshane 1402ca8bc08Sshane# Put value of record 1 / field 2 SerialTypeLen back. 1412ca8bc08Sshanedo_test corrupt6-1.9.3 { 1422ca8bc08Sshane db close 1432ca8bc08Sshane hexio_write test.db 1987 8103 1442ca8bc08Sshane sqlite3 db test.db 1452ca8bc08Sshane catchsql { 1462ca8bc08Sshane SELECT substr(x,1,8) FROM t1 1472ca8bc08Sshane } 1482ca8bc08Sshane} [list 0 {varint32 varint32} ] 1492ca8bc08Sshaneintegrity_check corrupt6-1.9.4 1502ca8bc08Sshane 1518ef42631Sshane# Adjust value of record 1 / field 1 SerialTypeLen and see if the 1528ef42631Sshane# corruption is detected. 1538ef42631Sshane# Set SerialTypeLen to FF 7F (2 bytes) 1548ef42631Sshanedo_test corrupt6-1.10.1 { 1558ef42631Sshane db close 1568ef42631Sshane hexio_write test.db 1923 FF7F 1578ef42631Sshane sqlite3 db test.db 1588ef42631Sshane catchsql { 1598ef42631Sshane SELECT substr(x,1,8) FROM t1 1608ef42631Sshane } 1618ef42631Sshane} [list 1 {database disk image is malformed}] 1628ef42631Sshane 1638ef42631Sshane# Adjust value of record 1 / field 1 SerialTypeLen and see if the 1648ef42631Sshane# corruption is detected. 1658ef42631Sshane# Set SerialTypeLen to FF FF 7F (3 bytes) 1668ef42631Sshanedo_test corrupt6-1.10.2 { 1678ef42631Sshane db close 1688ef42631Sshane hexio_write test.db 1923 FFFF7F 1698ef42631Sshane sqlite3 db test.db 1708ef42631Sshane catchsql { 1718ef42631Sshane SELECT substr(x,1,8) FROM t1 1728ef42631Sshane } 1738ef42631Sshane} [list 1 {database disk image is malformed}] 1748ef42631Sshane 1758ef42631Sshane# Adjust value of record 1 / field 1 SerialTypeLen and see if the 1768ef42631Sshane# corruption is detected. 1778ef42631Sshane# Set SerialTypeLen to FF FF FF 7F (4 bytes) 1788ef42631Sshanedo_test corrupt6-1.10.3 { 1798ef42631Sshane db close 1808ef42631Sshane hexio_write test.db 1923 FFFFFF7F 1818ef42631Sshane sqlite3 db test.db 1828ef42631Sshane catchsql { 1838ef42631Sshane SELECT substr(x,1,8) FROM t1 1848ef42631Sshane } 1858ef42631Sshane} [list 1 {database disk image is malformed}] 1868ef42631Sshane 1878ef42631Sshane# Adjust value of record 1 / field 1 SerialTypeLen and see if the 1888ef42631Sshane# corruption is detected. 1898ef42631Sshane# Set SerialTypeLen to FF FF FF FF 7F (5 bytes) 1908ef42631Sshanedo_test corrupt6-1.10.4 { 1918ef42631Sshane db close 1928ef42631Sshane hexio_write test.db 1923 FFFFFFFF7F 1938ef42631Sshane sqlite3 db test.db 1948ef42631Sshane catchsql { 1958ef42631Sshane SELECT substr(x,1,8) FROM t1 1968ef42631Sshane } 1978ef42631Sshane} [list 1 {database disk image is malformed}] 1988ef42631Sshane 1998ef42631Sshane# Adjust value of record 1 / field 1 SerialTypeLen and see if the 2008ef42631Sshane# corruption is detected. 2018ef42631Sshane# Set SerialTypeLen to FF FF FF FF FF 7F (6 bytes, and overflows). 2028ef42631Sshanedo_test corrupt6-1.10.5 { 2038ef42631Sshane db close 2048ef42631Sshane hexio_write test.db 1923 FFFFFFFFFF7F 2058ef42631Sshane sqlite3 db test.db 2068ef42631Sshane catchsql { 2078ef42631Sshane SELECT substr(x,1,8) FROM t1 2088ef42631Sshane } 2098ef42631Sshane} [list 1 {database disk image is malformed}] 2108ef42631Sshane 2118ef42631Sshane# Adjust value of record 1 / field 1 SerialTypeLen and see if the 2128ef42631Sshane# corruption is detected. 2138ef42631Sshane# Set SerialTypeLen to FF FF FF FF FF FF 7F (7 bytes, and overflows). 2148ef42631Sshanedo_test corrupt6-1.10.6 { 2158ef42631Sshane db close 2168ef42631Sshane hexio_write test.db 1923 FFFFFFFFFFFF7F 2178ef42631Sshane sqlite3 db test.db 2188ef42631Sshane catchsql { 2198ef42631Sshane SELECT substr(x,1,8) FROM t1 2208ef42631Sshane } 2218ef42631Sshane} [list 1 {database disk image is malformed}] 2228ef42631Sshane 2238ef42631Sshane# Adjust value of record 1 / field 1 SerialTypeLen and see if the 2248ef42631Sshane# corruption is detected. 2258ef42631Sshane# Set SerialTypeLen to FF FF FF FF FF FF FF 7F (8 bytes, and overflows). 2268ef42631Sshanedo_test corrupt6-1.10.7 { 2278ef42631Sshane db close 2288ef42631Sshane hexio_write test.db 1923 FFFFFFFFFFFFFF7F 2298ef42631Sshane sqlite3 db test.db 2308ef42631Sshane catchsql { 2318ef42631Sshane SELECT substr(x,1,8) FROM t1 2328ef42631Sshane } 2338ef42631Sshane} [list 1 {database disk image is malformed}] 2348ef42631Sshane 2358ef42631Sshane# Adjust value of record 1 / field 1 SerialTypeLen and see if the 2368ef42631Sshane# corruption is detected. 2378ef42631Sshane# Set SerialTypeLen to FF FF FF FF FF FF FF FF 7F (9 bytes, and overflows). 2388ef42631Sshanedo_test corrupt6-1.10.8 { 2398ef42631Sshane db close 2408ef42631Sshane hexio_write test.db 1923 FFFFFFFFFFFFFFFF7F 2418ef42631Sshane sqlite3 db test.db 2428ef42631Sshane catchsql { 2438ef42631Sshane SELECT substr(x,1,8) FROM t1 2448ef42631Sshane } 2458ef42631Sshane} [list 1 {database disk image is malformed}] 2468ef42631Sshane 2478ef42631Sshane# Adjust value of record 1 / field 1 SerialTypeLen and see if the 2488ef42631Sshane# corruption is detected. 2498ef42631Sshane# Set SerialTypeLen to FFFF FF FF FF FF FF FF FF 7F (10 bytes, and overflows). 2508ef42631Sshanedo_test corrupt6-1.10.9 { 2518ef42631Sshane db close 2528ef42631Sshane hexio_write test.db 1923 FFFFFFFFFFFFFFFFFF7F 2538ef42631Sshane sqlite3 db test.db 2548ef42631Sshane catchsql { 2558ef42631Sshane SELECT substr(x,1,8) FROM t1 2568ef42631Sshane } 2578ef42631Sshane} [list 1 {database disk image is malformed}] 2588ef42631Sshane 2592ca8bc08Sshanefinish_test 260