Commit a8ed71a2 authored by Stefano Garzarella's avatar Stefano Garzarella Committed by David S. Miller

vsock/test: add recv_buf() utility function

Move the code of recv_byte() out in a new utility function that
can be used to receive a generic buffer.

This new function can be used when we need to receive a custom
buffer and not just a single 'A' byte.
Signed-off-by: default avatarStefano Garzarella <sgarzare@redhat.com>
Reviewed-by: default avatarArseniy Krasnov <avkrasnov@salutedevices.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 685c6d5b
...@@ -211,6 +211,58 @@ int vsock_seqpacket_accept(unsigned int cid, unsigned int port, ...@@ -211,6 +211,58 @@ int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
return vsock_accept(cid, port, clientaddrp, SOCK_SEQPACKET); return vsock_accept(cid, port, clientaddrp, SOCK_SEQPACKET);
} }
/* Receive bytes in a buffer and check the return value.
*
* expected_ret:
* <0 Negative errno (for testing errors)
* 0 End-of-file
* >0 Success (bytes successfully read)
*/
void recv_buf(int fd, void *buf, size_t len, int flags, ssize_t expected_ret)
{
ssize_t nread = 0;
ssize_t ret;
timeout_begin(TIMEOUT);
do {
ret = recv(fd, buf + nread, len - nread, flags);
timeout_check("recv");
if (ret == 0 || (ret < 0 && errno != EINTR))
break;
nread += ret;
} while (nread < len);
timeout_end();
if (expected_ret < 0) {
if (ret != -1) {
fprintf(stderr, "bogus recv(2) return value %zd (expected %zd)\n",
ret, expected_ret);
exit(EXIT_FAILURE);
}
if (errno != -expected_ret) {
perror("recv");
exit(EXIT_FAILURE);
}
return;
}
if (ret < 0) {
perror("recv");
exit(EXIT_FAILURE);
}
if (nread != expected_ret) {
if (ret == 0)
fprintf(stderr, "unexpected EOF while receiving bytes\n");
fprintf(stderr, "bogus recv(2) bytes read %zd (expected %zd)\n",
nread, expected_ret);
exit(EXIT_FAILURE);
}
}
/* Transmit one byte and check the return value. /* Transmit one byte and check the return value.
* *
* expected_ret: * expected_ret:
...@@ -270,43 +322,9 @@ void send_byte(int fd, int expected_ret, int flags) ...@@ -270,43 +322,9 @@ void send_byte(int fd, int expected_ret, int flags)
void recv_byte(int fd, int expected_ret, int flags) void recv_byte(int fd, int expected_ret, int flags)
{ {
uint8_t byte; uint8_t byte;
ssize_t nread;
timeout_begin(TIMEOUT);
do {
nread = recv(fd, &byte, sizeof(byte), flags);
timeout_check("read");
} while (nread < 0 && errno == EINTR);
timeout_end();
if (expected_ret < 0) {
if (nread != -1) {
fprintf(stderr, "bogus recv(2) return value %zd\n",
nread);
exit(EXIT_FAILURE);
}
if (errno != -expected_ret) {
perror("read");
exit(EXIT_FAILURE);
}
return;
}
if (nread < 0) { recv_buf(fd, &byte, sizeof(byte), flags, expected_ret);
perror("read");
exit(EXIT_FAILURE);
}
if (nread == 0) {
if (expected_ret == 0)
return;
fprintf(stderr, "unexpected EOF while receiving byte\n");
exit(EXIT_FAILURE);
}
if (nread != sizeof(byte)) {
fprintf(stderr, "bogus recv(2) return value %zd\n", nread);
exit(EXIT_FAILURE);
}
if (byte != 'A') { if (byte != 'A') {
fprintf(stderr, "unexpected byte read %c\n", byte); fprintf(stderr, "unexpected byte read %c\n", byte);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
......
...@@ -42,6 +42,7 @@ int vsock_stream_accept(unsigned int cid, unsigned int port, ...@@ -42,6 +42,7 @@ int vsock_stream_accept(unsigned int cid, unsigned int port,
int vsock_seqpacket_accept(unsigned int cid, unsigned int port, int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
struct sockaddr_vm *clientaddrp); struct sockaddr_vm *clientaddrp);
void vsock_wait_remote_close(int fd); void vsock_wait_remote_close(int fd);
void recv_buf(int fd, void *buf, size_t len, int flags, ssize_t expected_ret);
void send_byte(int fd, int expected_ret, int flags); void send_byte(int fd, int expected_ret, int flags);
void recv_byte(int fd, int expected_ret, int flags); void recv_byte(int fd, int expected_ret, int flags);
void run_tests(const struct test_case *test_cases, void run_tests(const struct test_case *test_cases,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment