[OptTable] Fix typo VALUE => VALUES (NFCI) (#121523)
[llvm-project.git] / libc / test / src / unistd / link_test.cpp
blob3f1af2ec63f570b7a7c224fad2efa18211300ae9
1 //===-- Unittests for link ------------------------------------------------===//
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/link.h"
13 #include "src/unistd/unlink.h"
14 #include "test/UnitTest/ErrnoSetterMatcher.h"
15 #include "test/UnitTest/Test.h"
17 #include <sys/stat.h>
19 TEST(LlvmLibcLinkTest, CreateAndUnlink) {
20 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
21 constexpr const char *FILENAME = "link.test";
22 auto TEST_FILE = libc_make_test_file_path(FILENAME);
23 constexpr const char *FILENAME2 = "link.test.link";
24 auto TEST_FILE_LINK = libc_make_test_file_path(FILENAME2);
26 // The test strategy is as follows:
27 // 1. Create a normal file
28 // 2. Create a link to that file.
29 // 3. Open the link to check that the link was created.
30 // 4. Cleanup the file and its link.
31 LIBC_NAMESPACE::libc_errno = 0;
32 int write_fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
33 ASSERT_ERRNO_SUCCESS();
34 ASSERT_GT(write_fd, 0);
35 ASSERT_THAT(LIBC_NAMESPACE::close(write_fd), Succeeds(0));
36 ASSERT_THAT(LIBC_NAMESPACE::link(TEST_FILE, TEST_FILE_LINK), Succeeds(0));
38 int link_fd = LIBC_NAMESPACE::open(TEST_FILE_LINK, O_PATH);
39 ASSERT_GT(link_fd, 0);
40 ASSERT_ERRNO_SUCCESS();
41 ASSERT_THAT(LIBC_NAMESPACE::close(link_fd), Succeeds(0));
43 ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0));
44 ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE_LINK), Succeeds(0));
47 TEST(LlvmLibcLinkTest, LinkToNonExistentFile) {
48 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
49 ASSERT_THAT(LIBC_NAMESPACE::link("non-existent-file", "bad-link"),
50 Fails(ENOENT));