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 
28 using chromeos_update_engine::test_utils::FillWithData;
29 using std::string;
30 using std::vector;
31 
32 namespace chromeos_update_engine {
33 
34 class FullUpdateGeneratorTest : public ::testing::Test {
35  protected:
SetUp()36   void SetUp() override {
37     config_.is_delta = false;
38     config_.version.minor = 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 
TEST_F(FullUpdateGeneratorTest,RunTest)73 TEST_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(
90         static_cast<uint64_t>(i * config_.hard_chunk_size / config_.block_size),
91         aops[i].op.dst_extents(0).start_block())
92         << "i = " << i;
93     EXPECT_EQ(config_.hard_chunk_size / config_.block_size,
94               aops[i].op.dst_extents(0).num_blocks());
95     if (aops[i].op.type() != InstallOperation::REPLACE) {
96       EXPECT_EQ(InstallOperation::REPLACE_BZ, aops[i].op.type());
97     }
98   }
99 }
100 
101 // Test that if the chunk size is not a divisor of the image size, it handles
102 // correctly the last chunk of the partition.
TEST_F(FullUpdateGeneratorTest,ChunkSizeTooBig)103 TEST_F(FullUpdateGeneratorTest, ChunkSizeTooBig) {
104   config_.hard_chunk_size = 1024 * 1024;
105   config_.soft_chunk_size = config_.hard_chunk_size;
106   brillo::Blob new_part(1536 * 1024);  // 1.5 MiB
107   new_part_conf.size = new_part.size();
108 
109   EXPECT_TRUE(test_utils::WriteFileVector(new_part_conf.path, new_part));
110 
111   EXPECT_TRUE(generator_.GenerateOperations(config_,
112                                             new_part_conf,  // this is ignored
113                                             new_part_conf,
114                                             blob_file_.get(),
115                                             &aops));
116   // new_part has one chunk and a half.
117   EXPECT_EQ(2U, aops.size());
118   EXPECT_EQ(config_.hard_chunk_size / config_.block_size,
119             BlocksInExtents(aops[0].op.dst_extents()));
120   EXPECT_EQ((new_part.size() - config_.hard_chunk_size) / config_.block_size,
121             BlocksInExtents(aops[1].op.dst_extents()));
122 }
123 
124 // Test that if the image size is much smaller than the chunk size, it handles
125 // correctly the only chunk of the partition.
TEST_F(FullUpdateGeneratorTest,ImageSizeTooSmall)126 TEST_F(FullUpdateGeneratorTest, ImageSizeTooSmall) {
127   brillo::Blob new_part(16 * 1024);
128   new_part_conf.size = new_part.size();
129 
130   EXPECT_TRUE(test_utils::WriteFileVector(new_part_conf.path, new_part));
131 
132   EXPECT_TRUE(generator_.GenerateOperations(config_,
133                                             new_part_conf,  // this is ignored
134                                             new_part_conf,
135                                             blob_file_.get(),
136                                             &aops));
137 
138   // new_part has less than one chunk.
139   EXPECT_EQ(1U, aops.size());
140   EXPECT_EQ(new_part.size() / config_.block_size,
141             BlocksInExtents(aops[0].op.dst_extents()));
142 }
143 
144 }  // namespace chromeos_update_engine
145