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/errno/libc_errno.h"
10 #include "src/fcntl/open.h"
11 #include "src/unistd/access.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"
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.
23 LIBC_NAMESPACE::libc_errno
= 0;
24 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds
;
25 constexpr const char *FILENAME
= "access.test";
26 auto TEST_FILE
= libc_make_test_file_path(FILENAME
);
27 int fd
= LIBC_NAMESPACE::open(TEST_FILE
, O_WRONLY
| O_CREAT
, S_IRWXU
);
28 ASSERT_ERRNO_SUCCESS();
30 ASSERT_THAT(LIBC_NAMESPACE::close(fd
), Succeeds(0));
32 ASSERT_EQ(LIBC_NAMESPACE::access(TEST_FILE
, F_OK
), 0);
33 ASSERT_ERRNO_SUCCESS();
34 ASSERT_EQ(LIBC_NAMESPACE::access(TEST_FILE
, X_OK
| W_OK
| R_OK
), 0);
35 ASSERT_ERRNO_SUCCESS();
36 ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE
), Succeeds(0));
38 fd
= LIBC_NAMESPACE::open(TEST_FILE
, O_WRONLY
| O_CREAT
, S_IXUSR
);
39 ASSERT_ERRNO_SUCCESS();
41 ASSERT_THAT(LIBC_NAMESPACE::close(fd
), Succeeds(0));
42 ASSERT_EQ(LIBC_NAMESPACE::access(TEST_FILE
, F_OK
), 0);
43 ASSERT_ERRNO_SUCCESS();
44 ASSERT_EQ(LIBC_NAMESPACE::access(TEST_FILE
, X_OK
), 0);
45 ASSERT_ERRNO_SUCCESS();
46 ASSERT_EQ(LIBC_NAMESPACE::access(TEST_FILE
, R_OK
), -1);
47 ASSERT_ERRNO_EQ(EACCES
);
48 LIBC_NAMESPACE::libc_errno
= 0;
49 ASSERT_EQ(LIBC_NAMESPACE::access(TEST_FILE
, W_OK
), -1);
50 ASSERT_ERRNO_EQ(EACCES
);
51 LIBC_NAMESPACE::libc_errno
= 0;
52 ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE
), Succeeds(0));
55 TEST(LlvmLibcAccessTest
, AccessNonExistentFile
) {
56 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails
;
57 ASSERT_THAT(LIBC_NAMESPACE::access("testdata/non-existent-file", F_OK
),