git: 1f46c32c1f3d - main - tests/unix_seqpacket: provide function that returns bound & listening sock

From: Gleb Smirnoff <glebius_at_FreeBSD.org>
Date: Wed, 28 Feb 2024 22:33:09 UTC
The branch main has been updated by glebius:

URL: https://cgit.FreeBSD.org/src/commit/?id=1f46c32c1f3d0ea2a4c748a24aad06329cfb5f5a

commit 1f46c32c1f3d0ea2a4c748a24aad06329cfb5f5a
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2024-02-28 22:32:46 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2024-02-28 22:32:46 +0000

    tests/unix_seqpacket: provide function that returns bound & listening sock
    
    Use the function in mk_pair_of_sockets(), and in four existing tests -
    'listen_bound', 'connect', 'shutdown_send' and 'shutdown_send_sigpipe'.
    While here make mk_pair_of_sockets() return pointer to sockaddr_un instead
    of path.  This also fixes bug of returning pointer to stack memory of
    returning function.  However, the only caller that cares about this return
    is temporarily ifdefed out.  No functional change intended.
    
    Reviewed by:            tuexen
    Differential Revision:  https://reviews.freebsd.org/D43852
---
 tests/sys/kern/unix_seqpacket_test.c | 131 ++++++++++++++---------------------
 1 file changed, 52 insertions(+), 79 deletions(-)

diff --git a/tests/sys/kern/unix_seqpacket_test.c b/tests/sys/kern/unix_seqpacket_test.c
index be273fbe1246..3ea7bb3ea165 100644
--- a/tests/sys/kern/unix_seqpacket_test.c
+++ b/tests/sys/kern/unix_seqpacket_test.c
@@ -68,32 +68,50 @@ do_socketpair_nonblocking(int *sv)
 }
 
 /*
- * Returns a pair of sockets made the hard way: bind, listen, connect & accept
+ * Returns a bound and listening socket.
  * @return	const char* The path to the socket
  */
-static const char*
-mk_pair_of_sockets(int *sv)
+static const struct sockaddr_un *
+mk_listening_socket(int *sv)
 {
-	struct sockaddr_un sun;
 	/* ATF's isolation mechanisms will guarantee uniqueness of this file */
-	const char *path = "sock";
-	int s, err, s2, s1;
+	static const struct sockaddr_un sun = {
+		.sun_family = AF_LOCAL,
+		.sun_len = sizeof(sun),
+		.sun_path = "sock",
+	};
+	int s, r, l;
 
 	s = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
 	ATF_REQUIRE(s >= 0);
 
-	bzero(&sun, sizeof(sun));
-	sun.sun_family = AF_LOCAL;
-	sun.sun_len = sizeof(sun);
-	strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
-	err = bind(s, (struct sockaddr *)&sun, sizeof(sun));
-	err = listen(s, -1);
-	ATF_CHECK_EQ(0, err);
+	r = bind(s, (struct sockaddr *)&sun, sizeof(sun));
+	l = listen(s, -1);
+	ATF_CHECK_EQ(0, r);
+	ATF_CHECK_EQ(0, l);
+
+	if (sv != NULL)
+		*sv = s;
+
+	return (&sun);
+}
+
+/*
+ * Returns a pair of sockets made the hard way: bind, listen, connect & accept
+ * @return	const char* The path to the socket
+ */
+static const struct sockaddr_un *
+mk_pair_of_sockets(int *sv)
+{
+	const struct sockaddr_un *sun;
+	int s, s2, err, s1;
+
+	sun = mk_listening_socket(&s);
 
 	/* Create the other socket */
 	s2 = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
 	ATF_REQUIRE(s2 >= 0);
-	err = connect(s2, (struct sockaddr*)&sun, sizeof(sun));
+	err = connect(s2, (struct sockaddr *)sun, sizeof(*sun));
 	if (err != 0) {
 		perror("connect");
 		atf_tc_fail("connect(2) failed");
@@ -106,12 +124,14 @@ mk_pair_of_sockets(int *sv)
 		atf_tc_fail("accept(2) failed");
 	}
 
-	sv[0] = s1;
-	sv[1] = s2;
+	if (sv != NULL) {
+		sv[0] = s1;
+		sv[1] = s2;
+	}
 
 	close(s);
 
-	return (path);
+	return (sun);
 }
 
 static volatile sig_atomic_t got_sigpipe = 0;
@@ -454,22 +474,9 @@ ATF_TC_BODY(bind, tc)
 ATF_TC_WITHOUT_HEAD(listen_bound);
 ATF_TC_BODY(listen_bound, tc)
 {
-	struct sockaddr_un sun;
-	/* ATF's isolation mechanisms will guarantee uniqueness of this file */
-	const char *path = "sock";
-	int s, r, l;
-
-	s = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
-	ATF_REQUIRE(s >= 0);
+	int s;
 
-	bzero(&sun, sizeof(sun));
-	sun.sun_family = AF_LOCAL;
-	sun.sun_len = sizeof(sun);
-	strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
-	r = bind(s, (struct sockaddr *)&sun, sizeof(sun));
-	l = listen(s, -1);
-	ATF_CHECK_EQ(0, r);
-	ATF_CHECK_EQ(0, l);
+	(void)mk_listening_socket(&s);
 	close(s);
 }
 
