[flang] Accept polymorphic component element in storage_size
[llvm-project.git] / libc / test / src / unistd / access_test.cpp
blob30c3cb4572c19a2c226b9fe42a4f8611d19e5686
1 //===-- Unittests for access ----------------------------------------------===//
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/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"
17 #include <errno.h>
18 #include <unistd.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 errno = 0;
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);
27 ASSERT_EQ(errno, 0);
28 ASSERT_GT(fd, 0);
29 ASSERT_THAT(__llvm_libc::close(fd), Succeeds(0));
31 ASSERT_EQ(__llvm_libc::access(TEST_FILE, F_OK), 0);
32 ASSERT_EQ(errno, 0);
33 ASSERT_EQ(__llvm_libc::access(TEST_FILE, X_OK | W_OK | R_OK), 0);
34 ASSERT_EQ(errno, 0);
35 ASSERT_THAT(__llvm_libc::unlink(TEST_FILE), Succeeds(0));
37 fd = __llvm_libc::open(TEST_FILE, O_WRONLY | O_CREAT, S_IXUSR);
38 ASSERT_EQ(errno, 0);
39 ASSERT_GT(fd, 0);
40 ASSERT_THAT(__llvm_libc::close(fd), Succeeds(0));
41 ASSERT_EQ(__llvm_libc::access(TEST_FILE, F_OK), 0);
42 ASSERT_EQ(errno, 0);
43 ASSERT_EQ(__llvm_libc::access(TEST_FILE, X_OK), 0);
44 ASSERT_EQ(errno, 0);
45 ASSERT_EQ(__llvm_libc::access(TEST_FILE, R_OK), -1);
46 ASSERT_EQ(errno, EACCES);
47 errno = 0;
48 ASSERT_EQ(__llvm_libc::access(TEST_FILE, W_OK), -1);
49 ASSERT_EQ(errno, EACCES);
50 errno = 0;
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),
57 Fails(ENOENT));