1 //===-- Unittests for symlink ---------------------------------------------===//
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/symlink.h"
13 #include "src/unistd/unlink.h"
14 #include "test/UnitTest/ErrnoSetterMatcher.h"
15 #include "test/UnitTest/Test.h"
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
);
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(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));
39 ASSERT_THAT(LIBC_NAMESPACE::symlink(TEST_FILE_BASE
, TEST_FILE_LINK
),
42 int symlink_fd
= LIBC_NAMESPACE::open(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));
47 ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE
), Succeeds(0));
48 ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE_LINK
), Succeeds(0));
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"),