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 { "a", "k2", { SDB_TYPE_INTEGER, { .integer = 123 } } },
58 };
60 size_t i;
62 for (i = 0; i < SDB_STATIC_ARRAY_LEN(hosts); ++i) {
63 int status = sdb_store_host(hosts[i], 1);
64 fail_unless(status == 0,
65 "sdb_store_host(%s, 1) = %d; expected: 0",
66 hosts[i], status);
67 }
69 for (i = 0; i < SDB_STATIC_ARRAY_LEN(services); ++i) {
70 int status = sdb_store_service(services[i].host,
71 services[i].service, 1);
72 fail_unless(status == 0,
73 "sdb_store_service(%s, %s, 1) = %d; expected: 0",
74 services[i].host, services[i].service, status);
75 }
77 for (i = 0; i < SDB_STATIC_ARRAY_LEN(attrs); ++i) {
78 int status = sdb_store_attribute(attrs[i].host,
79 attrs[i].name, &attrs[i].value, 1);
80 fail_unless(status == 0,
81 "sdb_store_attribute(%s, %s, <val>, 1) = %d; expected: 0",
82 attrs[i].host, attrs[i].name, status);
83 }
84 } /* populate */
86 START_TEST(test_store_match_name)
87 {
88 sdb_store_base_t *obj;
90 struct {
91 int type;
92 const char *name;
93 _Bool re;
95 int expected;
96 } golden_data[] = {
97 { SDB_HOST, NULL, 0, 1 },
98 { SDB_HOST, NULL, 1, 1 },
99 { SDB_HOST, "a", 0, 1 },
100 { SDB_HOST, "a", 1, 1 },
101 { SDB_HOST, "b", 0, 0 },
102 { SDB_HOST, "b", 1, 0 },
103 { SDB_HOST, "^a$", 1, 1 },
104 { SDB_HOST, "^b$", 1, 0 },
105 { SDB_HOST, "^a$", 0, 0 },
106 { SDB_HOST, "^b$", 0, 0 },
107 { SDB_SERVICE, NULL, 0, 1 },
108 { SDB_SERVICE, NULL, 1, 1 },
109 { SDB_SERVICE, "s1", 0, 1 },
110 { SDB_SERVICE, "s2", 0, 1 },
111 { SDB_SERVICE, "s3", 0, 0 },
112 { SDB_SERVICE, "s4", 0, 0 },
113 { SDB_SERVICE, "^s1$", 1, 1 },
114 { SDB_SERVICE, "^s1$", 0, 0 },
115 { SDB_SERVICE, "x1", 0, 0 },
116 { SDB_SERVICE, "x1", 1, 0 },
117 { SDB_SERVICE, "x", 1, 0 },
118 { SDB_ATTRIBUTE, NULL, 0, 1 },
119 { SDB_ATTRIBUTE, NULL, 1, 1 },
120 { SDB_ATTRIBUTE, "k1", 0, 1 },
121 { SDB_ATTRIBUTE, "k2", 0, 1 },
122 { SDB_ATTRIBUTE, "k3", 0, 0 },
123 { SDB_ATTRIBUTE, "k4", 0, 0 },
124 { SDB_ATTRIBUTE, "k", 1, 1 },
125 { SDB_ATTRIBUTE, "1", 1, 1 },
126 { SDB_ATTRIBUTE, "3", 1, 0 },
127 };
129 size_t i;
131 obj = sdb_store_get_host("a");
132 fail_unless(obj != NULL,
133 "sdb_store_get_host(a) = NULL; expected: <host>");
135 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
136 sdb_store_matcher_t *m, *n;
137 char buf[1024];
138 int status;
140 m = sdb_store_name_matcher(golden_data[i].type,
141 golden_data[i].name, golden_data[i].re);
142 fail_unless(m != NULL,
143 "sdb_store_service_matcher(%d, %s, %d) = NULL; expected: <matcher>",
144 golden_data[i].type, golden_data[i].name, golden_data[i].re);
146 status = sdb_store_matcher_matches(m, obj);
147 fail_unless(status == golden_data[i].expected,
148 "sdb_store_matcher_matches(%s, <host a>) = %d; expected: %d",
149 sdb_store_matcher_tostring(m, buf, sizeof(buf)),
150 status, golden_data[i].expected);
152 n = sdb_store_inv_matcher(m);
153 fail_unless(n != NULL,
154 "sdb_store_inv_matcher() = NULL; expected: <matcher>");
155 sdb_object_deref(SDB_OBJ(m));
157 /* now match the inverted set of objects */
158 status = sdb_store_matcher_matches(n, obj);
159 fail_unless(status == !golden_data[i].expected,
160 "sdb_store_matcher_matches(%s, <host a>) = %d; expected: %d",
161 sdb_store_matcher_tostring(n, buf, sizeof(buf)),
162 status, !golden_data[i].expected);
164 sdb_object_deref(SDB_OBJ(n));
165 }
167 sdb_object_deref(SDB_OBJ(obj));
168 }
169 END_TEST
171 START_TEST(test_store_match_attr)
172 {
173 sdb_store_base_t *obj;
175 struct {
176 const char *attr_name;
177 const char *attr_value;
178 _Bool re;
180 int expected;
181 } golden_data[] = {
182 { "k1", NULL, 0, 1 },
183 { "k", NULL, 1, 0 },
184 { "1", NULL, 1, 0 },
185 { "k3", NULL, 0, 0 },
186 { "k3", 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", "^v1$", 1, 1 },
192 { "k1", "v", 1, 1 },
193 { "k1", "1", 1, 1 },
194 { "k1", "v2", 0, 0 },
195 { "k1", "v2", 1, 0 },
196 { "k", "v1", 0, 0 },
197 { "k", "v1", 1, 0 },
198 { "k3", "1", 0, 0 },
199 { "k3", "1", 1, 0 },
200 };
202 size_t i;
204 obj = sdb_store_get_host("a");
205 fail_unless(obj != NULL,
206 "sdb_store_get_host(a) = NULL; expected: <host>");
208 fail_unless(sdb_store_attr_matcher(NULL, "re", 0) == NULL,
209 "sdb_store_attr_matcher(NULL, \"re\", 0) = <m>; expected: NULL");
210 fail_unless(sdb_store_attr_matcher(NULL, "re", 1) == NULL,
211 "sdb_store_attr_matcher(NULL, \"re\", 1) = <m>; expected: NULL");
213 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
214 sdb_store_matcher_t *m, *n;
215 char buf[1024];
216 int status;
218 m = sdb_store_attr_matcher(golden_data[i].attr_name,
219 golden_data[i].attr_value, golden_data[i].re);
220 fail_unless(m != NULL,
221 "sdb_store_attr_matcher() = NULL; expected: <matcher>");
223 status = sdb_store_matcher_matches(m, obj);
224 fail_unless(status == golden_data[i].expected,
225 "sdb_store_matcher_matches({%s, <host a>) = %d; expected: %d",
226 sdb_store_matcher_tostring(m, buf, sizeof(buf)),
227 status, golden_data[i].expected);
229 n = sdb_store_inv_matcher(m);
230 fail_unless(n != NULL,
231 "sdb_store_inv_matcher() = NULL; expected: <matcher>");
232 sdb_object_deref(SDB_OBJ(m));
234 /* now match the inverted set of objects */
235 status = sdb_store_matcher_matches(n, obj);
236 fail_unless(status == !golden_data[i].expected,
237 "sdb_store_matcher_matches({%s, <host a>) = %d; expected: %d",
238 sdb_store_matcher_tostring(n, buf, sizeof(buf)),
239 status, !golden_data[i].expected);
241 sdb_object_deref(SDB_OBJ(n));
242 }
244 sdb_object_deref(SDB_OBJ(obj));
245 }
246 END_TEST
248 START_TEST(test_store_cond)
249 {
250 sdb_store_base_t *obj;
252 struct {
253 const char *attr;
254 const sdb_data_t value;
255 int expected_lt, expected_le, expected_eq, expected_ge, expected_gt;
256 } golden_data[] = {
257 { "k1", { SDB_TYPE_STRING, { .string = "v1" } }, 0, 1, 1, 1, 0 },
258 { "k1", { SDB_TYPE_STRING, { .string = "v2" } }, 1, 1, 0, 0, 0 },
259 { "k1", { SDB_TYPE_STRING, { .string = "v0" } }, 0, 0, 0, 1, 1 },
260 { "k2", { SDB_TYPE_INTEGER, { .integer = 123 } }, 0, 1, 1, 1, 0 },
261 { "k2", { SDB_TYPE_INTEGER, { .integer = 124 } }, 1, 1, 0, 0, 0 },
262 { "k2", { SDB_TYPE_INTEGER, { .integer = 122 } }, 0, 0, 0, 1, 1 },
263 /* key does not exist */
264 { "k3", { SDB_TYPE_STRING, { .string = "v1" } }, 0, 0, 0, 0, 0 },
265 { "k3", { SDB_TYPE_STRING, { .string = "123" } }, 0, 0, 0, 0, 0 },
266 { "k3", { SDB_TYPE_INTEGER, { .integer = 123 } }, 0, 0, 0, 0, 0 },
267 /* type mismatch */
268 { "k1", { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
269 { "k2", { SDB_TYPE_STRING, { .string = "123" } }, 0, 0, 0, 0, 0 },
270 };
272 int status;
273 size_t i;
275 obj = sdb_store_get_host("a");
276 fail_unless(obj != NULL,
277 "sdb_store_get_host(a) = NULL; expected: <host>");
279 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
280 sdb_store_cond_t *c;
281 char buf[1024];
282 size_t j;
284 struct {
285 sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *);
286 int *expected;
287 } tests[] = {
288 { sdb_store_lt_matcher, &golden_data[i].expected_lt },
289 { sdb_store_le_matcher, &golden_data[i].expected_le },
290 { sdb_store_eq_matcher, &golden_data[i].expected_eq },
291 { sdb_store_ge_matcher, &golden_data[i].expected_ge },
292 { sdb_store_gt_matcher, &golden_data[i].expected_gt },
293 };
295 sdb_data_format(&golden_data[i].value,
296 buf, sizeof(buf), SDB_UNQUOTED);
298 c = sdb_store_attr_cond(golden_data[i].attr,
299 &golden_data[i].value);
300 fail_unless(c != NULL,
301 "sdb_store_attr_cond(%s, %s) = NULL; expected: <cond>",
302 golden_data[i].attr, buf);
304 for (j = 0; j < SDB_STATIC_ARRAY_LEN(tests); ++j) {
305 sdb_store_matcher_t *m = tests[j].matcher(c);
306 char m_str[1024];
307 sdb_object_deref(SDB_OBJ(c));
308 status = sdb_store_matcher_matches(m, obj);
309 fail_unless(status == *tests[j].expected,
310 "sdb_store_matcher_matches(%s) = %d; expected: %d",
311 sdb_store_matcher_tostring(m, m_str, sizeof(m_str)),
312 status, *tests[j].expected);
313 }
314 }
315 }
316 END_TEST
318 START_TEST(test_store_match_op)
319 {
320 sdb_store_base_t *obj;
322 sdb_store_matcher_t *always = sdb_store_name_matcher(SDB_HOST, "a", 0);
323 sdb_store_matcher_t *never = sdb_store_name_matcher(SDB_HOST, "z", 0);
325 struct {
326 const char *op;
327 sdb_store_matcher_t *left;
328 sdb_store_matcher_t *right;
329 int expected;
330 } golden_data[] = {
331 { "OR", always, always, 1 },
332 { "OR", always, never, 1 },
333 { "OR", never, always, 1 },
334 { "OR", never, never, 0 },
335 { "AND", always, always, 1 },
336 { "AND", always, never, 0 },
337 { "AND", never, always, 0 },
338 { "AND", never, never, 0 },
339 };
341 int status;
342 size_t i;
344 obj = sdb_store_get_host("a");
346 status = sdb_store_matcher_matches(always, obj);
347 fail_unless(status == 1,
348 "INTERNAL ERROR: 'always' did not match host");
349 status = sdb_store_matcher_matches(never, obj);
350 fail_unless(status == 0,
351 "INTERNAL ERROR: 'never' matches host");
353 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
354 sdb_store_matcher_t *m;
356 if (! strcmp(golden_data[i].op, "OR"))
357 m = sdb_store_dis_matcher(golden_data[i].left,
358 golden_data[i].right);
359 else if (! strcmp(golden_data[i].op, "AND"))
360 m = sdb_store_con_matcher(golden_data[i].left,
361 golden_data[i].right);
362 else {
363 fail("INTERNAL ERROR: unexpected operator %s", golden_data[i].op);
364 continue;
365 }
367 #define TO_NAME(v) (((v) == always) ? "always" \
368 : ((v) == never) ? "never" : "<unknown>")
370 status = sdb_store_matcher_matches(m, obj);
371 fail_unless(status == golden_data[i].expected,
372 "%s(%s, %s) = %d; expected: %d", golden_data[i].op,
373 TO_NAME(golden_data[i].left), TO_NAME(golden_data[i].right),
374 status, golden_data[i].expected);
376 #undef TO_NAME
378 sdb_object_deref(SDB_OBJ(m));
379 }
381 sdb_object_deref(SDB_OBJ(always));
382 sdb_object_deref(SDB_OBJ(never));
384 sdb_object_deref(SDB_OBJ(obj));
385 }
386 END_TEST
388 START_TEST(test_parse_cmp)
389 {
390 sdb_data_t hostname = { SDB_TYPE_STRING, { .string = "hostname" } };
391 sdb_data_t srvname = { SDB_TYPE_STRING, { .string = "srvname" } };
392 sdb_data_t attrname = { SDB_TYPE_STRING, { .string = "attrname" } };
394 sdb_store_matcher_t *check;
396 size_t i;
398 struct {
399 const char *obj_type;
400 const char *attr;
401 const char *op;
402 const sdb_data_t value;
403 int expected;
404 } golden_data[] = {
405 { "host", "name", "=", hostname, MATCHER_NAME },
406 { "host", "name", "!=", hostname, MATCHER_NOT },
407 { "host", "name", "=~", hostname, MATCHER_NAME },
408 { "host", "name", "!~", hostname, MATCHER_NOT },
409 { "host", "attr", "=", hostname, -1 },
410 { "host", "attr", "!=", hostname, -1 },
411 { "host", "name", "&^", hostname, -1 },
412 { "host", "name", "<", hostname, -1 },
413 { "host", "name", "<=", hostname, -1 },
414 { "host", "name", ">=", hostname, -1 },
415 { "host", "name", ">", hostname, -1 },
416 { "service", "name", "=", srvname, MATCHER_NAME },
417 { "service", "name", "!=", srvname, MATCHER_NOT },
418 { "service", "name", "=~", srvname, MATCHER_NAME },
419 { "service", "name", "!~", srvname, MATCHER_NOT },
420 { "service", "attr", "=", srvname, -1 },
421 { "service", "attr", "!=", srvname, -1 },
422 { "service", "name", "&^", srvname, -1 },
423 { "service", "name", "<", srvname, -1 },
424 { "service", "name", "<=", srvname, -1 },
425 { "service", "name", ">=", srvname, -1 },
426 { "service", "name", ">", srvname, -1 },
427 { "attribute", "name", "=", attrname, MATCHER_NAME },
428 { "attribute", "name", "!=", attrname, MATCHER_NOT },
429 { "attribute", "name", "=~", attrname, MATCHER_NAME },
430 { "attribute", "name", "!~", attrname, MATCHER_NOT },
431 { "attribute", "name", "<", attrname, -1 },
432 { "attribute", "name", "<=", attrname, -1 },
433 { "attribute", "name", ">=", attrname, -1 },
434 { "attribute", "name", ">", attrname, -1 },
435 { "attribute", "attr", "=", attrname, MATCHER_ATTR },
436 { "attribute", "attr", "!=", attrname, MATCHER_NOT },
437 { "attribute", "attr", "=~", attrname, MATCHER_ATTR },
438 { "attribute", "attr", "!~", attrname, MATCHER_NOT },
439 { "attribute", "attr", "&^", attrname, -1 },
440 { "attribute", "attr", "<", attrname, MATCHER_LT },
441 { "attribute", "attr", "<=", attrname, MATCHER_LE },
442 /* { "attribute", "attr", "=", attrname, MATCHER_EQ }, */
443 { "attribute", "attr", ">=", attrname, MATCHER_GE },
444 { "attribute", "attr", ">", attrname, MATCHER_GT },
445 { "foo", "name", "=", attrname, -1 },
446 { "foo", "attr", "=", attrname, -1 },
447 };
449 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
450 char buf[1024];
452 check = sdb_store_matcher_parse_cmp(golden_data[i].obj_type,
453 golden_data[i].attr, golden_data[i].op,
454 &golden_data[i].value);
456 if (sdb_data_format(&golden_data[i].value,
457 buf, sizeof(buf), SDB_UNQUOTED) < 0)
458 snprintf(buf, sizeof(buf), "ERR");
460 if (golden_data[i].expected == -1) {
461 fail_unless(check == NULL,
462 "sdb_store_matcher_parse_cmp(%s, %s, %s, %s) = %p; "
463 "expected: NULL", golden_data[i].obj_type,
464 golden_data[i].attr, golden_data[i].op, buf, check);
465 continue;
466 }
468 fail_unless(check != NULL,
469 "sdb_store_matcher_parse_cmp(%s, %s, %s, %s) = %p; "
470 "expected: NULL", golden_data[i].obj_type,
471 golden_data[i].attr, golden_data[i].op, buf, check);
472 fail_unless(M(check)->type == golden_data[i].expected,
473 "sdb_store_matcher_parse_cmp(%s, %s, %s, %s) returned matcher "
474 "of type %d; expected: %d", golden_data[i].obj_type,
475 golden_data[i].attr, golden_data[i].op, buf,
476 M(check)->type, golden_data[i].expected);
478 sdb_object_deref(SDB_OBJ(check));
479 }
480 }
481 END_TEST
483 static int
484 lookup_cb(sdb_store_base_t *obj, void *user_data)
485 {
486 int *i = user_data;
488 fail_unless(obj != NULL,
489 "sdb_store_lookup callback received NULL obj; expected: "
490 "<store base obj>");
491 fail_unless(i != NULL,
492 "sdb_store_lookup callback received NULL user_data; "
493 "expected: <pointer to data>");
495 ++(*i);
496 return 0;
497 } /* lookup_cb */
499 START_TEST(test_lookup)
500 {
501 #define PTR_RE "0x[0-9a-f]+"
502 struct {
503 const char *query;
504 int expected;
505 const char *tostring_re;
506 } golden_data[] = {
507 { "host.name = 'a'", 1,
508 "OBJ\\[host\\]\\{ NAME\\{ 'a', \\(nil\\) \\} \\}" },
509 { "host.name =~ 'a|b'", 2,
510 "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
511 { "host.name =~ 'host'", 0,
512 "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
513 { "host.name =~ '.'", 3,
514 "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
515 { "service.name = 's1'", 2,
516 "OBJ\\[service\\]\\{ NAME\\{ 's1', \\(nil\\) } \\}" },
517 { "service.name =~ 's'", 2,
518 "OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}" },
519 { "service.name !~ 's'", 1,
520 "\\(NOT, OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}\\)" },
521 { "attribute.name = 'k1'", 1,
522 "OBJ\\[attribute\\]\\{ NAME\\{ 'k1', \\(nil\\) \\} " },
523 { "attribute.name = 'x'", 0,
524 "OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\}" },
525 { "attribute.k1 = 'v1'", 1,
526 "ATTR\\[k1\\]\\{ VALUE\\{ 'v1', \\(nil\\) \\} \\}" },
527 { "attribute.k1 != 'v1'", 2,
528 "\\(NOT, ATTR\\[k1\\]\\{ VALUE\\{ 'v1', \\(nil\\) \\} \\}\\)" },
529 { "attribute.k1 != 'v2'", 3,
530 "\\(NOT, ATTR\\[k1\\]\\{ VALUE\\{ 'v2', \\(nil\\) \\} \\}\\)" },
531 { "attribute.name != 'x' "
532 "AND attribute.y !~ 'x'", 3,
533 "\\(AND, "
534 "\\(NOT, OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\} \\}\\), "
535 "\\(NOT, ATTR\\[y\\]\\{ VALUE\\{ NULL, "PTR_RE" \\} \\}\\)\\)" },
536 };
538 int check, n;
539 size_t i;
541 n = 0;
542 check = sdb_store_lookup(NULL, lookup_cb, &n);
543 fail_unless(check == 0,
544 "sdb_store_lookup() = %d; expected: 0", check);
545 fail_unless(n == 3,
546 "sdb_store_lookup called callback %d times; expected: 3", (int)n);
548 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
549 sdb_store_matcher_t *m;
550 char buf[4096];
552 m = sdb_fe_parse_matcher(golden_data[i].query, -1);
553 fail_unless(m != NULL,
554 "sdb_fe_parse_matcher(%s, -1) = NULL; expected: <matcher>",
555 golden_data[i].query);
556 fail_unless(sdb_regmatches(golden_data[i].tostring_re,
557 sdb_store_matcher_tostring(m, buf, sizeof(buf))) == 0,
558 "sdb_fe_parse_matcher(%s, -1) = %s; expected: %s",
559 golden_data[i].query,
560 sdb_store_matcher_tostring(m, buf, sizeof(buf)),
561 golden_data[i].tostring_re);
563 n = 0;
564 sdb_store_lookup(m, lookup_cb, &n);
565 fail_unless(n == golden_data[i].expected,
566 "sdb_store_lookup(matcher{%s}) found %d hosts; expected: %d",
567 golden_data[i].query, n, golden_data[i].expected);
568 sdb_object_deref(SDB_OBJ(m));
569 }
570 }
571 END_TEST
573 Suite *
574 core_store_lookup_suite(void)
575 {
576 Suite *s = suite_create("core::store_lookup");
577 TCase *tc;
579 tc = tcase_create("core");
580 tcase_add_checked_fixture(tc, populate, sdb_store_clear);
581 tcase_add_test(tc, test_store_match_name);
582 tcase_add_test(tc, test_store_match_attr);
583 tcase_add_test(tc, test_store_cond);
584 tcase_add_test(tc, test_store_match_op);
585 tcase_add_test(tc, test_parse_cmp);
586 tcase_add_test(tc, test_lookup);
587 suite_add_tcase(s, tc);
589 return s;
590 } /* core_store_lookup_suite */
592 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */