| 1 | //===-- Unittests for fstat -----------------------------------------------===// |
| 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/sys/stat/fstat.h" |
| 11 | #include "src/unistd/close.h" |
| 12 | #include "src/unistd/unlink.h" |
| 13 | #include "test/UnitTest/ErrnoCheckingTest.h" |
| 14 | #include "test/UnitTest/ErrnoSetterMatcher.h" |
| 15 | #include "test/UnitTest/Test.h" |
| 16 | |
| 17 | #include "hdr/fcntl_macros.h" |
| 18 | #include <sys/stat.h> |
| 19 | |
| 20 | using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher; |
| 21 | using LlvmLibcFStatTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest; |
| 22 | |
| 23 | TEST_F(LlvmLibcFStatTest, CreatAndReadMode) { |
| 24 | // The test file is initially writable. We open it for writing and ensure |
| 25 | // that it indeed can be opened for writing. Next, we close the file and |
| 26 | // make it readonly using chmod. We test that chmod actually succeeded by |
| 27 | // trying to open the file for writing and failing. |
| 28 | constexpr const char *TEST_FILE = "testdata/fstat.test" ; |
| 29 | |
| 30 | int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_WRONLY, S_IRWXU); |
| 31 | ASSERT_GT(fd, 0); |
| 32 | ASSERT_ERRNO_SUCCESS(); |
| 33 | |
| 34 | struct stat statbuf; |
| 35 | ASSERT_THAT(LIBC_NAMESPACE::fstat(fd, &statbuf), Succeeds(0)); |
| 36 | |
| 37 | ASSERT_EQ(int(statbuf.st_mode), int(S_IRWXU | S_IFREG)); |
| 38 | |
| 39 | ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0)); |
| 40 | ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0)); |
| 41 | } |
| 42 | |
| 43 | TEST_F(LlvmLibcFStatTest, NonExistentFile) { |
| 44 | struct stat statbuf; |
| 45 | ASSERT_THAT(LIBC_NAMESPACE::fstat(-1, &statbuf), Fails(EBADF)); |
| 46 | } |
| 47 | |