1//===-- Unittests for unlink ----------------------------------------------===//
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/errno/libc_errno.h"
10#include "src/fcntl/open.h"
11#include "src/unistd/close.h"
12#include "src/unistd/unlink.h"
13#include "test/UnitTest/ErrnoSetterMatcher.h"
14#include "test/UnitTest/Test.h"
15
16#include <sys/stat.h>
17
18TEST(LlvmLibcUnlinkTest, CreateAndUnlink) {
19 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
20 constexpr const char *FILENAME = "unlink.test";
21 auto TEST_FILE = libc_make_test_file_path(FILENAME);
22 int write_fd = LIBC_NAMESPACE::open(path: TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
23 ASSERT_ERRNO_SUCCESS();
24 ASSERT_GT(write_fd, 0);
25 ASSERT_THAT(LIBC_NAMESPACE::close(write_fd), Succeeds(0));
26 ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0));
27}
28
29TEST(LlvmLibcUnlinkTest, UnlinkNonExistentFile) {
30 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
31 ASSERT_THAT(LIBC_NAMESPACE::unlink("non-existent-file"), Fails(ENOENT));
32}
33

source code of libc/test/src/unistd/unlink_test.cpp