| 1 |
# 2014 Dec 20 |
| 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 |
# This file tests that the FTS5 'integrity-check' command detects |
| 13 |
# inconsistencies (corruption) in the on-disk backing tables. |
| 14 |
# |
| 15 |
|
| 16 |
source [file join [file dirname [info script]] fts5_common.tcl] |
| 17 |
set testprefix fts5corrupt |
| 18 |
|
| 19 |
# If SQLITE_ENABLE_FTS5 is not defined, omit this file. |
| 20 |
ifcapable !fts5 { |
| 21 |
finish_test |
| 22 |
return |
| 23 |
} |
| 24 |
|
| 25 |
do_execsql_test 1.0 { |
| 26 |
CREATE VIRTUAL TABLE t1 USING fts5(x); |
| 27 |
INSERT INTO t1(t1, rank) VALUES('pgsz', 32); |
| 28 |
} |
| 29 |
|
| 30 |
do_test 1.1 { |
| 31 |
db transaction { |
| 32 |
for {set i 1} {$i < 200} {incr i} { |
| 33 |
set doc [list [string repeat x $i] [string repeat y $i]] |
| 34 |
execsql { INSERT INTO t1(rowid, x) VALUES($i, $doc) } |
| 35 |
} |
| 36 |
} |
| 37 |
fts5_level_segs t1 |
| 38 |
} {1} |
| 39 |
db_save |
| 40 |
|
| 41 |
do_execsql_test 1.2 { INSERT INTO t1(t1) VALUES('integrity-check') } |
| 42 |
set segid [lindex [fts5_level_segids t1] 0] |
| 43 |
|
| 44 |
sqlite3_db_config db DEFENSIVE 0 |
| 45 |
do_test 1.3 { |
| 46 |
execsql { |
| 47 |
DELETE FROM t1_data WHERE rowid = fts5_rowid('segment', $segid, 4); |
| 48 |
} |
| 49 |
catchsql { INSERT INTO t1(t1) VALUES('integrity-check') } |
| 50 |
} {1 {fts5: corruption found reading blob 137438953476 from table "t1"}} |
| 51 |
do_execsql_test 1.3b { |
| 52 |
PRAGMA integrity_check(t1); |
| 53 |
} {{fts5: corruption found reading blob 137438953476 from table "t1"}} |
| 54 |
|
| 55 |
do_test 1.4 { |
| 56 |
db_restore_and_reopen |
| 57 |
sqlite3_db_config db DEFENSIVE 0 |
| 58 |
execsql { |
| 59 |
UPDATE t1_data set block = X'00000000' || substr(block, 5) WHERE |
| 60 |
rowid = fts5_rowid('segment', $segid, 4); |
| 61 |
} |
| 62 |
catchsql { INSERT INTO t1(t1) VALUES('integrity-check') } |
| 63 |
} {1 {fts5: corruption found reading blob 137438953476 from table "t1"}} |
| 64 |
|
| 65 |
db_restore_and_reopen |
| 66 |
#db eval {SELECT rowid, fts5_decode(rowid, block) aS r FROM t1_data} {puts $r} |
| 67 |
|
| 68 |
|
| 69 |
#-------------------------------------------------------------------- |
| 70 |
# |
| 71 |
do_execsql_test 2.0 { |
| 72 |
CREATE VIRTUAL TABLE t2 USING fts5(x); |
| 73 |
INSERT INTO t2(t2, rank) VALUES('pgsz', 64); |
| 74 |
} |
| 75 |
db func rnddoc fts5_rnddoc |
| 76 |
do_test 2.1 { |
| 77 |
for {set i 0} {$i < 500} {incr i} { |
| 78 |
execsql { INSERT INTO t2 VALUES(rnddoc(50)) } |
| 79 |
} |
| 80 |
execsql { INSERT INTO t2(t2) VALUES('integrity-check') } |
| 81 |
} {} |
| 82 |
|
| 83 |
#-------------------------------------------------------------------- |
| 84 |
# A mundane test - missing row in the %_content table. |
| 85 |
# |
| 86 |
do_execsql_test 3.0 { |
| 87 |
CREATE VIRTUAL TABLE t3 USING fts5(x); |
| 88 |
INSERT INTO t3 VALUES('one o'); |
| 89 |
INSERT INTO t3 VALUES('two e'); |
| 90 |
INSERT INTO t3 VALUES('three o'); |
| 91 |
INSERT INTO t3 VALUES('four e'); |
| 92 |
INSERT INTO t3 VALUES('five o'); |
| 93 |
} |
| 94 |
do_execsql_test 3.1 { |
| 95 |
SELECT * FROM t3 WHERE t3 MATCH 'o' |
| 96 |
} {{one o} {three o} {five o}} |
| 97 |
sqlite3_db_config db DEFENSIVE 0 |
| 98 |
do_catchsql_test 3.1 { |
| 99 |
DELETE FROM t3_content WHERE rowid = 3; |
| 100 |
SELECT * FROM t3 WHERE t3 MATCH 'o'; |
| 101 |
} {1 {fts5: missing row 3 from content table 'main'.'t3_content'}} |
| 102 |
|
| 103 |
#-------------------------------------------------------------------- |
| 104 |
# |
| 105 |
reset_db |
| 106 |
do_execsql_test 4.0 { |
| 107 |
CREATE VIRTUAL TABLE t2 USING fts5(x); |
| 108 |
INSERT INTO t2 VALUES('one two three'); |
| 109 |
INSERT INTO t2 VALUES('four five six'); |
| 110 |
INSERT INTO t2 VALUES('seven eight nine'); |
| 111 |
INSERT INTO t2 VALUES('ten eleven twelve'); |
| 112 |
} |
| 113 |
do_execsql_test 4.1 { |
| 114 |
SELECT hex(block) FROM t2_data WHERE id=1; |
| 115 |
} {040C} |
| 116 |
do_execsql_test 4.2 { |
| 117 |
UPDATE t2_data SET block = X'0402' WHERE id=1 |
| 118 |
} |
| 119 |
breakpoint |
| 120 |
do_catchsql_test 4.3 { |
| 121 |
DELETE FROM t2 WHERE rowid=3 |
| 122 |
} {1 {database disk image is malformed}} |
| 123 |
|
| 124 |
finish_test |