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