From 5e031e639df65d7f12a0d2f776f188fde1b98fd9 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Mon, 20 Jan 2020 16:03:45 -0800 Subject: fixes #1163 compat tests are very brittle This only addresses the newly rewitten compat_tcp test, but it sets the groundwork for the other tests, so that when they are updated to the new acutest.h they can use the new marry code to establish connections cleanly and safely. --- src/compat/nanomsg/CMakeLists.txt | 10 +- src/compat/nanomsg/compat_tcp.c | 319 ----------------------------------- src/compat/nanomsg/compat_tcp_test.c | 309 +++++++++++++++++++++++++++++++++ src/compat/nanomsg/compat_testutil.h | 32 +++- src/protocol/reqrep0/xreq_test.c | 8 +- src/protocol/survey0/xsurvey_test.c | 16 +- 6 files changed, 357 insertions(+), 337 deletions(-) delete mode 100644 src/compat/nanomsg/compat_tcp.c create mode 100644 src/compat/nanomsg/compat_tcp_test.c (limited to 'src') diff --git a/src/compat/nanomsg/CMakeLists.txt b/src/compat/nanomsg/CMakeLists.txt index 2292d1a8..b2d99c3c 100644 --- a/src/compat/nanomsg/CMakeLists.txt +++ b/src/compat/nanomsg/CMakeLists.txt @@ -1,6 +1,5 @@ # -# Copyright 2018 Capitar IT Group BV -# Copyright 2018 Staysail Systems, Inc. +# Copyright 2020 Staysail Systems, Inc. # # This software is supplied under the terms of the MIT License, a # copy of which should be located in the distribution where this @@ -8,8 +7,9 @@ # found online at https://opensource.org/licenses/MIT. # -set(COMPAT_SOURCES compat/nanomsg/nn.c) +#set(COMPAT_SOURCES compat/nanomsg/nn.c) +nng_sources(nn.c) -set(NNG_SRCS ${NNG_SRCS} ${COMPAT_SOURCES} PARENT_SCOPE) +#set(NNG_SRCS ${NNG_SRCS} ${COMPAT_SOURCES} PARENT_SCOPE) -nng_test(compat_tcp) \ No newline at end of file +nng_test(compat_tcp_test) \ No newline at end of file diff --git a/src/compat/nanomsg/compat_tcp.c b/src/compat/nanomsg/compat_tcp.c deleted file mode 100644 index 6e0c2185..00000000 --- a/src/compat/nanomsg/compat_tcp.c +++ /dev/null @@ -1,319 +0,0 @@ -/* - Copyright 2019 Garrett D'Amore - Copyright (c) 2012 Martin Sustrik All rights reserved. - Copyright 2016 Franklin "Snaipe" Mathieu - - Permission is hereby granted, free of charge, to any person obtaining a - copy of this software and associated documentation files (the "Software"), - to deal in the Software without restriction, including without limitation - the rights to use, copy, modify, merge, publish, distribute, sublicense, - and/or sell copies of the Software, and to permit persons to whom - the Software is furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included - in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - DEALINGS IN THE SOFTWARE. -*/ - -// This test is a pretty much completely rewritten version of the -// legacy nanomsg tcp test. The NNG test infrastructure is a bit more -// robust, and we take advantage of that. - -#include -#include -#include -#include - -#include "compat_testutil.h" - -#include -#include - -void -test_bind_and_close(void) -{ - int sb; - char addr[32]; - uint16_t port = testutil_next_port(); - (void) snprintf(addr, sizeof(addr), "tcp://127.0.0.1:%u", port); - - TEST_CHECK((sb = nn_socket(AF_SP, NN_PAIR)) >= 0); - TEST_CHECK(nn_bind(sb, addr) >= 0); - TEST_CHECK(nn_close(sb) == 0); -} - -void -test_connect_and_close(void) -{ - int sc; - char addr[32]; - uint16_t port = testutil_next_port(); - (void) snprintf(addr, sizeof(addr), "tcp://127.0.0.1:%u", port); - - TEST_CHECK((sc = nn_socket(AF_SP, NN_PAIR)) >= 0); - TEST_CHECK(nn_connect(sc, addr) >= 0); - TEST_CHECK(nn_close(sc) == 0); -} - -void -test_bind_and_connect(void) -{ - int sb, sc; - char addr[32]; - uint16_t port = testutil_next_port(); - (void) snprintf(addr, sizeof(addr), "tcp://127.0.0.1:%u", port); - - TEST_CHECK((sb = nn_socket(AF_SP, NN_PAIR)) >= 0); - TEST_CHECK((sc = nn_socket(AF_SP, NN_PAIR)) >= 0); - TEST_CHECK(sb != sc); - TEST_CHECK(nn_bind(sb, addr) >= 0); - TEST_CHECK(nn_connect(sc, addr) >= 0); - - testutil_sleep(200); - - TEST_CHECK(nn_close(sb) == 0); - TEST_CHECK(nn_close(sc) == 0); -} - -void -test_bad_addresses(void) -{ - int s; - TEST_CHECK((s = nn_socket(AF_SP, NN_PAIR)) >= 0); - - TEST_NN_FAIL(nn_connect(s, "tcp://*:"), EINVAL); - TEST_NN_FAIL(nn_connect(s, "tcp://*:1000000"), EINVAL); - TEST_NN_FAIL(nn_connect(s, "tcp://*:some_port"), EINVAL); - TEST_NN_FAIL(nn_connect(s, "tcp://127.0.0.1"), EINVAL); - TEST_NN_FAIL(nn_connect(s, "tcp://:5555"), EINVAL); - TEST_NN_FAIL(nn_connect(s, "tcp://abc.123.---.#:5555"), EINVAL); - - TEST_NN_FAIL(nn_bind(s, "tcp://127.0.0.1:1000000"), EINVAL); - TEST_NN_PASS(nn_close(s)); -} - -void -test_no_delay(void) -{ - int s; - int opt; - size_t sz; - TEST_CHECK((s = nn_socket(AF_SP, NN_PAIR)) >= 0); - - sz = sizeof(opt); - TEST_NN_PASS(nn_getsockopt(s, NN_TCP, NN_TCP_NODELAY, &opt, &sz)); - TEST_CHECK(sz == sizeof(opt)); - TEST_CHECK(opt == 0); - opt = 2; - TEST_NN_FAIL( - nn_setsockopt(s, NN_TCP, NN_TCP_NODELAY, &opt, sz), EINVAL); - - opt = 1; - TEST_NN_PASS(nn_setsockopt(s, NN_TCP, NN_TCP_NODELAY, &opt, sz)); - - opt = 3; - TEST_NN_PASS(nn_getsockopt(s, NN_TCP, NN_TCP_NODELAY, &opt, &sz)); - TEST_CHECK(sz == sizeof(opt)); - TEST_CHECK(opt == 1); - TEST_NN_PASS(nn_close(s)); -} - -void -test_ping_pong(void) -{ - int sb, sc; - char addr[32]; - uint16_t port = testutil_next_port(); - (void) snprintf(addr, sizeof(addr), "tcp://127.0.0.1:%u", port); - - TEST_CHECK((sb = nn_socket(AF_SP, NN_PAIR)) >= 0); - TEST_CHECK((sc = nn_socket(AF_SP, NN_PAIR)) >= 0); - TEST_CHECK(sb != sc); - TEST_CHECK(nn_bind(sb, addr) >= 0); - TEST_CHECK(nn_connect(sc, addr) >= 0); - - testutil_sleep(200); - - /* Ping-pong test. */ - for (int i = 0; i != 100; ++i) { - - char buf[4]; - int n; - TEST_NN_PASS(nn_send(sc, "ABC", 3, 0)); - TEST_NN_PASS(n = nn_recv(sb, buf, 4, 0)); - TEST_CHECK(n == 3); - TEST_CHECK(memcmp(buf, "ABC", 3) == 0); - - TEST_NN_PASS(nn_send(sb, "DEF", 3, 0)); - TEST_NN_PASS(n = nn_recv(sc, buf, 4, 0)); - TEST_CHECK(n == 3); - TEST_CHECK(memcmp(buf, "DEF", 3) == 0); - } - - TEST_CHECK(nn_close(sb) == 0); - TEST_CHECK(nn_close(sc) == 0); -} - -// test_batch tests sending a batch of messages. It relies on having -// a reasonably deep buffer in the socket. -void -test_batch(void) -{ - int sb, sc; - char addr[32]; - uint16_t port = testutil_next_port(); - int opt; - size_t sz; - (void) snprintf(addr, sizeof(addr), "tcp://127.0.0.1:%u", port); - - TEST_CHECK((sb = nn_socket(AF_SP, NN_PAIR)) >= 0); - TEST_CHECK((sc = nn_socket(AF_SP, NN_PAIR)) >= 0); - TEST_CHECK(sb != sc); - TEST_CHECK(nn_bind(sb, addr) >= 0); - TEST_CHECK(nn_connect(sc, addr) >= 0); - opt = 1000; - sz = sizeof(opt); - TEST_NN_PASS(nn_setsockopt(sb, NN_SOL_SOCKET, NN_RCVTIMEO, &opt, sz)); - TEST_NN_PASS(nn_setsockopt(sb, NN_SOL_SOCKET, NN_SNDTIMEO, &opt, sz)); - TEST_NN_PASS(nn_setsockopt(sc, NN_SOL_SOCKET, NN_RCVTIMEO, &opt, sz)); - TEST_NN_PASS(nn_setsockopt(sc, NN_SOL_SOCKET, NN_SNDTIMEO, &opt, sz)); - - testutil_sleep(200); - - // We can send 10 of these, because TCP buffers a reasonable amount. - // Pushing say 100 of them may run into TCP buffering limitations. -#define DIGITS "0123456789012345678901234567890123456789" - for (int i = 0; i < 10; i++) { - TEST_NN_PASS(nn_send(sc, DIGITS, strlen(DIGITS) + 1, 0)); - } - - for (int i = 0; i < 10; i++) { - char buf[64]; - int n; - TEST_NN_PASS(n = nn_recv(sb, buf, sizeof(buf), 0)); - TEST_CHECK(n == (strlen(DIGITS) + 1)); - TEST_CHECK(memcmp(DIGITS, buf, n) == 0); - } - - TEST_CHECK(nn_close(sb) == 0); - TEST_CHECK(nn_close(sc) == 0); -} - -void -test_pair_reject(void) -{ - int sb, sc, sd; - char addr[32]; - uint16_t port = testutil_next_port(); - (void) snprintf(addr, sizeof(addr), "tcp://127.0.0.1:%u", port); - - TEST_CHECK((sb = nn_socket(AF_SP, NN_PAIR)) >= 0); - TEST_CHECK((sc = nn_socket(AF_SP, NN_PAIR)) >= 0); - TEST_CHECK((sd = nn_socket(AF_SP, NN_PAIR)) >= 0); - TEST_CHECK(sb != sc); - TEST_CHECK(nn_bind(sb, addr) >= 0); - TEST_CHECK(nn_connect(sc, addr) >= 0); - testutil_sleep(100); - TEST_CHECK(nn_connect(sd, addr) >= 0); - - testutil_sleep(200); - - TEST_CHECK(nn_close(sb) == 0); - TEST_CHECK(nn_close(sc) == 0); - TEST_CHECK(nn_close(sd) == 0); -} - -void -test_addr_in_use(void) -{ - int sb, sc; - char addr[32]; - uint16_t port = testutil_next_port(); - (void) snprintf(addr, sizeof(addr), "tcp://127.0.0.1:%u", port); - - TEST_CHECK((sb = nn_socket(AF_SP, NN_PAIR)) >= 0); - TEST_CHECK((sc = nn_socket(AF_SP, NN_PAIR)) >= 0); - TEST_CHECK(sb != sc); - TEST_NN_PASS(nn_bind(sb, addr)); - TEST_NN_FAIL(nn_bind(sc, addr), EADDRINUSE); - - TEST_CHECK(nn_close(sb) == 0); - TEST_CHECK(nn_close(sc) == 0); -} - -void -test_max_recv_size(void) -{ - int sb, sc; - int opt; - int n; - size_t sz; - char addr[32]; - char buf[64]; - uint16_t port = testutil_next_port(); - (void) snprintf(addr, sizeof(addr), "tcp://127.0.0.1:%u", port); - - TEST_CHECK((sb = nn_socket(AF_SP, NN_PAIR)) >= 0); - TEST_CHECK((sc = nn_socket(AF_SP, NN_PAIR)) >= 0); - TEST_CHECK(sb != sc); - opt = 100; - sz = sizeof(opt); - TEST_NN_PASS(nn_setsockopt(sb, NN_SOL_SOCKET, NN_RCVTIMEO, &opt, sz)); - - /* Test that NN_RCVMAXSIZE can be -1, but not lower */ - sz = sizeof(opt); - opt = -1; - TEST_NN_PASS( - nn_setsockopt(sb, NN_SOL_SOCKET, NN_RCVMAXSIZE, &opt, sz)); - - opt = -2; - TEST_NN_FAIL( - nn_setsockopt(sb, NN_SOL_SOCKET, NN_RCVMAXSIZE, &opt, sz), EINVAL); - - opt = 4; - TEST_NN_PASS( - nn_setsockopt(sb, NN_SOL_SOCKET, NN_RCVMAXSIZE, &opt, sz)); - opt = -5; - TEST_NN_PASS( - nn_getsockopt(sb, NN_SOL_SOCKET, NN_RCVMAXSIZE, &opt, &sz)); - TEST_CHECK(opt == 4); - TEST_CHECK(sz == sizeof(opt)); - - TEST_CHECK(nn_bind(sb, addr) >= 0); - TEST_CHECK(nn_connect(sc, addr) >= 0); - - testutil_sleep(200); - - TEST_NN_PASS(nn_send(sc, "ABC", 4, 0)); - TEST_NN_PASS(nn_send(sc, "012345", 6, 0)); - - TEST_NN_PASS(n = nn_recv(sb, buf, sizeof(buf), 0)); - TEST_CHECK(n == 4); - TEST_CHECK(strcmp(buf, "ABC") == 0); - - TEST_NN_FAIL(nn_recv(sb, buf, sizeof(buf), 0), ETIMEDOUT); - - TEST_NN_PASS(nn_close(sb)); - TEST_NN_PASS(nn_close(sc)); -} - -TEST_LIST = { - { "compat tcp bind and close ", test_bind_and_close }, - { "compat tcp connect and close ", test_connect_and_close }, - { "compat tcp bind and connect ", test_bind_and_connect }, - { "compat tcp invalid addresses", test_bad_addresses }, - { "compat tcp no delay option", test_no_delay }, - { "compat tcp ping pong", test_ping_pong }, - { "compat tcp send recv batch", test_batch }, - { "compat tcp pair reject", test_pair_reject }, - { "compat tcp addr in use", test_addr_in_use }, - { "compat tcp max recv size", test_max_recv_size }, - { NULL, NULL }, -}; diff --git a/src/compat/nanomsg/compat_tcp_test.c b/src/compat/nanomsg/compat_tcp_test.c new file mode 100644 index 00000000..8fb7f7b6 --- /dev/null +++ b/src/compat/nanomsg/compat_tcp_test.c @@ -0,0 +1,309 @@ +/* + Copyright 2020 Staysail Systems, Inc. + Copyright (c) 2012 Martin Sustrik All rights reserved. + Copyright 2016 Franklin "Snaipe" Mathieu + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom + the Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. +*/ + +// This test is a pretty much completely rewritten version of the +// legacy nanomsg tcp test. The NNG test infrastructure is a bit more +// robust, and we take advantage of that. + +#include +#include +#include + +#include "compat_testutil.h" + +#include +#include + +void +test_bind_and_close(void) +{ + int sb; + char addr[64]; + testutil_scratch_addr("tcp", sizeof (addr), addr); + + TEST_CHECK((sb = nn_socket(AF_SP, NN_PAIR)) >= 0); + TEST_CHECK(nn_bind(sb, addr) >= 0); + TEST_CHECK(nn_close(sb) == 0); +} + +void +test_connect_and_close(void) +{ + int sc; + char addr[64]; + testutil_scratch_addr("tcp", sizeof (addr), addr); + + TEST_NN_PASS((sc = nn_socket(AF_SP, NN_PAIR)) >= 0); + TEST_NN_PASS(nn_connect(sc, addr)); + TEST_NN_PASS(nn_close(sc)); +} + +void +test_bind_and_connect(void) +{ + int sb, sc, p1, p2; + char addr[64]; + + testutil_scratch_addr("tcp", sizeof (addr), addr); + + TEST_CHECK((sb = nn_socket(AF_SP, NN_PAIR)) >= 0); + TEST_CHECK((sc = nn_socket(AF_SP, NN_PAIR)) >= 0); + TEST_CHECK(sb != sc); + + TEST_NN_MARRY_EX(sb, sc, addr, p1, p2); + + TEST_CHECK(nn_close(sb) == 0); + TEST_CHECK(nn_close(sc) == 0); +} + +void +test_bad_addresses(void) +{ + int s; + TEST_CHECK((s = nn_socket(AF_SP, NN_PAIR)) >= 0); + + TEST_NN_FAIL(nn_connect(s, "tcp://*:"), EINVAL); + TEST_NN_FAIL(nn_connect(s, "tcp://*:1000000"), EINVAL); + TEST_NN_FAIL(nn_connect(s, "tcp://*:some_port"), EINVAL); + TEST_NN_FAIL(nn_connect(s, "tcp://127.0.0.1"), EINVAL); + TEST_NN_FAIL(nn_connect(s, "tcp://:5555"), EINVAL); + TEST_NN_FAIL(nn_connect(s, "tcp://abc.123.---.#:5555"), EINVAL); + + TEST_NN_FAIL(nn_bind(s, "tcp://127.0.0.1:1000000"), EINVAL); + TEST_NN_PASS(nn_close(s)); +} + +void +test_no_delay(void) +{ + int s; + int opt; + size_t sz; + TEST_CHECK((s = nn_socket(AF_SP, NN_PAIR)) >= 0); + + sz = sizeof(opt); + TEST_NN_PASS(nn_getsockopt(s, NN_TCP, NN_TCP_NODELAY, &opt, &sz)); + TEST_CHECK(sz == sizeof(opt)); + TEST_CHECK(opt == 0); + opt = 2; + TEST_NN_FAIL( + nn_setsockopt(s, NN_TCP, NN_TCP_NODELAY, &opt, sz), EINVAL); + + opt = 1; + TEST_NN_PASS(nn_setsockopt(s, NN_TCP, NN_TCP_NODELAY, &opt, sz)); + + opt = 3; + TEST_NN_PASS(nn_getsockopt(s, NN_TCP, NN_TCP_NODELAY, &opt, &sz)); + TEST_CHECK(sz == sizeof(opt)); + TEST_CHECK(opt == 1); + TEST_NN_PASS(nn_close(s)); +} + +void +test_ping_pong(void) +{ + int sb, sc, p1, p2; + char addr[64]; + + testutil_scratch_addr("tcp", sizeof(addr), addr); + + TEST_NN_PASS((sb = nn_socket(AF_SP, NN_PAIR))); + TEST_NN_PASS((sc = nn_socket(AF_SP, NN_PAIR))); + TEST_CHECK(sb != sc); + TEST_NN_MARRY_EX(sc, sb, addr, p1, p2); + TEST_CHECK(p1 >= 0); + TEST_CHECK(p2 >= 0); + + /* Ping-pong test. */ + for (int i = 0; i != 100; ++i) { + + char buf[4]; + int n; + TEST_NN_PASS(nn_send(sc, "ABC", 3, 0)); + TEST_NN_PASS(n = nn_recv(sb, buf, 4, 0)); + TEST_CHECK(n == 3); + TEST_CHECK(memcmp(buf, "ABC", 3) == 0); + + TEST_NN_PASS(nn_send(sb, "DEF", 3, 0)); + TEST_NN_PASS(n = nn_recv(sc, buf, 4, 0)); + TEST_CHECK(n == 3); + TEST_CHECK(memcmp(buf, "DEF", 3) == 0); + } + + TEST_CHECK(nn_close(sb) == 0); + TEST_CHECK(nn_close(sc) == 0); +} + +// test_batch tests sending a batch of messages. It relies on having +// a reasonably deep buffer in the socket. +void +test_batch(void) +{ + int sb, sc, p1, p2; + char addr[64]; + int opt; + size_t sz; + + testutil_scratch_addr("tcp", sizeof(addr), addr); + + TEST_NN_PASS((sb = nn_socket(AF_SP, NN_PAIR))); + TEST_NN_PASS((sc = nn_socket(AF_SP, NN_PAIR))); + TEST_CHECK(sb != sc); + opt = 1000; + sz = sizeof(opt); + TEST_NN_PASS(nn_setsockopt(sb, NN_SOL_SOCKET, NN_RCVTIMEO, &opt, sz)); + TEST_NN_PASS(nn_setsockopt(sb, NN_SOL_SOCKET, NN_SNDTIMEO, &opt, sz)); + TEST_NN_PASS(nn_setsockopt(sc, NN_SOL_SOCKET, NN_RCVTIMEO, &opt, sz)); + TEST_NN_PASS(nn_setsockopt(sc, NN_SOL_SOCKET, NN_SNDTIMEO, &opt, sz)); + + TEST_NN_MARRY_EX(sc, sb, addr, p1, p2); + TEST_CHECK(p1 >= 0); + TEST_CHECK(p2 >= 0); + + // We can send 10 of these, because TCP buffers a reasonable amount. + // Pushing say 100 of them may run into TCP buffering limitations. +#define DIGITS "0123456789012345678901234567890123456789" + for (int i = 0; i < 10; i++) { + TEST_NN_PASS(nn_send(sc, DIGITS, strlen(DIGITS) + 1, 0)); + } + + for (int i = 0; i < 10; i++) { + char buf[64]; + int n; + TEST_NN_PASS(n = nn_recv(sb, buf, sizeof(buf), 0)); + TEST_CHECK(n == (strlen(DIGITS) + 1)); + TEST_CHECK(memcmp(DIGITS, buf, n) == 0); + } + + TEST_CHECK(nn_close(sb) == 0); + TEST_CHECK(nn_close(sc) == 0); +} + +void +test_pair_reject(void) +{ + int sb, sc, sd, p1, p2; + char addr[32]; + + testutil_scratch_addr("tcp", sizeof(addr), addr); + + TEST_CHECK((sb = nn_socket(AF_SP, NN_PAIR)) >= 0); + TEST_CHECK((sc = nn_socket(AF_SP, NN_PAIR)) >= 0); + TEST_CHECK((sd = nn_socket(AF_SP, NN_PAIR)) >= 0); + TEST_CHECK(sb != sc); + + TEST_NN_MARRY_EX(sc, sb, addr, p1, p2); + + TEST_CHECK(nn_connect(sd, addr) >= 0); + testutil_sleep(200); + + TEST_CHECK(nn_close(sb) == 0); + TEST_CHECK(nn_close(sc) == 0); + TEST_CHECK(nn_close(sd) == 0); +} + +void +test_addr_in_use(void) +{ + int sb, sc; + char addr[64]; + + testutil_scratch_addr("tcp", sizeof(addr), addr); + + TEST_CHECK((sb = nn_socket(AF_SP, NN_PAIR)) >= 0); + TEST_CHECK((sc = nn_socket(AF_SP, NN_PAIR)) >= 0); + TEST_CHECK(sb != sc); + TEST_NN_PASS(nn_bind(sb, addr)); + TEST_NN_FAIL(nn_bind(sc, addr), EADDRINUSE); + + TEST_CHECK(nn_close(sb) == 0); + TEST_CHECK(nn_close(sc) == 0); +} + +void +test_max_recv_size(void) +{ + int sb, sc, p1, p2; + int opt; + int n; + size_t sz; + char addr[64]; + char buf[64]; + + testutil_scratch_addr("tcp", sizeof(addr), addr); + + TEST_CHECK((sb = nn_socket(AF_SP, NN_PAIR)) >= 0); + TEST_CHECK((sc = nn_socket(AF_SP, NN_PAIR)) >= 0); + TEST_CHECK(sb != sc); + opt = 100; + sz = sizeof(opt); + TEST_NN_PASS(nn_setsockopt(sb, NN_SOL_SOCKET, NN_RCVTIMEO, &opt, sz)); + + /* Test that NN_RCVMAXSIZE can be -1, but not lower */ + sz = sizeof(opt); + opt = -1; + TEST_NN_PASS( + nn_setsockopt(sb, NN_SOL_SOCKET, NN_RCVMAXSIZE, &opt, sz)); + + opt = -2; + TEST_NN_FAIL( + nn_setsockopt(sb, NN_SOL_SOCKET, NN_RCVMAXSIZE, &opt, sz), EINVAL); + + opt = 4; + TEST_NN_PASS( + nn_setsockopt(sb, NN_SOL_SOCKET, NN_RCVMAXSIZE, &opt, sz)); + opt = -5; + TEST_NN_PASS( + nn_getsockopt(sb, NN_SOL_SOCKET, NN_RCVMAXSIZE, &opt, &sz)); + TEST_CHECK(opt == 4); + TEST_CHECK(sz == sizeof(opt)); + + TEST_NN_MARRY_EX(sc, sb, addr, p1, p2); + + TEST_NN_PASS(nn_send(sc, "ABC", 4, 0)); + TEST_NN_PASS(nn_send(sc, "012345", 6, 0)); + + TEST_NN_PASS(n = nn_recv(sb, buf, sizeof(buf), 0)); + TEST_CHECK(n == 4); + TEST_CHECK(strcmp(buf, "ABC") == 0); + + TEST_NN_FAIL(nn_recv(sb, buf, sizeof(buf), 0), ETIMEDOUT); + + TEST_NN_PASS(nn_close(sb)); + TEST_NN_PASS(nn_close(sc)); +} + +TEST_LIST = { + { "compat tcp bind and close ", test_bind_and_close }, + { "compat tcp connect and close ", test_connect_and_close }, + { "compat tcp bind and connect ", test_bind_and_connect }, + { "compat tcp invalid addresses", test_bad_addresses }, + { "compat tcp no delay option", test_no_delay }, + { "compat tcp ping pong", test_ping_pong }, + { "compat tcp send recv batch", test_batch }, + { "compat tcp pair reject", test_pair_reject }, + { "compat tcp addr in use", test_addr_in_use }, + { "compat tcp max recv size", test_max_recv_size }, + { NULL, NULL }, +}; diff --git a/src/compat/nanomsg/compat_testutil.h b/src/compat/nanomsg/compat_testutil.h index afd75c34..113d6672 100644 --- a/src/compat/nanomsg/compat_testutil.h +++ b/src/compat/nanomsg/compat_testutil.h @@ -1,5 +1,5 @@ // -// Copyright 2019 Staysail Systems, Inc. +// Copyright 2020 Staysail Systems, Inc. // // This software is supplied under the terms of the MIT License, a // copy of which should be located in the distribution where this @@ -41,6 +41,36 @@ extern "C" { expect, nng_strerror(err_), result_); \ } while (0) +// These macros use some details of the socket and pipe which are not public. +// We do that to facilitate testing. Don't rely on this equivalence in your +// own application code. + +#define TEST_NN_MARRY(s1, s2) \ + do { \ + int rv_; \ + nng_socket s1_, s2_; \ + s1_.id = s1; \ + s2_.id = s2; \ + \ + TEST_CHECK_(testutil_marry(s1_, s2_) == 0, "marry %s", \ + nng_strerror(rv_)); \ + } while (0) + +#define TEST_NN_MARRY_EX(s1, s2, url, p1, p2) \ + do { \ + int rv_; \ + nng_socket s1_, s2_; \ + nng_pipe p1_, p2_; \ + s1_.id = s1; \ + s2_.id = s2; \ + rv_ = testutil_marry_ex(s1_, s2_, url, &p1_, &p2_); \ + TEST_CHECK_(rv_ == 0, "marry %s", nng_strerror(rv_)); \ + p1 = p1_.id; \ + p2 = p2_.id; \ + TEST_CHECK(p1 >= 0); \ + TEST_CHECK(p2 >= 0); \ + } while (0) + #ifdef __cplusplus }; #endif diff --git a/src/protocol/reqrep0/xreq_test.c b/src/protocol/reqrep0/xreq_test.c index e474a0ac..57ad7f0a 100644 --- a/src/protocol/reqrep0/xreq_test.c +++ b/src/protocol/reqrep0/xreq_test.c @@ -234,7 +234,7 @@ test_xreq_recv_header(void) TEST_NNG_PASS(nng_setopt_ms(rep, NNG_OPT_SENDTIMEO, 1000)); TEST_NNG_PASS(nng_setopt_ms(rep, NNG_OPT_SENDTIMEO, 1000)); - TEST_NNG_PASS(testutil_marry_ex(req, rep, &p1, &p2)); + TEST_NNG_PASS(testutil_marry_ex(req, rep, NULL, &p1, &p2)); // Simulate a few hops. TEST_NNG_PASS(nng_msg_alloc(&m, 0)); @@ -276,7 +276,7 @@ test_xreq_close_during_recv(void) TEST_NNG_PASS(nng_setopt_int(req, NNG_OPT_RECVBUF, 5)); TEST_NNG_PASS(nng_setopt_int(rep, NNG_OPT_SENDBUF, 20)); - TEST_NNG_PASS(testutil_marry_ex(req, rep, &p1, &p2)); + TEST_NNG_PASS(testutil_marry_ex(req, rep, NULL, &p1, &p2)); TEST_CHECK(nng_pipe_id(p1) > 0); TEST_CHECK(nng_pipe_id(p2) > 0); @@ -307,7 +307,7 @@ test_xreq_close_pipe_during_send(void) TEST_NNG_PASS(nng_setopt_int(rep, NNG_OPT_RECVBUF, 5)); TEST_NNG_PASS(nng_setopt_int(req, NNG_OPT_SENDBUF, 20)); - TEST_NNG_PASS(testutil_marry_ex(req, rep, &p1, &p2)); + TEST_NNG_PASS(testutil_marry_ex(req, rep, NULL, &p1, &p2)); TEST_CHECK(nng_pipe_id(p1) > 0); TEST_CHECK(nng_pipe_id(p2) > 0); @@ -329,7 +329,7 @@ test_xreq_ttl_option(void) nng_socket rep; int v; bool b; - size_t sz = sizeof(v); + size_t sz; const char *opt = NNG_OPT_MAXTTL; TEST_NNG_PASS(nng_req0_open_raw(&rep)); diff --git a/src/protocol/survey0/xsurvey_test.c b/src/protocol/survey0/xsurvey_test.c index b0123145..ff096de4 100644 --- a/src/protocol/survey0/xsurvey_test.c +++ b/src/protocol/survey0/xsurvey_test.c @@ -227,7 +227,7 @@ test_xsurvey_recv_header(void) nng_socket resp; nng_socket surv; nng_msg * m; - nng_pipe p1, p2; + nng_pipe p; uint32_t id; TEST_NNG_PASS(nng_respondent0_open_raw(&resp)); @@ -237,11 +237,11 @@ test_xsurvey_recv_header(void) TEST_NNG_PASS(nng_setopt_ms(resp, NNG_OPT_SENDTIMEO, 1000)); TEST_NNG_PASS(nng_setopt_ms(resp, NNG_OPT_SENDTIMEO, 1000)); - TEST_NNG_PASS(testutil_marry_ex(surv, resp, &p1, &p2)); + TEST_NNG_PASS(testutil_marry_ex(surv, resp, NULL, NULL, &p)); // Simulate a few hops. TEST_NNG_PASS(nng_msg_alloc(&m, 0)); - TEST_NNG_PASS(nng_msg_header_append_u32(m, nng_pipe_id(p2))); + TEST_NNG_PASS(nng_msg_header_append_u32(m, nng_pipe_id(p))); TEST_NNG_PASS(nng_msg_header_append_u32(m, 0x2)); TEST_NNG_PASS(nng_msg_header_append_u32(m, 0x1)); TEST_NNG_PASS(nng_msg_header_append_u32(m, 0x80000123u)); @@ -279,7 +279,7 @@ test_xsurvey_close_during_recv(void) TEST_NNG_PASS(nng_setopt_int(surv, NNG_OPT_RECVBUF, 1)); TEST_NNG_PASS(nng_setopt_int(resp, NNG_OPT_SENDBUF, 20)); - TEST_NNG_PASS(testutil_marry_ex(surv, resp, &p1, &p2)); + TEST_NNG_PASS(testutil_marry_ex(surv, resp, NULL, &p1, &p2)); TEST_CHECK(nng_pipe_id(p1) > 0); TEST_CHECK(nng_pipe_id(p2) > 0); @@ -310,7 +310,7 @@ test_xsurvey_close_pipe_during_send(void) TEST_NNG_PASS(nng_setopt_int(resp, NNG_OPT_RECVBUF, 5)); TEST_NNG_PASS(nng_setopt_int(surv, NNG_OPT_SENDBUF, 20)); - TEST_NNG_PASS(testutil_marry_ex(surv, resp, &p1, &p2)); + TEST_NNG_PASS(testutil_marry_ex(surv, resp, NULL, &p1, &p2)); TEST_CHECK(nng_pipe_id(p1) > 0); TEST_CHECK(nng_pipe_id(p2) > 0); @@ -332,7 +332,7 @@ test_xsurvey_ttl_option(void) nng_socket s; int v; bool b; - size_t sz = sizeof(v); + size_t sz; const char *opt = NNG_OPT_MAXTTL; TEST_NNG_PASS(nng_surveyor0_open_raw(&s)); @@ -403,7 +403,7 @@ TEST_LIST = { { "xsurvey close during recv", test_xsurvey_close_during_recv }, { "xsurvey close pipe during send", test_xsurvey_close_pipe_during_send }, - { "xsurvey ttl option", test_xsurvey_ttl_option }, - { "xsurvey broadcast", test_xsurvey_broadcast }, + { "xsurvey ttl option", test_xsurvey_ttl_option }, + { "xsurvey broadcast", test_xsurvey_broadcast }, { NULL, NULL }, }; -- cgit v1.2.3-70-g09d2