Fix masking of bits in serialised query
[xapian.git] / xapian-applications / omega / omegatest
blob99f708a5703b08e07a84cc4b062f6ea5411fa869
1 #!/bin/sh
2 # omegatest: Test omega CGI
4 # Copyright (C) 2015,2016 Olly Betts
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; either version 2 of the
9 # License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19 # USA
20 set -e
22 : ${OMEGA=./omega}
23 : ${SCRIPTINDEX=./scriptindex}
25 # Suppress HTTP Content-Type header.
26 SERVER_PROTOCOL=INCLUDED
27 export SERVER_PROTOCOL
29 # Set up an empty database.
30 TEST_DB=test-db
31 rm -rf "$TEST_DB"
32 echo 'inmemory' > "$TEST_DB"
34 # Simple template which just shows the parsed query.
35 TEST_TEMPLATE=test-template
36 printf '$querydescription' > "$TEST_TEMPLATE"
38 TEST_INDEXSCRIPT=test-indexscript
40 OMEGA_CONFIG_FILE=test-omega.conf
41 export OMEGA_CONFIG_FILE
42 cat > "$OMEGA_CONFIG_FILE" <<__END__
43 database_dir .
44 template_dir .
45 log_dir tmplog
46 default_template $TEST_TEMPLATE
47 default_db $TEST_DB
48 __END__
50 failed=0
52 testcase() {
53 expected=$1
54 shift
55 output=`$FAKETIME ${FAKE_NOW+"$FAKE_NOW"} "$OMEGA" "$@"`
56 if [ "$output" != "$expected" ] ; then
57 echo "$OMEGA $@:"
58 echo " expected: «$expected»"
59 echo " received: «$output»"
60 failed=`expr $failed + 1`
64 qtestcase() {
65 expected="Query($1)"
66 shift
67 testcase "$expected" "$@"
70 FAKETIME=
71 unset FAKE_NOW
73 # Test a few simple things.
74 qtestcase 'Zsimpl@1' P=simple
75 qtestcase '(chocolate@1 FILTER Tconfectionary/fudge)' P=Chocolate B=Tconfectionary/fudge
77 # Test date value ranges.
78 qtestcase 'VALUE_RANGE 0 2 ~' DATEVALUE=0 START=2000
79 qtestcase 'VALUE_RANGE 0 2 ~' DATEVALUE=0 START=200001
80 qtestcase 'VALUE_RANGE 0 2 ~' DATEVALUE=0 START=20000101
81 qtestcase 'VALUE_LE 1 1~' DATEVALUE=1 END=1999
82 qtestcase 'VALUE_LE 1 1~' DATEVALUE=1 END=199912
83 qtestcase 'VALUE_LE 1 1~' DATEVALUE=1 END=19991231
84 qtestcase 'VALUE_RANGE 2 201 ~' DATEVALUE=2 START=2010
85 qtestcase 'VALUE_RANGE 2 201 ~' DATEVALUE=2 START=201001
86 qtestcase 'VALUE_RANGE 2 201 ~' DATEVALUE=2 START=20100101
87 qtestcase 'VALUE_LE 3 198~' DATEVALUE=3 END=1989
88 qtestcase 'VALUE_LE 3 198~' DATEVALUE=3 END=198912
89 qtestcase 'VALUE_LE 3 198~' DATEVALUE=3 END=19891231
90 qtestcase 'VALUE_RANGE 4 1974 ~' DATEVALUE=4 START=1974
91 qtestcase 'VALUE_RANGE 4 1974 ~' DATEVALUE=4 START=197401
92 qtestcase 'VALUE_RANGE 4 1974 ~' DATEVALUE=4 START=19740101
93 qtestcase 'VALUE_LE 5 1974~' DATEVALUE=5 END=1974
94 qtestcase 'VALUE_LE 5 1974~' DATEVALUE=5 END=197412
95 qtestcase 'VALUE_LE 5 1974~' DATEVALUE=5 END=19741231
96 qtestcase 'VALUE_RANGE 6 20151 ~' DATEVALUE=6 START=201510
97 qtestcase 'VALUE_RANGE 6 20151 ~' DATEVALUE=6 START=20151001
98 qtestcase 'VALUE_LE 7 19870~' DATEVALUE=7 END=198709
99 qtestcase 'VALUE_LE 7 19870~' DATEVALUE=7 END=19870930
100 qtestcase 'VALUE_RANGE 8 201512 ~' DATEVALUE=8 START=201512
101 qtestcase 'VALUE_RANGE 8 201512 ~' DATEVALUE=8 START=20151201
102 qtestcase 'VALUE_LE 9 201511~' DATEVALUE=9 END=201511
103 qtestcase 'VALUE_LE 9 201511~' DATEVALUE=9 END=20151130
104 qtestcase 'VALUE_RANGE 10 2015021 ~' DATEVALUE=10 START=20150210
105 qtestcase 'VALUE_RANGE 10 2000022 ~' DATEVALUE=10 START=20000220
106 qtestcase 'VALUE_LE 11 19840401~' DATEVALUE=11 END=19840401
107 qtestcase 'VALUE_LE 11 19881128~' DATEVALUE=11 END=19881128
109 # Leap year tests:
110 qtestcase 'VALUE_LE 1 201502~' DATEVALUE=1 END=20150228
111 qtestcase 'VALUE_LE 1 198802~' DATEVALUE=1 END=19880229
112 qtestcase 'VALUE_LE 1 19880228~' DATEVALUE=1 END=19880228
113 qtestcase 'VALUE_LE 1 200002~' DATEVALUE=1 END=20000229
114 qtestcase 'VALUE_LE 1 20000228~' DATEVALUE=1 END=20000228
115 # FIXME: These two currently require 64-bit time_t:
116 #testcase 'VALUE_LE 1 190002~' DATEVALUE=1 END=19000228
117 #testcase 'VALUE_LE 1 210002~' DATEVALUE=1 END=21000228
119 # Month starts and ends:
120 qtestcase 'VALUE_RANGE 0 2015 201501~' DATEVALUE=0 START=20150101 END=20150131
121 qtestcase 'VALUE_RANGE 0 2015 20150130~' DATEVALUE=0 START=20150101 END=20150130
122 qtestcase 'VALUE_RANGE 0 201502 201502~' DATEVALUE=0 START=20150201 END=20150228
123 qtestcase 'VALUE_RANGE 0 201502 20150227~' DATEVALUE=0 START=20150201 END=20150227
124 qtestcase 'VALUE_RANGE 0 201503 201503~' DATEVALUE=0 START=20150301 END=20150331
125 qtestcase 'VALUE_RANGE 0 201503 20150330~' DATEVALUE=0 START=20150301 END=20150330
126 qtestcase 'VALUE_RANGE 0 201504 201504~' DATEVALUE=0 START=20150401 END=20150430
127 qtestcase 'VALUE_RANGE 0 201504 2015042~' DATEVALUE=0 START=20150401 END=20150429
128 qtestcase 'VALUE_RANGE 0 201505 201505~' DATEVALUE=0 START=20150501 END=20150531
129 qtestcase 'VALUE_RANGE 0 201505 20150530~' DATEVALUE=0 START=20150501 END=20150530
130 qtestcase 'VALUE_RANGE 0 201506 201506~' DATEVALUE=0 START=20150601 END=20150630
131 qtestcase 'VALUE_RANGE 0 201506 2015062~' DATEVALUE=0 START=20150601 END=20150629
132 qtestcase 'VALUE_RANGE 0 201507 201507~' DATEVALUE=0 START=20150701 END=20150731
133 qtestcase 'VALUE_RANGE 0 201507 20150730~' DATEVALUE=0 START=20150701 END=20150730
134 qtestcase 'VALUE_RANGE 0 201508 201508~' DATEVALUE=0 START=20150801 END=20150831
135 qtestcase 'VALUE_RANGE 0 201508 20150830~' DATEVALUE=0 START=20150801 END=20150830
136 qtestcase 'VALUE_RANGE 0 201509 20150~' DATEVALUE=0 START=20150901 END=20150930
137 qtestcase 'VALUE_RANGE 0 201509 2015092~' DATEVALUE=0 START=20150901 END=20150929
138 qtestcase 'VALUE_RANGE 0 20151 201510~' DATEVALUE=0 START=20151001 END=20151031
139 qtestcase 'VALUE_RANGE 0 20151 20151030~' DATEVALUE=0 START=20151001 END=20151030
140 qtestcase 'VALUE_RANGE 0 201511 201511~' DATEVALUE=0 START=20151101 END=20151130
141 qtestcase 'VALUE_RANGE 0 201511 2015112~' DATEVALUE=0 START=20151101 END=20151129
142 qtestcase 'VALUE_RANGE 0 201512 2015~' DATEVALUE=0 START=20151201 END=20151231
143 qtestcase 'VALUE_RANGE 0 201512 20151230~' DATEVALUE=0 START=20151201 END=20151230
145 # Forward spans:
146 qtestcase 'VALUE_RANGE 0 20151104 20151106~' DATEVALUE=0 START=20151104 SPAN=3
147 qtestcase 'VALUE_RANGE 0 20141104 20151103~' DATEVALUE=0 START=20141104 SPAN=365
149 # Backward spans:
150 qtestcase 'VALUE_RANGE 0 20151104 20151106~' DATEVALUE=0 END=20151106 SPAN=3
151 qtestcase 'VALUE_RANGE 0 20141104 20151103~' DATEVALUE=0 END=20151103 SPAN=365
153 # Check that if START, END and SPAN are all passed, START is ignored:
154 qtestcase 'VALUE_RANGE 0 20151104 20151106~' DATEVALUE=0 START=19700101 END=20151106 SPAN=3
156 # Check combining of filter terms:
157 qtestcase '(Horg AND Len)' B=Len B=Horg
158 qtestcase '(Len OR Lde)' B=Len B=Lde
159 qtestcase '((Horg OR Hcom) AND (Len OR Lfr))' B=Len B=Lfr B=Horg B=Hcom
161 # Check combining of filter terms with filter_op set:
162 printf '$setmap{nonexclusiveprefix,L,1,XAND,1}$setmap{boolprefix,lang,L,and,XAND,host,H,year,Y}$querydescription' > "$TEST_TEMPLATE"
163 qtestcase 'Len' B=Len
164 qtestcase '0 * Len' P=lang:en
165 qtestcase 'XANDtest' B=XANDtest
166 qtestcase '0 * XANDtest' P=and:test
167 qtestcase '(Len AND XANDtest)' B=Len B=XANDtest
168 qtestcase '0 * (XANDtest AND Len)' P='lang:en and:test'
169 qtestcase '(Len AND Lde)' B=Len B=Lde
170 qtestcase '0 * (Len AND Lde)' P='lang:en lang:de'
171 qtestcase '((Horg OR Hcom) AND (Len AND Lfr))' B=Len B=Lfr B=Horg B=Hcom
172 qtestcase '0 * ((Horg OR Hcom) AND (Len AND Lfr))' P='lang:en lang:fr host:org host:com'
173 qtestcase '((XANDa AND XANDb AND XANDc) AND (Y1998 OR Y2001))' B=Y1998 B=Y2001 B=XANDa B=XANDb B=XANDc
174 qtestcase '0 * (((XANDa AND XANDb) AND XANDc) AND (Y1998 OR Y2001))' P='year:1998 year:2001 and:a and:b and:c'
176 # Check combining of filters around CGI parameter 'N':
177 qtestcase '(Ztruth@1 AND_NOT Epdf)' P=truth N=Epdf
178 qtestcase '(Ztruth@1 AND_NOT (Ehtm OR Epdf))' P=truth N=Epdf N=Ehtm
179 qtestcase '(Ztruth@1 AND_NOT (Ehtm OR Epdf OR Lde OR Lfr))' P=truth N=Lfr N=Epdf N=Ehtm N=Lde
180 qtestcase '((Ztruth@1 FILTER (Lfr AND Lzh)) AND_NOT (Lde OR Len))' P=truth N=Lde N=Len B=Lfr B=Lzh
181 qtestcase '((Ztruth@1 FILTER Lfr) AND_NOT (Ehtm OR Epdf))' P=truth N=Epdf N=Ehtm B=Lfr
182 qtestcase '(<alldocuments> AND_NOT (Len OR Lfr))' N=Lfr N=Len
183 qtestcase '(VALUE_RANGE 0 2015 201501~ AND_NOT Len)' DATEVALUE=0 START=20150101 END=20150131 N=Len
185 # If faketime is available and works, test a range back from now.
186 if [ "`faketime '1980-12-08' date +%Y 2>&1`" = 1980 ] ; then
187 TZ=UTC
188 export TZ
189 FAKETIME=faketime
190 FAKE_NOW='2015-11-28 06:07:08'
191 qtestcase 'VALUE_RANGE 0 20151127060709 20151128060708' DATEVALUE=0 SPAN=1
192 FAKETIME=
193 unset FAKE_NOW
194 else
195 if [ $? = 127 ] ; then
196 echo "Skipping testcases which need 'faketime' tool installed"
197 else
198 echo "Skipping testcases which need 'faketime' tool - it's installed but doesn't work"
202 # Feature tests for $contains.
203 printf '$contains{$cgi{a},$cgi{b}}' > "$TEST_TEMPLATE"
204 testcase '6' P=text a=fish b="Hello fish"
205 testcase '' P=text a="Example" b="random"
207 NEWLINE='
210 # Feature tests for $match.
211 printf '$match{$cgi{a},$cgi{b},$cgi{c}}' > "$TEST_TEMPLATE"
212 testcase 'true' P=text a="ab*c+" b="ac"
213 testcase '' P=text a="acb" b="abc"
214 testcase 'true' P=text a="[A-Z]bcD" b="abcd" c="i"
215 testcase '' P=text a="[A-Z]bcD" b="abcd"
216 testcase 'true' P=text a="^abc$" b="${NEWLINE}abc${NEWLINE}def" c="m"
217 testcase '' P=text a="^abc$" b="${NEWLINE}abc${NEWLINE}def"
218 testcase 'true' P=text a="abc." b="abc${NEWLINE}" c="s"
219 testcase '' P=text a="abc." b="abc${NEWLINE}"
220 testcase 'true' P=text a=" ABC #test_comment " b="ABC" c="x"
221 testcase '' P=text a=" ABC #test_comment " b="ABC"
223 # Feature tests for $terms.
224 printf 'text : index\nhost : boolean=H\nfoo : boolean=XFOO' > "$TEST_INDEXSCRIPT"
225 rm -f "$TEST_DB"
226 $SCRIPTINDEX "$TEST_DB" "$TEST_INDEXSCRIPT" > /dev/null <<'END'
227 text=This is some text.
228 host=example.org
229 foo=bar
231 printf '$hitlist{$list{$if{$eq{$cgi{prefix},null},$terms,$terms{$cgi{prefix}}},|}}' > "$TEST_TEMPLATE"
232 testcase 'Ztext' P=text B=Hexample.org B=Hexample.com prefix=null
233 testcase 'Hexample.org|Ztext' P=text B=Hexample.org B=Hexample.com prefix=
234 testcase 'Hexample.org' P=text B=Hexample.org B=Hexample.com prefix=H
235 testcase 'Ztext' P=text B=Hexample.org B=Hexample.com prefix=Z
236 testcase '' P=text B=Hexample.org B=Hexample.com prefix=E
238 rm "$OMEGA_CONFIG_FILE" "$TEST_INDEXSCRIPT" "$TEST_TEMPLATE"
239 rm -rf "$TEST_DB"
240 if [ "$failed" = 0 ] ; then
241 exit 0
243 echo "Failed $failed test(s)"
244 exit 1