test-idna.cc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 IDNA : public ::testing::Test
  26. {
  27. protected:
  28. virtual void SetUp() {}
  29. virtual void TearDown() {}
  30. };
  31. TEST_F(IDNA, match)
  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_A) {
  37. smartdns::MockServer::AddIP(request, request->domain.c_str(), "1.2.3.4", 700);
  38. return smartdns::SERVER_REQUEST_OK;
  39. } else if (request->qtype == DNS_T_AAAA) {
  40. smartdns::MockServer::AddIP(request, request->domain.c_str(), "64:ff9b::102:304", 700);
  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. speed-check-mode none
  48. address /中国.com/10.10.10.10
  49. address /中国.com/64:ff9b::1010:1010
  50. )""");
  51. smartdns::Client client;
  52. ASSERT_TRUE(client.Query("xn--fiqs8s.com A", 60053));
  53. std::cout << client.GetResult() << std::endl;
  54. ASSERT_EQ(client.GetAnswerNum(), 1);
  55. EXPECT_EQ(client.GetStatus(), "NOERROR");
  56. EXPECT_EQ(client.GetAnswer()[0].GetName(), "xn--fiqs8s.com");
  57. EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 600);
  58. EXPECT_EQ(client.GetAnswer()[0].GetType(), "A");
  59. EXPECT_EQ(client.GetAnswer()[0].GetData(), "10.10.10.10");
  60. ASSERT_TRUE(client.Query("xn--fiqs8s.com AAAA", 60053));
  61. std::cout << client.GetResult() << std::endl;
  62. ASSERT_EQ(client.GetAnswerNum(), 1);
  63. EXPECT_EQ(client.GetStatus(), "NOERROR");
  64. EXPECT_EQ(client.GetAnswer()[0].GetName(), "xn--fiqs8s.com");
  65. EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 600);
  66. EXPECT_EQ(client.GetAnswer()[0].GetType(), "AAAA");
  67. EXPECT_EQ(client.GetAnswer()[0].GetData(), "64:ff9b::1010:1010");
  68. }