xref: /sqlite-3.40.0/test/corrupt2.test (revision 4dcbdbff)
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.2 2005/01/22 03:39:39 danielk1977 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 a]
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 a]
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 a]
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 a]
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
108finish_test
109
110