Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / unistd / linkat_test.cpp
blob2ef41cd4e75f6a7840b85ce93a12ed0257518e39
1 //===-- Unittests for linkat ----------------------------------------------===//
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/linkat.h"
13 #include "src/unistd/unlink.h"
14 #include "test/UnitTest/ErrnoSetterMatcher.h"
15 #include "test/UnitTest/Test.h"
17 TEST(LlvmLibcLinkatTest, CreateAndUnlink) {
18 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
19 constexpr const char *TEST_DIR = "testdata";
20 constexpr const char *TEST_FILE = "linkat.test";
21 constexpr const char *TEST_FILE_PATH = "testdata/linkat.test";
22 constexpr const char *TEST_FILE_LINK = "linkat.test.link";
23 constexpr const char *TEST_FILE_LINK_PATH = "testdata/linkat.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.
30 libc_errno = 0;
31 int write_fd =
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(
39 LIBC_NAMESPACE::linkat(dir_fd, TEST_FILE, dir_fd, TEST_FILE_LINK, 0),
40 Succeeds(0));
42 int link_fd = LIBC_NAMESPACE::open(TEST_FILE_LINK_PATH, O_PATH);
43 ASSERT_GT(link_fd, 0);
44 ASSERT_EQ(libc_errno, 0);
45 ASSERT_THAT(LIBC_NAMESPACE::close(link_fd), Succeeds(0));
47 ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE_PATH), Succeeds(0));
48 ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE_LINK_PATH), Succeeds(0));
49 ASSERT_THAT(LIBC_NAMESPACE::close(dir_fd), Succeeds(0));
52 TEST(LlvmLibcLinkatTest, LinkToNonExistentFile) {
53 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
54 ASSERT_THAT(LIBC_NAMESPACE::linkat(AT_FDCWD, "testdata/non-existent-file",
55 AT_FDCWD, "testdata/bad-link", 0),
56 Fails(ENOENT));