From cd9901549239bbe424bc70a48b09f33064d906b6 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Tue, 14 Jan 2025 23:38:54 -0800 Subject: [PATCH] tests: add some tcp dialer local address tests --- src/platform/tcp_stream_test.c | 46 +++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/src/platform/tcp_stream_test.c b/src/platform/tcp_stream_test.c index e2ca694b2..1ed415c7b 100644 --- a/src/platform/tcp_stream_test.c +++ b/src/platform/tcp_stream_test.c @@ -1,5 +1,5 @@ // -// Copyright 2024 Staysail Systems, Inc. +// Copyright 2025 Staysail Systems, Inc. // Copyright 2018 Capitar IT Group BV // // This software is supplied under the terms of the MIT License, a @@ -8,6 +8,7 @@ // found online at https://opensource.org/licenses/MIT. // +#include #include #include @@ -428,6 +429,48 @@ test_tcp_listen_activation_bad_arg(void) nng_stream_listener_free(l1); } +void +test_tcp_dialer_loc_addr(void) +{ + nng_stream_dialer *d; + nng_sockaddr sa; + NUTS_PASS(nng_stream_dialer_alloc(&d, "tcp://127.0.0.1:80")); + NUTS_PASS(nng_stream_dialer_get_addr(d, NNG_OPT_LOCADDR, &sa)); + NUTS_TRUE(sa.s_family == NNG_AF_UNSPEC); + + // cannot set a local port + sa.s_in.sa_family = NNG_AF_INET; + sa.s_in.sa_port = 8080; + NUTS_FAIL(nng_stream_dialer_set_addr(d, NNG_OPT_LOCADDR, &sa), + NNG_EADDRINVAL); + +#ifdef NNG_HAVE_INET6 + // cannot set a local port + sa.s_in6.sa_family = NNG_AF_INET6; + sa.s_in6.sa_port = 8080; + NUTS_FAIL(nng_stream_dialer_set_addr(d, NNG_OPT_LOCADDR, &sa), + NNG_EADDRINVAL); +#endif + + // cannot set it to a bogus family + sa.s_inproc.sa_family = NNG_AF_INPROC; + snprintf(sa.s_inproc.sa_name, sizeof(sa.s_inproc.sa_name), "junk"); + NUTS_FAIL(nng_stream_dialer_set_addr(d, NNG_OPT_LOCADDR, &sa), + NNG_EADDRINVAL); + + // bad type test + NUTS_FAIL( + nng_stream_dialer_set_int(d, NNG_OPT_LOCADDR, 42), NNG_EBADTYPE); + + // but we can set it to a legal value + sa.s_in.sa_family = NNG_AF_INET; + sa.s_in.sa_port = 0; + sa.s_in.sa_addr = nuts_be32(0x7F000001); + NUTS_PASS(nng_stream_dialer_set_addr(d, NNG_OPT_LOCADDR, &sa)); + + nng_stream_dialer_free(d); +} + NUTS_TESTS = { { "tcp stream", test_tcp_stream }, { "tcp listen accept cancel", test_tcp_listen_accept_cancel }, @@ -443,5 +486,6 @@ NUTS_TESTS = { test_tcp_listen_activation_bogus_fd }, { "tcp socket activation bad arg", test_tcp_listen_activation_bad_arg }, + { "tcp dialer local address", test_tcp_dialer_loc_addr }, { NULL, NULL }, };