1 //===-- Unittests for syscalls --------------------------------------------===//
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/unistd/syscall.h"
10 #include "test/ErrnoSetterMatcher.h"
11 #include "test/UnitTest/Test.h"
15 #include <sys/syscall.h> // For syscall numbers.
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
) {
31 ASSERT_GE(__llvm_libc::syscall(SYS_gettid
), 0l);
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);
42 char buf
[sizeof(LINK_VAL
)];
44 ASSERT_GE(__llvm_libc::syscall(SYS_readlink
, LINK
, buf
, sizeof(buf
)), 0l);
47 ASSERT_GE(__llvm_libc::syscall(SYS_unlink
, LINK
), 0l);
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";
58 __llvm_libc::syscall(SYS_open
, TEST_FILE
, O_WRONLY
| O_CREAT
, S_IRWXU
);
62 ASSERT_GE(__llvm_libc::syscall(SYS_pwrite64
, fd
, HELLO
, HELLO_SIZE
, 0), 0l);
65 ASSERT_GE(__llvm_libc::syscall(SYS_fsync
, fd
), 0l);
68 ASSERT_GE(__llvm_libc::syscall(SYS_close
, fd
), 0l);
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);
91 ASSERT_GE(__llvm_libc::syscall(SYS_close
, write_fd
), 0l);
94 int dir_fd
= __llvm_libc::syscall(SYS_open
, TEST_DIR
, O_DIRECTORY
, 0);
98 ASSERT_GE(__llvm_libc::syscall(SYS_linkat
, dir_fd
, TEST_FILE
, dir_fd
,
103 int link_fd
= __llvm_libc::syscall(SYS_open
, TEST_FILE_LINK_PATH
, O_PATH
, 0);
104 ASSERT_GT(link_fd
, 0);
107 ASSERT_GE(__llvm_libc::syscall(SYS_unlink
, TEST_FILE_PATH
), 0l);
110 ASSERT_GE(__llvm_libc::syscall(SYS_unlink
, TEST_FILE_LINK_PATH
), 0l);
113 ASSERT_GE(__llvm_libc::syscall(SYS_close
, dir_fd
), 0l);