Code

store_lookup: Made the attribute matcher a "standalone" matcher.
[sysdb.git] / t / unit / core / store_lookup_test.c
index c143a8aacc2dca51d291cb5485e34cb26454fc56..4bbfcc23864aa17e3a85a3d4e0563a7da4928eb3 100644 (file)
@@ -90,120 +90,59 @@ START_TEST(test_store_match)
                const char *hostname;
                const char *hostname_re;
 
-               const char *service_name;
-               const char *service_name_re;
-
                const char *attr_name;
-               const char *attr_name_re;
                const char *attr_value;
-               const char *attr_value_re;
+               _Bool re;
 
                int expected;
        } golden_data[] = {
                {
                        /* host */ NULL, NULL,
-                       /* svc  */ NULL, NULL,
-                       /* attr */ NULL, NULL, NULL, NULL, 1
+                       /* attr */ NULL, NULL, 0, 1
+               },
+               {
+                       /* host */ NULL, NULL,
+                       /* attr */ NULL, NULL, 1, 1
                },
                {
                        /* host */ "a", NULL,
-                       /* svc  */ NULL, NULL,
-                       /* attr */ NULL, NULL, NULL, NULL, 1
+                       /* attr */ NULL, NULL, 0, 1
                },
                {
                        /* host */ "b", NULL,
-                       /* svc  */ NULL, NULL,
-                       /* attr */ NULL, NULL, NULL, NULL, 0
+                       /* attr */ NULL, NULL, 0, 0
                },
                {
                        /* host */ NULL, "^a$",
-                       /* svc  */ NULL, NULL,
-                       /* attr */ NULL, NULL, NULL, NULL, 1
+                       /* attr */ NULL, NULL, 0, 1
                },
                {
                        /* host */ NULL, "^b$",
-                       /* svc  */ NULL, NULL,
-                       /* attr */ NULL, NULL, NULL, NULL, 0
+                       /* attr */ NULL, NULL, 0, 0
                },
                {
                        /* host */ "a", "^a$",
-                       /* svc  */ NULL, NULL,
-                       /* attr */ NULL, NULL, NULL, NULL, 1
+                       /* attr */ NULL, NULL, 0, 1
                },
                {
                        /* host */ "a", "^b$",
-                       /* svc  */ NULL, NULL,
-                       /* attr */ NULL, NULL, NULL, NULL, 0
+                       /* attr */ NULL, NULL, 0, 0
                },
                {
                        /* host */ "b", "^a$",
-                       /* svc  */ NULL, NULL,
-                       /* attr */ NULL, NULL, NULL, NULL, 0
-               },
-               {
-                       /* host */ "a", "^a$",
-                       /* svc  */ "s1", NULL,
-                       /* attr */ NULL, NULL, NULL, NULL, 1
-               },
-               {
-                       /* host */ "a", "^a$",
-                       /* svc  */ NULL, "^s1$",
-                       /* attr */ NULL, NULL, NULL, NULL, 1
-               },
-               {
-                       /* host */ "a", "^a$",
-                       /* svc  */ "s1", "^s1$",
-                       /* attr */ NULL, NULL, NULL, NULL, 1
-               },
-               {
-                       /* host */ "a", "^a$",
-                       /* svc  */ "x1", NULL,
-                       /* attr */ NULL, NULL, NULL, NULL, 0
-               },
-               {
-                       /* host */ "a", "^a$",
-                       /* svc  */ NULL, "x",
-                       /* attr */ NULL, NULL, NULL, NULL, 0
+                       /* attr */ NULL, NULL, 0, 0
                },
                {
                        /* host */ "a", "^a$",
-                       /* svc  */ "x1", "x",
-                       /* attr */ NULL, NULL, NULL, NULL, 0
+                       /* attr */ "k1", NULL, 0, 1
                },
                {
                        /* host */ "a", "^a$",
-                       /* svc  */ "s1", "x",
-                       /* attr */ NULL, NULL, NULL, NULL, 0
+                       /* attr */ NULL, "v1", 0, 1
                },
                {
                        /* host */ "a", "^a$",
-                       /* svc  */ "x1", "s",
-                       /* attr */ NULL, NULL, NULL, NULL, 0
-               },
-               {
-                       /* host */ "a", "^a$",
-                       /* svc  */ "s1", "^s1$",
-                       /* attr */ "k1", NULL, NULL, NULL, 1
-               },
-               {
-                       /* host */ "a", "^a$",
-                       /* svc  */ "s1", "^s1$",
-                       /* attr */ NULL, "^k", NULL, NULL, 1
-               },
-               {
-                       /* host */ "a", "^a$",
-                       /* svc  */ "s1", "^s1$",
-                       /* attr */ NULL, NULL, "v1", NULL, 1
-               },
-               {
-                       /* host */ "a", "^a$",
-                       /* svc  */ "s1", "^s1$",
-                       /* attr */ NULL, NULL, NULL, "^v1$", 1
-               },
-               {
-                       /* host */ "a", "^a$",
-                       /* svc  */ "s1", "^s1$",
-                       /* attr */ "k1", "1", "v1", "1", 1
+                       /* attr */ NULL, "^v1$", 1, 1
                },
        };
 
