• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2021 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 
19 #include <unistd.h>
20 
21 #include <android-base/file.h>
22 #include <gtest/gtest.h>
23 #include <libsnapshot/mock_snapshot_writer.h>
24 
25 #include "common/utils.h"
26 #include "update_engine/payload_consumer/extent_map.h"
27 #include "update_engine/payload_consumer/file_descriptor.h"
28 #include "update_engine/payload_consumer/xor_extent_writer.h"
29 #include "update_engine/payload_generator/delta_diff_generator.h"
30 #include "update_engine/payload_generator/extent_ranges.h"
31 #include "update_engine/payload_generator/merge_sequence_generator.h"
32 #include "update_engine/update_metadata.pb.h"
33 
34 namespace chromeos_update_engine {
35 
36 using testing::_;
37 using testing::Args;
38 using testing::Return;
39 
40 class XorExtentWriterTest : public ::testing::Test {
41  public:
42   static constexpr size_t NUM_BLOCKS = 50;
SetUp()43   void SetUp() override {
44     ASSERT_EQ(ftruncate64(source_part_.fd, kBlockSize * NUM_BLOCKS), 0);
45     ASSERT_EQ(ftruncate64(target_part_.fd, kBlockSize * NUM_BLOCKS), 0);
46 
47     // Fill source part with 1s, as we are computing XOR between source and
48     // target data later.
49     ASSERT_EQ(lseek(source_part_.fd, 0, SEEK_SET), 0);
50     brillo::Blob buffer(kBlockSize);
51     std::fill(buffer.begin(), buffer.end(), 1);
52     for (size_t i = 0; i < NUM_BLOCKS; i++) {
53       ASSERT_EQ(write(source_part_.fd, buffer.data(), buffer.size()),
54                 static_cast<ssize_t>(buffer.size()));
55     }
56     ASSERT_EQ(fsync(source_part_.fd), 0);
57     ASSERT_EQ(fsync(target_part_.fd), 0);
58     ASSERT_TRUE(source_fd_->Open(source_part_.path, O_RDONLY | O_CREAT, 0644));
59   }
60   InstallOperation op_;
61   FileDescriptorPtr source_fd_ = std::make_shared<EintrSafeFileDescriptor>();
62   ExtentMap<const CowMergeOperation*> xor_map_;
63   android::snapshot::MockSnapshotWriter cow_writer_;
64   TemporaryFile source_part_;
65   TemporaryFile target_part_;
66 };
67 
68 MATCHER_P2(BytesEqual,
69            bytes,
70            size,
71            "Check if args match expected value byte for byte") {
72   return std::get<1>(arg) == size && std::get<0>(arg) != nullptr &&
73          memcmp(std::get<0>(arg), bytes, size) == 0;
74 }
75 
TEST_F(XorExtentWriterTest,StreamTest)76 TEST_F(XorExtentWriterTest, StreamTest) {
77   constexpr auto COW_XOR = CowMergeOperation::COW_XOR;
78   ON_CALL(cow_writer_, EmitXorBlocks(_, _, _, _, _))
79       .WillByDefault(Return(true));
80   const auto op1 = CreateCowMergeOperation(
81       ExtentForRange(5, 2), ExtentForRange(5, 2), COW_XOR);
82   ASSERT_TRUE(xor_map_.AddExtent(op1.dst_extent(), &op1));
83   *op_.add_src_extents() = op1.src_extent();
84   *op_.add_dst_extents() = op1.dst_extent();
85 
86   const auto op2 = CreateCowMergeOperation(
87       ExtentForRange(45, 2), ExtentForRange(456, 2), COW_XOR);
88   ASSERT_TRUE(xor_map_.AddExtent(op2.dst_extent(), &op2));
89   *op_.add_src_extents() = ExtentForRange(45, 3);
90   *op_.add_dst_extents() = ExtentForRange(455, 3);
91 
92   const auto op3 = CreateCowMergeOperation(
93       ExtentForRange(12, 2), ExtentForRange(321, 2), COW_XOR, 777);
94   ASSERT_TRUE(xor_map_.AddExtent(op3.dst_extent(), &op3));
95   *op_.add_src_extents() = ExtentForRange(12, 4);
96   *op_.add_dst_extents() = ExtentForRange(320, 4);
97   XORExtentWriter writer_{op_, source_fd_, &cow_writer_, xor_map_};
98 
99   // OTA op:
100   // [5-6] => [5-6], [45-47] => [455-457], [12-15] => [320-323]
101 
102   // merge op:
103   // [5-6] => [5-6], [45-46] => [456-457], [12-13] => [321-322]
104 
105   // Expected result:
106   // [5-7], [45-47], [12-14] should be XOR blocks
107   // [320], [323], [455] should be regular replace blocks
108 
109   auto zeros = utils::GetReadonlyZeroBlock(kBlockSize * 10);
110   EXPECT_CALL(cow_writer_,
111               EmitRawBlocks(455, zeros->data() + 2 * kBlockSize, kBlockSize))
112       .With(Args<1, 2>(BytesEqual(zeros->data(), kBlockSize)))
113       .WillOnce(Return(true));
114   EXPECT_CALL(cow_writer_,
115               EmitRawBlocks(320, zeros->data() + 5 * kBlockSize, kBlockSize))
116       .With(Args<1, 2>(BytesEqual(zeros->data(), kBlockSize)))
117       .WillOnce(Return(true));
118   EXPECT_CALL(cow_writer_,
119               EmitRawBlocks(323, zeros->data() + 8 * kBlockSize, kBlockSize))
120       .With(Args<1, 2>(BytesEqual(zeros->data(), kBlockSize)))
121       .WillOnce(Return(true));
122 
123   EXPECT_CALL(cow_writer_, EmitXorBlocks(5, _, kBlockSize * 2, 5, 0))
124       .WillOnce(Return(true));
125   EXPECT_CALL(cow_writer_, EmitXorBlocks(456, _, kBlockSize * 2, 45, 0))
126       .WillOnce(Return(true));
127   EXPECT_CALL(cow_writer_, EmitXorBlocks(321, _, kBlockSize * 2, 12, 777))
128       .WillOnce(Return(true));
129 
130   ASSERT_TRUE(writer_.Init(op_.dst_extents(), kBlockSize));
131   ASSERT_TRUE(writer_.Write(zeros->data(), 9 * kBlockSize));
132 }
133 
134 }  // namespace chromeos_update_engine
135