2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2022 Alexander V. Chernikov
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
33 #include <sys/errno.h>
34 #include <sys/socket.h>
36 #include <netinet/in.h>
42 check_recvmsg(const char *test_name)
46 struct sockaddr_in sin;
47 struct sockaddr_in6 sin6;
48 struct sockaddr_un saun;
49 int *sizes, sizes_count;
53 if (!strcmp(test_name, "udp")) {
54 ss = socket(PF_INET, SOCK_DGRAM, 0);
56 rc = setsockopt(ss, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
58 bzero(&sin, sizeof(sin));
59 sin.sin_family = AF_INET;
60 sin.sin_len = sizeof(sin);
61 sin.sin_port = htons(6666);
62 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
63 sa = (struct sockaddr *)&sin;
64 rc = bind(ss, sa, sa->sa_len);
67 cs = socket(PF_INET, SOCK_DGRAM, 0);
69 int inet_sizes[] = {80, 255, 256, 1024, 4096, 9000};
70 sizes_count = sizeof(inet_sizes) / sizeof(int);
71 sizes = malloc(sizeof(inet_sizes));
72 memcpy(sizes, inet_sizes, sizeof(inet_sizes));
74 } else if (!strcmp(test_name, "udp6")) {
75 ss = socket(PF_INET6, SOCK_DGRAM, 0);
77 rc = setsockopt(ss, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
79 bzero(&sin6, sizeof(sin6));
80 sin6.sin6_family = AF_INET6;
81 sin6.sin6_len = sizeof(sin6);
82 sin6.sin6_port = htons(6666);
83 const struct in6_addr in6loopback = IN6ADDR_LOOPBACK_INIT;
84 sin6.sin6_addr = in6loopback;
85 sa = (struct sockaddr *)&sin6;
86 rc = bind(ss, sa, sa->sa_len);
89 cs = socket(PF_INET6, SOCK_DGRAM, 0);
91 int inet_sizes[] = {80, 255, 256, 1024, 4096, 9000};
92 sizes_count = sizeof(inet_sizes) / sizeof(int);
93 sizes = malloc(sizeof(inet_sizes));
94 memcpy(sizes, inet_sizes, sizeof(inet_sizes));
96 } else if (!strcmp(test_name, "unix")) {
97 const char *PATH = "/tmp/test_check_recvmsg_socket";
98 ss = socket(PF_UNIX, SOCK_DGRAM, 0);
100 rc = setsockopt(ss, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
102 bzero(&saun, sizeof(saun));
103 saun.sun_family = AF_UNIX;
104 strcpy(saun.sun_path, PATH);
105 saun.sun_len = sizeof(saun);
106 sa = (struct sockaddr *)&saun;
108 rc = bind(ss, sa, sa->sa_len);
111 cs = socket(PF_UNIX, SOCK_DGRAM, 0);
113 int unix_sizes[] = {80, 255, 256, 1024, 2000};
114 sizes_count = sizeof(unix_sizes) / sizeof(int);
115 sizes = malloc(sizeof(unix_sizes));
116 memcpy(sizes, unix_sizes, sizeof(unix_sizes));
121 memset(buf, 0xFF, sizeof(buf));
122 for (int i = 0; i < sizes_count; i++) {
125 rc = sendto(cs, buf, sz, 0, sa, sa->sa_len);
126 ATF_REQUIRE_EQ(rc, sz);
128 rc = recv(ss, NULL, 0, MSG_PEEK | MSG_TRUNC);
129 ATF_CHECK_EQ(rc, sz);
131 rc = recv(ss, tbuf, sizeof(tbuf), MSG_PEEK | MSG_TRUNC);
132 ATF_CHECK_EQ(rc, sz);
134 rc = recv(ss, tbuf, sizeof(tbuf), MSG_TRUNC);
135 ATF_CHECK_EQ(rc, sz);
142 ATF_TC_WITHOUT_HEAD(socket_afinet_udp_recv_trunc);
143 ATF_TC_BODY(socket_afinet_udp_recv_trunc, tc)
145 check_recvmsg("udp");
148 ATF_TC_WITHOUT_HEAD(socket_afinet6_udp_recv_trunc);
149 ATF_TC_BODY(socket_afinet6_udp_recv_trunc, tc)
151 check_recvmsg("udp6");
154 ATF_TC_WITHOUT_HEAD(socket_afunix_recv_trunc);
155 ATF_TC_BODY(socket_afunix_recv_trunc, tc)
157 check_recvmsg("unix");
164 ATF_TP_ADD_TC(tp, socket_afinet_udp_recv_trunc);
165 ATF_TP_ADD_TC(tp, socket_afinet6_udp_recv_trunc);
166 ATF_TP_ADD_TC(tp, socket_afunix_recv_trunc);
168 return atf_no_error();