extern "C" {
#include <sys/socket.h>
#include <sys/un.h>
#include <fcntl.h>
#include <unistd.h>
}
#include "mockfs.hh"
#include "utils.hh"
using namespace testing;
class Release: public FuseTest {
public:
void expect_lookup(const char *relpath, uint64_t ino, int times)
{
FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, 0, times);
}
void expect_release(uint64_t ino, uint64_t lock_owner,
uint32_t flags, int error)
{
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in.header.opcode == FUSE_RELEASE &&
in.header.nodeid == ino &&
in.body.release.lock_owner == lock_owner &&
in.body.release.fh == FH &&
in.body.release.flags == flags);
}, Eq(true)),
_)
).WillOnce(Invoke(ReturnErrno(error)))
.RetiresOnSaturation();
}
};
class ReleaseWithLocks: public Release {
virtual void SetUp() {
m_init_flags = FUSE_POSIX_LOCKS;
Release::SetUp();
}
};
TEST_F(Release, dup)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
uint64_t ino = 42;
int fd, fd2;
expect_lookup(RELPATH, ino, 1);
expect_open(ino, 0, 1);
expect_flush(ino, 1, ReturnErrno(0));
expect_release(ino, getpid(), O_RDONLY, 0);
fd = open(FULLPATH, O_RDONLY);
ASSERT_LE(0, fd) << strerror(errno);
fd2 = dup(fd);
ASSERT_LE(0, fd2) << strerror(errno);
ASSERT_EQ(0, close(fd2)) << strerror(errno);
ASSERT_EQ(0, close(fd)) << strerror(errno);
}
TEST_F(Release, eio)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
uint64_t ino = 42;
int fd;
expect_lookup(RELPATH, ino, 1);
expect_open(ino, 0, 1);
expect_flush(ino, 1, ReturnErrno(0));
expect_release(ino, getpid(), O_WRONLY, EIO);
fd = open(FULLPATH, O_WRONLY);
ASSERT_LE(0, fd) << strerror(errno);
ASSERT_TRUE(0 == close(fd) || errno == EIO) << strerror(errno);
}
TEST_F(Release, DISABLED_flags)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
uint64_t ino = 42;
int fd;
expect_lookup(RELPATH, ino, 1);
expect_open(ino, 0, 1);
expect_flush(ino, 1, ReturnErrno(0));
expect_release(ino, getpid(), O_RDWR | O_APPEND, 0);
fd = open(FULLPATH, O_RDWR | O_APPEND);
ASSERT_LE(0, fd) << strerror(errno);
ASSERT_EQ(0, close(fd)) << strerror(errno);
}
TEST_F(Release, multiple_opens)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
uint64_t ino = 42;
int fd, fd2;
expect_lookup(RELPATH, ino, 2);
expect_open(ino, 0, 2);
expect_flush(ino, 2, ReturnErrno(0));
expect_release(ino, getpid(), O_RDONLY, 0);
fd = open(FULLPATH, O_RDONLY);
ASSERT_LE(0, fd) << strerror(errno);
expect_release(ino, getpid(), O_WRONLY, 0);
fd2 = open(FULLPATH, O_WRONLY);
ASSERT_LE(0, fd2) << strerror(errno);
ASSERT_EQ(0, close(fd2)) << strerror(errno);
ASSERT_EQ(0, close(fd)) << strerror(errno);
}
TEST_F(Release, ok)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
uint64_t ino = 42;
int fd;
expect_lookup(RELPATH, ino, 1);
expect_open(ino, 0, 1);
expect_flush(ino, 1, ReturnErrno(0));
expect_release(ino, getpid(), O_RDONLY, 0);
fd = open(FULLPATH, O_RDONLY);
ASSERT_LE(0, fd) << strerror(errno);
ASSERT_EQ(0, close(fd)) << strerror(errno);
}
TEST_F(Release, scm_rights)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
struct msghdr msg;
struct iovec iov;
char message[CMSG_SPACE(sizeof(int))];
uint64_t ino = 42;
int fd;
int s[2];
union {
char buf[CMSG_SPACE(sizeof(fd))];
struct cmsghdr align;
} u;
expect_lookup(RELPATH, ino, 1);
expect_open(ino, 0, 1);
expect_flush(ino, 1, ReturnErrno(0));
expect_release(ino, getpid(), O_RDONLY, 0);
ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, s)) << strerror(errno);
fd = open(FULLPATH, O_RDONLY);
ASSERT_LE(0, fd) << strerror(errno);
memset(&message, 0, sizeof(message));
memset(&msg, 0, sizeof(msg));
iov.iov_base = NULL;
iov.iov_len = 0;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = u.buf,
msg.msg_controllen = sizeof(u.buf);
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
ASSERT_GE(sendmsg(s[0], &msg, 0), 0) << strerror(errno);
close(fd);
close(s[0]);
close(s[1]);
}
TEST_F(ReleaseWithLocks, unlock_on_close)
{
const char FULLPATH[] = "mountpoint/some_file.txt";
const char RELPATH[] = "some_file.txt";
uint64_t ino = 42;
int fd;
struct flock fl;
pid_t pid = getpid();
expect_lookup(RELPATH, ino, 1);
expect_open(ino, 0, 1);
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in.header.opcode == FUSE_SETLK &&
in.header.nodeid == ino &&
in.body.setlk.lk.type == F_RDLCK &&
in.body.setlk.fh == FH);
}, Eq(true)),
_)
).WillOnce(Invoke(ReturnErrno(0)));
EXPECT_CALL(*m_mock, process(
ResultOf([=](auto in) {
return (in.header.opcode == FUSE_SETLK &&
in.header.nodeid == ino &&
in.body.setlk.lk.type == F_UNLCK &&
in.body.setlk.fh == FH);
}, Eq(true)),
_)
).WillOnce(Invoke(ReturnErrno(0)));
expect_flush(ino, 1, ReturnErrno(0));
expect_release(ino, static_cast<uint64_t>(pid), O_RDWR, 0);
fd = open(FULLPATH, O_RDWR);
ASSERT_LE(0, fd) << strerror(errno);
fl.l_start = 0;
fl.l_len = 0;
fl.l_pid = pid;
fl.l_type = F_RDLCK;
fl.l_whence = SEEK_SET;
fl.l_sysid = 0;
ASSERT_NE(-1, fcntl(fd, F_SETLK, &fl)) << strerror(errno);
ASSERT_EQ(0, close(fd)) << strerror(errno);
}