test/vsock: add dobule bind connect test
authorFilippo Storniolo <f.storniolo95@gmail.com>
Fri, 3 Nov 2023 17:55:51 +0000 (18:55 +0100)
committerDavid S. Miller <davem@davemloft.net>
Tue, 7 Nov 2023 22:27:07 +0000 (22:27 +0000)
This add bind connect test which creates a listening server socket
and tries to connect a client with a bound local port to it twice.

Co-developed-by: Luigi Leonardi <luigi.leonardi@outlook.com>
Signed-off-by: Luigi Leonardi <luigi.leonardi@outlook.com>
Signed-off-by: Filippo Storniolo <f.storniolo95@gmail.com>
Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
tools/testing/vsock/util.c
tools/testing/vsock/util.h
tools/testing/vsock/vsock_test.c

index 2fc96f29bdf26cdf5f7b2be0577035b46c260fb7..ae2b33c21c452a7305bf4239aee33036327f8e8c 100644 (file)
@@ -85,6 +85,48 @@ void vsock_wait_remote_close(int fd)
        close(epollfd);
 }
 
+/* Bind to <bind_port>, connect to <cid, port> and return the file descriptor. */
+int vsock_bind_connect(unsigned int cid, unsigned int port, unsigned int bind_port, int type)
+{
+       struct sockaddr_vm sa_client = {
+               .svm_family = AF_VSOCK,
+               .svm_cid = VMADDR_CID_ANY,
+               .svm_port = bind_port,
+       };
+       struct sockaddr_vm sa_server = {
+               .svm_family = AF_VSOCK,
+               .svm_cid = cid,
+               .svm_port = port,
+       };
+
+       int client_fd, ret;
+
+       client_fd = socket(AF_VSOCK, type, 0);
+       if (client_fd < 0) {
+               perror("socket");
+               exit(EXIT_FAILURE);
+       }
+
+       if (bind(client_fd, (struct sockaddr *)&sa_client, sizeof(sa_client))) {
+               perror("bind");
+               exit(EXIT_FAILURE);
+       }
+
+       timeout_begin(TIMEOUT);
+       do {
+               ret = connect(client_fd, (struct sockaddr *)&sa_server, sizeof(sa_server));
+               timeout_check("connect");
+       } while (ret < 0 && errno == EINTR);
+       timeout_end();
+
+       if (ret < 0) {
+               perror("connect");
+               exit(EXIT_FAILURE);
+       }
+
+       return client_fd;
+}
+
 /* Connect to <cid, port> and return the file descriptor. */
 static int vsock_connect(unsigned int cid, unsigned int port, int type)
 {
@@ -223,6 +265,11 @@ int vsock_stream_accept(unsigned int cid, unsigned int port,
        return vsock_accept(cid, port, clientaddrp, SOCK_STREAM);
 }
 
+int vsock_stream_listen(unsigned int cid, unsigned int port)
+{
+       return vsock_listen(cid, port, SOCK_STREAM);
+}
+
 int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
                           struct sockaddr_vm *clientaddrp)
 {
index a77175d25864c7a628400c01dff790e4a8141095..03c88d0cb8610b5d106379fe36a61c8734cd4f5f 100644 (file)
@@ -36,9 +36,12 @@ struct test_case {
 void init_signals(void);
 unsigned int parse_cid(const char *str);
 int vsock_stream_connect(unsigned int cid, unsigned int port);
+int vsock_bind_connect(unsigned int cid, unsigned int port,
+                      unsigned int bind_port, int type);
 int vsock_seqpacket_connect(unsigned int cid, unsigned int port);
 int vsock_stream_accept(unsigned int cid, unsigned int port,
                        struct sockaddr_vm *clientaddrp);
+int vsock_stream_listen(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);
index c1f7bc9abd22319fb34fcf8b437071f0493153b0..5b0e93f9996cb18cc390d33645132532ab6bb85c 100644 (file)
@@ -1180,6 +1180,51 @@ static void test_stream_shutrd_server(const struct test_opts *opts)
        close(fd);
 }
 
+static void test_double_bind_connect_server(const struct test_opts *opts)
+{
+       int listen_fd, client_fd, i;
+       struct sockaddr_vm sa_client;
+       socklen_t socklen_client = sizeof(sa_client);
+
+       listen_fd = vsock_stream_listen(VMADDR_CID_ANY, 1234);
+
+       for (i = 0; i < 2; i++) {
+               control_writeln("LISTENING");
+
+               timeout_begin(TIMEOUT);
+               do {
+                       client_fd = accept(listen_fd, (struct sockaddr *)&sa_client,
+                                          &socklen_client);
+                       timeout_check("accept");
+               } while (client_fd < 0 && errno == EINTR);
+               timeout_end();
+
+               if (client_fd < 0) {
+                       perror("accept");
+                       exit(EXIT_FAILURE);
+               }
+
+               /* Waiting for remote peer to close connection */
+               vsock_wait_remote_close(client_fd);
+       }
+
+       close(listen_fd);
+}
+
+static void test_double_bind_connect_client(const struct test_opts *opts)
+{
+       int i, client_fd;
+
+       for (i = 0; i < 2; i++) {
+               /* Wait until server is ready to accept a new connection */
+               control_expectln("LISTENING");
+
+               client_fd = vsock_bind_connect(opts->peer_cid, 1234, 4321, SOCK_STREAM);
+
+               close(client_fd);
+       }
+}
+
 static struct test_case test_cases[] = {
        {
                .name = "SOCK_STREAM connection reset",
@@ -1285,6 +1330,11 @@ static struct test_case test_cases[] = {
                .run_client = test_stream_msgzcopy_empty_errq_client,
                .run_server = test_stream_msgzcopy_empty_errq_server,
        },
+       {
+               .name = "SOCK_STREAM double bind connect",
+               .run_client = test_double_bind_connect_client,
+               .run_server = test_double_bind_connect_server,
+       },
        {},
 };