1e310f8bdSSiva Chandra Reddy //===-- Unittests for stat ------------------------------------------------===// 2e310f8bdSSiva Chandra Reddy // 3e310f8bdSSiva Chandra Reddy // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4e310f8bdSSiva Chandra Reddy // See https://llvm.org/LICENSE.txt for license information. 5e310f8bdSSiva Chandra Reddy // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6e310f8bdSSiva Chandra Reddy // 7e310f8bdSSiva Chandra Reddy //===----------------------------------------------------------------------===// 8e310f8bdSSiva Chandra Reddy 9af783db7SSiva Chandra Reddy #include "src/errno/libc_errno.h" 10e310f8bdSSiva Chandra Reddy #include "src/fcntl/open.h" 11e310f8bdSSiva Chandra Reddy #include "src/sys/stat/stat.h" 12e310f8bdSSiva Chandra Reddy #include "src/unistd/close.h" 13e310f8bdSSiva Chandra Reddy #include "src/unistd/unlink.h" 144f1fe19dSSiva Chandra Reddy #include "test/UnitTest/ErrnoSetterMatcher.h" 15af1315c2SSiva Chandra Reddy #include "test/UnitTest/Test.h" 16e310f8bdSSiva Chandra Reddy 17*abc49cc1SJob Henandez Lara #include "hdr/fcntl_macros.h" 18e310f8bdSSiva Chandra Reddy #include <sys/stat.h> 19e310f8bdSSiva Chandra Reddy 20e310f8bdSSiva Chandra Reddy TEST(LlvmLibcStatTest, CreatAndReadMode) { 21b6bc9d72SGuillaume Chatelet using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails; 22b6bc9d72SGuillaume Chatelet using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds; 23e310f8bdSSiva Chandra Reddy 24e310f8bdSSiva Chandra Reddy // The test file is initially writable. We open it for writing and ensure 25e310f8bdSSiva Chandra Reddy // that it indeed can be opened for writing. Next, we close the file and 26e310f8bdSSiva Chandra Reddy // make it readonly using chmod. We test that chmod actually succeeded by 27e310f8bdSSiva Chandra Reddy // trying to open the file for writing and failing. 28e310f8bdSSiva Chandra Reddy constexpr const char *TEST_FILE = "testdata/stat.test"; 293eb1e6d8Smichaelrj-google LIBC_NAMESPACE::libc_errno = 0; 30e310f8bdSSiva Chandra Reddy 31b6bc9d72SGuillaume Chatelet int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_WRONLY, S_IRWXU); 32e310f8bdSSiva Chandra Reddy ASSERT_GT(fd, 0); 3373874f7aSGuillaume Chatelet ASSERT_ERRNO_SUCCESS(); 34b6bc9d72SGuillaume Chatelet ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0)); 35e310f8bdSSiva Chandra Reddy 36e310f8bdSSiva Chandra Reddy struct stat statbuf; 37b6bc9d72SGuillaume Chatelet ASSERT_THAT(LIBC_NAMESPACE::stat(TEST_FILE, &statbuf), Succeeds(0)); 38e310f8bdSSiva Chandra Reddy 39e310f8bdSSiva Chandra Reddy ASSERT_EQ(int(statbuf.st_mode), int(S_IRWXU | S_IFREG)); 40e310f8bdSSiva Chandra Reddy 41b6bc9d72SGuillaume Chatelet ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0)); 42e310f8bdSSiva Chandra Reddy } 43e310f8bdSSiva Chandra Reddy 44e310f8bdSSiva Chandra Reddy TEST(LlvmLibcStatTest, NonExistentFile) { 453eb1e6d8Smichaelrj-google LIBC_NAMESPACE::libc_errno = 0; 46b6bc9d72SGuillaume Chatelet using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails; 47e310f8bdSSiva Chandra Reddy struct stat statbuf; 48b6bc9d72SGuillaume Chatelet ASSERT_THAT(LIBC_NAMESPACE::stat("non-existent-file", &statbuf), 49b6bc9d72SGuillaume Chatelet Fails(ENOENT)); 503eb1e6d8Smichaelrj-google LIBC_NAMESPACE::libc_errno = 0; 51e310f8bdSSiva Chandra Reddy } 52