1 | //===-- Unittests for remove ----------------------------------------------===// |
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/stdio/remove.h" |
11 | #include "src/sys/stat/mkdirat.h" |
12 | #include "src/unistd/access.h" |
13 | #include "src/unistd/close.h" |
14 | #include "test/UnitTest/ErrnoSetterMatcher.h" |
15 | #include "test/UnitTest/Test.h" |
16 | |
17 | #include "src/errno/libc_errno.h" |
18 | #include <unistd.h> |
19 | |
20 | TEST(LlvmLibcRemoveTest, CreateAndRemoveFile) { |
21 | // The test strategy is to create a file and remove it, and also verify that |
22 | // it was removed. |
23 | LIBC_NAMESPACE::libc_errno = 0; |
24 | using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails; |
25 | using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds; |
26 | |
27 | constexpr const char *FILENAME = "remove.test.file" ; |
28 | auto TEST_FILE = libc_make_test_file_path(FILENAME); |
29 | int fd = LIBC_NAMESPACE::open(path: TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU); |
30 | ASSERT_ERRNO_SUCCESS(); |
31 | ASSERT_GT(fd, 0); |
32 | ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0)); |
33 | |
34 | ASSERT_THAT(LIBC_NAMESPACE::access(TEST_FILE, F_OK), Succeeds(0)); |
35 | ASSERT_THAT(LIBC_NAMESPACE::remove(TEST_FILE), Succeeds(0)); |
36 | ASSERT_THAT(LIBC_NAMESPACE::access(TEST_FILE, F_OK), Fails(ENOENT)); |
37 | } |
38 | |
39 | TEST(LlvmLibcRemoveTest, CreateAndRemoveDir) { |
40 | // The test strategy is to create a dir and remove it, and also verify that |
41 | // it was removed. |
42 | LIBC_NAMESPACE::libc_errno = 0; |
43 | using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails; |
44 | using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds; |
45 | constexpr const char *FILENAME = "remove.test.dir" ; |
46 | auto TEST_DIR = libc_make_test_file_path(FILENAME); |
47 | ASSERT_THAT(LIBC_NAMESPACE::mkdirat(AT_FDCWD, TEST_DIR, S_IRWXU), |
48 | Succeeds(0)); |
49 | |
50 | ASSERT_THAT(LIBC_NAMESPACE::access(TEST_DIR, F_OK), Succeeds(0)); |
51 | ASSERT_THAT(LIBC_NAMESPACE::remove(TEST_DIR), Succeeds(0)); |
52 | ASSERT_THAT(LIBC_NAMESPACE::access(TEST_DIR, F_OK), Fails(ENOENT)); |
53 | } |
54 | |
55 | TEST(LlvmLibcRemoveTest, RemoveNonExistent) { |
56 | using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails; |
57 | ASSERT_THAT(LIBC_NAMESPACE::remove("non-existent" ), Fails(ENOENT)); |
58 | } |
59 | |