xref: /freebsd/contrib/netbsd-tests/lib/libc/sys/t_connect.c (revision 6186fd1857626de0f7cb1a9e4dff19082f9ebb11)
1 /*	$NetBSD: t_connect.c,v 1.1 2011/11/05 18:19:02 jruoho Exp $	*/
2 /*
3  * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
4  * All rights reserved.
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 NETBSD FOUNDATION, INC. AND
16  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <err.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <unistd.h>
33 
34 #include <arpa/inet.h>
35 #include <netinet/in.h>
36 
37 #include <atf-c.h>
38 
39 ATF_TC(connect_low_port);
40 ATF_TC_HEAD(connect_low_port, tc)
41 {
42 	atf_tc_set_md_var(tc, "descr", "Checks that low-port allocation "
43 	    "works");
44 	atf_tc_set_md_var(tc, "require.user", "root");
45 }
46 ATF_TC_BODY(connect_low_port, tc)
47 {
48 	struct sockaddr_in sin, sinlist;
49 	int sd, val, slist;
50 	socklen_t slen;
51 
52 	slist = socket(AF_INET, SOCK_STREAM, 0);
53 	sd = socket(AF_INET, SOCK_STREAM, 0);
54 
55 	/* bind listening socket */
56 	memset(&sinlist, 0, sizeof(sinlist));
57 	sinlist.sin_family = AF_INET;
58 	sinlist.sin_port = htons(31522);
59 	sinlist.sin_addr.s_addr = inet_addr("127.0.0.1");
60 
61 	ATF_REQUIRE_EQ(bind(slist,
62 	    (struct sockaddr *)&sinlist, sizeof(sinlist)), 0);
63 	ATF_REQUIRE_EQ(listen(slist, 1), 0);
64 
65 	val = IP_PORTRANGE_LOW;
66 	if (setsockopt(sd, IPPROTO_IP, IP_PORTRANGE, &val,
67 	    sizeof(val)) == -1)
68 		atf_tc_fail("setsockopt failed: %s", strerror(errno));
69 
70 	memset(&sin, 0, sizeof(sin));
71 
72 	sin.sin_port = htons(31522);
73 	sin.sin_addr.s_addr = inet_addr("127.0.0.1");
74 	sin.sin_family = AF_INET;
75 
76 	if (connect(sd, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
77 		int serrno = errno;
78 		atf_tc_fail("connect failed: %s%s",
79 		    strerror(serrno),
80 		    serrno != EACCES ? "" :
81 		    " (see http://mail-index.netbsd.org/"
82 		    "source-changes/2007/12/16/0011.html)");
83 	}
84 
85 	slen = sizeof(sin);
86 	ATF_REQUIRE_EQ(getsockname(sd, (struct sockaddr *)&sin, &slen), 0);
87 	ATF_REQUIRE_EQ(slen, sizeof(sin));
88 	ATF_REQUIRE(ntohs(sin.sin_port) <= IPPORT_RESERVEDMAX);
89 
90 	close(sd);
91 }
92 
93 ATF_TP_ADD_TCS(tp)
94 {
95 
96 	ATF_TP_ADD_TC(tp, connect_low_port);
97 
98 	return atf_no_error();
99 }
100