diff options
Diffstat (limited to '')
| -rw-r--r-- | src/regress/lib/libc/sys/t_connect.c | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/src/regress/lib/libc/sys/t_connect.c b/src/regress/lib/libc/sys/t_connect.c new file mode 100644 index 0000000000..c1730a40c7 --- /dev/null +++ b/src/regress/lib/libc/sys/t_connect.c | |||
| @@ -0,0 +1,135 @@ | |||
| 1 | /* $OpenBSD: t_connect.c,v 1.1 2021/09/02 12:40:44 mbuhl Exp $ */ | ||
| 2 | /* $NetBSD: t_connect.c,v 1.3 2017/01/13 20:09:48 christos Exp $ */ | ||
| 3 | /* | ||
| 4 | * Copyright (c) 2007, 2008 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 | #include "macros.h" | ||
| 30 | |||
| 31 | #include <sys/socket.h> | ||
| 32 | #include <err.h> | ||
| 33 | #include <errno.h> | ||
| 34 | #include <string.h> | ||
| 35 | #include <unistd.h> | ||
| 36 | |||
| 37 | #include <arpa/inet.h> | ||
| 38 | #include <netinet/in.h> | ||
| 39 | |||
| 40 | #include "atf-c.h" | ||
| 41 | |||
| 42 | ATF_TC(connect_low_port); | ||
| 43 | ATF_TC_HEAD(connect_low_port, tc) | ||
| 44 | { | ||
| 45 | atf_tc_set_md_var(tc, "descr", "Checks that low-port allocation " | ||
| 46 | "works"); | ||
| 47 | atf_tc_set_md_var(tc, "require.user", "root"); | ||
| 48 | } | ||
| 49 | ATF_TC_BODY(connect_low_port, tc) | ||
| 50 | { | ||
| 51 | struct sockaddr_in sin, sinlist; | ||
| 52 | int sd, val, slist; | ||
| 53 | socklen_t slen; | ||
| 54 | |||
| 55 | slist = socket(AF_INET, SOCK_STREAM, 0); | ||
| 56 | sd = socket(AF_INET, SOCK_STREAM, 0); | ||
| 57 | |||
| 58 | ATF_REQUIRE(sd > 0); | ||
| 59 | ATF_REQUIRE(slist > 0); | ||
| 60 | |||
| 61 | /* bind listening socket */ | ||
| 62 | memset(&sinlist, 0, sizeof(sinlist)); | ||
| 63 | sinlist.sin_family = AF_INET; | ||
| 64 | sinlist.sin_port = htons(31522); | ||
| 65 | sinlist.sin_addr.s_addr = inet_addr("127.0.0.1"); | ||
| 66 | |||
| 67 | ATF_REQUIRE_EQ(bind(slist, | ||
| 68 | (struct sockaddr *)&sinlist, sizeof(sinlist)), 0); | ||
| 69 | ATF_REQUIRE_EQ(listen(slist, 1), 0); | ||
| 70 | |||
| 71 | val = IP_PORTRANGE_LOW; | ||
| 72 | if (setsockopt(sd, IPPROTO_IP, IP_PORTRANGE, &val, | ||
| 73 | sizeof(val)) == -1) | ||
| 74 | atf_tc_fail("setsockopt failed: %s", strerror(errno)); | ||
| 75 | |||
| 76 | memset(&sin, 0, sizeof(sin)); | ||
| 77 | |||
| 78 | sin.sin_port = htons(31522); | ||
| 79 | sin.sin_addr.s_addr = inet_addr("127.0.0.1"); | ||
| 80 | sin.sin_family = AF_INET; | ||
| 81 | |||
| 82 | if (connect(sd, (struct sockaddr *)&sin, sizeof(sin)) == -1) { | ||
| 83 | int serrno = errno; | ||
| 84 | atf_tc_fail("connect failed: %s%s", | ||
| 85 | strerror(serrno), | ||
| 86 | serrno != EACCES ? "" : | ||
| 87 | " (see http://mail-index.netbsd.org/" | ||
| 88 | "source-changes/2007/12/16/0011.html)"); | ||
| 89 | } | ||
| 90 | |||
| 91 | slen = sizeof(sin); | ||
| 92 | ATF_REQUIRE_EQ(getsockname(sd, (struct sockaddr *)&sin, &slen), 0); | ||
| 93 | ATF_REQUIRE_EQ(slen, sizeof(sin)); | ||
| 94 | ATF_REQUIRE(ntohs(sin.sin_port) <= IPPORT_RESERVEDMAX); | ||
| 95 | |||
| 96 | close(sd); | ||
| 97 | close(slist); | ||
| 98 | } | ||
| 99 | |||
| 100 | ATF_TC(connect_foreign_family); | ||
| 101 | ATF_TC_HEAD(connect_foreign_family, tc) | ||
| 102 | { | ||
| 103 | atf_tc_set_md_var(tc, "descr", "Checks that connecting a socket " | ||
| 104 | "with a different address family fails"); | ||
| 105 | } | ||
| 106 | ATF_TC_BODY(connect_foreign_family, tc) | ||
| 107 | { | ||
| 108 | struct sockaddr_in addr; | ||
| 109 | |||
| 110 | /* addr.sin_family = AF_UNSPEC = 0 */ | ||
| 111 | memset(&addr, 0, sizeof(addr)); | ||
| 112 | |||
| 113 | /* | ||
| 114 | * it is not necessary to initialize sin_{addr,port} since | ||
| 115 | * those structure members shall not be accessed if connect | ||
| 116 | * fails correctly. | ||
| 117 | */ | ||
| 118 | |||
| 119 | int sock = socket(AF_LOCAL, SOCK_STREAM, 0); | ||
| 120 | ATF_REQUIRE(sock != -1); | ||
| 121 | |||
| 122 | ATF_REQUIRE(-1 == connect(sock, (struct sockaddr *)&addr, sizeof(addr))); | ||
| 123 | ATF_REQUIRE(EAFNOSUPPORT == errno); | ||
| 124 | |||
| 125 | close(sock); | ||
| 126 | } | ||
| 127 | |||
| 128 | ATF_TP_ADD_TCS(tp) | ||
| 129 | { | ||
| 130 | |||
| 131 | ATF_TP_ADD_TC(tp, connect_low_port); | ||
| 132 | ATF_TP_ADD_TC(tp, connect_foreign_family); | ||
| 133 | |||
| 134 | return atf_no_error(); | ||
| 135 | } | ||
