fuse(4): add tests for CREATE, OPEN, READLINK, SETATTR and SYMLINK

The new SETATTR tests deal with already-open files.

PR:		235775
PR:		236231
Sponsored by:	The FreeBSD Foundation
This commit is contained in:
Alan Somers 2019-03-04 22:07:33 +00:00
parent 2343311052
commit 99fe8368c2
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/projects/fuse2/; revision=344785
7 changed files with 975 additions and 6 deletions

View File

@ -4,25 +4,49 @@ PACKAGE= tests
TESTSDIR= ${TESTSBASE}/sys/fs/fuse
ATF_TESTS_CXX+= create
ATF_TESTS_CXX+= getattr
ATF_TESTS_CXX+= lookup
ATF_TESTS_CXX+= open
ATF_TESTS_CXX+= readlink
ATF_TESTS_CXX+= setattr
ATF_TESTS_CXX+= symlink
SRCS.create+= create.cc
SRCS.create+= getmntopts.c
SRCS.create+= mockfs.cc
SRCS.create+= utils.cc
SRCS.getattr+= getattr.cc
SRCS.getattr+= getmntopts.c
SRCS.getattr+= mockfs.cc
SRCS.getattr+= utils.cc
SRCS.lookup+= lookup.cc
SRCS.lookup+= getmntopts.c
SRCS.lookup+= lookup.cc
SRCS.lookup+= mockfs.cc
SRCS.lookup+= utils.cc
SRCS.setattr+= setattr.cc
SRCS.open+= getmntopts.c
SRCS.open+= mockfs.cc
SRCS.open+= open.cc
SRCS.open+= utils.cc
SRCS.readlink+= getmntopts.c
SRCS.readlink+= mockfs.cc
SRCS.readlink+= readlink.cc
SRCS.readlink+= utils.cc
SRCS.setattr+= getmntopts.c
SRCS.setattr+= mockfs.cc
SRCS.setattr+= setattr.cc
SRCS.setattr+= utils.cc
SRCS.symlink+= getmntopts.c
SRCS.symlink+= mockfs.cc
SRCS.symlink+= symlink.cc
SRCS.symlink+= utils.cc
# TODO: drastically increase timeout after test development is mostly complete
TEST_METADATA+= timeout=10

383
tests/sys/fs/fuse/create.cc Normal file
View File

