test-dns64.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*************************************************************************
  2. *
  3. * Copyright (C) 2018-2024 Ruilin Peng (Nick) <[email protected]>.
  4. *
  5. * smartdns is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * smartdns is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "client.h"
  19. #include "dns.h"
  20. #include "include/utils.h"
  21. #include "server.h"
  22. #include "util.h"
  23. #include "gtest/gtest.h"
  24. #include <fstream>
  25. class DNS64 : public ::testing::Test
  26. {
  27. protected:
  28. virtual void SetUp() {}
  29. virtual void TearDown() {}
  30. };
  31. TEST_F(DNS64, no_dualstack)
  32. {
  33. smartdns::MockServer server_upstream;
  34. smartdns::Server server;
  35. server_upstream.Start("udp://0.0.0.0:61053", [&](struct smartdns::ServerRequestContext *request) {
  36. if (request->qtype == DNS_T_A) {
  37. smartdns::MockServer::AddIP(request, request->domain.c_str(), "1.2.3.4");
  38. return smartdns::SERVER_REQUEST_OK;
  39. }
  40. return smartdns::SERVER_REQUEST_SOA;
  41. });
  42. server.Start(R"""(bind [::]:60053
  43. server 127.0.0.1:61053
  44. dns64 64:ff9b::/96
  45. dualstack-ip-selection no
  46. )""");
  47. smartdns::Client client;
  48. ASSERT_TRUE(client.Query("a.com AAAA", 60053));
  49. std::cout << client.GetResult() << std::endl;
  50. ASSERT_EQ(client.GetAnswerNum(), 1);
  51. EXPECT_EQ(client.GetStatus(), "NOERROR");
  52. EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
  53. EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 3);
  54. EXPECT_EQ(client.GetAnswer()[0].GetType(), "AAAA");
  55. EXPECT_EQ(client.GetAnswer()[0].GetData(), "64:ff9b::102:304");
  56. }
  57. TEST_F(DNS64, with_dualstack)
  58. {
  59. smartdns::MockServer server_upstream;
  60. smartdns::Server server;
  61. server_upstream.Start("udp://0.0.0.0:61053", [&](struct smartdns::ServerRequestContext *request) {
  62. if (request->qtype == DNS_T_A) {
  63. smartdns::MockServer::AddIP(request, request->domain.c_str(), "1.2.3.4");
  64. return smartdns::SERVER_REQUEST_OK;
  65. }
  66. return smartdns::SERVER_REQUEST_SOA;
  67. });
  68. server.MockPing(PING_TYPE_ICMP, "1.2.3.4", 60, 200);
  69. server.Start(R"""(bind [::]:60053
  70. server 127.0.0.1:61053
  71. dns64 64:ff9b::/96
  72. )""");
  73. smartdns::Client client;
  74. ASSERT_TRUE(client.Query("a.com AAAA", 60053));
  75. std::cout << client.GetResult() << std::endl;
  76. ASSERT_EQ(client.GetAnswerNum(), 1);
  77. EXPECT_EQ(client.GetStatus(), "NOERROR");
  78. EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
  79. EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 3);
  80. EXPECT_EQ(client.GetAnswer()[0].GetType(), "AAAA");
  81. EXPECT_EQ(client.GetAnswer()[0].GetData(), "64:ff9b::102:304");
  82. EXPECT_LT(client.GetQueryTime(), 100);
  83. usleep(500000);
  84. ASSERT_TRUE(client.Query("a.com AAAA", 60053));
  85. std::cout << client.GetResult() << std::endl;
  86. ASSERT_EQ(client.GetAnswerNum(), 1);
  87. EXPECT_EQ(client.GetStatus(), "NOERROR");
  88. EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
  89. EXPECT_GT(client.GetAnswer()[0].GetTTL(), 500);
  90. EXPECT_EQ(client.GetAnswer()[0].GetType(), "AAAA");
  91. EXPECT_EQ(client.GetAnswer()[0].GetData(), "64:ff9b::102:304");
  92. EXPECT_LT(client.GetQueryTime(), 100);
  93. }
  94. TEST_F(DNS64, with_AAAA_result)
  95. {
  96. smartdns::MockServer server_upstream;
  97. smartdns::Server server;
  98. server_upstream.Start("udp://0.0.0.0:61053", [&](struct smartdns::ServerRequestContext *request) {
  99. if (request->qtype == DNS_T_A) {
  100. smartdns::MockServer::AddIP(request, request->domain.c_str(), "1.2.3.4");
  101. return smartdns::SERVER_REQUEST_OK;
  102. }
  103. if (request->qtype == DNS_T_AAAA) {
  104. smartdns::MockServer::AddIP(request, request->domain.c_str(), "2001:db8::1");
  105. smartdns::MockServer::AddIP(request, request->domain.c_str(), "2001:db8::2");
  106. return smartdns::SERVER_REQUEST_OK;
  107. }
  108. return smartdns::SERVER_REQUEST_SOA;
  109. });
  110. server.MockPing(PING_TYPE_ICMP, "1.2.3.4", 60, 300);
  111. server.MockPing(PING_TYPE_ICMP, "2001:db8::1", 60, 90);
  112. server.MockPing(PING_TYPE_ICMP, "2001:db8::2", 60, 100);
  113. server.Start(R"""(bind [::]:60053
  114. server 127.0.0.1:61053
  115. dns64 64:ff9b::/96
  116. )""");
  117. smartdns::Client client;
  118. ASSERT_TRUE(client.Query("a.com AAAA", 60053));
  119. std::cout << client.GetResult() << std::endl;
  120. ASSERT_EQ(client.GetAnswerNum(), 1);
  121. EXPECT_EQ(client.GetStatus(), "NOERROR");
  122. EXPECT_LT(client.GetQueryTime(), 1200);
  123. EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
  124. EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 3);
  125. EXPECT_EQ(client.GetAnswer()[0].GetData(), "2001:db8::1");
  126. }