[flang] Accept polymorphic component element in storage_size
[llvm-project.git] / libc / test / src / unistd / linkat_test.cpp
blob5a61b4e9301e8da39efb92d03bbfa54b90c7db4d
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/fcntl/open.h"
10 #include "src/unistd/close.h"
11 #include "src/unistd/linkat.h"
12 #include "src/unistd/unlink.h"
13 #include "test/ErrnoSetterMatcher.h"
14 #include "test/UnitTest/Test.h"
16 #include <errno.h>
18 TEST(LlvmLibcLinkatTest, CreateAndUnlink) {
19 using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
20 constexpr const char *TEST_DIR = "testdata";
21 constexpr const char *TEST_FILE = "linkat.test";
22 constexpr const char *TEST_FILE_PATH = "testdata/linkat.test";
23 constexpr const char *TEST_FILE_LINK = "linkat.test.link";
24 constexpr const char *TEST_FILE_LINK_PATH = "testdata/linkat.test.link";
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 errno = 0;
32 int write_fd = __llvm_libc::open(TEST_FILE_PATH, O_WRONLY | O_CREAT, S_IRWXU);
33 ASSERT_EQ(errno, 0);
34 ASSERT_GT(write_fd, 0);
35 ASSERT_THAT(__llvm_libc::close(write_fd), Succeeds(0));
37 int dir_fd = __llvm_libc::open(TEST_DIR, O_DIRECTORY);
38 ASSERT_THAT(__llvm_libc::linkat(dir_fd, TEST_FILE, dir_fd, TEST_FILE_LINK, 0),
39 Succeeds(0));
41 int link_fd = __llvm_libc::open(TEST_FILE_LINK_PATH, O_PATH);
42 ASSERT_GT(link_fd, 0);
43 ASSERT_EQ(errno, 0);
44 ASSERT_THAT(__llvm_libc::close(link_fd), Succeeds(0));
46 ASSERT_THAT(__llvm_libc::unlink(TEST_FILE_PATH), Succeeds(0));
47 ASSERT_THAT(__llvm_libc::unlink(TEST_FILE_LINK_PATH), Succeeds(0));
48 ASSERT_THAT(__llvm_libc::close(dir_fd), Succeeds(0));
51 TEST(LlvmLibcLinkatTest, LinkToNonExistentFile) {
52 using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
53 ASSERT_THAT(__llvm_libc::linkat(AT_FDCWD, "testdata/non-existent-file",
54 AT_FDCWD, "testdata/bad-link", 0),
55 Fails(ENOENT));