080-resolver.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. // SPDX-FileCopyrightText: 2019-2025 Ivan Baidakou
  3. #include <catch2/catch_all.hpp>
  4. #include <rotor.hpp>
  5. #include <rotor/asio.hpp>
  6. #include "test-utils.h"
  7. #include "utils/error_code.h"
  8. #include "net/resolver_actor.h"
  9. #include "utils/format.hpp"
  10. using namespace syncspirit;
  11. using namespace syncspirit::test;
  12. using namespace syncspirit::net;
  13. using namespace std::chrono_literals;
  14. namespace asio = boost::asio;
  15. namespace sys = boost::system;
  16. namespace r = rotor;
  17. namespace ra = r::asio;
  18. using configure_callback_t = std::function<void(r::plugin::plugin_base_t &)>;
  19. using response_ptr_t = r::intrusive_ptr_t<message::resolve_response_t>;
  20. using Catch::Matchers::StartsWith;
  21. auto timeout = r::pt::time_duration{r::pt::millisec{1000}};
  22. struct my_supervisor_t : ra::supervisor_asio_t {
  23. using parent_t = ra::supervisor_asio_t;
  24. using parent_t::parent_t;
  25. using responses_t = std::vector<response_ptr_t>;
  26. void configure(r::plugin::plugin_base_t &plugin) noexcept override {
  27. ra::supervisor_asio_t::configure(plugin);
  28. plugin.with_casted<r::plugin::starter_plugin_t>(
  29. [&](auto &p) { p.subscribe_actor(&my_supervisor_t::on_resolve); });
  30. if (configure_callback) {
  31. configure_callback(plugin);
  32. }
  33. }
  34. void on_resolve(message::resolve_response_t &res) noexcept { responses.emplace_back(&res); }
  35. responses_t responses;
  36. configure_callback_t configure_callback;
  37. };
  38. using supervisor_ptr_t = r::intrusive_ptr_t<my_supervisor_t>;
  39. using actor_ptr_t = r::intrusive_ptr_t<resolver_actor_t>;
  40. struct fixture_t {
  41. fixture_t() : ctx(io_ctx), root_path{unique_path()}, path_quard{root_path}, remote_resolver{io_ctx} {
  42. test::init_logging();
  43. log = utils::get_logger("fixture");
  44. rx_buff.resize(1500);
  45. }
  46. virtual void main() noexcept = 0;
  47. void run() {
  48. auto strand = std::make_shared<asio::io_context::strand>(io_ctx);
  49. sup = ctx.create_supervisor<my_supervisor_t>().strand(strand).timeout(timeout).create_registry().finish();
  50. sup->start();
  51. sup->do_process();
  52. hosts_path = root_path / "hosts";
  53. hosts_path_str = hosts_path.string();
  54. auto ep = asio::ip::udp::endpoint(asio::ip::make_address("127.0.0.1"), 0);
  55. remote_resolver.open(ep.protocol());
  56. remote_resolver.bind(ep);
  57. auto local_ep = remote_resolver.local_endpoint();
  58. log->info("remote resolver: {}", local_ep);
  59. main();
  60. sup->do_shutdown();
  61. sup->do_process();
  62. io_ctx.run();
  63. }
  64. asio::io_context io_ctx{1};
  65. ra::system_context_asio_t ctx;
  66. bfs::path root_path;
  67. bfs::path hosts_path;
  68. std::string hosts_path_str;
  69. path_guard_t path_quard;
  70. utils::logger_t log;
  71. supervisor_ptr_t sup;
  72. actor_ptr_t resolver;
  73. udp_socket_t remote_resolver;
  74. udp::endpoint resolver_endpoint;
  75. fmt::memory_buffer rx_buff;
  76. };
  77. void test_local_resolver() {
  78. struct F : fixture_t {
  79. void main() noexcept override {
  80. write_file(hosts_path, "127.0.0.2 lclhst.localdomain lclhst\n");
  81. resolver = sup->create_actor<resolver_actor_t>()
  82. .resolve_timeout(timeout / 2)
  83. .hosts_path(hosts_path_str.c_str())
  84. .server_addresses("127.0.0.1:1234")
  85. .timeout(timeout)
  86. .finish();
  87. sup->do_process();
  88. sup->request<payload::address_request_t>(resolver->get_address(), "lclhst", 123).send(timeout);
  89. sup->do_process();
  90. REQUIRE(sup->responses.size() == 1);
  91. auto results = sup->responses.at(0)->payload.res->results;
  92. REQUIRE(results.size() == 1);
  93. REQUIRE(results.at(0) == asio::ip::make_address("127.0.0.2"));
  94. // cache hit
  95. sup->request<payload::address_request_t>(resolver->get_address(), "lclhst", 123).send(timeout);
  96. sup->do_process();
  97. REQUIRE(sup->responses.size() == 2);
  98. results = sup->responses.at(1)->payload.res->results;
  99. REQUIRE(results.size() == 1);
  100. REQUIRE(results.at(0) == asio::ip::make_address("127.0.0.2"));
  101. }
  102. };
  103. F().run();
  104. }
  105. void test_success_resolver() {
  106. struct F : fixture_t {
  107. void main() noexcept override {
  108. auto local_port = remote_resolver.local_endpoint().port();
  109. write_file(hosts_path, "");
  110. auto buff = asio::buffer(rx_buff.data(), rx_buff.size());
  111. remote_resolver.async_receive_from(buff, resolver_endpoint, [&](sys::error_code ec, size_t bytes) -> void {
  112. log->info("received {} bytes from resolver, ec = {}", bytes, ec.value());
  113. const unsigned char reply[] = {0x0e, 0x51, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
  114. 0x00, 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x03, 0x63, 0x6f,
  115. 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x01, 0x00,
  116. 0x01, 0x00, 0x00, 0x01, 0x02, 0x00, 0x04, 0x8e, 0xfa, 0xcb, 0x8e};
  117. auto reply_str = std::string_view(reinterpret_cast<const char *>(reply), sizeof(reply));
  118. auto buff = asio::buffer(reply_str.data(), reply_str.size());
  119. remote_resolver.async_send_to(buff, resolver_endpoint, [&](sys::error_code ec, size_t bytes) {
  120. log->info("sent {} bytes to resolver, ec = {}", bytes, ec.value());
  121. });
  122. });
  123. resolver = sup->create_actor<resolver_actor_t>()
  124. .resolve_timeout(timeout / 2)
  125. .hosts_path(hosts_path_str.c_str())
  126. .server_addresses(fmt::format("127.0.0.1:{}", local_port))
  127. .timeout(timeout)
  128. .finish();
  129. sup->do_process();
  130. sup->request<payload::address_request_t>(resolver->get_address(), "google.com", 80).send(timeout);
  131. io_ctx.run();
  132. REQUIRE(sup->responses.size() == 1);
  133. auto &results = sup->responses.at(0)->payload.res->results;
  134. REQUIRE(results.size() == 1);
  135. REQUIRE_THAT(results.at(0).to_string(), StartsWith("142.250."));
  136. }
  137. };
  138. F().run();
  139. }
  140. void test_success_ip() {
  141. struct F : fixture_t {
  142. void main() noexcept override {
  143. auto local_port = remote_resolver.local_endpoint().port();
  144. write_file(hosts_path, "");
  145. resolver = sup->create_actor<resolver_actor_t>()
  146. .resolve_timeout(timeout / 2)
  147. .hosts_path(hosts_path_str.c_str())
  148. .server_addresses(fmt::format("127.0.0.1:{}", local_port))
  149. .timeout(timeout)
  150. .finish();
  151. sup->do_process();
  152. sup->request<payload::address_request_t>(resolver->get_address(), "127.0.0.1", 80).send(timeout);
  153. sup->do_process();
  154. REQUIRE(sup->responses.size() == 1);
  155. auto &results = sup->responses.at(0)->payload.res->results;
  156. REQUIRE(results.size() == 1);
  157. REQUIRE(results.at(0) == asio::ip::make_address("127.0.0.1"));
  158. }
  159. };
  160. F().run();
  161. }
  162. void test_success_ipv6() {
  163. struct F : fixture_t {
  164. void main() noexcept override {
  165. auto local_port = remote_resolver.local_endpoint().port();
  166. write_file(hosts_path, "");
  167. resolver = sup->create_actor<resolver_actor_t>()
  168. .resolve_timeout(timeout / 2)
  169. .hosts_path(hosts_path_str.c_str())
  170. .server_addresses(fmt::format("127.0.0.1:{}", local_port))
  171. .timeout(timeout)
  172. .finish();
  173. sup->do_process();
  174. sup->request<payload::address_request_t>(resolver->get_address(),
  175. "[fde8:6819:8685:4d00:be03:58ff:fe74:c854]", 80)
  176. .send(timeout);
  177. sup->do_process();
  178. REQUIRE(sup->responses.size() == 1);
  179. #if !defined(_WIN32_WINNT) || _WIN32_WINNT >= 0x0600
  180. auto &results = sup->responses.at(0)->payload.res->results;
  181. REQUIRE(results.size() == 1);
  182. REQUIRE(results.at(0) == asio::ip::make_address("fde8:6819:8685:4d00:be03:58ff:fe74:c854"));
  183. #else
  184. auto& ee = sup->responses.at(0)->payload.ee;
  185. REQUIRE(ee);
  186. REQUIRE(ee->ec == utils::make_error_code(utils::error_code_t::cares_failure));
  187. #endif
  188. }
  189. };
  190. F().run();
  191. }
  192. void test_garbage() {
  193. struct F : fixture_t {
  194. void main() noexcept override {
  195. auto local_port = remote_resolver.local_endpoint().port();
  196. write_file(hosts_path, "");
  197. auto buff = asio::buffer(rx_buff.data(), rx_buff.size());
  198. remote_resolver.async_receive_from(buff, resolver_endpoint, [&](sys::error_code ec, size_t bytes) -> void {
  199. log->info("received {} bytes from resolver, ec = {}", bytes, ec.value());
  200. const unsigned char reply[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  201. auto reply_str = std::string_view(reinterpret_cast<const char *>(reply), sizeof(reply));
  202. auto buff = asio::buffer(reply_str.data(), reply_str.size());
  203. remote_resolver.async_send_to(buff, resolver_endpoint, [&](sys::error_code ec, size_t bytes) {
  204. log->info("sent {} bytes to resolver, ec = {}", bytes, ec.value());
  205. });
  206. });
  207. resolver = sup->create_actor<resolver_actor_t>()
  208. .resolve_timeout(timeout / 2)
  209. .hosts_path(hosts_path_str.c_str())
  210. .server_addresses(fmt::format("127.0.0.1:{}", local_port))
  211. .timeout(timeout)
  212. .finish();
  213. sup->do_process();
  214. sup->request<payload::address_request_t>(resolver->get_address(), "google.com", 80).send(timeout);
  215. io_ctx.run();
  216. REQUIRE(sup->responses.size() == 1);
  217. auto &ee = sup->responses.at(0)->payload.ee;
  218. REQUIRE(ee);
  219. REQUIRE(ee->ec.value() == static_cast<int>(utils::error_code_t::cares_failure));
  220. }
  221. };
  222. F().run();
  223. }
  224. void test_multi_replies() {
  225. struct F : fixture_t {
  226. void main() noexcept override {
  227. auto local_port = remote_resolver.local_endpoint().port();
  228. write_file(hosts_path, "");
  229. auto buff = asio::buffer(rx_buff.data(), rx_buff.size());
  230. remote_resolver.async_receive_from(buff, resolver_endpoint, [&](sys::error_code ec, size_t bytes) -> void {
  231. log->info("received {} bytes from resolver, ec = {}", bytes, ec.value());
  232. const unsigned char reply[] = {
  233. 0x5e, 0x60, 0x81, 0x80, 0x00, 0x01, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x06, 0x72, 0x65, 0x6c,
  234. 0x61, 0x79, 0x73, 0x09, 0x73, 0x79, 0x6e, 0x63, 0x74, 0x68, 0x69, 0x6e, 0x67, 0x03, 0x6e, 0x65,
  235. 0x74, 0x00, 0x00, 0x01, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x05, 0x00, 0x01, 0x00, 0x00, 0x05, 0x31,
  236. 0x00, 0x0d, 0x0a, 0x70, 0x61, 0x72, 0x2d, 0x6b, 0x38, 0x73, 0x2d, 0x76, 0x34, 0xc0, 0x13, 0xc0,
  237. 0x32, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x67, 0x00, 0x04, 0x33, 0x9f, 0x56, 0xd0, 0xc0,
  238. 0x32, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x67, 0x00, 0x04, 0x33, 0x9f, 0x4b, 0x11};
  239. auto reply_str = std::string_view(reinterpret_cast<const char *>(reply), sizeof(reply));
  240. auto buff = asio::buffer(reply_str.data(), reply_str.size());
  241. remote_resolver.async_send_to(buff, resolver_endpoint, [&](sys::error_code ec, size_t bytes) {
  242. log->info("sent {} bytes to resolver, ec = {}", bytes, ec.value());
  243. });
  244. });
  245. resolver = sup->create_actor<resolver_actor_t>()
  246. .resolve_timeout(timeout / 2)
  247. .hosts_path(hosts_path_str.c_str())
  248. .server_addresses(fmt::format("127.0.0.1:{}", local_port))
  249. .timeout(timeout)
  250. .finish();
  251. sup->do_process();
  252. sup->request<payload::address_request_t>(resolver->get_address(), "relays.syncthing.net", 80).send(timeout);
  253. io_ctx.run();
  254. REQUIRE(sup->responses.size() == 1);
  255. auto &results = sup->responses.at(0)->payload.res->results;
  256. REQUIRE(results.size() == 2);
  257. REQUIRE(results.at(0) == asio::ip::make_address("51.159.86.208"));
  258. REQUIRE(results.at(1) == asio::ip::make_address("51.159.75.17"));
  259. }
  260. };
  261. F().run();
  262. }
  263. void test_wrong() {
  264. struct F : fixture_t {
  265. void main() noexcept override {
  266. auto local_port = remote_resolver.local_endpoint().port();
  267. write_file(hosts_path, "");
  268. auto buff = asio::buffer(rx_buff.data(), rx_buff.size());
  269. remote_resolver.async_receive_from(buff, resolver_endpoint, [&](sys::error_code ec, size_t bytes) -> void {
  270. log->info("received {} bytes from resolver, ec = {}", bytes, ec.value());
  271. const unsigned char reply[] = {0x0e, 0x51, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
  272. 0x00, 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x03, 0x63, 0x61,
  273. 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x01, 0x00,
  274. 0x01, 0x00, 0x00, 0x01, 0x02, 0x00, 0x04, 0x8e, 0xfa, 0xcb, 0x8e};
  275. auto reply_str = std::string_view(reinterpret_cast<const char *>(reply), sizeof(reply));
  276. auto buff = asio::buffer(reply_str.data(), reply_str.size());
  277. remote_resolver.async_send_to(buff, resolver_endpoint, [&](sys::error_code ec, size_t bytes) {
  278. log->info("sent {} bytes to resolver", bytes, ec.value());
  279. });
  280. });
  281. resolver = sup->create_actor<resolver_actor_t>()
  282. .resolve_timeout(timeout / 2)
  283. .hosts_path(hosts_path_str.c_str())
  284. .server_addresses(fmt::format("127.0.0.1:{}", local_port))
  285. .timeout(timeout)
  286. .finish();
  287. sup->do_process();
  288. sup->request<payload::address_request_t>(resolver->get_address(), "google.com", 80).send(timeout);
  289. io_ctx.run();
  290. REQUIRE(sup->responses.size() == 1);
  291. auto &ee = sup->responses.at(0)->payload.ee;
  292. REQUIRE(ee);
  293. REQUIRE(ee->ec.value() == static_cast<int>(utils::error_code_t::cares_failure));
  294. }
  295. };
  296. F().run();
  297. }
  298. void test_timeout() {
  299. struct F : fixture_t {
  300. void main() noexcept override {
  301. auto local_port = remote_resolver.local_endpoint().port();
  302. write_file(hosts_path, "");
  303. resolver = sup->create_actor<resolver_actor_t>()
  304. .resolve_timeout(timeout / 2)
  305. .hosts_path(hosts_path_str.c_str())
  306. .server_addresses(fmt::format("127.0.0.1:{}", local_port))
  307. .timeout(timeout)
  308. .finish();
  309. sup->do_process();
  310. sup->request<payload::address_request_t>(resolver->get_address(), "google.com", 80).send(timeout);
  311. io_ctx.run();
  312. REQUIRE(sup->responses.size() == 1);
  313. auto &ee = sup->responses.at(0)->payload.ee;
  314. REQUIRE(ee);
  315. REQUIRE(ee->ec.value() == static_cast<int>(r::error_code_t::request_timeout));
  316. }
  317. };
  318. F().run();
  319. }
  320. void test_cancellation() {
  321. struct F : fixture_t {
  322. void main() noexcept override {
  323. auto local_port = remote_resolver.local_endpoint().port();
  324. write_file(hosts_path, "");
  325. resolver = sup->create_actor<resolver_actor_t>()
  326. .resolve_timeout(timeout / 2)
  327. .hosts_path(hosts_path_str.c_str())
  328. .server_addresses(fmt::format("127.0.0.1:{}", local_port))
  329. .timeout(timeout)
  330. .finish();
  331. sup->do_process();
  332. auto resolver_address = resolver->get_address();
  333. auto request = sup->request<payload::address_request_t>(resolver_address, "google.com", 80).send(timeout);
  334. sup->send<message::resolve_cancel_t::payload_t>(resolver_address, request, sup->get_address());
  335. io_ctx.run();
  336. REQUIRE(sup->responses.size() == 1);
  337. auto &ee = sup->responses.at(0)->payload.ee;
  338. REQUIRE(ee);
  339. REQUIRE(ee->ec.value() == static_cast<int>(asio::error::operation_aborted));
  340. }
  341. };
  342. F().run();
  343. }
  344. int _init() {
  345. REGISTER_TEST_CASE(test_local_resolver, "test_local_resolver", "[resolver]");
  346. REGISTER_TEST_CASE(test_success_resolver, "test_success_resolver", "[resolver]");
  347. REGISTER_TEST_CASE(test_success_ip, "test_success_ip", "[resolver]");
  348. REGISTER_TEST_CASE(test_success_ipv6, "test_success_ipv6", "[resolver]");
  349. REGISTER_TEST_CASE(test_multi_replies, "test_multi_replies", "[resolver]");
  350. REGISTER_TEST_CASE(test_garbage, "test_garbage", "[resolver]");
  351. REGISTER_TEST_CASE(test_wrong, "test_wrong", "[resolver]");
  352. REGISTER_TEST_CASE(test_timeout, "test_timeout", "[resolver]");
  353. REGISTER_TEST_CASE(test_cancellation, "test_cancellation", "[resolver]");
  354. return 1;
  355. }
  356. static int v = _init();