Merge branch 'uds-preaccept' into 'master'
Add test for data transfer before accept for UDS See merge request redox-os/relibc!964
This commit is contained in:
@@ -1,2 +1,6 @@
|
||||
SOCK_STREAM
|
||||
child: ping
|
||||
parent: pong
|
||||
SOCK_DGRAM
|
||||
child: ping
|
||||
parent: pong
|
||||
|
||||
@@ -1,2 +1,6 @@
|
||||
SOCK_STREAM
|
||||
child: ping
|
||||
parent: pong
|
||||
SOCK_DGRAM
|
||||
child: ping
|
||||
parent: pong
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
#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"
|
||||
|
||||
// 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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user