59 lines · cpp
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-exception6//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/ErrnoCheckingTest.h"15#include "test/UnitTest/ErrnoSetterMatcher.h"16#include "test/UnitTest/Test.h"17 18#include <unistd.h>19 20using LlvmLibcRemoveTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;21 22TEST_F(LlvmLibcRemoveTest, CreateAndRemoveFile) {23 // The test strategy is to create a file and remove it, and also verify that24 // it was removed.25 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;26 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;27 28 constexpr const char *FILENAME = APPEND_LIBC_TEST("remove.test.file");29 auto TEST_FILE = libc_make_test_file_path(FILENAME);30 int fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);31 ASSERT_ERRNO_SUCCESS();32 ASSERT_GT(fd, 0);33 ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));34 35 ASSERT_THAT(LIBC_NAMESPACE::access(TEST_FILE, F_OK), Succeeds(0));36 ASSERT_THAT(LIBC_NAMESPACE::remove(TEST_FILE), Succeeds(0));37 ASSERT_THAT(LIBC_NAMESPACE::access(TEST_FILE, F_OK), Fails(ENOENT));38}39 40TEST_F(LlvmLibcRemoveTest, CreateAndRemoveDir) {41 // The test strategy is to create a dir and remove it, and also verify that42 // it was removed.43 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;44 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;45 constexpr const char *FILENAME = APPEND_LIBC_TEST("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 55TEST(LlvmLibcRemoveTest, RemoveNonExistent) {56 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;57 ASSERT_THAT(LIBC_NAMESPACE::remove("non-existent"), Fails(ENOENT));58}59