2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 The FreeBSD Foundation
6 * This software was developed by BFF Storage Systems, LLC under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/types.h>
35 #include <sys/socket.h>
43 using namespace testing;
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);
50 class Fifo: public FuseTest {
54 Fifo(): m_child(NULL) {};
57 if (m_child != NULL) {
58 pthread_join(m_child, NULL);
64 class Socket: public Fifo {};
67 static void* writer(void* arg) {
72 while (sent < msgsize) {
75 r = write(fd, MESSAGE + sent, msgsize - sent);
77 return (void*)(intptr_t)errno;
86 * Reading and writing FIFOs works. None of the I/O actually goes through FUSE
88 TEST_F(Fifo, read_write)
90 mode_t mode = S_IFIFO | 0755;
91 const int bufsize = 80;
92 char message[bufsize];
97 expect_lookup(RELPATH, ino, mode, 0, 1);
99 fd = open(FULLPATH, O_RDWR);
100 ASSERT_LE(0, fd) << strerror(errno);
101 ASSERT_EQ(0, pthread_create(&m_child, NULL, writer, &fd))
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";
109 ASSERT_STREQ(message, MESSAGE);
115 static void* socket_writer(void* arg __unused) {
118 struct sockaddr_un sa;
120 fd = socket(AF_UNIX, SOCK_STREAM, 0);
123 return (void*)(intptr_t)errno;
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));
131 return (void*)(intptr_t)errno;
134 while (sent < msgsize) {
137 r = write(fd, MESSAGE + sent, msgsize - sent);
139 return (void*)(intptr_t)errno;
150 * Reading and writing unix-domain sockets works. None of the I/O actually
153 TEST_F(Socket, read_write)
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;
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);
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;
180 EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
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;
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)))
199 ASSERT_EQ(0, pthread_create(&m_child, NULL, socket_writer, NULL))
201 connected = accept(fd, 0, 0);
202 ASSERT_LE(0, connected) << strerror(errno);
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";
210 ASSERT_STREQ(message, MESSAGE);