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_obj_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_obj_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 { "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 { "k3", "1", 0, 0 },
197 { "k3", "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 fail_unless(sdb_store_attr_matcher(NULL, "re", 0) == NULL,
207 "sdb_store_attr_matcher(NULL, \"re\", 0) = <m>; expected: NULL");
208 fail_unless(sdb_store_attr_matcher(NULL, "re", 1) == NULL,
209 "sdb_store_attr_matcher(NULL, \"re\", 1) = <m>; expected: NULL");
211 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
212 sdb_store_matcher_t *m, *n;
213 char buf[1024];
214 int status;
216 m = sdb_store_attr_matcher(golden_data[i].attr_name,
217 golden_data[i].attr_value, golden_data[i].re);
218 fail_unless(m != NULL,
219 "sdb_store_attr_matcher() = NULL; expected: <matcher>");
221 status = sdb_store_matcher_matches(m, obj);
222 fail_unless(status == golden_data[i].expected,
223 "sdb_store_matcher_matches({%s, <host a>) = %d; expected: %d",
224 sdb_store_matcher_tostring(m, buf, sizeof(buf)),
225 status, golden_data[i].expected);
227 n = sdb_store_inv_matcher(m);
228 fail_unless(n != NULL,
229 "sdb_store_inv_matcher() = NULL; expected: <matcher>");
230 sdb_object_deref(SDB_OBJ(m));
232 /* now match the inverted set of objects */
233 status = sdb_store_matcher_matches(n, obj);
234 fail_unless(status == !golden_data[i].expected,
235 "sdb_store_matcher_matches({%s, <host a>) = %d; expected: %d",
236 sdb_store_matcher_tostring(n, buf, sizeof(buf)),
237 status, !golden_data[i].expected);
239 sdb_object_deref(SDB_OBJ(n));
240 }
242 sdb_object_deref(SDB_OBJ(obj));
243 }
244 END_TEST
246 START_TEST(test_store_cond)
247 {
248 sdb_store_obj_t *obj;
250 struct {
251 const char *attr;
252 const sdb_data_t value;
253 int expected_lt, expected_le, expected_eq, expected_ge, expected_gt;
254 } golden_data[] = {
255 { "k1", { SDB_TYPE_STRING, { .string = "v1" } }, 0, 1, 1, 1, 0 },
256 { "k1", { SDB_TYPE_STRING, { .string = "v2" } }, 1, 1, 0, 0, 0 },
257 { "k1", { SDB_TYPE_STRING, { .string = "v0" } }, 0, 0, 0, 1, 1 },
258 { "k2", { SDB_TYPE_INTEGER, { .integer = 123 } }, 0, 1, 1, 1, 0 },
259 { "k2", { SDB_TYPE_INTEGER, { .integer = 124 } }, 1, 1, 0, 0, 0 },
260 { "k2", { SDB_TYPE_INTEGER, { .integer = 122 } }, 0, 0, 0, 1, 1 },
261 /* key does not exist */
262 { "k3", { SDB_TYPE_STRING, { .string = "v1" } }, 0, 0, 0, 0, 0 },
263 { "k3", { SDB_TYPE_STRING, { .string = "123" } }, 0, 0, 0, 0, 0 },
264 { "k3", { SDB_TYPE_INTEGER, { .integer = 123 } }, 0, 0, 0, 0, 0 },
265 /* type mismatch */
266 { "k1", { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
267 { "k2", { SDB_TYPE_STRING, { .string = "123" } }, 0, 0, 0, 0, 0 },
268 };
270 int status;
271 size_t i;
273 obj = sdb_store_get_host("a");
274 fail_unless(obj != NULL,
275 "sdb_store_get_host(a) = NULL; expected: <host>");
277 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
278 sdb_store_cond_t *c;
279 char buf[1024];
280 size_t j;
282 struct {
283 sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *);
284 int *expected;
285 } tests[] = {
286 { sdb_store_lt_matcher, &golden_data[i].expected_lt },
287 { sdb_store_le_matcher, &golden_data[i].expected_le },
288 { sdb_store_eq_matcher, &golden_data[i].expected_eq },
289 { sdb_store_ge_matcher, &golden_data[i].expected_ge },
290 { sdb_store_gt_matcher, &golden_data[i].expected_gt },
291 };
293 sdb_data_format(&golden_data[i].value,
294 buf, sizeof(buf), SDB_UNQUOTED);
296 c = sdb_store_attr_cond(golden_data[i].attr,
297 &golden_data[i].value);
298 fail_unless(c != NULL,
299 "sdb_store_attr_cond(%s, %s) = NULL; expected: <cond>",
300 golden_data[i].attr, buf);
302 for (j = 0; j < SDB_STATIC_ARRAY_LEN(tests); ++j) {
303 sdb_store_matcher_t *m;
304 char m_str[1024];
306 m = tests[j].matcher(c);
307 fail_unless(m != NULL,
308 "sdb_store_<cond>_matcher() = NULL; expected: <matcher>");
310 status = sdb_store_matcher_matches(m, obj);
311 fail_unless(status == *tests[j].expected,
312 "sdb_store_matcher_matches(%s) = %d; expected: %d",
313 sdb_store_matcher_tostring(m, m_str, sizeof(m_str)),
314 status, *tests[j].expected);
316 sdb_object_deref(SDB_OBJ(m));
317 }
319 sdb_object_deref(SDB_OBJ(c));
320 }
322 sdb_object_deref(SDB_OBJ(obj));
323 }
324 END_TEST
326 START_TEST(test_store_match_op)
327 {
328 sdb_store_obj_t *obj;
330 sdb_store_matcher_t *always = sdb_store_name_matcher(SDB_HOST, "a", 0);
331 sdb_store_matcher_t *never = sdb_store_name_matcher(SDB_HOST, "z", 0);
333 struct {
334 const char *op;
335 sdb_store_matcher_t *left;
336 sdb_store_matcher_t *right;
337 int expected;
338 } golden_data[] = {
339 { "OR", always, always, 1 },
340 { "OR", always, never, 1 },
341 { "OR", never, always, 1 },
342 { "OR", never, never, 0 },
343 { "AND", always, always, 1 },
344 { "AND", always, never, 0 },
345 { "AND", never, always, 0 },
346 { "AND", never, never, 0 },
347 };
349 int status;
350 size_t i;
352 obj = sdb_store_get_host("a");
354 status = sdb_store_matcher_matches(always, obj);
355 fail_unless(status == 1,
356 "INTERNAL ERROR: 'always' did not match host");
357 status = sdb_store_matcher_matches(never, obj);
358 fail_unless(status == 0,
359 "INTERNAL ERROR: 'never' matches host");
361 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
362 sdb_store_matcher_t *m;
364 if (! strcmp(golden_data[i].op, "OR"))
365 m = sdb_store_dis_matcher(golden_data[i].left,
366 golden_data[i].right);
367 else if (! strcmp(golden_data[i].op, "AND"))
368 m = sdb_store_con_matcher(golden_data[i].left,
369 golden_data[i].right);
370 else {
371 fail("INTERNAL ERROR: unexpected operator %s", golden_data[i].op);
372 continue;
373 }
375 #define TO_NAME(v) (((v) == always) ? "always" \
376 : ((v) == never) ? "never" : "<unknown>")
378 status = sdb_store_matcher_matches(m, obj);
379 fail_unless(status == golden_data[i].expected,
380 "%s(%s, %s) = %d; expected: %d", golden_data[i].op,
381 TO_NAME(golden_data[i].left), TO_NAME(golden_data[i].right),
382 status, golden_data[i].expected);
384 #undef TO_NAME
386 sdb_object_deref(SDB_OBJ(m));
387 }
389 sdb_object_deref(SDB_OBJ(always));
390 sdb_object_deref(SDB_OBJ(never));
392 sdb_object_deref(SDB_OBJ(obj));
393 }
394 END_TEST
396 START_TEST(test_parse_cmp)
397 {
398 sdb_data_t hostname = { SDB_TYPE_STRING, { .string = "hostname" } };
399 sdb_data_t srvname = { SDB_TYPE_STRING, { .string = "srvname" } };
400 sdb_data_t attrname = { SDB_TYPE_STRING, { .string = "attrname" } };
402 sdb_store_matcher_t *check;
404 size_t i;
406 struct {
407 const char *obj_type;
408 const char *attr;
409 const char *op;
410 const sdb_data_t value;
411 int expected;
412 } golden_data[] = {
413 { "host", "name", "=", hostname, MATCHER_NAME },
414 { "host", "name", "!=", hostname, MATCHER_NOT },
415 { "host", "name", "=~", hostname, MATCHER_NAME },
416 { "host", "name", "!~", hostname, MATCHER_NOT },
417 { "host", "attr", "=", hostname, -1 },
418 { "host", "attr", "!=", hostname, -1 },
419 { "host", "name", "&^", hostname, -1 },
420 { "host", "name", "<", hostname, -1 },
421 { "host", "name", "<=", hostname, -1 },
422 { "host", "name", ">=", hostname, -1 },
423 { "host", "name", ">", hostname, -1 },
424 { "service", "name", "=", srvname, MATCHER_NAME },
425 { "service", "name", "!=", srvname, MATCHER_NOT },
426 { "service", "name", "=~", srvname, MATCHER_NAME },
427 { "service", "name", "!~", srvname, MATCHER_NOT },
428 { "service", "attr", "=", srvname, -1 },
429 { "service", "attr", "!=", srvname, -1 },
430 { "service", "name", "&^", srvname, -1 },
431 { "service", "name", "<", srvname, -1 },
432 { "service", "name", "<=", srvname, -1 },
433 { "service", "name", ">=", srvname, -1 },
434 { "service", "name", ">", srvname, -1 },
435 { "attribute", "name", "=", attrname, MATCHER_NAME },
436 { "attribute", "name", "!=", attrname, MATCHER_NOT },
437 { "attribute", "name", "=~", attrname, MATCHER_NAME },
438 { "attribute", "name", "!~", attrname, MATCHER_NOT },
439 { "attribute", "name", "<", attrname, -1 },
440 { "attribute", "name", "<=", attrname, -1 },
441 { "attribute", "name", ">=", attrname, -1 },
442 { "attribute", "name", ">", attrname, -1 },
443 { "attribute", "attr", "=", attrname, MATCHER_ATTR },
444 { "attribute", "attr", "!=", attrname, MATCHER_NOT },
445 { "attribute", "attr", "=~", attrname, MATCHER_ATTR },
446 { "attribute", "attr", "!~", attrname, MATCHER_NOT },
447 { "attribute", "attr", "&^", attrname, -1 },
448 { "attribute", "attr", "<", attrname, MATCHER_LT },
449 { "attribute", "attr", "<=", attrname, MATCHER_LE },
450 /* { "attribute", "attr", "=", attrname, MATCHER_EQ }, */
451 { "attribute", "attr", ">=", attrname, MATCHER_GE },
452 { "attribute", "attr", ">", attrname, MATCHER_GT },
453 { "foo", "name", "=", attrname, -1 },
454 { "foo", "attr", "=", attrname, -1 },
455 };
457 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
458 char buf[1024];
460 check = sdb_store_matcher_parse_cmp(golden_data[i].obj_type,
461 golden_data[i].attr, golden_data[i].op,
462 &golden_data[i].value);
464 if (sdb_data_format(&golden_data[i].value,
465 buf, sizeof(buf), SDB_UNQUOTED) < 0)
466 snprintf(buf, sizeof(buf), "ERR");
468 if (golden_data[i].expected == -1) {
469 fail_unless(check == NULL,
470 "sdb_store_matcher_parse_cmp(%s, %s, %s, %s) = %p; "
471 "expected: NULL", golden_data[i].obj_type,
472 golden_data[i].attr, golden_data[i].op, buf, check);
473 continue;
474 }
476 fail_unless(check != NULL,
477 "sdb_store_matcher_parse_cmp(%s, %s, %s, %s) = %p; "
478 "expected: NULL", golden_data[i].obj_type,
479 golden_data[i].attr, golden_data[i].op, buf, check);
480 fail_unless(M(check)->type == golden_data[i].expected,
481 "sdb_store_matcher_parse_cmp(%s, %s, %s, %s) returned matcher "
482 "of type %d; expected: %d", golden_data[i].obj_type,
483 golden_data[i].attr, golden_data[i].op, buf,
484 M(check)->type, golden_data[i].expected);
486 sdb_object_deref(SDB_OBJ(check));
487 }
488 }
489 END_TEST
491 static int
492 lookup_cb(sdb_store_obj_t *obj, void *user_data)
493 {
494 int *i = user_data;
496 fail_unless(obj != NULL,
497 "sdb_store_lookup callback received NULL obj; expected: "
498 "<store base obj>");
499 fail_unless(i != NULL,
500 "sdb_store_lookup callback received NULL user_data; "
501 "expected: <pointer to data>");
503 ++(*i);
504 return 0;
505 } /* lookup_cb */
507 START_TEST(test_lookup)
508 {
509 #define PTR_RE "0x[0-9a-f]+"
510 struct {
511 const char *query;
512 int expected;
513 const char *tostring_re;
514 } golden_data[] = {
515 { "host.name = 'a'", 1,
516 "OBJ\\[host\\]\\{ NAME\\{ 'a', \\(nil\\) \\} \\}" },
517 { "host.name =~ 'a|b'", 2,
518 "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
519 { "host.name =~ 'host'", 0,
520 "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
521 { "host.name =~ '.'", 3,
522 "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
523 { "service.name = 's1'", 2,
524 "OBJ\\[service\\]\\{ NAME\\{ 's1', \\(nil\\) } \\}" },
525 { "service.name =~ 's'", 2,
526 "OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}" },
527 { "service.name !~ 's'", 1,
528 "\\(NOT, OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}\\)" },
529 { "attribute.name = 'k1'", 1,
530 "OBJ\\[attribute\\]\\{ NAME\\{ 'k1', \\(nil\\) \\} " },
531 { "attribute.name = 'x'", 0,
532 "OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\}" },
533 { "attribute.k1 = 'v1'", 1,
534 "ATTR\\[k1\\]\\{ VALUE\\{ 'v1', \\(nil\\) \\} \\}" },
535 { "attribute.k2 < 123", 0,
536 "ATTR\\[k2\\]\\{ < 123 \\}" },
537 { "attribute.k2 <= 123", 1,
538 "ATTR\\[k2\\]\\{ <= 123 \\}" },
539 { "attribute.k2 >= 123", 1,
540 "ATTR\\[k2\\]\\{ >= 123 \\}" },
541 { "attribute.k2 > 123", 0,
542 "ATTR\\[k2\\]\\{ > 123 \\}" },
543 { "attribute.k2 = 123", 1,
544 "ATTR\\[k2\\]\\{ = 123 \\}" },
545 { "attribute.k2 != 123", 2,
546 "\\(NOT, ATTR\\[k2\\]\\{ = 123 \\}\\)" },
547 { "attribute.k1 != 'v1'", 2,
548 "\\(NOT, ATTR\\[k1\\]\\{ VALUE\\{ 'v1', \\(nil\\) \\} \\}\\)" },
549 { "attribute.k1 != 'v2'", 3,
550 "\\(NOT, ATTR\\[k1\\]\\{ VALUE\\{ 'v2', \\(nil\\) \\} \\}\\)" },
551 { "attribute.name != 'x' "
552 "AND attribute.y !~ 'x'", 3,
553 "\\(AND, "
554 "\\(NOT, OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\} \\}\\), "
555 "\\(NOT, ATTR\\[y\\]\\{ VALUE\\{ NULL, "PTR_RE" \\} \\}\\)\\)" },
556 };
558 int check, n;
559 size_t i;
561 n = 0;
562 check = sdb_store_lookup(NULL, lookup_cb, &n);
563 fail_unless(check == 0,
564 "sdb_store_lookup() = %d; expected: 0", check);
565 fail_unless(n == 3,
566 "sdb_store_lookup called callback %d times; expected: 3", (int)n);
568 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
569 sdb_store_matcher_t *m;
570 char buf[4096];
572 m = sdb_fe_parse_matcher(golden_data[i].query, -1);
573 fail_unless(m != NULL,
574 "sdb_fe_parse_matcher(%s, -1) = NULL; expected: <matcher>",
575 golden_data[i].query);
576 fail_unless(sdb_regmatches(golden_data[i].tostring_re,
577 sdb_store_matcher_tostring(m, buf, sizeof(buf))) == 0,
578 "sdb_fe_parse_matcher(%s, -1) = %s; expected: %s",
579 golden_data[i].query,
580 sdb_store_matcher_tostring(m, buf, sizeof(buf)),
581 golden_data[i].tostring_re);
583 n = 0;
584 sdb_store_lookup(m, lookup_cb, &n);
585 fail_unless(n == golden_data[i].expected,
586 "sdb_store_lookup(matcher{%s}) found %d hosts; expected: %d",
587 golden_data[i].query, n, golden_data[i].expected);
588 sdb_object_deref(SDB_OBJ(m));
589 }
590 }
591 END_TEST
593 Suite *
594 core_store_lookup_suite(void)
595 {
596 Suite *s = suite_create("core::store_lookup");
597 TCase *tc;
599 tc = tcase_create("core");
600 tcase_add_checked_fixture(tc, populate, sdb_store_clear);
601 tcase_add_test(tc, test_store_match_name);
602 tcase_add_test(tc, test_store_match_attr);
603 tcase_add_test(tc, test_store_cond);
604 tcase_add_test(tc, test_store_match_op);
605 tcase_add_test(tc, test_parse_cmp);
606 tcase_add_test(tc, test_lookup);
607 suite_add_tcase(s, tc);
609 return s;
610 } /* core_store_lookup_suite */
612 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */