diff options
| author | Siva Chandra Reddy <sivachandra@google.com> | 2022-09-09 19:02:28 +0000 |
|---|---|---|
| committer | Siva Chandra Reddy <sivachandra@google.com> | 2022-09-21 18:35:02 +0000 |
| commit | e310f8bddf6ab87459df3f1172e18543c86e23b7 (patch) | |
| tree | 98f97c8f79b7d40050f350d1e7c5c65fa47c8163 /libc/test/src/sys/stat/fstat_test.cpp | |
| parent | f6e7ad56040ee8531be3375e04312d30700a58b2 (diff) | |
[libc] Add implementation of functions stat, fstat and lstat.
All supporting type and macro definitions have also been added.
Reviewed By: lntue
Differential Revision: https://reviews.llvm.org/D134262
Diffstat (limited to 'libc/test/src/sys/stat/fstat_test.cpp')
| -rw-r--r-- | libc/test/src/sys/stat/fstat_test.cpp | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/libc/test/src/sys/stat/fstat_test.cpp b/libc/test/src/sys/stat/fstat_test.cpp new file mode 100644 index 000000000000..d05f31d35aa2 --- /dev/null +++ b/libc/test/src/sys/stat/fstat_test.cpp @@ -0,0 +1,51 @@ +//===-- Unittests for fstat -----------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/fcntl/open.h" +#include "src/sys/stat/fstat.h" +#include "src/unistd/close.h" +#include "src/unistd/unlink.h" +#include "test/ErrnoSetterMatcher.h" +#include "utils/UnitTest/Test.h" +#include "utils/testutils/FDReader.h" + +#include <errno.h> +#include <fcntl.h> +#include <sys/stat.h> + +TEST(LlvmLibcFStatTest, CreatAndReadMode) { + using __llvm_libc::testing::ErrnoSetterMatcher::Fails; + using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds; + + // The test file is initially writable. We open it for writing and ensure + // that it indeed can be opened for writing. Next, we close the file and + // make it readonly using chmod. We test that chmod actually succeeded by + // trying to open the file for writing and failing. + constexpr const char *TEST_FILE = "testdata/fstat.test"; + errno = 0; + + int fd = __llvm_libc::open(TEST_FILE, O_CREAT | O_WRONLY, S_IRWXU); + ASSERT_GT(fd, 0); + ASSERT_EQ(errno, 0); + + struct stat statbuf; + ASSERT_THAT(__llvm_libc::fstat(fd, &statbuf), Succeeds(0)); + + ASSERT_EQ(int(statbuf.st_mode), int(S_IRWXU | S_IFREG)); + + ASSERT_THAT(__llvm_libc::close(fd), Succeeds(0)); + ASSERT_THAT(__llvm_libc::unlink(TEST_FILE), Succeeds(0)); +} + +TEST(LlvmLibcFStatTest, NonExistentFile) { + errno = 0; + using __llvm_libc::testing::ErrnoSetterMatcher::Fails; + struct stat statbuf; + ASSERT_THAT(__llvm_libc::fstat(-1, &statbuf), Fails(EBADF)); + errno = 0; +} |
