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# We must have the page_size pragma for these tests to work. 28# 29ifcapable !pager_pragmas { 30 finish_test 31 return 32} 33 34# Create a simple, small database. 35# 36do_test corrupt6-1.1 { 37 execsql { 38 PRAGMA auto_vacuum=OFF; 39 PRAGMA page_size=1024; 40 CREATE TABLE t1(x); 41 INSERT INTO t1(x) VALUES('varint32-01234567890123456789012345678901234567890123456789'); 42 INSERT INTO t1(x) VALUES('varint32-01234567890123456789012345678901234567890123456789'); 43 } 44 file size test.db 45} [expr {1024*2}] 46 47# Verify that the file format is as we expect. The page size 48# should be 1024 bytes. 49# 50do_test corrupt6-1.2 { 51 hexio_get_int [hexio_read test.db 16 2] 52} 1024 ;# The page size is 1024 53do_test corrupt6-1.3 { 54 hexio_get_int [hexio_read test.db 20 1] 55} 0 ;# Unused bytes per page is 0 56 57integrity_check corrupt6-1.4 58 59# Verify SerialTypeLen for first field of two records as we expect. 60# SerialTypeLen = (len*2+12) = 60*2+12 = 132 61do_test corrupt6-1.5.1 { 62 hexio_read test.db 1923 2 63} 8103 ;# First text field size is 81 03 == 131 64do_test corrupt6-1.5.2 { 65 hexio_read test.db 1987 2 66} 8103 ;# Second text field size is 81 03 == 131 67 68# Verify simple query results as expected. 69do_test corrupt6-1.6 { 70 db close 71 sqlite3 db test.db 72 catchsql { 73 SELECT substr(x,1,8) FROM t1 74 } 75} [list 0 {varint32 varint32} ] 76integrity_check corrupt6-1.7 77 78# Adjust value of record 1 / field 1 SerialTypeLen and see if the 79# corruption is detected. 80# Increase SerialTypeLen by 2. 81do_test corrupt6-1.8.1 { 82 db close 83 hexio_write test.db 1923 8105 84 sqlite3 db test.db 85 catchsql { 86 SELECT substr(x,1,8) FROM t1 87 } 88} [list 1 {database disk image is malformed}] 89 90# Adjust value of record 1 / field 1 SerialTypeLen and see if the 91# corruption is detected. 92# Decrease SerialTypeLen by 2. 93do_test corrupt6-1.8.2 { 94 db close 95 hexio_write test.db 1923 8101 96 sqlite3 db test.db 97 catchsql { 98 SELECT substr(x,1,8) FROM t1 99 } 100} [list 1 {database disk image is malformed}] 101 102# Put value of record 1 / field 1 SerialTypeLen back. 103do_test corrupt6-1.8.3 { 104 db close 105 hexio_write test.db 1923 8103 106 sqlite3 db test.db 107 catchsql { 108 SELECT substr(x,1,8) FROM t1 109 } 110} [list 0 {varint32 varint32} ] 111integrity_check corrupt6-1.8.4 112 113# Adjust value of record 2 / field 1 SerialTypeLen and see if the 114# corruption is detected. 115# Increase SerialTypeLen by 2. 116do_test corrupt6-1.9.1 { 117 db close 118 hexio_write test.db 1987 8105 119 sqlite3 db test.db 120 catchsql { 121 SELECT substr(x,1,8) FROM t1 122 } 123} [list 1 {database disk image is malformed}] 124 125# Adjust value of record 2 / field 2 SerialTypeLen and see if the 126# corruption is detected. 127# Decrease SerialTypeLen by 2. 128do_test corrupt6-1.9.2 { 129 db close 130 hexio_write test.db 1987 8101 131 sqlite3 db test.db 132 catchsql { 133 SELECT substr(x,1,8) FROM t1 134 } 135} [list 1 {database disk image is malformed}] 136 137# Put value of record 1 / field 2 SerialTypeLen back. 138do_test corrupt6-1.9.3 { 139 db close 140 hexio_write test.db 1987 8103 141 sqlite3 db test.db 142 catchsql { 143 SELECT substr(x,1,8) FROM t1 144 } 145} [list 0 {varint32 varint32} ] 146integrity_check corrupt6-1.9.4 147 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) 151do_test corrupt6-1.10.1 { 152 db close 153 hexio_write test.db 1923 FF7F 154 sqlite3 db test.db 155 catchsql { 156 SELECT substr(x,1,8) FROM t1 157 } 158} [list 1 {database disk image is malformed}] 159 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) 163do_test corrupt6-1.10.2 { 164 db close 165 hexio_write test.db 1923 FFFF7F 166 sqlite3 db test.db 167 catchsql { 168 SELECT substr(x,1,8) FROM t1 169 } 170} [list 1 {database disk image is malformed}] 171 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) 175do_test corrupt6-1.10.3 { 176 db close 177 hexio_write test.db 1923 FFFFFF7F 178 sqlite3 db test.db 179 catchsql { 180 SELECT substr(x,1,8) FROM t1 181 } 182} [list 1 {database disk image is malformed}] 183 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) 187do_test corrupt6-1.10.4 { 188 db close 189 hexio_write test.db 1923 FFFFFFFF7F 190 sqlite3 db test.db 191 catchsql { 192 SELECT substr(x,1,8) FROM t1 193 } 194} [list 1 {database disk image is malformed}] 195 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). 199do_test corrupt6-1.10.5 { 200 db close 201 hexio_write test.db 1923 FFFFFFFFFF7F 202 sqlite3 db test.db 203 catchsql { 204 SELECT substr(x,1,8) FROM t1 205 } 206} [list 1 {database disk image is malformed}] 207 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). 211do_test corrupt6-1.10.6 { 212 db close 213 hexio_write test.db 1923 FFFFFFFFFFFF7F 214 sqlite3 db test.db 215 catchsql { 216 SELECT substr(x,1,8) FROM t1 217 } 218} [list 1 {database disk image is malformed}] 219 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). 223do_test corrupt6-1.10.7 { 224 db close 225 hexio_write test.db 1923 FFFFFFFFFFFFFF7F 226 sqlite3 db test.db 227 catchsql { 228 SELECT substr(x,1,8) FROM t1 229 } 230} [list 1 {database disk image is malformed}] 231 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). 235do_test corrupt6-1.10.8 { 236 db close 237 hexio_write test.db 1923 FFFFFFFFFFFFFFFF7F 238 sqlite3 db test.db 239 catchsql { 240 SELECT substr(x,1,8) FROM t1 241 } 242} [list 1 {database disk image is malformed}] 243 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). 247do_test corrupt6-1.10.9 { 248 db close 249 hexio_write test.db 1923 FFFFFFFFFFFFFFFFFF7F 250 sqlite3 db test.db 251 catchsql { 252 SELECT substr(x,1,8) FROM t1 253 } 254} [list 1 {database disk image is malformed}] 255 256finish_test 257