test-domain-set.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 "util.h"
  23. #include "gtest/gtest.h"
  24. #include <fstream>
  25. class DomainSet : public ::testing::Test
  26. {
  27. protected:
  28. virtual void SetUp() {}
  29. virtual void TearDown() {}
  30. };
  31. TEST_F(DomainSet, set_add)
  32. {
  33. smartdns::MockServer server_upstream;
  34. smartdns::Server server;
  35. smartdns::TempFile file_set;
  36. std::vector<std::string> domain_list;
  37. int count = 16;
  38. std::string config = "domain-set -name test-set -file " + file_set.GetPath() + "\n";
  39. config += R"""(bind [::]:60053
  40. server 127.0.0.1:61053
  41. log-num 0
  42. log-console yes
  43. log-level info
  44. cache-persist no
  45. domain-rules /domain-set:test-set/ -c none --dualstack-ip-selection no -a 9.9.9.9
  46. )""";
  47. server_upstream.Start("udp://0.0.0.0:61053", [&](struct smartdns::ServerRequestContext *request) {
  48. if (request->qtype == DNS_T_A) {
  49. smartdns::MockServer::AddIP(request, request->domain.c_str(), "1.2.3.4");
  50. return smartdns::SERVER_REQUEST_OK;
  51. }
  52. return smartdns::SERVER_REQUEST_SOA;
  53. });
  54. for (int i = 0; i < count; i++) {
  55. auto domain = smartdns::GenerateRandomString(10) + "." + smartdns::GenerateRandomString(3);
  56. file_set.Write(domain);
  57. file_set.Write("\n");
  58. domain_list.emplace_back(domain);
  59. }
  60. std::cout << config << std::endl;
  61. server.Start(config);
  62. smartdns::Client client;
  63. for (auto &domain : domain_list) {
  64. ASSERT_TRUE(client.Query(domain, 60053));
  65. ASSERT_EQ(client.GetAnswerNum(), 1);
  66. EXPECT_EQ(client.GetStatus(), "NOERROR");
  67. EXPECT_EQ(client.GetAnswer()[0].GetName(), domain);
  68. EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 600);
  69. EXPECT_EQ(client.GetAnswer()[0].GetType(), "A");
  70. EXPECT_EQ(client.GetAnswer()[0].GetData(), "9.9.9.9");
  71. }
  72. ASSERT_TRUE(client.Query("a.com", 60053));
  73. ASSERT_EQ(client.GetAnswerNum(), 1);
  74. EXPECT_EQ(client.GetStatus(), "NOERROR");
  75. EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
  76. EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 3);
  77. EXPECT_EQ(client.GetAnswer()[0].GetType(), "A");
  78. EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
  79. }