Code

store_lookup: Don't allow 'IS <non-NULL>'.
[sysdb.git] / t / unit / core / store_lookup_test.c
index c125a5913a407c1e177998864d740d1427933d1d..7536f390bc96cfe9b556326271925c367ff6d0a7 100644 (file)
@@ -85,7 +85,7 @@ populate(void)
 
 START_TEST(test_store_match_name)
 {
-       sdb_store_base_t *obj;
+       sdb_store_obj_t *obj;
 
        struct {
                int type;
@@ -170,7 +170,7 @@ END_TEST
 
 START_TEST(test_store_match_attr)
 {
-       sdb_store_base_t *obj;
+       sdb_store_obj_t *obj;
 
        struct {
                const char *attr_name;
@@ -184,8 +184,6 @@ START_TEST(test_store_match_attr)
                { "1",  NULL,   1, 0 },
                { "k3", NULL,   0, 0 },
                { "k3", NULL,   1, 0 },
-               { "k3", NULL,   0, 0 },
-               { "k3", NULL,   1, 0 },
                { "k1", "v1",   0, 1 },
                { "k1", "v1",   1, 1 },
                { "k1", "^v1$", 1, 1 },
@@ -247,7 +245,7 @@ END_TEST
 
 START_TEST(test_store_cond)
 {
-       sdb_store_base_t *obj;
+       sdb_store_obj_t *obj;
 
        struct {
                const char *attr;
@@ -302,22 +300,32 @@ START_TEST(test_store_cond)
                                golden_data[i].attr, buf);
 
                for (j = 0; j < SDB_STATIC_ARRAY_LEN(tests); ++j) {
-                       sdb_store_matcher_t *m = tests[j].matcher(c);
+                       sdb_store_matcher_t *m;
                        char m_str[1024];
-                       sdb_object_deref(SDB_OBJ(c));
+
+                       m = tests[j].matcher(c);
+                       fail_unless(m != NULL,
+                                       "sdb_store_<cond>_matcher() = NULL; expected: <matcher>");
+
                        status = sdb_store_matcher_matches(m, obj);
                        fail_unless(status == *tests[j].expected,
                                        "sdb_store_matcher_matches(%s) = %d; expected: %d",
                                        sdb_store_matcher_tostring(m, m_str, sizeof(m_str)),
                                        status, *tests[j].expected);
+
+                       sdb_object_deref(SDB_OBJ(m));
                }
+
+               sdb_object_deref(SDB_OBJ(c));
        }
+
+       sdb_object_deref(SDB_OBJ(obj));
 }
 END_TEST
 
 START_TEST(test_store_match_op)
 {
-       sdb_store_base_t *obj;
+       sdb_store_obj_t *obj;
 
        sdb_store_matcher_t *always = sdb_store_name_matcher(SDB_HOST, "a", 0);
        sdb_store_matcher_t *never = sdb_store_name_matcher(SDB_HOST, "z", 0);
@@ -387,6 +395,10 @@ END_TEST
 
 START_TEST(test_parse_cmp)
 {
+       sdb_data_t hostname = { SDB_TYPE_STRING, { .string = "hostname" } };
+       sdb_data_t srvname  = { SDB_TYPE_STRING, { .string = "srvname" } };
+       sdb_data_t attrname = { SDB_TYPE_STRING, { .string = "attrname" } };
+
        sdb_store_matcher_t *check;
 
        size_t i;
@@ -395,56 +407,82 @@ START_TEST(test_parse_cmp)
                const char *obj_type;
                const char *attr;
                const char *op;
-               const char *value;
+               const sdb_data_t *value;
                int expected;
        } golden_data[] = {
-               { "host",      "name", "=",  "hostname", MATCHER_NAME },
-               { "host",      "name", "!=", "hostname", MATCHER_NOT },
-               { "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_NAME },
-               { "service",   "name", "!=", "srvname",  MATCHER_NOT },
-               { "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_NAME },
-               { "attribute", "name", "!=", "attrname", MATCHER_NOT },
-               { "attribute", "name", "=~", "attrname", MATCHER_NAME },
-               { "attribute", "name", "!~", "attrname", MATCHER_NOT },
-               { "attribute", "attr", "=",  "attrname", MATCHER_ATTR },
-               { "attribute", "attr", "!=", "attrname", MATCHER_NOT },
-               { "attribute", "attr", "=~", "attrname", MATCHER_ATTR },
-               { "attribute", "attr", "!~", "attrname", MATCHER_NOT },
-               { "attribute", "attr", "&^", "attrname", -1 },
+               { "host",      "name", "=",  &hostname, MATCHER_NAME },
+               { "host",      "name", "!=", &hostname, MATCHER_NOT },
+               { "host",      "name", "=~", &hostname, MATCHER_NAME },
+               { "host",      "name", "!~", &hostname, MATCHER_NOT },
+               { "host",      "attr", "=",  &hostname, -1 },
+               { "host",      "attr", "!=", &hostname, -1 },
+               { "host",      "name", "&^", &hostname, -1 },
+               { "host",      "name", "<",  &hostname, -1 },
+               { "host",      "name", "<=", &hostname, -1 },
+               { "host",      "name", ">=", &hostname, -1 },
+               { "host",      "name", ">",  &hostname, -1 },
+               { "service",   "name", "=",  &srvname,  MATCHER_NAME },
+               { "service",   "name", "!=", &srvname,  MATCHER_NOT },
+               { "service",   "name", "=~", &srvname,  MATCHER_NAME },
+               { "service",   "name", "!~", &srvname,  MATCHER_NOT },
+               { "service",   "attr", "=",  &srvname,  -1 },
+               { "service",   "attr", "!=", &srvname,  -1 },
+               { "service",   "name", "&^", &srvname,  -1 },
+               { "service",   "name", "<",  &srvname,  -1 },
+               { "service",   "name", "<=", &srvname,  -1 },
+               { "service",   "name", ">=", &srvname,  -1 },
+               { "service",   "name", ">",  &srvname,  -1 },
+               { "attribute", "name", "=",  &attrname, MATCHER_NAME },
+               { "attribute", "name", "!=", &attrname, MATCHER_NOT },
+               { "attribute", "name", "=~", &attrname, MATCHER_NAME },
+               { "attribute", "name", "!~", &attrname, MATCHER_NOT },
+               { "attribute", "name", "<",  &attrname, -1 },
+               { "attribute", "name", "<=", &attrname, -1 },
+               { "attribute", "name", ">=", &attrname, -1 },
+               { "attribute", "name", ">",  &attrname, -1 },
+               { "attribute", "attr", "=",  &attrname, MATCHER_ATTR },
+               { "attribute", "attr", "!=", &attrname, MATCHER_NOT },
+               { "attribute", "attr", "=~", &attrname, MATCHER_ATTR },
+               { "attribute", "attr", "!~", &attrname, MATCHER_NOT },
+               { "attribute", "attr", "&^", &attrname, -1 },
+               { "attribute", "attr", "<",  &attrname, MATCHER_LT },
+               { "attribute", "attr", "<=", &attrname, MATCHER_LE },
+/*             { "attribute", "attr", "=",  &attrname, MATCHER_EQ }, */
+               { "attribute", "attr", ">=", &attrname, MATCHER_GE },
+               { "attribute", "attr", ">",  &attrname, MATCHER_GT },
+               { "attribute", "attr", "IS", NULL,      MATCHER_ISNULL },
+               { "attribute", "attr", "IS", &attrname, -1 },
+               { "foo",       "name", "=",  &attrname, -1 },
+               { "foo",       "attr", "=",  &attrname, -1 },
        };
 
        for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
+               char buf[1024];
+
                check = sdb_store_matcher_parse_cmp(golden_data[i].obj_type,
-                               golden_data[i].attr, golden_data[i].op, golden_data[i].value);
+                               golden_data[i].attr, golden_data[i].op,
+                               golden_data[i].value);
+
+               if (sdb_data_format(golden_data[i].value,
+                                       buf, sizeof(buf), SDB_UNQUOTED) < 0)
+                       snprintf(buf, sizeof(buf), "ERR");
 
                if (golden_data[i].expected == -1) {
                        fail_unless(check == NULL,
                                        "sdb_store_matcher_parse_cmp(%s, %s, %s, %s) = %p; "
                                        "expected: NULL", golden_data[i].obj_type,
-                                       golden_data[i].attr, golden_data[i].op,
-                                       golden_data[i].value, check);
+                                       golden_data[i].attr, golden_data[i].op, buf, check);
                        continue;
                }
 
                fail_unless(check != NULL,
                                "sdb_store_matcher_parse_cmp(%s, %s, %s, %s) = %p; "
                                "expected: NULL", golden_data[i].obj_type,
-                               golden_data[i].attr, golden_data[i].op,
-                               golden_data[i].value, check);
+                               golden_data[i].attr, golden_data[i].op, buf, check);
                fail_unless(M(check)->type == golden_data[i].expected,
                                "sdb_store_matcher_parse_cmp(%s, %s, %s, %s) returned matcher "
                                "of type %d; expected: %d", golden_data[i].obj_type,
-                               golden_data[i].attr, golden_data[i].op, golden_data[i].value,
+                               golden_data[i].attr, golden_data[i].op, buf,
                                M(check)->type, golden_data[i].expected);
 
                sdb_object_deref(SDB_OBJ(check));
@@ -453,7 +491,7 @@ START_TEST(test_parse_cmp)
 END_TEST
 
 static int
-lookup_cb(sdb_store_base_t *obj, void *user_data)
+lookup_cb(sdb_store_obj_t *obj, void *user_data)
 {
        int *i = user_data;
 
@@ -476,32 +514,52 @@ START_TEST(test_lookup)
                int expected;
                const char *tostring_re;
        } golden_data[] = {
-               { "host.name = 'a'",       1,
+               { "host.name = 'a'",          1,
                        "OBJ\\[host\\]\\{ NAME\\{ 'a', \\(nil\\) \\} \\}" },
-               { "host.name =~ 'a|b'",    2,
+               { "host.name =~ 'a|b'",       2,
                        "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
-               { "host.name =~ 'host'",   0,
+               { "host.name =~ 'host'",      0,
                        "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
-               { "host.name =~ '.'",      3,
+               { "host.name =~ '.'",         3,
                        "OBJ\\[host\\]\\{ NAME\\{ NULL, "PTR_RE" \\} \\}" },
-               { "service.name = 's1'",   2,
+               { "service.name = 's1'",      2,
                        "OBJ\\[service\\]\\{ NAME\\{ 's1', \\(nil\\) } \\}" },
-               { "service.name =~ 's'",   2,
+               { "service.name =~ 's'",      2,
                        "OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}" },
-               { "service.name !~ 's'",   1,
+               { "service.name !~ 's'",      1,
                        "\\(NOT, OBJ\\[service\\]\\{ NAME\\{ NULL, "PTR_RE" } \\}\\)" },
-               { "attribute.name = 'k1'", 1,
+               { "attribute.name = 'k1'",    1,
                        "OBJ\\[attribute\\]\\{ NAME\\{ 'k1', \\(nil\\) \\} " },
-               { "attribute.name = 'x'",  0,
+               { "attribute.name = 'x'",     0,
                        "OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\}" },
-               { "attribute.k1 = 'v1'",   1,
+               { "attribute.k1 = 'v1'",      1,
                        "ATTR\\[k1\\]\\{ VALUE\\{ 'v1', \\(nil\\) \\} \\}" },
-               { "attribute.k1 != 'v1'",  2,
+               { "attribute.k1 IS NULL",     2,
+                       "\\(IS NULL, ATTR\\[k1\\]\\)" },
+               { "attribute.x1 IS NULL",     3,
+                       "\\(IS NULL, ATTR\\[x1\\]\\)" },
+               { "attribute.k1 IS NOT NULL", 1,
+                       "\\(NOT, \\(IS NULL, ATTR\\[k1\\]\\)\\)" },
+               { "attribute.x1 IS NOT NULL", 0,
+                       "\\(NOT, \\(IS NULL, ATTR\\[x1\\]\\)\\)" },
+               { "attribute.k2 < 123",       0,
+                       "ATTR\\[k2\\]\\{ < 123 \\}" },
+               { "attribute.k2 <= 123",      1,
+                       "ATTR\\[k2\\]\\{ <= 123 \\}" },
+               { "attribute.k2 >= 123",      1,
+                       "ATTR\\[k2\\]\\{ >= 123 \\}" },
+               { "attribute.k2 > 123",       0,
+                       "ATTR\\[k2\\]\\{ > 123 \\}" },
+               { "attribute.k2 = 123",       1,
+                       "ATTR\\[k2\\]\\{ = 123 \\}" },
+               { "attribute.k2 != 123",      2,
+                       "\\(NOT, ATTR\\[k2\\]\\{ = 123 \\}\\)" },
+               { "attribute.k1 != 'v1'",     2,
                        "\\(NOT, ATTR\\[k1\\]\\{ VALUE\\{ 'v1', \\(nil\\) \\} \\}\\)" },
-               { "attribute.k1 != 'v2'",  3,
+               { "attribute.k1 != 'v2'",     3,
                        "\\(NOT, ATTR\\[k1\\]\\{ VALUE\\{ 'v2', \\(nil\\) \\} \\}\\)" },
                { "attribute.name != 'x' "
-                 "AND attribute.y !~ 'x'", 3,
+                 "AND attribute.y !~ 'x'",   3,
                        "\\(AND, "
                                "\\(NOT, OBJ\\[attribute\\]\\{ NAME\\{ 'x', \\(nil\\) \\} \\}\\), "
                                "\\(NOT, ATTR\\[y\\]\\{ VALUE\\{ NULL, "PTR_RE" \\} \\}\\)\\)" },