From nobody Sat Mar 23 05:45:39 2024 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 4V1p8400d1z5DYvS; Sat, 23 Mar 2024 05:45:40 +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 4V1p836X93z4CVr; Sat, 23 Mar 2024 05:45:39 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1711172739; 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=CIS6DsDEfQqRbeW06maMf1CKqcYdeEra4uE8Ixo6cPs=; b=hkIJ7FGABsPvE4uL7xcB+IvVtAog1WMIho9u8puPBgCByDtGPMQ7jVzjBctomh4qH6Aneo S7vQkR9OQHZV/7lj5bA8RpMsajKeFoyz4M2ytuWiD+xUlwVwej+kE4Nr/BbY9UPRN1ceKR dBEerVOfG3sDIRo+Z8Qa5b3CEye4E+NJYwryaF94IkSHHZ7mrizv10VXgi3ZVE8nZSx6vN Xyp9eHGrvJ5QHMLRkpIwuD9L9B8rWIaKRrrDyRDMSiOo8w7KTgeun42xz662jH2D6zFHQd wCmiccXwbqoUFZfHxj+gzM5iUyD1+TDKHm2U+5CUZBCAFIuHfQud9XJ8uf2jMA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1711172739; a=rsa-sha256; cv=none; b=YJOEG4pUZJaNpdpC3usGfWISkK2U4Q3OKmEjCSLu2xZwIIBNWBs6ihYzYb7biCG+KAVqgI 1nH6HZEZHCeid7lNz9Xoqbeb3Rv8K8e5JI8Rs8O5Uuh2oUj5IDcY4WCy6Aow1TtluCIKT+ 29jMzcfOcFPJb2O2qtDXX9clWhdMAmNcH0Ed2WLy/eSgLnadHi9Td/4KSx2OuHJYRe41wh gHo5FFhoQM4Y+3sDTUGd8VyOKbdVzGOO8ZxkoZH0Kyi/BXY2VLGdjyuO4d1MLVLiOenOnB n3cyQClzfV+FUKZfCOZBZyAO1hBDvSD7zWxH+WaGsCpzOhry+rzL/BYeNOS5WA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1711172739; 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=CIS6DsDEfQqRbeW06maMf1CKqcYdeEra4uE8Ixo6cPs=; b=yTIwK+Uo+392CGahkTpPG6hFUWTr1xMicfPJOuUQhm4JUzah9fppXN3hdoqM6fq5n9qDtU QPNq2yd3c2OMZSN382NyrPyk9BVzzVItMgxUPnlAHUyOT/fm9obuDJypIrXX4PeYJbD72L pTcMwBeAP9SIjT4/CDJxSKeGjQkkmdZb58U9f1u0vG75SrR4sl/9xlhai8e1AuAb5wU3ek To/9RSWi3+oTUYkg28hZb5viPpgYdSEFyqOUrZhhhU2HHXJNNEto2lpHc4A9cOnIttR8ck FZ/mUUefTDXR3bC7WrNMZTly8cj13zntcRDTImA2w1Xf3fy/Wxcm3/f5mWZCBw== 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 4V1p835rNyzcGb; Sat, 23 Mar 2024 05:45:39 +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 42N5jdwS046816; Sat, 23 Mar 2024 05:45:39 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 42N5jdfw046813; Sat, 23 Mar 2024 05:45:39 GMT (envelope-from git) Date: Sat, 23 Mar 2024 05:45:39 GMT Message-Id: <202403230545.42N5jdfw046813@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: 8d3d9ca8bd3d - main - tests/netinet: add UDP socket I/O tests 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: 8d3d9ca8bd3db284f6ae671f1e816ba2822f0988 Auto-Submitted: auto-generated The branch main has been updated by glebius: URL: https://cgit.FreeBSD.org/src/commit/?id=8d3d9ca8bd3db284f6ae671f1e816ba2822f0988 commit 8d3d9ca8bd3db284f6ae671f1e816ba2822f0988 Author: Gleb Smirnoff AuthorDate: 2024-03-23 05:44:16 +0000 Commit: Gleb Smirnoff CommitDate: 2024-03-23 05:44:16 +0000 tests/netinet: add UDP socket I/O tests Start a file that would collect tests for I/O functionality of a UDP socket, targeted on how a socket interacts with userland rather than with wire side of the protocol. First version tests that MSG_TRUNC and MSG_PEEK are working correctly. --- tests/sys/netinet/Makefile | 3 +- tests/sys/netinet/udp_io.c | 139 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 1 deletion(-) diff --git a/tests/sys/netinet/Makefile b/tests/sys/netinet/Makefile index b86457c2d723..fb3281028aff 100644 --- a/tests/sys/netinet/Makefile +++ b/tests/sys/netinet/Makefile @@ -12,7 +12,8 @@ ATF_TESTS_C= ip_reass_test \ socket_afinet \ tcp_connect_port_test \ tcp_implied_connect \ - tcp_md5_getsockopt + tcp_md5_getsockopt \ + udp_io ATF_TESTS_SH= arp \ carp \ diff --git a/tests/sys/netinet/udp_io.c b/tests/sys/netinet/udp_io.c new file mode 100644 index 000000000000..6e0c4d9fe6d0 --- /dev/null +++ b/tests/sys/netinet/udp_io.c @@ -0,0 +1,139 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2024 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 + +/* + * Create a pair of UDP sockets. The first one is bound to a local + * address and the second one is connected to it. + */ +static void +udp_socketpair(int *s) +{ + struct sockaddr_in sin = { + .sin_family = AF_INET, + .sin_len = sizeof(sin), + }; + socklen_t slen = sizeof(sin); + int b, c; + + ATF_REQUIRE((b = socket(PF_INET, SOCK_DGRAM, 0)) > 0); + ATF_REQUIRE((c = socket(PF_INET, SOCK_DGRAM, 0)) > 0); + ATF_REQUIRE(bind(b, (struct sockaddr *)&sin, sizeof(sin)) == 0); + ATF_REQUIRE(getsockname(b, (struct sockaddr *)&sin, &slen) == 0); + ATF_REQUIRE(connect(c, (struct sockaddr *)&sin, sizeof(sin)) == 0); + + s[0] = b; + s[1] = c; +} + +/* + * Check MSG_TRUNC. + */ +ATF_TC_WITHOUT_HEAD(trunc); +ATF_TC_BODY(trunc, tc) +{ + char sbuf[] = "Hello, peer!", rbuf[sizeof(sbuf)]; + struct iovec iov = { + .iov_base = sbuf, + .iov_len = sizeof(sbuf), + }; + struct msghdr msg = { + .msg_iov = &iov, + .msg_iovlen = 1, + }; + int s[2]; + u_int n; + + udp_socketpair(s); + + ATF_REQUIRE(sendmsg(s[1], &msg, 0) == sizeof(sbuf)); + n = (arc4random() % (sizeof(sbuf) - 1)) + 1; + iov.iov_base = rbuf; + iov.iov_len = n; + ATF_REQUIRE(recvmsg(s[0], &msg, 0) == n); + ATF_REQUIRE(msg.msg_flags == MSG_TRUNC); + ATF_REQUIRE(strncmp(sbuf, rbuf, n) == 0); + iov.iov_len = sizeof(rbuf); + ATF_REQUIRE(recvmsg(s[0], &msg, MSG_DONTWAIT) == -1); + ATF_REQUIRE(errno == EAGAIN); + + close(s[0]); + close(s[1]); +} + +/* + * Check MSG_PEEK. + */ +ATF_TC_WITHOUT_HEAD(peek); +ATF_TC_BODY(peek, tc) +{ + char sbuf[] = "Hello, peer!", rbuf[sizeof(sbuf)]; + struct iovec iov = { + .iov_base = sbuf, + .iov_len = sizeof(sbuf), + }; + struct msghdr msg = { + .msg_iov = &iov, + .msg_iovlen = 1, + }; + int s[2]; + u_int n; + + udp_socketpair(s); + + ATF_REQUIRE(sendmsg(s[1], &msg, 0) == sizeof(sbuf)); + iov.iov_base = rbuf; + for (int i = 0; i < 10; i++) { + n = (arc4random() % sizeof(sbuf)) + 1; + iov.iov_len = n; + ATF_REQUIRE(recvmsg(s[0], &msg, MSG_PEEK) == n); + if (n < sizeof(sbuf)) + ATF_REQUIRE(msg.msg_flags == (MSG_PEEK | MSG_TRUNC)); + else + ATF_REQUIRE(msg.msg_flags == MSG_PEEK); + ATF_REQUIRE(strncmp(sbuf, rbuf, n) == 0); + } + + close(s[0]); + close(s[1]); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, trunc); + ATF_TP_ADD_TC(tp, peek); + + return (atf_no_error()); +}