[flang] Accept polymorphic component element in storage_size
[llvm-project.git] / libc / test / src / unistd / syscall_test.cpp
blob7d0cc309806bf4eb4340aa58d4afdae04ed4370a
1 //===-- Unittests for syscalls --------------------------------------------===//
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/unistd/syscall.h"
10 #include "test/ErrnoSetterMatcher.h"
11 #include "test/UnitTest/Test.h"
13 #include <errno.h>
14 #include <fcntl.h>
15 #include <sys/syscall.h> // For syscall numbers.
16 #include <unistd.h>
18 using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
20 // We only do a smoke test here. Actual functionality tests are
21 // done by the unit tests of the syscall wrappers like mmap.
22 // The goal is to test syscalls with a wide number of args.
24 // There is no function named "syscall" in llvm-libc, we instead use a macro to
25 // set up the arguments properly. We still need to specify the namespace though
26 // because the macro generates a call to the actual internal function
27 // (__llvm_libc_syscall) which is inside the namespace.
28 TEST(LlvmLibcSyscallTest, TrivialCall) {
29 errno = 0;
31 ASSERT_GE(__llvm_libc::syscall(SYS_gettid), 0l);
32 ASSERT_EQ(errno, 0);
35 TEST(LlvmLibcSyscallTest, SymlinkCreateDestroy) {
36 constexpr const char LINK_VAL[] = "syscall_readlink_test_value";
37 constexpr const char LINK[] = "testdata/syscall_readlink.test.link";
39 ASSERT_GE(__llvm_libc::syscall(SYS_symlink, LINK_VAL, LINK), 0l);
40 ASSERT_EQ(errno, 0);
42 char buf[sizeof(LINK_VAL)];
44 ASSERT_GE(__llvm_libc::syscall(SYS_readlink, LINK, buf, sizeof(buf)), 0l);
45 ASSERT_EQ(errno, 0);
47 ASSERT_GE(__llvm_libc::syscall(SYS_unlink, LINK), 0l);
48 ASSERT_EQ(errno, 0);
51 TEST(LlvmLibcSyscallTest, FileReadWrite) {
52 constexpr const char HELLO[] = "hello";
53 constexpr int HELLO_SIZE = sizeof(HELLO);
55 constexpr const char *TEST_FILE = "testdata/syscall_pread_pwrite.test";
57 int fd =
58 __llvm_libc::syscall(SYS_open, TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
59 ASSERT_GT(fd, 0);
60 ASSERT_EQ(errno, 0);
62 ASSERT_GE(__llvm_libc::syscall(SYS_pwrite64, fd, HELLO, HELLO_SIZE, 0), 0l);
63 ASSERT_EQ(errno, 0);
65 ASSERT_GE(__llvm_libc::syscall(SYS_fsync, fd), 0l);
66 ASSERT_EQ(errno, 0);
68 ASSERT_GE(__llvm_libc::syscall(SYS_close, fd), 0l);
69 ASSERT_EQ(errno, 0);
72 TEST(LlvmLibcSyscallTest, FileLinkCreateDestroy) {
73 constexpr const char *TEST_DIR = "testdata";
74 constexpr const char *TEST_FILE = "syscall_linkat.test";
75 constexpr const char *TEST_FILE_PATH = "testdata/syscall_linkat.test";
76 constexpr const char *TEST_FILE_LINK = "syscall_linkat.test.link";
77 constexpr const char *TEST_FILE_LINK_PATH =
78 "testdata/syscall_linkat.test.link";
80 // The test strategy is as follows:
81 // 1. Create a normal file
82 // 2. Create a link to that file.
83 // 3. Open the link to check that the link was created.
84 // 4. Cleanup the file and its link.
86 int write_fd = __llvm_libc::syscall(SYS_open, TEST_FILE_PATH,
87 O_WRONLY | O_CREAT, S_IRWXU);
88 ASSERT_GT(write_fd, 0);
89 ASSERT_EQ(errno, 0);
91 ASSERT_GE(__llvm_libc::syscall(SYS_close, write_fd), 0l);
92 ASSERT_EQ(errno, 0);
94 int dir_fd = __llvm_libc::syscall(SYS_open, TEST_DIR, O_DIRECTORY, 0);
95 ASSERT_GT(dir_fd, 0);
96 ASSERT_EQ(errno, 0);
98 ASSERT_GE(__llvm_libc::syscall(SYS_linkat, dir_fd, TEST_FILE, dir_fd,
99 TEST_FILE_LINK, 0),
100 0l);
101 ASSERT_EQ(errno, 0);
103 int link_fd = __llvm_libc::syscall(SYS_open, TEST_FILE_LINK_PATH, O_PATH, 0);
104 ASSERT_GT(link_fd, 0);
105 ASSERT_EQ(errno, 0);
107 ASSERT_GE(__llvm_libc::syscall(SYS_unlink, TEST_FILE_PATH), 0l);
108 ASSERT_EQ(errno, 0);
110 ASSERT_GE(__llvm_libc::syscall(SYS_unlink, TEST_FILE_LINK_PATH), 0l);
111 ASSERT_EQ(errno, 0);
113 ASSERT_GE(__llvm_libc::syscall(SYS_close, dir_fd), 0l);
114 ASSERT_EQ(errno, 0);