52 lines · cpp
1//===-- Unittests for chown -----------------------------------------------===//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/unistd/chown.h"11#include "src/unistd/close.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 23using LlvmLibcChownTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;24 25TEST_F(LlvmLibcChownTest, ChownSuccess) {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 = "chown.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::chown(TEST_FILE, 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 47TEST_F(LlvmLibcChownTest, ChownNonExistentFile) {48 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;49 ASSERT_THAT(LIBC_NAMESPACE::chown("non-existent-file", 1000, 1000),50 Fails(ENOENT));51}52