xref: /sqlite-3.40.0/test/corrupt6.test (revision 2ca8bc08)
1# 2008 May 6
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.  It specifically focuses
15# on corrupt SerialTypeLen values.
16#
17# $Id: corrupt6.test,v 1.1 2008/05/07 18:59:29 shane Exp $
18
19set testdir [file dirname $argv0]
20source $testdir/tester.tcl
21
22# We must have the page_size pragma for these tests to work.
23#
24ifcapable !pager_pragmas {
25  finish_test
26  return
27}
28
29# Create a simple, small database.
30#
31do_test corrupt6-1.1 {
32  execsql {
33    PRAGMA auto_vacuum=OFF;
34    PRAGMA page_size=1024;
35    CREATE TABLE t1(x);
36    INSERT INTO t1(x) VALUES('varint32-01234567890123456789012345678901234567890123456789');
37    INSERT INTO t1(x) VALUES('varint32-01234567890123456789012345678901234567890123456789');
38  }
39  file size test.db
40} [expr {1024*2}]
41
42# Verify that the file format is as we expect.  The page size
43# should be 1024 bytes.
44#
45do_test corrupt6-1.2 {
46  hexio_get_int [hexio_read test.db 16 2]
47} 1024   ;# The page size is 1024
48do_test corrupt6-1.3 {
49  hexio_get_int [hexio_read test.db 20 1]
50} 0      ;# Unused bytes per page is 0
51
52integrity_check corrupt6-1.4
53
54# Verify SerialTypeLen for first field of two records as we expect.
55# SerialTypeLen = (len*2+12) = 60*2+12 = 132
56do_test corrupt6-1.5.1 {
57  hexio_read test.db 1923 2
58} 8103      ;# First text field size if 81 03 == 131
59do_test corrupt6-1.5.2 {
60  hexio_read test.db 1987 2
61} 8103      ;# Second text field size if 81 03 == 131
62
63# Verify simple query results as expected.
64do_test corrupt6-1.6 {
65  db close
66  sqlite3 db test.db
67  catchsql {
68    SELECT substr(x,1,8) FROM t1
69  }
70} [list 0 {varint32 varint32} ]
71integrity_check corrupt6-1.7
72
73# Adjust value of record 1 / field 1 SerialTypeLen and see if the
74# corruption is detected.
75# Increase SerialTypeLen by 2.
76do_test corrupt6-1.8.1 {
77  db close
78  hexio_write test.db 1923 8105
79  sqlite3 db test.db
80  catchsql {
81    SELECT substr(x,1,8) FROM t1
82  }
83} [list 1 {database disk image is malformed}]
84
85# Adjust value of record 1 / field 1 SerialTypeLen and see if the
86# corruption is detected.
87# Decrease SerialTypeLen by 2.
88do_test corrupt6-1.8.2 {
89  db close
90  hexio_write test.db 1923 8101
91  sqlite3 db test.db
92  catchsql {
93    SELECT substr(x,1,8) FROM t1
94  }
95} [list 1 {database disk image is malformed}]
96
97# Put value of record 1 / field 1 SerialTypeLen back.
98do_test corrupt6-1.8.3 {
99  db close
100  hexio_write test.db 1923 8103
101  sqlite3 db test.db
102  catchsql {
103    SELECT substr(x,1,8) FROM t1
104  }
105} [list 0 {varint32 varint32} ]
106integrity_check corrupt6-1.8.4
107
108# Adjust value of record 2 / field 1 SerialTypeLen and see if the
109# corruption is detected.
110# Increase SerialTypeLen by 2.
111do_test corrupt6-1.9.1 {
112  db close
113  hexio_write test.db 1987 8105
114  sqlite3 db test.db
115  catchsql {
116    SELECT substr(x,1,8) FROM t1
117  }
118} [list 1 {database disk image is malformed}]
119
120# Adjust value of record 2 / field 2 SerialTypeLen and see if the
121# corruption is detected.
122# Decrease SerialTypeLen by 2.
123do_test corrupt6-1.9.2 {
124  db close
125  hexio_write test.db 1987 8101
126  sqlite3 db test.db
127  catchsql {
128    SELECT substr(x,1,8) FROM t1
129  }
130} [list 1 {database disk image is malformed}]
131
132# Put value of record 1 / field 2 SerialTypeLen back.
133do_test corrupt6-1.9.3 {
134  db close
135  hexio_write test.db 1987 8103
136  sqlite3 db test.db
137  catchsql {
138    SELECT substr(x,1,8) FROM t1
139  }
140} [list 0 {varint32 varint32} ]
141integrity_check corrupt6-1.9.4
142
143finish_test
144