test-cname.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*************************************************************************
  2. *
  3. * Copyright (C) 2018-2024 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 Cname : public ::testing::Test
  24. {
  25. protected:
  26. virtual void SetUp() {}
  27. virtual void TearDown() {}
  28. };
  29. TEST_F(Cname, cname)
  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. return smartdns::SERVER_REQUEST_SOA;
  36. }
  37. smartdns::MockServer::AddIP(request, request->domain.c_str(), "1.2.3.4", 611);
  38. EXPECT_EQ(request->domain, "e.com");
  39. return smartdns::SERVER_REQUEST_OK;
  40. });
  41. server.Start(R"""(bind [::]:60053
  42. cname /a.com/b.com
  43. cname /b.com/c.com
  44. cname /c.com/d.com
  45. cname /d.com/e.com
  46. server 127.0.0.1:61053
  47. )""");
  48. smartdns::Client client;
  49. ASSERT_TRUE(client.Query("a.com", 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(), "a.com");
  54. EXPECT_EQ(client.GetAnswer()[0].GetData(), "b.com.");
  55. EXPECT_EQ(client.GetAnswer()[1].GetData(), "1.2.3.4");
  56. }
  57. TEST_F(Cname, subdomain1)
  58. {
  59. smartdns::MockServer server_upstream;
  60. smartdns::Server server;
  61. server_upstream.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) {
  62. if (request->qtype != DNS_T_A) {
  63. return smartdns::SERVER_REQUEST_SOA;
  64. }
  65. if (request->domain == "s.a.com") {
  66. smartdns::MockServer::AddIP(request, request->domain.c_str(), "4.5.6.7", 700);
  67. return smartdns::SERVER_REQUEST_OK;
  68. }
  69. smartdns::MockServer::AddIP(request, request->domain.c_str(), "1.2.3.4", 611);
  70. return smartdns::SERVER_REQUEST_OK;
  71. });
  72. server.Start(R"""(bind [::]:60053
  73. cname /a.com/s.a.com
  74. server 127.0.0.1:61053
  75. )""");
  76. smartdns::Client client;
  77. ASSERT_TRUE(client.Query("a.com", 60053));
  78. std::cout << client.GetResult() << std::endl;
  79. ASSERT_EQ(client.GetAnswerNum(), 2);
  80. EXPECT_EQ(client.GetStatus(), "NOERROR");
  81. EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
  82. EXPECT_EQ(client.GetAnswer()[0].GetData(), "s.a.com.");
  83. EXPECT_EQ(client.GetAnswer()[1].GetData(), "4.5.6.7");
  84. }
  85. TEST_F(Cname, subdomain2)
  86. {
  87. smartdns::MockServer server_upstream;
  88. smartdns::Server server;
  89. server_upstream.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) {
  90. if (request->qtype != DNS_T_A) {
  91. return smartdns::SERVER_REQUEST_SOA;
  92. }
  93. if (request->domain == "a.s.a.com") {
  94. smartdns::MockServer::AddIP(request, request->domain.c_str(), "4.5.6.7", 700);
  95. return smartdns::SERVER_REQUEST_OK;
  96. }
  97. smartdns::MockServer::AddIP(request, request->domain.c_str(), "1.2.3.4", 611);
  98. return smartdns::SERVER_REQUEST_OK;
  99. });
  100. server.Start(R"""(bind [::]:60053
  101. cname /a.com/s.a.com
  102. cname /s.a.com/a.s.a.com
  103. server 127.0.0.1:61053
  104. )""");
  105. smartdns::Client client;
  106. ASSERT_TRUE(client.Query("a.com", 60053));
  107. std::cout << client.GetResult() << std::endl;
  108. ASSERT_EQ(client.GetAnswerNum(), 2);
  109. EXPECT_EQ(client.GetStatus(), "NOERROR");
  110. EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
  111. EXPECT_EQ(client.GetAnswer()[0].GetData(), "s.a.com.");
  112. EXPECT_EQ(client.GetAnswer()[1].GetData(), "4.5.6.7");
  113. }
  114. TEST_F(Cname, loop)
  115. {
  116. smartdns::MockServer server_upstream;
  117. smartdns::Server server;
  118. server_upstream.Start("udp://0.0.0.0:61053", [](struct smartdns::ServerRequestContext *request) {
  119. if (request->qtype != DNS_T_A) {
  120. return smartdns::SERVER_REQUEST_SOA;
  121. }
  122. if (request->domain == "s.a.com") {
  123. smartdns::MockServer::AddIP(request, request->domain.c_str(), "4.5.6.7", 700);
  124. return smartdns::SERVER_REQUEST_OK;
  125. }
  126. smartdns::MockServer::AddIP(request, request->domain.c_str(), "1.2.3.4", 611);
  127. return smartdns::SERVER_REQUEST_OK;
  128. });
  129. server.Start(R"""(bind [::]:60053
  130. cname /a.com/c.a.com
  131. cname /c.a.com/s.a.com
  132. server 127.0.0.1:61053
  133. )""");
  134. smartdns::Client client;
  135. ASSERT_TRUE(client.Query("a.com", 60053));
  136. std::cout << client.GetResult() << std::endl;
  137. ASSERT_EQ(client.GetAnswerNum(), 2);
  138. EXPECT_EQ(client.GetStatus(), "NOERROR");
  139. EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
  140. EXPECT_EQ(client.GetAnswer()[0].GetData(), "c.a.com.");
  141. EXPECT_EQ(client.GetAnswer()[1].GetData(), "4.5.6.7");
  142. }