test-bind.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 "include/utils.h"
  20. #include "server.h"
  21. #include "gtest/gtest.h"
  22. TEST(Bind, tls)
  23. {
  24. Defer
  25. {
  26. unlink("/tmp/smartdns-cert.pem");
  27. unlink("/tmp/smartdns-key.pem");
  28. };
  29. smartdns::Server server_wrap;
  30. smartdns::Server server;
  31. server.Start(R"""(bind [::]:61053
  32. server-tls 127.0.0.1:60053 -no-check-certificate
  33. log-num 0
  34. log-console yes
  35. log-level debug
  36. cache-persist no)""");
  37. server_wrap.Start(R"""(bind-tls [::]:60053
  38. address /example.com/1.2.3.4
  39. log-num 0
  40. log-console yes
  41. log-level debug
  42. cache-persist no)""");
  43. smartdns::Client client;
  44. ASSERT_TRUE(client.Query("example.com", 61053));
  45. ASSERT_EQ(client.GetAnswerNum(), 1);
  46. EXPECT_EQ(client.GetStatus(), "NOERROR");
  47. EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
  48. }
  49. TEST(Bind, udp_tcp)
  50. {
  51. smartdns::MockServer server_upstream;
  52. smartdns::MockServer server_upstream2;
  53. smartdns::Server server;
  54. server_upstream.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) {
  55. unsigned char addr[4] = {1, 2, 3, 4};
  56. dns_add_A(request->response_packet, DNS_RRS_AN, request->domain.c_str(), 611, addr);
  57. request->response_packet->head.rcode = DNS_RC_NOERROR;
  58. return smartdns::SERVER_REQUEST_OK;
  59. });
  60. server.Start(R"""(
  61. bind [::]:60053
  62. bind-tcp [::]:60053
  63. server 127.0.0.1:61053
  64. log-num 0
  65. log-console yes
  66. log-level debug
  67. cache-persist no)""");
  68. smartdns::Client client;
  69. ASSERT_TRUE(client.Query("a.com +tcp", 60053));
  70. std::cout << client.GetResult() << std::endl;
  71. ASSERT_EQ(client.GetAnswerNum(), 1);
  72. EXPECT_EQ(client.GetStatus(), "NOERROR");
  73. EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 3);
  74. EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
  75. ASSERT_TRUE(client.Query("a.com", 60053));
  76. std::cout << client.GetResult() << std::endl;
  77. ASSERT_EQ(client.GetAnswerNum(), 1);
  78. EXPECT_EQ(client.GetStatus(), "NOERROR");
  79. EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 611);
  80. EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
  81. }
  82. TEST(Bind, self)
  83. {
  84. smartdns::MockServer server_upstream;
  85. smartdns::Server server;
  86. server_upstream.Start("udp://0.0.0.0:62053", [](struct smartdns::ServerRequestContext *request) {
  87. if (request->qtype == DNS_T_A) {
  88. smartdns::MockServer::AddIP(request, request->domain.c_str(), "1.2.3.4");
  89. return smartdns::SERVER_REQUEST_OK;
  90. }
  91. return smartdns::SERVER_REQUEST_SOA;
  92. });
  93. server.Start(R"""(
  94. bind [::]:60053 -group self
  95. server 127.0.0.1:61053 -group self
  96. bind [::]:61053 -group upstream
  97. server 127.0.0.1:62053 -group upstream
  98. log-num 0
  99. log-console yes
  100. log-level info
  101. cache-persist no)""");
  102. smartdns::Client client;
  103. ASSERT_TRUE(client.Query("a.com", 60053));
  104. std::cout << client.GetResult() << std::endl;
  105. ASSERT_EQ(client.GetAnswerNum(), 1);
  106. EXPECT_EQ(client.GetStatus(), "NOERROR");
  107. EXPECT_LT(client.GetQueryTime(), 100);
  108. EXPECT_EQ(client.GetAnswer()[0].GetTTL(), 3);
  109. EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
  110. }