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/rtree/rtreeA.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 / rtree / rtreeA.test` 291 lines ยท 8128 bytes Raw Blame History 1 # 2010 September 22 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 # This file contains tests for the r-tree module. Specifically, it tests 12 # that corrupt or inconsistent databases do not cause crashes in the r-tree 13 # module. 14 # 15 16 if {![info exists testdir]} { 17 set testdir [file join [file dirname [info script]] .. .. test] 18 } 19 source $testdir/tester.tcl 20 ifcapable !rtree { finish_test ; return } 21 set testprefix rtreeA 22 23 proc create_t1 {} { 24 db close 25 forcedelete test.db 26 sqlite3 db test.db 27 execsql { 28 PRAGMA page_size = 1024; 29 CREATE VIRTUAL TABLE t1 USING rtree(id, x1, x2, y1, y2); 30 } 31 } 32 proc populate_t1 {} { 33 execsql BEGIN 34 for {set i 0} {$i < 500} {incr i} { 35 set x2 [expr $i+5] 36 set y2 [expr $i+5] 37 execsql { INSERT INTO t1 VALUES($i, $i, $x2, $i, $y2) } 38 } 39 execsql COMMIT 40 sqlite3_db_config db DEFENSIVE 0 41 } 42 43 proc truncate_node {nodeno nTrunc} { 44 set blob [db one {SELECT data FROM t1_node WHERE nodeno=$nodeno}] 45 if {$nTrunc<0} {set nTrunc "end-$nTrunc"} 46 set blob [string range $blob 0 $nTrunc] 47 db eval { UPDATE t1_node SET data = $blob WHERE nodeno=$nodeno } 48 } 49 50 proc set_tree_depth {tbl {newvalue ""}} { 51 set blob [db one "SELECT data FROM ${tbl}_node WHERE nodeno=1"] 52 53 if {$newvalue == ""} { 54 binary scan $blob Su oldvalue 55 return $oldvalue 56 } 57 58 set blob [binary format Sua* $newvalue [string range $blob 2 end]] 59 db eval "UPDATE ${tbl}_node SET data = \$blob WHERE nodeno=1" 60 return [set_tree_depth $tbl] 61 } 62 63 proc set_entry_count {tbl nodeno {newvalue ""}} { 64 set blob [db one "SELECT data FROM ${tbl}_node WHERE nodeno=$nodeno"] 65 66 if {$newvalue == ""} { 67 binary scan [string range $blob 2 end] Su oldvalue 68 return $oldvalue 69 } 70 71 set blob [binary format a*Sua* \ 72 [string range $blob 0 1] $newvalue [string range $blob 4 end] 73 ] 74 db eval "UPDATE ${tbl}_node SET data = \$blob WHERE nodeno=$nodeno" 75 return [set_entry_count $tbl $nodeno] 76 } 77 78 79 proc do_corruption_tests {prefix args} { 80 set testarray [lindex $args end] 81 set errormsg {database disk image is malformed} 82 83 foreach {z value} [lrange $args 0 end-1] { 84 set n [string length $z] 85 if {$n>=2 && [string equal -length $n $z "-error"]} { 86 set errormsg $value 87 } 88 } 89 90 foreach {tn sql} $testarray { 91 do_catchsql_test $prefix.$tn $sql [list 1 $errormsg] 92 } 93 } 94 95 #------------------------------------------------------------------------- 96 # Test the libraries response if the %_node table is completely empty 97 # (i.e. the root node is missing), or has been removed from the database 98 # entirely. 99 # 100 create_t1 101 populate_t1 102 do_execsql_test rtreeA-1.0 { 103 DELETE FROM t1_node; 104 } {} 105 106 do_corruption_tests rtreeA-1.1 { 107 1 "SELECT * FROM t1" 108 2 "SELECT * FROM t1 WHERE rowid=5" 109 3 "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)" 110 4 "SELECT * FROM t1 WHERE x1<10 AND x2>12" 111 } 112 113 do_execsql_test rtreeA-1.1.1 { 114 SELECT rtreecheck('main', 't1') 115 } {{Node 1 missing from database 116 Wrong number of entries in %_rowid table - expected 0, actual 500 117 Wrong number of entries in %_parent table - expected 0, actual 25}} 118 119 do_execsql_test rtreeA-1.2.0 { DROP TABLE t1_node } {} 120 do_corruption_tests rtreeA-1.2 -error "database disk image is malformed" { 121 1 "SELECT * FROM t1" 122 2 "SELECT * FROM t1 WHERE rowid=5" 123 3 "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)" 124 4 "SELECT * FROM t1 WHERE x1<10 AND x2>12" 125 } 126 127 #------------------------------------------------------------------------- 128 # Test the libraries response if some of the entries in the %_node table 129 # are the wrong size. 130 # 131 create_t1 132 populate_t1 133 do_test rtreeA-2.1.0 { 134 set nodes [db eval {select nodeno FROM t1_node}] 135 foreach {a b c} $nodes { truncate_node $c 200 } 136 } {} 137 do_corruption_tests rtreeA-2.1 { 138 1 "SELECT * FROM t1" 139 2 "SELECT * FROM t1 WHERE rowid=5" 140 3 "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)" 141 4 "SELECT * FROM t1 WHERE x1<10 AND x2>12" 142 } 143 144 create_t1 145 populate_t1 146 do_test rtreeA-2.2.0 { truncate_node 1 200 } {} 147 do_corruption_tests rtreeA-2.2 { 148 1 "SELECT * FROM t1" 149 2 "SELECT * FROM t1 WHERE +rowid=5" 150 3 "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)" 151 4 "SELECT * FROM t1 WHERE x1<10 AND x2>12" 152 } 153 154 #------------------------------------------------------------------------- 155 # Set the "depth" of the tree stored on the root node incorrectly. Test 156 # that this does not cause any problems. 157 # 158 create_t1 159 populate_t1 160 do_test rtreeA-3.1.0.1 { set_tree_depth t1 } {1} 161 do_test rtreeA-3.1.0.2 { set_tree_depth t1 3 } {3} 162 do_corruption_tests rtreeA-3.1 { 163 1 "SELECT * FROM t1" 164 2 "SELECT * FROM t1 WHERE +rowid=5" 165 3 "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)" 166 } 167 168 do_execsql_test rtreeA-3.1.0.3 { 169 SELECT rtreecheck('main', 't1')!='ok' 170 } {1} 171 172 do_test rtreeA-3.2.0 { set_tree_depth t1 1000 } {1000} 173 do_corruption_tests rtreeA-3.2 { 174 1 "SELECT * FROM t1" 175 2 "SELECT * FROM t1 WHERE +rowid=5" 176 3 "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)" 177 } 178 179 create_t1 180 populate_t1 181 do_test rtreeA-3.3.0 { 182 execsql { DELETE FROM t1 WHERE rowid = 0 } 183 set_tree_depth t1 65535 184 } {65535} 185 do_corruption_tests rtreeA-3.3 { 186 1 "SELECT * FROM t1" 187 2 "SELECT * FROM t1 WHERE +rowid=5" 188 3 "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)" 189 } 190 191 do_execsql_test rtreeA-3.3.3.4 { 192 SELECT rtreecheck('main', 't1') 193 } {{Rtree depth out of range (65535) 194 Wrong number of entries in %_rowid table - expected 0, actual 499 195 Wrong number of entries in %_parent table - expected 0, actual 25}} 196 197 #------------------------------------------------------------------------- 198 # Set the "number of entries" field on some nodes incorrectly. 199 # 200 create_t1 201 populate_t1 202 do_test rtreeA-4.1.0 { 203 set_entry_count t1 1 4000 204 } {4000} 205 do_corruption_tests rtreeA-4.1 { 206 1 "SELECT * FROM t1" 207 2 "SELECT * FROM t1 WHERE +rowid=5" 208 3 "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)" 209 4 "SELECT * FROM t1 WHERE x1<10 AND x2>12" 210 } 211 212 #------------------------------------------------------------------------- 213 # Remove entries from the %_parent table and check that this does not 214 # cause a crash. 215 # 216 create_t1 217 populate_t1 218 do_execsql_test rtreeA-5.1.0 { DELETE FROM t1_parent } {} 219 do_corruption_tests rtreeA-5.1 { 220 1 "DELETE FROM t1 WHERE +rowid = 5" 221 2 "DELETE FROM t1" 222 } 223 224 do_execsql_test rtreeA-5.2 { 225 SELECT rtreecheck('main', 't1')!='ok' 226 } {1} 227 228 #------------------------------------------------------------------------- 229 # Add some bad entries to the %_parent table. 230 # 231 create_t1 232 populate_t1 233 do_execsql_test rtreeA-6.1.0 { 234 UPDATE t1_parent set parentnode = parentnode+1 235 } {} 236 do_corruption_tests rtreeA-6.1 { 237 1 "DELETE FROM t1 WHERE rowid = 5" 238 2 "UPDATE t1 SET x1=x1+1, x2=x2+1" 239 } 240 241 do_execsql_test rtreeA-6.2 { 242 SELECT rtreecheck('main', 't1')!='ok' 243 } {1} 244 245 #------------------------------------------------------------------------- 246 # Truncated blobs in the _node table. 247 # 248 create_t1 249 populate_t1 250 sqlite3 db test.db 251 sqlite3_db_config db DEFENSIVE 0 252 do_execsql_test rtreeA-7.100 { 253 UPDATE t1_node SET data=x'' WHERE rowid=1; 254 } {} 255 do_catchsql_test rtreeA-7.110 { 256 SELECT * FROM t1 WHERE x1>0 AND x1<100 AND x2>0 AND x2<100; 257 } {1 {undersize RTree blobs in "t1_node"}} 258 do_test rtreeA-7.120 { 259 sqlite3_extended_errcode db 260 } {SQLITE_CORRUPT_VTAB} 261 262 #------------------------------------------------------------------------- 263 reset_db 264 265 set N 300 266 set rec [binary format SS 0 $N] 267 for {set ii 1} {$ii<=$N} {incr ii} { 268 set cell [binary format Wff $ii $ii [expr $ii+1]] 269 set rec [binary format a*a* $rec $cell] 270 } 271 272 do_execsql_test 8.1 { 273 CREATE VIRTUAL TABLE rt USING rtree(id, x1, x2); 274 INSERT INTO rt VALUES(1, 0.0, 1.0); 275 UPDATE rt_node SET data = $rec WHERE nodeno = 1; 276 DELETE FROM rt_rowid; 277 WITH s(i) AS ( 278 SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<300 279 ) 280 INSERT INTO rt_rowid SELECT i, 1 FROM s; 281 DELETE FROM rt_parent; 282 } 283 284 db close 285 sqlite3 db test.db 286 287 do_execsql_test 8.2 { 288 SELECT count(*) FROM rt WHERE id = 1 289 } {1} 290 291 finish_test