xref: /sqlite-3.40.0/test/corruptA.test (revision ff4fa772)
1a29afcbdSdrh# 2008 July 11
2a29afcbdSdrh#
3a29afcbdSdrh# The author disclaims copyright to this source code.  In place of
4a29afcbdSdrh# a legal notice, here is a blessing:
5a29afcbdSdrh#
6a29afcbdSdrh#    May you do good and not evil.
7a29afcbdSdrh#    May you find forgiveness for yourself and forgive others.
8a29afcbdSdrh#    May you share freely, never taking more than you give.
9a29afcbdSdrh#
10a29afcbdSdrh#***********************************************************************
11a29afcbdSdrh# This file implements regression tests for SQLite library.
12a29afcbdSdrh#
13a29afcbdSdrh# This file implements tests to make sure SQLite does not crash or
14a29afcbdSdrh# segfault if it sees a corrupt database file.  It specifically focuses
15a29afcbdSdrh# on corrupt database headers.
16a29afcbdSdrh#
17a29afcbdSdrh# $Id: corruptA.test,v 1.1 2008/07/11 16:39:23 drh Exp $
18a29afcbdSdrh
19a29afcbdSdrhset testdir [file dirname $argv0]
20a29afcbdSdrhsource $testdir/tester.tcl
21a29afcbdSdrh
22ae23162eSshaneh# Do not use a codec for tests in this file, as the database file is
23ae23162eSshaneh# manipulated directly using tcl scripts (using the [hexio_write] command).
24ae23162eSshaneh#
25ae23162eSshanehdo_not_use_codec
26ae23162eSshaneh
2709fe6143Sdrh# These tests deal with corrupt database files
2809fe6143Sdrh#
2909fe6143Sdrhdatabase_may_be_corrupt
3009fe6143Sdrh
31a29afcbdSdrh
32a29afcbdSdrh# Create a database to work with.
33a29afcbdSdrh#
34a29afcbdSdrhdo_test corruptA-1.1 {
35a29afcbdSdrh  execsql {
36a29afcbdSdrh    CREATE TABLE t1(x);
37a29afcbdSdrh    INSERT INTO t1(x) VALUES(1);
38a29afcbdSdrh  }
39a29afcbdSdrh  expr {[file size test.db]>=1024}
40a29afcbdSdrh} {1}
41a29afcbdSdrhintegrity_check corruptA-1.2
42a29afcbdSdrh
43a29afcbdSdrh# Corrupt the file header in various ways and make sure the corruption
44a29afcbdSdrh# is detected when opening the database file.
45a29afcbdSdrh#
46a29afcbdSdrhdb close
47fda06befSmistachkinforcecopy test.db test.db-template
48a29afcbdSdrh
495cf53537Sdanset unreadable_version 02
505cf53537Sdanifcapable wal { set unreadable_version 03 }
51a29afcbdSdrhdo_test corruptA-2.1 {
52fda06befSmistachkin  forcecopy test.db-template test.db
535cf53537Sdan  hexio_write test.db 19 $unreadable_version   ;# the read format number
54a29afcbdSdrh  sqlite3 db test.db
55a29afcbdSdrh  catchsql {SELECT * FROM t1}
56*ff4fa772Sdrh} {1 {file is not a database}}
57a29afcbdSdrh
58a29afcbdSdrhdo_test corruptA-2.2 {
59a29afcbdSdrh  db close
60fda06befSmistachkin  forcecopy test.db-template test.db
61a29afcbdSdrh  hexio_write test.db 21 41   ;# max embedded payload fraction
62a29afcbdSdrh  sqlite3 db test.db
63a29afcbdSdrh  catchsql {SELECT * FROM t1}
64*ff4fa772Sdrh} {1 {file is not a database}}
65a29afcbdSdrh
66a29afcbdSdrhdo_test corruptA-2.3 {
67a29afcbdSdrh  db close
68fda06befSmistachkin  forcecopy test.db-template test.db
69a29afcbdSdrh  hexio_write test.db 22 1f   ;# min embedded payload fraction
70a29afcbdSdrh  sqlite3 db test.db
71a29afcbdSdrh  catchsql {SELECT * FROM t1}
72*ff4fa772Sdrh} {1 {file is not a database}}
73a29afcbdSdrh
74a29afcbdSdrhdo_test corruptA-2.4 {
75a29afcbdSdrh  db close
76fda06befSmistachkin  forcecopy test.db-template test.db
77a29afcbdSdrh  hexio_write test.db 23 21   ;# min leaf payload fraction
78a29afcbdSdrh  sqlite3 db test.db
79a29afcbdSdrh  catchsql {SELECT * FROM t1}
80*ff4fa772Sdrh} {1 {file is not a database}}
81a29afcbdSdrh
82a29afcbdSdrh
83a29afcbdSdrhfinish_test
84