From 147406fba6a892af36aa2773593412e6289cfe65 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sat, 23 Nov 2024 18:18:03 -0800 Subject: [PATCH 1/3] ipc: Add NNG_OPT_REMADDR for dialer. --- src/platform/posix/posix_ipcdial.c | 12 +++ src/platform/windows/win_ipcdial.c | 12 +++ src/sp/transport/ipc/ipc_test.c | 129 +++++++++++++++++++++++++++++ 3 files changed, 153 insertions(+) diff --git a/src/platform/posix/posix_ipcdial.c b/src/platform/posix/posix_ipcdial.c index 81a8f1dc7..1f7728b7b 100644 --- a/src/platform/posix/posix_ipcdial.c +++ b/src/platform/posix/posix_ipcdial.c @@ -240,7 +240,19 @@ ipc_dialer_dial(void *arg, nni_aio *aio) nni_aio_finish_error(aio, rv); } +static int +ipc_dialer_get_remaddr(void *arg, void *buf, size_t *szp, nni_type t) +{ + ipc_dialer *d = arg; + + return (nni_copyout_sockaddr(&d->sa, buf, szp, t)); +} + static const nni_option ipc_dialer_options[] = { + { + .o_name = NNG_OPT_REMADDR, + .o_get = ipc_dialer_get_remaddr, + }, { .o_name = NULL, }, diff --git a/src/platform/windows/win_ipcdial.c b/src/platform/windows/win_ipcdial.c index d84b959db..a09d120c4 100644 --- a/src/platform/windows/win_ipcdial.c +++ b/src/platform/windows/win_ipcdial.c @@ -202,7 +202,19 @@ ipc_dialer_free(void *arg) NNI_FREE_STRUCT(d); } +static int +ipc_dialer_get_remaddr(void *arg, void *buf, size_t *szp, nni_type t) +{ + ipc_dialer *d = arg; + + return (nni_copyout_sockaddr(&d->sa, buf, szp, t)); +} + static const nni_option ipc_dialer_options[] = { + { + .o_name = NNG_OPT_REMADDR, + .o_get = ipc_dialer_get_remaddr, + }, { .o_name = NULL, }, diff --git a/src/sp/transport/ipc/ipc_test.c b/src/sp/transport/ipc/ipc_test.c index 083326bb2..6a4021a02 100644 --- a/src/sp/transport/ipc/ipc_test.c +++ b/src/sp/transport/ipc/ipc_test.c @@ -69,6 +69,8 @@ test_ipc_dialer_properties(void) NUTS_FAIL(nng_dialer_get_addr(d, NNG_OPT_LOCADDR, &sa), NNG_ENOTSUP); NUTS_FAIL(nng_dialer_set_addr(d, NNG_OPT_LOCADDR, &sa), NNG_ENOTSUP); + NUTS_PASS(nng_dialer_get_addr(d, NNG_OPT_REMADDR, &sa)); + NUTS_TRUE(sa.s_family == NNG_AF_IPC); z = 8192; NUTS_PASS(nng_dialer_set_size(d, NNG_OPT_RECVMAXSZ, z)); @@ -143,6 +145,95 @@ test_ipc_listener_properties(void) NUTS_CLOSE(s); } +void +test_ipc_ping_pong(void) +{ + nng_socket s0; + nng_socket s1; + char *addr; + + NUTS_ENABLE_LOG(NNG_LOG_INFO); + NUTS_ADDR(addr, "ipc"); + NUTS_OPEN(s0); + NUTS_OPEN(s1); + NUTS_PASS(nng_socket_set_ms(s0, NNG_OPT_RECVTIMEO, 100)); + NUTS_PASS(nng_socket_set_ms(s0, NNG_OPT_SENDTIMEO, 100)); + NUTS_PASS(nng_socket_set_ms(s1, NNG_OPT_RECVTIMEO, 100)); + NUTS_PASS(nng_socket_set_ms(s1, NNG_OPT_SENDTIMEO, 100)); + + NUTS_MARRY_EX(s0, s1, addr, NULL, NULL); + + NUTS_SEND(s0, "ping"); + NUTS_RECV(s1, "ping"); + NUTS_SEND(s1, "pong"); + NUTS_RECV(s0, "pong"); + NUTS_CLOSE(s0); + NUTS_CLOSE(s1); +} + +void +test_ipc_ping_pong_many(void) +{ + nng_socket s0; + nng_socket s1; + char *addr; + + NUTS_ADDR(addr, "ipc"); + NUTS_OPEN(s0); + NUTS_OPEN(s1); + NUTS_PASS(nng_socket_set_ms(s0, NNG_OPT_RECVTIMEO, 100)); + NUTS_PASS(nng_socket_set_ms(s0, NNG_OPT_SENDTIMEO, 100)); + NUTS_PASS(nng_socket_set_ms(s1, NNG_OPT_RECVTIMEO, 100)); + NUTS_PASS(nng_socket_set_ms(s1, NNG_OPT_SENDTIMEO, 100)); + + NUTS_MARRY_EX(s0, s1, addr, NULL, NULL); + + for (int i = 0; i < 100; i++) { + NUTS_SEND(s0, "ping"); + NUTS_RECV(s1, "ping"); + NUTS_SEND(s1, "pong"); + NUTS_RECV(s0, "pong"); + } + NUTS_CLOSE(s0); + NUTS_CLOSE(s1); +} + +void +test_ipc_huge_msg(void) +{ + nng_socket s0; + nng_socket s1; + char *addr; + nng_msg *m; + + NUTS_ADDR(addr, "ipc"); + NUTS_PASS(nng_msg_alloc(&m, 1 << 20)); + memset(nng_msg_body(m), 'a', 1 << 20); + NUTS_OPEN(s0); + NUTS_OPEN(s1); + NUTS_PASS(nng_socket_set_ms(s0, NNG_OPT_RECVTIMEO, 100)); + NUTS_PASS(nng_socket_set_ms(s0, NNG_OPT_SENDTIMEO, 100)); + NUTS_PASS(nng_socket_set_ms(s1, NNG_OPT_RECVTIMEO, 100)); + NUTS_PASS(nng_socket_set_ms(s1, NNG_OPT_SENDTIMEO, 100)); + + NUTS_MARRY_EX(s0, s1, addr, NULL, NULL); + + NUTS_PASS(nng_sendmsg(s0, m, 0)); + NUTS_PASS(nng_recvmsg(s1, &m, 0)); + + NUTS_TRUE(nng_msg_len(m) == 1 << 20); + char *body = nng_msg_body(m); + for (int i = 0; i < 1 << 20; i++) { + if (body[i] != 'a') { + NUTS_TRUE(body[i] == 'a'); + break; + } + } + nng_msg_free(m); + NUTS_CLOSE(s0); + NUTS_CLOSE(s1); +} + void test_ipc_recv_max(void) { @@ -177,6 +268,23 @@ test_ipc_recv_max(void) NUTS_CLOSE(s1); } +void +test_ipc_connect_refused(void) +{ + nng_socket s0; + nng_dialer d; + char *addr; + + NUTS_ENABLE_LOG(NNG_LOG_INFO); + NUTS_ADDR(addr, "ipc"); + NUTS_OPEN(s0); + NUTS_PASS(nng_socket_set_ms(s0, NNG_OPT_RECVTIMEO, 100)); + NUTS_PASS(nng_socket_set_size(s0, NNG_OPT_RECVMAXSZ, 200)); + NUTS_PASS(nng_dialer_create(&d, s0, addr)); + NUTS_FAIL(nng_dialer_start(d, 0), NNG_ECONNREFUSED); + NUTS_CLOSE(s0); +} + void test_ipc_connect_blocking(void) { @@ -250,6 +358,22 @@ test_ipc_listen_accept_cancel(void) nng_stream_listener_free(l); } +void +test_ipc_listen_duplicate(void) +{ + nng_socket s0; + char *addr; + + NUTS_ENABLE_LOG(NNG_LOG_INFO); + NUTS_ADDR(addr, "ipc"); + NUTS_OPEN(s0); + + // start a listening stream listener but do not call accept + NUTS_PASS(nng_listen(s0, addr, NULL, 0)); + NUTS_FAIL(nng_listen(s0, addr, NULL, 0), NNG_EADDRINUSE); + NUTS_CLOSE(s0); +} + void test_ipc_listener_clean_stale(void) { @@ -556,10 +680,15 @@ TEST_LIST = { { "ipc dialer props", test_ipc_dialer_properties }, { "ipc listener perms", test_ipc_listener_perms }, { "ipc listener props", test_ipc_listener_properties }, + { "ipc ping pong", test_ipc_ping_pong }, + { "ipc ping pong many", test_ipc_ping_pong_many }, + { "ipc huge msg", test_ipc_huge_msg }, { "ipc recv max", test_ipc_recv_max }, + { "ipc connect refused", test_ipc_connect_refused }, { "ipc connect blocking", test_ipc_connect_blocking }, { "ipc connect blocking accept", test_ipc_connect_blocking_accept }, { "ipc listen cleanup stale", test_ipc_listener_clean_stale }, + { "ipc listen duplicate", test_ipc_listen_duplicate }, { "ipc listen accept cancel", test_ipc_listen_accept_cancel }, { "ipc abstract sockets", test_abstract_sockets }, { "ipc abstract auto bind", test_abstract_auto_bind }, From 52408ba30c0d2babeab27eae9bf5e91b0d61c8cd Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sat, 23 Nov 2024 18:24:33 -0800 Subject: [PATCH 2/3] Remove the ipc legacy test - everything is covered in the new suite. --- tests/CMakeLists.txt | 1 - tests/ipc.c | 79 -------------------------------------------- 2 files changed, 80 deletions(-) delete mode 100644 tests/ipc.c diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2fcc331eb..a42dd23a4 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -129,7 +129,6 @@ add_nng_test(files 5) add_nng_test1(httpclient 60 NNG_SUPP_HTTP) add_nng_test1(httpserver 30 NNG_SUPP_HTTP) add_nng_test(inproc 5) -add_nng_test(ipc 5) add_nng_test(ipcsupp 10) add_nng_test(multistress 60) add_nng_test(nonblock 60) diff --git a/tests/ipc.c b/tests/ipc.c deleted file mode 100644 index 6ea0830cd..000000000 --- a/tests/ipc.c +++ /dev/null @@ -1,79 +0,0 @@ -// -// Copyright 2021 Staysail Systems, Inc. -// Copyright 2018 Capitar IT Group BV -// Copyright 2018 Devolutions -// -// This software is supplied under the terms of the MIT License, a -// copy of which should be located in the distribution where this -// file was obtained (LICENSE.txt). A copy of the license may also be -// found online at https://opensource.org/licenses/MIT. -// - -#ifdef _WIN32 -#else -#include -#ifdef NNG_HAVE_GETPEERUCRED -#include -#endif -#endif - -#include -#include - -#include "convey.h" -#include "trantest.h" - -// IPC tests. -static int -check_props(nng_msg *msg) -{ - nng_pipe p; - size_t z; - nng_sockaddr la; - nng_sockaddr ra; - uint64_t id; - - p = nng_msg_get_pipe(msg); - So(nng_pipe_id(p) > 0); - So(nng_pipe_get_addr(p, NNG_OPT_LOCADDR, &la) == 0); - So(la.s_family == NNG_AF_IPC); - // untyped - So(nng_pipe_get_addr(p, NNG_OPT_REMADDR, &ra) == 0); - So(ra.s_family == NNG_AF_IPC); - - So(nng_pipe_get_size(p, NNG_OPT_REMADDR, &z) == NNG_EBADTYPE); - -#ifdef _WIN32 - So(nng_pipe_get_uint64(p, NNG_OPT_IPC_PEER_UID, &id) == NNG_ENOTSUP); - So(nng_pipe_get_uint64(p, NNG_OPT_IPC_PEER_GID, &id) == NNG_ENOTSUP); - So(nng_pipe_get_uint64(p, NNG_OPT_IPC_PEER_ZONEID, &id) == - NNG_ENOTSUP); - So(nng_pipe_get_uint64(p, NNG_OPT_IPC_PEER_PID, &id) == 0); - So(id == GetCurrentProcessId()); -#else - So(nng_pipe_get_uint64(p, NNG_OPT_IPC_PEER_UID, &id) == 0); - So(id == (uint64_t) getuid()); - So(nng_pipe_get_uint64(p, NNG_OPT_IPC_PEER_GID, &id) == 0); - So(id == (uint64_t) getgid()); - -#if defined(NNG_HAVE_SOPEERCRED) || defined(NNG_HAVE_GETPEERUCRED) || \ - (defined(NNG_HAVE_LOCALPEERCRED) && defined(NNG_HAVE_LOCALPEERPID)) - So(nng_pipe_get_uint64(p, NNG_OPT_IPC_PEER_PID, &id) == 0); - So(id == (uint64_t) getpid()); -#else - So(nng_pipe_get_uint64(p, NNG_OPT_IPC_PEER_PID, &id) == NNG_ENOTSUP); -#endif - -#ifdef NNG_HAVE_GETPEERUCRED - So(nng_pipe_get_uint64(p, NNG_OPT_IPC_PEER_ZONEID, &id) == 0); - So(id == (uint64_t) getzoneid()); -#else - So(nng_pipe_get_uint64(p, NNG_OPT_IPC_PEER_ZONEID, &id) == - NNG_ENOTSUP); -#endif -#endif - return (0); -} - -TestMain("IPC Transport", - { trantest_test_extended("ipc:///tmp/nng_ipc_test_%u", check_props); }) From 47b22e7e91ee8e881d1b46094871f0440961f1ba Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sat, 23 Nov 2024 18:25:08 -0800 Subject: [PATCH 3/3] Remove pollfd test - covered in protocol tests --- tests/CMakeLists.txt | 1 - tests/pollfd.c | 113 ------------------------------------------- 2 files changed, 114 deletions(-) delete mode 100644 tests/pollfd.c diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a42dd23a4..3868a9552 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -134,7 +134,6 @@ add_nng_test(multistress 60) add_nng_test(nonblock 60) add_nng_test(options 5) add_nng_test(pipe 5) -add_nng_test(pollfd 5) add_nng_test(scalability 20 ON) add_nng_test(synch 5) add_nng_test(tcpsupp 10) diff --git a/tests/pollfd.c b/tests/pollfd.c deleted file mode 100644 index 5cc2d89a6..000000000 --- a/tests/pollfd.c +++ /dev/null @@ -1,113 +0,0 @@ -// -// Copyright 2024 Staysail Systems, Inc. -// Copyright 2018 Capitar IT Group BV -// -// This software is supplied under the terms of the MIT License, a -// copy of which should be located in the distribution where this -// file was obtained (LICENSE.txt). A copy of the license may also be -// found online at https://opensource.org/licenses/MIT. -// - -#include - -#ifndef _WIN32 -#include -#include -#define INVALID_SOCKET -1 -#else - -#define poll WSAPoll -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN -#endif - -#include -#include - -#include -#include - -#endif - -#include -#include -#include -#include - -#include "convey.h" -#include "stubs.h" - -TestMain("Poll FDs", { - Convey("Given a connected pair of sockets", { - nng_socket s1; - nng_socket s2; - - So(nng_pair1_open(&s1) == 0); - So(nng_pair1_open(&s2) == 0); - Reset({ - nng_close(s1); - nng_close(s2); - }); - So(nng_listen(s1, "inproc://yeahbaby", NULL, 0) == 0); - So(nng_dial(s2, "inproc://yeahbaby", NULL, 0) == 0); - nng_msleep(50); - - Convey("We can get a recv FD", { - int fd; - - So(nng_socket_get_recv_poll_fd(s1, &fd) == 0); - So(fd != (int) INVALID_SOCKET); - - Convey("And it is always the same fd", { - int fd2; - So(nng_socket_get_recv_poll_fd(s1, &fd2) == 0); - So(fd2 == fd); - }); - - Convey("And they start non pollable", { - struct pollfd pfd; - pfd.fd = fd; - pfd.events = POLLIN; - pfd.revents = 0; - - So(poll(&pfd, 1, 0) == 0); - So(pfd.revents == 0); - }); - - Convey("But if we write they are pollable", { - struct pollfd pfd; - pfd.fd = fd; - pfd.events = POLLIN; - pfd.revents = 0; - - So(nng_send(s2, "kick", 5, 0) == 0); - So(poll(&pfd, 1, 1000) == 1); - So((pfd.revents & POLLIN) != 0); - }); - }); - - Convey("We can get a send FD", { - int fd; - - So(nng_socket_get_send_poll_fd(s1, &fd) == 0); - So(fd != (int) INVALID_SOCKET); - So(nng_send(s1, "oops", 4, 0) == 0); - }); - }); - - Convey("We cannot get a send FD for PULL", { - nng_socket s3; - int fd; - So(nng_pull0_open(&s3) == 0); - Reset({ nng_close(s3); }); - So(nng_socket_get_send_poll_fd(s3, &fd) == NNG_ENOTSUP); - }); - - Convey("We cannot get a recv FD for PUSH", { - nng_socket s3; - int fd; - So(nng_push0_open(&s3) == 0); - Reset({ nng_close(s3); }); - So(nng_socket_get_recv_poll_fd(s3, &fd) == NNG_ENOTSUP); - }); -})