Add more sys_socket tests
This commit is contained in:
+6
-4
@@ -124,6 +124,12 @@ EXPECT_NAMES=\
|
||||
string/stpcpy \
|
||||
string/stpncpy \
|
||||
strings \
|
||||
sys_socket/recv \
|
||||
sys_socket/recvfrom \
|
||||
sys_socket/unixpeername \
|
||||
sys_socket/unixrecv \
|
||||
sys_socket/unixrecvfrom \
|
||||
sys_socket/unixsocketpair \
|
||||
sys_stat/chmod \
|
||||
sys_stat/lstat \
|
||||
sys_stat/fstatat \
|
||||
@@ -269,10 +275,6 @@ VARIED_NAMES=\
|
||||
sys_epoll/epoll \
|
||||
sys_resource/constants \
|
||||
sys_socket/getpeername \
|
||||
sys_socket/recv \
|
||||
sys_socket/recvfrom \
|
||||
sys_socket/unixpeername \
|
||||
sys_socket/unixrecvfrom \
|
||||
sys_stat/stat \
|
||||
sys_statvfs/statvfs \
|
||||
sys_utsname/uname \
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
send foo
|
||||
recv foo
|
||||
@@ -0,0 +1,2 @@
|
||||
sendto bar
|
||||
recvfrom bar
|
||||
@@ -0,0 +1,3 @@
|
||||
listen sockname [AF_UNIX]: root/unixpeername.sock
|
||||
client peername [AF_UNIX]: root/unixpeername.sock
|
||||
client sockname [AF_UNIX]: (unnamed)
|
||||
@@ -0,0 +1,2 @@
|
||||
send ipsum
|
||||
recv ipsum
|
||||
@@ -0,0 +1,2 @@
|
||||
send lorem
|
||||
recvfrom lorem
|
||||
@@ -0,0 +1,2 @@
|
||||
child: ping
|
||||
parent: pong
|
||||
@@ -0,0 +1,2 @@
|
||||
send foo
|
||||
recv foo
|
||||
@@ -0,0 +1,2 @@
|
||||
sendto bar
|
||||
recvfrom bar
|
||||
@@ -0,0 +1,3 @@
|
||||
listen sockname [AF_UNIX]: root/unixpeername.sock
|
||||
client peername [AF_UNIX]: root/unixpeername.sock
|
||||
client sockname [AF_UNIX]: (unnamed)
|
||||
@@ -0,0 +1,2 @@
|
||||
send ipsum
|
||||
recv ipsum
|
||||
@@ -0,0 +1,2 @@
|
||||
send lorem
|
||||
recvfrom lorem
|
||||
@@ -0,0 +1,2 @@
|
||||
child: ping
|
||||
parent: pong
|
||||
@@ -0,0 +1,65 @@
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
#include "test_helpers.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int status;
|
||||
const char* socket_path = "unix_stream.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) {
|
||||
// to test that blocking in accept() works
|
||||
usleep(500000);
|
||||
|
||||
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 *msg = "ipsum";
|
||||
printf("send %s\n", msg);
|
||||
status = send(client_fd, msg, 6, 0);
|
||||
ERROR_IF(send, status, == -1);
|
||||
|
||||
close(client_fd);
|
||||
return 0;
|
||||
} else {
|
||||
int accepted_fd = accept(server_fd, NULL, NULL);
|
||||
ERROR_IF(accept, accepted_fd, == -1);
|
||||
|
||||
char x[6];
|
||||
ssize_t amount = recv(accepted_fd, x, 6, 0);
|
||||
ERROR_IF(recv, amount, == -1);
|
||||
|
||||
printf("recv %s\n", x);
|
||||
close(accepted_fd);
|
||||
close(server_fd);
|
||||
|
||||
wait(NULL);
|
||||
unlink(socket_path);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "test_helpers.h"
|
||||
|
||||
volatile sig_atomic_t signal_received = 0;
|
||||
|
||||
void handle_sigusr1(int sig) {
|
||||
(void)sig;
|
||||
signal_received = 1;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
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);
|
||||
ERROR_IF(socketpair, status, == -1);
|
||||
|
||||
pid = fork();
|
||||
ERROR_IF(fork, pid, == -1);
|
||||
|
||||
if (pid == 0) {
|
||||
close(sv[0]);
|
||||
|
||||
ssize_t n = recv(sv[1], buf, sizeof(buf), 0);
|
||||
ERROR_IF(recv, n, == -1);
|
||||
printf("child: %s\n", buf);
|
||||
|
||||
status = send(sv[1], child_msg, 5, 0);
|
||||
ERROR_IF(send, status, == -1);
|
||||
|
||||
close(sv[1]);
|
||||
} else {
|
||||
close(sv[1]);
|
||||
|
||||
status = send(sv[0], parent_msg, 5, 0);
|
||||
ERROR_IF(send, status, == -1);
|
||||
|
||||
ssize_t n = recv(sv[0], buf, sizeof(buf), 0);
|
||||
ERROR_IF(recv, n, == -1);
|
||||
printf("parent: %s\n", buf);
|
||||
|
||||
close(sv[0]);
|
||||
wait(NULL);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user