From 2e3e03bfdaebad517f6cdf8858a8f79da3b89ce0 Mon Sep 17 00:00:00 2001 From: Nick Peng Date: Wed, 15 Mar 2023 23:57:25 +0800 Subject: [PATCH] test: add some test and fix some bugs --- src/dns_server.c | 63 ++++++++++++++--------- src/fast_ping.h | 8 +-- src/tlog.c | 24 ++++++--- test/cases/test-bind.cc | 73 ++++++++++++++++++++++++++ test/cases/test-discard-block-ip.cc | 80 +++++++++++++++++++++++++++++ test/cases/test-mock-server.cc | 2 +- test/cases/test-ping.cc | 54 +++++++++++++++++++ test/cases/test-tls-server.cc | 50 ------------------ test/client.cc | 3 ++ 9 files changed, 270 insertions(+), 87 deletions(-) create mode 100644 test/cases/test-bind.cc create mode 100644 test/cases/test-discard-block-ip.cc create mode 100644 test/cases/test-ping.cc delete mode 100644 test/cases/test-tls-server.cc diff --git a/src/dns_server.c b/src/dns_server.c index 6f58847793..3a2db762f4 100644 --- a/src/dns_server.c +++ b/src/dns_server.c @@ -38,7 +38,11 @@ #include #include #include +#include +#include +#include #include +#include #include #include #include @@ -48,10 +52,6 @@ #include #include #include -#include -#include -#include -#include #define DNS_MAX_EVENTS 256 #define IPV6_READY_CHECK_TIME 180 @@ -372,6 +372,21 @@ static int _dns_server_has_bind_flag(struct dns_request *request, uint32_t flag) return -1; } +static int _dns_server_get_reply_ttl(struct dns_request *request, int ttl) +{ + int reply_ttl = ttl; + + if ((request->passthrough == 0 || request->passthrough == 2) && dns_conf_cachesize > 0 && + request->check_order_list->orders[0].type != DOMAIN_CHECK_NONE) { + reply_ttl = dns_conf_serve_expired_reply_ttl; + if (reply_ttl < 2) { + reply_ttl = 2; + } + } + + return reply_ttl; +} + static int _dns_server_get_conf_ttl(struct dns_request *request, int ttl) { int rr_ttl = dns_conf_rr_ttl; @@ -1745,7 +1760,6 @@ static int _dns_request_update_ttl(struct dns_server_post_context *context) if (ttl > dns_conf_rr_ttl_reply_max) { ttl %= dns_conf_rr_ttl_reply_max; - } if (ttl == 0) { @@ -1770,6 +1784,7 @@ static int _dns_request_update_ttl(struct dns_server_post_context *context) static int _dns_request_post(struct dns_server_post_context *context) { struct dns_request *request = context->request; + char clientip[DNS_MAX_CNAME_LEN] = {0}; int ret = 0; tlog(TLOG_DEBUG, "reply %s qtype: %d, rcode: %d, reply: %d", request->domain, request->qtype, @@ -1827,6 +1842,10 @@ static int _dns_request_post(struct dns_server_post_context *context) return -1; } + tlog(TLOG_INFO, "reply %s to %s, qtype: %d, id: %d, group: %s", request->domain, + get_host_by_addr(clientip, sizeof(clientip), (struct sockaddr *)&request->addr), request->qtype, request->id, + request->dns_group_name[0] != '\0' ? request->dns_group_name : "default"); + ret = _dns_reply_inpacket(request, context->inpacket, context->inpacket_len); if (ret != 0) { tlog(TLOG_WARN, "reply raw packet to client failed."); @@ -1983,7 +2002,7 @@ static int _dns_server_force_dualstack(struct dns_request *request) static int _dns_server_request_complete_with_all_IPs(struct dns_request *request, int with_all_ips) { int ttl = 0; - int reply_ttl = 0; + struct dns_server_post_context context; if (request->rcode == DNS_RC_SERVFAIL || request->rcode == DNS_RC_NXDOMAIN) { ttl = DNS_SERVER_FAIL_TTL; @@ -2029,23 +2048,13 @@ static int _dns_server_request_complete_with_all_IPs(struct dns_request *request } out: - reply_ttl = ttl; - if (request->passthrough == 0 && dns_conf_cachesize > 0 && - request->check_order_list->orders[0].type != DOMAIN_CHECK_NONE) { - reply_ttl = dns_conf_serve_expired_reply_ttl; - if (reply_ttl < 2) { - reply_ttl = 2; - } - } - - struct dns_server_post_context context; _dns_server_post_context_init(&context, request); context.do_cache = 1; context.do_ipset = 1; context.do_force_soa = request->dualstack_selection_force_soa; context.do_audit = 1; context.do_reply = 1; - context.reply_ttl = reply_ttl; + context.reply_ttl = _dns_server_get_reply_ttl(request, ttl); context.skip_notify_count = 1; context.select_all_best_ip = with_all_ips; context.no_release_parent = 1; @@ -3042,8 +3051,7 @@ static int _dns_server_passthrough_rule_check(struct dns_request *request, const /* Ad blocking result */ if (addr[0] == 0 || addr[0] == 127) { /* If half of the servers return the same result, then ignore this address */ - if (atomic_inc_return(&request->adblock) <= (dns_server_num() / 2 + dns_server_num() % 2)) { - request->rcode = DNS_RC_NOERROR; + if (atomic_read(&request->adblock) <= (dns_server_num() / 2 + dns_server_num() % 2)) { _dns_server_request_release(request); return 0; } @@ -3087,8 +3095,7 @@ static int _dns_server_passthrough_rule_check(struct dns_request *request, const /* Ad blocking result */ if (_dns_server_is_adblock_ipv6(addr) == 0) { /* If half of the servers return the same result, then ignore this address */ - if (atomic_inc_return(&request->adblock) <= (dns_server_num() / 2 + dns_server_num() % 2)) { - request->rcode = DNS_RC_NOERROR; + if (atomic_read(&request->adblock) <= (dns_server_num() / 2 + dns_server_num() % 2)) { _dns_server_request_release(request); return 0; } @@ -3262,6 +3269,8 @@ static int _dns_server_reply_passthrough(struct dns_server_post_context *context _dns_result_child_post(context); if (request->conn && context->do_reply == 1) { + 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); if (ret != 0) { @@ -3269,6 +3278,10 @@ static int _dns_server_reply_passthrough(struct dns_server_post_context *context return -1; } _dns_reply_inpacket(request, context->inpacket, context->inpacket_len); + + tlog(TLOG_INFO, "reply %s to %s, qtype: %d, id: %d, group: %s", request->domain, + get_host_by_addr(clientip, sizeof(clientip), (struct sockaddr *)&request->addr), request->qtype, + request->id, request->dns_group_name[0] != '\0' ? request->dns_group_name : "default"); } return _dns_server_reply_all_pending_list(request, context); @@ -3379,7 +3392,7 @@ static int dns_server_resolve_callback(const char *domain, dns_result_type rtype } if (rtype == DNS_QUERY_RESULT) { - tlog(TLOG_DEBUG, "query result from server %s: %d, type: %d", dns_client_get_server_ip(server_info), + tlog(TLOG_DEBUG, "query result from server %s:%d, type: %d", dns_client_get_server_ip(server_info), dns_client_get_server_port(server_info), dns_client_get_server_type(server_info)); if (request->passthrough == 1 && atomic_read(&request->notified) == 0) { @@ -3411,8 +3424,8 @@ static int dns_server_resolve_callback(const char *domain, dns_result_type rtype context.do_audit = 1; context.do_reply = 1; context.do_ipset = 1; - context.reply_ttl = 2; - context.cache_ttl = 2; + context.reply_ttl = _dns_server_get_reply_ttl(request, ttl); + context.cache_ttl = _dns_server_get_conf_ttl(request, ttl); context.no_check_add_ip = 1; _dns_server_reply_passthrough(&context); request->cname[0] = 0; @@ -5225,7 +5238,7 @@ static int _dns_server_recv(struct dns_server_conn_head *conn, unsigned char *in goto errout; } - tlog(TLOG_INFO, "query server %s from %s, qtype: %d\n", request->domain, name, request->qtype); + tlog(TLOG_INFO, "query %s from %s, qtype: %d, id: %d\n", request->domain, name, request->qtype, request->id); ret = _dns_server_do_query(request, 1); if (ret != 0) { diff --git a/src/fast_ping.h b/src/fast_ping.h index cb68e1d28e..8c2237a9a2 100644 --- a/src/fast_ping.h +++ b/src/fast_ping.h @@ -21,9 +21,9 @@ #include #include -#ifdef __cpluscplus +#ifdef __cplusplus extern "C" { -#endif +#endif /*__cplusplus */ typedef enum { PING_TYPE_ICMP = 1, @@ -54,8 +54,8 @@ int fast_ping_init(void); void fast_ping_exit(void); -#ifdef __cpluscplus +#ifdef __cplusplus } -#endif +#endif /*__cplusplus */ #endif // !FAST_PING_H diff --git a/src/tlog.c b/src/tlog.c index a93b493e28..3622dc6eb2 100644 --- a/src/tlog.c +++ b/src/tlog.c @@ -628,30 +628,40 @@ int tlog_printf(struct tlog_log *log, const char *format, ...) return len; } -static int _tlog_early_print(const char *format, va_list ap) +static int _tlog_early_print(tlog_level level, const char *file, int line, const char *func, const char *format, va_list ap) { char log_buf[TLOG_MAX_LINE_LEN]; size_t len = 0; size_t out_len = 0; + struct tlog_time cur_time; int unused __attribute__((unused)); if (tlog_disable_early_print) { return 0; } - len = vsnprintf(log_buf, sizeof(log_buf), format, ap); + if (_tlog_gettime(&cur_time) != 0) { + return -1; + } + + len = snprintf(log_buf, sizeof(log_buf), "[%.4d-%.2d-%.2d %.2d:%.2d:%.2d,%.3d][%5s][%17s:%-4d] ", + cur_time.year, cur_time.mon, cur_time.mday, cur_time.hour, cur_time.min, cur_time.sec, cur_time.usec / 1000, + tlog_get_level_string(level), file, line); out_len = len; + len = vsnprintf(log_buf + out_len, sizeof(log_buf) - out_len - 1, format, ap); + out_len += len; if (len <= 0) { return -1; - } else if (len >= sizeof(log_buf)) { - out_len = sizeof(log_buf); + } else if (len >= sizeof(log_buf) - 1) { + out_len = sizeof(log_buf) - 1; } - unused = write(STDOUT_FILENO, log_buf, out_len); if (log_buf[out_len - 1] != '\n') { - unused = write(STDOUT_FILENO, "\n", 1); + log_buf[out_len] = '\n'; + out_len++; } + unused = write(STDOUT_FILENO, log_buf, out_len); return len; } @@ -664,7 +674,7 @@ int tlog_vext(tlog_level level, const char *file, int line, const char *func, vo } if (tlog.root == NULL) { - return _tlog_early_print(format, ap); + return _tlog_early_print(level, file, line, func, format, ap); } if (unlikely(tlog.root->logsize <= 0)) { diff --git a/test/cases/test-bind.cc b/test/cases/test-bind.cc new file mode 100644 index 0000000000..3f35119faa --- /dev/null +++ b/test/cases/test-bind.cc @@ -0,0 +1,73 @@ +#include "client.h" +#include "include/utils.h" +#include "server.h" +#include "gtest/gtest.h" + +TEST(Bind, tls) +{ + Defer + { + unlink("/tmp/smartdns-cert.pem"); + unlink("/tmp/smartdns-key.pem"); + }; + + smartdns::Server server_wrap; + smartdns::Server server; + + server.Start(R"""(bind [::]:61053 +server-tls 127.0.0.1:60053 -no-check-certificate +log-num 0 +log-console yes +log-level debug +cache-persist no)"""); + server_wrap.Start(R"""(bind-tls [::]:60053 +address /example.com/1.2.3.4 +log-num 0 +log-console yes +log-level debug +cache-persist no)"""); + smartdns::Client client; + ASSERT_TRUE(client.Query("example.com", 61053)); + ASSERT_EQ(client.GetAnswerNum(), 1); + EXPECT_EQ(client.GetStatus(), "NOERROR"); + EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4"); +} + +TEST(Bind, udp_tcp) +{ + smartdns::MockServer server_upstream; + smartdns::MockServer server_upstream2; + smartdns::Server server; + + server_upstream.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) { + unsigned char addr[4] = {1, 2, 3, 4}; + dns_add_A(request->response_packet, DNS_RRS_AN, request->domain.c_str(), 611, addr); + request->response_packet->head.rcode = DNS_RC_NOERROR; + return true; + }); + + server.Start(R"""( +bind [::]:60053 +bind-tcp [::]:60053 +server 127.0.0.1:61053 +log-num 0 +log-console yes +log-level debug +cache-persist no)"""); + smartdns::Client client; + ASSERT_TRUE(client.Query("a.com +tcp", 60053)); + std::cout << client.GetResult() << std::endl; + ASSERT_EQ(client.GetAnswerNum(), 1); + EXPECT_EQ(client.GetStatus(), "NOERROR"); + 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].GetTTL(), 611); + EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4"); + +} + diff --git a/test/cases/test-discard-block-ip.cc b/test/cases/test-discard-block-ip.cc new file mode 100644 index 0000000000..7e267ffdb7 --- /dev/null +++ b/test/cases/test-discard-block-ip.cc @@ -0,0 +1,80 @@ +#include "client.h" +#include "dns.h" +#include "include/utils.h" +#include "server.h" +#include "gtest/gtest.h" + +TEST(DiscardBlockIP, first_ping) +{ + smartdns::MockServer server_upstream1; + smartdns::MockServer server_upstream2; + smartdns::Server server; + + server_upstream1.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) { + 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; + return true; + }); + + server_upstream2.Start("udp://0.0.0.0:62053", [](struct smartdns::ServerRequestContext *request) { + 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); + request->response_packet->head.rcode = DNS_RC_NOERROR; + return true; + }); + + server.Start(R"""(bind [::]:60053 +server 127.0.0.1:61053 +server 127.0.0.1:62053 +log-num 0 +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].GetTTL(), 600); + EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4"); +} + +TEST(DiscardBlockIP, first_response) +{ + smartdns::MockServer server_upstream1; + smartdns::MockServer server_upstream2; + smartdns::Server server; + + server_upstream1.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) { + 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; + return true; + }); + + server_upstream2.Start("udp://0.0.0.0:62053", [](struct smartdns::ServerRequestContext *request) { + 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); + request->response_packet->head.rcode = DNS_RC_NOERROR; + return true; + }); + + server.Start(R"""(bind [::]:60053 +server 127.0.0.1:61053 +server 127.0.0.1:62053 +log-num 0 +log-console yes +log-level debug +response-mode fastest-response +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].GetTTL(), 3); + EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4"); +} \ No newline at end of file diff --git a/test/cases/test-mock-server.cc b/test/cases/test-mock-server.cc index ccbb43870e..290e042cad 100644 --- a/test/cases/test-mock-server.cc +++ b/test/cases/test-mock-server.cc @@ -3,7 +3,7 @@ #include "server.h" #include "gtest/gtest.h" -TEST(server, mock) +TEST(MockServer, query_fail) { smartdns::MockServer server; smartdns::Client client; diff --git a/test/cases/test-ping.cc b/test/cases/test-ping.cc new file mode 100644 index 0000000000..bf9a947f7e --- /dev/null +++ b/test/cases/test-ping.cc @@ -0,0 +1,54 @@ +#include "client.h" +#include "fast_ping.h" +#include "include/utils.h" +#include "server.h" +#include "gtest/gtest.h" +#include "tlog.h" + +class Ping : public ::testing::Test +{ + protected: + virtual void SetUp() + { + EXPECT_EQ(fast_ping_init(), 0); + loglevel = tlog_getlevel(); + tlog_setlevel(TLOG_DEBUG); + } + virtual void TearDown() + { + fast_ping_exit(); + tlog_setlevel(loglevel); + } + private: + tlog_level loglevel; +}; + +void ping_result_callback(struct ping_host_struct *ping_host, const char *host, FAST_PING_RESULT result, + struct sockaddr *addr, socklen_t addr_len, int seqno, int ttl, struct timeval *tv, int error, + void *userptr) +{ + int *count = (int *)userptr; + *count = 1; +} + +TEST_F(Ping, DISABLED_icmp) +{ + struct ping_host_struct *ping_host; + int count = 0; + 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); + fast_ping_stop(ping_host); + EXPECT_EQ(count, 1); +} + +TEST_F(Ping, DISABLED_tcp) +{ + struct ping_host_struct *ping_host; + int count = 0; + ping_host = fast_ping_start(PING_TYPE_TCP, "127.0.0.1:1", 1, 1, 200, ping_result_callback, &count); + ASSERT_NE(ping_host, nullptr); + usleep(10000); + fast_ping_stop(ping_host); + EXPECT_EQ(count, 1); +} diff --git a/test/cases/test-tls-server.cc b/test/cases/test-tls-server.cc deleted file mode 100644 index 8bb59b8157..0000000000 --- a/test/cases/test-tls-server.cc +++ /dev/null @@ -1,50 +0,0 @@ -#include "client.h" -#include "include/utils.h" -#include "server.h" -#include "gtest/gtest.h" - -TEST(server, TLSServer) -{ - Defer - { - unlink("/tmp/smartdns-cert.pem"); - unlink("/tmp/smartdns-key.pem"); - }; - - smartdns::Server server_wrap; - smartdns::Server server; - - server.Start(R"""(bind [::]:61053 -server-tls 127.0.0.1:60053 -no-check-certificate -log-num 0 -log-console yes -log-level debug -cache-persist no)"""); - server_wrap.Start(R"""(bind-tls [::]:60053 -address /example.com/1.2.3.4 -log-num 0 -log-console yes -log-level debug -cache-persist no)"""); - smartdns::Client client; - ASSERT_TRUE(client.Query("example.com", 61053)); - ASSERT_EQ(client.GetAnswerNum(), 1); - EXPECT_EQ(client.GetStatus(), "NOERROR"); - EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4"); -} - -TEST(server, DISABLED_TLSCN) -{ - smartdns::Server server; - - server.Start(R"""(bind [::]:60053 -server tls://1.0.0.1:853 -log-num 0 -log-console yes -log-level debug -cache-persist no)"""); - smartdns::Client client; - ASSERT_TRUE(client.Query("www.example.com", 60053)); - EXPECT_EQ(client.GetStatus(), "NOERROR"); -} - diff --git a/test/client.cc b/test/client.cc index afb08f37d6..2f80856511 100644 --- a/test/client.cc +++ b/test/client.cc @@ -110,6 +110,8 @@ Client::Client() {} bool Client::Query(const std::string &dig_cmds, int port, const std::string &ip) { + Clear(); + std::string cmd = "dig "; if (port > 0) { cmd += "-p " + std::to_string(port); @@ -140,6 +142,7 @@ bool Client::Query(const std::string &dig_cmds, int port, const std::string &ip) if (ParserResult() == false) { Clear(); + return false; } return true;