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/errno/libc_errno.h"
10 #include "src/fcntl/open.h"
11 #include "src/unistd/close.h"
12 #include "src/unistd/fchdir.h"
13 #include "test/UnitTest/ErrnoSetterMatcher.h"
14 #include "test/UnitTest/Test.h"
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 "fchdir" operation
23 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds
;
24 constexpr const char *TEST_DIR
= "testdata";
25 constexpr const char *TEST_FILE
= "testdata/fchdir.test";
26 constexpr const char *TEST_FILE_BASE
= "fchdir.test";
29 int dir_fd
= LIBC_NAMESPACE::open(TEST_DIR
, O_DIRECTORY
);
31 ASSERT_EQ(libc_errno
, 0);
32 int file_fd
= LIBC_NAMESPACE::open(TEST_FILE
, O_PATH
);
33 ASSERT_GT(file_fd
, 0);
34 ASSERT_EQ(libc_errno
, 0);
35 ASSERT_THAT(LIBC_NAMESPACE::close(file_fd
), Succeeds(0));
37 ASSERT_THAT(LIBC_NAMESPACE::fchdir(dir_fd
), Succeeds(0));
38 file_fd
= LIBC_NAMESPACE::open(TEST_FILE_BASE
, O_PATH
);
39 ASSERT_GT(file_fd
, 0);
40 ASSERT_EQ(libc_errno
, 0);
41 ASSERT_THAT(LIBC_NAMESPACE::close(file_fd
), Succeeds(0));
42 ASSERT_THAT(LIBC_NAMESPACE::close(dir_fd
), Succeeds(0));
45 TEST(LlvmLibcChdirTest
, ChangeToNonExistentDir
) {
46 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails
;
48 ASSERT_EQ(LIBC_NAMESPACE::fchdir(0), -1);
49 ASSERT_NE(libc_errno
, 0);