@ -0,0 +1,383 @@
/*-
* Copyright (c) 2019 The FreeBSD Foundation
* All rights reserved.
*
* 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 <fcntl.h>
}
#include "mockfs.hh"
#include "utils.hh"
using namespace testing;
class Create: public FuseTest {};
/*
* If FUSE_CREATE sets the attr_valid, then subsequent GETATTRs should use the
* attribute cache
*/
/* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=235775 */
TEST_F(Create, DISABLED_attr_cache)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
mode_t mode = 0755;
uint64_t ino = 42;
int fd;
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_LOOKUP &&
strcmp(in->body.lookup, RELPATH) == 0);
}, Eq(true)),
_)
).WillOnce(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
out->header.error = -ENOENT;
out->header.len = sizeof(out->header);
}));
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
const char *name = (const char*)in->body.bytes +
sizeof(fuse_open_in);
return (in->header.opcode == FUSE_CREATE &&
(0 == strcmp(RELPATH, name)));
}, Eq(true)),
_)
).WillOnce(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
SET_OUT_HEADER_LEN(out, create);
out->body.create.entry.attr.mode = S_IFREG | mode;
out->body.create.entry.nodeid = ino;
out->body.create.entry.entry_valid = UINT64_MAX;
out->body.create.entry.attr_valid = UINT64_MAX;
}));
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_GETATTR &&
in->header.nodeid == ino);
}, Eq(true)),
_)
).Times(0);
fd = open(FULLPATH, O_CREAT | O_EXCL, mode);
EXPECT_LE(0, fd) << strerror(errno);
/* Deliberately leak fd. close(2) will be tested in release.cc */
}
/*
* The fuse daemon fails the request with EEXIST. This usually indicates a
* race condition: some other FUSE client created the file in between when the
* kernel checked for it with lookup and tried to create it with create
*/
TEST_F(Create, eexist)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
mode_t mode = 0755;
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_LOOKUP &&
strcmp(in->body.lookup, RELPATH) == 0);
}, Eq(true)),
_)
).WillOnce(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
out->header.error = -ENOENT;
out->header.len = sizeof(out->header);
}));
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
const char *name = (const char*)in->body.bytes +
sizeof(fuse_open_in);
return (in->header.opcode == FUSE_CREATE &&
(0 == strcmp(RELPATH, name)));
}, Eq(true)),
_)
).WillOnce(Invoke([](auto in, auto out) {
out->header.unique = in->header.unique;
out->header.error = -EEXIST;
out->header.len = sizeof(out->header);
}));
EXPECT_NE(0, open(FULLPATH, O_CREAT | O_EXCL, mode));
EXPECT_EQ(EEXIST, errno);
}
// TODO: enosys: kernel should fall back to mknod/open
/*
* Creating a new file after FUSE_LOOKUP returned a negative cache entry
*/
/* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236231 */
TEST_F(Create, DISABLED_entry_cache_negative)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
mode_t mode = 0755;
uint64_t ino = 42;
int fd;
/* create will first do a LOOKUP, adding a negative cache entry */
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_LOOKUP &&
strcmp(in->body.lookup, RELPATH) == 0);
}, Eq(true)),
_)
).WillOnce(Invoke([=](auto in, auto out) {
/* nodeid means ENOENT and cache it */
out->body.entry.nodeid = 0;
out->header.unique = in->header.unique;
out->header.error = 0;
/*
* Set entry_valid = 0 because this test isn't concerned with
* whether or not we actually cache negative entries, only with
* whether we interpret negative cache responses correctly.
*/
out->body.entry.entry_valid = 0;
SET_OUT_HEADER_LEN(out, entry);
}));
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
const char *name = (const char*)in->body.bytes +
sizeof(fuse_open_in);
return (in->header.opcode == FUSE_CREATE &&
(0 == strcmp(RELPATH, name)));
}, Eq(true)),
_)
).WillOnce(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
SET_OUT_HEADER_LEN(out, create);
out->body.create.entry.attr.mode = S_IFREG | mode;
out->body.create.entry.nodeid = ino;
out->body.create.entry.entry_valid = UINT64_MAX;
out->body.create.entry.attr_valid = UINT64_MAX;
}));
/* Until the attr cache is working, we may send an additional GETATTR */
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_GETATTR &&
in->header.nodeid == ino);
}, Eq(true)),
_)
).WillRepeatedly(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
SET_OUT_HEADER_LEN(out, attr);
out->body.attr.attr.ino = ino; // Must match nodeid
out->body.attr.attr.mode = S_IFREG | 0644;
}));
fd = open(FULLPATH, O_CREAT | O_EXCL, mode);
ASSERT_LE(0, fd) << strerror(errno);
/* Deliberately leak fd. close(2) will be tested in release.cc */
}
/*
* Creating a new file should purge any negative namecache entries
*/
/* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236231 */
TEST_F(Create, DISABLED_entry_cache_negative_purge)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
mode_t mode = 0755;
uint64_t ino = 42;
int fd;
/* create will first do a LOOKUP, adding a negative cache entry */
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_LOOKUP &&
strcmp(in->body.lookup, RELPATH) == 0);
}, Eq(true)),
_)
).Times(1)
.WillOnce(Invoke([=](auto in, auto out) {
/* nodeid means ENOENT and cache it */
out->body.entry.nodeid = 0;
out->header.unique = in->header.unique;
out->header.error = 0;
out->body.entry.entry_valid = UINT64_MAX;
SET_OUT_HEADER_LEN(out, entry);
})).RetiresOnSaturation();
/* Then the CREATE should purge the negative cache entry */
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
const char *name = (const char*)in->body.bytes +
sizeof(fuse_open_in);
return (in->header.opcode == FUSE_CREATE &&
(0 == strcmp(RELPATH, name)));
}, Eq(true)),
_)
).WillOnce(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
SET_OUT_HEADER_LEN(out, create);
out->body.create.entry.attr.mode = S_IFREG | mode;
out->body.create.entry.nodeid = ino;
out->body.create.entry.attr_valid = UINT64_MAX;
}));
/* Until the attr cache is working, we may send an additional GETATTR */
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_GETATTR &&
in->header.nodeid == ino);
}, Eq(true)),
_)
).WillRepeatedly(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
SET_OUT_HEADER_LEN(out, attr);
out->body.attr.attr.ino = ino; // Must match nodeid
out->body.attr.attr.mode = S_IFREG | 0644;
}));
fd = open(FULLPATH, O_CREAT | O_EXCL, mode);
ASSERT_LE(0, fd) << strerror(errno);
/* Finally, a subsequent lookup should query the daemon */
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_LOOKUP &&
strcmp(in->body.lookup, RELPATH) == 0);
}, Eq(true)),
_)
).Times(1)
.WillOnce(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
out->header.error = 0;
out->body.entry.nodeid = ino;
out->body.entry.attr.mode = S_IFREG | mode;
SET_OUT_HEADER_LEN(out, entry);
}));
ASSERT_EQ(0, access(FULLPATH, F_OK)) << strerror(errno);
/* Deliberately leak fd. close(2) will be tested in release.cc */
}
/*
* The daemon is responsible for checking file permissions (unless the
* default_permissions mount option was used)
*/
TEST_F(Create, eperm)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
mode_t mode = 0755;
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_LOOKUP &&
strcmp(in->body.lookup, RELPATH) == 0);
}, Eq(true)),
_)
).WillOnce(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
out->header.error = -ENOENT;
out->header.len = sizeof(out->header);
}));
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
const char *name = (const char*)in->body.bytes +
sizeof(fuse_open_in);
return (in->header.opcode == FUSE_CREATE &&
(0 == strcmp(RELPATH, name)));
}, Eq(true)),
_)
).WillOnce(Invoke([](auto in, auto out) {
out->header.unique = in->header.unique;
out->header.error = -EPERM;
out->header.len = sizeof(out->header);
}));
EXPECT_NE(0, open(FULLPATH, O_CREAT | O_EXCL, mode));
EXPECT_EQ(EPERM, errno);
}
TEST_F(Create, ok)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
mode_t mode = 0755;
uint64_t ino = 42;
int fd;
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_LOOKUP &&
strcmp(in->body.lookup, RELPATH) == 0);
}, Eq(true)),
_)
).WillOnce(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
out->header.error = -ENOENT;
out->header.len = sizeof(out->header);
}));
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
const char *name = (const char*)in->body.bytes +
sizeof(fuse_open_in);
return (in->header.opcode == FUSE_CREATE &&
(0 == strcmp(RELPATH, name)));
}, Eq(true)),
_)
).WillOnce(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
SET_OUT_HEADER_LEN(out, create);
out->body.create.entry.attr.mode = S_IFREG | mode;
out->body.create.entry.nodeid = ino;
out->body.create.entry.entry_valid = UINT64_MAX;
out->body.create.entry.attr_valid = UINT64_MAX;
}));
/* Until the attr cache is working, we may send an additional GETATTR */
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_GETATTR &&
in->header.nodeid == ino);
}, Eq(true)),
_)
).WillRepeatedly(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
SET_OUT_HEADER_LEN(out, attr);
out->body.attr.attr.ino = ino; // Must match nodeid
out->body.attr.attr.mode = S_IFREG | 0644;
}));
fd = open(FULLPATH, O_CREAT | O_EXCL, mode);
EXPECT_LE(0, fd) << strerror(errno);
/* Deliberately leak fd. close(2) will be tested in release.cc */
}

