Skip to content

Commit

Permalink
test: fix test case issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
pymumu committed Jan 16, 2025
1 parent fdc1a41 commit 1e41a64
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/dns_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions src/dns_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
29 changes: 25 additions & 4 deletions src/fast_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions test/cases/test-ping.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions test/cases/test-speed-check.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions test/include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
17 changes: 13 additions & 4 deletions test/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include <unistd.h>
#include <vector>

extern int dns_ping_cap_force_enable;

namespace smartdns
{

Expand Down Expand Up @@ -65,7 +67,7 @@ void MockServer::Stop()

if (fd_ > 0) {
close(fd_);
fd_ = -1;;
fd_ = -1;
}
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
Expand All @@ -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]);
}
};
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -443,7 +450,9 @@ void Server::Stop(bool graceful)
}
}

waitpid(pid_, nullptr, 0);
if (pid_ > 0) {
waitpid(pid_, nullptr, 0);
}

pid_ = 0;
}
Expand Down
19 changes: 19 additions & 0 deletions test/utils.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "include/utils.h"
#include "util.h"
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <netinet/in.h>
Expand Down Expand Up @@ -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

0 comments on commit 1e41a64

Please sign in to comment.