051-model-file_info.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "catch.hpp"
  2. #include "test-utils.h"
  3. #include "model/file_info.h"
  4. #include "model/cluster.h"
  5. using namespace syncspirit;
  6. using namespace syncspirit::test;
  7. using namespace syncspirit::model;
  8. namespace bfs = boost::filesystem;
  9. TEST_CASE("block iterator", "[model]") {
  10. db::Device db_d1;
  11. db_d1.set_id(test::device_id2sha256("KHQNO2S-5QSILRK-YX4JZZ4-7L77APM-QNVGZJT-EKU7IFI-PNEPBMY-4MXFMQD"));
  12. db_d1.set_name("d1");
  13. db_d1.set_cert_name("d1_cert_name");
  14. auto d1 = model::device_ptr_t(new model::device_t(db_d1, 2));
  15. db::Folder db_f;
  16. db_f.set_id("2");
  17. db_f.set_label("f2-label");
  18. db_f.set_path("/some/path/d2");
  19. auto folder = model::folder_ptr_t(new model::folder_t(db_f, 1));
  20. cluster_ptr_t cluster = new cluster_t(d1);
  21. auto folders = model::folders_map_t();
  22. folders.put(folder);
  23. cluster->assign_folders(std::move(folders));
  24. folder->assign_cluster(cluster.get());
  25. folder->assign_device(d1);
  26. SECTION("no blocks") {
  27. proto::FileInfo info;
  28. auto file = file_info_t(info, folder.get());
  29. auto bi = file.iterate_blocks();
  30. REQUIRE(!bi);
  31. }
  32. SECTION("a few remote blocks") {
  33. proto::BlockInfo b1;
  34. b1.set_hash("h1");
  35. b1.set_size(1024);
  36. proto::BlockInfo b2;
  37. b2.set_hash("h2");
  38. b2.set_size(9);
  39. proto::FileInfo info;
  40. *info.add_blocks() = b1;
  41. *info.add_blocks() = b2;
  42. auto file = file_info_t(info, folder.get());
  43. SECTION("normal iteration") {
  44. auto bi = file.iterate_blocks();
  45. REQUIRE((bool)bi);
  46. auto r1 = bi.next();
  47. REQUIRE((bool)bi);
  48. CHECK(r1.block_index == 0);
  49. CHECK(r1.block->get_hash() == "h1");
  50. auto r2 = bi.next();
  51. REQUIRE(!bi);
  52. CHECK(r2.block_index == 1);
  53. CHECK(r2.block->get_hash() == "h2");
  54. REQUIRE(!bi);
  55. }
  56. SECTION("a block is locally available") {
  57. file.mark_local_available(0);
  58. auto bi = file.iterate_blocks();
  59. REQUIRE((bool)bi);
  60. auto r1 = bi.next();
  61. REQUIRE((bool)bi);
  62. CHECK(r1.block_index == 0);
  63. CHECK(r1.block->get_hash() == "h1");
  64. CHECK(r1.block->local_file().file_info == &file);
  65. auto r2 = bi.next();
  66. REQUIRE(!bi);
  67. CHECK(r2.block_index == 1);
  68. CHECK(r2.block->get_hash() == "h2");
  69. REQUIRE(!bi);
  70. }
  71. SECTION("iteration, then reset") {
  72. auto bi = file.iterate_blocks();
  73. REQUIRE((bool)bi);
  74. auto r1 = bi.next();
  75. REQUIRE((bool)bi);
  76. CHECK(r1.block_index == 0);
  77. CHECK(r1.block->get_hash() == "h1");
  78. bi.reset();
  79. REQUIRE(!bi);
  80. }
  81. SECTION("no iteration on sync") {
  82. file.mark_sync();
  83. auto bi = file.iterate_blocks();
  84. REQUIRE(!bi);
  85. }
  86. SECTION("no iteration on newer") {
  87. file.mark_newver();
  88. auto bi = file.iterate_blocks();
  89. REQUIRE(!bi);
  90. }
  91. }
  92. }