1 /*
2 * SysDB - t/unit/frontend/parser_test.c
3 * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #include "frontend/connection.h"
29 #include "frontend/parser.h"
30 #include "core/store-private.h"
31 #include "core/object.h"
32 #include "libsysdb_test.h"
34 #include <check.h>
36 /*
37 * tests
38 */
40 START_TEST(test_parse)
41 {
42 struct {
43 const char *query;
44 int len;
45 int expected;
46 sdb_conn_state_t expected_cmd;
47 } golden_data[] = {
48 /* empty commands */
49 { NULL, -1, -1, 0 },
50 { "", -1, 0, 0 },
51 { ";", -1, 0, 0 },
52 { ";;", -1, 0, 0 },
54 /* valid commands */
55 { "FETCH host 'host'", -1, 1, CONNECTION_FETCH },
56 { "FETCH host 'host' FILTER "
57 "host = 'host'", -1, 1, CONNECTION_FETCH },
59 { "LIST hosts", -1, 1, CONNECTION_LIST },
60 { "LIST hosts -- foo", -1, 1, CONNECTION_LIST },
61 { "LIST hosts;", -1, 1, CONNECTION_LIST },
62 { "LIST hosts; INVALID", 11, 1, CONNECTION_LIST },
63 { "LIST hosts FILTER "
64 "host = 'host'", -1, 1, CONNECTION_LIST },
66 { "LOOKUP hosts", -1, 1, CONNECTION_LOOKUP },
67 { "LOOKUP hosts MATCHING "
68 "host = 'host'", -1, 1, CONNECTION_LOOKUP },
69 { "LOOKUP hosts MATCHING NOT "
70 "host = 'host'", -1, 1, CONNECTION_LOOKUP },
71 { "LOOKUP hosts MATCHING "
72 "host =~ 'p' AND "
73 "service =~ 'p'", -1, 1, CONNECTION_LOOKUP },
74 { "LOOKUP hosts MATCHING NOT "
75 "host =~ 'p' AND "
76 "service =~ 'p'", -1, 1, CONNECTION_LOOKUP },
77 { "LOOKUP hosts MATCHING "
78 "host =~ 'p' AND "
79 "service =~ 'p' OR "
80 "service =~ 'r'", -1, 1, CONNECTION_LOOKUP },
81 { "LOOKUP hosts MATCHING NOT "
82 "host =~ 'p' AND "
83 "service =~ 'p' OR "
84 "service =~ 'r'", -1, 1, CONNECTION_LOOKUP },
85 { "LOOKUP hosts MATCHING "
86 "host =~ 'p' "
87 "FILTER :age > 1D", -1, 1, CONNECTION_LOOKUP },
88 { "LOOKUP hosts MATCHING "
89 "host =~ 'p' "
90 "FILTER :age > 1D AND "
91 ":interval < 240s" , -1, 1, CONNECTION_LOOKUP },
92 { "LOOKUP hosts MATCHING "
93 "host =~ 'p' "
94 "FILTER NOT :age>1D", -1, 1, CONNECTION_LOOKUP },
95 { "LOOKUP hosts MATCHING "
96 "host =~ 'p' "
97 "FILTER :age>"
98 ":interval", -1, 1, CONNECTION_LOOKUP },
100 /* numeric constants */
101 { "LOOKUP hosts MATCHING "
102 "attribute.foo = "
103 "1234", -1, 1, CONNECTION_LOOKUP },
104 { "LOOKUP hosts MATCHING "
105 "attribute.foo != "
106 "+234", -1, 1, CONNECTION_LOOKUP },
107 { "LOOKUP hosts MATCHING "
108 "attribute.foo < "
109 "-234", -1, 1, CONNECTION_LOOKUP },
110 { "LOOKUP hosts MATCHING "
111 "attribute.foo > "
112 "12.4", -1, 1, CONNECTION_LOOKUP },
113 { "LOOKUP hosts MATCHING "
114 "attribute.foo <= "
115 "12. + .3", -1, 1, CONNECTION_LOOKUP },
116 { "LOOKUP hosts MATCHING "
117 "attribute.foo >= "
118 ".4", -1, 1, CONNECTION_LOOKUP },
119 { "LOOKUP hosts MATCHING "
120 "attribute.foo = "
121 "+12e3", -1, 1, CONNECTION_LOOKUP },
122 { "LOOKUP hosts MATCHING "
123 "attribute.foo = "
124 "+12e-3", -1, 1, CONNECTION_LOOKUP },
125 { "LOOKUP hosts MATCHING "
126 "attribute.foo = "
127 "-12e+3", -1, 1, CONNECTION_LOOKUP },
129 /* date, time, interval constants */
130 { "LOOKUP hosts MATCHING "
131 "attribute.foo = "
132 "1 Y 42D", -1, 1, CONNECTION_LOOKUP },
133 { "LOOKUP hosts MATCHING "
134 "attribute.foo = "
135 "1s 42D", -1, 1, CONNECTION_LOOKUP },
136 /*
137 * TODO: Something like 1Y42D should work as well but it doesn't since
138 * the scanner will tokenize it into {digit}{identifier} :-/
139 *
140 { "LOOKUP hosts MATCHING "
141 "attribute.foo = "
142 "1Y42D", -1, 1, CONNECTION_LOOKUP },
143 */
145 /* NULL */
146 { "LOOKUP hosts MATCHING "
147 "attribute.foo "
148 "IS NULL", -1, 1, CONNECTION_LOOKUP },
149 { "LOOKUP hosts MATCHING "
150 "attribute.foo "
151 "IS NOT NULL", -1, 1, CONNECTION_LOOKUP },
152 { "LOOKUP hosts MATCHING "
153 "NOT attribute.foo "
154 "IS NULL", -1, 1, CONNECTION_LOOKUP },
155 { "LOOKUP hosts MATCHING "
156 "host IS NULL", -1, -1, 0 },
157 { "LOOKUP hosts MATCHING "
158 "service IS NULL", -1, -1, 0 },
160 /* invalid numeric constants */
161 { "LOOKUP hosts MATCHING "
162 "attribute.foo = "
163 "+-12e+3", -1, -1, 0 },
164 { "LOOKUP hosts MATCHING "
165 "attribute.foo = "
166 "-12e-+3", -1, -1, 0 },
167 { "LOOKUP hosts MATCHING "
168 "attribute.foo = "
169 "e+3", -1, -1, 0 },
170 { "LOOKUP hosts MATCHING "
171 "attribute.foo = "
172 "3e", -1, -1, 0 },
173 /* following SQL standard, we don't support hex numbers */
174 { "LOOKUP hosts MATCHING "
175 "attribute.foo = "
176 "0x12", -1, -1, 0 },
178 /* invalid expressions */
179 { "LOOKUP hosts MATCHING "
180 "attribute.foo = "
181 "1.23 + 'foo'", -1, -1, 0 },
183 /* comments */
184 { "/* some comment */", -1, 0, 0 },
185 { "-- another comment", -1, 0, 0 },
187 /* syntax errors */
188 { "INVALID", -1, -1, 0 },
189 { "FETCH host", -1, -1, 0 },
190 { "FETCH 'host'", -1, -1, 0 },
191 { "LIST hosts; INVALID", -1, -1, 0 },
192 { "/* some incomplete", -1, -1, 0 },
194 { "LIST", -1, -1, 0 },
195 { "LIST foo", -1, -1, 0 },
196 { "LIST hosts MATCHING "
197 "host = 'host'", -1, -1, 0 },
198 { "LIST foo FILTER "
199 "host = 'host'", -1, -1, 0 },
200 { "FETCH host 'host' MATCHING "
201 "host = 'host'", -1, -1, 0 },
202 { "FETCH foo 'host'", -1, -1, 0 },
203 { "FETCH foo 'host' FILTER "
204 "host = 'host'", -1, -1, 0 },
206 { "LOOKUP foo", -1, -1, 0 },
207 { "LOOKUP foo MATCHING "
208 "host = 'host'", -1, -1, 0 },
209 { "LOOKUP foo FILTER "
210 "host = 'host'", -1, -1, 0 },
211 { "LOOKUP foo MATCHING "
212 "host = 'host' FILTER "
213 "host = 'host'", -1, -1, 0 },
214 };
216 size_t i;
217 sdb_llist_t *check;
219 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
220 sdb_object_t *obj;
221 _Bool ok;
223 check = sdb_fe_parse(golden_data[i].query, golden_data[i].len);
224 if (golden_data[i].expected < 0)
225 ok = check == 0;
226 else
227 ok = sdb_llist_len(check) == (size_t)golden_data[i].expected;
229 fail_unless(ok, "sdb_fe_parse(%s) = %p (len: %zu); expected: %d",
230 golden_data[i].query, check, sdb_llist_len(check),
231 golden_data[i].expected);
233 if (! check)
234 continue;
236 if ((! golden_data[i].expected_cmd)
237 || (golden_data[i].expected <= 0)) {
238 sdb_llist_destroy(check);
239 continue;
240 }
242 obj = sdb_llist_get(check, 0);
243 fail_unless(SDB_CONN_NODE(obj)->cmd == golden_data[i].expected_cmd,
244 "sdb_fe_parse(%s)->cmd = %i; expected: %d",
245 golden_data[i].query, SDB_CONN_NODE(obj)->cmd,
246 golden_data[i].expected_cmd);
247 sdb_object_deref(obj);
248 sdb_llist_destroy(check);
249 }
250 }
251 END_TEST
253 START_TEST(test_parse_matcher)
254 {
255 struct {
256 const char *expr;
257 int len;
258 int expected;
259 } golden_data[] = {
260 /* empty expressions */
261 { NULL, -1, -1 },
262 { "", -1, -1 },
264 /* valid expressions */
265 { "host = 'localhost'", -1, MATCHER_NAME },
266 { "host != 'localhost'", -1, MATCHER_NOT },
267 { "host =~ 'host'", -1, MATCHER_NAME },
268 { "host !~ 'host'", -1, MATCHER_NOT },
269 { "host = 'localhost' -- foo", -1, MATCHER_NAME },
270 { "host = 'host' <garbage>", 13, MATCHER_NAME },
271 /* match hosts by service */
272 { "service = 'name'", -1, MATCHER_NAME },
273 { "service != 'name'", -1, MATCHER_NOT },
274 { "service =~ 'pattern'", -1, MATCHER_NAME },
275 { "service !~ 'pattern'", -1, MATCHER_NOT },
276 /* match hosts by attribute */
277 { "attribute = 'name'", -1, MATCHER_NAME },
278 { "attribute != 'name'", -1, MATCHER_NOT },
279 { "attribute =~ 'pattern'", -1, MATCHER_NAME },
280 { "attribute !~ 'pattern'", -1, MATCHER_NOT },
281 /* composite expressions */
282 { "host =~ 'pattern' AND "
283 "service =~ 'pattern'", -1, MATCHER_AND },
284 { "host =~ 'pattern' OR "
285 "service =~ 'pattern'", -1, MATCHER_OR },
286 { "NOT host = 'host'", -1, MATCHER_NOT },
287 /* numeric expressions */
288 { "attribute.foo < 123", -1, MATCHER_LT },
289 { "attribute.foo <= 123", -1, MATCHER_LE },
290 { "attribute.foo = 123", -1, MATCHER_EQ },
291 { "attribute.foo >= 123", -1, MATCHER_GE },
292 { "attribute.foo > 123", -1, MATCHER_GT },
293 /* NULL; while this is an implementation detail,
294 * IS NULL currently maps to an equality matcher */
295 { "attribute.foo IS NULL", -1, MATCHER_ISNULL },
296 { "attribute.foo IS NOT NULL", -1, MATCHER_NOT },
298 /* object field matchers */
299 { ":last_update < 10s", -1, MATCHER_LT },
300 { ":AGE <= 1m", -1, MATCHER_LE },
301 { ":interval = 10h", -1, MATCHER_EQ },
302 { ":Last_Update >= 24D", -1, MATCHER_GE },
303 { ":age > 1M", -1, MATCHER_GT },
304 { ":age != 20Y", -1, MATCHER_NOT },
305 { ":backend != 'be'", -1, MATCHER_NOT },
306 { ":age <= 2 * :interval", -1, MATCHER_LE },
308 /* check operator precedence */
309 { "host = 'name' OR "
310 "service = 'name' AND "
311 "attribute = 'name' OR "
312 "attribute.foo = 'bar'", -1, MATCHER_OR },
313 { "host = 'name' AND "
314 "service = 'name' AND "
315 "attribute = 'name' OR "
316 "attribute.foo = 'bar'", -1, MATCHER_OR },
317 { "host = 'name' AND "
318 "service = 'name' OR "
319 "attribute = 'name' AND "
320 "attribute.foo = 'bar'", -1, MATCHER_OR },
321 { "(host = 'name' OR "
322 "service = 'name') AND "
323 "(attribute = 'name' OR "
324 "attribute.foo = 'bar')", -1, MATCHER_AND },
325 { "NOT host = 'name' OR "
326 "service = 'name'", -1, MATCHER_OR },
327 { "NOT host = 'name' OR "
328 "NOT service = 'name'", -1, MATCHER_OR },
329 { "NOT (host = 'name' OR "
330 "NOT service = 'name')", -1, MATCHER_NOT },
332 /* syntax errors */
333 { "LIST", -1, -1 },
334 { "foo &^ bar", -1, -1 },
335 };
337 size_t i;
339 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
340 sdb_store_matcher_t *m;
341 m = sdb_fe_parse_matcher(golden_data[i].expr, golden_data[i].len);
343 if (golden_data[i].expected < 0) {
344 fail_unless(m == NULL,
345 "sdb_fe_parse_matcher(%s) = %p; expected: NULL",
346 golden_data[i].expr, m);
347 continue;
348 }
350 fail_unless(m != NULL, "sdb_fe_parse_matcher(%s) = NULL; "
351 "expected: <matcher>", golden_data[i].expr);
352 fail_unless(M(m)->type == golden_data[i].expected,
353 "sdb_fe_parse_matcher(%s) returned matcher of type %d; "
354 "expected: %d", golden_data[i].expr, M(m)->type,
355 golden_data[i].expected);
357 sdb_object_deref(SDB_OBJ(m));
358 }
359 }
360 END_TEST
362 Suite *
363 fe_parser_suite(void)
364 {
365 Suite *s = suite_create("frontend::parser");
366 TCase *tc;
368 tc = tcase_create("core");
369 tcase_add_test(tc, test_parse);
370 tcase_add_test(tc, test_parse_matcher);
371 suite_add_tcase(s, tc);
373 return s;
374 } /* util_parser_suite */
376 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */