From 1e41a64110cf2ca9278f76d348c286efdacbdfea Mon Sep 17 00:00:00 2001 From: Nick Peng Date: Thu, 16 Jan 2025 23:26:35 +0800 Subject: [PATCH] test: fix test case issue. --- src/dns_conf.c | 5 +++++ src/dns_conf.h | 1 + src/fast_ping.c | 29 +++++++++++++++++++++++++---- test/cases/test-ping.cc | 7 +++++++ test/cases/test-speed-check.cc | 9 +++++++-- test/include/utils.h | 2 ++ test/server.cc | 17 +++++++++++++---- test/utils.cc | 19 +++++++++++++++++++ 8 files changed, 79 insertions(+), 10 deletions(-) diff --git a/src/dns_conf.c b/src/dns_conf.c index 51a4f03690..8b5ae8c620 100644 --- a/src/dns_conf.c +++ b/src/dns_conf.c @@ -105,6 +105,7 @@ struct dns_domain_check_orders dns_conf_default_check_orders = { }, }; static int dns_has_cap_ping = 0; +int dns_ping_cap_force_enable = 0; /* logging */ int dns_conf_log_level = TLOG_ERROR; @@ -6392,6 +6393,10 @@ static int _dns_ping_cap_check(void) dns_has_cap_ping = 1; } + if (dns_ping_cap_force_enable) { + dns_has_cap_ping = 1; + } + return 0; } diff --git a/src/dns_conf.h b/src/dns_conf.h index 6f427997f0..c3706f8645 100644 --- a/src/dns_conf.h +++ b/src/dns_conf.h @@ -738,6 +738,7 @@ extern int dns_no_pidfile; extern int dns_no_daemon; extern int dns_restart_on_crash; extern size_t dns_socket_buff_size; +extern int dns_ping_cap_force_enable; void dns_server_load_exit(void); diff --git a/src/fast_ping.c b/src/fast_ping.c index 4ee3e4f11a..b50a7d8828 100644 --- a/src/fast_ping.c +++ b/src/fast_ping.c @@ -191,6 +191,7 @@ static int bool_print_log = 1; static void _fast_ping_host_put(struct ping_host_struct *ping_host); static int _fast_ping_get_addr_by_type(PING_TYPE type, const char *ip_str, int port, struct addrinfo **out_gai, FAST_PING_TYPE *out_ping_type); +static int _fast_ping_create_icmp(FAST_PING_TYPE type); static void _fast_ping_wakeup_thread(void) { @@ -704,12 +705,27 @@ static void _fast_ping_host_remove(struct ping_host_struct *ping_host) _fast_ping_host_put(ping_host); } +static int _fast_ping_icmp_create_socket(struct ping_host_struct *ping_host) +{ + if (_fast_ping_create_icmp(ping_host->type) < 0) { + goto errout; + } + + return 0; +errout: + return -1; +} + static int _fast_ping_sendping_v6(struct ping_host_struct *ping_host) { struct fast_ping_packet *packet = &ping_host->packet; struct icmp6_hdr *icmp6 = &packet->icmp6; int len = 0; + if (_fast_ping_icmp_create_socket(ping_host) < 0) { + goto errout; + } + if (ping.fd_icmp6 <= 0) { errno = EADDRNOTAVAIL; goto errout; @@ -795,6 +811,15 @@ static int _fast_ping_send_fake(struct ping_host_struct *ping_host, struct fast_ static int _fast_ping_sendping_v4(struct ping_host_struct *ping_host) { + if (_fast_ping_icmp_create_socket(ping_host) < 0) { + goto errout; + } + + if (ping.fd_icmp <= 0) { + errno = EADDRNOTAVAIL; + goto errout; + } + struct fast_ping_packet *packet = &ping_host->packet; struct icmp *icmp = &packet->icmp; int len = 0; @@ -1260,10 +1285,6 @@ static int _fast_ping_get_addr_by_icmp(const char *ip_str, int port, struct addr break; } - if (_fast_ping_create_icmp(ping_type) < 0) { - goto errout; - } - if (out_gai != NULL) { gai = _fast_ping_getaddr(ip_str, service, socktype, sockproto); if (gai == NULL) { diff --git a/test/cases/test-ping.cc b/test/cases/test-ping.cc index ceb49ad685..33e2676403 100644 --- a/test/cases/test-ping.cc +++ b/test/cases/test-ping.cc @@ -54,6 +54,13 @@ TEST_F(Ping, icmp) { struct ping_host_struct *ping_host; int count = 0; + + if (smartdns::IsICMPAvailable() == false) { + tlog(TLOG_INFO, "ICMP is not available, skip this test."); + GTEST_SKIP(); + return; + } + ping_host = fast_ping_start(PING_TYPE_ICMP, "127.0.0.1", 1, 1, 200, ping_result_callback, &count); ASSERT_NE(ping_host, nullptr); usleep(10000); diff --git a/test/cases/test-speed-check.cc b/test/cases/test-speed-check.cc index e4a348e380..4b04895064 100644 --- a/test/cases/test-speed-check.cc +++ b/test/cases/test-speed-check.cc @@ -55,7 +55,9 @@ domain-rules /a.com/ -r fastest-response std::cout << client.GetResult() << std::endl; ASSERT_EQ(client.GetAnswerNum(), 1); EXPECT_EQ(client.GetStatus(), "NOERROR"); - EXPECT_GT(client.GetQueryTime(), 100); + if (smartdns::IsICMPAvailable()) { + EXPECT_GT(client.GetQueryTime(), 100); + } EXPECT_EQ(client.GetAnswer()[0].GetName(), "b.com"); EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 600); @@ -131,7 +133,10 @@ domain-rules /a.com/ -c none std::cout << client.GetResult() << std::endl; ASSERT_EQ(client.GetAnswerNum(), 1); EXPECT_EQ(client.GetStatus(), "NOERROR"); - EXPECT_GT(client.GetQueryTime(), 200); + if (smartdns::IsICMPAvailable()) { + EXPECT_GT(client.GetQueryTime(), 200); + } + EXPECT_EQ(client.GetAnswer()[0].GetName(), "b.com"); EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 600); diff --git a/test/include/utils.h b/test/include/utils.h index 7c2e98e13d..e47104660b 100644 --- a/test/include/utils.h +++ b/test/include/utils.h @@ -109,5 +109,7 @@ int ParserArg(const std::string &cmd, std::vector &args); std::vector GetAvailableIPAddresses(); +bool IsICMPAvailable(); + } // namespace smartdns #endif // _SMARTDNS_TEST_UTILS_ diff --git a/test/server.cc b/test/server.cc index efd7863812..b76b7dfd6e 100644 --- a/test/server.cc +++ b/test/server.cc @@ -35,6 +35,8 @@ #include #include +extern int dns_ping_cap_force_enable; + namespace smartdns { @@ -65,7 +67,7 @@ void MockServer::Stop() if (fd_ > 0) { close(fd_); - fd_ = -1;; + fd_ = -1; } } @@ -102,7 +104,7 @@ void MockServer::Run() struct ServerRequestContext request; memset(&request, 0, sizeof(request)); - int ret = dns_decode(packet, sizeof(packet_buff), in_buff, len); + ret = dns_decode(packet, sizeof(packet_buff), in_buff, len); if (ret == 0) { request.packet = packet; query_id = packet->head.id; @@ -324,6 +326,8 @@ void Server::StartPost(void *arg) if (has_ipv6 == true) { fast_ping_fake_ip_add(PING_TYPE_ICMP, "2001::", 64, 10); + fast_ping_fake_ip_add(PING_TYPE_TCP, "[2001::]:80", 64, 10); + fast_ping_fake_ip_add(PING_TYPE_TCP, "[2001::]:443", 64, 10); dns_server_check_ipv6_ready(); } } @@ -342,7 +346,7 @@ bool Server::Start(const std::string &conf, enum CONF_TYPE type) close(fds[0]); } - if (fds[0] > 0) { + if (fds[1] > 0) { close(fds[1]); } }; @@ -387,6 +391,7 @@ cache-persist no } smartdns_reg_post_func(Server::StartPost, this); + dns_ping_cap_force_enable = 1; smartdns_main(args.size(), argv, fds[1], 0); _exit(1); } else if (pid < 0) { @@ -401,7 +406,9 @@ cache-persist no } smartdns_reg_post_func(Server::StartPost, this); + dns_ping_cap_force_enable = 1; smartdns_main(args.size(), argv, fds[1], 1); + dns_ping_cap_force_enable = 0; smartdns_reg_post_func(nullptr, nullptr); }); } else { @@ -443,7 +450,9 @@ void Server::Stop(bool graceful) } } - waitpid(pid_, nullptr, 0); + if (pid_ > 0) { + waitpid(pid_, nullptr, 0); + } pid_ = 0; } diff --git a/test/utils.cc b/test/utils.cc index b1036e5fc2..03104debd8 100644 --- a/test/utils.cc +++ b/test/utils.cc @@ -1,4 +1,5 @@ #include "include/utils.h" +#include "util.h" #include #include #include @@ -305,4 +306,22 @@ std::vector GetAvailableIPAddresses() return ipAddresses; } +bool IsICMPAvailable() +{ + int fd = -1; + if (has_unprivileged_ping()) { + fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP); + } else { + fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); + } + + if (fd < 0) { + return false; + } + + close(fd); + + return true; +} + } // namespace smartdns \ No newline at end of file