aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2020-11-09 22:27:43 -0800
committerGarrett D'Amore <garrett@damore.org>2020-11-09 22:27:43 -0800
commit2a55e0a279a0b94e204e0ffead73505100a0481f (patch)
treed49051c76a0b1dc134281d5ba83869c519dd0f2d /src
parentac03c65f4f53265a5ef05d4792e84cf590f5e73e (diff)
downloadnng-2a55e0a279a0b94e204e0ffead73505100a0481f.tar.gz
nng-2a55e0a279a0b94e204e0ffead73505100a0481f.tar.bz2
nng-2a55e0a279a0b94e204e0ffead73505100a0481f.zip
fixes #1332 Test nng.ws failed, "Incorrect URL paths do not work"
This moves some of the fragile tests to a new test suite that is a bit more careful with IPv4 vs. IPv6. Hopefully it will be a bit more resilient as a result.
Diffstat (limited to 'src')
-rw-r--r--src/transport/ipc/CMakeLists.txt4
-rw-r--r--src/transport/ws/CMakeLists.txt25
-rw-r--r--src/transport/ws/ws_test.c150
3 files changed, 162 insertions, 17 deletions
diff --git a/src/transport/ipc/CMakeLists.txt b/src/transport/ipc/CMakeLists.txt
index d9b771a9..00e81d54 100644
--- a/src/transport/ipc/CMakeLists.txt
+++ b/src/transport/ipc/CMakeLists.txt
@@ -1,5 +1,5 @@
#
-# Copyright 2019 Staysail Systems, Inc. <info@staysail.tech>
+# Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
# Copyright 2018 Capitar IT Group BV <info@capitar.com>
#
# This software is supplied under the terms of the MIT License, a
@@ -15,4 +15,4 @@ mark_as_advanced(NNG_TRANSPORT_IPC)
nng_sources_if(NNG_TRANSPORT_IPC ipc.c)
nng_headers_if(NNG_TRANSPORT_IPC nng/transport/ipc/ipc.h)
nng_defines_if(NNG_TRANSPORT_IPC NNG_TRANSPORT_IPC)
-nng_test(ipc_test) \ No newline at end of file
+nng_test_if(NNG_TRANSPORT_IPC ipc_test) \ No newline at end of file
diff --git a/src/transport/ws/CMakeLists.txt b/src/transport/ws/CMakeLists.txt
index 6e618010..8d8b5965 100644
--- a/src/transport/ws/CMakeLists.txt
+++ b/src/transport/ws/CMakeLists.txt
@@ -1,5 +1,5 @@
#
-# Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
+# Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
# Copyright 2018 Capitar IT Group BV <info@capitar.com>
#
# This software is supplied under the terms of the MIT License, a
@@ -16,24 +16,19 @@ CMAKE_DEPENDENT_OPTION(NNG_TRANSPORT_WSS "Enable WSS transport" ON
"NNG_ENABLE_TLS" OFF)
mark_as_advanced(NNG_TRANSPORT_WSS)
-set(_DEFS)
-
-if (NNG_TRANSPORT_WS)
- list(APPEND _DEFS -DNNG_TRANSPORT_WS)
-endif()
-if (NNG_TRANSPORT_WSS)
- list(APPEND _DEFS -DNNG_TRANSPORT_WSS)
-endif()
-
if (NNG_TRANSPORT_WS OR NNG_TRANSPORT_WSS)
-
# Make sure things we *MUST* have are enabled.
set(NNG_SUPP_WEBSOCKET ON PARENT_SCOPE)
set(NNG_SUPP_HTTP ON PARENT_SCOPE)
set(NNG_SUPP_BASE64 ON PARENT_SCOPE)
set(NNG_SUPP_SHA1 ON PARENT_SCOPE)
-
- set(_SRCS transport/ws/websocket.c ${PROJECT_SOURCE_DIR}/include/nng/transport/ws/websocket.h)
- set(NNG_DEFS ${NNG_DEFS} ${_DEFS} PARENT_SCOPE)
- set(NNG_SRCS ${NNG_SRCS} ${_SRCS} PARENT_SCOPE)
+ set(WS_ON ON)
endif()
+
+nng_defines_if(NNG_TRANSPORT_WS NNG_TRANSPORT_WS)
+nng_defines_if(NNG_TRANSPORT_WSS NNG_TRANSPORT_WSS)
+nng_sources_if(WS_ON websocket.c)
+nng_headers_if(WS_ON nng/transport/ws/websocket.h)
+nng_test_if(WS_ON ws_test)
+
+
diff --git a/src/transport/ws/ws_test.c b/src/transport/ws/ws_test.c
new file mode 100644
index 00000000..3a6e7fdf
--- /dev/null
+++ b/src/transport/ws/ws_test.c
@@ -0,0 +1,150 @@
+//
+// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
+//
+// 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 <testutil.h>
+
+#include <nng/protocol/pair0/pair.h>
+
+#include <acutest.h>
+
+static void
+test_ws_url_path_filters(void)
+{
+ nng_socket s1;
+ nng_socket s2;
+ char addr[NNG_MAXADDRLEN];
+
+ TEST_NNG_PASS(nng_pair0_open(&s1));
+ TEST_NNG_PASS(nng_pair0_open(&s2));
+
+ testutil_scratch_addr("ws", sizeof(addr), addr);
+ TEST_NNG_PASS(nng_listen(s1, addr, NULL, 0));
+
+ // Now try we just remove the last character for now.
+ // This will make the path different.
+ addr[strlen(addr) - 1] = '\0';
+ TEST_NNG_FAIL(nng_dial(s2, addr, NULL, 0), NNG_ECONNREFUSED);
+
+ TEST_NNG_PASS(nng_close(s1));
+ TEST_NNG_PASS(nng_close(s2));
+}
+
+static void
+test_wild_card_port(void)
+{
+ nng_socket s1;
+ nng_socket s2;
+ nng_socket s3;
+ nng_socket s4;
+ nng_socket s5;
+ nng_socket s6;
+
+ nng_listener l1;
+ nng_listener l2;
+ nng_listener l3;
+ int port1;
+ int port2;
+ int port3;
+ char ws_url[128];
+ TEST_NNG_PASS(nng_pair0_open(&s1));
+ TEST_NNG_PASS(nng_pair0_open(&s2));
+ TEST_NNG_PASS(nng_pair0_open(&s3));
+ TEST_NNG_PASS(nng_pair0_open(&s4));
+ TEST_NNG_PASS(nng_pair0_open(&s5));
+ TEST_NNG_PASS(nng_pair0_open(&s6));
+ TEST_NNG_PASS(nng_listen(s1, "ws://127.0.0.1:0/one", &l1, 0));
+ TEST_NNG_PASS(
+ nng_listener_getopt_int(l1, NNG_OPT_TCP_BOUND_PORT, &port1));
+ TEST_CHECK(port1 != 0);
+ snprintf(ws_url, sizeof(ws_url), "ws4://127.0.0.1:%d/two", port1);
+ TEST_NNG_PASS(nng_listen(s2, ws_url, &l2, 0));
+ TEST_NNG_PASS(
+ nng_listener_getopt_int(l2, NNG_OPT_TCP_BOUND_PORT, &port2));
+ TEST_CHECK(port1 != 0);
+ TEST_CHECK(port1 == port2);
+ // Now try a different wild card port.
+ TEST_NNG_PASS(nng_listen(s3, "ws4://127.0.0.1:0/three", &l3, 0));
+ TEST_NNG_PASS(
+ nng_listener_getopt_int(l3, NNG_OPT_TCP_BOUND_PORT, &port3));
+ TEST_CHECK(port3 != 0);
+ TEST_CHECK(port3 != port1);
+
+ // Let's make sure can dial to each.
+ snprintf(ws_url, sizeof(ws_url), "ws://127.0.0.1:%d/one", port1);
+ TEST_NNG_PASS(nng_dial(s4, ws_url, NULL, 0));
+ snprintf(ws_url, sizeof(ws_url), "ws://127.0.0.1:%d/two", port2);
+ TEST_NNG_PASS(nng_dial(s6, ws_url, NULL, 0));
+ snprintf(ws_url, sizeof(ws_url), "ws://127.0.0.1:%d/three", port3);
+ TEST_NNG_PASS(nng_dial(s6, ws_url, NULL, 0));
+
+ TEST_NNG_PASS(nng_close(s1));
+ TEST_NNG_PASS(nng_close(s2));
+ TEST_NNG_PASS(nng_close(s3));
+ TEST_NNG_PASS(nng_close(s4));
+ TEST_NNG_PASS(nng_close(s5));
+ TEST_NNG_PASS(nng_close(s6));
+}
+
+static void
+test_wild_card_host(void)
+{
+ nng_socket s1;
+ nng_socket s2;
+ char addr[NNG_MAXADDRLEN];
+ uint16_t port;
+
+ TEST_NNG_PASS(nng_pair0_open(&s1));
+ TEST_NNG_PASS(nng_pair0_open(&s2));
+
+ port = testutil_next_port();
+
+ // we use ws4 to ensure 127.0.0.1 binding
+ snprintf(addr, sizeof(addr), "ws4://*:%u/test", port);
+ TEST_NNG_PASS(nng_listen(s1, addr, NULL, 0));
+ nng_msleep(100);
+
+ snprintf(addr, sizeof(addr), "ws://127.0.0.1:%u/test", port);
+ TEST_NNG_PASS(nng_dial(s2, addr, NULL, 0));
+
+ TEST_NNG_PASS(nng_close(s1));
+ TEST_NNG_PASS(nng_close(s2));
+}
+
+static void
+test_empty_host(void)
+{
+ nng_socket s1;
+ nng_socket s2;
+ char addr[NNG_MAXADDRLEN];
+ uint16_t port;
+
+ TEST_NNG_PASS(nng_pair0_open(&s1));
+ TEST_NNG_PASS(nng_pair0_open(&s2));
+
+ port = testutil_next_port();
+
+ // we use ws4 to ensure 127.0.0.1 binding
+ snprintf(addr, sizeof(addr), "ws4://:%u/test", port);
+ TEST_NNG_PASS(nng_listen(s1, addr, NULL, 0));
+ nng_msleep(100);
+
+ snprintf(addr, sizeof(addr), "ws://127.0.0.1:%u/test", port);
+ TEST_NNG_PASS(nng_dial(s2, addr, NULL, 0));
+
+ TEST_NNG_PASS(nng_close(s1));
+ TEST_NNG_PASS(nng_close(s2));
+}
+
+TEST_LIST = {
+ { "ws url path filters", test_ws_url_path_filters },
+ { "ws wild card port", test_wild_card_port },
+ { "ws wild card host", test_wild_card_host },
+ { "ws empty host", test_empty_host },
+ { NULL, NULL },
+}; \ No newline at end of file