Code

store_lookup: Removed the (now unused) host 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_name)
86 {
87         sdb_store_base_t *obj;
89         struct {
90                 int type;
91                 const char *name;
92                 _Bool re;
94                 int expected;
95         } golden_data[] = {
96                 { SDB_HOST,      NULL,   0, 1 },
97                 { SDB_HOST,      NULL,   1, 1 },
98                 { SDB_HOST,      "a",    0, 1 },
99                 { SDB_HOST,      "a",    1, 1 },
100                 { SDB_HOST,      "b",    0, 0 },
101                 { SDB_HOST,      "b",    1, 0 },
102                 { SDB_HOST,      "^a$",  1, 1 },
103                 { SDB_HOST,      "^b$",  1, 0 },
104                 { SDB_HOST,      "^a$",  0, 0 },
105                 { SDB_HOST,      "^b$",  0, 0 },
106                 { SDB_SERVICE,   NULL,   0, 1 },
107                 { SDB_SERVICE,   NULL,   1, 1 },
108                 { SDB_SERVICE,   "s1",   0, 1 },
109                 { SDB_SERVICE,   "s2",   0, 1 },
110                 { SDB_SERVICE,   "s3",   0, 0 },
111                 { SDB_SERVICE,   "s4",   0, 0 },
112                 { SDB_SERVICE,   "^s1$", 1, 1 },
113                 { SDB_SERVICE,   "^s1$", 0, 0 },
114                 { SDB_SERVICE,   "x1",   0, 0 },
115                 { SDB_SERVICE,   "x1",   1, 0 },
116                 { SDB_SERVICE,   "x",    1, 0 },
117                 { SDB_ATTRIBUTE, NULL,   0, 1 },
118                 { SDB_ATTRIBUTE, NULL,   1, 1 },
119                 { SDB_ATTRIBUTE, "k1",   0, 1 },
120                 { SDB_ATTRIBUTE, "k2",   0, 0 },
121                 { SDB_ATTRIBUTE, "k3",   0, 0 },
122                 { SDB_ATTRIBUTE, "k",    1, 1 },
123                 { SDB_ATTRIBUTE, "1",    1, 1 },
124                 { SDB_ATTRIBUTE, "2",    1, 0 },
125         };
127         size_t i;
129         obj = sdb_store_get_host("a");
130         fail_unless(obj != NULL,
131                         "sdb_store_get_host(a) = NULL; expected: <host>");
133         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
134                 sdb_store_matcher_t *m, *n;
135                 char buf[1024];
136                 int status;
138                 m = sdb_store_name_matcher(golden_data[i].type,
139                                 golden_data[i].name, golden_data[i].re);
140                 fail_unless(m != NULL,
141                                 "sdb_store_service_matcher(%d, %s, %d) = NULL; expected: <matcher>",
142                                 golden_data[i].type, golden_data[i].name, golden_data[i].re);
144                 status = sdb_store_matcher_matches(m, obj);
145                 fail_unless(status == golden_data[i].expected,
146                                 "sdb_store_matcher_matches(%s, <host a>) = %d; expected: %d",
147                                 sdb_store_matcher_tostring(m, buf, sizeof(buf)),
148                                 status, golden_data[i].expected);
150                 n = sdb_store_inv_matcher(m);
151                 fail_unless(n != NULL,
152                                 "sdb_store_inv_matcher() = NULL; expected: <matcher>");
153                 sdb_object_deref(SDB_OBJ(m));
155                 /* now match the inverted set of objects */
156                 status = sdb_store_matcher_matches(n, obj);
157                 fail_unless(status == !golden_data[i].expected,
158                                 "sdb_store_matcher_matches(%s, <host a>) = %d; expected: %d",
159                                 sdb_store_matcher_tostring(n, buf, sizeof(buf)),
160                                 status, !golden_data[i].expected);
162                 sdb_object_deref(SDB_OBJ(n));
163         }
165         sdb_object_deref(SDB_OBJ(obj));
167 END_TEST
169 START_TEST(test_store_match_attr)
171         sdb_store_base_t *obj;
173         struct {
174                 const char *attr_name;
175                 const char *attr_value;
176                 _Bool re;
178                 int expected;
179         } golden_data[] = {
180                 { NULL, NULL,   0, 1 },
181                 { NULL, NULL,   1, 1 },
182                 { "k1", NULL,   0, 1 },
183                 { "k",  NULL,   1, 0 },
184                 { "1",  NULL,   1, 0 },
185                 { "k2", NULL,   0, 0 },
186                 { "k2", NULL,   1, 0 },
187                 { "k3", NULL,   0, 0 },
188                 { "k3", NULL,   1, 0 },
189                 { "k1", "v1",   0, 1 },
190                 { "k1", "v1",   1, 1 },
191                 { "k1", "v",    1, 1 },
192                 { "k1", "1",    1, 1 },
193                 { NULL, "v1",   0, 1 },
194                 { NULL, "^v1$", 1, 1 },
195                 { "k1", "v2",   0, 0 },
196                 { "k1", "v2",   1, 0 },
197                 { "k",  "v1",   0, 0 },
198                 { "k",  "v1",   1, 0 },
199                 { "k2", "1",    0, 0 },
200                 { "k2", "1",    1, 0 },
201         };
203         size_t i;
205         obj = sdb_store_get_host("a");
206         fail_unless(obj != NULL,
207                         "sdb_store_get_host(a) = NULL; expected: <host>");
209         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
210                 sdb_store_matcher_t *m, *n;
211                 char buf[1024];
212                 int status;
214                 m = sdb_store_attr_matcher(golden_data[i].attr_name,
215                                 golden_data[i].attr_value, golden_data[i].re);
216                 fail_unless(m != NULL,
217                                 "sdb_store_attr_matcher() = NULL; expected: <matcher>");
219                 status = sdb_store_matcher_matches(m, obj);
220                 fail_unless(status == golden_data[i].expected,
221                                 "sdb_store_matcher_matches({%s, <host a>) = %d; expected: %d",
222                                 sdb_store_matcher_tostring(m, buf, sizeof(buf)),
223                                 status, golden_data[i].expected);
225                 n = sdb_store_inv_matcher(m);
226                 fail_unless(n != NULL,
227                                 "sdb_store_inv_matcher() = NULL; expected: <matcher>");
228                 sdb_object_deref(SDB_OBJ(m));
230                 /* now match the inverted set of objects */
231                 status = sdb_store_matcher_matches(n, obj);
232                 fail_unless(status == !golden_data[i].expected,
233                                 "sdb_store_matcher_matches({%s, <host a>) = %d; expected: %d",
234                                 sdb_store_matcher_tostring(n, buf, sizeof(buf)),
235                                 status, !golden_data[i].expected);
237                 sdb_object_deref(SDB_OBJ(n));
238         }
240         sdb_object_deref(SDB_OBJ(obj));
242 END_TEST
244 START_TEST(test_store_match_op)
246         sdb_store_base_t *obj;
248         sdb_store_matcher_t *always = sdb_store_name_matcher(SDB_HOST, "a", 0);
249         sdb_store_matcher_t *never = sdb_store_name_matcher(SDB_HOST, "z", 0);
251         struct {
252                 const char *op;
253                 sdb_store_matcher_t *left;
254                 sdb_store_matcher_t *right;
255                 int expected;
256         } golden_data[] = {
257                 { "OR",  always, always, 1 },
258                 { "OR",  always, never,  1 },
259                 { "OR",  never,  always, 1 },
260                 { "OR",  never,  never,  0 },
261                 { "AND", always, always, 1 },
262                 { "AND", always, never,  0 },
263                 { "AND", never,  always, 0 },
264                 { "AND", never,  never,  0 },
265         };
267         int status;
268         size_t i;
270         obj = sdb_store_get_host("a");
272         status = sdb_store_matcher_matches(always, obj);
273         fail_unless(status == 1,
274                         "INTERNAL ERROR: 'always' did not match host");
275         status = sdb_store_matcher_matches(never, obj);
276         fail_unless(status == 0,
277                         "INTERNAL ERROR: 'never' matches host");
279         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
280                 sdb_store_matcher_t *m;
282                 if (! strcmp(golden_data[i].op, "OR"))
283                         m = sdb_store_dis_matcher(golden_data[i].left,
284                                         golden_data[i].right);
285                 else if (! strcmp(golden_data[i].op, "AND"))
286                         m = sdb_store_con_matcher(golden_data[i].left,
287                                         golden_data[i].right);
288                 else {
289                         fail("INTERNAL ERROR: unexpected operator %s", golden_data[i].op);
290                         continue;
291                 }
293 #define TO_NAME(v) (((v) == always) ? "always" \
294                 : ((v) == never) ? "never" : "<unknown>")
296                 status = sdb_store_matcher_matches(m, obj);
297                 fail_unless(status == golden_data[i].expected,
298                                 "%s(%s, %s) = %d; expected: %d", golden_data[i].op,
299                                 TO_NAME(golden_data[i].left), TO_NAME(golden_data[i].right),
300                                 status, golden_data[i].expected);
302 #undef TO_NAME
304                 sdb_object_deref(SDB_OBJ(m));
305         }
307         sdb_object_deref(SDB_OBJ(always));
308         sdb_object_deref(SDB_OBJ(never));
310         sdb_object_deref(SDB_OBJ(obj));
312 END_TEST
314 START_TEST(test_parse_cmp)
316         sdb_store_matcher_t *check;
318         size_t i;
320         struct {
321                 const char *obj_type;
322                 const char *attr;
323                 const char *op;
324                 const char *value;
325                 int expected;
326         } golden_data[] = {
327                 { "host",      "name", "=",  "hostname", MATCHER_NAME },
328                 { "host",      "name", "!=", "hostname", MATCHER_NOT },
329                 { "host",      "name", "=~", "hostname", MATCHER_NAME },
330                 { "host",      "name", "!~", "hostname", MATCHER_NOT },
331                 { "host",      "attr", "=",  "hostname", -1 },
332                 { "host",      "attr", "!=", "hostname", -1 },
333                 { "host",      "name", "&^", "hostname", -1 },
334                 { "service",   "name", "=",  "srvname",  MATCHER_NAME },
335                 { "service",   "name", "!=", "srvname",  MATCHER_NOT },
336                 { "service",   "name", "=~", "srvname",  MATCHER_NAME },
337                 { "service",   "name", "!~", "srvname",  MATCHER_NOT },
338                 { "service",   "attr", "=",  "srvname",  -1 },
339                 { "service",   "attr", "!=", "srvname",  -1 },
340                 { "service",   "name", "&^", "srvname",  -1 },
341                 { "attribute", "name", "=",  "attrname", MATCHER_NAME },
342                 { "attribute", "name", "!=", "attrname", MATCHER_NOT },
343                 { "attribute", "name", "=~", "attrname", MATCHER_NAME },
344                 { "attribute", "name", "!~", "attrname", MATCHER_NOT },
345                 { "attribute", "attr", "=",  "attrname", MATCHER_ATTR },
346                 { "attribute", "attr", "!=", "attrname", MATCHER_NOT },
347                 { "attribute", "attr", "=~", "attrname", MATCHER_ATTR },
348                 { "attribute", "attr", "!~", "attrname", MATCHER_NOT },
349                 { "attribute", "attr", "&^", "attrname", -1 },
350         };
352         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
353                 check = sdb_store_matcher_parse_cmp(golden_data[i].obj_type,
354                                 golden_data[i].attr, golden_data[i].op, golden_data[i].value);
356                 if (golden_data[i].expected == -1) {
357                         fail_unless(check == NULL,
358                                         "sdb_store_matcher_parse_cmp(%s, %s, %s, %s) = %p; "
359                                         "expected: NULL", golden_data[i].obj_type,
360                                         golden_data[i].attr, golden_data[i].op,
361                                         golden_data[i].value, check);
362                         continue;
363                 }
365                 fail_unless(check != NULL,
366                                 "sdb_store_matcher_parse_cmp(%s, %s, %s, %s) = %p; "
367                                 "expected: NULL", golden_data[i].obj_type,
368                                 golden_data[i].attr, golden_data[i].op,
369                                 golden_data[i].value, check);
370                 fail_unless(M(check)->type == golden_data[i].expected,
371                                 "sdb_store_matcher_parse_cmp(%s, %s, %s, %s) returned matcher "
372                                 "of type %d; expected: %d", golden_data[i].obj_type,
373                                 golden_data[i].attr, golden_data[i].op, golden_data[i].value,
374                                 M(check)->type, golden_data[i].expected);
376                 sdb_object_deref(SDB_OBJ(check));
377         }
379 END_TEST
381 static int
382 lookup_cb(sdb_store_base_t *obj, void *user_data)
384         int *i = user_data;
386         fail_unless(obj != NULL,
387                         "sdb_store_lookup callback received NULL obj; expected: "
388                         "<store base obj>");
389         fail_unless(i != NULL,
390                         "sdb_store_lookup callback received NULL user_data; "
391                         "expected: <pointer to data>");
393         ++(*i);
394         return 0;
395 } /* lookup_cb */
397 START_TEST(test_lookup)
399 #define PTR_RE "0x[0-9a-f]+"
400         struct {
401                 const char *query;
402                 int expected;
403                 const char *tostring_re;
404         } golden_data[] = {
405                 { "host.name = 'a'",       1,
406                         "OBJ\\[host\\]\\{ NAME\\{ 'a', \\(nil\\) \\} \\}" },
407                 { "host.name =~ 'a|b'",    2,
408                         "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
409                 { "host.name =~ 'host'",   0,
410                         "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
411                 { "host.name =~ '.'",      3,
412                         "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
413                 { "service.name = 's1'",   2,
414                         "OBJ\\[service\\]\\{ NAME\\{ 's1', \\(nil\\) } \\}" },
415                 { "service.name =~ 's'",   2,
416                         "OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}" },
417                 { "service.name !~ 's'",   1,
418                         "\\(NOT, OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}\\)" },
419                 { "attribute.name = 'k1'", 1,
420                         "OBJ\\[attribute\\]\\{ NAME\\{ 'k1', \\(nil\\) \\} " },
421                 { "attribute.name = 'x'",  0,
422                         "OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\}" },
423                 { "attribute.k1 = 'v1'",   1,
424                         "ATTR\\[k1\\]\\{ VALUE\\{ 'v1', \\(nil\\) \\} \\}" },
425                 { "attribute.k1 != 'v1'",  2,
426                         "\\(NOT, ATTR\\[k1\\]\\{ VALUE\\{ 'v1', \\(nil\\) \\} \\}\\)" },
427                 { "attribute.k1 != 'v2'",  3,
428                         "\\(NOT, ATTR\\[k1\\]\\{ VALUE\\{ 'v2', \\(nil\\) \\} \\}\\)" },
429                 { "attribute.name != 'x' "
430                   "AND attribute.y !~ 'x'", 3,
431                         "\\(AND, "
432                                 "\\(NOT, OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\} \\}\\), "
433                                 "\\(NOT, ATTR\\[y\\]\\{ VALUE\\{ NULL, "PTR_RE" \\} \\}\\)\\)" },
434         };
436         int check, n;
437         size_t i;
439         n = 0;
440         check = sdb_store_lookup(NULL, lookup_cb, &n);
441         fail_unless(check == 0,
442                         "sdb_store_lookup() = %d; expected: 0", check);
443         fail_unless(n == 3,
444                         "sdb_store_lookup called callback %d times; expected: 3", (int)n);
446         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
447                 sdb_store_matcher_t *m;
448                 char buf[4096];
450                 m = sdb_fe_parse_matcher(golden_data[i].query, -1);
451                 fail_unless(m != NULL,
452                                 "sdb_fe_parse_matcher(%s, -1) = NULL; expected: <matcher>",
453                                 golden_data[i].query);
454                 fail_unless(sdb_regmatches(golden_data[i].tostring_re,
455                                         sdb_store_matcher_tostring(m, buf, sizeof(buf))) == 0,
456                                 "sdb_fe_parse_matcher(%s, -1) = %s; expected: %s",
457                                 golden_data[i].query,
458                                 sdb_store_matcher_tostring(m, buf, sizeof(buf)),
459                                 golden_data[i].tostring_re);
461                 n = 0;
462                 sdb_store_lookup(m, lookup_cb, &n);
463                 fail_unless(n == golden_data[i].expected,
464                                 "sdb_store_lookup(matcher{%s}) found %d hosts; expected: %d",
465                                 golden_data[i].query, n, golden_data[i].expected);
466                 sdb_object_deref(SDB_OBJ(m));
467         }
469 END_TEST
471 Suite *
472 core_store_lookup_suite(void)
474         Suite *s = suite_create("core::store_lookup");
475         TCase *tc;
477         tc = tcase_create("core");
478         tcase_add_checked_fixture(tc, populate, sdb_store_clear);
479         tcase_add_test(tc, test_store_match_name);
480         tcase_add_test(tc, test_store_match_attr);
481         tcase_add_test(tc, test_store_match_op);
482         tcase_add_test(tc, test_parse_cmp);
483         tcase_add_test(tc, test_lookup);
484         suite_add_tcase(s, tc);
486         return s;
487 } /* core_store_lookup_suite */
489 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */