071-fs_actor.cpp 29 KB

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