1 | //===-- Unittests for link ------------------------------------------------===// |
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/link.h" |
12 | #include "src/unistd/unlink.h" |
13 | #include "test/UnitTest/ErrnoCheckingTest.h" |
14 | #include "test/UnitTest/ErrnoSetterMatcher.h" |
15 | #include "test/UnitTest/Test.h" |
16 | |
17 | #include <sys/stat.h> |
18 | |
19 | using LlvmLibcLinkTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest; |
20 | |
21 | TEST_F(LlvmLibcLinkTest, CreateAndUnlink) { |
22 | using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds; |
23 | constexpr const char *FILENAME = "link.test" ; |
24 | auto TEST_FILE = libc_make_test_file_path(FILENAME); |
25 | constexpr const char *FILENAME2 = "link.test.link" ; |
26 | auto TEST_FILE_LINK = libc_make_test_file_path(FILENAME2); |
27 | |
28 | // The test strategy is as follows: |
29 | // 1. Create a normal file |
30 | // 2. Create a link to that file. |
31 | // 3. Open the link to check that the link was created. |
32 | // 4. Cleanup the file and its link. |
33 | int write_fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU); |
34 | ASSERT_ERRNO_SUCCESS(); |
35 | ASSERT_GT(write_fd, 0); |
36 | ASSERT_THAT(LIBC_NAMESPACE::close(write_fd), Succeeds(0)); |
37 | ASSERT_THAT(LIBC_NAMESPACE::link(TEST_FILE, TEST_FILE_LINK), Succeeds(0)); |
38 | |
39 | int link_fd = LIBC_NAMESPACE::open(TEST_FILE_LINK, O_PATH); |
40 | ASSERT_GT(link_fd, 0); |
41 | ASSERT_ERRNO_SUCCESS(); |
42 | ASSERT_THAT(LIBC_NAMESPACE::close(link_fd), Succeeds(0)); |
43 | |
44 | ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0)); |
45 | ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE_LINK), Succeeds(0)); |
46 | } |
47 | |
48 | TEST_F(LlvmLibcLinkTest, LinkToNonExistentFile) { |
49 | using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails; |
50 | ASSERT_THAT(LIBC_NAMESPACE::link("non-existent-file" , "bad-link" ), |
51 | Fails(ENOENT)); |
52 | } |
53 | |