1# 2017-03-03 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# 12 13set testdir [file dirname $argv0] 14source $testdir/tester.tcl 15set testprefix corruptK 16 17if {[permutation]=="mmap"} { 18 finish_test 19 return 20} 21 22# This module uses hard-coded offsets which do not work if the reserved_bytes 23# value is nonzero. 24if {[nonzero_reserved_bytes]} {finish_test; return;} 25database_may_be_corrupt 26 27# Initialize the database. 28# 29do_execsql_test 1.1 { 30 PRAGMA page_size=1024; 31 PRAGMA auto_vacuum=0; 32 CREATE TABLE t1(x); 33 34 INSERT INTO t1 VALUES(randomblob(20)); 35 INSERT INTO t1 VALUES(randomblob(100)); -- make this into a free slot 36 INSERT INTO t1 VALUES(randomblob(27)); -- this one will be corrupt 37 INSERT INTO t1 VALUES(randomblob(800)); 38 39 DELETE FROM t1 WHERE rowid=2; -- free the 100 byte slot 40 PRAGMA page_count 41} {2} 42 43 44# Corrupt the database so that the blob stored immediately before 45# the free slot (rowid==3) has an overlarge length field. So that 46# we can use sqlite3_blob_write() to manipulate the size field of 47# the free slot. 48# 49# Then use sqlite3_blob_write() to set the size of said free slot 50# to 24 bytes (instead of the actual 100). 51# 52# Then use the new 24 byte slot. Leaving the in-memory version of 53# the page with zero free slots and a large nFree value. Then try 54# to allocate another slot to get to defragmentPage(). 55# 56do_test 1.2 { 57 db close 58 hexio_write test.db [expr 1024 + 0x360] 21 59 hexio_write test.db [expr 1024 + 0x363] [format %x [expr 31*2 + 12]] 60 sqlite3 db test.db 61 62 set fd [db incrblob t1 x 3] 63 fconfigure $fd -translation binary -encoding binary 64 seek $fd 30 65 puts -nonewline $fd "\x18" 66 close $fd 67} {} 68do_execsql_test 1.3 { 69 INSERT INTO t1 VALUES(randomblob(20)); 70} 71do_catchsql_test 1.4 { 72 INSERT INTO t1 VALUES(randomblob(90)); 73} {1 {database disk image is malformed}} 74 75#------------------------------------------------------------------------- 76reset_db 77do_execsql_test 2.1 { 78 PRAGMA page_size=1024; 79 PRAGMA auto_vacuum=0; 80 CREATE TABLE t1(x); 81 82 INSERT INTO t1 VALUES(randomblob(20)); 83 INSERT INTO t1 VALUES(randomblob(20)); -- free this one 84 INSERT INTO t1 VALUES(randomblob(20)); 85 INSERT INTO t1 VALUES(randomblob(20)); -- and this one 86 INSERT INTO t1 VALUES(randomblob(20)); -- corrupt this one. 87 88 DELETE FROM t1 WHERE rowid IN(2, 4); 89 PRAGMA page_count 90} {2} 91 92do_test 2.2 { 93 db close 94 hexio_write test.db [expr 1024 + 0x388] 53 95 hexio_write test.db [expr 1024 + 0x38A] 03812C 96 97 sqlite3 db test.db 98 set fd [db incrblob t1 x 5] 99 fconfigure $fd -translation binary -encoding binary 100 101 seek $fd 22 102 puts -nonewline $fd "\x5d" 103 close $fd 104} {} 105 106do_catchsql_test 2.3 { 107 INSERT INTO t1 VALUES(randomblob(900)); 108} {1 {database disk image is malformed}} 109 110 111 112 113finish_test 114