Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/ext/fts5/test/fts5corrupt.test # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / ext / fts5 / test / fts5corrupt.test` 187 lines ยท 6091 bytes Raw Blame History 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 do_catchsql_test 4.3 { 120 DELETE FROM t2 WHERE rowid=3 121 } {1 {database disk image is malformed}} 122 123 #-------------------------------------------------------------------- 124 # Bug 2026-08-19T01:42:02Z 125 # 126 reset_db 127 do_execsql_test 5.0 { 128 PRAGMA writable_schema=ON; 129 CREATE VIRTUAL TABLE t1 USING fts5(x); 130 INSERT INTO t1(t1, rank) VALUES('pgsz', 32); 131 WITH RECURSIVE c(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM c WHERE i<200) 132 INSERT INTO t1(rowid, x) 133 SELECT i, CASE WHEN i%10=0 OR i=1 THEN 'a b c x' ELSE 'a b c' END FROM c; 134 UPDATE t1_data SET block=x'01'||substr(block,2) WHERE id=`206158430209`; 135 INSERT INTO t1_data(id, block) 136 SELECT `206158430209` + i*`2147483648`, if(i=32,x'000101',x'010101') 137 FROM (WITH RECURSIVE r(i) AS ( 138 SELECT 1 UNION ALL SELECT i+1 FROM r WHERE i<32 139 ) SELECT i FROM r 140 ); 141 } {} 142 do_catchsql_test 5.1 { 143 SELECT rowid FROM t1 WHERE t1 MATCH 'a AND x'; 144 } {1 {database disk image is malformed}} 145 reset_db 146 do_execsql_test 5.2 { 147 PRAGMA writable_schema=ON; 148 CREATE VIRTUAL TABLE t1 USING fts5(x); 149 INSERT INTO t1(t1, rank) VALUES('pgsz', 32); 150 WITH RECURSIVE c(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM c WHERE i<200) 151 INSERT INTO t1(rowid, x) 152 SELECT i, CASE WHEN i%10=0 OR i=1 THEN 'a b c x' ELSE 'a b c' END FROM c; 153 UPDATE t1_data SET block=x'01'||substr(block,2) WHERE id=`206158430209`; 154 INSERT INTO t1_data(id, block) 155 SELECT `206158430209` + i*`2147483648`, if(i=31,x'000101',x'010101') 156 FROM (WITH RECURSIVE r(i) AS ( 157 SELECT 1 UNION ALL SELECT i+1 FROM r WHERE i<31 158 ) SELECT i FROM r 159 ); 160 } {} 161 do_catchsql_test 5.3 { 162 SELECT rowid FROM t1 WHERE t1 MATCH 'a AND x'; 163 } {0 {1 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200}} 164 165 reset_db 166 sqlite3_fts5_may_be_corrupt 1 167 do_execsql_test 6.0 { 168 CREATE VIRTUAL TABLE t USING fts5(x); 169 INSERT INTO t(t,rank) VALUES('pgsz',32); 170 WITH r(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM r WHERE i<3000) 171 INSERT INTO t(rowid,x) SELECT i, 'a b c d' FROM r; 172 INSERT INTO t(t) VALUES('optimize'); 173 PRAGMA writable_schema=ON; 174 UPDATE t_data SET block=x'' WHERE id=`206158430209`; 175 PRAGMA writable_schema=RESET; 176 } 177 do_execsql_test 6.1 { 178 SELECT count(*) FROM t WHERE t MATCH 'a' AND rowid>2500; 179 } {500} 180 do_catchsql_test 6.2 { 181 SELECT rowid FROM t WHERE t MATCH 'a' ORDER BY rowid DESC LIMIT 1; 182 } {1 {fts5: corruption found reading blob `137438953472` from table "t"}} 183 do_catchsql_test 6.3 { 184 INSERT INTO t(t) VALUES('integrity-check'); 185 } {1 {fts5: corruption found reading blob `137438953472` from table "t"}} 186 187 finish_test