1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2023 Gleb Smirnoff <glebius@FreeBSD.org> 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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/socket.h> 29 #include <netinet/in.h> 30 #include <errno.h> 31 #include <fcntl.h> 32 #include <string.h> 33 34 #include <atf-c.h> 35 36 static int 37 tcp4_listensock(struct sockaddr_in *sin) 38 { 39 int l; 40 41 ATF_REQUIRE((l = socket(PF_INET, SOCK_STREAM, 0)) > 0); 42 ATF_REQUIRE(setsockopt(l, SOL_SOCKET, SO_REUSEADDR, &(socklen_t){1}, 43 sizeof(int)) == 0); 44 *sin = (struct sockaddr_in){ 45 .sin_len = sizeof(sin), 46 .sin_family = AF_INET, 47 .sin_addr.s_addr = htonl(INADDR_LOOPBACK), 48 }; 49 ATF_REQUIRE(bind(l, (struct sockaddr *)sin, sizeof(*sin)) == 0); 50 ATF_REQUIRE(getsockname(l, (struct sockaddr *)sin, 51 &(socklen_t){ sizeof(*sin) }) == 0); 52 ATF_REQUIRE(listen(l, -1) == 0); 53 54 return (l); 55 } 56 57 static int 58 tcp4_clientsock(struct sockaddr_in *sin) 59 { 60 int s; 61 62 ATF_REQUIRE((s = socket(PF_INET, SOCK_STREAM, 0)) > 0); 63 ATF_REQUIRE(connect(s, (struct sockaddr *)sin, sizeof(*sin)) == 0); 64 65 return (s); 66 } 67 68 ATF_TC_WITHOUT_HEAD(tcp4_zerolen); 69 ATF_TC_BODY(tcp4_zerolen, tc) 70 { 71 static char canary[sizeof(struct sockaddr_in)] = 72 { [0 ... sizeof(struct sockaddr_in) - 1] = 0xa }; 73 struct sockaddr_in sin, ret; 74 socklen_t salen; 75 int l; 76 77 l = tcp4_listensock(&sin); 78 (void )tcp4_clientsock(&sin); 79 80 memcpy(&ret, &canary, sizeof(ret)); 81 salen = 0; 82 ATF_REQUIRE(accept(l, (struct sockaddr *)&ret, &salen) > 0); 83 ATF_REQUIRE(memcmp(&ret, &canary, sizeof(ret)) == 0); 84 #if 0 85 /* Linux behavior. Matches my reading of accept(2) and POSIX. */ 86 ATF_REQUIRE(salen == sizeof(struct sockaddr_in)); 87 #endif 88 /* Note: Linux will block for connection here, we fail immediately. */ 89 ATF_REQUIRE(accept(l, (struct sockaddr *)&ret, NULL) == -1); 90 ATF_REQUIRE(errno == EFAULT); 91 } 92 93 ATF_TC_WITHOUT_HEAD(tcp4); 94 ATF_TC_BODY(tcp4, tc) 95 { 96 struct sockaddr_in sin, ret; 97 socklen_t salen; 98 int l, s; 99 100 l = tcp4_listensock(&sin); 101 s = tcp4_clientsock(&sin); 102 103 salen = sizeof(struct sockaddr_in) + 2; 104 ATF_REQUIRE(accept(l, (struct sockaddr *)&ret, &salen) > 0); 105 ATF_REQUIRE(salen == sizeof(struct sockaddr_in)); 106 ATF_REQUIRE(getsockname(s, (struct sockaddr *)&sin, 107 &(socklen_t){ sizeof(sin) }) == 0); 108 ATF_REQUIRE(memcmp(&ret, &sin, sizeof(sin)) == 0); 109 } 110 111 ATF_TC_WITHOUT_HEAD(tcp4_noaddr); 112 ATF_TC_BODY(tcp4_noaddr, tc) 113 { 114 struct sockaddr_in sin; 115 int l; 116 117 l = tcp4_listensock(&sin); 118 (void )tcp4_clientsock(&sin); 119 120 ATF_REQUIRE(accept(l, NULL, NULL) > 0); 121 } 122 123 ATF_TP_ADD_TCS(tp) 124 { 125 ATF_TP_ADD_TC(tp, tcp4); 126 ATF_TP_ADD_TC(tp, tcp4_noaddr); 127 ATF_TP_ADD_TC(tp, tcp4_zerolen); 128 129 return (atf_no_error()); 130 } 131