xref: /sqlite-3.40.0/test/corrupt3.test (revision 9bccde3d)
1# 2007 April 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.
15#
16# $Id: corrupt3.test,v 1.2 2007/04/06 21:42:22 drh Exp $
17
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
21# This module uses hard-coded offsets which do not work if the reserved_bytes
22# value is nonzero.
23if {[nonzero_reserved_bytes]} {finish_test; return;}
24
25# These tests deal with corrupt database files
26#
27database_may_be_corrupt
28
29# We must have the page_size pragma for these tests to work.
30#
31ifcapable !pager_pragmas||direct_read {
32  finish_test
33  return
34}
35
36# Create a database with an overflow page.
37#
38do_test corrupt3-1.1 {
39  set bigstring [string repeat 0123456789 200]
40  execsql {
41    PRAGMA auto_vacuum=OFF;
42    PRAGMA page_size=1024;
43    CREATE TABLE t1(x);
44    INSERT INTO t1 VALUES($bigstring);
45  }
46  file size test.db
47} [expr {1024*3}]
48
49# Verify that the file format is as we expect.  The page size
50# should be 1024 bytes.  The only record should have a single
51# overflow page.  The overflow page is page 3.  The pointer to
52# the overflow page is on the last 4 bytes of page 2.
53#
54do_test corrupt3-1.2 {
55  hexio_get_int [hexio_read test.db 16 2]
56} 1024   ;# The page size is 1024
57do_test corrupt3-1.3 {
58  hexio_get_int [hexio_read test.db 20 1]
59} 0      ;# Unused bytes per page is 0
60do_test corrupt3-1.4 {
61  hexio_get_int [hexio_read test.db 2044 4]
62} 3      ;# Overflow page is 3
63do_test corrupt3-1.5 {
64  hexio_get_int [hexio_read test.db 2048 4]
65} 0      ;# First chained overflow is 0
66
67integrity_check corrupt3-1.6
68
69# Make the overflow chain loop back on itself.   See if the
70# corruption is detected.   (Actually, the last pointer in
71# an overflow chain is ignored, so this is not an error.)
72#
73do_test corrupt3-1.7 {
74  db close
75  hexio_write test.db 2048 [hexio_render_int32 3]
76  sqlite3 db test.db
77  catchsql {
78    SELECT x FROM t1
79  }
80} [list 0 $bigstring]
81integrity_check corrupt3-1.8
82
83# Change the pointer for the first page of the overflow
84# change to be a non-existant page.
85#
86do_test corrupt3-1.9 {
87  db close
88  hexio_write test.db 2044 [hexio_render_int32 4]
89  sqlite3 db test.db
90  catchsql {
91    SELECT substr(x,1,10) FROM t1
92  }
93} [list 0 0123456789]
94do_test corrupt3-1.10 {
95  catchsql {
96    PRAGMA integrity_check
97  }
98} {0 {{*** in database main ***
99On tree page 2 cell 0: invalid page number 4
100Page 3 is never used}}}
101do_test corrupt3-1.11 {
102  db close
103  hexio_write test.db 2044 [hexio_render_int32 0]
104  sqlite3 db test.db
105  catchsql {
106    SELECT substr(x,1,10) FROM t1
107  }
108} [list 1 {database disk image is malformed}]
109do_test corrupt3-1.12 {
110  catchsql {
111    PRAGMA integrity_check
112  }
113} {0 {{*** in database main ***
114On tree page 2 cell 0: 1 of 1 pages missing from overflow list starting at 0
115Page 3 is never used}}}
116
117finish_test
118