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/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/unlinkat.h"
14 #include "test/UnitTest/ErrnoSetterMatcher.h"
15 #include "test/UnitTest/Test.h"
17 TEST(LlvmLibcUnlinkatTest
, CreateAndDeleteTest
) {
18 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds
;
19 constexpr const char *TEST_DIR
= "testdata";
20 constexpr const char *TEST_FILE
= "openat.test";
21 int dir_fd
= LIBC_NAMESPACE::open(TEST_DIR
, O_DIRECTORY
);
22 ASSERT_EQ(libc_errno
, 0);
25 LIBC_NAMESPACE::openat(dir_fd
, TEST_FILE
, O_WRONLY
| O_CREAT
, S_IRWXU
);
26 ASSERT_EQ(libc_errno
, 0);
27 ASSERT_GT(write_fd
, 0);
28 ASSERT_THAT(LIBC_NAMESPACE::close(write_fd
), Succeeds(0));
29 ASSERT_THAT(LIBC_NAMESPACE::unlinkat(dir_fd
, TEST_FILE
, 0), Succeeds(0));
30 ASSERT_THAT(LIBC_NAMESPACE::close(dir_fd
), Succeeds(0));
33 TEST(LlvmLibcUnlinkatTest
, UnlinkatNonExistentFile
) {
34 constexpr const char *TEST_DIR
= "testdata";
35 int dir_fd
= LIBC_NAMESPACE::open(TEST_DIR
, O_DIRECTORY
);
36 ASSERT_EQ(libc_errno
, 0);
38 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails
;
39 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds
;
40 ASSERT_THAT(LIBC_NAMESPACE::unlinkat(dir_fd
, "non-existent-file", 0),
42 ASSERT_THAT(LIBC_NAMESPACE::close(dir_fd
), Succeeds(0));