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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
// 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 <sys/uio.h>
#include <unistd.h>
#include <utime.h>
#include <string>
#include <vector>
#include "gtest/gtest.h"
#include "test/fuse/linux/fuse_fd_util.h"
#include "test/util/cleanup.h"
#include "test/util/fs_util.h"
#include "test/util/fuse_util.h"
#include "test/util/test_util.h"
namespace gvisor {
namespace testing {
namespace {
class SetStatTest : public FuseFdTest {
public:
void SetUp() override {
FuseFdTest::SetUp();
test_dir_path_ = JoinPath(mount_point_.path(), test_dir_);
test_file_path_ = JoinPath(mount_point_.path(), test_file_);
}
protected:
const uint64_t fh = 23;
const std::string test_dir_ = "testdir";
const std::string test_file_ = "testfile";
const mode_t test_dir_mode_ = S_IFDIR | S_IRUSR | S_IWUSR | S_IXUSR;
const mode_t test_file_mode_ = S_IFREG | S_IRUSR | S_IWUSR | S_IXUSR;
std::string test_dir_path_;
std::string test_file_path_;
};
TEST_F(SetStatTest, ChmodDir) {
// Set up fixture.
SetServerInodeLookup(test_dir_, test_dir_mode_);
struct fuse_out_header out_header = {
.len = sizeof(struct fuse_out_header) + sizeof(struct fuse_attr_out),
.error = 0,
};
mode_t set_mode = S_IRGRP | S_IWGRP | S_IXGRP;
struct fuse_attr_out out_payload = {
.attr = DefaultFuseAttr(set_mode, 2),
};
auto iov_out = FuseGenerateIovecs(out_header, out_payload);
SetServerResponse(FUSE_SETATTR, iov_out);
// Make syscall.
EXPECT_THAT(chmod(test_dir_path_.c_str(), set_mode), SyscallSucceeds());
// Check FUSE request.
struct fuse_in_header in_header;
struct fuse_setattr_in in_payload;
auto iov_in = FuseGenerateIovecs(in_header, in_payload);
GetServerActualRequest(iov_in);
EXPECT_EQ(in_header.len, sizeof(in_header) + sizeof(in_payload));
EXPECT_EQ(in_header.opcode, FUSE_SETATTR);
EXPECT_EQ(in_header.uid, 0);
EXPECT_EQ(in_header.gid, 0);
EXPECT_EQ(in_payload.valid, FATTR_MODE);
EXPECT_EQ(in_payload.mode, S_IFDIR | set_mode);
}
TEST_F(SetStatTest, ChownDir) {
// Set up fixture.
SetServerInodeLookup(test_dir_, test_dir_mode_);
struct fuse_out_header out_header = {
.len = sizeof(struct fuse_out_header) + sizeof(struct fuse_attr_out),
.error = 0,
};
struct fuse_attr_out out_payload = {
.attr = DefaultFuseAttr(test_dir_mode_, 2),
};
auto iov_out = FuseGenerateIovecs(out_header, out_payload);
SetServerResponse(FUSE_SETATTR, iov_out);
// Make syscall.
EXPECT_THAT(chown(test_dir_path_.c_str(), 1025, 1025), SyscallSucceeds());
// Check FUSE request.
struct fuse_in_header in_header;
struct fuse_setattr_in in_payload;
auto iov_in = FuseGenerateIovecs(in_header, in_payload);
GetServerActualRequest(iov_in);
EXPECT_EQ(in_header.len, sizeof(in_header) + sizeof(in_payload));
EXPECT_EQ(in_header.opcode, FUSE_SETATTR);
EXPECT_EQ(in_header.uid, 0);
EXPECT_EQ(in_header.gid, 0);
EXPECT_EQ(in_payload.valid, FATTR_UID | FATTR_GID);
EXPECT_EQ(in_payload.uid, 1025);
EXPECT_EQ(in_payload.gid, 1025);
}
TEST_F(SetStatTest, TruncateFile) {
// Set up fixture.
SetServerInodeLookup(test_file_, test_file_mode_);
struct fuse_out_header out_header = {
.len = sizeof(struct fuse_out_header) + sizeof(struct fuse_attr_out),
.error = 0,
};
struct fuse_attr_out out_payload = {
.attr = DefaultFuseAttr(S_IFREG | S_IRUSR | S_IWUSR, 2),
};
auto iov_out = FuseGenerateIovecs(out_header, out_payload);
SetServerResponse(FUSE_SETATTR, iov_out);
// Make syscall.
EXPECT_THAT(truncate(test_file_path_.c_str(), 321), SyscallSucceeds());
// Check FUSE request.
struct fuse_in_header in_header;
struct fuse_setattr_in in_payload;
auto iov_in = FuseGenerateIovecs(in_header, in_payload);
GetServerActualRequest(iov_in);
EXPECT_EQ(in_header.len, sizeof(in_header) + sizeof(in_payload));
EXPECT_EQ(in_header.opcode, FUSE_SETATTR);
EXPECT_EQ(in_header.uid, 0);
EXPECT_EQ(in_header.gid, 0);
EXPECT_EQ(in_payload.valid, FATTR_SIZE);
EXPECT_EQ(in_payload.size, 321);
}
TEST_F(SetStatTest, UtimeFile) {
// Set up fixture.
SetServerInodeLookup(test_file_, test_file_mode_);
struct fuse_out_header out_header = {
.len = sizeof(struct fuse_out_header) + sizeof(struct fuse_attr_out),
.error = 0,
};
struct fuse_attr_out out_payload = {
.attr = DefaultFuseAttr(S_IFREG | S_IRUSR | S_IWUSR, 2),
};
auto iov_out = FuseGenerateIovecs(out_header, out_payload);
SetServerResponse(FUSE_SETATTR, iov_out);
// Make syscall.
time_t expected_atime = 1597159766, expected_mtime = 1597159765;
struct utimbuf times = {
.actime = expected_atime,
.modtime = expected_mtime,
};
EXPECT_THAT(utime(test_file_path_.c_str(), ×), SyscallSucceeds());
// Check FUSE request.
struct fuse_in_header in_header;
struct fuse_setattr_in in_payload;
auto iov_in = FuseGenerateIovecs(in_header, in_payload);
GetServerActualRequest(iov_in);
EXPECT_EQ(in_header.len, sizeof(in_header) + sizeof(in_payload));
EXPECT_EQ(in_header.opcode, FUSE_SETATTR);
EXPECT_EQ(in_header.uid, 0);
EXPECT_EQ(in_header.gid, 0);
EXPECT_EQ(in_payload.valid, FATTR_ATIME | FATTR_MTIME);
EXPECT_EQ(in_payload.atime, expected_atime);
EXPECT_EQ(in_payload.mtime, expected_mtime);
}
TEST_F(SetStatTest, UtimesFile) {
// Set up fixture.
SetServerInodeLookup(test_file_, test_file_mode_);
struct fuse_out_header out_header = {
.len = sizeof(struct fuse_out_header) + sizeof(struct fuse_attr_out),
.error = 0,
};
struct fuse_attr_out out_payload = {
.attr = DefaultFuseAttr(test_file_mode_, 2),
};
auto iov_out = FuseGenerateIovecs(out_header, out_payload);
SetServerResponse(FUSE_SETATTR, iov_out);
// Make syscall.
struct timeval expected_times[2] = {
{
.tv_sec = 1597159766,
.tv_usec = 234945,
},
{
.tv_sec = 1597159765,
.tv_usec = 232341,
},
};
EXPECT_THAT(utimes(test_file_path_.c_str(), expected_times),
SyscallSucceeds());
// Check FUSE request.
struct fuse_in_header in_header;
struct fuse_setattr_in in_payload;
auto iov_in = FuseGenerateIovecs(in_header, in_payload);
GetServerActualRequest(iov_in);
EXPECT_EQ(in_header.len, sizeof(in_header) + sizeof(in_payload));
EXPECT_EQ(in_header.opcode, FUSE_SETATTR);
EXPECT_EQ(in_header.uid, 0);
EXPECT_EQ(in_header.gid, 0);
EXPECT_EQ(in_payload.valid, FATTR_ATIME | FATTR_MTIME);
EXPECT_EQ(in_payload.atime, expected_times[0].tv_sec);
EXPECT_EQ(in_payload.atimensec, expected_times[0].tv_usec * 1000);
EXPECT_EQ(in_payload.mtime, expected_times[1].tv_sec);
EXPECT_EQ(in_payload.mtimensec, expected_times[1].tv_usec * 1000);
}
TEST_F(SetStatTest, FtruncateFile) {
// Set up fixture.
SetServerInodeLookup(test_file_, test_file_mode_);
auto fd = ASSERT_NO_ERRNO_AND_VALUE(OpenPath(test_file_path_, O_RDWR, fh));
auto close_fd = CloseFD(fd);
struct fuse_out_header out_header = {
.len = sizeof(struct fuse_out_header) + sizeof(struct fuse_attr_out),
.error = 0,
};
struct fuse_attr_out out_payload = {
.attr = DefaultFuseAttr(test_file_mode_, 2),
};
auto iov_out = FuseGenerateIovecs(out_header, out_payload);
SetServerResponse(FUSE_SETATTR, iov_out);
// Make syscall.
EXPECT_THAT(ftruncate(fd.get(), 321), SyscallSucceeds());
// Check FUSE request.
struct fuse_in_header in_header;
struct fuse_setattr_in in_payload;
auto iov_in = FuseGenerateIovecs(in_header, in_payload);
GetServerActualRequest(iov_in);
EXPECT_EQ(in_header.len, sizeof(in_header) + sizeof(in_payload));
EXPECT_EQ(in_header.opcode, FUSE_SETATTR);
EXPECT_EQ(in_header.uid, 0);
EXPECT_EQ(in_header.gid, 0);
EXPECT_EQ(in_payload.valid, FATTR_SIZE | FATTR_FH);
EXPECT_EQ(in_payload.fh, fh);
EXPECT_EQ(in_payload.size, 321);
}
TEST_F(SetStatTest, FchmodFile) {
// Set up fixture.
SetServerInodeLookup(test_file_, test_file_mode_);
auto fd = ASSERT_NO_ERRNO_AND_VALUE(OpenPath(test_file_path_, O_RDWR, fh));
auto close_fd = CloseFD(fd);
struct fuse_out_header out_header = {
.len = sizeof(struct fuse_out_header) + sizeof(struct fuse_attr_out),
.error = 0,
};
mode_t set_mode = S_IROTH | S_IWOTH | S_IXOTH;
struct fuse_attr_out out_payload = {
.attr = DefaultFuseAttr(set_mode, 2),
};
auto iov_out = FuseGenerateIovecs(out_header, out_payload);
SetServerResponse(FUSE_SETATTR, iov_out);
// Make syscall.
EXPECT_THAT(fchmod(fd.get(), set_mode), SyscallSucceeds());
// Check FUSE request.
struct fuse_in_header in_header;
struct fuse_setattr_in in_payload;
auto iov_in = FuseGenerateIovecs(in_header, in_payload);
GetServerActualRequest(iov_in);
EXPECT_EQ(in_header.len, sizeof(in_header) + sizeof(in_payload));
EXPECT_EQ(in_header.opcode, FUSE_SETATTR);
EXPECT_EQ(in_header.uid, 0);
EXPECT_EQ(in_header.gid, 0);
EXPECT_EQ(in_payload.valid, FATTR_MODE | FATTR_FH);
EXPECT_EQ(in_payload.fh, fh);
EXPECT_EQ(in_payload.mode, S_IFREG | set_mode);
}
TEST_F(SetStatTest, FchownFile) {
// Set up fixture.
SetServerInodeLookup(test_file_, test_file_mode_);
auto fd = ASSERT_NO_ERRNO_AND_VALUE(OpenPath(test_file_path_, O_RDWR, fh));
auto close_fd = CloseFD(fd);
struct fuse_out_header out_header = {
.len = sizeof(struct fuse_out_header) + sizeof(struct fuse_attr_out),
.error = 0,
};
struct fuse_attr_out out_payload = {
.attr = DefaultFuseAttr(S_IFREG | S_IRUSR | S_IWUSR | S_IXUSR, 2),
};
auto iov_out = FuseGenerateIovecs(out_header, out_payload);
SetServerResponse(FUSE_SETATTR, iov_out);
// Make syscall.
EXPECT_THAT(fchown(fd.get(), 1025, 1025), SyscallSucceeds());
// Check FUSE request.
struct fuse_in_header in_header;
struct fuse_setattr_in in_payload;
auto iov_in = FuseGenerateIovecs(in_header, in_payload);
GetServerActualRequest(iov_in);
EXPECT_EQ(in_header.len, sizeof(in_header) + sizeof(in_payload));
EXPECT_EQ(in_header.opcode, FUSE_SETATTR);
EXPECT_EQ(in_header.uid, 0);
EXPECT_EQ(in_header.gid, 0);
EXPECT_EQ(in_payload.valid, FATTR_UID | FATTR_GID | FATTR_FH);
EXPECT_EQ(in_payload.fh, fh);
EXPECT_EQ(in_payload.uid, 1025);
EXPECT_EQ(in_payload.gid, 1025);
}
} // namespace
} // namespace testing
} // namespace gvisor
|