full_update_generator_unittest.cc revision 5fe0c4ede81b82ae3425ddbbb698eceef14cbc78
1//
2// Copyright (C) 2010 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/full_update_generator.h"
18
19#include <string>
20#include <vector>
21
22#include <gtest/gtest.h>
23
24#include "update_engine/common/test_utils.h"
25#include "update_engine/payload_consumer/payload_constants.h"
26#include "update_engine/payload_generator/extent_utils.h"
27
28using chromeos_update_engine::test_utils::FillWithData;
29using std::string;
30using std::vector;
31
32namespace chromeos_update_engine {
33
34class FullUpdateGeneratorTest : public ::testing::Test {
35 protected:
36  void SetUp() override {
37    config_.is_delta = false;
38    config_.minor_version = kFullPayloadMinorVersion;
39    config_.hard_chunk_size = 128 * 1024;
40    config_.block_size = 4096;
41
42    EXPECT_TRUE(utils::MakeTempFile("FullUpdateTest_partition.XXXXXX",
43                                    &new_part_conf.path,
44                                    nullptr));
45    EXPECT_TRUE(utils::MakeTempFile("FullUpdateTest_blobs.XXXXXX",
46                                    &out_blobs_path_,
47                                    &out_blobs_fd_));
48
49    blob_file_.reset(new BlobFileWriter(out_blobs_fd_, &out_blobs_length_));
50    part_path_unlinker_.reset(new ScopedPathUnlinker(new_part_conf.path));
51    out_blobs_unlinker_.reset(new ScopedPathUnlinker(out_blobs_path_));
52  }
53
54  PayloadGenerationConfig config_;
55  PartitionConfig new_part_conf{"part"};
56
57  vector<AnnotatedOperation> aops;
58
59  // Output file holding the payload blobs.
60  string out_blobs_path_;
61  int out_blobs_fd_{-1};
62  off_t out_blobs_length_{0};
63  ScopedFdCloser out_blobs_fd_closer_{&out_blobs_fd_};
64
65  std::unique_ptr<BlobFileWriter> blob_file_;
66  std::unique_ptr<ScopedPathUnlinker> part_path_unlinker_;
67  std::unique_ptr<ScopedPathUnlinker> out_blobs_unlinker_;
68
69  // FullUpdateGenerator under test.
70  FullUpdateGenerator generator_;
71};
72
73TEST_F(FullUpdateGeneratorTest, RunTest) {
74  brillo::Blob new_part(9 * 1024 * 1024);
75  FillWithData(&new_part);
76  new_part_conf.size = new_part.size();
77
78  EXPECT_TRUE(test_utils::WriteFileVector(new_part_conf.path, new_part));
79
80  EXPECT_TRUE(generator_.GenerateOperations(config_,
81                                            new_part_conf,  // this is ignored
82                                            new_part_conf,
83                                            blob_file_.get(),
84                                            &aops));
85  int64_t new_part_chunks = new_part_conf.size / config_.hard_chunk_size;
86  EXPECT_EQ(new_part_chunks, static_cast<int64_t>(aops.size()));
87  for (off_t i = 0; i < new_part_chunks; ++i) {
88    EXPECT_EQ(1, aops[i].op.dst_extents_size());
89    EXPECT_EQ(static_cast<uint64_t>(i * config_.hard_chunk_size / config_.block_size),
90              aops[i].op.dst_extents(0).start_block()) << "i = " << i;
91    EXPECT_EQ(config_.hard_chunk_size / config_.block_size,
92              aops[i].op.dst_extents(0).num_blocks());
93    if (aops[i].op.type() != InstallOperation::REPLACE) {
94      EXPECT_EQ(InstallOperation::REPLACE_BZ, aops[i].op.type());
95    }
96  }
97}
98
99// Test that if the chunk size is not a divisor of the image size, it handles
100// correctly the last chunk of the partition.
101TEST_F(FullUpdateGeneratorTest, ChunkSizeTooBig) {
102  config_.hard_chunk_size = 1024 * 1024;
103  config_.soft_chunk_size = config_.hard_chunk_size;
104  brillo::Blob new_part(1536 * 1024);  // 1.5 MiB
105  new_part_conf.size = new_part.size();
106
107  EXPECT_TRUE(test_utils::WriteFileVector(new_part_conf.path, new_part));
108
109  EXPECT_TRUE(generator_.GenerateOperations(config_,
110                                            new_part_conf,  // this is ignored
111                                            new_part_conf,
112                                            blob_file_.get(),
113                                            &aops));
114  // new_part has one chunk and a half.
115  EXPECT_EQ(2U, aops.size());
116  EXPECT_EQ(config_.hard_chunk_size / config_.block_size,
117            BlocksInExtents(aops[0].op.dst_extents()));
118  EXPECT_EQ((new_part.size() - config_.hard_chunk_size) / config_.block_size,
119            BlocksInExtents(aops[1].op.dst_extents()));
120}
121
122// Test that if the image size is much smaller than the chunk size, it handles
123// correctly the only chunk of the partition.
124TEST_F(FullUpdateGeneratorTest, ImageSizeTooSmall) {
125  brillo::Blob new_part(16 * 1024);
126  new_part_conf.size = new_part.size();
127
128  EXPECT_TRUE(test_utils::WriteFileVector(new_part_conf.path, new_part));
129
130  EXPECT_TRUE(generator_.GenerateOperations(config_,
131                                            new_part_conf,  // this is ignored
132                                            new_part_conf,
133                                            blob_file_.get(),
134                                            &aops));
135
136  // new_part has less than one chunk.
137  EXPECT_EQ(1U, aops.size());
138  EXPECT_EQ(new_part.size() / config_.block_size,
139            BlocksInExtents(aops[0].op.dst_extents()));
140}
141
142}  // namespace chromeos_update_engine
143