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 | //===----------------------------------------------------------------------===// |
8 | |
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" |
16 | |
17 | namespace cpp = LIBC_NAMESPACE::cpp; |
18 | |
19 | TEST(LlvmLibcReadlinkTest, CreateAndUnlink) { |
20 | using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds; |
21 | constexpr const char *FILENAME = "readlink_test_value" ; |
22 | auto LINK_VAL = libc_make_test_file_path(FILENAME); |
23 | constexpr const char *FILENAME2 = "readlink.test.link" ; |
24 | auto LINK = libc_make_test_file_path(FILENAME2); |
25 | LIBC_NAMESPACE::libc_errno = 0; |
26 | |
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)); |
32 | |
33 | char buf[sizeof(LINK_VAL)]; |
34 | ssize_t len = LIBC_NAMESPACE::readlink(LINK, buf, sizeof(buf)); |
35 | ASSERT_ERRNO_SUCCESS(); |
36 | ASSERT_EQ(cpp::string_view(buf, len), cpp::string_view(LINK_VAL)); |
37 | |
38 | ASSERT_THAT(LIBC_NAMESPACE::unlink(LINK), Succeeds(0)); |
39 | } |
40 | |
41 | TEST(LlvmLibcReadlinkTest, ReadlinkInNonExistentPath) { |
42 | using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails; |
43 | char buf[8]; |
44 | ASSERT_THAT(LIBC_NAMESPACE::readlink("non-existent-link" , buf, sizeof(buf)), |
45 | Fails(ENOENT)); |
46 | } |
47 | |