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

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