Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / unistd / readlink_test.cpp
blobb2d018a50ec79312a6ba02dff505b8a02ded754c
1 //===-- Unittests for readlink --------------------------------------------===//
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/readlink.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 namespace cpp = LIBC_NAMESPACE::cpp;
19 TEST(LlvmLibcReadlinkTest, CreateAndUnlink) {
20 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
21 constexpr const char LINK_VAL[] = "readlink_test_value";
22 constexpr const char LINK[] = "testdata/readlink.test.link";
23 libc_errno = 0;
25 // The test strategy is as follows:
26 // 1. Create a symlink with value LINK_VAL.
27 // 2. Read the symlink with readlink. The link value read should be LINK_VAL
28 // 3. Cleanup the symlink created in step #1.
29 ASSERT_THAT(LIBC_NAMESPACE::symlink(LINK_VAL, LINK), Succeeds(0));
31 char buf[sizeof(LINK_VAL)];
32 ssize_t len = LIBC_NAMESPACE::readlink(LINK, buf, sizeof(buf));
33 ASSERT_EQ(libc_errno, 0);
34 ASSERT_EQ(cpp::string_view(buf, len), cpp::string_view(LINK_VAL));
36 ASSERT_THAT(LIBC_NAMESPACE::unlink(LINK), Succeeds(0));
39 TEST(LlvmLibcReadlinkTest, ReadlinkInNonExistentPath) {
40 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
41 char buf[8];
42 ASSERT_THAT(LIBC_NAMESPACE::readlink("non-existent-link", buf, sizeof(buf)),
43 Fails(ENOENT));