Define `__asterinas__` macro in tests

This commit is contained in:
Ruihan Li 2025-12-14 22:07:07 +08:00 committed by Tate, Hongliang Tian
parent da67967f6d
commit f30bd698d1
11 changed files with 100 additions and 65 deletions

View File

@ -181,9 +181,15 @@ FN_TEST(path)
TEST_ERRNO(mmap(NULL, PAGE_SIZE, PROT_READ, MAP_SHARED, fd, 0), EBADF); TEST_ERRNO(mmap(NULL, PAGE_SIZE, PROT_READ, MAP_SHARED, fd, 0), EBADF);
// FIXME: Asterinas reports `EACCES` because it performs the permission check first. // FIXME: Asterinas reports `EACCES` because it performs the permission check first.
// TEST_ERRNO(mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, #ifdef __asterinas__
// 0), TEST_ERRNO(mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
// EBADF); 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_SH), EBADF);
TEST_ERRNO(flock(fd, LOCK_UN), 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); TEST_ERRNO(mmap(NULL, PAGE_SIZE, PROT_READ, MAP_SHARED, fd, 0), EBADF);
// FIXME: Asterinas reports `EACCES` because it performs the permission check first. // FIXME: Asterinas reports `EACCES` because it performs the permission check first.
// TEST_ERRNO(mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, #ifdef __asterinas__
// 0), TEST_ERRNO(mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
// EBADF); 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_SH), EBADF);
TEST_ERRNO(flock(fd, LOCK_UN), EBADF); TEST_ERRNO(flock(fd, LOCK_UN), EBADF);

View File

@ -42,8 +42,12 @@ FN_TEST(mremap)
TEST_ERRNO(mremap(addr, 0, PAGE_SIZE, 0), EINVAL); TEST_ERRNO(mremap(addr, 0, PAGE_SIZE, 0), EINVAL);
// There is no enough room to expand the mapping. // There is no enough room to expand the mapping.
// FIXME: Asterinas returns EACCESS here, which is not a correct error code. // FIXME: Asterinas returns EACCES here, which is not a correct error code.
// TEST_ERRNO(mremap(addr, PAGE_SIZE, 2 * PAGE_SIZE, 0), ENOMEM); #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; char *hole = addr + 2 * PAGE_SIZE;

View File

