xref: /sqlite-3.40.0/test/corrupt2.test (revision 8a29dfde)
1# 2004 August 30
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.
15#
16# $Id: corrupt2.test,v 1.5 2008/03/19 13:03:34 drh Exp $
17
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
21# The following tests - corrupt2-1.* - create some databases corrupted in
22# specific ways and ensure that SQLite detects them as corrupt.
23#
24do_test corrupt2-1.1 {
25  execsql {
26    CREATE TABLE abc(a, b, c);
27  }
28} {}
29
30do_test corrupt2-1.2 {
31
32  # Corrupt the 16 byte magic string at the start of the file
33  file delete -force corrupt.db
34  file delete -force corrupt.db-journal
35  copy_file test.db corrupt.db
36  set f [open corrupt.db RDWR]
37  seek $f 8 start
38  puts $f blah
39  close $f
40
41  sqlite3 db2 corrupt.db
42  catchsql {
43    SELECT * FROM sqlite_master;
44  } db2
45} {1 {file is encrypted or is not a database}}
46
47do_test corrupt2-1.3 {
48  db2 close
49
50  # Corrupt the page-size (bytes 16 and 17 of page 1).
51  file delete -force corrupt.db
52  file delete -force corrupt.db-journal
53  copy_file test.db corrupt.db
54  set f [open corrupt.db RDWR]
55  fconfigure $f -encoding binary
56  seek $f 16 start
57  puts -nonewline $f "\x00\xFF"
58  close $f
59
60  sqlite3 db2 corrupt.db
61  catchsql {
62    SELECT * FROM sqlite_master;
63  } db2
64} {1 {file is encrypted or is not a database}}
65
66do_test corrupt2-1.4 {
67  db2 close
68
69  # Corrupt the free-block list on page 1.
70  file delete -force corrupt.db
71  file delete -force corrupt.db-journal
72  copy_file test.db corrupt.db
73  set f [open corrupt.db RDWR]
74  fconfigure $f -encoding binary
75  seek $f 101 start
76  puts -nonewline $f "\xFF\xFF"
77  close $f
78
79  sqlite3 db2 corrupt.db
80  catchsql {
81    SELECT * FROM sqlite_master;
82  } db2
83} {1 {database disk image is malformed}}
84
85do_test corrupt2-1.5 {
86  db2 close
87
88  # Corrupt the free-block list on page 1.
89  file delete -force corrupt.db
90  file delete -force corrupt.db-journal
91  copy_file test.db corrupt.db
92  set f [open corrupt.db RDWR]
93  fconfigure $f -encoding binary
94  seek $f 101 start
95  puts -nonewline $f "\x00\xC8"
96  seek $f 200 start
97  puts -nonewline $f "\x00\x00"
98  puts -nonewline $f "\x10\x00"
99  close $f
100
101  sqlite3 db2 corrupt.db
102  catchsql {
103    SELECT * FROM sqlite_master;
104  } db2
105} {1 {database disk image is malformed}}
106db2 close
107
108# Corrupt a database by having 2 indices of the same name:
109do_test corrupt2-2.1 {
110
111  file delete -force corrupt.db
112  file delete -force corrupt.db-journal
113  copy_file test.db corrupt.db
114
115  sqlite3 db2 corrupt.db
116  execsql {
117    CREATE INDEX a1 ON abc(a);
118    CREATE INDEX a2 ON abc(b);
119    PRAGMA writable_schema = 1;
120    UPDATE sqlite_master
121      SET name = 'a3', sql = 'CREATE INDEX a3' || substr(sql, 16, 10000)
122      WHERE type = 'index';
123    PRAGMA writable_schema = 0;
124  } db2
125
126  db2 close
127  sqlite3 db2 corrupt.db
128  catchsql {
129    SELECT * FROM sqlite_master;
130  } db2
131} {1 {malformed database schema (a3) - index a3 already exists}}
132
133db2 close
134
135finish_test
136