test-discard-block-ip.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*************************************************************************
  2. *
  3. * Copyright (C) 2018-2025 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 "smartdns/dns.h"
  20. #include "include/utils.h"
  21. #include "server.h"
  22. #include "gtest/gtest.h"
  23. TEST(DiscardBlockIP, first_ping)
  24. {
  25. smartdns::MockServer server_upstream1;
  26. smartdns::MockServer server_upstream2;
  27. smartdns::Server server;
  28. server_upstream1.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) {
  29. if (request->qtype != DNS_T_A) {
  30. return smartdns::SERVER_REQUEST_SOA;
  31. }
  32. unsigned char addr[4] = {0, 0, 0, 0};
  33. dns_add_A(request->response_packet, DNS_RRS_AN, request->domain.c_str(), 611, addr);
  34. request->response_packet->head.rcode = DNS_RC_NOERROR;
  35. return smartdns::SERVER_REQUEST_OK;
  36. });
  37. server_upstream2.Start("udp://0.0.0.0:62053", [](struct smartdns::ServerRequestContext *request) {
  38. if (request->qtype != DNS_T_A) {
  39. return smartdns::SERVER_REQUEST_SOA;
  40. }
  41. unsigned char addr[4] = {1, 2, 3, 4};
  42. usleep(20000);
  43. dns_add_A(request->response_packet, DNS_RRS_AN, request->domain.c_str(), 611, addr);
  44. request->response_packet->head.rcode = DNS_RC_NOERROR;
  45. return smartdns::SERVER_REQUEST_OK;
  46. });
  47. server.Start(R"""(bind [::]:60053
  48. server 127.0.0.1:61053
  49. server 127.0.0.1:62053
  50. )""");
  51. smartdns::Client client;
  52. ASSERT_TRUE(client.Query("a.com", 60053));
  53. std::cout << client.GetResult() << std::endl;
  54. ASSERT_EQ(client.GetAnswerNum(), 1);
  55. EXPECT_EQ(client.GetStatus(), "NOERROR");
  56. EXPECT_LT(client.GetQueryTime(), 100);
  57. EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 3);
  58. EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
  59. }
  60. TEST(DiscardBlockIP, first_response)
  61. {
  62. smartdns::MockServer server_upstream1;
  63. smartdns::MockServer server_upstream2;
  64. smartdns::Server server;
  65. server_upstream1.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) {
  66. unsigned char addr[4] = {0, 0, 0, 0};
  67. dns_add_A(request->response_packet, DNS_RRS_AN, request->domain.c_str(), 611, addr);
  68. request->response_packet->head.rcode = DNS_RC_NOERROR;
  69. return smartdns::SERVER_REQUEST_OK;
  70. });
  71. server_upstream2.Start("udp://0.0.0.0:62053", [](struct smartdns::ServerRequestContext *request) {
  72. unsigned char addr[4] = {1, 2, 3, 4};
  73. usleep(20000);
  74. dns_add_A(request->response_packet, DNS_RRS_AN, request->domain.c_str(), 611, addr);
  75. request->response_packet->head.rcode = DNS_RC_NOERROR;
  76. return smartdns::SERVER_REQUEST_OK;
  77. });
  78. server.Start(R"""(bind [::]:60053
  79. server 127.0.0.1:61053
  80. server 127.0.0.1:62053
  81. response-mode fastest-response
  82. )""");
  83. smartdns::Client client;
  84. ASSERT_TRUE(client.Query("a.com", 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].GetTTL(), 3);
  89. EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
  90. }