Enhance the command-line completion extension to return the names of
[sqlite.git] / test / corrupt4.test
blob55969c6a8ce59040f026c5149b7c9dd2b6d36847
1 # 2007 Sept 7
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: corrupt4.test,v 1.1 2007/09/07 14:32:07 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 {
32   finish_test
33   return
36 # Create a database with a freelist containing at least two pages.
38 do_test corrupt4-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     CREATE TABLE t2(y);
46     INSERT INTO t2 VALUES(1);
47     DROP TABLE t1;
48   }
49   file size test.db
50 } [expr {1024*4}]
52 # Verify that there are two pages on the freelist.
54 do_test corrupt4-1.2 {
55   execsql {PRAGMA freelist_count}
56 } {2}
58 # Get the page number for the trunk of the freelist.
60 set trunkpgno [hexio_get_int [hexio_read test.db 32 4]]
61 set baseaddr [expr {($trunkpgno-1)*1024}]
63 # Verify that the trunk of the freelist has exactly one
64 # leaf.
66 do_test corrupt4-1.3 {
67   hexio_get_int [hexio_read test.db [expr {$::baseaddr+4}] 4]
68 } {1}
70 # Insert a negative number as the number of leaves on the trunk.
71 # Then try to add a new element to the freelist.
73 do_test corrupt4-1.4 {
74   hexio_write test.db [expr {$::baseaddr+4}] [hexio_render_int32 -100000000]
75   db close
76   sqlite3 db test.db
77   catchsql {
78     DROP TABLE t2
79   }
80 } {1 {database disk image is malformed}}
82 finish_test