Merge fixes from the 3.21 branch.
[sqlite.git] / test / corruptF.test
blob8c4fd84219365cea4a3d7fa7e89b343cfd95a33a
1 # 2012 January 12
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 corruptF
17 # Do not use a codec for tests in this file, as the database file is
18 # manipulated directly using tcl scripts (using the [hexio_write] command).
20 do_not_use_codec
22 # These tests deal with corrupt database files
24 database_may_be_corrupt
26 proc str {i} { format %08d $i }
28 # Create a 6 page database containing a single table - t1. Table t1 
29 # consists of page 2 (the root page) and pages 5 and 6 (leaf pages). 
30 # Database pages 3 and 4 are on the free list.
32 proc create_test_db {} {
33   catch { db close }
34   forcedelete test.db
35   sqlite3 db test.db
36   db func str str
37   execsql {
38     PRAGMA auto_vacuum = 0;
39     PRAGMA page_size = 1024;
40     CREATE TABLE t1(x);         /* root page = 2 */
41     CREATE TABLE t2(x);         /* root page = 3 */
42     CREATE TABLE t3(x);         /* root page = 4 */
44     INSERT INTO t1 VALUES(str(1));
45     INSERT INTO t1 SELECT str(rowid+1) FROM t1;
46     INSERT INTO t1 SELECT str(rowid+2) FROM t1;
47     INSERT INTO t1 SELECT str(rowid+4) FROM t1;
48     INSERT INTO t1 SELECT str(rowid+8) FROM t1;
49     INSERT INTO t1 SELECT str(rowid+16) FROM t1;
50     INSERT INTO t1 SELECT str(rowid+32) FROM t1;
51     INSERT INTO t1 SELECT str(rowid+64) FROM t1;
52     DROP TABLE t2;
53     DROP TABLE t3;
54   }
55   db close
58 do_test 1.1 { create_test_db } {}
60 # Check the db is as we expect. 6 pages in total, with 3 and 4 on the free
61 # list. Page 3 is the free list trunk and page 4 is a leaf.
63 do_test 1.2 { file size test.db } [expr 6*1024]
64 do_test 1.3 { hexio_read test.db 32 4 } 00000003
65 do_test 1.4 { hexio_read test.db [expr 2*1024] 12 } 000000000000000100000004
67 # Change the free-list entry to page 6 and reopen the db file.
68 do_test 1.5 { 
69   hexio_write test.db [expr 2*1024 + 8] 00000006 
70   sqlite3 db test.db
71 } {}
73 # Now create a new table in the database file. The root of the new table
74 # is page 6, which is also the right-most leaf page in table t1.
76 do_execsql_test 1.6 { 
77   CREATE TABLE t4(x);
78   SELECT * FROM sqlite_master;
79 } {
80   table t1 t1 2 {CREATE TABLE t1(x)} 
81   table t4 t4 6 {CREATE TABLE t4(x)}
84 # At one point this was causing an assert to fail.
86 # This statement opens a cursor on table t1 and does a full table scan. As
87 # each row is visited, it is copied into table t4. There is no temporary
88 # table.
90 # When the t1 cursor reaches page 6 (which is both the right-most leaf of
91 # t1 and the root of t4), it continues to iterate through the keys within
92 # it (which at this point are keys that have been inserted into t4). And
93 # for each row visited, another row is inserted into page 6 - it being the
94 # root page of t4. Eventually, page 6 becomes full and the height of the
95 # b-tree for table t4 increased. From the point of view of the t1 cursor,
96 # this unexpectedly reduces the number of keys on page 6 in the middle of
97 # its iteration, which causes an assert() to fail.
99 db_save_and_close
100 if 1 {
101 for {set i 0} {$i < 128} {incr i} {
102   db_restore_and_reopen
103   do_test 1.7.$i { 
104     set res [
105       catchsql { INSERT INTO t4 SELECT x FROM t1 WHERE rowid>$i }
106     ]
107     if {$res == "0 {}" || $res == "1 {database disk image is malformed}"} {
108       set res ""
109     }
110     set res
111   } {}
115 do_test 2.1 { create_test_db } {}
116 do_test 2.2 { file size test.db } [expr 6*1024]
117 do_test 2.3 { hexio_read test.db 32 4 } 00000003
118 do_test 2.4 { hexio_read test.db [expr 2*1024] 12 } 000000000000000100000004
120 # Change the free-list entry to page 5 and reopen the db file.
121 do_test 2.5 { 
122   hexio_write test.db [expr 2*1024 + 8] 00000005 
123   sqlite3 db test.db
124 } {}
126 # Now create a new table in the database file. The root of the new table
127 # is page 5, which is also the right-most leaf page in table t1.
129 do_execsql_test 2.6 { 
130   CREATE TABLE t4(x);
131   SELECT * FROM sqlite_master;
132 } {
133   table t1 t1 2 {CREATE TABLE t1(x)} 
134   table t4 t4 5 {CREATE TABLE t4(x)}
137 db_save_and_close
138 for {set i 127} {$i >= 0} {incr i -1} {
139   db_restore_and_reopen
140   do_test 2.7.$i { 
141     set res [
142       catchsql { 
143         INSERT INTO t4 SELECT x FROM t1 WHERE rowid<$i ORDER BY rowid DESC 
144       }
145     ]
146     if {$res == "0 {}" || $res == "1 {database disk image is malformed}"} {
147       set res ""
148     }
149     set res
150   } {}
153 finish_test