diff mbox series

[net-next,1/5] vsock/test: add recv_buf() utility function

Message ID 20230915121452.87192-2-sgarzare@redhat.com (mailing list archive)
State Accepted
Delegated to: Netdev Maintainers
Headers show
Series vsock/test: add recv_buf()/send_buf() utility functions and some improvements | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 9 this patch: 9
netdev/cc_maintainers success CCed 3 of 3 maintainers
netdev/build_clang success Errors and warnings before: 9 this patch: 9
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 9 this patch: 9
netdev/checkpatch warning WARNING: line length of 82 exceeds 80 columns WARNING: line length of 90 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Stefano Garzarella Sept. 15, 2023, 12:14 p.m. UTC
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: Stefano Garzarella <sgarzare@redhat.com>
---
 tools/testing/vsock/util.h |  1 +
 tools/testing/vsock/util.c | 88 +++++++++++++++++++++++---------------
 2 files changed, 54 insertions(+), 35 deletions(-)

Comments

Arseniy Krasnov Sept. 15, 2023, 7:23 p.m. UTC | #1
On 15.09.2023 15:14, Stefano Garzarella wrote:
> 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: Stefano Garzarella <sgarzare@redhat.com>
> ---
>  tools/testing/vsock/util.h |  1 +
>  tools/testing/vsock/util.c | 88 +++++++++++++++++++++++---------------
>  2 files changed, 54 insertions(+), 35 deletions(-)

Reviewed-by: Arseniy Krasnov <avkrasnov@salutedevices.com>

> 
> diff --git a/tools/testing/vsock/util.h b/tools/testing/vsock/util.h
> index fb99208a95ea..fe31f267e67e 100644
> --- a/tools/testing/vsock/util.h
> +++ b/tools/testing/vsock/util.h
> @@ -42,6 +42,7 @@ 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 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,
> diff --git a/tools/testing/vsock/util.c b/tools/testing/vsock/util.c
> index 01b636d3039a..2826902706e8 100644
> --- a/tools/testing/vsock/util.c
> +++ b/tools/testing/vsock/util.c
> @@ -211,6 +211,58 @@ int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
>  	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.
>   *
>   * expected_ret:
> @@ -270,43 +322,9 @@ void send_byte(int fd, int expected_ret, int flags)
>  void recv_byte(int fd, int expected_ret, int flags)
>  {
>  	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) {
> -		perror("read");
> -		exit(EXIT_FAILURE);
> -	}
> -	if (nread == 0) {
> -		if (expected_ret == 0)
> -			return;
> +	recv_buf(fd, &byte, sizeof(byte), flags, expected_ret);
>  
> -		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') {
>  		fprintf(stderr, "unexpected byte read %c\n", byte);
>  		exit(EXIT_FAILURE);
diff mbox series

Patch

diff --git a/tools/testing/vsock/util.h b/tools/testing/vsock/util.h
index fb99208a95ea..fe31f267e67e 100644
--- a/tools/testing/vsock/util.h
+++ b/tools/testing/vsock/util.h
@@ -42,6 +42,7 @@  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 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,
diff --git a/tools/testing/vsock/util.c b/tools/testing/vsock/util.c
index 01b636d3039a..2826902706e8 100644
--- a/tools/testing/vsock/util.c
+++ b/tools/testing/vsock/util.c
@@ -211,6 +211,58 @@  int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
 	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.
  *
  * expected_ret:
@@ -270,43 +322,9 @@  void send_byte(int fd, int expected_ret, int flags)
 void recv_byte(int fd, int expected_ret, int flags)
 {
 	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) {
-		perror("read");
-		exit(EXIT_FAILURE);
-	}
-	if (nread == 0) {
-		if (expected_ret == 0)
-			return;
+	recv_buf(fd, &byte, sizeof(byte), flags, expected_ret);
 
-		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') {
 		fprintf(stderr, "unexpected byte read %c\n", byte);
 		exit(EXIT_FAILURE);