Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tcp dialer tests: add local address tests #2037

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/core/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -1528,16 +1528,21 @@ nni_pipe_add(nni_pipe *p)

// nni_pipe_start attempts to start the pipe, adding it to the socket and
// endpoints and calling callbacks, etc. The pipe should already have finished
// any negotiation needed at the transport layer.
// any negotiation needed at the transport layer. Note carefully that the pipe
// may be destroyed before this function returns, as a result of work done by
// this function.
void
nni_pipe_start(nni_pipe *p)
{
// exactly one of these must be set.
NNI_ASSERT(p->p_listener == NULL || p->p_dialer == NULL);
NNI_ASSERT(p->p_listener != NULL || p->p_dialer != NULL);

// NB: starting the pipe can actually cause the pipe
// to be deallocated before this returns (if it is rejected)
if (p->p_listener) {
NNI_ASSERT(p->p_dialer == NULL);
listener_start_pipe(p->p_listener, p);
}
if (p->p_dialer) {
NNI_ASSERT(p->p_listener == NULL);
} else if (p->p_dialer) {
dialer_start_pipe(p->p_dialer, p);
}
}
Expand Down
11 changes: 4 additions & 7 deletions src/platform/posix/posix_sockaddr.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ nni_posix_nn2sockaddr(void *sa, const nni_sockaddr *na)
sin = (void *) sa;
nsin = &na->s_in;
memset(sin, 0, sizeof(*sin));
sin->sin_family = PF_INET;
sin->sin_family = AF_INET;
sin->sin_port = nsin->sa_port;
sin->sin_addr.s_addr = nsin->sa_addr;
return (sizeof(*sin));
Expand All @@ -63,10 +63,7 @@ nni_posix_nn2sockaddr(void *sa, const nni_sockaddr *na)
sin6 = (void *) sa;
nsin6 = &na->s_in6;
memset(sin6, 0, sizeof(*sin6));
#ifdef SIN6_LEN
sin6->sin6_len = sizeof(*sin6);
#endif
sin6->sin6_family = PF_INET6;
sin6->sin6_family = AF_INET6;
sin6->sin6_port = nsin6->sa_port;
sin6->sin6_scope_id = nsin6->sa_scope;
memcpy(sin6->sin6_addr.s6_addr, nsin6->sa_addr, 16);
Expand All @@ -82,7 +79,7 @@ nni_posix_nn2sockaddr(void *sa, const nni_sockaddr *na)
if (nni_strlcpy(spath->sun_path, nspath->sa_path, sz) >= sz) {
return (0);
}
spath->sun_family = PF_UNIX;
spath->sun_family = AF_UNIX;
return (sizeof(*spath));

case NNG_AF_ABSTRACT:
Expand All @@ -92,7 +89,7 @@ nni_posix_nn2sockaddr(void *sa, const nni_sockaddr *na)
return (0);
}
memset(spath, 0, sizeof(*spath));
spath->sun_family = PF_UNIX;
spath->sun_family = AF_UNIX;
spath->sun_path[0] = '\0'; // abstract starts with nul

// We support auto-bind with an empty string. There is
Expand Down
14 changes: 6 additions & 8 deletions src/platform/posix/posix_tcpdial.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,16 +379,14 @@
default:
return (NNG_EADDRINVAL);
}
if (d != NULL) {
nni_mtx_lock(&d->mtx);
if (d->closed) {
nni_mtx_unlock(&d->mtx);
return (NNG_ECLOSED);
}
d->src = ss;
d->srclen = len;
nni_mtx_lock(&d->mtx);
if (d->closed) {
nni_mtx_unlock(&d->mtx);
return (NNG_ECLOSED);

Check warning on line 385 in src/platform/posix/posix_tcpdial.c

View check run for this annotation

Codecov / codecov/patch

src/platform/posix/posix_tcpdial.c#L385

Added line #L385 was not covered by tests
}
d->src = ss;
d->srclen = len;
nni_mtx_unlock(&d->mtx);
return (0);
}

Expand Down
133 changes: 133 additions & 0 deletions src/platform/tcp_stream_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,131 @@ test_tcp_listen_activation_bad_arg(void)
nng_stream_listener_free(l1);
}

void
test_tcp_dialer_local_bind(void)
{
nng_stream_dialer *d;
nng_sockaddr sa = { 0 };

NUTS_PASS(nng_stream_dialer_alloc(&d, "tcp://127.0.0.1:80"));

sa.s_in.sa_family = NNG_AF_INET;
sa.s_in.sa_addr = nuts_be32(0x7F000001); // loopback
sa.s_in.sa_port = 0;
NUTS_PASS(nng_stream_dialer_set_addr(d, NNG_OPT_LOCADDR, &sa));
memset(&sa, 0, sizeof(sa));
NUTS_PASS(nng_stream_dialer_get_addr(d, NNG_OPT_LOCADDR, &sa));
NUTS_TRUE(sa.s_in.sa_family == NNG_AF_INET);
NUTS_TRUE(sa.s_in.sa_addr == nuts_be32(0x7F000001));
NUTS_TRUE(sa.s_in.sa_port == 0);
nng_stream_dialer_free(d);
}

