github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/syscalls/linux/chdir.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 <linux/limits.h> 17 #include <sys/socket.h> 18 #include <sys/stat.h> 19 #include <sys/types.h> 20 #include <sys/un.h> 21 22 #include "gtest/gtest.h" 23 #include "test/util/capability_util.h" 24 #include "test/util/temp_path.h" 25 #include "test/util/test_util.h" 26 27 namespace gvisor { 28 namespace testing { 29 30 namespace { 31 32 TEST(ChdirTest, Success) { 33 auto old_dir = GetAbsoluteTestTmpdir(); 34 auto temp_dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()); 35 EXPECT_THAT(chdir(temp_dir.path().c_str()), SyscallSucceeds()); 36 // Temp path destructor deletes the newly created tmp dir and Sentry rejects 37 // saving when its current dir is still pointing to the path. Switch to a 38 // permanent path here. 39 EXPECT_THAT(chdir(old_dir.c_str()), SyscallSucceeds()); 40 } 41 42 TEST(ChdirTest, PermissionDenied) { 43 // Drop capabilities that allow us to override directory permissions. 44 AutoCapability cap1(CAP_DAC_OVERRIDE, false); 45 AutoCapability cap2(CAP_DAC_READ_SEARCH, false); 46 47 auto temp_dir = ASSERT_NO_ERRNO_AND_VALUE( 48 TempPath::CreateDirWith(GetAbsoluteTestTmpdir(), 0666 /* mode */)); 49 EXPECT_THAT(chdir(temp_dir.path().c_str()), SyscallFailsWithErrno(EACCES)); 50 } 51 52 TEST(ChdirTest, NotDir) { 53 auto temp_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile()); 54 EXPECT_THAT(chdir(temp_file.path().c_str()), SyscallFailsWithErrno(ENOTDIR)); 55 } 56 57 TEST(ChdirTest, NotExist) { 58 EXPECT_THAT(chdir("/foo/bar"), SyscallFailsWithErrno(ENOENT)); 59 } 60 61 } // namespace 62 63 } // namespace testing 64 } // namespace gvisor