Revise variable declaration moved in the previous check-in so sqlite3VdbeReset()...
[sqlite.git] / test / corruptK.test
blobb20c2d8bf0c2176f972d4a5b87dbee4119eea81a
1 # 2017-03-03
3 # The author disclaims copyright to this source code.  In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
13 set testdir [file dirname $argv0]
14 source $testdir/tester.tcl
15 set testprefix corruptK
17 if {[permutation]=="mmap"} {
18   finish_test
19   return
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 database_may_be_corrupt
27 # Initialize the database.
28
29 do_execsql_test 1.1 {
30   PRAGMA page_size=1024;
31   PRAGMA auto_vacuum=0;
32   CREATE TABLE t1(x);
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));
39   DELETE FROM t1 WHERE rowid=2;  -- free the 100 byte slot
40   PRAGMA page_count
41 } {2}
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.
49 # Then use sqlite3_blob_write() to set the size of said free slot
50 # to 24 bytes (instead of the actual 100).
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().
56 do_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
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 } {}
68 do_execsql_test 1.3 {
69   INSERT INTO t1 VALUES(randomblob(20));
71 do_catchsql_test 1.4 {
72   INSERT INTO t1 VALUES(randomblob(90));
73 } {1 {database disk image is malformed}}
75 #-------------------------------------------------------------------------
76 reset_db
77 do_execsql_test 2.1 {
78   PRAGMA page_size=1024;
79   PRAGMA auto_vacuum=0;
80   CREATE TABLE t1(x);
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.
88   DELETE FROM t1 WHERE rowid IN(2, 4);
89   PRAGMA page_count
90 } {2}
92 do_test 2.2 {
93   db close
94   hexio_write test.db [expr 1024 + 0x388] 53
95   hexio_write test.db [expr 1024 + 0x38A] 03812C
97   sqlite3 db test.db
98   set fd [db incrblob t1 x 5]
99   fconfigure $fd -translation binary -encoding binary
101   seek $fd 22
102   puts -nonewline $fd "\x5d"
103   close $fd
104 } {}
106 do_catchsql_test 2.3 {
107   INSERT INTO t1 VALUES(randomblob(900));
108 } {1 {database disk image is malformed}}
113 finish_test