• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //===----------------------------------------------------------------------===//
8 
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"
16 
17 #include <fcntl.h>
18 
19 namespace cpp = LIBC_NAMESPACE::cpp;
20 
TEST(LlvmLibcReadlinkatTest,CreateAndUnlink)21 TEST(LlvmLibcReadlinkatTest, CreateAndUnlink) {
22   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
23   constexpr const char *FILENAME = "readlinkat_test_value";
24   auto LINK_VAL = libc_make_test_file_path(FILENAME);
25   constexpr const char *FILENAME2 = "readlinkat.test.link";
26   auto LINK = libc_make_test_file_path(FILENAME2);
27   LIBC_NAMESPACE::libc_errno = 0;
28 
29   // The test strategy is as follows:
30   //   1. Create a symlink with value LINK_VAL.
31   //   2. Read the symlink with readlink. The link value read should be LINK_VAL
32   //   3. Cleanup the symlink created in step #1.
33   ASSERT_THAT(LIBC_NAMESPACE::symlink(LINK_VAL, LINK), Succeeds(0));
34 
35   char buf[sizeof(LINK_VAL)];
36   ssize_t len = LIBC_NAMESPACE::readlinkat(AT_FDCWD, LINK, buf, sizeof(buf));
37   ASSERT_ERRNO_SUCCESS();
38   ASSERT_EQ(cpp::string_view(buf, len), cpp::string_view(LINK_VAL));
39 
40   ASSERT_THAT(LIBC_NAMESPACE::unlink(LINK), Succeeds(0));
41 }
42 
TEST(LlvmLibcReadlinkatTest,ReadlinkInNonExistentPath)43 TEST(LlvmLibcReadlinkatTest, ReadlinkInNonExistentPath) {
44   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
45   char buf[8];
46   ASSERT_THAT(LIBC_NAMESPACE::readlinkat(AT_FDCWD, "non-existent-link", buf,
47                                          sizeof(buf)),
48               Fails(ENOENT));
49 }
50