aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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
-rw-r--r--tests/testutil.h1
-rw-r--r--tests/ws.c91
5 files changed, 163 insertions, 108 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
diff --git a/tests/testutil.h b/tests/testutil.h
index eb7ab7d1..63979cff 100644
--- a/tests/testutil.h
+++ b/tests/testutil.h
@@ -15,6 +15,7 @@
// The following headers are provided for test code convenience.
#include <nng/nng.h>
+#include <nng/supplemental/util/platform.h>
#ifdef __cplusplus
extern "C" {
diff --git a/tests/ws.c b/tests/ws.c
index 4e12a788..70d2e8da 100644
--- a/tests/ws.c
+++ b/tests/ws.c
@@ -84,96 +84,5 @@ check_props_v4(nng_msg *msg)
TestMain("WebSocket Transport", {
trantest_test_extended("ws://127.0.0.1:%u/test", check_props_v4);
- Convey("Empty hostname works", {
- nng_socket s1;
- nng_socket s2;
- char addr[NNG_MAXADDRLEN];
-
- So(nng_pair_open(&s1) == 0);
- So(nng_pair_open(&s2) == 0);
- Reset({
- nng_close(s2);
- nng_close(s1);
- });
- trantest_next_address(addr, "ws4://:%u/test");
- So(nng_listen(s1, addr, NULL, 0) == 0);
- nng_msleep(100);
- // reset port back one
- trantest_prev_address(addr, "ws://127.0.0.1:%u/test");
- So(nng_dial(s2, addr, NULL, 0) == 0);
- });
-
- Convey("Wild card hostname works", {
- nng_socket s1;
- nng_socket s2;
- char addr[NNG_MAXADDRLEN];
-
- So(nng_pair_open(&s1) == 0);
- So(nng_pair_open(&s2) == 0);
- Reset({
- nng_close(s2);
- nng_close(s1);
- });
- trantest_next_address(addr, "ws4://*:%u/test");
- So(nng_listen(s1, addr, NULL, 0) == 0);
- nng_msleep(100);
- // reset port back one
- trantest_prev_address(addr, "ws://127.0.0.1:%u/test");
- So(nng_dial(s2, addr, NULL, 0) == 0);
- });
-
- Convey("Incorrect URL paths do not work", {
- nng_socket s1;
- nng_socket s2;
- char addr[NNG_MAXADDRLEN];
-
- So(nng_pair_open(&s1) == 0);
- So(nng_pair_open(&s2) == 0);
- Reset({
- nng_close(s2);
- nng_close(s1);
- });
- trantest_next_address(addr, "ws://:%u/test");
- So(nng_listen(s1, addr, NULL, 0) == 0);
- // reset port back one
- trantest_prev_address(addr, "ws://localhost:%u/nothere");
- So(nng_dial(s2, addr, NULL, 0) == NNG_ECONNREFUSED);
- });
-
- // This test covers bug 821.
- Convey("Double wildcard listen works", {
- nng_socket s1;
- nng_socket s2;
- So(nng_pair_open(&s1) == 0);
- So(nng_pair_open(&s2) == 0);
- Reset({
- nng_close(s1);
- nng_close(s2);
- });
- So(nng_listen(s1, "ws4://*:5599/one", NULL, 0) == 0);
- So(nng_listen(s1, "ws4://*:5599/two", NULL, 0) == 0);
- So(nng_dial(s2, "ws://127.0.0.1:5599/one", NULL, 0) == 0);
- });
-
- Convey("Wild card port works and can be used again", {
- nng_socket s1;
- nng_socket s2;
- nng_listener l1;
- int port;
- char ws_url[128];
- So(nng_pair_open(&s1) == 0);
- So(nng_pair_open(&s2) == 0);
- Reset({
- nng_close(s1);
- nng_close(s2);
- });
- So(nng_listen(s1, "ws://*:0/one", &l1, 0) == 0);
- So(nng_listener_getopt_int(
- l1, NNG_OPT_TCP_BOUND_PORT, &port) == 0);
- So(port != 0);
- snprintf(ws_url, sizeof(ws_url), "ws://*:%d/two", port);
- So(nng_listen(s2, ws_url, NULL, 0) == 0);
- });
-
nng_fini();
})