aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/CMakeLists.txt4
-rw-r--r--tests/sock.c1
-rw-r--r--tests/trantest.h2
-rw-r--r--tests/zt.c195
4 files changed, 202 insertions, 0 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 0c0973b2..fdfd6055 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -128,6 +128,10 @@ add_nng_test(device 5)
add_nng_test(errors 2)
add_nng_test(pair1 5)
+if (NNG_HAVE_ZEROTIER)
+ add_nng_test(zt 60)
+endif()
+
# compatbility tests
add_nng_compat_test(compat_block 5)
add_nng_compat_test(compat_bug777 5)
diff --git a/tests/sock.c b/tests/sock.c
index 21b12792..6c911250 100644
--- a/tests/sock.c
+++ b/tests/sock.c
@@ -182,6 +182,7 @@ TestMain("Socket Operations", {
&sz) == 0);
So(v == 0);
+ sz = 0;
So(nng_getopt(s1, nng_optid_reconnmaxt, &v,
&sz) == 0);
So(v == 0);
diff --git a/tests/trantest.h b/tests/trantest.h
index afb5a881..2e5f859d 100644
--- a/tests/trantest.h
+++ b/tests/trantest.h
@@ -137,6 +137,8 @@ trantest_send_recv(trantest *tt)
So(nng_dial(tt->reqsock, tt->addr, &d, 0) == 0);
So(d != 0);
+ nng_usleep(20000); // listener may be behind slightly
+
send = NULL;
So(nng_msg_alloc(&send, 0) == 0);
So(send != NULL);
diff --git a/tests/zt.c b/tests/zt.c
new file mode 100644
index 00000000..fe6023a1
--- /dev/null
+++ b/tests/zt.c
@@ -0,0 +1,195 @@
+//
+// Copyright 2017 Garrett D'Amore <garrett@damore.org>
+// Copyright 2017 Capitar IT Group BV <info@capitar.com>
+//
+// 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 "convey.h"
+#include "trantest.h"
+
+extern int nng_zt_register(void);
+extern const char *nng_opt_zt_home;
+extern int nng_optid_zt_home;
+extern int nng_optid_zt_node;
+extern int nng_optid_zt_status;
+extern int nng_optid_zt_network_name;
+extern int nng_zt_status_ok;
+
+// zerotier tests.
+
+// This network is an open network setup exclusively for nng testing.
+// Do not attach to it in production.
+#define NWID "a09acf02337b057b"
+
+#ifdef _WIN32
+
+int
+mkdir(const char *path, int mode)
+{
+ CreateDirectory(path, NULL);
+}
+#else
+#include <sys/stat.h>
+#include <unistd.h>
+#endif // WIN32
+
+TestMain("ZeroTier Transport", {
+
+ char path1[NNG_MAXADDRLEN] = "/tmp/zt_server";
+ char path2[NNG_MAXADDRLEN] = "/tmp/zt_client";
+ unsigned port;
+
+ port = 5555;
+
+ Convey("We can register the zero tier transport",
+ { So(nng_zt_register() == 0); });
+
+ Convey("We can create a zt listener", {
+ nng_listener l;
+ nng_socket s;
+ char addr[NNG_MAXADDRLEN];
+ int rv;
+
+ snprintf(addr, sizeof(addr), "zt://" NWID ":%u", port);
+
+ So(nng_pair_open(&s) == 0);
+ Reset({ nng_close(s); });
+
+ So(nng_listener_create(&l, s, addr) == 0);
+
+ Convey("We can lookup zerotier home option id", {
+ So(nng_optid_zt_home > 0);
+ So(nng_option_lookup(nng_opt_zt_home) ==
+ nng_optid_zt_home);
+ });
+
+ Convey("And it can be started...", {
+
+ mkdir(path1, 0700);
+
+ So(nng_listener_setopt(l, nng_optid_zt_home, path1,
+ strlen(path1) + 1) == 0);
+
+ So(nng_listener_start(l, 0) == 0);
+ })
+ });
+
+ Convey("We can create a zt dialer", {
+ nng_dialer d;
+ nng_socket s;
+ char addr[NNG_MAXADDRLEN];
+ int rv;
+ // uint64_t node = 0xb000072fa6ull; // my personal host
+ uint64_t node = 0x2d2f619cccull; // my personal host
+
+ snprintf(addr, sizeof(addr), "zt://" NWID "/%llx:%u",
+ (unsigned long long) node, port);
+
+ So(nng_pair_open(&s) == 0);
+ Reset({ nng_close(s); });
+
+ So(nng_dialer_create(&d, s, addr) == 0);
+
+ Convey("We can lookup zerotier home option id", {
+ So(nng_optid_zt_home > 0);
+ So(nng_option_lookup(nng_opt_zt_home) ==
+ nng_optid_zt_home);
+ });
+ });
+
+ Convey("We can create an ephemeral listener", {
+ nng_dialer d;
+ nng_listener l;
+ nng_socket s;
+ char addr[NNG_MAXADDRLEN];
+ int rv;
+ uint64_t node1 = 0;
+ uint64_t node2 = 0;
+
+ snprintf(addr, sizeof(addr), "zt://" NWID ":%u", port);
+
+ So(nng_pair_open(&s) == 0);
+ Reset({ nng_close(s); });
+
+ So(nng_listener_create(&l, s, addr) == 0);
+
+ So(nng_listener_getopt_usec(l, nng_optid_zt_node, &node1) ==
+ 0);
+ So(node1 != 0);
+
+ Convey("Network name & status options work", {
+ char name[NNG_MAXADDRLEN];
+ size_t namesz;
+ int status;
+
+ namesz = sizeof(name);
+ nng_usleep(10000000);
+ So(nng_listener_getopt(l, nng_optid_zt_network_name,
+ name, &namesz) == 0);
+ So(strcmp(name, "nng_test_open") == 0);
+ So(nng_listener_getopt_int(
+ l, nng_optid_zt_status, &status) == 0);
+ So(status == nng_zt_status_ok);
+ });
+ Convey("Connection refused works", {
+ snprintf(addr, sizeof(addr), "zt://" NWID "/%llx:%u",
+ (unsigned long long) node1, 42u);
+ So(nng_dialer_create(&d, s, addr) == 0);
+ So(nng_dialer_getopt_usec(
+ d, nng_optid_zt_node, &node2) == 0);
+ So(node2 == node1);
+ So(nng_dialer_start(d, 0) == NNG_ECONNREFUSED);
+ });
+ });
+
+ Convey("We can create a zt pair (dialer & listener)", {
+ nng_dialer d;
+ nng_listener l;
+ nng_socket s1;
+ nng_socket s2;
+ char addr1[NNG_MAXADDRLEN];
+ char addr2[NNG_MAXADDRLEN];
+ int rv;
+ uint64_t node;
+
+ port = 9944;
+ // uint64_t node = 0xb000072fa6ull; // my personal host
+
+ snprintf(addr1, sizeof(addr1), "zt://" NWID ":%u", port);
+
+ So(nng_pair_open(&s1) == 0);
+ So(nng_pair_open(&s2) == 0);
+ Reset({
+ nng_close(s1);
+ // This sleep allows us to ensure disconnect
+ // messages work.
+ nng_usleep(1000000);
+ nng_close(s2);
+ });
+
+ So(nng_listener_create(&l, s1, addr1) == 0);
+ So(nng_listener_setopt(
+ l, nng_optid_zt_home, path1, strlen(path1) + 1) == 0);
+
+ So(nng_listener_start(l, 0) == 0);
+ node = 0;
+ So(nng_listener_getopt_usec(l, nng_optid_zt_node, &node) == 0);
+ So(node != 0);
+
+ snprintf(addr2, sizeof(addr2), "zt://" NWID "/%llx:%u",
+ (unsigned long long) node, port);
+ So(nng_dialer_create(&d, s2, addr2) == 0);
+ So(nng_dialer_setopt(
+ d, nng_optid_zt_home, path2, strlen(path2) + 1) == 0);
+ So(nng_dialer_start(d, 0) == 0);
+
+ });
+
+ trantest_test_all("zt://" NWID "/*:%u");
+
+ nng_fini();
+})