xref: /freebsd/tests/sys/fs/fusefs/fifo.cc (revision 6be3386466ab79a84b48429ae66244f21526d3df)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2019 The FreeBSD Foundation
5  *
6  * This software was developed by BFF Storage Systems, LLC under sponsorship
7  * from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32 
33 extern "C" {
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <sys/un.h>
37 #include <fcntl.h>
38 }
39 
40 #include "mockfs.hh"
41 #include "utils.hh"
42 
43 using namespace testing;
44 
45 const char FULLPATH[] = "mountpoint/some_fifo";
46 const char RELPATH[] = "some_fifo";
47 const char MESSAGE[] = "Hello, World!\n";
48 const int msgsize = sizeof(MESSAGE);
49 
50 class Fifo: public FuseTest {
51 public:
52 pthread_t m_child;
53 
54 Fifo(): m_child(NULL) {};
55 
56 void TearDown() {
57 	if (m_child != NULL) {
58 		pthread_join(m_child, NULL);
59 	}
60 	FuseTest::TearDown();
61 }
62 };
63 
64 class Socket: public Fifo {};
65 
66 /* Writer thread */
67 static void* writer(void* arg) {
68 	ssize_t sent = 0;
69 	int fd;
70 
71 	fd = *(int*)arg;
72 	while (sent < msgsize) {
73 		ssize_t r;
74 
75 		r = write(fd, MESSAGE + sent, msgsize - sent);
76 		if (r < 0)
77 			return (void*)(intptr_t)errno;
78 		else
79 			sent += r;
80 
81 	}
82 	return 0;
83 }
84 
85 /*
86  * Reading and writing FIFOs works.  None of the I/O actually goes through FUSE
87  */
88 TEST_F(Fifo, read_write)
89 {
90 	mode_t mode = S_IFIFO | 0755;
91 	const int bufsize = 80;
92 	char message[bufsize];
93 	ssize_t recvd = 0, r;
94 	uint64_t ino = 42;
95 	int fd;
96 
97 	expect_lookup(RELPATH, ino, mode, 0, 1);
98 
99 	fd = open(FULLPATH, O_RDWR);
100 	ASSERT_LE(0, fd) << strerror(errno);
101 	ASSERT_EQ(0, pthread_create(&m_child, NULL, writer, &fd))
102 		<< strerror(errno);
103 	while (recvd < msgsize) {
104 		r = read(fd, message + recvd, bufsize - recvd);
105 		ASSERT_LE(0, r) << strerror(errno);
106 		ASSERT_LT(0, r) << "unexpected EOF";
107 		recvd += r;
108 	}
109 	ASSERT_STREQ(message, MESSAGE);
110 
111 	leak(fd);
112 }
113 
114 /* Writer thread */
115 static void* socket_writer(void* arg __unused) {
116 	ssize_t sent = 0;
117 	int fd, err;
118 	struct sockaddr_un sa;
119 
120 	fd = socket(AF_UNIX, SOCK_STREAM, 0);
121 	if (fd < 0) {
122 		perror("socket");
123 		return (void*)(intptr_t)errno;
124 	}
125 	sa.sun_family = AF_UNIX;
126 	strlcpy(sa.sun_path, FULLPATH, sizeof(sa.sun_path));
127 	sa.sun_len = sizeof(FULLPATH);
128 	err = connect(fd, (struct sockaddr*)&sa, sizeof(sa));
129 	if (err < 0) {
130 		perror("connect");
131 		return (void*)(intptr_t)errno;
132 	}
133 
134 	while (sent < msgsize) {
135 		ssize_t r;
136 
137 		r = write(fd, MESSAGE + sent, msgsize - sent);
138 		if (r < 0)
139 			return (void*)(intptr_t)errno;
140 		else
141 			sent += r;
142 
143 	}
144 
145 	FuseTest::leak(fd);
146 	return 0;
147 }
148 
149 /*
150  * Reading and writing unix-domain sockets works.  None of the I/O actually
151  * goes through FUSE.
152  */
153 TEST_F(Socket, read_write)
154 {
155 	mode_t mode = S_IFSOCK | 0755;
156 	const int bufsize = 80;
157 	char message[bufsize];
158 	struct sockaddr_un sa;
159 	ssize_t recvd = 0, r;
160 	uint64_t ino = 42;
161 	int fd, connected;
162 	Sequence seq;
163 
164 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
165 		.WillOnce(Invoke(ReturnErrno(ENOENT)));
166 	EXPECT_CALL(*m_mock, process(
167 		ResultOf([=](auto in) {
168 			return (in.header.opcode == FUSE_MKNOD);
169 		}, Eq(true)),
170 		_)
171 	).InSequence(seq)
172 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
173 		SET_OUT_HEADER_LEN(out, entry);
174 		out.body.entry.attr.mode = mode;
175 		out.body.entry.nodeid = ino;
176 		out.body.entry.entry_valid = UINT64_MAX;
177 		out.body.entry.attr_valid = UINT64_MAX;
178 	})));
179 
180 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
181 	.InSequence(seq)
182 	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
183 		SET_OUT_HEADER_LEN(out, entry);
184 		out.body.entry.attr.mode = mode;
185 		out.body.entry.nodeid = ino;
186 		out.body.entry.attr.nlink = 1;
187 		out.body.entry.attr_valid = UINT64_MAX;
188 		out.body.entry.entry_valid = UINT64_MAX;
189 	})));
190 
191 	fd = socket(AF_UNIX, SOCK_STREAM, 0);
192 	ASSERT_LE(0, fd) << strerror(errno);
193 	sa.sun_family = AF_UNIX;
194 	strlcpy(sa.sun_path, FULLPATH, sizeof(sa.sun_path));
195 	sa.sun_len = sizeof(FULLPATH);
196 	ASSERT_EQ(0, bind(fd, (struct sockaddr*)&sa, sizeof(sa)))
197 		<< strerror(errno);
198 	listen(fd, 5);
199 	ASSERT_EQ(0, pthread_create(&m_child, NULL, socket_writer, NULL))
200 		<< strerror(errno);
201 	connected = accept(fd, 0, 0);
202 	ASSERT_LE(0, connected) << strerror(errno);
203 
204 	while (recvd < msgsize) {
205 		r = read(connected, message + recvd, bufsize - recvd);
206 		ASSERT_LE(0, r) << strerror(errno);
207 		ASSERT_LT(0, r) << "unexpected EOF";
208 		recvd += r;
209 	}
210 	ASSERT_STREQ(message, MESSAGE);
211 
212 	leak(fd);
213 }
214