Path: blob/main/tests/sys/fs/fusefs/default_permissions_privileged.cc
39537 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2019 The FreeBSD Foundation4*5* This software was developed by BFF Storage Systems, LLC under sponsorship6* from the FreeBSD Foundation.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND18* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE21* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT25* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY26* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF27* SUCH DAMAGE.28*/2930/*31* Tests for the "default_permissions" mount option that require a privileged32* user.33*/3435extern "C" {36#include <sys/types.h>37#include <sys/extattr.h>3839#include <fcntl.h>40#include <unistd.h>41}4243#include "mockfs.hh"44#include "utils.hh"4546using namespace testing;4748class DefaultPermissionsPrivileged: public FuseTest {49virtual void SetUp() {50m_default_permissions = true;51FuseTest::SetUp();52if (HasFatalFailure() || IsSkipped())53return;5455if (geteuid() != 0) {56GTEST_SKIP() << "This test requires a privileged user";57}5859/* With -o default_permissions, FUSE_ACCESS should never be called */60EXPECT_CALL(*m_mock, process(61ResultOf([=](auto in) {62return (in.header.opcode == FUSE_ACCESS);63}, Eq(true)),64_)65).Times(0);66}6768public:69void expect_getattr(uint64_t ino, mode_t mode, uint64_t attr_valid, int times,70uid_t uid = 0, gid_t gid = 0)71{72EXPECT_CALL(*m_mock, process(73ResultOf([=](auto in) {74return (in.header.opcode == FUSE_GETATTR &&75in.header.nodeid == ino);76}, Eq(true)),77_)78).Times(times)79.WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {80SET_OUT_HEADER_LEN(out, attr);81out.body.attr.attr.ino = ino; // Must match nodeid82out.body.attr.attr.mode = mode;83out.body.attr.attr.size = 0;84out.body.attr.attr.uid = uid;85out.body.attr.attr.gid = gid;86out.body.attr.attr_valid = attr_valid;87})));88}8990void expect_lookup(const char *relpath, uint64_t ino, mode_t mode,91uint64_t attr_valid, uid_t uid = 0, gid_t gid = 0)92{93FuseTest::expect_lookup(relpath, ino, mode, 0, 1, attr_valid, uid, gid);94}9596};9798class Setattr: public DefaultPermissionsPrivileged {};99100TEST_F(Setattr, sticky_regular_file)101{102const char FULLPATH[] = "mountpoint/some_file.txt";103const char RELPATH[] = "some_file.txt";104const uint64_t ino = 42;105const mode_t oldmode = 0644;106const mode_t newmode = 01644;107108expect_getattr(1, S_IFDIR | 0755, UINT64_MAX, 1);109expect_lookup(RELPATH, ino, S_IFREG | oldmode, UINT64_MAX, geteuid());110EXPECT_CALL(*m_mock, process(111ResultOf([](auto in) {112return (in.header.opcode == FUSE_SETATTR);113}, Eq(true)),114_)115).WillOnce(Invoke(ReturnImmediate([](auto in __unused, auto& out) {116SET_OUT_HEADER_LEN(out, attr);117out.body.attr.attr.mode = S_IFREG | newmode;118})));119120EXPECT_EQ(0, chmod(FULLPATH, newmode)) << strerror(errno);121}122123124125126