aboutsummaryrefslogtreecommitdiff
path: root/tests/sys/fs/fusefs/open.cc
blob: 1212a7047f268620e0aba773ea9c66d9a98713aa (plain) (blame)
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
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2019 The FreeBSD Foundation
 *
 * This software was developed by BFF Storage Systems, LLC under sponsorship
 * from the FreeBSD Foundation.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

extern "C" {
#include <sys/wait.h>

#include <fcntl.h>
#include <semaphore.h>
}

#include "mockfs.hh"
#include "utils.hh"

using namespace testing;

class Open: public FuseTest {

public:

/* Test an OK open of a file with the given flags */
void test_ok(int os_flags, int fuse_flags) {
	const char FULLPATH[] = "mountpoint/some_file.txt";
	const char RELPATH[] = "some_file.txt";
	uint64_t ino = 42;
	int fd;

	FuseTest::expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
	EXPECT_CALL(*m_mock, process(
		ResultOf([=](auto in) {
			return (in.header.opcode == FUSE_OPEN &&
				in.body.open.flags == (uint32_t)fuse_flags &&
				in.header.nodeid == ino);
		}, Eq(true)),
		_)
	).WillOnce(Invoke(ReturnImmediate([](auto in __unused, auto& out) {
		out.header.len = sizeof(out.header);
		SET_OUT_HEADER_LEN(out, open);
	})));

	fd = open(FULLPATH, os_flags);
	ASSERT_LE(0, fd) << strerror(errno);
	leak(fd);
}
};

/* 
 * fusefs(4) does not support I/O on device nodes (neither does UFS).  But it
 * shouldn't crash
 */
TEST_F(Open, chr)
{
	const char FULLPATH[] = "mountpoint/zero";
	const char RELPATH[] = "zero";
	uint64_t ino = 42;

	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
	.WillRepeatedly(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
		SET_OUT_HEADER_LEN(out, entry);
		out.body.entry.attr.mode = S_IFCHR | 0644;
		out.body.entry.nodeid = ino;
		out.body.entry.attr.nlink = 1;
		out.body.entry.attr_valid = UINT64_MAX;
		out.body.entry.attr.rdev = 44;	/* /dev/zero's rdev */
	})));

	ASSERT_EQ(-1, open(FULLPATH, O_RDONLY));
	EXPECT_EQ(EOPNOTSUPP, errno);
}

/* 
 * The fuse daemon fails the request with enoent.  This usually indicates a
 * race condition: some other FUSE client removed the file in between when the
 * kernel checked for it with lookup and tried to open it
 */
TEST_F(Open, enoent)
{
	const char FULLPATH[] = "mountpoint/some_file.txt";
	const char RELPATH[] = "some_file.txt";
	uint64_t ino = 42;
	sem_t sem;

	ASSERT_EQ(0, sem_init(&sem, 0, 0)) << strerror(errno);

	expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
	EXPECT_CALL(*m_mock, process(
		ResultOf([=](auto in) {
			return (in.header.opcode == FUSE_OPEN &&
				in.header.nodeid == ino);
		}, Eq(true)),
		_)
	).WillOnce(Invoke(ReturnErrno(ENOENT)));
	// Since FUSE_OPEN returns ENOENT, the kernel will reclaim the vnode
	// and send a FUSE_FORGET
	expect_forget(ino, 1, &sem);

	ASSERT_EQ(-1, open(FULLPATH, O_RDONLY));
	EXPECT_EQ(ENOENT, errno);

	sem_wait(&sem);
	sem_destroy(&sem);
}

/* 
 * The daemon is responsible for checking file permissions (unless the
 * default_permissions mount option was used)
 */
TEST_F(Open, eperm)
{
	const char FULLPATH[] = "mountpoint/some_file.txt";
	const char RELPATH[] = "some_file.txt";
	uint64_t ino = 42;

	expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 1);
	EXPECT_CALL(*m_mock, process(
		ResultOf([=](auto in) {
			return (in.header.opcode == FUSE_OPEN &&
				in.header.nodeid == ino);
		}, Eq(true)),
		_)
	).WillOnce(Invoke(ReturnErrno(EPERM)));
	ASSERT_EQ(-1, open(FULLPATH, O_RDONLY));
	EXPECT_EQ(EPERM, errno);
}

/* 
 * fusefs must issue multiple FUSE_OPEN operations if clients with different
 * credentials open the same file, even if they use the same mode.  This is
 * necessary so that the daemon can validate each set of credentials.
 */
