xref: /sqlite-3.40.0/test/corruptG.test (revision 7aa3ebee)
1# 2013-08-01
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#
12
13set testdir [file dirname $argv0]
14source $testdir/tester.tcl
15set testprefix corruptG
16
17# Do not use a codec for tests in this file, as the database file is
18# manipulated directly using tcl scripts (using the [hexio_write] command).
19#
20do_not_use_codec
21
22# These tests deal with corrupt database files
23#
24database_may_be_corrupt
25
26# Create a simple database with a single entry.  Then corrupt the
27# header-size varint on the index payload so that it maps into a
28# negative number.  Try to use the database.
29#
30
31do_execsql_test 1.1 {
32  PRAGMA page_size=512;
33  CREATE TABLE t1(a,b,c);
34  INSERT INTO t1(rowid,a,b,c) VALUES(52,'abc','xyz','123');
35  CREATE INDEX t1abc ON t1(a,b,c);
36}
37
38set idxroot [db one {SELECT rootpage FROM sqlite_master WHERE name = 't1abc'}]
39
40# Corrupt the file
41db close
42hexio_write test.db [expr {$idxroot*512 - 15}] 888080807f
43sqlite3 db test.db
44
45# Try to use the file.
46do_test 1.2 {
47  catchsql {
48    SELECT c FROM t1 WHERE a>'abc';
49  }
50} {1 {database disk image is malformed}}
51do_test 1.3 {
52  catchsql {
53     PRAGMA integrity_check
54  }
55} {1 {database disk image is malformed}}
56do_test 1.4 {
57  catchsql {
58    SELECT c FROM t1 ORDER BY a;
59  }
60} {1 {database disk image is malformed}}
61
62# Corrupt the same file in a slightly different way.  Make the record header
63# sane, but corrupt one of the serial_type value to indicate a huge payload
64# such that the payload begins in allocated space but overflows the buffer.
65#
66db close
67hexio_write test.db [expr {$idxroot*512-15}] 0513ff7f01
68sqlite3 db test.db
69
70do_test 2.1 {
71  catchsql {
72    SELECT rowid FROM t1 WHERE a='abc' and b='xyz123456789XYZ';
73  }
74} {1 {database disk image is malformed}}
75
76finish_test
77