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
45 blob_file_writer_.reset(
46 new BlobFileWriter(blob_file_.fd(), &out_blobs_length_));
47 }
48
49 PayloadGenerationConfig config_;
50 PartitionConfig new_part_conf{"part"};
51
52 vector<AnnotatedOperation> aops;
53
54 // Output file holding the payload blobs.
55 off_t out_blobs_length_{0};
56 ScopedTempFile part_file_{"FullUpdateTest_partition.XXXXXX"};
57
58 ScopedTempFile blob_file_{"FullUpdateTest_blobs.XXXXXX", true};
59 std::unique_ptr<BlobFileWriter> blob_file_writer_;
60
61 // FullUpdateGenerator under test.
62 FullUpdateGenerator generator_;
63 };
64
TEST_F(FullUpdateGeneratorTest,RunTest)65 TEST_F(FullUpdateGeneratorTest, RunTest) {
66 brillo::Blob new_part(9 * 1024 * 1024);
67 FillWithData(&new_part);
68 new_part_conf.size = new_part.size();
69
70 EXPECT_TRUE(test_utils::WriteFileVector(new_part_conf.path, new_part));
71
72 EXPECT_TRUE(generator_.GenerateOperations(config_,
73 new_part_conf, // this is ignored
74 new_part_conf,
75 blob_file_writer_.get(),
76 &aops));
77 int64_t new_part_chunks = new_part_conf.size / config_.hard_chunk_size;
78 EXPECT_EQ(new_part_chunks, static_cast<int64_t>(aops.size()));
79 for (off_t i = 0; i < new_part_chunks; ++i) {
80 EXPECT_EQ(1, aops[i].op.dst_extents_size());
81 EXPECT_EQ(
82 static_cast<uint64_t>(i * config_.hard_chunk_size / config_.block_size),
83 aops[i].op.dst_extents(0).start_block())
84 << "i = " << i;
85 EXPECT_EQ(config_.hard_chunk_size / config_.block_size,
86 aops[i].op.dst_extents(0).num_blocks());
87 if (aops[i].op.type() != InstallOperation::REPLACE) {
88 EXPECT_EQ(InstallOperation::REPLACE_XZ, aops[i].op.type());
89 }
90 }
91 }
92
93 // Test that if the chunk size is not a divisor of the image size, it handles
94 // correctly the last chunk of the partition.
TEST_F(FullUpdateGeneratorTest,ChunkSizeTooBig)95 TEST_F(FullUpdateGeneratorTest, ChunkSizeTooBig) {
96 config_.hard_chunk_size = 1024 * 1024;
97 config_.soft_chunk_size = config_.hard_chunk_size;
98 brillo::Blob new_part(1536 * 1024); // 1.5 MiB
99 new_part_conf.size = new_part.size();
100
101 EXPECT_TRUE(test_utils::WriteFileVector(new_part_conf.path, new_part));
102
103 EXPECT_TRUE(generator_.GenerateOperations(config_,
104 new_part_conf, // this is ignored
105 new_part_conf,
106 blob_file_writer_.get(),
107 &aops));
108 // new_part has one chunk and a half.
109 EXPECT_EQ(2U, aops.size());
110 EXPECT_EQ(config_.hard_chunk_size / config_.block_size,
111 utils::BlocksInExtents(aops[0].op.dst_extents()));
112 EXPECT_EQ((new_part.size() - config_.hard_chunk_size) / config_.block_size,
113 utils::BlocksInExtents(aops[1].op.dst_extents()));
114 }
115
116 // Test that if the image size is much smaller than the chunk size, it handles
117 // correctly the only chunk of the partition.
TEST_F(FullUpdateGeneratorTest,ImageSizeTooSmall)118 TEST_F(FullUpdateGeneratorTest, ImageSizeTooSmall) {
119 brillo::Blob new_part(16 * 1024);
120 new_part_conf.size = new_part.size();
121
122 EXPECT_TRUE(test_utils::WriteFileVector(new_part_conf.path, new_part));
123
124 EXPECT_TRUE(generator_.GenerateOperations(config_,
125 new_part_conf, // this is ignored
126 new_part_conf,
127 blob_file_writer_.get(),
128 &aops));
129
130 // new_part has less than one chunk.
131 EXPECT_EQ(1U, aops.size());
132 EXPECT_EQ(new_part.size() / config_.block_size,
133 utils::BlocksInExtents(aops[0].op.dst_extents()));
134 }
135
136 } // namespace chromeos_update_engine
137