From f30bd698d1b3d91452b3dd4311c8d15ebb4caeb8 Mon Sep 17 00:00:00 2001 From: Ruihan Li Date: Sun, 14 Dec 2025 22:07:07 +0800 Subject: [PATCH] Define `__asterinas__` macro in tests --- test/src/apps/file_io/access_err.c | 24 ++++++++++++----- test/src/apps/mmap/mmap_and_mremap.c | 8 ++++-- test/src/apps/namespace/setns.c | 23 +++++++++-------- test/src/apps/network/netlink_route.c | 6 ++++- test/src/apps/network/tcp_err.c | 15 ++++++----- test/src/apps/network/tcp_poll.c | 8 +++--- test/src/apps/network/tcp_reuseaddr.c | 21 +++++++++------ test/src/apps/network/udp_broadcast.c | 37 +++++++++++++++++---------- test/src/apps/process/group_session.c | 5 ++-- test/src/apps/process/wait4.c | 9 ++++--- test/src/apps/signal_c/signal_test2.c | 9 +------ 11 files changed, 100 insertions(+), 65 deletions(-) diff --git a/test/src/apps/file_io/access_err.c b/test/src/apps/file_io/access_err.c index e1a3882ab..adcac5ecd 100644 --- a/test/src/apps/file_io/access_err.c +++ b/test/src/apps/file_io/access_err.c @@ -181,9 +181,15 @@ FN_TEST(path) TEST_ERRNO(mmap(NULL, PAGE_SIZE, PROT_READ, MAP_SHARED, fd, 0), EBADF); // FIXME: Asterinas reports `EACCES` because it performs the permission check first. - // TEST_ERRNO(mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, - // 0), - // EBADF); +#ifdef __asterinas__ + TEST_ERRNO(mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, + 0), + EACCES); +#else + TEST_ERRNO(mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, + 0), + EBADF); +#endif TEST_ERRNO(flock(fd, LOCK_SH), EBADF); TEST_ERRNO(flock(fd, LOCK_UN), EBADF); @@ -214,9 +220,15 @@ FN_TEST(path) TEST_ERRNO(mmap(NULL, PAGE_SIZE, PROT_READ, MAP_SHARED, fd, 0), EBADF); // FIXME: Asterinas reports `EACCES` because it performs the permission check first. - // TEST_ERRNO(mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, - // 0), - // EBADF); +#ifdef __asterinas__ + TEST_ERRNO(mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, + 0), + EACCES); +#else + TEST_ERRNO(mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, + 0), + EBADF); +#endif TEST_ERRNO(flock(fd, LOCK_SH), EBADF); TEST_ERRNO(flock(fd, LOCK_UN), EBADF); diff --git a/test/src/apps/mmap/mmap_and_mremap.c b/test/src/apps/mmap/mmap_and_mremap.c index 2811fed30..eeed68cae 100644 --- a/test/src/apps/mmap/mmap_and_mremap.c +++ b/test/src/apps/mmap/mmap_and_mremap.c @@ -42,8 +42,12 @@ FN_TEST(mremap) TEST_ERRNO(mremap(addr, 0, PAGE_SIZE, 0), EINVAL); // There is no enough room to expand the mapping. - // FIXME: Asterinas returns EACCESS here, which is not a correct error code. - // TEST_ERRNO(mremap(addr, PAGE_SIZE, 2 * PAGE_SIZE, 0), ENOMEM); + // FIXME: Asterinas returns EACCES here, which is not a correct error code. +#ifdef __asterinas__ + TEST_ERRNO(mremap(addr, PAGE_SIZE, 2 * PAGE_SIZE, 0), EACCES); +#else + TEST_ERRNO(mremap(addr, PAGE_SIZE, 2 * PAGE_SIZE, 0), ENOMEM); +#endif char *hole = addr + 2 * PAGE_SIZE; diff --git a/test/src/apps/namespace/setns.c b/test/src/apps/namespace/setns.c index 1c4b1234b..843f9ba2e 100644 --- a/test/src/apps/namespace/setns.c +++ b/test/src/apps/namespace/setns.c @@ -3,10 +3,7 @@ #define _GNU_SOURCE #include #include -#include -#include #include -#include #include #include "../test.h" @@ -15,10 +12,12 @@ FN_TEST(set_ns_empty_flags) { // FIXME: The following test will fail on Asterinas // because it currently does not support ns file. - // const char *ns_path = "/proc/self/ns/user"; - // int fd_ns = TEST_SUCC(open(ns_path, O_RDONLY)); - // TEST_ERRNO(setns(fd_ns, 0), EINVAL); - // TEST_SUCC(close(fd_ns)); +#ifndef __asterinas__ + const char *ns_path = "/proc/self/ns/user"; + int fd_ns = TEST_SUCC(open(ns_path, O_RDONLY)); + TEST_ERRNO(setns(fd_ns, 0), EINVAL); + TEST_SUCC(close(fd_ns)); +#endif pid_t pid = getpid(); int pidfd = TEST_SUCC(syscall(SYS_pidfd_open, pid, 0)); @@ -33,10 +32,12 @@ FN_TEST(set_self_ns) // current user namespace. This is different from other namespaces. // FIXME: The following test will fail on Asterinas // because it currently does not support ns file. - // const char *ns_path = "/proc/self/ns/user"; - // int fd_ns = TEST_SUCC(open(ns_path, O_RDONLY)); - // TEST_ERRNO(setns(fd_ns, CLONE_NEWUSER), EINVAL); - // TEST_SUCC(close(fd_ns)); +#ifndef __asterinas__ + const char *ns_path = "/proc/self/ns/user"; + int fd_ns = TEST_SUCC(open(ns_path, O_RDONLY)); + TEST_ERRNO(setns(fd_ns, CLONE_NEWUSER), EINVAL); + TEST_SUCC(close(fd_ns)); +#endif pid_t pid = getpid(); int pidfd = TEST_SUCC(syscall(SYS_pidfd_open, pid, 0)); diff --git a/test/src/apps/network/netlink_route.c b/test/src/apps/network/netlink_route.c index af21d47cb..428c7b264 100644 --- a/test/src/apps/network/netlink_route.c +++ b/test/src/apps/network/netlink_route.c @@ -224,7 +224,11 @@ FN_TEST(get_link_error) // Invalid name attribute (too short) with index req.ifi.ifi_index = 1234; // FIXME: Asterinas will report `EINVAL` because it performs strict validation. - // TEST_ERROR_SEGMENT(ENODEV); +#ifdef __asterinas__ + TEST_ERROR_SEGMENT(EINVAL); +#else + TEST_ERROR_SEGMENT(ENODEV); +#endif // Invalid name attribute (too long) with index req.hdr.nlmsg_len = sizeof(struct nl_req) + 1; diff --git a/test/src/apps/network/tcp_err.c b/test/src/apps/network/tcp_err.c index 1aeba254f..b4236fc1b 100644 --- a/test/src/apps/network/tcp_err.c +++ b/test/src/apps/network/tcp_err.c @@ -243,14 +243,15 @@ FN_TEST(bind_reuseaddr) TEST_ERRNO(bind(sk2, psaddr, addrlen), EADDRINUSE); + TEST_SUCC(setsockopt(sk1, SOL_SOCKET, SO_REUSEADDR, &disable, + sizeof(disable))); + TEST_SUCC(setsockopt(sk2, SOL_SOCKET, SO_REUSEADDR, &enable, + sizeof(enable))); // FIXME: The test will fail in Asterinas since it doesn't check - // if the previous socket was bound with `SO_REUSEADDR` - // - // TEST_SUCC(setsockopt(sk1, SOL_SOCKET, SO_REUSEADDR, &disable, - // sizeof(disable))); - // TEST_SUCC(setsockopt(sk2, SOL_SOCKET, SO_REUSEADDR, &enable, - // sizeof(enable))); - // TEST_ERRNO(bind(sk2, psaddr, addrlen), EADDRINUSE); + // if the previous socket was bound with `SO_REUSEADDR`. +#ifndef __asterinas__ + TEST_ERRNO(bind(sk2, psaddr, addrlen), EADDRINUSE); +#endif TEST_SUCC(setsockopt(sk1, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable))); diff --git a/test/src/apps/network/tcp_poll.c b/test/src/apps/network/tcp_poll.c index dacbc8040..d6f18809c 100644 --- a/test/src/apps/network/tcp_poll.c +++ b/test/src/apps/network/tcp_poll.c @@ -246,11 +246,13 @@ FN_TEST(poll_shutdown_readwrite) // FIXME: This socket error should be `EPIPE`, but in Asterinas it is // `ECONNRESET`. See the Linux implementation for details: // . - // - // TEST_RES(getsockopt(sk_connect, SOL_SOCKET, SO_ERROR, &err, &errlen), - // errlen == sizeof(err) && err == EPIPE); +#ifdef __asterinas__ TEST_RES(getsockopt(sk_accept, SOL_SOCKET, SO_ERROR, &err, &errlen), errlen == sizeof(err) && err == ECONNRESET); +#else + TEST_RES(getsockopt(sk_connect, SOL_SOCKET, SO_ERROR, &err, &errlen), + errlen == sizeof(err) && err == EPIPE); +#endif } END_TEST() diff --git a/test/src/apps/network/tcp_reuseaddr.c b/test/src/apps/network/tcp_reuseaddr.c index 2a0285c60..0c782ab1e 100644 --- a/test/src/apps/network/tcp_reuseaddr.c +++ b/test/src/apps/network/tcp_reuseaddr.c @@ -77,7 +77,9 @@ FN_TEST(bind_to_listening_port) // Currently, Asterinas does not check whether the port is already in use // by a listening socket when binding, so this test will fail. - // TEST_ERRNO(bind(sock2, (struct sockaddr *)&addr, addrlen), EADDRINUSE); +#ifndef __asterinas__ + TEST_ERRNO(bind(sock2, (struct sockaddr *)&addr, addrlen), EADDRINUSE); +#endif } END_TEST() @@ -105,7 +107,9 @@ FN_TEST(listen_on_the_same_port) // Currently, Asterinas does not check whether the port is already in use // by a listening socket when binding, so this test will fail. - // TEST_ERRNO(bind(sock3, (struct sockaddr *)&addr, addrlen), EADDRINUSE); +#ifndef __asterinas__ + TEST_ERRNO(bind(sock3, (struct sockaddr *)&addr, addrlen), EADDRINUSE); +#endif } END_TEST() @@ -188,16 +192,17 @@ FN_TEST(disable_reuse_after_bound) TEST_SUCC(getsockname(sock1, (struct sockaddr *)&addr, &addrlen)); + option = 1; + TEST_SUCC(setsockopt(sock2, SOL_SOCKET, SO_REUSEADDR, &option, + sizeof(option))); // The following test succeeds on Linux because Linux does not allow disabling // SO_REUSEADDR for TCP sockets once the socket is bound. In contrast, Asterinas // enforces a stricter rule: the port can be reused only if all sockets bound to it // have port reuse enabled. // See the discussion at . - // - // option = 1; - // TEST_SUCC(setsockopt(sock2, SOL_SOCKET, SO_REUSEADDR, &option, - // sizeof(option))); - // TEST_SUCC(bind(sock2, (struct sockaddr *)&addr, addrlen)); +#ifndef __asterinas__ + TEST_SUCC(bind(sock2, (struct sockaddr *)&addr, addrlen)); +#endif } END_TEST() @@ -207,4 +212,4 @@ FN_SETUP(cleanup) CHECK(close(sock2)); CHECK(close(sock3)); } -END_SETUP() \ No newline at end of file +END_SETUP() diff --git a/test/src/apps/network/udp_broadcast.c b/test/src/apps/network/udp_broadcast.c index f8842fca5..fc960a2f8 100644 --- a/test/src/apps/network/udp_broadcast.c +++ b/test/src/apps/network/udp_broadcast.c @@ -1,9 +1,6 @@ // SPDX-License-Identifier: MPL-2.0 #define _GNU_SOURCE -#include -#include -#include #include #include #include @@ -48,7 +45,9 @@ FN_SETUP(create_and_bind) receiver = CHECK(socket(AF_INET, SOCK_DGRAM, 0)); // FIXME: Asterinas cannot support binding to broadcast addresses now. // So all below code related to receiver is commented out. - // CHECK(bind(receiver, (struct sockaddr *)&broadcast_addr, addr_len)); +#ifndef __asterinas__ + CHECK(bind(receiver, (struct sockaddr *)&broadcast_addr, addr_len)); +#endif } END_SETUP() @@ -83,8 +82,10 @@ FN_TEST(basic_broadcast) TEST_SUCC(sendto(sender, MESSAGE, MESSAGE_LEN, 0, (struct sockaddr *)&broadcast_addr, addr_len)); - // TEST_SUCC(recvfrom(receiver, buf, sizeof(buf), 0, - // (struct sockaddr *)&received_addr, &addr_len)); +#ifndef __asterinas__ + TEST_SUCC(recvfrom(receiver, buf, sizeof(buf), 0, + (struct sockaddr *)&received_addr, &addr_len)); +#endif } END_TEST() @@ -120,8 +121,10 @@ FN_TEST(disable_receiver_broadcast) TEST_SUCC(sendto(sender, MESSAGE, MESSAGE_LEN, 0, (struct sockaddr *)&broadcast_addr, addr_len)); - // TEST_SUCC(recvfrom(receiver, buf, sizeof(buf), 0, - // (struct sockaddr *)&received_addr, &addr_len)); +#ifndef __asterinas__ + TEST_SUCC(recvfrom(receiver, buf, sizeof(buf), 0, + (struct sockaddr *)&received_addr, &addr_len)); +#endif } END_TEST() @@ -132,16 +135,20 @@ FN_TEST(connect_then_disable_broadcast) TEST_SUCC( connect(sender, (struct sockaddr *)&broadcast_addr, addr_len)); TEST_SUCC(send(sender, MESSAGE, MESSAGE_LEN, 0)); - // TEST_SUCC(recvfrom(receiver, buf, sizeof(buf), 0, - // (struct sockaddr *)&received_addr, &addr_len)); +#ifndef __asterinas__ + TEST_SUCC(recvfrom(receiver, buf, sizeof(buf), 0, + (struct sockaddr *)&received_addr, &addr_len)); +#endif broadcast_opt = 0; TEST_SUCC(setsockopt(sender, SOL_SOCKET, SO_BROADCAST, &broadcast_opt, broadcast_opt_len)); TEST_SUCC(send(sender, MESSAGE, MESSAGE_LEN, 0)); - // TEST_SUCC(recvfrom(receiver, buf, sizeof(buf), 0, - // (struct sockaddr *)&received_addr, &addr_len)); +#ifndef __asterinas__ + TEST_SUCC(recvfrom(receiver, buf, sizeof(buf), 0, + (struct sockaddr *)&received_addr, &addr_len)); +#endif TEST_ERRNO(sendto(sender, MESSAGE, MESSAGE_LEN, 0, (struct sockaddr *)&broadcast_addr, addr_len), @@ -153,7 +160,9 @@ FN_TEST(connect_then_disable_broadcast) // FIXME: Asterinas cannot pass the following case. // The problem may be that we should invalidate the connected state // once the above connect fails. - // TEST_ERRNO(send(sender, MESSAGE, MESSAGE_LEN, 0), EACCES); +#ifndef __asterinas__ + TEST_ERRNO(send(sender, MESSAGE, MESSAGE_LEN, 0), EACCES); +#endif } END_TEST() @@ -162,4 +171,4 @@ FN_SETUP(cleanup) CHECK(close(sender)); CHECK(close(receiver)); } -END_SETUP() \ No newline at end of file +END_SETUP() diff --git a/test/src/apps/process/group_session.c b/test/src/apps/process/group_session.c index 8103c7e4d..b58dece7e 100644 --- a/test/src/apps/process/group_session.c +++ b/test/src/apps/process/group_session.c @@ -140,8 +140,9 @@ FN_TEST(setsid_session_leader) { // FIXME: We fail this test to work around a gVisor bug. // See comments in `Process::to_new_session` for details. - // - // TEST_ERRNO(setsid(), EPERM); +#ifndef __asterinas__ + TEST_ERRNO(setsid(), EPERM); +#endif } END_TEST() diff --git a/test/src/apps/process/wait4.c b/test/src/apps/process/wait4.c index cc64eefbb..4c13e8f5f 100644 --- a/test/src/apps/process/wait4.c +++ b/test/src/apps/process/wait4.c @@ -2,7 +2,6 @@ #include "../test.h" -#include #include #include #include @@ -300,11 +299,15 @@ FN_TEST(nested_signals) // when the thread is stopped, while Linux does not. TEST_SUCC(kill(child_pid, SIGINT)); sleep(1); - // TEST_ERRNO(read(pipe_fds[0], buf, 1), EAGAIN); +#ifndef __asterinas__ + TEST_ERRNO(read(pipe_fds[0], buf, 1), EAGAIN); +#endif TEST_SUCC(kill(child_pid, SIGTRAP)); sleep(1); - // TEST_ERRNO(read(pipe_fds[0], buf, 1), EAGAIN); +#ifndef __asterinas__ + TEST_ERRNO(read(pipe_fds[0], buf, 1), EAGAIN); +#endif TEST_RES(wait4(child_pid, &status, WSTOPPED, NULL), _ret == child_pid && WIFSTOPPED(status) && diff --git a/test/src/apps/signal_c/signal_test2.c b/test/src/apps/signal_c/signal_test2.c index 648fc6a08..f89206042 100644 --- a/test/src/apps/signal_c/signal_test2.c +++ b/test/src/apps/signal_c/signal_test2.c @@ -33,14 +33,7 @@ FN_TEST(kill_blocked_and_ignored) signal(SIGCHLD, &signal_handler); - // FIXME: Currently, Asterinas never queues an ignored signal, so this test - // will fail. See the comments at `PosixThread::enqueue_signal_locked` for - // more details. - // - // received_signals = 0; - // TEST_RES(sigprocmask(SIG_UNBLOCK, &sigs, NULL), received_signals == 1); - // - sigprocmask(SIG_UNBLOCK, &sigs, NULL); + TEST_RES(sigprocmask(SIG_UNBLOCK, &sigs, NULL), received_signals == 1); } END_TEST()