deflate_utils_unittest.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //
  2. // Copyright (C) 2017 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/deflate_utils.h"
  17. #include <unistd.h>
  18. #include <algorithm>
  19. #include <utility>
  20. #include <vector>
  21. #include <gtest/gtest.h>
  22. #include "update_engine/common/test_utils.h"
  23. #include "update_engine/common/utils.h"
  24. #include "update_engine/payload_generator/extent_ranges.h"
  25. #include "update_engine/payload_generator/extent_utils.h"
  26. using puffin::BitExtent;
  27. using puffin::ByteExtent;
  28. using std::vector;
  29. namespace chromeos_update_engine {
  30. namespace deflate_utils {
  31. // This creates a sudo-random BitExtents from ByteExtents for simpler testing.
  32. vector<BitExtent> ByteToBitExtent(const vector<ByteExtent>& byte_extents) {
  33. vector<BitExtent> bit_extents;
  34. for (auto& byte_extent : byte_extents) {
  35. bit_extents.emplace_back(byte_extent.offset * 8 + (byte_extent.offset & 7),
  36. byte_extent.length * 8 - (byte_extent.length & 7));
  37. }
  38. return bit_extents;
  39. }
  40. TEST(DeflateUtilsTest, ExtentsShiftTest) {
  41. vector<Extent> base_extents = {ExtentForRange(10, 10),
  42. ExtentForRange(70, 10),
  43. ExtentForRange(50, 10),
  44. ExtentForRange(30, 10),
  45. ExtentForRange(90, 10)};
  46. vector<Extent> over_extents = {ExtentForRange(2, 2),
  47. ExtentForRange(5, 2),
  48. ExtentForRange(7, 3),
  49. ExtentForRange(13, 10),
  50. ExtentForRange(25, 20),
  51. ExtentForRange(47, 3)};
  52. vector<Extent> out_over_extents = {ExtentForRange(12, 2),
  53. ExtentForRange(15, 2),
  54. ExtentForRange(17, 3),
  55. ExtentForRange(73, 7),
  56. ExtentForRange(50, 3),
  57. ExtentForRange(55, 5),
  58. ExtentForRange(30, 10),
  59. ExtentForRange(90, 5),
  60. ExtentForRange(97, 3)};
  61. EXPECT_TRUE(ShiftExtentsOverExtents(base_extents, &over_extents));
  62. EXPECT_EQ(over_extents, out_over_extents);
  63. // Failure case
  64. base_extents = {ExtentForRange(10, 10)};
  65. over_extents = {ExtentForRange(2, 12)};
  66. EXPECT_FALSE(ShiftExtentsOverExtents(base_extents, &over_extents));
  67. }
  68. TEST(DeflateUtilsTest, ShiftBitExtentsOverExtentsTest) {
  69. vector<Extent> base_extents = {ExtentForRange(3, 1),
  70. ExtentForRange(1, 1),
  71. ExtentForRange(5, 1),
  72. ExtentForRange(7, 1),
  73. ExtentForRange(9, 1)};
  74. vector<BitExtent> over_extents =
  75. ByteToBitExtent({{0, 0}, {100, 2000}, {4096, 0}, {5000, 5000}});
  76. vector<BitExtent> out_over_extents =
  77. ByteToBitExtent({{12288, 0}, {12388, 2000}, {4096, 0}});
  78. ASSERT_TRUE(ShiftBitExtentsOverExtents(base_extents, &over_extents));
  79. EXPECT_EQ(over_extents, out_over_extents);
  80. }
  81. TEST(DeflateUtilsTest, ShiftBitExtentsOverExtentsBoundaryTest) {
  82. vector<Extent> base_extents = {ExtentForRange(1, 1)};
  83. vector<BitExtent> over_extents = ByteToBitExtent({{2, 4096}});
  84. vector<BitExtent> out_over_extents = {};
  85. EXPECT_FALSE(ShiftBitExtentsOverExtents(base_extents, &over_extents));
  86. base_extents = {ExtentForRange(1, 1)};
  87. over_extents = {};
  88. out_over_extents = {};
  89. EXPECT_TRUE(ShiftBitExtentsOverExtents(base_extents, &over_extents));
  90. EXPECT_EQ(over_extents, out_over_extents);
  91. base_extents = {};
  92. over_extents = {};
  93. out_over_extents = {};
  94. EXPECT_TRUE(ShiftBitExtentsOverExtents(base_extents, &over_extents));
  95. EXPECT_EQ(over_extents, out_over_extents);
  96. base_extents = {};
  97. over_extents = ByteToBitExtent({{0, 1}});
  98. out_over_extents = ByteToBitExtent({{0, 1}});
  99. EXPECT_FALSE(ShiftBitExtentsOverExtents(base_extents, &over_extents));
  100. EXPECT_EQ(over_extents, out_over_extents);
  101. base_extents = {ExtentForRange(1, 2)};
  102. over_extents = ByteToBitExtent({{0, 3 * 4096}, {4 * 4096, 4096}});
  103. out_over_extents = ByteToBitExtent({{0, 3 * 4096}, {4 * 4096, 4096}});
  104. EXPECT_FALSE(ShiftBitExtentsOverExtents(base_extents, &over_extents));
  105. EXPECT_EQ(over_extents, out_over_extents);
  106. }
  107. TEST(DeflateUtilsTest, FindDeflatesTest) {
  108. vector<Extent> extents = {
  109. ExtentForRange(1, 1), ExtentForRange(3, 1), ExtentForRange(5, 1)};
  110. vector<BitExtent> in_deflates = ByteToBitExtent({{0, 0},
  111. {10, 400},
  112. {4096, 0},
  113. {3000, 2000},
  114. {4096, 100},
  115. {4097, 100},
  116. {8100, 92},
  117. {8100, 93},
  118. {8100, 6000},
  119. {25000, 1}});
  120. vector<BitExtent> expected_out_deflates =
  121. ByteToBitExtent({{4096, 0}, {4096, 100}, {4097, 100}, {8100, 92}});
  122. vector<BitExtent> out_deflates;
  123. out_deflates = FindDeflates(extents, in_deflates);
  124. EXPECT_EQ(out_deflates, expected_out_deflates);
  125. }
  126. TEST(DeflateUtilsTest, FindDeflatesBoundaryTest) {
  127. vector<Extent> extents = {};
  128. vector<BitExtent> in_deflates = ByteToBitExtent({{0, 0}, {8100, 93}});
  129. vector<BitExtent> expected_out_deflates = {};
  130. vector<BitExtent> out_deflates;
  131. out_deflates = FindDeflates(extents, in_deflates);
  132. EXPECT_EQ(out_deflates, expected_out_deflates);
  133. extents = {};
  134. in_deflates = {};
  135. out_deflates = FindDeflates(extents, in_deflates);
  136. EXPECT_EQ(out_deflates, expected_out_deflates);
  137. }
  138. TEST(DeflateUtilsTest, CompactTest) {
  139. vector<Extent> extents = {
  140. ExtentForRange(1, 1), ExtentForRange(5, 1), ExtentForRange(3, 1)};
  141. vector<BitExtent> in_deflates =
  142. ByteToBitExtent({{4096, 0}, {12288, 4096}, {4096, 100}, {20480, 100}});
  143. vector<BitExtent> expected_out_deflates =
  144. ByteToBitExtent({{0, 0}, {0, 100}, {4096, 100}, {8192, 4096}});
  145. vector<BitExtent> out_deflates;
  146. ASSERT_TRUE(CompactDeflates(extents, in_deflates, &out_deflates));
  147. EXPECT_EQ(out_deflates, expected_out_deflates);
  148. }
  149. TEST(DeflateUtilsTest, CompactBoundaryTest) {
  150. vector<Extent> extents = {};
  151. vector<BitExtent> in_deflates = ByteToBitExtent({{4096, 0}});
  152. vector<BitExtent> expected_out_deflates = {};
  153. vector<BitExtent> out_deflates;
  154. EXPECT_FALSE(CompactDeflates(extents, in_deflates, &out_deflates));
  155. EXPECT_EQ(out_deflates, expected_out_deflates);
  156. extents = {};
  157. in_deflates = {};
  158. ASSERT_TRUE(CompactDeflates(extents, in_deflates, &out_deflates));
  159. EXPECT_EQ(out_deflates, expected_out_deflates);
  160. extents = {ExtentForRange(1, 1)};
  161. in_deflates = {};
  162. ASSERT_TRUE(CompactDeflates(extents, in_deflates, &out_deflates));
  163. EXPECT_EQ(out_deflates, expected_out_deflates);
  164. }
  165. } // namespace deflate_utils
  166. } // namespace chromeos_update_engine