gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/syscalls/linux/fchdir.cc (about)

     1  // Copyright 2018 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 <fcntl.h>
    16  #include <sys/socket.h>
    17  #include <sys/stat.h>
    18  #include <sys/types.h>
    19  #include <sys/un.h>
    20  
    21  #include "gtest/gtest.h"
    22  #include "test/util/capability_util.h"
    23  #include "test/util/temp_path.h"
    24  #include "test/util/test_util.h"
    25  
    26  namespace gvisor {
    27  namespace testing {
    28  
    29  namespace {
    30  
    31  TEST(FchdirTest, Success) {
    32    auto temp_dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
    33    int fd;
    34    ASSERT_THAT(fd = open(temp_dir.path().c_str(), O_DIRECTORY | O_RDONLY),
    35                SyscallSucceeds());
    36  
    37    EXPECT_THAT(fchdir(fd), SyscallSucceeds());
    38    EXPECT_THAT(close(fd), SyscallSucceeds());
    39    // Change CWD to a permanent location as temp dirs will be cleaned up.
    40    EXPECT_THAT(chdir("/"), SyscallSucceeds());
    41  }
    42  
    43  TEST(FchdirTest, InvalidFD) {
    44    EXPECT_THAT(fchdir(-1), SyscallFailsWithErrno(EBADF));
    45  }
    46  
    47  TEST(FchdirTest, PermissionDenied) {
    48    // Drop capabilities that allow us to override directory permissions.
    49    AutoCapability cap1(CAP_DAC_OVERRIDE, false);
    50    AutoCapability cap2(CAP_DAC_READ_SEARCH, false);
    51  
    52    auto temp_dir = ASSERT_NO_ERRNO_AND_VALUE(
    53        TempPath::CreateDirWith(GetAbsoluteTestTmpdir(), 0666 /* mode */));
    54  
    55    int fd;
    56    ASSERT_THAT(fd = open(temp_dir.path().c_str(), O_DIRECTORY | O_RDONLY),
    57                SyscallSucceeds());
    58  
    59    EXPECT_THAT(fchdir(fd), SyscallFailsWithErrno(EACCES));
    60    EXPECT_THAT(close(fd), SyscallSucceeds());
    61  }
    62  
    63  TEST(FchdirTest, NotDir) {
    64    auto temp_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
    65  
    66    int fd;
    67    ASSERT_THAT(fd = open(temp_file.path().c_str(), O_CREAT | O_RDONLY, 0777),
    68                SyscallSucceeds());
    69  
    70    EXPECT_THAT(fchdir(fd), SyscallFailsWithErrno(ENOTDIR));
    71    EXPECT_THAT(close(fd), SyscallSucceeds());
    72  }
    73  
    74  TEST(FchdirTest, FchdirWithOpath) {
    75    auto temp_dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
    76    FileDescriptor fd = ASSERT_NO_ERRNO_AND_VALUE(Open(temp_dir.path(), O_PATH));
    77    ASSERT_THAT(open(temp_dir.path().c_str(), O_DIRECTORY | O_PATH),
    78                SyscallSucceeds());
    79  
    80    EXPECT_THAT(fchdir(fd.get()), SyscallSucceeds());
    81    // Change CWD to a permanent location as temp dirs will be cleaned up.
    82    EXPECT_THAT(chdir("/"), SyscallSucceeds());
    83  }
    84  
    85  }  // namespace
    86  
    87  }  // namespace testing
    88  }  // namespace gvisor