Alex Deymo | f006135 | 2015-07-01 14:59:15 -0700 | [diff] [blame^] | 1 | // Copyright 2015 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "update_engine/payload_generator/block_mapping.h" |
| 6 | |
| 7 | #include <fcntl.h> |
| 8 | #include <sys/stat.h> |
| 9 | #include <sys/types.h> |
| 10 | |
| 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
| 14 | #include <gtest/gtest.h> |
| 15 | |
| 16 | #include "update_engine/test_utils.h" |
| 17 | #include "update_engine/utils.h" |
| 18 | |
| 19 | using std::string; |
| 20 | using std::vector; |
| 21 | |
| 22 | namespace chromeos_update_engine { |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | } // namespace |
| 27 | |
| 28 | class BlockMappingTest : public ::testing::Test { |
| 29 | protected: |
| 30 | void SetUp() override { |
| 31 | EXPECT_TRUE(utils::MakeTempFile("BlockMappingTest_old.XXXXXX", |
| 32 | &old_part_path_, |
| 33 | nullptr)); |
| 34 | EXPECT_TRUE(utils::MakeTempFile("BlockMappingTest_new.XXXXXX", |
| 35 | &new_part_path_, |
| 36 | nullptr)); |
| 37 | |
| 38 | old_part_unlinker_.reset(new ScopedPathUnlinker(old_part_path_)); |
| 39 | new_part_unlinker_.reset(new ScopedPathUnlinker(new_part_path_)); |
| 40 | } |
| 41 | |
| 42 | // Old new partition files used in testing. |
| 43 | string old_part_path_; |
| 44 | string new_part_path_; |
| 45 | std::unique_ptr<ScopedPathUnlinker> old_part_unlinker_; |
| 46 | std::unique_ptr<ScopedPathUnlinker> new_part_unlinker_; |
| 47 | |
| 48 | size_t block_size_{1024}; |
| 49 | BlockMapping bm_{block_size_}; // BlockMapping under test. |
| 50 | }; |
| 51 | |
| 52 | TEST_F(BlockMappingTest, FirstAddedBlockIsZero) { |
| 53 | chromeos::Blob blob(block_size_); |
| 54 | // The BlockMapping just assigns the block ids in order, so it doesn't matter |
| 55 | // what are the contents of the first block. |
| 56 | blob[0] = 42; |
| 57 | EXPECT_EQ(0, bm_.AddBlock(blob)); |
| 58 | blob[0] = 5; |
| 59 | EXPECT_EQ(1, bm_.AddBlock(blob)); |
| 60 | } |
| 61 | |
| 62 | TEST_F(BlockMappingTest, BlocksAreNotKeptInMemory) { |
| 63 | test_utils::WriteFileString(old_part_path_, string(block_size_, 'a')); |
| 64 | int old_fd = HANDLE_EINTR(open(old_part_path_.c_str(), O_RDONLY)); |
| 65 | ScopedFdCloser old_fd_closer(&old_fd); |
| 66 | |
| 67 | EXPECT_EQ(0, bm_.AddDiskBlock(old_fd, 0)); |
| 68 | |
| 69 | // Check that the block_data is not stored on memory if we just used the block |
| 70 | // once. |
| 71 | for (const auto& it : bm_.mapping_) { |
| 72 | for (const BlockMapping::UniqueBlock& ublock : it.second) { |
| 73 | EXPECT_TRUE(ublock.block_data.empty()); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | chromeos::Blob block(block_size_, 'a'); |
| 78 | for (int i = 0; i < 5; ++i) { |
| 79 | // Re-add the same block 5 times. |
| 80 | EXPECT_EQ(0, bm_.AddBlock(block)); |
| 81 | } |
| 82 | |
| 83 | for (const auto& it : bm_.mapping_) { |
| 84 | for (const BlockMapping::UniqueBlock& ublock : it.second) { |
| 85 | EXPECT_FALSE(ublock.block_data.empty()); |
| 86 | // The block was loaded from disk only 4 times, and after that the counter |
| 87 | // is not updated anymore. |
| 88 | EXPECT_EQ(4, ublock.times_read); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | TEST_F(BlockMappingTest, MapPartitionBlocks) { |
| 94 | // A string with 10 blocks where all the blocks are different. |
| 95 | string old_contents(10 * block_size_, '\0'); |
| 96 | for (size_t i = 0; i < old_contents.size(); ++i) |
| 97 | old_contents[i] = 4 + i / block_size_; |
| 98 | test_utils::WriteFileString(old_part_path_, old_contents); |
| 99 | |
| 100 | // A string including the block with all zeros and overlapping some of the |
| 101 | // other blocks in old_contents. |
| 102 | string new_contents(6 * block_size_, '\0'); |
| 103 | for (size_t i = 0; i < new_contents.size(); ++i) |
| 104 | new_contents[i] = i / block_size_; |
| 105 | test_utils::WriteFileString(new_part_path_, new_contents); |
| 106 | |
| 107 | vector<BlockMapping::BlockId> old_ids, new_ids; |
| 108 | EXPECT_TRUE(MapPartitionBlocks(old_part_path_, |
| 109 | new_part_path_, |
| 110 | old_contents.size(), |
| 111 | new_contents.size(), |
| 112 | block_size_, |
| 113 | &old_ids, |
| 114 | &new_ids)); |
| 115 | |
| 116 | EXPECT_EQ((vector<BlockMapping::BlockId>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}), |
| 117 | old_ids); |
| 118 | EXPECT_EQ((vector<BlockMapping::BlockId>{0, 11, 12, 13, 1, 2}), |
| 119 | new_ids); |
| 120 | } |
| 121 | |
| 122 | } // namespace chromeos_update_engine |