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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
// Copyright 2020 The gVisor Authors.
//
// 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 <errno.h>
#include <fcntl.h>
#include <linux/fuse.h>
#include <sys/stat.h>
#include <sys/statfs.h>
#include <sys/types.h>
#include <unistd.h>
#include <string>
#include <vector>
#include "gtest/gtest.h"
#include "test/fuse/linux/fuse_base.h"
#include "test/util/fuse_util.h"
#include "test/util/test_util.h"
namespace gvisor {
namespace testing {
namespace {
class WriteTest : public FuseTest {
void SetUp() override {
FuseTest::SetUp();
test_file_path_ = JoinPath(mount_point_.path().c_str(), test_file_);
}
// TearDown overrides the parent's function
// to skip checking the unconsumed release request at the end.
void TearDown() override { UnmountFuse(); }
protected:
const std::string test_file_ = "test_file";
const mode_t test_file_mode_ = S_IFREG | S_IRWXU | S_IRWXG | S_IRWXO;
const uint64_t test_fh_ = 1;
const uint32_t open_flag_ = O_RDWR;
std::string test_file_path_;
PosixErrorOr<FileDescriptor> OpenTestFile(const std::string &path,
uint64_t size = 512) {
SetServerInodeLookup(test_file_, test_file_mode_, size);
struct fuse_out_header out_header_open = {
.len = sizeof(struct fuse_out_header) + sizeof(struct fuse_open_out),
};
struct fuse_open_out out_payload_open = {
.fh = test_fh_,
.open_flags = open_flag_,
};
auto iov_out_open = FuseGenerateIovecs(out_header_open, out_payload_open);
SetServerResponse(FUSE_OPEN, iov_out_open);
auto res = Open(path.c_str(), open_flag_);
if (res.ok()) {
SkipServerActualRequest();
}
return res;
}
};
class WriteTestSmallMaxWrite : public WriteTest {
void SetUp() override {
MountFuse();
SetUpFuseServer(&fuse_init_payload);
test_file_path_ = JoinPath(mount_point_.path().c_str(), test_file_);
}
protected:
const static uint32_t max_write_ = 4096;
constexpr static struct fuse_init_out fuse_init_payload = {
.major = 7,
.max_write = max_write_,
};
const uint32_t size_fragment = max_write_;
};
TEST_F(WriteTest, WriteNormal) {
auto fd = ASSERT_NO_ERRNO_AND_VALUE(OpenTestFile(test_file_path_));
// Prepare for the write.
const int n_write = 10;
struct fuse_out_header out_header_write = {
.len = sizeof(struct fuse_out_header) + sizeof(struct fuse_write_out),
};
struct fuse_write_out out_payload_write = {
.size = n_write,
};
auto iov_out_write = FuseGenerateIovecs(out_header_write, out_payload_write);
SetServerResponse(FUSE_WRITE, iov_out_write);
// Issue the write.
std::vector<char> buf(n_write);
RandomizeBuffer(buf.data(), buf.size());
EXPECT_THAT(write(fd.get(), buf.data(), n_write),
SyscallSucceedsWithValue(n_write));
// Check the write request.
struct fuse_in_header in_header_write;
struct fuse_write_in in_payload_write;
std::vector<char> payload_buf(n_write);
auto iov_in_write =
FuseGenerateIovecs(in_header_write, in_payload_write, payload_buf);
GetServerActualRequest(iov_in_write);
EXPECT_EQ(in_payload_write.fh, test_fh_);
EXPECT_EQ(in_header_write.len,
sizeof(in_header_write) + sizeof(in_payload_write));
EXPECT_EQ(in_header_write.opcode, FUSE_WRITE);
EXPECT_EQ(in_payload_write.offset, 0);
EXPECT_EQ(in_payload_write.size, n_write);
EXPECT_EQ(buf, payload_buf);
}
TEST_F(WriteTest, WriteShort) {
auto fd = ASSERT_NO_ERRNO_AND_VALUE(OpenTestFile(test_file_path_));
// Prepare for the write.
const int n_write = 10, n_written = 5;
struct fuse_out_header out_header_write = {
.len = sizeof(struct fuse_out_header) + sizeof(struct fuse_write_out),
};
struct fuse_write_out out_payload_write = {
.size = n_written,
};
auto iov_out_write = FuseGenerateIovecs(out_header_write, out_payload_write);
SetServerResponse(FUSE_WRITE, iov_out_write);
// Issue the write.
std::vector<char> buf(n_write);
RandomizeBuffer(buf.data(), buf.size());
EXPECT_THAT(write(fd.get(), buf.data(), n_write),
SyscallSucceedsWithValue(n_written));
// Check the write request.
struct fuse_in_header in_header_write;
struct fuse_write_in in_payload_write;
std::vector<char> payload_buf(n_write);
auto iov_in_write =
FuseGenerateIovecs(in_header_write, in_payload_write, payload_buf);
GetServerActualRequest(iov_in_write);
EXPECT_EQ(in_payload_write.fh, test_fh_);
EXPECT_EQ(in_header_write.len,
sizeof(in_header_write) + sizeof(in_payload_write));
EXPECT_EQ(in_header_write.opcode, FUSE_WRITE);
EXPECT_EQ(in_payload_write.offset, 0);
EXPECT_EQ(in_payload_write.size, n_write);
EXPECT_EQ(buf, payload_buf);
}
TEST_F(WriteTest, WriteShortZero) {
auto fd = ASSERT_NO_ERRNO_AND_VALUE(OpenTestFile(test_file_path_));
// Prepare for the write.
const int n_write = 10;
struct fuse_out_header out_header_write = {
.len = sizeof(struct fuse_out_header) + sizeof(struct fuse_write_out),
};
struct fuse_write_out out_payload_write = {
.size = 0,
};
auto iov_out_write = FuseGenerateIovecs(out_header_write, out_payload_write);
SetServerResponse(FUSE_WRITE, iov_out_write);
// Issue the write.
std::vector<char> buf(n_write);
RandomizeBuffer(buf.data(), buf.size());
EXPECT_THAT(write(fd.get(), buf.data(), n_write), SyscallFailsWithErrno(EIO));
// Check the write request.
struct fuse_in_header in_header_write;
struct fuse_write_in in_payload_write;
std::vector<char> payload_buf(n_write);
auto iov_in_write =
FuseGenerateIovecs(in_header_write, in_payload_write, payload_buf);
GetServerActualRequest(iov_in_write);
EXPECT_EQ(in_payload_write.fh, test_fh_);
EXPECT_EQ(in_header_write.len,
sizeof(in_header_write) + sizeof(in_payload_write));
EXPECT_EQ(in_header_write.opcode, FUSE_WRITE);
EXPECT_EQ(in_payload_write.offset, 0);
EXPECT_EQ(in_payload_write.size, n_write);
EXPECT_EQ(buf, payload_buf);
}
TEST_F(WriteTest, WriteZero) {
auto fd = ASSERT_NO_ERRNO_AND_VALUE(OpenTestFile(test_file_path_));
// Issue the write.
std::vector<char> buf(0);
EXPECT_THAT(write(fd.get(), buf.data(), 0), SyscallSucceedsWithValue(0));
}
TEST_F(WriteTest, PWrite) {
const int file_size = 512;
auto fd = ASSERT_NO_ERRNO_AND_VALUE(OpenTestFile(test_file_path_, file_size));
// Prepare for the write.
const int n_write = 10;
struct fuse_out_header out_header_write = {
.len = sizeof(struct fuse_out_header) + sizeof(struct fuse_write_out),
};
struct fuse_write_out out_payload_write = {
.size = n_write,
};
auto iov_out_write = FuseGenerateIovecs(out_header_write, out_payload_write);
SetServerResponse(FUSE_WRITE, iov_out_write);
// Issue the write.
std::vector<char> buf(n_write);
RandomizeBuffer(buf.data(), buf.size());
const int offset_write = file_size >> 1;
EXPECT_THAT(pwrite(fd.get(), buf.data(), n_write, offset_write),
SyscallSucceedsWithValue(n_write));
// Check the write request.
struct fuse_in_header in_header_write;
struct fuse_write_in in_payload_write;
std::vector<char> payload_buf(n_write);
auto iov_in_write =
FuseGenerateIovecs(in_header_write, in_payload_write, payload_buf);
GetServerActualRequest(iov_in_write);
EXPECT_EQ(in_payload_write.fh, test_fh_);
EXPECT_EQ(in_header_write.len,
sizeof(in_header_write) + sizeof(in_payload_write));
EXPECT_EQ(in_header_write.opcode, FUSE_WRITE);
EXPECT_EQ(in_payload_write.offset, offset_write);
EXPECT_EQ(in_payload_write.size, n_write);
EXPECT_EQ(buf, payload_buf);
}
TEST_F(WriteTestSmallMaxWrite, WriteSmallMaxWrie) {
const int n_fragment = 10;
const int n_write = size_fragment * n_fragment;
auto fd = ASSERT_NO_ERRNO_AND_VALUE(OpenTestFile(test_file_path_, n_write));
// Prepare for the write.
struct fuse_out_header out_header_write = {
.len = sizeof(struct fuse_out_header) + sizeof(struct fuse_write_out),
};
struct fuse_write_out out_payload_write = {
.size = size_fragment,
};
auto iov_out_write = FuseGenerateIovecs(out_header_write, out_payload_write);
for (int i = 0; i < n_fragment; ++i) {
SetServerResponse(FUSE_WRITE, iov_out_write);
}
// Issue the write.
std::vector<char> buf(n_write);
RandomizeBuffer(buf.data(), buf.size());
EXPECT_THAT(write(fd.get(), buf.data(), n_write),
SyscallSucceedsWithValue(n_write));
ASSERT_EQ(GetServerNumUnsentResponses(), 0);
ASSERT_EQ(GetServerNumUnconsumedRequests(), n_fragment);
// Check the write request.
struct fuse_in_header in_header_write;
struct fuse_write_in in_payload_write;
std::vector<char> payload_buf(size_fragment);
auto iov_in_write =
FuseGenerateIovecs(in_header_write, in_payload_write, payload_buf);
for (int i = 0; i < n_fragment; ++i) {
GetServerActualRequest(iov_in_write);
EXPECT_EQ(in_payload_write.fh, test_fh_);
EXPECT_EQ(in_header_write.len,
sizeof(in_header_write) + sizeof(in_payload_write));
EXPECT_EQ(in_header_write.opcode, FUSE_WRITE);
EXPECT_EQ(in_payload_write.offset, i * size_fragment);
EXPECT_EQ(in_payload_write.size, size_fragment);
auto it = buf.begin() + i * size_fragment;
EXPECT_EQ(std::vector<char>(it, it + size_fragment), payload_buf);
}
}
} // namespace
} // namespace testing
} // namespace gvisor
|