blob: 4910594b41fd4f003cede5f0169a04366efb932b [file] [log] [blame]
Kelvin Zhang9bd519d2020-09-23 12:55:19 -04001//
2// Copyright (C) 2020 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 <memory>
18#include <vector>
19
20#include <brillo/secure_blob.h>
21#include <gtest/gtest.h>
22
23#include "update_engine/common/dynamic_partition_control_stub.h"
24#include "update_engine/common/error_code.h"
25#include "update_engine/common/fake_prefs.h"
26#include "update_engine/common/hash_calculator.h"
27#include "update_engine/common/test_utils.h"
28#include "update_engine/common/utils.h"
Kelvin Zhang9bd519d2020-09-23 12:55:19 -040029#include "update_engine/payload_consumer/extent_writer.h"
30#include "update_engine/payload_consumer/fake_file_descriptor.h"
31#include "update_engine/payload_consumer/file_descriptor.h"
32#include "update_engine/payload_consumer/install_plan.h"
Kelvin Zhangcfc531f2022-08-24 17:58:53 +000033#include "update_engine/payload_consumer/partition_writer.h"
34#include "update_engine/payload_consumer/payload_constants.h"
Kelvin Zhang9bd519d2020-09-23 12:55:19 -040035#include "update_engine/payload_generator/annotated_operation.h"
36#include "update_engine/payload_generator/delta_diff_generator.h"
37#include "update_engine/payload_generator/extent_ranges.h"
Kelvin Zhang9bd519d2020-09-23 12:55:19 -040038#include "update_engine/payload_generator/payload_generation_config.h"
39#include "update_engine/update_metadata.pb.h"
40
41namespace chromeos_update_engine {
42
43class PartitionWriterTest : public testing::Test {
44 public:
45 // Helper function to pretend that the ECC file descriptor was already opened.
46 // Returns a pointer to the created file descriptor.
47 FakeFileDescriptor* SetFakeECCFile(size_t size) {
Kelvin Zhangab3ce602021-02-24 14:46:40 -050048 EXPECT_FALSE(writer_.verified_source_fd_.source_ecc_fd_)
49 << "source_ecc_fdb already open.";
Kelvin Zhang9bd519d2020-09-23 12:55:19 -040050 FakeFileDescriptor* ret = new FakeFileDescriptor();
51 fake_ecc_fd_.reset(ret);
52 // Call open to simulate it was already opened.
53 ret->Open("", 0);
54 ret->SetFileSize(size);
Kelvin Zhangab3ce602021-02-24 14:46:40 -050055 writer_.verified_source_fd_.source_ecc_fd_ = fake_ecc_fd_;
Kelvin Zhang9bd519d2020-09-23 12:55:19 -040056 return ret;
57 }
58
59 uint64_t GetSourceEccRecoveredFailures() const {
Kelvin Zhangab3ce602021-02-24 14:46:40 -050060 return writer_.verified_source_fd_.source_ecc_recovered_failures_;
Kelvin Zhang9bd519d2020-09-23 12:55:19 -040061 }
62
63 AnnotatedOperation GenerateSourceCopyOp(const brillo::Blob& copied_data,
64 bool add_hash,
65 PartitionConfig* old_part = nullptr) {
66 PayloadGenerationConfig config;
67 const uint64_t kDefaultBlockSize = config.block_size;
68 EXPECT_EQ(0U, copied_data.size() % kDefaultBlockSize);
69 uint64_t num_blocks = copied_data.size() / kDefaultBlockSize;
70 AnnotatedOperation aop;
71 *(aop.op.add_src_extents()) = ExtentForRange(0, num_blocks);
72 *(aop.op.add_dst_extents()) = ExtentForRange(0, num_blocks);
73 aop.op.set_type(InstallOperation::SOURCE_COPY);
74 brillo::Blob src_hash;
75 EXPECT_TRUE(HashCalculator::RawHashOfData(copied_data, &src_hash));
76 if (add_hash)
77 aop.op.set_src_sha256_hash(src_hash.data(), src_hash.size());
78
79 return aop;
80 }
81
82 brillo::Blob PerformSourceCopyOp(const InstallOperation& op,
83 const brillo::Blob blob_data) {
Kelvin Zhangab3ce602021-02-24 14:46:40 -050084 LOG(INFO) << "Using source part " << source_partition.path();
Kelvin Zhang9bd519d2020-09-23 12:55:19 -040085 FileDescriptorPtr fd(new EintrSafeFileDescriptor());
Kelvin Zhang4d22ca22021-02-09 14:06:25 -050086 DirectExtentWriter extent_writer{fd};
Kelvin Zhang9bd519d2020-09-23 12:55:19 -040087 EXPECT_TRUE(fd->Open(source_partition.path().c_str(), O_RDWR));
Kelvin Zhangab3ce602021-02-24 14:46:40 -050088 if (HasFailure()) {
89 return {};
90 }
Kelvin Zhang4d22ca22021-02-09 14:06:25 -050091 EXPECT_TRUE(extent_writer.Init(op.src_extents(), kBlockSize));
Kelvin Zhangab3ce602021-02-24 14:46:40 -050092 if (HasFailure()) {
93 return {};
94 }
Kelvin Zhang9bd519d2020-09-23 12:55:19 -040095 EXPECT_TRUE(extent_writer.Write(blob_data.data(), blob_data.size()));
Kelvin Zhangab3ce602021-02-24 14:46:40 -050096 if (HasFailure()) {
97 return {};
98 }
99 fd->Flush();
Kelvin Zhang9bd519d2020-09-23 12:55:19 -0400100
Kelvin Zhang9bd519d2020-09-23 12:55:19 -0400101 install_part_.source_size = blob_data.size();
102 install_part_.target_size = blob_data.size();
103
104 ErrorCode error;
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400105 EXPECT_TRUE(writer_.Init(&install_plan_, true, 0));
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500106 if (HasFailure()) {
107 return {};
108 }
Kelvin Zhang9bd519d2020-09-23 12:55:19 -0400109 EXPECT_TRUE(writer_.PerformSourceCopyOperation(op, &error));
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400110 writer_.CheckpointUpdateProgress(1);
Kelvin Zhang9bd519d2020-09-23 12:55:19 -0400111
112 brillo::Blob output_data;
113 EXPECT_TRUE(utils::ReadFile(target_partition.path(), &output_data));
114 return output_data;
115 }
116
117 FakePrefs prefs_{};
118 InstallPlan install_plan_{};
119 InstallPlan::Payload payload_{};
120 DynamicPartitionControlStub dynamic_control_{};
121 FileDescriptorPtr fake_ecc_fd_{};
122 DeltaArchiveManifest manifest_{};
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500123 ScopedTempFile source_partition{"source-part-XXXXXX"};
124 ScopedTempFile target_partition{"target-part-XXXXXX"};
125 InstallPlan::Partition install_part_{.source_path = source_partition.path(),
126 .target_path = target_partition.path()};
Kelvin Zhang9bd519d2020-09-23 12:55:19 -0400127 PartitionUpdate partition_update_{};
Kelvin Zhang59928f12020-11-11 21:21:27 +0000128 PartitionWriter writer_{
129 partition_update_, install_part_, &dynamic_control_, kBlockSize, false};
Kelvin Zhang9bd519d2020-09-23 12:55:19 -0400130};
131// Test that the error-corrected file descriptor is used to read a partition
132// when no hash is available for SOURCE_COPY but it falls back to the normal
133// file descriptor when the size of the error corrected one is too small.
134TEST_F(PartitionWriterTest, ErrorCorrectionSourceCopyWhenNoHashFallbackTest) {
135 constexpr size_t kCopyOperationSize = 4 * 4096;
Amin Hassani42c2f982020-10-29 12:10:05 -0700136 ScopedTempFile source("Source-XXXXXX");
Kelvin Zhang9bd519d2020-09-23 12:55:19 -0400137 // Setup the source path with the right expected data.
138 brillo::Blob expected_data = FakeFileDescriptorData(kCopyOperationSize);
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500139 ASSERT_TRUE(test_utils::WriteFileVector(source.path(), expected_data));
Kelvin Zhang9bd519d2020-09-23 12:55:19 -0400140
141 // Setup the fec file descriptor as the fake stream, with smaller data than
142 // the expected.
143 FakeFileDescriptor* fake_fec = SetFakeECCFile(kCopyOperationSize / 2);
144
145 PartitionConfig old_part(kPartitionNameRoot);
146 old_part.path = source.path();
147 old_part.size = expected_data.size();
148
149 // The payload operation doesn't include an operation hash.
150 auto source_copy_op = GenerateSourceCopyOp(expected_data, false, &old_part);
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500151 ASSERT_NO_FATAL_FAILURE();
Kelvin Zhang9bd519d2020-09-23 12:55:19 -0400152 auto output_data = PerformSourceCopyOp(source_copy_op.op, expected_data);
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500153 ASSERT_NO_FATAL_FAILURE();
Kelvin Zhang9bd519d2020-09-23 12:55:19 -0400154 ASSERT_EQ(output_data, expected_data);
155
156 // Verify that the fake_fec was attempted to be used. Since the file
157 // descriptor is shorter it can actually do more than one read to realize it
158 // reached the EOF.
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500159 ASSERT_LE(1U, fake_fec->GetReadOps().size());
Kelvin Zhang9bd519d2020-09-23 12:55:19 -0400160 // This fallback doesn't count as an error-corrected operation since the
161 // operation hash was not available.
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500162 ASSERT_EQ(0U, GetSourceEccRecoveredFailures());
Kelvin Zhang9bd519d2020-09-23 12:55:19 -0400163}
164
165// Test that the error-corrected file descriptor is used to read the partition
166// since the source partition doesn't match the operation hash.
167TEST_F(PartitionWriterTest, ErrorCorrectionSourceCopyFallbackTest) {
168 constexpr size_t kCopyOperationSize = 4 * 4096;
169 // Write invalid data to the source image, which doesn't match the expected
170 // hash.
171 brillo::Blob invalid_data(kCopyOperationSize, 0x55);
172
173 // Setup the fec file descriptor as the fake stream, which matches
174 // |expected_data|.
175 FakeFileDescriptor* fake_fec = SetFakeECCFile(kCopyOperationSize);
176 brillo::Blob expected_data = FakeFileDescriptorData(kCopyOperationSize);
177
178 auto source_copy_op = GenerateSourceCopyOp(expected_data, true);
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500179 ASSERT_NO_FATAL_FAILURE();
Kelvin Zhang9bd519d2020-09-23 12:55:19 -0400180 auto output_data = PerformSourceCopyOp(source_copy_op.op, invalid_data);
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500181 ASSERT_NO_FATAL_FAILURE();
Kelvin Zhang9bd519d2020-09-23 12:55:19 -0400182 ASSERT_EQ(output_data, expected_data);
183
184 // Verify that the fake_fec was actually used.
Kelvin Zhang37b9b702021-02-23 10:30:37 -0500185 EXPECT_GE(fake_fec->GetReadOps().size(), 1U);
Kelvin Zhang9bd519d2020-09-23 12:55:19 -0400186 EXPECT_EQ(1U, GetSourceEccRecoveredFailures());
187}
188
189TEST_F(PartitionWriterTest, ChooseSourceFDTest) {
190 constexpr size_t kSourceSize = 4 * 4096;
Amin Hassani42c2f982020-10-29 12:10:05 -0700191 ScopedTempFile source("Source-XXXXXX");
Kelvin Zhang9bd519d2020-09-23 12:55:19 -0400192 // Write invalid data to the source image, which doesn't match the expected
193 // hash.
194 brillo::Blob invalid_data(kSourceSize, 0x55);
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500195 ASSERT_TRUE(test_utils::WriteFileVector(source.path(), invalid_data));
Kelvin Zhang9bd519d2020-09-23 12:55:19 -0400196
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500197 writer_.verified_source_fd_.source_fd_ =
198 std::make_shared<EintrSafeFileDescriptor>();
199 writer_.verified_source_fd_.source_fd_->Open(source.path().c_str(), O_RDONLY);
Kelvin Zhang9bd519d2020-09-23 12:55:19 -0400200
201 // Setup the fec file descriptor as the fake stream, which matches
202 // |expected_data|.
203 FakeFileDescriptor* fake_fec = SetFakeECCFile(kSourceSize);
204 brillo::Blob expected_data = FakeFileDescriptorData(kSourceSize);
205
206 InstallOperation op;
207 *(op.add_src_extents()) = ExtentForRange(0, kSourceSize / 4096);
208 brillo::Blob src_hash;
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500209 ASSERT_TRUE(HashCalculator::RawHashOfData(expected_data, &src_hash));
Kelvin Zhang9bd519d2020-09-23 12:55:19 -0400210 op.set_src_sha256_hash(src_hash.data(), src_hash.size());
211
212 ErrorCode error = ErrorCode::kSuccess;
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500213 ASSERT_EQ(writer_.verified_source_fd_.source_ecc_fd_,
214 writer_.ChooseSourceFD(op, &error));
215 ASSERT_EQ(ErrorCode::kSuccess, error);
Kelvin Zhang9bd519d2020-09-23 12:55:19 -0400216 // Verify that the fake_fec was actually used.
Kelvin Zhangab3ce602021-02-24 14:46:40 -0500217 ASSERT_EQ(1U, fake_fec->GetReadOps().size());
218 ASSERT_EQ(1U, GetSourceEccRecoveredFailures());
Kelvin Zhang9bd519d2020-09-23 12:55:19 -0400219}
220
221} // namespace chromeos_update_engine