Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / unistd / readlinkat_test.cpp
blob19f0ceec3cf7c5982a35afba9c5ef488e8ac990c
1 //===-- Unittests for readlinkat ------------------------------------------===//
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/__support/CPP/string_view.h"
10 #include "src/errno/libc_errno.h"
11 #include "src/unistd/readlinkat.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 #include <fcntl.h>
19 namespace cpp = LIBC_NAMESPACE::cpp;
21 TEST(LlvmLibcReadlinkatTest, CreateAndUnlink) {
22 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
23 constexpr const char LINK_VAL[] = "readlinkat_test_value";
24 constexpr const char LINK[] = "testdata/readlinkat.test.link";
25 libc_errno = 0;
27 // The test strategy is as follows:
28 // 1. Create a symlink with value LINK_VAL.
29 // 2. Read the symlink with readlink. The link value read should be LINK_VAL
30 // 3. Cleanup the symlink created in step #1.
31 ASSERT_THAT(LIBC_NAMESPACE::symlink(LINK_VAL, LINK), Succeeds(0));
33 char buf[sizeof(LINK_VAL)];
34 ssize_t len = LIBC_NAMESPACE::readlinkat(AT_FDCWD, LINK, buf, sizeof(buf));
35 ASSERT_EQ(libc_errno, 0);
36 ASSERT_EQ(cpp::string_view(buf, len), cpp::string_view(LINK_VAL));
38 ASSERT_THAT(LIBC_NAMESPACE::unlink(LINK), Succeeds(0));
41 TEST(LlvmLibcReadlinkatTest, ReadlinkInNonExistentPath) {
42 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
43 char buf[8];
44 ASSERT_THAT(LIBC_NAMESPACE::readlinkat(AT_FDCWD, "non-existent-link", buf,
45 sizeof(buf)),
46 Fails(ENOENT));