From b9b5c31b19df95d672ddc76cdfde29318f78b5ea Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sat, 30 Sep 2017 22:07:21 -0700 Subject: Add support for ZT sockaddr properties (pipe). Also add a generic property test function to trantest. --- tests/trantest.h | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/zt.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 114 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/trantest.h b/tests/trantest.h index 4b4d0335..542cff32 100644 --- a/tests/trantest.h +++ b/tests/trantest.h @@ -28,6 +28,8 @@ typedef struct { unsigned trantest_port = 0; +typedef int (*trantest_proptest_t)(nng_msg *, nng_listener, nng_dialer); + void trantest_next_address(char *out, const char *template) { @@ -169,6 +171,44 @@ trantest_send_recv(trantest *tt) }); } +void +trantest_check_properties(trantest *tt, trantest_proptest_t f) +{ + Convey("Properties test", { + nng_listener l; + nng_dialer d; + nng_msg * send; + nng_msg * recv; + size_t len; + nng_pipe p; + char url[NNG_MAXADDRLEN]; + size_t sz; + int rv; + + So(nng_listen(tt->repsock, tt->addr, &l, 0) == 0); + So(l != 0); + 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); + So(nng_msg_append(send, "props", 5) == 0); + + So(nng_sendmsg(tt->reqsock, send, 0) == 0); + recv = NULL; + So(nng_recvmsg(tt->repsock, &recv, 0) == 0); + So(recv != NULL); + So(nng_msg_len(recv) == 5); + So(strcmp(nng_msg_body(recv), "props") == 0); + rv = f(recv, l, d); + nng_msg_free(recv); + So(rv == 0); + }); +} + void trantest_send_recv_large(trantest *tt) { @@ -240,3 +280,23 @@ trantest_test_all(const char *addr) trantest_send_recv_large(&tt); }) } + +void +trantest_test_extended(const char *addr, trantest_proptest_t f) +{ + trantest tt; + + Convey("Given transport", { + trantest_init(&tt, addr); + + Reset({ trantest_fini(&tt); }); + + trantest_scheme(&tt); + trantest_conn_refused(&tt); + trantest_duplicate_listen(&tt); + trantest_listen_accept(&tt); + trantest_send_recv(&tt); + trantest_send_recv_large(&tt); + trantest_check_properties(&tt, f); + }) +} diff --git a/tests/zt.c b/tests/zt.c index 4826b6bf..b57d0f5b 100644 --- a/tests/zt.c +++ b/tests/zt.c @@ -36,6 +36,59 @@ mkdir(const char *path, int mode) #include #endif // WIN32 +static int +check_props(nng_msg *msg, nng_listener l, nng_dialer d) +{ + nng_sockaddr la, ra; + nng_pipe p; + size_t z; + size_t mtu; + uint64_t nwid; + p = nng_msg_get_pipe(msg); + So(p > 0); + + // Check local address. + z = sizeof(nng_sockaddr); + So(nng_pipe_getopt(p, NNG_OPT_LOCADDR, &la, &z) == 0); + So(z == sizeof(la)); + So(la.s_un.s_family == NNG_AF_ZT); + So(la.s_un.s_zt.sa_port == (trantest_port - 1)); + So(la.s_un.s_zt.sa_nwid == 0xa09acf02337b057bull); + So(la.s_un.s_zt.sa_nodeid != 0); + + // Check remote address. + z = sizeof(nng_sockaddr); + So(nng_pipe_getopt(p, NNG_OPT_REMADDR, &ra, &z) == 0); + So(z == sizeof(ra)); + So(ra.s_un.s_family == NNG_AF_ZT); + So(ra.s_un.s_zt.sa_port != 0); + So(ra.s_un.s_zt.sa_nwid == 0xa09acf02337b057bull); + So(ra.s_un.s_zt.sa_nodeid == la.s_un.s_zt.sa_nodeid); + + // Check network ID. + z = sizeof(nwid); + nwid = 0; + So(nng_pipe_getopt(p, "zt:nwid", &nwid, &z) == 0); + So(nwid = 0xa09acf02337b057bull); + + z = sizeof(nwid); + nwid = 0; + So(nng_dialer_getopt(d, "zt:nwid", &nwid, &z) == 0); + So(nwid = 0xa09acf02337b057bull); + + z = sizeof(nwid); + nwid = 0; + So(nng_listener_getopt(l, "zt:nwid", &nwid, &z) == 0); + So(nwid = 0xa09acf02337b057bull); + + // Check MTU + z = sizeof(mtu); + So(nng_pipe_getopt(p, "zt:mtu", &mtu, &z) == 0); + So(mtu >= 1000 && mtu <= 10000); + + return (0); +} + TestMain("ZeroTier Transport", { char path1[NNG_MAXADDRLEN] = "/tmp/zt_server"; @@ -175,7 +228,7 @@ TestMain("ZeroTier Transport", { }); - trantest_test_all("zt://" NWID "/*:%u"); + trantest_test_extended("zt://" NWID "/*:%u", check_props); nng_fini(); }) -- cgit v1.2.3-70-g09d2