016-relay-support.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. // SPDX-FileCopyrightText: 2023-2024 Ivan Baidakou
  3. #include "test-utils.h"
  4. #include "proto/relay_support.h"
  5. using namespace syncspirit::proto::relay;
  6. TEST_CASE("relay proto", "[relay]") {
  7. std::string buff;
  8. SECTION("successful cases") {
  9. SECTION("ping") {
  10. auto sz = serialize(ping_t{}, buff);
  11. REQUIRE(sz);
  12. auto r = parse(buff);
  13. auto msg = std::get_if<wrapped_message_t>(&r);
  14. REQUIRE(msg);
  15. CHECK(msg->length == sz);
  16. auto target = std::get_if<ping_t>(&msg->message);
  17. REQUIRE(target);
  18. }
  19. SECTION("pong") {
  20. auto sz = serialize(pong_t{}, buff);
  21. REQUIRE(sz);
  22. auto r = parse(buff);
  23. auto msg = std::get_if<wrapped_message_t>(&r);
  24. REQUIRE(msg);
  25. CHECK(msg->length == sz);
  26. auto target = std::get_if<pong_t>(&msg->message);
  27. REQUIRE(target);
  28. }
  29. SECTION("join_relay_request") {
  30. auto sz = serialize(join_relay_request_t{}, buff);
  31. REQUIRE(sz);
  32. auto r = parse(buff);
  33. auto msg = std::get_if<wrapped_message_t>(&r);
  34. REQUIRE(msg);
  35. CHECK(msg->length == sz);
  36. auto target = std::get_if<join_relay_request_t>(&msg->message);
  37. REQUIRE(target);
  38. }
  39. SECTION("join_session_request") {
  40. auto source = join_session_request_t{"lorem impsum dolor"};
  41. auto sz = serialize(source, buff);
  42. REQUIRE(sz);
  43. auto r = parse(buff);
  44. auto msg = std::get_if<wrapped_message_t>(&r);
  45. REQUIRE(msg);
  46. CHECK(msg->length == sz);
  47. auto target = std::get_if<join_session_request_t>(&msg->message);
  48. REQUIRE(target);
  49. CHECK(target->key == source.key);
  50. }
  51. SECTION("response") {
  52. auto source = response_t{404, "not found"};
  53. auto sz = serialize(source, buff);
  54. REQUIRE(sz);
  55. auto r = parse(buff);
  56. auto msg = std::get_if<wrapped_message_t>(&r);
  57. REQUIRE(msg);
  58. CHECK(msg->length == sz);
  59. auto target = std::get_if<response_t>(&msg->message);
  60. REQUIRE(target);
  61. CHECK(target->code == source.code);
  62. CHECK(target->details == source.details);
  63. }
  64. SECTION("connect_request") {
  65. auto source = connect_request_t{"lorem impsum dolor"};
  66. auto sz = serialize(source, buff);
  67. REQUIRE(sz);
  68. auto r = parse(buff);
  69. auto msg = std::get_if<wrapped_message_t>(&r);
  70. REQUIRE(msg);
  71. CHECK(msg->length == sz);
  72. auto target = std::get_if<connect_request_t>(&msg->message);
  73. REQUIRE(target);
  74. CHECK(target->device_id == source.device_id);
  75. }
  76. SECTION("session_invitation") {
  77. auto ip = boost::asio::ip::address_v4::from_string("127.0.0.1");
  78. auto source = session_invitation_t{"lorem", "impsum", ip, 1234, true};
  79. auto sz = serialize(source, buff);
  80. REQUIRE(sz);
  81. auto r = parse(buff);
  82. auto msg = std::get_if<wrapped_message_t>(&r);
  83. REQUIRE(msg);
  84. CHECK(msg->length == sz);
  85. auto target = std::get_if<session_invitation_t>(&msg->message);
  86. REQUIRE(target);
  87. CHECK(target->from == source.from);
  88. CHECK(target->key == source.key);
  89. CHECK(target->address.value() == ip);
  90. CHECK(target->server_socket == source.server_socket);
  91. }
  92. SECTION("session_invitation (host of zeroes)") {
  93. auto ip = boost::asio::ip::address_v4(0);
  94. auto source = session_invitation_t{"lorem", "impsum", ip, 1234, true};
  95. auto sz = serialize(source, buff);
  96. REQUIRE(sz);
  97. auto r = parse(buff);
  98. auto msg = std::get_if<wrapped_message_t>(&r);
  99. REQUIRE(msg);
  100. CHECK(msg->length == sz);
  101. auto target = std::get_if<session_invitation_t>(&msg->message);
  102. REQUIRE(target);
  103. CHECK(target->from == source.from);
  104. CHECK(target->key == source.key);
  105. CHECK(!target->address.has_value());
  106. CHECK(target->server_socket == source.server_socket);
  107. }
  108. SECTION("response sample") {
  109. const unsigned char data[] = {0x9e, 0x79, 0xbc, 0x40, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00,
  110. 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07,
  111. 0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x00};
  112. auto ptr = reinterpret_cast<const char *>(data);
  113. auto r = parse({ptr, sizeof(data)});
  114. auto msg = std::get_if<wrapped_message_t>(&r);
  115. REQUIRE(msg);
  116. CHECK(msg->length == 28);
  117. auto target = std::get_if<response_t>(&msg->message);
  118. REQUIRE(target);
  119. CHECK(target->code == 0);
  120. auto details = std::string_view("success");
  121. CHECK(target->details == details);
  122. }
  123. SECTION("response sample-2") {
  124. const unsigned char data[] = {0x9e, 0x79, 0xbc, 0x40, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  125. 0x14, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09, 0x6e, 0x6f,
  126. 0x74, 0x20, 0x66, 0x6f, 0x75, 0x6e, 0x64, 0x00, 0x00, 0x00};
  127. auto ptr = reinterpret_cast<const char *>(data);
  128. auto r = parse({ptr, sizeof(data)});
  129. auto msg = std::get_if<wrapped_message_t>(&r);
  130. REQUIRE(msg);
  131. CHECK(msg->length == 32);
  132. auto target = std::get_if<response_t>(&msg->message);
  133. REQUIRE(target);
  134. CHECK(target->code == 1);
  135. auto details = std::string_view("not found");
  136. CHECK(target->details == details);
  137. }
  138. SECTION("session invitation sample") {
  139. const unsigned char data[] = {0x9e, 0x79, 0xbc, 0x40, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x4a, 0x00,
  140. 0x00, 0x00, 0x20, 0xf8, 0x31, 0xf5, 0x75, 0xea, 0x61, 0x8a, 0x2f, 0x15, 0xef,
  141. 0x67, 0x68, 0x36, 0x4a, 0x62, 0x89, 0xfc, 0x76, 0xb6, 0x73, 0xc8, 0x5a, 0x2a,
  142. 0xbe, 0x60, 0x8f, 0x4a, 0xff, 0x27, 0xba, 0x39, 0x02, 0x00, 0x00, 0x00, 0x12,
  143. 0x6c, 0x6f, 0x72, 0x65, 0x6d, 0x2d, 0x69, 0x6d, 0x73, 0x70, 0x75, 0x6d, 0x2d,
  144. 0x64, 0x6f, 0x6c, 0x6f, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x39,
  145. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00};
  146. auto ptr = reinterpret_cast<const char *>(data);
  147. auto str = std::string_view(ptr, sizeof(data));
  148. auto r = parse(str);
  149. auto msg = std::get_if<wrapped_message_t>(&r);
  150. REQUIRE(msg);
  151. CHECK(msg->length == str.size());
  152. auto target = std::get_if<session_invitation_t>(&msg->message);
  153. REQUIRE(target);
  154. CHECK(target->from.size() == 32);
  155. CHECK(target->key == "lorem-imspum-dolor");
  156. CHECK(!target->address.has_value());
  157. CHECK(target->port == 12345);
  158. CHECK(target->server_socket);
  159. }
  160. }
  161. SECTION("incomplete") {
  162. auto r = parse(buff);
  163. auto msg = std::get_if<incomplete_t>(&r);
  164. REQUIRE(msg);
  165. serialize(ping_t{}, buff);
  166. r = parse(std::string_view(buff.data(), buff.size() - 1));
  167. msg = std::get_if<incomplete_t>(&r);
  168. REQUIRE(msg);
  169. }
  170. SECTION("protocol errors") {
  171. SECTION("wrong magic") {
  172. auto sz = serialize(ping_t{}, buff);
  173. REQUIRE(sz);
  174. buff[0] = -1;
  175. auto r = parse(buff);
  176. CHECK(std::get_if<protocol_error_t>(&r));
  177. }
  178. SECTION("wrong size") {
  179. auto sz = serialize(ping_t{}, buff);
  180. REQUIRE(sz);
  181. buff[4] = -1;
  182. auto r = parse(buff);
  183. CHECK(std::get_if<protocol_error_t>(&r));
  184. }
  185. SECTION("wrong type") {
  186. auto sz = serialize(ping_t{}, buff);
  187. REQUIRE(sz);
  188. buff[9] = -1;
  189. auto r = parse(buff);
  190. CHECK(std::get_if<protocol_error_t>(&r));
  191. }
  192. }
  193. }
  194. TEST_CASE("endpoing parsing", "[relay]") {
  195. std::string body = R""(
  196. {
  197. "relays": [
  198. {
  199. "url": "relay://130.61.176.206:22067/?id=OAKAXEX-7HE764M-5EWVN7U-SZCQU4D-ZPXF2TY-SNTL2LL-Y5RVGVM-U7WBRA3&pingInterval=1m30s&networkTimeout=2m0s&sessionLimitBps=0&globalLimitBps=0&statusAddr=:22070&providedBy=ina",
  200. "location": {
  201. "latitude": 50.1049,
  202. "longitude": 8.6295,
  203. "city": "Frankfurt am Main",
  204. "country": "DE",
  205. "continent": "EU"
  206. },
  207. "stats": {
  208. "startTime": "2022-04-16T09:12:21.941097618Z",
  209. "uptimeSeconds": 1312802,
  210. "numPendingSessionKeys": 0,
  211. "numActiveSessions": 46,
  212. "numConnections": 494,
  213. "numProxies": 88,
  214. "bytesProxied": 493207937616,
  215. "goVersion": "go1.16.3",
  216. "goOS": "linux",
  217. "goArch": "arm64",
  218. "goMaxProcs": 4,
  219. "goNumRoutine": 1136,
  220. "kbps10s1m5m15m30m60m": [
  221. 241,
  222. 284,
  223. 309,
  224. 332,
  225. 355,
  226. 312
  227. ],
  228. "options": {
  229. "network-timeout": 120,
  230. "ping-interval": 60,
  231. "message-timeout": 60,
  232. "per-session-rate": 0,
  233. "global-rate": 0,
  234. "pools": [
  235. "https://relays.syncthing.net/endpoint"
  236. ],
  237. "provided-by": "ina"
  238. }
  239. },
  240. "statsRetrieved": "2022-05-01T13:52:24.524417759Z"
  241. }
  242. ]
  243. }
  244. )"";
  245. auto r = parse_endpoint(body);
  246. REQUIRE(r);
  247. REQUIRE(r.value().size() == 1);
  248. auto relay = r.value()[0];
  249. CHECK(relay->uri->buffer() ==
  250. "relay://130.61.176.206:22067/"
  251. "?id=OAKAXEX-7HE764M-5EWVN7U-SZCQU4D-ZPXF2TY-SNTL2LL-Y5RVGVM-U7WBRA3&pingInterval=1m30s&"
  252. "networkTimeout=2m0s&sessionLimitBps=0&globalLimitBps=0&statusAddr=:22070&providedBy=ina");
  253. CHECK(relay->device_id.get_short() == "OAKAXEX");
  254. auto &l = relay->location;
  255. CHECK(abs(l.latitude - 50.1049) < 0.0001);
  256. CHECK(abs(l.longitude - 8.6295) < 0.0001);
  257. CHECK(l.city == "Frankfurt am Main");
  258. CHECK(l.country == "DE");
  259. CHECK(l.continent == "EU");
  260. CHECK(relay->ping_interval == pt::seconds{90});
  261. auto device = parse_device(relay->uri);
  262. REQUIRE(device);
  263. CHECK(device.value() == relay->device_id);
  264. }