xref: /sqlite-3.40.0/test/corruptJ.test (revision af3906a7)
1# 2015-03-30
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#
12# Corruption consisting of a database page that thinks it is a child
13# of itself.
14#
15
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18set testprefix corruptJ
19
20if {[permutation]=="mmap"} {
21  finish_test
22  return
23}
24
25# This module uses hard-coded offsets which do not work if the reserved_bytes
26# value is nonzero.
27if {[nonzero_reserved_bytes]} {finish_test; return;}
28
29database_may_be_corrupt
30
31# Initialize the database.
32#
33do_execsql_test 1.1 {
34  PRAGMA page_size=1024;
35  PRAGMA auto_vacuum=0;
36  CREATE TABLE t1(a,b);
37  WITH RECURSIVE c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<10)
38    INSERT INTO t1(a,b) SELECT i, zeroblob(700) FROM c;
39} {}
40db close
41
42# Corrupt the root page of the t1 table such that the left-child pointer
43# for the very first cell points back to the root.  Then try to DROP the
44# table.  The clearDatabasePage() routine should not loop.
45#
46do_test 1.2 {
47  hexio_write test.db [expr {2*1024-2}] 02
48  sqlite3 db test.db
49  catchsql { DROP TABLE t1 }
50} {1 {database disk image is malformed}}
51
52# Similar test using a WITHOUT ROWID table
53#
54do_test 2.1 {
55  db close
56  forcedelete test.db
57  sqlite3 db test.db
58  db eval {
59    PRAGMA page_size=1024;
60    PRAGMA auto_vacuum=0;
61    CREATE TABLE t1(a,b,PRIMARY KEY(a,b)) WITHOUT ROWID;
62    WITH RECURSIVE c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<100)
63      INSERT INTO t1(a,b) SELECT i, zeroblob(200) FROM c;
64  }
65} {}
66
67# The table is three levels deep.  Corrupt the left child of an intermediate
68# page so that it points back to the root page.
69#
70do_test 2.2 {
71  db close
72  hexio_read test.db [expr {9*1024+391}] 8
73} {00000008814D0401}
74do_test 2.2b {
75  hexio_write test.db [expr {9*1024+391}] 00000002
76  sqlite3 db test.db
77  catchsql { PRAGMA secure_delete=ON; DROP TABLE t1; }
78} {1 {database disk image is malformed}}
79
80finish_test
81