xref: /freebsd/lib/libnv/tests/nvlist_send_recv_test.c (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
1 /*-
2  * Copyright (c) 2013 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Pawel Jakub Dawidek under sponsorship from
6  * the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/param.h>
31 #include <sys/socket.h>
32 #include <sys/sysctl.h>
33 #include <sys/wait.h>
34 #include <sys/nv.h>
35 
36 #include <stdlib.h>
37 #include <err.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <paths.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <unistd.h>
44 
45 #include <atf-c.h>
46 
47 #define	ALPHABET	"abcdefghijklmnopqrstuvwxyz"
48 #define	fd_is_valid(fd)	(fcntl((fd), F_GETFL) != -1 || errno != EBADF)
49 
50 static void
51 send_nvlist_child(int sock)
52 {
53 	nvlist_t *nvl;
54 	nvlist_t *empty;
55 	int pfd[2];
56 
57 	nvl = nvlist_create(0);
58 	empty = nvlist_create(0);
59 
60 	nvlist_add_bool(nvl, "nvlist/bool/true", true);
61 	nvlist_add_bool(nvl, "nvlist/bool/false", false);
62 	nvlist_add_number(nvl, "nvlist/number/0", 0);
63 	nvlist_add_number(nvl, "nvlist/number/1", 1);
64 	nvlist_add_number(nvl, "nvlist/number/-1", -1);
65 	nvlist_add_number(nvl, "nvlist/number/UINT64_MAX", UINT64_MAX);
66 	nvlist_add_number(nvl, "nvlist/number/INT64_MIN", INT64_MIN);
67 	nvlist_add_number(nvl, "nvlist/number/INT64_MAX", INT64_MAX);
68 	nvlist_add_string(nvl, "nvlist/string/", "");
69 	nvlist_add_string(nvl, "nvlist/string/x", "x");
70 	nvlist_add_string(nvl, "nvlist/string/" ALPHABET, ALPHABET);
71 
72 	nvlist_add_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO",
73 	    STDERR_FILENO);
74 	if (pipe(pfd) == -1)
75 		err(EXIT_FAILURE, "pipe");
76 	if (write(pfd[1], "test", 4) != 4)
77 		err(EXIT_FAILURE, "write");
78 	close(pfd[1]);
79 	nvlist_add_descriptor(nvl, "nvlist/descriptor/pipe_rd", pfd[0]);
80 	close(pfd[0]);
81 
82 	nvlist_add_binary(nvl, "nvlist/binary/x", "x", 1);
83 	nvlist_add_binary(nvl, "nvlist/binary/" ALPHABET, ALPHABET,
84 	    sizeof(ALPHABET));
85 	nvlist_move_nvlist(nvl, "nvlist/nvlist/empty", empty);
86 	nvlist_add_nvlist(nvl, "nvlist/nvlist", nvl);
87 
88 	nvlist_send(sock, nvl);
89 
90 	nvlist_destroy(nvl);
91 }
92 
93 static void
94 send_nvlist_parent(int sock)
95 {
96 	nvlist_t *nvl;
97 	const nvlist_t *cnvl, *empty;
98 	const char *name, *cname;
99 	void *cookie, *ccookie;
100 	int type, ctype, fd;
101 	size_t size;
102 	char buf[4];
103 
104 	nvl = nvlist_recv(sock, 0);
105 	ATF_REQUIRE(nvlist_error(nvl) == 0);
106 	if (nvlist_error(nvl) != 0)
107 		err(1, "nvlist_recv() failed");
108 
109 	cookie = NULL;
110 
111 	name = nvlist_next(nvl, &type, &cookie);
112 	ATF_REQUIRE(name != NULL);
113 	ATF_REQUIRE(type == NV_TYPE_BOOL);
114 	ATF_REQUIRE(strcmp(name, "nvlist/bool/true") == 0);
115 	ATF_REQUIRE(nvlist_get_bool(nvl, name) == true);
116 
117 	name = nvlist_next(nvl, &type, &cookie);
118 	ATF_REQUIRE(name != NULL);
119 	ATF_REQUIRE(type == NV_TYPE_BOOL);
120 	ATF_REQUIRE(strcmp(name, "nvlist/bool/false") == 0);
121 	ATF_REQUIRE(nvlist_get_bool(nvl, name) == false);
122 
123 	name = nvlist_next(nvl, &type, &cookie);
124 	ATF_REQUIRE(name != NULL);
125 	ATF_REQUIRE(type == NV_TYPE_NUMBER);
126 	ATF_REQUIRE(strcmp(name, "nvlist/number/0") == 0);
127 	ATF_REQUIRE(nvlist_get_number(nvl, name) == 0);
128 
129 	name = nvlist_next(nvl, &type, &cookie);
130 	ATF_REQUIRE(name != NULL);
131 	ATF_REQUIRE(type == NV_TYPE_NUMBER);
132 	ATF_REQUIRE(strcmp(name, "nvlist/number/1") == 0);
133 	ATF_REQUIRE(nvlist_get_number(nvl, name) == 1);
134 
135 	name = nvlist_next(nvl, &type, &cookie);
136 	ATF_REQUIRE(name != NULL);
137 	ATF_REQUIRE(type == NV_TYPE_NUMBER);
138 	ATF_REQUIRE(strcmp(name, "nvlist/number/-1") == 0);
139 	ATF_REQUIRE((int)nvlist_get_number(nvl, name) == -1);
140 
141 	name = nvlist_next(nvl, &type, &cookie);
142 	ATF_REQUIRE(name != NULL);
143 	ATF_REQUIRE(type == NV_TYPE_NUMBER);
144 	ATF_REQUIRE(strcmp(name, "nvlist/number/UINT64_MAX") == 0);
145 	ATF_REQUIRE(nvlist_get_number(nvl, name) == UINT64_MAX);
146 
147 	name = nvlist_next(nvl, &type, &cookie);
148 	ATF_REQUIRE(name != NULL);
149 	ATF_REQUIRE(type == NV_TYPE_NUMBER);
150 	ATF_REQUIRE(strcmp(name, "nvlist/number/INT64_MIN") == 0);
151 	ATF_REQUIRE((int64_t)nvlist_get_number(nvl, name) == INT64_MIN);
152 
153 	name = nvlist_next(nvl, &type, &cookie);
154 	ATF_REQUIRE(name != NULL);
155 	ATF_REQUIRE(type == NV_TYPE_NUMBER);
156 	ATF_REQUIRE(strcmp(name, "nvlist/number/INT64_MAX") == 0);
157 	ATF_REQUIRE((int64_t)nvlist_get_number(nvl, name) == INT64_MAX);
158 
159 	name = nvlist_next(nvl, &type, &cookie);
160 	ATF_REQUIRE(name != NULL);
161 	ATF_REQUIRE(type == NV_TYPE_STRING);
162 	ATF_REQUIRE(strcmp(name, "nvlist/string/") == 0);
163 	ATF_REQUIRE(strcmp(nvlist_get_string(nvl, name), "") == 0);
164 
165 	name = nvlist_next(nvl, &type, &cookie);
166 	ATF_REQUIRE(name != NULL);
167 	ATF_REQUIRE(type == NV_TYPE_STRING);
168 	ATF_REQUIRE(strcmp(name, "nvlist/string/x") == 0);
169 	ATF_REQUIRE(strcmp(nvlist_get_string(nvl, name), "x") == 0);
170 
171 	name = nvlist_next(nvl, &type, &cookie);
172 	ATF_REQUIRE(name != NULL);
173 	ATF_REQUIRE(type == NV_TYPE_STRING);
174 	ATF_REQUIRE(strcmp(name, "nvlist/string/" ALPHABET) == 0);
175 	ATF_REQUIRE(strcmp(nvlist_get_string(nvl, name), ALPHABET) == 0);
176 
177 	name = nvlist_next(nvl, &type, &cookie);
178 	ATF_REQUIRE(name != NULL);
179 	ATF_REQUIRE(type == NV_TYPE_DESCRIPTOR);
180 	ATF_REQUIRE(strcmp(name, "nvlist/descriptor/STDERR_FILENO") == 0);
181 	ATF_REQUIRE(fd_is_valid(nvlist_get_descriptor(nvl, name)));
182 
183 	name = nvlist_next(nvl, &type, &cookie);
184 	ATF_REQUIRE(name != NULL);
185 	ATF_REQUIRE(type == NV_TYPE_DESCRIPTOR);
186 	ATF_REQUIRE(strcmp(name, "nvlist/descriptor/pipe_rd") == 0);
187 	fd = nvlist_get_descriptor(nvl, name);
188 	ATF_REQUIRE(fd_is_valid(fd));
189 	ATF_REQUIRE(read(fd, buf, sizeof(buf)) == 4);
190 	ATF_REQUIRE(strncmp(buf, "test", sizeof(buf)) == 0);
191 
192 	name = nvlist_next(nvl, &type, &cookie);
193 	ATF_REQUIRE(name != NULL);
194 	ATF_REQUIRE(type == NV_TYPE_BINARY);
195 	ATF_REQUIRE(strcmp(name, "nvlist/binary/x") == 0);
196 	ATF_REQUIRE(memcmp(nvlist_get_binary(nvl, name, NULL), "x", 1) == 0);
197 	ATF_REQUIRE(memcmp(nvlist_get_binary(nvl, name, &size), "x", 1) == 0);
198 	ATF_REQUIRE(size == 1);
199 
200 	name = nvlist_next(nvl, &type, &cookie);
201 	ATF_REQUIRE(name != NULL);
202 	ATF_REQUIRE(type == NV_TYPE_BINARY);
203 	ATF_REQUIRE(strcmp(name, "nvlist/binary/" ALPHABET) == 0);
204 	ATF_REQUIRE(memcmp(nvlist_get_binary(nvl, name, NULL), ALPHABET,
205 	    sizeof(ALPHABET)) == 0);
206 	ATF_REQUIRE(memcmp(nvlist_get_binary(nvl, name, &size), ALPHABET,
207 	    sizeof(ALPHABET)) == 0);
208 	ATF_REQUIRE(size == sizeof(ALPHABET));
209 
210 	name = nvlist_next(nvl, &type, &cookie);
211 	ATF_REQUIRE(name != NULL);
212 	ATF_REQUIRE(type == NV_TYPE_NVLIST);
213 	ATF_REQUIRE(strcmp(name, "nvlist/nvlist/empty") == 0);
214 	cnvl = nvlist_get_nvlist(nvl, name);
215 	ATF_REQUIRE(nvlist_empty(cnvl));
216 
217 	name = nvlist_next(nvl, &type, &cookie);
218 	ATF_REQUIRE(name != NULL);
219 	ATF_REQUIRE(type == NV_TYPE_NVLIST);
220 	ATF_REQUIRE(strcmp(name, "nvlist/nvlist") == 0);
221 	cnvl = nvlist_get_nvlist(nvl, name);
222 
223 	ccookie = NULL;
224 
225 	cname = nvlist_next(cnvl, &ctype, &ccookie);
226 	ATF_REQUIRE(cname != NULL);
227 	ATF_REQUIRE(ctype == NV_TYPE_BOOL);
228 	ATF_REQUIRE(strcmp(cname, "nvlist/bool/true") == 0);
229 	ATF_REQUIRE(nvlist_get_bool(cnvl, cname) == true);
230 
231 	cname = nvlist_next(cnvl, &ctype, &ccookie);
232 	ATF_REQUIRE(cname != NULL);
233 	ATF_REQUIRE(ctype == NV_TYPE_BOOL);
234 	ATF_REQUIRE(strcmp(cname, "nvlist/bool/false") == 0);
235 	ATF_REQUIRE(nvlist_get_bool(cnvl, cname) == false);
236 
237 	cname = nvlist_next(cnvl, &ctype, &ccookie);
238 	ATF_REQUIRE(cname != NULL);
239 	ATF_REQUIRE(ctype == NV_TYPE_NUMBER);
240 	ATF_REQUIRE(strcmp(cname, "nvlist/number/0") == 0);
241 	ATF_REQUIRE(nvlist_get_number(cnvl, cname) == 0);
242 
243 	cname = nvlist_next(cnvl, &ctype, &ccookie);
244 	ATF_REQUIRE(cname != NULL);
245 	ATF_REQUIRE(ctype == NV_TYPE_NUMBER);
246 	ATF_REQUIRE(strcmp(cname, "nvlist/number/1") == 0);
247 	ATF_REQUIRE(nvlist_get_number(cnvl, cname) == 1);
248 
249 	cname = nvlist_next(cnvl, &ctype, &ccookie);
250 	ATF_REQUIRE(cname != NULL);
251 	ATF_REQUIRE(ctype == NV_TYPE_NUMBER);
252 	ATF_REQUIRE(strcmp(cname, "nvlist/number/-1") == 0);
253 	ATF_REQUIRE((int)nvlist_get_number(cnvl, cname) == -1);
254 
255 	cname = nvlist_next(cnvl, &ctype, &ccookie);
256 	ATF_REQUIRE(cname != NULL);
257 	ATF_REQUIRE(ctype == NV_TYPE_NUMBER);
258 	ATF_REQUIRE(strcmp(cname, "nvlist/number/UINT64_MAX") == 0);
259 	ATF_REQUIRE(nvlist_get_number(cnvl, cname) == UINT64_MAX);
260 
261 	cname = nvlist_next(cnvl, &ctype, &ccookie);
262 	ATF_REQUIRE(cname != NULL);
263 	ATF_REQUIRE(ctype == NV_TYPE_NUMBER);
264 	ATF_REQUIRE(strcmp(cname, "nvlist/number/INT64_MIN") == 0);
265 	ATF_REQUIRE((int64_t)nvlist_get_number(cnvl, cname) == INT64_MIN);
266 
267 	cname = nvlist_next(cnvl, &ctype, &ccookie);
268 	ATF_REQUIRE(cname != NULL);
269 	ATF_REQUIRE(ctype == NV_TYPE_NUMBER);
270 	ATF_REQUIRE(strcmp(cname, "nvlist/number/INT64_MAX") == 0);
271 	ATF_REQUIRE((int64_t)nvlist_get_number(cnvl, cname) == INT64_MAX);
272 
273 	cname = nvlist_next(cnvl, &ctype, &ccookie);
274 	ATF_REQUIRE(cname != NULL);
275 	ATF_REQUIRE(ctype == NV_TYPE_STRING);
276 	ATF_REQUIRE(strcmp(cname, "nvlist/string/") == 0);
277 	ATF_REQUIRE(strcmp(nvlist_get_string(cnvl, cname), "") == 0);
278 
279 	cname = nvlist_next(cnvl, &ctype, &ccookie);
280 	ATF_REQUIRE(cname != NULL);
281 	ATF_REQUIRE(ctype == NV_TYPE_STRING);
282 	ATF_REQUIRE(strcmp(cname, "nvlist/string/x") == 0);
283 	ATF_REQUIRE(strcmp(nvlist_get_string(cnvl, cname), "x") == 0);
284 
285 	cname = nvlist_next(cnvl, &ctype, &ccookie);
286 	ATF_REQUIRE(cname != NULL);
287 	ATF_REQUIRE(ctype == NV_TYPE_STRING);
288 	ATF_REQUIRE(strcmp(cname, "nvlist/string/" ALPHABET) == 0);
289 	ATF_REQUIRE(strcmp(nvlist_get_string(cnvl, cname), ALPHABET) == 0);
290 
291 	cname = nvlist_next(cnvl, &ctype, &ccookie);
292 	ATF_REQUIRE(cname != NULL);
293 	ATF_REQUIRE(ctype == NV_TYPE_DESCRIPTOR);
294 	ATF_REQUIRE(strcmp(cname, "nvlist/descriptor/STDERR_FILENO") == 0);
295 	ATF_REQUIRE(fd_is_valid(nvlist_get_descriptor(cnvl, cname)));
296 
297 	cname = nvlist_next(cnvl, &ctype, &ccookie);
298 	ATF_REQUIRE(cname != NULL);
299 	ATF_REQUIRE(ctype == NV_TYPE_DESCRIPTOR);
300 	ATF_REQUIRE(strcmp(cname, "nvlist/descriptor/pipe_rd") == 0);
301 	ATF_REQUIRE(fd_is_valid(nvlist_get_descriptor(cnvl, cname)));
302 
303 	cname = nvlist_next(cnvl, &ctype, &ccookie);
304 	ATF_REQUIRE(cname != NULL);
305 	ATF_REQUIRE(ctype == NV_TYPE_BINARY);
306 	ATF_REQUIRE(strcmp(cname, "nvlist/binary/x") == 0);
307 	ATF_REQUIRE(memcmp(nvlist_get_binary(cnvl, cname, NULL), "x", 1) == 0);
308 	ATF_REQUIRE(memcmp(nvlist_get_binary(cnvl, cname, &size), "x", 1) == 0);
309 	ATF_REQUIRE(size == 1);
310 
311 	cname = nvlist_next(cnvl, &ctype, &ccookie);
312 	ATF_REQUIRE(cname != NULL);
313 	ATF_REQUIRE(ctype == NV_TYPE_BINARY);
314 	ATF_REQUIRE(strcmp(cname, "nvlist/binary/" ALPHABET) == 0);
315 	ATF_REQUIRE(memcmp(nvlist_get_binary(cnvl, cname, NULL), ALPHABET,
316 	    sizeof(ALPHABET)) == 0);
317 	ATF_REQUIRE(memcmp(nvlist_get_binary(cnvl, cname, &size), ALPHABET,
318 	    sizeof(ALPHABET)) == 0);
319 	ATF_REQUIRE(size == sizeof(ALPHABET));
320 
321 	cname = nvlist_next(cnvl, &ctype, &ccookie);
322 	ATF_REQUIRE(cname != NULL);
323 	ATF_REQUIRE(ctype == NV_TYPE_NVLIST);
324 	ATF_REQUIRE(strcmp(cname, "nvlist/nvlist/empty") == 0);
325 	empty = nvlist_get_nvlist(cnvl, cname);
326 	ATF_REQUIRE(nvlist_empty(empty));
327 
328 	cname = nvlist_next(cnvl, &ctype, &ccookie);
329 	ATF_REQUIRE(cname == NULL);
330 
331 	name = nvlist_next(nvl, &type, &cookie);
332 	ATF_REQUIRE(name == NULL);
333 
334 	nvlist_destroy(nvl);
335 }
336 
337 static void
338 nvlist_send_recv__send_nvlist(short sotype)
339 {
340 	int socks[2], status;
341 	pid_t pid;
342 
343 	ATF_REQUIRE(socketpair(PF_UNIX, sotype, 0, socks) == 0);
344 
345 	pid = fork();
346 	ATF_REQUIRE(pid >= 0);
347 	if (pid == 0) {
348 		/* Child. */
349 		(void)close(socks[0]);
350 		send_nvlist_child(socks[1]);
351 		_exit(0);
352 	}
353 
354 	(void)close(socks[1]);
355 	send_nvlist_parent(socks[0]);
356 
357 	ATF_REQUIRE(waitpid(pid, &status, 0) == pid);
358 	ATF_REQUIRE(status == 0);
359 }
360 
361 static void
362 nvlist_send_recv__send_closed_fd(short sotype)
363 {
364 	nvlist_t *nvl;
365 	int socks[2];
366 
367 	ATF_REQUIRE(socketpair(PF_UNIX, sotype, 0, socks) == 0);
368 
369 	nvl = nvlist_create(0);
370 	ATF_REQUIRE(nvl != NULL);
371 	nvlist_add_descriptor(nvl, "fd", 12345);
372 	ATF_REQUIRE(nvlist_error(nvl) == EBADF);
373 
374 	ATF_REQUIRE_ERRNO(EBADF, nvlist_send(socks[1], nvl) != 0);
375 }
376 
377 static int
378 nopenfds(void)
379 {
380 	size_t len;
381 	int error, mib[4], n;
382 
383 	mib[0] = CTL_KERN;
384 	mib[1] = KERN_PROC;
385 	mib[2] = KERN_PROC_NFDS;
386 	mib[3] = 0;
387 
388 	len = sizeof(n);
389 	error = sysctl(mib, nitems(mib), &n, &len, NULL, 0);
390 	if (error != 0)
391 		return (-1);
392 	return (n);
393 }
394 
395 #define	NFDS	512
396 
397 static void
398 send_many_fds_child(int sock)
399 {
400 	char name[16];
401 	nvlist_t *nvl;
402 	int anfds, bnfds, fd, i, j;
403 
404 	fd = open(_PATH_DEVNULL, O_RDONLY);
405 	ATF_REQUIRE(fd >= 0);
406 
407 	for (i = 1; i < NFDS; i++) {
408 		nvl = nvlist_create(0);
409 		bnfds = nopenfds();
410 		if (bnfds == -1)
411 			err(EXIT_FAILURE, "sysctl");
412 
413 		for (j = 0; j < i; j++) {
414 			snprintf(name, sizeof(name), "fd%d", j);
415 			nvlist_add_descriptor(nvl, name, fd);
416 		}
417 		nvlist_send(sock, nvl);
418 		nvlist_destroy(nvl);
419 
420 		anfds = nopenfds();
421 		if (anfds == -1)
422 			err(EXIT_FAILURE, "sysctl");
423 		if (anfds != bnfds)
424 			errx(EXIT_FAILURE, "fd count mismatch");
425 	}
426 }
427 
428 static void
429 nvlist_send_recv__send_many_fds(short sotype)
430 {
431 	char name[16];
432 	nvlist_t *nvl;
433 	int anfds, bnfds, fd, i, j, socks[2], status;
434 	pid_t pid;
435 
436 	ATF_REQUIRE(socketpair(PF_UNIX, sotype, 0, socks) == 0);
437 
438 	pid = fork();
439 	ATF_REQUIRE(pid >= 0);
440 	if (pid == 0) {
441 		/* Child. */
442 		(void)close(socks[0]);
443 		send_many_fds_child(socks[1]);
444 		_exit(0);
445 	}
446 
447 	(void)close(socks[1]);
448 
449 	for (i = 1; i < NFDS; i++) {
450 		bnfds = nopenfds();
451 		ATF_REQUIRE(bnfds != -1);
452 
453 		nvl = nvlist_recv(socks[0], 0);
454 		ATF_REQUIRE(nvl != NULL);
455 		for (j = 0; j < i; j++) {
456 			snprintf(name, sizeof(name), "fd%d", j);
457 			fd = nvlist_take_descriptor(nvl, name);
458 			ATF_REQUIRE(close(fd) == 0);
459 		}
460 		nvlist_destroy(nvl);
461 
462 		anfds = nopenfds();
463 		ATF_REQUIRE(anfds != -1);
464 		ATF_REQUIRE(anfds == bnfds);
465 	}
466 
467 	ATF_REQUIRE(waitpid(pid, &status, 0) == pid);
468 	ATF_REQUIRE(status == 0);
469 }
470 
471 /*
472  * This test needs to tune the following sysctl's:
473  *      net.local.dgram.maxdgram
474  *      net.local.dgram.recvspace
475  */
476 ATF_TC_WITHOUT_HEAD(nvlist_send_recv__send_many_fds__dgram);
477 ATF_TC_BODY(nvlist_send_recv__send_many_fds__dgram, tc)
478 {
479 	u_long maxdgram, recvspace, temp_maxdgram, temp_recvspace;
480 	size_t len;
481 	int error;
482 
483 	atf_tc_skip("https://bugs.freebsd.org/260891");
484 
485 	/* size of the largest datagram to send */
486 	temp_maxdgram = 16772;
487 	len = sizeof(maxdgram);
488 	error = sysctlbyname("net.local.dgram.maxdgram", &maxdgram,
489 	    &len, &temp_maxdgram, sizeof(temp_maxdgram));
490 	if (error != 0)
491 		atf_tc_skip("cannot set net.local.dgram.maxdgram: %s", strerror(errno));
492 
493 	/*
494 	 * The receive queue fills up quicker than it's being emptied,
495 	 * bump it to a sufficiently large enough value, 1M.
496 	 */
497 	temp_recvspace = 1048576;
498 	len = sizeof(recvspace);
499 	error = sysctlbyname("net.local.dgram.recvspace", &recvspace,
500 	    &len, &temp_recvspace, sizeof(temp_recvspace));
501 	if (error != 0)
502 		atf_tc_skip("cannot set net.local.dgram.recvspace: %s", strerror(errno));
503 
504 	nvlist_send_recv__send_many_fds(SOCK_DGRAM);
505 
506 	/* restore original values */
507 	error = sysctlbyname("net.local.dgram.maxdgram", NULL, NULL, &maxdgram, sizeof(maxdgram));
508 	if (error != 0)
509 		warn("failed to restore net.local.dgram.maxdgram");
510 
511 	error = sysctlbyname("net.local.dgram.recvspace", NULL, NULL, &recvspace, sizeof(recvspace));
512 	if (error != 0)
513 		warn("failed to restore net.local.dgram.recvspace");
514 }
515 
516 ATF_TC_WITHOUT_HEAD(nvlist_send_recv__send_many_fds__stream);
517 ATF_TC_BODY(nvlist_send_recv__send_many_fds__stream, tc)
518 {
519 	nvlist_send_recv__send_many_fds(SOCK_STREAM);
520 }
521 
522 ATF_TC_WITHOUT_HEAD(nvlist_send_recv__send_nvlist__dgram);
523 ATF_TC_BODY(nvlist_send_recv__send_nvlist__dgram, tc)
524 {
525 	nvlist_send_recv__send_nvlist(SOCK_DGRAM);
526 }
527 
528 ATF_TC_WITHOUT_HEAD(nvlist_send_recv__send_nvlist__stream);
529 ATF_TC_BODY(nvlist_send_recv__send_nvlist__stream, tc)
530 {
531 	nvlist_send_recv__send_nvlist(SOCK_STREAM);
532 }
533 
534 ATF_TC_WITHOUT_HEAD(nvlist_send_recv__send_closed_fd__dgram);
535 ATF_TC_BODY(nvlist_send_recv__send_closed_fd__dgram, tc)
536 {
537 	nvlist_send_recv__send_closed_fd(SOCK_DGRAM);
538 }
539 
540 ATF_TC_WITHOUT_HEAD(nvlist_send_recv__send_closed_fd__stream);
541 ATF_TC_BODY(nvlist_send_recv__send_closed_fd__stream, tc)
542 {
543 	nvlist_send_recv__send_closed_fd(SOCK_STREAM);
544 }
545 
546 ATF_TP_ADD_TCS(tp)
547 {
548 
549 	ATF_TP_ADD_TC(tp, nvlist_send_recv__send_nvlist__dgram);
550 	ATF_TP_ADD_TC(tp, nvlist_send_recv__send_nvlist__stream);
551 	ATF_TP_ADD_TC(tp, nvlist_send_recv__send_closed_fd__dgram);
552 	ATF_TP_ADD_TC(tp, nvlist_send_recv__send_closed_fd__stream);
553 	ATF_TP_ADD_TC(tp, nvlist_send_recv__send_many_fds__dgram);
554 	ATF_TP_ADD_TC(tp, nvlist_send_recv__send_many_fds__stream);
555 
556 	return (atf_no_error());
557 }
558