xref: /aosp_15_r20/external/llvm-libc/test/src/fcntl/openat_test.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Unittests for openat ----------------------------------------------===//
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/errno/libc_errno.h"
10 #include "src/fcntl/open.h"
11 #include "src/fcntl/openat.h"
12 #include "src/unistd/close.h"
13 #include "src/unistd/read.h"
14 #include "test/UnitTest/ErrnoSetterMatcher.h"
15 #include "test/UnitTest/Test.h"
16 
17 #include "hdr/fcntl_macros.h"
18 
TEST(LlvmLibcUniStd,OpenAndReadTest)19 TEST(LlvmLibcUniStd, OpenAndReadTest) {
20   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
21   constexpr const char *TEST_DIR = "testdata";
22   constexpr const char *TEST_FILE = "openat.test";
23   int dir_fd = LIBC_NAMESPACE::open(TEST_DIR, O_DIRECTORY);
24   ASSERT_ERRNO_SUCCESS();
25   ASSERT_GT(dir_fd, 0);
26   constexpr const char TEST_MSG[] = "openat test";
27   constexpr int TEST_MSG_SIZE = sizeof(TEST_MSG) - 1;
28 
29   int read_fd = LIBC_NAMESPACE::openat(dir_fd, TEST_FILE, O_RDONLY);
30   ASSERT_ERRNO_SUCCESS();
31   ASSERT_GT(read_fd, 0);
32   char read_buf[TEST_MSG_SIZE];
33   ASSERT_THAT(LIBC_NAMESPACE::read(read_fd, read_buf, TEST_MSG_SIZE),
34               Succeeds(TEST_MSG_SIZE));
35   ASSERT_THAT(LIBC_NAMESPACE::close(read_fd), Succeeds(0));
36   ASSERT_THAT(LIBC_NAMESPACE::close(dir_fd), Succeeds(0));
37 }
38 
TEST(LlvmLibcUniStd,FailTest)39 TEST(LlvmLibcUniStd, FailTest) {
40   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
41   EXPECT_THAT(LIBC_NAMESPACE::openat(AT_FDCWD, "openat.test", O_RDONLY),
42               Fails(ENOENT));
43 }
44