Snapshot of upstream SQLite 3.40.1
[sqlcipher.git] / ext / fts5 / test / fts5corrupt.test
blob5f13513ec724dc250922a60b736a503e6077ec11
1 # 2014 Dec 20
3 # The author disclaims copyright to this source code.  In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
12 # This file tests that the FTS5 'integrity-check' command detects 
13 # inconsistencies (corruption) in the on-disk backing tables.
16 source [file join [file dirname [info script]] fts5_common.tcl]
17 set testprefix fts5corrupt
19 # If SQLITE_ENABLE_FTS5 is defined, omit this file.
20 ifcapable !fts5 {
21   finish_test
22   return
25 do_execsql_test 1.0 {
26   CREATE VIRTUAL TABLE t1 USING fts5(x);
27   INSERT INTO t1(t1, rank) VALUES('pgsz', 32);
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
41 do_execsql_test 1.2 { INSERT INTO t1(t1) VALUES('integrity-check') }
42 set segid [lindex [fts5_level_segids t1] 0]
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 {database disk image is malformed}}
52 do_test 1.4 {
53   db_restore_and_reopen
54   sqlite3_db_config db DEFENSIVE 0
55   execsql {
56     UPDATE t1_data set block = X'00000000' || substr(block, 5) WHERE
57     rowid = fts5_rowid('segment', $segid, 4);
58   }
59   catchsql { INSERT INTO t1(t1) VALUES('integrity-check') }
60 } {1 {database disk image is malformed}}
62 db_restore_and_reopen
63 #db eval {SELECT rowid, fts5_decode(rowid, block) aS r FROM t1_data} {puts $r}
66 #--------------------------------------------------------------------
68 do_execsql_test 2.0 {
69   CREATE VIRTUAL TABLE t2 USING fts5(x);
70   INSERT INTO t2(t2, rank) VALUES('pgsz', 64);
72 db func rnddoc fts5_rnddoc
73 do_test 2.1 {
74   for {set i 0} {$i < 500} {incr i} {
75     execsql { INSERT INTO t2 VALUES(rnddoc(50)) }
76   }
77   execsql { INSERT INTO t2(t2) VALUES('integrity-check') }
78 } {}
80 #--------------------------------------------------------------------
81 # A mundane test - missing row in the %_content table.
83 do_execsql_test 3.0 {
84   CREATE VIRTUAL TABLE t3 USING fts5(x);
85   INSERT INTO t3 VALUES('one o');
86   INSERT INTO t3 VALUES('two e');
87   INSERT INTO t3 VALUES('three o');
88   INSERT INTO t3 VALUES('four e');
89   INSERT INTO t3 VALUES('five o');
91 do_execsql_test 3.1 {
92   SELECT * FROM t3 WHERE t3 MATCH 'o'
93 } {{one o} {three o} {five o}}
94 sqlite3_db_config db DEFENSIVE 0
95 do_catchsql_test 3.1 {
96   DELETE FROM t3_content WHERE rowid = 3;
97   SELECT * FROM t3 WHERE t3 MATCH 'o';
98 } {1 {database disk image is malformed}}
100 finish_test