[flang] Accept polymorphic component element in storage_size
[llvm-project.git] / libc / test / src / sys / stat / stat_test.cpp
blobba6e302465627e5d4e0887fb95ea593662002e7d
1 //===-- Unittests for stat ------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
9 #include "src/fcntl/open.h"
10 #include "src/sys/stat/stat.h"
11 #include "src/unistd/close.h"
12 #include "src/unistd/unlink.h"
13 #include "test/ErrnoSetterMatcher.h"
14 #include "test/UnitTest/Test.h"
15 #include "utils/testutils/FDReader.h"
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <sys/stat.h>
21 TEST(LlvmLibcStatTest, CreatAndReadMode) {
22 using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
23 using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
25 // The test file is initially writable. We open it for writing and ensure
26 // that it indeed can be opened for writing. Next, we close the file and
27 // make it readonly using chmod. We test that chmod actually succeeded by
28 // trying to open the file for writing and failing.
29 constexpr const char *TEST_FILE = "testdata/stat.test";
30 errno = 0;
32 int fd = __llvm_libc::open(TEST_FILE, O_CREAT | O_WRONLY, S_IRWXU);
33 ASSERT_GT(fd, 0);
34 ASSERT_EQ(errno, 0);
35 ASSERT_THAT(__llvm_libc::close(fd), Succeeds(0));
37 struct stat statbuf;
38 ASSERT_THAT(__llvm_libc::stat(TEST_FILE, &statbuf), Succeeds(0));
40 ASSERT_EQ(int(statbuf.st_mode), int(S_IRWXU | S_IFREG));
42 ASSERT_THAT(__llvm_libc::unlink(TEST_FILE), Succeeds(0));
45 TEST(LlvmLibcStatTest, NonExistentFile) {
46 errno = 0;
47 using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
48 struct stat statbuf;
49 ASSERT_THAT(__llvm_libc::stat("non-existent-file", &statbuf), Fails(ENOENT));
50 errno = 0;