xref: /llvm-project/libc/test/src/unistd/fchdir_test.cpp (revision b6bc9d72f65a5086f310f321e969d96e9a559e75)
105532289SSiva Chandra Reddy //===-- Unittests for fchdir ----------------------------------------------===//
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/close.h"
1205532289SSiva Chandra Reddy #include "src/unistd/fchdir.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 "fchdir" 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/fchdir.test";
2605532289SSiva Chandra Reddy   constexpr const char *TEST_FILE_BASE = "fchdir.test";
2767de5381SSiva Chandra Reddy   libc_errno = 0;
2805532289SSiva Chandra Reddy 
29*b6bc9d72SGuillaume Chatelet   int dir_fd = LIBC_NAMESPACE::open(TEST_DIR, O_DIRECTORY);
3005532289SSiva Chandra Reddy   ASSERT_GT(dir_fd, 0);
3167de5381SSiva Chandra Reddy   ASSERT_EQ(libc_errno, 0);
32*b6bc9d72SGuillaume Chatelet   int file_fd = LIBC_NAMESPACE::open(TEST_FILE, O_PATH);
3305532289SSiva Chandra Reddy   ASSERT_GT(file_fd, 0);
3467de5381SSiva Chandra Reddy   ASSERT_EQ(libc_errno, 0);
35*b6bc9d72SGuillaume Chatelet   ASSERT_THAT(LIBC_NAMESPACE::close(file_fd), Succeeds(0));
3605532289SSiva Chandra Reddy 
37*b6bc9d72SGuillaume Chatelet   ASSERT_THAT(LIBC_NAMESPACE::fchdir(dir_fd), Succeeds(0));
38*b6bc9d72SGuillaume Chatelet   file_fd = LIBC_NAMESPACE::open(TEST_FILE_BASE, O_PATH);
3905532289SSiva Chandra Reddy   ASSERT_GT(file_fd, 0);
4067de5381SSiva Chandra Reddy   ASSERT_EQ(libc_errno, 0);
41*b6bc9d72SGuillaume Chatelet   ASSERT_THAT(LIBC_NAMESPACE::close(file_fd), Succeeds(0));
42*b6bc9d72SGuillaume Chatelet   ASSERT_THAT(LIBC_NAMESPACE::close(dir_fd), Succeeds(0));
4305532289SSiva Chandra Reddy }
4405532289SSiva Chandra Reddy 
4505532289SSiva Chandra Reddy TEST(LlvmLibcChdirTest, ChangeToNonExistentDir) {
46*b6bc9d72SGuillaume Chatelet   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
4767de5381SSiva Chandra Reddy   libc_errno = 0;
48*b6bc9d72SGuillaume Chatelet   ASSERT_EQ(LIBC_NAMESPACE::fchdir(0), -1);
4967de5381SSiva Chandra Reddy   ASSERT_NE(libc_errno, 0);
5067de5381SSiva Chandra Reddy   libc_errno = 0;
5105532289SSiva Chandra Reddy }
52