test-ip-rule.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*************************************************************************
  2. *
  3. * Copyright (C) 2018-2023 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 IPRule : public ::testing::Test
  24. {
  25. protected:
  26. virtual void SetUp() {}
  27. virtual void TearDown() {}
  28. };
  29. TEST_F(IPRule, white_list)
  30. {
  31. smartdns::MockServer server_upstream;
  32. smartdns::MockServer server_upstream2;
  33. smartdns::Server server;
  34. server_upstream.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) {
  35. if (request->qtype != DNS_T_A) {
  36. return smartdns::SERVER_REQUEST_SOA;
  37. }
  38. smartdns::MockServer::AddIP(request, request->domain.c_str(), "1.2.3.4", 611);
  39. return smartdns::SERVER_REQUEST_OK;
  40. });
  41. server_upstream2.Start("udp://0.0.0.0:62053", [](struct smartdns::ServerRequestContext *request) {
  42. if (request->qtype != DNS_T_A) {
  43. return smartdns::SERVER_REQUEST_SOA;
  44. }
  45. smartdns::MockServer::AddIP(request, request->domain.c_str(), "4.5.6.7", 611);
  46. return smartdns::SERVER_REQUEST_OK;
  47. });
  48. /* this ip will be discard, but is reachable */
  49. server.MockPing(PING_TYPE_ICMP, "1.2.3.4", 60, 10);
  50. server.Start(R"""(bind [::]:60053
  51. server udp://127.0.0.1:61053 -whitelist-ip
  52. server udp://127.0.0.1:62053 -whitelist-ip
  53. whitelist-ip 4.5.6.7/24
  54. log-num 0
  55. log-console yes
  56. log-level debug
  57. cache-persist no)""");
  58. smartdns::Client client;
  59. ASSERT_TRUE(client.Query("a.com", 60053));
  60. std::cout << client.GetResult() << std::endl;
  61. ASSERT_EQ(client.GetAnswerNum(), 1);
  62. EXPECT_EQ(client.GetStatus(), "NOERROR");
  63. EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
  64. EXPECT_EQ(client.GetAnswer()[0].GetData(), "4.5.6.7");
  65. }
  66. TEST_F(IPRule, black_list)
  67. {
  68. smartdns::MockServer server_upstream;
  69. smartdns::MockServer server_upstream2;
  70. smartdns::Server server;
  71. server_upstream.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) {
  72. if (request->qtype != DNS_T_A) {
  73. return smartdns::SERVER_REQUEST_SOA;
  74. }
  75. smartdns::MockServer::AddIP(request, request->domain.c_str(), "1.2.3.4", 611);
  76. return smartdns::SERVER_REQUEST_OK;
  77. });
  78. server_upstream2.Start("udp://0.0.0.0:62053", [](struct smartdns::ServerRequestContext *request) {
  79. if (request->qtype != DNS_T_A) {
  80. return smartdns::SERVER_REQUEST_SOA;
  81. }
  82. smartdns::MockServer::AddIP(request, request->domain.c_str(), "4.5.6.7", 611);
  83. return smartdns::SERVER_REQUEST_OK;
  84. });
  85. /* this ip will be discard, but is reachable */
  86. server.MockPing(PING_TYPE_ICMP, "4.5.6.7", 60, 10);
  87. server.Start(R"""(bind [::]:60053
  88. server udp://127.0.0.1:61053 -blacklist-ip
  89. server udp://127.0.0.1:62053 -blacklist-ip
  90. blacklist-ip 4.5.6.7/24
  91. log-num 0
  92. log-console yes
  93. log-level debug
  94. cache-persist no)""");
  95. smartdns::Client client;
  96. ASSERT_TRUE(client.Query("a.com", 60053));
  97. std::cout << client.GetResult() << std::endl;
  98. ASSERT_EQ(client.GetAnswerNum(), 1);
  99. EXPECT_EQ(client.GetStatus(), "NOERROR");
  100. EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
  101. EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
  102. }
  103. TEST_F(IPRule, ignore_ip)
  104. {
  105. smartdns::MockServer server_upstream;
  106. smartdns::MockServer server_upstream2;
  107. smartdns::Server server;
  108. server_upstream.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) {
  109. if (request->qtype != DNS_T_A) {
  110. return smartdns::SERVER_REQUEST_SOA;
  111. }
  112. smartdns::MockServer::AddIP(request, request->domain.c_str(), "1.2.3.4", 611);
  113. smartdns::MockServer::AddIP(request, request->domain.c_str(), "4.5.6.7", 611);
  114. smartdns::MockServer::AddIP(request, request->domain.c_str(), "7.8.9.10", 611);
  115. return smartdns::SERVER_REQUEST_OK;
  116. });
  117. /* this ip will be discard, but is reachable */
  118. server.MockPing(PING_TYPE_ICMP, "1.2.3.4", 60, 10);
  119. server.MockPing(PING_TYPE_ICMP, "4.5.6.7", 60, 90);
  120. server.MockPing(PING_TYPE_ICMP, "7.8.9.10", 60, 40);
  121. server.Start(R"""(bind [::]:60053
  122. server udp://127.0.0.1:61053 -blacklist-ip
  123. ignore-ip 1.2.3.0/24
  124. log-num 0
  125. log-console yes
  126. log-level debug
  127. cache-persist no)""");
  128. smartdns::Client client;
  129. ASSERT_TRUE(client.Query("a.com", 60053));
  130. std::cout << client.GetResult() << std::endl;
  131. ASSERT_EQ(client.GetAnswerNum(), 1);
  132. EXPECT_EQ(client.GetStatus(), "NOERROR");
  133. EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
  134. EXPECT_EQ(client.GetAnswer()[0].GetData(), "7.8.9.10");
  135. }