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