From 9d6cd7bd9770102650d384438bacef1f35465737 Mon Sep 17 00:00:00 2001 From: Anthony Penniston Date: Mon, 24 Aug 2020 12:53:13 -0700 Subject: [PATCH 1/8] support for system-assigned ports during test --- test/ares-test.cc | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/test/ares-test.cc b/test/ares-test.cc index ec4b46054..6088d8074 100644 --- a/test/ares-test.cc +++ b/test/ares-test.cc @@ -197,6 +197,22 @@ MockServer::MockServer(int family, int port, int tcpport) addr.sin_port = htons(udpport_); int udprc = bind(udpfd_, (struct sockaddr*)&addr, sizeof(addr)); EXPECT_EQ(0, udprc) << "Failed to bind AF_INET to UDP port " << udpport_; + // retrieve system-assigned port + if (udpport_ == 0) { + int len = sizeof(addr); + int result = getsockname(udpfd_, (struct sockaddr*) & addr, &len); + EXPECT_NE(SOCKET_ERROR, result); + udpport_ = ntohs(addr.sin_port); + EXPECT_NE(0, udpport_); + } + if (tcpport_ == 0) + { + int len = sizeof(addr); + int result = getsockname(tcpfd_, (struct sockaddr*) &addr, &len); + EXPECT_NE(SOCKET_ERROR, result); + tcpport_ = ntohs(addr.sin_port); + EXPECT_NE(0, tcpport_); + } } else { EXPECT_EQ(AF_INET6, family); struct sockaddr_in6 addr; @@ -209,6 +225,22 @@ MockServer::MockServer(int family, int port, int tcpport) addr.sin6_port = htons(udpport_); int udprc = bind(udpfd_, (struct sockaddr*)&addr, sizeof(addr)); EXPECT_EQ(0, udprc) << "Failed to bind AF_INET6 to UDP port " << udpport_; + // retrieve system-assigned port + if (udpport_ == 0) { + int len = sizeof(addr); + int result = getsockname(udpfd_, (struct sockaddr*)&addr, &len); + EXPECT_NE(SOCKET_ERROR, result); + udpport_ = ntohs(addr.sin6_port); + EXPECT_NE(0, udpport_); + } + if (tcpport_ == 0) + { + int len = sizeof(addr); + int result = getsockname(tcpfd_, (struct sockaddr*) &addr, &len); + EXPECT_NE(SOCKET_ERROR, result); + tcpport_ = ntohs(addr.sin6_port); + EXPECT_NE(0, tcpport_); + } } if (verbose) std::cerr << "Configured " << (family == AF_INET ? "IPv4" : "IPv6") @@ -403,9 +435,9 @@ MockChannelOptsTest::MockChannelOptsTest(int count, } // Point the library at the first mock server by default (overridden below). - opts.udp_port = mock_port; + opts.udp_port = server_.udpport(); optmask |= ARES_OPT_UDP_PORT; - opts.tcp_port = mock_port; + opts.tcp_port = server_.tcpport(); optmask |= ARES_OPT_TCP_PORT; // If not already overridden, set short-ish timeouts. From ab287541ad131108d290e59ddfa91fb70af157f9 Mon Sep 17 00:00:00 2001 From: Anthony Penniston Date: Mon, 24 Aug 2020 13:11:29 -0700 Subject: [PATCH 2/8] fix spacing --- test/ares-test.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/ares-test.cc b/test/ares-test.cc index 6088d8074..b29268c19 100644 --- a/test/ares-test.cc +++ b/test/ares-test.cc @@ -200,7 +200,7 @@ MockServer::MockServer(int family, int port, int tcpport) // retrieve system-assigned port if (udpport_ == 0) { int len = sizeof(addr); - int result = getsockname(udpfd_, (struct sockaddr*) & addr, &len); + int result = getsockname(udpfd_, (struct sockaddr*)&addr, &len); EXPECT_NE(SOCKET_ERROR, result); udpport_ = ntohs(addr.sin_port); EXPECT_NE(0, udpport_); @@ -208,7 +208,7 @@ MockServer::MockServer(int family, int port, int tcpport) if (tcpport_ == 0) { int len = sizeof(addr); - int result = getsockname(tcpfd_, (struct sockaddr*) &addr, &len); + int result = getsockname(tcpfd_, (struct sockaddr*)&addr, &len); EXPECT_NE(SOCKET_ERROR, result); tcpport_ = ntohs(addr.sin_port); EXPECT_NE(0, tcpport_); @@ -236,7 +236,7 @@ MockServer::MockServer(int family, int port, int tcpport) if (tcpport_ == 0) { int len = sizeof(addr); - int result = getsockname(tcpfd_, (struct sockaddr*) &addr, &len); + int result = getsockname(tcpfd_, (struct sockaddr*)&addr, &len); EXPECT_NE(SOCKET_ERROR, result); tcpport_ = ntohs(addr.sin6_port); EXPECT_NE(0, tcpport_); From fdec2544f73f69713a97d96b92af191f5e8eeb5e Mon Sep 17 00:00:00 2001 From: Anthony Penniston Date: Mon, 24 Aug 2020 14:57:41 -0700 Subject: [PATCH 3/8] getsockname can return different types on different systems, use deduced type for the result. --- test/ares-test.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/ares-test.cc b/test/ares-test.cc index b29268c19..820db5530 100644 --- a/test/ares-test.cc +++ b/test/ares-test.cc @@ -200,7 +200,7 @@ MockServer::MockServer(int family, int port, int tcpport) // retrieve system-assigned port if (udpport_ == 0) { int len = sizeof(addr); - int result = getsockname(udpfd_, (struct sockaddr*)&addr, &len); + auto result = getsockname(udpfd_, (struct sockaddr*)&addr, &len); EXPECT_NE(SOCKET_ERROR, result); udpport_ = ntohs(addr.sin_port); EXPECT_NE(0, udpport_); @@ -208,7 +208,7 @@ MockServer::MockServer(int family, int port, int tcpport) if (tcpport_ == 0) { int len = sizeof(addr); - int result = getsockname(tcpfd_, (struct sockaddr*)&addr, &len); + auto result = getsockname(tcpfd_, (struct sockaddr*)&addr, &len); EXPECT_NE(SOCKET_ERROR, result); tcpport_ = ntohs(addr.sin_port); EXPECT_NE(0, tcpport_); From cf4f454ecba2c3bfde4d62f6d627daee1b6fd2dd Mon Sep 17 00:00:00 2001 From: Anthony Penniston Date: Mon, 24 Aug 2020 15:09:19 -0700 Subject: [PATCH 4/8] type deduction for result of getsockname --- test/ares-test.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/ares-test.cc b/test/ares-test.cc index 820db5530..ec3a2f18a 100644 --- a/test/ares-test.cc +++ b/test/ares-test.cc @@ -228,7 +228,7 @@ MockServer::MockServer(int family, int port, int tcpport) // retrieve system-assigned port if (udpport_ == 0) { int len = sizeof(addr); - int result = getsockname(udpfd_, (struct sockaddr*)&addr, &len); + auto result = getsockname(udpfd_, (struct sockaddr*)&addr, &len); EXPECT_NE(SOCKET_ERROR, result); udpport_ = ntohs(addr.sin6_port); EXPECT_NE(0, udpport_); @@ -236,7 +236,7 @@ MockServer::MockServer(int family, int port, int tcpport) if (tcpport_ == 0) { int len = sizeof(addr); - int result = getsockname(tcpfd_, (struct sockaddr*)&addr, &len); + auto result = getsockname(tcpfd_, (struct sockaddr*)&addr, &len); EXPECT_NE(SOCKET_ERROR, result); tcpport_ = ntohs(addr.sin6_port); EXPECT_NE(0, tcpport_); From 01d175e605363ff52857c1383f7e6e43a39f1c06 Mon Sep 17 00:00:00 2001 From: Anthony Penniston Date: Mon, 24 Aug 2020 17:29:13 -0700 Subject: [PATCH 5/8] compare getsockname to 0 for success (more portable); fix spacing --- ares__sortaddrinfo.c | 2 +- test/ares-test.cc | 46 +++++++++++++++++++++----------------------- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/ares__sortaddrinfo.c b/ares__sortaddrinfo.c index c0e2ea152..58f8a0780 100644 --- a/ares__sortaddrinfo.c +++ b/ares__sortaddrinfo.c @@ -427,7 +427,7 @@ static int find_src_addr(ares_channel channel, return 0; } - if (getsockname(sock, src_addr, &len) == -1) + if (getsockname(sock, src_addr, &len) != 0) { ares__close_socket(channel, sock); return -1; diff --git a/test/ares-test.cc b/test/ares-test.cc index ec3a2f18a..af7be6154 100644 --- a/test/ares-test.cc +++ b/test/ares-test.cc @@ -199,19 +199,18 @@ MockServer::MockServer(int family, int port, int tcpport) EXPECT_EQ(0, udprc) << "Failed to bind AF_INET to UDP port " << udpport_; // retrieve system-assigned port if (udpport_ == 0) { - int len = sizeof(addr); - auto result = getsockname(udpfd_, (struct sockaddr*)&addr, &len); - EXPECT_NE(SOCKET_ERROR, result); - udpport_ = ntohs(addr.sin_port); - EXPECT_NE(0, udpport_); + ares_socklen_t len = sizeof(addr); + auto result = getsockname(udpfd_, (struct sockaddr*)&addr, &len); + EXPECT_EQ(0, result); + udpport_ = ntohs(addr.sin_port); + EXPECT_NE(0, udpport_); } - if (tcpport_ == 0) - { - int len = sizeof(addr); - auto result = getsockname(tcpfd_, (struct sockaddr*)&addr, &len); - EXPECT_NE(SOCKET_ERROR, result); - tcpport_ = ntohs(addr.sin_port); - EXPECT_NE(0, tcpport_); + if (tcpport_ == 0) { + ares_socklen_t len = sizeof(addr); + auto result = getsockname(tcpfd_, (struct sockaddr*)&addr, &len); + EXPECT_EQ(0, result); + tcpport_ = ntohs(addr.sin_port); + EXPECT_NE(0, tcpport_); } } else { EXPECT_EQ(AF_INET6, family); @@ -227,19 +226,18 @@ MockServer::MockServer(int family, int port, int tcpport) EXPECT_EQ(0, udprc) << "Failed to bind AF_INET6 to UDP port " << udpport_; // retrieve system-assigned port if (udpport_ == 0) { - int len = sizeof(addr); - auto result = getsockname(udpfd_, (struct sockaddr*)&addr, &len); - EXPECT_NE(SOCKET_ERROR, result); - udpport_ = ntohs(addr.sin6_port); - EXPECT_NE(0, udpport_); + ares_socklen_t len = sizeof(addr); + auto result = getsockname(udpfd_, (struct sockaddr*)&addr, &len); + EXPECT_EQ(0, result); + udpport_ = ntohs(addr.sin6_port); + EXPECT_NE(0, udpport_); } - if (tcpport_ == 0) - { - int len = sizeof(addr); - auto result = getsockname(tcpfd_, (struct sockaddr*)&addr, &len); - EXPECT_NE(SOCKET_ERROR, result); - tcpport_ = ntohs(addr.sin6_port); - EXPECT_NE(0, tcpport_); + if (tcpport_ == 0) { + ares_socklen_t len = sizeof(addr); + auto result = getsockname(tcpfd_, (struct sockaddr*)&addr, &len); + EXPECT_EQ(0, result); + tcpport_ = ntohs(addr.sin6_port); + EXPECT_NE(0, tcpport_); } } if (verbose) std::cerr << "Configured " From 60cbcbd6cbbd58f12615c883d96e4c4c7c63f7cf Mon Sep 17 00:00:00 2001 From: Anthony Penniston Date: Tue, 25 Aug 2020 12:21:03 -0700 Subject: [PATCH 6/8] if using system-assigned ports, ensure all servers use the system-assigned port --- test/ares-test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/ares-test.cc b/test/ares-test.cc index af7be6154..6b42f03f2 100644 --- a/test/ares-test.cc +++ b/test/ares-test.cc @@ -411,7 +411,7 @@ MockChannelOptsTest::NiceMockServers MockChannelOptsTest::BuildServers(int count NiceMockServers servers; assert(count > 0); for (int ii = 0; ii < count; ii++) { - std::unique_ptr server(new NiceMockServer(family, base_port + ii)); + std::unique_ptr server(new NiceMockServer(family, base_port ? base_port + ii : 0)); servers.push_back(std::move(server)); } return servers; From 7ac0346814bdce214edb12cd875bb3ee2e436448 Mon Sep 17 00:00:00 2001 From: Anthony Penniston Date: Tue, 25 Aug 2020 12:47:01 -0700 Subject: [PATCH 7/8] use the system-assigned port by default --- test/ares-test.cc | 26 ++++++++++++++------------ test/ares-test.h | 2 +- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/test/ares-test.cc b/test/ares-test.cc index 6b42f03f2..2c4cfaeeb 100644 --- a/test/ares-test.cc +++ b/test/ares-test.cc @@ -30,7 +30,8 @@ namespace ares { namespace test { bool verbose = false; -int mock_port = 5300; +static constexpr int dynamic_port = 0; +int mock_port = dynamic_port; const std::vector both_families = {AF_INET, AF_INET6}; const std::vector ipv4_family = {AF_INET}; @@ -169,8 +170,8 @@ void DefaultChannelModeTest::Process() { ProcessWork(channel_, NoExtraFDs, nullptr); } -MockServer::MockServer(int family, int port, int tcpport) - : udpport_(port), tcpport_(tcpport ? tcpport : udpport_), qid_(-1) { +MockServer::MockServer(int family, int port) + : udpport_(port), tcpport_(udpport_), qid_(-1) { // Create a TCP socket to receive data on. tcpfd_ = socket(family, SOCK_STREAM, 0); EXPECT_NE(-1, tcpfd_); @@ -198,19 +199,19 @@ MockServer::MockServer(int family, int port, int tcpport) int udprc = bind(udpfd_, (struct sockaddr*)&addr, sizeof(addr)); EXPECT_EQ(0, udprc) << "Failed to bind AF_INET to UDP port " << udpport_; // retrieve system-assigned port - if (udpport_ == 0) { + if (udpport_ == dynamic_port) { ares_socklen_t len = sizeof(addr); auto result = getsockname(udpfd_, (struct sockaddr*)&addr, &len); EXPECT_EQ(0, result); udpport_ = ntohs(addr.sin_port); - EXPECT_NE(0, udpport_); + EXPECT_NE(dynamic_port, udpport_); } - if (tcpport_ == 0) { + if (tcpport_ == dynamic_port) { ares_socklen_t len = sizeof(addr); auto result = getsockname(tcpfd_, (struct sockaddr*)&addr, &len); EXPECT_EQ(0, result); tcpport_ = ntohs(addr.sin_port); - EXPECT_NE(0, tcpport_); + EXPECT_NE(dynamic_port, tcpport_); } } else { EXPECT_EQ(AF_INET6, family); @@ -225,19 +226,19 @@ MockServer::MockServer(int family, int port, int tcpport) int udprc = bind(udpfd_, (struct sockaddr*)&addr, sizeof(addr)); EXPECT_EQ(0, udprc) << "Failed to bind AF_INET6 to UDP port " << udpport_; // retrieve system-assigned port - if (udpport_ == 0) { + if (udpport_ == dynamic_port) { ares_socklen_t len = sizeof(addr); auto result = getsockname(udpfd_, (struct sockaddr*)&addr, &len); EXPECT_EQ(0, result); udpport_ = ntohs(addr.sin6_port); - EXPECT_NE(0, udpport_); + EXPECT_NE(dynamic_port, udpport_); } - if (tcpport_ == 0) { + if (tcpport_ == dynamic_port) { ares_socklen_t len = sizeof(addr); auto result = getsockname(tcpfd_, (struct sockaddr*)&addr, &len); EXPECT_EQ(0, result); tcpport_ = ntohs(addr.sin6_port); - EXPECT_NE(0, tcpport_); + EXPECT_NE(dynamic_port, tcpport_); } } if (verbose) std::cerr << "Configured " @@ -411,7 +412,8 @@ MockChannelOptsTest::NiceMockServers MockChannelOptsTest::BuildServers(int count NiceMockServers servers; assert(count > 0); for (int ii = 0; ii < count; ii++) { - std::unique_ptr server(new NiceMockServer(family, base_port ? base_port + ii : 0)); + int port = base_port == dynamic_port ? dynamic_port : base_port + ii; + std::unique_ptr server(new NiceMockServer(family, port)); servers.push_back(std::move(server)); } return servers; diff --git a/test/ares-test.h b/test/ares-test.h index a3d1d37c1..e92bf3b05 100644 --- a/test/ares-test.h +++ b/test/ares-test.h @@ -136,7 +136,7 @@ class DefaultChannelModeTest // Mock DNS server to allow responses to be scripted by tests. class MockServer { public: - MockServer(int family, int port, int tcpport = 0); + MockServer(int family, int port); ~MockServer(); // Mock method indicating the processing of a particular From d26bf497d31c0a308585ebe527f4dceb4dfb754b Mon Sep 17 00:00:00 2001 From: Anthony Penniston Date: Tue, 25 Aug 2020 12:52:05 -0700 Subject: [PATCH 8/8] remove ability to specifiy tcp port independently to MockServer since the default value is ambiguous with the system-assigned port --- test/ares-test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/ares-test.cc b/test/ares-test.cc index 2c4cfaeeb..9cfb64b33 100644 --- a/test/ares-test.cc +++ b/test/ares-test.cc @@ -171,7 +171,7 @@ void DefaultChannelModeTest::Process() { } MockServer::MockServer(int family, int port) - : udpport_(port), tcpport_(udpport_), qid_(-1) { + : udpport_(port), tcpport_(port), qid_(-1) { // Create a TCP socket to receive data on. tcpfd_ = socket(family, SOCK_STREAM, 0); EXPECT_NE(-1, tcpfd_);