Code

store, frontend: Added support for matching NULL attributes.
[sysdb.git] / t / unit / frontend / parser_test.c
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'",        -1,  1, CONNECTION_FETCH  },
56                 { "LIST",                -1,  1, CONNECTION_LIST   },
57                 { "LIST -- comment",     -1,  1, CONNECTION_LIST   },
58                 { "LIST;",               -1,  1, CONNECTION_LIST   },
59                 { "LIST; INVALID",        5,  1, CONNECTION_LIST   },
61                 { "LOOKUP hosts WHERE "
62                   "host.name = 'host'",  -1,  1, CONNECTION_LOOKUP },
63                 { "LOOKUP hosts WHERE NOT "
64                   "host.name = 'host'",  -1,  1, CONNECTION_LOOKUP },
65                 { "LOOKUP hosts WHERE "
66                   "host.name =~ 'p' AND "
67                   "service.name =~ 'p'", -1,  1, CONNECTION_LOOKUP },
68                 { "LOOKUP hosts WHERE NOT "
69                   "host.name =~ 'p' AND "
70                   "service.name =~ 'p'", -1,  1, CONNECTION_LOOKUP },
71                 { "LOOKUP hosts WHERE "
72                   "host.name =~ 'p' AND "
73                   "service.name =~ 'p' OR "
74                   "service.name =~ 'r'", -1,  1, CONNECTION_LOOKUP },
75                 { "LOOKUP hosts WHERE NOT "
76                   "host.name =~ 'p' AND "
77                   "service.name =~ 'p' OR "
78                   "service.name =~ 'r'", -1,  1, CONNECTION_LOOKUP },
80                 /* numeric constants */
81                 { "LOOKUP hosts WHERE "
82                   "attribute.foo = "
83                   "1234",                -1,  1, CONNECTION_LOOKUP },
84                 { "LOOKUP hosts WHERE "
85                   "attribute.foo != "
86                   "+234",                -1,  1, CONNECTION_LOOKUP },
87                 { "LOOKUP hosts WHERE "
88                   "attribute.foo < "
89                   "-234",                -1,  1, CONNECTION_LOOKUP },
90                 { "LOOKUP hosts WHERE "
91                   "attribute.foo > "
92                   "12.4",                -1,  1, CONNECTION_LOOKUP },
93                 { "LOOKUP hosts WHERE "
94                   "attribute.foo <= "
95                   "12.",                 -1,  1, CONNECTION_LOOKUP },
96                 { "LOOKUP hosts WHERE "
97                   "attribute.foo >= "
98                   ".4",                  -1,  1, CONNECTION_LOOKUP },
99                 { "LOOKUP hosts WHERE "
100                   "attribute.foo = "
101                   "+12e3",               -1,  1, CONNECTION_LOOKUP },
102                 { "LOOKUP hosts WHERE "
103                   "attribute.foo = "
104                   "+12e-3",              -1,  1, CONNECTION_LOOKUP },
105                 { "LOOKUP hosts WHERE "
106                   "attribute.foo = "
107                   "-12e+3",              -1,  1, CONNECTION_LOOKUP },
109                 /* NULL */
110                 { "LOOKUP hosts WHERE "
111                   "attribute.foo "
112                   "IS NULL",             -1,  1, CONNECTION_LOOKUP },
113                 { "LOOKUP hosts WHERE "
114                   "attribute.foo "
115                   "IS NOT NULL",         -1,  1, CONNECTION_LOOKUP },
116                 { "LOOKUP hosts WHERE "
117                   "NOT attribute.foo "
118                   "IS NULL",             -1,  1, CONNECTION_LOOKUP },
119                 { "LOOKUP hosts WHERE "
120                   "host.name IS NULL",   -1, -1, 0 },
121                 { "LOOKUP hosts WHERE "
122                   "service.name "
123                   "IS NULL",             -1, -1, 0 },
125                 /* invalid numeric constants */
126                 { "LOOKUP hosts WHERE "
127                   "attribute.foo = "
128                   "+-12e+3",             -1, -1, 0 },
129                 { "LOOKUP hosts WHERE "
130                   "attribute.foo = "
131                   "-12e-+3",             -1, -1, 0 },
132                 { "LOOKUP hosts WHERE "
133                   "attribute.foo = "
134                   "e+3",                 -1, -1, 0 },
135                 { "LOOKUP hosts WHERE "
136                   "attribute.foo = "
137                   "3e",                  -1, -1, 0 },
138                 /* following SQL standard, we don't support hex numbers */
139                 { "LOOKUP hosts WHERE "
140                   "attribute.foo = "
141                   "0x12",                -1, -1, 0 },
143                 /* comments */
144                 { "/* some comment */",  -1,  0, 0 },
145                 { "-- another comment",  -1,  0, 0 },
147                 /* syntax errors */
148                 { "INVALID",             -1, -1, 0 },
149                 { "FETCH host",          -1, -1, 0 },
150                 { "LIST; INVALID",        8, -1, 0 },
151                 { "/* some incomplete",  -1, -1, 0 },
153                 { "LOOKUP hosts",        -1, -1, 0 },
154                 { "LOOKUP foo WHERE "
155                   "host.name = 'host'",  -1, -1, 0 },
156         };
158         size_t i;
159         sdb_llist_t *check;
161         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
162                 sdb_object_t *obj;
163                 _Bool ok;
165                 check = sdb_fe_parse(golden_data[i].query, golden_data[i].len);
166                 if (golden_data[i].expected < 0)
167                         ok = check == 0;
168                 else
169                         ok = sdb_llist_len(check) == (size_t)golden_data[i].expected;
171                 fail_unless(ok, "sdb_fe_parse(%s) = %p (len: %zu); expected: %d",
172                                 golden_data[i].query, check, sdb_llist_len(check),
173                                 golden_data[i].expected);
175                 if (! check)
176                         continue;
178                 if ((! golden_data[i].expected_cmd)
179                                 || (golden_data[i].expected <= 0)) {
180                         sdb_llist_destroy(check);
181                         continue;
182                 }
184                 obj = sdb_llist_get(check, 0);
185                 fail_unless(SDB_CONN_NODE(obj)->cmd == golden_data[i].expected_cmd,
186                                 "sdb_fe_parse(%s)->cmd = %i; expected: %d",
187                                 golden_data[i].query, SDB_CONN_NODE(obj)->cmd,
188                                 golden_data[i].expected_cmd);
189                 sdb_object_deref(obj);
190                 sdb_llist_destroy(check);
191         }
193 END_TEST
195 START_TEST(test_parse_matcher)
197         struct {
198                 const char *expr;
199                 int len;
200                 int expected;
201         } golden_data[] = {
202                 /* empty expressions */
203                 { NULL,                             -1, -1 },
204                 { "",                               -1, -1 },
206                 /* valid expressions */
207                 { "host.name = 'localhost'",        -1,  MATCHER_NAME },
208                 { "host.name != 'localhost'",       -1,  MATCHER_NOT },
209                 { "host.name =~ 'host'",            -1,  MATCHER_NAME },
210                 { "host.name !~ 'host'",            -1,  MATCHER_NOT },
211                 { "host.name = 'localhost' -- foo", -1,  MATCHER_NAME },
212                 { "host.name = 'host' <garbage>",   18,  MATCHER_NAME },
213                 /* match hosts by service */
214                 { "service.name = 'name'",          -1,  MATCHER_NAME },
215                 { "service.name != 'name'",         -1,  MATCHER_NOT },
216                 { "service.name =~ 'pattern'",      -1,  MATCHER_NAME },
217                 { "service.name !~ 'pattern'",      -1,  MATCHER_NOT },
218                 /* match hosts by attribute */
219                 { "attribute.name = 'name'",        -1,  MATCHER_NAME },
220                 { "attribute.name != 'name'",       -1,  MATCHER_NOT },
221                 { "attribute.name =~ 'pattern'",    -1,  MATCHER_NAME },
222                 { "attribute.name !~ 'pattern'",    -1,  MATCHER_NOT },
223                 /* composite expressions */
224                 { "host.name =~ 'pattern' AND "
225                   "service.name =~ 'pattern'",      -1,  MATCHER_AND },
226                 { "host.name =~ 'pattern' OR "
227                   "service.name =~ 'pattern'",      -1,  MATCHER_OR },
228                 { "NOT host.name = 'host'",         -1,  MATCHER_NOT },
229                 /* numeric expressions */
230                 { "attribute.foo < 123",            -1,  MATCHER_LT },
231                 { "attribute.foo <= 123",           -1,  MATCHER_LE },
232                 { "attribute.foo = 123",            -1,  MATCHER_EQ },
233                 { "attribute.foo >= 123",           -1,  MATCHER_GE },
234                 { "attribute.foo > 123",            -1,  MATCHER_GT },
235                 /* NULL; while this is an implementation detail,
236                  * IS NULL currently maps to an equality matcher */
237                 { "attribute.foo IS NULL",          -1,  MATCHER_ISNULL },
238                 { "attribute.foo IS NOT NULL",      -1,  MATCHER_NOT },
240                 /* check operator precedence */
241                 { "host.name = 'name' OR "
242                   "service.name = 'name' AND "
243                   "attribute.name = 'name' OR "
244                   "attribute.foo = 'bar'",          -1,  MATCHER_OR },
245                 { "host.name = 'name' AND "
246                   "service.name = 'name' AND "
247                   "attribute.name = 'name' OR "
248                   "attribute.foo = 'bar'",          -1,  MATCHER_OR },
249                 { "host.name = 'name' AND "
250                   "service.name = 'name' OR "
251                   "attribute.name = 'name' AND "
252                   "attribute.foo = 'bar'",          -1,  MATCHER_OR },
253                 { "(host.name = 'name' OR "
254                   "service.name = 'name') AND "
255                   "(attribute.name = 'name' OR "
256                   "attribute.foo = 'bar')",         -1,  MATCHER_AND },
257                 { "NOT host.name = 'name' OR "
258                   "service.name = 'name'",          -1,  MATCHER_OR },
259                 { "NOT host.name = 'name' OR "
260                   "NOT service.name = 'name'",      -1,  MATCHER_OR },
261                 { "NOT (host.name = 'name' OR "
262                   "NOT service.name = 'name')",     -1,  MATCHER_NOT },
264                 /* syntax errors */
265                 { "LIST",                           -1, -1 },
266                 { "foo &^ bar",                     -1, -1 },
267         };
269         size_t i;
271         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
272                 sdb_store_matcher_t *m;
273                 m = sdb_fe_parse_matcher(golden_data[i].expr, golden_data[i].len);
275                 if (golden_data[i].expected < 0) {
276                         fail_unless(m == NULL,
277                                         "sdb_fe_parse_matcher(%s) = %p; expected: NULL",
278                                         golden_data[i].expr, m);
279                         continue;
280                 }
282                 fail_unless(m != NULL, "sdb_fe_parse_matcher(%s) = NULL; "
283                                 "expected: <matcher>", golden_data[i].expr);
284                 fail_unless(M(m)->type == golden_data[i].expected,
285                                 "sdb_fe_parse_matcher(%s) returned matcher of type %d; "
286                                 "expected: %d", golden_data[i].expr, M(m)->type,
287                                 golden_data[i].expected);
289                 sdb_object_deref(SDB_OBJ(m));
290         }
292 END_TEST
294 Suite *
295 fe_parser_suite(void)
297         Suite *s = suite_create("frontend::parser");
298         TCase *tc;
300         tc = tcase_create("core");
301         tcase_add_test(tc, test_parse);
302         tcase_add_test(tc, test_parse_matcher);
303         suite_add_tcase(s, tc);
305         return s;
306 } /* util_parser_suite */
308 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */