git: 8338ea1d3cc7 - main - tests/netlink: add minimal test for a group writer

From: Gleb Smirnoff <glebius_at_FreeBSD.org>
Date: Wed, 10 Jan 2024 00:58:25 UTC
The branch main has been updated by glebius:

URL: https://cgit.FreeBSD.org/src/commit/?id=8338ea1d3cc705f08674285a884d3aa485bf6290

commit 8338ea1d3cc705f08674285a884d3aa485bf6290
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2024-01-10 00:56:51 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2024-01-10 00:56:51 +0000

    tests/netlink: add minimal test for a group writer
    
    Subscribe a socket for RTNLGRP_IPV4_ROUTE announcements, add & delete
    a route and check that announcements came in.
---
 tests/sys/netlink/netlink_socket.c | 49 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)

diff --git a/tests/sys/netlink/netlink_socket.c b/tests/sys/netlink/netlink_socket.c
index 51a3824a31f2..3015a585199b 100644
--- a/tests/sys/netlink/netlink_socket.c
+++ b/tests/sys/netlink/netlink_socket.c
@@ -39,6 +39,9 @@
 #include <netlink/netlink.h>
 #include <netlink/netlink_route.h>
 
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
 #include <atf-c.h>
 
 static struct itimerval itv = {
@@ -253,6 +256,51 @@ ATF_TC_BODY(sizes, tc)
 	cmsg_check(&msg);
 }
 
+/*
+ * Check that NETLINK_ADD_MEMBERSHIP subscribes us.  Add & delete a temporary
+ * route and check if announcements came in.
+ */
+ATF_TC_WITHOUT_HEAD(membership);
+ATF_TC_BODY(membership, tc)
+{
+	struct {
+		struct nlmsghdr hdr;
+		struct rtmsg rtm;
+		struct nlattr rta_dst;
+		struct in_addr dst;
+		struct nlattr rta_oif;
+		uint32_t oif;
+	} msg = {
+		.hdr.nlmsg_type = RTM_NEWROUTE,
+		.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL,
+		.hdr.nlmsg_len = sizeof(msg),
+		.rtm.rtm_family = AF_INET,
+		.rtm.rtm_protocol = RTPROT_STATIC,
+		.rtm.rtm_type = RTN_UNICAST,
+		.rtm.rtm_dst_len = 32,
+		.rta_dst.nla_type = RTA_DST,
+		.rta_dst.nla_len = sizeof(struct in_addr) +
+		    sizeof(struct nlattr),
+		.dst.s_addr = inet_addr("127.0.0.127"),
+		.rta_oif.nla_type = RTA_OIF,
+		.rta_oif.nla_len = sizeof(uint32_t) + sizeof(struct nlattr),
+		.oif = 1,
+	};
+	int fd;
+
+	ATF_REQUIRE((fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) != -1);
+	ATF_REQUIRE(setsockopt(fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
+	    &(int){RTNLGRP_IPV4_ROUTE}, sizeof(int)) == 0);
+	ATF_REQUIRE(send(fd, &msg, sizeof(msg), 0) == sizeof(msg));
+	msg.hdr.nlmsg_type = RTM_DELROUTE;
+	msg.hdr.nlmsg_len -= sizeof(struct nlattr) + sizeof(uint32_t);
+	ATF_REQUIRE(send(fd, &msg, msg.hdr.nlmsg_len, 0) == msg.hdr.nlmsg_len);
+	ATF_REQUIRE(recv(fd, &msg, sizeof(msg), 0) == sizeof(msg));
+	ATF_REQUIRE(msg.hdr.nlmsg_type == RTM_NEWROUTE);
+	ATF_REQUIRE(recv(fd, &msg, sizeof(msg), 0) == sizeof(msg));
+	ATF_REQUIRE(msg.hdr.nlmsg_type == RTM_DELROUTE);
+}
+
 ATF_TP_ADD_TCS(tp)
 {
 	if (modfind("netlink") == -1)
@@ -261,6 +309,7 @@ ATF_TP_ADD_TCS(tp)
 	ATF_TP_ADD_TC(tp, overflow);
 	ATF_TP_ADD_TC(tp, peek);
 	ATF_TP_ADD_TC(tp, sizes);
+	ATF_TP_ADD_TC(tp, membership);
 
 	return (atf_no_error());
 }