xref: /freebsd/tests/sys/netinet/tcp_implied_connect.c (revision 70f5c6e34ffd21c5dd41e35c72dc1b0e84d67a21)
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 
36861274c9SGleb Smirnoff ATF_TC_WITHOUT_HEAD(tcp_implied_connect);
ATF_TC_BODY(tcp_implied_connect,tc)37861274c9SGleb Smirnoff ATF_TC_BODY(tcp_implied_connect, tc)
38861274c9SGleb Smirnoff {
39*70f5c6e3SGleb Smirnoff 	struct sockaddr_in sin = {
40*70f5c6e3SGleb Smirnoff 		.sin_family = AF_INET,
41*70f5c6e3SGleb Smirnoff 		.sin_len = sizeof(sin),
42*70f5c6e3SGleb Smirnoff 	};
43*70f5c6e3SGleb Smirnoff 	const char buf[] = "hello";
44*70f5c6e3SGleb 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);
54861274c9SGleb Smirnoff 	ATF_REQUIRE(listen(s, -1) == 0);
55861274c9SGleb Smirnoff #if 0
56861274c9SGleb Smirnoff 	/*
57861274c9SGleb Smirnoff 	 * The disabled code is that you would normally do.
58861274c9SGleb Smirnoff 	 */
59861274c9SGleb Smirnoff 	ATF_REQUIRE(connect(c, (struct sockaddr *)&sin, sizeof(sin)) == 0);
60861274c9SGleb Smirnoff 	ATF_REQUIRE(send(c, &buf, sizeof(buf), 0) == sizeof(buf));
61861274c9SGleb Smirnoff #else
62861274c9SGleb Smirnoff 	/*
63861274c9SGleb Smirnoff 	 * And this is implied connect.
64861274c9SGleb Smirnoff 	 */
65861274c9SGleb Smirnoff 	ATF_REQUIRE(sendto(c, &buf, sizeof(buf), 0, (struct sockaddr *)&sin,
66861274c9SGleb Smirnoff 	    sizeof(sin)) == sizeof(buf));
67861274c9SGleb Smirnoff #endif
68861274c9SGleb Smirnoff 
69861274c9SGleb Smirnoff 	ATF_REQUIRE((a = accept(s, NULL, NULL)) != 1);
70861274c9SGleb Smirnoff 	ATF_REQUIRE(recv(a, &repl, sizeof(repl), 0) == sizeof(buf));
71861274c9SGleb Smirnoff 	ATF_REQUIRE(strcmp(buf, repl) == 0);
72861274c9SGleb Smirnoff }
73861274c9SGleb Smirnoff 
ATF_TP_ADD_TCS(tp)74861274c9SGleb Smirnoff ATF_TP_ADD_TCS(tp)
75861274c9SGleb Smirnoff {
76861274c9SGleb Smirnoff 	ATF_TP_ADD_TC(tp, tcp_implied_connect);
77861274c9SGleb Smirnoff 
78861274c9SGleb Smirnoff 	return (atf_no_error());
79861274c9SGleb Smirnoff }
80