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