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"
19 TEST(LlvmLibcUnlinkatTest
, CreateAndDeleteTest
) {
20 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds
;
21 constexpr const char *FILENAME
= "testdata";
22 auto TEST_DIR
= libc_make_test_file_path(FILENAME
);
23 constexpr const char *FILENAME2
= "openat.test";
24 auto TEST_FILE
= libc_make_test_file_path(FILENAME2
);
25 int dir_fd
= LIBC_NAMESPACE::open(TEST_DIR
, O_DIRECTORY
);
26 ASSERT_ERRNO_SUCCESS();
29 LIBC_NAMESPACE::openat(dir_fd
, TEST_FILE
, O_WRONLY
| O_CREAT
, S_IRWXU
);
30 ASSERT_ERRNO_SUCCESS();
31 ASSERT_GT(write_fd
, 0);
32 ASSERT_THAT(LIBC_NAMESPACE::close(write_fd
), Succeeds(0));
33 ASSERT_THAT(LIBC_NAMESPACE::unlinkat(dir_fd
, TEST_FILE
, 0), Succeeds(0));
34 ASSERT_THAT(LIBC_NAMESPACE::close(dir_fd
), Succeeds(0));
37 TEST(LlvmLibcUnlinkatTest
, UnlinkatNonExistentFile
) {
38 constexpr const char *FILENAME
= "testdata";
39 auto TEST_DIR
= libc_make_test_file_path(FILENAME
);
40 int dir_fd
= LIBC_NAMESPACE::open(TEST_DIR
, O_DIRECTORY
);
41 ASSERT_ERRNO_SUCCESS();
43 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails
;
44 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds
;
45 ASSERT_THAT(LIBC_NAMESPACE::unlinkat(dir_fd
, "non-existent-file", 0),
47 ASSERT_THAT(LIBC_NAMESPACE::close(dir_fd
), Succeeds(0));