xref: /minix3/tests/lib/libc/sys/t_connect.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: t_connect.c,v 1.2 2015/04/05 23:17:41 rtr Exp $	*/
211be35a1SLionel Sambuc /*
311be35a1SLionel Sambuc  * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
411be35a1SLionel Sambuc  * All rights reserved.
511be35a1SLionel Sambuc  *
611be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
711be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
811be35a1SLionel Sambuc  * are met:
911be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1011be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1111be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1211be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
1311be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
1411be35a1SLionel Sambuc  *
1511be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
1611be35a1SLionel Sambuc  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
1711be35a1SLionel Sambuc  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1811be35a1SLionel Sambuc  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1911be35a1SLionel Sambuc  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
2011be35a1SLionel Sambuc  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2111be35a1SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2211be35a1SLionel Sambuc  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2311be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
2411be35a1SLionel Sambuc  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2511be35a1SLionel Sambuc  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
2611be35a1SLionel Sambuc  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2711be35a1SLionel Sambuc  */
2811be35a1SLionel Sambuc 
2911be35a1SLionel Sambuc #include <err.h>
3011be35a1SLionel Sambuc #include <errno.h>
3111be35a1SLionel Sambuc #include <string.h>
3211be35a1SLionel Sambuc #include <unistd.h>
3311be35a1SLionel Sambuc 
3411be35a1SLionel Sambuc #include <arpa/inet.h>
3511be35a1SLionel Sambuc #include <netinet/in.h>
3611be35a1SLionel Sambuc 
3711be35a1SLionel Sambuc #include <atf-c.h>
3811be35a1SLionel Sambuc 
3911be35a1SLionel Sambuc ATF_TC(connect_low_port);
ATF_TC_HEAD(connect_low_port,tc)4011be35a1SLionel Sambuc ATF_TC_HEAD(connect_low_port, tc)
4111be35a1SLionel Sambuc {
4211be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks that low-port allocation "
4311be35a1SLionel Sambuc 	    "works");
4411be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "require.user", "root");
4511be35a1SLionel Sambuc }
ATF_TC_BODY(connect_low_port,tc)4611be35a1SLionel Sambuc ATF_TC_BODY(connect_low_port, tc)
4711be35a1SLionel Sambuc {
4811be35a1SLionel Sambuc 	struct sockaddr_in sin, sinlist;
4911be35a1SLionel Sambuc 	int sd, val, slist;
5011be35a1SLionel Sambuc 	socklen_t slen;
5111be35a1SLionel Sambuc 
5211be35a1SLionel Sambuc 	slist = socket(AF_INET, SOCK_STREAM, 0);
5311be35a1SLionel Sambuc 	sd = socket(AF_INET, SOCK_STREAM, 0);
5411be35a1SLionel Sambuc 
5511be35a1SLionel Sambuc 	/* bind listening socket */
5611be35a1SLionel Sambuc 	memset(&sinlist, 0, sizeof(sinlist));
5711be35a1SLionel Sambuc 	sinlist.sin_family = AF_INET;
5811be35a1SLionel Sambuc 	sinlist.sin_port = htons(31522);
5911be35a1SLionel Sambuc 	sinlist.sin_addr.s_addr = inet_addr("127.0.0.1");
6011be35a1SLionel Sambuc 
6111be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(bind(slist,
6211be35a1SLionel Sambuc 	    (struct sockaddr *)&sinlist, sizeof(sinlist)), 0);
6311be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(listen(slist, 1), 0);
6411be35a1SLionel Sambuc 
6511be35a1SLionel Sambuc 	val = IP_PORTRANGE_LOW;
6611be35a1SLionel Sambuc 	if (setsockopt(sd, IPPROTO_IP, IP_PORTRANGE, &val,
6711be35a1SLionel Sambuc 	    sizeof(val)) == -1)
6811be35a1SLionel Sambuc 		atf_tc_fail("setsockopt failed: %s", strerror(errno));
6911be35a1SLionel Sambuc 
7011be35a1SLionel Sambuc 	memset(&sin, 0, sizeof(sin));
7111be35a1SLionel Sambuc 
7211be35a1SLionel Sambuc 	sin.sin_port = htons(31522);
7311be35a1SLionel Sambuc 	sin.sin_addr.s_addr = inet_addr("127.0.0.1");
7411be35a1SLionel Sambuc 	sin.sin_family = AF_INET;
7511be35a1SLionel Sambuc 
7611be35a1SLionel Sambuc 	if (connect(sd, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
7711be35a1SLionel Sambuc 		int serrno = errno;
7811be35a1SLionel Sambuc 		atf_tc_fail("connect failed: %s%s",
7911be35a1SLionel Sambuc 		    strerror(serrno),
8011be35a1SLionel Sambuc 		    serrno != EACCES ? "" :
8111be35a1SLionel Sambuc 		    " (see http://mail-index.netbsd.org/"
8211be35a1SLionel Sambuc 		    "source-changes/2007/12/16/0011.html)");
8311be35a1SLionel Sambuc 	}
8411be35a1SLionel Sambuc 
8511be35a1SLionel Sambuc 	slen = sizeof(sin);
8611be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(getsockname(sd, (struct sockaddr *)&sin, &slen), 0);
8711be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(slen, sizeof(sin));
8811be35a1SLionel Sambuc 	ATF_REQUIRE(ntohs(sin.sin_port) <= IPPORT_RESERVEDMAX);
8911be35a1SLionel Sambuc 
9011be35a1SLionel Sambuc 	close(sd);
9111be35a1SLionel Sambuc }
9211be35a1SLionel Sambuc 
93*0a6a1f1dSLionel Sambuc ATF_TC(connect_foreign_family);
ATF_TC_HEAD(connect_foreign_family,tc)94*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(connect_foreign_family, tc)
95*0a6a1f1dSLionel Sambuc {
96*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks that connecting a socket "
97*0a6a1f1dSLionel Sambuc 	    "with a different address family fails");
98*0a6a1f1dSLionel Sambuc }
ATF_TC_BODY(connect_foreign_family,tc)99*0a6a1f1dSLionel Sambuc ATF_TC_BODY(connect_foreign_family, tc)
100*0a6a1f1dSLionel Sambuc {
101*0a6a1f1dSLionel Sambuc 	struct sockaddr_in addr;
102*0a6a1f1dSLionel Sambuc 
103*0a6a1f1dSLionel Sambuc 	/* addr.sin_family = AF_UNSPEC = 0 */
104*0a6a1f1dSLionel Sambuc 	memset(&addr, 0, sizeof(addr));
105*0a6a1f1dSLionel Sambuc 
106*0a6a1f1dSLionel Sambuc 	/*
107*0a6a1f1dSLionel Sambuc 	 * it is not necessary to initialize sin_{addr,port} since
108*0a6a1f1dSLionel Sambuc 	 * those structure members shall not be accessed if connect
109*0a6a1f1dSLionel Sambuc 	 * fails correctly.
110*0a6a1f1dSLionel Sambuc 	 */
111*0a6a1f1dSLionel Sambuc 
112*0a6a1f1dSLionel Sambuc 	int sock = socket(AF_LOCAL, SOCK_STREAM, 0);
113*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(sock != -1);
114*0a6a1f1dSLionel Sambuc 
115*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(-1 == connect(sock, (struct sockaddr *)&addr, sizeof(addr)));
116*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(EAFNOSUPPORT == errno);
117*0a6a1f1dSLionel Sambuc 
118*0a6a1f1dSLionel Sambuc 	close(sock);
119*0a6a1f1dSLionel Sambuc }
120*0a6a1f1dSLionel Sambuc 
ATF_TP_ADD_TCS(tp)12111be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
12211be35a1SLionel Sambuc {
12311be35a1SLionel Sambuc 
12411be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, connect_low_port);
125*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, connect_foreign_family);
12611be35a1SLionel Sambuc 
12711be35a1SLionel Sambuc 	return atf_no_error();
12811be35a1SLionel Sambuc }
129