Browse Source

test: fix test case issue.

Nick Peng 9 months ago
parent
commit
1e41a64110
8 changed files with 79 additions and 10 deletions
  1. 5 0
      src/dns_conf.c
  2. 1 0
      src/dns_conf.h
  3. 25 4
      src/fast_ping.c
  4. 7 0
      test/cases/test-ping.cc
  5. 7 2
      test/cases/test-speed-check.cc
  6. 2 0
      test/include/utils.h
  7. 13 4
      test/server.cc
  8. 19 0
      test/utils.cc

+ 5 - 0
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;
 }
 

+ 1 - 0
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);
 

+ 25 - 4
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 @@ errout:
 
 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) {

+ 7 - 0
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);

+ 7 - 2
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);
 

+ 2 - 0
test/include/utils.h

@@ -109,5 +109,7 @@ int ParserArg(const std::string &cmd, std::vector<std::string> &args);
 
 std::vector<std::string> GetAvailableIPAddresses();
 
+bool IsICMPAvailable();
+
 } // namespace smartdns
 #endif // _SMARTDNS_TEST_UTILS_

+ 13 - 4
test/server.cc

@@ -35,6 +35,8 @@
 #include <unistd.h>
 #include <vector>
 
+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;
 }

+ 19 - 0
test/utils.cc

@@ -1,4 +1,5 @@
 #include "include/utils.h"
+#include "util.h"
 #include <arpa/inet.h>
 #include <ifaddrs.h>
 #include <netinet/in.h>
@@ -305,4 +306,22 @@ std::vector<std::string> 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