test-domain-set.cc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 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. domain-rules /domain-set:test-set/ -c none --dualstack-ip-selection no -a 9.9.9.9
  42. )""";
  43. server_upstream.Start("udp://0.0.0.0:61053", [&](struct smartdns::ServerRequestContext *request) {
  44. if (request->qtype == DNS_T_A) {
  45. smartdns::MockServer::AddIP(request, request->domain.c_str(), "1.2.3.4");
  46. return smartdns::SERVER_REQUEST_OK;
  47. }
  48. return smartdns::SERVER_REQUEST_SOA;
  49. });
  50. for (int i = 0; i < count; i++) {
  51. auto domain = smartdns::GenerateRandomString(10) + "." + smartdns::GenerateRandomString(3);
  52. file_set.Write(domain);
  53. file_set.Write("\n");
  54. domain_list.emplace_back(domain);
  55. }
  56. std::cout << config << std::endl;
  57. server.Start(config);
  58. smartdns::Client client;
  59. for (auto &domain : domain_list) {
  60. ASSERT_TRUE(client.Query(domain, 60053));
  61. ASSERT_EQ(client.GetAnswerNum(), 1);
  62. EXPECT_EQ(client.GetStatus(), "NOERROR");
  63. EXPECT_EQ(client.GetAnswer()[0].GetName(), domain);
  64. EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 600);
  65. EXPECT_EQ(client.GetAnswer()[0].GetType(), "A");
  66. EXPECT_EQ(client.GetAnswer()[0].GetData(), "9.9.9.9");
  67. }
  68. ASSERT_TRUE(client.Query("a.com", 60053));
  69. ASSERT_EQ(client.GetAnswerNum(), 1);
  70. EXPECT_EQ(client.GetStatus(), "NOERROR");
  71. EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
  72. EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 3);
  73. EXPECT_EQ(client.GetAnswer()[0].GetType(), "A");
  74. EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
  75. }