xref: /freebsd/tests/sys/netinet/udp_io.c (revision 6580f5c38dd5b01aeeaed16b370f1a12423437f0)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2024 Gleb Smirnoff <glebius@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <errno.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 
35 #include <atf-c.h>
36 
37 /*
38  * Create a pair of UDP sockets.  The first one is bound to a local
39  * address and the second one is connected to it.
40  */
41 static void
42 udp_socketpair(int *s)
43 {
44 	struct sockaddr_in sin = {
45 		.sin_family = AF_INET,
46 		.sin_len = sizeof(sin),
47 	};
48 	socklen_t slen = sizeof(sin);
49 	int b, c;
50 
51 	ATF_REQUIRE((b = socket(PF_INET, SOCK_DGRAM, 0)) > 0);
52 	ATF_REQUIRE((c = socket(PF_INET, SOCK_DGRAM, 0)) > 0);
53 	ATF_REQUIRE(bind(b, (struct sockaddr *)&sin, sizeof(sin)) == 0);
54 	ATF_REQUIRE(getsockname(b, (struct sockaddr *)&sin, &slen) == 0);
55 	ATF_REQUIRE(connect(c, (struct sockaddr *)&sin, sizeof(sin)) == 0);
56 
57 	s[0] = b;
58 	s[1] = c;
59 }
60 
61 /*
62  * Check MSG_TRUNC.
63  */
64 ATF_TC_WITHOUT_HEAD(trunc);
65 ATF_TC_BODY(trunc, tc)
66 {
67 	char sbuf[] = "Hello, peer!", rbuf[sizeof(sbuf)];
68 	struct iovec iov = {
69 		.iov_base = sbuf,
70 		.iov_len = sizeof(sbuf),
71 	};
72 	struct msghdr msg = {
73 		.msg_iov = &iov,
74 		.msg_iovlen = 1,
75 	};
76 	int s[2];
77 	u_int n;
78 
79 	udp_socketpair(s);
80 
81 	ATF_REQUIRE(sendmsg(s[1], &msg, 0) == sizeof(sbuf));
82 	n = (arc4random() % (sizeof(sbuf) - 1)) + 1;
83 	iov.iov_base = rbuf;
84 	iov.iov_len = n;
85 	ATF_REQUIRE(recvmsg(s[0], &msg, 0) == (ssize_t)n);
86 	ATF_REQUIRE(msg.msg_flags == MSG_TRUNC);
87 	ATF_REQUIRE(strncmp(sbuf, rbuf, n) == 0);
88 	iov.iov_len = sizeof(rbuf);
89 	ATF_REQUIRE(recvmsg(s[0], &msg, MSG_DONTWAIT) == -1);
90 	ATF_REQUIRE(errno == EAGAIN);
91 
92 	close(s[0]);
93 	close(s[1]);
94 }
95 
96 /*
97  * Check MSG_PEEK.
98  */
99 ATF_TC_WITHOUT_HEAD(peek);
100 ATF_TC_BODY(peek, tc)
101 {
102 	char sbuf[] = "Hello, peer!", rbuf[sizeof(sbuf)];
103 	struct iovec iov = {
104 		.iov_base = sbuf,
105 		.iov_len = sizeof(sbuf),
106 	};
107 	struct msghdr msg = {
108 		.msg_iov = &iov,
109 		.msg_iovlen = 1,
110 	};
111 	int s[2];
112 	u_int n;
113 
114 	udp_socketpair(s);
115 
116 	ATF_REQUIRE(sendmsg(s[1], &msg, 0) == sizeof(sbuf));
117 	iov.iov_base = rbuf;
118 	for (int i = 0; i < 10; i++) {
119 		n = (arc4random() % sizeof(sbuf)) + 1;
120 		iov.iov_len = n;
121 		ATF_REQUIRE(recvmsg(s[0], &msg, MSG_PEEK) == (ssize_t)n);
122 		if (n < sizeof(sbuf))
123 			ATF_REQUIRE(msg.msg_flags == (MSG_PEEK | MSG_TRUNC));
124 		else
125 			ATF_REQUIRE(msg.msg_flags == MSG_PEEK);
126 		ATF_REQUIRE(strncmp(sbuf, rbuf, n) == 0);
127 	}
128 
129 	close(s[0]);
130 	close(s[1]);
131 }
132 
133 ATF_TP_ADD_TCS(tp)
134 {
135 	ATF_TP_ADD_TC(tp, trunc);
136 	ATF_TP_ADD_TC(tp, peek);
137 
138 	return (atf_no_error());
139 }
140