block_mapping_unittest.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/block_mapping.h"
  17. #include <fcntl.h>
  18. #include <sys/stat.h>
  19. #include <sys/types.h>
  20. #include <string>
  21. #include <vector>
  22. #include <gtest/gtest.h>
  23. #include "update_engine/common/test_utils.h"
  24. #include "update_engine/common/utils.h"
  25. using std::string;
  26. using std::vector;
  27. namespace chromeos_update_engine {
  28. class BlockMappingTest : public ::testing::Test {
  29. protected:
  30. // Old new partition files used in testing.
  31. test_utils::ScopedTempFile old_part_{"BlockMappingTest_old.XXXXXX"};
  32. test_utils::ScopedTempFile new_part_{"BlockMappingTest_new.XXXXXX"};
  33. size_t block_size_{1024};
  34. BlockMapping bm_{block_size_}; // BlockMapping under test.
  35. };
  36. TEST_F(BlockMappingTest, FirstAddedBlockIsZero) {
  37. brillo::Blob blob(block_size_);
  38. // The BlockMapping just assigns the block ids in order, so it doesn't matter
  39. // what are the contents of the first block.
  40. blob[0] = 42;
  41. EXPECT_EQ(0, bm_.AddBlock(blob));
  42. blob[0] = 5;
  43. EXPECT_EQ(1, bm_.AddBlock(blob));
  44. }
  45. TEST_F(BlockMappingTest, BlocksAreNotKeptInMemory) {
  46. test_utils::WriteFileString(old_part_.path(), string(block_size_, 'a'));
  47. int old_fd = HANDLE_EINTR(open(old_part_.path().c_str(), O_RDONLY));
  48. ScopedFdCloser old_fd_closer(&old_fd);
  49. EXPECT_EQ(0, bm_.AddDiskBlock(old_fd, 0));
  50. // Check that the block_data is not stored on memory if we just used the block
  51. // once.
  52. for (const auto& it : bm_.mapping_) {
  53. for (const BlockMapping::UniqueBlock& ublock : it.second) {
  54. EXPECT_TRUE(ublock.block_data.empty());
  55. }
  56. }
  57. brillo::Blob block(block_size_, 'a');
  58. for (int i = 0; i < 5; ++i) {
  59. // Re-add the same block 5 times.
  60. EXPECT_EQ(0, bm_.AddBlock(block));
  61. }
  62. for (const auto& it : bm_.mapping_) {
  63. for (const BlockMapping::UniqueBlock& ublock : it.second) {
  64. EXPECT_FALSE(ublock.block_data.empty());
  65. // The block was loaded from disk only 4 times, and after that the counter
  66. // is not updated anymore.
  67. EXPECT_EQ(4U, ublock.times_read);
  68. }
  69. }
  70. }
  71. TEST_F(BlockMappingTest, MapPartitionBlocks) {
  72. // A string with 10 blocks where all the blocks are different.
  73. string old_contents(10 * block_size_, '\0');
  74. for (size_t i = 0; i < old_contents.size(); ++i)
  75. old_contents[i] = 4 + i / block_size_;
  76. test_utils::WriteFileString(old_part_.path(), old_contents);
  77. // A string including the block with all zeros and overlapping some of the
  78. // other blocks in old_contents.
  79. string new_contents(6 * block_size_, '\0');
  80. for (size_t i = 0; i < new_contents.size(); ++i)
  81. new_contents[i] = i / block_size_;
  82. test_utils::WriteFileString(new_part_.path(), new_contents);
  83. vector<BlockMapping::BlockId> old_ids, new_ids;
  84. EXPECT_TRUE(MapPartitionBlocks(old_part_.path(),
  85. new_part_.path(),
  86. old_contents.size(),
  87. new_contents.size(),
  88. block_size_,
  89. &old_ids,
  90. &new_ids));
  91. EXPECT_EQ((vector<BlockMapping::BlockId>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}),
  92. old_ids);
  93. EXPECT_EQ((vector<BlockMapping::BlockId>{0, 11, 12, 13, 1, 2}), new_ids);
  94. }
  95. } // namespace chromeos_update_engine