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 #***********************************************************************
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
16 if {![info exists testdir]} {
17 set testdir [file join [file dirname [info script]] .. .. test]
19 source $testdir/tester.tcl
20 ifcapable !rtree { finish_test ; return }
27 PRAGMA page_size = 1024;
28 CREATE VIRTUAL TABLE t1 USING rtree(id, x1, x2, y1, y2);
33 for {set i 0} {$i < 500} {incr i} {
36 execsql { INSERT INTO t1 VALUES($i, $i, $x2, $i, $y2) }
41 proc truncate_node {nodeno nTrunc} {
42 set blob [db one {SELECT data FROM t1_node WHERE nodeno=$nodeno}]
43 if {$nTrunc<0} {set nTrunc "end-$nTrunc"}
44 set blob [string range $blob 0 $nTrunc]
45 db eval { UPDATE t1_node SET data = $blob WHERE nodeno=$nodeno }
48 proc set_tree_depth {tbl {newvalue ""}} {
49 set blob [db one "SELECT data FROM ${tbl}_node WHERE nodeno=1"]
51 if {$newvalue == ""} {
52 binary scan $blob Su oldvalue
56 set blob [binary format Sua* $newvalue [string range $blob 2 end]]
57 db eval "UPDATE ${tbl}_node SET data = \$blob WHERE nodeno=1"
58 return [set_tree_depth $tbl]
61 proc set_entry_count {tbl nodeno {newvalue ""}} {
62 set blob [db one "SELECT data FROM ${tbl}_node WHERE nodeno=$nodeno"]
64 if {$newvalue == ""} {
65 binary scan [string range $blob 2 end] Su oldvalue
69 set blob [binary format a*Sua* \
70 [string range $blob 0 1] $newvalue [string range $blob 4 end]
72 db eval "UPDATE ${tbl}_node SET data = \$blob WHERE nodeno=$nodeno"
73 return [set_entry_count $tbl $nodeno]
77 proc do_corruption_tests {prefix args} {
78 set testarray [lindex $args end]
79 set errormsg {database disk image is malformed}
81 foreach {z value} [lrange $args 0 end-1] {
82 set n [string length $z]
83 if {$n>=2 && [string equal -length $n $z "-error"]} {
88 foreach {tn sql} $testarray {
89 do_catchsql_test $prefix.$tn $sql [list 1 $errormsg]
93 #-------------------------------------------------------------------------
94 # Test the libraries response if the %_node table is completely empty
95 # (i.e. the root node is missing), or has been removed from the database
100 do_execsql_test rtreeA-1.0 {
104 do_corruption_tests rtreeA-1.1 {
106 2 "SELECT * FROM t1 WHERE rowid=5"
107 3 "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)"
108 4 "SELECT * FROM t1 WHERE x1<10 AND x2>12"
111 do_execsql_test rtreeA-1.1.1 {
112 SELECT rtreecheck('main', 't1')
113 } {{Node 1 missing from database
114 Wrong number of entries in %_rowid table - expected 0, actual 500
115 Wrong number of entries in %_parent table - expected 0, actual 23}}
117 do_execsql_test rtreeA-1.2.0 { DROP TABLE t1_node } {}
118 do_corruption_tests rtreeA-1.2 -error "database disk image is malformed" {
120 2 "SELECT * FROM t1 WHERE rowid=5"
121 3 "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)"
122 4 "SELECT * FROM t1 WHERE x1<10 AND x2>12"
125 #-------------------------------------------------------------------------
126 # Test the libraries response if some of the entries in the %_node table
127 # are the wrong size.
131 do_test rtreeA-2.1.0 {
132 set nodes [db eval {select nodeno FROM t1_node}]
133 foreach {a b c} $nodes { truncate_node $c 200 }
135 do_corruption_tests rtreeA-2.1 {
137 2 "SELECT * FROM t1 WHERE rowid=5"
138 3 "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)"
139 4 "SELECT * FROM t1 WHERE x1<10 AND x2>12"
144 do_test rtreeA-2.2.0 { truncate_node 1 200 } {}
145 do_corruption_tests rtreeA-2.2 {
147 2 "SELECT * FROM t1 WHERE rowid=5"
148 3 "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)"
149 4 "SELECT * FROM t1 WHERE x1<10 AND x2>12"
152 #-------------------------------------------------------------------------
153 # Set the "depth" of the tree stored on the root node incorrectly. Test
154 # that this does not cause any problems.
158 do_test rtreeA-3.1.0.1 { set_tree_depth t1 } {1}
159 do_test rtreeA-3.1.0.2 { set_tree_depth t1 3 } {3}
160 do_corruption_tests rtreeA-3.1 {
162 2 "SELECT * FROM t1 WHERE rowid=5"
163 3 "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)"
166 do_execsql_test rtreeA-3.1.0.3 {
167 SELECT rtreecheck('main', 't1')!="ok"
170 do_test rtreeA-3.2.0 { set_tree_depth t1 1000 } {1000}
171 do_corruption_tests rtreeA-3.2 {
173 2 "SELECT * FROM t1 WHERE rowid=5"
174 3 "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)"
179 do_test rtreeA-3.3.0 {
180 execsql { DELETE FROM t1 WHERE rowid = 0 }
181 set_tree_depth t1 65535
183 do_corruption_tests rtreeA-3.3 {
185 2 "SELECT * FROM t1 WHERE rowid=5"
186 3 "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)"
189 do_execsql_test rtreeA-3.3.3.4 {
190 SELECT rtreecheck('main', 't1')
191 } {{Rtree depth out of range (65535)
192 Wrong number of entries in %_rowid table - expected 0, actual 499
193 Wrong number of entries in %_parent table - expected 0, actual 23}}
195 #-------------------------------------------------------------------------
196 # Set the "number of entries" field on some nodes incorrectly.
200 do_test rtreeA-4.1.0 {
201 set_entry_count t1 1 4000
203 do_corruption_tests rtreeA-4.1 {
205 2 "SELECT * FROM t1 WHERE rowid=5"
206 3 "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)"
207 4 "SELECT * FROM t1 WHERE x1<10 AND x2>12"
210 #-------------------------------------------------------------------------
211 # Remove entries from the %_parent table and check that this does not
216 do_execsql_test rtreeA-5.1.0 { DELETE FROM t1_parent } {}
217 do_corruption_tests rtreeA-5.1 {
218 1 "DELETE FROM t1 WHERE rowid = 5"
222 do_execsql_test rtreeA-5.2 {
223 SELECT rtreecheck('main', 't1')!="ok"
226 #-------------------------------------------------------------------------
227 # Add some bad entries to the %_parent table.
231 do_execsql_test rtreeA-6.1.0 {
232 UPDATE t1_parent set parentnode = parentnode+1
234 do_corruption_tests rtreeA-6.1 {
235 1 "DELETE FROM t1 WHERE rowid = 5"
236 2 "UPDATE t1 SET x1=x1+1, x2=x2+1"
239 do_execsql_test rtreeA-6.2 {
240 SELECT rtreecheck('main', 't1')!="ok"
243 #-------------------------------------------------------------------------
244 # Truncated blobs in the _node table.
249 do_execsql_test rtreeA-7.100 {
250 UPDATE t1_node SET data=x'' WHERE rowid=1;
252 do_catchsql_test rtreeA-7.110 {
253 SELECT * FROM t1 WHERE x1>0 AND x1<100 AND x2>0 AND x2<100;
254 } {1 {undersize RTree blobs in "t1_node"}}
255 do_test rtreeA-7.120 {
256 sqlite3_extended_errcode db
257 } {SQLITE_CORRUPT_VTAB}