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 #***********************************************************************
12 # This file contains tests to verify that malloc() errors that occur
13 # within the FTS3 module code are handled correctly.
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
18 ifcapable !fts3 { finish_test ; return }
19 source $testdir/malloc_common.tcl
20 source $testdir/fts3_common.tcl
22 # Ensure the lookaside buffer is disabled for these tests.
25 sqlite3_db_config_lookaside db 0 0 0
27 set sqlite_fts3_enable_parentheses 1
32 # fts3_malloc-1.*: Test OOM during CREATE and DROP table statements.
33 # fts3_malloc-2.*: Test OOM during SELECT operations.
34 # fts3_malloc-3.*: Test OOM during SELECT operations with a larger database.
35 # fts3_malloc-4.*: Test OOM during database write operations.
36 # fts3_malloc-5.*: Test that a couple of memory leaks that could follow
37 # OOM in tokenizer code have been fixed.
41 proc normal_list {l} {
43 foreach elem $l {lappend ret $elem}
47 do_write_test fts3_malloc-1.1 sqlite_master {
48 CREATE VIRTUAL TABLE ft1 USING fts3(a, b)
50 do_write_test fts3_malloc-1.2 sqlite_master {
51 CREATE VIRTUAL TABLE ft2 USING fts3([a], [b]);
53 do_write_test fts3_malloc-1.3 sqlite_master {
54 CREATE VIRTUAL TABLE ft3 USING fts3('a', "b");
56 do_write_test fts3_malloc-1.4 sqlite_master {
57 CREATE VIRTUAL TABLE ft4 USING fts3(`a`, 'fred''s column');
59 do_error_test fts3_malloc-1.5 {
60 CREATE VIRTUAL TABLE ft5 USING fts3(a, b, tokenize unknown)
61 } {unknown tokenizer: unknown}
62 do_write_test fts3_malloc-1.6 sqlite_master {
63 CREATE VIRTUAL TABLE ft6 USING fts3(a, b, tokenize porter)
66 # Test the xConnect/xDisconnect methods:
67 #db eval { ATTACH 'test2.db' AS aux }
68 #do_write_test fts3_malloc-1.6 aux.sqlite_master {
69 # CREATE VIRTUAL TABLE aux.ft7 USING fts3(a, b, c);
71 #do_write_test fts3_malloc-1.6 aux.sqlite_master {
72 # CREATE VIRTUAL TABLE aux.ft7 USING fts3(a, b, c);
77 do_test fts3_malloc-2.0 {
85 execsql { CREATE VIRTUAL TABLE ft USING fts3(a, b) }
86 for {set ii 1} {$ii < 32} {incr ii} {
89 if {$ii & 0x01} {lappend a one ; lappend b neung}
90 if {$ii & 0x02} {lappend a two ; lappend b song }
91 if {$ii & 0x04} {lappend a three ; lappend b sahm }
92 if {$ii & 0x08} {lappend a four ; lappend b see }
93 if {$ii & 0x10} {lappend a five ; lappend b hah }
94 execsql { INSERT INTO ft VALUES($a, $b) }
98 foreach {tn sql result} {
99 1 "SELECT count(*) FROM sqlite_master" {5}
100 2 "SELECT * FROM ft WHERE docid = 1" {one neung}
101 3 "SELECT * FROM ft WHERE docid = 2" {two song}
102 4 "SELECT * FROM ft WHERE docid = 3" {{one two} {neung song}}
104 5 "SELECT a FROM ft" {
105 {one} {two} {one two}
106 {three} {one three} {two three}
107 {one two three} {four} {one four}
108 {two four} {one two four} {three four}
109 {one three four} {two three four} {one two three four}
110 {five} {one five} {two five}
111 {one two five} {three five} {one three five}
112 {two three five} {one two three five} {four five}
113 {one four five} {two four five} {one two four five}
114 {three four five} {one three four five} {two three four five}
115 {one two three four five}
118 6 "SELECT a FROM ft WHERE a MATCH 'one'" {
119 {one} {one two} {one three} {one two three}
120 {one four} {one two four} {one three four} {one two three four}
121 {one five} {one two five} {one three five} {one two three five}
122 {one four five} {one two four five}
123 {one three four five} {one two three four five}
126 7 "SELECT a FROM ft WHERE a MATCH 'o*'" {
127 {one} {one two} {one three} {one two three}
128 {one four} {one two four} {one three four} {one two three four}
129 {one five} {one two five} {one three five} {one two three five}
130 {one four five} {one two four five}
131 {one three four five} {one two three four five}
134 8 "SELECT a FROM ft WHERE a MATCH 'o* t*'" {
135 {one two} {one three} {one two three}
136 {one two four} {one three four} {one two three four}
137 {one two five} {one three five} {one two three five}
138 {one two four five} {one three four five} {one two three four five}
141 9 "SELECT a FROM ft WHERE a MATCH '\"o* t*\"'" {
142 {one two} {one three} {one two three}
143 {one two four} {one three four} {one two three four}
144 {one two five} {one three five} {one two three five}
145 {one two four five} {one three four five} {one two three four five}
148 10 {SELECT a FROM ft WHERE a MATCH '"o* f*"'} {
149 {one four} {one five} {one four five}
152 11 {SELECT a FROM ft WHERE a MATCH '"one two three"'} {
156 {one two three four five}
159 12 {SELECT a FROM ft WHERE a MATCH '"two three four"'} {
162 {two three four five}
163 {one two three four five}
166 12 {SELECT a FROM ft WHERE a MATCH '"two three" five'} {
167 {two three five} {one two three five}
168 {two three four five} {one two three four five}
171 13 {SELECT a FROM ft WHERE ft MATCH '"song sahm" hah'} {
172 {two three five} {one two three five}
173 {two three four five} {one two three four five}
176 14 {SELECT a FROM ft WHERE b MATCH 'neung'} {
178 {one three} {one two three}
179 {one four} {one two four}
180 {one three four} {one two three four}
181 {one five} {one two five}
182 {one three five} {one two three five}
183 {one four five} {one two four five}
184 {one three four five} {one two three four five}
187 15 {SELECT a FROM ft WHERE b MATCH '"neung song sahm"'} {
188 {one two three} {one two three four}
189 {one two three five} {one two three four five}
192 16 {SELECT a FROM ft WHERE b MATCH 'hah "song sahm"'} {
193 {two three five} {one two three five}
194 {two three four five} {one two three four five}
197 17 {SELECT a FROM ft WHERE b MATCH 'song OR sahm'} {
198 {two} {one two} {three}
199 {one three} {two three} {one two three}
200 {two four} {one two four} {three four}
201 {one three four} {two three four} {one two three four}
202 {two five} {one two five} {three five}
203 {one three five} {two three five} {one two three five}
204 {two four five} {one two four five} {three four five}
205 {one three four five} {two three four five} {one two three four five}
208 18 {SELECT a FROM ft WHERE a MATCH 'three NOT two'} {
209 {three} {one three} {three four}
210 {one three four} {three five} {one three five}
211 {three four five} {one three four five}
214 19 {SELECT a FROM ft WHERE b MATCH 'sahm NOT song'} {
215 {three} {one three} {three four}
216 {one three four} {three five} {one three five}
217 {three four five} {one three four five}
220 20 {SELECT a FROM ft WHERE ft MATCH 'sahm NOT song'} {
221 {three} {one three} {three four}
222 {one three four} {three five} {one three five}
223 {three four five} {one three four five}
226 21 {SELECT a FROM ft WHERE b MATCH 'neung NEAR song NEAR sahm'} {
227 {one two three} {one two three four}
228 {one two three five} {one two three four five}
232 set result [normal_list $result]
233 do_select_test fts3_malloc-2.$tn $sql $result
236 do_test fts3_malloc-3.0 {
238 for {set ii 32} {$ii < 1024} {incr ii} {
241 if {$ii & 0x0001} {lappend a one ; lappend b neung }
242 if {$ii & 0x0002} {lappend a two ; lappend b song }
243 if {$ii & 0x0004} {lappend a three ; lappend b sahm }
244 if {$ii & 0x0008} {lappend a four ; lappend b see }
245 if {$ii & 0x0010} {lappend a five ; lappend b hah }
246 if {$ii & 0x0020} {lappend a six ; lappend b hok }
247 if {$ii & 0x0040} {lappend a seven ; lappend b jet }
248 if {$ii & 0x0080} {lappend a eight ; lappend b bairt }
249 if {$ii & 0x0100} {lappend a nine ; lappend b gow }
250 if {$ii & 0x0200} {lappend a ten ; lappend b sip }
251 execsql { INSERT INTO ft VALUES($a, $b) }
255 foreach {tn sql result} {
256 1 "SELECT count(*) FROM ft" {1023}
258 2 "SELECT a FROM ft WHERE a MATCH 'one two three four five six seven eight'" {
259 {one two three four five six seven eight}
260 {one two three four five six seven eight nine}
261 {one two three four five six seven eight ten}
262 {one two three four five six seven eight nine ten}
265 3 {SELECT count(*), sum(docid) FROM ft WHERE a MATCH 'o*'} {
269 4 {SELECT count(*), sum(docid) FROM ft WHERE a MATCH '"two three four"'} {
273 set result [normal_list $result]
274 do_select_test fts3_malloc-3.$tn $sql $result
277 do_test fts3_malloc-4.0 {
278 execsql { DELETE FROM ft WHERE docid>=32 }
281 1 "DELETE FROM ft WHERE ft MATCH 'one'"
282 2 "DELETE FROM ft WHERE ft MATCH 'three'"
283 3 "DELETE FROM ft WHERE ft MATCH 'five'"
285 do_write_test fts3_malloc-4.1.$tn ft_content $sql
287 do_test fts3_malloc-4.2 {
288 execsql { SELECT a FROM ft }
289 } {two four {two four}}
291 do_write_test fts3_malloc-5.1 ft_content {
292 INSERT INTO ft VALUES('short alongertoken reallyquitealotlongerimeanit andthistokenisjustsolongthatonemightbeforgivenforimaginingthatitwasmerelyacontrivedexampleandnotarealtoken', 'cynics!')
294 do_test fts3_malloc-5.2 {
295 execsql { CREATE VIRTUAL TABLE ft8 USING fts3(x, tokenize porter) }
297 do_write_test fts3_malloc-5.3 ft_content {
298 INSERT INTO ft8 VALUES('short alongertoken reallyquitealotlongerimeanit andthistokenisjustsolongthatonemightbeforgivenforimaginingthatitwasmerelyacontrivedexampleandnotarealtoken')