1 //===-- Unittests for fchdir ----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "src/fcntl/open.h"
10 #include "src/unistd/close.h"
11 #include "src/unistd/fchdir.h"
12 #include "test/ErrnoSetterMatcher.h"
13 #include "test/UnitTest/Test.h"
14 #include "utils/testutils/FDReader.h"
19 TEST(LlvmLibcChdirTest
, ChangeAndOpen
) {
20 // The idea of this test is that we will first open an existing test file
21 // without changing the directory to make sure it exists. Next, we change
22 // directory and open the same file to make sure that the "fchdir" operation
24 using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds
;
25 constexpr const char *TEST_DIR
= "testdata";
26 constexpr const char *TEST_FILE
= "testdata/fchdir.test";
27 constexpr const char *TEST_FILE_BASE
= "fchdir.test";
30 int dir_fd
= __llvm_libc::open(TEST_DIR
, O_DIRECTORY
);
33 int file_fd
= __llvm_libc::open(TEST_FILE
, O_PATH
);
34 ASSERT_GT(file_fd
, 0);
36 ASSERT_THAT(__llvm_libc::close(file_fd
), Succeeds(0));
38 ASSERT_THAT(__llvm_libc::fchdir(dir_fd
), Succeeds(0));
39 file_fd
= __llvm_libc::open(TEST_FILE_BASE
, O_PATH
);
40 ASSERT_GT(file_fd
, 0);
42 ASSERT_THAT(__llvm_libc::close(file_fd
), Succeeds(0));
43 ASSERT_THAT(__llvm_libc::close(dir_fd
), Succeeds(0));
46 TEST(LlvmLibcChdirTest
, ChangeToNonExistentDir
) {
47 using __llvm_libc::testing::ErrnoSetterMatcher::Fails
;
49 ASSERT_EQ(__llvm_libc::fchdir(0), -1);