View File

@ -44,12 +44,19 @@ extern "C" {
extern int verbosity;
/* This struct isn't defined by fuse_kernel.h or libfuse, but it should be */
struct fuse_create_out {
struct fuse_entry_out entry;
struct fuse_open_out open;
};
union fuse_payloads_in {
/* value is from fuse_kern_chan.c in fusefs-libs */
uint8_t bytes[0x21000 - sizeof(struct fuse_in_header)];
fuse_forget_in forget;
fuse_init_in init;
char lookup[0];
fuse_open_in open;
fuse_setattr_in setattr;
};
@ -59,9 +66,16 @@ struct mockfs_buf_in {
};
union fuse_payloads_out {
fuse_init_out init;
fuse_entry_out entry;
fuse_attr_out attr;
fuse_create_out create;
fuse_entry_out entry;
fuse_init_out init;
fuse_open_out open;
/*
* The protocol places no limits on the length of the string. This is
* merely convenient for testing.
*/
char str[80];
};
struct mockfs_buf_out {

168
tests/sys/fs/fuse/open.cc Normal file
View File

@ -0,0 +1,168 @@
/*-
* Copyright (c) 2019 The FreeBSD Foundation
* All rights reserved.
*
* 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 <fcntl.h>
}
#include "mockfs.hh"
#include "utils.hh"
using namespace testing;
class Open: public FuseTest {};
/*
* 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;
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_LOOKUP &&
strcmp(in->body.lookup, RELPATH) == 0);
}, Eq(true)),
_)
).WillOnce(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
SET_OUT_HEADER_LEN(out, entry);
out->body.entry.attr.mode = S_IFREG | 0644;
out->body.entry.nodeid = ino;
}));
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_OPEN &&
in->header.nodeid == ino);
}, Eq(true)),
_)
).WillOnce(Invoke([](auto in, auto out) {
out->header.unique = in->header.unique;
out->header.error = -ENOENT;
out->header.len = sizeof(out->header);
}));
EXPECT_NE(0, open(FULLPATH, O_RDONLY));
EXPECT_EQ(ENOENT, errno);
}
/*
* 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_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_LOOKUP &&
strcmp(in->body.lookup, RELPATH) == 0);
}, Eq(true)),
_)
).WillOnce(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
SET_OUT_HEADER_LEN(out, entry);
out->body.entry.attr.mode = S_IFREG | 0644;
out->body.entry.nodeid = ino;
}));
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_OPEN &&
in->header.nodeid == ino);
}, Eq(true)),
_)
).WillOnce(Invoke([](auto in, auto out) {
out->header.unique = in->header.unique;
out->header.error = -EPERM;
out->header.len = sizeof(out->header);
}));
EXPECT_NE(0, open(FULLPATH, O_RDONLY));
EXPECT_EQ(EPERM, errno);
}
TEST_F(Open, ok)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
uint64_t ino = 42;
int fd;
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_LOOKUP &&
strcmp(in->body.lookup, RELPATH) == 0);
}, Eq(true)),
_)
).WillOnce(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
SET_OUT_HEADER_LEN(out, entry);
out->body.entry.attr.mode = S_IFREG | 0644;
out->body.entry.nodeid = ino;
out->body.entry.attr_valid = UINT64_MAX;
}));
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_OPEN &&
in->header.nodeid == ino);
}, Eq(true)),
_)
).WillOnce(Invoke([](auto in, auto out) {
out->header.unique = in->header.unique;
out->header.len = sizeof(out->header);
SET_OUT_HEADER_LEN(out, open);
}));
/* Until the attr cache is working, we may send an additional GETATTR */
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_GETATTR &&
in->header.nodeid == ino);
}, Eq(true)),
_)
).WillRepeatedly(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
SET_OUT_HEADER_LEN(out, attr);
out->body.attr.attr.ino = ino; // Must match nodeid
out->body.attr.attr.mode = S_IFREG | 0644;
}));
fd = open(FULLPATH, O_RDONLY);
EXPECT_LE(0, fd) << strerror(errno);
/* Deliberately leak fd. close(2) will be tested in release.cc */
}

