Code

store/frontend: Added support for integer and float comparison.
[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;
306                         char m_str[1024];
308                         m = tests[j].matcher(c);
309                         fail_unless(m != NULL,
310                                         "sdb_store_<cond>_matcher() = NULL; expected: <matcher>");
312                         status = sdb_store_matcher_matches(m, obj);
313                         fail_unless(status == *tests[j].expected,
314                                         "sdb_store_matcher_matches(%s) = %d; expected: %d",
315                                         sdb_store_matcher_tostring(m, m_str, sizeof(m_str)),
316                                         status, *tests[j].expected);
318                         sdb_object_deref(SDB_OBJ(m));
319                 }
321                 sdb_object_deref(SDB_OBJ(c));
322         }
324         sdb_object_deref(SDB_OBJ(obj));
326 END_TEST
328 START_TEST(test_store_match_op)
330         sdb_store_base_t *obj;
332         sdb_store_matcher_t *always = sdb_store_name_matcher(SDB_HOST, "a", 0);
333         sdb_store_matcher_t *never = sdb_store_name_matcher(SDB_HOST, "z", 0);
335         struct {
336                 const char *op;
337                 sdb_store_matcher_t *left;
338                 sdb_store_matcher_t *right;
339                 int expected;
340         } golden_data[] = {
341                 { "OR",  always, always, 1 },
342                 { "OR",  always, never,  1 },
343                 { "OR",  never,  always, 1 },
344                 { "OR",  never,  never,  0 },
345                 { "AND", always, always, 1 },
346                 { "AND", always, never,  0 },
347                 { "AND", never,  always, 0 },
348                 { "AND", never,  never,  0 },
349         };
351         int status;
352         size_t i;
354         obj = sdb_store_get_host("a");
356         status = sdb_store_matcher_matches(always, obj);
357         fail_unless(status == 1,
358                         "INTERNAL ERROR: 'always' did not match host");
359         status = sdb_store_matcher_matches(never, obj);
360         fail_unless(status == 0,
361                         "INTERNAL ERROR: 'never' matches host");
363         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
364                 sdb_store_matcher_t *m;
366                 if (! strcmp(golden_data[i].op, "OR"))
367                         m = sdb_store_dis_matcher(golden_data[i].left,
368                                         golden_data[i].right);
369                 else if (! strcmp(golden_data[i].op, "AND"))
370                         m = sdb_store_con_matcher(golden_data[i].left,
371                                         golden_data[i].right);
372                 else {
373                         fail("INTERNAL ERROR: unexpected operator %s", golden_data[i].op);
374                         continue;
375                 }
377 #define TO_NAME(v) (((v) == always) ? "always" \
378                 : ((v) == never) ? "never" : "<unknown>")
380                 status = sdb_store_matcher_matches(m, obj);
381                 fail_unless(status == golden_data[i].expected,
382                                 "%s(%s, %s) = %d; expected: %d", golden_data[i].op,
383                                 TO_NAME(golden_data[i].left), TO_NAME(golden_data[i].right),
384                                 status, golden_data[i].expected);
386 #undef TO_NAME
388                 sdb_object_deref(SDB_OBJ(m));
389         }
391         sdb_object_deref(SDB_OBJ(always));
392         sdb_object_deref(SDB_OBJ(never));
394         sdb_object_deref(SDB_OBJ(obj));
396 END_TEST
398 START_TEST(test_parse_cmp)
400         sdb_data_t hostname = { SDB_TYPE_STRING, { .string = "hostname" } };
401         sdb_data_t srvname  = { SDB_TYPE_STRING, { .string = "srvname" } };
402         sdb_data_t attrname = { SDB_TYPE_STRING, { .string = "attrname" } };
404         sdb_store_matcher_t *check;
406         size_t i;
408         struct {
409                 const char *obj_type;
410                 const char *attr;
411                 const char *op;
412                 const sdb_data_t value;
413                 int expected;
414         } golden_data[] = {
415                 { "host",      "name", "=",  hostname, MATCHER_NAME },
416                 { "host",      "name", "!=", hostname, MATCHER_NOT },
417                 { "host",      "name", "=~", hostname, MATCHER_NAME },
418                 { "host",      "name", "!~", hostname, MATCHER_NOT },
419                 { "host",      "attr", "=",  hostname, -1 },
420                 { "host",      "attr", "!=", hostname, -1 },
421                 { "host",      "name", "&^", hostname, -1 },
422                 { "host",      "name", "<",  hostname, -1 },
423                 { "host",      "name", "<=", hostname, -1 },
424                 { "host",      "name", ">=", hostname, -1 },
425                 { "host",      "name", ">",  hostname, -1 },
426                 { "service",   "name", "=",  srvname,  MATCHER_NAME },
427                 { "service",   "name", "!=", srvname,  MATCHER_NOT },
428                 { "service",   "name", "=~", srvname,  MATCHER_NAME },
429                 { "service",   "name", "!~", srvname,  MATCHER_NOT },
430                 { "service",   "attr", "=",  srvname,  -1 },
431                 { "service",   "attr", "!=", srvname,  -1 },
432                 { "service",   "name", "&^", srvname,  -1 },
433                 { "service",   "name", "<",  srvname,  -1 },
434                 { "service",   "name", "<=", srvname,  -1 },
435                 { "service",   "name", ">=", srvname,  -1 },
436                 { "service",   "name", ">",  srvname,  -1 },
437                 { "attribute", "name", "=",  attrname, MATCHER_NAME },
438                 { "attribute", "name", "!=", attrname, MATCHER_NOT },
439                 { "attribute", "name", "=~", attrname, MATCHER_NAME },
440                 { "attribute", "name", "!~", attrname, MATCHER_NOT },
441                 { "attribute", "name", "<",  attrname, -1 },
442                 { "attribute", "name", "<=", attrname, -1 },
443                 { "attribute", "name", ">=", attrname, -1 },
444                 { "attribute", "name", ">",  attrname, -1 },
445                 { "attribute", "attr", "=",  attrname, MATCHER_ATTR },
446                 { "attribute", "attr", "!=", attrname, MATCHER_NOT },
447                 { "attribute", "attr", "=~", attrname, MATCHER_ATTR },
448                 { "attribute", "attr", "!~", attrname, MATCHER_NOT },
449                 { "attribute", "attr", "&^", attrname, -1 },
450                 { "attribute", "attr", "<",  attrname, MATCHER_LT },
451                 { "attribute", "attr", "<=", attrname, MATCHER_LE },
452 /*              { "attribute", "attr", "=",  attrname, MATCHER_EQ }, */
453                 { "attribute", "attr", ">=", attrname, MATCHER_GE },
454                 { "attribute", "attr", ">",  attrname, MATCHER_GT },
455                 { "foo",       "name", "=",  attrname, -1 },
456                 { "foo",       "attr", "=",  attrname, -1 },
457         };
459         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
460                 char buf[1024];
462                 check = sdb_store_matcher_parse_cmp(golden_data[i].obj_type,
463                                 golden_data[i].attr, golden_data[i].op,
464                                 &golden_data[i].value);
466                 if (sdb_data_format(&golden_data[i].value,
467                                         buf, sizeof(buf), SDB_UNQUOTED) < 0)
468                         snprintf(buf, sizeof(buf), "ERR");
470                 if (golden_data[i].expected == -1) {
471                         fail_unless(check == NULL,
472                                         "sdb_store_matcher_parse_cmp(%s, %s, %s, %s) = %p; "
473                                         "expected: NULL", golden_data[i].obj_type,
474                                         golden_data[i].attr, golden_data[i].op, buf, check);
475                         continue;
476                 }
478                 fail_unless(check != NULL,
479                                 "sdb_store_matcher_parse_cmp(%s, %s, %s, %s) = %p; "
480                                 "expected: NULL", golden_data[i].obj_type,
481                                 golden_data[i].attr, golden_data[i].op, buf, check);
482                 fail_unless(M(check)->type == golden_data[i].expected,
483                                 "sdb_store_matcher_parse_cmp(%s, %s, %s, %s) returned matcher "
484                                 "of type %d; expected: %d", golden_data[i].obj_type,
485                                 golden_data[i].attr, golden_data[i].op, buf,
486                                 M(check)->type, golden_data[i].expected);
488                 sdb_object_deref(SDB_OBJ(check));
489         }
491 END_TEST
493 static int
494 lookup_cb(sdb_store_base_t *obj, void *user_data)
496         int *i = user_data;
498         fail_unless(obj != NULL,
499                         "sdb_store_lookup callback received NULL obj; expected: "
500                         "<store base obj>");
501         fail_unless(i != NULL,
502                         "sdb_store_lookup callback received NULL user_data; "
503                         "expected: <pointer to data>");
505         ++(*i);
506         return 0;
507 } /* lookup_cb */
509 START_TEST(test_lookup)
511 #define PTR_RE "0x[0-9a-f]+"
512         struct {
513                 const char *query;
514                 int expected;
515                 const char *tostring_re;
516         } golden_data[] = {
517                 { "host.name = 'a'",       1,
518                         "OBJ\\[host\\]\\{ NAME\\{ 'a', \\(nil\\) \\} \\}" },
519                 { "host.name =~ 'a|b'",    2,
520                         "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
521                 { "host.name =~ 'host'",   0,
522                         "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
523                 { "host.name =~ '.'",      3,
524                         "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
525                 { "service.name = 's1'",   2,
526                         "OBJ\\[service\\]\\{ NAME\\{ 's1', \\(nil\\) } \\}" },
527                 { "service.name =~ 's'",   2,
528                         "OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}" },
529                 { "service.name !~ 's'",   1,
530                         "\\(NOT, OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}\\)" },
531                 { "attribute.name = 'k1'", 1,
532                         "OBJ\\[attribute\\]\\{ NAME\\{ 'k1', \\(nil\\) \\} " },
533                 { "attribute.name = 'x'",  0,
534                         "OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\}" },
535                 { "attribute.k1 = 'v1'",   1,
536                         "ATTR\\[k1\\]\\{ VALUE\\{ 'v1', \\(nil\\) \\} \\}" },
537                 { "attribute.k2 < 123",    0,
538                         "ATTR\\[k2\\]\\{ < 123 \\}" },
539                 { "attribute.k2 <= 123",   1,
540                         "ATTR\\[k2\\]\\{ <= 123 \\}" },
541                 { "attribute.k2 >= 123",   1,
542                         "ATTR\\[k2\\]\\{ >= 123 \\}" },
543                 { "attribute.k2 > 123",    0,
544                         "ATTR\\[k2\\]\\{ > 123 \\}" },
545                 { "attribute.k2 = 123",    1,
546                         "ATTR\\[k2\\]\\{ = 123 \\}" },
547                 { "attribute.k2 != 123",   2,
548                         "\\(NOT, ATTR\\[k2\\]\\{ = 123 \\}\\)" },
549                 { "attribute.k1 != 'v1'",  2,
550                         "\\(NOT, ATTR\\[k1\\]\\{ VALUE\\{ 'v1', \\(nil\\) \\} \\}\\)" },
551                 { "attribute.k1 != 'v2'",  3,
552                         "\\(NOT, ATTR\\[k1\\]\\{ VALUE\\{ 'v2', \\(nil\\) \\} \\}\\)" },
553                 { "attribute.name != 'x' "
554                   "AND attribute.y !~ 'x'", 3,
555                         "\\(AND, "
556                                 "\\(NOT, OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\} \\}\\), "
557                                 "\\(NOT, ATTR\\[y\\]\\{ VALUE\\{ NULL, "PTR_RE" \\} \\}\\)\\)" },
558         };
560         int check, n;
561         size_t i;
563         n = 0;
564         check = sdb_store_lookup(NULL, lookup_cb, &n);
565         fail_unless(check == 0,
566                         "sdb_store_lookup() = %d; expected: 0", check);
567         fail_unless(n == 3,
568                         "sdb_store_lookup called callback %d times; expected: 3", (int)n);
570         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
571                 sdb_store_matcher_t *m;
572                 char buf[4096];
574                 m = sdb_fe_parse_matcher(golden_data[i].query, -1);
575                 fail_unless(m != NULL,
576                                 "sdb_fe_parse_matcher(%s, -1) = NULL; expected: <matcher>",
577                                 golden_data[i].query);
578                 fail_unless(sdb_regmatches(golden_data[i].tostring_re,
579                                         sdb_store_matcher_tostring(m, buf, sizeof(buf))) == 0,
580                                 "sdb_fe_parse_matcher(%s, -1) = %s; expected: %s",
581                                 golden_data[i].query,
582                                 sdb_store_matcher_tostring(m, buf, sizeof(buf)),
583                                 golden_data[i].tostring_re);
585                 n = 0;
586                 sdb_store_lookup(m, lookup_cb, &n);
587                 fail_unless(n == golden_data[i].expected,
588                                 "sdb_store_lookup(matcher{%s}) found %d hosts; expected: %d",
589                                 golden_data[i].query, n, golden_data[i].expected);
590                 sdb_object_deref(SDB_OBJ(m));
591         }
593 END_TEST
595 Suite *
596 core_store_lookup_suite(void)
598         Suite *s = suite_create("core::store_lookup");
599         TCase *tc;
601         tc = tcase_create("core");
602         tcase_add_checked_fixture(tc, populate, sdb_store_clear);
603         tcase_add_test(tc, test_store_match_name);
604         tcase_add_test(tc, test_store_match_attr);
605         tcase_add_test(tc, test_store_cond);
606         tcase_add_test(tc, test_store_match_op);
607         tcase_add_test(tc, test_parse_cmp);
608         tcase_add_test(tc, test_lookup);
609         suite_add_tcase(s, tc);
611         return s;
612 } /* core_store_lookup_suite */
614 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */