diff options
Diffstat (limited to 'src/libnm-glib-aux/tests/test-shared-general.c')
-rw-r--r-- | src/libnm-glib-aux/tests/test-shared-general.c | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/src/libnm-glib-aux/tests/test-shared-general.c b/src/libnm-glib-aux/tests/test-shared-general.c index 563c370243..84d87d4848 100644 --- a/src/libnm-glib-aux/tests/test-shared-general.c +++ b/src/libnm-glib-aux/tests/test-shared-general.c @@ -2313,6 +2313,130 @@ test_inet_utils(void) /*****************************************************************************/ +static gboolean +_inet_parse(int addr_family, const char *str, gboolean accept_legacy, gpointer out_addr) +{ + int addr_family2 = -1; + int *const p_addr_family2 = nmtst_get_rand_bool() ? &addr_family2 : NULL; + NMIPAddr addr; + gboolean success; + + g_assert(NM_IN_SET(addr_family, AF_INET, AF_INET6)); + + success = + nm_inet_parse_bin_full((p_addr_family2 && nmtst_get_rand_bool()) ? AF_UNSPEC : addr_family, + accept_legacy, + str, + p_addr_family2, + &addr); + + if (success) { + g_assert(!p_addr_family2 || NM_IN_SET(*p_addr_family2, AF_INET, AF_INET6)); + if (p_addr_family2 && *p_addr_family2 != addr_family) { + success = FALSE; + } else + g_assert(!p_addr_family2 || *p_addr_family2 == addr_family); + } else + g_assert(addr_family2 == -1); + + if (out_addr && success) + nm_ip_addr_set(addr_family, out_addr, &addr); + + return success; +} + +#define _inet_parse_fail(check, accept_legacy) \ + G_STMT_START \ + { \ + NMIPAddr _addr; \ + gboolean _success; \ + \ + _success = _inet_parse(nmtst_get_rand_bool() ? AF_INET : AF_INET6, \ + "" check "", \ + (accept_legacy), \ + nmtst_get_rand_bool() ? &_addr : NULL); \ + g_assert(!_success); \ + } \ + G_STMT_END + +#define _inet_parse_good(check, expected, accept_legacy) \ + G_STMT_START \ + { \ + int _accept_legacy = (accept_legacy); \ + const char *const _check = "" check ""; \ + const char *const _expected = expected ?: _check; \ + NMIPAddr _addr[2]; \ + gboolean _success[2]; \ + \ + if (_accept_legacy == -1) \ + _accept_legacy = nmtst_get_rand_bool(); \ + \ + _success[0] = _inet_parse(AF_INET6, _check, _accept_legacy, &_addr[0]); \ + _success[1] = _inet_parse(AF_INET, _check, _accept_legacy, &_addr[1]); \ + \ + g_assert(NM_IN_SET(_success[0], FALSE, TRUE)); \ + g_assert(NM_IN_SET(_success[1], FALSE, TRUE)); \ + g_assert(_success[0] != _success[1]); \ + \ + if (_success[0]) \ + nmtst_assert_ip6_address(&_addr[0].addr6, _expected); \ + else \ + nmtst_assert_ip4_address(_addr[1].addr4, _expected); \ + \ + if (_success[1]) { \ + in_addr_t _a4; \ + int _r; \ + \ + _r = nmtst_inet_aton(_check, &_a4); \ + g_assert_cmpint(_r, ==, 0); \ + nmtst_assert_ip4_address(_a4, _expected); \ + } \ + } \ + G_STMT_END + +static void +test_inet_parse_ip4_legacy(void) +{ + _inet_parse_fail("", -1); + _inet_parse_fail(" ", -1); + _inet_parse_fail("a", -1); + _inet_parse_fail("0", -1); + _inet_parse_fail("0.1", -1); + _inet_parse_fail("0.4.1", -1); + _inet_parse_fail("1.2.3.05", FALSE); + _inet_parse_fail("192.000.002.010", FALSE); + _inet_parse_fail("1.2.3..5", -1); + _inet_parse_fail("1.2.3.0x", -1); + _inet_parse_fail("0xC0000234", -1); + _inet_parse_fail("192.0.2.2X", -1); + _inet_parse_fail("192.0.2.3 Y", -1); + _inet_parse_fail("192.0.2.4\nZ", -1); + _inet_parse_fail("192.0.2.5\tT", -1); + _inet_parse_fail("192.0.2.6 Y", -1); + _inet_parse_fail("192.0.2.7\n", -1); + _inet_parse_fail("192.0.2.7\t", -1); + _inet_parse_fail("192.0.2.7 ", -1); + _inet_parse_fail("00x0019.0000001.000000.0x1", -1); + _inet_parse_fail("192.0.2.7.", -1); + _inet_parse_fail("192.0.2.7.0", -1); + + _inet_parse_good("192.0.2.1", NULL, -1); + _inet_parse_good("1.2.3.4", NULL, -1); + _inet_parse_good("192.167.3.4", NULL, -1); + + _inet_parse_good("192.000.002.010", "192.0.2.8", TRUE); + _inet_parse_good("255.000.000.000", "255.0.0.0", TRUE); + _inet_parse_good("1.2.3.05", "1.2.3.5", TRUE); + _inet_parse_good("01.2.3.05", "1.2.3.5", TRUE); + _inet_parse_good("192.00167.0003.4", "192.119.3.4", TRUE); + _inet_parse_good("0x19.00167.0003.4", "25.119.3.4", TRUE); + _inet_parse_good("0x19.000000167.0000003.4", "25.119.3.4", TRUE); + _inet_parse_good("0x0019.000000167.0000003.04", "25.119.3.4", TRUE); + _inet_parse_good("0x0019.0000001.000000.0x1", "25.1.0.1", TRUE); +} + +/*****************************************************************************/ + static void test_garray(void) { @@ -2495,6 +2619,7 @@ main(int argc, char **argv) g_test_add_func("/general/test_path_simplify", test_path_simplify); g_test_add_func("/general/test_hostname_is_valid", test_hostname_is_valid); g_test_add_func("/general/test_inet_utils", test_inet_utils); + g_test_add_func("/general/test_inet_parse_ip4_legacy", test_inet_parse_ip4_legacy); g_test_add_func("/general/test_garray", test_garray); g_test_add_func("/general/test_nm_prioq", test_nm_prioq); g_test_add_func("/general/test_nm_random", test_nm_random); |