From: Arjun Shankar <arjun@redhat.com>
To: Sergey Kolosov <skolosov@redhat.com>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH v3 2/2] socket: Add new test for connect
Date: Wed, 10 Apr 2024 18:13:53 +0200 [thread overview]
Message-ID: <CAG_osaay9k75y20P6zET+hLA-PCiEiSwswpMfG2y8f2aPtAuQQ@mail.gmail.com> (raw)
In-Reply-To: <20240410160006.176758-2-skolosov@redhat.com>
Hi Sergey,
> This commit adds a simple bind/accept/connect test for an IPv4 TCP
> connection to a local process via the loopback interface.
> ---
> Changes from v2: fix some indentation issues.
> ---
Thanks. This looks good to me.
Reviewed-by: Arjun Shankar <arjun@redhat.com>
I will push this along with the other patch in this series in a bit.
> socket/Makefile | 1 +
> socket/tst-connect.c | 113 +++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 114 insertions(+)
> create mode 100644 socket/tst-connect.c
>
> diff --git a/socket/Makefile b/socket/Makefile
> index 74ca5b8452..fc1bd0a260 100644
> --- a/socket/Makefile
> +++ b/socket/Makefile
> @@ -70,6 +70,7 @@ tests := \
> tst-accept4 \
> tst-cmsg_cloexec \
> tst-cmsghdr \
> + tst-connect \
> tst-sockopt \
> # tests
>
> diff --git a/socket/tst-connect.c b/socket/tst-connect.c
> new file mode 100644
> index 0000000000..ec2fdd92c0
> --- /dev/null
> +++ b/socket/tst-connect.c
> @@ -0,0 +1,113 @@
> +/* Test the connect function.
> + Copyright (C) 2024 Free Software Foundation, Inc.
> + This file is part of the GNU C Library.
> +
> + The GNU C Library is free software; you can redistribute it and/or
> + modify it under the terms of the GNU Lesser General Public
> + License as published by the Free Software Foundation; either
> + version 2.1 of the License, or (at your option) any later version.
> +
> + The GNU C Library is distributed in the hope that it will be useful,
> + but WITHOUT ANY WARRANTY; without even the implied warranty of
> + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> + Lesser General Public License for more details.
> +
> + You should have received a copy of the GNU Lesser General Public
> + License along with the GNU C Library; if not, see
> + <https://www.gnu.org/licenses/>. */
> +
> +#include <arpa/inet.h>
> +#include <errno.h>
> +#include <fcntl.h>
> +#include <signal.h>
> +#include <stdbool.h>
> +#include <support/check.h>
> +#include <support/xsocket.h>
> +#include <support/xunistd.h>
> +#include <sys/socket.h>
> +#include <stdio.h>
> +
> +static struct sockaddr_in server_address;
> +
> +int
> +open_socket_inet_tcp (void)
> +{
> + int fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
> + if (fd < 0)
> + {
> + if (errno == EAFNOSUPPORT)
> + FAIL_UNSUPPORTED ("The host does not support IPv4");
> + else
> + FAIL_EXIT1 ("socket (AF_INET, SOCK_STREAM, IPPROTO_TCP): %m\n");
> + }
> + return fd;
> +}
> +
> +static pid_t
> +start_server (void)
> +{
> + server_address.sin_family = AF_INET;
> + server_address.sin_port = 0;
> + server_address.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
> +
> + int server_sock = open_socket_inet_tcp ();
> +
> + xbind (server_sock, (struct sockaddr *) &server_address,
> + sizeof (server_address));
> +
> + socklen_t sa_len = sizeof (server_address);
> + xgetsockname (server_sock, (struct sockaddr *) &server_address, &sa_len);
> + xlisten (server_sock, 5);
> +
> + pid_t my_pid = xfork ();
> + if (my_pid > 0)
> + {
> + xclose (server_sock);
> + return my_pid;
> + }
> +
> + struct sockaddr_in client_address;
> + socklen_t ca_len = sizeof (server_address);
> + int client_sock = xaccept (server_sock, (struct sockaddr *) &client_address,
> + &ca_len);
> + printf ("socket accepted %d\n", client_sock);
> +
> + _exit (0);
> +}
> +
> +static int
> +do_test (void)
> +{
> + pid_t serv_pid;
> + struct sockaddr_in peer;
> + socklen_t peer_len;
> +
> + serv_pid = start_server ();
> + int client_sock = open_socket_inet_tcp ();
> + xconnect (client_sock, (const struct sockaddr *) &server_address,
> + sizeof (server_address));
> +
> + /* A second connect with same arguments should fail with EISCONN. */
> + int result = connect (client_sock,
> + (const struct sockaddr *) &server_address,
> + sizeof (server_address));
> + if (result == 0 || errno != EISCONN)
> + FAIL_EXIT1 ("Second connect (%d), should fail with EISCONN: %m",
> + client_sock);
> +
> + peer_len = sizeof (peer);
> + xgetpeername (client_sock, (struct sockaddr *) &peer, &peer_len);
> + TEST_COMPARE (peer_len, sizeof (peer));
> + TEST_COMPARE (peer.sin_port, server_address.sin_port);
> + TEST_COMPARE_BLOB (&peer.sin_addr, sizeof (peer.sin_addr),
> + &server_address.sin_addr,
> + sizeof (server_address.sin_addr));
> +
> + int status;
> + xwaitpid (serv_pid, &status, 0);
> + TEST_COMPARE (status, 0);
> +
> + return 0;
> +}
> +
> +#include <support/test-driver.c>
> --
> 2.44.0
>
--
Arjun Shankar
he/him/his
prev parent reply other threads:[~2024-04-10 16:54 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-10 15:58 [PATCH v3 1/2] libsupport: Add xgetpeername Sergey Kolosov
2024-04-10 15:58 ` [PATCH v3 2/2] socket: Add new test for connect Sergey Kolosov
2024-04-10 16:13 ` Arjun Shankar [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAG_osaay9k75y20P6zET+hLA-PCiEiSwswpMfG2y8f2aPtAuQQ@mail.gmail.com \
--to=arjun@redhat.com \
--cc=libc-alpha@sourceware.org \
--cc=skolosov@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).