git: 367e8adb4be9 - main - tests/netgraph: start ng_socket test suite

From: Gleb Smirnoff <glebius_at_FreeBSD.org>
Date: Mon, 20 Nov 2023 22:37:19 UTC
The branch main has been updated by glebius:

URL: https://cgit.FreeBSD.org/src/commit/?id=367e8adb4be91ca3c0d40c085eaf47467d0ad25d

commit 367e8adb4be91ca3c0d40c085eaf47467d0ad25d
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2023-11-20 22:36:58 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2023-11-20 22:36:58 +0000

    tests/netgraph: start ng_socket test suite
    
    Just one check now, check node name.
---
 tests/sys/netgraph/Makefile |  2 ++
 tests/sys/netgraph/socket.c | 69 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 71 insertions(+)

diff --git a/tests/sys/netgraph/Makefile b/tests/sys/netgraph/Makefile
index 74e4046482ef..8bf91c68ad6b 100644
--- a/tests/sys/netgraph/Makefile
+++ b/tests/sys/netgraph/Makefile
@@ -14,11 +14,13 @@ TEST_METADATA.ng_macfilter_test+=	required_programs="perl"
 ATF_TESTS_C+=	basic		\
 		bridge		\
 		hub		\
+		socket		\
 		vlan_rotate	\
 
 SRCS.basic=	basic.c util.c
 SRCS.bridge=	bridge.c util.c
 SRCS.hub=	hub.c util.c
+SRCS.socket=	socket.c
 SRCS.vlan_rotate=vlan_rotate.c util.c
 
 LIBADD+=	netgraph
diff --git a/tests/sys/netgraph/socket.c b/tests/sys/netgraph/socket.c
new file mode 100644
index 000000000000..b5216dcc2c39
--- /dev/null
+++ b/tests/sys/netgraph/socket.c
@@ -0,0 +1,69 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2023 Gleb Smirnoff <glebius@FreeBSD.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/socket.h>
+#include <netgraph.h>
+#include <netgraph/ng_socket.h>
+
+#include <errno.h>
+
+#include <atf-c.h>
+
+ATF_TC_WITHOUT_HEAD(getsockname);
+ATF_TC_BODY(getsockname, tc)
+{
+	struct sockaddr_ng sg;
+	socklen_t len = sizeof(struct sockaddr_ng);
+#define	NAME	"0123456789012345678901234567891"	/* 31 chars */
+	char name[NG_NODESIZ] = NAME;
+	int cs;
+
+#if 0
+	/* Unnamed node. */
+	ATF_REQUIRE(NgMkSockNode(NULL, &cs, NULL) == 0);
+	ATF_REQUIRE(getsockname(cs, (struct sockaddr *)&sg, &len) == 0);
+	close(cs);
+	/* Unnamed node doesn't return any name/ID now. */
+#endif
+
+	/* Named node. */
+	ATF_REQUIRE(NgMkSockNode(name, &cs, NULL) == 0);
+	ATF_REQUIRE(getsockname(cs, (struct sockaddr *)&sg, &len) == 0);
+#if 0
+	/* sockaddr_ng truncates name now. */
+	ATF_REQUIRE(strcmp(sg.sg_data, NAME) == 0);
+#else
+	ATF_REQUIRE(strncmp(sg.sg_data, NAME, sizeof(sg.sg_data)) == 0);
+#endif
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+	ATF_TP_ADD_TC(tp, getsockname);
+
+	return (atf_no_error());
+}