github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/fuse/linux/mount_test.cc (about)

     1  // Copyright 2020 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  #include <errno.h>
    16  #include <fcntl.h>
    17  #include <sys/mount.h>
    18  #include <unistd.h>
    19  
    20  #include "gtest/gtest.h"
    21  #include "test/util/mount_util.h"
    22  #include "test/util/temp_path.h"
    23  #include "test/util/test_util.h"
    24  
    25  namespace gvisor {
    26  namespace testing {
    27  
    28  namespace {
    29  
    30  TEST(FuseMount, Success) {
    31    const FileDescriptor fd =
    32        ASSERT_NO_ERRNO_AND_VALUE(Open("/dev/fuse", O_WRONLY));
    33    std::string mopts =
    34        absl::StrFormat("fd=%d,user_id=%d,group_id=%d,rootmode=0777", fd.get(),
    35                        getuid(), getgid());
    36  
    37    const auto dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
    38  
    39    const auto mount =
    40        ASSERT_NO_ERRNO_AND_VALUE(Mount("", dir.path(), "fuse", 0, mopts, 0));
    41  }
    42  
    43  TEST(FuseMount, FDNotParsable) {
    44    int devfd;
    45    EXPECT_THAT(devfd = open("/dev/fuse", O_RDWR), SyscallSucceeds());
    46    std::string mount_opts = "fd=thiscantbeparsed";
    47    TempPath mount_dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
    48    EXPECT_THAT(mount("fuse", mount_dir.path().c_str(), "fuse",
    49                      MS_NODEV | MS_NOSUID, mount_opts.c_str()),
    50                SyscallFailsWithErrno(EINVAL));
    51  }
    52  
    53  TEST(FuseMount, NoDevice) {
    54    const auto dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
    55  
    56    EXPECT_THAT(mount("", dir.path().c_str(), "fuse", 0, ""),
    57                SyscallFailsWithErrno(EINVAL));
    58  }
    59  
    60  TEST(FuseMount, ClosedFD) {
    61    FileDescriptor f = ASSERT_NO_ERRNO_AND_VALUE(Open("/dev/fuse", O_WRONLY));
    62    int fd = f.release();
    63    close(fd);
    64    std::string mopts = absl::StrCat("fd=", std::to_string(fd));
    65  
    66    const auto dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
    67  
    68    EXPECT_THAT(mount("", dir.path().c_str(), "fuse", 0, mopts.c_str()),
    69                SyscallFailsWithErrno(EINVAL));
    70  }
    71  
    72  TEST(FuseMount, BadFD) {
    73    const auto dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
    74    auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
    75    const FileDescriptor fd =
    76        ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDWR));
    77    std::string mopts = absl::StrCat("fd=", std::to_string(fd.get()));
    78  
    79    EXPECT_THAT(mount("", dir.path().c_str(), "fuse", 0, mopts.c_str()),
    80                SyscallFailsWithErrno(EINVAL));
    81  }
    82  
    83  }  // namespace
    84  
    85  }  // namespace testing
    86  }  // namespace gvisor