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));
166 }
167 END_TEST
169 START_TEST(test_store_match_attr)
170 {
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 { "k1", NULL, 0, 1 },
181 { "k", NULL, 1, 0 },
182 { "1", NULL, 1, 0 },
183 { "k2", NULL, 0, 0 },
184 { "k2", NULL, 1, 0 },
185 { "k3", NULL, 0, 0 },
186 { "k3", NULL, 1, 0 },
187 { "k1", "v1", 0, 1 },
188 { "k1", "v1", 1, 1 },
189 { "k1", "^v1$", 1, 1 },
190 { "k1", "v", 1, 1 },
191 { "k1", "1", 1, 1 },
192 { "k1", "v2", 0, 0 },
193 { "k1", "v2", 1, 0 },
194 { "k", "v1", 0, 0 },
195 { "k", "v1", 1, 0 },
196 { "k2", "1", 0, 0 },
197 { "k2", "1", 1, 0 },
198 };
200 size_t i;
202 obj = sdb_store_get_host("a");
203 fail_unless(obj != NULL,
204 "sdb_store_get_host(a) = NULL; expected: <host>");
206 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
207 sdb_store_matcher_t *m, *n;
208 char buf[1024];
209 int status;
211 m = sdb_store_attr_matcher(golden_data[i].attr_name,
212 golden_data[i].attr_value, golden_data[i].re);
213 fail_unless(m != NULL,
214 "sdb_store_attr_matcher() = NULL; expected: <matcher>");
216 status = sdb_store_matcher_matches(m, obj);
217 fail_unless(status == golden_data[i].expected,
218 "sdb_store_matcher_matches({%s, <host a>) = %d; expected: %d",
219 sdb_store_matcher_tostring(m, buf, sizeof(buf)),
220 status, golden_data[i].expected);
222 n = sdb_store_inv_matcher(m);
223 fail_unless(n != NULL,
224 "sdb_store_inv_matcher() = NULL; expected: <matcher>");
225 sdb_object_deref(SDB_OBJ(m));
227 /* now match the inverted set of objects */
228 status = sdb_store_matcher_matches(n, obj);
229 fail_unless(status == !golden_data[i].expected,
230 "sdb_store_matcher_matches({%s, <host a>) = %d; expected: %d",
231 sdb_store_matcher_tostring(n, buf, sizeof(buf)),
232 status, !golden_data[i].expected);
234 sdb_object_deref(SDB_OBJ(n));
235 }
237 sdb_object_deref(SDB_OBJ(obj));
238 }
239 END_TEST
241 START_TEST(test_store_match_op)
242 {
243 sdb_store_base_t *obj;
245 sdb_store_matcher_t *always = sdb_store_name_matcher(SDB_HOST, "a", 0);
246 sdb_store_matcher_t *never = sdb_store_name_matcher(SDB_HOST, "z", 0);
248 struct {
249 const char *op;
250 sdb_store_matcher_t *left;
251 sdb_store_matcher_t *right;
252 int expected;
253 } golden_data[] = {
254 { "OR", always, always, 1 },
255 { "OR", always, never, 1 },
256 { "OR", never, always, 1 },
257 { "OR", never, never, 0 },
258 { "AND", always, always, 1 },
259 { "AND", always, never, 0 },
260 { "AND", never, always, 0 },
261 { "AND", never, never, 0 },
262 };
264 int status;
265 size_t i;
267 obj = sdb_store_get_host("a");
269 status = sdb_store_matcher_matches(always, obj);
270 fail_unless(status == 1,
271 "INTERNAL ERROR: 'always' did not match host");
272 status = sdb_store_matcher_matches(never, obj);
273 fail_unless(status == 0,
274 "INTERNAL ERROR: 'never' matches host");
276 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
277 sdb_store_matcher_t *m;
279 if (! strcmp(golden_data[i].op, "OR"))
280 m = sdb_store_dis_matcher(golden_data[i].left,
281 golden_data[i].right);
282 else if (! strcmp(golden_data[i].op, "AND"))
283 m = sdb_store_con_matcher(golden_data[i].left,
284 golden_data[i].right);
285 else {
286 fail("INTERNAL ERROR: unexpected operator %s", golden_data[i].op);
287 continue;
288 }
290 #define TO_NAME(v) (((v) == always) ? "always" \
291 : ((v) == never) ? "never" : "<unknown>")
293 status = sdb_store_matcher_matches(m, obj);
294 fail_unless(status == golden_data[i].expected,
295 "%s(%s, %s) = %d; expected: %d", golden_data[i].op,
296 TO_NAME(golden_data[i].left), TO_NAME(golden_data[i].right),
297 status, golden_data[i].expected);
299 #undef TO_NAME
301 sdb_object_deref(SDB_OBJ(m));
302 }
304 sdb_object_deref(SDB_OBJ(always));
305 sdb_object_deref(SDB_OBJ(never));
307 sdb_object_deref(SDB_OBJ(obj));
308 }
309 END_TEST
311 START_TEST(test_parse_cmp)
312 {
313 sdb_store_matcher_t *check;
315 size_t i;
317 struct {
318 const char *obj_type;
319 const char *attr;
320 const char *op;
321 const char *value;
322 int expected;
323 } golden_data[] = {
324 { "host", "name", "=", "hostname", MATCHER_NAME },
325 { "host", "name", "!=", "hostname", MATCHER_NOT },
326 { "host", "name", "=~", "hostname", MATCHER_NAME },
327 { "host", "name", "!~", "hostname", MATCHER_NOT },
328 { "host", "attr", "=", "hostname", -1 },
329 { "host", "attr", "!=", "hostname", -1 },
330 { "host", "name", "&^", "hostname", -1 },
331 { "service", "name", "=", "srvname", MATCHER_NAME },
332 { "service", "name", "!=", "srvname", MATCHER_NOT },
333 { "service", "name", "=~", "srvname", MATCHER_NAME },
334 { "service", "name", "!~", "srvname", MATCHER_NOT },
335 { "service", "attr", "=", "srvname", -1 },
336 { "service", "attr", "!=", "srvname", -1 },
337 { "service", "name", "&^", "srvname", -1 },
338 { "attribute", "name", "=", "attrname", MATCHER_NAME },
339 { "attribute", "name", "!=", "attrname", MATCHER_NOT },
340 { "attribute", "name", "=~", "attrname", MATCHER_NAME },
341 { "attribute", "name", "!~", "attrname", MATCHER_NOT },
342 { "attribute", "attr", "=", "attrname", MATCHER_ATTR },
343 { "attribute", "attr", "!=", "attrname", MATCHER_NOT },
344 { "attribute", "attr", "=~", "attrname", MATCHER_ATTR },
345 { "attribute", "attr", "!~", "attrname", MATCHER_NOT },
346 { "attribute", "attr", "&^", "attrname", -1 },
347 };
349 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
350 check = sdb_store_matcher_parse_cmp(golden_data[i].obj_type,
351 golden_data[i].attr, golden_data[i].op, golden_data[i].value);
353 if (golden_data[i].expected == -1) {
354 fail_unless(check == NULL,
355 "sdb_store_matcher_parse_cmp(%s, %s, %s, %s) = %p; "
356 "expected: NULL", golden_data[i].obj_type,
357 golden_data[i].attr, golden_data[i].op,
358 golden_data[i].value, check);
359 continue;
360 }
362 fail_unless(check != NULL,
363 "sdb_store_matcher_parse_cmp(%s, %s, %s, %s) = %p; "
364 "expected: NULL", golden_data[i].obj_type,
365 golden_data[i].attr, golden_data[i].op,
366 golden_data[i].value, check);
367 fail_unless(M(check)->type == golden_data[i].expected,
368 "sdb_store_matcher_parse_cmp(%s, %s, %s, %s) returned matcher "
369 "of type %d; expected: %d", golden_data[i].obj_type,
370 golden_data[i].attr, golden_data[i].op, golden_data[i].value,
371 M(check)->type, golden_data[i].expected);
373 sdb_object_deref(SDB_OBJ(check));
374 }
375 }
376 END_TEST
378 static int
379 lookup_cb(sdb_store_base_t *obj, void *user_data)
380 {
381 int *i = user_data;
383 fail_unless(obj != NULL,
384 "sdb_store_lookup callback received NULL obj; expected: "
385 "<store base obj>");
386 fail_unless(i != NULL,
387 "sdb_store_lookup callback received NULL user_data; "
388 "expected: <pointer to data>");
390 ++(*i);
391 return 0;
392 } /* lookup_cb */
394 START_TEST(test_lookup)
395 {
396 #define PTR_RE "0x[0-9a-f]+"
397 struct {
398 const char *query;
399 int expected;
400 const char *tostring_re;
401 } golden_data[] = {
402 { "host.name = 'a'", 1,
403 "OBJ\\[host\\]\\{ NAME\\{ 'a', \\(nil\\) \\} \\}" },
404 { "host.name =~ 'a|b'", 2,
405 "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
406 { "host.name =~ 'host'", 0,
407 "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
408 { "host.name =~ '.'", 3,
409 "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
410 { "service.name = 's1'", 2,
411 "OBJ\\[service\\]\\{ NAME\\{ 's1', \\(nil\\) } \\}" },
412 { "service.name =~ 's'", 2,
413 "OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}" },
414 { "service.name !~ 's'", 1,
415 "\\(NOT, OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}\\)" },
416 { "attribute.name = 'k1'", 1,
417 "OBJ\\[attribute\\]\\{ NAME\\{ 'k1', \\(nil\\) \\} " },
418 { "attribute.name = 'x'", 0,
419 "OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\}" },
420 { "attribute.k1 = 'v1'", 1,
421 "ATTR\\[k1\\]\\{ VALUE\\{ 'v1', \\(nil\\) \\} \\}" },
422 { "attribute.k1 != 'v1'", 2,
423 "\\(NOT, ATTR\\[k1\\]\\{ VALUE\\{ 'v1', \\(nil\\) \\} \\}\\)" },
424 { "attribute.k1 != 'v2'", 3,
425 "\\(NOT, ATTR\\[k1\\]\\{ VALUE\\{ 'v2', \\(nil\\) \\} \\}\\)" },
426 { "attribute.name != 'x' "
427 "AND attribute.y !~ 'x'", 3,
428 "\\(AND, "
429 "\\(NOT, OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\} \\}\\), "
430 "\\(NOT, ATTR\\[y\\]\\{ VALUE\\{ NULL, "PTR_RE" \\} \\}\\)\\)" },
431 };
433 int check, n;
434 size_t i;
436 n = 0;
437 check = sdb_store_lookup(NULL, lookup_cb, &n);
438 fail_unless(check == 0,
439 "sdb_store_lookup() = %d; expected: 0", check);
440 fail_unless(n == 3,
441 "sdb_store_lookup called callback %d times; expected: 3", (int)n);
443 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
444 sdb_store_matcher_t *m;
445 char buf[4096];
447 m = sdb_fe_parse_matcher(golden_data[i].query, -1);
448 fail_unless(m != NULL,
449 "sdb_fe_parse_matcher(%s, -1) = NULL; expected: <matcher>",
450 golden_data[i].query);
451 fail_unless(sdb_regmatches(golden_data[i].tostring_re,
452 sdb_store_matcher_tostring(m, buf, sizeof(buf))) == 0,
453 "sdb_fe_parse_matcher(%s, -1) = %s; expected: %s",
454 golden_data[i].query,
455 sdb_store_matcher_tostring(m, buf, sizeof(buf)),
456 golden_data[i].tostring_re);
458 n = 0;
459 sdb_store_lookup(m, lookup_cb, &n);
460 fail_unless(n == golden_data[i].expected,
461 "sdb_store_lookup(matcher{%s}) found %d hosts; expected: %d",
462 golden_data[i].query, n, golden_data[i].expected);
463 sdb_object_deref(SDB_OBJ(m));
464 }
465 }
466 END_TEST
468 Suite *
469 core_store_lookup_suite(void)
470 {
471 Suite *s = suite_create("core::store_lookup");
472 TCase *tc;
474 tc = tcase_create("core");
475 tcase_add_checked_fixture(tc, populate, sdb_store_clear);
476 tcase_add_test(tc, test_store_match_name);
477 tcase_add_test(tc, test_store_match_attr);
478 tcase_add_test(tc, test_store_match_op);
479 tcase_add_test(tc, test_parse_cmp);
480 tcase_add_test(tc, test_lookup);
481 suite_add_tcase(s, tc);
483 return s;
484 } /* core_store_lookup_suite */
486 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */