From nobody Fri Jun 03 19:56:29 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 E21701BD672A; Fri, 3 Jun 2022 19:56:31 +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 4LFDCz3p5Jz4r5X; Fri, 3 Jun 2022 19:56:31 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1654286191; 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=OYzDRQgR1zNp/rsHYN+8w+BUsixBWw47JSFtLVKK+RI=; b=REMUMpTaYjk2UcXZ7Xr83VOE2+3oEn4oofI4bHSjblzyNmYi/zXfrJXSkHh5MrLS4sc7z3 R84ri/xzJ0M9CrG1N5fLvEHkAt168qW3OSVQ4DujvACrBl/fYy0L9iMt3eat3Mwc8nVYLJ NIUy39MWADFyyVKGXJN6y97vQgVYj7CI53aGSUMzkyViUf+lOnrjRbppB1FxkYq7zICHwV 47aEToFZWRajChA1jU5akjOcfqA7+O3CnEbDEHYRJi2yU33KU9kLYn9xSTEifgymlPrDo6 FpxslF6j+mDPu+RzPUxYgoxTsMQKoukEEaBB1uIVre8u/1SnjV4lnGZxDTWFxA== 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 0A47E7C0C; Fri, 3 Jun 2022 19:56:30 +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 253JuT6g080663; Fri, 3 Jun 2022 19:56:29 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 253JuTSu080662; Fri, 3 Jun 2022 19:56:29 GMT (envelope-from git) Date: Fri, 3 Jun 2022 19:56:29 GMT Message-Id: <202206031956.253JuTSu080662@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: 70d07b20897b - main - tests/unix_dgram: add test for event dispatchers 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: 70d07b20897be6febcf6d2fcbe95c2f34eae48b0 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1654286191; 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=OYzDRQgR1zNp/rsHYN+8w+BUsixBWw47JSFtLVKK+RI=; b=ILOdOlEAkUE/VMuMzfr70wVTUZJ8qILaWYsHXoW1oICac7DIk8DAkzg2J2qtj8FNoIhcFa rLWIHCdaRehgO6MxSmknxtuUhYosSzkJyGr7+HGXlvJG/bqfTnOJSIged7vC00sO4/iXPl oUr/2HKvEtAQ1d1rPBJCflcqTmxTNV4GyR2KyN5UBwvAzfiKYJzgVWu+85tDARDoBu8u1e HObsfT2yodT62k+AHFU8OAu1wBU4Zl9I3Q7qVteZmofH3Bg76iTSrO61nm8+Ronxa/1Me8 A5875ln0SsoQsbfl9f14lJcEN3o39tb4urjcMwfQrvWRzEg1DUep03EVtaV71w== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1654286191; a=rsa-sha256; cv=none; b=ZXjTKDm8rOSDcr5G2Z0IvrsG8TixXxTuIAgzbTbkzswwrF1Nty+81vt4za/MXBASE8b+Vh QUkK7wP4grgCVjp8OnREiy+6M4NZxcfsn8e/o/lUM3w3D7NQnlGAmxJrDVjYbfAMaBK+Np a/ie3cSQyLvc5DYBpqzja3wiWB9nS1bF+AgnXKkylwgwRkDKgc0kfYv38HD0tHcGjYe033 HeAn2pX7yjiwxf3JdEvfjMlDM3zT7D/ONx8BQ4iR6IFetC787jCNw5Eomq6YzLFLrlLY19 jV6Bm0hEDidrcCEYsKWa6ovmAu+TJnfFR+3PuosSBDnctncVc/ux3vwhFweAoA== 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=70d07b20897be6febcf6d2fcbe95c2f34eae48b0 commit 70d07b20897be6febcf6d2fcbe95c2f34eae48b0 Author: Gleb Smirnoff AuthorDate: 2022-06-03 19:55:44 +0000 Commit: Gleb Smirnoff CommitDate: 2022-06-03 19:55:44 +0000 tests/unix_dgram: add test for event dispatchers Put some data into a socket and check that: o select(2) sees readable data o kevent(2) sees data, and correctly sees data size o ioctl(FIONREAD) sees correct size o aio(4) successfully reads the data Repeat the test twice for a connected socket and not-connected. With future implementation these two cases would exercise different code. --- tests/sys/kern/unix_dgram.c | 104 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 98 insertions(+), 6 deletions(-) diff --git a/tests/sys/kern/unix_dgram.c b/tests/sys/kern/unix_dgram.c index 4bcb84587f46..2279eed7b604 100644 --- a/tests/sys/kern/unix_dgram.c +++ b/tests/sys/kern/unix_dgram.c @@ -26,9 +26,13 @@ */ #include +#include +#include +#include #include #include #include +#include #include #include #include @@ -52,6 +56,12 @@ static struct sigaction sigact = { .sa_handler = sigalarm, }; +static struct sockaddr_un sun = { + .sun_family = AF_LOCAL, + .sun_len = sizeof(sun), + .sun_path = "unix_dgram_listener", +}; + /* * Fill socket to a state when next send(len) would fail. * @@ -160,17 +170,11 @@ ATF_TC_BODY(basic, tc) 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); @@ -212,11 +216,99 @@ ATF_TC_BODY(one2many, tc) ATF_REQUIRE(send(many[1], buf, sizeof(buf), 0) == sizeof(buf)); } +/* + * Check that various mechanism report socket as readable and having + * 42 bytes of data. + */ +static void +test42(int fd) +{ + + /* ioctl(FIONREAD) */ + int data; + + ATF_REQUIRE(ioctl(fd, FIONREAD, &data) != -1); + ATF_REQUIRE(data == 42); + + /* select(2) */ + fd_set rfds; + + FD_ZERO(&rfds); + FD_SET(fd, &rfds); + ATF_REQUIRE(select(fd + 1, &rfds, NULL, NULL, NULL) == 1); + ATF_REQUIRE(FD_ISSET(fd, &rfds)); + + /* kevent(2) */ + struct kevent ev; + int kq; + + ATF_REQUIRE((kq = kqueue()) != -1); + EV_SET(&ev, fd, EVFILT_READ, EV_ADD, NOTE_LOWAT, 41, NULL); + ATF_REQUIRE(kevent(kq, &ev, 1, NULL, 0, NULL) == 0); + ATF_REQUIRE(kevent(kq, NULL, 0, &ev, 1, NULL) == 1); + ATF_REQUIRE(ev.filter == EVFILT_READ); + ATF_REQUIRE(ev.data == 42); + + /* aio(4) */ + char buf[50]; + struct aiocb aio = { + .aio_nbytes = 50, + .aio_fildes = fd, + .aio_buf = buf, + }, *aiop; + + ATF_REQUIRE(aio_read(&aio) == 0); + ATF_REQUIRE(aio_waitcomplete(&aiop, NULL) == 42); + ATF_REQUIRE(aiop == &aio); +} + +/* + * Send data and control in connected & unconnected mode and check that + * various event mechanisms see the data, but don't count control bytes. + */ +ATF_TC_WITHOUT_HEAD(event); +ATF_TC_BODY(event, tc) +{ + int fd[2]; + char buf[50]; + struct iovec iov = { + .iov_base = buf, + .iov_len = 42, + }; + struct cmsghdr cmsg = { + .cmsg_len = CMSG_LEN(0), + .cmsg_level = SOL_SOCKET, + .cmsg_type = SCM_TIMESTAMP, + }; + struct msghdr msghdr = { + .msg_iov = &iov, + .msg_iovlen = 1, + .msg_control = &cmsg, + .msg_controllen = CMSG_LEN(0), + }; + + /* Connected socket */ + ATF_REQUIRE(socketpair(PF_UNIX, SOCK_DGRAM, 0, fd) != -1); + ATF_REQUIRE(sendmsg(fd[0], &msghdr, 0) == 42); + test42(fd[1]); + close(fd[0]); + close(fd[1]); + + /* Not-connected send */ + ATF_REQUIRE((fd[0] = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0); + ATF_REQUIRE((fd[1] = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0); + ATF_REQUIRE(bind(fd[0], (struct sockaddr *)&sun, sizeof(sun)) == 0); + ATF_REQUIRE(sendto(fd[1], buf, 42, 0, (struct sockaddr *)&sun, + sizeof(sun)) == 42); + test42(fd[0]); +} + ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, basic); ATF_TP_ADD_TC(tp, one2many); + ATF_TP_ADD_TC(tp, event); return (atf_no_error()); }