TEST_F(Open, multiple_creds)
{
	const static char FULLPATH[] = "mountpoint/some_file.txt";
	const static char RELPATH[] = "some_file.txt";
	int fd1, status;
	const static uint64_t ino = 42;
	const static uint64_t fh0 = 100, fh1 = 200;

	/* Fork a child to open the file with different credentials */
	fork(false, &status, [&] {

		expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 2);
		EXPECT_CALL(*m_mock, process(
			ResultOf([=](auto in) {
				return (in.header.opcode == FUSE_OPEN &&
					in.header.pid == (uint32_t)getpid() &&
					in.header.nodeid == ino);
			}, Eq(true)),
			_)
		).WillOnce(Invoke(
			ReturnImmediate([](auto in __unused, auto& out) {
			out.body.open.fh = fh0;
			out.header.len = sizeof(out.header);
			SET_OUT_HEADER_LEN(out, open);
		})));

		EXPECT_CALL(*m_mock, process(
			ResultOf([=](auto in) {
				return (in.header.opcode == FUSE_OPEN &&
					in.header.pid != (uint32_t)getpid() &&
					in.header.nodeid == ino);
			}, Eq(true)),
			_)
		).WillOnce(Invoke(
			ReturnImmediate([](auto in __unused, auto& out) {
			out.body.open.fh = fh1;
			out.header.len = sizeof(out.header);
			SET_OUT_HEADER_LEN(out, open);
		})));
		expect_flush(ino, 2, ReturnErrno(0));
		expect_release(ino, fh0);
		expect_release(ino, fh1);

		fd1 = open(FULLPATH, O_RDONLY);
		ASSERT_LE(0, fd1) << strerror(errno);
	}, [] {
		int fd0;

		fd0 = open(FULLPATH, O_RDONLY);
		if (fd0 < 0) {
			perror("open");
			return(1);
		}
		leak(fd0);
		return 0;
	}
	);
	ASSERT_EQ(0, WEXITSTATUS(status));

	close(fd1);
}

/* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236340 */
TEST_F(Open, DISABLED_o_append)
{
	test_ok(O_WRONLY | O_APPEND, O_WRONLY | O_APPEND);
}

/* The kernel is supposed to filter out this flag */
TEST_F(Open, o_creat)
{
	test_ok(O_WRONLY | O_CREAT, O_WRONLY);
}

/* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236340 */
TEST_F(Open, DISABLED_o_direct)
{
	test_ok(O_WRONLY | O_DIRECT, O_WRONLY | O_DIRECT);
}

/* The kernel is supposed to filter out this flag */
TEST_F(Open, o_excl)
{
	test_ok(O_WRONLY | O_EXCL, O_WRONLY);
}

TEST_F(Open, o_exec)
{
	test_ok(O_EXEC, O_EXEC);
}

/* The kernel is supposed to filter out this flag */
TEST_F(Open, o_noctty)
{
	test_ok(O_WRONLY | O_NOCTTY, O_WRONLY);
}

TEST_F(Open, o_rdonly)
{
	test_ok(O_RDONLY, O_RDONLY);
}

/* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236340 */
TEST_F(Open, DISABLED_o_trunc)
{
	test_ok(O_WRONLY | O_TRUNC, O_WRONLY | O_TRUNC);
}

TEST_F(Open, o_wronly)
{
	test_ok(O_WRONLY, O_WRONLY);
}

TEST_F(Open, o_rdwr)
{
	test_ok(O_RDWR, O_RDWR);
}

/*
 * If a fuse server returns ENOSYS to a
 * FUSE_OPEN, then it and subsequent FUSE_OPEN and FUSE_RELEASE operations will
 * also succeed automatically without being sent to the server.
 */
TEST_F(Open, enosys)
{
	const char FULLPATH[] = "mountpoint/some_file.txt";
	const char RELPATH[] = "some_file.txt";
	uint64_t ino = 42;
	int fd;

	FuseTest::expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 2);
	EXPECT_CALL(*m_mock, process(
		ResultOf([=](auto in) {
			return (in.header.opcode == FUSE_OPEN &&
				in.body.open.flags == (uint32_t)O_RDONLY &&
				in.header.nodeid == ino);
		}, Eq(true)),
		_)
	).Times(1)
	.WillOnce(Invoke(ReturnErrno(ENOSYS)));
	expect_flush(ino, 1, ReturnErrno(ENOSYS));

	fd = open(FULLPATH, O_RDONLY);
	ASSERT_LE(0, fd) << strerror(errno);
	close(fd);

	fd = open(FULLPATH, O_RDONLY);
	ASSERT_LE(0, fd) << strerror(errno);

	leak(fd);
}