-
-
Notifications
You must be signed in to change notification settings - Fork 497
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: add some tcp dialer local address tests
- Loading branch information
Showing
1 changed file
with
45 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -8,6 +8,7 @@ | |
// found online at https://opensource.org/licenses/MIT. | ||
// | ||
|
||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#include <nng/nng.h> | ||
|
@@ -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 }, | ||
}; |