021-orphaned-blocks.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. // SPDX-FileCopyrightText: 2024 Ivan Baidakou
  3. #include "test-utils.h"
  4. #include "access.h"
  5. #include "model/device.h"
  6. #include "model/cluster.h"
  7. #include "model/misc/orphaned_blocks.h"
  8. #include "diff-builder.h"
  9. using namespace syncspirit;
  10. using namespace syncspirit::model;
  11. using namespace syncspirit::proto;
  12. using namespace syncspirit::test;
  13. namespace bfs = std::filesystem;
  14. TEST_CASE("orphaned blocks, all removed for single file", "[model]") {
  15. test::init_logging();
  16. auto my_id = device_id_t::from_string("KHQNO2S-5QSILRK-YX4JZZ4-7L77APM-QNVGZJT-EKU7IFI-PNEPBMY-4MXFMQD").value();
  17. auto my_device = device_t::create(my_id, "my-device").value();
  18. auto cluster = cluster_ptr_t(new cluster_t(my_device, 1));
  19. cluster->get_devices().put(my_device);
  20. auto builder = diff_builder_t(*cluster);
  21. REQUIRE(builder.upsert_folder("fid", "/some/path").apply());
  22. auto b1_hash = utils::sha256_digest("12345").value();
  23. auto b2_hash = utils::sha256_digest("56789").value();
  24. auto b3_hash = utils::sha256_digest("00000").value();
  25. SECTION("1 file with 2 different blocsk erased") {
  26. proto::FileInfo pr_file;
  27. pr_file.set_name("a.txt");
  28. pr_file.set_block_size(5ul);
  29. pr_file.set_size(10ul);
  30. auto b1 = pr_file.add_blocks();
  31. b1->set_hash(b1_hash);
  32. b1->set_offset(0);
  33. b1->set_size(5);
  34. auto b2 = pr_file.add_blocks();
  35. b2->set_hash(b2_hash);
  36. b2->set_offset(5ul);
  37. b2->set_size(5);
  38. auto bi1 = model::block_info_t::create(*b1).value();
  39. auto bi2 = model::block_info_t::create(*b2).value();
  40. REQUIRE(builder.local_update("fid", pr_file).apply());
  41. auto &folder_infos = cluster->get_folders().by_id("fid")->get_folder_infos();
  42. auto &file_infos = folder_infos.by_device(*my_device)->get_file_infos();
  43. auto file = file_infos.by_name("a.txt");
  44. REQUIRE(file);
  45. auto orphans = model::orphaned_blocks_t();
  46. orphans.record(*file);
  47. auto blocks = orphans.deduce();
  48. REQUIRE(blocks.size() == 2);
  49. CHECK(blocks.contains(bi1->get_key()));
  50. CHECK(blocks.contains(bi2->get_key()));
  51. }
  52. SECTION("1 file with 2 same blocks erased") {
  53. proto::FileInfo pr_file;
  54. pr_file.set_name("a.txt");
  55. pr_file.set_block_size(5ul);
  56. pr_file.set_size(10ul);
  57. auto b1 = pr_file.add_blocks();
  58. b1->set_hash(b1_hash);
  59. b1->set_offset(0);
  60. b1->set_size(5);
  61. auto b2 = pr_file.add_blocks();
  62. b2->set_hash(b1_hash);
  63. b2->set_offset(5ul);
  64. b2->set_size(5);
  65. auto bi = model::block_info_t::create(*b1).value();
  66. REQUIRE(builder.local_update("fid", pr_file).apply());
  67. auto &folder_infos = cluster->get_folders().by_id("fid")->get_folder_infos();
  68. auto &file_infos = folder_infos.by_device(*my_device)->get_file_infos();
  69. auto file = file_infos.by_name("a.txt");
  70. REQUIRE(file);
  71. auto orphans = model::orphaned_blocks_t();
  72. orphans.record(*file);
  73. auto blocks = orphans.deduce();
  74. REQUIRE(blocks.size() == 1);
  75. CHECK(blocks.contains(bi->get_key()));
  76. }
  77. SECTION("2 file with 1 shared erased") {
  78. proto::FileInfo pr_file_1;
  79. pr_file_1.set_name("a.txt");
  80. pr_file_1.set_block_size(5ul);
  81. pr_file_1.set_size(10ul);
  82. auto b_1_1 = pr_file_1.add_blocks();
  83. b_1_1->set_hash(b1_hash);
  84. b_1_1->set_offset(0);
  85. b_1_1->set_size(5);
  86. auto b_1_2 = pr_file_1.add_blocks();
  87. b_1_2->set_hash(b2_hash);
  88. b_1_2->set_offset(5ul);
  89. b_1_2->set_size(5);
  90. proto::FileInfo pr_file_2;
  91. pr_file_2.set_name("b.txt");
  92. pr_file_2.set_block_size(5ul);
  93. pr_file_2.set_size(10ul);
  94. auto b_2_1 = pr_file_2.add_blocks();
  95. b_2_1->set_hash(b1_hash);
  96. b_2_1->set_offset(0);
  97. b_2_1->set_size(5);
  98. auto b_2_2 = pr_file_2.add_blocks();
  99. b_2_2->set_hash(b3_hash);
  100. b_2_2->set_offset(5ul);
  101. b_2_2->set_size(5);
  102. auto bi_1 = model::block_info_t::create(*b_1_1).value();
  103. auto bi_2 = model::block_info_t::create(*b_1_2).value();
  104. auto bi_3 = model::block_info_t::create(*b_2_2).value();
  105. REQUIRE(builder.local_update("fid", pr_file_1).apply());
  106. REQUIRE(builder.local_update("fid", pr_file_2).apply());
  107. auto &folder_infos = cluster->get_folders().by_id("fid")->get_folder_infos();
  108. auto &file_infos = folder_infos.by_device(*my_device)->get_file_infos();
  109. auto file_1 = file_infos.by_name("a.txt");
  110. auto file_2 = file_infos.by_name("b.txt");
  111. REQUIRE(file_1);
  112. REQUIRE(file_2);
  113. auto orphans = model::orphaned_blocks_t();
  114. orphans.record(*file_1);
  115. auto blocks = orphans.deduce();
  116. REQUIRE(blocks.size() == 1);
  117. CHECK(blocks.contains(bi_2->get_key()));
  118. orphans.record(*file_2);
  119. blocks = orphans.deduce();
  120. REQUIRE(blocks.size() == 3);
  121. CHECK(blocks.contains(bi_1->get_key()));
  122. CHECK(blocks.contains(bi_2->get_key()));
  123. CHECK(blocks.contains(bi_3->get_key()));
  124. }
  125. }