aboutsummaryrefslogtreecommitdiff
path: root/src/platform/resolver_test.c
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2024-11-17 18:23:17 -0800
committerGarrett D'Amore <garrett@damore.org>2024-11-17 22:05:20 -0800
commit85aff44e00e836eda618d4f1cf013bce38b3fd44 (patch)
tree94b2dca800d6d254baae17932a017e031c17ce67 /src/platform/resolver_test.c
parentef82d4792bf59b1fe8053d9bb5ac924b443d8a78 (diff)
downloadnng-85aff44e00e836eda618d4f1cf013bce38b3fd44.tar.gz
nng-85aff44e00e836eda618d4f1cf013bce38b3fd44.tar.bz2
nng-85aff44e00e836eda618d4f1cf013bce38b3fd44.zip
URL u_port should be a number not a string.
The idea here is to reduce the dynamic allocations used for URLs, and also the back and forth with parsing begin strings and port numbers. We always resolve to a port number, and this is easier for everyone. The real goal in the long term is to eliminate dynamic allocation of the URL fields altogether, but that requires a little more work. This is a step in the right direction.
Diffstat (limited to 'src/platform/resolver_test.c')
-rw-r--r--src/platform/resolver_test.c57
1 files changed, 34 insertions, 23 deletions
diff --git a/src/platform/resolver_test.c b/src/platform/resolver_test.c
index 64554291..9c7db2c0 100644
--- a/src/platform/resolver_test.c
+++ b/src/platform/resolver_test.c
@@ -9,6 +9,7 @@
//
#include "core/nng_impl.h"
+#include "core/platform.h"
#include <nuts.h>
@@ -41,8 +42,8 @@ test_google_dns(void)
nng_sockaddr sa;
NUTS_PASS(nng_aio_alloc(&aio, NULL, NULL));
- nni_resolv_ip("google-public-dns-a.google.com", "80", NNG_AF_INET,
- true, &sa, aio);
+ nni_resolv_ip(
+ "google-public-dns-a.google.com", 80, NNG_AF_INET, true, &sa, aio);
nng_aio_wait(aio);
NUTS_PASS(nng_aio_result(aio));
NUTS_TRUE(sa.s_in.sa_family == NNG_AF_INET);
@@ -52,13 +53,29 @@ test_google_dns(void)
}
void
+test_hostname_too_long(void)
+{
+ nng_aio *aio;
+ nng_sockaddr sa;
+ char buffer[512];
+
+ memset(buffer, 'a', sizeof(buffer) - 1);
+ buffer[sizeof(buffer) - 1] = '\0';
+ NUTS_PASS(nng_aio_alloc(&aio, NULL, NULL));
+ nni_resolv_ip(buffer, 80, NNG_AF_INET, true, &sa, aio);
+ nng_aio_wait(aio);
+ NUTS_FAIL(nng_aio_result(aio), NNG_EADDRINVAL);
+ nng_aio_free(aio);
+}
+
+void
test_numeric_addr(void)
{
nng_aio *aio;
nng_sockaddr sa;
NUTS_PASS(nng_aio_alloc(&aio, NULL, NULL));
- nni_resolv_ip("8.8.4.4", "69", NNG_AF_INET, true, &sa, aio);
+ nni_resolv_ip("8.8.4.4", 69, NNG_AF_INET, true, &sa, aio);
nng_aio_wait(aio);
NUTS_PASS(nng_aio_result(aio));
NUTS_TRUE(sa.s_in.sa_family == NNG_AF_INET);
@@ -79,7 +96,7 @@ test_numeric_v6(void)
}
NUTS_MSG("IPV6 support present");
NUTS_PASS(nng_aio_alloc(&aio, NULL, NULL));
- nni_resolv_ip("::1", "80", NNG_AF_INET6, true, &sa, aio);
+ nni_resolv_ip("::1", 80, NNG_AF_INET6, true, &sa, aio);
nng_aio_wait(aio);
NUTS_PASS(nng_aio_result(aio));
NUTS_TRUE(sa.s_in6.sa_family == NNG_AF_INET6);
@@ -94,14 +111,21 @@ test_service_names(void)
{
nng_aio *aio;
nng_sockaddr sa;
+ uint16_t port;
NUTS_PASS(nng_aio_alloc(&aio, NULL, NULL));
- nni_resolv_ip("8.8.4.4", "http", NNG_AF_INET, true, &sa, aio);
+ nni_resolv_ip("8.8.4.4", 80, NNG_AF_INET, true, &sa, aio);
nng_aio_wait(aio);
NUTS_PASS(nng_aio_result(aio));
NUTS_TRUE(sa.s_in.sa_port == nuts_be16(80));
NUTS_TRUE(sa.s_in.sa_addr = nuts_be32(0x08080404));
nng_aio_free(aio);
+
+ NUTS_PASS(nni_get_port_by_name("http", &port));
+ NUTS_TRUE(port == 80);
+
+ NUTS_PASS(nni_get_port_by_name("25", &port));
+ NUTS_TRUE(port == 25);
}
void
@@ -111,7 +135,7 @@ test_localhost_v4(void)
nng_sockaddr sa;
NUTS_PASS(nng_aio_alloc(&aio, NULL, NULL));
- nni_resolv_ip("localhost", "80", NNG_AF_INET, true, &sa, aio);
+ nni_resolv_ip("localhost", 80, NNG_AF_INET, true, &sa, aio);
nng_aio_wait(aio);
NUTS_PASS(nng_aio_result(aio));
NUTS_TRUE(sa.s_in.sa_family == NNG_AF_INET);
@@ -127,7 +151,7 @@ test_localhost_unspecified(void)
nng_sockaddr sa;
NUTS_PASS(nng_aio_alloc(&aio, NULL, NULL));
- nni_resolv_ip("localhost", "80", NNG_AF_UNSPEC, true, &sa, aio);
+ nni_resolv_ip("localhost", 80, NNG_AF_UNSPEC, true, &sa, aio);
nng_aio_wait(aio);
NUTS_PASS(nng_aio_result(aio));
NUTS_TRUE(
@@ -154,7 +178,7 @@ test_null_passive(void)
nng_sockaddr sa;
NUTS_PASS(nng_aio_alloc(&aio, NULL, NULL));
- nni_resolv_ip(NULL, "80", NNG_AF_INET, true, &sa, aio);
+ nni_resolv_ip(NULL, 80, NNG_AF_INET, true, &sa, aio);
nng_aio_wait(aio);
NUTS_PASS(nng_aio_result(aio));
NUTS_TRUE(sa.s_in.sa_family == NNG_AF_INET);
@@ -170,7 +194,7 @@ test_null_not_passive(void)
nng_sockaddr sa;
NUTS_PASS(nng_aio_alloc(&aio, NULL, NULL));
- nni_resolv_ip(NULL, "80", NNG_AF_INET, false, &sa, aio);
+ nni_resolv_ip(NULL, 80, NNG_AF_INET, false, &sa, aio);
nng_aio_wait(aio);
// We can either get invalid address, or a loopback address.
// Most systems do the former, but Linux does the latter.
@@ -184,21 +208,9 @@ test_null_not_passive(void)
nng_aio_free(aio);
}
-void
-test_bad_port_number(void)
-{
- nng_aio *aio;
- nng_sockaddr sa;
-
- NUTS_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);
- NUTS_FAIL(nng_aio_result(aio), NNG_EADDRINVAL);
- nng_aio_free(aio);
-}
-
NUTS_TESTS = {
{ "resolve google dns", test_google_dns },
+ { "resolve hostname too long", test_hostname_too_long },
{ "resolve numeric addr", test_numeric_addr },
#ifdef NNG_ENABLE_IPV6
{ "resolve numeric v6", test_numeric_v6 },
@@ -208,6 +220,5 @@ NUTS_TESTS = {
{ "resolve localhost unspecified", test_localhost_unspecified },
{ "resolve null passive", test_null_passive },
{ "resolve null not passive", test_null_not_passive },
- { "resolve bad port number", test_bad_port_number },
{ NULL, NULL },
};