xref: /sqlite-3.40.0/test/corrupt8.test (revision 09fe6143)
11013148bSdrh# 2008 July 9
2fa67c3c5Sdrh#
3fa67c3c5Sdrh# The author disclaims copyright to this source code.  In place of
4fa67c3c5Sdrh# a legal notice, here is a blessing:
5fa67c3c5Sdrh#
6fa67c3c5Sdrh#    May you do good and not evil.
7fa67c3c5Sdrh#    May you find forgiveness for yourself and forgive others.
8fa67c3c5Sdrh#    May you share freely, never taking more than you give.
9fa67c3c5Sdrh#
10fa67c3c5Sdrh#***********************************************************************
11fa67c3c5Sdrh# This file implements regression tests for SQLite library.
12fa67c3c5Sdrh#
13fa67c3c5Sdrh# This file implements tests to make sure SQLite does not crash or
14fa67c3c5Sdrh# segfault if it sees a corrupt database file.  It specifically focuses
15fa67c3c5Sdrh# on corrupt pointer map pages.
16fa67c3c5Sdrh#
171013148bSdrh# $Id: corrupt8.test,v 1.2 2008/07/11 03:34:10 drh Exp $
18fa67c3c5Sdrh
19fa67c3c5Sdrhset testdir [file dirname $argv0]
20fa67c3c5Sdrhsource $testdir/tester.tcl
21fa67c3c5Sdrh
2268928b6cSdan# Do not use a codec for tests in this file, as the database file is
2368928b6cSdan# manipulated directly using tcl scripts (using the [hexio_write] command).
2468928b6cSdan#
2568928b6cSdando_not_use_codec
2668928b6cSdan
27*09fe6143Sdrh# These tests deal with corrupt database files
28*09fe6143Sdrh#
29*09fe6143Sdrhdatabase_may_be_corrupt
30*09fe6143Sdrh
31fa67c3c5Sdrh# We must have the page_size pragma for these tests to work.
32fa67c3c5Sdrh#
33fa67c3c5Sdrhifcapable !pager_pragmas||!autovacuum {
34fa67c3c5Sdrh  finish_test
35fa67c3c5Sdrh  return
36fa67c3c5Sdrh}
37fa67c3c5Sdrh
38fa67c3c5Sdrh# Create a database to work with.
39fa67c3c5Sdrh#
40fa67c3c5Sdrhdo_test corrupt8-1.1 {
41fa67c3c5Sdrh  execsql {
42fa67c3c5Sdrh    PRAGMA auto_vacuum=1;
43fa67c3c5Sdrh    PRAGMA page_size=1024;
44fa67c3c5Sdrh    CREATE TABLE t1(x);
45fa67c3c5Sdrh    INSERT INTO t1(x) VALUES(1);
46fa67c3c5Sdrh    INSERT INTO t1(x) VALUES(2);
47fa67c3c5Sdrh    INSERT INTO t1(x) SELECT x+2 FROM t1;
48fa67c3c5Sdrh    INSERT INTO t1(x) SELECT x+4 FROM t1;
49fa67c3c5Sdrh    INSERT INTO t1(x) SELECT x+8 FROM t1;
50fa67c3c5Sdrh    INSERT INTO t1(x) SELECT x+16 FROM t1;
51fa67c3c5Sdrh    INSERT INTO t1(x) SELECT x+32 FROM t1;
52fa67c3c5Sdrh    INSERT INTO t1(x) SELECT x+64 FROM t1;
53fa67c3c5Sdrh    INSERT INTO t1(x) SELECT x+128 FROM t1;
54fa67c3c5Sdrh    INSERT INTO t1(x) SELECT x+256 FROM t1;
55fa67c3c5Sdrh    CREATE TABLE t2(a,b);
56fa67c3c5Sdrh    INSERT INTO t2 SELECT x, x*x FROM t1;
57fa67c3c5Sdrh  }
58fa67c3c5Sdrh  expr {[file size test.db]>1024*12}
59fa67c3c5Sdrh} {1}
60fa67c3c5Sdrhintegrity_check corrupt8-1.2
61fa67c3c5Sdrh
62fa67c3c5Sdrh# Loop through each ptrmap entry.  Corrupt the entry and make sure the
63fa67c3c5Sdrh# corruption is detected by the integrity_check.
64fa67c3c5Sdrh#
65fa67c3c5Sdrhfor {set i 1024} {$i<2048} {incr i 5} {
66fa67c3c5Sdrh  set oldval [hexio_read test.db $i 1]
67fa67c3c5Sdrh  if {$oldval==0} break
68fa67c3c5Sdrh  hexio_write test.db $i 00
69fa67c3c5Sdrh  do_test corrupt8-2.$i.0 {
70fa67c3c5Sdrh    db close
71fa67c3c5Sdrh    sqlite3 db test.db
72fa67c3c5Sdrh    set x [db eval {PRAGMA integrity_check}]
73fa67c3c5Sdrh    expr {$x!="ok"}
74fa67c3c5Sdrh  } {1}
75fa67c3c5Sdrh  for {set k 1} {$k<=5} {incr k} {
76fa67c3c5Sdrh    if {$k==$oldval} continue
77fa67c3c5Sdrh    hexio_write test.db $i 0$k
78fa67c3c5Sdrh    do_test corrupt8-2.$i.$k {
79fa67c3c5Sdrh      db close
80fa67c3c5Sdrh      sqlite3 db test.db
81fa67c3c5Sdrh      set x [db eval {PRAGMA integrity_check}]
82fa67c3c5Sdrh      expr {$x!="ok"}
83fa67c3c5Sdrh    } {1}
84fa67c3c5Sdrh  }
85fa67c3c5Sdrh  hexio_write test.db $i 06
86fa67c3c5Sdrh  do_test corrupt8-2.$i.6 {
87fa67c3c5Sdrh    db close
88fa67c3c5Sdrh    sqlite3 db test.db
89fa67c3c5Sdrh    set x [db eval {PRAGMA integrity_check}]
90fa67c3c5Sdrh    expr {$x!="ok"}
91fa67c3c5Sdrh  } {1}
92fa67c3c5Sdrh  hexio_write test.db $i $oldval
93fa67c3c5Sdrh  if {$oldval>2} {
94fa67c3c5Sdrh    set i2 [expr {$i+1+$i%4}]
95fa67c3c5Sdrh    set oldval [hexio_read test.db $i2 1]
96fa67c3c5Sdrh    hexio_write test.db $i2 [format %02x [expr {($oldval+1)&0xff}]]
97fa67c3c5Sdrh    do_test corrupt8-2.$i.7 {
98fa67c3c5Sdrh      db close
99fa67c3c5Sdrh      sqlite3 db test.db
100fa67c3c5Sdrh      set x [db eval {PRAGMA integrity_check}]
101fa67c3c5Sdrh      expr {$x!="ok"}
102fa67c3c5Sdrh    } {1}
103fa67c3c5Sdrh    hexio_write test.db $i2 $oldval
104fa67c3c5Sdrh  }
105fa67c3c5Sdrh}
106fa67c3c5Sdrh
107fa67c3c5Sdrh
108fa67c3c5Sdrhfinish_test
109