Code

store_lookup: Added support for <, <=, >=, > when comparing attribute values.
[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                 { "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));
169 END_TEST
171 START_TEST(test_store_match_attr)
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));
246 END_TEST
248 START_TEST(test_store_cond)
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         }
316 END_TEST
318 START_TEST(test_store_match_op)
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));
386 END_TEST
388 START_TEST(test_parse_cmp)
390         sdb_store_matcher_t *check;
392         size_t i;
394         struct {
395                 const char *obj_type;
396                 const char *attr;
397                 const char *op;
398                 const char *value;
399                 int expected;
400         } golden_data[] = {
401                 { "host",      "name", "=",  "hostname", MATCHER_NAME },
402                 { "host",      "name", "!=", "hostname", MATCHER_NOT },
403                 { "host",      "name", "=~", "hostname", MATCHER_NAME },
404                 { "host",      "name", "!~", "hostname", MATCHER_NOT },
405                 { "host",      "attr", "=",  "hostname", -1 },
406                 { "host",      "attr", "!=", "hostname", -1 },
407                 { "host",      "name", "&^", "hostname", -1 },
408                 { "host",      "name", "<",  "hostname", -1 },
409                 { "host",      "name", "<=", "hostname", -1 },
410                 { "host",      "name", ">=", "hostname", -1 },
411                 { "host",      "name", ">",  "hostname", -1 },
412                 { "service",   "name", "=",  "srvname",  MATCHER_NAME },
413                 { "service",   "name", "!=", "srvname",  MATCHER_NOT },
414                 { "service",   "name", "=~", "srvname",  MATCHER_NAME },
415                 { "service",   "name", "!~", "srvname",  MATCHER_NOT },
416                 { "service",   "attr", "=",  "srvname",  -1 },
417                 { "service",   "attr", "!=", "srvname",  -1 },
418                 { "service",   "name", "&^", "srvname",  -1 },
419                 { "service",   "name", "<",  "srvname",  -1 },
420                 { "service",   "name", "<=", "srvname",  -1 },
421                 { "service",   "name", ">=", "srvname",  -1 },
422                 { "service",   "name", ">",  "srvname",  -1 },
423                 { "attribute", "name", "=",  "attrname", MATCHER_NAME },
424                 { "attribute", "name", "!=", "attrname", MATCHER_NOT },
425                 { "attribute", "name", "=~", "attrname", MATCHER_NAME },
426                 { "attribute", "name", "!~", "attrname", MATCHER_NOT },
427                 { "attribute", "name", "<",  "attrname", -1 },
428                 { "attribute", "name", "<=", "attrname", -1 },
429                 { "attribute", "name", ">=", "attrname", -1 },
430                 { "attribute", "name", ">",  "attrname", -1 },
431                 { "attribute", "attr", "=",  "attrname", MATCHER_ATTR },
432                 { "attribute", "attr", "!=", "attrname", MATCHER_NOT },
433                 { "attribute", "attr", "=~", "attrname", MATCHER_ATTR },
434                 { "attribute", "attr", "!~", "attrname", MATCHER_NOT },
435                 { "attribute", "attr", "&^", "attrname", -1 },
436                 { "attribute", "attr", "<",  "attrname", MATCHER_LT },
437                 { "attribute", "attr", "<=", "attrname", MATCHER_LE },
438 /*              { "attribute", "attr", "=",  "attrname", MATCHER_EQ }, */
439                 { "attribute", "attr", ">=", "attrname", MATCHER_GE },
440                 { "attribute", "attr", ">",  "attrname", MATCHER_GT },
441                 { "foo",       "name", "=",  "bar",      -1 },
442                 { "foo",       "attr", "=",  "bar",      -1 },
443         };
445         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
446                 check = sdb_store_matcher_parse_cmp(golden_data[i].obj_type,
447                                 golden_data[i].attr, golden_data[i].op, golden_data[i].value);
449                 if (golden_data[i].expected == -1) {
450                         fail_unless(check == NULL,
451                                         "sdb_store_matcher_parse_cmp(%s, %s, %s, %s) = %p; "
452                                         "expected: NULL", golden_data[i].obj_type,
453                                         golden_data[i].attr, golden_data[i].op,
454                                         golden_data[i].value, check);
455                         continue;
456                 }
458                 fail_unless(check != NULL,
459                                 "sdb_store_matcher_parse_cmp(%s, %s, %s, %s) = %p; "
460                                 "expected: NULL", golden_data[i].obj_type,
461                                 golden_data[i].attr, golden_data[i].op,
462                                 golden_data[i].value, check);
463                 fail_unless(M(check)->type == golden_data[i].expected,
464                                 "sdb_store_matcher_parse_cmp(%s, %s, %s, %s) returned matcher "
465                                 "of type %d; expected: %d", golden_data[i].obj_type,
466                                 golden_data[i].attr, golden_data[i].op, golden_data[i].value,
467                                 M(check)->type, golden_data[i].expected);
469                 sdb_object_deref(SDB_OBJ(check));
470         }
472 END_TEST
474 static int
475 lookup_cb(sdb_store_base_t *obj, void *user_data)
477         int *i = user_data;
479         fail_unless(obj != NULL,
480                         "sdb_store_lookup callback received NULL obj; expected: "
481                         "<store base obj>");
482         fail_unless(i != NULL,
483                         "sdb_store_lookup callback received NULL user_data; "
484                         "expected: <pointer to data>");
486         ++(*i);
487         return 0;
488 } /* lookup_cb */
490 START_TEST(test_lookup)
492 #define PTR_RE "0x[0-9a-f]+"
493         struct {
494                 const char *query;
495                 int expected;
496                 const char *tostring_re;
497         } golden_data[] = {
498                 { "host.name = 'a'",       1,
499                         "OBJ\\[host\\]\\{ NAME\\{ 'a', \\(nil\\) \\} \\}" },
500                 { "host.name =~ 'a|b'",    2,
501                         "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
502                 { "host.name =~ 'host'",   0,
503                         "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
504                 { "host.name =~ '.'",      3,
505                         "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
506                 { "service.name = 's1'",   2,
507                         "OBJ\\[service\\]\\{ NAME\\{ 's1', \\(nil\\) } \\}" },
508                 { "service.name =~ 's'",   2,
509                         "OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}" },
510                 { "service.name !~ 's'",   1,
511                         "\\(NOT, OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}\\)" },
512                 { "attribute.name = 'k1'", 1,
513                         "OBJ\\[attribute\\]\\{ NAME\\{ 'k1', \\(nil\\) \\} " },
514                 { "attribute.name = 'x'",  0,
515                         "OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\}" },
516                 { "attribute.k1 = 'v1'",   1,
517                         "ATTR\\[k1\\]\\{ VALUE\\{ 'v1', \\(nil\\) \\} \\}" },
518                 { "attribute.k1 != 'v1'",  2,
519                         "\\(NOT, ATTR\\[k1\\]\\{ VALUE\\{ 'v1', \\(nil\\) \\} \\}\\)" },
520                 { "attribute.k1 != 'v2'",  3,
521                         "\\(NOT, ATTR\\[k1\\]\\{ VALUE\\{ 'v2', \\(nil\\) \\} \\}\\)" },
522                 { "attribute.name != 'x' "
523                   "AND attribute.y !~ 'x'", 3,
524                         "\\(AND, "
525                                 "\\(NOT, OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\} \\}\\), "
526                                 "\\(NOT, ATTR\\[y\\]\\{ VALUE\\{ NULL, "PTR_RE" \\} \\}\\)\\)" },
527         };
529         int check, n;
530         size_t i;
532         n = 0;
533         check = sdb_store_lookup(NULL, lookup_cb, &n);
534         fail_unless(check == 0,
535                         "sdb_store_lookup() = %d; expected: 0", check);
536         fail_unless(n == 3,
537                         "sdb_store_lookup called callback %d times; expected: 3", (int)n);
539         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
540                 sdb_store_matcher_t *m;
541                 char buf[4096];
543                 m = sdb_fe_parse_matcher(golden_data[i].query, -1);
544                 fail_unless(m != NULL,
545                                 "sdb_fe_parse_matcher(%s, -1) = NULL; expected: <matcher>",
546                                 golden_data[i].query);
547                 fail_unless(sdb_regmatches(golden_data[i].tostring_re,
548                                         sdb_store_matcher_tostring(m, buf, sizeof(buf))) == 0,
549                                 "sdb_fe_parse_matcher(%s, -1) = %s; expected: %s",
550                                 golden_data[i].query,
551                                 sdb_store_matcher_tostring(m, buf, sizeof(buf)),
552                                 golden_data[i].tostring_re);
554                 n = 0;
555                 sdb_store_lookup(m, lookup_cb, &n);
556                 fail_unless(n == golden_data[i].expected,
557                                 "sdb_store_lookup(matcher{%s}) found %d hosts; expected: %d",
558                                 golden_data[i].query, n, golden_data[i].expected);
559                 sdb_object_deref(SDB_OBJ(m));
560         }
562 END_TEST
564 Suite *
565 core_store_lookup_suite(void)
567         Suite *s = suite_create("core::store_lookup");
568         TCase *tc;
570         tc = tcase_create("core");
571         tcase_add_checked_fixture(tc, populate, sdb_store_clear);
572         tcase_add_test(tc, test_store_match_name);
573         tcase_add_test(tc, test_store_match_attr);
574         tcase_add_test(tc, test_store_cond);
575         tcase_add_test(tc, test_store_match_op);
576         tcase_add_test(tc, test_parse_cmp);
577         tcase_add_test(tc, test_lookup);
578         suite_add_tcase(s, tc);
580         return s;
581 } /* core_store_lookup_suite */
583 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */