xref: /freebsd/tests/sys/kern/socket_accept.c (revision 6fa42b91ca3f481912af98c4d49c44507eb1b8e1)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2023 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 <fcntl.h>
32 #include <string.h>
33 
34 #include <atf-c.h>
35 
36 static int
37 tcp4_listensock(struct sockaddr_in *sin)
38 {
39 	int l;
40 
41 	ATF_REQUIRE((l = socket(PF_INET, SOCK_STREAM, 0)) > 0);
42 	ATF_REQUIRE(setsockopt(l, SOL_SOCKET, SO_REUSEADDR, &(socklen_t){1},
43 	    sizeof(int)) == 0);
44 	*sin = (struct sockaddr_in){
45 		.sin_len = sizeof(sin),
46 		.sin_family = AF_INET,
47 		.sin_addr.s_addr = htonl(INADDR_LOOPBACK),
48 	};
49 	ATF_REQUIRE(bind(l, (struct sockaddr *)sin, sizeof(*sin)) == 0);
50 	ATF_REQUIRE(getsockname(l, (struct sockaddr *)sin,
51 	    &(socklen_t){ sizeof(*sin) }) == 0);
52 	ATF_REQUIRE(listen(l, -1) == 0);
53 
54 	return (l);
55 }
56 
57 static int
58 tcp4_clientsock(struct sockaddr_in *sin)
59 {
60 	int s;
61 
62 	ATF_REQUIRE((s = socket(PF_INET, SOCK_STREAM, 0)) > 0);
63 	ATF_REQUIRE(connect(s, (struct sockaddr *)sin, sizeof(*sin)) == 0);
64 
65 	return (s);
66 }
67 
68 ATF_TC_WITHOUT_HEAD(tcp4_zerolen);
69 ATF_TC_BODY(tcp4_zerolen, tc)
70 {
71 	static char canary[sizeof(struct sockaddr_in)] =
72 	    { [0 ... sizeof(struct sockaddr_in) - 1] = 0xa };
73 	struct sockaddr_in sin, ret;
74 	socklen_t salen;
75 	int l;
76 
77 	l = tcp4_listensock(&sin);
78 	(void )tcp4_clientsock(&sin);
79 
80 	memcpy(&ret, &canary, sizeof(ret));
81 	salen = 0;
82 	ATF_REQUIRE(accept(l, (struct sockaddr *)&ret, &salen) > 0);
83 	ATF_REQUIRE(memcmp(&ret, &canary, sizeof(ret)) == 0);
84 	ATF_REQUIRE(salen == sizeof(struct sockaddr_in));
85 	/* Note: Linux will block for connection here, we fail immediately. */
86 	ATF_REQUIRE(accept(l, (struct sockaddr *)&ret, NULL) == -1);
87 	ATF_REQUIRE(errno == EFAULT);
88 }
89 
90 ATF_TC_WITHOUT_HEAD(tcp4);
91 ATF_TC_BODY(tcp4, tc)
92 {
93 	struct sockaddr_in sin, ret;
94 	socklen_t salen;
95 	int l, s;
96 
97 	l = tcp4_listensock(&sin);
98 	s = tcp4_clientsock(&sin);
99 
100 	salen = sizeof(struct sockaddr_in) + 2;
101 	ATF_REQUIRE(accept(l, (struct sockaddr *)&ret, &salen) > 0);
102 	ATF_REQUIRE(salen == sizeof(struct sockaddr_in));
103 	ATF_REQUIRE(getsockname(s, (struct sockaddr *)&sin,
104 	    &(socklen_t){ sizeof(sin) }) == 0);
105 	ATF_REQUIRE(memcmp(&ret, &sin, sizeof(sin)) == 0);
106 }
107 
108 ATF_TC_WITHOUT_HEAD(tcp4_noaddr);
109 ATF_TC_BODY(tcp4_noaddr, tc)
110 {
111 	struct sockaddr_in sin;
112 	int l;
113 
114 	l = tcp4_listensock(&sin);
115 	(void )tcp4_clientsock(&sin);
116 
117 	ATF_REQUIRE(accept(l, NULL, NULL) > 0);
118 }
119 
120 ATF_TP_ADD_TCS(tp)
121 {
122 	ATF_TP_ADD_TC(tp, tcp4);
123 	ATF_TP_ADD_TC(tp, tcp4_noaddr);
124 	ATF_TP_ADD_TC(tp, tcp4_zerolen);
125 
126 	return (atf_no_error());
127 }
128