| 1 |
# 2015 Apr 24 |
| 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 FTS5 handles corrupt databases (i.e. internal |
| 13 |
# inconsistencies in the backing tables) correctly. In this case |
| 14 |
# "correctly" means without crashing. |
| 15 |
# |
| 16 |
|
| 17 |
source [file join [file dirname [info script]] fts5_common.tcl] |
| 18 |
set testprefix fts5corrupt2 |
| 19 |
|
| 20 |
# If SQLITE_ENABLE_FTS5 is not defined, omit this file. |
| 21 |
ifcapable !fts5 { |
| 22 |
finish_test |
| 23 |
return |
| 24 |
} |
| 25 |
sqlite3_fts5_may_be_corrupt 1 |
| 26 |
|
| 27 |
# Create a simple FTS5 table containing 100 documents. Each document |
| 28 |
# contains 10 terms, each of which start with the character "x". |
| 29 |
# |
| 30 |
expr srand(0) |
| 31 |
db func rnddoc fts5_rnddoc |
| 32 |
do_execsql_test 1.0 { |
| 33 |
CREATE VIRTUAL TABLE t1 USING fts5(x); |
| 34 |
INSERT INTO t1(t1, rank) VALUES('pgsz', 32); |
| 35 |
WITH ii(i) AS (SELECT 1 UNION SELECT i+1 FROM ii WHERE i<100) |
| 36 |
INSERT INTO t1 SELECT rnddoc(10) FROM ii; |
| 37 |
} |
| 38 |
set mask [expr 31 << 31] |
| 39 |
|
| 40 |
if 0 { |
| 41 |
|
| 42 |
# Test 1: |
| 43 |
# |
| 44 |
# For each page in the t1_data table, open a transaction and DELETE |
| 45 |
# the t1_data entry. Then run: |
| 46 |
# |
| 47 |
# * an integrity-check, and |
| 48 |
# * unless the deleted block was a b-tree node, a query for "t1 MATCH 'x*'" |
| 49 |
# |
| 50 |
# and check that the corruption is detected in both cases. The |
| 51 |
# rollback the transaction. |
| 52 |
# |
| 53 |
# Test 2: |
| 54 |
# |
| 55 |
# Same thing, except instead of deleting a row from t1_data, replace its |
| 56 |
# blob content with integer value 14. |
| 57 |
# |
| 58 |
foreach {tno stmt} { |
| 59 |
1 { DELETE FROM t1_data WHERE rowid=$rowid } |
| 60 |
2 { UPDATE t1_data SET block=14 WHERE rowid=$rowid } |
| 61 |
} { |
| 62 |
set tn 0 |
| 63 |
foreach rowid [db eval {SELECT rowid FROM t1_data WHERE rowid>10}] { |
| 64 |
incr tn |
| 65 |
#if {$tn!=224} continue |
| 66 |
|
| 67 |
do_test 1.$tno.$tn.1.$rowid { |
| 68 |
execsql { BEGIN } |
| 69 |
execsql $stmt |
| 70 |
catchsql { INSERT INTO t1(t1) VALUES('integrity-check') } |
| 71 |
} {1 {database disk image is malformed}} |
| 72 |
|
| 73 |
if {($rowid & $mask)==0} { |
| 74 |
# Node is a leaf node, not a b-tree node. |
| 75 |
do_catchsql_test 1.$tno.$tn.2.$rowid { |
| 76 |
SELECT rowid FROM t1 WHERE t1 MATCH 'x*' |
| 77 |
} {1 {database disk image is malformed}} |
| 78 |
} |
| 79 |
|
| 80 |
do_execsql_test 1.$tno.$tn.3.$rowid { |
| 81 |
ROLLBACK; |
| 82 |
INSERT INTO t1(t1) VALUES('integrity-check'); |
| 83 |
} {} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
# Using the same database as the 1.* tests. |
| 90 |
# |
| 91 |
# Run N-1 tests, where N is the number of bytes in the rightmost leaf page |
| 92 |
# of the fts index. For test $i, truncate the rightmost leafpage to $i |
| 93 |
# bytes. Then test both the integrity-check detects the corruption. |
| 94 |
# |
| 95 |
# Also tested is that "MATCH 'x*'" does not crash and sometimes reports |
| 96 |
# corruption. It may not report the db as corrupt because truncating the |
| 97 |
# final leaf to some sizes may create a valid leaf page. |
| 98 |
# |
| 99 |
set lrowid [db one {SELECT max(rowid) FROM t1_data WHERE (rowid & $mask)=0}] |
| 100 |
set nbyte [db one {SELECT length(block) FROM t1_data WHERE rowid=$lrowid}] |
| 101 |
set all [db eval {SELECT rowid FROM t1}] |
| 102 |
sqlite3_db_config db DEFENSIVE 0 |
| 103 |
unset -nocomplain res |
| 104 |
for {set i [expr $nbyte-2]} {$i>=0} {incr i -1} { |
| 105 |
do_execsql_test 2.$i.1 { |
| 106 |
BEGIN; |
| 107 |
UPDATE t1_data SET block = substr(block, 1, $i) WHERE rowid=$lrowid; |
| 108 |
} |
| 109 |
|
| 110 |
do_catchsql_test 2.$i.2 { |
| 111 |
INSERT INTO t1(t1) VALUES('integrity-check'); |
| 112 |
} {/1.*fts5: corruption.*/} |
| 113 |
|
| 114 |
do_test 2.$i.3 { |
| 115 |
set res [catchsql {SELECT rowid FROM t1 WHERE t1 MATCH 'x*'}] |
| 116 |
expr { |
| 117 |
[string match {*fts5: corruption*} $res] |
| 118 |
|| $res=="0 {$all}" |
| 119 |
} |
| 120 |
} 1 |
| 121 |
|
| 122 |
do_execsql_test 2.$i.4 { |
| 123 |
ROLLBACK; |
| 124 |
INSERT INTO t1(t1) VALUES('integrity-check'); |
| 125 |
} {} |
| 126 |
} |
| 127 |
|
| 128 |
#------------------------------------------------------------------------- |
| 129 |
# Test that corruption in leaf page headers is detected by queries that use |
| 130 |
# doclist-indexes. |
| 131 |
# |
| 132 |
set doc "A B C D E F G H I J " |
| 133 |
do_execsql_test 3.0 { |
| 134 |
CREATE VIRTUAL TABLE x3 USING fts5(tt); |
| 135 |
INSERT INTO x3(x3, rank) VALUES('pgsz', 32); |
| 136 |
WITH ii(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM ii WHERE i<1000) |
| 137 |
INSERT INTO x3 |
| 138 |
SELECT ($doc || CASE WHEN (i%50)==0 THEN 'X' ELSE 'Y' END) FROM ii; |
| 139 |
} |
| 140 |
|
| 141 |
foreach {tn hdr} { |
| 142 |
1 "\x00\x00\x00\x00" |
| 143 |
2 "\xFF\xFF\xFF\xFF" |
| 144 |
3 "\x44\x45" |
| 145 |
} { |
| 146 |
set tn2 0 |
| 147 |
set nCorrupt 0 |
| 148 |
set nCorrupt2 0 |
| 149 |
foreach rowid [db eval {SELECT rowid FROM x3_data WHERE rowid>10}] { |
| 150 |
if {$rowid & $mask} continue |
| 151 |
incr tn2 |
| 152 |
do_test 3.$tn.$tn2.1 { |
| 153 |
execsql BEGIN |
| 154 |
|
| 155 |
set fd [db incrblob main x3_data block $rowid] |
| 156 |
fconfigure $fd -translation binary |
| 157 |
set existing [read $fd [string length $hdr]] |
| 158 |
seek $fd 0 |
| 159 |
puts -nonewline $fd $hdr |
| 160 |
close $fd |
| 161 |
|
| 162 |
set res [catchsql {SELECT rowid FROM x3 WHERE x3 MATCH 'x AND a'}] |
| 163 |
if {[string match {*fts5: corruption*} $res]} {incr nCorrupt} |
| 164 |
set {} 1 |
| 165 |
} {1} |
| 166 |
|
| 167 |
if {($tn2 % 10)==0 && $existing != $hdr} { |
| 168 |
do_test 3.$tn.$tn2.2 { |
| 169 |
catchsql { INSERT INTO x3(x3) VALUES('integrity-check') } |
| 170 |
} {/.*fts5: corruption.*/} |
| 171 |
do_execsql_test 3.$tn.$tn2.3 { |
| 172 |
PRAGMA integrity_check(x3); |
| 173 |
} {/.*fts5: corruption.*/} |
| 174 |
} |
| 175 |
|
| 176 |
execsql ROLLBACK |
| 177 |
} |
| 178 |
|
| 179 |
do_test 3.$tn.x { expr $nCorrupt>0 } 1 |
| 180 |
} |
| 181 |
|
| 182 |
#-------------------------------------------------------------------- |
| 183 |
# |
| 184 |
set doc "A B C D E F G H I J " |
| 185 |
do_execsql_test 4.0 { |
| 186 |
CREATE VIRTUAL TABLE x4 USING fts5(tt); |
| 187 |
INSERT INTO x4(x4, rank) VALUES('pgsz', 32); |
| 188 |
WITH ii(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM ii WHERE i<10) |
| 189 |
INSERT INTO x4 |
| 190 |
SELECT ($doc || CASE WHEN (i%50)==0 THEN 'X' ELSE 'Y' END) FROM ii; |
| 191 |
} |
| 192 |
|
| 193 |
foreach {tn nCut} { |
| 194 |
1 1 |
| 195 |
2 10 |
| 196 |
} { |
| 197 |
set tn2 0 |
| 198 |
set nCorrupt 0 |
| 199 |
foreach rowid [db eval {SELECT rowid FROM x4_data WHERE rowid>10}] { |
| 200 |
if {$rowid & $mask} continue |
| 201 |
incr tn2 |
| 202 |
do_test 4.$tn.$tn2 { |
| 203 |
execsql { |
| 204 |
BEGIN; |
| 205 |
UPDATE x4_data SET block = substr(block, 1, length(block)-$nCut) |
| 206 |
WHERE id = $rowid; |
| 207 |
} |
| 208 |
|
| 209 |
set res [catchsql { |
| 210 |
SELECT rowid FROM x4 WHERE x4 MATCH 'a' ORDER BY 1 DESC |
| 211 |
}] |
| 212 |
if {[string match {*fts5: corruption*} $res]} {incr nCorrupt} |
| 213 |
set {} 1 |
| 214 |
} {1} |
| 215 |
|
| 216 |
execsql ROLLBACK |
| 217 |
} |
| 218 |
|
| 219 |
# do_test 4.$tn.x { expr $nCorrupt>0 } 1 |
| 220 |
} |
| 221 |
|
| 222 |
set doc [string repeat "A B C " 1000] |
| 223 |
do_execsql_test 5.0 { |
| 224 |
CREATE VIRTUAL TABLE x5 USING fts5(tt); |
| 225 |
INSERT INTO x5(x5, rank) VALUES('pgsz', 32); |
| 226 |
WITH ii(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM ii WHERE i<10) |
| 227 |
INSERT INTO x5 SELECT $doc FROM ii; |
| 228 |
} |
| 229 |
|
| 230 |
foreach {tn hdr} { |
| 231 |
1 "\x00\x01" |
| 232 |
} { |
| 233 |
set tn2 0 |
| 234 |
set nCorrupt 0 |
| 235 |
foreach rowid [db eval {SELECT rowid FROM x5_data WHERE rowid>10}] { |
| 236 |
if {$rowid & $mask} continue |
| 237 |
incr tn2 |
| 238 |
do_test 5.$tn.$tn2 { |
| 239 |
execsql BEGIN |
| 240 |
|
| 241 |
set fd [db incrblob main x5_data block $rowid] |
| 242 |
fconfigure $fd -translation binary |
| 243 |
puts -nonewline $fd $hdr |
| 244 |
close $fd |
| 245 |
|
| 246 |
catchsql { INSERT INTO x5(x5) VALUES('integrity-check') } |
| 247 |
set {} {} |
| 248 |
} {} |
| 249 |
|
| 250 |
execsql ROLLBACK |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
#-------------------------------------------------------------------- |
| 255 |
reset_db |
| 256 |
sqlite3_db_config db DEFENSIVE 0 |
| 257 |
do_execsql_test 6.1 { |
| 258 |
CREATE VIRTUAL TABLE x5 USING fts5(tt); |
| 259 |
INSERT INTO x5 VALUES('a'); |
| 260 |
INSERT INTO x5 VALUES('a a'); |
| 261 |
INSERT INTO x5 VALUES('a a a'); |
| 262 |
INSERT INTO x5 VALUES('a a a a'); |
| 263 |
|
| 264 |
UPDATE x5_docsize SET sz = X'' WHERE id=3; |
| 265 |
} |
| 266 |
proc colsize {cmd i} { |
| 267 |
$cmd xColumnSize $i |
| 268 |
} |
| 269 |
sqlite3_fts5_create_function db colsize colsize |
| 270 |
|
| 271 |
do_catchsql_test 6.2 { |
| 272 |
SELECT colsize(x5, 0) FROM x5 WHERE x5 MATCH 'a' |
| 273 |
} {1 SQLITE_CORRUPT_VTAB} |
| 274 |
|
| 275 |
|
| 276 |
sqlite3_fts5_may_be_corrupt 0 |
| 277 |
finish_test |