016-relay-support.cpp 11 KB

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