060-bep.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. // SPDX-FileCopyrightText: 2019-2023 Ivan Baidakou
  3. #include "test-utils.h"
  4. #include "proto/bep_support.h"
  5. #include "model/device_id.h"
  6. #include "utils/error_code.h"
  7. #include "utils/uri.h"
  8. #include "syncspirit-config.h"
  9. using namespace syncspirit;
  10. using namespace syncspirit::test;
  11. using namespace syncspirit::utils;
  12. using namespace syncspirit::proto;
  13. using namespace syncspirit::model;
  14. TEST_CASE("announce", "[bep]") {
  15. unsigned char buff_raw[] = {
  16. 0x2e, 0xa7, 0xd9, 0x0b, 0x0a, 0x20, 0x51, 0xe0, 0xd7, 0x6a, 0x5d, 0x84, 0x90, 0xb8, 0xe2, 0xfc, 0x4e,
  17. 0x73, 0xcf, 0xaf, 0xff, 0x03, 0xe0, 0xda, 0x9b, 0x29, 0x99, 0x15, 0x4f, 0xa0, 0xaf, 0x69, 0x1e, 0x16,
  18. 0x63, 0x8c, 0xb9, 0x59, 0x12, 0x13, 0x74, 0x63, 0x70, 0x3a, 0x2f, 0x2f, 0x30, 0x2e, 0x30, 0x2e, 0x30,
  19. 0x2e, 0x30, 0x3a, 0x32, 0x32, 0x30, 0x30, 0x30, 0x12, 0x19, 0x74, 0x63, 0x70, 0x3a, 0x2f, 0x2f, 0x31,
  20. 0x39, 0x32, 0x2e, 0x31, 0x36, 0x38, 0x2e, 0x31, 0x30, 0x30, 0x2e, 0x36, 0x3a, 0x32, 0x32, 0x30, 0x30,
  21. 0x30, 0x12, 0x0f, 0x74, 0x63, 0x70, 0x3a, 0x2f, 0x2f, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x3a,
  22. 0x30, 0x12, 0x14, 0x71, 0x75, 0x69, 0x63, 0x3a, 0x2f, 0x2f, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x30,
  23. 0x3a, 0x32, 0x32, 0x30, 0x30, 0x30, 0x12, 0x1a, 0x71, 0x75, 0x69, 0x63, 0x3a, 0x2f, 0x2f, 0x31, 0x39,
  24. 0x32, 0x2e, 0x31, 0x36, 0x38, 0x2e, 0x31, 0x30, 0x30, 0x2e, 0x36, 0x3a, 0x32, 0x32, 0x30, 0x30, 0x30,
  25. 0x18, 0xaa, 0x97, 0xfb, 0xd6, 0xb7, 0x99, 0xde, 0xd8, 0x40};
  26. constexpr auto buff_sz = sizeof(buff_raw);
  27. auto buff = boost::asio::buffer(buff_raw, buff_sz);
  28. auto r = parse_announce(buff);
  29. REQUIRE((bool)r);
  30. auto &v = r.value();
  31. auto device_id = device_id_t::from_sha256(v->id());
  32. REQUIRE((bool)device_id);
  33. CHECK(device_id.value().get_value() == "KHQNO2S-5QSILRK-YX4JZZ4-7L77APM-QNVGZJT-EKU7IFI-PNEPBMY-4MXFMQD");
  34. REQUIRE(v->addresses_size() == 5);
  35. CHECK(v->addresses(1) == "tcp://192.168.100.6:22000");
  36. SECTION("make") {
  37. fmt::memory_buffer out;
  38. out.resize(1500);
  39. payload::URIs uris{utils::parse("tcp://192.168.100.6:22000").value()};
  40. auto sz =
  41. make_announce_message(out, "KHQNO2S-5QSILRK-YX4JZZ4-7L77APM-QNVGZJT-EKU7IFI-PNEPBMY-4MXFMQD", uris, 1234);
  42. CHECK(sz == 99);
  43. }
  44. }
  45. TEST_CASE("hello", "[bep]") {
  46. unsigned char buff_raw[] = {0x2e, 0xa7, 0xd9, 0x0b, 0x00, 0x1f, 0x0a, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c,
  47. 0x68, 0x6f, 0x73, 0x74, 0x12, 0x09, 0x73, 0x79, 0x6e, 0x63, 0x74, 0x68, 0x69,
  48. 0x6e, 0x67, 0x1a, 0x07, 0x76, 0x31, 0x2e, 0x31, 0x31, 0x2e, 0x31};
  49. constexpr auto buff_sz = sizeof(buff_raw);
  50. auto buff = boost::asio::buffer(buff_raw, buff_sz);
  51. auto r = parse_bep(buff);
  52. REQUIRE((bool)r);
  53. auto &v = r.value();
  54. CHECK(v.consumed == buff_sz);
  55. auto &msg = std::get<proto::message::Hello>(v.message);
  56. CHECK(msg->device_name() == "localhost");
  57. CHECK(msg->client_name() == "syncthing");
  58. CHECK(msg->client_version() == "v1.11.1");
  59. SECTION("make") {
  60. fmt::memory_buffer out;
  61. out.resize(1500);
  62. payload::URIs uris{utils::parse("tcp://192.168.100.6:22000").value()};
  63. make_hello_message(out, "test-device");
  64. auto buff = boost::asio::buffer(out.data(), out.size());
  65. auto r = parse_bep(buff);
  66. REQUIRE((bool)r);
  67. auto &v = r.value();
  68. CHECK(v.consumed == out.size());
  69. auto &msg = std::get<proto::message::Hello>(v.message);
  70. CHECK(msg->device_name() == "test-device");
  71. CHECK(msg->client_name() == "syncspirit");
  72. CHECK(msg->client_version() == SYNCSPIRIT_VERSION);
  73. }
  74. }
  75. TEST_CASE("cluster config", "[bep]") {
  76. unsigned char buff_raw[] = {
  77. 0x00, 0x02, 0x10, 0x01, 0x00, 0x00, 0x00, 0xb5, 0x00, 0x00, 0x00, 0xc2, 0xa1, 0x0a, 0xbf, 0x01, 0x0a, 0x04,
  78. 0x64, 0x61, 0x74, 0x61, 0x12, 0x06, 0x00, 0xf5, 0x58, 0x82, 0x01, 0x34, 0x0a, 0x20, 0x45, 0x72, 0x75, 0x36,
  79. 0xab, 0xf5, 0xe5, 0xb7, 0xf9, 0xe7, 0x8d, 0xbd, 0xbf, 0xeb, 0x54, 0x72, 0xdd, 0x65, 0x06, 0x27, 0x18, 0xc6,
  80. 0x0b, 0x75, 0x04, 0xbe, 0xdc, 0x1f, 0x14, 0xa4, 0x01, 0xbf, 0x12, 0x07, 0x68, 0x70, 0x2d, 0x6e, 0x6f, 0x74,
  81. 0x65, 0x1a, 0x07, 0x64, 0x79, 0x6e, 0x61, 0x6d, 0x69, 0x63, 0x82, 0x01, 0x42, 0x0a, 0x20, 0x51, 0xe0, 0xd7,
  82. 0x6a, 0x5d, 0x84, 0x90, 0xb8, 0xe2, 0xfc, 0x4e, 0x73, 0xcf, 0xaf, 0xff, 0x03, 0xe0, 0xda, 0x9b, 0x29, 0x99,
  83. 0x15, 0x4f, 0xa0, 0xaf, 0x69, 0x1e, 0x16, 0x63, 0x8c, 0xb9, 0x59, 0x12, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c,
  84. 0x68, 0x6f, 0x73, 0x74, 0x39, 0x00, 0xc1, 0x30, 0x05, 0x40, 0xc3, 0xe4, 0xdd, 0xa2, 0xbe, 0x8a, 0xb2, 0xed,
  85. 0x3d, 0x7c, 0x00, 0xf9, 0x11, 0x89, 0x7f, 0x93, 0xb0, 0x95, 0x15, 0x3f, 0xd3, 0x52, 0xe4, 0xf5, 0xfa, 0xbd,
  86. 0x8c, 0xc5, 0x2d, 0x7a, 0x2d, 0x21, 0x60, 0xf3, 0x76, 0x6e, 0xd7, 0xc5, 0xce, 0x27, 0x5f, 0xed, 0xe5, 0x76,
  87. 0x12, 0x7c, 0x00, 0x50, 0x6e, 0x61, 0x6d, 0x69, 0x63};
  88. constexpr auto buff_sz = sizeof(buff_raw);
  89. auto buff = boost::asio::buffer(buff_raw, buff_sz);
  90. auto r = parse_bep(buff);
  91. REQUIRE((bool)r);
  92. auto &v = r.value();
  93. CHECK(v.consumed == buff_sz);
  94. auto &msg = std::get<proto::message::ClusterConfig>(v.message);
  95. REQUIRE(msg->folders_size() == 1);
  96. auto &folder = msg->folders(0);
  97. CHECK(folder.label() == "data");
  98. CHECK(!folder.read_only());
  99. REQUIRE(folder.devices_size() == 3);
  100. auto &d1 = folder.devices(0);
  101. auto &d2 = folder.devices(1);
  102. auto &d3 = folder.devices(2);
  103. CHECK(device_id_t::from_sha256(d1.id()).value().get_value() ==
  104. "IVZHKNV-L6XS3PS-6PHRW63-722UOLD-OWKBRHD-DDAW5IV-EX3OB6F-FEAG7QQ");
  105. CHECK(device_id_t::from_sha256(d2.id()).value().get_value() ==
  106. "KHQNO2S-5QSILRK-YX4JZZ4-7L77APM-QNVGZJT-EKU7IFI-PNEPBMY-4MXFMQD");
  107. CHECK(device_id_t::from_sha256(d3.id()).value().get_value() ==
  108. "RF7ZHME-VCU75G2-UXE6X5L-3DGFFVV-5C2ILA6-N3G5V6F-FZYTV73-PFOYJAR");
  109. CHECK(!d1.introducer());
  110. CHECK(!d2.introducer());
  111. CHECK(!d3.introducer());
  112. CHECK(d1.name() == "hp-note");
  113. CHECK(d2.name() == "localhost");
  114. CHECK(d3.name() == "hp-note");
  115. CHECK(d1.addresses_size() == 1);
  116. CHECK(d1.addresses(0) == "dynamic");
  117. CHECK(d2.addresses_size() == 1);
  118. CHECK(d2.addresses(0) == "dynamic");
  119. SECTION("corrupt data a little bit") {
  120. buff_raw[11] = 0xC0;
  121. auto buff = boost::asio::buffer(buff_raw, buff_sz);
  122. auto r = parse_bep(buff);
  123. REQUIRE(!r);
  124. CHECK(r.error() == utils::make_error_code(utils::bep_error_code_t::lz4_decoding));
  125. }
  126. SECTION("serialize, round-trip") {
  127. fmt::memory_buffer buff;
  128. serialize(buff, *msg);
  129. auto r2 = parse_bep(asio::buffer(buff.begin(), buff.size()));
  130. REQUIRE(r2);
  131. auto &v2 = r.value();
  132. auto &msg2 = std::get<proto::message::ClusterConfig>(v.message);
  133. CHECK(v2.consumed == buff_sz);
  134. CHECK(msg2->folders(0).devices(0).id() == msg->folders(0).devices(0).id());
  135. }
  136. }