xref: /llvm-project/libc/test/src/unistd/chdir_test.cpp (revision b6bc9d72f65a5086f310f321e969d96e9a559e75)
105532289SSiva Chandra Reddy //===-- Unittests for chdir -----------------------------------------------===//
205532289SSiva Chandra Reddy //
305532289SSiva Chandra Reddy // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
405532289SSiva Chandra Reddy // See https://llvm.org/LICENSE.txt for license information.
505532289SSiva Chandra Reddy // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
605532289SSiva Chandra Reddy //
705532289SSiva Chandra Reddy //===----------------------------------------------------------------------===//
805532289SSiva Chandra Reddy 
967de5381SSiva Chandra Reddy #include "src/errno/libc_errno.h"
1005532289SSiva Chandra Reddy #include "src/fcntl/open.h"
1105532289SSiva Chandra Reddy #include "src/unistd/chdir.h"
1205532289SSiva Chandra Reddy #include "src/unistd/close.h"
134f1fe19dSSiva Chandra Reddy #include "test/UnitTest/ErrnoSetterMatcher.h"
14af1315c2SSiva Chandra Reddy #include "test/UnitTest/Test.h"
1505532289SSiva Chandra Reddy 
1605532289SSiva Chandra Reddy #include <fcntl.h>
1705532289SSiva Chandra Reddy 
1805532289SSiva Chandra Reddy TEST(LlvmLibcChdirTest, ChangeAndOpen) {
1905532289SSiva Chandra Reddy   // The idea of this test is that we will first open an existing test file
2005532289SSiva Chandra Reddy   // without changing the directory to make sure it exists. Next, we change
2105532289SSiva Chandra Reddy   // directory and open the same file to make sure that the "chdir" operation
2205532289SSiva Chandra Reddy   // succeeded.
23*b6bc9d72SGuillaume Chatelet   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
2405532289SSiva Chandra Reddy   constexpr const char *TEST_DIR = "testdata";
2505532289SSiva Chandra Reddy   constexpr const char *TEST_FILE = "testdata/chdir.test";
2605532289SSiva Chandra Reddy   constexpr const char *TEST_FILE_BASE = "chdir.test";
2767de5381SSiva Chandra Reddy   libc_errno = 0;
2805532289SSiva Chandra Reddy 
29*b6bc9d72SGuillaume Chatelet   int fd = LIBC_NAMESPACE::open(TEST_FILE, O_PATH);
3005532289SSiva Chandra Reddy   ASSERT_GT(fd, 0);
3167de5381SSiva Chandra Reddy   ASSERT_EQ(libc_errno, 0);
32*b6bc9d72SGuillaume Chatelet   ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
3305532289SSiva Chandra Reddy 
34*b6bc9d72SGuillaume Chatelet   ASSERT_THAT(LIBC_NAMESPACE::chdir(TEST_DIR), Succeeds(0));
35*b6bc9d72SGuillaume Chatelet   fd = LIBC_NAMESPACE::open(TEST_FILE_BASE, O_PATH);
3605532289SSiva Chandra Reddy   ASSERT_GT(fd, 0);
3767de5381SSiva Chandra Reddy   ASSERT_EQ(libc_errno, 0);
38*b6bc9d72SGuillaume Chatelet   ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
3905532289SSiva Chandra Reddy }
4005532289SSiva Chandra Reddy 
4105532289SSiva Chandra Reddy TEST(LlvmLibcChdirTest, ChangeToNonExistentDir) {
4267de5381SSiva Chandra Reddy   libc_errno = 0;
43*b6bc9d72SGuillaume Chatelet   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
44*b6bc9d72SGuillaume Chatelet   ASSERT_THAT(LIBC_NAMESPACE::chdir("non-existent-dir"), Fails(ENOENT));
4567de5381SSiva Chandra Reddy   libc_errno = 0;
4605532289SSiva Chandra Reddy }
47