Code

store/frontend: Added support for integer and float comparison.
[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                 /* invalid numeric constants */
110                 { "LOOKUP hosts WHERE "
111                   "attribute.foo = "
112                   "+-12e+3",             -1, -1, 0 },
113                 { "LOOKUP hosts WHERE "
114                   "attribute.foo = "
115                   "-12e-+3",             -1, -1, 0 },
116                 { "LOOKUP hosts WHERE "
117                   "attribute.foo = "
118                   "e+3",                 -1, -1, 0 },
119                 { "LOOKUP hosts WHERE "
120                   "attribute.foo = "
121                   "3e",                  -1, -1, 0 },
122                 /* following SQL standard, we don't support hex numbers */
123                 { "LOOKUP hosts WHERE "
124                   "attribute.foo = "
125                   "0x12",                -1, -1, 0 },
127                 /* comments */
128                 { "/* some comment */",  -1,  0, 0 },
129                 { "-- another comment",  -1,  0, 0 },
131                 /* syntax errors */
132                 { "INVALID",             -1, -1, 0 },
133                 { "FETCH host",          -1, -1, 0 },
134                 { "LIST; INVALID",        8, -1, 0 },
135                 { "/* some incomplete",  -1, -1, 0 },
137                 { "LOOKUP hosts",        -1, -1, 0 },
138                 { "LOOKUP foo WHERE "
139                   "host.name = 'host'",  -1, -1, 0 },
140         };
142         size_t i;
143         sdb_llist_t *check;
145         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
146                 sdb_object_t *obj;
147                 _Bool ok;
149                 check = sdb_fe_parse(golden_data[i].query, golden_data[i].len);
150                 if (golden_data[i].expected < 0)
151                         ok = check == 0;
152                 else
153                         ok = sdb_llist_len(check) == (size_t)golden_data[i].expected;
155                 fail_unless(ok, "sdb_fe_parse(%s) = %p (len: %zu); expected: %d",
156                                 golden_data[i].query, check, sdb_llist_len(check),
157                                 golden_data[i].expected);
159                 if (! check)
160                         continue;
162                 if ((! golden_data[i].expected_cmd)
163                                 || (golden_data[i].expected <= 0)) {
164                         sdb_llist_destroy(check);
165                         continue;
166                 }
168                 obj = sdb_llist_get(check, 0);
169                 fail_unless(SDB_CONN_NODE(obj)->cmd == golden_data[i].expected_cmd,
170                                 "sdb_fe_parse(%s)->cmd = %i; expected: %d",
171                                 golden_data[i].query, SDB_CONN_NODE(obj)->cmd,
172                                 golden_data[i].expected_cmd);
173                 sdb_object_deref(obj);
174                 sdb_llist_destroy(check);
175         }
177 END_TEST
179 START_TEST(test_parse_matcher)
181         struct {
182                 const char *expr;
183                 int len;
184                 int expected;
185         } golden_data[] = {
186                 /* empty expressions */
187                 { NULL,                             -1, -1 },
188                 { "",                               -1, -1 },
190                 /* valid expressions */
191                 { "host.name = 'localhost'",        -1,  MATCHER_NAME },
192                 { "host.name != 'localhost'",       -1,  MATCHER_NOT },
193                 { "host.name =~ 'host'",            -1,  MATCHER_NAME },
194                 { "host.name !~ 'host'",            -1,  MATCHER_NOT },
195                 { "host.name = 'localhost' -- foo", -1,  MATCHER_NAME },
196                 { "host.name = 'host' <garbage>",   18,  MATCHER_NAME },
197                 /* match hosts by service */
198                 { "service.name = 'name'",          -1,  MATCHER_NAME },
199                 { "service.name != 'name'",         -1,  MATCHER_NOT },
200                 { "service.name =~ 'pattern'",      -1,  MATCHER_NAME },
201                 { "service.name !~ 'pattern'",      -1,  MATCHER_NOT },
202                 /* match hosts by attribute */
203                 { "attribute.name = 'name'",        -1,  MATCHER_NAME },
204                 { "attribute.name != 'name'",       -1,  MATCHER_NOT },
205                 { "attribute.name =~ 'pattern'",    -1,  MATCHER_NAME },
206                 { "attribute.name !~ 'pattern'",    -1,  MATCHER_NOT },
207                 /* composite expressions */
208                 { "host.name =~ 'pattern' AND "
209                   "service.name =~ 'pattern'",      -1,  MATCHER_AND },
210                 { "host.name =~ 'pattern' OR "
211                   "service.name =~ 'pattern'",      -1,  MATCHER_OR },
212                 { "NOT host.name = 'host'",         -1,  MATCHER_NOT },
214                 /* check operator precedence */
215                 { "host.name = 'name' OR "
216                   "service.name = 'name' AND "
217                   "attribute.name = 'name' OR "
218                   "attribute.foo = 'bar'",          -1,  MATCHER_OR },
219                 { "host.name = 'name' AND "
220                   "service.name = 'name' AND "
221                   "attribute.name = 'name' OR "
222                   "attribute.foo = 'bar'",          -1,  MATCHER_OR },
223                 { "host.name = 'name' AND "
224                   "service.name = 'name' OR "
225                   "attribute.name = 'name' AND "
226                   "attribute.foo = 'bar'",          -1,  MATCHER_OR },
227                 { "(host.name = 'name' OR "
228                   "service.name = 'name') AND "
229                   "(attribute.name = 'name' OR "
230                   "attribute.foo = 'bar')",         -1,  MATCHER_AND },
231                 { "NOT host.name = 'name' OR "
232                   "service.name = 'name'",          -1,  MATCHER_OR },
233                 { "NOT host.name = 'name' OR "
234                   "NOT service.name = 'name'",      -1,  MATCHER_OR },
235                 { "NOT (host.name = 'name' OR "
236                   "NOT service.name = 'name')",     -1,  MATCHER_NOT },
238                 /* syntax errors */
239                 { "LIST",                           -1, -1 },
240                 { "foo &^ bar",                     -1, -1 },
241         };
243         size_t i;
245         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
246                 sdb_store_matcher_t *m;
247                 m = sdb_fe_parse_matcher(golden_data[i].expr, golden_data[i].len);
249                 if (golden_data[i].expected < 0) {
250                         fail_unless(m == NULL,
251                                         "sdb_fe_parse_matcher(%s) = %p; expected: NULL",
252                                         golden_data[i].expr, m);
253                         continue;
254                 }
256                 fail_unless(m != NULL, "sdb_fe_parse_matcher(%s) = NULL; "
257                                 "expected: <matcher>", golden_data[i].expr);
258                 fail_unless(M(m)->type == golden_data[i].expected,
259                                 "sdb_fe_parse_matcher(%s) returned matcher of type %d; "
260                                 "expected: %d", golden_data[i].expr, M(m)->type,
261                                 golden_data[i].expected);
263                 sdb_object_deref(SDB_OBJ(m));
264         }
266 END_TEST
268 Suite *
269 fe_parser_suite(void)
271         Suite *s = suite_create("frontend::parser");
272         TCase *tc;
274         tc = tcase_create("core");
275         tcase_add_test(tc, test_parse);
276         tcase_add_test(tc, test_parse_matcher);
277         suite_add_tcase(s, tc);
279         return s;
280 } /* util_parser_suite */
282 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */