1 //===-- Unittests for unlinkat --------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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 "test/UnitTest/Test.h"
15 #include "utils/testutils/FDReader.h"
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
);
27 __llvm_libc::openat(dir_fd
, TEST_FILE
, O_WRONLY
| O_CREAT
, S_IRWXU
);
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));
35 TEST(LlvmLibcUnlinkatTest
, UnlinkatNonExistentFile
) {
36 constexpr const char *TEST_DIR
= "testdata";
37 int dir_fd
= __llvm_libc::open(TEST_DIR
, O_DIRECTORY
);
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),
44 ASSERT_THAT(__llvm_libc::close(dir_fd
), Succeeds(0));