From nobody Thu May 12 18:03:03 2022 X-Original-To: dev-commits-src-main@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 11D521AC4BDE; Thu, 12 May 2022 18:03:04 +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 4KzflD00Jsz3k8Z; Thu, 12 May 2022 18:03:04 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1652378584; 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=fkjnUmynH3LUqO3fvk4XApgdrtB2nq63TlJ9t5qXZHg=; b=dNCWwQ+lxGKAwoK4EB5cm0dgsJZyy9faKni32K/AqZI/giOn+IAlWXZBHCbF2kKCFMZjhz PuwfKDVvI/dPCJTe7sm/ueb1c1FyfW91KZdOs7Hbs6iNh24kdiQDhRNBan0GGwFnDzxIhR VifpjeUQ3hIh4q4Hz0ROqyj6EWMGjFNwVm74oW09odp+HtcQCooWBF1IUQwpSjisl30ljz 8m7CrrvTLWHI0b0s+tdWJ1GMeuQ+OdmvQZGKUTxA8Fj9v+dWBcjB15eo6x+g9doRk7GeQP eonmnnGqafgUsQKR62YM5A8X24wYTONfjV68az2TgctYoUXc+O1UoWbVROg1JA== 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 D3CBC12E78; Thu, 12 May 2022 18:03:03 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 24CI33gl027970; Thu, 12 May 2022 18:03:03 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 24CI33S8027969; Thu, 12 May 2022 18:03:03 GMT (envelope-from git) Date: Thu, 12 May 2022 18:03:03 GMT Message-Id: <202205121803.24CI33S8027969@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: 6d317723608e - main - tests/kern: add tests for PF_UNIX/SOCK_DGRAM List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@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: 6d317723608e73ee2314da6f7b01f62340e802f4 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1652378584; 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=fkjnUmynH3LUqO3fvk4XApgdrtB2nq63TlJ9t5qXZHg=; b=IDcqgU8eyGCV9bLLMBJS2CvfORJ2CiM50EAtZSRc7+01MoyOq/CW303l2p6ywuHDcef/ae DhrTSPXW7qAEESrfzmLfLT/RZb6rONjjjWWtAEUpKeNP8iBY0jFcDIaNDduytO8JKoian6 ITlI5uZwwF4ut/1o6txsqkVNGycrU1K1oLUprcDDhvphxjRLG73HIKnp/Atx3COa08WvVr FFaBpPbJV/K5FmIhe/8u+/sAByaFPvPhxpCi3iMxFifC/n83SeMR9PtNL4T9fxX9+uEsRv 3fNtTCe7AXjTFGl9BRAz5cImtfgiAlsYOwywa1GnKrcIcSOLyEaj2wNnnsOcIQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1652378584; a=rsa-sha256; cv=none; b=RRL9a9JJtXFpNO1MLianH/w1SUarXYspEXPH+NYoJlZIjbhKwZFro0qJNpVI2BQUpPTAaW TzsViFGWRUxnUjzWejjBeDhmLVutMkTmRhFkKk3VTaihC5iiD5eY6KZ3I4VgQecbftsOIf ++E9+tfoVHbXV3S+J+pGDsDoZVs94crr43L2sUyMKfw4kNi3SdUfpSk8kzddvOAnSkR/DL 7c/5H1KnRZjRji02T5O7E03/jrs1RErKldCO6bLzhuS14Q1yfPTS+8UsnNgrJkeGPWI5BJ ag7lJD7h5EMgGj2C2FAKRFwpW3Ffho827fTPXIclK6yiGFe9jVLxzpIBVL2d6A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by glebius: URL: https://cgit.FreeBSD.org/src/commit/?id=6d317723608e73ee2314da6f7b01f62340e802f4 commit 6d317723608e73ee2314da6f7b01f62340e802f4 Author: Gleb Smirnoff AuthorDate: 2022-05-12 18:02:41 +0000 Commit: Gleb Smirnoff CommitDate: 2022-05-12 18:02:41 +0000 tests/kern: add tests for PF_UNIX/SOCK_DGRAM --- tests/sys/kern/Makefile | 1 + tests/sys/kern/unix_dgram.c | 217 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 218 insertions(+) diff --git a/tests/sys/kern/Makefile b/tests/sys/kern/Makefile index e2702d202389..3f0e40d6a0dd 100644 --- a/tests/sys/kern/Makefile +++ b/tests/sys/kern/Makefile @@ -35,6 +35,7 @@ ATF_TESTS_C+= subr_physmem_test PLAIN_TESTS_C+= subr_unit_test ATF_TESTS_C+= sysctl_kern_proc ATF_TESTS_C+= sys_getrandom +ATF_TESTS_C+= unix_dgram ATF_TESTS_C+= unix_passfd_test ATF_TESTS_C+= unix_seqpacket_test TEST_METADATA.unix_seqpacket_test+= timeout="15" diff --git a/tests/sys/kern/unix_dgram.c b/tests/sys/kern/unix_dgram.c new file mode 100644 index 000000000000..66a9ab6092ee --- /dev/null +++ b/tests/sys/kern/unix_dgram.c @@ -0,0 +1,217 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2022 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 +#include +#include + +#include + +static struct itimerval itv = { + .it_interval = { 0, 0 }, + .it_value = { 1, 0 }, /* one second */ +}; +static sig_atomic_t timer_done = 0; +static void +sigalarm(int sig __unused) +{ + + timer_done = 1; +} + +static struct sigaction sigact = { + .sa_handler = sigalarm, +}; + +/* + * Fill socket to a state when next send(len) would fail. + */ +static void +fill(int fd, void *buf, ssize_t len) +{ + unsigned long recvspace; + size_t llen = sizeof(unsigned long); + ssize_t sent; + + ATF_REQUIRE(sysctlbyname("net.local.dgram.recvspace", &recvspace, + &llen, NULL, 0) == 0); + for (sent = 0; sent + len < (ssize_t)recvspace; sent += len) + ATF_REQUIRE(send(fd, buf, len, 0) == len); +} + +ATF_TC_WITHOUT_HEAD(basic); +ATF_TC_BODY(basic, tc) +{ + struct msghdr msg; + struct iovec iov[1]; + unsigned long maxdgram; + size_t llen = sizeof(unsigned long); + int fd[2]; + char *buf; + + /* Allocate and initialize: + * - fd[0] to send, fd[1] to receive + * - buf[maxdgram] for data + */ + ATF_REQUIRE(sysctlbyname("net.local.dgram.maxdgram", &maxdgram, + &llen, NULL, 0) == 0); + ATF_REQUIRE(socketpair(PF_UNIX, SOCK_DGRAM, 0, fd) != -1); + buf = malloc(maxdgram + 1); + ATF_REQUIRE(buf); + msg = (struct msghdr ){ + .msg_iov = iov, + .msg_iovlen = 1, + }; + iov[0] = (struct iovec ){ + .iov_base = buf, + }; + + /* Fail to send > maxdgram. */ + ATF_REQUIRE(send(fd[0], buf, maxdgram + 1, 0) == -1); + ATF_REQUIRE(errno == EMSGSIZE); + + /* Send maxdgram. */ + ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == (ssize_t)maxdgram); + + /* Exercise MSG_PEEK, full and truncated.. */ + ATF_REQUIRE(recv(fd[1], buf, maxdgram, MSG_PEEK) == (ssize_t)maxdgram); + iov[0].iov_len = 42; + ATF_REQUIRE(recvmsg(fd[1], &msg, MSG_PEEK) == 42); + ATF_REQUIRE(msg.msg_flags == (MSG_PEEK | MSG_TRUNC)); + + /* Receive maxdgram. */ + iov[0].iov_len = maxdgram; + ATF_REQUIRE(recvmsg(fd[1], &msg, 0) == (ssize_t)maxdgram); + ATF_REQUIRE(msg.msg_flags == 0); + + /* Receive truncated message. */ + ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == (ssize_t)maxdgram); + iov[0].iov_len = maxdgram / 2; + ATF_REQUIRE(recvmsg(fd[1], &msg, 0) == (ssize_t)maxdgram / 2); + ATF_REQUIRE(msg.msg_flags == MSG_TRUNC); + + /* Empty: block. */ + ATF_REQUIRE(sigaction(SIGALRM, &sigact, NULL) == 0); + ATF_REQUIRE(timer_done == 0); + ATF_REQUIRE(setitimer(ITIMER_REAL, &itv, NULL) == 0); + ATF_REQUIRE(recv(fd[1], buf, maxdgram, 0) == -1); + ATF_REQUIRE(errno == EINTR); + ATF_REQUIRE(timer_done == 1); + + /* Don't block with MSG_DONTWAIT. */ + ATF_REQUIRE(recv(fd[1], buf, maxdgram, MSG_DONTWAIT) == -1); + ATF_REQUIRE(errno == EAGAIN); + + /* Don't block with O_NONBLOCK. */ + ATF_REQUIRE(fcntl(fd[1], F_SETFL, O_NONBLOCK) != -1); + ATF_REQUIRE(recv(fd[1], buf, maxdgram, 0) == -1); + ATF_REQUIRE(errno == EAGAIN); + + /* Fail with ENOBUFS on full socket. */ + fill(fd[0], buf, maxdgram); + ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == -1); + ATF_REQUIRE(errno == ENOBUFS); + + /* Fail with EAGAIN with O_NONBLOCK set. */ + ATF_REQUIRE(fcntl(fd[0], F_SETFL, O_NONBLOCK) != -1); + ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == -1); + ATF_REQUIRE(errno == EAGAIN); + + /* Remote side closed -> ECONNRESET. */ + close(fd[1]); + ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == -1); + ATF_REQUIRE(errno == ECONNRESET); +} + +ATF_TC_WITHOUT_HEAD(one2many); +ATF_TC_BODY(one2many, tc) +{ + struct sockaddr_un sun; + const char *path = "unix_dgram_listener"; + int one, many[2], two; + char buf[1024]; + + /* Establish one to many connection. */ + ATF_REQUIRE((one = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0); + bzero(&sun, sizeof(sun)); + sun.sun_family = AF_LOCAL; + sun.sun_len = sizeof(sun); + strlcpy(sun.sun_path, path, sizeof(sun.sun_path)); + ATF_REQUIRE(bind(one, (struct sockaddr *)&sun, sizeof(sun)) == 0); + /* listen(2) shall fail. */ + ATF_REQUIRE(listen(one, -1) != 0); + for (int i = 0; i < 2; i++) { + ATF_REQUIRE((many[i] = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0); + ATF_REQUIRE(connect(many[i], (struct sockaddr *)&sun, + sizeof(sun)) == 0); + } + + /* accept() on UNIX/DGRAM is invalid. */ + ATF_REQUIRE(accept(one, NULL, NULL) == -1); + ATF_REQUIRE(errno == EINVAL); + + /* + * Connecting a bound socket to self: a strange, useless, but + * historically existing edge case that is not explicitly described + * in SuS, neither is forbidden there. Works on FreeBSD and Linux. + */ + ATF_REQUIRE(connect(one, (struct sockaddr *)&sun, sizeof(sun)) == 0); + ATF_REQUIRE(send(one, buf, 42, 0) == 42); + ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == 42); + + /* + * Sending from an unconnected socket to a bound socket. Connection is + * created for the duration of the syscall. + */ + ATF_REQUIRE((two = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0); + ATF_REQUIRE(sendto(two, buf, 43, 0, (struct sockaddr *)&sun, + sizeof(sun)) == 43); + ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == 43); + + /* One sender can fill the receive side. + * Current behavior which needs improvement. + */ + fill(many[0], buf, sizeof(buf)); + ATF_REQUIRE(send(many[1], buf, sizeof(buf), 0) == -1); + ATF_REQUIRE(errno == ENOBUFS); + ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == sizeof(buf)); + ATF_REQUIRE(send(many[1], buf, sizeof(buf), 0) == sizeof(buf)); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, basic); + ATF_TP_ADD_TC(tp, one2many); + + return (atf_no_error()); +}