1 //===-- Unittests for readlinkat ------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "src/__support/CPP/string_view.h"
10 #include "src/errno/libc_errno.h"
11 #include "src/string/string_utils.h"
12 #include "src/unistd/readlinkat.h"
13 #include "src/unistd/symlink.h"
14 #include "src/unistd/unlink.h"
15 #include "test/UnitTest/ErrnoSetterMatcher.h"
16 #include "test/UnitTest/Test.h"
18 #include "hdr/fcntl_macros.h"
20 namespace cpp
= LIBC_NAMESPACE::cpp
;
22 TEST(LlvmLibcReadlinkatTest
, CreateAndUnlink
) {
23 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds
;
24 constexpr const char *FILENAME
= "readlinkat_test_file";
25 auto LINK_VAL
= libc_make_test_file_path(FILENAME
);
26 constexpr const char *FILENAME2
= "readlinkat_test_file.link";
27 auto LINK
= libc_make_test_file_path(FILENAME2
);
28 LIBC_NAMESPACE::libc_errno
= 0;
30 // The test strategy is as follows:
31 // 1. Create a symlink with value LINK_VAL.
32 // 2. Read the symlink with readlink. The link value read should be LINK_VAL
33 // 3. Cleanup the symlink created in step #1.
34 ASSERT_THAT(LIBC_NAMESPACE::symlink(LINK_VAL
, LINK
), Succeeds(0));
37 ssize_t len
= LIBC_NAMESPACE::readlinkat(
38 AT_FDCWD
, LINK
, buf
, LIBC_NAMESPACE::internal::string_length(FILENAME
));
39 ASSERT_ERRNO_SUCCESS();
40 ASSERT_EQ(cpp::string_view(buf
, len
), cpp::string_view(LINK_VAL
));
42 ASSERT_THAT(LIBC_NAMESPACE::unlink(LINK
), Succeeds(0));
45 TEST(LlvmLibcReadlinkatTest
, ReadlinkInNonExistentPath
) {
46 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails
;
47 constexpr auto LEN
= 8;
50 LIBC_NAMESPACE::readlinkat(AT_FDCWD
, "non-existent-link", buf
, LEN
),