rtreecheck.test 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # 2017 August 17
  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. #
  13. if {![info exists testdir]} {
  14. set testdir [file join [file dirname [info script]] .. .. test]
  15. }
  16. source $testdir/tester.tcl
  17. set testprefix rtreecheck
  18. ifcapable !rtree {
  19. finish_test
  20. return
  21. }
  22. proc swap_int32 {blob i0 i1} {
  23. binary scan $blob I* L
  24. set a [lindex $L $i0]
  25. set b [lindex $L $i1]
  26. lset L $i0 $b
  27. lset L $i1 $a
  28. binary format I* $L
  29. }
  30. proc set_int32 {blob idx val} {
  31. binary scan $blob I* L
  32. lset L $idx $val
  33. binary format I* $L
  34. }
  35. do_catchsql_test 1.0 {
  36. SELECT rtreecheck();
  37. } {1 {wrong number of arguments to function rtreecheck()}}
  38. do_catchsql_test 1.1 {
  39. SELECT rtreecheck(0,0,0);
  40. } {1 {wrong number of arguments to function rtreecheck()}}
  41. proc setup_simple_db {{module rtree}} {
  42. reset_db
  43. db func swap_int32 swap_int32
  44. execsql "
  45. CREATE VIRTUAL TABLE r1 USING $module (id, x1, x2, y1, y2);
  46. INSERT INTO r1 VALUES(1, 5, 5, 5, 5); -- 3
  47. INSERT INTO r1 VALUES(2, 6, 6, 6, 6); -- 9
  48. INSERT INTO r1 VALUES(3, 7, 7, 7, 7); -- 15
  49. INSERT INTO r1 VALUES(4, 8, 8, 8, 8); -- 21
  50. INSERT INTO r1 VALUES(5, 9, 9, 9, 9); -- 27
  51. "
  52. sqlite3_db_config db DEFENSIVE 0
  53. }
  54. setup_simple_db
  55. do_execsql_test 2.1 {
  56. SELECT rtreecheck('r1')
  57. } {ok}
  58. do_execsql_test 2.2 {
  59. UPDATE r1_node SET data = swap_int32(data, 3, 9);
  60. UPDATE r1_node SET data = swap_int32(data, 23, 29);
  61. }
  62. do_execsql_test 2.3 {
  63. SELECT rtreecheck('r1')
  64. } {{Dimension 0 of cell 0 on node 1 is corrupt
  65. Dimension 1 of cell 3 on node 1 is corrupt}}
  66. do_execsql_test 2.3b {
  67. PRAGMA integrity_check;
  68. } {{In RTree main.r1:
  69. Dimension 0 of cell 0 on node 1 is corrupt
  70. Dimension 1 of cell 3 on node 1 is corrupt}}
  71. setup_simple_db
  72. do_execsql_test 2.4 {
  73. DELETE FROM r1_rowid WHERE rowid = 3;
  74. SELECT rtreecheck('r1')
  75. } {{Mapping (3 -> 1) missing from %_rowid table
  76. Wrong number of entries in %_rowid table - expected 5, actual 4}}
  77. do_execsql_test 2.4b {
  78. PRAGMA integrity_check
  79. } {{In RTree main.r1:
  80. Mapping (3 -> 1) missing from %_rowid table
  81. Wrong number of entries in %_rowid table - expected 5, actual 4}}
  82. setup_simple_db
  83. do_execsql_test 2.5 {
  84. UPDATE r1_rowid SET nodeno=2 WHERE rowid=3;
  85. SELECT rtreecheck('r1')
  86. } {{Found (3 -> 2) in %_rowid table, expected (3 -> 1)}}
  87. do_execsql_test 2.5b {
  88. PRAGMA integrity_check
  89. } {{In RTree main.r1:
  90. Found (3 -> 2) in %_rowid table, expected (3 -> 1)}}
  91. reset_db
  92. do_execsql_test 3.0 {
  93. CREATE VIRTUAL TABLE r1 USING rtree_i32(id, x1, x2);
  94. INSERT INTO r1 VALUES(1, 0x7FFFFFFF*-1, 0x7FFFFFFF);
  95. INSERT INTO r1 VALUES(2, 0x7FFFFFFF*-1, 5);
  96. INSERT INTO r1 VALUES(3, -5, 5);
  97. INSERT INTO r1 VALUES(4, 5, 0x11111111);
  98. INSERT INTO r1 VALUES(5, 5, 0x00800000);
  99. INSERT INTO r1 VALUES(6, 5, 0x00008000);
  100. INSERT INTO r1 VALUES(7, 5, 0x00000080);
  101. INSERT INTO r1 VALUES(8, 5, 0x40490fdb);
  102. INSERT INTO r1 VALUES(9, 0x7f800000, 0x7f900000);
  103. SELECT rtreecheck('r1');
  104. PRAGMA integrity_check;
  105. } {ok ok}
  106. do_execsql_test 3.1 {
  107. CREATE VIRTUAL TABLE r2 USING rtree_i32(id, x1, x2);
  108. INSERT INTO r2 VALUES(2, -1*(1<<31), -1*(1<<31)+5);
  109. SELECT rtreecheck('r2');
  110. PRAGMA integrity_check;
  111. } {ok ok}
  112. sqlite3_db_config db DEFENSIVE 0
  113. do_execsql_test 3.2 {
  114. BEGIN;
  115. UPDATE r2_node SET data = X'123456';
  116. SELECT rtreecheck('r2')!='ok';
  117. } {1}
  118. do_execsql_test 3.3 {
  119. ROLLBACK;
  120. UPDATE r2_node SET data = X'00001234';
  121. SELECT rtreecheck('r2')!='ok';
  122. } {1}
  123. do_execsql_test 3.4 {
  124. PRAGMA integrity_check;
  125. } {{In RTree main.r2:
  126. Node 1 is too small for cell count of 4660 (4 bytes)
  127. Wrong number of entries in %_rowid table - expected 0, actual 1}}
  128. do_execsql_test 4.0 {
  129. CREATE TABLE notanrtree(i);
  130. SELECT rtreecheck('notanrtree');
  131. } {{Schema corrupt or not an rtree}}
  132. #-------------------------------------------------------------------------
  133. #
  134. reset_db
  135. db func set_int32 set_int32
  136. do_execsql_test 5.0 {
  137. CREATE VIRTUAL TABLE r3 USING rtree_i32(id, x1, x2, y1, y2);
  138. WITH x(i) AS (
  139. SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<1000
  140. )
  141. INSERT INTO r3 SELECT i, i, i, i, i FROM x;
  142. }
  143. sqlite3_db_config db DEFENSIVE 0
  144. do_execsql_test 5.1 {
  145. BEGIN;
  146. UPDATE r3_node SET data = set_int32(data, 3, 5000);
  147. UPDATE r3_node SET data = set_int32(data, 4, 5000);
  148. SELECT rtreecheck('r3')=='ok'
  149. } 0
  150. do_execsql_test 5.2 {
  151. ROLLBACK;
  152. BEGIN;
  153. UPDATE r3_node SET data = set_int32(data, 3, 0);
  154. UPDATE r3_node SET data = set_int32(data, 4, 0);
  155. SELECT rtreecheck('r3')=='ok'
  156. } 0
  157. #-------------------------------------------------------------------------
  158. # dbsqlfuzz 4a1399d39bf9feccbf6b290da51d3b30103a4bf6
  159. #
  160. reset_db
  161. do_execsql_test 6.0 {
  162. PRAGMA encoding = 'utf16';
  163. CREATE VIRTUAL TABLE t1 USING rtree(id, x, y);
  164. }
  165. db close
  166. sqlite3 db test.db
  167. if {[permutation]=="inmemory_journal"} {
  168. # This doesn't hit an SQLITE_LOCKED in this permutation as the schema
  169. # has already been loaded.
  170. do_catchsql_test 6.1.inmemory_journal {
  171. SELECT ( 'elvis' IN(SELECT rtreecheck('t1')) ) FROM (SELECT 1) GROUP BY 1;
  172. } {0 0}
  173. } else {
  174. do_catchsql_test 6.1 {
  175. SELECT ( 'elvis' IN(SELECT rtreecheck('t1')) ) FROM (SELECT 1) GROUP BY 1;
  176. } {0 0}
  177. }
  178. finish_test