xref: /sqlite-3.40.0/test/corruptH.test (revision 7df42aba)
1# 2014-01-20
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 corruptH
16
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).
19#
20do_not_use_codec
21database_may_be_corrupt
22
23# Initialize the database.
24#
25do_execsql_test 1.1 {
26  PRAGMA page_size=1024;
27
28  CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
29  INSERT INTO t1 VALUES(1, 'one');
30  INSERT INTO t1 VALUES(2, 'two');
31
32  CREATE TABLE t2(x);
33  INSERT INTO t2 VALUES(randomblob(200));
34  INSERT INTO t2 SELECT randomblob(200) FROM t2;
35  INSERT INTO t2 SELECT randomblob(200) FROM t2;
36  INSERT INTO t2 SELECT randomblob(200) FROM t2;
37  INSERT INTO t2 SELECT randomblob(200) FROM t2;
38  INSERT INTO t2 SELECT randomblob(200) FROM t2;
39  INSERT INTO t2 SELECT randomblob(200) FROM t2;
40} {}
41
42# Corrupt the file so that the root page of t1 is also linked into t2 as
43# a leaf page.
44#
45do_test 1.2 {
46  db eval { SELECT name, rootpage FROM sqlite_master } {
47    set r($name) $rootpage
48  }
49  db close
50  hexio_write test.db [expr {($r(t2)-1)*1024 + 11}] [format %.2X $r(t1)]
51  sqlite3 db test.db
52} {}
53
54do_test 1.3 {
55  db eval { PRAGMA secure_delete=1 }
56  list [catch {
57    db eval { SELECT * FROM t1 WHERE a IN (1, 2) } {
58      db eval { DELETE FROM t2 }
59    }
60  } msg] $msg
61} {1 {database disk image is malformed}}
62
63#-------------------------------------------------------------------------
64reset_db
65
66# Initialize the database.
67#
68do_execsql_test 2.1 {
69  PRAGMA page_size=1024;
70
71  CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
72  INSERT INTO t1 VALUES(1, 'one');
73  INSERT INTO t1 VALUES(2, 'two');
74
75  CREATE TABLE t3(x);
76
77  CREATE TABLE t2(x PRIMARY KEY) WITHOUT ROWID;
78  INSERT INTO t2 VALUES(randomblob(100));
79
80  DROP TABLE t3;
81} {}
82
83do_test 2.2 {
84  db eval { SELECT name, rootpage FROM sqlite_master } {
85    set r($name) $rootpage
86  }
87  db close
88  set fl [hexio_get_int [hexio_read test.db 32 4]]
89
90  hexio_write test.db [expr {($fl-1) * 1024 + 0}] 00000000
91  hexio_write test.db [expr {($fl-1) * 1024 + 4}] 00000001
92  hexio_write test.db [expr {($fl-1) * 1024 + 8}] [format %.8X $r(t1)]
93  hexio_write test.db 36 00000002
94
95  sqlite3 db test.db
96} {}
97
98do_test 2.3 {
99  list [catch {
100  db eval { SELECT * FROM t1 WHERE a IN (1, 2) } {
101    db eval {
102      INSERT INTO t2 SELECT randomblob(100) FROM t2;
103      INSERT INTO t2 SELECT randomblob(100) FROM t2;
104      INSERT INTO t2 SELECT randomblob(100) FROM t2;
105      INSERT INTO t2 SELECT randomblob(100) FROM t2;
106      INSERT INTO t2 SELECT randomblob(100) FROM t2;
107    }
108  }
109  } msg] $msg
110} {1 {database disk image is malformed}}
111
112#-------------------------------------------------------------------------
113reset_db
114
115# Initialize the database.
116#
117do_execsql_test 3.1 {
118  PRAGMA page_size=1024;
119
120  CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
121  INSERT INTO t1 VALUES(1, 'one');
122  INSERT INTO t1 VALUES(2, 'two');
123
124  CREATE TABLE t2(c INTEGER PRAGMA KEY, d);
125  INSERT INTO t2 VALUES(1, randomblob(1100));
126} {}
127
128do_test 3.2 {
129  db eval { SELECT name, rootpage FROM sqlite_master } {
130    set r($name) $rootpage
131  }
132  db close
133
134  hexio_write test.db [expr {($r(t2)-1) * 1024 + 1020}] 00000002
135
136  sqlite3 db test.db
137} {}
138
139do_test 3.3 {
140  list [catch {
141  db eval { SELECT * FROM t1 WHERE a IN (1, 2) } {
142    db eval {
143      DELETE FROM t2 WHERE c=1;
144    }
145  }
146  } msg] $msg
147} {1 {database disk image is malformed}}
148
149finish_test
150
151