xref: /freebsd/tests/sys/netgraph/hub.c (revision d99a40e5ba23a2b00e90fba73205fcbc7abc1388)
1*7863faa7SLutz Donnerhacke /*-
2*7863faa7SLutz Donnerhacke  * SPDX-License-Identifier: BSD-3-Clause
3*7863faa7SLutz Donnerhacke  *
4*7863faa7SLutz Donnerhacke  * Copyright 2021 Lutz Donnerhacke
5*7863faa7SLutz Donnerhacke  *
6*7863faa7SLutz Donnerhacke  * Redistribution and use in source and binary forms, with or without
7*7863faa7SLutz Donnerhacke  * modification, are permitted provided that the following conditions
8*7863faa7SLutz Donnerhacke  * are met:
9*7863faa7SLutz Donnerhacke  *
10*7863faa7SLutz Donnerhacke  * 1. Redistributions of source code must retain the above copyright
11*7863faa7SLutz Donnerhacke  *    notice, this list of conditions and the following disclaimer.
12*7863faa7SLutz Donnerhacke  * 2. Redistributions in binary form must reproduce the above
13*7863faa7SLutz Donnerhacke  *    copyright notice, this list of conditions and the following
14*7863faa7SLutz Donnerhacke  *    disclaimer in the documentation and/or other materials provided
15*7863faa7SLutz Donnerhacke  *    with the distribution.
16*7863faa7SLutz Donnerhacke  * 3. Neither the name of the copyright holder nor the names of its
17*7863faa7SLutz Donnerhacke  *    contributors may be used to endorse or promote products derived
18*7863faa7SLutz Donnerhacke  *    from this software without specific prior written permission.
19*7863faa7SLutz Donnerhacke  *
20*7863faa7SLutz Donnerhacke  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
21*7863faa7SLutz Donnerhacke  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
22*7863faa7SLutz Donnerhacke  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23*7863faa7SLutz Donnerhacke  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24*7863faa7SLutz Donnerhacke  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25*7863faa7SLutz Donnerhacke  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26*7863faa7SLutz Donnerhacke  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27*7863faa7SLutz Donnerhacke  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28*7863faa7SLutz Donnerhacke  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29*7863faa7SLutz Donnerhacke  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*7863faa7SLutz Donnerhacke  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
31*7863faa7SLutz Donnerhacke  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*7863faa7SLutz Donnerhacke  * SUCH DAMAGE.
33*7863faa7SLutz Donnerhacke  */
34*7863faa7SLutz Donnerhacke #include <atf-c.h>
35*7863faa7SLutz Donnerhacke #include <errno.h>
36*7863faa7SLutz Donnerhacke #include <stdio.h>
37*7863faa7SLutz Donnerhacke 
38*7863faa7SLutz Donnerhacke #include "util.h"
39*7863faa7SLutz Donnerhacke 
40*7863faa7SLutz Donnerhacke ATF_TC(basic);
ATF_TC_HEAD(basic,conf)41*7863faa7SLutz Donnerhacke ATF_TC_HEAD(basic, conf)
42*7863faa7SLutz Donnerhacke {
43*7863faa7SLutz Donnerhacke 	atf_tc_set_md_var(conf, "require.user", "root");
44*7863faa7SLutz Donnerhacke }
45*7863faa7SLutz Donnerhacke 
ATF_TC_BODY(basic,dummy)46*7863faa7SLutz Donnerhacke ATF_TC_BODY(basic, dummy)
47*7863faa7SLutz Donnerhacke {
48*7863faa7SLutz Donnerhacke 	char		msg[] = "test";
49*7863faa7SLutz Donnerhacke 	ng_counter_t	r;
50*7863faa7SLutz Donnerhacke 
51*7863faa7SLutz Donnerhacke 	ng_errors(PASS);
52*7863faa7SLutz Donnerhacke 	ng_shutdown("hub:");
53*7863faa7SLutz Donnerhacke 	ng_errors(FAIL);
54*7863faa7SLutz Donnerhacke 
55*7863faa7SLutz Donnerhacke 	ng_init();
56*7863faa7SLutz Donnerhacke 	ng_mkpeer(".", "a", "hub", "a");
57*7863faa7SLutz Donnerhacke 	ng_name("a", "hub");
58*7863faa7SLutz Donnerhacke 	ng_connect(".", "b", "hub:", "b");
59*7863faa7SLutz Donnerhacke 	ng_connect(".", "c", "hub:", "c");
60*7863faa7SLutz Donnerhacke 
61*7863faa7SLutz Donnerhacke 	/* do not bounce back */
62*7863faa7SLutz Donnerhacke 	ng_register_data("a", get_data0);
63*7863faa7SLutz Donnerhacke 	ng_counter_clear(r);
64*7863faa7SLutz Donnerhacke 	ng_send_data("a", msg, sizeof(msg));
65*7863faa7SLutz Donnerhacke 	ng_handle_events(50, r);
66*7863faa7SLutz Donnerhacke 	ATF_CHECK(r[0] == 0);
67*7863faa7SLutz Donnerhacke 
68*7863faa7SLutz Donnerhacke 	/* send to others */
69*7863faa7SLutz Donnerhacke 	ng_register_data("b", get_data0);
70*7863faa7SLutz Donnerhacke 	ng_register_data("c", get_data0);
71*7863faa7SLutz Donnerhacke 	ng_counter_clear(r);
72*7863faa7SLutz Donnerhacke 	ng_send_data("a", msg, sizeof(msg));
73*7863faa7SLutz Donnerhacke 	ng_handle_events(50, r);
74*7863faa7SLutz Donnerhacke 	ATF_CHECK(r[0] == 2);
75*7863faa7SLutz Donnerhacke 
76*7863faa7SLutz Donnerhacke 	ng_counter_clear(r);
77*7863faa7SLutz Donnerhacke 	ng_send_data("b", msg, sizeof(msg));
78*7863faa7SLutz Donnerhacke 	ng_handle_events(50, r);
79*7863faa7SLutz Donnerhacke 	ATF_CHECK(r[0] == 2);
80*7863faa7SLutz Donnerhacke 
81*7863faa7SLutz Donnerhacke 	ng_counter_clear(r);
82*7863faa7SLutz Donnerhacke 	ng_send_data("c", msg, sizeof(msg));
83*7863faa7SLutz Donnerhacke 	ng_handle_events(50, r);
84*7863faa7SLutz Donnerhacke 	ATF_CHECK(r[0] == 2);
85*7863faa7SLutz Donnerhacke 
86*7863faa7SLutz Donnerhacke 	/* remove a link */
87*7863faa7SLutz Donnerhacke 	ng_rmhook(".", "b");
88*7863faa7SLutz Donnerhacke 	ng_counter_clear(r);
89*7863faa7SLutz Donnerhacke 	ng_send_data("a", msg, sizeof(msg));
90*7863faa7SLutz Donnerhacke 	ng_handle_events(50, r);
91*7863faa7SLutz Donnerhacke 	ATF_CHECK(r[0] == 1);
92*7863faa7SLutz Donnerhacke 
93*7863faa7SLutz Donnerhacke 	ng_shutdown("hub:");
94*7863faa7SLutz Donnerhacke }
95*7863faa7SLutz Donnerhacke 
96*7863faa7SLutz Donnerhacke ATF_TC(persistence);
ATF_TC_HEAD(persistence,conf)97*7863faa7SLutz Donnerhacke ATF_TC_HEAD(persistence, conf)
98*7863faa7SLutz Donnerhacke {
99*7863faa7SLutz Donnerhacke 	atf_tc_set_md_var(conf, "require.user", "root");
100*7863faa7SLutz Donnerhacke }
101*7863faa7SLutz Donnerhacke 
ATF_TC_BODY(persistence,dummy)102*7863faa7SLutz Donnerhacke ATF_TC_BODY(persistence, dummy)
103*7863faa7SLutz Donnerhacke {
104*7863faa7SLutz Donnerhacke 	ng_errors(PASS);
105*7863faa7SLutz Donnerhacke 	ng_shutdown("hub:");
106*7863faa7SLutz Donnerhacke 	ng_errors(FAIL);
107*7863faa7SLutz Donnerhacke 
108*7863faa7SLutz Donnerhacke 	ng_init();
109*7863faa7SLutz Donnerhacke 	ng_mkpeer(".", "a", "hub", "a");
110*7863faa7SLutz Donnerhacke 	ng_name("a", "hub");
111*7863faa7SLutz Donnerhacke 
112*7863faa7SLutz Donnerhacke 	ng_send_msg("hub:", "setpersistent");
113*7863faa7SLutz Donnerhacke 	ng_rmhook(".", "a");
114*7863faa7SLutz Donnerhacke 
115*7863faa7SLutz Donnerhacke 	ng_shutdown("hub:");
116*7863faa7SLutz Donnerhacke }
117*7863faa7SLutz Donnerhacke 
118*7863faa7SLutz Donnerhacke ATF_TC(loop);
ATF_TC_HEAD(loop,conf)119*7863faa7SLutz Donnerhacke ATF_TC_HEAD(loop, conf)
120*7863faa7SLutz Donnerhacke {
121*7863faa7SLutz Donnerhacke 	atf_tc_set_md_var(conf, "require.user", "root");
122*7863faa7SLutz Donnerhacke }
123*7863faa7SLutz Donnerhacke 
ATF_TC_BODY(loop,dummy)124*7863faa7SLutz Donnerhacke ATF_TC_BODY(loop, dummy)
125*7863faa7SLutz Donnerhacke {
126*7863faa7SLutz Donnerhacke 	ng_counter_t	r;
127*7863faa7SLutz Donnerhacke 	int		i;
128*7863faa7SLutz Donnerhacke 	char		msg[] = "LOOP Alert!";
129*7863faa7SLutz Donnerhacke 
130*7863faa7SLutz Donnerhacke 	ng_errors(PASS);
131*7863faa7SLutz Donnerhacke 	ng_shutdown("hub1:");
132*7863faa7SLutz Donnerhacke 	ng_shutdown("hub2:");
133*7863faa7SLutz Donnerhacke 	ng_errors(FAIL);
134*7863faa7SLutz Donnerhacke 
135*7863faa7SLutz Donnerhacke 	ng_init();
136*7863faa7SLutz Donnerhacke 	ng_mkpeer(".", "a", "hub", "a");
137*7863faa7SLutz Donnerhacke 	ng_name("a", "hub1");
138*7863faa7SLutz Donnerhacke 	ng_mkpeer(".", "b", "hub", "b");
139*7863faa7SLutz Donnerhacke 	ng_name("b", "hub2");
140*7863faa7SLutz Donnerhacke 
141*7863faa7SLutz Donnerhacke 	ng_register_data("a", get_data0);
142*7863faa7SLutz Donnerhacke 	ng_register_data("b", get_data0);
143*7863faa7SLutz Donnerhacke 
144*7863faa7SLutz Donnerhacke 	/*-
145*7863faa7SLutz Donnerhacke 	 * Open loop
146*7863faa7SLutz Donnerhacke 	 *
147*7863faa7SLutz Donnerhacke 	 *    /-- hub1
148*7863faa7SLutz Donnerhacke 	 * . <    |
149*7863faa7SLutz Donnerhacke 	 *    \-- hub2
150*7863faa7SLutz Donnerhacke 	 */
151*7863faa7SLutz Donnerhacke 	ng_connect("hub1:", "xc1", "hub2:", "xc1");
152*7863faa7SLutz Donnerhacke 
153*7863faa7SLutz Donnerhacke 	ng_counter_clear(r);
154*7863faa7SLutz Donnerhacke 	ng_send_data("a", msg, sizeof(msg));
155*7863faa7SLutz Donnerhacke 	ng_handle_events(50, r);
156*7863faa7SLutz Donnerhacke 	ATF_CHECK(r[0] == 1);
157*7863faa7SLutz Donnerhacke 
158*7863faa7SLutz Donnerhacke 	/*-
159*7863faa7SLutz Donnerhacke 	 * Closed loop, DANGEROUS!
160*7863faa7SLutz Donnerhacke 	 *
161*7863faa7SLutz Donnerhacke 	 *    /-- hub1 -\
162*7863faa7SLutz Donnerhacke 	 * . <     |    |
163*7863faa7SLutz Donnerhacke 	 *    \-- hub2 -/
164*7863faa7SLutz Donnerhacke 	 */
165*7863faa7SLutz Donnerhacke 	ng_connect("hub1:", "xc2", "hub2:", "xc2");
166*7863faa7SLutz Donnerhacke 
167*7863faa7SLutz Donnerhacke 	ng_counter_clear(r);
168*7863faa7SLutz Donnerhacke 	ng_send_data("a", msg, sizeof(msg));
169*7863faa7SLutz Donnerhacke 	for (i = 0; i < 10; i++)	/* don't run forever */
170*7863faa7SLutz Donnerhacke 		if (!ng_handle_event(50, r))
171*7863faa7SLutz Donnerhacke 			break;
172*7863faa7SLutz Donnerhacke 	ATF_CHECK(r[0] > 7);
173*7863faa7SLutz Donnerhacke 
174*7863faa7SLutz Donnerhacke 	ng_shutdown("hub1:");
175*7863faa7SLutz Donnerhacke 	ng_shutdown("hub2:");
176*7863faa7SLutz Donnerhacke }
177*7863faa7SLutz Donnerhacke 
178*7863faa7SLutz Donnerhacke ATF_TC(many_hooks);
ATF_TC_HEAD(many_hooks,conf)179*7863faa7SLutz Donnerhacke ATF_TC_HEAD(many_hooks, conf)
180*7863faa7SLutz Donnerhacke {
181*7863faa7SLutz Donnerhacke 	atf_tc_set_md_var(conf, "require.user", "root");
182*7863faa7SLutz Donnerhacke }
183*7863faa7SLutz Donnerhacke 
ATF_TC_BODY(many_hooks,dummy)184*7863faa7SLutz Donnerhacke ATF_TC_BODY(many_hooks, dummy)
185*7863faa7SLutz Donnerhacke {
186*7863faa7SLutz Donnerhacke 	ng_counter_t	r;
187*7863faa7SLutz Donnerhacke 	int		i;
188*7863faa7SLutz Donnerhacke 	char		msg[] = "test";
189*7863faa7SLutz Donnerhacke 	const int	HOOKS = 1000;
190*7863faa7SLutz Donnerhacke 
191*7863faa7SLutz Donnerhacke 	ng_errors(PASS);
192*7863faa7SLutz Donnerhacke 	ng_shutdown("hub:");
193*7863faa7SLutz Donnerhacke 	ng_errors(FAIL);
194*7863faa7SLutz Donnerhacke 
195*7863faa7SLutz Donnerhacke 	ng_init();
196*7863faa7SLutz Donnerhacke 	ng_mkpeer(".", "a", "hub", "a");
197*7863faa7SLutz Donnerhacke 	ng_name("a", "hub");
198*7863faa7SLutz Donnerhacke 
199*7863faa7SLutz Donnerhacke 	ng_register_data("a", get_data0);
200*7863faa7SLutz Donnerhacke 	ng_counter_clear(r);
201*7863faa7SLutz Donnerhacke 	for (i = 0; i < HOOKS; i++)
202*7863faa7SLutz Donnerhacke 	{
203*7863faa7SLutz Donnerhacke 		char		hook[20];
204*7863faa7SLutz Donnerhacke 
205*7863faa7SLutz Donnerhacke 		snprintf(hook, sizeof(hook), "hook%d", i);
206*7863faa7SLutz Donnerhacke 		ng_connect(".", hook, "hub:", hook);
207*7863faa7SLutz Donnerhacke 		ng_errors(PASS);
208*7863faa7SLutz Donnerhacke 		ng_send_data(hook, msg, sizeof(msg));
209*7863faa7SLutz Donnerhacke 		ng_errors(FAIL);
210*7863faa7SLutz Donnerhacke 		if (errno != 0)
211*7863faa7SLutz Donnerhacke 			break;
212*7863faa7SLutz Donnerhacke 		ng_handle_events(50, r);
213*7863faa7SLutz Donnerhacke 	}
214*7863faa7SLutz Donnerhacke 	ATF_CHECK(r[0] > 100);
215*7863faa7SLutz Donnerhacke 	atf_tc_expect_fail("Implementation limitation (%d)", i);
216*7863faa7SLutz Donnerhacke 	ATF_CHECK(r[0] == HOOKS);
217*7863faa7SLutz Donnerhacke 	atf_tc_expect_pass();
218*7863faa7SLutz Donnerhacke 
219*7863faa7SLutz Donnerhacke 	ng_shutdown("hub:");
220*7863faa7SLutz Donnerhacke }
221*7863faa7SLutz Donnerhacke 
222*7863faa7SLutz Donnerhacke 
ATF_TP_ADD_TCS(hub)223*7863faa7SLutz Donnerhacke ATF_TP_ADD_TCS(hub)
224*7863faa7SLutz Donnerhacke {
225*7863faa7SLutz Donnerhacke 	ATF_TP_ADD_TC(hub, basic);
226*7863faa7SLutz Donnerhacke 	ATF_TP_ADD_TC(hub, loop);
227*7863faa7SLutz Donnerhacke 	ATF_TP_ADD_TC(hub, persistence);
228*7863faa7SLutz Donnerhacke 	ATF_TP_ADD_TC(hub, many_hooks);
229*7863faa7SLutz Donnerhacke 
230*7863faa7SLutz Donnerhacke 	return atf_no_error();
231*7863faa7SLutz Donnerhacke }
232