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
37ATF_TC_WITHOUT_HEAD(socket_afinet);
38ATF_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
48ATF_TC_WITHOUT_HEAD(socket_afinet_bind_zero);
49ATF_TC_BODY(socket_afinet_bind_zero, tc)
50{
51	int sd, rc;
52	struct sockaddr_in sin;
53
54	if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
55		atf_tc_skip("doesn't work when mac_portacl(4) loaded (https://bugs.freebsd.org/238781)");
56
57	sd = socket(PF_INET, SOCK_DGRAM, 0);
58	ATF_CHECK(sd >= 0);
59
60	bzero(&sin, sizeof(sin));
61	/*
62	 * For AF_INET we do not check the family in in_pcbbind_setup(9),
63	 * sa_len gets set from the syscall argument in getsockaddr(9),
64	 * so we bind to 0:0.
65	 */
66	rc = bind(sd, (struct sockaddr *)&sin, sizeof(sin));
67	ATF_CHECK_EQ(0, rc);
68
69	close(sd);
70}
71
72ATF_TC_WITHOUT_HEAD(socket_afinet_bind_ok);
73ATF_TC_BODY(socket_afinet_bind_ok, tc)
74{
75	int sd, rc;
76	struct sockaddr_in sin;
77
78	sd = socket(PF_INET, SOCK_DGRAM, 0);
79	ATF_CHECK(sd >= 0);
80
81	bzero(&sin, sizeof(sin));
82	sin.sin_family = AF_INET;
83	sin.sin_len = sizeof(sin);
84	sin.sin_port = htons(6666);
85	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
86	rc = bind(sd, (struct sockaddr *)&sin, sizeof(sin));
87	ATF_CHECK_EQ(0, rc);
88
89	close(sd);
90}
91
92ATF_TP_ADD_TCS(tp)
93{
94
95	ATF_TP_ADD_TC(tp, socket_afinet);
96	ATF_TP_ADD_TC(tp, socket_afinet_bind_zero);
97	ATF_TP_ADD_TC(tp, socket_afinet_bind_ok);
98
99	return atf_no_error();
100}
101