@@ -214,36 +153,26 @@ START_TEST(test_store_match)
                        "sdb_store_get_host(a) = NULL; expected: <host>");
 
        for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
-               sdb_store_matcher_t *h, *s, *a, *n;
+               sdb_store_matcher_t *h, *a, *n;
+               char buf[1024];
                int status;
 
-               s = sdb_store_service_matcher(golden_data[i].service_name,
-                               golden_data[i].service_name_re, NULL);
-               fail_unless(s != NULL,
-                               "sdb_store_service_matcher() = NULL; expected: <matcher>");
-
                a = sdb_store_attr_matcher(golden_data[i].attr_name,
-                               golden_data[i].attr_name_re, golden_data[i].attr_value,
-                               golden_data[i].attr_value_re);
+                               golden_data[i].attr_value, golden_data[i].re);
                fail_unless(a != NULL,
                                "sdb_store_attr_matcher() = NULL; expected: <matcher>");
 
                h = sdb_store_host_matcher(golden_data[i].hostname,
-                               golden_data[i].hostname_re, s, a);
+                               golden_data[i].hostname_re, a);
                fail_unless(h != NULL,
                                "sdb_store_host_matcher() = NULL: expected: <matcher>");
                /* pass ownership to the host matcher */
-               sdb_object_deref(SDB_OBJ(s));
                sdb_object_deref(SDB_OBJ(a));
 
                status = sdb_store_matcher_matches(h, obj);
                fail_unless(status == golden_data[i].expected,
-                               "sdb_store_matcher_matches({{%s, %s},{%s, %s},"
-                               "{%s, %s, %s, %s}}, <host a>) = %d; expected: %d",
-                               golden_data[i].hostname, golden_data[i].hostname_re,
-                               golden_data[i].service_name, golden_data[i].service_name_re,
-                               golden_data[i].attr_name, golden_data[i].attr_name_re,
-                               golden_data[i].attr_value, golden_data[i].attr_value_re,
+                               "sdb_store_matcher_matches({%s, <host a>) = %d; expected: %d",
+                               sdb_store_matcher_tostring(h, buf, sizeof(buf)),
                                status, golden_data[i].expected);
 
                n = sdb_store_inv_matcher(h);
@@ -254,12 +183,8 @@ START_TEST(test_store_match)
                /* now match the inverted set of objects */
                status = sdb_store_matcher_matches(n, obj);
                fail_unless(status == !golden_data[i].expected,
-                               "sdb_store_matcher_matches(NOT{{%s, %s},{%s, %s},"
-                               "{%s, %s, %s, %s}}, <host a>) = %d; expected: %d",
-                               golden_data[i].hostname, golden_data[i].hostname_re,
-                               golden_data[i].service_name, golden_data[i].service_name_re,
-                               golden_data[i].attr_name, golden_data[i].attr_name_re,
-                               golden_data[i].attr_value, golden_data[i].attr_value_re,
+                               "sdb_store_matcher_matches({%s, <host a>) = %d; expected: %d",
+                               sdb_store_matcher_tostring(n, buf, sizeof(buf)),
                                status, !golden_data[i].expected);
 
                sdb_object_deref(SDB_OBJ(n));
