xref: /sqlite-3.40.0/test/corruptE.test (revision 7aa3ebee)
1# 2010 February 18
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 specifcally
15# focuses on rowid order corruption.
16#
17
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
21# Do not use a codec for tests in this file, as the database file is
22# manipulated directly using tcl scripts (using the [hexio_write] command).
23#
24do_not_use_codec
25
26# These tests deal with corrupt database files
27#
28database_may_be_corrupt
29
30# Do not run the tests in this file if ENABLE_OVERSIZE_CELL_CHECK is on.
31#
32ifcapable oversize_cell_check {
33  finish_test
34  return
35}
36
37# Construct a compact, dense database for testing.
38#
39do_test corruptE-1.1 {
40  execsql {
41    PRAGMA auto_vacuum = 0;
42    PRAGMA legacy_file_format=1;
43    BEGIN;
44    CREATE TABLE t1(x,y);
45    INSERT INTO t1 VALUES(1,1);
46    INSERT OR IGNORE INTO t1 SELECT x*2,y FROM t1;
47    INSERT OR IGNORE INTO t1 SELECT x*3,y FROM t1;
48    INSERT OR IGNORE INTO t1 SELECT x*5,y FROM t1;
49    INSERT OR IGNORE INTO t1 SELECT x*7,y FROM t1;
50    INSERT OR IGNORE INTO t1 SELECT x*11,y FROM t1;
51    INSERT OR IGNORE INTO t1 SELECT x*13,y FROM t1;
52    INSERT OR IGNORE INTO t1 SELECT x*17,y FROM t1;
53    INSERT OR IGNORE INTO t1 SELECT x*19,y FROM t1;
54    CREATE INDEX t1i1 ON t1(x);
55    CREATE TABLE t2 AS SELECT x,2 as y FROM t1 WHERE rowid%5!=0 ORDER BY rowid;
56    COMMIT;
57  }
58} {}
59
60ifcapable {integrityck} {
61  integrity_check corruptE-1.2
62}
63
64# Setup for the tests.  Make a backup copy of the good database in test.bu.
65#
66db close
67forcecopy test.db test.bu
68sqlite3 db test.db
69set fsize [file size test.db]
70
71
72do_test corruptE-2.1 {
73  db close
74  forcecopy test.bu test.db
75
76  # insert corrupt byte(s)
77  hexio_write test.db 2041 [format %02x 0x2e]
78
79  sqlite3 db test.db
80
81  catchsql {PRAGMA integrity_check}
82} {/ out of order/}
83
84do_test corruptE-2.2 {
85  db close
86  forcecopy test.bu test.db
87
88  # insert corrupt byte(s)
89  hexio_write test.db 2047 [format %02x 0x84]
90
91  sqlite3 db test.db
92
93  catchsql {PRAGMA integrity_check}
94} {/ Extends off end of page/}
95
96do_test corruptE-2.3 {
97  db close
98  forcecopy test.bu test.db
99
100  # insert corrupt byte(s)
101  hexio_write test.db 7420 [format %02x 0xa8]
102  hexio_write test.db 10459 [format %02x 0x8d]
103
104  sqlite3 db test.db
105
106  catchsql {PRAGMA integrity_check}
107} {/out of order/}
108
109do_test corruptE-2.4 {
110  db close
111  forcecopy test.bu test.db
112
113  # insert corrupt byte(s)
114  hexio_write test.db 10233 [format %02x 0xd0]
115
116  sqlite3 db test.db
117
118  catchsql {PRAGMA integrity_check}
119} {/out of order/}
120
121
122set tests [list {10233 0xd0} \
123                {941 0x42} \
124                {2041 0xd0} \
125                {2042 0x1f} \
126                {2274 0x75} \
127                {3267 0xf2} \
128                {5113 0x36} \
129                {10233 0x84} \
130                {10234 0x74} \
131                {10239 0x41} \
132                {11273 0x28} \
133                {11461 0xe6} \
134                {12297 0xd7} \
135                {13303 0x53} ]
136
137set tc 1
138foreach test $tests {
139  do_test corruptE-3.$tc {
140    db close
141    forcecopy test.bu test.db
142
143    # insert corrupt byte(s)
144    hexio_write test.db [lindex $test 0] [format %02x [lindex $test 1]]
145
146    sqlite3 db test.db
147
148    catchsql {PRAGMA integrity_check}
149  } {/out of order/}
150  incr tc 1
151}
152
153finish_test
154