From 3f4660900a71816df505c2e634eef86a86afcda3 Mon Sep 17 00:00:00 2001 From: Uri Simchoni Date: Thu, 16 Sep 2021 20:03:59 +0300 Subject: [PATCH] selftest: test tsocket_address_inet_from_hostport_strings Signed-off-by: Uri Simchoni Reviewed-by: Andrew Bartlett Autobuild-User(master): Andrew Bartlett Autobuild-Date(master): Tue Sep 28 10:34:12 UTC 2021 on sn-devel-184 --- lib/tsocket/tests/test_bsd_addr.c | 143 ++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) diff --git a/lib/tsocket/tests/test_bsd_addr.c b/lib/tsocket/tests/test_bsd_addr.c index baefb8ce236..ac6e798b952 100644 --- a/lib/tsocket/tests/test_bsd_addr.c +++ b/lib/tsocket/tests/test_bsd_addr.c @@ -208,11 +208,154 @@ static void test_address_inet_from_strings(void **state) TALLOC_FREE(addr_s); } +static void test_address_inet_from_hostport_strings(void **state) +{ + int rc = 0; + int save_errno; + TALLOC_CTX *mem_ctx = *state; + struct tsocket_address *addr = NULL; + char *addr_s = NULL; + + /* + * IPv4 host:port + */ + rc = tsocket_address_inet_from_hostport_strings( + mem_ctx, "ip", "1.2.3.4:5678", 1234, &addr); + assert_return_code(rc, errno); + assert_non_null(addr); + addr_s = tsocket_address_string(addr, mem_ctx); + assert_non_null(addr_s); + assert_string_equal(addr_s, "ipv4:1.2.3.4:5678"); + TALLOC_FREE(addr); + TALLOC_FREE(addr_s); + + /* + * IPv4 host + */ + rc = tsocket_address_inet_from_hostport_strings( + mem_ctx, "ip", "1.2.3.4", 1234, &addr); + assert_return_code(rc, errno); + assert_non_null(addr); + addr_s = tsocket_address_string(addr, mem_ctx); + assert_non_null(addr_s); + assert_string_equal(addr_s, "ipv4:1.2.3.4:1234"); + TALLOC_FREE(addr); + TALLOC_FREE(addr_s); + + /* + * IPv6 [host]:port + */ + rc = tsocket_address_inet_from_hostport_strings( + mem_ctx, "ip", "[2001::1]:5678", 1234, &addr); + assert_return_code(rc, errno); + assert_non_null(addr); + addr_s = tsocket_address_string(addr, mem_ctx); + assert_non_null(addr_s); + assert_string_equal(addr_s, "ipv6:2001::1:5678"); + TALLOC_FREE(addr); + TALLOC_FREE(addr_s); + + /* + * IPv6 [host] + */ + rc = tsocket_address_inet_from_hostport_strings( + mem_ctx, "ip", "[2001::1]", 1234, &addr); + assert_return_code(rc, errno); + assert_non_null(addr); + addr_s = tsocket_address_string(addr, mem_ctx); + assert_non_null(addr_s); + assert_string_equal(addr_s, "ipv6:2001::1:1234"); + TALLOC_FREE(addr); + TALLOC_FREE(addr_s); + + /* + * IPv6 host + */ + rc = tsocket_address_inet_from_hostport_strings( + mem_ctx, "ip", "2001::1", 1234, &addr); + assert_return_code(rc, errno); + assert_non_null(addr); + addr_s = tsocket_address_string(addr, mem_ctx); + assert_non_null(addr_s); + assert_string_equal(addr_s, "ipv6:2001::1:1234"); + TALLOC_FREE(addr); + TALLOC_FREE(addr_s); + + /* + * Given NULL, verify it returns something + */ + rc = tsocket_address_inet_from_hostport_strings( + mem_ctx, "ipv6", NULL, 1234, &addr); + assert_return_code(rc, errno); + assert_non_null(addr); + addr_s = tsocket_address_string(addr, mem_ctx); + assert_non_null(addr_s); + assert_string_equal(addr_s, "ipv6::::1234"); + TALLOC_FREE(addr); + TALLOC_FREE(addr_s); + + /* + * [host]grarbage + */ + errno = 0; + rc = tsocket_address_inet_from_hostport_strings( + mem_ctx, "ip", "[2001::1]garbage", 1234, &addr); + save_errno = errno; + assert_int_equal(rc, -1); + assert_int_not_equal(save_errno, 0); + assert_null(addr); + + /* + * [host]:grarbage + */ + errno = 0; + rc = tsocket_address_inet_from_hostport_strings( + mem_ctx, "ip", "[2001::1]:garbage", 1234, &addr); + save_errno = errno; + assert_int_equal(rc, -1); + assert_int_not_equal(save_errno, 0); + assert_null(addr); + + /* + * host:grarbage + */ + errno = 0; + rc = tsocket_address_inet_from_hostport_strings( + mem_ctx, "ip", "1.2.3.4:garbage", 1234, &addr); + save_errno = errno; + assert_int_equal(rc, -1); + assert_int_not_equal(save_errno, 0); + assert_null(addr); + + /* + * [host]: + */ + errno = 0; + rc = tsocket_address_inet_from_hostport_strings( + mem_ctx, "ip", "[2001::1]:100000", 1234, &addr); + save_errno = errno; + assert_int_equal(rc, -1); + assert_int_not_equal(save_errno, 0); + assert_null(addr); + + /* + * host: + */ + errno = 0; + rc = tsocket_address_inet_from_hostport_strings( + mem_ctx, "ip", "1.2.3.4:100000", 1234, &addr); + save_errno = errno; + assert_int_equal(rc, -1); + assert_int_not_equal(save_errno, 0); + assert_null(addr); +} + int main(int argc, char *argv[]) { int rc; const struct CMUnitTest tests[] = { cmocka_unit_test(test_address_inet_from_strings), + cmocka_unit_test(test_address_inet_from_hostport_strings), }; if (argc == 2) {