1//===-- Unittests for fchdir ----------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "src/fcntl/open.h"
10#include "src/unistd/close.h"
11#include "src/unistd/fchdir.h"
12#include "test/UnitTest/ErrnoCheckingTest.h"
13#include "test/UnitTest/ErrnoSetterMatcher.h"
14#include "test/UnitTest/Test.h"
15
16#include "hdr/fcntl_macros.h"
17
18using LlvmLibcChdirTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
19
20TEST_F(LlvmLibcChdirTest, ChangeAndOpen) {
21 // The idea of this test is that we will first open an existing test file
22 // without changing the directory to make sure it exists. Next, we change
23 // directory and open the same file to make sure that the "fchdir" operation
24 // succeeded.
25 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
26 constexpr const char *FILENAME = "testdata";
27 auto TEST_DIR = libc_make_test_file_path(FILENAME);
28 constexpr const char *FILENAME2 = "testdata/fchdir.test";
29 auto TEST_FILE = libc_make_test_file_path(FILENAME2);
30 constexpr const char *FILENAME3 = "fchdir.test";
31 auto TEST_FILE_BASE = libc_make_test_file_path(FILENAME3);
32
33 int dir_fd = LIBC_NAMESPACE::open(TEST_DIR, O_DIRECTORY);
34 ASSERT_GT(dir_fd, 0);
35 ASSERT_ERRNO_SUCCESS();
36 int file_fd = LIBC_NAMESPACE::open(TEST_FILE, O_PATH);
37 ASSERT_GT(file_fd, 0);
38 ASSERT_ERRNO_SUCCESS();
39 ASSERT_THAT(LIBC_NAMESPACE::close(file_fd), Succeeds(0));
40
41 ASSERT_THAT(LIBC_NAMESPACE::fchdir(dir_fd), Succeeds(0));
42 file_fd = LIBC_NAMESPACE::open(TEST_FILE_BASE, O_PATH);
43 ASSERT_GT(file_fd, 0);
44 ASSERT_ERRNO_SUCCESS();
45 ASSERT_THAT(LIBC_NAMESPACE::close(file_fd), Succeeds(0));
46 ASSERT_THAT(LIBC_NAMESPACE::close(dir_fd), Succeeds(0));
47}
48
49TEST_F(LlvmLibcChdirTest, ChangeToNonExistentDir) {
50 ASSERT_EQ(LIBC_NAMESPACE::fchdir(0), -1);
51 ASSERT_ERRNO_FAILURE();
52}
53

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of libc/test/src/unistd/fchdir_test.cpp