| 1 | //===-- Unittests for linkat ----------------------------------------------===// |
| 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/linkat.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 LlvmLibcLinkatTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest; |
| 20 | |
| 21 | TEST_F(LlvmLibcLinkatTest, CreateAndUnlink) { |
| 22 | using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds; |
| 23 | constexpr const char *FILENAME = "testdata" ; |
| 24 | auto TEST_DIR = libc_make_test_file_path(FILENAME); |
| 25 | constexpr const char *FILENAME2 = "linkat.test" ; |
| 26 | auto TEST_FILE = libc_make_test_file_path(FILENAME2); |
| 27 | constexpr const char *FILENAME3 = "testdata/linkat.test" ; |
| 28 | auto TEST_FILE_PATH = libc_make_test_file_path(FILENAME3); |
| 29 | constexpr const char *FILENAME4 = "linkat.test.link" ; |
| 30 | auto TEST_FILE_LINK = libc_make_test_file_path(FILENAME4); |
| 31 | constexpr const char *FILENAME5 = "testdata/linkat.test.link" ; |
| 32 | auto TEST_FILE_LINK_PATH = libc_make_test_file_path(FILENAME5); |
| 33 | |
| 34 | // The test strategy is as follows: |
| 35 | // 1. Create a normal file |
| 36 | // 2. Create a link to that file. |
| 37 | // 3. Open the link to check that the link was created. |
| 38 | // 4. Cleanup the file and its link. |
| 39 | int write_fd = |
| 40 | LIBC_NAMESPACE::open(TEST_FILE_PATH, O_WRONLY | O_CREAT, S_IRWXU); |
| 41 | ASSERT_ERRNO_SUCCESS(); |
| 42 | ASSERT_GT(write_fd, 0); |
| 43 | ASSERT_THAT(LIBC_NAMESPACE::close(write_fd), Succeeds(0)); |
| 44 | |
| 45 | int dir_fd = LIBC_NAMESPACE::open(TEST_DIR, O_DIRECTORY); |
| 46 | ASSERT_THAT( |
| 47 | LIBC_NAMESPACE::linkat(dir_fd, TEST_FILE, dir_fd, TEST_FILE_LINK, 0), |
| 48 | Succeeds(0)); |
| 49 | |
| 50 | int link_fd = LIBC_NAMESPACE::open(TEST_FILE_LINK_PATH, O_PATH); |
| 51 | ASSERT_GT(link_fd, 0); |
| 52 | ASSERT_ERRNO_SUCCESS(); |
| 53 | ASSERT_THAT(LIBC_NAMESPACE::close(link_fd), Succeeds(0)); |
| 54 | |
| 55 | ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE_PATH), Succeeds(0)); |
| 56 | ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE_LINK_PATH), Succeeds(0)); |
| 57 | ASSERT_THAT(LIBC_NAMESPACE::close(dir_fd), Succeeds(0)); |
| 58 | } |
| 59 | |
| 60 | TEST_F(LlvmLibcLinkatTest, LinkToNonExistentFile) { |
| 61 | using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails; |
| 62 | ASSERT_THAT(LIBC_NAMESPACE::linkat(AT_FDCWD, "testdata/non-existent-file" , |
| 63 | AT_FDCWD, "testdata/bad-link" , 0), |
| 64 | Fails(ENOENT)); |
| 65 | } |
| 66 | |