xref: /freebsd/tests/sys/netinet/socket_afinet.c (revision 2f513db72b034fd5ef7f080b11be5c711c15186a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2019 Bjoern A. Zeeb
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/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/errno.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 
35 #include <atf-c.h>
36 
37 ATF_TC_WITHOUT_HEAD(socket_afinet);
38 ATF_TC_BODY(socket_afinet, tc)
39 {
40 	int sd;
41 
42 	sd = socket(PF_INET, SOCK_DGRAM, 0);
43 	ATF_CHECK(sd >= 0);
44 
45 	close(sd);
46 }
47 
48 ATF_TC_WITHOUT_HEAD(socket_afinet_bind_zero);
49 ATF_TC_BODY(socket_afinet_bind_zero, tc)
50 {
51 	int sd, rc;
52 	struct sockaddr_in sin;
53 
54 	atf_tc_skip("doesn't work when mac_portacl(4) loaded (bug238781)");
55 
56 	sd = socket(PF_INET, SOCK_DGRAM, 0);
57 	ATF_CHECK(sd >= 0);
58 
59 	bzero(&sin, sizeof(sin));
60 	/*
61 	 * For AF_INET we do not check the family in in_pcbbind_setup(9),
62 	 * sa_len gets set from the syscall argument in getsockaddr(9),
63 	 * so we bind to 0:0.
64 	 */
65 	rc = bind(sd, (struct sockaddr *)&sin, sizeof(sin));
66 	ATF_CHECK_EQ(0, rc);
67 
68 	close(sd);
69 }
70 
71 ATF_TC_WITHOUT_HEAD(socket_afinet_bind_ok);
72 ATF_TC_BODY(socket_afinet_bind_ok, tc)
73 {
74 	int sd, rc;
75 	struct sockaddr_in sin;
76 
77 	sd = socket(PF_INET, SOCK_DGRAM, 0);
78 	ATF_CHECK(sd >= 0);
79 
80 	bzero(&sin, sizeof(sin));
81 	sin.sin_family = AF_INET;
82 	sin.sin_len = sizeof(sin);
83 	sin.sin_port = htons(6666);
84 	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
85 	rc = bind(sd, (struct sockaddr *)&sin, sizeof(sin));
86 	ATF_CHECK_EQ(0, rc);
87 
88 	close(sd);
89 }
90 
91 ATF_TP_ADD_TCS(tp)
92 {
93 
94 	ATF_TP_ADD_TC(tp, socket_afinet);
95 	ATF_TP_ADD_TC(tp, socket_afinet_bind_zero);
96 	ATF_TP_ADD_TC(tp, socket_afinet_bind_ok);
97 
98 	return atf_no_error();
99 }
100