| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- /*************************************************************************
- *
- * Copyright (C) 2018-2025 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 "smartdns/dns.h"
- #include "include/utils.h"
- #include "server.h"
- #include "smartdns/util.h"
- #include "gtest/gtest.h"
- #include <fstream>
- class DNS64 : public ::testing::Test
- {
- protected:
- virtual void SetUp() {}
- virtual void TearDown() {}
- };
- TEST_F(DNS64, no_dualstack)
- {
- smartdns::MockServer server_upstream;
- smartdns::Server server;
- server_upstream.Start("udp://0.0.0.0:61053", [&](struct smartdns::ServerRequestContext *request) {
- if (request->qtype == DNS_T_A) {
- smartdns::MockServer::AddIP(request, request->domain.c_str(), "1.2.3.4");
- return smartdns::SERVER_REQUEST_OK;
- }
- return smartdns::SERVER_REQUEST_SOA;
- });
- server.Start(R"""(bind [::]:60053
- server 127.0.0.1:61053
- dns64 64:ff9b::/96
- dualstack-ip-selection no
- )""");
- smartdns::Client client;
- ASSERT_TRUE(client.Query("a.com AAAA", 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].GetType(), "AAAA");
- EXPECT_EQ(client.GetAnswer()[0].GetData(), "64:ff9b::102:304");
- }
- TEST_F(DNS64, with_dualstack)
- {
- smartdns::MockServer server_upstream;
- smartdns::Server server;
- server_upstream.Start("udp://0.0.0.0:61053", [&](struct smartdns::ServerRequestContext *request) {
- if (request->qtype == DNS_T_A) {
- smartdns::MockServer::AddIP(request, request->domain.c_str(), "1.2.3.4");
- return smartdns::SERVER_REQUEST_OK;
- }
- return smartdns::SERVER_REQUEST_SOA;
- });
- server.MockPing(PING_TYPE_ICMP, "1.2.3.4", 60, 200);
- server.Start(R"""(bind [::]:60053
- server 127.0.0.1:61053
- dns64 64:ff9b::/96
- )""");
- smartdns::Client client;
- ASSERT_TRUE(client.Query("a.com AAAA", 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].GetType(), "AAAA");
- EXPECT_EQ(client.GetAnswer()[0].GetData(), "64:ff9b::102:304");
- EXPECT_LT(client.GetQueryTime(), 100);
- usleep(500000);
- ASSERT_TRUE(client.Query("a.com AAAA", 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_GT(client.GetAnswer()[0].GetTTL(), 500);
- EXPECT_EQ(client.GetAnswer()[0].GetType(), "AAAA");
- EXPECT_EQ(client.GetAnswer()[0].GetData(), "64:ff9b::102:304");
- EXPECT_LT(client.GetQueryTime(), 100);
- }
- TEST_F(DNS64, with_AAAA_result)
- {
- smartdns::MockServer server_upstream;
- smartdns::Server server;
- server_upstream.Start("udp://0.0.0.0:61053", [&](struct smartdns::ServerRequestContext *request) {
- if (request->qtype == DNS_T_A) {
- smartdns::MockServer::AddIP(request, request->domain.c_str(), "1.2.3.4");
- return smartdns::SERVER_REQUEST_OK;
- }
- if (request->qtype == DNS_T_AAAA) {
- smartdns::MockServer::AddIP(request, request->domain.c_str(), "2001:db8::1");
- smartdns::MockServer::AddIP(request, request->domain.c_str(), "2001:db8::2");
- return smartdns::SERVER_REQUEST_OK;
- }
- return smartdns::SERVER_REQUEST_SOA;
- });
- server.MockPing(PING_TYPE_ICMP, "1.2.3.4", 60, 300);
- server.MockPing(PING_TYPE_ICMP, "2001:db8::1", 60, 90);
- server.MockPing(PING_TYPE_ICMP, "2001:db8::2", 60, 100);
- server.Start(R"""(bind [::]:60053
- server 127.0.0.1:61053
- dns64 64:ff9b::/96
- )""");
- smartdns::Client client;
- ASSERT_TRUE(client.Query("a.com AAAA", 60053));
- std::cout << client.GetResult() << std::endl;
- ASSERT_EQ(client.GetAnswerNum(), 1);
- EXPECT_EQ(client.GetStatus(), "NOERROR");
- EXPECT_LT(client.GetQueryTime(), 1200);
- EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
- EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 3);
- EXPECT_EQ(client.GetAnswer()[0].GetData(), "2001:db8::1");
- }
- TEST_F(DNS64, ipv4_in_ipv6)
- {
- smartdns::MockServer server_upstream;
- smartdns::Server server;
- server_upstream.Start("udp://0.0.0.0:61053", [&](struct smartdns::ServerRequestContext *request) {
- if (request->qtype == DNS_T_A) {
- return smartdns::SERVER_REQUEST_SOA;
- }
- if (request->qtype == DNS_T_AAAA) {
- smartdns::MockServer::AddIP(request, request->domain.c_str(), "::ffff:1.2.3.4");
- smartdns::MockServer::AddIP(request, request->domain.c_str(), "2001:db8::1");
- return smartdns::SERVER_REQUEST_OK;
- }
- return smartdns::SERVER_REQUEST_SOA;
- });
- server.MockPing(PING_TYPE_ICMP, "::ffff:1.2.3.4", 60, 10);
- server.MockPing(PING_TYPE_ICMP, "2001:db8::1", 60, 90);
- server.Start(R"""(bind [::]:60053
- server 127.0.0.1:61053
- )""");
- smartdns::Client client;
- ASSERT_TRUE(client.Query("a.com AAAA", 60053));
- std::cout << client.GetResult() << std::endl;
- ASSERT_EQ(client.GetAnswerNum(), 1);
- EXPECT_EQ(client.GetStatus(), "NOERROR");
- EXPECT_LT(client.GetQueryTime(), 1200);
- EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
- EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 600);
- EXPECT_EQ(client.GetAnswer()[0].GetData(), "::ffff:1.2.3.4");
- }
|