1# 2007 Sept 7
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: corrupt4.test,v 1.1 2007/09/07 14:32:07 drh Exp $
17
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
21# Do not use a codec for tests in this file, as the database file is
22# manipulated directly using tcl scripts (using the [hexio_write] command).
23#
24do_not_use_codec
25
26# We must have the page_size pragma for these tests to work.
27#
28ifcapable !pager_pragmas {
29  finish_test
30  return
31}
32
33# Create a database with a freelist containing at least two pages.
34#
35do_test corrupt4-1.1 {
36  set bigstring [string repeat 0123456789 200]
37  execsql {
38    PRAGMA auto_vacuum=OFF;
39    PRAGMA page_size=1024;
40    CREATE TABLE t1(x);
41    INSERT INTO t1 VALUES($bigstring);
42    CREATE TABLE t2(y);
43    INSERT INTO t2 VALUES(1);
44    DROP TABLE t1;
45  }
46  file size test.db
47} [expr {1024*4}]
48
49# Verify that there are two pages on the freelist.
50#
51do_test corrupt4-1.2 {
52  execsql {PRAGMA freelist_count}
53} {2}
54
55# Get the page number for the trunk of the freelist.
56#
57set trunkpgno [hexio_get_int [hexio_read test.db 32 4]]
58set baseaddr [expr {($trunkpgno-1)*1024}]
59
60# Verify that the trunk of the freelist has exactly one
61# leaf.
62#
63do_test corrupt4-1.3 {
64  hexio_get_int [hexio_read test.db [expr {$::baseaddr+4}] 4]
65} {1}
66
67# Insert a negative number as the number of leaves on the trunk.
68# Then try to add a new element to the freelist.
69#
70do_test corrupt4-1.4 {
71  hexio_write test.db [expr {$::baseaddr+4}] [hexio_render_int32 -100000000]
72  db close
73  sqlite3 db test.db
74  catchsql {
75    DROP TABLE t2
76  }
77} {1 {database disk image is malformed}}
78
79finish_test
80