1 //===-- Unittests for linkat ----------------------------------------------===//
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/fcntl/open.h"
10 #include "src/unistd/close.h"
11 #include "src/unistd/linkat.h"
12 #include "src/unistd/unlink.h"
13 #include "test/ErrnoSetterMatcher.h"
14 #include "test/UnitTest/Test.h"
18 TEST(LlvmLibcLinkatTest
, CreateAndUnlink
) {
19 using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds
;
20 constexpr const char *TEST_DIR
= "testdata";
21 constexpr const char *TEST_FILE
= "linkat.test";
22 constexpr const char *TEST_FILE_PATH
= "testdata/linkat.test";
23 constexpr const char *TEST_FILE_LINK
= "linkat.test.link";
24 constexpr const char *TEST_FILE_LINK_PATH
= "testdata/linkat.test.link";
26 // The test strategy is as follows:
27 // 1. Create a normal file
28 // 2. Create a link to that file.
29 // 3. Open the link to check that the link was created.
30 // 4. Cleanup the file and its link.
32 int write_fd
= __llvm_libc::open(TEST_FILE_PATH
, O_WRONLY
| O_CREAT
, S_IRWXU
);
34 ASSERT_GT(write_fd
, 0);
35 ASSERT_THAT(__llvm_libc::close(write_fd
), Succeeds(0));
37 int dir_fd
= __llvm_libc::open(TEST_DIR
, O_DIRECTORY
);
38 ASSERT_THAT(__llvm_libc::linkat(dir_fd
, TEST_FILE
, dir_fd
, TEST_FILE_LINK
, 0),
41 int link_fd
= __llvm_libc::open(TEST_FILE_LINK_PATH
, O_PATH
);
42 ASSERT_GT(link_fd
, 0);
44 ASSERT_THAT(__llvm_libc::close(link_fd
), Succeeds(0));
46 ASSERT_THAT(__llvm_libc::unlink(TEST_FILE_PATH
), Succeeds(0));
47 ASSERT_THAT(__llvm_libc::unlink(TEST_FILE_LINK_PATH
), Succeeds(0));
48 ASSERT_THAT(__llvm_libc::close(dir_fd
), Succeeds(0));
51 TEST(LlvmLibcLinkatTest
, LinkToNonExistentFile
) {
52 using __llvm_libc::testing::ErrnoSetterMatcher::Fails
;
53 ASSERT_THAT(__llvm_libc::linkat(AT_FDCWD
, "testdata/non-existent-file",
54 AT_FDCWD
, "testdata/bad-link", 0),