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