@ -3,10 +3,7 @@
#define _GNU_SOURCE #define _GNU_SOURCE
#include <sched.h> #include <sched.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h>
#include <sys/syscall.h> #include <sys/syscall.h>
#include "../test.h" #include "../test.h"
@ -15,10 +12,12 @@ FN_TEST(set_ns_empty_flags)
{ {
// FIXME: The following test will fail on Asterinas // FIXME: The following test will fail on Asterinas
// because it currently does not support ns file. // because it currently does not support ns file.
// const char *ns_path = "/proc/self/ns/user"; #ifndef __asterinas__
// int fd_ns = TEST_SUCC(open(ns_path, O_RDONLY)); const char *ns_path = "/proc/self/ns/user";
// TEST_ERRNO(setns(fd_ns, 0), EINVAL); int fd_ns = TEST_SUCC(open(ns_path, O_RDONLY));
// TEST_SUCC(close(fd_ns)); TEST_ERRNO(setns(fd_ns, 0), EINVAL);
TEST_SUCC(close(fd_ns));
#endif
pid_t pid = getpid(); pid_t pid = getpid();
int pidfd = TEST_SUCC(syscall(SYS_pidfd_open, pid, 0)); 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. // current user namespace. This is different from other namespaces.
// FIXME: The following test will fail on Asterinas // FIXME: The following test will fail on Asterinas
// because it currently does not support ns file. // because it currently does not support ns file.
// const char *ns_path = "/proc/self/ns/user"; #ifndef __asterinas__
// int fd_ns = TEST_SUCC(open(ns_path, O_RDONLY)); const char *ns_path = "/proc/self/ns/user";
// TEST_ERRNO(setns(fd_ns, CLONE_NEWUSER), EINVAL); int fd_ns = TEST_SUCC(open(ns_path, O_RDONLY));
// TEST_SUCC(close(fd_ns)); TEST_ERRNO(setns(fd_ns, CLONE_NEWUSER), EINVAL);
TEST_SUCC(close(fd_ns));
#endif
pid_t pid = getpid(); pid_t pid = getpid();
int pidfd = TEST_SUCC(syscall(SYS_pidfd_open, pid, 0)); int pidfd = TEST_SUCC(syscall(SYS_pidfd_open, pid, 0));

View File

@ -224,7 +224,11 @@ FN_TEST(get_link_error)
// Invalid name attribute (too short) with index // Invalid name attribute (too short) with index
req.ifi.ifi_index = 1234; req.ifi.ifi_index = 1234;
// FIXME: Asterinas will report `EINVAL` because it performs strict validation. // 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 // Invalid name attribute (too long) with index
req.hdr.nlmsg_len = sizeof(struct nl_req) + 1; req.hdr.nlmsg_len = sizeof(struct nl_req) + 1;

View File

@ -243,14 +243,15 @@ FN_TEST(bind_reuseaddr)
TEST_ERRNO(bind(sk2, psaddr, addrlen), EADDRINUSE); 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 // FIXME: The test will fail in Asterinas since it doesn't check
// if the previous socket was bound with `SO_REUSEADDR` // if the previous socket was bound with `SO_REUSEADDR`.
// #ifndef __asterinas__
// TEST_SUCC(setsockopt(sk1, SOL_SOCKET, SO_REUSEADDR, &disable, TEST_ERRNO(bind(sk2, psaddr, addrlen), EADDRINUSE);
// sizeof(disable))); #endif
// TEST_SUCC(setsockopt(sk2, SOL_SOCKET, SO_REUSEADDR, &enable,
// sizeof(enable)));
// TEST_ERRNO(bind(sk2, psaddr, addrlen), EADDRINUSE);
TEST_SUCC(setsockopt(sk1, SOL_SOCKET, SO_REUSEADDR, &enable, TEST_SUCC(setsockopt(sk1, SOL_SOCKET, SO_REUSEADDR, &enable,
sizeof(enable))); sizeof(enable)));

View File

@ -246,11 +246,13 @@ FN_TEST(poll_shutdown_readwrite)
// FIXME: This socket error should be `EPIPE`, but in Asterinas it is // FIXME: This socket error should be `EPIPE`, but in Asterinas it is
// `ECONNRESET`. See the Linux implementation for details: // `ECONNRESET`. See the Linux implementation for details:
// <https://github.com/torvalds/linux/blob/848e076317446f9c663771ddec142d7c2eb4cb43/net/ipv4/tcp_input.c#L4553-L4555>. // <https://github.com/torvalds/linux/blob/848e076317446f9c663771ddec142d7c2eb4cb43/net/ipv4/tcp_input.c#L4553-L4555>.
// #ifdef __asterinas__
// TEST_RES(getsockopt(sk_connect, SOL_SOCKET, SO_ERROR, &err, &errlen),
// errlen == sizeof(err) && err == EPIPE);
TEST_RES(getsockopt(sk_accept, SOL_SOCKET, SO_ERROR, &err, &errlen), TEST_RES(getsockopt(sk_accept, SOL_SOCKET, SO_ERROR, &err, &errlen),
errlen == sizeof(err) && err == ECONNRESET); 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() END_TEST()

View File

@ -77,7 +77,9 @@ FN_TEST(bind_to_listening_port)
// Currently, Asterinas does not check whether the port is already in use // Currently, Asterinas does not check whether the port is already in use
// by a listening socket when binding, so this test will fail. // 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() 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 // Currently, Asterinas does not check whether the port is already in use
// by a listening socket when binding, so this test will fail. // 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() END_TEST()
@ -188,16 +192,17 @@ FN_TEST(disable_reuse_after_bound)
TEST_SUCC(getsockname(sock1, (struct sockaddr *)&addr, &addrlen)); 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 // 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 // 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 // enforces a stricter rule: the port can be reused only if all sockets bound to it
// have port reuse enabled. // have port reuse enabled.
// See the discussion at <https://github.com/asterinas/asterinas/pull/2277#discussion_r2230139244>. // See the discussion at <https://github.com/asterinas/asterinas/pull/2277#discussion_r2230139244>.
// #ifndef __asterinas__
// option = 1; TEST_SUCC(bind(sock2, (struct sockaddr *)&addr, addrlen));
// TEST_SUCC(setsockopt(sock2, SOL_SOCKET, SO_REUSEADDR, &option, #endif
// sizeof(option)));
// TEST_SUCC(bind(sock2, (struct sockaddr *)&addr, addrlen));
} }
END_TEST() END_TEST()
@ -207,4 +212,4 @@ FN_SETUP(cleanup)
CHECK(close(sock2)); CHECK(close(sock2));
CHECK(close(sock3)); CHECK(close(sock3));
} }
END_SETUP() END_SETUP()

View File

@ -1,9 +1,6 @@
// SPDX-License-Identifier: MPL-2.0 // SPDX-License-Identifier: MPL-2.0
#define _GNU_SOURCE #define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
@ -48,7 +45,9 @@ FN_SETUP(create_and_bind)
receiver = CHECK(socket(AF_INET, SOCK_DGRAM, 0)); receiver = CHECK(socket(AF_INET, SOCK_DGRAM, 0));
// FIXME: Asterinas cannot support binding to broadcast addresses now. // FIXME: Asterinas cannot support binding to broadcast addresses now.
// So all below code related to receiver is commented out. // 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() END_SETUP()
@ -83,8 +82,10 @@ FN_TEST(basic_broadcast)
TEST_SUCC(sendto(sender, MESSAGE, MESSAGE_LEN, 0, TEST_SUCC(sendto(sender, MESSAGE, MESSAGE_LEN, 0,
(struct sockaddr *)&broadcast_addr, addr_len)); (struct sockaddr *)&broadcast_addr, addr_len));
// TEST_SUCC(recvfrom(receiver, buf, sizeof(buf), 0, #ifndef __asterinas__
// (struct sockaddr *)&received_addr, &addr_len)); TEST_SUCC(recvfrom(receiver, buf, sizeof(buf), 0,
(struct sockaddr *)&received_addr, &addr_len));
#endif
} }
END_TEST() END_TEST()
@ -120,8 +121,10 @@ FN_TEST(disable_receiver_broadcast)
TEST_SUCC(sendto(sender, MESSAGE, MESSAGE_LEN, 0, TEST_SUCC(sendto(sender, MESSAGE, MESSAGE_LEN, 0,
(struct sockaddr *)&broadcast_addr, addr_len)); (struct sockaddr *)&broadcast_addr, addr_len));
// TEST_SUCC(recvfrom(receiver, buf, sizeof(buf), 0, #ifndef __asterinas__
// (struct sockaddr *)&received_addr, &addr_len)); TEST_SUCC(recvfrom(receiver, buf, sizeof(buf), 0,
(struct sockaddr *)&received_addr, &addr_len));
#endif
} }
END_TEST() END_TEST()
@ -132,16 +135,20 @@ FN_TEST(connect_then_disable_broadcast)
TEST_SUCC( TEST_SUCC(
connect(sender, (struct sockaddr *)&broadcast_addr, addr_len)); connect(sender, (struct sockaddr *)&broadcast_addr, addr_len));
TEST_SUCC(send(sender, MESSAGE, MESSAGE_LEN, 0)); TEST_SUCC(send(sender, MESSAGE, MESSAGE_LEN, 0));
// TEST_SUCC(recvfrom(receiver, buf, sizeof(buf), 0, #ifndef __asterinas__
// (struct sockaddr *)&received_addr, &addr_len)); TEST_SUCC(recvfrom(receiver, buf, sizeof(buf), 0,
(struct sockaddr *)&received_addr, &addr_len));
#endif
broadcast_opt = 0; broadcast_opt = 0;
TEST_SUCC(setsockopt(sender, SOL_SOCKET, SO_BROADCAST, &broadcast_opt, TEST_SUCC(setsockopt(sender, SOL_SOCKET, SO_BROADCAST, &broadcast_opt,
broadcast_opt_len)); broadcast_opt_len));
TEST_SUCC(send(sender, MESSAGE, MESSAGE_LEN, 0)); TEST_SUCC(send(sender, MESSAGE, MESSAGE_LEN, 0));
// TEST_SUCC(recvfrom(receiver, buf, sizeof(buf), 0, #ifndef __asterinas__
// (struct sockaddr *)&received_addr, &addr_len)); TEST_SUCC(recvfrom(receiver, buf, sizeof(buf), 0,
(struct sockaddr *)&received_addr, &addr_len));
#endif
TEST_ERRNO(sendto(sender, MESSAGE, MESSAGE_LEN, 0, TEST_ERRNO(sendto(sender, MESSAGE, MESSAGE_LEN, 0,
(struct sockaddr *)&broadcast_addr, addr_len), (struct sockaddr *)&broadcast_addr, addr_len),
@ -153,7 +160,9 @@ FN_TEST(connect_then_disable_broadcast)
// FIXME: Asterinas cannot pass the following case. // FIXME: Asterinas cannot pass the following case.
// The problem may be that we should invalidate the connected state // The problem may be that we should invalidate the connected state
// once the above connect fails. // 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() END_TEST()
@ -162,4 +171,4 @@ FN_SETUP(cleanup)
CHECK(close(sender)); CHECK(close(sender));
CHECK(close(receiver)); CHECK(close(receiver));
} }
END_SETUP() END_SETUP()

View File

@ -140,8 +140,9 @@ FN_TEST(setsid_session_leader)
{ {
// FIXME: We fail this test to work around a gVisor bug. // FIXME: We fail this test to work around a gVisor bug.
// See comments in `Process::to_new_session` for details. // See comments in `Process::to_new_session` for details.
// #ifndef __asterinas__
// TEST_ERRNO(setsid(), EPERM); TEST_ERRNO(setsid(), EPERM);
#endif
} }
END_TEST() END_TEST()

View File

@ -2,7 +2,6 @@
#include "../test.h" #include "../test.h"
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
@ -300,11 +299,15 @@ FN_TEST(nested_signals)
// when the thread is stopped, while Linux does not. // when the thread is stopped, while Linux does not.
TEST_SUCC(kill(child_pid, SIGINT)); TEST_SUCC(kill(child_pid, SIGINT));
sleep(1); 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)); TEST_SUCC(kill(child_pid, SIGTRAP));
sleep(1); 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), TEST_RES(wait4(child_pid, &status, WSTOPPED, NULL),
_ret == child_pid && WIFSTOPPED(status) && _ret == child_pid && WIFSTOPPED(status) &&

View File

@ -33,14 +33,7 @@ FN_TEST(kill_blocked_and_ignored)
signal(SIGCHLD, &signal_handler); signal(SIGCHLD, &signal_handler);
// FIXME: Currently, Asterinas never queues an ignored signal, so this test TEST_RES(sigprocmask(SIG_UNBLOCK, &sigs, NULL), received_signals == 1);
// 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);
} }
END_TEST() END_TEST()