The branch, master has been updated via 95d8cdf0c36 tsocket: set errno on some failures of tsocket_address_inet_from_strings via 7217c67a4ad selftest: add a unit test for tsocket_address_inet_from_strings from 6b9b0439d84 heimdal: Remove lex.yy.c file left over from a bug in lexyacc.sh
https://git.samba.org/?p=samba.git;a=shortlog;h=master - Log ----------------------------------------------------------------- commit 95d8cdf0c361b6e3398614d28b0fb120c81649a9 Author: Uri Simchoni <u...@samba.org> Date: Sun Sep 12 22:23:53 2021 +0300 tsocket: set errno on some failures of tsocket_address_inet_from_strings Fix setting errno on all failure modes of tsocket_address_inet_from_strings. Signed-off-by: Uri Simchoni <u...@samba.org> Reviewed-by: Jeremy Allison <j...@samba.org> Autobuild-User(master): Jeremy Allison <j...@samba.org> Autobuild-Date(master): Mon Sep 13 22:27:59 UTC 2021 on sn-devel-184 commit 7217c67a4ad70bab524cd67f76c74afa240cdf29 Author: Uri Simchoni <u...@samba.org> Date: Sat Sep 11 22:57:06 2021 +0300 selftest: add a unit test for tsocket_address_inet_from_strings Signed-off-by: Uri Simchoni <u...@samba.org> Reviewed-by: Jeremy Allison <j...@samba.org> ----------------------------------------------------------------------- Summary of changes: lib/tsocket/tests/test_bsd_addr.c | 183 ++++++++++++++++++++++++++++++++++++++ lib/tsocket/tsocket_bsd.c | 2 + lib/tsocket/wscript_build | 6 ++ selftest/tests.py | 2 + 4 files changed, 193 insertions(+) create mode 100644 lib/tsocket/tests/test_bsd_addr.c Changeset truncated at 500 lines: diff --git a/lib/tsocket/tests/test_bsd_addr.c b/lib/tsocket/tests/test_bsd_addr.c new file mode 100644 index 00000000000..9f9fa29ec2d --- /dev/null +++ b/lib/tsocket/tests/test_bsd_addr.c @@ -0,0 +1,183 @@ +/* + * Unix SMB/CIFS implementation. + * + * Copyright (C) 2021 Uri Simchoni <u...@samba.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <errno.h> +#include <setjmp.h> +#include <stdarg.h> +#include <stddef.h> +#include <stdint.h> + +#include <cmocka.h> +#include <tsocket.h> + +static int setup(void **state) +{ + TALLOC_CTX *mem_ctx = talloc_new(NULL); + + assert_non_null(mem_ctx); + *state = mem_ctx; + + return 0; +} + +static int teardown(void **state) +{ + TALLOC_CTX *mem_ctx = *state; + TALLOC_FREE(mem_ctx); + + return 0; +} + +static void test_address_inet_from_strings(void **state) +{ + int rc = 0; + int save_errno; + TALLOC_CTX *mem_ctx = *state; + struct tsocket_address *addr = NULL; + char *addr_s = NULL; + + /* + * Unspecified IP family, given an IPv4 address + */ + rc = tsocket_address_inet_from_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"); + assert_true(tsocket_address_is_inet(addr, "ip")); + assert_true(tsocket_address_is_inet(addr, "ipv4")); + assert_false(tsocket_address_is_inet(addr, "ipv6")); + assert_int_equal(tsocket_address_inet_port(addr), 1234); + TALLOC_FREE(addr); + TALLOC_FREE(addr_s); + + /* + * Expecting IPv4, given an IPv4 address + */ + rc = tsocket_address_inet_from_strings(mem_ctx, "ipv4", "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"); + assert_true(tsocket_address_is_inet(addr, "ip")); + assert_true(tsocket_address_is_inet(addr, "ipv4")); + assert_false(tsocket_address_is_inet(addr, "ipv6")); + assert_int_equal(tsocket_address_inet_port(addr), 1234); + TALLOC_FREE(addr); + TALLOC_FREE(addr_s); + + /* + * Expecting IPv6, given an IPv4 address + */ + errno = 0; + rc = tsocket_address_inet_from_strings(mem_ctx, "ipv6", "1.2.3.4", 1234, + &addr); + save_errno = errno; + assert_int_equal(rc, -1); + assert_int_not_equal(save_errno, 0); + assert_null(addr); + + /* + * Unspecified IP family, given an IPv6 address + */ + rc = tsocket_address_inet_from_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"); + assert_true(tsocket_address_is_inet(addr, "ip")); + assert_false(tsocket_address_is_inet(addr, "ipv4")); + assert_true(tsocket_address_is_inet(addr, "ipv6")); + assert_int_equal(tsocket_address_inet_port(addr), 1234); + TALLOC_FREE(addr); + TALLOC_FREE(addr_s); + + /* + * Expecting IPv4, given an IPv6 address + */ + errno = 0; + rc = tsocket_address_inet_from_strings(mem_ctx, "ipv4", "2001::1", 1234, + &addr); + save_errno = errno; + assert_int_equal(rc, -1); + assert_int_not_equal(save_errno, 0); + assert_null(addr); + + /* + * expecting IPv6, given an IPv6 address + */ + rc = tsocket_address_inet_from_strings(mem_ctx, "ipv6", "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"); + assert_true(tsocket_address_is_inet(addr, "ip")); + assert_false(tsocket_address_is_inet(addr, "ipv4")); + assert_true(tsocket_address_is_inet(addr, "ipv6")); + assert_int_equal(tsocket_address_inet_port(addr), 1234); + TALLOC_FREE(addr); + TALLOC_FREE(addr_s); + + /* + * Unspecified IP family, given an illegal address + */ + errno = 0; + rc = tsocket_address_inet_from_strings(mem_ctx, "ip", "localhost", 1234, + &addr); + save_errno = errno; + assert_int_equal(rc, -1); + assert_int_not_equal(save_errno, 0); + assert_null(addr); + + /* + * Illegal IP family + */ + errno = 0; + rc = tsocket_address_inet_from_strings(mem_ctx, "ipx", "1.2.3.4", 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), + }; + + if (argc == 2) { + cmocka_set_test_filter(argv[1]); + } + cmocka_set_message_output(CM_OUTPUT_SUBUNIT); + + rc = cmocka_run_group_tests(tests, setup, teardown); + + return rc; +} diff --git a/lib/tsocket/tsocket_bsd.c b/lib/tsocket/tsocket_bsd.c index 10ea1347149..39a95e4c5ec 100644 --- a/lib/tsocket/tsocket_bsd.c +++ b/lib/tsocket/tsocket_bsd.c @@ -411,6 +411,8 @@ int _tsocket_address_inet_from_strings(TALLOC_CTX *mem_ctx, if (ret != 0) { switch (ret) { case EAI_FAIL: + case EAI_NONAME: + case EAI_ADDRFAMILY: errno = EINVAL; break; } diff --git a/lib/tsocket/wscript_build b/lib/tsocket/wscript_build index 420daff8174..8fe83f16f11 100644 --- a/lib/tsocket/wscript_build +++ b/lib/tsocket/wscript_build @@ -7,3 +7,9 @@ bld.SAMBA_SUBSYSTEM('LIBTSOCKET', public_headers='tsocket.h tsocket_internal.h', ) +bld.SAMBA_BINARY('test_tsocket_bsd_addr', + source='tests/test_bsd_addr.c', + deps='cmocka replace LIBTSOCKET', + local_include=False, + for_selftest=True) + diff --git a/selftest/tests.py b/selftest/tests.py index fade8eaacf5..128374210c4 100644 --- a/selftest/tests.py +++ b/selftest/tests.py @@ -437,3 +437,5 @@ if with_elasticsearch_backend: [os.path.join(bindir(), "default/source3/test_mdsparser_es")] + [configuration]) plantestsuite("samba.unittests.credentials", "none", [os.path.join(bindir(), "default/auth/credentials/test_creds")]) +plantestsuite("samba.unittests.tsocket_bsd_addr", "none", + [os.path.join(bindir(), "default/lib/tsocket/test_tsocket_bsd_addr")]) -- Samba Shared Repository