19821f1d3SAlan Somers /*- 29821f1d3SAlan Somers * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 39821f1d3SAlan Somers * 49821f1d3SAlan Somers * Copyright (c) 2019 The FreeBSD Foundation 59821f1d3SAlan Somers * 69821f1d3SAlan Somers * This software was developed by BFF Storage Systems, LLC under sponsorship 79821f1d3SAlan Somers * from the FreeBSD Foundation. 89821f1d3SAlan Somers * 99821f1d3SAlan Somers * Redistribution and use in source and binary forms, with or without 109821f1d3SAlan Somers * modification, are permitted provided that the following conditions 119821f1d3SAlan Somers * are met: 129821f1d3SAlan Somers * 1. Redistributions of source code must retain the above copyright 139821f1d3SAlan Somers * notice, this list of conditions and the following disclaimer. 149821f1d3SAlan Somers * 2. Redistributions in binary form must reproduce the above copyright 159821f1d3SAlan Somers * notice, this list of conditions and the following disclaimer in the 169821f1d3SAlan Somers * documentation and/or other materials provided with the distribution. 179821f1d3SAlan Somers * 189821f1d3SAlan Somers * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 199821f1d3SAlan Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 209821f1d3SAlan Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 219821f1d3SAlan Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 229821f1d3SAlan Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 239821f1d3SAlan Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 249821f1d3SAlan Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 259821f1d3SAlan Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 269821f1d3SAlan Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 279821f1d3SAlan Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 289821f1d3SAlan Somers * SUCH DAMAGE. 291fa8ebfbSAlan Somers * 301fa8ebfbSAlan Somers * $FreeBSD$ 319821f1d3SAlan Somers */ 329821f1d3SAlan Somers 3309c01e67SAlan Somers extern "C" { 349821f1d3SAlan Somers #include <sys/param.h> 3509c01e67SAlan Somers #include <sys/mman.h> 369821f1d3SAlan Somers #include <sys/module.h> 379821f1d3SAlan Somers #include <sys/sysctl.h> 3809c01e67SAlan Somers #include <sys/wait.h> 3909c01e67SAlan Somers 40e5b50fe7SAlan Somers #include <dirent.h> 41d4fd0c81SAlan Somers #include <fcntl.h> 4261b0a927SAlan Somers #include <grp.h> 4309c01e67SAlan Somers #include <pwd.h> 4409c01e67SAlan Somers #include <semaphore.h> 4509c01e67SAlan Somers #include <unistd.h> 4609c01e67SAlan Somers } 479821f1d3SAlan Somers 489821f1d3SAlan Somers #include <gtest/gtest.h> 499821f1d3SAlan Somers 509821f1d3SAlan Somers #include "mockfs.hh" 519821f1d3SAlan Somers #include "utils.hh" 529821f1d3SAlan Somers 539821f1d3SAlan Somers using namespace testing; 549821f1d3SAlan Somers 558eecd9ceSAlan Somers /* 568eecd9ceSAlan Somers * The default max_write is set to this formula in libfuse, though 578eecd9ceSAlan Somers * individual filesystems can lower it. The "- 4096" was added in 588eecd9ceSAlan Somers * commit 154ffe2, with the commit message "fix". 598eecd9ceSAlan Somers */ 608eecd9ceSAlan Somers const uint32_t libfuse_max_write = 32 * getpagesize() + 0x1000 - 4096; 618eecd9ceSAlan Somers 628eecd9ceSAlan Somers /* 638eecd9ceSAlan Somers * Set the default max_write to a distinct value from MAXPHYS to catch bugs 648eecd9ceSAlan Somers * that confuse the two. 658eecd9ceSAlan Somers */ 668eecd9ceSAlan Somers const uint32_t default_max_write = MIN(libfuse_max_write, MAXPHYS / 2); 678eecd9ceSAlan Somers 688eecd9ceSAlan Somers 699821f1d3SAlan Somers /* Check that fusefs(4) is accessible and the current user can mount(2) */ 709821f1d3SAlan Somers void check_environment() 719821f1d3SAlan Somers { 729821f1d3SAlan Somers const char *devnode = "/dev/fuse"; 73b43a935cSAlan Somers const char *bsdextended_node = "security.mac.bsdextended.enabled"; 74b43a935cSAlan Somers int bsdextended_val = 0; 75b43a935cSAlan Somers size_t bsdextended_size = sizeof(bsdextended_val); 76b43a935cSAlan Somers int bsdextended_found; 779821f1d3SAlan Somers const char *usermount_node = "vfs.usermount"; 789821f1d3SAlan Somers int usermount_val = 0; 799821f1d3SAlan Somers size_t usermount_size = sizeof(usermount_val); 809821f1d3SAlan Somers if (eaccess(devnode, R_OK | W_OK)) { 819821f1d3SAlan Somers if (errno == ENOENT) { 829821f1d3SAlan Somers GTEST_SKIP() << devnode << " does not exist"; 839821f1d3SAlan Somers } else if (errno == EACCES) { 849821f1d3SAlan Somers GTEST_SKIP() << devnode << 859821f1d3SAlan Somers " is not accessible by the current user"; 869821f1d3SAlan Somers } else { 879821f1d3SAlan Somers GTEST_SKIP() << strerror(errno); 889821f1d3SAlan Somers } 899821f1d3SAlan Somers } 90b43a935cSAlan Somers // mac_bsdextended(4), when enabled, generates many more GETATTR 91b43a935cSAlan Somers // operations. The fusefs tests' expectations don't account for those, 92b43a935cSAlan Somers // and adding extra code to handle them obfuscates the real purpose of 93b43a935cSAlan Somers // the tests. Better just to skip the fusefs tests if mac_bsdextended 94b43a935cSAlan Somers // is enabled. 95b43a935cSAlan Somers bsdextended_found = sysctlbyname(bsdextended_node, &bsdextended_val, 96b43a935cSAlan Somers &bsdextended_size, NULL, 0); 97b43a935cSAlan Somers if (bsdextended_found == 0 && bsdextended_val != 0) 98b43a935cSAlan Somers GTEST_SKIP() << 99b43a935cSAlan Somers "The fusefs tests are incompatible with mac_bsdextended."; 1008e765737SAlan Somers ASSERT_EQ(sysctlbyname(usermount_node, &usermount_val, &usermount_size, 1018e765737SAlan Somers NULL, 0), 102b43a935cSAlan Somers 0); 1039821f1d3SAlan Somers if (geteuid() != 0 && !usermount_val) 1049821f1d3SAlan Somers GTEST_SKIP() << "current user is not allowed to mount"; 1059821f1d3SAlan Somers } 1069821f1d3SAlan Somers 107b0ecfb42SAlan Somers const char *cache_mode_to_s(enum cache_mode cm) { 108b0ecfb42SAlan Somers switch (cm) { 109b0ecfb42SAlan Somers case Uncached: 110b0ecfb42SAlan Somers return "Uncached"; 111b0ecfb42SAlan Somers case Writethrough: 112b0ecfb42SAlan Somers return "Writethrough"; 113b0ecfb42SAlan Somers case Writeback: 114b0ecfb42SAlan Somers return "Writeback"; 115b0ecfb42SAlan Somers case WritebackAsync: 116b0ecfb42SAlan Somers return "WritebackAsync"; 117b0ecfb42SAlan Somers default: 118b0ecfb42SAlan Somers return "Unknown"; 119b0ecfb42SAlan Somers } 120b0ecfb42SAlan Somers } 121b0ecfb42SAlan Somers 122c2265ae7SAlan Somers bool is_unsafe_aio_enabled(void) { 123c2265ae7SAlan Somers const char *node = "vfs.aio.enable_unsafe"; 124c2265ae7SAlan Somers int val = 0; 125c2265ae7SAlan Somers size_t size = sizeof(val); 126c2265ae7SAlan Somers 127c2265ae7SAlan Somers if (sysctlbyname(node, &val, &size, NULL, 0)) { 128c2265ae7SAlan Somers perror("sysctlbyname"); 129c2265ae7SAlan Somers return (false); 130c2265ae7SAlan Somers } 131c2265ae7SAlan Somers return (val != 0); 132c2265ae7SAlan Somers } 133c2265ae7SAlan Somers 1349821f1d3SAlan Somers class FuseEnv: public Environment { 1359821f1d3SAlan Somers virtual void SetUp() { 1369821f1d3SAlan Somers } 1379821f1d3SAlan Somers }; 1389821f1d3SAlan Somers 1399821f1d3SAlan Somers void FuseTest::SetUp() { 140f2704f05SAlan Somers const char *maxbcachebuf_node = "vfs.maxbcachebuf"; 141f2704f05SAlan Somers const char *maxphys_node = "kern.maxphys"; 1429821f1d3SAlan Somers int val = 0; 1439821f1d3SAlan Somers size_t size = sizeof(val); 1449821f1d3SAlan Somers 1459821f1d3SAlan Somers /* 1469821f1d3SAlan Somers * XXX check_environment should be called from FuseEnv::SetUp, but 1479821f1d3SAlan Somers * can't due to https://github.com/google/googletest/issues/2189 1489821f1d3SAlan Somers */ 1499821f1d3SAlan Somers check_environment(); 1509821f1d3SAlan Somers if (IsSkipped()) 1519821f1d3SAlan Somers return; 1529821f1d3SAlan Somers 153f2704f05SAlan Somers ASSERT_EQ(0, sysctlbyname(maxbcachebuf_node, &val, &size, NULL, 0)) 1549821f1d3SAlan Somers << strerror(errno); 1559821f1d3SAlan Somers m_maxbcachebuf = val; 156f2704f05SAlan Somers ASSERT_EQ(0, sysctlbyname(maxphys_node, &val, &size, NULL, 0)) 157f2704f05SAlan Somers << strerror(errno); 158f2704f05SAlan Somers m_maxphys = val; 1599821f1d3SAlan Somers 1609821f1d3SAlan Somers try { 16191ff3a0dSAlan Somers m_mock = new MockFS(m_maxreadahead, m_allow_other, 162140bb492SAlan Somers m_default_permissions, m_push_symlinks_in, m_ro, 1638eecd9ceSAlan Somers m_pm, m_init_flags, m_kernel_minor_version, 164ed74f781SAlan Somers m_maxwrite, m_async, m_noclusterr, m_time_gran, 165ed74f781SAlan Somers m_nointr); 166caf5f57dSAlan Somers /* 167caf5f57dSAlan Somers * FUSE_ACCESS is called almost universally. Expecting it in 168caf5f57dSAlan Somers * each test case would be super-annoying. Instead, set a 169caf5f57dSAlan Somers * default expectation for FUSE_ACCESS and return ENOSYS. 170caf5f57dSAlan Somers * 171caf5f57dSAlan Somers * Individual test cases can override this expectation since 172caf5f57dSAlan Somers * googlemock evaluates expectations in LIFO order. 173caf5f57dSAlan Somers */ 174caf5f57dSAlan Somers EXPECT_CALL(*m_mock, process( 175caf5f57dSAlan Somers ResultOf([=](auto in) { 17629edc611SAlan Somers return (in.header.opcode == FUSE_ACCESS); 177caf5f57dSAlan Somers }, Eq(true)), 178caf5f57dSAlan Somers _) 179caf5f57dSAlan Somers ).Times(AnyNumber()) 180caf5f57dSAlan Somers .WillRepeatedly(Invoke(ReturnErrno(ENOSYS))); 181a1c9f4adSAlan Somers /* 182a1c9f4adSAlan Somers * FUSE_BMAP is called for most test cases that read data. Set 183a1c9f4adSAlan Somers * a default expectation and return ENOSYS. 184a1c9f4adSAlan Somers * 185a1c9f4adSAlan Somers * Individual test cases can override this expectation since 186a1c9f4adSAlan Somers * googlemock evaluates expectations in LIFO order. 187a1c9f4adSAlan Somers */ 188a1c9f4adSAlan Somers EXPECT_CALL(*m_mock, process( 189a1c9f4adSAlan Somers ResultOf([=](auto in) { 190a1c9f4adSAlan Somers return (in.header.opcode == FUSE_BMAP); 191a1c9f4adSAlan Somers }, Eq(true)), 192a1c9f4adSAlan Somers _) 193a1c9f4adSAlan Somers ).Times(AnyNumber()) 194a1c9f4adSAlan Somers .WillRepeatedly(Invoke(ReturnErrno(ENOSYS))); 1959821f1d3SAlan Somers } catch (std::system_error err) { 1969821f1d3SAlan Somers FAIL() << err.what(); 1979821f1d3SAlan Somers } 1989821f1d3SAlan Somers } 1999821f1d3SAlan Somers 20091ff3a0dSAlan Somers void 20191ff3a0dSAlan Somers FuseTest::expect_access(uint64_t ino, mode_t access_mode, int error) 20291ff3a0dSAlan Somers { 20391ff3a0dSAlan Somers EXPECT_CALL(*m_mock, process( 20491ff3a0dSAlan Somers ResultOf([=](auto in) { 20529edc611SAlan Somers return (in.header.opcode == FUSE_ACCESS && 20629edc611SAlan Somers in.header.nodeid == ino && 20729edc611SAlan Somers in.body.access.mask == access_mode); 20891ff3a0dSAlan Somers }, Eq(true)), 20991ff3a0dSAlan Somers _) 21091ff3a0dSAlan Somers ).WillOnce(Invoke(ReturnErrno(error))); 21191ff3a0dSAlan Somers } 21291ff3a0dSAlan Somers 2139f10f423SAlan Somers void 2143429092cSAlan Somers FuseTest::expect_destroy(int error) 2153429092cSAlan Somers { 2163429092cSAlan Somers EXPECT_CALL(*m_mock, process( 2173429092cSAlan Somers ResultOf([=](auto in) { 21829edc611SAlan Somers return (in.header.opcode == FUSE_DESTROY); 2193429092cSAlan Somers }, Eq(true)), 2203429092cSAlan Somers _) 22129edc611SAlan Somers ).WillOnce(Invoke( ReturnImmediate([&](auto in, auto& out) { 2223429092cSAlan Somers m_mock->m_quit = true; 22329edc611SAlan Somers out.header.len = sizeof(out.header); 22429edc611SAlan Somers out.header.unique = in.header.unique; 22529edc611SAlan Somers out.header.error = -error; 2263429092cSAlan Somers }))); 2273429092cSAlan Somers } 2283429092cSAlan Somers 2293429092cSAlan Somers void 2309f10f423SAlan Somers FuseTest::expect_flush(uint64_t ino, int times, ProcessMockerT r) 2319f10f423SAlan Somers { 2329f10f423SAlan Somers EXPECT_CALL(*m_mock, process( 2339f10f423SAlan Somers ResultOf([=](auto in) { 23429edc611SAlan Somers return (in.header.opcode == FUSE_FLUSH && 23529edc611SAlan Somers in.header.nodeid == ino); 2369f10f423SAlan Somers }, Eq(true)), 2379f10f423SAlan Somers _) 2389f10f423SAlan Somers ).Times(times) 2399f10f423SAlan Somers .WillRepeatedly(Invoke(r)); 2409f10f423SAlan Somers } 2419f10f423SAlan Somers 242ff4fbdf5SAlan Somers void 2432d6bf515SAlan Somers FuseTest::expect_forget(uint64_t ino, uint64_t nlookup, sem_t *sem) 244ff4fbdf5SAlan Somers { 245ff4fbdf5SAlan Somers EXPECT_CALL(*m_mock, process( 246ff4fbdf5SAlan Somers ResultOf([=](auto in) { 24729edc611SAlan Somers return (in.header.opcode == FUSE_FORGET && 24829edc611SAlan Somers in.header.nodeid == ino && 24929edc611SAlan Somers in.body.forget.nlookup == nlookup); 250ff4fbdf5SAlan Somers }, Eq(true)), 251ff4fbdf5SAlan Somers _) 2522d6bf515SAlan Somers ).WillOnce(Invoke([=](auto in __unused, auto &out __unused) { 2532d6bf515SAlan Somers if (sem != NULL) 2542d6bf515SAlan Somers sem_post(sem); 255ff4fbdf5SAlan Somers /* FUSE_FORGET has no response! */ 256ff4fbdf5SAlan Somers })); 257ff4fbdf5SAlan Somers } 258ff4fbdf5SAlan Somers 2599821f1d3SAlan Somers void FuseTest::expect_getattr(uint64_t ino, uint64_t size) 2609821f1d3SAlan Somers { 2619821f1d3SAlan Somers EXPECT_CALL(*m_mock, process( 2629821f1d3SAlan Somers ResultOf([=](auto in) { 26329edc611SAlan Somers return (in.header.opcode == FUSE_GETATTR && 26429edc611SAlan Somers in.header.nodeid == ino); 2659821f1d3SAlan Somers }, Eq(true)), 2669821f1d3SAlan Somers _) 26729edc611SAlan Somers ).WillOnce(Invoke(ReturnImmediate([=](auto i __unused, auto& out) { 2689821f1d3SAlan Somers SET_OUT_HEADER_LEN(out, attr); 26929edc611SAlan Somers out.body.attr.attr.ino = ino; // Must match nodeid 27029edc611SAlan Somers out.body.attr.attr.mode = S_IFREG | 0644; 27129edc611SAlan Somers out.body.attr.attr.size = size; 27229edc611SAlan Somers out.body.attr.attr_valid = UINT64_MAX; 2739821f1d3SAlan Somers }))); 2749821f1d3SAlan Somers } 2759821f1d3SAlan Somers 276*bfcb817bSAlan Somers void FuseTest::expect_getxattr(uint64_t ino, const char *attr, ProcessMockerT r) 277*bfcb817bSAlan Somers { 278*bfcb817bSAlan Somers EXPECT_CALL(*m_mock, process( 279*bfcb817bSAlan Somers ResultOf([=](auto in) { 280*bfcb817bSAlan Somers const char *a = (const char*)in.body.bytes + 281*bfcb817bSAlan Somers sizeof(fuse_getxattr_in); 282*bfcb817bSAlan Somers return (in.header.opcode == FUSE_GETXATTR && 283*bfcb817bSAlan Somers in.header.nodeid == ino && 284*bfcb817bSAlan Somers 0 == strcmp(attr, a)); 285*bfcb817bSAlan Somers }, Eq(true)), 286*bfcb817bSAlan Somers _) 287*bfcb817bSAlan Somers ).WillOnce(Invoke(r)); 288*bfcb817bSAlan Somers } 289*bfcb817bSAlan Somers 2909821f1d3SAlan Somers void FuseTest::expect_lookup(const char *relpath, uint64_t ino, mode_t mode, 291474ba6faSAlan Somers uint64_t size, int times, uint64_t attr_valid, uid_t uid, gid_t gid) 2929821f1d3SAlan Somers { 293a34cdd26SAlan Somers EXPECT_LOOKUP(FUSE_ROOT_ID, relpath) 2949821f1d3SAlan Somers .Times(times) 29529edc611SAlan Somers .WillRepeatedly(Invoke( 29629edc611SAlan Somers ReturnImmediate([=](auto in __unused, auto& out) { 2979821f1d3SAlan Somers SET_OUT_HEADER_LEN(out, entry); 29829edc611SAlan Somers out.body.entry.attr.mode = mode; 29929edc611SAlan Somers out.body.entry.nodeid = ino; 30029edc611SAlan Somers out.body.entry.attr.nlink = 1; 30129edc611SAlan Somers out.body.entry.attr_valid = attr_valid; 30229edc611SAlan Somers out.body.entry.attr.size = size; 30329edc611SAlan Somers out.body.entry.attr.uid = uid; 30429edc611SAlan Somers out.body.entry.attr.gid = gid; 3059821f1d3SAlan Somers }))); 3069821f1d3SAlan Somers } 3079821f1d3SAlan Somers 30816bd2d47SAlan Somers void FuseTest::expect_lookup_7_8(const char *relpath, uint64_t ino, mode_t mode, 30916bd2d47SAlan Somers uint64_t size, int times, uint64_t attr_valid, uid_t uid, gid_t gid) 31016bd2d47SAlan Somers { 311a34cdd26SAlan Somers EXPECT_LOOKUP(FUSE_ROOT_ID, relpath) 31216bd2d47SAlan Somers .Times(times) 31329edc611SAlan Somers .WillRepeatedly(Invoke( 31429edc611SAlan Somers ReturnImmediate([=](auto in __unused, auto& out) { 31516bd2d47SAlan Somers SET_OUT_HEADER_LEN(out, entry_7_8); 31629edc611SAlan Somers out.body.entry.attr.mode = mode; 31729edc611SAlan Somers out.body.entry.nodeid = ino; 31829edc611SAlan Somers out.body.entry.attr.nlink = 1; 31929edc611SAlan Somers out.body.entry.attr_valid = attr_valid; 32029edc611SAlan Somers out.body.entry.attr.size = size; 32129edc611SAlan Somers out.body.entry.attr.uid = uid; 32229edc611SAlan Somers out.body.entry.attr.gid = gid; 32316bd2d47SAlan Somers }))); 32416bd2d47SAlan Somers } 32516bd2d47SAlan Somers 3269821f1d3SAlan Somers void FuseTest::expect_open(uint64_t ino, uint32_t flags, int times) 3279821f1d3SAlan Somers { 3289821f1d3SAlan Somers EXPECT_CALL(*m_mock, process( 3299821f1d3SAlan Somers ResultOf([=](auto in) { 33029edc611SAlan Somers return (in.header.opcode == FUSE_OPEN && 33129edc611SAlan Somers in.header.nodeid == ino); 3329821f1d3SAlan Somers }, Eq(true)), 3339821f1d3SAlan Somers _) 3349821f1d3SAlan Somers ).Times(times) 33529edc611SAlan Somers .WillRepeatedly(Invoke( 33629edc611SAlan Somers ReturnImmediate([=](auto in __unused, auto& out) { 33729edc611SAlan Somers out.header.len = sizeof(out.header); 3389821f1d3SAlan Somers SET_OUT_HEADER_LEN(out, open); 33929edc611SAlan Somers out.body.open.fh = FH; 34029edc611SAlan Somers out.body.open.open_flags = flags; 3419821f1d3SAlan Somers }))); 3429821f1d3SAlan Somers } 3439821f1d3SAlan Somers 3449821f1d3SAlan Somers void FuseTest::expect_opendir(uint64_t ino) 3459821f1d3SAlan Somers { 346363a7416SAlan Somers /* opendir(3) calls fstatfs */ 3479821f1d3SAlan Somers EXPECT_CALL(*m_mock, process( 3489821f1d3SAlan Somers ResultOf([](auto in) { 34929edc611SAlan Somers return (in.header.opcode == FUSE_STATFS); 3509821f1d3SAlan Somers }, Eq(true)), 3519821f1d3SAlan Somers _) 35229edc611SAlan Somers ).WillRepeatedly(Invoke( 35329edc611SAlan Somers ReturnImmediate([=](auto i __unused, auto& out) { 3549821f1d3SAlan Somers SET_OUT_HEADER_LEN(out, statfs); 3559821f1d3SAlan Somers }))); 3569821f1d3SAlan Somers 3579821f1d3SAlan Somers EXPECT_CALL(*m_mock, process( 3589821f1d3SAlan Somers ResultOf([=](auto in) { 35929edc611SAlan Somers return (in.header.opcode == FUSE_OPENDIR && 36029edc611SAlan Somers in.header.nodeid == ino); 3619821f1d3SAlan Somers }, Eq(true)), 3629821f1d3SAlan Somers _) 36329edc611SAlan Somers ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) { 36429edc611SAlan Somers out.header.len = sizeof(out.header); 3659821f1d3SAlan Somers SET_OUT_HEADER_LEN(out, open); 36629edc611SAlan Somers out.body.open.fh = FH; 3679821f1d3SAlan Somers }))); 3689821f1d3SAlan Somers } 3699821f1d3SAlan Somers 3709821f1d3SAlan Somers void FuseTest::expect_read(uint64_t ino, uint64_t offset, uint64_t isize, 371d4fd0c81SAlan Somers uint64_t osize, const void *contents, int flags) 3729821f1d3SAlan Somers { 3739821f1d3SAlan Somers EXPECT_CALL(*m_mock, process( 3749821f1d3SAlan Somers ResultOf([=](auto in) { 37529edc611SAlan Somers return (in.header.opcode == FUSE_READ && 37629edc611SAlan Somers in.header.nodeid == ino && 37729edc611SAlan Somers in.body.read.fh == FH && 37829edc611SAlan Somers in.body.read.offset == offset && 379d4fd0c81SAlan Somers in.body.read.size == isize && 380d4fd0c81SAlan Somers flags == -1 ? 381d4fd0c81SAlan Somers (in.body.read.flags == O_RDONLY || 382d4fd0c81SAlan Somers in.body.read.flags == O_RDWR) 383d4fd0c81SAlan Somers : in.body.read.flags == (uint32_t)flags); 3849821f1d3SAlan Somers }, Eq(true)), 3859821f1d3SAlan Somers _) 38629edc611SAlan Somers ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) { 38729edc611SAlan Somers out.header.len = sizeof(struct fuse_out_header) + osize; 38829edc611SAlan Somers memmove(out.body.bytes, contents, osize); 3899821f1d3SAlan Somers }))).RetiresOnSaturation(); 3909821f1d3SAlan Somers } 3919821f1d3SAlan Somers 392e5b50fe7SAlan Somers void FuseTest::expect_readdir(uint64_t ino, uint64_t off, 393e5b50fe7SAlan Somers std::vector<struct dirent> &ents) 394e5b50fe7SAlan Somers { 395e5b50fe7SAlan Somers EXPECT_CALL(*m_mock, process( 396e5b50fe7SAlan Somers ResultOf([=](auto in) { 39729edc611SAlan Somers return (in.header.opcode == FUSE_READDIR && 39829edc611SAlan Somers in.header.nodeid == ino && 39929edc611SAlan Somers in.body.readdir.fh == FH && 40029edc611SAlan Somers in.body.readdir.offset == off); 401e5b50fe7SAlan Somers }, Eq(true)), 402e5b50fe7SAlan Somers _) 40329edc611SAlan Somers ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) { 40429edc611SAlan Somers struct fuse_dirent *fde = (struct fuse_dirent*)&(out.body); 405e5b50fe7SAlan Somers int i = 0; 406e5b50fe7SAlan Somers 40729edc611SAlan Somers out.header.error = 0; 40829edc611SAlan Somers out.header.len = 0; 409e5b50fe7SAlan Somers 410e5b50fe7SAlan Somers for (const auto& it: ents) { 411e5b50fe7SAlan Somers size_t entlen, entsize; 412e5b50fe7SAlan Somers 413e5b50fe7SAlan Somers fde->ino = it.d_fileno; 414e5b50fe7SAlan Somers fde->off = it.d_off; 415e5b50fe7SAlan Somers fde->type = it.d_type; 416e5b50fe7SAlan Somers fde->namelen = it.d_namlen; 417e5b50fe7SAlan Somers strncpy(fde->name, it.d_name, it.d_namlen); 418e5b50fe7SAlan Somers entlen = FUSE_NAME_OFFSET + fde->namelen; 419e5b50fe7SAlan Somers entsize = FUSE_DIRENT_SIZE(fde); 420e5b50fe7SAlan Somers /* 421e5b50fe7SAlan Somers * The FUSE protocol does not require zeroing out the 422e5b50fe7SAlan Somers * unused portion of the name. But it's a good 423e5b50fe7SAlan Somers * practice to prevent information disclosure to the 424e5b50fe7SAlan Somers * FUSE client, even though the client is usually the 425e5b50fe7SAlan Somers * kernel 426e5b50fe7SAlan Somers */ 427e5b50fe7SAlan Somers memset(fde->name + fde->namelen, 0, entsize - entlen); 42829edc611SAlan Somers if (out.header.len + entsize > in.body.read.size) { 429e5b50fe7SAlan Somers printf("Overflow in readdir expectation: i=%d\n" 430e5b50fe7SAlan Somers , i); 431e5b50fe7SAlan Somers break; 432e5b50fe7SAlan Somers } 43329edc611SAlan Somers out.header.len += entsize; 434e5b50fe7SAlan Somers fde = (struct fuse_dirent*) 435b7774b82SAlan Somers ((intmax_t*)fde + entsize / sizeof(intmax_t)); 436e5b50fe7SAlan Somers i++; 437e5b50fe7SAlan Somers } 43829edc611SAlan Somers out.header.len += sizeof(out.header); 439e5b50fe7SAlan Somers }))); 440e5b50fe7SAlan Somers 441e5b50fe7SAlan Somers } 44242d50d16SAlan Somers void FuseTest::expect_release(uint64_t ino, uint64_t fh) 4439821f1d3SAlan Somers { 4449821f1d3SAlan Somers EXPECT_CALL(*m_mock, process( 4459821f1d3SAlan Somers ResultOf([=](auto in) { 44629edc611SAlan Somers return (in.header.opcode == FUSE_RELEASE && 44729edc611SAlan Somers in.header.nodeid == ino && 44829edc611SAlan Somers in.body.release.fh == fh); 4499821f1d3SAlan Somers }, Eq(true)), 4509821f1d3SAlan Somers _) 451e0bec057SAlan Somers ).WillOnce(Invoke(ReturnErrno(0))); 4529821f1d3SAlan Somers } 453e0bec057SAlan Somers 45435cf0e7eSAlan Somers void FuseTest::expect_releasedir(uint64_t ino, ProcessMockerT r) 45535cf0e7eSAlan Somers { 45635cf0e7eSAlan Somers EXPECT_CALL(*m_mock, process( 45735cf0e7eSAlan Somers ResultOf([=](auto in) { 45829edc611SAlan Somers return (in.header.opcode == FUSE_RELEASEDIR && 45929edc611SAlan Somers in.header.nodeid == ino && 46029edc611SAlan Somers in.body.release.fh == FH); 46135cf0e7eSAlan Somers }, Eq(true)), 46235cf0e7eSAlan Somers _) 46335cf0e7eSAlan Somers ).WillOnce(Invoke(r)); 46435cf0e7eSAlan Somers } 46535cf0e7eSAlan Somers 466ff4fbdf5SAlan Somers void FuseTest::expect_unlink(uint64_t parent, const char *path, int error) 467ff4fbdf5SAlan Somers { 468ff4fbdf5SAlan Somers EXPECT_CALL(*m_mock, process( 469ff4fbdf5SAlan Somers ResultOf([=](auto in) { 47029edc611SAlan Somers return (in.header.opcode == FUSE_UNLINK && 47129edc611SAlan Somers 0 == strcmp(path, in.body.unlink) && 47229edc611SAlan Somers in.header.nodeid == parent); 473ff4fbdf5SAlan Somers }, Eq(true)), 474ff4fbdf5SAlan Somers _) 475ff4fbdf5SAlan Somers ).WillOnce(Invoke(ReturnErrno(error))); 476ff4fbdf5SAlan Somers } 477ff4fbdf5SAlan Somers 4789821f1d3SAlan Somers void FuseTest::expect_write(uint64_t ino, uint64_t offset, uint64_t isize, 479bda39894SAlan Somers uint64_t osize, uint32_t flags_set, uint32_t flags_unset, 480bda39894SAlan Somers const void *contents) 4819821f1d3SAlan Somers { 4829821f1d3SAlan Somers EXPECT_CALL(*m_mock, process( 4839821f1d3SAlan Somers ResultOf([=](auto in) { 48429edc611SAlan Somers const char *buf = (const char*)in.body.bytes + 4859821f1d3SAlan Somers sizeof(struct fuse_write_in); 4869821f1d3SAlan Somers bool pid_ok; 487bda39894SAlan Somers uint32_t wf = in.body.write.write_flags; 4889821f1d3SAlan Somers 489bda39894SAlan Somers if (wf & FUSE_WRITE_CACHE) 4909821f1d3SAlan Somers pid_ok = true; 4919821f1d3SAlan Somers else 49229edc611SAlan Somers pid_ok = (pid_t)in.header.pid == getpid(); 4939821f1d3SAlan Somers 49429edc611SAlan Somers return (in.header.opcode == FUSE_WRITE && 49529edc611SAlan Somers in.header.nodeid == ino && 49629edc611SAlan Somers in.body.write.fh == FH && 49729edc611SAlan Somers in.body.write.offset == offset && 49829edc611SAlan Somers in.body.write.size == isize && 4999821f1d3SAlan Somers pid_ok && 500bda39894SAlan Somers (wf & flags_set) == flags_set && 501bda39894SAlan Somers (wf & flags_unset) == 0 && 502d4fd0c81SAlan Somers (in.body.write.flags == O_WRONLY || 503d4fd0c81SAlan Somers in.body.write.flags == O_RDWR) && 5049821f1d3SAlan Somers 0 == bcmp(buf, contents, isize)); 5059821f1d3SAlan Somers }, Eq(true)), 5069821f1d3SAlan Somers _) 50729edc611SAlan Somers ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) { 5089821f1d3SAlan Somers SET_OUT_HEADER_LEN(out, write); 50929edc611SAlan Somers out.body.write.size = osize; 5109821f1d3SAlan Somers }))); 5119821f1d3SAlan Somers } 5129821f1d3SAlan Somers 51316bd2d47SAlan Somers void FuseTest::expect_write_7_8(uint64_t ino, uint64_t offset, uint64_t isize, 514bda39894SAlan Somers uint64_t osize, const void *contents) 51516bd2d47SAlan Somers { 51616bd2d47SAlan Somers EXPECT_CALL(*m_mock, process( 51716bd2d47SAlan Somers ResultOf([=](auto in) { 51829edc611SAlan Somers const char *buf = (const char*)in.body.bytes + 51916bd2d47SAlan Somers FUSE_COMPAT_WRITE_IN_SIZE; 52029edc611SAlan Somers bool pid_ok = (pid_t)in.header.pid == getpid(); 52129edc611SAlan Somers return (in.header.opcode == FUSE_WRITE && 52229edc611SAlan Somers in.header.nodeid == ino && 52329edc611SAlan Somers in.body.write.fh == FH && 52429edc611SAlan Somers in.body.write.offset == offset && 52529edc611SAlan Somers in.body.write.size == isize && 52616bd2d47SAlan Somers pid_ok && 52716bd2d47SAlan Somers 0 == bcmp(buf, contents, isize)); 52816bd2d47SAlan Somers }, Eq(true)), 52916bd2d47SAlan Somers _) 53029edc611SAlan Somers ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) { 53116bd2d47SAlan Somers SET_OUT_HEADER_LEN(out, write); 53229edc611SAlan Somers out.body.write.size = osize; 53316bd2d47SAlan Somers }))); 53416bd2d47SAlan Somers } 53516bd2d47SAlan Somers 53661b0a927SAlan Somers void 53761b0a927SAlan Somers get_unprivileged_id(uid_t *uid, gid_t *gid) 53809c01e67SAlan Somers { 53909c01e67SAlan Somers struct passwd *pw; 54061b0a927SAlan Somers struct group *gr; 54109c01e67SAlan Somers 54209c01e67SAlan Somers /* 54309c01e67SAlan Somers * First try "tests", Kyua's default unprivileged user. XXX after 54409c01e67SAlan Somers * GoogleTest gains a proper Kyua wrapper, get this with the Kyua API 54509c01e67SAlan Somers */ 54609c01e67SAlan Somers pw = getpwnam("tests"); 54709c01e67SAlan Somers if (pw == NULL) { 54809c01e67SAlan Somers /* Fall back to "nobody" */ 54909c01e67SAlan Somers pw = getpwnam("nobody"); 55009c01e67SAlan Somers } 55109c01e67SAlan Somers if (pw == NULL) 55209c01e67SAlan Somers GTEST_SKIP() << "Test requires an unprivileged user"; 55361b0a927SAlan Somers /* Use group "nobody", which is Kyua's default unprivileged group */ 55461b0a927SAlan Somers gr = getgrnam("nobody"); 55561b0a927SAlan Somers if (gr == NULL) 55661b0a927SAlan Somers GTEST_SKIP() << "Test requires an unprivileged group"; 55709c01e67SAlan Somers *uid = pw->pw_uid; 55861b0a927SAlan Somers *gid = gr->gr_gid; 55909c01e67SAlan Somers } 56009c01e67SAlan Somers 56109c01e67SAlan Somers void 562a1542146SAlan Somers FuseTest::fork(bool drop_privs, int *child_status, 563a1542146SAlan Somers std::function<void()> parent_func, 56409c01e67SAlan Somers std::function<int()> child_func) 56509c01e67SAlan Somers { 56609c01e67SAlan Somers sem_t *sem; 56709c01e67SAlan Somers int mprot = PROT_READ | PROT_WRITE; 56809c01e67SAlan Somers int mflags = MAP_ANON | MAP_SHARED; 56909c01e67SAlan Somers pid_t child; 57009c01e67SAlan Somers uid_t uid; 57161b0a927SAlan Somers gid_t gid; 57209c01e67SAlan Somers 57309c01e67SAlan Somers if (drop_privs) { 57461b0a927SAlan Somers get_unprivileged_id(&uid, &gid); 57509c01e67SAlan Somers if (IsSkipped()) 57609c01e67SAlan Somers return; 57709c01e67SAlan Somers } 57809c01e67SAlan Somers 57909c01e67SAlan Somers sem = (sem_t*)mmap(NULL, sizeof(*sem), mprot, mflags, -1, 0); 58009c01e67SAlan Somers ASSERT_NE(MAP_FAILED, sem) << strerror(errno); 58109c01e67SAlan Somers ASSERT_EQ(0, sem_init(sem, 1, 0)) << strerror(errno); 58209c01e67SAlan Somers 58309c01e67SAlan Somers if ((child = ::fork()) == 0) { 58409c01e67SAlan Somers /* In child */ 58509c01e67SAlan Somers int err = 0; 58609c01e67SAlan Somers 58709c01e67SAlan Somers if (sem_wait(sem)) { 58809c01e67SAlan Somers perror("sem_wait"); 58909c01e67SAlan Somers err = 1; 59009c01e67SAlan Somers goto out; 59109c01e67SAlan Somers } 59209c01e67SAlan Somers 59361b0a927SAlan Somers if (drop_privs && 0 != setegid(gid)) { 59461b0a927SAlan Somers perror("setegid"); 59561b0a927SAlan Somers err = 1; 59661b0a927SAlan Somers goto out; 59761b0a927SAlan Somers } 59809c01e67SAlan Somers if (drop_privs && 0 != setreuid(-1, uid)) { 59909c01e67SAlan Somers perror("setreuid"); 60009c01e67SAlan Somers err = 1; 60109c01e67SAlan Somers goto out; 60209c01e67SAlan Somers } 60309c01e67SAlan Somers err = child_func(); 60409c01e67SAlan Somers 60509c01e67SAlan Somers out: 60609c01e67SAlan Somers sem_destroy(sem); 60709c01e67SAlan Somers _exit(err); 60809c01e67SAlan Somers } else if (child > 0) { 60909c01e67SAlan Somers /* 61009c01e67SAlan Somers * In parent. Cleanup must happen here, because it's still 61109c01e67SAlan Somers * privileged. 61209c01e67SAlan Somers */ 61309c01e67SAlan Somers m_mock->m_child_pid = child; 61409c01e67SAlan Somers ASSERT_NO_FATAL_FAILURE(parent_func()); 61509c01e67SAlan Somers 61609c01e67SAlan Somers /* Signal the child process to go */ 61709c01e67SAlan Somers ASSERT_EQ(0, sem_post(sem)) << strerror(errno); 61809c01e67SAlan Somers 619a1542146SAlan Somers ASSERT_LE(0, wait(child_status)) << strerror(errno); 62009c01e67SAlan Somers } else { 62109c01e67SAlan Somers FAIL() << strerror(errno); 62209c01e67SAlan Somers } 62309c01e67SAlan Somers munmap(sem, sizeof(*sem)); 624a1542146SAlan Somers return; 62509c01e67SAlan Somers } 62609c01e67SAlan Somers 6279821f1d3SAlan Somers static void usage(char* progname) { 6289821f1d3SAlan Somers fprintf(stderr, "Usage: %s [-v]\n\t-v increase verbosity\n", progname); 6299821f1d3SAlan Somers exit(2); 6309821f1d3SAlan Somers } 6319821f1d3SAlan Somers 6329821f1d3SAlan Somers int main(int argc, char **argv) { 6339821f1d3SAlan Somers int ch; 6349821f1d3SAlan Somers FuseEnv *fuse_env = new FuseEnv; 6359821f1d3SAlan Somers 6369821f1d3SAlan Somers InitGoogleTest(&argc, argv); 6379821f1d3SAlan Somers AddGlobalTestEnvironment(fuse_env); 6389821f1d3SAlan Somers 6399821f1d3SAlan Somers while ((ch = getopt(argc, argv, "v")) != -1) { 6409821f1d3SAlan Somers switch (ch) { 6419821f1d3SAlan Somers case 'v': 6429821f1d3SAlan Somers verbosity++; 6439821f1d3SAlan Somers break; 6449821f1d3SAlan Somers default: 6459821f1d3SAlan Somers usage(argv[0]); 6469821f1d3SAlan Somers break; 6479821f1d3SAlan Somers } 6489821f1d3SAlan Somers } 6499821f1d3SAlan Somers 6509821f1d3SAlan Somers return (RUN_ALL_TESTS()); 6519821f1d3SAlan Somers } 652