test-srv.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 "smartdns/util.h"
  23. #include "gtest/gtest.h"
  24. #include <fstream>
  25. class SRV : public ::testing::Test
  26. {
  27. protected:
  28. virtual void SetUp() {}
  29. virtual void TearDown() {}
  30. };
  31. TEST_F(SRV, query)
  32. {
  33. smartdns::MockServer server_upstream;
  34. smartdns::Server server;
  35. server_upstream.Start("udp://0.0.0.0:61053", [&](struct smartdns::ServerRequestContext *request) {
  36. if (request->qtype != DNS_T_SRV) {
  37. return smartdns::SERVER_REQUEST_SOA;
  38. }
  39. struct dns_packet *packet = request->response_packet;
  40. dns_add_SRV(packet, DNS_RRS_AN, request->domain.c_str(), 603, 1, 1, 443, "www.example.com");
  41. dns_add_SRV(packet, DNS_RRS_AN, request->domain.c_str(), 603, 1, 1, 443, "www1.example.com");
  42. return smartdns::SERVER_REQUEST_OK;
  43. });
  44. server.Start(R"""(bind [::]:60053
  45. server 127.0.0.1:61053
  46. speed-check-mode none
  47. )""");
  48. smartdns::Client client;
  49. ASSERT_TRUE(client.Query("_ldap._tcp.local.com SRV", 60053));
  50. std::cout << client.GetResult() << std::endl;
  51. ASSERT_EQ(client.GetAnswerNum(), 2);
  52. EXPECT_EQ(client.GetStatus(), "NOERROR");
  53. EXPECT_EQ(client.GetAnswer()[0].GetName(), "_ldap._tcp.local.com");
  54. EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 603);
  55. EXPECT_EQ(client.GetAnswer()[0].GetType(), "SRV");
  56. EXPECT_EQ(client.GetAnswer()[0].GetData(), "1 1 443 www.example.com.");
  57. }
  58. TEST_F(SRV, match)
  59. {
  60. smartdns::MockServer server_upstream;
  61. smartdns::Server server;
  62. server_upstream.Start("udp://0.0.0.0:61053", [&](struct smartdns::ServerRequestContext *request) {
  63. if (request->qtype != DNS_T_SRV) {
  64. return smartdns::SERVER_REQUEST_SOA;
  65. }
  66. struct dns_packet *packet = request->response_packet;
  67. dns_add_SRV(packet, DNS_RRS_AN, request->domain.c_str(), 603, 1, 1, 443, "www.example.com");
  68. dns_add_SRV(packet, DNS_RRS_AN, request->domain.c_str(), 603, 1, 1, 443, "www1.example.com");
  69. return smartdns::SERVER_REQUEST_OK;
  70. });
  71. server.Start(R"""(bind [::]:60053
  72. server 127.0.0.1:61053
  73. srv-record /_ldap._tcp.local.com/www.a.com,443,1,1
  74. srv-record /_ldap._tcp.local.com/www1.a.com,443,1,1
  75. srv-record /_ldap._tcp.local.com/www2.a.com,443,1,1
  76. speed-check-mode none
  77. )""");
  78. smartdns::Client client;
  79. ASSERT_TRUE(client.Query("_ldap._tcp.local.com SRV", 60053));
  80. std::cout << client.GetResult() << std::endl;
  81. ASSERT_EQ(client.GetAnswerNum(), 3);
  82. EXPECT_EQ(client.GetStatus(), "NOERROR");
  83. EXPECT_EQ(client.GetAnswer()[0].GetName(), "_ldap._tcp.local.com");
  84. EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 600);
  85. EXPECT_EQ(client.GetAnswer()[0].GetType(), "SRV");
  86. EXPECT_EQ(client.GetAnswer()[0].GetData(), "1 1 443 www.a.com.");
  87. }