From nobody Thu Nov 16 16:24:42 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4SWQNW0q56z50qHy; Thu, 16 Nov 2023 16:24:43 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4SWQNW0MTzz3Fg1; Thu, 16 Nov 2023 16:24:43 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1700151883; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=N8sxdNi5ZHOk2enD/70dhX0JT4UCZ/SwvUK9leadrEg=; b=LN0WVL3LxlpyvTSU4VuMmEDlG1mzUBpjZuegUzoBaFOCvGsv1vjRVNZrxuYOGs9dmelEz2 RugmLdXTsp5kQPYsZ+l0C1Mf4CQsWnGl4ZpluoknAUVZn8jBthyfRKGDruWwcFDGHbwFzy j9hAtcXtRm7Zid09O08wOxAOVQ83pxXH+c4JCGzaDqSFs4mSoEA5yV2dJDgKqHtSERCsQ4 0sQaDa7cnTmYh3hM3J92miNHSyV2aA0d1W4SlkL/crWCWP4G9Pl2oYEg7G1wzn/M7FXR3+ CkLQkaT83OuVwhI64pNImb2GwOYzqdLhd2d3i+K9OWTJqi5Nf9EbH1m7kIDsZg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1700151883; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=N8sxdNi5ZHOk2enD/70dhX0JT4UCZ/SwvUK9leadrEg=; b=S5iJsQ7i31lFb8v+l75F5ZlgBYYmgfqskxyW6wZ6RgaDNJaxlcIx+vmFaxIYt8RRc4xHgG sd0utocd8j4vURnVFdoEZ6scdsoYnG9GL0Uc/dEI2ZROPtilQs1LdqINEK6/72Ruu5vEjK tNxAJt5kYvAQd0Wh3z40SoHBt+YnymK/aGKd2YgIHmzMX5kV06SvNLHVrGTiQvubfiunuW UZhI/gVSjSBs10kWKLrDHEKUM9vplU2pBl37ns4+7W9p2KIoNDBi8R98xY+D5uJkp/kVSg wc43ERYWPrZaXWQlDm1/Tf7/tJM7k6gH6RWnyMGgMNMYYgckCC8sjHQdpC4Mbw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1700151883; a=rsa-sha256; cv=none; b=Dm7VzZ+sJjXTNPZS6WZkvDodMLvMDKPc/CtaAOHr6W6arR5gSpKSx14DI5LDvxj+xeEK3p +siS1Xndi+9vVZMqv6/nEnV1dv5plzppYwybzGWfnCHQB2hb4EtLGvxpUfzTy0VX6NvyeJ VhFW6xEiMx48ANFKkVFCvy/Pn+8gvoiK9vGJ0U4Z3eeVxOpb/NG2BA2GmFF6CcMNU9HE2S HVOuxrS4jUghnuGkDtTu9/dqFrAIY4Z4X4OixC9dSH0SuUE+uPFy+e1lkKRt+k9kCXmSO1 Oh28iUbmxqX17eVzQr5QgqzQSb6ywIeDuNS0JEqfliegqwMeXJLVgQHJt+G+tQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4SWQNV6Xhzz16cn; Thu, 16 Nov 2023 16:24:42 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 3AGGOgHV002468; Thu, 16 Nov 2023 16:24:42 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3AGGOgUE002465; Thu, 16 Nov 2023 16:24:42 GMT (envelope-from git) Date: Thu, 16 Nov 2023 16:24:42 GMT Message-Id: <202311161624.3AGGOgUE002465@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Gleb Smirnoff Subject: git: 070d9e3540e6 - main - socket tests: add socket_accept List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: glebius X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 070d9e3540e63540e09c915963c2fc0cc55d3b2a Auto-Submitted: auto-generated The branch main has been updated by glebius: URL: https://cgit.FreeBSD.org/src/commit/?id=070d9e3540e63540e09c915963c2fc0cc55d3b2a commit 070d9e3540e63540e09c915963c2fc0cc55d3b2a Author: Gleb Smirnoff AuthorDate: 2023-11-16 16:23:48 +0000 Commit: Gleb Smirnoff CommitDate: 2023-11-16 16:23:48 +0000 socket tests: add socket_accept Start with most basic functionality on a TCP socket. --- tests/sys/kern/Makefile | 1 + tests/sys/kern/socket_accept.c | 130 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) diff --git a/tests/sys/kern/Makefile b/tests/sys/kern/Makefile index e449d9deeed1..308cfd22a0ec 100644 --- a/tests/sys/kern/Makefile +++ b/tests/sys/kern/Makefile @@ -28,6 +28,7 @@ ATF_TESTS_C+= reaper ATF_TESTS_C+= sched_affinity ATF_TESTS_C+= sigaltstack ATF_TESTS_C+= sigwait +ATF_TESTS_C+= socket_accept ATF_TESTS_C+= socket_accf ATF_TESTS_C+= socket_msg_trunc ATF_TESTS_C+= socket_msg_waitall diff --git a/tests/sys/kern/socket_accept.c b/tests/sys/kern/socket_accept.c new file mode 100644 index 000000000000..278eb2fb9853 --- /dev/null +++ b/tests/sys/kern/socket_accept.c @@ -0,0 +1,130 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2023 Gleb Smirnoff + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include + +#include + +static int +tcp4_listensock(struct sockaddr_in *sin) +{ + int l; + + ATF_REQUIRE((l = socket(PF_INET, SOCK_STREAM, 0)) > 0); + ATF_REQUIRE(setsockopt(l, SOL_SOCKET, SO_REUSEADDR, &(socklen_t){1}, + sizeof(int)) == 0); + *sin = (struct sockaddr_in){ + .sin_len = sizeof(sin), + .sin_family = AF_INET, + .sin_addr.s_addr = htonl(INADDR_LOOPBACK), + }; + ATF_REQUIRE(bind(l, (struct sockaddr *)sin, sizeof(*sin)) == 0); + ATF_REQUIRE(getsockname(l, (struct sockaddr *)sin, + &(socklen_t){ sizeof(*sin) }) == 0); + ATF_REQUIRE(listen(l, -1) == 0); + + return (l); +} + +static int +tcp4_clientsock(struct sockaddr_in *sin) +{ + int s; + + ATF_REQUIRE((s = socket(PF_INET, SOCK_STREAM, 0)) > 0); + ATF_REQUIRE(connect(s, (struct sockaddr *)sin, sizeof(*sin)) == 0); + + return (s); +} + +ATF_TC_WITHOUT_HEAD(tcp4_zerolen); +ATF_TC_BODY(tcp4_zerolen, tc) +{ + static char canary[sizeof(struct sockaddr_in)] = + { [0 ... sizeof(struct sockaddr_in) - 1] = 0xa }; + struct sockaddr_in sin, ret; + socklen_t salen; + int l; + + l = tcp4_listensock(&sin); + (void )tcp4_clientsock(&sin); + + memcpy(&ret, &canary, sizeof(ret)); + salen = 0; + ATF_REQUIRE(accept(l, (struct sockaddr *)&ret, &salen) > 0); + ATF_REQUIRE(memcmp(&ret, &canary, sizeof(ret)) == 0); +#if 0 + /* Linux behavior. Matches my reading of accept(2) and POSIX. */ + ATF_REQUIRE(salen == sizeof(struct sockaddr_in)); +#endif + /* Note: Linux will block for connection here, we fail immediately. */ + ATF_REQUIRE(accept(l, (struct sockaddr *)&ret, NULL) == -1); + ATF_REQUIRE(errno == EFAULT); +} + +ATF_TC_WITHOUT_HEAD(tcp4); +ATF_TC_BODY(tcp4, tc) +{ + struct sockaddr_in sin, ret; + socklen_t salen; + int l, s; + + l = tcp4_listensock(&sin); + s = tcp4_clientsock(&sin); + + salen = sizeof(struct sockaddr_in) + 2; + ATF_REQUIRE(accept(l, (struct sockaddr *)&ret, &salen) > 0); + ATF_REQUIRE(salen == sizeof(struct sockaddr_in)); + ATF_REQUIRE(getsockname(s, (struct sockaddr *)&sin, + &(socklen_t){ sizeof(sin) }) == 0); + ATF_REQUIRE(memcmp(&ret, &sin, sizeof(sin)) == 0); +} + +ATF_TC_WITHOUT_HEAD(tcp4_noaddr); +ATF_TC_BODY(tcp4_noaddr, tc) +{ + struct sockaddr_in sin; + int l; + + l = tcp4_listensock(&sin); + (void )tcp4_clientsock(&sin); + + ATF_REQUIRE(accept(l, NULL, NULL) > 0); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, tcp4); + ATF_TP_ADD_TC(tp, tcp4_noaddr); + ATF_TP_ADD_TC(tp, tcp4_zerolen); + + return (atf_no_error()); +}