1 //===-- Unittests for unlinkat --------------------------------------------===// 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/unlinkat.h" 13 #include "test/ErrnoSetterMatcher.h" 14 #include "utils/UnitTest/Test.h" 15 #include "utils/testutils/FDReader.h" 16 17 #include <errno.h> 18 19 TEST(LlvmLibcUnlinkatTest, CreateAndDeleteTest) { 20 using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds; 21 constexpr const char *TEST_DIR = "testdata"; 22 constexpr const char *TEST_FILE = "openat.test"; 23 int dir_fd = __llvm_libc::open(TEST_DIR, O_DIRECTORY); 24 ASSERT_EQ(errno, 0); 25 ASSERT_GT(dir_fd, 0); 26 int write_fd = 27 __llvm_libc::openat(dir_fd, TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU); 28 ASSERT_EQ(errno, 0); 29 ASSERT_GT(write_fd, 0); 30 ASSERT_THAT(__llvm_libc::close(write_fd), Succeeds(0)); 31 ASSERT_THAT(__llvm_libc::unlinkat(dir_fd, TEST_FILE, 0), Succeeds(0)); 32 ASSERT_THAT(__llvm_libc::close(dir_fd), Succeeds(0)); 33 } 34 35 TEST(LlvmLibcUnlinkatTest, UnlinkatNonExistentFile) { 36 constexpr const char *TEST_DIR = "testdata"; 37 int dir_fd = __llvm_libc::open(TEST_DIR, O_DIRECTORY); 38 ASSERT_EQ(errno, 0); 39 ASSERT_GT(dir_fd, 0); 40 using __llvm_libc::testing::ErrnoSetterMatcher::Fails; 41 using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds; 42 ASSERT_THAT(__llvm_libc::unlinkat(dir_fd, "non-existent-file", 0), 43 Fails(ENOENT)); 44 ASSERT_THAT(__llvm_libc::close(dir_fd), Succeeds(0)); 45 } 46