1*71db0c75SAndroid Build Coastguard Worker //===-- Unittests for symlinkat -------------------------------------------===//
2*71db0c75SAndroid Build Coastguard Worker //
3*71db0c75SAndroid Build Coastguard Worker // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*71db0c75SAndroid Build Coastguard Worker // See https://llvm.org/LICENSE.txt for license information.
5*71db0c75SAndroid Build Coastguard Worker // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*71db0c75SAndroid Build Coastguard Worker //
7*71db0c75SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
8*71db0c75SAndroid Build Coastguard Worker
9*71db0c75SAndroid Build Coastguard Worker #include "src/errno/libc_errno.h"
10*71db0c75SAndroid Build Coastguard Worker #include "src/fcntl/open.h"
11*71db0c75SAndroid Build Coastguard Worker #include "src/unistd/close.h"
12*71db0c75SAndroid Build Coastguard Worker #include "src/unistd/symlinkat.h"
13*71db0c75SAndroid Build Coastguard Worker #include "src/unistd/unlink.h"
14*71db0c75SAndroid Build Coastguard Worker #include "test/UnitTest/ErrnoSetterMatcher.h"
15*71db0c75SAndroid Build Coastguard Worker #include "test/UnitTest/Test.h"
16*71db0c75SAndroid Build Coastguard Worker
17*71db0c75SAndroid Build Coastguard Worker #include <sys/stat.h>
18*71db0c75SAndroid Build Coastguard Worker
TEST(LlvmLibcSymlinkatTest,CreateAndUnlink)19*71db0c75SAndroid Build Coastguard Worker TEST(LlvmLibcSymlinkatTest, CreateAndUnlink) {
20*71db0c75SAndroid Build Coastguard Worker using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
21*71db0c75SAndroid Build Coastguard Worker constexpr const char *FILENAME = "testdata";
22*71db0c75SAndroid Build Coastguard Worker auto TEST_DIR = libc_make_test_file_path(FILENAME);
23*71db0c75SAndroid Build Coastguard Worker constexpr const char *FILENAME2 = "symlinkat.test";
24*71db0c75SAndroid Build Coastguard Worker auto TEST_FILE = libc_make_test_file_path(FILENAME2);
25*71db0c75SAndroid Build Coastguard Worker constexpr const char *FILENAME3 = "testdata/symlinkat.test";
26*71db0c75SAndroid Build Coastguard Worker auto TEST_FILE_PATH = libc_make_test_file_path(FILENAME3);
27*71db0c75SAndroid Build Coastguard Worker constexpr const char *FILENAME4 = "symlinkat.test.link";
28*71db0c75SAndroid Build Coastguard Worker auto TEST_FILE_LINK = libc_make_test_file_path(FILENAME4);
29*71db0c75SAndroid Build Coastguard Worker constexpr const char *FILENAME5 = "testdata/symlinkat.test.link";
30*71db0c75SAndroid Build Coastguard Worker auto TEST_FILE_LINK_PATH = libc_make_test_file_path(FILENAME5);
31*71db0c75SAndroid Build Coastguard Worker
32*71db0c75SAndroid Build Coastguard Worker // The test strategy is as follows:
33*71db0c75SAndroid Build Coastguard Worker // 1. Create a normal file
34*71db0c75SAndroid Build Coastguard Worker // 2. Create a link to that file.
35*71db0c75SAndroid Build Coastguard Worker // 3. Open the link to check that the link was created.
36*71db0c75SAndroid Build Coastguard Worker // 4. Cleanup the file and its link.
37*71db0c75SAndroid Build Coastguard Worker LIBC_NAMESPACE::libc_errno = 0;
38*71db0c75SAndroid Build Coastguard Worker int write_fd =
39*71db0c75SAndroid Build Coastguard Worker LIBC_NAMESPACE::open(TEST_FILE_PATH, O_WRONLY | O_CREAT, S_IRWXU);
40*71db0c75SAndroid Build Coastguard Worker ASSERT_ERRNO_SUCCESS();
41*71db0c75SAndroid Build Coastguard Worker ASSERT_GT(write_fd, 0);
42*71db0c75SAndroid Build Coastguard Worker ASSERT_THAT(LIBC_NAMESPACE::close(write_fd), Succeeds(0));
43*71db0c75SAndroid Build Coastguard Worker
44*71db0c75SAndroid Build Coastguard Worker int dir_fd = LIBC_NAMESPACE::open(TEST_DIR, O_DIRECTORY);
45*71db0c75SAndroid Build Coastguard Worker ASSERT_THAT(LIBC_NAMESPACE::symlinkat(TEST_FILE, dir_fd, TEST_FILE_LINK),
46*71db0c75SAndroid Build Coastguard Worker Succeeds(0));
47*71db0c75SAndroid Build Coastguard Worker
48*71db0c75SAndroid Build Coastguard Worker int link_fd = LIBC_NAMESPACE::open(TEST_FILE_LINK_PATH, O_PATH);
49*71db0c75SAndroid Build Coastguard Worker ASSERT_GT(link_fd, 0);
50*71db0c75SAndroid Build Coastguard Worker ASSERT_ERRNO_SUCCESS();
51*71db0c75SAndroid Build Coastguard Worker ASSERT_THAT(LIBC_NAMESPACE::close(link_fd), Succeeds(0));
52*71db0c75SAndroid Build Coastguard Worker
53*71db0c75SAndroid Build Coastguard Worker ASSERT_THAT(LIBC_NAMESPACE::close(dir_fd), Succeeds(0));
54*71db0c75SAndroid Build Coastguard Worker ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE_LINK_PATH), Succeeds(0));
55*71db0c75SAndroid Build Coastguard Worker ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE_PATH), Succeeds(0));
56*71db0c75SAndroid Build Coastguard Worker }
57*71db0c75SAndroid Build Coastguard Worker
TEST(LlvmLibcSymlinkatTest,SymlinkInNonExistentPath)58*71db0c75SAndroid Build Coastguard Worker TEST(LlvmLibcSymlinkatTest, SymlinkInNonExistentPath) {
59*71db0c75SAndroid Build Coastguard Worker using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
60*71db0c75SAndroid Build Coastguard Worker ASSERT_THAT(LIBC_NAMESPACE::symlinkat("non-existent-dir/non-existent-file",
61*71db0c75SAndroid Build Coastguard Worker AT_FDCWD, "non-existent-dir/bad-link"),
62*71db0c75SAndroid Build Coastguard Worker Fails(ENOENT));
63*71db0c75SAndroid Build Coastguard Worker }
64