Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / unistd / symlink_test.cpp
blob84d963b7a207ce8f86d2a68cdb4bcdc26384c9e4
1 //===-- Unittests for symlink ---------------------------------------------===//
2 //
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
6 //
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.
28 libc_errno = 0;
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),
35 Succeeds(0));
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"),
50 Fails(ENOENT));