1 /* $NetBSD: t_listen.c,v 1.5 2017/01/13 20:41:50 christos Exp $ */
3 * Copyright (c) 2007 The NetBSD Foundation, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/socket.h>
37 #include <arpa/inet.h>
38 #include <netinet/in.h>
41 #include <sys/socket.h>
44 static const char *path = "listen";
46 ATF_TC_WITH_CLEANUP(listen_err);
47 ATF_TC_HEAD(listen_err, tc)
49 atf_tc_set_md_var(tc, "descr",
50 "Checks errors from listen(2) (PR standards/46150)");
53 ATF_TC_BODY(listen_err, tc)
55 static const size_t siz = sizeof(struct sockaddr_in);
56 struct sockaddr_in sina, sinb;
59 (void)memset(&sina, 0, sizeof(struct sockaddr_in));
60 (void)memset(&sinb, 0, sizeof(struct sockaddr_in));
62 sina.sin_family = AF_INET;
63 sina.sin_port = htons(31522);
64 sina.sin_addr.s_addr = inet_addr("127.0.0.1");
66 sinb.sin_family = AF_INET;
67 sinb.sin_port = htons(31522);
68 sinb.sin_addr.s_addr = inet_addr("127.0.0.1");
70 fda = socket(AF_INET, SOCK_STREAM, 0);
71 fdb = socket(AF_INET, SOCK_STREAM, 0);
72 fdc = open("listen", O_RDWR | O_CREAT, 0600);
74 ATF_REQUIRE(fda >= 0 && fdb >= 0 && fdc >= 0);
75 ATF_REQUIRE_ERRNO(ENOTSOCK, listen(fdc, 1) == -1);
80 ATF_REQUIRE(bind(fda, (struct sockaddr *)&sina, siz) == 0);
81 ATF_REQUIRE(listen(fda, 1) == 0);
84 * According to IEEE Std 1003.1-2008: if the socket is
85 * already connected, the call should fail with EINVAL.
87 ATF_REQUIRE(connect(fdb, (struct sockaddr *)&sinb, siz) == 0);
88 ATF_REQUIRE_ERRNO(EINVAL, listen(fdb, 1) == -1);
93 ATF_REQUIRE_ERRNO(EBADF, connect(fdb,
94 (struct sockaddr *)&sinb, siz) == -1);
97 ATF_TC_CLEANUP(listen_err, tc)
102 ATF_TC(listen_low_port);
103 ATF_TC_HEAD(listen_low_port, tc)
105 atf_tc_set_md_var(tc, "descr", "Does low-port allocation work?");
106 atf_tc_set_md_var(tc, "require.user", "root");
109 ATF_TC_BODY(listen_low_port, tc)
113 sd = socket(AF_INET, SOCK_STREAM, 0);
114 ATF_REQUIRE_MSG(sd != -1, "socket failed: %s", strerror(errno));
116 val = IP_PORTRANGE_LOW;
117 if (setsockopt(sd, IPPROTO_IP, IP_PORTRANGE, &val,
119 atf_tc_fail("setsockopt failed: %s", strerror(errno));
121 if (listen(sd, 5) == -1) {
123 atf_tc_fail("listen failed: %s%s",
125 serrno != EACCES ? "" :
126 " (see http://mail-index.netbsd.org/"
127 "source-changes/2007/12/16/0011.html)");
136 ATF_TP_ADD_TC(tp, listen_err);
137 ATF_TP_ADD_TC(tp, listen_low_port);