2 * SPDX-License-Identifier: BSD-2-Clause
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>
31 #include <sys/errno.h>
32 #include <sys/socket.h>
34 #include <netinet/in.h>
40 check_recvmsg(const char *test_name)
44 struct sockaddr_in sin;
45 struct sockaddr_in6 sin6;
46 struct sockaddr_un saun;
47 int *sizes, sizes_count;
51 if (!strcmp(test_name, "udp")) {
52 ss = socket(PF_INET, SOCK_DGRAM, 0);
54 rc = setsockopt(ss, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
56 bzero(&sin, sizeof(sin));
57 sin.sin_family = AF_INET;
58 sin.sin_len = sizeof(sin);
59 sin.sin_port = htons(6666);
60 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
61 sa = (struct sockaddr *)&sin;
62 rc = bind(ss, sa, sa->sa_len);
65 cs = socket(PF_INET, SOCK_DGRAM, 0);
67 int inet_sizes[] = {80, 255, 256, 1024, 4096, 9000};
68 sizes_count = sizeof(inet_sizes) / sizeof(int);
69 sizes = malloc(sizeof(inet_sizes));
70 memcpy(sizes, inet_sizes, sizeof(inet_sizes));
72 } else if (!strcmp(test_name, "udp6")) {
73 ss = socket(PF_INET6, SOCK_DGRAM, 0);
75 rc = setsockopt(ss, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
77 bzero(&sin6, sizeof(sin6));
78 sin6.sin6_family = AF_INET6;
79 sin6.sin6_len = sizeof(sin6);
80 sin6.sin6_port = htons(6666);
81 const struct in6_addr in6loopback = IN6ADDR_LOOPBACK_INIT;
82 sin6.sin6_addr = in6loopback;
83 sa = (struct sockaddr *)&sin6;
84 rc = bind(ss, sa, sa->sa_len);
87 cs = socket(PF_INET6, SOCK_DGRAM, 0);
89 int inet_sizes[] = {80, 255, 256, 1024, 4096, 9000};
90 sizes_count = sizeof(inet_sizes) / sizeof(int);
91 sizes = malloc(sizeof(inet_sizes));
92 memcpy(sizes, inet_sizes, sizeof(inet_sizes));
94 } else if (!strcmp(test_name, "unix")) {
95 const char *PATH = "/tmp/test_check_recvmsg_socket";
96 ss = socket(PF_UNIX, SOCK_DGRAM, 0);
98 rc = setsockopt(ss, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
100 bzero(&saun, sizeof(saun));
101 saun.sun_family = AF_UNIX;
102 strcpy(saun.sun_path, PATH);
103 saun.sun_len = sizeof(saun);
104 sa = (struct sockaddr *)&saun;
106 rc = bind(ss, sa, sa->sa_len);
109 cs = socket(PF_UNIX, SOCK_DGRAM, 0);
111 int unix_sizes[] = {80, 255, 256, 1024, 2000};
112 sizes_count = sizeof(unix_sizes) / sizeof(int);
113 sizes = malloc(sizeof(unix_sizes));
114 memcpy(sizes, unix_sizes, sizeof(unix_sizes));
119 memset(buf, 0xFF, sizeof(buf));
120 for (int i = 0; i < sizes_count; i++) {
123 rc = sendto(cs, buf, sz, 0, sa, sa->sa_len);
124 ATF_REQUIRE_EQ(rc, sz);
126 rc = recv(ss, NULL, 0, MSG_PEEK | MSG_TRUNC);
127 ATF_CHECK_EQ(rc, sz);
129 rc = recv(ss, tbuf, sizeof(tbuf), MSG_PEEK | MSG_TRUNC);
130 ATF_CHECK_EQ(rc, sz);
132 rc = recv(ss, tbuf, sizeof(tbuf), MSG_TRUNC);
133 ATF_CHECK_EQ(rc, sz);
140 ATF_TC_WITHOUT_HEAD(socket_afinet_udp_recv_trunc);
141 ATF_TC_BODY(socket_afinet_udp_recv_trunc, tc)
143 check_recvmsg("udp");
146 ATF_TC_WITHOUT_HEAD(socket_afinet6_udp_recv_trunc);
147 ATF_TC_BODY(socket_afinet6_udp_recv_trunc, tc)
149 check_recvmsg("udp6");
152 ATF_TC_WITHOUT_HEAD(socket_afunix_recv_trunc);
153 ATF_TC_BODY(socket_afunix_recv_trunc, tc)
155 check_recvmsg("unix");
162 ATF_TP_ADD_TC(tp, socket_afinet_udp_recv_trunc);
163 ATF_TP_ADD_TC(tp, socket_afinet6_udp_recv_trunc);
164 ATF_TP_ADD_TC(tp, socket_afunix_recv_trunc);
166 return atf_no_error();