test-nameserver.cc 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. class NameServer : public ::testing::Test
  24. {
  25. protected:
  26. virtual void SetUp() {}
  27. virtual void TearDown() {}
  28. };
  29. TEST_F(NameServer, cname)
  30. {
  31. smartdns::MockServer server_upstream;
  32. smartdns::MockServer server_upstream1;
  33. smartdns::MockServer server_upstream2;
  34. smartdns::Server server;
  35. server_upstream.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) {
  36. if (request->qtype != DNS_T_A) {
  37. return smartdns::SERVER_REQUEST_SOA;
  38. }
  39. smartdns::MockServer::AddIP(request, request->domain.c_str(), "9.10.11.12", 611);
  40. return smartdns::SERVER_REQUEST_OK;
  41. });
  42. server_upstream1.Start("udp://0.0.0.0:62053", [](struct smartdns::ServerRequestContext *request) {
  43. if (request->qtype != DNS_T_A) {
  44. return smartdns::SERVER_REQUEST_SOA;
  45. }
  46. smartdns::MockServer::AddIP(request, request->domain.c_str(), "1.2.3.4", 611);
  47. return smartdns::SERVER_REQUEST_OK;
  48. });
  49. server_upstream2.Start("udp://0.0.0.0:63053", [](struct smartdns::ServerRequestContext *request) {
  50. if (request->qtype != DNS_T_A) {
  51. return smartdns::SERVER_REQUEST_SOA;
  52. }
  53. smartdns::MockServer::AddIP(request, request->domain.c_str(), "5.6.7.8", 611);
  54. return smartdns::SERVER_REQUEST_OK;
  55. });
  56. server.Start(R"""(bind [::]:60053
  57. server 127.0.0.1:61053
  58. server 127.0.0.1:62053 -group g1 -exclude-default-group
  59. server 127.0.0.1:63053 -group g2 -exclude-default-group
  60. nameserver /a.com/g1
  61. nameserver /b.com/g2
  62. )""");
  63. smartdns::Client client;
  64. ASSERT_TRUE(client.Query("a.com", 60053));
  65. std::cout << client.GetResult() << std::endl;
  66. ASSERT_EQ(client.GetAnswerNum(), 1);
  67. EXPECT_EQ(client.GetStatus(), "NOERROR");
  68. EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
  69. EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
  70. ASSERT_TRUE(client.Query("b.com", 60053));
  71. std::cout << client.GetResult() << std::endl;
  72. ASSERT_EQ(client.GetAnswerNum(), 1);
  73. EXPECT_EQ(client.GetStatus(), "NOERROR");
  74. EXPECT_EQ(client.GetAnswer()[0].GetName(), "b.com");
  75. EXPECT_EQ(client.GetAnswer()[0].GetData(), "5.6.7.8");
  76. ASSERT_TRUE(client.Query("c.com", 60053));
  77. std::cout << client.GetResult() << std::endl;
  78. ASSERT_EQ(client.GetAnswerNum(), 1);
  79. EXPECT_EQ(client.GetStatus(), "NOERROR");
  80. EXPECT_EQ(client.GetAnswer()[0].GetName(), "c.com");
  81. EXPECT_EQ(client.GetAnswer()[0].GetData(), "9.10.11.12");
  82. }