1 /* $OpenBSD: t_listen.c,v 1.1.1.1 2019/11/19 19:57:03 bluhm Exp $ */
2 /* $NetBSD: t_listen.c,v 1.6 2019/07/09 16:24:01 maya Exp $ */
3 /*
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include "macros.h"
31
32 #include <sys/socket.h>
33 #include "atf-c.h"
34 #include <err.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <string.h>
38 #include <unistd.h>
39
40 #include <arpa/inet.h>
41 #include <netinet/in.h>
42
43 static const char *path = "listen";
44
45 ATF_TC_WITH_CLEANUP(listen_err);
ATF_TC_HEAD(listen_err,tc)46 ATF_TC_HEAD(listen_err, tc)
47 {
48 atf_tc_set_md_var(tc, "descr",
49 "Checks errors from listen(2) (PR standards/46150)");
50 }
51
ATF_TC_BODY(listen_err,tc)52 ATF_TC_BODY(listen_err, tc)
53 {
54 static const size_t siz = sizeof(struct sockaddr_in);
55 struct sockaddr_in sina, sinb;
56 int fda, fdb, fdc;
57
58 (void)memset(&sina, 0, sizeof(struct sockaddr_in));
59 (void)memset(&sinb, 0, sizeof(struct sockaddr_in));
60
61 sina.sin_family = AF_INET;
62 sina.sin_port = htons(31522);
63 sina.sin_addr.s_addr = inet_addr("127.0.0.1");
64
65 sinb.sin_family = AF_INET;
66 sinb.sin_port = htons(31522);
67 sinb.sin_addr.s_addr = inet_addr("127.0.0.1");
68
69 fda = socket(AF_INET, SOCK_STREAM, 0);
70 fdb = socket(AF_INET, SOCK_STREAM, 0);
71 fdc = open("listen", O_RDWR | O_CREAT, 0600);
72
73 ATF_REQUIRE(fda >= 0 && fdb >= 0 && fdc >= 0);
74 ATF_REQUIRE_ERRNO(ENOTSOCK, listen(fdc, 1) == -1);
75
76 (void)close(fdc);
77 (void)unlink(path);
78
79 ATF_REQUIRE(bind(fda, (struct sockaddr *)&sina, siz) == 0);
80 ATF_REQUIRE(listen(fda, 1) == 0);
81
82 /*
83 * According to IEEE Std 1003.1-2008: if the socket is
84 * already connected, the call should fail with EINVAL.
85 */
86 ATF_REQUIRE(connect(fdb, (struct sockaddr *)&sinb, siz) == 0);
87 ATF_REQUIRE_ERRNO(EINVAL, listen(fdb, 1) == -1);
88
89 (void)close(fda);
90 (void)close(fdb);
91
92 ATF_REQUIRE_ERRNO(EBADF, connect(fdb,
93 (struct sockaddr *)&sinb, siz) == -1);
94 }
95
ATF_TC_CLEANUP(listen_err,tc)96 ATF_TC_CLEANUP(listen_err, tc)
97 {
98 (void)unlink(path);
99 }
100
101 ATF_TC(listen_low_port);
ATF_TC_HEAD(listen_low_port,tc)102 ATF_TC_HEAD(listen_low_port, tc)
103 {
104 atf_tc_set_md_var(tc, "descr", "Does low-port allocation work?");
105 atf_tc_set_md_var(tc, "require.user", "root");
106 }
107
ATF_TC_BODY(listen_low_port,tc)108 ATF_TC_BODY(listen_low_port, tc)
109 {
110 int sd, val;
111
112 sd = socket(AF_INET, SOCK_STREAM, 0);
113 ATF_REQUIRE_MSG(sd != -1, "socket failed: %s", strerror(errno));
114
115 val = IP_PORTRANGE_LOW;
116 if (setsockopt(sd, IPPROTO_IP, IP_PORTRANGE, &val,
117 sizeof(val)) == -1)
118 atf_tc_fail("setsockopt failed: %s", strerror(errno));
119
120 if (listen(sd, 5) == -1) {
121 int serrno = errno;
122 atf_tc_fail("listen failed: %s%s",
123 strerror(serrno),
124 serrno != EACCES ? "" :
125 " (see http://mail-index.netbsd.org/"
126 "source-changes/2007/12/16/0011.html)");
127 }
128
129 close(sd);
130 }
131
ATF_TP_ADD_TCS(tp)132 ATF_TP_ADD_TCS(tp)
133 {
134
135 ATF_TP_ADD_TC(tp, listen_err);
136 ATF_TP_ADD_TC(tp, listen_low_port);
137
138 return atf_no_error();
139 }
140