Code

store, frontend: Let lookups support arbitrary expressions for 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_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));
169 END_TEST
171 START_TEST(test_store_match_attr)
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));
244 END_TEST
246 START_TEST(test_store_cond)
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_expr_t *expr;
279                 sdb_store_cond_t *c;
280                 char buf[1024];
281                 size_t j;
283                 struct {
284                         sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *);
285                         int *expected;
286                 } tests[] = {
287                         { sdb_store_lt_matcher, &golden_data[i].expected_lt },
288                         { sdb_store_le_matcher, &golden_data[i].expected_le },
289                         { sdb_store_eq_matcher, &golden_data[i].expected_eq },
290                         { sdb_store_ge_matcher, &golden_data[i].expected_ge },
291                         { sdb_store_gt_matcher, &golden_data[i].expected_gt },
292                 };
294                 sdb_data_format(&golden_data[i].value,
295                                 buf, sizeof(buf), SDB_UNQUOTED);
297                 expr = sdb_store_expr_constvalue(&golden_data[i].value);
298                 fail_unless(expr != NULL,
299                                 "sdb_store_expr_constvalue(%s) = NULL; expected: <expr>",
300                                 buf);
302                 c = sdb_store_attr_cond(golden_data[i].attr, expr);
303                 sdb_object_deref(SDB_OBJ(expr));
304                 fail_unless(c != NULL,
305                                 "sdb_store_attr_cond(%s, expr{%s}) = NULL; expected: <cond>",
306                                 golden_data[i].attr, buf);
308                 for (j = 0; j < SDB_STATIC_ARRAY_LEN(tests); ++j) {
309                         sdb_store_matcher_t *m;
310                         char m_str[1024];
312                         m = tests[j].matcher(c);
313                         fail_unless(m != NULL,
314                                         "sdb_store_<cond>_matcher() = NULL; expected: <matcher>");
316                         status = sdb_store_matcher_matches(m, obj);
317                         fail_unless(status == *tests[j].expected,
318                                         "sdb_store_matcher_matches(%s) = %d; expected: %d",
319                                         sdb_store_matcher_tostring(m, m_str, sizeof(m_str)),
320                                         status, *tests[j].expected);
322                         sdb_object_deref(SDB_OBJ(m));
323                 }
325                 sdb_object_deref(SDB_OBJ(c));
326         }
328         sdb_object_deref(SDB_OBJ(obj));
330 END_TEST
332 START_TEST(test_store_match_op)
334         sdb_store_obj_t *obj;
336         sdb_store_matcher_t *always = sdb_store_name_matcher(SDB_HOST, "a", 0);
337         sdb_store_matcher_t *never = sdb_store_name_matcher(SDB_HOST, "z", 0);
339         struct {
340                 const char *op;
341                 sdb_store_matcher_t *left;
342                 sdb_store_matcher_t *right;
343                 int expected;
344         } golden_data[] = {
345                 { "OR",  always, always, 1 },
346                 { "OR",  always, never,  1 },
347                 { "OR",  never,  always, 1 },
348                 { "OR",  never,  never,  0 },
349                 { "AND", always, always, 1 },
350                 { "AND", always, never,  0 },
351                 { "AND", never,  always, 0 },
352                 { "AND", never,  never,  0 },
353         };
355         int status;
356         size_t i;
358         obj = sdb_store_get_host("a");
360         status = sdb_store_matcher_matches(always, obj);
361         fail_unless(status == 1,
362                         "INTERNAL ERROR: 'always' did not match host");
363         status = sdb_store_matcher_matches(never, obj);
364         fail_unless(status == 0,
365                         "INTERNAL ERROR: 'never' matches host");
367         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
368                 sdb_store_matcher_t *m;
370                 if (! strcmp(golden_data[i].op, "OR"))
371                         m = sdb_store_dis_matcher(golden_data[i].left,
372                                         golden_data[i].right);
373                 else if (! strcmp(golden_data[i].op, "AND"))
374                         m = sdb_store_con_matcher(golden_data[i].left,
375                                         golden_data[i].right);
376                 else {
377                         fail("INTERNAL ERROR: unexpected operator %s", golden_data[i].op);
378                         continue;
379                 }
381 #define TO_NAME(v) (((v) == always) ? "always" \
382                 : ((v) == never) ? "never" : "<unknown>")
384                 status = sdb_store_matcher_matches(m, obj);
385                 fail_unless(status == golden_data[i].expected,
386                                 "%s(%s, %s) = %d; expected: %d", golden_data[i].op,
387                                 TO_NAME(golden_data[i].left), TO_NAME(golden_data[i].right),
388                                 status, golden_data[i].expected);
390 #undef TO_NAME
392                 sdb_object_deref(SDB_OBJ(m));
393         }
395         sdb_object_deref(SDB_OBJ(always));
396         sdb_object_deref(SDB_OBJ(never));
398         sdb_object_deref(SDB_OBJ(obj));
400 END_TEST
402 START_TEST(test_parse_cmp)
404         sdb_data_t hostname = { SDB_TYPE_STRING, { .string = "hostname" } };
405         sdb_data_t srvname  = { SDB_TYPE_STRING, { .string = "srvname" } };
406         sdb_data_t attrname = { SDB_TYPE_STRING, { .string = "attrname" } };
408         sdb_store_matcher_t *check;
410         size_t i;
412         struct {
413                 const char *obj_type;
414                 const char *attr;
415                 const char *op;
416                 const sdb_data_t *value;
417                 int expected;
418         } golden_data[] = {
419                 { "host",      NULL,   "=",  &hostname, MATCHER_NAME },
420                 { "host",      NULL,   "!=", &hostname, MATCHER_NOT },
421                 { "host",      NULL,   "=~", &hostname, MATCHER_NAME },
422                 { "host",      NULL,   "!~", &hostname, MATCHER_NOT },
423                 { "host",      "attr", "=",  &hostname, -1 },
424                 { "host",      "attr", "!=", &hostname, -1 },
425                 { "host",      NULL,   "&^", &hostname, -1 },
426                 { "host",      NULL,   "<",  &hostname, -1 },
427                 { "host",      NULL,   "<=", &hostname, -1 },
428                 { "host",      NULL,   ">=", &hostname, -1 },
429                 { "host",      NULL,   ">",  &hostname, -1 },
430                 { "host",      NULL,   "=",  NULL,      -1 },
431                 { "service",   NULL,   "=",  &srvname,  MATCHER_NAME },
432                 { "service",   NULL,   "!=", &srvname,  MATCHER_NOT },
433                 { "service",   NULL,   "=~", &srvname,  MATCHER_NAME },
434                 { "service",   NULL,   "!~", &srvname,  MATCHER_NOT },
435                 { "service",   "attr", "=",  &srvname,  -1 },
436                 { "service",   "attr", "!=", &srvname,  -1 },
437                 { "service",   NULL,   "&^", &srvname,  -1 },
438                 { "service",   NULL,   "<",  &srvname,  -1 },
439                 { "service",   NULL,   "<=", &srvname,  -1 },
440                 { "service",   NULL,   ">=", &srvname,  -1 },
441                 { "service",   NULL,   ">",  &srvname,  -1 },
442                 { "service",   NULL,   "=",  NULL,      -1 },
443                 { "attribute", NULL,   "=",  &attrname, MATCHER_NAME },
444                 { "attribute", NULL,   "!=", &attrname, MATCHER_NOT },
445                 { "attribute", NULL,   "=~", &attrname, MATCHER_NAME },
446                 { "attribute", NULL,   "!~", &attrname, MATCHER_NOT },
447                 { "attribute", NULL,   "<",  &attrname, -1 },
448                 { "attribute", NULL,   "<=", &attrname, -1 },
449                 { "attribute", NULL,   ">=", &attrname, -1 },
450                 { "attribute", NULL,   ">",  &attrname, -1 },
451                 { "attribute", NULL,   "=",  NULL,      -1 },
452                 { "attribute", "attr", "=",  &attrname, MATCHER_ATTR },
453                 { "attribute", "attr", "!=", &attrname, MATCHER_NOT },
454                 { "attribute", "attr", "=~", &attrname, MATCHER_ATTR },
455                 { "attribute", "attr", "!~", &attrname, MATCHER_NOT },
456                 { "attribute", "attr", "&^", &attrname, -1 },
457                 { "attribute", "attr", "<",  NULL,      -1 },
458                 { "attribute", "attr", "<",  &attrname, MATCHER_LT },
459                 { "attribute", "attr", "<=", &attrname, MATCHER_LE },
460 /*              { "attribute", "attr", "=",  &attrname, MATCHER_EQ }, */
461                 { "attribute", "attr", ">=", &attrname, MATCHER_GE },
462                 { "attribute", "attr", ">",  &attrname, MATCHER_GT },
463                 { "attribute", "attr", "IS", NULL,      MATCHER_ISNULL },
464                 { "attribute", "attr", "IS", &attrname, -1 },
465                 { "foo",       NULL,   "=",  &attrname, -1 },
466                 { "foo",       "attr", "=",  &attrname, -1 },
467         };
469         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
470                 sdb_store_expr_t *expr;
471                 char buf[1024];
473                 if (sdb_data_format(golden_data[i].value,
474                                         buf, sizeof(buf), SDB_UNQUOTED) < 0)
475                         snprintf(buf, sizeof(buf), "ERR");
477                 expr = sdb_store_expr_constvalue(golden_data[i].value);
478                 fail_unless(expr != NULL || golden_data[i].value == NULL,
479                                 "sdb_store_expr_constvalue(%s) = NULL; expected: <expr>",
480                                 buf);
482                 check = sdb_store_matcher_parse_cmp(golden_data[i].obj_type,
483                                 golden_data[i].attr, golden_data[i].op, expr);
484                 sdb_object_deref(SDB_OBJ(expr));
486                 if (golden_data[i].expected == -1) {
487                         fail_unless(check == NULL,
488                                         "sdb_store_matcher_parse_cmp(%s, %s, %s, expr{%s}) = %p; "
489                                         "expected: NULL", golden_data[i].obj_type,
490                                         golden_data[i].attr, golden_data[i].op, buf, check);
491                         continue;
492                 }
494                 fail_unless(check != NULL,
495                                 "sdb_store_matcher_parse_cmp(%s, %s, %s, %s) = %p; "
496                                 "expected: NULL", golden_data[i].obj_type,
497                                 golden_data[i].attr, golden_data[i].op, buf, check);
498                 fail_unless(M(check)->type == golden_data[i].expected,
499                                 "sdb_store_matcher_parse_cmp(%s, %s, %s, %s) returned matcher "
500                                 "of type %d; expected: %d", golden_data[i].obj_type,
501                                 golden_data[i].attr, golden_data[i].op, buf,
502                                 M(check)->type, golden_data[i].expected);
504                 sdb_object_deref(SDB_OBJ(check));
505         }
507 END_TEST
509 static int
510 scan_cb(sdb_store_obj_t *obj, void *user_data)
512         int *i = user_data;
514         fail_unless(obj != NULL,
515                         "sdb_store_scan callback received NULL obj; expected: "
516                         "<store base obj>");
517         fail_unless(i != NULL,
518                         "sdb_store_scan callback received NULL user_data; "
519                         "expected: <pointer to data>");
521         ++(*i);
522         return 0;
523 } /* scan_cb */
525 START_TEST(test_scan)
527 #define PTR_RE "0x[0-9a-f]+"
528         struct {
529                 const char *query;
530                 int expected;
531                 const char *tostring_re;
532         } golden_data[] = {
533                 { "host = 'a'",               1,
534                         "OBJ\\[host\\]\\{ NAME\\{ 'a', \\(nil\\) \\} \\}" },
535                 { "host =~ 'a|b'",            2,
536                         "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
537                 { "host =~ 'host'",           0,
538                         "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
539                 { "host =~ '.'",              3,
540                         "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
541                 { "service = 's1'",           2,
542                         "OBJ\\[service\\]\\{ NAME\\{ 's1', \\(nil\\) } \\}" },
543                 { "service =~ 's'",           2,
544                         "OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}" },
545                 { "service !~ 's'",           1,
546                         "\\(NOT, OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}\\)" },
547                 { "attribute = 'k1'",         1,
548                         "OBJ\\[attribute\\]\\{ NAME\\{ 'k1', \\(nil\\) \\} " },
549                 { "attribute = 'x'",          0,
550                         "OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\}" },
551                 { "attribute.k1 = 'v1'",      1,
552                         "ATTR\\[k1\\]\\{ VALUE\\{ 'v1', \\(nil\\) \\} \\}" },
553                 { "attribute.k1 IS NULL",     2,
554                         "\\(IS NULL, ATTR\\[k1\\]\\)" },
555                 { "attribute.x1 IS NULL",     3,
556                         "\\(IS NULL, ATTR\\[x1\\]\\)" },
557                 { "attribute.k1 IS NOT NULL", 1,
558                         "\\(NOT, \\(IS NULL, ATTR\\[k1\\]\\)\\)" },
559                 { "attribute.x1 IS NOT NULL", 0,
560                         "\\(NOT, \\(IS NULL, ATTR\\[x1\\]\\)\\)" },
561                 { "attribute.k2 < 123",       0,
562                         "ATTR\\[k2\\]\\{ < 123 \\}" },
563                 { "attribute.k2 <= 123",      1,
564                         "ATTR\\[k2\\]\\{ <= 123 \\}" },
565                 { "attribute.k2 >= 123",      1,
566                         "ATTR\\[k2\\]\\{ >= 123 \\}" },
567                 { "attribute.k2 > 123",       0,
568                         "ATTR\\[k2\\]\\{ > 123 \\}" },
569                 { "attribute.k2 = 123",       1,
570                         "ATTR\\[k2\\]\\{ = 123 \\}" },
571                 { "attribute.k2 != 123",      2,
572                         "\\(NOT, ATTR\\[k2\\]\\{ = 123 \\}\\)" },
573                 { "attribute.k1 != 'v1'",     2,
574                         "\\(NOT, ATTR\\[k1\\]\\{ VALUE\\{ 'v1', \\(nil\\) \\} \\}\\)" },
575                 { "attribute.k1 != 'v2'",     3,
576                         "\\(NOT, ATTR\\[k1\\]\\{ VALUE\\{ 'v2', \\(nil\\) \\} \\}\\)" },
577                 { "attribute != 'x' "
578                   "AND attribute.y !~ 'x'",   3,
579                         "\\(AND, "
580                                 "\\(NOT, OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\} \\}\\), "
581                                 "\\(NOT, ATTR\\[y\\]\\{ VALUE\\{ NULL, "PTR_RE" \\} \\}\\)\\)" },
582         };
584         int check, n;
585         size_t i;
587         n = 0;
588         check = sdb_store_scan(NULL, scan_cb, &n);
589         fail_unless(check == 0,
590                         "sdb_store_scan() = %d; expected: 0", check);
591         fail_unless(n == 3,
592                         "sdb_store_scan called callback %d times; expected: 3", (int)n);
594         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
595                 sdb_store_matcher_t *m;
596                 char buf[4096];
598                 m = sdb_fe_parse_matcher(golden_data[i].query, -1);
599                 fail_unless(m != NULL,
600                                 "sdb_fe_parse_matcher(%s, -1) = NULL; expected: <matcher>",
601                                 golden_data[i].query);
602                 fail_unless(sdb_regmatches(golden_data[i].tostring_re,
603                                         sdb_store_matcher_tostring(m, buf, sizeof(buf))) == 0,
604                                 "sdb_fe_parse_matcher(%s, -1) = %s; expected: %s",
605                                 golden_data[i].query,
606                                 sdb_store_matcher_tostring(m, buf, sizeof(buf)),
607                                 golden_data[i].tostring_re);
609                 n = 0;
610                 sdb_store_scan(m, scan_cb, &n);
611                 fail_unless(n == golden_data[i].expected,
612                                 "sdb_store_scan(matcher{%s}) found %d hosts; expected: %d",
613                                 golden_data[i].query, n, golden_data[i].expected);
614                 sdb_object_deref(SDB_OBJ(m));
615         }
617 END_TEST
619 Suite *
620 core_store_lookup_suite(void)
622         Suite *s = suite_create("core::store_lookup");
623         TCase *tc;
625         tc = tcase_create("core");
626         tcase_add_checked_fixture(tc, populate, sdb_store_clear);
627         tcase_add_test(tc, test_store_match_name);
628         tcase_add_test(tc, test_store_match_attr);
629         tcase_add_test(tc, test_store_cond);
630         tcase_add_test(tc, test_store_match_op);
631         tcase_add_test(tc, test_parse_cmp);
632         tcase_add_test(tc, test_scan);
633         suite_add_tcase(s, tc);
635         return s;
636 } /* core_store_lookup_suite */
638 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */