1 //===-- Unittests for stat ------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "src/errno/libc_errno.h"
10 #include "src/fcntl/open.h"
11 #include "src/sys/stat/stat.h"
12 #include "src/unistd/close.h"
13 #include "src/unistd/unlink.h"
14 #include "test/UnitTest/ErrnoSetterMatcher.h"
15 #include "test/UnitTest/Test.h"
17 #include "hdr/fcntl_macros.h"
20 TEST(LlvmLibcStatTest
, CreatAndReadMode
) {
21 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails
;
22 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds
;
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/stat.test";
29 LIBC_NAMESPACE::libc_errno
= 0;
31 int fd
= LIBC_NAMESPACE::open(TEST_FILE
, O_CREAT
| O_WRONLY
, S_IRWXU
);
33 ASSERT_ERRNO_SUCCESS();
34 ASSERT_THAT(LIBC_NAMESPACE::close(fd
), Succeeds(0));
37 ASSERT_THAT(LIBC_NAMESPACE::stat(TEST_FILE
, &statbuf
), Succeeds(0));
39 ASSERT_EQ(int(statbuf
.st_mode
), int(S_IRWXU
| S_IFREG
));
41 ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE
), Succeeds(0));
44 TEST(LlvmLibcStatTest
, NonExistentFile
) {
45 LIBC_NAMESPACE::libc_errno
= 0;
46 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails
;
48 ASSERT_THAT(LIBC_NAMESPACE::stat("non-existent-file", &statbuf
),
50 LIBC_NAMESPACE::libc_errno
= 0;