diff options
| author | Garrett D'Amore <garrett@damore.org> | 2024-11-30 23:36:12 -0500 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2024-11-30 23:36:12 -0500 |
| commit | f02ba32919962b5268157d490080781611f6146e (patch) | |
| tree | 0c85ed2d97db1ac03c3c6ce9545fc3c731768f91 | |
| parent | 0bd34114fded68765ac665ba2f8b99c8f5289bb3 (diff) | |
| download | nng-f02ba32919962b5268157d490080781611f6146e.tar.gz nng-f02ba32919962b5268157d490080781611f6146e.tar.bz2 nng-f02ba32919962b5268157d490080781611f6146e.zip | |
tests: convert ipcsupp tests to NUTS
| -rw-r--r-- | src/platform/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/platform/ipc_stream_test.c | 111 | ||||
| -rw-r--r-- | tests/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | tests/ipcsupp.c | 140 |
4 files changed, 112 insertions, 141 deletions
diff --git a/src/platform/CMakeLists.txt b/src/platform/CMakeLists.txt index ce0cb0b7..929552ea 100644 --- a/src/platform/CMakeLists.txt +++ b/src/platform/CMakeLists.txt @@ -13,6 +13,7 @@ nng_directory(platform) add_subdirectory(posix) add_subdirectory(windows) +nng_test(ipc_stream_test) nng_test(platform_test) nng_test(resolver_test) nng_test(tcp_stream_test) diff --git a/src/platform/ipc_stream_test.c b/src/platform/ipc_stream_test.c new file mode 100644 index 00000000..caf7861a --- /dev/null +++ b/src/platform/ipc_stream_test.c @@ -0,0 +1,111 @@ +// +// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2018 Capitar IT Group BV <info@capitar.com> +// Copyright 2018 Devolutions <info@devolutions.net> +// +// 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 <string.h> + +#include <nng/nng.h> + +#include <nuts.h> + +void +test_ipc_stream(void) +{ + nng_stream_dialer *d = NULL; + nng_stream_listener *l = NULL; + char *url; + nng_aio *daio = NULL; + nng_aio *laio = NULL; + nng_aio *maio = NULL; + nng_stream *c1 = NULL; + nng_stream *c2 = NULL; + nng_aio *aio1; + nng_aio *aio2; + nng_iov iov; + nng_sockaddr sa2; + char buf1[5]; + char buf2[5]; + + NUTS_ADDR(url, "ipc"); + NUTS_PASS(nng_aio_alloc(&daio, NULL, NULL)); + NUTS_PASS(nng_aio_alloc(&laio, NULL, NULL)); + NUTS_PASS(nng_aio_alloc(&maio, NULL, NULL)); + NUTS_PASS(nng_aio_alloc(&aio1, NULL, NULL)); + NUTS_PASS(nng_aio_alloc(&aio2, NULL, NULL)); + + NUTS_PASS(nng_stream_listener_alloc(&l, url)); + NUTS_PASS(nng_stream_listener_listen(l)); + + NUTS_PASS(nng_stream_dialer_alloc(&d, url)); + nng_stream_dialer_dial(d, daio); + nng_stream_listener_accept(l, laio); + + nng_aio_wait(daio); + NUTS_PASS(nng_aio_result(daio)); + nng_aio_wait(laio); + NUTS_PASS(nng_aio_result(laio)); + + c1 = nng_aio_get_output(daio, 0); + c2 = nng_aio_get_output(laio, 0); + NUTS_TRUE(c1 != NULL); + NUTS_TRUE(c2 != NULL); + + // This relies on send completing for + // for just 5 bytes, and on recv doing + // the same. Technically this isn't + // guaranteed, but it would be weird + // to split such a small payload. + memcpy(buf1, "TEST", 5); + memset(buf2, 0, 5); + iov.iov_buf = buf1; + iov.iov_len = 5; + + nng_aio_set_iov(aio1, 1, &iov); + + iov.iov_buf = buf2; + iov.iov_len = 5; + nng_aio_set_iov(aio2, 1, &iov); + nng_stream_send(c1, aio1); + nng_stream_recv(c2, aio2); + nng_aio_wait(aio1); + nng_aio_wait(aio2); + + NUTS_PASS(nng_aio_result(aio1)); + NUTS_TRUE(nng_aio_count(aio1) == 5); + + NUTS_PASS(nng_aio_result(aio2)); + NUTS_TRUE(nng_aio_count(aio2) == 5); + + NUTS_TRUE(memcmp(buf1, buf2, 5) == 0); + + NUTS_PASS(nng_stream_get_addr(c2, NNG_OPT_LOCADDR, &sa2)); + NUTS_TRUE(sa2.s_ipc.sa_family == NNG_AF_IPC); + NUTS_MATCH(sa2.s_ipc.sa_path, url + strlen("ipc://")); + + nng_aio_free(aio1); + nng_aio_free(aio2); + nng_aio_free(daio); + nng_aio_free(laio); + nng_aio_free(maio); + + nng_stream_listener_close(l); + nng_stream_dialer_close(d); + nng_stream_listener_free(l); + nng_stream_dialer_free(d); + nng_stream_close(c1); + nng_stream_free(c1); + nng_stream_close(c2); + nng_stream_free(c2); +} + +NUTS_TESTS = { + { "ipc stream", test_ipc_stream }, + { NULL, NULL }, +}; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 6be06a8d..c54b78a9 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -127,7 +127,6 @@ endif () add_nng_test(files 5) add_nng_test1(httpclient 60 NNG_SUPP_HTTP) add_nng_test1(httpserver 30 NNG_SUPP_HTTP) -add_nng_test(ipcsupp 10) add_nng_test(wss 30) add_nng_test1(zt 60 NNG_TRANSPORT_ZEROTIER) diff --git a/tests/ipcsupp.c b/tests/ipcsupp.c deleted file mode 100644 index bd751885..00000000 --- a/tests/ipcsupp.c +++ /dev/null @@ -1,140 +0,0 @@ -// -// Copyright 2021 Staysail Systems, Inc. <info@staysail.tech> -// Copyright 2018 Capitar IT Group BV <info@capitar.com> -// Copyright 2018 Devolutions <info@devolutions.net> -// -// 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 <string.h> - -#include <nng/nng.h> - -#include "convey.h" -#include "stubs.h" - -static int num = 0; - -TestMain("Supplemental IPC", { - Convey("We can create a dialer and listener", { - nng_stream_dialer * d; - nng_stream_listener *l; - char url[64]; - - snprintf(url, sizeof(url), "ipc:///tmp/ipcsupp_test%d", num); - num++; - So(nng_stream_dialer_alloc(&d, url) == 0); - So(nng_stream_listener_alloc(&l, url) == 0); - Reset({ - nng_stream_listener_close(l); - nng_stream_dialer_close(d); - nng_stream_listener_free(l); - nng_stream_dialer_free(d); - }); - Convey("Listener listens", { - So(nng_stream_listener_listen(l) == 0); - - Convey("We can dial it", { - nng_aio * daio = NULL; - nng_aio * laio = NULL; - nng_aio * maio = NULL; - nng_stream *c1 = NULL; - nng_stream *c2 = NULL; - - So(nng_aio_alloc(&daio, NULL, NULL) == 0); - So(nng_aio_alloc(&laio, NULL, NULL) == 0); - So(nng_aio_alloc(&maio, NULL, NULL) == 0); - - Reset({ - nng_aio_free(daio); - nng_aio_free(laio); - nng_aio_free(maio); - if (c1 != NULL) { - nng_stream_close(c1); - nng_stream_free(c1); - } - if (c2 != NULL) { - nng_stream_close(c2); - nng_stream_free(c2); - } - }); - - nng_stream_dialer_dial(d, daio); - nng_stream_listener_accept(l, laio); - - nng_aio_wait(daio); - nng_aio_wait(laio); - - So(nng_aio_result(daio) == 0); - So(nng_aio_result(laio) == 0); - - c1 = nng_aio_get_output(daio, 0); - c2 = nng_aio_get_output(laio, 0); - So(c1 != NULL); - So(c2 != NULL); - - Convey("They exchange messages", { - nng_aio * aio1; - nng_aio * aio2; - nng_iov iov; - nng_sockaddr sa2; - char buf1[5]; - char buf2[5]; - - So(nng_aio_alloc(&aio1, NULL, NULL) == - 0); - So(nng_aio_alloc(&aio2, NULL, NULL) == - 0); - - Reset({ - nng_aio_free(aio1); - nng_aio_free(aio2); - }); - - // This relies on send completing for - // for just 5 bytes, and on recv doing - // the same. Technically this isn't - // guaranteed, but it would be weird - // to split such a small payload. - memcpy(buf1, "TEST", 5); - memset(buf2, 0, 5); - iov.iov_buf = buf1; - iov.iov_len = 5; - - nng_aio_set_iov(aio1, 1, &iov); - - iov.iov_buf = buf2; - iov.iov_len = 5; - nng_aio_set_iov(aio2, 1, &iov); - nng_stream_send(c1, aio1); - nng_stream_recv(c2, aio2); - nng_aio_wait(aio1); - nng_aio_wait(aio2); - - So(nng_aio_result(aio1) == 0); - So(nng_aio_count(aio1) == 5); - - So(nng_aio_result(aio2) == 0); - So(nng_aio_count(aio2) == 5); - - So(memcmp(buf1, buf2, 5) == 0); - - Convey("Socket name matches", { - So(nng_stream_get_addr(c2, - NNG_OPT_LOCADDR, - &sa2) == 0); - So(sa2.s_ipc.sa_family == - NNG_AF_IPC); - So(strcmp(sa2.s_ipc.sa_path, - url + - strlen("ipc://")) == - 0); - }); - }); - }); - }); - }); -}) |
