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