blob: d5af9d0d23dd03f950c0ad14167224d3ff7f2c0c [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Petkov7a22d792010-11-08 14:10:00 -080016
Alex Deymoaab50e32014-11-10 19:55:35 -080017#include "update_engine/payload_generator/full_update_generator.h"
18
Darin Petkov7a22d792010-11-08 14:10:00 -080019#include <string>
20#include <vector>
21
22#include <gtest/gtest.h>
23
Alex Deymo39910dc2015-11-09 17:04:30 -080024#include "update_engine/common/test_utils.h"
25#include "update_engine/payload_consumer/payload_constants.h"
Alex Deymo6c396a92015-06-30 19:00:51 -070026#include "update_engine/payload_generator/extent_utils.h"
Darin Petkov7a22d792010-11-08 14:10:00 -080027
Alex Deymo10875d92014-11-10 21:52:57 -080028using chromeos_update_engine::test_utils::FillWithData;
Darin Petkov7a22d792010-11-08 14:10:00 -080029using std::string;
30using std::vector;
31
32namespace chromeos_update_engine {
33
Alex Deymof1cbe172015-03-05 15:58:37 -080034class FullUpdateGeneratorTest : public ::testing::Test {
35 protected:
36 void SetUp() override {
37 config_.is_delta = false;
Alex Deymocbf09892015-09-11 16:13:16 -070038 config_.minor_version = kFullPayloadMinorVersion;
Alex Deymo2d3b2d62015-07-17 17:34:36 -070039 config_.hard_chunk_size = 128 * 1024;
Alex Deymof1cbe172015-03-05 15:58:37 -080040 config_.block_size = 4096;
Alex Deymo6c396a92015-06-30 19:00:51 -070041
Sen Jiangebdf17d2015-08-19 11:53:27 -070042 EXPECT_TRUE(utils::MakeTempFile("FullUpdateTest_partition.XXXXXX",
43 &new_part_conf.path,
Alex Deymo6c396a92015-06-30 19:00:51 -070044 nullptr));
45 EXPECT_TRUE(utils::MakeTempFile("FullUpdateTest_blobs.XXXXXX",
46 &out_blobs_path_,
47 &out_blobs_fd_));
48
Sen Jiang8cc502d2015-08-10 10:04:54 -070049 blob_file_.reset(new BlobFileWriter(out_blobs_fd_, &out_blobs_length_));
Sen Jiangebdf17d2015-08-19 11:53:27 -070050 part_path_unlinker_.reset(new ScopedPathUnlinker(new_part_conf.path));
Alex Deymo6c396a92015-06-30 19:00:51 -070051 out_blobs_unlinker_.reset(new ScopedPathUnlinker(out_blobs_path_));
Alex Deymof1cbe172015-03-05 15:58:37 -080052 }
53
54 PayloadGenerationConfig config_;
Sen Jiang625406c2015-09-16 16:35:23 -070055 PartitionConfig new_part_conf{"part"};
Sen Jiangebdf17d2015-08-19 11:53:27 -070056
57 vector<AnnotatedOperation> aops;
Alex Deymo6c396a92015-06-30 19:00:51 -070058
59 // Output file holding the payload blobs.
60 string out_blobs_path_;
61 int out_blobs_fd_{-1};
Sen Jiang8cc502d2015-08-10 10:04:54 -070062 off_t out_blobs_length_{0};
Alex Deymo6c396a92015-06-30 19:00:51 -070063 ScopedFdCloser out_blobs_fd_closer_{&out_blobs_fd_};
64
Sen Jiang8cc502d2015-08-10 10:04:54 -070065 std::unique_ptr<BlobFileWriter> blob_file_;
Sen Jiangebdf17d2015-08-19 11:53:27 -070066 std::unique_ptr<ScopedPathUnlinker> part_path_unlinker_;
Alex Deymo6c396a92015-06-30 19:00:51 -070067 std::unique_ptr<ScopedPathUnlinker> out_blobs_unlinker_;
68
69 // FullUpdateGenerator under test.
70 FullUpdateGenerator generator_;
Alex Deymof1cbe172015-03-05 15:58:37 -080071};
Darin Petkov7a22d792010-11-08 14:10:00 -080072
Alex Deymof1cbe172015-03-05 15:58:37 -080073TEST_F(FullUpdateGeneratorTest, RunTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070074 brillo::Blob new_part(9 * 1024 * 1024);
Sen Jiangebdf17d2015-08-19 11:53:27 -070075 FillWithData(&new_part);
76 new_part_conf.size = new_part.size();
Alex Deymof1cbe172015-03-05 15:58:37 -080077
Sen Jiangebdf17d2015-08-19 11:53:27 -070078 EXPECT_TRUE(test_utils::WriteFileVector(new_part_conf.path, new_part));
Darin Petkov7a22d792010-11-08 14:10:00 -080079
Alex Deymo6c396a92015-06-30 19:00:51 -070080 EXPECT_TRUE(generator_.GenerateOperations(config_,
Sen Jiangebdf17d2015-08-19 11:53:27 -070081 new_part_conf, // this is ignored
82 new_part_conf,
Sen Jiang8cc502d2015-08-10 10:04:54 -070083 blob_file_.get(),
Sen Jiangebdf17d2015-08-19 11:53:27 -070084 &aops));
85 int64_t new_part_chunks = new_part_conf.size / config_.hard_chunk_size;
Alex Deymo5fe0c4e2016-02-16 18:46:24 -080086 EXPECT_EQ(new_part_chunks, static_cast<int64_t>(aops.size()));
Sen Jiangebdf17d2015-08-19 11:53:27 -070087 for (off_t i = 0; i < new_part_chunks; ++i) {
88 EXPECT_EQ(1, aops[i].op.dst_extents_size());
Alex Deymo80f70ff2016-02-10 16:08:11 -080089 EXPECT_EQ(static_cast<uint64_t>(i * config_.hard_chunk_size / config_.block_size),
Sen Jiangebdf17d2015-08-19 11:53:27 -070090 aops[i].op.dst_extents(0).start_block()) << "i = " << i;
Alex Deymo2d3b2d62015-07-17 17:34:36 -070091 EXPECT_EQ(config_.hard_chunk_size / config_.block_size,
Sen Jiangebdf17d2015-08-19 11:53:27 -070092 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 Petkov7a22d792010-11-08 14:10:00 -080095 }
96 }
97}
98
Alex Deymo6c396a92015-06-30 19:00:51 -070099// Test that if the chunk size is not a divisor of the image size, it handles
Sen Jiangebdf17d2015-08-19 11:53:27 -0700100// correctly the last chunk of the partition.
Alex Deymo6c396a92015-06-30 19:00:51 -0700101TEST_F(FullUpdateGeneratorTest, ChunkSizeTooBig) {
Alex Deymo2d3b2d62015-07-17 17:34:36 -0700102 config_.hard_chunk_size = 1024 * 1024;
103 config_.soft_chunk_size = config_.hard_chunk_size;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700104 brillo::Blob new_part(1536 * 1024); // 1.5 MiB
Sen Jiangebdf17d2015-08-19 11:53:27 -0700105 new_part_conf.size = new_part.size();
Alex Deymo6c396a92015-06-30 19:00:51 -0700106
Sen Jiangebdf17d2015-08-19 11:53:27 -0700107 EXPECT_TRUE(test_utils::WriteFileVector(new_part_conf.path, new_part));
Alex Deymo6c396a92015-06-30 19:00:51 -0700108
109 EXPECT_TRUE(generator_.GenerateOperations(config_,
Sen Jiangebdf17d2015-08-19 11:53:27 -0700110 new_part_conf, // this is ignored
111 new_part_conf,
Sen Jiang8cc502d2015-08-10 10:04:54 -0700112 blob_file_.get(),
Sen Jiangebdf17d2015-08-19 11:53:27 -0700113 &aops));
114 // new_part has one chunk and a half.
Alex Deymo80f70ff2016-02-10 16:08:11 -0800115 EXPECT_EQ(2U, aops.size());
Alex Deymo2d3b2d62015-07-17 17:34:36 -0700116 EXPECT_EQ(config_.hard_chunk_size / config_.block_size,
Sen Jiangebdf17d2015-08-19 11:53:27 -0700117 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 Deymo6c396a92015-06-30 19:00:51 -0700121
Sen Jiangebdf17d2015-08-19 11:53:27 -0700122// 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) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700125 brillo::Blob new_part(16 * 1024);
Sen Jiangebdf17d2015-08-19 11:53:27 -0700126 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 Deymo80f70ff2016-02-10 16:08:11 -0800137 EXPECT_EQ(1U, aops.size());
Sen Jiangebdf17d2015-08-19 11:53:27 -0700138 EXPECT_EQ(new_part.size() / config_.block_size,
139 BlocksInExtents(aops[0].op.dst_extents()));
Alex Deymo6c396a92015-06-30 19:00:51 -0700140}
141
Darin Petkov7a22d792010-11-08 14:10:00 -0800142} // namespace chromeos_update_engine