|
| 1 | +//===-- Unittests for fchown ----------------------------------------------===// |
| 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/unistd/close.h" |
| 11 | +#include "src/unistd/fchown.h" |
| 12 | +#include "src/unistd/getgid.h" |
| 13 | +#include "src/unistd/getuid.h" |
| 14 | +#include "src/unistd/unlink.h" |
| 15 | + |
| 16 | +#include "test/UnitTest/ErrnoCheckingTest.h" |
| 17 | +#include "test/UnitTest/ErrnoSetterMatcher.h" |
| 18 | +#include "test/UnitTest/Test.h" |
| 19 | + |
| 20 | +#include "hdr/fcntl_macros.h" |
| 21 | +#include <sys/stat.h> |
| 22 | + |
| 23 | +using LlvmLibcFchownTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest; |
| 24 | + |
| 25 | +TEST_F(LlvmLibcFchownTest, FchownSuccess) { |
| 26 | + using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds; |
| 27 | + uid_t my_uid = LIBC_NAMESPACE::getuid(); |
| 28 | + gid_t my_gid = LIBC_NAMESPACE::getgid(); |
| 29 | + constexpr const char *FILENAME = "fchown.test"; |
| 30 | + auto TEST_FILE = libc_make_test_file_path(FILENAME); |
| 31 | + |
| 32 | + // Create a test file. |
| 33 | + int write_fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU); |
| 34 | + ASSERT_ERRNO_SUCCESS(); |
| 35 | + ASSERT_GT(write_fd, 0); |
| 36 | + |
| 37 | + // Change the ownership of the file. |
| 38 | + ASSERT_THAT(LIBC_NAMESPACE::fchown(write_fd, my_uid, my_gid), Succeeds(0)); |
| 39 | + |
| 40 | + // Close the file descriptor. |
| 41 | + ASSERT_THAT(LIBC_NAMESPACE::close(write_fd), Succeeds(0)); |
| 42 | + |
| 43 | + // Clean up the test file. |
| 44 | + ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0)); |
| 45 | +} |
| 46 | + |
| 47 | +TEST_F(LlvmLibcFchownTest, FchownInvalidFileDescriptor) { |
| 48 | + using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails; |
| 49 | + ASSERT_THAT(LIBC_NAMESPACE::fchown(-1, 1000, 1000), Fails(EBADF)); |
| 50 | +} |
0 commit comments