test-qtype-soa.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 QtypeSOA : public ::testing::Test
  24. {
  25. protected:
  26. virtual void SetUp() {}
  27. virtual void TearDown() {}
  28. };
  29. TEST_F(QtypeSOA, AAAA_HTTPS)
  30. {
  31. smartdns::MockServer server_upstream;
  32. smartdns::Server server;
  33. server_upstream.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) {
  34. if (request->qtype == DNS_T_A) {
  35. smartdns::MockServer::AddIP(request, request->domain.c_str(), "1.2.3.4");
  36. smartdns::MockServer::AddIP(request, request->domain.c_str(), "5.6.7.8");
  37. return smartdns::SERVER_REQUEST_OK;
  38. } else if (request->qtype == DNS_T_AAAA) {
  39. smartdns::MockServer::AddIP(request, request->domain.c_str(), "2001:db8::1");
  40. smartdns::MockServer::AddIP(request, request->domain.c_str(), "2001:db8::2");
  41. return smartdns::SERVER_REQUEST_OK;
  42. }
  43. return smartdns::SERVER_REQUEST_SOA;
  44. });
  45. server.Start(R"""(bind [::]:60053
  46. server 127.0.0.1:61053
  47. log-num 0
  48. log-console yes
  49. log-level debug
  50. force-qtype-SOA 28 65
  51. cache-persist no)""");
  52. smartdns::Client client;
  53. ASSERT_TRUE(client.Query("a.com AAAA", 60053));
  54. std::cout << client.GetResult() << std::endl;
  55. ASSERT_EQ(client.GetAuthorityNum(), 1);
  56. EXPECT_EQ(client.GetStatus(), "NOERROR");
  57. EXPECT_EQ(client.GetAuthority()[0].GetName(), "a.com");
  58. EXPECT_EQ(client.GetAuthority()[0].GetTTL(), 30);
  59. EXPECT_EQ(client.GetAuthority()[0].GetType(), "SOA");
  60. ASSERT_TRUE(client.Query("a.com -t HTTPS", 60053));
  61. std::cout << client.GetResult() << std::endl;
  62. ASSERT_EQ(client.GetAuthorityNum(), 1);
  63. EXPECT_EQ(client.GetStatus(), "NOERROR");
  64. EXPECT_EQ(client.GetAuthority()[0].GetName(), "a.com");
  65. EXPECT_EQ(client.GetAuthority()[0].GetTTL(), 30);
  66. EXPECT_EQ(client.GetAuthority()[0].GetType(), "SOA");
  67. ASSERT_TRUE(client.Query("a.com A", 60053));
  68. ASSERT_EQ(client.GetAnswerNum(), 1);
  69. EXPECT_EQ(client.GetStatus(), "NOERROR");
  70. EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
  71. }