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