Code

store: Removed unused old sdb_store_attr_matcher().
[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 <assert.h>
35 #include <check.h>
36 #include <string.h>
38 static void
39 populate(void)
40 {
41         const char *hosts[] = { "a", "b", "c" };
43         struct {
44                 const char *host;
45                 const char *metric;
46         } metrics[] = {
47                 { "a", "m1" },
48                 { "b", "m1" },
49                 { "b", "m2" },
50         };
52         struct {
53                 const char *host;
54                 const char *service;
55         } services[] = {
56                 { "a", "s1" },
57                 { "a", "s2" },
58                 { "b", "s1" },
59                 { "b", "s3" },
60         };
62         struct {
63                 const char *host;
64                 const char *name;
65                 sdb_data_t  value;
66         } attrs[] = {
67                 { "a", "k1", { SDB_TYPE_STRING, { .string = "v1" } } },
68                 { "a", "k2", { SDB_TYPE_INTEGER, { .integer = 123 } } },
69                 { "b", "k1", { SDB_TYPE_STRING, { .string = "v2" } } },
70         };
72         size_t i;
74         for (i = 0; i < SDB_STATIC_ARRAY_LEN(hosts); ++i) {
75                 int status = sdb_store_host(hosts[i], 1);
76                 fail_unless(status == 0,
77                                 "sdb_store_host(%s, 1) = %d; expected: 0",
78                                 hosts[i], status);
79         }
81         for (i = 0; i < SDB_STATIC_ARRAY_LEN(metrics); ++i) {
82                 int status = sdb_store_metric(metrics[i].host,
83                                 metrics[i].metric, /* store */ NULL, 1);
84                 fail_unless(status == 0,
85                                 "sdb_store_metric(%s, %s, NULL, 1) = %d; expected: 0",
86                                 metrics[i].host, metrics[i].metric, status);
87         }
89         for (i = 0; i < SDB_STATIC_ARRAY_LEN(services); ++i) {
90                 int status = sdb_store_service(services[i].host,
91                                 services[i].service, 1);
92                 fail_unless(status == 0,
93                                 "sdb_store_service(%s, %s, 1) = %d; expected: 0",
94                                 services[i].host, services[i].service, status);
95         }
97         for (i = 0; i < SDB_STATIC_ARRAY_LEN(attrs); ++i) {
98                 int status = sdb_store_attribute(attrs[i].host,
99                                 attrs[i].name, &attrs[i].value, 1);
100                 fail_unless(status == 0,
101                                 "sdb_store_attribute(%s, %s, <val>, 1) = %d; expected: 0",
102                                 attrs[i].host, attrs[i].name, status);
103         }
104 } /* populate */
106 START_TEST(test_store_match_name)
108         sdb_store_obj_t *obj;
110         struct {
111                 int type;
112                 const char *name;
113                 _Bool re;
115                 int expected;
116         } golden_data[] = {
117                 { SDB_HOST,      NULL,   0, 1 },
118                 { SDB_HOST,      NULL,   1, 1 },
119                 { SDB_HOST,      "a",    0, 1 },
120                 { SDB_HOST,      "a",    1, 1 },
121                 { SDB_HOST,      "b",    0, 0 },
122                 { SDB_HOST,      "b",    1, 0 },
123                 { SDB_HOST,      "^a$",  1, 1 },
124                 { SDB_HOST,      "^b$",  1, 0 },
125                 { SDB_HOST,      "^a$",  0, 0 },
126                 { SDB_HOST,      "^b$",  0, 0 },
127                 { SDB_METRIC,    NULL,   0, 1 },
128                 { SDB_METRIC,    NULL,   1, 1 },
129                 { SDB_METRIC,    "m1",   0, 1 },
130                 { SDB_METRIC,    "m1",   1, 1 },
131                 { SDB_METRIC,    "^m1$", 1, 1 },
132                 { SDB_METRIC,    "m",    1, 1 },
133                 { SDB_METRIC,    "s",    1, 0 },
134                 { SDB_METRIC,    "m2",   0, 0 },
135                 { SDB_METRIC,    "x1",   0, 0 },
136                 { SDB_METRIC,    "x1",   1, 0 },
137                 { SDB_SERVICE,   NULL,   0, 1 },
138                 { SDB_SERVICE,   NULL,   1, 1 },
139                 { SDB_SERVICE,   "s1",   0, 1 },
140                 { SDB_SERVICE,   "s2",   0, 1 },
141                 { SDB_SERVICE,   "s3",   0, 0 },
142                 { SDB_SERVICE,   "s4",   0, 0 },
143                 { SDB_SERVICE,   "^s1$", 1, 1 },
144                 { SDB_SERVICE,   "^s1$", 0, 0 },
145                 { SDB_SERVICE,   "x1",   0, 0 },
146                 { SDB_SERVICE,   "x1",   1, 0 },
147                 { SDB_SERVICE,   "x",    1, 0 },
148                 { SDB_ATTRIBUTE, NULL,   0, 1 },
149                 { SDB_ATTRIBUTE, NULL,   1, 1 },
150                 { SDB_ATTRIBUTE, "k1",   0, 1 },
151                 { SDB_ATTRIBUTE, "k2",   0, 1 },
152                 { SDB_ATTRIBUTE, "k3",   0, 0 },
153                 { SDB_ATTRIBUTE, "k4",   0, 0 },
154                 { SDB_ATTRIBUTE, "k",    1, 1 },
155                 { SDB_ATTRIBUTE, "1",    1, 1 },
156                 { SDB_ATTRIBUTE, "3",    1, 0 },
157         };
159         size_t i;
161         obj = sdb_store_get_host("a");
162         fail_unless(obj != NULL,
163                         "sdb_store_get_host(a) = NULL; expected: <host>");
165         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
166                 sdb_store_matcher_t *m, *n;
167                 int status;
169                 m = sdb_store_name_matcher(golden_data[i].type,
170                                 golden_data[i].name, golden_data[i].re);
171                 fail_unless(m != NULL,
172                                 "sdb_store_service_matcher(%d, %s, %d) = NULL; "
173                                 "expected: <matcher>", golden_data[i].type,
174                                 golden_data[i].name, golden_data[i].re);
176                 status = sdb_store_matcher_matches(m, obj, /* filter */ NULL);
177                 fail_unless(status == golden_data[i].expected,
178                                 "sdb_store_matcher_matches(%s->%s, <host a>, NULL) = %d; "
179                                 "expected: %d", SDB_STORE_TYPE_TO_NAME(golden_data[i].type),
180                                 golden_data[i].name, status, golden_data[i].expected);
182                 n = sdb_store_inv_matcher(m);
183                 fail_unless(n != NULL,
184                                 "sdb_store_inv_matcher() = NULL; expected: <matcher>");
185                 sdb_object_deref(SDB_OBJ(m));
187                 /* now match the inverted set of objects */
188                 status = sdb_store_matcher_matches(n, obj, /* filter */ NULL);
189                 fail_unless(status == !golden_data[i].expected,
190                                 "sdb_store_matcher_matches(%s->%s, <host a>, NULL) = %d; "
191                                 "expected: %d", SDB_STORE_TYPE_TO_NAME(golden_data[i].type),
192                                 golden_data[i].name, status, !golden_data[i].expected);
194                 sdb_object_deref(SDB_OBJ(n));
195         }
197         sdb_object_deref(SDB_OBJ(obj));
199 END_TEST
201 START_TEST(test_cmp_attr)
203         sdb_store_obj_t *host;
205         struct {
206                 const char *attr;
207                 const sdb_data_t value;
208                 int expected_lt, expected_le, expected_eq, expected_ge, expected_gt;
209         } golden_data[] = {
210                 { "k1", { SDB_TYPE_STRING,  { .string  = "v1" } },  0, 1, 1, 1, 0 },
211                 { "k1", { SDB_TYPE_STRING,  { .string  = "v2" } },  1, 1, 0, 0, 0 },
212                 { "k1", { SDB_TYPE_STRING,  { .string  = "v0" } },  0, 0, 0, 1, 1 },
213                 { "k1", { SDB_TYPE_STRING,  { .string  = "0" } },   0, 0, 0, 1, 1 },
214                 { "k2", { SDB_TYPE_INTEGER, { .integer = 123 } },   0, 1, 1, 1, 0 },
215                 { "k2", { SDB_TYPE_INTEGER, { .integer = 124 } },   1, 1, 0, 0, 0 },
216                 { "k2", { SDB_TYPE_INTEGER, { .integer = 122 } },   0, 0, 0, 1, 1 },
217                 /* key does not exist */
218                 { "k3", { SDB_TYPE_STRING,  { .string  = "v1" } },  0, 0, 0, 0, 0 },
219                 { "k3", { SDB_TYPE_STRING,  { .string  = "123" } }, 0, 0, 0, 0, 0 },
220                 { "k3", { SDB_TYPE_INTEGER, { .integer = 123 } },   0, 0, 0, 0, 0 },
221                 /* type mismatch */
222                 { "k1", { SDB_TYPE_INTEGER, { .integer = 0 } },     0, 0, 0, 1, 1 },
223                 { "k2", { SDB_TYPE_STRING,  { .string  = "123" } }, 0, 1, 1, 1, 0 },
224         };
226         int status;
227         size_t i;
229         host = sdb_store_get_host("a");
230         fail_unless(host != NULL,
231                         "sdb_store_get_host(a) = NULL; expected: <host>");
233         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
234                 sdb_store_expr_t *attr;
235                 sdb_store_expr_t *value;
236                 char value_str[1024];
237                 size_t j;
239                 struct {
240                         sdb_store_matcher_t *(*matcher)(sdb_store_expr_t *,
241                                         sdb_store_expr_t *);
242                         int expected;
243                 } tests[] = {
244                         { sdb_store_lt_matcher, golden_data[i].expected_lt },
245                         { sdb_store_le_matcher, golden_data[i].expected_le },
246                         { sdb_store_eq_matcher, golden_data[i].expected_eq },
247                         { sdb_store_ge_matcher, golden_data[i].expected_ge },
248                         { sdb_store_gt_matcher, golden_data[i].expected_gt },
249                 };
251                 sdb_data_format(&golden_data[i].value,
252                                 value_str, sizeof(value_str), SDB_UNQUOTED);
254                 attr = sdb_store_expr_attrvalue(golden_data[i].attr);
255                 fail_unless(attr != NULL,
256                                 "sdb_store_expr_attrvalue(%s) = NULL; expected: <expr>",
257                                 golden_data[i].attr);
259                 value = sdb_store_expr_constvalue(&golden_data[i].value);
260                 fail_unless(value != NULL,
261                                 "sdb_store_expr_constvalue(%s) = NULL; expected: <expr>",
262                                 value_str);
264                 for (j = 0; j < SDB_STATIC_ARRAY_LEN(tests); ++j) {
265                         sdb_store_matcher_t *m;
267                         m = tests[j].matcher(attr, value);
268                         fail_unless(m != NULL,
269                                         "sdb_store_<cond>_matcher() = NULL; expected: <matcher>");
271                         status = sdb_store_matcher_matches(m, host, /* filter */ NULL);
272                         fail_unless(status == tests[j].expected,
273                                         "sdb_store_matcher_matches(<m>, <host>, NULL) = %d; "
274                                         "expected: %d", status, tests[j].expected);
276                         sdb_object_deref(SDB_OBJ(m));
277                 }
279                 sdb_object_deref(SDB_OBJ(attr));
280                 sdb_object_deref(SDB_OBJ(value));
281         }
283         sdb_object_deref(SDB_OBJ(host));
285 END_TEST
287 START_TEST(test_cmp_obj)
289         struct {
290                 const char *host;
291                 int field;
292                 const sdb_data_t value;
293                 int expected_lt, expected_le, expected_eq, expected_ge, expected_gt;
294         } golden_data[] = {
295                 { "b", SDB_FIELD_NAME,
296                         { SDB_TYPE_STRING, { .string = "a" } },   0, 0, 0, 1, 1 },
297                 { "b", SDB_FIELD_NAME,
298                         { SDB_TYPE_STRING, { .string = "b" } },   0, 1, 1, 1, 0 },
299                 { "b", SDB_FIELD_NAME,
300                         { SDB_TYPE_STRING, { .string = "c" } },   1, 1, 0, 0, 0 },
301                 /* last-update = 1 for all objects */
302                 { "a", SDB_FIELD_LAST_UPDATE,
303                         { SDB_TYPE_DATETIME, { .datetime = 1 } }, 0, 1, 1, 1, 0 },
304                 { "a", SDB_FIELD_LAST_UPDATE,
305                         { SDB_TYPE_DATETIME, { .datetime = 2 } }, 1, 1, 0, 0, 0 },
306                 { "a", SDB_FIELD_LAST_UPDATE,
307                         { SDB_TYPE_DATETIME, { .datetime = 0 } }, 0, 0, 0, 1, 1 },
308                 /* age > 0 for all objects */
309                 { "a", SDB_FIELD_AGE,
310                         { SDB_TYPE_DATETIME, { .datetime = 0 } }, 0, 0, 0, 1, 1 },
311                 /* interval = 0 for all objects */
312                 { "a", SDB_FIELD_INTERVAL,
313                         { SDB_TYPE_DATETIME, { .datetime = 0 } }, 0, 1, 1, 1, 0 },
314                 { "a", SDB_FIELD_INTERVAL,
315                         { SDB_TYPE_DATETIME, { .datetime = 1 } }, 1, 1, 0, 0, 0 },
316                 /* type mismatch */
317                 /* TODO: let matchers only use data_strcmp for attributes
318                  *       everything else has a well-known type
319                 { "a", SDB_FIELD_LAST_UPDATE,
320                         { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
321                 { "a", SDB_FIELD_AGE,
322                         { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
323                 { "a", SDB_FIELD_INTERVAL,
324                         { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
325                 { "a", SDB_FIELD_BACKEND,
326                         { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
327                 { "a", SDB_FIELD_BACKEND,
328                         { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 },
329                 */
330                 /* (64bit) integer value without zero-bytes */
331                 /*
332                 { "a", SDB_FIELD_BACKEND,
333                         { SDB_TYPE_INTEGER, { .integer = 0xffffffffffffffffL } },
334                         0, 0, 0, 0, 0 },
335                 */
336         };
338         int status;
339         size_t i;
341         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
342                 sdb_store_obj_t *host;
343                 sdb_store_expr_t *field;
344                 sdb_store_expr_t *value;
345                 char value_str[1024];
346                 size_t j;
348                 struct {
349                         sdb_store_matcher_t *(*matcher)(sdb_store_expr_t *,
350                                         sdb_store_expr_t *);
351                         int expected;
352                 } tests[] = {
353                         { sdb_store_lt_matcher, golden_data[i].expected_lt },
354                         { sdb_store_le_matcher, golden_data[i].expected_le },
355                         { sdb_store_eq_matcher, golden_data[i].expected_eq },
356                         { sdb_store_ge_matcher, golden_data[i].expected_ge },
357                         { sdb_store_gt_matcher, golden_data[i].expected_gt },
358                 };
359                 char *op_str[] = { "<", "<=", "=", ">=", ">" };
361                 assert(SDB_STATIC_ARRAY_LEN(tests) == SDB_STATIC_ARRAY_LEN(op_str));
363                 host = sdb_store_get_host(golden_data[i].host);
364                 fail_unless(host != NULL,
365                                 "sdb_store_get_host(%s) = NULL; expected: <host>",
366                                 golden_data[i].host);
368                 sdb_data_format(&golden_data[i].value,
369                                 value_str, sizeof(value_str), SDB_UNQUOTED);
371                 field = sdb_store_expr_fieldvalue(golden_data[i].field);
372                 fail_unless(field != NULL,
373                                 "sdb_store_expr_fieldvalue(%d) = NULL; "
374                                 "expected: <expr>", golden_data[i].field);
376                 value = sdb_store_expr_constvalue(&golden_data[i].value);
377                 fail_unless(value != NULL,
378                                 "sdb_store_expr_constvalue(%s) = NULL; "
379                                 "expected: <expr>", value_str);
381                 for (j = 0; j < SDB_STATIC_ARRAY_LEN(tests); ++j) {
382                         char m_str[1024];
383                         sdb_store_matcher_t *m;
385                         snprintf(m_str, sizeof(m_str), "%s %s %s",
386                                         SDB_FIELD_TO_NAME(golden_data[i].field),
387                                         op_str[j], value_str);
389                         m = tests[j].matcher(field, value);
390                         fail_unless(m != NULL,
391                                         "sdb_store_<cond>_matcher() = NULL; expected: <matcher>");
393                         status = sdb_store_matcher_matches(m, host, /* filter */ NULL);
394                         fail_unless(status == tests[j].expected,
395                                         "sdb_store_matcher_matches(<%s>, <host '%s'>, NULL) = %d; "
396                                         "expected: %d", m_str, golden_data[i].host, status,
397                                         tests[j].expected);
399                         sdb_object_deref(SDB_OBJ(m));
400                 }
402                 sdb_object_deref(SDB_OBJ(field));
403                 sdb_object_deref(SDB_OBJ(value));
404                 sdb_object_deref(SDB_OBJ(host));
405         }
407 END_TEST
409 START_TEST(test_store_match_op)
411         sdb_store_obj_t *obj;
413         sdb_store_matcher_t *always = sdb_store_name_matcher(SDB_HOST, "a", 0);
414         sdb_store_matcher_t *never = sdb_store_name_matcher(SDB_HOST, "z", 0);
416         struct {
417                 const char *op;
418                 sdb_store_matcher_t *left;
419                 sdb_store_matcher_t *right;
420                 int expected;
421         } golden_data[] = {
422                 { "OR",  always, always, 1 },
423                 { "OR",  always, never,  1 },
424                 { "OR",  never,  always, 1 },
425                 { "OR",  never,  never,  0 },
426                 { "AND", always, always, 1 },
427                 { "AND", always, never,  0 },
428                 { "AND", never,  always, 0 },
429                 { "AND", never,  never,  0 },
430         };
432         int status;
433         size_t i;
435         obj = sdb_store_get_host("a");
437         status = sdb_store_matcher_matches(always, obj, /* filter */ NULL);
438         fail_unless(status == 1,
439                         "INTERNAL ERROR: 'always' did not match host");
440         status = sdb_store_matcher_matches(never, obj, /* filter */ NULL);
441         fail_unless(status == 0,
442                         "INTERNAL ERROR: 'never' matches host");
444         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
445                 sdb_store_matcher_t *m;
447                 if (! strcmp(golden_data[i].op, "OR"))
448                         m = sdb_store_dis_matcher(golden_data[i].left,
449                                         golden_data[i].right);
450                 else if (! strcmp(golden_data[i].op, "AND"))
451                         m = sdb_store_con_matcher(golden_data[i].left,
452                                         golden_data[i].right);
453                 else {
454                         fail("INTERNAL ERROR: unexpected operator %s", golden_data[i].op);
455                         continue;
456                 }
458 #define TO_NAME(v) (((v) == always) ? "always" \
459                 : ((v) == never) ? "never" : "<unknown>")
461                 status = sdb_store_matcher_matches(m, obj, /* filter */ NULL);
462                 fail_unless(status == golden_data[i].expected,
463                                 "%s(%s, %s, NULL) = %d; expected: %d", golden_data[i].op,
464                                 TO_NAME(golden_data[i].left), TO_NAME(golden_data[i].right),
465                                 status, golden_data[i].expected);
467 #undef TO_NAME
469                 sdb_object_deref(SDB_OBJ(m));
470         }
472         sdb_object_deref(SDB_OBJ(always));
473         sdb_object_deref(SDB_OBJ(never));
475         sdb_object_deref(SDB_OBJ(obj));
477 END_TEST
479 START_TEST(test_parse_cmp)
481         sdb_data_t hostname   = { SDB_TYPE_STRING,  { .string  = "hostname" } };
482         sdb_data_t metricname = { SDB_TYPE_STRING,  { .string  = "metricname" } };
483         sdb_data_t srvname    = { SDB_TYPE_STRING,  { .string  = "srvname" } };
484         sdb_data_t attrname   = { SDB_TYPE_STRING,  { .string  = "attrname" } };
486         sdb_store_matcher_t *check;
488         size_t i;
490         struct {
491                 const char *obj_type;
492                 const char *op;
493                 const sdb_data_t *value;
494                 int expected;
495         } golden_data[] = {
496                 { "host",      "=",  &hostname,   MATCHER_NAME },
497                 { "host",      "!=", &hostname,   MATCHER_NOT },
498                 { "host",      "=~", &hostname,   MATCHER_NAME },
499                 { "host",      "!~", &hostname,   MATCHER_NOT },
500                 { "host",      "&^", &hostname,   -1 },
501                 { "host",      "<",  &hostname,   -1 },
502                 { "host",      "<=", &hostname,   -1 },
503                 { "host",      ">=", &hostname,   -1 },
504                 { "host",      ">",  &hostname,   -1 },
505                 { "host",      "=",  NULL,        -1 },
506                 { "metric",    "=",  &metricname, MATCHER_NAME },
507                 { "metric",    "!=", &metricname, MATCHER_NOT },
508                 { "metric",    "=~", &metricname, MATCHER_NAME },
509                 { "metric",    "!~", &metricname, MATCHER_NOT },
510                 { "metric",    "&^", &metricname, -1 },
511                 { "metric",    "<",  &metricname, -1 },
512                 { "metric",    "<=", &metricname, -1 },
513                 { "metric",    ">=", &metricname, -1 },
514                 { "metric",    ">",  &metricname, -1 },
515                 { "metric",    "=",  NULL,        -1 },
516                 { "service",   "=",  &srvname,    MATCHER_NAME },
517                 { "service",   "!=", &srvname,    MATCHER_NOT },
518                 { "service",   "=~", &srvname,    MATCHER_NAME },
519                 { "service",   "!~", &srvname,    MATCHER_NOT },
520                 { "service",   "&^", &srvname,    -1 },
521                 { "service",   "<",  &srvname,    -1 },
522                 { "service",   "<=", &srvname,    -1 },
523                 { "service",   ">=", &srvname,    -1 },
524                 { "service",   ">",  &srvname,    -1 },
525                 { "service",   "=",  NULL,        -1 },
526                 { "attribute", "=",  &attrname,   MATCHER_NAME },
527                 { "attribute", "!=", &attrname,   MATCHER_NOT },
528                 { "attribute", "=~", &attrname,   MATCHER_NAME },
529                 { "attribute", "!~", &attrname,   MATCHER_NOT },
530                 { "attribute", "<",  &attrname,   -1 },
531                 { "attribute", "<=", &attrname,   -1 },
532                 { "attribute", ">=", &attrname,   -1 },
533                 { "attribute", ">",  &attrname,   -1 },
534                 { "attribute", "=",  NULL,        -1 },
535                 { "foo",       "=",  &attrname,   -1 },
536         };
538         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
539                 sdb_store_expr_t *expr;
540                 char buf[1024];
542                 if (sdb_data_format(golden_data[i].value,
543                                         buf, sizeof(buf), SDB_UNQUOTED) < 0)
544                         snprintf(buf, sizeof(buf), "ERR");
546                 expr = sdb_store_expr_constvalue(golden_data[i].value);
547                 fail_unless(expr != NULL || golden_data[i].value == NULL,
548                                 "sdb_store_expr_constvalue(%s) = NULL; expected: <expr>",
549                                 buf);
551                 check = sdb_store_matcher_parse_cmp(golden_data[i].obj_type,
552                                 golden_data[i].op, expr);
553                 sdb_object_deref(SDB_OBJ(expr));
555                 if (golden_data[i].expected == -1) {
556                         fail_unless(check == NULL,
557                                         "sdb_store_matcher_parse_cmp(%s, %s, expr{%s}) = %p; "
558                                         "expected: NULL", golden_data[i].obj_type,
559                                         golden_data[i].op, buf, check);
560                         continue;
561                 }
563                 fail_unless(check != NULL,
564                                 "sdb_store_matcher_parse_cmp(%s, %s, %s) = %p; "
565                                 "expected: <expr>", golden_data[i].obj_type,
566                                 golden_data[i].op, buf, check);
567                 fail_unless(M(check)->type == golden_data[i].expected,
568                                 "sdb_store_matcher_parse_cmp(%s, %s, %s) returned matcher "
569                                 "of type %d; expected: %d", golden_data[i].obj_type,
570                                 golden_data[i].op, buf, M(check)->type, golden_data[i].expected);
572                 sdb_object_deref(SDB_OBJ(check));
573         }
575 END_TEST
577 static int
578 scan_cb(sdb_store_obj_t *obj, void *user_data)
580         int *i = user_data;
582         fail_unless(obj != NULL,
583                         "sdb_store_scan callback received NULL obj; expected: "
584                         "<store base obj>");
585         fail_unless(i != NULL,
586                         "sdb_store_scan callback received NULL user_data; "
587                         "expected: <pointer to data>");
589         ++(*i);
590         return 0;
591 } /* scan_cb */
593 START_TEST(test_scan)
595         struct {
596                 const char *query;
597                 const char *filter;
598                 int expected;
599         } golden_data[] = {
600                 /* TODO: check the name of the expected hosts */
601                 { "host = 'a'", NULL,                  1 },
602                 { "host = 'a'", "host = 'x'",          0 }, /* filter never matches */
603                 { "host = 'a'",
604                         "NOT attribute['x'] = ''",         1 }, /* filter always matches */
605                 { "host =~ 'a|b'", NULL,               2 },
606                 { "host =~ 'host'", NULL,              0 },
607                 { "host =~ '.'", NULL,                 3 },
608                 { "metric = 'm1'", NULL,               2 },
609                 { "metric= 'm1'", "host = 'x'",        0 }, /* filter never matches */
610                 { "metric = 'm1'",
611                         "NOT attribute['x'] = ''",         2 }, /* filter always matches */
612                 { "metric =~ 'm'", NULL,               2 },
613                 { "metric !~ 'm'", NULL,               1 },
614                 { "metric =~ 'x'", NULL,               0 },
615                 { "service = 's1'", NULL,              2 },
616                 { "service = 's1'", "host = 'x'",      0 }, /* filter never matches */
617                 { "service = 's1'",
618                         "NOT attribute['x'] = ''",         2 }, /* filter always matches */
619                 { "service =~ 's'", NULL,              2 },
620                 { "service !~ 's'", NULL,              1 },
621                 { "attribute = 'k1'", NULL,            2 },
622                 { "attribute = 'k1'", "host = 'x'",    0 }, /* filter never matches */
623                 { "attribute = 'k1'",
624                         "NOT attribute['x'] = ''",         2 }, /* filter always matches */
625                 { "attribute =~ 'k'", NULL,            2 },
626                 { "attribute =~ '1'", NULL,            2 },
627                 { "attribute =~ '2'", NULL,            1 },
628                 { "attribute = 'x'", NULL,             0 },
629                 { "attribute =~ 'x'", NULL,            0 },
630                 { "attribute['k1'] = 'v1'", NULL,      1 },
631                 { "attribute['k1'] =~ 'v1'", NULL,     1 },
632                 { "attribute['k1'] =~ '^v1$'", NULL,   1 },
633                 { "attribute['k1'] =~ 'v'", NULL,      2 },
634                 { "attribute['k1'] =~ '1'", NULL,      1 },
635                 { "attribute['k1'] !~ 'v'", NULL,      1 },
636                 { "attribute['k1'] = 'v2'", NULL,      1 },
637                 { "attribute['k1'] =~ 'v2'", NULL,     1 },
638                 { "attribute['x1'] =~ 'v'", NULL,      0 },
639                 { "attribute['x1'] =~ 'NULL'", NULL,   0 },
640                 { "attribute['x1'] !~ 'v'", NULL,      3 },
641                 { "attribute['k1'] IS NULL", NULL,     1 },
642                 { "attribute['x1'] IS NULL", NULL,     3 },
643                 { "attribute['k1'] IS NOT NULL", NULL, 2 },
644                 { "attribute['x1'] IS NOT NULL", NULL, 0 },
645                 { "attribute['k2'] < 123", NULL,       0 },
646                 { "attribute['k2'] <= 123", NULL,      1 },
647                 { "attribute['k2'] >= 123", NULL,      1 },
648                 { "attribute['k2'] > 123", NULL,       0 },
649                 { "attribute['k2'] = 123", NULL,       1 },
650                 { "attribute['k2'] != 123", NULL,      0 },
651                 { "attribute['k1'] != 'v1'", NULL,     1 },
652                 { "attribute['k1'] != 'v2'", NULL,     1 },
653                 { "attribute != 'x' "
654                   "AND attribute['y'] !~ 'x'", NULL,   3 },
655         };
657         int check, n;
658         size_t i;
660         n = 0;
661         check = sdb_store_scan(/* matcher */ NULL, /* filter */ NULL,
662                         scan_cb, &n);
663         fail_unless(check == 0,
664                         "sdb_store_scan() = %d; expected: 0", check);
665         fail_unless(n == 3,
666                         "sdb_store_scan called callback %d times; expected: 3", (int)n);
668         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
669                 sdb_store_matcher_t *m, *filter = NULL;
671                 m = sdb_fe_parse_matcher(golden_data[i].query, -1);
672                 fail_unless(m != NULL,
673                                 "sdb_fe_parse_matcher(%s, -1) = NULL; expected: <matcher>",
674                                 golden_data[i].query);
676                 if (golden_data[i].filter) {
677                         filter = sdb_fe_parse_matcher(golden_data[i].filter, -1);
678                         fail_unless(filter != NULL,
679                                         "sdb_fe_parse_matcher(%s, -1) = NULL; "
680                                         "expected: <matcher>", golden_data[i].filter);
681                 }
683                 n = 0;
684                 sdb_store_scan(m, filter, scan_cb, &n);
685                 fail_unless(n == golden_data[i].expected,
686                                 "sdb_store_scan(matcher{%s}, filter{%s}) found %d hosts; "
687                                 "expected: %d", golden_data[i].query, golden_data[i].filter,
688                                 n, golden_data[i].expected);
689                 sdb_object_deref(SDB_OBJ(filter));
690                 sdb_object_deref(SDB_OBJ(m));
691         }
693 END_TEST
695 Suite *
696 core_store_lookup_suite(void)
698         Suite *s = suite_create("core::store_lookup");
699         TCase *tc;
701         tc = tcase_create("core");
702         tcase_add_checked_fixture(tc, populate, sdb_store_clear);
703         tcase_add_test(tc, test_store_match_name);
704         tcase_add_test(tc, test_cmp_attr);
705         tcase_add_test(tc, test_cmp_obj);
706         tcase_add_test(tc, test_store_match_op);
707         tcase_add_test(tc, test_parse_cmp);
708         tcase_add_test(tc, test_scan);
709         suite_add_tcase(s, tc);
711         return s;
712 } /* core_store_lookup_suite */
714 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */