fts5corrupt.test 3.5 KB

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