109977bb9Sdan# 2009 October 22 209977bb9Sdan# 309977bb9Sdan# The author disclaims copyright to this source code. In place of 409977bb9Sdan# a legal notice, here is a blessing: 509977bb9Sdan# 609977bb9Sdan# May you do good and not evil. 709977bb9Sdan# May you find forgiveness for yourself and forgive others. 809977bb9Sdan# May you share freely, never taking more than you give. 909977bb9Sdan# 1009977bb9Sdan#*********************************************************************** 1109977bb9Sdan# 1209977bb9Sdan# This file contains tests to verify that malloc() errors that occur 1309977bb9Sdan# within the FTS3 module code are handled correctly. 1409977bb9Sdan# 1509977bb9Sdan 1609977bb9Sdanset testdir [file dirname $argv0] 1709977bb9Sdansource $testdir/tester.tcl 1809977bb9Sdanifcapable !fts3 { finish_test ; return } 1909977bb9Sdansource $testdir/malloc_common.tcl 2091f0ce39Sdansource $testdir/fts3_common.tcl 2109977bb9Sdan 22948a5f88Sdan# Ensure the lookaside buffer is disabled for these tests. 23948a5f88Sdan# 24948a5f88Sdansqlite3 db test.db 25948a5f88Sdansqlite3_db_config_lookaside db 0 0 0 26948a5f88Sdan 2791f0ce39Sdanset sqlite_fts3_enable_parentheses 1 28948a5f88Sdanset DO_MALLOC_TEST 1 2991f0ce39Sdan 30948a5f88Sdan# Test organization: 31948a5f88Sdan# 3216708c4aSdan# fts3_malloc-1.*: Test OOM during CREATE and DROP table statements. 3316708c4aSdan# fts3_malloc-2.*: Test OOM during SELECT operations. 3416708c4aSdan# fts3_malloc-3.*: Test OOM during SELECT operations with a larger database. 3516708c4aSdan# fts3_malloc-4.*: Test OOM during database write operations. 36d3789b98Sdan# fts3_malloc-5.*: Test that a couple of memory leaks that could follow 37d3789b98Sdan# OOM in tokenizer code have been fixed. 38948a5f88Sdan# 3909977bb9Sdan 4091f0ce39Sdan 4109977bb9Sdanproc normal_list {l} { 4209977bb9Sdan set ret [list] 4309977bb9Sdan foreach elem $l {lappend ret $elem} 4409977bb9Sdan set ret 4509977bb9Sdan} 4609977bb9Sdan 47948a5f88Sdando_write_test fts3_malloc-1.1 sqlite_master { 48948a5f88Sdan CREATE VIRTUAL TABLE ft1 USING fts3(a, b) 49948a5f88Sdan} 50948a5f88Sdando_write_test fts3_malloc-1.2 sqlite_master { 51948a5f88Sdan CREATE VIRTUAL TABLE ft2 USING fts3([a], [b]); 52948a5f88Sdan} 53948a5f88Sdando_write_test fts3_malloc-1.3 sqlite_master { 54948a5f88Sdan CREATE VIRTUAL TABLE ft3 USING fts3('a', "b"); 55948a5f88Sdan} 56948a5f88Sdando_write_test fts3_malloc-1.4 sqlite_master { 57948a5f88Sdan CREATE VIRTUAL TABLE ft4 USING fts3(`a`, 'fred''s column'); 58948a5f88Sdan} 59948a5f88Sdando_error_test fts3_malloc-1.5 { 60948a5f88Sdan CREATE VIRTUAL TABLE ft5 USING fts3(a, b, tokenize unknown) 61948a5f88Sdan} {unknown tokenizer: unknown} 62948a5f88Sdando_write_test fts3_malloc-1.6 sqlite_master { 63948a5f88Sdan CREATE VIRTUAL TABLE ft6 USING fts3(a, b, tokenize porter) 64948a5f88Sdan} 65*9faa6482Sdando_write_test fts3_malloc-1.7 sqlite_master { 66*9faa6482Sdan CREATE VIRTUAL TABLE ft7 USING fts4(a, b, notindexed=b) 67*9faa6482Sdan} 68948a5f88Sdan 69948a5f88Sdan# Test the xConnect/xDisconnect methods: 70f13b704eSdan#db eval { ATTACH 'test2.db' AS aux } 71f13b704eSdan#do_write_test fts3_malloc-1.6 aux.sqlite_master { 72f13b704eSdan# CREATE VIRTUAL TABLE aux.ft7 USING fts3(a, b, c); 73f13b704eSdan#} 74f13b704eSdan#do_write_test fts3_malloc-1.6 aux.sqlite_master { 75f13b704eSdan# CREATE VIRTUAL TABLE aux.ft7 USING fts3(a, b, c); 76f13b704eSdan#} 77948a5f88Sdan 78948a5f88Sdan 7909977bb9Sdan 8009977bb9Sdando_test fts3_malloc-2.0 { 81f13b704eSdan execsql { 82f13b704eSdan DROP TABLE ft1; 83f13b704eSdan DROP TABLE ft2; 84f13b704eSdan DROP TABLE ft3; 85f13b704eSdan DROP TABLE ft4; 86f13b704eSdan DROP TABLE ft6; 87*9faa6482Sdan DROP TABLE ft7; 88f13b704eSdan } 8909977bb9Sdan execsql { CREATE VIRTUAL TABLE ft USING fts3(a, b) } 9009977bb9Sdan for {set ii 1} {$ii < 32} {incr ii} { 9109977bb9Sdan set a [list] 9209977bb9Sdan set b [list] 9309977bb9Sdan if {$ii & 0x01} {lappend a one ; lappend b neung} 9409977bb9Sdan if {$ii & 0x02} {lappend a two ; lappend b song } 9509977bb9Sdan if {$ii & 0x04} {lappend a three ; lappend b sahm } 9609977bb9Sdan if {$ii & 0x08} {lappend a four ; lappend b see } 9709977bb9Sdan if {$ii & 0x10} {lappend a five ; lappend b hah } 9809977bb9Sdan execsql { INSERT INTO ft VALUES($a, $b) } 9909977bb9Sdan } 10009977bb9Sdan} {} 10109977bb9Sdan 10209977bb9Sdanforeach {tn sql result} { 10309977bb9Sdan 1 "SELECT count(*) FROM sqlite_master" {5} 10409977bb9Sdan 2 "SELECT * FROM ft WHERE docid = 1" {one neung} 10509977bb9Sdan 3 "SELECT * FROM ft WHERE docid = 2" {two song} 10609977bb9Sdan 4 "SELECT * FROM ft WHERE docid = 3" {{one two} {neung song}} 10709977bb9Sdan 10809977bb9Sdan 5 "SELECT a FROM ft" { 10909977bb9Sdan {one} {two} {one two} 11009977bb9Sdan {three} {one three} {two three} 11109977bb9Sdan {one two three} {four} {one four} 11209977bb9Sdan {two four} {one two four} {three four} 11309977bb9Sdan {one three four} {two three four} {one two three four} 11409977bb9Sdan {five} {one five} {two five} 11509977bb9Sdan {one two five} {three five} {one three five} 11609977bb9Sdan {two three five} {one two three five} {four five} 11709977bb9Sdan {one four five} {two four five} {one two four five} 11809977bb9Sdan {three four five} {one three four five} {two three four five} 11909977bb9Sdan {one two three four five} 12009977bb9Sdan } 12109977bb9Sdan 12209977bb9Sdan 6 "SELECT a FROM ft WHERE a MATCH 'one'" { 12309977bb9Sdan {one} {one two} {one three} {one two three} 12409977bb9Sdan {one four} {one two four} {one three four} {one two three four} 12509977bb9Sdan {one five} {one two five} {one three five} {one two three five} 12609977bb9Sdan {one four five} {one two four five} 12709977bb9Sdan {one three four five} {one two three four five} 12809977bb9Sdan } 12909977bb9Sdan 13009977bb9Sdan 7 "SELECT a FROM ft WHERE a MATCH 'o*'" { 13109977bb9Sdan {one} {one two} {one three} {one two three} 13209977bb9Sdan {one four} {one two four} {one three four} {one two three four} 13309977bb9Sdan {one five} {one two five} {one three five} {one two three five} 13409977bb9Sdan {one four five} {one two four five} 13509977bb9Sdan {one three four five} {one two three four five} 13609977bb9Sdan } 13709977bb9Sdan 13809977bb9Sdan 8 "SELECT a FROM ft WHERE a MATCH 'o* t*'" { 13909977bb9Sdan {one two} {one three} {one two three} 14009977bb9Sdan {one two four} {one three four} {one two three four} 14109977bb9Sdan {one two five} {one three five} {one two three five} 14209977bb9Sdan {one two four five} {one three four five} {one two three four five} 14309977bb9Sdan } 14409977bb9Sdan 14509977bb9Sdan 9 "SELECT a FROM ft WHERE a MATCH '\"o* t*\"'" { 14609977bb9Sdan {one two} {one three} {one two three} 14709977bb9Sdan {one two four} {one three four} {one two three four} 14809977bb9Sdan {one two five} {one three five} {one two three five} 14909977bb9Sdan {one two four five} {one three four five} {one two three four five} 15009977bb9Sdan } 15109977bb9Sdan 15209977bb9Sdan 10 {SELECT a FROM ft WHERE a MATCH '"o* f*"'} { 15309977bb9Sdan {one four} {one five} {one four five} 15409977bb9Sdan } 15509977bb9Sdan 15609977bb9Sdan 11 {SELECT a FROM ft WHERE a MATCH '"one two three"'} { 15709977bb9Sdan {one two three} 15809977bb9Sdan {one two three four} 15909977bb9Sdan {one two three five} 16009977bb9Sdan {one two three four five} 16109977bb9Sdan } 16209977bb9Sdan 16309977bb9Sdan 12 {SELECT a FROM ft WHERE a MATCH '"two three four"'} { 16409977bb9Sdan {two three four} 16509977bb9Sdan {one two three four} 16609977bb9Sdan {two three four five} 16709977bb9Sdan {one two three four five} 16809977bb9Sdan } 16909977bb9Sdan 17009977bb9Sdan 12 {SELECT a FROM ft WHERE a MATCH '"two three" five'} { 17109977bb9Sdan {two three five} {one two three five} 17209977bb9Sdan {two three four five} {one two three four five} 17309977bb9Sdan } 17409977bb9Sdan 17509977bb9Sdan 13 {SELECT a FROM ft WHERE ft MATCH '"song sahm" hah'} { 17609977bb9Sdan {two three five} {one two three five} 17709977bb9Sdan {two three four five} {one two three four five} 17809977bb9Sdan } 17909977bb9Sdan 18009977bb9Sdan 14 {SELECT a FROM ft WHERE b MATCH 'neung'} { 18109977bb9Sdan {one} {one two} 18209977bb9Sdan {one three} {one two three} 18309977bb9Sdan {one four} {one two four} 18409977bb9Sdan {one three four} {one two three four} 18509977bb9Sdan {one five} {one two five} 18609977bb9Sdan {one three five} {one two three five} 18709977bb9Sdan {one four five} {one two four five} 18809977bb9Sdan {one three four five} {one two three four five} 18909977bb9Sdan } 19009977bb9Sdan 19109977bb9Sdan 15 {SELECT a FROM ft WHERE b MATCH '"neung song sahm"'} { 19209977bb9Sdan {one two three} {one two three four} 19309977bb9Sdan {one two three five} {one two three four five} 19409977bb9Sdan } 19509977bb9Sdan 19609977bb9Sdan 16 {SELECT a FROM ft WHERE b MATCH 'hah "song sahm"'} { 19709977bb9Sdan {two three five} {one two three five} 19809977bb9Sdan {two three four five} {one two three four five} 19909977bb9Sdan } 20009977bb9Sdan 20109977bb9Sdan 17 {SELECT a FROM ft WHERE b MATCH 'song OR sahm'} { 20209977bb9Sdan {two} {one two} {three} 20309977bb9Sdan {one three} {two three} {one two three} 20409977bb9Sdan {two four} {one two four} {three four} 20509977bb9Sdan {one three four} {two three four} {one two three four} 20609977bb9Sdan {two five} {one two five} {three five} 20709977bb9Sdan {one three five} {two three five} {one two three five} 20809977bb9Sdan {two four five} {one two four five} {three four five} 20909977bb9Sdan {one three four five} {two three four five} {one two three four five} 21009977bb9Sdan } 21109977bb9Sdan 21209977bb9Sdan 18 {SELECT a FROM ft WHERE a MATCH 'three NOT two'} { 21309977bb9Sdan {three} {one three} {three four} 21409977bb9Sdan {one three four} {three five} {one three five} 21509977bb9Sdan {three four five} {one three four five} 21609977bb9Sdan } 21709977bb9Sdan 21809977bb9Sdan 19 {SELECT a FROM ft WHERE b MATCH 'sahm NOT song'} { 21909977bb9Sdan {three} {one three} {three four} 22009977bb9Sdan {one three four} {three five} {one three five} 22109977bb9Sdan {three four five} {one three four five} 22209977bb9Sdan } 22309977bb9Sdan 22409977bb9Sdan 20 {SELECT a FROM ft WHERE ft MATCH 'sahm NOT song'} { 22509977bb9Sdan {three} {one three} {three four} 22609977bb9Sdan {one three four} {three five} {one three five} 22709977bb9Sdan {three four five} {one three four five} 22809977bb9Sdan } 22909977bb9Sdan 23009977bb9Sdan 21 {SELECT a FROM ft WHERE b MATCH 'neung NEAR song NEAR sahm'} { 23109977bb9Sdan {one two three} {one two three four} 23209977bb9Sdan {one two three five} {one two three four five} 23309977bb9Sdan } 23409977bb9Sdan 23509977bb9Sdan} { 23609977bb9Sdan set result [normal_list $result] 23791f0ce39Sdan do_select_test fts3_malloc-2.$tn $sql $result 23809977bb9Sdan} 23909977bb9Sdan 24009977bb9Sdando_test fts3_malloc-3.0 { 24109977bb9Sdan execsql BEGIN 24209977bb9Sdan for {set ii 32} {$ii < 1024} {incr ii} { 24309977bb9Sdan set a [list] 24409977bb9Sdan set b [list] 24509977bb9Sdan if {$ii & 0x0001} {lappend a one ; lappend b neung } 24609977bb9Sdan if {$ii & 0x0002} {lappend a two ; lappend b song } 24709977bb9Sdan if {$ii & 0x0004} {lappend a three ; lappend b sahm } 24809977bb9Sdan if {$ii & 0x0008} {lappend a four ; lappend b see } 24909977bb9Sdan if {$ii & 0x0010} {lappend a five ; lappend b hah } 25009977bb9Sdan if {$ii & 0x0020} {lappend a six ; lappend b hok } 25109977bb9Sdan if {$ii & 0x0040} {lappend a seven ; lappend b jet } 25209977bb9Sdan if {$ii & 0x0080} {lappend a eight ; lappend b bairt } 25309977bb9Sdan if {$ii & 0x0100} {lappend a nine ; lappend b gow } 25409977bb9Sdan if {$ii & 0x0200} {lappend a ten ; lappend b sip } 25509977bb9Sdan execsql { INSERT INTO ft VALUES($a, $b) } 25609977bb9Sdan } 25709977bb9Sdan execsql COMMIT 25809977bb9Sdan} {} 25909977bb9Sdanforeach {tn sql result} { 26009977bb9Sdan 1 "SELECT count(*) FROM ft" {1023} 26191f0ce39Sdan 26209977bb9Sdan 2 "SELECT a FROM ft WHERE a MATCH 'one two three four five six seven eight'" { 26309977bb9Sdan {one two three four five six seven eight} 26409977bb9Sdan {one two three four five six seven eight nine} 26509977bb9Sdan {one two three four five six seven eight ten} 26609977bb9Sdan {one two three four five six seven eight nine ten} 26709977bb9Sdan } 26809977bb9Sdan 26991f0ce39Sdan 3 {SELECT count(*), sum(docid) FROM ft WHERE a MATCH 'o*'} { 27091f0ce39Sdan 512 262144 27191f0ce39Sdan } 27291f0ce39Sdan 27309977bb9Sdan 4 {SELECT count(*), sum(docid) FROM ft WHERE a MATCH '"two three four"'} { 27409977bb9Sdan 128 66368 27509977bb9Sdan } 27609977bb9Sdan} { 27709977bb9Sdan set result [normal_list $result] 27891f0ce39Sdan do_select_test fts3_malloc-3.$tn $sql $result 27909977bb9Sdan} 28009977bb9Sdan 28191f0ce39Sdando_test fts3_malloc-4.0 { 28291f0ce39Sdan execsql { DELETE FROM ft WHERE docid>=32 } 28391f0ce39Sdan} {} 28491f0ce39Sdanforeach {tn sql} { 28591f0ce39Sdan 1 "DELETE FROM ft WHERE ft MATCH 'one'" 28691f0ce39Sdan 2 "DELETE FROM ft WHERE ft MATCH 'three'" 28791f0ce39Sdan 3 "DELETE FROM ft WHERE ft MATCH 'five'" 28891f0ce39Sdan} { 28991f0ce39Sdan do_write_test fts3_malloc-4.1.$tn ft_content $sql 29091f0ce39Sdan} 29191f0ce39Sdando_test fts3_malloc-4.2 { 29291f0ce39Sdan execsql { SELECT a FROM ft } 29391f0ce39Sdan} {two four {two four}} 29491f0ce39Sdan 295d3789b98Sdando_write_test fts3_malloc-5.1 ft_content { 296d3789b98Sdan INSERT INTO ft VALUES('short alongertoken reallyquitealotlongerimeanit andthistokenisjustsolongthatonemightbeforgivenforimaginingthatitwasmerelyacontrivedexampleandnotarealtoken', 'cynics!') 297d3789b98Sdan} 298d3789b98Sdando_test fts3_malloc-5.2 { 299d3789b98Sdan execsql { CREATE VIRTUAL TABLE ft8 USING fts3(x, tokenize porter) } 300d3789b98Sdan} {} 30149fc3364Sdan 302d3789b98Sdando_write_test fts3_malloc-5.3 ft_content { 303d3789b98Sdan INSERT INTO ft8 VALUES('short alongertoken reallyquitealotlongerimeanit andthistokenisjustsolongthatonemightbeforgivenforimaginingthatitwasmerelyacontrivedexampleandnotarealtoken') 304d3789b98Sdan} 305d3789b98Sdan 30691f0ce39Sdan 30709977bb9Sdanfinish_test 30809977bb9Sdan