Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 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 | // |
Darin Petkov | 7a22d79 | 2010-11-08 14:10:00 -0800 | [diff] [blame] | 16 | |
Alex Deymo | aab50e3 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 17 | #include "update_engine/payload_generator/full_update_generator.h" |
| 18 | |
Darin Petkov | 7a22d79 | 2010-11-08 14:10:00 -0800 | [diff] [blame] | 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
| 22 | #include <gtest/gtest.h> |
| 23 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 24 | #include "update_engine/common/test_utils.h" |
| 25 | #include "update_engine/payload_consumer/payload_constants.h" |
Alex Deymo | 6c396a9 | 2015-06-30 19:00:51 -0700 | [diff] [blame] | 26 | #include "update_engine/payload_generator/extent_utils.h" |
Darin Petkov | 7a22d79 | 2010-11-08 14:10:00 -0800 | [diff] [blame] | 27 | |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 28 | using chromeos_update_engine::test_utils::FillWithData; |
Darin Petkov | 7a22d79 | 2010-11-08 14:10:00 -0800 | [diff] [blame] | 29 | using std::string; |
| 30 | using std::vector; |
| 31 | |
| 32 | namespace chromeos_update_engine { |
| 33 | |
Alex Deymo | f1cbe17 | 2015-03-05 15:58:37 -0800 | [diff] [blame] | 34 | class FullUpdateGeneratorTest : public ::testing::Test { |
| 35 | protected: |
| 36 | void SetUp() override { |
| 37 | config_.is_delta = false; |
Alex Deymo | cbf0989 | 2015-09-11 16:13:16 -0700 | [diff] [blame] | 38 | config_.minor_version = kFullPayloadMinorVersion; |
Alex Deymo | 2d3b2d6 | 2015-07-17 17:34:36 -0700 | [diff] [blame] | 39 | config_.hard_chunk_size = 128 * 1024; |
Alex Deymo | f1cbe17 | 2015-03-05 15:58:37 -0800 | [diff] [blame] | 40 | config_.block_size = 4096; |
Alex Deymo | 6c396a9 | 2015-06-30 19:00:51 -0700 | [diff] [blame] | 41 | |
Sen Jiang | ebdf17d | 2015-08-19 11:53:27 -0700 | [diff] [blame] | 42 | EXPECT_TRUE(utils::MakeTempFile("FullUpdateTest_partition.XXXXXX", |
| 43 | &new_part_conf.path, |
Alex Deymo | 6c396a9 | 2015-06-30 19:00:51 -0700 | [diff] [blame] | 44 | nullptr)); |
| 45 | EXPECT_TRUE(utils::MakeTempFile("FullUpdateTest_blobs.XXXXXX", |
| 46 | &out_blobs_path_, |
| 47 | &out_blobs_fd_)); |
| 48 | |
Sen Jiang | 8cc502d | 2015-08-10 10:04:54 -0700 | [diff] [blame] | 49 | blob_file_.reset(new BlobFileWriter(out_blobs_fd_, &out_blobs_length_)); |
Sen Jiang | ebdf17d | 2015-08-19 11:53:27 -0700 | [diff] [blame] | 50 | part_path_unlinker_.reset(new ScopedPathUnlinker(new_part_conf.path)); |
Alex Deymo | 6c396a9 | 2015-06-30 19:00:51 -0700 | [diff] [blame] | 51 | out_blobs_unlinker_.reset(new ScopedPathUnlinker(out_blobs_path_)); |
Alex Deymo | f1cbe17 | 2015-03-05 15:58:37 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | PayloadGenerationConfig config_; |
Sen Jiang | 625406c | 2015-09-16 16:35:23 -0700 | [diff] [blame] | 55 | PartitionConfig new_part_conf{"part"}; |
Sen Jiang | ebdf17d | 2015-08-19 11:53:27 -0700 | [diff] [blame] | 56 | |
| 57 | vector<AnnotatedOperation> aops; |
Alex Deymo | 6c396a9 | 2015-06-30 19:00:51 -0700 | [diff] [blame] | 58 | |
| 59 | // Output file holding the payload blobs. |
| 60 | string out_blobs_path_; |
| 61 | int out_blobs_fd_{-1}; |
Sen Jiang | 8cc502d | 2015-08-10 10:04:54 -0700 | [diff] [blame] | 62 | off_t out_blobs_length_{0}; |
Alex Deymo | 6c396a9 | 2015-06-30 19:00:51 -0700 | [diff] [blame] | 63 | ScopedFdCloser out_blobs_fd_closer_{&out_blobs_fd_}; |
| 64 | |
Sen Jiang | 8cc502d | 2015-08-10 10:04:54 -0700 | [diff] [blame] | 65 | std::unique_ptr<BlobFileWriter> blob_file_; |
Sen Jiang | ebdf17d | 2015-08-19 11:53:27 -0700 | [diff] [blame] | 66 | std::unique_ptr<ScopedPathUnlinker> part_path_unlinker_; |
Alex Deymo | 6c396a9 | 2015-06-30 19:00:51 -0700 | [diff] [blame] | 67 | std::unique_ptr<ScopedPathUnlinker> out_blobs_unlinker_; |
| 68 | |
| 69 | // FullUpdateGenerator under test. |
| 70 | FullUpdateGenerator generator_; |
Alex Deymo | f1cbe17 | 2015-03-05 15:58:37 -0800 | [diff] [blame] | 71 | }; |
Darin Petkov | 7a22d79 | 2010-11-08 14:10:00 -0800 | [diff] [blame] | 72 | |
Alex Deymo | f1cbe17 | 2015-03-05 15:58:37 -0800 | [diff] [blame] | 73 | TEST_F(FullUpdateGeneratorTest, RunTest) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 74 | brillo::Blob new_part(9 * 1024 * 1024); |
Sen Jiang | ebdf17d | 2015-08-19 11:53:27 -0700 | [diff] [blame] | 75 | FillWithData(&new_part); |
| 76 | new_part_conf.size = new_part.size(); |
Alex Deymo | f1cbe17 | 2015-03-05 15:58:37 -0800 | [diff] [blame] | 77 | |
Sen Jiang | ebdf17d | 2015-08-19 11:53:27 -0700 | [diff] [blame] | 78 | EXPECT_TRUE(test_utils::WriteFileVector(new_part_conf.path, new_part)); |
Darin Petkov | 7a22d79 | 2010-11-08 14:10:00 -0800 | [diff] [blame] | 79 | |
Alex Deymo | 6c396a9 | 2015-06-30 19:00:51 -0700 | [diff] [blame] | 80 | EXPECT_TRUE(generator_.GenerateOperations(config_, |
Sen Jiang | ebdf17d | 2015-08-19 11:53:27 -0700 | [diff] [blame] | 81 | new_part_conf, // this is ignored |
| 82 | new_part_conf, |
Sen Jiang | 8cc502d | 2015-08-10 10:04:54 -0700 | [diff] [blame] | 83 | blob_file_.get(), |
Sen Jiang | ebdf17d | 2015-08-19 11:53:27 -0700 | [diff] [blame] | 84 | &aops)); |
| 85 | int64_t new_part_chunks = new_part_conf.size / config_.hard_chunk_size; |
Alex Deymo | 5fe0c4e | 2016-02-16 18:46:24 -0800 | [diff] [blame] | 86 | EXPECT_EQ(new_part_chunks, static_cast<int64_t>(aops.size())); |
Sen Jiang | ebdf17d | 2015-08-19 11:53:27 -0700 | [diff] [blame] | 87 | for (off_t i = 0; i < new_part_chunks; ++i) { |
| 88 | EXPECT_EQ(1, aops[i].op.dst_extents_size()); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 89 | EXPECT_EQ(static_cast<uint64_t>(i * config_.hard_chunk_size / config_.block_size), |
Sen Jiang | ebdf17d | 2015-08-19 11:53:27 -0700 | [diff] [blame] | 90 | aops[i].op.dst_extents(0).start_block()) << "i = " << i; |
Alex Deymo | 2d3b2d6 | 2015-07-17 17:34:36 -0700 | [diff] [blame] | 91 | EXPECT_EQ(config_.hard_chunk_size / config_.block_size, |
Sen Jiang | ebdf17d | 2015-08-19 11:53:27 -0700 | [diff] [blame] | 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()); |
Darin Petkov | 7a22d79 | 2010-11-08 14:10:00 -0800 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
Alex Deymo | 6c396a9 | 2015-06-30 19:00:51 -0700 | [diff] [blame] | 99 | // Test that if the chunk size is not a divisor of the image size, it handles |
Sen Jiang | ebdf17d | 2015-08-19 11:53:27 -0700 | [diff] [blame] | 100 | // correctly the last chunk of the partition. |
Alex Deymo | 6c396a9 | 2015-06-30 19:00:51 -0700 | [diff] [blame] | 101 | TEST_F(FullUpdateGeneratorTest, ChunkSizeTooBig) { |
Alex Deymo | 2d3b2d6 | 2015-07-17 17:34:36 -0700 | [diff] [blame] | 102 | config_.hard_chunk_size = 1024 * 1024; |
| 103 | config_.soft_chunk_size = config_.hard_chunk_size; |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 104 | brillo::Blob new_part(1536 * 1024); // 1.5 MiB |
Sen Jiang | ebdf17d | 2015-08-19 11:53:27 -0700 | [diff] [blame] | 105 | new_part_conf.size = new_part.size(); |
Alex Deymo | 6c396a9 | 2015-06-30 19:00:51 -0700 | [diff] [blame] | 106 | |
Sen Jiang | ebdf17d | 2015-08-19 11:53:27 -0700 | [diff] [blame] | 107 | EXPECT_TRUE(test_utils::WriteFileVector(new_part_conf.path, new_part)); |
Alex Deymo | 6c396a9 | 2015-06-30 19:00:51 -0700 | [diff] [blame] | 108 | |
| 109 | EXPECT_TRUE(generator_.GenerateOperations(config_, |
Sen Jiang | ebdf17d | 2015-08-19 11:53:27 -0700 | [diff] [blame] | 110 | new_part_conf, // this is ignored |
| 111 | new_part_conf, |
Sen Jiang | 8cc502d | 2015-08-10 10:04:54 -0700 | [diff] [blame] | 112 | blob_file_.get(), |
Sen Jiang | ebdf17d | 2015-08-19 11:53:27 -0700 | [diff] [blame] | 113 | &aops)); |
| 114 | // new_part has one chunk and a half. |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 115 | EXPECT_EQ(2U, aops.size()); |
Alex Deymo | 2d3b2d6 | 2015-07-17 17:34:36 -0700 | [diff] [blame] | 116 | EXPECT_EQ(config_.hard_chunk_size / config_.block_size, |
Sen Jiang | ebdf17d | 2015-08-19 11:53:27 -0700 | [diff] [blame] | 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 | } |
Alex Deymo | 6c396a9 | 2015-06-30 19:00:51 -0700 | [diff] [blame] | 121 | |
Sen Jiang | ebdf17d | 2015-08-19 11:53:27 -0700 | [diff] [blame] | 122 | // Test that if the image size is much smaller than the chunk size, it handles |
| 123 | // correctly the only chunk of the partition. |
| 124 | TEST_F(FullUpdateGeneratorTest, ImageSizeTooSmall) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 125 | brillo::Blob new_part(16 * 1024); |
Sen Jiang | ebdf17d | 2015-08-19 11:53:27 -0700 | [diff] [blame] | 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. |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 137 | EXPECT_EQ(1U, aops.size()); |
Sen Jiang | ebdf17d | 2015-08-19 11:53:27 -0700 | [diff] [blame] | 138 | EXPECT_EQ(new_part.size() / config_.block_size, |
| 139 | BlocksInExtents(aops[0].op.dst_extents())); |
Alex Deymo | 6c396a9 | 2015-06-30 19:00:51 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Darin Petkov | 7a22d79 | 2010-11-08 14:10:00 -0800 | [diff] [blame] | 142 | } // namespace chromeos_update_engine |