test/socket-tcp-skip-in-flight: clean up the test by using the test library

Submitted by Andrei Vagin on Jan. 9, 2019, 6:56 a.m.

Details

Message ID 20190109065650.4232-1-avagin@gmail.com
State Accepted
Series "test/socket-tcp-skip-in-flight: clean up the test by using the test library"
Headers show

Commit Message

Andrei Vagin Jan. 9, 2019, 6:56 a.m.
The test library has functions to create tcp sockets.

Cc: Radostin Stoyanov <rstoyanov1@gmail.com>
Signed-off-by: Andrei Vagin <avagin@gmail.com>
---
 test/zdtm/static/socket-tcp-skip-in-flight.c | 134 ++++++---------------------
 1 file changed, 30 insertions(+), 104 deletions(-)

Patch hide | download patch | download mbox

diff --git a/test/zdtm/static/socket-tcp-skip-in-flight.c b/test/zdtm/static/socket-tcp-skip-in-flight.c
index eef73d992..8805e7d1a 100644
--- a/test/zdtm/static/socket-tcp-skip-in-flight.c
+++ b/test/zdtm/static/socket-tcp-skip-in-flight.c
@@ -16,138 +16,64 @@  const char *test_author = "Radostin Stoyanov <rstoyanov1@gmail.com>";
  * in-flight TCP connections are ignored.
  */
 
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
 #include <sys/types.h>
 #include <sys/socket.h>
-#include <netinet/in.h>
-#include <netdb.h>
-#include <linux/types.h>
-#include <netinet/tcp.h>
 
-#define PORT 1234
-#define HOST "127.0.0.1"
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
 
-static int check_socket_state(int sk, int state)
-{
-		int err;
-		struct {
-				__u8    tcpi_state;
-		} info;
-		socklen_t len = sizeof(info);
-
-		err = getsockopt(sk, IPPROTO_TCP, TCP_INFO, (void *)&info, &len);
-		if (err != 0) {
-				pr_perror("Can't get socket state\n");
-				return -1;
-		} else if (info.tcpi_state != state) {
-				fail("Invalid socket state (%i)\n", (int)info.tcpi_state);
-				return -1;
-		}
-
-		return 0;
-}
 
-int open_socket()
+int main(int argc, char **argv)
 {
-	int fd;
-	fd = socket(ZDTM_FAMILY, SOCK_STREAM, 0);
-	if (fd < 0) {
-		fail("Failed to open socket\n");
-		return -1;
-	}
-	return fd;
-}
+	int port = 9990;
+	int fd_s, fd_c, fd;
+	int flags;
 
-int server()
-{
-	 int fd_s;
-	 struct sockaddr_in serv_addr;
+	test_init(argc, argv);
 
-	 fd_s = open_socket();
-	 if (fd_s < 0)
+	fd_s = tcp_init_server(ZDTM_FAMILY, &port);
+	if (fd_s < 0)
 		return -1;
 
-	 bzero((char *) &serv_addr, sizeof(serv_addr));
-	 serv_addr.sin_family = ZDTM_FAMILY;
-	 serv_addr.sin_addr.s_addr = INADDR_ANY;
-	 serv_addr.sin_port = htons(PORT);
-
-	 if (bind(fd_s, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
-		fail("Failed to bind");
+	flags = fcntl(fd_s, F_GETFL, 0);
+	if (fcntl(fd_s, F_SETFL, flags | O_NONBLOCK)) {
+		pr_perror("Unable to set O_NONBLOCK");
 		return -1;
-	 }
-
-	 listen(fd_s, 1);
-
-	 /* Listen but do not accept connect()-ed TCP connection. */
-
-	 return fd_s;
-}
-
-int client()
-{
-	int fd_c;
-	struct sockaddr_in serv_addr;
-	struct hostent *server;
+	}
 
-	fd_c = open_socket();
+	fd_c = tcp_init_client(ZDTM_FAMILY, "localhost", port);
 	if (fd_c < 0)
 		return -1;
 
-	server = gethostbyname(HOST);
-	if (server == NULL) {
-		fail("Failed to get host by name\n");
-		return -1;
-	}
+	test_daemon();
+	test_waitsig();
 
-	bzero((char *) &serv_addr, sizeof(serv_addr));
-	serv_addr.sin_family = ZDTM_FAMILY;
-	bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
-	serv_addr.sin_port = htons(PORT);
-	if (connect(fd_c,(struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
-		fail("Failed to get host by name\n");
-		return -1;
+	if (close(fd_c)) {
+		fail("Unable to close a client socket");
+		return 1;
 	}
 
-   return fd_c;
-}
-
-int main(int argc, char **argv)
-{
-	int fd_s;
-	int fd_c;
-
-	test_init(argc, argv);
-
-	fd_s = server();
-	if (fd_s < 0) {
-		fail("Failed to initialize server\n");
-		return -1;
-	}
+	fd = tcp_accept_server(fd_s);
+	if (fd >= 0)
+		close(fd);
 
-	fd_c = client();
+	fd_c = tcp_init_client(ZDTM_FAMILY, "localhost", port);
 	if (fd_c < 0) {
-		fail("Failed to initialize client\n");
+		fail("Unable to create a client socket");
 		return -1;
 	}
 
-	if (check_socket_state(fd_s, TCP_LISTEN)) {
-		fail("Server socket state before restore isn't TCP_LISTEN\n");
+	fd = tcp_accept_server(fd_s);
+	if (fd < 0) {
+		fail("Unable to accept a new connection");
 		return 1;
 	}
 
-	test_daemon();
-	test_waitsig();
+	close(fd);
 
-	if (check_socket_state(fd_s, TCP_LISTEN)) {
-		fail("Server socket state after restore isn't TCP_LISTEN\n");
-		return 1;
-	}
-
-	close(fd_s);
 	close(fd_c);
+	close(fd_s);
 
 	pass();
 	return 0;

Comments

Radostin Stoyanov Jan. 9, 2019, 10:19 a.m.
On 09/01/2019 06:56, Andrei Vagin wrote:
> The test library has functions to create tcp sockets.
>
> Cc: Radostin Stoyanov <rstoyanov1@gmail.com>
> Signed-off-by: Andrei Vagin <avagin@gmail.com>
> ---
>  test/zdtm/static/socket-tcp-skip-in-flight.c | 134 ++++++---------------------
>  1 file changed, 30 insertions(+), 104 deletions(-)
>
> diff --git a/test/zdtm/static/socket-tcp-skip-in-flight.c b/test/zdtm/static/socket-tcp-skip-in-flight.c
> index eef73d992..8805e7d1a 100644
> --- a/test/zdtm/static/socket-tcp-skip-in-flight.c
> +++ b/test/zdtm/static/socket-tcp-skip-in-flight.c

Reviewed-by: Radostin Stoyanov <rstoyanov1@gmail.com>

Radostin