blob: 1ef4783b0ba462a081bbc7166b0c7f2ab1a3daf2 [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"
29#include "update_engine/payload_consumer/delta_performer.h"
30#include "update_engine/payload_consumer/extent_reader.h"
31#include "update_engine/payload_consumer/extent_writer.h"
32#include "update_engine/payload_consumer/fake_file_descriptor.h"
33#include "update_engine/payload_consumer/file_descriptor.h"
34#include "update_engine/payload_consumer/install_plan.h"
35#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"
38#include "update_engine/payload_generator/payload_file.h"
39#include "update_engine/payload_generator/payload_generation_config.h"
40#include "update_engine/update_metadata.pb.h"
41
42namespace chromeos_update_engine {
43
44class PartitionWriterTest : public testing::Test {
45 public:
46 // Helper function to pretend that the ECC file descriptor was already opened.
47 // Returns a pointer to the created file descriptor.
48 FakeFileDescriptor* SetFakeECCFile(size_t size) {
49 EXPECT_FALSE(writer_.source_ecc_fd_) << "source_ecc_fd_ already open.";
50 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);
55 writer_.source_ecc_fd_ = fake_ecc_fd_;
56 return ret;
57 }
58
59 uint64_t GetSourceEccRecoveredFailures() const {
60 return writer_.source_ecc_recovered_failures_;
61 }
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) {
Amin Hassani42c2f982020-10-29 12:10:05 -070084 ScopedTempFile source_partition("Blob-XXXXXX");
Kelvin Zhang9bd519d2020-09-23 12:55:19 -040085 DirectExtentWriter extent_writer;
86 FileDescriptorPtr fd(new EintrSafeFileDescriptor());
87 EXPECT_TRUE(fd->Open(source_partition.path().c_str(), O_RDWR));
88 EXPECT_TRUE(extent_writer.Init(fd, op.src_extents(), kBlockSize));
89 EXPECT_TRUE(extent_writer.Write(blob_data.data(), blob_data.size()));
90
Amin Hassani42c2f982020-10-29 12:10:05 -070091 ScopedTempFile target_partition("Blob-XXXXXX");
Kelvin Zhang9bd519d2020-09-23 12:55:19 -040092
93 install_part_.source_path = source_partition.path();
94 install_part_.target_path = target_partition.path();
95 install_part_.source_size = blob_data.size();
96 install_part_.target_size = blob_data.size();
97
98 ErrorCode error;
99 EXPECT_TRUE(writer_.Init(&install_plan_, true));
100 EXPECT_TRUE(writer_.PerformSourceCopyOperation(op, &error));
101
102 brillo::Blob output_data;
103 EXPECT_TRUE(utils::ReadFile(target_partition.path(), &output_data));
104 return output_data;
105 }
106
107 FakePrefs prefs_{};
108 InstallPlan install_plan_{};
109 InstallPlan::Payload payload_{};
110 DynamicPartitionControlStub dynamic_control_{};
111 FileDescriptorPtr fake_ecc_fd_{};
112 DeltaArchiveManifest manifest_{};
113 PartitionUpdate partition_update_{};
114 InstallPlan::Partition install_part_{};
115 PartitionWriter writer_{
116 partition_update_, install_part_, &dynamic_control_, kBlockSize, false};
117};
118// Test that the error-corrected file descriptor is used to read a partition
119// when no hash is available for SOURCE_COPY but it falls back to the normal
120// file descriptor when the size of the error corrected one is too small.
121TEST_F(PartitionWriterTest, ErrorCorrectionSourceCopyWhenNoHashFallbackTest) {
122 constexpr size_t kCopyOperationSize = 4 * 4096;
Amin Hassani42c2f982020-10-29 12:10:05 -0700123 ScopedTempFile source("Source-XXXXXX");
Kelvin Zhang9bd519d2020-09-23 12:55:19 -0400124 // Setup the source path with the right expected data.
125 brillo::Blob expected_data = FakeFileDescriptorData(kCopyOperationSize);
126 EXPECT_TRUE(test_utils::WriteFileVector(source.path(), expected_data));
127
128 // Setup the fec file descriptor as the fake stream, with smaller data than
129 // the expected.
130 FakeFileDescriptor* fake_fec = SetFakeECCFile(kCopyOperationSize / 2);
131
132 PartitionConfig old_part(kPartitionNameRoot);
133 old_part.path = source.path();
134 old_part.size = expected_data.size();
135
136 // The payload operation doesn't include an operation hash.
137 auto source_copy_op = GenerateSourceCopyOp(expected_data, false, &old_part);
138
139 auto output_data = PerformSourceCopyOp(source_copy_op.op, expected_data);
140 ASSERT_EQ(output_data, expected_data);
141
142 // Verify that the fake_fec was attempted to be used. Since the file
143 // descriptor is shorter it can actually do more than one read to realize it
144 // reached the EOF.
145 EXPECT_LE(1U, fake_fec->GetReadOps().size());
146 // This fallback doesn't count as an error-corrected operation since the
147 // operation hash was not available.
148 EXPECT_EQ(0U, GetSourceEccRecoveredFailures());
149}
150
151// Test that the error-corrected file descriptor is used to read the partition
152// since the source partition doesn't match the operation hash.
153TEST_F(PartitionWriterTest, ErrorCorrectionSourceCopyFallbackTest) {
154 constexpr size_t kCopyOperationSize = 4 * 4096;
155 // Write invalid data to the source image, which doesn't match the expected
156 // hash.
157 brillo::Blob invalid_data(kCopyOperationSize, 0x55);
158
159 // Setup the fec file descriptor as the fake stream, which matches
160 // |expected_data|.
161 FakeFileDescriptor* fake_fec = SetFakeECCFile(kCopyOperationSize);
162 brillo::Blob expected_data = FakeFileDescriptorData(kCopyOperationSize);
163
164 auto source_copy_op = GenerateSourceCopyOp(expected_data, true);
165 auto output_data = PerformSourceCopyOp(source_copy_op.op, invalid_data);
166 ASSERT_EQ(output_data, expected_data);
167
168 // Verify that the fake_fec was actually used.
169 EXPECT_EQ(1U, fake_fec->GetReadOps().size());
170 EXPECT_EQ(1U, GetSourceEccRecoveredFailures());
171}
172
173TEST_F(PartitionWriterTest, ChooseSourceFDTest) {
174 constexpr size_t kSourceSize = 4 * 4096;
Amin Hassani42c2f982020-10-29 12:10:05 -0700175 ScopedTempFile source("Source-XXXXXX");
Kelvin Zhang9bd519d2020-09-23 12:55:19 -0400176 // Write invalid data to the source image, which doesn't match the expected
177 // hash.
178 brillo::Blob invalid_data(kSourceSize, 0x55);
179 EXPECT_TRUE(test_utils::WriteFileVector(source.path(), invalid_data));
180
181 writer_.source_fd_ = std::make_shared<EintrSafeFileDescriptor>();
182 writer_.source_fd_->Open(source.path().c_str(), O_RDONLY);
183
184 // Setup the fec file descriptor as the fake stream, which matches
185 // |expected_data|.
186 FakeFileDescriptor* fake_fec = SetFakeECCFile(kSourceSize);
187 brillo::Blob expected_data = FakeFileDescriptorData(kSourceSize);
188
189 InstallOperation op;
190 *(op.add_src_extents()) = ExtentForRange(0, kSourceSize / 4096);
191 brillo::Blob src_hash;
192 EXPECT_TRUE(HashCalculator::RawHashOfData(expected_data, &src_hash));
193 op.set_src_sha256_hash(src_hash.data(), src_hash.size());
194
195 ErrorCode error = ErrorCode::kSuccess;
196 EXPECT_EQ(writer_.source_ecc_fd_, writer_.ChooseSourceFD(op, &error));
197 EXPECT_EQ(ErrorCode::kSuccess, error);
198 // Verify that the fake_fec was actually used.
199 EXPECT_EQ(1U, fake_fec->GetReadOps().size());
200 EXPECT_EQ(1U, GetSourceEccRecoveredFailures());
201}
202
203} // namespace chromeos_update_engine