@@ -477,27 +484,15 @@ ATF_TC_BODY(listen_bound, tc)
 ATF_TC_WITHOUT_HEAD(connect);
 ATF_TC_BODY(connect, tc)
 {
-	struct sockaddr_un sun;
-	/* ATF's isolation mechanisms will guarantee uniqueness of this file */
-	const char *path = "sock";
-	int s, r, err, l, s2;
+	const struct sockaddr_un *sun;
+	int s, err, s2;
 
-	s = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
-	ATF_REQUIRE(s >= 0);
-
-	bzero(&sun, sizeof(sun));
-	sun.sun_family = AF_LOCAL;
-	sun.sun_len = sizeof(sun);
-	strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
-	r = bind(s, (struct sockaddr *)&sun, sizeof(sun));
-	l = listen(s, -1);
-	ATF_CHECK_EQ(0, r);
-	ATF_CHECK_EQ(0, l);
+	sun = mk_listening_socket(&s);
 
 	/* Create the other socket */
 	s2 = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
 	ATF_REQUIRE(s2 >= 0);
-	err = connect(s2, (struct sockaddr*)&sun, sizeof(sun));
+	err = connect(s2, (struct sockaddr *)sun, sizeof(*sun));
 	if (err != 0) {
 		perror("connect");
 		atf_tc_fail("connect(2) failed");
@@ -670,7 +665,7 @@ ATF_TC_WITHOUT_HEAD(sendto_recvfrom);
 ATF_TC_BODY(sendto_recvfrom, tc)
 {
 #ifdef TEST_SEQ_PACKET_SOURCE_ADDRESS
-	const char* path;
+	const sockaddr_un *sun;
 #endif
 	struct sockaddr_storage from;
 	int sv[2];
@@ -683,7 +678,7 @@ ATF_TC_BODY(sendto_recvfrom, tc)
 
 	/* setup the socket pair */
 #ifdef TEST_SEQ_PACKET_SOURCE_ADDRESS
-	path =
+	sun =
 #endif
 		mk_pair_of_sockets(sv);
 
@@ -714,7 +709,7 @@ ATF_TC_BODY(sendto_recvfrom, tc)
 	 * these checks may be reenabled
 	 */
 	ATF_CHECK_EQ(PF_LOCAL, from.ss_family);
-	ATF_CHECK_STREQ(path, ((struct sockaddr_un*)&from)->sun_path);
+	ATF_CHECK_STREQ(sun->sun_path, ((struct sockaddr_un*)&from)->sun_path);
 #endif
 	close(sv[0]);
 	close(sv[1]);
@@ -756,28 +751,17 @@ ATF_TC_BODY(send_recv_with_connect, tc)
 ATF_TC_WITHOUT_HEAD(shutdown_send);
 ATF_TC_BODY(shutdown_send, tc)
 {
-	struct sockaddr_un sun;
-	/* ATF's isolation mechanisms will guarantee uniqueness of this file */
-	const char *path = "sock";
+	const struct sockaddr_un *sun;
 	const char *data = "data";
 	ssize_t datalen, ssize;
 	int s, err, s2;
 
-	s = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
-	ATF_REQUIRE(s >= 0);
-
-	bzero(&sun, sizeof(sun));
-	sun.sun_family = AF_LOCAL;
-	sun.sun_len = sizeof(sun);
-	strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
-	err = bind(s, (struct sockaddr *)&sun, sizeof(sun));
-	err = listen(s, -1);
-	ATF_CHECK_EQ(0, err);
+	sun = mk_listening_socket(&s);
 
 	/* Create the other socket */
 	s2 = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
 	ATF_REQUIRE(s2 >= 0);
-	err = connect(s2, (struct sockaddr*)&sun, sizeof(sun));
+	err = connect(s2, (struct sockaddr *)sun, sizeof(*sun));
 	if (err != 0) {
 		perror("connect");
 		atf_tc_fail("connect(2) failed");
@@ -797,28 +781,17 @@ ATF_TC_BODY(shutdown_send, tc)
 ATF_TC_WITHOUT_HEAD(shutdown_send_sigpipe);
 ATF_TC_BODY(shutdown_send_sigpipe, tc)
 {
-	struct sockaddr_un sun;
-	/* ATF's isolation mechanisms will guarantee uniqueness of this file */
-	const char *path = "sock";
+	const struct sockaddr_un *sun;
 	const char *data = "data";
 	ssize_t datalen;
 	int s, err, s2;
 
-	s = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
-	ATF_REQUIRE(s >= 0);
-
-	bzero(&sun, sizeof(sun));
-	sun.sun_family = AF_LOCAL;
-	sun.sun_len = sizeof(sun);
-	strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
-	err = bind(s, (struct sockaddr *)&sun, sizeof(sun));
-	err = listen(s, -1);
-	ATF_CHECK_EQ(0, err);
+	sun = mk_listening_socket(&s);
 
 	/* Create the other socket */
 	s2 = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
 	ATF_REQUIRE(s2 >= 0);
-	err = connect(s2, (struct sockaddr*)&sun, sizeof(sun));
+	err = connect(s2, (struct sockaddr *)sun, sizeof(*sun));
 	if (err != 0) {
 		perror("connect");
 		atf_tc_fail("connect(2) failed");