aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2020-10-31 18:47:07 -0700
committerGarrett D'Amore <garrett@damore.org>2020-10-31 23:10:12 -0700
commit452ecf5ae83adc9ae77518746f4f81171c42248c (patch)
treed81730eef3c19775abf0715831dc18e3f9885d21 /tests
parent587bc765ee69acfabf3bc8b88a70806c07b61f87 (diff)
downloadnng-452ecf5ae83adc9ae77518746f4f81171c42248c.tar.gz
nng-452ecf5ae83adc9ae77518746f4f81171c42248c.tar.bz2
nng-452ecf5ae83adc9ae77518746f4f81171c42248c.zip
fixes #1311 reduce wasted use for nni_aio
fixes #1317 IPv6 listener get port is incorrect fixes #1319 Want symbolic service names This is phase 1 of reducing the memory foot-print of aios, and also of pipes. This removes the largest consumer the socket address information, from the aio, which was only used by a few consumers.
Diffstat (limited to 'tests')
-rw-r--r--tests/CMakeLists.txt5
-rw-r--r--tests/resolv.c345
2 files changed, 172 insertions, 178 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index e1a708d5..737aa4d7 100644
--- a/tests/CMakeLists.txt
+++ b/tests/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>
# Copyright (c) 2012 Martin Sustrik All rights reserved.
# Copyright (c) 2013 GoPivotal, Inc. All rights reserved.
@@ -130,8 +130,10 @@ nng_test(bug1247)
nng_test(id)
nng_test(platform)
nng_test(reconnect)
+nng_test(resolv)
nng_test(sock)
+
add_nng_test(device 5)
add_nng_test(errors 2)
add_nng_test(files 5)
@@ -149,7 +151,6 @@ add_nng_test(nonblock 60)
add_nng_test(options 5)
add_nng_test(pipe 5)
add_nng_test(pollfd 5)
-add_nng_test1(resolv 10 NNG_STATIC_LIB)
add_nng_test(scalability 20 ON)
add_nng_test(set_recvmaxsize 2)
add_nng_test1(stats 5 NNG_ENABLE_STATS)
diff --git a/tests/resolv.c b/tests/resolv.c
index bcfb78a4..43168cdb 100644
--- a/tests/resolv.c
+++ b/tests/resolv.c
@@ -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
@@ -8,199 +8,192 @@
// found online at https://opensource.org/licenses/MIT.
//
+#include "testutil.h"
+
#include <string.h>
-#include "convey.h"
#include "core/nng_impl.h"
#include "stubs.h"
+#include "acutest.h"
+
#ifndef _WIN32
-#include <arpa/inet.h>
+#include <arpa/inet.h> // for htons, htonl
#endif
-static const char *
-ip4tostr(void *addr)
+uint8_t v6loop[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
+
+void
+test_google_dns(void)
{
- static char buf[256];
+ nng_aio * aio;
+ nng_sockaddr sa;
+
+ TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL));
+ nni_resolv_ip("google-public-dns-a.google.com", "80", NNG_AF_INET,
+ true, &sa, aio);
+ nng_aio_wait(aio);
+ TEST_NNG_PASS(nng_aio_result(aio));
+ TEST_CHECK(sa.s_in.sa_family == NNG_AF_INET);
+ TEST_CHECK(sa.s_in.sa_port == ntohs(80));
+ TEST_CHECK(sa.s_in.sa_addr == 0x08080808); // aka 8.8.8.8
+ nng_aio_free(aio);
+}
-#ifdef _WIN32
- return (InetNtop(AF_INET, addr, buf, sizeof(buf)));
+void
+test_numeric_addr(void)
+{
+ nng_aio * aio;
+ nng_sockaddr sa;
+
+ TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL));
+ nni_resolv_ip("8.8.4.4", "69", NNG_AF_INET, true, &sa, aio);
+ nng_aio_wait(aio);
+ TEST_NNG_PASS(nng_aio_result(aio));
+ TEST_CHECK(sa.s_in.sa_family == NNG_AF_INET);
+ TEST_CHECK(sa.s_in.sa_port == ntohs(69));
+ TEST_CHECK(sa.s_in.sa_addr == ntohl(0x08080404)); // 8.8.4.4.
+ nng_aio_free(aio);
+}
-#else
- return (inet_ntop(AF_INET, addr, buf, sizeof(buf)));
+void
+test_numeric_v6(void)
+{
+ nng_aio * aio;
+ nng_sockaddr sa;
+
+ // Travis CI has moved some of their services to host that
+ // apparently don't support IPv6 at all. This is very sad.
+ // CircleCI 2.0 is in the same boat. (Amazon to blame.)
+ if ((getenv("TRAVIS") != NULL) || (getenv("CIRCLECI") != NULL)) {
+ return; // skip this one.
+ }
+
+ TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL));
+ nni_resolv_ip("::1", "80", NNG_AF_INET6, true, &sa, aio);
+ nng_aio_wait(aio);
+ TEST_NNG_PASS(nng_aio_result(aio));
+ TEST_CHECK(sa.s_in6.sa_family == NNG_AF_INET6);
+ TEST_CHECK(sa.s_in6.sa_port == ntohs(80));
+ TEST_CHECK(memcmp(sa.s_in6.sa_addr, v6loop, 16) == 0);
+ nng_aio_free(aio);
+}
-#endif
+void
+test_service_names(void)
+{
+ nng_aio * aio;
+ nng_sockaddr sa;
+
+ TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL));
+ nni_resolv_ip("8.8.4.4", "http", NNG_AF_INET, true, &sa, aio);
+ nng_aio_wait(aio);
+ TEST_NNG_PASS(nng_aio_result(aio));
+ TEST_CHECK(sa.s_in.sa_port == ntohs(80));
+ TEST_CHECK(sa.s_in.sa_addr = ntohl(0x08080404));
+ nng_aio_free(aio);
}
-static const char *
-ip6tostr(void *addr)
+void
+test_localhost_v4(void)
{
- static char buf[256];
+ nng_aio * aio;
+ nng_sockaddr sa;
+
+ TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL));
+ nni_resolv_ip("localhost", "80", NNG_AF_INET, true, &sa, aio);
+ nng_aio_wait(aio);
+ TEST_NNG_PASS(nng_aio_result(aio));
+ TEST_CHECK(sa.s_in.sa_family == NNG_AF_INET);
+ TEST_CHECK(sa.s_in.sa_port == ntohs(80));
+ TEST_CHECK(sa.s_in.sa_addr == ntohl(0x7f000001));
+ nng_aio_free(aio);
+}
-#ifdef _WIN32
- return (InetNtop(AF_INET6, addr, buf, sizeof(buf)));
+void
+test_localhost_unspec(void)
+{
+ nng_aio * aio;
+ nng_sockaddr sa;
+
+ TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL));
+ nni_resolv_ip("localhost", "80", NNG_AF_UNSPEC, true, &sa, aio);
+ nng_aio_wait(aio);
+ TEST_NNG_PASS(nng_aio_result(aio));
+ TEST_CHECK(
+ (sa.s_family == NNG_AF_INET) || (sa.s_family == NNG_AF_INET6));
+ switch (sa.s_family) {
+ case NNG_AF_INET:
+ TEST_CHECK(sa.s_in.sa_port == ntohs(80));
+ TEST_CHECK(sa.s_in.sa_addr == ntohl(0x7f000001));
+ break;
+ case NNG_AF_INET6:
+ TEST_CHECK(sa.s_in6.sa_port == ntohs(80));
+ TEST_CHECK(memcmp(sa.s_in6.sa_addr, v6loop, 16) == 0);
+ break;
+ }
+ nng_aio_free(aio);
+}
-#else
- return (inet_ntop(AF_INET6, addr, buf, sizeof(buf)));
+void
+test_null_passive(void)
+{
+ nng_aio * aio;
+ nng_sockaddr sa;
+
+ TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL));
+ nni_resolv_ip(NULL, "80", NNG_AF_INET, true, &sa, aio);
+ nng_aio_wait(aio);
+ TEST_NNG_PASS(nng_aio_result(aio));
+ TEST_CHECK(sa.s_in.sa_family == NNG_AF_INET);
+ TEST_CHECK(sa.s_in.sa_port == ntohs(80));
+ TEST_CHECK(sa.s_in.sa_addr == 0); // INADDR_ANY
+ nng_aio_free(aio);
+}
-#endif
+void
+test_null_not_passive(void)
+{
+ nng_aio * aio;
+ nng_sockaddr sa;
+
+ TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL));
+ nni_resolv_ip(NULL, "80", NNG_AF_INET, false, &sa, aio);
+ nng_aio_wait(aio);
+ // We can either get NNG_EADDRINVAL, or a loopback address.
+ // Most systems do the former, but Linux does the latter.
+ if (nng_aio_result(aio) == 0) {
+ TEST_CHECK(sa.s_family == NNG_AF_INET);
+ TEST_CHECK(sa.s_in.sa_addr == htonl(0x7f000001));
+ TEST_CHECK(sa.s_in.sa_port == htons(80));
+ } else {
+ TEST_NNG_FAIL(nng_aio_result(aio), NNG_EADDRINVAL);
+ }
+ nng_aio_free(aio);
}
-// These work on Darwin, and should work on illumos, but they may
-// depend on the local resolver configuration. We elect not to depend
-// too much on them, since localhost can be configured weirdly. Notably
-// the normal assumptions on Linux do *not* hold true.
-#if 0
- Convey("Localhost IPv6 resolves", {
- nng_aio *aio;
- const char *str;
- nng_sockaddr sa;
- So(nng_aio_alloc(&aio, NULL, NULL) == 0);
- So(nng_aio_set_input(aio, 0, &sa) == 0);
- nni_tcp_resolv("localhost", "80", NNG_AF_INET6, 1, aio);
- nng_aio_wait(aio);
- So(nng_aio_result(aio) == 0);
- So(sa.s_in6.sa_family == NNG_AF_INET6);
- So(sa.s_in6.sa_port == ntohs(80));
- str = ip6tostr(&sa.s_in6.sa_addr);
- So(strcmp(str, "::1") == 0);
- nng_aio_free(aio);
- }
-#endif
+void
+test_bad_port_number(void)
+{
+ nng_aio * aio;
+ nng_sockaddr sa;
+
+ TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL));
+ nni_resolv_ip("1.1.1.1", "1000000", NNG_AF_INET, true, &sa, aio);
+ nng_aio_wait(aio);
+ TEST_NNG_FAIL(nng_aio_result(aio), NNG_EADDRINVAL);
+ nng_aio_free(aio);
+}
-TestMain("Resolver", {
- nni_init();
-
- Convey("Google DNS IPv4 resolves", {
- nng_aio * aio;
- const char * str;
- nng_sockaddr sa;
-
- So(nng_aio_alloc(&aio, NULL, NULL) == 0);
- nni_tcp_resolv("google-public-dns-a.google.com", "80",
- NNG_AF_INET, 1, aio);
- nng_aio_wait(aio);
- So(nng_aio_result(aio) == 0);
- nni_aio_get_sockaddr(aio, &sa);
- So(sa.s_in.sa_family == NNG_AF_INET);
- So(sa.s_in.sa_port == ntohs(80));
- str = ip4tostr(&sa.s_in.sa_addr);
- So(strcmp(str, "8.8.8.8") == 0);
- nng_aio_free(aio);
- });
- Convey("Numeric UDP resolves", {
- nng_aio * aio;
- const char * str;
- nng_sockaddr sa;
-
- So(nng_aio_alloc(&aio, NULL, NULL) == 0);
- nni_udp_resolv("8.8.4.4", "69", NNG_AF_INET, 1, aio);
- nng_aio_wait(aio);
- So(nng_aio_result(aio) == 0);
- nni_aio_get_sockaddr(aio, &sa);
- So(sa.s_in.sa_family == NNG_AF_INET);
- So(sa.s_in.sa_port == ntohs(69));
- str = ip4tostr(&sa.s_in.sa_addr);
- So(strcmp(str, "8.8.4.4") == 0);
- nng_aio_free(aio);
- });
- Convey("Numeric v4 resolves", {
- nng_aio * aio;
- const char * str;
- nng_sockaddr sa;
-
- So(nng_aio_alloc(&aio, NULL, NULL) == 0);
- nni_tcp_resolv("8.8.4.4", "80", NNG_AF_INET, 1, aio);
- nng_aio_wait(aio);
- So(nng_aio_result(aio) == 0);
- nni_aio_get_sockaddr(aio, &sa);
- So(sa.s_in.sa_family == NNG_AF_INET);
- So(sa.s_in.sa_port == ntohs(80));
- str = ip4tostr(&sa.s_in.sa_addr);
- So(strcmp(str, "8.8.4.4") == 0);
- nng_aio_free(aio);
- });
-
- Convey("Numeric v6 resolves", {
- nng_aio * aio;
- const char * str;
- nng_sockaddr sa;
-
- // Travis CI has moved some of their services to host that
- // apparently don't support IPv6 at all. This is very sad.
- // CircleCI 2.0 is in the same boat. (Amazon to blame.)
- if ((getenv("TRAVIS") != NULL) ||
- (getenv("CIRCLECI") != NULL)) {
- ConveySkip("IPv6 missing from CI provider");
- }
-
- So(nng_aio_alloc(&aio, NULL, NULL) == 0);
- nni_tcp_resolv("::1", "80", NNG_AF_INET6, 1, aio);
- nng_aio_wait(aio);
- So(nng_aio_result(aio) == 0);
- nni_aio_get_sockaddr(aio, &sa);
- So(sa.s_in6.sa_family == NNG_AF_INET6);
- So(sa.s_in6.sa_port == ntohs(80));
- str = ip6tostr(&sa.s_in6.sa_addr);
- So(strcmp(str, "::1") == 0);
- nng_aio_free(aio);
- });
-
- Convey("Name service names not supported", {
- nng_aio *aio;
-
- So(nng_aio_alloc(&aio, NULL, NULL) == 0);
- nni_tcp_resolv("8.8.4.4", "http", NNG_AF_INET, 1, aio);
- nng_aio_wait(aio);
- So(nng_aio_result(aio) == NNG_EADDRINVAL);
- nng_aio_free(aio);
- });
-
- Convey("Localhost IPv4 resolves", {
- nng_aio * aio;
- const char * str;
- nng_sockaddr sa;
-
- So(nng_aio_alloc(&aio, NULL, NULL) == 0);
- nni_tcp_resolv("localhost", "80", NNG_AF_INET, 1, aio);
- nng_aio_wait(aio);
- So(nng_aio_result(aio) == 0);
- nni_aio_get_sockaddr(aio, &sa);
- So(sa.s_in.sa_family == NNG_AF_INET);
- So(sa.s_in.sa_port == ntohs(80));
- So(sa.s_in.sa_addr == ntohl(0x7f000001));
- str = ip4tostr(&sa.s_in.sa_addr);
- So(strcmp(str, "127.0.0.1") == 0);
- nng_aio_free(aio);
- });
-
- Convey("Localhost UNSPEC resolves", {
- nng_aio * aio;
- const char * str;
- nng_sockaddr sa;
-
- So(nng_aio_alloc(&aio, NULL, NULL) == 0);
- nni_tcp_resolv("localhost", "80", NNG_AF_UNSPEC, 1, aio);
- nng_aio_wait(aio);
- So(nng_aio_result(aio) == 0);
- nni_aio_get_sockaddr(aio, &sa);
- So((sa.s_family == NNG_AF_INET) ||
- (sa.s_family == NNG_AF_INET6));
- switch (sa.s_family) {
- case NNG_AF_INET:
- So(sa.s_in.sa_port == ntohs(80));
- So(sa.s_in.sa_addr == ntohl(0x7f000001));
- str = ip4tostr(&sa.s_in.sa_addr);
- So(strcmp(str, "127.0.0.1") == 0);
- break;
- case NNG_AF_INET6:
- So(sa.s_in6.sa_port == ntohs(80));
- str = ip6tostr(&sa.s_in6.sa_addr);
- So(strcmp(str, "::1") == 0);
- break;
- }
- nng_aio_free(aio);
- });
-
- nni_fini();
-})
+TEST_LIST = {
+ { "resolve google dns", test_google_dns },
+ { "resolve numeric addr", test_numeric_addr },
+ { "resolve numeric v6", test_numeric_v6 },
+ { "resolve service names", test_service_names },
+ { "resolve localhost v4", test_localhost_v4 },
+ { "resolve localhost unspec", test_localhost_unspec },
+ { "resolve null passive", test_null_passive },
+ { "resolve null not passive", test_null_not_passive },
+ { "resolve bad port number", test_bad_port_number },
+ { NULL, NULL },
+};