diff options
author | Thomas Haller <thaller@redhat.com> | 2022-05-06 10:47:41 (GMT) |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2024-05-17 18:52:59 (GMT) |
commit | 49f7822961f5bc6b18cd2a2d3f3b8d2ab0896d3f (patch) | |
tree | f734f4a4da775d3be14144b236105e572454e654 | |
parent | 2ebbc0342ea49df16f801eb11703ec9f868d2f97 (diff) | |
download | libnl-49f7822961f5bc6b18cd2a2d3f3b8d2ab0896d3f.zip libnl-49f7822961f5bc6b18cd2a2d3f3b8d2ab0896d3f.tar.gz libnl-49f7822961f5bc6b18cd2a2d3f3b8d2ab0896d3f.tar.bz2 |
tests: add a very basic test for route cache
Hopefully more to come, and more to improve.
-rw-r--r-- | tests/cksuite-all-netns.c | 32 | ||||
-rw-r--r-- | tests/nl-test-util.c | 50 | ||||
-rw-r--r-- | tests/nl-test-util.h | 10 |
3 files changed, 91 insertions, 1 deletions
diff --git a/tests/cksuite-all-netns.c b/tests/cksuite-all-netns.c index c6a5ce2..9b32ab8 100644 --- a/tests/cksuite-all-netns.c +++ b/tests/cksuite-all-netns.c @@ -302,6 +302,36 @@ END_TEST /*****************************************************************************/ +static void _route_init(int addr_family, struct nl_sock **sk, + struct nl_cache **cache) +{ + ck_assert(sk && !*sk); + ck_assert(cache && !*cache); + + *sk = _nltst_socket(NETLINK_ROUTE); + *cache = _nltst_rtnl_route_alloc_cache(*sk, addr_family); +} + +START_TEST(route_1) +{ + _nl_auto_nl_socket struct nl_sock *sk = NULL; + _nl_auto_nl_cache struct nl_cache *cache = NULL; + + if (_nltst_skip_no_iproute2("route_1")) + return; + + _nltst_add_link(NULL, "v1", "dummy", NULL); + _nltst_system("ip -d link set v1 up"); + + _route_init(AF_INET6, &sk, &cache); + + _nltst_assert_route_cache(cache, "fe80::/64", "6 fe80::*/128", + "ff00::/8"); +} +END_TEST + +/*****************************************************************************/ + Suite *make_nl_netns_suite(void) { Suite *suite = suite_create("netns"); @@ -311,7 +341,7 @@ Suite *make_nl_netns_suite(void) nltst_netns_fixture_teardown); tcase_add_test(tc, cache_and_clone); tcase_add_loop_test(tc, test_create_iface, 0, 17); - + tcase_add_test(tc, route_1); suite_add_tcase(suite, tc); return suite; diff --git a/tests/nl-test-util.c b/tests/nl-test-util.c index 66fb4cb..99a51a4 100644 --- a/tests/nl-test-util.c +++ b/tests/nl-test-util.c @@ -990,3 +990,53 @@ bool _nltst_select_route_match(struct nl_object *route, return false; } + +/*****************************************************************************/ + +void _nltst_assert_route_list(struct nl_object *const *objs, ssize_t len, + const char *const *expected_routes) +{ + size_t l; + size_t i; + + if (len < 0) { + l = 0; + if (objs) { + while (objs[l]) + l++; + } + } else + l = len; + + for (i = 0; i < l; i++) { + struct nl_object *route = objs[i]; + _nltst_auto_clear_select_route NLTstSelectRoute select_route = { + 0 + }; + _nl_auto_free char *s = _nltst_object_to_string(route); + + if (!expected_routes[i]) { + ck_abort_msg( + "No more expected route, but have route %zu (of %zu) as %s", + i + 1, l, s); + } + + _nltst_select_route_parse(expected_routes[i], &select_route); + + _nltst_select_route_match(route, &select_route, true); + } +} + +void _nltst_assert_route_cache_v(struct nl_cache *cache, + const char *const *expected_routes) +{ + _nl_auto_free struct nl_object **objs = NULL; + size_t len; + + ck_assert(cache); + ck_assert(expected_routes); + + objs = _nltst_cache_get_all(cache, &len); + + _nltst_assert_route_list(objs, len, expected_routes); +} diff --git a/tests/nl-test-util.h b/tests/nl-test-util.h index f843072..b751cc5 100644 --- a/tests/nl-test-util.h +++ b/tests/nl-test-util.h @@ -507,4 +507,14 @@ void _nltst_delete_link(struct nl_sock *sk, const char *ifname); void _nltst_get_link(struct nl_sock *sk, const char *ifname, int *out_ifindex, struct rtnl_link **out_link); +void _nltst_assert_route_list(struct nl_object *const *objs, ssize_t len, + const char *const *expected_routes); + +void _nltst_assert_route_cache_v(struct nl_cache *cache, + const char *const *expected_routes); + +#define _nltst_assert_route_cache(cache, ...) \ + _nltst_assert_route_cache_v(cache, \ + ((const char *const[200]){ __VA_ARGS__ })) + #endif /* __NL_TEST_UTIL_H__ */ |