Skip to content

Commit

Permalink
tests: add some tcp dialer local address tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gdamore committed Jan 15, 2025
1 parent 5b73132 commit cd99015
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/platform/tcp_stream_test.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2024 Staysail Systems, Inc. <[email protected]>
// Copyright 2025 Staysail Systems, Inc. <[email protected]>
// Copyright 2018 Capitar IT Group BV <[email protected]>
//
// This software is supplied under the terms of the MIT License, a
Expand All @@ -8,6 +8,7 @@
// found online at https://opensource.org/licenses/MIT.
//

#include <stdio.h>
#include <string.h>

#include <nng/nng.h>
Expand Down Expand Up @@ -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 },
Expand All @@ -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 },
};

0 comments on commit cd99015

Please sign in to comment.