1 //===-- Unittests for chdir -----------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "src/errno/libc_errno.h" 10 #include "src/fcntl/open.h" 11 #include "src/unistd/chdir.h" 12 #include "src/unistd/close.h" 13 #include "test/UnitTest/ErrnoSetterMatcher.h" 14 #include "test/UnitTest/Test.h" 15 16 #include <fcntl.h> 17 18 TEST(LlvmLibcChdirTest, ChangeAndOpen) { 19 // The idea of this test is that we will first open an existing test file 20 // without changing the directory to make sure it exists. Next, we change 21 // directory and open the same file to make sure that the "chdir" operation 22 // succeeded. 23 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds; 24 constexpr const char *TEST_DIR = "testdata"; 25 constexpr const char *TEST_FILE = "testdata/chdir.test"; 26 constexpr const char *TEST_FILE_BASE = "chdir.test"; 27 libc_errno = 0; 28 29 int fd = LIBC_NAMESPACE::open(TEST_FILE, O_PATH); 30 ASSERT_GT(fd, 0); 31 ASSERT_EQ(libc_errno, 0); 32 ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0)); 33 34 ASSERT_THAT(LIBC_NAMESPACE::chdir(TEST_DIR), Succeeds(0)); 35 fd = LIBC_NAMESPACE::open(TEST_FILE_BASE, O_PATH); 36 ASSERT_GT(fd, 0); 37 ASSERT_EQ(libc_errno, 0); 38 ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0)); 39 } 40 41 TEST(LlvmLibcChdirTest, ChangeToNonExistentDir) { 42 libc_errno = 0; 43 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails; 44 ASSERT_THAT(LIBC_NAMESPACE::chdir("non-existent-dir"), Fails(ENOENT)); 45 libc_errno = 0; 46 } 47