1# 2010 June 15
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
13set testdir [file dirname $argv0]
14source $testdir/tester.tcl
15
16set ::testprefix fts3fault
17
18# If SQLITE_ENABLE_FTS3 is not defined, omit this file.
19ifcapable !fts3 { finish_test ; return }
20
21if 1 {
22
23# Test error handling in the sqlite3Fts3Init() function. This is the 
24# function that registers the FTS3 module and various support functions
25# with SQLite.
26#
27do_faultsim_test 1 -body { 
28  sqlite3 db test.db 
29  expr 0
30} -test {
31  catch { db close }
32}
33
34# Test error handling in an "ALTER TABLE ... RENAME TO" statement on an
35# FTS3 table. Specifically, test renaming the table within a transaction
36# after it has been written to.
37#
38faultsim_delete_and_reopen
39do_execsql_test 2.0 {
40  CREATE VIRTUAL TABLE t1 USING fts3;
41  INSERT INTO t1 VALUES('test renaming the table');
42  INSERT INTO t1 VALUES(' after it has been written');
43}
44do_faultsim_test 2 -prep { 
45  sqlite3 db test.db
46  execsql {
47    BEGIN;
48      INSERT INTO t1 VALUES('registers the FTS3 module');
49      INSERT INTO t1 VALUES('various support functions');
50  }
51} -body {
52  execsql { ALTER TABLE t1 RENAME TO t2 }
53} -test {
54  faultsim_test_result {0 {}} 
55}
56
57# Test error handling in the special case where a single prefix query 
58# matches terms that reside on a large range of leaf nodes.
59#
60do_test fts3fault-3.0 {
61  sqlite3 db test.db
62  execsql { CREATE VIRTUAL TABLE t3 USING fts4; }
63  execsql { INSERT INTO t3(t3) VALUES('nodesize=50') }
64  execsql { BEGIN }
65  for {set i 0} {$i < 1000} {incr i} {
66    execsql { INSERT INTO t3 VALUES('aaa' || $i) }
67  }
68  execsql { COMMIT }
69} {}
70
71do_faultsim_test 3 -faults oom-transient -prep { 
72  sqlite3 db test.db
73  execsql { SELECT * FROM t3 WHERE t3 MATCH 'x' }
74} -body {
75  execsql { SELECT count(rowid) FROM t3 WHERE t3 MATCH 'aa*' }
76} -test {
77  faultsim_test_result {0 1000} 
78}
79
80do_test fts3fault-4.0 {
81  faultsim_delete_and_reopen
82  execsql { 
83    CREATE VIRTUAL TABLE t4 USING fts4; 
84    INSERT INTO t4 VALUES('The British Government called on');
85    INSERT INTO t4 VALUES('as pesetas then became much');
86  }
87} {}
88faultsim_save_and_close
89do_faultsim_test 4 -prep { 
90  faultsim_restore_and_reopen
91  execsql { SELECT content FROM t4 }
92} -body {
93  execsql { SELECT optimize(t4) FROM t4 LIMIT 1 }
94} -test {
95  faultsim_test_result {0 {{Index optimized}}}
96}
97
98do_test fts3fault-5.0 {
99  faultsim_delete_and_reopen
100  execsql { 
101    CREATE VIRTUAL TABLE t5 USING fts4; 
102    INSERT INTO t5 VALUES('The British Government called on');
103    INSERT INTO t5 VALUES('as pesetas then became much');
104  }
105} {}
106faultsim_save_and_close
107do_faultsim_test 5 -prep { 
108  faultsim_restore_and_reopen
109  execsql { 
110    BEGIN;
111      INSERT INTO t5 VALUES('influential in shaping his future outlook');
112      INSERT INTO t5 VALUES('might be acceptable to the British electorate');
113  }
114} -body {
115  execsql { SELECT rowid FROM t5 WHERE t5 MATCH 'british' }
116} -test {
117  faultsim_test_result {0 {1 4}}
118}
119
120do_test fts3fault-6.0 {
121  faultsim_delete_and_reopen
122  execsql { CREATE VIRTUAL TABLE t6 USING fts4 }
123} {}
124faultsim_save_and_close
125do_faultsim_test 6 -prep { 
126  faultsim_restore_and_reopen
127  execsql { SELECT rowid FROM t6 }
128} -body {
129  execsql { DROP TABLE t6 }
130} -test {
131  faultsim_test_result {0 {}}
132}
133
134# Test various malloc failures while processing FTS4 parameters.
135#
136do_faultsim_test 7.1 -prep { 
137  faultsim_delete_and_reopen
138} -body {
139  execsql { CREATE VIRTUAL TABLE t1 USING fts4(a, b, matchinfo=fts3) }
140} -test {
141  faultsim_test_result {0 {}}
142}
143do_faultsim_test 7.2 -prep { 
144  faultsim_delete_and_reopen
145} -body {
146  execsql { CREATE VIRTUAL TABLE t1 USING fts4(a, b, matchinfo=fs3) }
147} -test {
148  faultsim_test_result {1 {unrecognized matchinfo: fs3}} \
149                       {1 {vtable constructor failed: t1}}
150}
151do_faultsim_test 7.3 -prep { 
152  faultsim_delete_and_reopen
153} -body {
154  execsql { CREATE VIRTUAL TABLE t1 USING fts4(a, b, matchnfo=fts3) }
155} -test {
156  faultsim_test_result {1 {unrecognized parameter: matchnfo=fts3}} \
157                       {1 {vtable constructor failed: t1}}
158}
159
160}
161
162proc mit {blob} {
163  set scan(littleEndian) i*
164  set scan(bigEndian) I*
165  binary scan $blob $scan($::tcl_platform(byteOrder)) r
166  return $r
167}
168
169do_test 8.0 {
170  faultsim_delete_and_reopen
171  execsql { CREATE VIRTUAL TABLE t8 USING fts4 }
172  execsql "INSERT INTO t8 VALUES('a b c')"
173  execsql "INSERT INTO t8 VALUES('b b b')"
174  execsql "INSERT INTO t8 VALUES('[string repeat {c } 50000]')"
175  execsql "INSERT INTO t8 VALUES('d d d')"
176  execsql "INSERT INTO t8 VALUES('e e e')"
177  execsql "INSERT INTO t8(t8) VALUES('optimize')"
178  faultsim_save_and_close
179} {}
180
181do_faultsim_test 8.1 -prep { 
182  faultsim_restore_and_reopen
183  db func mit mit
184} -body {
185  execsql { SELECT mit(matchinfo(t8, 'x')) FROM t8 WHERE t8 MATCH 'a b c' }
186} -test {
187  faultsim_test_result {0 {{1 1 1 1 4 2 1 5 5}}}
188}
189do_faultsim_test 8.2 -faults oom-t* -prep { 
190  faultsim_restore_and_reopen
191  db func mit mit
192} -body {
193  execsql { SELECT mit(matchinfo(t8, 's')) FROM t8 WHERE t8 MATCH 'a b c' }
194} -test {
195  faultsim_test_result {0 3}
196}
197do_faultsim_test 8.3 -prep { 
198  faultsim_restore_and_reopen
199  db func mit mit
200} -body {
201  execsql { SELECT mit(matchinfo(t8, 'a')) FROM t8 WHERE t8 MATCH 'a b c' }
202} -test {
203  faultsim_test_result {0 10002}
204}
205do_faultsim_test 8.4 -prep { 
206  faultsim_restore_and_reopen
207  db func mit mit
208} -body {
209  execsql { SELECT mit(matchinfo(t8, 'l')) FROM t8 WHERE t8 MATCH 'a b c' }
210} -test {
211  faultsim_test_result {0 3}
212}
213
214do_test 9.0 {
215  faultsim_delete_and_reopen
216  execsql {
217    CREATE VIRTUAL TABLE t9 USING fts4(tokenize=porter);
218    INSERT INTO t9 VALUES(
219      'this record is used toooooooooooooooooooooooooooooooooooooo try to'
220    );
221    SELECT offsets(t9) FROM t9 WHERE t9 MATCH 'to*';
222  }
223  faultsim_save_and_close
224} {}
225do_faultsim_test 9.1 -prep {
226  faultsim_restore_and_reopen
227} -body {
228  execsql { SELECT offsets(t9) FROM t9 WHERE t9 MATCH 'to*' }
229} -test {
230  faultsim_test_result {0 {{0 0 20 39 0 0 64 2}}}
231}
232
233finish_test
234