Skip to content

Commit ac97598

Browse files
committed
test: fix test case issue.
1 parent fdc1a41 commit ac97598

File tree

8 files changed

+79
-10
lines changed

8 files changed

+79
-10
lines changed

Diff for: src/dns_conf.c

+5
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ struct dns_domain_check_orders dns_conf_default_check_orders = {
105105
},
106106
};
107107
static int dns_has_cap_ping = 0;
108+
int dns_ping_cap_force_enable = 0;
108109

109110
/* logging */
110111
int dns_conf_log_level = TLOG_ERROR;
@@ -6392,6 +6393,10 @@ static int _dns_ping_cap_check(void)
63926393
dns_has_cap_ping = 1;
63936394
}
63946395

6396+
if (dns_ping_cap_force_enable) {
6397+
dns_has_cap_ping = 1;
6398+
}
6399+
63956400
return 0;
63966401
}
63976402

Diff for: src/dns_conf.h

+1
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,7 @@ extern int dns_no_pidfile;
738738
extern int dns_no_daemon;
739739
extern int dns_restart_on_crash;
740740
extern size_t dns_socket_buff_size;
741+
extern int dns_ping_cap_force_enable;
741742

742743
void dns_server_load_exit(void);
743744

Diff for: src/fast_ping.c

+25-4
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ static int bool_print_log = 1;
191191
static void _fast_ping_host_put(struct ping_host_struct *ping_host);
192192
static int _fast_ping_get_addr_by_type(PING_TYPE type, const char *ip_str, int port, struct addrinfo **out_gai,
193193
FAST_PING_TYPE *out_ping_type);
194+
static int _fast_ping_create_icmp(FAST_PING_TYPE type);
194195

195196
static void _fast_ping_wakeup_thread(void)
196197
{
@@ -704,12 +705,27 @@ static void _fast_ping_host_remove(struct ping_host_struct *ping_host)
704705
_fast_ping_host_put(ping_host);
705706
}
706707

