test-server.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 "gtest/gtest.h"
  23. class Server : public ::testing::Test
  24. {
  25. protected:
  26. virtual void SetUp() {}
  27. virtual void TearDown() {}
  28. };
  29. TEST_F(Server, all_unreach)
  30. {
  31. smartdns::MockServer server_upstream;
  32. smartdns::Server server;
  33. server_upstream.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) {
  34. if (request->qtype != DNS_T_A) {
  35. return smartdns::SERVER_REQUEST_SOA;
  36. }
  37. smartdns::MockServer::AddIP(request, request->domain.c_str(), "1.2.3.4", 611);
  38. EXPECT_EQ(request->domain, "e.com");
  39. return smartdns::SERVER_REQUEST_OK;
  40. });
  41. server.MockPing(PING_TYPE_ICMP, "2001::", 128, 10000);
  42. server.Start(R"""(bind [::]:60053
  43. bind-tcp [::]:60053
  44. server tls://255.255.255.255
  45. server https://255.255.255.255
  46. server tcp://255.255.255.255
  47. )""");
  48. smartdns::Client client;
  49. ASSERT_TRUE(client.Query("a.com", 60053));
  50. std::cout << client.GetResult() << std::endl;
  51. EXPECT_EQ(client.GetStatus(), "SERVFAIL");
  52. EXPECT_EQ(client.GetAnswerNum(), 0);
  53. /* server should not crash */
  54. ASSERT_TRUE(client.Query("a.com +tcp", 60053));
  55. std::cout << client.GetResult() << std::endl;
  56. EXPECT_EQ(client.GetStatus(), "SERVFAIL");
  57. EXPECT_EQ(client.GetAnswerNum(), 0);
  58. }
  59. TEST_F(Server, one_nxdomain)
  60. {
  61. smartdns::MockServer server_upstream;
  62. smartdns::MockServer server_upstream1;
  63. smartdns::Server server;
  64. server_upstream.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) {
  65. if (request->qtype != DNS_T_A) {
  66. return smartdns::SERVER_REQUEST_SOA;
  67. }
  68. usleep(50000);
  69. smartdns::MockServer::AddIP(request, request->domain.c_str(), "1.2.3.4", 611);
  70. return smartdns::SERVER_REQUEST_OK;
  71. });
  72. server_upstream1.Start("udp://0.0.0.0:62053",
  73. [](struct smartdns::ServerRequestContext *request) { return smartdns::SERVER_REQUEST_SOA; });
  74. server.MockPing(PING_TYPE_ICMP, "2001::", 128, 10000);
  75. server.Start(R"""(bind [::]:60053
  76. bind-tcp [::]:60053
  77. server 127.0.0.1:61053
  78. server 127.0.0.1:62053
  79. )""");
  80. smartdns::Client client;
  81. ASSERT_TRUE(client.Query("a.com", 60053));
  82. std::cout << client.GetResult() << std::endl;
  83. ASSERT_EQ(client.GetAnswerNum(), 1);
  84. EXPECT_EQ(client.GetStatus(), "NOERROR");
  85. EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
  86. EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
  87. }
  88. TEST_F(Server, retry_no_result_with_NOERROR)
  89. {
  90. smartdns::MockServer server_upstream;
  91. smartdns::MockServer server_upstream1;
  92. smartdns::Server server;
  93. int count = 0;
  94. server_upstream.Start("udp://0.0.0.0:61053", [&](struct smartdns::ServerRequestContext *request) {
  95. if (request->qtype != DNS_T_A) {
  96. return smartdns::SERVER_REQUEST_SOA;
  97. }
  98. if (count++ < 2) {
  99. dns_add_domain(request->response_packet, request->domain.c_str(), request->qtype, request->qclass);
  100. request->response_packet->head.tc = 1;
  101. return smartdns::SERVER_REQUEST_OK;
  102. }
  103. smartdns::MockServer::AddIP(request, request->domain.c_str(), "1.2.3.4", 611);
  104. return smartdns::SERVER_REQUEST_OK;
  105. });
  106. server.Start(R"""(bind [::]:60053
  107. bind-tcp [::]:60053
  108. server 127.0.0.1:61053
  109. dualstack-ip-selection no
  110. )""");
  111. smartdns::Client client;
  112. ASSERT_TRUE(client.Query("a.com", 60053));
  113. std::cout << client.GetResult() << std::endl;
  114. ASSERT_EQ(client.GetAnswerNum(), 1);
  115. EXPECT_EQ(client.GetStatus(), "NOERROR");
  116. EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
  117. EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
  118. }
  119. TEST_F(Server, retry_no_response)
  120. {
  121. smartdns::MockServer server_upstream;
  122. smartdns::MockServer server_upstream1;
  123. smartdns::Server server;
  124. int count = 0;
  125. server_upstream.Start("udp://0.0.0.0:61053", [&](struct smartdns::ServerRequestContext *request) {
  126. count++;
  127. return smartdns::SERVER_REQUEST_NO_RESPONSE;
  128. });
  129. server.Start(R"""(bind [::]:60053
  130. bind-tcp [::]:60053
  131. server 127.0.0.1:61053
  132. )""");
  133. smartdns::Client client;
  134. ASSERT_TRUE(client.Query("a.com", 60053));
  135. std::cout << client.GetResult() << std::endl;
  136. ASSERT_EQ(client.GetAnswerNum(), 0);
  137. EXPECT_EQ(client.GetStatus(), "SERVFAIL");
  138. EXPECT_GE(client.GetQueryTime(), 1500);
  139. EXPECT_GE(count, 4);
  140. }
  141. TEST_F(Server, max_queries)
  142. {
  143. smartdns::MockServer server_upstream;
  144. smartdns::MockServer server_upstream1;
  145. smartdns::Server server;
  146. int count = 0;
  147. server_upstream.Start("udp://0.0.0.0:61053", [&](struct smartdns::ServerRequestContext *request) {
  148. smartdns::MockServer::AddIP(request, request->domain.c_str(), "1.2.3.4", 611);
  149. sleep(1);
  150. return smartdns::SERVER_REQUEST_OK;
  151. });
  152. server.MockPing(PING_TYPE_ICMP, "1.2.3.4", 128, 10);
  153. server.Start(R"""(bind [::]:60053
  154. bind-tcp [::]:60053
  155. server 127.0.0.1:61053
  156. dualstack-ip-selection no
  157. max-query-limit 2
  158. )""");
  159. std::vector<std::thread> threads;
  160. int success_num = 0;
  161. int refused_num = 0;
  162. for (int i = 0; i < 5; i++) {
  163. auto t = std::thread([&]() {
  164. smartdns::Client client;
  165. ASSERT_TRUE(client.Query("a.com", 60053));
  166. if (client.GetStatus() == "NOERROR") {
  167. success_num++;
  168. EXPECT_EQ(client.GetStatus(), "NOERROR");
  169. ASSERT_EQ(client.GetAnswerNum(), 1);
  170. EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
  171. EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
  172. } else if (client.GetStatus() == "REFUSED") {
  173. refused_num++;
  174. } else {
  175. FAIL();
  176. }
  177. });
  178. threads.push_back(std::move(t));
  179. }
  180. for (auto &t : threads) {
  181. t.join();
  182. }
  183. EXPECT_EQ(success_num, 2);
  184. EXPECT_EQ(refused_num, 3);
  185. for (int i = 0; i < 5; i++) {
  186. smartdns::Client client;
  187. ASSERT_TRUE(client.Query("a.com", 60053));
  188. EXPECT_EQ(client.GetStatus(), "NOERROR");
  189. ASSERT_EQ(client.GetAnswerNum(), 1);
  190. EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
  191. EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
  192. }
  193. }
  194. TEST_F(Server, interface)
  195. {
  196. smartdns::MockServer server_upstream;
  197. smartdns::Server server;
  198. server_upstream.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) {
  199. if (request->qtype != DNS_T_A) {
  200. return smartdns::SERVER_REQUEST_SOA;
  201. }
  202. smartdns::MockServer::AddIP(request, request->domain.c_str(), "1.2.3.4", 611);
  203. return smartdns::SERVER_REQUEST_OK;
  204. });
  205. server.MockPing(PING_TYPE_ICMP, "2001::", 128, 10000);
  206. server.Start(R"""(bind [::]:60053
  207. bind-tcp [::]:60053
  208. server 127.0.0.1:61053 -interface lo
  209. )""");
  210. smartdns::Client client;
  211. ASSERT_TRUE(client.Query("a.com", 60053));
  212. std::cout << client.GetResult() << std::endl;
  213. ASSERT_EQ(client.GetAnswerNum(), 1);
  214. EXPECT_EQ(client.GetStatus(), "NOERROR");
  215. EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
  216. EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
  217. }
  218. TEST_F(Server, refused)
  219. {
  220. smartdns::MockServer server_upstream;
  221. smartdns::MockServer server_upstream1;
  222. smartdns::Server server;
  223. int count = 0;
  224. server_upstream.Start("udp://0.0.0.0:61053", [&](struct smartdns::ServerRequestContext *request) {
  225. request->response_packet->head.rcode = DNS_RC_REFUSED;
  226. dns_add_domain(request->response_packet, request->domain.c_str(), request->qtype, request->qclass);
  227. return smartdns::SERVER_REQUEST_OK;
  228. });
  229. server.Start(R"""(bind [::]:60053
  230. bind-tcp [::]:60053
  231. server 127.0.0.1:61053
  232. dualstack-ip-selection no
  233. )""");
  234. smartdns::Client client;
  235. ASSERT_TRUE(client.Query("a.com", 60053));
  236. std::cout << client.GetResult() << std::endl;
  237. ASSERT_EQ(client.GetAnswerNum(), 0);
  238. EXPECT_EQ(client.GetStatus(), "REFUSED");
  239. EXPECT_LT(client.GetQueryTime(), 100);
  240. }