Sfoglia il codice sorgente

test: add test case for cache and fix some issue

Nick Peng 2 anni fa
parent
commit
bf68385d0e

+ 59 - 12
src/dns_client.c

@@ -3337,6 +3337,7 @@ static int _dns_client_send_packet(struct dns_query_struct *query, void *packet,
 	int send_err = 0;
 	int i = 0;
 	int total_server = 0;
+	int send_count = 0;
 
 	query->send_tick = get_tick_count();
 
@@ -3368,6 +3369,7 @@ static int _dns_client_send_packet(struct dns_query_struct *query, void *packet,
 			}
 
 			atomic_inc(&query->dns_request_sent);
+			send_count++;
 			errno = 0;
 			switch (server_info->type) {
 			case DNS_SERVER_UDP:
@@ -3402,6 +3404,7 @@ static int _dns_client_send_packet(struct dns_query_struct *query, void *packet,
 						 server_info->type);
 					_dns_client_close_socket(server_info);
 					atomic_dec(&query->dns_request_sent);
+					send_count--;
 					continue;
 				}
 
@@ -3416,6 +3419,7 @@ static int _dns_client_send_packet(struct dns_query_struct *query, void *packet,
 				}
 
 				atomic_dec(&query->dns_request_sent);
+				send_count--;
 				continue;
 			}
 			time(&server_info->last_send);
@@ -3423,12 +3427,12 @@ static int _dns_client_send_packet(struct dns_query_struct *query, void *packet,
 		}
 		pthread_mutex_unlock(&client.server_list_lock);
 
-		if (atomic_read(&query->dns_request_sent) > 0) {
+		if (send_count > 0) {
 			break;
 		}
 	}
 