708+
static int _fast_ping_icmp_create_socket(struct ping_host_struct *ping_host)
709+
{
710+
if (_fast_ping_create_icmp(ping_host->type) < 0) {
711+
goto errout;
712+
}
713+
714+
return 0;
715+
errout:
716+
return -1;
717+
}
718+
707719
static int _fast_ping_sendping_v6(struct ping_host_struct *ping_host)
708720
{
709721
struct fast_ping_packet *packet = &ping_host->packet;
710722
struct icmp6_hdr *icmp6 = &packet->icmp6;
711723
int len = 0;
712724

725+
if (_fast_ping_icmp_create_socket(ping_host) < 0) {
726+
goto errout;
727+
}
728+
713729
if (ping.fd_icmp6 <= 0) {
714730
errno = EADDRNOTAVAIL;
715731
goto errout;
@@ -795,6 +811,15 @@ static int _fast_ping_send_fake(struct ping_host_struct *ping_host, struct fast_
795811

796812
static int _fast_ping_sendping_v4(struct ping_host_struct *ping_host)
797813
{
814+
if (_fast_ping_icmp_create_socket(ping_host) < 0) {
815+
goto errout;
816+
}
817+
818+
if (ping.fd_icmp <= 0) {
819+
errno = EADDRNOTAVAIL;
820+
goto errout;
821+
}
822+
798823
struct fast_ping_packet *packet = &ping_host->packet;
799824
struct icmp *icmp = &packet->icmp;
800825
int len = 0;
@@ -1260,10 +1285,6 @@ static int _fast_ping_get_addr_by_icmp(const char *ip_str, int port, struct addr
12601285
break;
12611286
}
12621287

1263-
if (_fast_ping_create_icmp(ping_type) < 0) {
1264-
goto errout;
1265-
}
1266-
12671288
if (out_gai != NULL) {
12681289
gai = _fast_ping_getaddr(ip_str, service, socktype, sockproto);
12691290
if (gai == NULL) {

Diff for: test/cases/test-ping.cc

+7
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ TEST_F(Ping, icmp)
5454
{
5555
struct ping_host_struct *ping_host;
5656
int count = 0;
57+
58+
if (smartdns::IsICMPAvailable() == false) {
59+
tlog(TLOG_INFO, "ICMP is not available, skip this test.");
60+
GTEST_SKIP();
61+
return;
62+
}
63+
5764
ping_host = fast_ping_start(PING_TYPE_ICMP, "127.0.0.1", 1, 1, 200, ping_result_callback, &count);
5865
ASSERT_NE(ping_host, nullptr);
5966
usleep(10000);

Diff for: test/cases/test-speed-check.cc

+7-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ domain-rules /a.com/ -r fastest-response
5555
std::cout << client.GetResult() << std::endl;
5656
ASSERT_EQ(client.GetAnswerNum(), 1);
5757
EXPECT_EQ(client.GetStatus(), "NOERROR");
58-
EXPECT_GT(client.GetQueryTime(), 100);
58+
if (smartdns::IsICMPAvailable()) {
59+
EXPECT_GT(client.GetQueryTime(), 100);
60+
}
5961
EXPECT_EQ(client.GetAnswer()[0].GetName(), "b.com");
6062
EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 600);
6163

@@ -131,7 +133,10 @@ domain-rules /a.com/ -c none
131133
std::cout << client.GetResult() << std::endl;
132134
ASSERT_EQ(client.GetAnswerNum(), 1);
133135
EXPECT_EQ(client.GetStatus(), "NOERROR");
134-
EXPECT_GT(client.GetQueryTime(), 200);
136+
if (smartdns::IsICMPAvailable()) {
137+
EXPECT_GT(client.GetQueryTime(), 200);
138+
}
139+
135140
EXPECT_EQ(client.GetAnswer()[0].GetName(), "b.com");
136141
EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 600);
137142

Diff for: test/include/utils.h

+2
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,7 @@ int ParserArg(const std::string &cmd, std::vector<std::string> &args);
109109

110110
std::vector<std::string> GetAvailableIPAddresses();
111111

112+
bool IsICMPAvailable();
113+
112114
} // namespace smartdns
113115
#endif // _SMARTDNS_TEST_UTILS_

Diff for: test/server.cc

+13-4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
#include <unistd.h>
3636
#include <vector>
3737

38+
extern int dns_ping_cap_force_enable;
39+
3840
namespace smartdns
3941
{
4042

@@ -65,7 +67,7 @@ void MockServer::Stop()
6567

6668
if (fd_ > 0) {
6769
close(fd_);
68-
fd_ = -1;;
70+
fd_ = -1;
6971
}
7072
}
7173

@@ -102,7 +104,7 @@ void MockServer::Run()
102104
struct ServerRequestContext request;
103105
memset(&request, 0, sizeof(request));
104106

105-
int ret = dns_decode(packet, sizeof(packet_buff), in_buff, len);
107+
ret = dns_decode(packet, sizeof(packet_buff), in_buff, len);
106108
if (ret == 0) {
107109
request.packet = packet;
108110
query_id = packet->head.id;
@@ -324,6 +326,8 @@ void Server::StartPost(void *arg)
324326

325327
if (has_ipv6 == true) {
326328
fast_ping_fake_ip_add(PING_TYPE_ICMP, "2001::", 64, 10);
329+
fast_ping_fake_ip_add(PING_TYPE_TCP, "[2001::]:80", 64, 10);
330+
fast_ping_fake_ip_add(PING_TYPE_TCP, "[2001::]:443", 64, 10);
327331
dns_server_check_ipv6_ready();
328332
}
329333
}
@@ -342,7 +346,7 @@ bool Server::Start(const std::string &conf, enum CONF_TYPE type)
342346
close(fds[0]);
343347
}
344348

345-
if (fds[0] > 0) {
349+
if (fds[1] > 0) {
346350
close(fds[1]);
347351
}
348352
};
@@ -387,6 +391,7 @@ cache-persist no
387391
}
388392

389393
smartdns_reg_post_func(Server::StartPost, this);
394+
dns_ping_cap_force_enable = 1;
390395
smartdns_main(args.size(), argv, fds[1], 0);
391396
_exit(1);
392397
} else if (pid < 0) {
@@ -401,7 +406,9 @@ cache-persist no
401406
}
402407

403408
smartdns_reg_post_func(Server::StartPost, this);
409+
dns_ping_cap_force_enable = 1;
404410
smartdns_main(args.size(), argv, fds[1], 1);
411+
dns_ping_cap_force_enable = 0;
405412
smartdns_reg_post_func(nullptr, nullptr);
406413
});
407414
} else {
@@ -443,7 +450,9 @@ void Server::Stop(bool graceful)
443450
}
444451
}
445452

446-
waitpid(pid_, nullptr, 0);
453+
if (pid_ > 0) {
454+
waitpid(pid_, nullptr, 0);
455+
}
447456

448457
pid_ = 0;
449458
}

Diff for: test/utils.cc

+19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "include/utils.h"
2+
#include "util.h"
23
#include <arpa/inet.h>
34
#include <ifaddrs.h>
45
#include <netinet/in.h>
@@ -305,4 +306,22 @@ std::vector<std::string> GetAvailableIPAddresses()
305306
return ipAddresses;
306307
}
307308

309+
bool IsICMPAvailable()
310+
{
311+
int fd = -1;
312+
if (has_unprivileged_ping()) {
313+
fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
314+
} else {
315+
fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
316+
}
317+
318+
if (fd < 0) {
319+
return false;
320+
}
321+
322+
close(fd);
323+
324+
return true;
325+
}
326+
308327
} // namespace smartdns

0 commit comments

Comments
 (0)