Fix a problem causing the recovery extension to use excessive memory and CPU time...
[sqlite.git] / test / corrupt3.test
blob691302f7af5dc87d1e3a5de4d001a89abbb3101a
1 # 2007 April 6
3 # The author disclaims copyright to this source code.  In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library.
13 # This file implements tests to make sure SQLite does not crash or
14 # segfault if it sees a corrupt database file.
16 # $Id: corrupt3.test,v 1.2 2007/04/06 21:42:22 drh Exp $
18 set testdir [file dirname $argv0]
19 source $testdir/tester.tcl
21 # This module uses hard-coded offsets which do not work if the reserved_bytes
22 # value is nonzero.
23 if {[nonzero_reserved_bytes]} {finish_test; return;}
25 # These tests deal with corrupt database files
27 database_may_be_corrupt
29 # We must have the page_size pragma for these tests to work.
31 ifcapable !pager_pragmas||direct_read {
32   finish_test
33   return
36 # Create a database with an overflow page.
38 do_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}]
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.
54 do_test corrupt3-1.2 {
55   hexio_get_int [hexio_read test.db 16 2]
56 } 1024   ;# The page size is 1024
57 do_test corrupt3-1.3 {
58   hexio_get_int [hexio_read test.db 20 1]
59 } 0      ;# Unused bytes per page is 0
60 do_test corrupt3-1.4 {
61   hexio_get_int [hexio_read test.db 2044 4]
62 } 3      ;# Overflow page is 3
63 do_test corrupt3-1.5 {
64   hexio_get_int [hexio_read test.db 2048 4]
65 } 0      ;# First chained overflow is 0
67 integrity_check corrupt3-1.6
69 # Make the overflow chain loop back on itself.   See if the
70 # corruption is detected.
72 do_test corrupt3-1.7 {
73   db close
74   hexio_write test.db 2048 [hexio_render_int32 3]
75   sqlite3 db test.db
76   catchsql {
77     SELECT x FROM t1
78   }
79 } [list 0 $bigstring]
80 do_test corrupt3-1.8 {
81   catchsql {
82     PRAGMA integrity_check
83   }
84 } {0 {{*** in database main ***
85 Tree 2 page 2 cell 0: 2nd reference to page 3}}}
87 # Change the pointer for the first page of the overflow
88 # change to be a non-existant page.
90 do_test corrupt3-1.9 {
91   db close
92   hexio_write test.db 2044 [hexio_render_int32 4]
93   sqlite3 db test.db
94   catchsql {
95     SELECT substr(x,1,10) FROM t1
96   }
97 } [list 1 {database disk image is malformed}]
98 do_test corrupt3-1.10 {
99   catchsql {
100     PRAGMA integrity_check
101   }
102 } {0 {{*** in database main ***
103 Tree 2 page 2 cell 0: invalid page number 4
104 Page 3: never used}}}
105 do_test corrupt3-1.11 {
106   db close
107   hexio_write test.db 2044 [hexio_render_int32 0]
108   sqlite3 db test.db
109   catchsql {
110     SELECT substr(x,1,10) FROM t1
111   }
112 } [list 1 {database disk image is malformed}]
113 do_test corrupt3-1.12 {
114   catchsql {
115     PRAGMA integrity_check
116   }
117 } {0 {{*** in database main ***
118 Tree 2 page 2 cell 0: overflow list length is 0 but should be 1
119 Page 3: never used}}}
121 finish_test