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"
17 TEST(LlvmLibcSymlinkTest
, CreateAndUnlink
) {
18 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds
;
19 constexpr const char *TEST_FILE_BASE
= "symlink.test";
20 constexpr const char *TEST_FILE
= "testdata/symlink.test";
21 constexpr const char *TEST_FILE_LINK
= "testdata/symlink.test.symlink";
23 // The test strategy is as follows:
24 // 1. Create a normal file
25 // 2. Create a symlink to that file.
26 // 3. Open the symlink to check that the symlink was created.
27 // 4. Cleanup the file and its symlink.
29 int write_fd
= LIBC_NAMESPACE::open(TEST_FILE
, O_WRONLY
| O_CREAT
, S_IRWXU
);
30 ASSERT_EQ(libc_errno
, 0);
31 ASSERT_GT(write_fd
, 0);
32 ASSERT_THAT(LIBC_NAMESPACE::close(write_fd
), Succeeds(0));
34 ASSERT_THAT(LIBC_NAMESPACE::symlink(TEST_FILE_BASE
, TEST_FILE_LINK
),
37 int symlink_fd
= LIBC_NAMESPACE::open(TEST_FILE_LINK
, O_PATH
);
38 ASSERT_GT(symlink_fd
, 0);
39 ASSERT_EQ(libc_errno
, 0);
40 ASSERT_THAT(LIBC_NAMESPACE::close(symlink_fd
), Succeeds(0));
42 ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE
), Succeeds(0));
43 ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE_LINK
), Succeeds(0));
46 TEST(LlvmLibcSymlinkTest
, SymlinkInNonExistentPath
) {
47 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails
;
48 ASSERT_THAT(LIBC_NAMESPACE::symlink("non-existent-dir/non-existent-file",
49 "non-existent-dir/bad-symlink"),