xref: /sqlite-3.40.0/test/corrupt8.test (revision f2fcd075)
1# 2008 July 9
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.  It specifically focuses
15# on corrupt pointer map pages.
16#
17# $Id: corrupt8.test,v 1.2 2008/07/11 03:34:10 drh Exp $
18
19set testdir [file dirname $argv0]
20source $testdir/tester.tcl
21
22# Do not use a codec for tests in this file, as the database file is
23# manipulated directly using tcl scripts (using the [hexio_write] command).
24#
25do_not_use_codec
26
27# We must have the page_size pragma for these tests to work.
28#
29ifcapable !pager_pragmas||!autovacuum {
30  finish_test
31  return
32}
33
34# Create a database to work with.
35#
36do_test corrupt8-1.1 {
37  execsql {
38    PRAGMA auto_vacuum=1;
39    PRAGMA page_size=1024;
40    CREATE TABLE t1(x);
41    INSERT INTO t1(x) VALUES(1);
42    INSERT INTO t1(x) VALUES(2);
43    INSERT INTO t1(x) SELECT x+2 FROM t1;
44    INSERT INTO t1(x) SELECT x+4 FROM t1;
45    INSERT INTO t1(x) SELECT x+8 FROM t1;
46    INSERT INTO t1(x) SELECT x+16 FROM t1;
47    INSERT INTO t1(x) SELECT x+32 FROM t1;
48    INSERT INTO t1(x) SELECT x+64 FROM t1;
49    INSERT INTO t1(x) SELECT x+128 FROM t1;
50    INSERT INTO t1(x) SELECT x+256 FROM t1;
51    CREATE TABLE t2(a,b);
52    INSERT INTO t2 SELECT x, x*x FROM t1;
53  }
54  expr {[file size test.db]>1024*12}
55} {1}
56integrity_check corrupt8-1.2
57
58# Loop through each ptrmap entry.  Corrupt the entry and make sure the
59# corruption is detected by the integrity_check.
60#
61for {set i 1024} {$i<2048} {incr i 5} {
62  set oldval [hexio_read test.db $i 1]
63  if {$oldval==0} break
64  hexio_write test.db $i 00
65  do_test corrupt8-2.$i.0 {
66    db close
67    sqlite3 db test.db
68    set x [db eval {PRAGMA integrity_check}]
69    expr {$x!="ok"}
70  } {1}
71  for {set k 1} {$k<=5} {incr k} {
72    if {$k==$oldval} continue
73    hexio_write test.db $i 0$k
74    do_test corrupt8-2.$i.$k {
75      db close
76      sqlite3 db test.db
77      set x [db eval {PRAGMA integrity_check}]
78      expr {$x!="ok"}
79    } {1}
80  }
81  hexio_write test.db $i 06
82  do_test corrupt8-2.$i.6 {
83    db close
84    sqlite3 db test.db
85    set x [db eval {PRAGMA integrity_check}]
86    expr {$x!="ok"}
87  } {1}
88  hexio_write test.db $i $oldval
89  if {$oldval>2} {
90    set i2 [expr {$i+1+$i%4}]
91    set oldval [hexio_read test.db $i2 1]
92    hexio_write test.db $i2 [format %02x [expr {($oldval+1)&0xff}]]
93    do_test corrupt8-2.$i.7 {
94      db close
95      sqlite3 db test.db
96      set x [db eval {PRAGMA integrity_check}]
97      expr {$x!="ok"}
98    } {1}
99    hexio_write test.db $i2 $oldval
100  }
101}
102
103
104finish_test
105