From: Yu Watanabe Date: Fri, 10 May 2024 03:17:10 +0000 (+0900) Subject: test: add basic tests for in_addr_prefix_covers_full() X-Git-Tag: v256-rc2~51^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4591efffc05529e1c6d51468954fda98d0cd5cba;p=thirdparty%2Fsystemd.git test: add basic tests for in_addr_prefix_covers_full() --- diff --git a/src/test/test-in-addr-util.c b/src/test/test-in-addr-util.c index 0d159669402..fcc9a0a72d4 100644 --- a/src/test/test-in-addr-util.c +++ b/src/test/test-in-addr-util.c @@ -405,4 +405,55 @@ TEST(in_addr_prefixlen_to_netmask) { } } +static void in_addr_prefix_covers_full_one(const char *prefix, const char *address, int expected) { + union in_addr_union p, a; + unsigned char plen, alen; + int family, r; + + assert_se(in_addr_prefix_from_string_auto(prefix, &family, &p, &plen) >= 0); + assert_se(in_addr_prefix_from_string(address, family, &a, &alen) >= 0); + r = in_addr_prefix_covers_full(family, &p, plen, &a, alen); + if (r != expected) + log_error("in_addr_prefix_covers_full(%s, %s)=%i (expected=%i)", prefix, address, r, expected); + assert_se(r == expected); +} + +TEST(in_addr_prefix_covers_full) { + /* From issue #32715. */ + in_addr_prefix_covers_full_one("192.168.235.129/32", "192.168.0.128/32", 0); + in_addr_prefix_covers_full_one("192.168.235.130/32", "192.168.0.128/32", 0); + in_addr_prefix_covers_full_one("169.254.0.0/17", "192.168.0.128/32", 0); + in_addr_prefix_covers_full_one("169.254.128.0/17", "192.168.0.128/32", 0); + in_addr_prefix_covers_full_one("0.0.0.0/1", "192.168.0.128/32", 0); + in_addr_prefix_covers_full_one("128.0.0.0/1", "192.168.0.128/32", 1); + in_addr_prefix_covers_full_one("0.0.0.0/0", "192.168.0.128/32", 1); + + for (unsigned i = 0; i <= 32; i++) { + _cleanup_free_ char *prefix = NULL; + + assert_se(asprintf(&prefix, "192.168.0.128/%u", i) >= 0); + + for (unsigned j = 0; j <= 32; j++) { + _cleanup_free_ char *address = NULL; + + assert_se(asprintf(&address, "192.168.0.128/%u", j) >= 0); + in_addr_prefix_covers_full_one(prefix, address, i <= j); + } + } + + for (unsigned i = 0; i <= 32; i++) { + _cleanup_free_ char *prefix = NULL; + + assert_se(asprintf(&prefix, "192.168.235.129/%u", i) >= 0); + in_addr_prefix_covers_full_one(prefix, "192.168.0.128/32", i <= 16); + } + + for (unsigned i = 0; i <= 128; i++) { + _cleanup_free_ char *prefix = NULL; + + assert_se(asprintf(&prefix, "dead:beef::/%u", i) >= 0); + in_addr_prefix_covers_full_one(prefix, "dead:0:beef::1/128", i <= 16); + } +} + DEFINE_TEST_MAIN(LOG_DEBUG);