Skip to content

Commit 2f373f8

Browse files
michalvaskobradh352
andcommitted
tests UPDATE new leafref backlinks test
Co-authored-by: Brad House <[email protected]>
1 parent b7e024d commit 2f373f8

File tree

1 file changed

+220
-0
lines changed

1 file changed

+220
-0
lines changed

tests/utests/schema/test_schema.c

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,6 +1887,225 @@ test_lysc_path(void **state)
18871887
free(path);
18881888
}
18891889

1890+
/* TEST */
1891+
static ly_bool
1892+
compare_str_nodeset(struct ly_set *expected, struct ly_set *received)
1893+
{
1894+
ly_bool is_error = 0;
1895+
size_t r;
1896+
size_t e;
1897+
1898+
for (e = 0; expected && e < expected->count; e++) {
1899+
const char *epath = expected->objs[e];
1900+
ly_bool found = 0;
1901+
1902+
for (r = 0; received && (r < received->count); r++) {
1903+
const char *rpath = received->objs[r];
1904+
1905+
if (!strcmp(epath, rpath)) {
1906+
found = 1;
1907+
break;
1908+
}
1909+
}
1910+
1911+
if (!found) {
1912+
fprintf(stderr, "< %s\n", epath);
1913+
is_error = 1;
1914+
}
1915+
}
1916+
1917+
/* If the count was equal and there was no error, no need to scan again */
1918+
if (expected && received && (expected->count == received->count) && !is_error) {
1919+
return 1;
1920+
}
1921+
1922+
for (r = 0; received && (r < received->count); r++) {
1923+
ly_bool found = 0;
1924+
const char *rpath = received->objs[r];
1925+
1926+
for (e = 0; expected && (e < expected->count) && !found; e++) {
1927+
char *epath = expected->objs[e];
1928+
1929+
if (!strcmp(epath, rpath)) {
1930+
found = 1;
1931+
break;
1932+
}
1933+
}
1934+
if (!found) {
1935+
fprintf(stderr, "> %s\n", rpath);
1936+
}
1937+
}
1938+
1939+
return 0;
1940+
}
1941+
1942+
static struct ly_set *
1943+
strlist_to_pathset(const char **pathlist)
1944+
{
1945+
struct ly_set *set = NULL;
1946+
uint32_t i;
1947+
1948+
if (!pathlist || !pathlist[0]) {
1949+
return NULL;
1950+
}
1951+
1952+
ly_set_new(&set);
1953+
1954+
for (i = 0; pathlist[i]; i++) {
1955+
ly_set_add(set, pathlist[i], 0, NULL);
1956+
}
1957+
1958+
return set;
1959+
}
1960+
1961+
static struct ly_set *
1962+
lysc_nodeset_to_pathset(struct ly_set *nodeset)
1963+
{
1964+
struct ly_set *set = NULL;
1965+
uint32_t i;
1966+
1967+
if (!nodeset || !nodeset->count) {
1968+
return NULL;
1969+
}
1970+
1971+
ly_set_new(&set);
1972+
1973+
for (i = 0; i < nodeset->count; i++) {
1974+
char *path = lysc_path(nodeset->snodes[i], LYSC_PATH_DATA, NULL, 0);
1975+
1976+
ly_set_add(set, path, 0, NULL);
1977+
}
1978+
1979+
return set;
1980+
}
1981+
1982+
static void
1983+
test_lysc_backlinks(void **state)
1984+
{
1985+
const char *expect1[] = {
1986+
/* Built-ins, not sure how to exclude those when not limiting by
1987+
* path */
1988+
"/ietf-yang-library:yang-library/module-set/module/deviation",
1989+
"/ietf-yang-library:yang-library/schema/module-set",
1990+
"/ietf-yang-library:yang-library/datastore/schema",
1991+
"/ietf-yang-library:yang-library-update/content-id",
1992+
"/ietf-yang-library:yang-library-change/module-set-id",
1993+
/* Normal expected */
1994+
"/b:my_extref_list/my_extref",
1995+
"/a:refstr",
1996+
"/a:refnum",
1997+
"/b:my_extref_union",
1998+
NULL
1999+
};
2000+
2001+
const char *expect2[] = {
2002+
"/b:my_extref_list/my_extref",
2003+
"/a:refstr",
2004+
"/b:my_extref_union",
2005+
NULL
2006+
};
2007+
2008+
const char *expect3[] = {
2009+
"/b:my_extref_list/my_extref",
2010+
"/a:refstr",
2011+
"/a:refnum",
2012+
"/b:my_extref_union",
2013+
NULL
2014+
};
2015+
2016+
struct {
2017+
const char *match_path;
2018+
ly_bool match_ancestors;
2019+
const char **expected_paths;
2020+
} tests[] = {
2021+
{NULL, 0, expect1},
2022+
{"/a:my_list/my_leaf_string", 0, expect2},
2023+
{"/a:my_list", 1, expect3}
2024+
};
2025+
const char *str;
2026+
uint32_t i;
2027+
2028+
str = "module a {\n"
2029+
" namespace urn:a;\n"
2030+
" prefix a;\n"
2031+
" list my_list {\n"
2032+
" key my_leaf_string;\n"
2033+
" leaf my_leaf_string {\n"
2034+
" type string;\n"
2035+
" }\n"
2036+
" leaf my_leaf_number {\n"
2037+
" type uint32;\n"
2038+
" }\n"
2039+
" }\n"
2040+
" leaf refstr {\n"
2041+
" type leafref {\n"
2042+
" path \"../my_list/my_leaf_string\";\n"
2043+
" }\n"
2044+
" }\n"
2045+
" leaf refnum {\n"
2046+
" type leafref {\n"
2047+
" path \"../my_list/my_leaf_number\";\n"
2048+
" }\n"
2049+
" }\n"
2050+
"}\n";
2051+
2052+
assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
2053+
CHECK_LOG_CTX(NULL, NULL, 0);
2054+
2055+
str = "module b {\n"
2056+
" namespace urn:b;\n"
2057+
" prefix b;\n"
2058+
" import a {\n"
2059+
" prefix a;\n"
2060+
" }\n"
2061+
" list my_extref_list {\n"
2062+
" key my_leaf_string;\n"
2063+
" leaf my_leaf_string {\n"
2064+
" type string;\n"
2065+
" }\n"
2066+
" leaf my_extref {\n"
2067+
" type leafref {\n"
2068+
" path \"/a:my_list/a:my_leaf_string\";\n"
2069+
" }\n"
2070+
" }\n"
2071+
" }\n"
2072+
" leaf my_extref_union {\n"
2073+
" type union {\n"
2074+
" type leafref {\n"
2075+
" path \"/a:my_list/a:my_leaf_string\";\n"
2076+
" }\n"
2077+
" type leafref {\n"
2078+
" path \"/a:my_list/a:my_leaf_number\";\n"
2079+
" }\n"
2080+
" type uint32;\n"
2081+
" }\n"
2082+
" }\n"
2083+
"}\n";
2084+
2085+
assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
2086+
CHECK_LOG_CTX(NULL, NULL, 0);
2087+
2088+
for (i = 0; i < sizeof tests / sizeof *tests; i++) {
2089+
const struct lysc_node *node = NULL;
2090+
struct ly_set *set = NULL, *expected = NULL, *received = NULL;
2091+
2092+
if (tests[i].match_path) {
2093+
node = lys_find_path(UTEST_LYCTX, NULL, tests[i].match_path, 0);
2094+
assert_non_null(node);
2095+
}
2096+
2097+
assert_int_equal(LY_SUCCESS, lysc_node_lref_backlinks(UTEST_LYCTX, node, tests[i].match_ancestors, &set));
2098+
2099+
expected = strlist_to_pathset(tests[i].expected_paths);
2100+
received = lysc_nodeset_to_pathset(set);
2101+
assert_int_equal(1, compare_str_nodeset(expected, received));
2102+
2103+
ly_set_free(expected, NULL);
2104+
ly_set_free(received, free);
2105+
ly_set_free(set, NULL);
2106+
}
2107+
}
2108+
18902109
int
18912110
main(void)
18922111
{
@@ -1909,6 +2128,7 @@ main(void)
19092128
UTEST(test_extension_compile),
19102129
UTEST(test_ext_recursive),
19112130
UTEST(test_lysc_path),
2131+
UTEST(test_lysc_backlinks),
19122132
};
19132133

19142134
return cmocka_run_group_tests(tests, NULL, NULL);

0 commit comments

Comments
 (0)