@@ -344,7 +269,7 @@ START_TEST(test_store_match_name)
                fail_unless(status == !golden_data[i].expected,
                                "sdb_store_matcher_matches(%s, <host a>) = %d; expected: %d",
                                sdb_store_matcher_tostring(n, buf, sizeof(buf)),
-                               status, golden_data[i].expected);
+                               status, !golden_data[i].expected);
 
                sdb_object_deref(SDB_OBJ(n));
        }
@@ -357,8 +282,8 @@ START_TEST(test_store_match_op)
 {
        sdb_store_base_t *obj;
 
-       sdb_store_matcher_t *always = sdb_store_host_matcher(NULL, NULL, NULL, NULL);
-       sdb_store_matcher_t *never = sdb_store_host_matcher("a", "b", NULL, NULL);
+       sdb_store_matcher_t *always = sdb_store_host_matcher(NULL, NULL, NULL);
+       sdb_store_matcher_t *never = sdb_store_host_matcher("a", "b", NULL);
 
        struct {
                const char *op;
@@ -436,27 +361,27 @@ START_TEST(test_parse_cmp)
                const char *value;
                int expected;
        } golden_data[] = {
-               { "host",      "name", "=",  "hostname", MATCHER_HOST },
+               { "host",      "name", "=",  "hostname", MATCHER_NAME },
                { "host",      "name", "!=", "hostname", MATCHER_NOT },
-               { "host",      "name", "=~", "hostname", MATCHER_HOST },
+               { "host",      "name", "=~", "hostname", MATCHER_NAME },
                { "host",      "name", "!~", "hostname", MATCHER_NOT },
                { "host",      "attr", "=",  "hostname", -1 },
                { "host",      "attr", "!=", "hostname", -1 },
                { "host",      "name", "&^", "hostname", -1 },
-               { "service",   "name", "=",  "srvname",  MATCHER_HOST },
+               { "service",   "name", "=",  "srvname",  MATCHER_NAME },
                { "service",   "name", "!=", "srvname",  MATCHER_NOT },
-               { "service",   "name", "=~", "srvname",  MATCHER_HOST },
+               { "service",   "name", "=~", "srvname",  MATCHER_NAME },
                { "service",   "name", "!~", "srvname",  MATCHER_NOT },
                { "service",   "attr", "=",  "srvname",  -1 },
                { "service",   "attr", "!=", "srvname",  -1 },
                { "service",   "name", "&^", "srvname",  -1 },
-               { "attribute", "name", "=",  "attrname", MATCHER_HOST },
+               { "attribute", "name", "=",  "attrname", MATCHER_NAME },
                { "attribute", "name", "!=", "attrname", MATCHER_NOT },
-               { "attribute", "name", "=~", "attrname", MATCHER_HOST },
+               { "attribute", "name", "=~", "attrname", MATCHER_NAME },
                { "attribute", "name", "!~", "attrname", MATCHER_NOT },
-               { "attribute", "attr", "=",  "attrname", MATCHER_HOST },
+               { "attribute", "attr", "=",  "attrname", MATCHER_ATTR },
                { "attribute", "attr", "!=", "attrname", MATCHER_NOT },
-               { "attribute", "attr", "=~", "attrname", MATCHER_HOST },
+               { "attribute", "attr", "=~", "attrname", MATCHER_ATTR },
                { "attribute", "attr", "!~", "attrname", MATCHER_NOT },
                { "attribute", "attr", "&^", "attrname", -1 },
        };
@@ -515,52 +440,34 @@ START_TEST(test_lookup)
                const char *tostring_re;
        } golden_data[] = {
                { "host.name = 'a'",       1,
-                       "HOST\\{ NAME\\{ 'a', \\(nil\\) \\}, SERVICE\\{\\}, ATTR\\{\\} \\}" },
+                       "OBJ\\[host\\]\\{ NAME\\{ 'a', \\(nil\\) \\} \\}" },
                { "host.name =~ 'a|b'",    2,
-                       "HOST\\{ NAME\\{ NULL, "PTR_RE" \\}, SERVICE\\{\\}, ATTR\\{\\} \\}" },
+                       "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
                { "host.name =~ 'host'",   0,
-                       "HOST\\{ NAME\\{ NULL, "PTR_RE" \\}, SERVICE\\{\\}, ATTR\\{\\} \\}" },
+                       "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
                { "host.name =~ '.'",      3,
-                       "HOST\\{ NAME\\{ NULL, "PTR_RE" \\}, SERVICE\\{\\}, ATTR\\{\\} \\}" },
+                       "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
                { "service.name = 's1'",   2,
-                       "HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{ "
-                                       "NAME\\{ 's1', \\(nil\\) }, ATTR\\{\\} "
-                               "\\}, ATTR\\{\\} \\}" },
+                       "OBJ\\[service\\]\\{ NAME\\{ 's1', \\(nil\\) } \\}" },
                { "service.name =~ 's'",   2,
-                       "HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{ "
-                                       "NAME\\{ NULL, "PTR_RE" }, ATTR\\{\\} "
-                               "\\}, ATTR\\{\\} \\}" },
+                       "OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}" },
                { "service.name !~ 's'",   1,
-                       "(NOT, HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{ "
-                                       "NAME\\{ NULL, "PTR_RE" }, ATTR\\{\\} "
-                               "\\}, ATTR\\{\\} \\})" },
+                       "\\(NOT, OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}\\)" },
                { "attribute.name = 'k1'", 1,
-                       "HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{\\}, ATTR\\{ "
-                                       "NAME\\{ 'k1', \\(nil\\) }, VALUE\\{ NULL, \\(nil\\) \\} "
-                               "\\} \\}" },
+                       "OBJ\\[attribute\\]\\{ NAME\\{ 'k1', \\(nil\\) \\} " },
                { "attribute.name = 'x'",  0,
-                       "HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{\\}, ATTR\\{ "
-                                       "NAME\\{ 'x', \\(nil\\) }, VALUE\\{ NULL, \\(nil\\) \\} "
-                               "\\} \\}" },
+                       "OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\}" },
                { "attribute.k1 = 'v1'",   1,
-                       "HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{\\}, ATTR\\{ "
-                                       "NAME\\{ 'k1', \\(nil\\) }, VALUE\\{ 'v1', \\(nil\\) \\} "
-                               "\\} \\}" },
+                       "ATTR\\[k1\\]\\{ VALUE\\{ 'v1', \\(nil\\) \\} \\}" },
                { "attribute.k1 != 'v1'",  2,
-                       "(NOT, HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{\\}, ATTR\\{ "
-                                       "NAME\\{ 'k1', \\(nil\\) }, VALUE\\{ 'v1', \\(nil\\) \\} "
-                               "\\} \\})" },
+                       "\\(NOT, ATTR\\[k1\\]\\{ VALUE\\{ 'v1', \\(nil\\) \\} \\}\\)" },
                { "attribute.k1 != 'v2'",  3,
-                       "(NOT, HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{\\}, ATTR\\{ "
-                                       "NAME\\{ 'k1', \\(nil\\) }, VALUE\\{ 'v2', \\(nil\\) \\} "
-                               "\\} \\})" },
+                       "\\(NOT, ATTR\\[k1\\]\\{ VALUE\\{ 'v2', \\(nil\\) \\} \\}\\)" },
                { "attribute.name != 'x' "
                  "AND attribute.y !~ 'x'", 3,
-                       "\\(AND, \\(NOT, HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{\\}, ATTR\\{ "
-                                       "NAME\\{ 'x', \\(nil\\) }, VALUE\\{ NULL, \\(nil\\) \\} "
-                               "\\} \\}\\), \\(NOT, HOST\\{ NAME\\{ NULL, \\(nil\\) \\}, SERVICE\\{\\}, ATTR\\{ "
-                                               "NAME\\{ 'y', \\(nil\\) }, VALUE\\{ NULL, "PTR_RE" \\} "
-                                       "\\} \\}\\)\\)" },
+                       "\\(AND, "
+                               "\\(NOT, OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\} \\}\\), "
+                               "\\(NOT, ATTR\\[y\\]\\{ VALUE\\{ NULL, "PTR_RE" \\} \\}\\)\\)" },
        };
 
        int check, n;