Commit ebdada9d authored by David S. Miller's avatar David S. Miller

Merge branch 'vsock-tests'

Stefano Garzarella says:

====================
vsock/test: add recv_buf()/send_buf() utility functions and some improvements

We recently found that some tests were failing [1].

The problem was that we were not waiting for all the bytes correctly,
so we had a partial read. I had initially suggested using MSG_WAITALL,
but this could have timeout problems.

Since we already had send_byte() and recv_byte() that handled the timeout,
but also the expected return value, I moved that code to two new functions
that we can now use to send/receive generic buffers.

The last commit is just an improvement to a test I found difficult to
understand while using the new functions.

@Arseniy a review and some testing are really appreciated :-)

[1] https://lore.kernel.org/netdev/63xflnwiohdfo6m3vnrrxgv2ulplencpwug5qqacugqh7xxpu3@tsczkuqgwurb/
====================
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents 685c6d5b bc7bea45
......@@ -211,102 +211,138 @@ int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
return vsock_accept(cid, port, clientaddrp, SOCK_SEQPACKET);
}
/* Transmit one byte and check the return value.
/* Transmit bytes from a buffer and check the return value.
*
* expected_ret:
* <0 Negative errno (for testing errors)
* 0 End-of-file
* 1 Success
* >0 Success (bytes successfully written)
*/
void send_byte(int fd, int expected_ret, int flags)
void send_buf(int fd, const void *buf, size_t len, int flags,
ssize_t expected_ret)
{
const uint8_t byte = 'A';
ssize_t nwritten;
ssize_t nwritten = 0;
ssize_t ret;
timeout_begin(TIMEOUT);
do {
nwritten = send(fd, &byte, sizeof(byte), flags);
timeout_check("write");
} while (nwritten < 0 && errno == EINTR);
ret = send(fd, buf + nwritten, len - nwritten, flags);
timeout_check("send");
if (ret == 0 || (ret < 0 && errno != EINTR))
break;
nwritten += ret;
} while (nwritten < len);
timeout_end();
if (expected_ret < 0) {
if (nwritten != -1) {
fprintf(stderr, "bogus send(2) return value %zd\n",
nwritten);
if (ret != -1) {
fprintf(stderr, "bogus send(2) return value %zd (expected %zd)\n",
ret, expected_ret);
exit(EXIT_FAILURE);
}
if (errno != -expected_ret) {
perror("write");
perror("send");
exit(EXIT_FAILURE);
}
return;
}
if (nwritten < 0) {
perror("write");
if (ret < 0) {
perror("send");
exit(EXIT_FAILURE);
}
if (nwritten == 0) {
if (expected_ret == 0)
return;
fprintf(stderr, "unexpected EOF while sending byte\n");
exit(EXIT_FAILURE);
}
if (nwritten != sizeof(byte)) {
fprintf(stderr, "bogus send(2) return value %zd\n", nwritten);
if (nwritten != expected_ret) {
if (ret == 0)
fprintf(stderr, "unexpected EOF while sending bytes\n");
fprintf(stderr, "bogus send(2) bytes written %zd (expected %zd)\n",
nwritten, expected_ret);
exit(EXIT_FAILURE);
}
}
/* Receive one byte and check the return value.
/* Receive bytes in a buffer and check the return value.
*
* expected_ret:
* <0 Negative errno (for testing errors)
* 0 End-of-file
* 1 Success
* >0 Success (bytes successfully read)
*/
void recv_byte(int fd, int expected_ret, int flags)
void recv_buf(int fd, void *buf, size_t len, int flags, ssize_t expected_ret)
{
uint8_t byte;
ssize_t nread;
ssize_t nread = 0;
ssize_t ret;
timeout_begin(TIMEOUT);
do {
nread = recv(fd, &byte, sizeof(byte), flags);
timeout_check("read");
} while (nread < 0 && errno == EINTR);
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 (nread != -1) {
fprintf(stderr, "bogus recv(2) return value %zd\n",
nread);
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("read");
perror("recv");
exit(EXIT_FAILURE);
}
return;
}
if (nread < 0) {
perror("read");
if (ret < 0) {
perror("recv");
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);
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.
*
* expected_ret:
* <0 Negative errno (for testing errors)
* 0 End-of-file
* 1 Success
*/
void send_byte(int fd, int expected_ret, int flags)
{
const uint8_t byte = 'A';
send_buf(fd, &byte, sizeof(byte), flags, expected_ret);
}
/* Receive one byte and check the return value.
*
* expected_ret:
* <0 Negative errno (for testing errors)
* 0 End-of-file
* 1 Success
*/
void recv_byte(int fd, int expected_ret, int flags)
{
uint8_t byte;
recv_buf(fd, &byte, sizeof(byte), flags, expected_ret);
if (byte != 'A') {
fprintf(stderr, "unexpected byte read %c\n", byte);
exit(EXIT_FAILURE);
......
......@@ -42,6 +42,9 @@ int vsock_stream_accept(unsigned int cid, unsigned int port,
int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
struct sockaddr_vm *clientaddrp);
void vsock_wait_remote_close(int fd);
void send_buf(int fd, const void *buf, size_t len, int flags,
ssize_t expected_ret);
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 recv_byte(int fd, int expected_ret, int flags);
void run_tests(const struct test_case *test_cases,
......
This diff is collapsed.
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