From e2b8acb05d47425534fd6470fac4a86a261011c9 Mon Sep 17 00:00:00 2001 From: Wildan M Date: Fri, 6 Feb 2026 14:17:45 +0700 Subject: [PATCH] Add test for data transfer before accept for UDS --- .../sys_socket/unixsocketpair.stdout | 4 ++ .../sys_socket/unixsocketpair.stdout | 4 ++ tests/sys_socket/unixrecvearly.c | 66 +++++++++++++++++++ tests/sys_socket/unixsocketpair.c | 11 +++- tests/sys_socket/unixwrite.c | 14 +++- 5 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 tests/sys_socket/unixrecvearly.c diff --git a/tests/expected/bins_dynamic/sys_socket/unixsocketpair.stdout b/tests/expected/bins_dynamic/sys_socket/unixsocketpair.stdout index d25823c516..bb7fc428fa 100644 --- a/tests/expected/bins_dynamic/sys_socket/unixsocketpair.stdout +++ b/tests/expected/bins_dynamic/sys_socket/unixsocketpair.stdout @@ -1,2 +1,6 @@ +SOCK_STREAM +child: ping +parent: pong +SOCK_DGRAM child: ping parent: pong diff --git a/tests/expected/bins_static/sys_socket/unixsocketpair.stdout b/tests/expected/bins_static/sys_socket/unixsocketpair.stdout index d25823c516..bb7fc428fa 100644 --- a/tests/expected/bins_static/sys_socket/unixsocketpair.stdout +++ b/tests/expected/bins_static/sys_socket/unixsocketpair.stdout @@ -1,2 +1,6 @@ +SOCK_STREAM +child: ping +parent: pong +SOCK_DGRAM child: ping parent: pong diff --git a/tests/sys_socket/unixrecvearly.c b/tests/sys_socket/unixrecvearly.c new file mode 100644 index 0000000000..6c496f53f2 --- /dev/null +++ b/tests/sys_socket/unixrecvearly.c @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include +#include + +#include "test_helpers.h" + +// similar to "unixrecv", but this test if recv() works before accept(). +int main(void) +{ + int status; + const char* socket_path = "unix_read.sock"; + unlink(socket_path); + + int server_fd = socket(AF_UNIX, SOCK_STREAM, 0); + ERROR_IF(socket, server_fd, == -1); + + struct sockaddr_un addr = { .sun_family = AF_UNIX }; + strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1); + + status = bind(server_fd, (struct sockaddr*)&addr, sizeof(struct sockaddr_un)); + ERROR_IF(bind, status, == -1); + + status = listen(server_fd, 5); + ERROR_IF(listen, status, == -1); + + pid_t pid = fork(); + ERROR_IF(fork, pid, == -1); + + if (pid == 0) { + int client_fd = socket(AF_UNIX, SOCK_STREAM, 0); + ERROR_IF(socket, client_fd, == -1); + + status = connect(client_fd, (struct sockaddr*)&addr, sizeof(struct sockaddr_un)); + ERROR_IF(connect, status, == -1); + + char x[6]; + ssize_t amount = recv(client_fd, x, 6, 0); + ERROR_IF(recv, amount, == -1); + printf("recv %s\n", x); + + close(client_fd); + return 0; + } else { + // to test that blocking in recv() works + usleep(500000); + + int accepted_fd = accept(server_fd, NULL, NULL); + ERROR_IF(accept, accepted_fd, == -1); + + char *msg = "ipsum"; + printf("send %s\n", msg); + status = send(accepted_fd, msg, 6, 0); + ERROR_IF(send, status, == -1); + + close(accepted_fd); + close(server_fd); + + wait(NULL); + unlink(socket_path); + } + + return 0; +} \ No newline at end of file diff --git a/tests/sys_socket/unixsocketpair.c b/tests/sys_socket/unixsocketpair.c index 014318393d..0f5aa0f618 100644 --- a/tests/sys_socket/unixsocketpair.c +++ b/tests/sys_socket/unixsocketpair.c @@ -10,14 +10,14 @@ #include "test_helpers.h" -int main(void) { +void socketpair_test(int flags) { int sv[2]; pid_t pid; char buf[64]; const char *parent_msg = "ping"; const char *child_msg = "pong"; - int status = socketpair(AF_UNIX, SOCK_STREAM, 0, sv); + int status = socketpair(AF_UNIX, flags, 0, sv); ERROR_IF(socketpair, status, == -1); pid = fork(); @@ -34,6 +34,7 @@ int main(void) { ERROR_IF(send, status, == -1); close(sv[1]); + exit(0); } else { close(sv[1]); @@ -47,6 +48,12 @@ int main(void) { close(sv[0]); wait(NULL); } +} +int main(void) { + printf("SOCK_STREAM\n"); + socketpair_test(SOCK_STREAM); + printf("SOCK_DGRAM\n"); + socketpair_test(SOCK_DGRAM); return 0; } \ No newline at end of file diff --git a/tests/sys_socket/unixwrite.c b/tests/sys_socket/unixwrite.c index 2233680999..4386c66be2 100644 --- a/tests/sys_socket/unixwrite.c +++ b/tests/sys_socket/unixwrite.c @@ -9,6 +9,9 @@ #include "test_helpers.h" +// similar to "unixrecv", but this test if write() works before accept(). +// "unixrecvfrom" is the similar test to this one, but using SOCK_DGRAM. +// "unixrecvearly" is also the similar test to this one, but using recv(). int main() { int status; const char* socket_path = "unix_write.sock"; @@ -35,9 +38,18 @@ int main() { ERROR_IF(connect, status, == -1); char *msg = "yes"; - ssize_t ret = write(client_fd, msg, 3); + ssize_t ret = write(client_fd, msg, 4); ERROR_IF(write, ret, == -1); + UNEXP_IF(raise, ret, != 4); + int accepted_fd = accept(server_fd, NULL, NULL); + ERROR_IF(accept, accepted_fd, == -1); + + char x[4]; + ssize_t amount = read(accepted_fd, x, 4); + ERROR_IF(read, amount, == -1); + + close(accepted_fd); close(client_fd); close(server_fd); return 0;