xref: /freebsd/tests/sys/netlink/test_snl_generic.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 
5 #include <sys/param.h>
6 #include <sys/module.h>
7 
8 #include <netlink/netlink.h>
9 #include "netlink/netlink_snl.h"
10 #include "netlink/netlink_snl_generic.h"
11 
12 #include <atf-c.h>
13 
14 static void
15 require_netlink(void)
16 {
17 	if (modfind("netlink") == -1)
18 		atf_tc_skip("netlink module not loaded");
19 }
20 
21 ATF_TC(snl_verify_genl_parsers);
22 ATF_TC_HEAD(snl_verify_genl_parsers, tc)
23 {
24 	atf_tc_set_md_var(tc, "descr", "Tests snl(3) generic parsers are correct");
25 }
26 
27 ATF_TC_BODY(snl_verify_genl_parsers, tc)
28 {
29 	SNL_VERIFY_PARSERS(snl_all_genl_parsers);
30 
31 }
32 
33 ATF_TC(test_snl_get_genl_family_success);
34 ATF_TC_HEAD(test_snl_get_genl_family_success, tc)
35 {
36 	atf_tc_set_md_var(tc, "descr", "Tests successfull resolution of the 'nlctrl' family");
37 }
38 
39 ATF_TC_BODY(test_snl_get_genl_family_success, tc)
40 {
41 	struct snl_state ss;
42 
43 	require_netlink();
44 
45 	if (!snl_init(&ss, NETLINK_GENERIC))
46 		atf_tc_fail("snl_init() failed");
47 
48 	ATF_CHECK_EQ(snl_get_genl_family(&ss, "nlctrl"), GENL_ID_CTRL);
49 }
50 
51 ATF_TC(test_snl_get_genl_family_failure);
52 ATF_TC_HEAD(test_snl_get_genl_family_failure, tc)
53 {
54 	atf_tc_set_md_var(tc, "descr", "Tests unsuccessfull resolution of 'no-such-family' family");
55 }
56 
57 ATF_TC_BODY(test_snl_get_genl_family_failure, tc)
58 {
59 	struct snl_state ss;
60 
61 	require_netlink();
62 
63 	if (!snl_init(&ss, NETLINK_GENERIC))
64 		atf_tc_fail("snl_init() failed");
65 
66 	ATF_CHECK_EQ(snl_get_genl_family(&ss, "no-such-family"), 0);
67 }
68 
69 ATF_TC(test_snl_get_genl_family_groups);
70 ATF_TC_HEAD(test_snl_get_genl_family_groups, tc)
71 {
72 	atf_tc_set_md_var(tc, "descr", "Tests getting 'nlctrl' groups");
73 }
74 
75 ATF_TC_BODY(test_snl_get_genl_family_groups, tc)
76 {
77 	struct snl_state ss;
78 	struct snl_writer nw;
79 	struct nlmsghdr *hdr;
80 
81 	require_netlink();
82 
83 	if (!snl_init(&ss, NETLINK_GENERIC))
84 		atf_tc_fail("snl_init() failed");
85 
86 	snl_init_writer(&ss, &nw);
87 	hdr = snl_create_genl_msg_request(&nw, GENL_ID_CTRL, CTRL_CMD_GETFAMILY);
88 	snl_add_msg_attr_string(&nw, CTRL_ATTR_FAMILY_NAME, "nlctrl");
89 	hdr = snl_finalize_msg(&nw);
90 	snl_send_message(&ss, hdr);
91 
92 	hdr = snl_read_reply(&ss, hdr->nlmsg_seq);
93 	ATF_CHECK(hdr != NULL);
94 	ATF_CHECK(hdr->nlmsg_type != NLMSG_ERROR);
95 
96 	struct _getfamily_attrs attrs = {};
97 
98 	ATF_CHECK(snl_parse_nlmsg(&ss, hdr, &_genl_ctrl_getfam_parser, &attrs));
99 	ATF_CHECK_EQ(attrs.mcast_groups.num_groups, 1);
100 
101 	struct snl_genl_ctrl_mcast_group *group = attrs.mcast_groups.groups[0];
102 
103 	ATF_CHECK(group->mcast_grp_id > 0);
104 	ATF_CHECK(!strcmp(group->mcast_grp_name, "notify"));
105 }
106 
107 ATF_TP_ADD_TCS(tp)
108 {
109 	ATF_TP_ADD_TC(tp, snl_verify_genl_parsers);
110 	ATF_TP_ADD_TC(tp, test_snl_get_genl_family_success);
111 	ATF_TP_ADD_TC(tp, test_snl_get_genl_family_failure);
112 	ATF_TP_ADD_TC(tp, test_snl_get_genl_family_groups);
113 
114 	return (atf_no_error());
115 }
116 
117