extent_utils_unittest.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // Copyright (C) 2015 The Android Open Source Project
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #include "update_engine/payload_generator/extent_utils.h"
  17. #include <utility>
  18. #include <vector>
  19. #include <gtest/gtest.h>
  20. #include "update_engine/common/test_utils.h"
  21. #include "update_engine/payload_consumer/payload_constants.h"
  22. #include "update_engine/payload_generator/extent_ranges.h"
  23. using std::vector;
  24. namespace chromeos_update_engine {
  25. class ExtentUtilsTest : public ::testing::Test {};
  26. TEST(ExtentUtilsTest, AppendSparseToExtentsTest) {
  27. vector<Extent> extents;
  28. EXPECT_EQ(0U, extents.size());
  29. AppendBlockToExtents(&extents, kSparseHole);
  30. EXPECT_EQ(1U, extents.size());
  31. AppendBlockToExtents(&extents, 0);
  32. EXPECT_EQ(2U, extents.size());
  33. AppendBlockToExtents(&extents, kSparseHole);
  34. AppendBlockToExtents(&extents, kSparseHole);
  35. ASSERT_EQ(3U, extents.size());
  36. EXPECT_EQ(kSparseHole, extents[0].start_block());
  37. EXPECT_EQ(1U, extents[0].num_blocks());
  38. EXPECT_EQ(0U, extents[1].start_block());
  39. EXPECT_EQ(1U, extents[1].num_blocks());
  40. EXPECT_EQ(kSparseHole, extents[2].start_block());
  41. EXPECT_EQ(2U, extents[2].num_blocks());
  42. }
  43. TEST(ExtentUtilsTest, BlocksInExtentsTest) {
  44. {
  45. vector<Extent> extents;
  46. EXPECT_EQ(0U, utils::BlocksInExtents(extents));
  47. extents.push_back(ExtentForRange(0, 1));
  48. EXPECT_EQ(1U, utils::BlocksInExtents(extents));
  49. extents.push_back(ExtentForRange(23, 55));
  50. EXPECT_EQ(56U, utils::BlocksInExtents(extents));
  51. extents.push_back(ExtentForRange(1, 2));
  52. EXPECT_EQ(58U, utils::BlocksInExtents(extents));
  53. }
  54. {
  55. google::protobuf::RepeatedPtrField<Extent> extents;
  56. EXPECT_EQ(0U, utils::BlocksInExtents(extents));
  57. *extents.Add() = ExtentForRange(0, 1);
  58. EXPECT_EQ(1U, utils::BlocksInExtents(extents));
  59. *extents.Add() = ExtentForRange(23, 55);
  60. EXPECT_EQ(56U, utils::BlocksInExtents(extents));
  61. *extents.Add() = ExtentForRange(1, 2);
  62. EXPECT_EQ(58U, utils::BlocksInExtents(extents));
  63. }
  64. }
  65. TEST(ExtentUtilsTest, ExtendExtentsTest) {
  66. InstallOperation first_op;
  67. *(first_op.add_src_extents()) = ExtentForRange(1, 1);
  68. *(first_op.add_src_extents()) = ExtentForRange(3, 1);
  69. InstallOperation second_op;
  70. *(second_op.add_src_extents()) = ExtentForRange(4, 2);
  71. *(second_op.add_src_extents()) = ExtentForRange(8, 2);
  72. ExtendExtents(first_op.mutable_src_extents(), second_op.src_extents());
  73. vector<Extent> first_op_vec;
  74. ExtentsToVector(first_op.src_extents(), &first_op_vec);
  75. EXPECT_EQ(
  76. (vector<Extent>{
  77. ExtentForRange(1, 1), ExtentForRange(3, 3), ExtentForRange(8, 2)}),
  78. first_op_vec);
  79. }
  80. TEST(ExtentUtilsTest, NormalizeExtentsSimpleList) {
  81. // Make sure it works when there's just one extent.
  82. vector<Extent> extents;
  83. NormalizeExtents(&extents);
  84. EXPECT_EQ(0U, extents.size());
  85. extents = {ExtentForRange(0, 3)};
  86. NormalizeExtents(&extents);
  87. EXPECT_EQ(1U, extents.size());
  88. EXPECT_EQ(ExtentForRange(0, 3), extents[0]);
  89. }
  90. TEST(ExtentUtilsTest, NormalizeExtentsTest) {
  91. vector<Extent> extents = {ExtentForRange(0, 3),
  92. ExtentForRange(3, 2),
  93. ExtentForRange(5, 1),
  94. ExtentForRange(8, 4),
  95. ExtentForRange(13, 1),
  96. ExtentForRange(14, 2)};
  97. NormalizeExtents(&extents);
  98. EXPECT_EQ(3U, extents.size());
  99. EXPECT_EQ(ExtentForRange(0, 6), extents[0]);
  100. EXPECT_EQ(ExtentForRange(8, 4), extents[1]);
  101. EXPECT_EQ(ExtentForRange(13, 3), extents[2]);
  102. }
  103. TEST(ExtentUtilsTest, ExtentsSublistTest) {
  104. vector<Extent> extents = {
  105. ExtentForRange(10, 10), ExtentForRange(30, 10), ExtentForRange(50, 10)};
  106. // Simple empty result cases.
  107. EXPECT_EQ(vector<Extent>(), ExtentsSublist(extents, 1000, 20));
  108. EXPECT_EQ(vector<Extent>(), ExtentsSublist(extents, 5, 0));
  109. EXPECT_EQ(vector<Extent>(), ExtentsSublist(extents, 30, 1));
  110. // Normal test cases.
  111. EXPECT_EQ(vector<Extent>{ExtentForRange(13, 2)},
  112. ExtentsSublist(extents, 3, 2));
  113. EXPECT_EQ(vector<Extent>{ExtentForRange(15, 5)},
  114. ExtentsSublist(extents, 5, 5));
  115. EXPECT_EQ((vector<Extent>{ExtentForRange(15, 5), ExtentForRange(30, 5)}),
  116. ExtentsSublist(extents, 5, 10));
  117. EXPECT_EQ((vector<Extent>{
  118. ExtentForRange(13, 7),
  119. ExtentForRange(30, 10),
  120. ExtentForRange(50, 3),
  121. }),
  122. ExtentsSublist(extents, 3, 20));
  123. // Extact match case.
  124. EXPECT_EQ(vector<Extent>{ExtentForRange(30, 10)},
  125. ExtentsSublist(extents, 10, 10));
  126. EXPECT_EQ(vector<Extent>{ExtentForRange(50, 10)},
  127. ExtentsSublist(extents, 20, 10));
  128. // Cases where the requested num_blocks is too big.
  129. EXPECT_EQ(vector<Extent>{ExtentForRange(53, 7)},
  130. ExtentsSublist(extents, 23, 100));
  131. EXPECT_EQ((vector<Extent>{ExtentForRange(34, 6), ExtentForRange(50, 10)}),
  132. ExtentsSublist(extents, 14, 100));
  133. }
  134. } // namespace chromeos_update_engine