1 //===-- Unittests for remove ----------------------------------------------===//
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/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"
17 #include "src/errno/libc_errno.h"
20 TEST(LlvmLibcRemoveTest
, CreateAndRemoveFile
) {
21 // The test strategy is to create a file and remove it, and also verify that
23 LIBC_NAMESPACE::libc_errno
= 0;
24 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails
;
25 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds
;
27 constexpr const char *FILENAME
= "remove.test.file";
28 auto TEST_FILE
= libc_make_test_file_path(FILENAME
);
29 int fd
= LIBC_NAMESPACE::open(TEST_FILE
, O_WRONLY
| O_CREAT
, S_IRWXU
);
30 ASSERT_ERRNO_SUCCESS();
32 ASSERT_THAT(LIBC_NAMESPACE::close(fd
), Succeeds(0));
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
));
39 TEST(LlvmLibcRemoveTest
, CreateAndRemoveDir
) {
40 // The test strategy is to create a dir and remove it, and also verify that
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
),
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
));
55 TEST(LlvmLibcRemoveTest
, RemoveNonExistent
) {
56 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails
;
57 ASSERT_THAT(LIBC_NAMESPACE::remove("non-existent"), Fails(ENOENT
));