huangcm
2025-02-24 69ed55dec4b2116a19e4cca4393cbc014fce5fb2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//
// Copyright (C) 2009 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
 
#include "update_engine/payload_consumer/extent_writer.h"
 
#include <fcntl.h>
 
#include <algorithm>
#include <memory>
#include <string>
#include <vector>
 
#include <brillo/secure_blob.h>
#include <gtest/gtest.h>
 
#include "update_engine/common/test_utils.h"
#include "update_engine/common/utils.h"
#include "update_engine/payload_consumer/payload_constants.h"
#include "update_engine/payload_generator/extent_ranges.h"
 
using chromeos_update_engine::test_utils::ExpectVectorsEq;
using std::min;
using std::string;
using std::vector;
 
namespace chromeos_update_engine {
 
static_assert(sizeof(off_t) == 8, "off_t not 64 bit");
 
namespace {
const size_t kBlockSize = 4096;
}
 
class ExtentWriterTest : public ::testing::Test {
 protected:
  void SetUp() override {
    fd_.reset(new EintrSafeFileDescriptor);
    ASSERT_TRUE(fd_->Open(temp_file_.path().c_str(), O_RDWR, 0600));
  }
  void TearDown() override { fd_->Close(); }
 
  // Writes data to an extent writer in 'chunk_size' chunks with
  // the first chunk of size first_chunk_size. It calculates what the
  // resultant file should look like and ensure that the extent writer
  // wrote the file correctly.
  void WriteAlignedExtents(size_t chunk_size, size_t first_chunk_size);
 
  FileDescriptorPtr fd_;
  test_utils::ScopedTempFile temp_file_{"ExtentWriterTest-file.XXXXXX"};
};
 
TEST_F(ExtentWriterTest, SimpleTest) {
  vector<Extent> extents = {ExtentForRange(1, 1)};
  const string bytes = "1234";
  DirectExtentWriter direct_writer;
  EXPECT_TRUE(
      direct_writer.Init(fd_, {extents.begin(), extents.end()}, kBlockSize));
  EXPECT_TRUE(direct_writer.Write(bytes.data(), bytes.size()));
 
  EXPECT_EQ(static_cast<off_t>(kBlockSize + bytes.size()),
            utils::FileSize(temp_file_.path()));
 
  brillo::Blob result_file;
  EXPECT_TRUE(utils::ReadFile(temp_file_.path(), &result_file));
 
  brillo::Blob expected_file(kBlockSize);
  expected_file.insert(
      expected_file.end(), bytes.data(), bytes.data() + bytes.size());
  ExpectVectorsEq(expected_file, result_file);
}
 
TEST_F(ExtentWriterTest, ZeroLengthTest) {
  vector<Extent> extents = {ExtentForRange(1, 1)};
  DirectExtentWriter direct_writer;
  EXPECT_TRUE(
      direct_writer.Init(fd_, {extents.begin(), extents.end()}, kBlockSize));
  EXPECT_TRUE(direct_writer.Write(nullptr, 0));
}
 
TEST_F(ExtentWriterTest, OverflowExtentTest) {
  WriteAlignedExtents(kBlockSize * 3, kBlockSize * 3);
}
 
TEST_F(ExtentWriterTest, UnalignedWriteTest) {
  WriteAlignedExtents(7, 7);
}
 
TEST_F(ExtentWriterTest, LargeUnalignedWriteTest) {
  WriteAlignedExtents(kBlockSize * 2, kBlockSize / 2);
}
 
void ExtentWriterTest::WriteAlignedExtents(size_t chunk_size,
                                           size_t first_chunk_size) {
  vector<Extent> extents = {
      ExtentForRange(1, 1), ExtentForRange(0, 1), ExtentForRange(2, 1)};
  brillo::Blob data(kBlockSize * 3);
  test_utils::FillWithData(&data);
 
  DirectExtentWriter direct_writer;
  EXPECT_TRUE(
      direct_writer.Init(fd_, {extents.begin(), extents.end()}, kBlockSize));
 
  size_t bytes_written = 0;
  while (bytes_written < data.size()) {
    size_t bytes_to_write = min(data.size() - bytes_written, chunk_size);
    if (bytes_written == 0) {
      bytes_to_write = min(data.size() - bytes_written, first_chunk_size);
    }
    EXPECT_TRUE(direct_writer.Write(&data[bytes_written], bytes_to_write));
    bytes_written += bytes_to_write;
  }
 
  EXPECT_EQ(static_cast<off_t>(data.size()),
            utils::FileSize(temp_file_.path()));
 
  brillo::Blob result_file;
  EXPECT_TRUE(utils::ReadFile(temp_file_.path(), &result_file));
 
  brillo::Blob expected_file;
  expected_file.insert(expected_file.end(),
                       data.begin() + kBlockSize,
                       data.begin() + kBlockSize * 2);
  expected_file.insert(
      expected_file.end(), data.begin(), data.begin() + kBlockSize);
  expected_file.insert(
      expected_file.end(), data.begin() + kBlockSize * 2, data.end());
  ExpectVectorsEq(expected_file, result_file);
}
 
TEST_F(ExtentWriterTest, SparseFileTest) {
  vector<Extent> extents = {ExtentForRange(1, 1),
                            ExtentForRange(kSparseHole, 2),
                            ExtentForRange(0, 1)};
  const int block_count = 4;
  const int on_disk_count = 2;
 
  brillo::Blob data(17);
  test_utils::FillWithData(&data);
 
  DirectExtentWriter direct_writer;
  EXPECT_TRUE(
      direct_writer.Init(fd_, {extents.begin(), extents.end()}, kBlockSize));
 
  size_t bytes_written = 0;
  while (bytes_written < (block_count * kBlockSize)) {
    size_t bytes_to_write =
        min(block_count * kBlockSize - bytes_written, data.size());
    EXPECT_TRUE(direct_writer.Write(data.data(), bytes_to_write));
    bytes_written += bytes_to_write;
  }
 
  // check file size, then data inside
  ASSERT_EQ(static_cast<off_t>(2 * kBlockSize),
            utils::FileSize(temp_file_.path()));
 
  brillo::Blob resultant_data;
  EXPECT_TRUE(utils::ReadFile(temp_file_.path(), &resultant_data));
 
  // Create expected data
  brillo::Blob expected_data(on_disk_count * kBlockSize);
  brillo::Blob big(block_count * kBlockSize);
  for (brillo::Blob::size_type i = 0; i < big.size(); i++) {
    big[i] = data[i % data.size()];
  }
  memcpy(&expected_data[kBlockSize], &big[0], kBlockSize);
  memcpy(&expected_data[0], &big[3 * kBlockSize], kBlockSize);
  ExpectVectorsEq(expected_data, resultant_data);
}
 
}  // namespace chromeos_update_engine