071-fs_actor.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. // SPDX-FileCopyrightText: 2019-2024 Ivan Baidakou
  3. #include "test-utils.h"
  4. #include "fs/file_actor.h"
  5. #include "fs/utils.h"
  6. #include "diff-builder.h"
  7. #include "net/messages.h"
  8. #include "test_supervisor.h"
  9. #include "access.h"
  10. #include "model/cluster.h"
  11. #include "model/misc/resolver.h"
  12. #include "access.h"
  13. #include <boost/filesystem.hpp>
  14. using namespace syncspirit;
  15. using namespace syncspirit::db;
  16. using namespace syncspirit::test;
  17. using namespace syncspirit::model;
  18. using namespace syncspirit::net;
  19. namespace bfs = boost::filesystem;
  20. namespace {
  21. struct fixture_t {
  22. using msg_t = net::message::load_cluster_response_t;
  23. using msg_ptr_t = r::intrusive_ptr_t<msg_t>;
  24. using blk_res_t = fs::message::block_response_t;
  25. using blk_res_ptr_t = r::intrusive_ptr_t<blk_res_t>;
  26. fixture_t() noexcept : root_path{bfs::unique_path()}, path_guard{root_path} {
  27. utils::set_default("trace");
  28. bfs::create_directory(root_path);
  29. sequencer = make_sequencer(67);
  30. }
  31. virtual supervisor_t::configure_callback_t configure() noexcept {
  32. return [&](r::plugin::plugin_base_t &plugin) {
  33. plugin.template with_casted<r::plugin::starter_plugin_t>([&](auto &p) {
  34. p.subscribe_actor(r::lambda<msg_t>([&](msg_t &msg) { reply = &msg; }));
  35. p.subscribe_actor(r::lambda<blk_res_t>([&](blk_res_t &msg) { block_reply = &msg; }));
  36. });
  37. };
  38. }
  39. virtual void run() noexcept {
  40. auto my_id =
  41. device_id_t::from_string("KHQNO2S-5QSILRK-YX4JZZ4-7L77APM-QNVGZJT-EKU7IFI-PNEPBMY-4MXFMQD").value();
  42. my_device = device_t::create(my_id, "my-device").value();
  43. cluster = new cluster_t(my_device, 1);
  44. auto peer_id =
  45. device_id_t::from_string("VUV42CZ-IQD5A37-RPEBPM4-VVQK6E4-6WSKC7B-PVJQHHD-4PZD44V-ENC6WAZ").value();
  46. peer_device = device_t::create(peer_id, "peer-device").value();
  47. cluster->get_devices().put(my_device);
  48. cluster->get_devices().put(peer_device);
  49. r::system_context_t ctx;
  50. sup = ctx.create_supervisor<supervisor_t>()
  51. .auto_finish(false)
  52. .auto_ack_blocks(false)
  53. .timeout(timeout)
  54. .create_registry()
  55. .finish();
  56. sup->cluster = cluster;
  57. sup->configure_callback = configure();
  58. sup->start();
  59. sup->do_process();
  60. CHECK(static_cast<r::actor_base_t *>(sup.get())->access<to::state>() == r::state_t::OPERATIONAL);
  61. auto sha256 = peer_device->device_id().get_sha256();
  62. file_actor = sup->create_actor<fs::file_actor_t>()
  63. .mru_size(2)
  64. .cluster(cluster)
  65. .sequencer(sup->sequencer)
  66. .timeout(timeout)
  67. .finish();
  68. sup->do_process();
  69. CHECK(static_cast<r::actor_base_t *>(file_actor.get())->access<to::state>() == r::state_t::OPERATIONAL);
  70. file_addr = file_actor->get_address();
  71. auto builder = diff_builder_t(*cluster);
  72. builder.upsert_folder(folder_id, root_path.string(), "my-label")
  73. .apply(*sup)
  74. .update_peer(peer_device->device_id(), "some_name", "some-cn", true)
  75. .apply(*sup)
  76. .share_folder(sha256, folder_id)
  77. .apply(*sup);
  78. folder = cluster->get_folders().by_id(folder_id);
  79. folder_my = folder->get_folder_infos().by_device(*my_device);
  80. folder_peer = folder->get_folder_infos().by_device(*peer_device);
  81. main();
  82. reply.reset();
  83. sup->shutdown();
  84. sup->do_process();
  85. CHECK(static_cast<r::actor_base_t *>(sup.get())->access<to::state>() == r::state_t::SHUT_DOWN);
  86. }
  87. virtual void main() noexcept {}
  88. r::address_ptr_t file_addr;
  89. r::pt::time_duration timeout = r::pt::millisec{10};
  90. cluster_ptr_t cluster;
  91. model::sequencer_ptr_t sequencer;
  92. model::device_ptr_t peer_device;
  93. model::device_ptr_t my_device;
  94. model::folder_ptr_t folder;
  95. model::folder_info_ptr_t folder_my;
  96. model::folder_info_ptr_t folder_peer;
  97. r::intrusive_ptr_t<supervisor_t> sup;
  98. r::intrusive_ptr_t<fs::file_actor_t> file_actor;
  99. bfs::path root_path;
  100. path_guard_t path_guard;
  101. r::system_context_t ctx;
  102. msg_ptr_t reply;
  103. blk_res_ptr_t block_reply;
  104. std::string_view folder_id = "1234-5678";
  105. };
  106. } // namespace
  107. void test_remote_copy() {
  108. struct F : fixture_t {
  109. void main() noexcept override {
  110. proto::FileInfo pr_fi;
  111. std::int64_t modified = 1641828421;
  112. pr_fi.set_name("q.txt");
  113. pr_fi.set_modified_s(modified);
  114. pr_fi.set_sequence(folder_peer->get_max_sequence() + 1);
  115. auto version = pr_fi.mutable_version();
  116. auto counter = version->add_counters();
  117. counter->set_id(1);
  118. counter->set_value(peer_device->device_id().get_uint());
  119. auto builder = diff_builder_t(*cluster, file_addr);
  120. auto make_file = [&]() {
  121. auto file = file_info_t::create(sequencer->next_uuid(), pr_fi, folder_peer).value();
  122. REQUIRE(folder_peer->add_strict(file));
  123. return file;
  124. };
  125. SECTION("empty regular file") {
  126. auto peer_file = make_file();
  127. builder.remote_copy(*peer_file).apply(*sup);
  128. auto my_file = folder_my->get_file_infos().by_name(peer_file->get_name());
  129. auto &path = my_file->get_path();
  130. REQUIRE(bfs::exists(path));
  131. REQUIRE(bfs::file_size(path) == 0);
  132. REQUIRE(bfs::last_write_time(path) == 1641828421);
  133. }
  134. SECTION("empty regular file a subdir") {
  135. pr_fi.set_name("a/b/c/d/e.txt");
  136. auto peer_file = make_file();
  137. builder.remote_copy(*peer_file).apply(*sup);
  138. auto file = folder_my->get_file_infos().by_name(pr_fi.name());
  139. auto &path = file->get_path();
  140. REQUIRE(bfs::exists(path));
  141. REQUIRE(bfs::file_size(path) == 0);
  142. }
  143. SECTION("non-empty regular file") {
  144. pr_fi.set_size(5);
  145. pr_fi.set_block_size(5);
  146. auto b = proto::BlockInfo();
  147. b.set_hash(utils::sha256_digest("12345").value());
  148. b.set_weak_hash(555);
  149. b.set_size(5ul);
  150. auto bi = block_info_t::create(b).value();
  151. auto &blocks_map = cluster->get_blocks();
  152. blocks_map.put(bi);
  153. auto peer_file = make_file();
  154. peer_file->assign_block(bi, 0);
  155. builder.remote_copy(*peer_file).apply(*sup);
  156. auto file = folder_my->get_file_infos().by_name(pr_fi.name());
  157. auto filename = std::string(file->get_name()) + ".syncspirit-tmp";
  158. auto path = root_path / filename;
  159. REQUIRE(bfs::exists(path));
  160. REQUIRE(bfs::file_size(path) == 5);
  161. }
  162. SECTION("directory") {
  163. pr_fi.set_type(proto::FileInfoType::DIRECTORY);
  164. auto peer_file = make_file();
  165. builder.remote_copy(*peer_file).apply(*sup);
  166. auto file = folder_my->get_file_infos().by_name(pr_fi.name());
  167. auto &path = file->get_path();
  168. REQUIRE(bfs::exists(path));
  169. REQUIRE(bfs::is_directory(path));
  170. }
  171. #ifndef SYNCSPIRIT_WIN
  172. SECTION("symlink") {
  173. bfs::path target = root_path / "some-existing-file";
  174. write_file(target, "zzz");
  175. pr_fi.set_type(proto::FileInfoType::SYMLINK);
  176. pr_fi.set_symlink_target(target.string());
  177. auto peer_file = make_file();
  178. builder.remote_copy(*peer_file).apply(*sup);
  179. auto file = folder_my->get_file_infos().by_name(pr_fi.name());
  180. auto &path = file->get_path();
  181. CHECK(!bfs::exists(path));
  182. CHECK(bfs::is_symlink(path));
  183. CHECK(bfs::read_symlink(path) == target);
  184. }
  185. #endif
  186. SECTION("deleted file") {
  187. pr_fi.set_deleted(true);
  188. bfs::path target = root_path / pr_fi.name();
  189. write_file(target, "zzz");
  190. REQUIRE(bfs::exists(target));
  191. auto peer_file = make_file();
  192. builder.remote_copy(*peer_file).apply(*sup);
  193. auto file = folder_my->get_file_infos().by_name(pr_fi.name());
  194. CHECK(file->is_deleted());
  195. REQUIRE(!bfs::exists(target));
  196. }
  197. }
  198. };
  199. F().run();
  200. }
  201. void test_append_block() {
  202. struct F : fixture_t {
  203. void main() noexcept override {
  204. std::int64_t modified = 1641828421;
  205. proto::FileInfo pr_source;
  206. auto next_sequence = 7ul;
  207. pr_source.set_name("q.txt");
  208. pr_source.set_block_size(5ul);
  209. pr_source.set_modified_s(modified);
  210. auto version = pr_source.mutable_version();
  211. auto counter = version->add_counters();
  212. counter->set_id(1);
  213. counter->set_value(peer_device->device_id().get_uint());
  214. auto bi = proto::BlockInfo();
  215. bi.set_size(5);
  216. bi.set_weak_hash(12);
  217. bi.set_hash(utils::sha256_digest("12345").value());
  218. bi.set_offset(0);
  219. auto b = block_info_t::create(bi).value();
  220. auto bi2 = proto::BlockInfo();
  221. bi2.set_size(5);
  222. bi2.set_weak_hash(12);
  223. bi2.set_hash(utils::sha256_digest("67890").value());
  224. bi2.set_offset(0);
  225. auto b2 = block_info_t::create(bi2).value();
  226. cluster->get_blocks().put(b);
  227. cluster->get_blocks().put(b2);
  228. auto blocks = std::vector<block_info_ptr_t>{b, b2};
  229. auto make_file = [&](size_t block_count) {
  230. pr_source.set_sequence(++next_sequence);
  231. auto copy = pr_source;
  232. auto &v = *copy.mutable_version();
  233. auto version = model::version_t(v);
  234. version.update(*peer_device);
  235. version.to_proto(v);
  236. auto file = file_info_t::create(sequencer->next_uuid(), copy, folder_peer).value();
  237. for (size_t i = 0; i < block_count; ++i) {
  238. file->assign_block(blocks[i], i);
  239. }
  240. REQUIRE(folder_peer->add_strict(file));
  241. return file;
  242. };
  243. auto builder = diff_builder_t(*cluster, file_addr);
  244. SECTION("file with 1 block") {
  245. pr_source.set_size(5ul);
  246. auto peer_file = make_file(1);
  247. builder.append_block(*peer_file, 0, "12345").apply(*sup).finish_file(*peer_file).apply(*sup);
  248. auto file = folder_my->get_file_infos().by_name(pr_source.name());
  249. auto path = root_path / std::string(file->get_name());
  250. REQUIRE(bfs::exists(path));
  251. REQUIRE(bfs::file_size(path) == 5);
  252. auto data = read_file(path);
  253. CHECK(data == "12345");
  254. CHECK(bfs::last_write_time(path) == 1641828421);
  255. }
  256. SECTION("file with 2 different blocks") {
  257. pr_source.set_size(10ul);
  258. auto peer_file = make_file(2);
  259. builder.append_block(*peer_file, 0, "12345").apply(*sup);
  260. auto filename = std::string(peer_file->get_name()) + ".syncspirit-tmp";
  261. auto path = root_path / filename;
  262. #ifndef SYNCSPIRIT_WIN
  263. REQUIRE(bfs::exists(path));
  264. REQUIRE(bfs::file_size(path) == 10);
  265. auto data = read_file(path);
  266. CHECK(data.substr(0, 5) == "12345");
  267. #endif
  268. builder.append_block(*peer_file, 1, "67890").apply(*sup);
  269. SECTION("add 2nd block") {
  270. builder.finish_file(*peer_file).apply(*sup);
  271. filename = std::string(peer_file->get_name());
  272. path = root_path / filename;
  273. REQUIRE(bfs::exists(path));
  274. REQUIRE(bfs::file_size(path) == 10);
  275. auto data = read_file(path);
  276. CHECK(data == "1234567890");
  277. CHECK(bfs::last_write_time(path) == 1641828421);
  278. }
  279. #ifndef SYNCSPIRIT_WIN
  280. SECTION("remove folder (simulate err)") {
  281. bfs::remove_all(root_path);
  282. builder.finish_file(*peer_file).apply(*sup);
  283. CHECK(static_cast<r::actor_base_t *>(file_actor.get())->access<to::state>() ==
  284. r::state_t::SHUT_DOWN);
  285. }
  286. #endif
  287. }
  288. }
  289. };
  290. F().run();
  291. }
  292. void test_clone_block() {
  293. struct F : fixture_t {
  294. void main() noexcept override {
  295. auto bi = proto::BlockInfo();
  296. bi.set_size(5);
  297. bi.set_weak_hash(12);
  298. bi.set_hash(utils::sha256_digest("12345").value());
  299. bi.set_offset(0);
  300. auto b = block_info_t::create(bi).value();
  301. auto bi2 = proto::BlockInfo();
  302. bi2.set_size(5);
  303. bi2.set_weak_hash(12);
  304. bi2.set_hash(utils::sha256_digest("67890").value());
  305. bi2.set_offset(0);
  306. auto b2 = block_info_t::create(bi2).value();
  307. cluster->get_blocks().put(b);
  308. cluster->get_blocks().put(b2);
  309. auto blocks = std::vector<block_info_ptr_t>{b, b2};
  310. std::int64_t modified = 1641828421;
  311. proto::FileInfo pr_source;
  312. pr_source.set_name("a.txt");
  313. pr_source.set_block_size(5ul);
  314. pr_source.set_modified_s(modified);
  315. auto version = pr_source.mutable_version();
  316. auto counter = version->add_counters();
  317. counter->set_id(1);
  318. counter->set_value(peer_device->device_id().get_uint());
  319. auto next_sequence = 7ul;
  320. auto make_file = [&](const proto::FileInfo &fi, size_t count) {
  321. auto copy = fi;
  322. copy.set_sequence(++next_sequence);
  323. auto file = file_info_t::create(sequencer->next_uuid(), copy, folder_peer).value();
  324. for (size_t i = 0; i < count; ++i) {
  325. file->assign_block(blocks[i], i);
  326. }
  327. REQUIRE(folder_peer->add_strict(file));
  328. return file;
  329. };
  330. auto builder = diff_builder_t(*cluster, file_addr);
  331. SECTION("source & target are different files") {
  332. proto::FileInfo pr_target;
  333. pr_target.set_name("b.txt");
  334. pr_target.set_block_size(5ul);
  335. (*pr_target.mutable_version()) = *version;
  336. SECTION("single block target file") {
  337. pr_source.set_size(5ul);
  338. pr_target.set_size(5ul);
  339. pr_target.set_modified_s(modified);
  340. auto source = make_file(pr_source, 1);
  341. auto target = make_file(pr_target, 1);
  342. auto source_file = folder_peer->get_file_infos().by_name(pr_source.name());
  343. builder.append_block(*source_file, 0, "12345").apply(*sup).finish_file(*source_file).apply(*sup);
  344. auto target_file = folder_peer->get_file_infos().by_name(pr_target.name());
  345. auto block = source_file->get_blocks()[0];
  346. auto file_block = model::file_block_t(block.get(), target_file.get(), 0);
  347. builder.clone_block(file_block).apply(*sup).finish_file(*target).apply(*sup);
  348. auto path = root_path / std::string(target_file->get_name());
  349. REQUIRE(bfs::exists(path));
  350. REQUIRE(bfs::file_size(path) == 5);
  351. auto data = read_file(path);
  352. CHECK(data == "12345");
  353. CHECK(bfs::last_write_time(path) == 1641828421);
  354. }
  355. SECTION("multi block target file") {
  356. pr_source.set_size(10ul);
  357. pr_target.set_size(10ul);
  358. auto source = make_file(pr_source, 2);
  359. auto target = make_file(pr_target, 2);
  360. auto source_file = folder_peer->get_file_infos().by_name(pr_source.name());
  361. builder.append_block(*source_file, 0, "12345").append_block(*source_file, 1, "67890").apply(*sup);
  362. auto target_file = folder_peer->get_file_infos().by_name(pr_target.name());
  363. auto blocks = source_file->get_blocks();
  364. auto fb_1 = model::file_block_t(blocks[0].get(), target_file.get(), 0);
  365. auto fb_2 = model::file_block_t(blocks[1].get(), target_file.get(), 1);
  366. builder.clone_block(fb_1).clone_block(fb_2).apply(*sup).finish_file(*target).apply(*sup);
  367. auto filename = std::string(target_file->get_name());
  368. auto path = root_path / filename;
  369. REQUIRE(bfs::exists(path));
  370. REQUIRE(bfs::file_size(path) == 10);
  371. auto data = read_file(path);
  372. CHECK(data == "1234567890");
  373. }
  374. SECTION("source/target different sizes") {
  375. pr_source.set_size(5ul);
  376. pr_target.set_size(10ul);
  377. auto target = make_file(pr_target, 2);
  378. pr_source.set_sequence(folder_peer->get_max_sequence() + 1);
  379. auto source = file_info_t::create(sequencer->next_uuid(), pr_source, folder_peer).value();
  380. source->assign_block(blocks[1], 0);
  381. REQUIRE(folder_peer->add_strict(source));
  382. auto source_file = folder_peer->get_file_infos().by_name(pr_source.name());
  383. auto target_file = folder_peer->get_file_infos().by_name(pr_target.name());
  384. builder.append_block(*source_file, 0, "67890").append_block(*target_file, 0, "12345").apply(*sup);
  385. auto blocks = source_file->get_blocks();
  386. auto fb = model::file_block_t(blocks[0].get(), target_file.get(), 1);
  387. builder.clone_block(fb).apply(*sup).finish_file(*target).apply(*sup);
  388. auto filename = std::string(target_file->get_name());
  389. auto path = root_path / filename;
  390. REQUIRE(bfs::exists(path));
  391. REQUIRE(bfs::file_size(path) == 10);
  392. auto data = read_file(path);
  393. CHECK(data == "1234567890");
  394. }
  395. }
  396. SECTION("source & target are is the same file") {
  397. pr_source.set_sequence(folder_peer->get_max_sequence() + 1);
  398. pr_source.set_size(10ul);
  399. auto source = file_info_t::create(sequencer->next_uuid(), pr_source, folder_peer).value();
  400. source->assign_block(blocks[0], 0);
  401. source->assign_block(blocks[0], 1);
  402. REQUIRE(folder_peer->add_strict(source));
  403. auto source_file = folder_peer->get_file_infos().by_name(pr_source.name());
  404. auto target_file = source_file;
  405. builder.append_block(*source_file, 0, "12345").apply(*sup);
  406. auto block = source_file->get_blocks()[0];
  407. auto file_block = model::file_block_t(block.get(), target_file.get(), 1);
  408. builder.clone_block(file_block).apply(*sup).finish_file(*source).apply(*sup);
  409. auto path = root_path / std::string(target_file->get_name());
  410. REQUIRE(bfs::exists(path));
  411. REQUIRE(bfs::file_size(path) == 10);
  412. auto data = read_file(path);
  413. CHECK(data == "1234512345");
  414. CHECK(bfs::last_write_time(path) == 1641828421);
  415. }
  416. }
  417. };
  418. F().run();
  419. }
  420. void test_requesting_block() {
  421. struct F : fixture_t {
  422. void main() noexcept override {
  423. auto bi = proto::BlockInfo();
  424. bi.set_size(5);
  425. bi.set_weak_hash(12);
  426. bi.set_hash(utils::sha256_digest("12345").value());
  427. bi.set_offset(0);
  428. auto b = block_info_t::create(bi).value();
  429. auto bi2 = proto::BlockInfo();
  430. bi2.set_size(5);
  431. bi2.set_weak_hash(12);
  432. bi2.set_hash(utils::sha256_digest("67890").value());
  433. bi2.set_offset(0);
  434. auto b2 = block_info_t::create(bi2).value();
  435. cluster->get_blocks().put(b);
  436. cluster->get_blocks().put(b2);
  437. bfs::path target = root_path / "a.txt";
  438. std::int64_t modified = 1641828421;
  439. proto::FileInfo pr_source;
  440. pr_source.set_name("a.txt");
  441. pr_source.set_block_size(5ul);
  442. pr_source.set_modified_s(modified);
  443. pr_source.set_size(10);
  444. pr_source.set_sequence(folder_my->get_max_sequence() + 1);
  445. auto version = pr_source.mutable_version();
  446. auto counter = version->add_counters();
  447. counter->set_id(1);
  448. counter->set_value(my_device->device_id().get_uint());
  449. *pr_source.add_blocks() = bi;
  450. *pr_source.add_blocks() = bi2;
  451. auto file = file_info_t::create(sequencer->next_uuid(), pr_source, folder_my).value();
  452. file->assign_block(b, 0);
  453. file->assign_block(b2, 1);
  454. folder_my->add_relaxed(file);
  455. auto req = proto::Request();
  456. req.set_folder(std::string(folder->get_id()));
  457. req.set_name("a.txt");
  458. req.set_offset(0);
  459. req.set_size(5);
  460. auto req_ptr = proto::message::Request(new proto::Request(req));
  461. auto msg = r::make_message<fs::payload::block_request_t>(file_actor->get_address(), std::move(req_ptr),
  462. sup->get_address());
  463. SECTION("error, no file") {
  464. sup->put(msg);
  465. sup->do_process();
  466. REQUIRE(block_reply);
  467. REQUIRE(block_reply->payload.remote_request);
  468. REQUIRE(block_reply->payload.ec);
  469. REQUIRE(block_reply->payload.data.empty());
  470. }
  471. SECTION("error, oversized request") {
  472. write_file(target, "1234");
  473. sup->put(msg);
  474. sup->do_process();
  475. REQUIRE(block_reply);
  476. REQUIRE(block_reply->payload.remote_request);
  477. REQUIRE(block_reply->payload.ec);
  478. REQUIRE(block_reply->payload.data.empty());
  479. }
  480. SECTION("successful file reading") {
  481. write_file(target, "1234567890");
  482. sup->put(msg);
  483. sup->do_process();
  484. REQUIRE(block_reply);
  485. REQUIRE(block_reply->payload.remote_request);
  486. REQUIRE(!block_reply->payload.ec);
  487. REQUIRE(block_reply->payload.data == "12345");
  488. req.set_offset(5);
  489. auto req_ptr = proto::message::Request(new proto::Request(req));
  490. auto msg = r::make_message<fs::payload::block_request_t>(file_actor->get_address(), std::move(req_ptr),
  491. sup->get_address());
  492. sup->put(msg);
  493. sup->do_process();
  494. REQUIRE(block_reply);
  495. REQUIRE(block_reply->payload.remote_request);
  496. REQUIRE(!block_reply->payload.ec);
  497. REQUIRE(block_reply->payload.data == "67890");
  498. }
  499. }
  500. };
  501. F().run();
  502. }
  503. void test_conflicts() {
  504. struct F : fixture_t {
  505. void main() noexcept override {
  506. auto builder = diff_builder_t(*cluster, file_addr);
  507. auto &blocks_map = cluster->get_blocks();
  508. proto::FileInfo pr_fi;
  509. std::int64_t modified = 1641828421;
  510. pr_fi.set_name("q.txt");
  511. pr_fi.set_modified_s(modified);
  512. pr_fi.set_sequence(folder_peer->get_max_sequence() + 1);
  513. SECTION("non-empty (via file_finish)") {
  514. pr_fi.set_block_size(5);
  515. pr_fi.set_size(5);
  516. auto peer_block = []() {
  517. auto block = proto::BlockInfo();
  518. block.set_hash(utils::sha256_digest("12345").value());
  519. block.set_offset(0);
  520. block.set_size(5);
  521. auto bi = block_info_t::create(block).value();
  522. return bi;
  523. }();
  524. blocks_map.put(peer_block);
  525. auto my_block = []() {
  526. auto block = proto::BlockInfo();
  527. block.set_hash(utils::sha256_digest("67890").value());
  528. block.set_offset(0);
  529. block.set_size(5);
  530. auto bi = block_info_t::create(block).value();
  531. return bi;
  532. }();
  533. blocks_map.put(my_block);
  534. SECTION("remote win") {
  535. auto peer_file = [&]() {
  536. auto counter = pr_fi.mutable_version()->add_counters();
  537. counter->set_id(peer_device->device_id().get_uint());
  538. counter->set_value(10);
  539. *pr_fi.add_blocks() = peer_block->as_bep(0);
  540. pr_fi.set_modified_s(1734690000);
  541. auto file = file_info_t::create(sequencer->next_uuid(), pr_fi, folder_peer).value();
  542. REQUIRE(folder_peer->add_strict(file));
  543. pr_fi.mutable_version()->clear_counters();
  544. pr_fi.clear_blocks();
  545. file->assign_block(peer_block, 0);
  546. return file;
  547. }();
  548. auto my_file = [&]() {
  549. auto counter = pr_fi.mutable_version()->add_counters();
  550. counter->set_id(my_device->device_id().get_uint());
  551. counter->set_value(5);
  552. *pr_fi.add_blocks() = my_block->as_bep(0);
  553. pr_fi.set_modified_s(1734600000);
  554. auto file = file_info_t::create(sequencer->next_uuid(), pr_fi, folder_my).value();
  555. REQUIRE(folder_my->add_strict(file));
  556. file->mark_local();
  557. file->assign_block(my_block, 0);
  558. return file;
  559. }();
  560. bfs::path kept_file = root_path / pr_fi.name();
  561. write_file(kept_file, "12345");
  562. REQUIRE(model::resolve(*peer_file) == advance_action_t::resolve_remote_win);
  563. builder.append_block(*peer_file, 0, "67890").apply(*sup).finish_file(*peer_file).apply(*sup);
  564. auto conflict_file = root_path / my_file->make_conflicting_name();
  565. CHECK(read_file(kept_file) == "67890");
  566. CHECK(bfs::exists(conflict_file));
  567. CHECK(read_file(conflict_file) == "12345");
  568. }
  569. }
  570. SECTION("remote win emtpy (file vs directory)") {
  571. auto peer_file = [&]() {
  572. auto counter = pr_fi.mutable_version()->add_counters();
  573. counter->set_id(peer_device->device_id().get_uint());
  574. counter->set_value(10);
  575. pr_fi.set_modified_s(1734690000);
  576. auto file = file_info_t::create(sequencer->next_uuid(), pr_fi, folder_peer).value();
  577. REQUIRE(folder_peer->add_strict(file));
  578. pr_fi.mutable_version()->clear_counters();
  579. return file;
  580. }();
  581. auto my_file = [&]() {
  582. auto counter = pr_fi.mutable_version()->add_counters();
  583. counter->set_id(my_device->device_id().get_uint());
  584. counter->set_value(5);
  585. pr_fi.set_modified_s(1734600000);
  586. auto file = file_info_t::create(sequencer->next_uuid(), pr_fi, folder_my).value();
  587. REQUIRE(folder_my->add_strict(file));
  588. file->mark_local();
  589. return file;
  590. }();
  591. bfs::path kept_file = root_path / pr_fi.name();
  592. bfs::create_directories(kept_file);
  593. builder.advance(*peer_file).apply(*sup);
  594. auto conflict_file = root_path / my_file->make_conflicting_name();
  595. CHECK(bfs::exists(conflict_file));
  596. CHECK(bfs::exists(kept_file));
  597. CHECK(bfs::is_directory(conflict_file));
  598. CHECK(bfs::is_regular_file(kept_file));
  599. }
  600. }
  601. };
  602. F().run();
  603. }
  604. int _init() {
  605. REGISTER_TEST_CASE(test_remote_copy, "test_remote_copy", "[fs]");
  606. REGISTER_TEST_CASE(test_append_block, "test_append_block", "[fs]");
  607. REGISTER_TEST_CASE(test_clone_block, "test_clone_block", "[fs]");
  608. REGISTER_TEST_CASE(test_requesting_block, "test_requesting_block", "[fs]");
  609. REGISTER_TEST_CASE(test_conflicts, "test_conflicts", "[fs]");
  610. return 1;
  611. }
  612. static int v = _init();