void
test_tcp_dialer_local_bind_v6(void)
{
#ifdef NNG_ENABLE_IPV6
nng_stream_dialer *d;
nng_sockaddr sa = { 0 };
uint8_t loopback[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1 };

NUTS_PASS(nng_stream_dialer_alloc(&d, "tcp://[::1]:80"));

sa.s_in6.sa_family = NNG_AF_INET6;
memcpy(sa.s_in6.sa_addr, loopback, 16);
sa.s_in6.sa_addr[15] = 1;
sa.s_in6.sa_port = 0;
NUTS_PASS(nng_stream_dialer_set_addr(d, NNG_OPT_LOCADDR, &sa));
memset(&sa, 0, sizeof(sa));
NUTS_PASS(nng_stream_dialer_get_addr(d, NNG_OPT_LOCADDR, &sa));
NUTS_TRUE(sa.s_in6.sa_family == NNG_AF_INET6);
NUTS_TRUE(memcmp(sa.s_in6.sa_addr, loopback, 16) == 0);
NUTS_TRUE(sa.s_in6.sa_port == 0);
nng_stream_dialer_free(d);
#else
NUTS_SKIP("No IPv6 support");
#endif
}

void
test_tcp_dialer_local_bind_no_port(void)
{
nng_stream_dialer *d;
nng_sockaddr sa = { 0 };

NUTS_PASS(nng_stream_dialer_alloc(&d, "tcp://127.0.0.1:80"));

memset(&sa, 0, sizeof(sa));
sa.s_family = NNG_AF_INPROC;
NUTS_PASS(nng_stream_dialer_get_addr(d, NNG_OPT_LOCADDR, &sa));

NUTS_TRUE(sa.s_in.sa_family == NNG_AF_UNSPEC);
nng_stream_dialer_free(d);
}

void
test_tcp_dialer_local_bind_bad_family(void)
{
nng_stream_dialer *d;
nng_sockaddr sa = { 0 };

NUTS_PASS(nng_stream_dialer_alloc(&d, "tcp://127.0.0.1:80"));

memset(&sa, 0, sizeof(sa));
sa.s_family = NNG_AF_INPROC;
NUTS_FAIL(nng_stream_dialer_set_addr(d, NNG_OPT_LOCADDR, &sa),
NNG_EADDRINVAL);

memset(&sa, 0, sizeof(sa));
sa.s_ipc.sa_family = NNG_AF_IPC;
snprintf(sa.s_ipc.sa_path, sizeof(sa.s_ipc.sa_path), "%s", "/bogus");
NUTS_FAIL(nng_stream_dialer_set_addr(d, NNG_OPT_LOCADDR, &sa),
NNG_EADDRINVAL);

nng_stream_dialer_free(d);
}

void
test_tcp_dialer_local_unbound(void)
{
nng_stream_dialer *d;
nng_sockaddr sa = { 0 };

NUTS_PASS(nng_stream_dialer_alloc(&d, "tcp://127.0.0.1:80"));

sa.s_in.sa_family = NNG_AF_INET;
sa.s_in.sa_addr = nuts_be32(0x7F000001); // loopback
sa.s_in.sa_port = nuts_be16(1212);
NUTS_FAIL(nng_stream_dialer_set_addr(d, NNG_OPT_LOCADDR, &sa),
NNG_EADDRINVAL);

#ifdef NNG_ENABLE_IPV6
memset(&sa, 0, sizeof(sa));
sa.s_in6.sa_family = NNG_AF_INET6;
sa.s_in6.sa_port = nuts_be16(1212);
NUTS_FAIL(nng_stream_dialer_set_addr(d, NNG_OPT_LOCADDR, &sa),
NNG_EADDRINVAL);
#endif

nng_stream_dialer_free(d);
}

void
test_tcp_dialer_local_bind_bad_type(void)
{
nng_stream_dialer *d;
nng_sockaddr sa = { 0 };

NUTS_PASS(nng_stream_dialer_alloc(&d, "tcp://127.0.0.1:80"));

memset(&sa, 0, sizeof(sa));
sa.s_family = NNG_AF_INPROC;
NUTS_FAIL(nng_stream_dialer_set_bool(d, NNG_OPT_LOCADDR, false),
NNG_EBADTYPE);
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 +568,13 @@ NUTS_TESTS = {
test_tcp_listen_activation_bogus_fd },
{ "tcp socket activation bad arg",
test_tcp_listen_activation_bad_arg },
{ "tcp dialer local bind", test_tcp_dialer_local_bind },
{ "tcp dialer local bind v6", test_tcp_dialer_local_bind_v6 },
{ "tcp dialer local bind no port",
test_tcp_dialer_local_bind_no_port },
{ "tcp dialer local unbound", test_tcp_dialer_local_unbound },
{ "tcp dialer local bad family",
test_tcp_dialer_local_bind_bad_family },
{ "tcp dialer local bad type", test_tcp_dialer_local_bind_bad_type },
{ NULL, NULL },
};
Loading