Code

store_lookup: Made the attribute matcher a "standalone" matcher.
[sysdb.git] / t / unit / core / store_lookup_test.c
1 /*
2  * SysDB - t/unit/core/store_lookup_test.c
3  * Copyright (C) 2014 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 "core/store.h"
29 #include "core/store-private.h"
30 #include "frontend/parser.h"
31 #include "libsysdb_test.h"
33 #include <check.h>
34 #include <string.h>
36 static void
37 populate(void)
38 {
39         const char *hosts[] = { "a", "b", "c" };
41         struct {
42                 const char *host;
43                 const char *service;
44         } services[] = {
45                 { "a", "s1" },
46                 { "a", "s2" },
47                 { "b", "s1" },
48                 { "b", "s3" },
49         };
51         struct {
52                 const char *host;
53                 const char *name;
54                 sdb_data_t  value;
55         } attrs[] = {
56                 { "a", "k1", { SDB_TYPE_STRING, { .string = "v1" } } },
57         };
59         size_t i;
61         for (i = 0; i < SDB_STATIC_ARRAY_LEN(hosts); ++i) {
62                 int status = sdb_store_host(hosts[i], 1);
63                 fail_unless(status == 0,
64                                 "sdb_store_host(%s, 1) = %d; expected: 0",
65                                 hosts[i], status);
66         }
68         for (i = 0; i < SDB_STATIC_ARRAY_LEN(services); ++i) {
69                 int status = sdb_store_service(services[i].host,
70                                 services[i].service, 1);
71                 fail_unless(status == 0,
72                                 "sdb_store_service(%s, %s, 1) = %d; expected: 0",
73                                 services[i].host, services[i].service, status);
74         }
76         for (i = 0; i < SDB_STATIC_ARRAY_LEN(attrs); ++i) {
77                 int status = sdb_store_attribute(attrs[i].host,
78                                 attrs[i].name, &attrs[i].value, 1);
79                 fail_unless(status == 0,
80                                 "sdb_store_attribute(%s, %s, <val>, 1) = %d; expected: 0",
81                                 attrs[i].host, attrs[i].name, status);
82         }
83 } /* populate */
85 START_TEST(test_store_match)
86 {
87         sdb_store_base_t *obj;
89         struct {
90                 const char *hostname;
91                 const char *hostname_re;
93                 const char *attr_name;
94                 const char *attr_value;
95                 _Bool re;
97                 int expected;
98         } golden_data[] = {
99                 {
100                         /* host */ NULL, NULL,
101                         /* attr */ NULL, NULL, 0, 1
102                 },
103                 {
104                         /* host */ NULL, NULL,
105                         /* attr */ NULL, NULL, 1, 1
106                 },
107                 {
108                         /* host */ "a", NULL,
109                         /* attr */ NULL, NULL, 0, 1
110                 },
111                 {
112                         /* host */ "b", NULL,
113                         /* attr */ NULL, NULL, 0, 0
114                 },
115                 {
116                         /* host */ NULL, "^a$",
117                         /* attr */ NULL, NULL, 0, 1
118                 },
119                 {
120                         /* host */ NULL, "^b$",
121                         /* attr */ NULL, NULL, 0, 0
122                 },
123                 {
124                         /* host */ "a", "^a$",
125                         /* attr */ NULL, NULL, 0, 1
126                 },
127                 {
128                         /* host */ "a", "^b$",
129                         /* attr */ NULL, NULL, 0, 0
130                 },
131                 {
132                         /* host */ "b", "^a$",
133                         /* attr */ NULL, NULL, 0, 0
134                 },
135                 {
136                         /* host */ "a", "^a$",
137                         /* attr */ "k1", NULL, 0, 1
138                 },
139                 {
140                         /* host */ "a", "^a$",
141                         /* attr */ NULL, "v1", 0, 1
142                 },
143                 {
144                         /* host */ "a", "^a$",
145                         /* attr */ NULL, "^v1$", 1, 1
146                 },
147         };
149         size_t i;
151         obj = sdb_store_get_host("a");
152         fail_unless(obj != NULL,
153                         "sdb_store_get_host(a) = NULL; expected: <host>");
155         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
156                 sdb_store_matcher_t *h, *a, *n;
157                 char buf[1024];
158                 int status;
160                 a = sdb_store_attr_matcher(golden_data[i].attr_name,
161                                 golden_data[i].attr_value, golden_data[i].re);
162                 fail_unless(a != NULL,
163                                 "sdb_store_attr_matcher() = NULL; expected: <matcher>");
165                 h = sdb_store_host_matcher(golden_data[i].hostname,
166                                 golden_data[i].hostname_re, a);
167                 fail_unless(h != NULL,
168                                 "sdb_store_host_matcher() = NULL: expected: <matcher>");
169                 /* pass ownership to the host matcher */
170                 sdb_object_deref(SDB_OBJ(a));
172                 status = sdb_store_matcher_matches(h, obj);
173                 fail_unless(status == golden_data[i].expected,
174                                 "sdb_store_matcher_matches({%s, <host a>) = %d; expected: %d",
175                                 sdb_store_matcher_tostring(h, buf, sizeof(buf)),
176                                 status, golden_data[i].expected);
178                 n = sdb_store_inv_matcher(h);
179                 fail_unless(n != NULL,
180                                 "sdb_store_inv_matcher() = NULL; expected: <matcher>");
181                 sdb_object_deref(SDB_OBJ(h));
183                 /* now match the inverted set of objects */
184                 status = sdb_store_matcher_matches(n, obj);
185                 fail_unless(status == !golden_data[i].expected,
186                                 "sdb_store_matcher_matches({%s, <host a>) = %d; expected: %d",
187                                 sdb_store_matcher_tostring(n, buf, sizeof(buf)),
188                                 status, !golden_data[i].expected);
190                 sdb_object_deref(SDB_OBJ(n));
191         }
193         sdb_object_deref(SDB_OBJ(obj));
195 END_TEST
197 START_TEST(test_store_match_name)
199         sdb_store_base_t *obj;
201         struct {
202                 int type;
203                 const char *name;
204                 _Bool re;
206                 int expected;
207         } golden_data[] = {
208                 { SDB_HOST,      NULL,   0, 1 },
209                 { SDB_HOST,      NULL,   1, 1 },
210                 { SDB_HOST,      "a",    0, 1 },
211                 { SDB_HOST,      "a",    1, 1 },
212                 { SDB_HOST,      "b",    0, 0 },
213                 { SDB_HOST,      "b",    1, 0 },
214                 { SDB_HOST,      "^a$",  1, 1 },
215                 { SDB_HOST,      "^b$",  1, 0 },
216                 { SDB_HOST,      "^a$",  0, 0 },
217                 { SDB_HOST,      "^b$",  0, 0 },
218                 { SDB_SERVICE,   NULL,   0, 1 },
219                 { SDB_SERVICE,   NULL,   1, 1 },
220                 { SDB_SERVICE,   "s1",   0, 1 },
221                 { SDB_SERVICE,   "s2",   0, 1 },
222                 { SDB_SERVICE,   "s3",   0, 0 },
223                 { SDB_SERVICE,   "s4",   0, 0 },
224                 { SDB_SERVICE,   "^s1$", 1, 1 },
225                 { SDB_SERVICE,   "^s1$", 0, 0 },
226                 { SDB_SERVICE,   "x1",   0, 0 },
227                 { SDB_SERVICE,   "x1",   1, 0 },
228                 { SDB_SERVICE,   "x",    1, 0 },
229                 { SDB_ATTRIBUTE, NULL,   0, 1 },
230                 { SDB_ATTRIBUTE, NULL,   1, 1 },
231                 { SDB_ATTRIBUTE, "k1",   0, 1 },
232                 { SDB_ATTRIBUTE, "k2",   0, 0 },
233                 { SDB_ATTRIBUTE, "k3",   0, 0 },
234                 { SDB_ATTRIBUTE, "k",    1, 1 },
235                 { SDB_ATTRIBUTE, "1",    1, 1 },
236                 { SDB_ATTRIBUTE, "2",    1, 0 },
237         };
239         size_t i;
241         obj = sdb_store_get_host("a");
242         fail_unless(obj != NULL,
243                         "sdb_store_get_host(a) = NULL; expected: <host>");
245         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
246                 sdb_store_matcher_t *m, *n;
247                 char buf[1024];
248                 int status;
250                 m = sdb_store_name_matcher(golden_data[i].type,
251                                 golden_data[i].name, golden_data[i].re);
252                 fail_unless(m != NULL,
253                                 "sdb_store_service_matcher(%d, %s, %d) = NULL; expected: <matcher>",
254                                 golden_data[i].type, golden_data[i].name, golden_data[i].re);
256                 status = sdb_store_matcher_matches(m, obj);
257                 fail_unless(status == golden_data[i].expected,
258                                 "sdb_store_matcher_matches(%s, <host a>) = %d; expected: %d",
259                                 sdb_store_matcher_tostring(m, buf, sizeof(buf)),
260                                 status, golden_data[i].expected);
262                 n = sdb_store_inv_matcher(m);
263                 fail_unless(n != NULL,
264                                 "sdb_store_inv_matcher() = NULL; expected: <matcher>");
265                 sdb_object_deref(SDB_OBJ(m));
267                 /* now match the inverted set of objects */
268                 status = sdb_store_matcher_matches(n, obj);
269                 fail_unless(status == !golden_data[i].expected,
270                                 "sdb_store_matcher_matches(%s, <host a>) = %d; expected: %d",
271                                 sdb_store_matcher_tostring(n, buf, sizeof(buf)),
272                                 status, !golden_data[i].expected);
274                 sdb_object_deref(SDB_OBJ(n));
275         }
277         sdb_object_deref(SDB_OBJ(obj));
279 END_TEST
281 START_TEST(test_store_match_op)
283         sdb_store_base_t *obj;
285         sdb_store_matcher_t *always = sdb_store_host_matcher(NULL, NULL, NULL);
286         sdb_store_matcher_t *never = sdb_store_host_matcher("a", "b", NULL);
288         struct {
289                 const char *op;
290                 sdb_store_matcher_t *left;
291                 sdb_store_matcher_t *right;
292                 int expected;
293         } golden_data[] = {
294                 { "OR",  always, always, 1 },
295                 { "OR",  always, never,  1 },
296                 { "OR",  never,  always, 1 },
297                 { "OR",  never,  never,  0 },
298                 { "AND", always, always, 1 },
299                 { "AND", always, never,  0 },
300                 { "AND", never,  always, 0 },
301                 { "AND", never,  never,  0 },
302         };
304         int status;
305         size_t i;
307         obj = sdb_store_get_host("a");
309         status = sdb_store_matcher_matches(always, obj);
310         fail_unless(status == 1,
311                         "INTERNAL ERROR: 'always' did not match host");
312         status = sdb_store_matcher_matches(never, obj);
313         fail_unless(status == 0,
314                         "INTERNAL ERROR: 'never' matches host");
316         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
317                 sdb_store_matcher_t *m;
319                 if (! strcmp(golden_data[i].op, "OR"))
320                         m = sdb_store_dis_matcher(golden_data[i].left,
321                                         golden_data[i].right);
322                 else if (! strcmp(golden_data[i].op, "AND"))
323                         m = sdb_store_con_matcher(golden_data[i].left,
324                                         golden_data[i].right);
325                 else {
326                         fail("INTERNAL ERROR: unexpected operator %s", golden_data[i].op);
327                         continue;
328                 }
330 #define TO_NAME(v) (((v) == always) ? "always" \
331                 : ((v) == never) ? "never" : "<unknown>")
333                 status = sdb_store_matcher_matches(m, obj);
334                 fail_unless(status == golden_data[i].expected,
335                                 "%s(%s, %s) = %d; expected: %d", golden_data[i].op,
336                                 TO_NAME(golden_data[i].left), TO_NAME(golden_data[i].right),
337                                 status, golden_data[i].expected);
339 #undef TO_NAME
341                 sdb_object_deref(SDB_OBJ(m));
342         }
344         sdb_object_deref(SDB_OBJ(always));
345         sdb_object_deref(SDB_OBJ(never));
347         sdb_object_deref(SDB_OBJ(obj));
349 END_TEST
351 START_TEST(test_parse_cmp)
353         sdb_store_matcher_t *check;
355         size_t i;
357         struct {
358                 const char *obj_type;
359                 const char *attr;
360                 const char *op;
361                 const char *value;
362                 int expected;
363         } golden_data[] = {
364                 { "host",      "name", "=",  "hostname", MATCHER_NAME },
365                 { "host",      "name", "!=", "hostname", MATCHER_NOT },
366                 { "host",      "name", "=~", "hostname", MATCHER_NAME },
367                 { "host",      "name", "!~", "hostname", MATCHER_NOT },
368                 { "host",      "attr", "=",  "hostname", -1 },
369                 { "host",      "attr", "!=", "hostname", -1 },
370                 { "host",      "name", "&^", "hostname", -1 },
371                 { "service",   "name", "=",  "srvname",  MATCHER_NAME },
372                 { "service",   "name", "!=", "srvname",  MATCHER_NOT },
373                 { "service",   "name", "=~", "srvname",  MATCHER_NAME },
374                 { "service",   "name", "!~", "srvname",  MATCHER_NOT },
375                 { "service",   "attr", "=",  "srvname",  -1 },
376                 { "service",   "attr", "!=", "srvname",  -1 },
377                 { "service",   "name", "&^", "srvname",  -1 },
378                 { "attribute", "name", "=",  "attrname", MATCHER_NAME },
379                 { "attribute", "name", "!=", "attrname", MATCHER_NOT },
380                 { "attribute", "name", "=~", "attrname", MATCHER_NAME },
381                 { "attribute", "name", "!~", "attrname", MATCHER_NOT },
382                 { "attribute", "attr", "=",  "attrname", MATCHER_ATTR },
383                 { "attribute", "attr", "!=", "attrname", MATCHER_NOT },
384                 { "attribute", "attr", "=~", "attrname", MATCHER_ATTR },
385                 { "attribute", "attr", "!~", "attrname", MATCHER_NOT },
386                 { "attribute", "attr", "&^", "attrname", -1 },
387         };
389         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
390                 check = sdb_store_matcher_parse_cmp(golden_data[i].obj_type,
391                                 golden_data[i].attr, golden_data[i].op, golden_data[i].value);
393                 if (golden_data[i].expected == -1) {
394                         fail_unless(check == NULL,
395                                         "sdb_store_matcher_parse_cmp(%s, %s, %s, %s) = %p; "
396                                         "expected: NULL", golden_data[i].obj_type,
397                                         golden_data[i].attr, golden_data[i].op,
398                                         golden_data[i].value, check);
399                         continue;
400                 }
402                 fail_unless(check != NULL,
403                                 "sdb_store_matcher_parse_cmp(%s, %s, %s, %s) = %p; "
404                                 "expected: NULL", golden_data[i].obj_type,
405                                 golden_data[i].attr, golden_data[i].op,
406                                 golden_data[i].value, check);
407                 fail_unless(M(check)->type == golden_data[i].expected,
408                                 "sdb_store_matcher_parse_cmp(%s, %s, %s, %s) returned matcher "
409                                 "of type %d; expected: %d", golden_data[i].obj_type,
410                                 golden_data[i].attr, golden_data[i].op, golden_data[i].value,
411                                 M(check)->type, golden_data[i].expected);
413                 sdb_object_deref(SDB_OBJ(check));
414         }
416 END_TEST
418 static int
419 lookup_cb(sdb_store_base_t *obj, void *user_data)
421         int *i = user_data;
423         fail_unless(obj != NULL,
424                         "sdb_store_lookup callback received NULL obj; expected: "
425                         "<store base obj>");
426         fail_unless(i != NULL,
427                         "sdb_store_lookup callback received NULL user_data; "
428                         "expected: <pointer to data>");
430         ++(*i);
431         return 0;
432 } /* lookup_cb */
434 START_TEST(test_lookup)
436 #define PTR_RE "0x[0-9a-f]+"
437         struct {
438                 const char *query;
439                 int expected;
440                 const char *tostring_re;
441         } golden_data[] = {
442                 { "host.name = 'a'",       1,
443                         "OBJ\\[host\\]\\{ NAME\\{ 'a', \\(nil\\) \\} \\}" },
444                 { "host.name =~ 'a|b'",    2,
445                         "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
446                 { "host.name =~ 'host'",   0,
447                         "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
448                 { "host.name =~ '.'",      3,
449                         "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
450                 { "service.name = 's1'",   2,
451                         "OBJ\\[service\\]\\{ NAME\\{ 's1', \\(nil\\) } \\}" },
452                 { "service.name =~ 's'",   2,
453                         "OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}" },
454                 { "service.name !~ 's'",   1,
455                         "\\(NOT, OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}\\)" },
456                 { "attribute.name = 'k1'", 1,
457                         "OBJ\\[attribute\\]\\{ NAME\\{ 'k1', \\(nil\\) \\} " },
458                 { "attribute.name = 'x'",  0,
459                         "OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\}" },
460                 { "attribute.k1 = 'v1'",   1,
461                         "ATTR\\[k1\\]\\{ VALUE\\{ 'v1', \\(nil\\) \\} \\}" },
462                 { "attribute.k1 != 'v1'",  2,
463                         "\\(NOT, ATTR\\[k1\\]\\{ VALUE\\{ 'v1', \\(nil\\) \\} \\}\\)" },
464                 { "attribute.k1 != 'v2'",  3,
465                         "\\(NOT, ATTR\\[k1\\]\\{ VALUE\\{ 'v2', \\(nil\\) \\} \\}\\)" },
466                 { "attribute.name != 'x' "
467                   "AND attribute.y !~ 'x'", 3,
468                         "\\(AND, "
469                                 "\\(NOT, OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\} \\}\\), "
470                                 "\\(NOT, ATTR\\[y\\]\\{ VALUE\\{ NULL, "PTR_RE" \\} \\}\\)\\)" },
471         };
473         int check, n;
474         size_t i;
476         n = 0;
477         check = sdb_store_lookup(NULL, lookup_cb, &n);
478         fail_unless(check == 0,
479                         "sdb_store_lookup() = %d; expected: 0", check);
480         fail_unless(n == 3,
481                         "sdb_store_lookup called callback %d times; expected: 3", (int)n);
483         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
484                 sdb_store_matcher_t *m;
485                 char buf[4096];
487                 m = sdb_fe_parse_matcher(golden_data[i].query, -1);
488                 fail_unless(m != NULL,
489                                 "sdb_fe_parse_matcher(%s, -1) = NULL; expected: <matcher>",
490                                 golden_data[i].query);
491                 fail_unless(sdb_regmatches(golden_data[i].tostring_re,
492                                         sdb_store_matcher_tostring(m, buf, sizeof(buf))) == 0,
493                                 "sdb_fe_parse_matcher(%s, -1) = %s; expected: %s",
494                                 golden_data[i].query,
495                                 sdb_store_matcher_tostring(m, buf, sizeof(buf)),
496                                 golden_data[i].tostring_re);
498                 n = 0;
499                 sdb_store_lookup(m, lookup_cb, &n);
500                 fail_unless(n == golden_data[i].expected,
501                                 "sdb_store_lookup(matcher{%s}) found %d hosts; expected: %d",
502                                 golden_data[i].query, n, golden_data[i].expected);
503                 sdb_object_deref(SDB_OBJ(m));
504         }
506 END_TEST
508 Suite *
509 core_store_lookup_suite(void)
511         Suite *s = suite_create("core::store_lookup");
512         TCase *tc;
514         tc = tcase_create("core");
515         tcase_add_checked_fixture(tc, populate, sdb_store_clear);
516         tcase_add_test(tc, test_store_match);
517         tcase_add_test(tc, test_store_match_name);
518         tcase_add_test(tc, test_store_match_op);
519         tcase_add_test(tc, test_parse_cmp);
520         tcase_add_test(tc, test_lookup);
521         suite_add_tcase(s, tc);
523         return s;
524 } /* core_store_lookup_suite */
526 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */