xref: /llvm-project/libc/test/src/fcntl/openat_test.cpp (revision abc49cc19463970d5523d7d3332e4c1f83bc2ef7)
14abfe47eSSiva Chandra Reddy //===-- Unittests for openat ----------------------------------------------===//
24abfe47eSSiva Chandra Reddy //
34abfe47eSSiva Chandra Reddy // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44abfe47eSSiva Chandra Reddy // See https://llvm.org/LICENSE.txt for license information.
54abfe47eSSiva Chandra Reddy // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
64abfe47eSSiva Chandra Reddy //
74abfe47eSSiva Chandra Reddy //===----------------------------------------------------------------------===//
84abfe47eSSiva Chandra Reddy 
9d49b993fSSiva Chandra Reddy #include "src/errno/libc_errno.h"
104abfe47eSSiva Chandra Reddy #include "src/fcntl/open.h"
114abfe47eSSiva Chandra Reddy #include "src/fcntl/openat.h"
124abfe47eSSiva Chandra Reddy #include "src/unistd/close.h"
134abfe47eSSiva Chandra Reddy #include "src/unistd/read.h"
144f1fe19dSSiva Chandra Reddy #include "test/UnitTest/ErrnoSetterMatcher.h"
15af1315c2SSiva Chandra Reddy #include "test/UnitTest/Test.h"
164abfe47eSSiva Chandra Reddy 
17*abc49cc1SJob Henandez Lara #include "hdr/fcntl_macros.h"
184abfe47eSSiva Chandra Reddy 
194abfe47eSSiva Chandra Reddy TEST(LlvmLibcUniStd, OpenAndReadTest) {
20b6bc9d72SGuillaume Chatelet   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
214abfe47eSSiva Chandra Reddy   constexpr const char *TEST_DIR = "testdata";
224abfe47eSSiva Chandra Reddy   constexpr const char *TEST_FILE = "openat.test";
23b6bc9d72SGuillaume Chatelet   int dir_fd = LIBC_NAMESPACE::open(TEST_DIR, O_DIRECTORY);
2473874f7aSGuillaume Chatelet   ASSERT_ERRNO_SUCCESS();
254abfe47eSSiva Chandra Reddy   ASSERT_GT(dir_fd, 0);
264abfe47eSSiva Chandra Reddy   constexpr const char TEST_MSG[] = "openat test";
274abfe47eSSiva Chandra Reddy   constexpr int TEST_MSG_SIZE = sizeof(TEST_MSG) - 1;
284abfe47eSSiva Chandra Reddy 
29b6bc9d72SGuillaume Chatelet   int read_fd = LIBC_NAMESPACE::openat(dir_fd, TEST_FILE, O_RDONLY);
3073874f7aSGuillaume Chatelet   ASSERT_ERRNO_SUCCESS();
314abfe47eSSiva Chandra Reddy   ASSERT_GT(read_fd, 0);
324abfe47eSSiva Chandra Reddy   char read_buf[TEST_MSG_SIZE];
33b6bc9d72SGuillaume Chatelet   ASSERT_THAT(LIBC_NAMESPACE::read(read_fd, read_buf, TEST_MSG_SIZE),
344abfe47eSSiva Chandra Reddy               Succeeds(TEST_MSG_SIZE));
35b6bc9d72SGuillaume Chatelet   ASSERT_THAT(LIBC_NAMESPACE::close(read_fd), Succeeds(0));
36b6bc9d72SGuillaume Chatelet   ASSERT_THAT(LIBC_NAMESPACE::close(dir_fd), Succeeds(0));
374abfe47eSSiva Chandra Reddy }
384abfe47eSSiva Chandra Reddy 
394abfe47eSSiva Chandra Reddy TEST(LlvmLibcUniStd, FailTest) {
40b6bc9d72SGuillaume Chatelet   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
41b6bc9d72SGuillaume Chatelet   EXPECT_THAT(LIBC_NAMESPACE::openat(AT_FDCWD, "openat.test", O_RDONLY),
424abfe47eSSiva Chandra Reddy               Fails(ENOENT));
434abfe47eSSiva Chandra Reddy }
44