View File

@ -0,0 +1,115 @@
/*-
* Copyright (c) 2019 The FreeBSD Foundation
* All rights reserved.
*
* 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 <unistd.h>
}
#include "mockfs.hh"
#include "utils.hh"
using namespace testing;
class Readlink: public FuseTest {};
TEST_F(Readlink, eloop)
{
const char FULLPATH[] = "mountpoint/src";
const char RELPATH[] = "src";
const uint64_t ino = 42;
char buf[80];
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_LOOKUP &&
strcmp(in->body.lookup, RELPATH) == 0);
}, Eq(true)),
_)
).WillOnce(Invoke([](auto in, auto out) {
out->header.unique = in->header.unique;
SET_OUT_HEADER_LEN(out, entry);
out->body.entry.attr.mode = S_IFLNK | 0777;
out->body.entry.nodeid = ino;
}));
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_READLINK &&
in->header.nodeid == ino);
}, Eq(true)),
_)
).WillOnce(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
out->header.error = -ELOOP;
out->header.len = sizeof(out->header);
}));
EXPECT_EQ(-1, readlink(FULLPATH, buf, sizeof(buf)));
EXPECT_EQ(ELOOP, errno);
}
TEST_F(Readlink, ok)
{
const char FULLPATH[] = "mountpoint/src";
const char RELPATH[] = "src";
const char dst[] = "dst";
const uint64_t ino = 42;
char buf[80];
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_LOOKUP &&
strcmp(in->body.lookup, RELPATH) == 0);
}, Eq(true)),
_)
).WillOnce(Invoke([](auto in, auto out) {
out->header.unique = in->header.unique;
SET_OUT_HEADER_LEN(out, entry);
out->body.entry.attr.mode = S_IFLNK | 0777;
out->body.entry.nodeid = ino;
}));
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_READLINK &&
in->header.nodeid == ino);
}, Eq(true)),
_)
).WillOnce(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
strlcpy(out->body.str, dst, sizeof(out->body.str));
out->header.len = sizeof(out->header) + strlen(dst) + 1;
}));
EXPECT_EQ((ssize_t)strlen(dst) + 1,
readlink(FULLPATH, buf, sizeof(buf)));
EXPECT_STREQ(dst, buf);
}

View File

@ -132,8 +132,6 @@ TEST_F(Setattr, chown)
}
/* Change the mode of an open file, by its file descriptor */
//TODO TEST_F(Setattr, fchmod) {}
/*
* FUSE daemons are allowed to check permissions however they like. If the
@ -176,6 +174,157 @@ TEST_F(Setattr, eperm)
EXPECT_EQ(EPERM, errno);
}
/* Change the mode of an open file, by its file descriptor */
TEST_F(Setattr, fchmod)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
uint64_t ino = 42;
int fd;
const mode_t oldmode = 0755;
const mode_t newmode = 0644;
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_LOOKUP &&
strcmp(in->body.lookup, RELPATH) == 0);
}, Eq(true)),
_)
).WillOnce(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
SET_OUT_HEADER_LEN(out, entry);
out->body.entry.attr.mode = S_IFREG | oldmode;
out->body.entry.nodeid = ino;
out->body.entry.attr_valid = UINT64_MAX;
}));
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_OPEN &&
in->header.nodeid == ino);
}, Eq(true)),
_)
).WillOnce(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
out->header.len = sizeof(out->header);
SET_OUT_HEADER_LEN(out, open);
}));
/* Until the attr cache is working, we may send an additional GETATTR */
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_GETATTR &&
in->header.nodeid == ino);
}, Eq(true)),
_)
).WillRepeatedly(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
SET_OUT_HEADER_LEN(out, attr);
out->body.attr.attr.ino = ino; // Must match nodeid
out->body.attr.attr.mode = S_IFREG | oldmode;
}));
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
/* In protocol 7.23, ctime will be changed too */
uint32_t valid = FATTR_MODE;
return (in->header.opcode == FUSE_SETATTR &&
in->header.nodeid == ino &&
in->body.setattr.valid == valid &&
in->body.setattr.mode == newmode);
}, Eq(true)),
_)
).WillOnce(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
SET_OUT_HEADER_LEN(out, attr);
out->body.attr.attr.ino = ino; // Must match nodeid
out->body.attr.attr.mode = S_IFREG | newmode;
}));
fd = open(FULLPATH, O_RDONLY);
ASSERT_LE(0, fd) << strerror(errno);
ASSERT_EQ(0, fchmod(fd, newmode)) << strerror(errno);
/* Deliberately leak fd. close(2) will be tested in release.cc */
}
/* Change the size of an open file, by its file descriptor */
TEST_F(Setattr, ftruncate)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
uint64_t ino = 42;
int fd;
uint64_t fh = 0xdeadbeef1a7ebabe;
const off_t oldsize = 99;
const off_t newsize = 12345;
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_LOOKUP &&
strcmp(in->body.lookup, RELPATH) == 0);
}, Eq(true)),
_)
).WillOnce(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
SET_OUT_HEADER_LEN(out, entry);
out->body.entry.attr.mode = S_IFREG | 0755;
out->body.entry.nodeid = ino;
out->body.entry.attr_valid = UINT64_MAX;
out->body.entry.attr.size = oldsize;
}));
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_OPEN &&
in->header.nodeid == ino);
}, Eq(true)),
_)
).WillOnce(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
out->header.len = sizeof(out->header);
SET_OUT_HEADER_LEN(out, open);
out->body.open.fh = fh;
}));
/* Until the attr cache is working, we may send an additional GETATTR */
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_GETATTR &&
in->header.nodeid == ino);
}, Eq(true)),
_)
).WillRepeatedly(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
SET_OUT_HEADER_LEN(out, attr);
out->body.attr.attr.ino = ino; // Must match nodeid
out->body.attr.attr.mode = S_IFREG | 0755;
out->body.attr.attr.size = oldsize;
}));
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
/* In protocol 7.23, ctime will be changed too */
uint32_t valid = FATTR_SIZE | FATTR_FH;
return (in->header.opcode == FUSE_SETATTR &&
in->header.nodeid == ino &&
in->body.setattr.valid == valid &&
in->body.setattr.fh == fh);
}, Eq(true)),
_)
).WillOnce(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
SET_OUT_HEADER_LEN(out, attr);
out->body.attr.attr.ino = ino; // Must match nodeid
out->body.attr.attr.mode = S_IFREG | 0755;
out->body.attr.attr.size = newsize;
}));
fd = open(FULLPATH, O_RDWR);
ASSERT_LE(0, fd) << strerror(errno);
ASSERT_EQ(0, ftruncate(fd, newsize)) << strerror(errno);
/* Deliberately leak fd. close(2) will be tested in release.cc */
}
/* Change the size of the file */
TEST_F(Setattr, truncate) {
const char FULLPATH[] = "mountpoint/some_file.txt";

View File

@ -0,0 +1,116 @@
/*-
* Copyright (c) 2019 The FreeBSD Foundation
* All rights reserved.
*
* 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 <unistd.h>
}
#include "mockfs.hh"
#include "utils.hh"
using namespace testing;
class Symlink: public FuseTest {};
TEST_F(Symlink, enospc)
{
const char FULLPATH[] = "mountpoint/lnk";
const char RELPATH[] = "lnk";
const char dst[] = "dst";
//const uint64_t ino = 42;
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_LOOKUP &&
strcmp(in->body.lookup, RELPATH) == 0);
}, Eq(true)),
_)
).WillOnce(Invoke([](auto in, auto out) {
out->header.unique = in->header.unique;
out->header.error = -ENOENT;
out->header.len = sizeof(out->header);
}));
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
const char *name = (const char*)in->body.bytes;
const char *linkname = name + strlen(name) + 1;
return (in->header.opcode == FUSE_SYMLINK &&
(0 == strcmp(linkname, dst)) &&
(0 == strcmp(name, RELPATH)));
}, Eq(true)),
_)
).WillOnce(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
out->header.error = -ENOSPC;
out->header.len = sizeof(out->header);
}));
EXPECT_EQ(-1, symlink(dst, FULLPATH));
EXPECT_EQ(ENOSPC, errno);
}
TEST_F(Symlink, ok)
{
const char FULLPATH[] = "mountpoint/src";
const char RELPATH[] = "src";
const char dst[] = "dst";
const uint64_t ino = 42;
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in->header.opcode == FUSE_LOOKUP &&
strcmp(in->body.lookup, RELPATH) == 0);
}, Eq(true)),
_)
).WillOnce(Invoke([](auto in, auto out) {
out->header.unique = in->header.unique;
out->header.error = -ENOENT;
out->header.len = sizeof(out->header);
}));
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
const char *name = (const char*)in->body.bytes;
const char *linkname = name + strlen(name) + 1;
return (in->header.opcode == FUSE_SYMLINK &&
(0 == strcmp(linkname, dst)) &&
(0 == strcmp(name, RELPATH)));
}, Eq(true)),
_)
).WillOnce(Invoke([=](auto in, auto out) {
out->header.unique = in->header.unique;
SET_OUT_HEADER_LEN(out, entry);
out->body.entry.attr.mode = S_IFLNK | 0777;
out->body.entry.nodeid = ino;
}));
EXPECT_EQ(0, symlink(dst, FULLPATH)) << strerror(errno);
}