1 //===-- Unittests for chdir -----------------------------------------------===//
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/chdir.h"
12 #include "src/unistd/close.h"
13 #include "test/UnitTest/ErrnoSetterMatcher.h"
14 #include "test/UnitTest/Test.h"
16 #include "hdr/fcntl_macros.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 "chdir" operation
23 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds
;
24 constexpr const char *FILENAME
= "testdata";
25 auto TEST_DIR
= libc_make_test_file_path(FILENAME
);
26 constexpr const char *FILENAME2
= "testdata/chdir.test";
27 auto TEST_FILE
= libc_make_test_file_path(FILENAME2
);
28 constexpr const char *FILENAME3
= "chdir.test";
29 auto TEST_FILE_BASE
= libc_make_test_file_path(FILENAME3
);
30 LIBC_NAMESPACE::libc_errno
= 0;
32 int fd
= LIBC_NAMESPACE::open(TEST_FILE
, O_PATH
);
34 ASSERT_ERRNO_SUCCESS();
35 ASSERT_THAT(LIBC_NAMESPACE::close(fd
), Succeeds(0));
37 ASSERT_THAT(LIBC_NAMESPACE::chdir(TEST_DIR
), Succeeds(0));
38 fd
= LIBC_NAMESPACE::open(TEST_FILE_BASE
, O_PATH
);
40 ASSERT_ERRNO_SUCCESS();
41 ASSERT_THAT(LIBC_NAMESPACE::close(fd
), Succeeds(0));
44 TEST(LlvmLibcChdirTest
, ChangeToNonExistentDir
) {
45 LIBC_NAMESPACE::libc_errno
= 0;
46 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails
;
47 ASSERT_THAT(LIBC_NAMESPACE::chdir("non-existent-dir"), Fails(ENOENT
));
48 LIBC_NAMESPACE::libc_errno
= 0;