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 err = connect(fd, (struct sockaddr*)&sa, sizeof(sa)); 128 if (err < 0) { 129 perror("connect"); 130 return (void*)(intptr_t)errno; 131 } 132 133 while (sent < msgsize) { 134 ssize_t r; 135 136 r = write(fd, MESSAGE + sent, msgsize - sent); 137 if (r < 0) 138 return (void*)(intptr_t)errno; 139 else 140 sent += r; 141 142 } 143 return 0; 144 } 145 146 /* 147 * Reading and writing unix-domain sockets works. None of the I/O actually 148 * goes through FUSE. 149 */ 150 TEST_F(Socket, read_write) 151 { 152 mode_t mode = S_IFSOCK | 0755; 153 const int bufsize = 80; 154 char message[bufsize]; 155 struct sockaddr_un sa; 156 ssize_t recvd = 0, r; 157 uint64_t ino = 42; 158 int fd, connected; 159 Sequence seq; 160 161 EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH) 162 .WillOnce(Invoke(ReturnErrno(ENOENT))); 163 EXPECT_CALL(*m_mock, process( 164 ResultOf([=](auto in) { 165 return (in.header.opcode == FUSE_MKNOD); 166 }, Eq(true)), 167 _) 168 ).InSequence(seq) 169 .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) { 170 SET_OUT_HEADER_LEN(out, entry); 171 out.body.entry.attr.mode = mode; 172 out.body.entry.nodeid = ino; 173 out.body.entry.entry_valid = UINT64_MAX; 174 out.body.entry.attr_valid = UINT64_MAX; 175 }))); 176 177 EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH) 178 .InSequence(seq) 179 .WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) { 180 SET_OUT_HEADER_LEN(out, entry); 181 out.body.entry.attr.mode = mode; 182 out.body.entry.nodeid = ino; 183 out.body.entry.attr.nlink = 1; 184 out.body.entry.attr_valid = UINT64_MAX; 185 out.body.entry.entry_valid = UINT64_MAX; 186 }))); 187 188 fd = socket(AF_UNIX, SOCK_STREAM, 0); 189 ASSERT_LE(0, fd) << strerror(errno); 190 sa.sun_family = AF_UNIX; 191 strlcpy(sa.sun_path, FULLPATH, sizeof(sa.sun_path)); 192 ASSERT_EQ(0, bind(fd, (struct sockaddr*)&sa, sizeof(sa))) 193 << strerror(errno); 194 listen(fd, 5); 195 ASSERT_EQ(0, pthread_create(&m_child, NULL, socket_writer, NULL)) 196 << strerror(errno); 197 connected = accept(fd, 0, 0); 198 ASSERT_LE(0, connected) << strerror(errno); 199 200 while (recvd < msgsize) { 201 r = read(connected, message + recvd, bufsize - recvd); 202 ASSERT_LE(0, r) << strerror(errno); 203 ASSERT_LT(0, r) << "unexpected EOF"; 204 recvd += r; 205 } 206 ASSERT_STREQ(message, MESSAGE); 207 208 leak(fd); 209 } 210