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