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/UnitTest/ErrnoCheckingTest.h" |
14 | #include "test/UnitTest/ErrnoSetterMatcher.h" |
15 | #include "test/UnitTest/Test.h" |
16 | |
17 | #include <sys/stat.h> |
18 | |
19 | using LlvmLibcUnlinkatTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest; |
20 | |
21 | TEST_F(LlvmLibcUnlinkatTest, CreateAndDeleteTest) { |
22 | using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds; |
23 | constexpr const char *FILENAME = "testdata" ; |
24 | auto TEST_DIR = libc_make_test_file_path(FILENAME); |
25 | constexpr const char *FILENAME2 = "openat.test" ; |
26 | auto TEST_FILE = libc_make_test_file_path(FILENAME2); |
27 | int dir_fd = LIBC_NAMESPACE::open(TEST_DIR, O_DIRECTORY); |
28 | ASSERT_ERRNO_SUCCESS(); |
29 | ASSERT_GT(dir_fd, 0); |
30 | int write_fd = |
31 | LIBC_NAMESPACE::openat(dir_fd, TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU); |
32 | ASSERT_ERRNO_SUCCESS(); |
33 | ASSERT_GT(write_fd, 0); |
34 | ASSERT_THAT(LIBC_NAMESPACE::close(write_fd), Succeeds(0)); |
35 | ASSERT_THAT(LIBC_NAMESPACE::unlinkat(dir_fd, TEST_FILE, 0), Succeeds(0)); |
36 | ASSERT_THAT(LIBC_NAMESPACE::close(dir_fd), Succeeds(0)); |
37 | } |
38 | |
39 | TEST_F(LlvmLibcUnlinkatTest, UnlinkatNonExistentFile) { |
40 | constexpr const char *FILENAME = "testdata" ; |
41 | auto TEST_DIR = libc_make_test_file_path(FILENAME); |
42 | int dir_fd = LIBC_NAMESPACE::open(TEST_DIR, O_DIRECTORY); |
43 | ASSERT_ERRNO_SUCCESS(); |
44 | ASSERT_GT(dir_fd, 0); |
45 | using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails; |
46 | using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds; |
47 | ASSERT_THAT(LIBC_NAMESPACE::unlinkat(dir_fd, "non-existent-file" , 0), |
48 | Fails(ENOENT)); |
49 | ASSERT_THAT(LIBC_NAMESPACE::close(dir_fd), Succeeds(0)); |
50 | } |
51 | |