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.2.0 { DROP TABLE t1_node } {}
112 do_corruption_tests rtreeA-1.2 -error "SQL logic error or missing database" {
114 2 "SELECT * FROM t1 WHERE rowid=5"
115 3 "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)"
116 4 "SELECT * FROM t1 WHERE x1<10 AND x2>12"
119 #-------------------------------------------------------------------------
120 # Test the libraries response if some of the entries in the %_node table
121 # are the wrong size.
125 do_test rtreeA-2.1.0 {
126 set nodes [db eval {select nodeno FROM t1_node}]
127 foreach {a b c} $nodes { truncate_node $c 200 }
129 do_corruption_tests rtreeA-2.1 {
131 2 "SELECT * FROM t1 WHERE rowid=5"
132 3 "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)"
133 4 "SELECT * FROM t1 WHERE x1<10 AND x2>12"
138 do_test rtreeA-2.2.0 { truncate_node 1 200 } {}
139 do_corruption_tests rtreeA-2.2 {
141 2 "SELECT * FROM t1 WHERE rowid=5"
142 3 "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)"
143 4 "SELECT * FROM t1 WHERE x1<10 AND x2>12"
146 #-------------------------------------------------------------------------
147 # Set the "depth" of the tree stored on the root node incorrectly. Test
148 # that this does not cause any problems.
152 do_test rtreeA-3.1.0.1 { set_tree_depth t1 } {1}
153 do_test rtreeA-3.1.0.2 { set_tree_depth t1 3 } {3}
154 do_corruption_tests rtreeA-3.1 {
156 2 "SELECT * FROM t1 WHERE rowid=5"
157 3 "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)"
160 do_test rtreeA-3.2.0 { set_tree_depth t1 1000 } {1000}
161 do_corruption_tests rtreeA-3.2 {
163 2 "SELECT * FROM t1 WHERE rowid=5"
164 3 "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)"
169 do_test rtreeA-3.3.0 {
170 execsql { DELETE FROM t1 WHERE rowid = 0 }
171 set_tree_depth t1 65535
173 do_corruption_tests rtreeA-3.3 {
175 2 "SELECT * FROM t1 WHERE rowid=5"
176 3 "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)"
179 #-------------------------------------------------------------------------
180 # Set the "number of entries" field on some nodes incorrectly.
184 do_test rtreeA-4.1.0 {
185 set_entry_count t1 1 4000
187 do_corruption_tests rtreeA-4.1 {
189 2 "SELECT * FROM t1 WHERE rowid=5"
190 3 "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)"
191 4 "SELECT * FROM t1 WHERE x1<10 AND x2>12"
194 #-------------------------------------------------------------------------
195 # Remove entries from the %_parent table and check that this does not
200 do_execsql_test rtreeA-5.1.0 { DELETE FROM t1_parent } {}
201 do_corruption_tests rtreeA-5.1 {
202 1 "DELETE FROM t1 WHERE rowid = 5"
206 #-------------------------------------------------------------------------
207 # Add some bad entries to the %_parent table.
211 do_execsql_test rtreeA-6.1.0 {
212 UPDATE t1_parent set parentnode = parentnode+1
214 do_corruption_tests rtreeA-6.1 {
215 1 "DELETE FROM t1 WHERE rowid = 5"
216 2 "UPDATE t1 SET x1=x1+1, x2=x2+1"