1 //===-- Unittests for symlinkat -------------------------------------------===//
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/symlinkat.h"
13 #include "src/unistd/unlink.h"
14 #include "test/UnitTest/ErrnoSetterMatcher.h"
15 #include "test/UnitTest/Test.h"
17 TEST(LlvmLibcSymlinkatTest
, CreateAndUnlink
) {
18 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds
;
19 constexpr const char *TEST_DIR
= "testdata";
20 constexpr const char *TEST_FILE
= "symlinkat.test";
21 constexpr const char *TEST_FILE_PATH
= "testdata/symlinkat.test";
22 constexpr const char *TEST_FILE_LINK
= "symlinkat.test.link";
23 constexpr const char *TEST_FILE_LINK_PATH
= "testdata/symlinkat.test.link";
25 // The test strategy is as follows:
26 // 1. Create a normal file
27 // 2. Create a link to that file.
28 // 3. Open the link to check that the link was created.
29 // 4. Cleanup the file and its link.
32 LIBC_NAMESPACE::open(TEST_FILE_PATH
, O_WRONLY
| O_CREAT
, S_IRWXU
);
33 ASSERT_EQ(libc_errno
, 0);
34 ASSERT_GT(write_fd
, 0);
35 ASSERT_THAT(LIBC_NAMESPACE::close(write_fd
), Succeeds(0));
37 int dir_fd
= LIBC_NAMESPACE::open(TEST_DIR
, O_DIRECTORY
);
38 ASSERT_THAT(LIBC_NAMESPACE::symlinkat(TEST_FILE
, dir_fd
, TEST_FILE_LINK
),
41 int link_fd
= LIBC_NAMESPACE::open(TEST_FILE_LINK_PATH
, O_PATH
);
42 ASSERT_GT(link_fd
, 0);
43 ASSERT_EQ(libc_errno
, 0);
44 ASSERT_THAT(LIBC_NAMESPACE::close(link_fd
), Succeeds(0));
46 ASSERT_THAT(LIBC_NAMESPACE::close(dir_fd
), Succeeds(0));
47 ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE_LINK_PATH
), Succeeds(0));
48 ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE_PATH
), Succeeds(0));
51 TEST(LlvmLibcSymlinkatTest
, SymlinkInNonExistentPath
) {
52 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails
;
53 ASSERT_THAT(LIBC_NAMESPACE::symlinkat("non-existent-dir/non-existent-file",
54 AT_FDCWD
, "non-existent-dir/bad-link"),