1 //===-- Unittests for access ----------------------------------------------===//
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/fcntl/open.h"
10 #include "src/unistd/access.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"
20 TEST(LlvmLibcAccessTest
, CreateAndTest
) {
21 // The test strategy is to repeatedly create a file in different modes and
22 // test that it is accessable in those modes but not in others.
24 using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds
;
25 constexpr const char *TEST_FILE
= "testdata/access.test";
26 int fd
= __llvm_libc::open(TEST_FILE
, O_WRONLY
| O_CREAT
, S_IRWXU
);
29 ASSERT_THAT(__llvm_libc::close(fd
), Succeeds(0));
31 ASSERT_EQ(__llvm_libc::access(TEST_FILE
, F_OK
), 0);
33 ASSERT_EQ(__llvm_libc::access(TEST_FILE
, X_OK
| W_OK
| R_OK
), 0);
35 ASSERT_THAT(__llvm_libc::unlink(TEST_FILE
), Succeeds(0));
37 fd
= __llvm_libc::open(TEST_FILE
, O_WRONLY
| O_CREAT
, S_IXUSR
);
40 ASSERT_THAT(__llvm_libc::close(fd
), Succeeds(0));
41 ASSERT_EQ(__llvm_libc::access(TEST_FILE
, F_OK
), 0);
43 ASSERT_EQ(__llvm_libc::access(TEST_FILE
, X_OK
), 0);
45 ASSERT_EQ(__llvm_libc::access(TEST_FILE
, R_OK
), -1);
46 ASSERT_EQ(errno
, EACCES
);
48 ASSERT_EQ(__llvm_libc::access(TEST_FILE
, W_OK
), -1);
49 ASSERT_EQ(errno
, EACCES
);
51 ASSERT_THAT(__llvm_libc::unlink(TEST_FILE
), Succeeds(0));
54 TEST(LlvmLibcAccessTest
, AccessNonExistentFile
) {
55 using __llvm_libc::testing::ErrnoSetterMatcher::Fails
;
56 ASSERT_THAT(__llvm_libc::access("testdata/non-existent-file", F_OK
),