1861274c9SGleb Smirnoff /*-
2861274c9SGleb Smirnoff * SPDX-License-Identifier: BSD-2-Clause
3861274c9SGleb Smirnoff *
4861274c9SGleb Smirnoff * Copyright (c) 2024 Gleb Smirnoff <glebius@FreeBSD.org>
5861274c9SGleb Smirnoff *
6861274c9SGleb Smirnoff * Redistribution and use in source and binary forms, with or without
7861274c9SGleb Smirnoff * modification, are permitted provided that the following conditions
8861274c9SGleb Smirnoff * are met:
9861274c9SGleb Smirnoff * 1. Redistributions of source code must retain the above copyright
10861274c9SGleb Smirnoff * notice, this list of conditions and the following disclaimer.
11861274c9SGleb Smirnoff * 2. Redistributions in binary form must reproduce the above copyright
12861274c9SGleb Smirnoff * notice, this list of conditions and the following disclaimer in the
13861274c9SGleb Smirnoff * documentation and/or other materials provided with the distribution.
14861274c9SGleb Smirnoff *
15861274c9SGleb Smirnoff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16861274c9SGleb Smirnoff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17861274c9SGleb Smirnoff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18861274c9SGleb Smirnoff * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19861274c9SGleb Smirnoff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20861274c9SGleb Smirnoff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21861274c9SGleb Smirnoff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22861274c9SGleb Smirnoff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23861274c9SGleb Smirnoff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24861274c9SGleb Smirnoff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25861274c9SGleb Smirnoff * SUCH DAMAGE.
26861274c9SGleb Smirnoff */
27861274c9SGleb Smirnoff
28861274c9SGleb Smirnoff #include <sys/socket.h>
29861274c9SGleb Smirnoff #include <netinet/in.h>
30861274c9SGleb Smirnoff #include <stdio.h>
31861274c9SGleb Smirnoff #include <unistd.h>
32861274c9SGleb Smirnoff #include <string.h>
33861274c9SGleb Smirnoff
34861274c9SGleb Smirnoff #include <atf-c.h>
35861274c9SGleb Smirnoff
36*cd05c880SGleb Smirnoff ATF_TC_WITHOUT_HEAD(implied_connect);
ATF_TC_BODY(implied_connect,tc)37*cd05c880SGleb Smirnoff ATF_TC_BODY(implied_connect, tc)
38861274c9SGleb Smirnoff {
3970f5c6e3SGleb Smirnoff struct sockaddr_in sin = {
4070f5c6e3SGleb Smirnoff .sin_family = AF_INET,
4170f5c6e3SGleb Smirnoff .sin_len = sizeof(sin),
4270f5c6e3SGleb Smirnoff };
4370f5c6e3SGleb Smirnoff const char buf[] = "hello";
4470f5c6e3SGleb Smirnoff char repl[sizeof(buf)];
45861274c9SGleb Smirnoff socklen_t len;
46861274c9SGleb Smirnoff int s, c, a;
47861274c9SGleb Smirnoff
48861274c9SGleb Smirnoff ATF_REQUIRE(s = socket(PF_INET, SOCK_STREAM, 0));
49861274c9SGleb Smirnoff ATF_REQUIRE(c = socket(PF_INET, SOCK_STREAM, 0));
50861274c9SGleb Smirnoff
51861274c9SGleb Smirnoff ATF_REQUIRE(bind(s, (struct sockaddr *)&sin, sizeof(sin)) == 0);
52861274c9SGleb Smirnoff len = sizeof(sin);
53861274c9SGleb Smirnoff ATF_REQUIRE(getsockname(s, (struct sockaddr *)&sin, &len) == 0);
5450789d0bSGleb Smirnoff sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
55861274c9SGleb Smirnoff ATF_REQUIRE(listen(s, -1) == 0);
56861274c9SGleb Smirnoff #if 0
57861274c9SGleb Smirnoff /*
58861274c9SGleb Smirnoff * The disabled code is that you would normally do.
59861274c9SGleb Smirnoff */
60861274c9SGleb Smirnoff ATF_REQUIRE(connect(c, (struct sockaddr *)&sin, sizeof(sin)) == 0);
61861274c9SGleb Smirnoff ATF_REQUIRE(send(c, &buf, sizeof(buf), 0) == sizeof(buf));
62861274c9SGleb Smirnoff #else
63861274c9SGleb Smirnoff /*
64861274c9SGleb Smirnoff * And this is implied connect.
65861274c9SGleb Smirnoff */
66861274c9SGleb Smirnoff ATF_REQUIRE(sendto(c, &buf, sizeof(buf), 0, (struct sockaddr *)&sin,
67861274c9SGleb Smirnoff sizeof(sin)) == sizeof(buf));
68861274c9SGleb Smirnoff #endif
69861274c9SGleb Smirnoff
70861274c9SGleb Smirnoff ATF_REQUIRE((a = accept(s, NULL, NULL)) != 1);
71861274c9SGleb Smirnoff ATF_REQUIRE(recv(a, &repl, sizeof(repl), 0) == sizeof(buf));
72861274c9SGleb Smirnoff ATF_REQUIRE(strcmp(buf, repl) == 0);
73861274c9SGleb Smirnoff }
74861274c9SGleb Smirnoff
75*cd05c880SGleb Smirnoff /*
76*cd05c880SGleb Smirnoff * A disconnected TCP socket shall return the local address it used before
77*cd05c880SGleb Smirnoff * it was disconnected.
78*cd05c880SGleb Smirnoff */
79*cd05c880SGleb Smirnoff ATF_TC_WITHOUT_HEAD(getsockname_disconnected);
ATF_TC_BODY(getsockname_disconnected,tc)80*cd05c880SGleb Smirnoff ATF_TC_BODY(getsockname_disconnected, tc)
81*cd05c880SGleb Smirnoff {
82*cd05c880SGleb Smirnoff struct sockaddr_in sin = {
83*cd05c880SGleb Smirnoff .sin_family = AF_INET,
84*cd05c880SGleb Smirnoff .sin_len = sizeof(sin),
85*cd05c880SGleb Smirnoff };
86*cd05c880SGleb Smirnoff socklen_t len;
87*cd05c880SGleb Smirnoff int s, c, a;
88*cd05c880SGleb Smirnoff
89*cd05c880SGleb Smirnoff ATF_REQUIRE(s = socket(PF_INET, SOCK_STREAM, 0));
90*cd05c880SGleb Smirnoff ATF_REQUIRE(c = socket(PF_INET, SOCK_STREAM, 0));
91*cd05c880SGleb Smirnoff
92*cd05c880SGleb Smirnoff ATF_REQUIRE(bind(s, (struct sockaddr *)&sin, sizeof(sin)) == 0);
93*cd05c880SGleb Smirnoff len = sizeof(sin);
94*cd05c880SGleb Smirnoff ATF_REQUIRE(getsockname(s, (struct sockaddr *)&sin, &len) == 0);
95*cd05c880SGleb Smirnoff sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
96*cd05c880SGleb Smirnoff ATF_REQUIRE(listen(s, -1) == 0);
97*cd05c880SGleb Smirnoff ATF_REQUIRE(connect(c, (struct sockaddr *)&sin, sizeof(sin)) == 0);
98*cd05c880SGleb Smirnoff ATF_REQUIRE((a = accept(s, NULL, NULL)) != 1);
99*cd05c880SGleb Smirnoff ATF_REQUIRE(close(a) == 0);
100*cd05c880SGleb Smirnoff ATF_REQUIRE(getsockname(c, (struct sockaddr *)&sin, &len) == 0);
101*cd05c880SGleb Smirnoff ATF_REQUIRE(sin.sin_addr.s_addr == htonl(INADDR_LOOPBACK));
102*cd05c880SGleb Smirnoff
103*cd05c880SGleb Smirnoff close(c);
104*cd05c880SGleb Smirnoff close(s);
105*cd05c880SGleb Smirnoff }
106*cd05c880SGleb Smirnoff
ATF_TP_ADD_TCS(tp)107861274c9SGleb Smirnoff ATF_TP_ADD_TCS(tp)
108861274c9SGleb Smirnoff {
109*cd05c880SGleb Smirnoff ATF_TP_ADD_TC(tp, implied_connect);
110*cd05c880SGleb Smirnoff ATF_TP_ADD_TC(tp, getsockname_disconnected);
111861274c9SGleb Smirnoff
112861274c9SGleb Smirnoff return (atf_no_error());
113861274c9SGleb Smirnoff }
114