1
0

fts3malloc.test 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. # 2009 October 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. #
  12. # This file contains tests to verify that malloc() errors that occur
  13. # within the FTS3 module code are handled correctly.
  14. #
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. ifcapable !fts3 { finish_test ; return }
  18. source $testdir/malloc_common.tcl
  19. source $testdir/fts3_common.tcl
  20. # Ensure the lookaside buffer is disabled for these tests.
  21. #
  22. sqlite3 db test.db
  23. sqlite3_db_config_lookaside db 0 0 0
  24. set sqlite_fts3_enable_parentheses 1
  25. set DO_MALLOC_TEST 1
  26. # Test organization:
  27. #
  28. # fts3_malloc-1.*: Test OOM during CREATE and DROP table statements.
  29. # fts3_malloc-2.*: Test OOM during SELECT operations.
  30. # fts3_malloc-3.*: Test OOM during SELECT operations with a larger database.
  31. # fts3_malloc-4.*: Test OOM during database write operations.
  32. # fts3_malloc-5.*: Test that a couple of memory leaks that could follow
  33. # OOM in tokenizer code have been fixed.
  34. #
  35. proc normal_list {l} {
  36. set ret [list]
  37. foreach elem $l {lappend ret $elem}
  38. set ret
  39. }
  40. do_write_test fts3_malloc-1.1 sqlite_master {
  41. CREATE VIRTUAL TABLE ft1 USING fts3(a, b)
  42. }
  43. do_write_test fts3_malloc-1.2 sqlite_master {
  44. CREATE VIRTUAL TABLE ft2 USING fts3([a], [b]);
  45. }
  46. do_write_test fts3_malloc-1.3 sqlite_master {
  47. CREATE VIRTUAL TABLE ft3 USING fts3('a', "b");
  48. }
  49. do_write_test fts3_malloc-1.4 sqlite_master {
  50. CREATE VIRTUAL TABLE ft4 USING fts3(`a`, 'fred''s column');
  51. }
  52. do_error_test fts3_malloc-1.5 {
  53. CREATE VIRTUAL TABLE ft5 USING fts3(a, b, tokenize unknown)
  54. } {unknown tokenizer: unknown}
  55. do_write_test fts3_malloc-1.6 sqlite_master {
  56. CREATE VIRTUAL TABLE ft6 USING fts3(a, b, tokenize porter)
  57. }
  58. do_write_test fts3_malloc-1.7 sqlite_master {
  59. CREATE VIRTUAL TABLE ft7 USING fts4(a, b, notindexed=b)
  60. }
  61. # Test the xConnect/xDisconnect methods:
  62. #db eval { ATTACH 'test2.db' AS aux }
  63. #do_write_test fts3_malloc-1.6 aux.sqlite_master {
  64. # CREATE VIRTUAL TABLE aux.ft7 USING fts3(a, b, c);
  65. #}
  66. #do_write_test fts3_malloc-1.6 aux.sqlite_master {
  67. # CREATE VIRTUAL TABLE aux.ft7 USING fts3(a, b, c);
  68. #}
  69. do_test fts3_malloc-2.0 {
  70. execsql {
  71. DROP TABLE ft1;
  72. DROP TABLE ft2;
  73. DROP TABLE ft3;
  74. DROP TABLE ft4;
  75. DROP TABLE ft6;
  76. DROP TABLE ft7;
  77. }
  78. execsql { CREATE VIRTUAL TABLE ft USING fts3(a, b) }
  79. for {set ii 1} {$ii < 32} {incr ii} {
  80. set a [list]
  81. set b [list]
  82. if {$ii & 0x01} {lappend a one ; lappend b neung}
  83. if {$ii & 0x02} {lappend a two ; lappend b song }
  84. if {$ii & 0x04} {lappend a three ; lappend b sahm }
  85. if {$ii & 0x08} {lappend a four ; lappend b see }
  86. if {$ii & 0x10} {lappend a five ; lappend b hah }
  87. execsql { INSERT INTO ft VALUES($a, $b) }
  88. }
  89. } {}
  90. foreach {tn sql result} {
  91. 1 "SELECT count(*) FROM sqlite_master" {5}
  92. 2 "SELECT * FROM ft WHERE docid = 1" {one neung}
  93. 3 "SELECT * FROM ft WHERE docid = 2" {two song}
  94. 4 "SELECT * FROM ft WHERE docid = 3" {{one two} {neung song}}
  95. 5 "SELECT a FROM ft" {
  96. {one} {two} {one two}
  97. {three} {one three} {two three}
  98. {one two three} {four} {one four}
  99. {two four} {one two four} {three four}
  100. {one three four} {two three four} {one two three four}
  101. {five} {one five} {two five}
  102. {one two five} {three five} {one three five}
  103. {two three five} {one two three five} {four five}
  104. {one four five} {two four five} {one two four five}
  105. {three four five} {one three four five} {two three four five}
  106. {one two three four five}
  107. }
  108. 6 "SELECT a FROM ft WHERE a MATCH 'one'" {
  109. {one} {one two} {one three} {one two three}
  110. {one four} {one two four} {one three four} {one two three four}
  111. {one five} {one two five} {one three five} {one two three five}
  112. {one four five} {one two four five}
  113. {one three four five} {one two three four five}
  114. }
  115. 7 "SELECT a FROM ft WHERE a MATCH 'o*'" {
  116. {one} {one two} {one three} {one two three}
  117. {one four} {one two four} {one three four} {one two three four}
  118. {one five} {one two five} {one three five} {one two three five}
  119. {one four five} {one two four five}
  120. {one three four five} {one two three four five}
  121. }
  122. 8 "SELECT a FROM ft WHERE a MATCH 'o* t*'" {
  123. {one two} {one three} {one two three}
  124. {one two four} {one three four} {one two three four}
  125. {one two five} {one three five} {one two three five}
  126. {one two four five} {one three four five} {one two three four five}
  127. }
  128. 9 "SELECT a FROM ft WHERE a MATCH '\"o* t*\"'" {
  129. {one two} {one three} {one two three}
  130. {one two four} {one three four} {one two three four}
  131. {one two five} {one three five} {one two three five}
  132. {one two four five} {one three four five} {one two three four five}
  133. }
  134. 10 {SELECT a FROM ft WHERE a MATCH '"o* f*"'} {
  135. {one four} {one five} {one four five}
  136. }
  137. 11 {SELECT a FROM ft WHERE a MATCH '"one two three"'} {
  138. {one two three}
  139. {one two three four}
  140. {one two three five}
  141. {one two three four five}
  142. }
  143. 12 {SELECT a FROM ft WHERE a MATCH '"two three four"'} {
  144. {two three four}
  145. {one two three four}
  146. {two three four five}
  147. {one two three four five}
  148. }
  149. 12 {SELECT a FROM ft WHERE a MATCH '"two three" five'} {
  150. {two three five} {one two three five}
  151. {two three four five} {one two three four five}
  152. }
  153. 13 {SELECT a FROM ft WHERE ft MATCH '"song sahm" hah'} {
  154. {two three five} {one two three five}
  155. {two three four five} {one two three four five}
  156. }
  157. 14 {SELECT a FROM ft WHERE b MATCH 'neung'} {
  158. {one} {one two}
  159. {one three} {one two three}
  160. {one four} {one two four}
  161. {one three four} {one two three four}
  162. {one five} {one two five}
  163. {one three five} {one two three five}
  164. {one four five} {one two four five}
  165. {one three four five} {one two three four five}
  166. }
  167. 15 {SELECT a FROM ft WHERE b MATCH '"neung song sahm"'} {
  168. {one two three} {one two three four}
  169. {one two three five} {one two three four five}
  170. }
  171. 16 {SELECT a FROM ft WHERE b MATCH 'hah "song sahm"'} {
  172. {two three five} {one two three five}
  173. {two three four five} {one two three four five}
  174. }
  175. 17 {SELECT a FROM ft WHERE b MATCH 'song OR sahm'} {
  176. {two} {one two} {three}
  177. {one three} {two three} {one two three}
  178. {two four} {one two four} {three four}
  179. {one three four} {two three four} {one two three four}
  180. {two five} {one two five} {three five}
  181. {one three five} {two three five} {one two three five}
  182. {two four five} {one two four five} {three four five}
  183. {one three four five} {two three four five} {one two three four five}
  184. }
  185. 18 {SELECT a FROM ft WHERE a MATCH 'three NOT two'} {
  186. {three} {one three} {three four}
  187. {one three four} {three five} {one three five}
  188. {three four five} {one three four five}
  189. }
  190. 19 {SELECT a FROM ft WHERE b MATCH 'sahm NOT song'} {
  191. {three} {one three} {three four}
  192. {one three four} {three five} {one three five}
  193. {three four five} {one three four five}
  194. }
  195. 20 {SELECT a FROM ft WHERE ft MATCH 'sahm NOT song'} {
  196. {three} {one three} {three four}
  197. {one three four} {three five} {one three five}
  198. {three four five} {one three four five}
  199. }
  200. 21 {SELECT a FROM ft WHERE b MATCH 'neung NEAR song NEAR sahm'} {
  201. {one two three} {one two three four}
  202. {one two three five} {one two three four five}
  203. }
  204. } {
  205. set result [normal_list $result]
  206. do_select_test fts3_malloc-2.$tn $sql $result
  207. }
  208. do_test fts3_malloc-3.0 {
  209. execsql BEGIN
  210. for {set ii 32} {$ii < 1024} {incr ii} {
  211. set a [list]
  212. set b [list]
  213. if {$ii & 0x0001} {lappend a one ; lappend b neung }
  214. if {$ii & 0x0002} {lappend a two ; lappend b song }
  215. if {$ii & 0x0004} {lappend a three ; lappend b sahm }
  216. if {$ii & 0x0008} {lappend a four ; lappend b see }
  217. if {$ii & 0x0010} {lappend a five ; lappend b hah }
  218. if {$ii & 0x0020} {lappend a six ; lappend b hok }
  219. if {$ii & 0x0040} {lappend a seven ; lappend b jet }
  220. if {$ii & 0x0080} {lappend a eight ; lappend b bairt }
  221. if {$ii & 0x0100} {lappend a nine ; lappend b gow }
  222. if {$ii & 0x0200} {lappend a ten ; lappend b sip }
  223. execsql { INSERT INTO ft VALUES($a, $b) }
  224. }
  225. execsql COMMIT
  226. } {}
  227. foreach {tn sql result} {
  228. 1 "SELECT count(*) FROM ft" {1023}
  229. 2 "SELECT a FROM ft WHERE a MATCH 'one two three four five six seven eight'" {
  230. {one two three four five six seven eight}
  231. {one two three four five six seven eight nine}
  232. {one two three four five six seven eight ten}
  233. {one two three four five six seven eight nine ten}
  234. }
  235. 3 {SELECT count(*), sum(docid) FROM ft WHERE a MATCH 'o*'} {
  236. 512 262144
  237. }
  238. 4 {SELECT count(*), sum(docid) FROM ft WHERE a MATCH '"two three four"'} {
  239. 128 66368
  240. }
  241. } {
  242. set result [normal_list $result]
  243. do_select_test fts3_malloc-3.$tn $sql $result
  244. }
  245. do_test fts3_malloc-4.0 {
  246. execsql { DELETE FROM ft WHERE docid>=32 }
  247. } {}
  248. foreach {tn sql} {
  249. 1 "DELETE FROM ft WHERE ft MATCH 'one'"
  250. 2 "DELETE FROM ft WHERE ft MATCH 'three'"
  251. 3 "DELETE FROM ft WHERE ft MATCH 'five'"
  252. } {
  253. do_write_test fts3_malloc-4.1.$tn ft_content $sql
  254. }
  255. do_test fts3_malloc-4.2 {
  256. execsql { SELECT a FROM ft }
  257. } {two four {two four}}
  258. do_write_test fts3_malloc-5.1 ft_content {
  259. INSERT INTO ft VALUES('short alongertoken reallyquitealotlongerimeanit andthistokenisjustsolongthatonemightbeforgivenforimaginingthatitwasmerelyacontrivedexampleandnotarealtoken', 'cynics!')
  260. }
  261. do_test fts3_malloc-5.2 {
  262. execsql { CREATE VIRTUAL TABLE ft8 USING fts3(x, tokenize porter) }
  263. } {}
  264. do_write_test fts3_malloc-5.3 ft_content {
  265. INSERT INTO ft8 VALUES('short alongertoken reallyquitealotlongerimeanit andthistokenisjustsolongthatonemightbeforgivenforimaginingthatitwasmerelyacontrivedexampleandnotarealtoken')
  266. }
  267. finish_test