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