1 //
2 // Copyright (C) 2017 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_consumer/file_descriptor_utils.h"
18
19 #include <fcntl.h>
20
21 #include <string>
22 #include <utility>
23 #include <vector>
24
25 #include <brillo/data_encoding.h>
26 #include <gtest/gtest.h>
27
28 #include "update_engine/common/hash_calculator.h"
29 #include "update_engine/common/test_utils.h"
30 #include "update_engine/common/utils.h"
31 #include "update_engine/payload_consumer/fake_file_descriptor.h"
32 #include "update_engine/payload_consumer/file_descriptor.h"
33 #include "update_engine/payload_generator/extent_ranges.h"
34
35 using google::protobuf::RepeatedPtrField;
36
37 namespace chromeos_update_engine {
38
39 namespace {
40
CreateExtentList(const std::vector<std::pair<uint64_t,uint64_t>> & lst)41 RepeatedPtrField<Extent> CreateExtentList(
42 const std::vector<std::pair<uint64_t, uint64_t>>& lst) {
43 RepeatedPtrField<Extent> result;
44 for (const auto& item : lst) {
45 *result.Add() = ExtentForRange(item.first, item.second);
46 }
47 return result;
48 }
49
50 } // namespace
51
52 class FileDescriptorUtilsTest : public ::testing::Test {
53 protected:
SetUp()54 void SetUp() override {
55 EXPECT_TRUE(target_->Open(tgt_file_.path().c_str(), O_RDWR));
56 }
57
58 // Check that the |target_| file contains |expected_contents|.
ExpectTarget(const std::string & expected_contents)59 void ExpectTarget(const std::string& expected_contents) {
60 std::string target_contents;
61 EXPECT_TRUE(utils::ReadFile(tgt_file_.path(), &target_contents));
62 EXPECT_EQ(expected_contents.size(), target_contents.size());
63 if (target_contents != expected_contents) {
64 ADD_FAILURE() << "Contents don't match.";
65 LOG(INFO) << "Expected contents:";
66 utils::HexDumpString(expected_contents);
67 LOG(INFO) << "Actual contents:";
68 utils::HexDumpString(target_contents);
69 }
70 }
71
72 ScopedTempFile tgt_file_{"fd_tgt.XXXXXX"};
73
74 // Source and target file descriptor used for testing the tools.
75 FakeFileDescriptor* fake_source_{new FakeFileDescriptor()};
76 FileDescriptorPtr source_{fake_source_};
77 FileDescriptorPtr target_{new EintrSafeFileDescriptor()};
78 };
79
80 // Source and target extents should have the same number of blocks.
TEST_F(FileDescriptorUtilsTest,CopyAndHashExtentsMismatchBlocksTest)81 TEST_F(FileDescriptorUtilsTest, CopyAndHashExtentsMismatchBlocksTest) {
82 auto src_extents = CreateExtentList({{1, 4}});
83 auto tgt_extents = CreateExtentList({{0, 5}});
84
85 EXPECT_FALSE(fd_utils::CopyAndHashExtents(
86 source_, src_extents, target_, tgt_extents, 4, nullptr));
87 }
88
89 // Failing to read from the source should fail the copy.
TEST_F(FileDescriptorUtilsTest,CopyAndHashExtentsReadFailureTest)90 TEST_F(FileDescriptorUtilsTest, CopyAndHashExtentsReadFailureTest) {
91 auto extents = CreateExtentList({{0, 5}});
92 fake_source_->AddFailureRange(10, 5);
93
94 EXPECT_FALSE(fd_utils::CopyAndHashExtents(
95 source_, extents, target_, extents, 4, nullptr));
96 }
97
98 // Failing to write to the target should fail the copy.
TEST_F(FileDescriptorUtilsTest,CopyAndHashExtentsWriteFailureTest)99 TEST_F(FileDescriptorUtilsTest, CopyAndHashExtentsWriteFailureTest) {
100 auto src_extents = CreateExtentList({{0, 2}});
101 auto tgt_extents = CreateExtentList({{5, 2}});
102 fake_source_->AddFailureRange(5 * 4, 10);
103
104 // Note that we pass |source_| as the target as well, which should fail to
105 // write.
106 EXPECT_FALSE(fd_utils::CopyAndHashExtents(
107 source_, src_extents, source_, tgt_extents, 4, nullptr));
108 }
109
110 // Test that we can copy extents without hashing them, allowing a nullptr
111 // pointer as hash_out.
TEST_F(FileDescriptorUtilsTest,CopyAndHashExtentsWithoutHashingTest)112 TEST_F(FileDescriptorUtilsTest, CopyAndHashExtentsWithoutHashingTest) {
113 auto extents = CreateExtentList({{0, 5}});
114
115 EXPECT_TRUE(fd_utils::CopyAndHashExtents(
116 source_, extents, target_, extents, 4, nullptr));
117 ExpectTarget("00000001000200030004");
118 }
119
120 // CopyAndHash() can take different number of extents in the source and target
121 // files, as long as the number of blocks is the same. Test that it handles it
122 // properly.
TEST_F(FileDescriptorUtilsTest,CopyAndHashExtentsManyToOneTest)123 TEST_F(FileDescriptorUtilsTest, CopyAndHashExtentsManyToOneTest) {
124 brillo::Blob hash_out;
125 // Reorder the input as 1 4 2 3 0.
126 auto src_extents = CreateExtentList({{1, 1}, {4, 1}, {2, 2}, {0, 1}});
127 auto tgt_extents = CreateExtentList({{0, 5}});
128
129 EXPECT_TRUE(fd_utils::CopyAndHashExtents(
130 source_, src_extents, target_, tgt_extents, 4, &hash_out));
131 const char kExpectedResult[] = "00010004000200030000";
132 ExpectTarget(kExpectedResult);
133
134 brillo::Blob expected_hash;
135 EXPECT_TRUE(HashCalculator::RawHashOfBytes(
136 kExpectedResult, strlen(kExpectedResult), &expected_hash));
137 EXPECT_EQ(expected_hash, hash_out);
138 }
139
TEST_F(FileDescriptorUtilsTest,CopyAndHashExtentsManyToManyTest)140 TEST_F(FileDescriptorUtilsTest, CopyAndHashExtentsManyToManyTest) {
141 brillo::Blob hash_out;
142 auto src_extents = CreateExtentList({{1, 1}, {4, 1}, {2, 2}, {0, 1}});
143 auto tgt_extents = CreateExtentList({{2, 3}, {0, 2}});
144
145 EXPECT_TRUE(fd_utils::CopyAndHashExtents(
146 source_, src_extents, target_, tgt_extents, 4, &hash_out));
147 // The reads always match the source extent list of blocks (up to the
148 // internal buffer size).
149 std::vector<std::pair<uint64_t, uint64_t>> kExpectedOps = {
150 {4, 4}, {16, 4}, {8, 8}, {0, 4}};
151 EXPECT_EQ(kExpectedOps, fake_source_->GetReadOps());
152
153 // The output here is as in the previous test but the first 3 4-byte blocks
154 // are at the end of the stream. The expected hash is as in the previous
155 // example anyway since the hash doesn't depend on the order of the target
156 // blocks.
157 const char kExpectedResult[] = "00030000000100040002";
158 ExpectTarget(kExpectedResult);
159
160 // The data in the order that the reader processes (and hashes) it.
161 const char kExpectedOrderedData[] = "00010004000200030000";
162 brillo::Blob expected_hash;
163 EXPECT_TRUE(HashCalculator::RawHashOfBytes(
164 kExpectedOrderedData, strlen(kExpectedOrderedData), &expected_hash));
165 EXPECT_EQ(expected_hash, hash_out);
166 }
167
168 // Failing to read from the source should fail the hash calculation.
TEST_F(FileDescriptorUtilsTest,ReadAndHashExtentsReadFailureTest)169 TEST_F(FileDescriptorUtilsTest, ReadAndHashExtentsReadFailureTest) {
170 auto extents = CreateExtentList({{0, 5}});
171 fake_source_->AddFailureRange(10, 5);
172 brillo::Blob hash_out;
173 EXPECT_FALSE(fd_utils::ReadAndHashExtents(source_, extents, 4, &hash_out));
174 }
175
176 // Test that if hash_out is null, it still works.
TEST_F(FileDescriptorUtilsTest,ReadAndHashExtentsWithoutHashingTest)177 TEST_F(FileDescriptorUtilsTest, ReadAndHashExtentsWithoutHashingTest) {
178 auto extents = CreateExtentList({{0, 5}});
179 EXPECT_TRUE(fd_utils::ReadAndHashExtents(source_, extents, 4, nullptr));
180 }
181
182 // Tests that it can calculate the hash properly.
TEST_F(FileDescriptorUtilsTest,ReadAndHashExtentsTest)183 TEST_F(FileDescriptorUtilsTest, ReadAndHashExtentsTest) {
184 // Reorder the input as 1 4 2 3 0.
185 auto extents = CreateExtentList({{1, 1}, {4, 1}, {2, 2}, {0, 1}});
186 brillo::Blob hash_out;
187 EXPECT_TRUE(fd_utils::ReadAndHashExtents(source_, extents, 4, &hash_out));
188
189 const char kExpectedResult[] = "00010004000200030000";
190 brillo::Blob expected_hash;
191 EXPECT_TRUE(HashCalculator::RawHashOfBytes(
192 kExpectedResult, strlen(kExpectedResult), &expected_hash));
193 EXPECT_EQ(expected_hash, hash_out);
194 }
195
196 } // namespace chromeos_update_engine
197