-	if (atomic_read(&query->dns_request_sent) <= 0) {
+	if (send_count <= 0) {
 		tlog(TLOG_WARN, "Send query to upstream server failed, total server number %d", total_server);
 		return -1;
 	}
@@ -3590,12 +3594,60 @@ static int _dns_client_query_parser_options(struct dns_query_struct *query, stru
 	return 0;
 }
 
+static int _dns_client_add_hashmap(struct dns_query_struct *query)
+{
+	uint32_t key = 0;
+	struct hlist_node *tmp = NULL;
+	struct dns_query_struct *query_check = NULL;
+	int is_exists = 0;
+	int loop = 0;
+
+	while (loop ++ <= 32) {
+		if (RAND_bytes((unsigned char *)&query->sid, sizeof(query->sid)) != 1) {
+			query->sid = random();
+		}
+
+		key = hash_string(query->domain);
+		key = jhash(&query->sid, sizeof(query->sid), key);
+		key = jhash(&query->qtype, sizeof(query->qtype), key);
+		is_exists = 0;
+		pthread_mutex_lock(&client.domain_map_lock);
+		hash_for_each_possible_safe(client.domain_map, query_check, tmp, domain_node, key)
+		{
+			if (query->sid != query_check->sid) {
+				continue;
+			}
+
+			if (query->qtype != query_check->qtype) {
+				continue;
+			}
+
+			if (strncmp(query_check->domain, query->domain, DNS_MAX_CNAME_LEN) != 0) {
+				continue;
+			}
+
+			is_exists = 1;
+			break;
+		}
+
+		if (is_exists == 1) {
+			pthread_mutex_unlock(&client.domain_map_lock);
+			continue;
+		}
+		
+		hash_add(client.domain_map, &query->domain_node, key);
+		pthread_mutex_unlock(&client.domain_map_lock);
+		break;
+	}
+
+	return 0;
+}
+
 int dns_client_query(const char *domain, int qtype, dns_client_callback callback, void *user_ptr,
 					 const char *group_name, struct dns_query_options *options)
 {
 	struct dns_query_struct *query = NULL;
 	int ret = 0;
-	uint32_t key = 0;
 	int unused __attribute__((unused));
 
 	if (domain == NULL) {
@@ -3620,9 +3672,6 @@ int dns_client_query(const char *domain, int qtype, dns_client_callback callback
 	query->qtype = qtype;
 	query->send_tick = 0;
 	query->has_result = 0;
-	if (RAND_bytes((unsigned char *)&query->sid, sizeof(query->sid)) != 1) {
-		query->sid = random();
-	}
 	query->server_group = _dns_client_get_dnsserver_group(group_name);
 	if (query->server_group == NULL) {
 		tlog(TLOG_ERROR, "get dns server group %s failed.", group_name);
@@ -3636,12 +3685,10 @@ int dns_client_query(const char *domain, int qtype, dns_client_callback callback
 
 	_dns_client_query_get(query);
 	/* add query to hashtable */
-	key = hash_string(domain);
-	key = jhash(&query->sid, sizeof(query->sid), key);
-	key = jhash(&query->qtype, sizeof(query->qtype), key);
-	pthread_mutex_lock(&client.domain_map_lock);
-	hash_add(client.domain_map, &query->domain_node, key);
-	pthread_mutex_unlock(&client.domain_map_lock);
+	if (_dns_client_add_hashmap(query) != 0) {
+		tlog(TLOG_ERROR, "add query to hash map failed.");
+		goto errout;
+	}
 
 	/* send query */
 	_dns_client_query_get(query);

+ 49 - 18
src/dns_server.c

@@ -1748,7 +1748,7 @@ static int _dns_result_child_post(struct dns_server_post_context *context)
 	return 0;
 }
 
-static int _dns_request_update_ttl(struct dns_server_post_context *context)
+static int _dns_request_update_id_ttl(struct dns_server_post_context *context)
 {
 	int ttl = context->reply_ttl;
 	struct dns_request *request = context->request;
@@ -1767,17 +1767,22 @@ static int _dns_request_update_ttl(struct dns_server_post_context *context)
 		}
 	}
 
-	if (ttl > 0) {
-		struct dns_update_param param;
-		param.id = request->id;
-		param.cname_ttl = ttl;
-		param.ip_ttl = ttl;
-		if (dns_packet_update(context->inpacket, context->inpacket_len, &param) != 0) {
-			tlog(TLOG_ERROR, "update packet info failed.");
-			return -1;
+	if (ttl == 0) {
+		ttl = request->ip_ttl;
+		if (ttl == 0) {
+			ttl = _dns_server_get_conf_ttl(request, ttl);
 		}
 	}
 
+	struct dns_update_param param;
+	param.id = request->id;
+	param.cname_ttl = ttl;
+	param.ip_ttl = ttl;
+	if (dns_packet_update(context->inpacket, context->inpacket_len, &param) != 0) {
+		tlog(TLOG_ERROR, "update packet info failed.");
+		return -1;
+	}
+
 	return 0;
 }
 
@@ -1836,7 +1841,7 @@ static int _dns_request_post(struct dns_server_post_context *context)
 		return 0;
 	}
 
-	ret = _dns_request_update_ttl(context);
+	ret = _dns_request_update_id_ttl(context);
 	if (ret != 0) {
 		tlog(TLOG_ERROR, "update packet ttl failed.");
 		return -1;
@@ -2765,7 +2770,7 @@ static int _dns_server_process_answer_A(struct dns_rrs *rrs, struct dns_request
 		return -2;
 	}
 
-	if (request->has_ip == 0) {
+	if (atomic_read(&request->ip_map_num) == 0) {
 		request->has_ip = 1;
 		memcpy(request->ip_addr, addr, DNS_RR_A_LEN);
 		request->ip_ttl = _dns_server_get_conf_ttl(request, ttl);
@@ -2842,7 +2847,7 @@ static int _dns_server_process_answer_AAAA(struct dns_rrs *rrs, struct dns_reque
 		return -2;
 	}
 
-	if (request->has_ip == 0) {
+	if (atomic_read(&request->ip_map_num) == 0) {
 		request->has_ip = 1;
 		memcpy(request->ip_addr, addr, DNS_RR_AAAA_LEN);
 		request->ip_ttl = _dns_server_get_conf_ttl(request, ttl);
@@ -3113,6 +3118,9 @@ static int _dns_server_passthrough_rule_check(struct dns_request *request, const
 					char tmpname[DNS_MAX_CNAME_LEN];
 					char tmpbuf[DNS_MAX_CNAME_LEN];
 					dns_get_CNAME(rrs, tmpname, DNS_MAX_CNAME_LEN, &ttl, tmpbuf, DNS_MAX_CNAME_LEN);
+					if (request->ip_ttl == 0) {
+						request->ip_ttl = ttl;
+					}
 				}
 				break;
 			}
@@ -3272,7 +3280,7 @@ static int _dns_server_reply_passthrough(struct dns_server_post_context *context
 		char clientip[DNS_MAX_CNAME_LEN] = {0};
 
 		/* When passthrough, modify the id to be the id of the client request. */
-		int ret = _dns_request_update_ttl(context);
+		int ret = _dns_request_update_id_ttl(context);
 		if (ret != 0) {
 			tlog(TLOG_ERROR, "update packet ttl failed.");
 			return -1;
@@ -3294,14 +3302,12 @@ static void _dns_server_query_end(struct dns_request *request)
 
 	pthread_mutex_lock(&request->ip_map_lock);
 	ip_num = atomic_read(&request->ip_map_num);
-	/* if adblock ip address exist */
-	ip_num += atomic_read(&request->adblock) == 0 ? 0 : 1;
 	request_wait = request->request_wait;
 	request->request_wait--;
 	pthread_mutex_unlock(&request->ip_map_lock);
 
 	/* Not need to wait check result if only has one ip address */
-	if (ip_num == 1 && request_wait == 1) {
+	if (ip_num <= 1 && request_wait == 1) {
 		if (request->dualstack_selection_query == 1) {
 			if ((dns_conf_ipset_no_speed.ipv4_enable || dns_conf_nftset_no_speed.ip6_enable ||
 				 dns_conf_ipset_no_speed.ipv6_enable || dns_conf_nftset_no_speed.ip6_enable) &&
@@ -3328,7 +3334,8 @@ static int dns_server_dualstack_callback(const char *domain, dns_rtcode_t rtcode
 										 unsigned int ping_time, void *user_ptr)
 {
 	struct dns_request *request = (struct dns_request *)user_ptr;
-	tlog(TLOG_DEBUG, "dualstack result: domain: %s, ip: %s, type: %d, ping: %d, rcode: %d", domain, ip, addr_type, ping_time, rtcode);
+	tlog(TLOG_DEBUG, "dualstack result: domain: %s, ip: %s, type: %d, ping: %d, rcode: %d", domain, ip, addr_type,
+		 ping_time, rtcode);
 	if (request == NULL) {
 		return -1;
 	}
@@ -3426,6 +3433,7 @@ static int dns_server_resolve_callback(const char *domain, dns_result_type rtype
 				context.do_ipset = 1;
 				context.reply_ttl = _dns_server_get_reply_ttl(request, ttl);
 				context.cache_ttl = _dns_server_get_conf_ttl(request, ttl);
+				request->ip_ttl = context.cache_ttl;
 				context.no_check_add_ip = 1;
 				_dns_server_reply_passthrough(&context);
 				request->cname[0] = 0;
@@ -5341,7 +5349,8 @@ errout:
 	return ret;
 }
 
-static int _dns_server_process_udp(struct dns_server_conn_udp *udpconn, struct epoll_event *event, unsigned long now)
+static int _dns_server_process_udp_one(struct dns_server_conn_udp *udpconn, struct epoll_event *event,
+									   unsigned long now)
 {
 	int len = 0;
 	unsigned char inpacket[DNS_IN_PACKSIZE];
@@ -5366,6 +5375,9 @@ static int _dns_server_process_udp(struct dns_server_conn_udp *udpconn, struct e
 
 	len = recvmsg(udpconn->head.fd, &msg, MSG_DONTWAIT);
 	if (len < 0) {
+		if (errno == EAGAIN || errno == EWOULDBLOCK) {
+			return -2;
+		}
 		tlog(TLOG_ERROR, "recvfrom failed, %s\n", strerror(errno));
 		return -1;
 	}
@@ -5386,6 +5398,25 @@ static int _dns_server_process_udp(struct dns_server_conn_udp *udpconn, struct e
 	return _dns_server_recv(&udpconn->head, inpacket, len, &local, local_len, &from, from_len);
 }
 
+static int _dns_server_process_udp(struct dns_server_conn_udp *udpconn, struct epoll_event *event, unsigned long now)
+{
+	int count = 0;
+	while (count < 32) {
+		int ret = _dns_server_process_udp_one(udpconn, event, now);
+		if (ret != 0) {
+			if (ret == -2) {
+				return 0;
+			}
+
+			return ret;
+		}
+
+		count++;
+	}
+
+	return 0;
+}
+
 static void _dns_server_client_touch(struct dns_server_conn_head *conn)
 {
 	time(&conn->last_request_time);

+ 1 - 1
test/cases/test-bind.cc

@@ -84,7 +84,7 @@ cache-persist no)""");
 	std::cout << client.GetResult() << std::endl;
 	ASSERT_EQ(client.GetAnswerNum(), 1);
 	EXPECT_EQ(client.GetStatus(), "NOERROR");
-	EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 611);
+	EXPECT_GT(client.GetAnswer()[0].GetTTL(), 609);
 	EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
 }
 

+ 175 - 0
test/cases/test-cache.cc

@@ -0,0 +1,175 @@
+/*************************************************************************
+ *
+ * Copyright (C) 2018-2023 Ruilin Peng (Nick) <[email protected]>.
+ *
+ * smartdns is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * smartdns is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "client.h"
+#include "dns.h"
+#include "include/utils.h"
+#include "server.h"
+#include "gtest/gtest.h"
+
+class Cache : public ::testing::Test
+{
+  protected:
+	void SetUp() override {}
+
+	void TearDown() override {}
+};
+
+TEST_F(Cache, min)
+{
+	smartdns::MockServer server_upstream;
+	smartdns::Server server;
+
+	server_upstream.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) {
+		std::string domain = request->domain;
+		if (request->domain.length() == 0) {
+			return smartdns::SERVER_REQUEST_ERROR;
+		}
+
+		if (request->qtype == DNS_T_A) {
+			unsigned char addr[4] = {1, 2, 3, 4};
+			dns_add_A(request->response_packet, DNS_RRS_AN, domain.c_str(), 0, addr);
+		} else if (request->qtype == DNS_T_AAAA) {
+			unsigned char addr[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
+			dns_add_AAAA(request->response_packet, DNS_RRS_AN, domain.c_str(), 0, addr);
+		} else {
+			return smartdns::SERVER_REQUEST_ERROR;
+		}
+
+		request->response_packet->head.rcode = DNS_RC_NOERROR;
+		return smartdns::SERVER_REQUEST_OK;
+	});
+
+	server.Start(R"""(bind [::]:60053
+server 127.0.0.1:61053
+log-num 0
+cache-size 1
+rr-ttl-min 1
+speed-check-mode none
+response-mode fastest-response
+log-console yes
+log-level debug
+cache-persist no)""");
+	smartdns::Client client;
+	ASSERT_TRUE(client.Query("a.com", 60053));
+	std::cout << client.GetResult() << std::endl;
+	ASSERT_EQ(client.GetAnswerNum(), 1);
+	EXPECT_EQ(client.GetStatus(), "NOERROR");
+	EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
+	EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 1);
+	EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
+}
+
+TEST_F(Cache, max_reply_ttl)
+{
+	smartdns::MockServer server_upstream;
+	smartdns::Server server;
+
+	server_upstream.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) {
+		std::string domain = request->domain;
+		if (request->domain.length() == 0) {
+			return smartdns::SERVER_REQUEST_ERROR;
+		}
+
+		if (request->qtype == DNS_T_A) {
+			unsigned char addr[4] = {1, 2, 3, 4};
+			dns_add_A(request->response_packet, DNS_RRS_AN, domain.c_str(), 0, addr);
+		} else if (request->qtype == DNS_T_AAAA) {
+			unsigned char addr[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
+			dns_add_AAAA(request->response_packet, DNS_RRS_AN, domain.c_str(), 0, addr);
+		} else {
+			return smartdns::SERVER_REQUEST_ERROR;
+		}
+
+		request->response_packet->head.rcode = DNS_RC_NOERROR;
+		return smartdns::SERVER_REQUEST_OK;
+	});
+
+	server.Start(R"""(bind [::]:60053
+server 127.0.0.1:61053
+log-num 0
+cache-size 1
+rr-ttl-min 600
+rr-ttl-reply-max 5
+speed-check-mode none
+response-mode fastest-response
+log-console yes
+log-level debug
+cache-persist no)""");
+	smartdns::Client client;
+	ASSERT_TRUE(client.Query("a.com", 60053));
+	std::cout << client.GetResult() << std::endl;
+	ASSERT_EQ(client.GetAnswerNum(), 1);
+	EXPECT_EQ(client.GetStatus(), "NOERROR");
+	EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
+	EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 5);
+	EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
+}
+
+TEST_F(Cache, max_reply_ttl_expired)
+{
+	smartdns::MockServer server_upstream;
+	smartdns::Server server;
+
+	server_upstream.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) {
+		std::string domain = request->domain;
+		if (request->domain.length() == 0) {
+			return smartdns::SERVER_REQUEST_ERROR;
+		}
+
+		if (request->qtype == DNS_T_A) {
+			unsigned char addr[4] = {1, 2, 3, 4};
+			dns_add_A(request->response_packet, DNS_RRS_AN, domain.c_str(), 0, addr);
+		} else if (request->qtype == DNS_T_AAAA) {
+			unsigned char addr[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
+			dns_add_AAAA(request->response_packet, DNS_RRS_AN, domain.c_str(), 0, addr);
+		} else {
+			return smartdns::SERVER_REQUEST_ERROR;
+		}
+
+		request->response_packet->head.rcode = DNS_RC_NOERROR;
+		return smartdns::SERVER_REQUEST_OK;
+	});
+
+	server.Start(R"""(bind [::]:60053
+server 127.0.0.1:61053
+log-num 0
+cache-size 1
+rr-ttl-min 600
+rr-ttl-reply-max 5
+log-console yes
+log-level debug
+cache-persist no)""");
+	smartdns::Client client;
+	ASSERT_TRUE(client.Query("a.com", 60053));
+	std::cout << client.GetResult() << std::endl;
+	ASSERT_EQ(client.GetAnswerNum(), 1);
+	EXPECT_EQ(client.GetStatus(), "NOERROR");
+	EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
+	EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 3);
+	EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
+
+	ASSERT_TRUE(client.Query("a.com", 60053));
+	std::cout << client.GetResult() << std::endl;
+	ASSERT_EQ(client.GetAnswerNum(), 1);
+	EXPECT_EQ(client.GetStatus(), "NOERROR");
+	EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
+	EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 5);
+	EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
+}
+

+ 9 - 1
test/cases/test-discard-block-ip.cc

@@ -29,6 +29,10 @@ TEST(DiscardBlockIP, first_ping)
 	smartdns::Server server;
 
 	server_upstream1.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) {
+		if (request->qtype != DNS_T_A) {
+			return smartdns::SERVER_REQUEST_SOA;
+		}
+
 		unsigned char addr[4] = {0, 0, 0, 0};
 		dns_add_A(request->response_packet, DNS_RRS_AN, request->domain.c_str(), 611, addr);
 		request->response_packet->head.rcode = DNS_RC_NOERROR;
@@ -36,6 +40,9 @@ TEST(DiscardBlockIP, first_ping)
 	});
 
 	server_upstream2.Start("udp://0.0.0.0:62053", [](struct smartdns::ServerRequestContext *request) {
+		if (request->qtype != DNS_T_A) {
+			return smartdns::SERVER_REQUEST_SOA;
+		}
 		unsigned char addr[4] = {1, 2, 3, 4};
 		usleep(20000);
 		dns_add_A(request->response_packet, DNS_RRS_AN, request->domain.c_str(), 611, addr);
@@ -55,7 +62,8 @@ cache-persist no)""");
 	std::cout << client.GetResult() << std::endl;
 	ASSERT_EQ(client.GetAnswerNum(), 1);
 	EXPECT_EQ(client.GetStatus(), "NOERROR");
-	EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 600);
+	EXPECT_LT(client.GetQueryTime(), 100);
+	EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 3);
 	EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
 }
 

+ 1 - 1
test/cases/test-perf.cc

@@ -30,7 +30,7 @@ class Perf : public ::testing::Test
 	virtual void TearDown() {}
 };
 
-TEST(Perf, no_speed_check)
+TEST_F(Perf, no_speed_check)
 {
 	smartdns::MockServer server_upstream;
 	smartdns::Server server;

+ 2 - 2
test/cases/test-ping.cc

@@ -50,7 +50,7 @@ void ping_result_callback(struct ping_host_struct *ping_host, const char *host,
 	*count = 1;
 }
 
-TEST_F(Ping, DISABLED_icmp)
+TEST_F(Ping, icmp)
 {
 	struct ping_host_struct *ping_host;
 	int count = 0;
@@ -61,7 +61,7 @@ TEST_F(Ping, DISABLED_icmp)
 	EXPECT_EQ(count, 1);
 }
 
-TEST_F(Ping, DISABLED_tcp)
+TEST_F(Ping, tcp)
 {
 	struct ping_host_struct *ping_host;
 	int count = 0;

+ 96 - 0
test/cases/test-same-pending-query.cc

@@ -0,0 +1,96 @@
+/*************************************************************************
+ *
+ * Copyright (C) 2018-2023 Ruilin Peng (Nick) <[email protected]>.
+ *
+ * smartdns is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * smartdns is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "client.h"
+#include "dns.h"
+#include "include/utils.h"
+#include "server.h"
+#include "util.h"
+#include "gtest/gtest.h"
+#include <fstream>
+
+class SamePending : public ::testing::Test
+{
+  protected:
+	virtual void SetUp() {}
+	virtual void TearDown() {}
+};
+
+TEST_F(SamePending, pending)
+{
+	smartdns::MockServer server_upstream;
+	smartdns::Server server;
+	std::map<int, int> qid_map;
+
+	server_upstream.Start("udp://0.0.0.0:61053", [&](struct smartdns::ServerRequestContext *request) {
+		std::string domain = request->domain;
+		if (qid_map.find(request->packet->head.id) != qid_map.end()) {
+			qid_map[request->packet->head.id]++;
+			usleep(5000);
+		} else {
+			qid_map[request->packet->head.id] = 1;
+			usleep(20000);
+		}
+
+		if (request->domain.length() == 0) {
+			return smartdns::SERVER_REQUEST_ERROR;
+		}
+
+		if (request->qtype == DNS_T_A) {
+			unsigned char addr[4] = {1, 2, 3, 4};
+			dns_add_A(request->response_packet, DNS_RRS_AN, domain.c_str(), 61, addr);
+		} else if (request->qtype == DNS_T_AAAA) {
+			unsigned char addr[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
+			dns_add_AAAA(request->response_packet, DNS_RRS_AN, domain.c_str(), 61, addr);
+		} else {
+			return smartdns::SERVER_REQUEST_ERROR;
+		}
+
+		request->response_packet->head.rcode = DNS_RC_NOERROR;
+		return smartdns::SERVER_REQUEST_OK;
+	});
+
+	server.Start(R"""(bind [::]:60053
+server 127.0.0.1:61053
+cache-size 0
+log-num 0
+log-console yes
+speed-check-mode none
+log-level error
+cache-persist no)""");
+
+	std::vector<std::thread> threads;
+	uint64_t tick = get_tick_count();
+	for (int i = 0; i < 10; i++) {
+		auto t = std::thread([&]() {
+			for (int j = 0; j < 10; j++) {
+				smartdns::Client client;
+				ASSERT_TRUE(client.Query("a.com", 60053));
+				ASSERT_EQ(client.GetAnswerNum(), 1);
+				EXPECT_EQ(client.GetStatus(), "NOERROR");
+				EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
+				EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
+			}
+		});
+		threads.push_back(std::move(t));
+	}
+
+	for (auto &t : threads) {
+		t.join();
+	}
+}

+ 2 - 2
test/client.cc

@@ -125,10 +125,10 @@ bool Client::Query(const std::string &dig_cmds, int port, const std::string &ip)
 
 	cmd += " " + dig_cmds;
 	cmd += " +tries=1";
-	FILE *fp = NULL;
+	FILE *fp = nullptr;
 
 	fp = popen(cmd.c_str(), "r");
-	if (fp == NULL) {
+	if (fp == nullptr) {
 		return false;
 	}
 

+ 6 - 5
test/server.cc

@@ -106,7 +106,7 @@ void MockServer::Run()
 				request.packet = packet;
 				query_id = packet->head.id;
 				if (packet->head.qr == DNS_QR_QUERY) {
-					struct dns_rrs *rrs = NULL;
+					struct dns_rrs *rrs = nullptr;
 					int rr_count = 0;
 					int qtype = 0;
 					int qclass = 0;
@@ -213,7 +213,7 @@ bool MockServer::GetAddr(const std::string &host, const std::string port, int ty
 
 {
 	struct addrinfo hints;
-	struct addrinfo *result = NULL;
+	struct addrinfo *result = nullptr;
 
 	memset(&hints, 0, sizeof(hints));
 	hints.ai_family = AF_UNSPEC;
@@ -231,7 +231,8 @@ errout:
 	if (result) {
 		freeaddrinfo(result);
 	}
-	return NULL;
+
+	return false;
 }
 
 bool MockServer::Start(const std::string &url, ServerRequest callback)
@@ -378,7 +379,7 @@ void Server::Stop(bool graceful)
 		}
 	}
 
-	waitpid(pid_, NULL, 0);
+	waitpid(pid_, nullptr, 0);
 
 	pid_ = 0;
 	if (clean_conf_file_ == true) {
@@ -394,7 +395,7 @@ bool Server::IsRunning()
 		return false;
 	}
 
-	if (waitpid(pid_, NULL, WNOHANG) == 0) {
+	if (waitpid(pid_, nullptr, WNOHANG) == 0) {
 		return true;
 	}
 

+ 5 - 5
test/utils.cc

@@ -9,17 +9,17 @@ namespace smartdns
 
 bool IsCommandExists(const std::string &cmd)
 {
-	char *copy_path = NULL;
+	char *copy_path = nullptr;
 	char cmd_path[4096];
 	const char *env_path = getenv("PATH");
-	char *save_ptr = NULL;
+	char *save_ptr = nullptr;
 
-	if (env_path == NULL) {
+	if (env_path == nullptr) {
 		env_path = "/bin:/usr/bin:/usr/local/bin";
 	}
 
 	copy_path = strdup(env_path);
-	if (copy_path == NULL) {
+	if (copy_path == nullptr) {
 		return false;
 	}
 
@@ -28,7 +28,7 @@ bool IsCommandExists(const std::string &cmd)
 		free(copy_path);
 	};
 
-	for (char *tok = strtok_r(copy_path, ":", &save_ptr); tok; tok = strtok_r(NULL, ":", &save_ptr)) {
+	for (char *tok = strtok_r(copy_path, ":", &save_ptr); tok; tok = strtok_r(nullptr, ":", &save_ptr)) {
 		snprintf(cmd_path, sizeof(cmd_path), "%s/%s", tok, cmd.c_str());
 		if (access(cmd_path, X_OK) != 0) {
 			continue;