1 //===-- Unittests for link ------------------------------------------------===//
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/errno/libc_errno.h"
10 #include "src/fcntl/open.h"
11 #include "src/unistd/close.h"
12 #include "src/unistd/link.h"
13 #include "src/unistd/unlink.h"
14 #include "test/UnitTest/ErrnoSetterMatcher.h"
15 #include "test/UnitTest/Test.h"
17 TEST(LlvmLibcLinkTest
, CreateAndUnlink
) {
18 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds
;
19 constexpr const char *TEST_FILE
= "testdata/link.test";
20 constexpr const char *TEST_FILE_LINK
= "testdata/link.test.link";
22 // The test strategy is as follows:
23 // 1. Create a normal file
24 // 2. Create a link to that file.
25 // 3. Open the link to check that the link was created.
26 // 4. Cleanup the file and its link.
28 int write_fd
= LIBC_NAMESPACE::open(TEST_FILE
, O_WRONLY
| O_CREAT
, S_IRWXU
);
29 ASSERT_EQ(libc_errno
, 0);
30 ASSERT_GT(write_fd
, 0);
31 ASSERT_THAT(LIBC_NAMESPACE::close(write_fd
), Succeeds(0));
32 ASSERT_THAT(LIBC_NAMESPACE::link(TEST_FILE
, TEST_FILE_LINK
), Succeeds(0));
34 int link_fd
= LIBC_NAMESPACE::open(TEST_FILE_LINK
, O_PATH
);
35 ASSERT_GT(link_fd
, 0);
36 ASSERT_EQ(libc_errno
, 0);
37 ASSERT_THAT(LIBC_NAMESPACE::close(link_fd
), Succeeds(0));
39 ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE
), Succeeds(0));
40 ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE_LINK
), Succeeds(0));
43 TEST(LlvmLibcLinkTest
, LinkToNonExistentFile
) {
44 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails
;
46 LIBC_NAMESPACE::link("testdata/non-existent-file", "testdata/bad-link"),