xref: /sqlite-3.40.0/test/corruptE.test (revision 66c48907)
1195475d8Sshaneh# 2010 February 18
2195475d8Sshaneh#
3195475d8Sshaneh# The author disclaims copyright to this source code.  In place of
4195475d8Sshaneh# a legal notice, here is a blessing:
5195475d8Sshaneh#
6195475d8Sshaneh#    May you do good and not evil.
7195475d8Sshaneh#    May you find forgiveness for yourself and forgive others.
8195475d8Sshaneh#    May you share freely, never taking more than you give.
9195475d8Sshaneh#
10195475d8Sshaneh#***********************************************************************
11195475d8Sshaneh# This file implements regression tests for SQLite library.
12195475d8Sshaneh#
13195475d8Sshaneh# This file implements tests to make sure SQLite does not crash or
14195475d8Sshaneh# segfault if it sees a corrupt database file.  It specifcally
15195475d8Sshaneh# focuses on rowid order corruption.
16195475d8Sshaneh#
17195475d8Sshaneh
18195475d8Sshanehset testdir [file dirname $argv0]
19195475d8Sshanehsource $testdir/tester.tcl
20195475d8Sshaneh
21af3906a7Sdrh# This module uses hard-coded offsets which do not work if the reserved_bytes
22af3906a7Sdrh# value is nonzero.
23af3906a7Sdrhif {[nonzero_reserved_bytes]} {finish_test; return;}
2468928b6cSdan
2509fe6143Sdrh# These tests deal with corrupt database files
2609fe6143Sdrh#
2709fe6143Sdrhdatabase_may_be_corrupt
2809fe6143Sdrh
294d077f92Sdrh# Do not run the tests in this file if ENABLE_OVERSIZE_CELL_CHECK is on.
304d077f92Sdrh#
314d077f92Sdrhifcapable oversize_cell_check {
324d077f92Sdrh  finish_test
334d077f92Sdrh  return
344d077f92Sdrh}
354d077f92Sdrh
36195475d8Sshaneh# Construct a compact, dense database for testing.
37195475d8Sshaneh#
38195475d8Sshanehdo_test corruptE-1.1 {
39*66c48907Sdrh  sqlite3_db_config db LEGACY_FILE_FORMAT 1
40195475d8Sshaneh  execsql {
41195475d8Sshaneh    PRAGMA auto_vacuum = 0;
42195475d8Sshaneh    BEGIN;
43195475d8Sshaneh    CREATE TABLE t1(x,y);
44195475d8Sshaneh    INSERT INTO t1 VALUES(1,1);
45195475d8Sshaneh    INSERT OR IGNORE INTO t1 SELECT x*2,y FROM t1;
46195475d8Sshaneh    INSERT OR IGNORE INTO t1 SELECT x*3,y FROM t1;
47195475d8Sshaneh    INSERT OR IGNORE INTO t1 SELECT x*5,y FROM t1;
48195475d8Sshaneh    INSERT OR IGNORE INTO t1 SELECT x*7,y FROM t1;
49195475d8Sshaneh    INSERT OR IGNORE INTO t1 SELECT x*11,y FROM t1;
50195475d8Sshaneh    INSERT OR IGNORE INTO t1 SELECT x*13,y FROM t1;
51195475d8Sshaneh    INSERT OR IGNORE INTO t1 SELECT x*17,y FROM t1;
52195475d8Sshaneh    INSERT OR IGNORE INTO t1 SELECT x*19,y FROM t1;
53195475d8Sshaneh    CREATE INDEX t1i1 ON t1(x);
543f4d1d1bSdrh    CREATE TABLE t2 AS SELECT x,2 as y FROM t1 WHERE rowid%5!=0 ORDER BY rowid;
55195475d8Sshaneh    COMMIT;
56195475d8Sshaneh  }
57195475d8Sshaneh} {}
58195475d8Sshaneh
59195475d8Sshanehifcapable {integrityck} {
60195475d8Sshaneh  integrity_check corruptE-1.2
61195475d8Sshaneh}
62195475d8Sshaneh
63195475d8Sshaneh# Setup for the tests.  Make a backup copy of the good database in test.bu.
64195475d8Sshaneh#
65195475d8Sshanehdb close
66fda06befSmistachkinforcecopy test.db test.bu
67195475d8Sshanehsqlite3 db test.db
68195475d8Sshanehset fsize [file size test.db]
69195475d8Sshaneh
70195475d8Sshaneh
71195475d8Sshanehdo_test corruptE-2.1 {
72195475d8Sshaneh  db close
73fda06befSmistachkin  forcecopy test.bu test.db
74195475d8Sshaneh
75195475d8Sshaneh  # insert corrupt byte(s)
76195475d8Sshaneh  hexio_write test.db 2041 [format %02x 0x2e]
77195475d8Sshaneh
78195475d8Sshaneh  sqlite3 db test.db
79195475d8Sshaneh
80cbc6b71fSdrh  catchsql {PRAGMA integrity_check}
81cbc6b71fSdrh} {/ out of order/}
82195475d8Sshaneh
83195475d8Sshanehdo_test corruptE-2.2 {
84195475d8Sshaneh  db close
85fda06befSmistachkin  forcecopy test.bu test.db
86195475d8Sshaneh
87195475d8Sshaneh  # insert corrupt byte(s)
88195475d8Sshaneh  hexio_write test.db 2047 [format %02x 0x84]
89195475d8Sshaneh
90195475d8Sshaneh  sqlite3 db test.db
91195475d8Sshaneh
92cbc6b71fSdrh  catchsql {PRAGMA integrity_check}
93cbc6b71fSdrh} {/ Extends off end of page/}
94195475d8Sshaneh
95195475d8Sshanehdo_test corruptE-2.3 {
96195475d8Sshaneh  db close
97fda06befSmistachkin  forcecopy test.bu test.db
98195475d8Sshaneh
99195475d8Sshaneh  # insert corrupt byte(s)
100195475d8Sshaneh  hexio_write test.db 7420 [format %02x 0xa8]
101195475d8Sshaneh  hexio_write test.db 10459 [format %02x 0x8d]
102195475d8Sshaneh
103195475d8Sshaneh  sqlite3 db test.db
104195475d8Sshaneh
105cbc6b71fSdrh  catchsql {PRAGMA integrity_check}
106cbc6b71fSdrh} {/out of order/}
107195475d8Sshaneh
108195475d8Sshanehdo_test corruptE-2.4 {
109195475d8Sshaneh  db close
110fda06befSmistachkin  forcecopy test.bu test.db
111195475d8Sshaneh
112195475d8Sshaneh  # insert corrupt byte(s)
113195475d8Sshaneh  hexio_write test.db 10233 [format %02x 0xd0]
114195475d8Sshaneh
115195475d8Sshaneh  sqlite3 db test.db
116195475d8Sshaneh
117cbc6b71fSdrh  catchsql {PRAGMA integrity_check}
118cbc6b71fSdrh} {/out of order/}
119195475d8Sshaneh
120195475d8Sshaneh
121195475d8Sshanehset tests [list {10233 0xd0} \
122195475d8Sshaneh                {941 0x42} \
123195475d8Sshaneh                {2041 0xd0} \
124195475d8Sshaneh                {2042 0x1f} \
125195475d8Sshaneh                {2274 0x75} \
126195475d8Sshaneh                {3267 0xf2} \
127195475d8Sshaneh                {5113 0x36} \
128195475d8Sshaneh                {10233 0x84} \
129195475d8Sshaneh                {10234 0x74} \
130195475d8Sshaneh                {10239 0x41} \
131195475d8Sshaneh                {11273 0x28} \
132195475d8Sshaneh                {11461 0xe6} \
133195475d8Sshaneh                {12297 0xd7} \
134195475d8Sshaneh                {13303 0x53} ]
135195475d8Sshaneh
136195475d8Sshanehset tc 1
137195475d8Sshanehforeach test $tests {
138195475d8Sshaneh  do_test corruptE-3.$tc {
139195475d8Sshaneh    db close
140fda06befSmistachkin    forcecopy test.bu test.db
141195475d8Sshaneh
142195475d8Sshaneh    # insert corrupt byte(s)
143195475d8Sshaneh    hexio_write test.db [lindex $test 0] [format %02x [lindex $test 1]]
144195475d8Sshaneh
145195475d8Sshaneh    sqlite3 db test.db
146195475d8Sshaneh
147cbc6b71fSdrh    catchsql {PRAGMA integrity_check}
148cbc6b71fSdrh  } {/out of order/}
149195475d8Sshaneh  incr tc 1
150195475d8Sshaneh}
151195475d8Sshaneh
152195475d8Sshanehfinish_test
153