From nobody Tue Oct 17 13:37:09 2023 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 4S8w5219L0z4xWT5; Tue, 17 Oct 2023 13:37:10 +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 4S8w514Jq8z3G4t; Tue, 17 Oct 2023 13:37:09 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1697549829; 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=Fkv+G5kmL8LRADhorMuEUexOZHSu8du8Fss6tdDVqVU=; b=fnhTlHWABwBncsAENkmjySbZbgJsXOkloMQZvsptRGmcYO0IEajL3ndz1dDCn0JJNW9XVa TCadk46NkK2kVtD21YwZRf9jGrE1DNYlPuuL3AH7fu4ipxH6z18VACruZ26T+vM0nBC8ho qTW5jeDM+xrm4DF3az4IrTcV8GEwHob6G7jaM4T0pgTLVUDkQ8lGvdinB10+uVKswZdYre +OlnmxANPey3r336OeVTJyqJPLXb8zqJXek8WLAzP3srMe/WXrGNtyuG5UtFfgIABAt8JY gWfGLpHdvOTzY3MUVVvcMLqCgq6GM9syfRfKxPSjKxq4L0vM6F09T4hFcSnFiQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1697549829; a=rsa-sha256; cv=none; b=T4q6gVVz3lBjtasGffUitujzHHH49obb0f82XyyxpiCeTufNLyu5W8yhraAMiM0Dz56ubF xaPZxokqX7JmwThBuFXF2R8GpaUB/WAI8ZfUwduHIcoAlHWCNeVYY+IhtvjZfCZjiFKoyx CL5zJVuYqPtu3RLwHrmn5f4gZFWRyPxaW4Yq/90Xog03wOr7eSwEpJR33hrg9ctPaMb2ae O3ta9P1VOFVxhgIcLEUYCeYysobhdZOVHqd3fHRMUOyu7cIrlW2OqWGl4Y/k0o/wQjV3Is XETSJzG2wVf2k8Sdxk7Hc8KEH5m7cdNIW6IznUW6HNS+gbdii325a6EElqDwtw== 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=1697549829; 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=Fkv+G5kmL8LRADhorMuEUexOZHSu8du8Fss6tdDVqVU=; b=GsWQQsdEgo3TZe1xKqzYUqfGASY4V+9V4WD5UoZrWX0LcCShy1qB/W0cPZFVXy7cNdjeNk qIssy20UuH36b38lLhv6OJPtrb+vWiuLD9g4MIgCgH+ttkl6jLv4xChwPTaMiKf+qndOeR KOe6fzs7DNku2Ee8BnWoA2fsea6lO95UcZBhfBV0dWYhEFyBquaTSJBFticuOdyzPfwT1L GJmyYtaxPrthkbFY5N4zTYAqmnrcnxkFKVPrb8HTVzFUkw3V7wCBe7ASp55DMutgwLTJNi VSDSdbIHt7+ILHLqsYaPRjftYE3jkZrmkMV6CBykT/aY5MH9LqJyRioGEKyk8A== 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 4S8w513QmSzB7W; Tue, 17 Oct 2023 13:37:09 +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 39HDb90B095881; Tue, 17 Oct 2023 13:37:09 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 39HDb9cB095879; Tue, 17 Oct 2023 13:37:09 GMT (envelope-from git) Date: Tue, 17 Oct 2023 13:37:09 GMT Message-Id: <202310171337.39HDb9cB095879@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: b5e7dbac756a - main - socket tests: Clean up the MSG_TRUNC regression tests a bit 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: markj X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: b5e7dbac756afb49c58315c7081737b34a1d2dfd Auto-Submitted: auto-generated The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=b5e7dbac756afb49c58315c7081737b34a1d2dfd commit b5e7dbac756afb49c58315c7081737b34a1d2dfd Author: Mark Johnston AuthorDate: 2023-10-16 21:35:07 +0000 Commit: Mark Johnston CommitDate: 2023-10-17 13:12:36 +0000 socket tests: Clean up the MSG_TRUNC regression tests a bit - Fix style. - Move test case-specific code out of the shared function and into the individual test cases. - Remove unneeded setting of SO_REUSEPORT. - Avoid unnecessary copying. - Use ATF_REQUIRE* instead of ATF_CHECK*. The former cause test execution to stop after a failed assertion, which is what we want. - Add a test case for AF_LOCAL/SOCK_SEQPACKET sockets. MFC after: 1 week --- tests/sys/kern/socket_msg_trunc.c | 230 +++++++++++++++++++++----------------- 1 file changed, 128 insertions(+), 102 deletions(-) diff --git a/tests/sys/kern/socket_msg_trunc.c b/tests/sys/kern/socket_msg_trunc.c index a863705adbae..469b9e1503cd 100644 --- a/tests/sys/kern/socket_msg_trunc.c +++ b/tests/sys/kern/socket_msg_trunc.c @@ -25,143 +25,169 @@ * SUCH DAMAGE. */ -#include -#include -#include +#include #include #include #include + #include + #include +#include +#include #include static void -check_recvmsg(const char *test_name) +check_recvmsg(int cs, int ss, struct sockaddr *sa, const size_t sizes[], + size_t nsizes) { - int ss, cs, rc; - struct sockaddr *sa; - struct sockaddr_in sin; - struct sockaddr_in6 sin6; - struct sockaddr_un saun; - int *sizes, sizes_count; - int one = 1; - - - if (!strcmp(test_name, "udp")) { - ss = socket(PF_INET, SOCK_DGRAM, 0); - ATF_CHECK(ss >= 0); - rc = setsockopt(ss, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)); - ATF_CHECK_EQ(0, rc); - bzero(&sin, sizeof(sin)); - sin.sin_family = AF_INET; - sin.sin_len = sizeof(sin); - sin.sin_port = htons(6666); - sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); - sa = (struct sockaddr *)&sin; - rc = bind(ss, sa, sa->sa_len); - ATF_CHECK_EQ(0, rc); - - cs = socket(PF_INET, SOCK_DGRAM, 0); - ATF_CHECK(cs >= 0); - int inet_sizes[] = {80, 255, 256, 1024, 4096, 9000}; - sizes_count = sizeof(inet_sizes) / sizeof(int); - sizes = malloc(sizeof(inet_sizes)); - memcpy(sizes, inet_sizes, sizeof(inet_sizes)); - - } else if (!strcmp(test_name, "udp6")) { - ss = socket(PF_INET6, SOCK_DGRAM, 0); - ATF_CHECK(ss >= 0); - rc = setsockopt(ss, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)); - ATF_CHECK_EQ(0, rc); - bzero(&sin6, sizeof(sin6)); - sin6.sin6_family = AF_INET6; - sin6.sin6_len = sizeof(sin6); - sin6.sin6_port = htons(6666); - const struct in6_addr in6loopback = IN6ADDR_LOOPBACK_INIT; - sin6.sin6_addr = in6loopback; - sa = (struct sockaddr *)&sin6; - rc = bind(ss, sa, sa->sa_len); - ATF_CHECK_EQ(0, rc); - - cs = socket(PF_INET6, SOCK_DGRAM, 0); - ATF_CHECK(cs >= 0); - int inet_sizes[] = {80, 255, 256, 1024, 4096, 9000}; - sizes_count = sizeof(inet_sizes) / sizeof(int); - sizes = malloc(sizeof(inet_sizes)); - memcpy(sizes, inet_sizes, sizeof(inet_sizes)); - - } else if (!strcmp(test_name, "unix")) { - const char *PATH = "/tmp/test_check_recvmsg_socket"; - ss = socket(PF_UNIX, SOCK_DGRAM, 0); - ATF_CHECK(ss >= 0); - rc = setsockopt(ss, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)); - ATF_CHECK_EQ(0, rc); - bzero(&saun, sizeof(saun)); - saun.sun_family = AF_UNIX; - strcpy(saun.sun_path, PATH); - saun.sun_len = sizeof(saun); - sa = (struct sockaddr *)&saun; - unlink(PATH); - rc = bind(ss, sa, sa->sa_len); - ATF_CHECK_EQ(0, rc); - - cs = socket(PF_UNIX, SOCK_DGRAM, 0); - ATF_CHECK(cs >= 0); - int unix_sizes[] = {80, 255, 256, 1024, 2000}; - sizes_count = sizeof(unix_sizes) / sizeof(int); - sizes = malloc(sizeof(unix_sizes)); - memcpy(sizes, unix_sizes, sizeof(unix_sizes)); - } else - return; - char buf[4096]; + memset(buf, 0xFF, sizeof(buf)); - for (int i = 0; i < sizes_count; i++) { - int sz = sizes[i]; + for (size_t i = 0; i < nsizes; i++) { + ssize_t rc; + size_t sz = sizes[i]; char tbuf[1]; + rc = sendto(cs, buf, sz, 0, sa, sa->sa_len); - ATF_REQUIRE_EQ(rc, sz); + ATF_REQUIRE_MSG(rc != -1, "sendto failed: %s", strerror(errno)); + ATF_REQUIRE((size_t)rc == sz); rc = recv(ss, NULL, 0, MSG_PEEK | MSG_TRUNC); - ATF_CHECK_EQ(rc, sz); + ATF_REQUIRE_MSG(rc >= 0, "recv failed: %s", strerror(errno)); + ATF_REQUIRE((size_t)rc == sz); rc = recv(ss, tbuf, sizeof(tbuf), MSG_PEEK | MSG_TRUNC); - ATF_CHECK_EQ(rc, sz); + ATF_REQUIRE_MSG(rc >= 0, "recv failed: %s", strerror(errno)); + ATF_REQUIRE((size_t)rc == sz); rc = recv(ss, tbuf, sizeof(tbuf), MSG_TRUNC); - ATF_CHECK_EQ(rc, sz); + ATF_REQUIRE_MSG(rc >= 0, "recv failed: %s", strerror(errno)); + ATF_REQUIRE((size_t)rc == sz); } - close(ss); - close(cs); + ATF_REQUIRE(close(cs) == 0); + ATF_REQUIRE(close(ss) == 0); } -ATF_TC_WITHOUT_HEAD(socket_afinet_udp_recv_trunc); -ATF_TC_BODY(socket_afinet_udp_recv_trunc, tc) +ATF_TC_WITHOUT_HEAD(recv_trunc_afinet_udp); +ATF_TC_BODY(recv_trunc_afinet_udp, tc) { - check_recvmsg("udp"); + struct sockaddr_in sin; + struct sockaddr *sa; + int ss, cs, rc; + + ss = socket(PF_INET, SOCK_DGRAM, 0); + ATF_REQUIRE(ss >= 0); + + memset(&sin, 0, sizeof(sin)); + sin.sin_family = AF_INET; + sin.sin_len = sizeof(sin); + sin.sin_port = htons(6666); + sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + sa = (struct sockaddr *)&sin; + rc = bind(ss, sa, sa->sa_len); + ATF_REQUIRE_MSG(rc == 0, "bind failed: %s", strerror(errno)); + + cs = socket(PF_INET, SOCK_DGRAM, 0); + ATF_REQUIRE(cs >= 0); + + size_t sizes[] = {80, 255, 256, 1024, 4096, 9000}; + check_recvmsg(cs, ss, sa, sizes, nitems(sizes)); } -ATF_TC_WITHOUT_HEAD(socket_afinet6_udp_recv_trunc); -ATF_TC_BODY(socket_afinet6_udp_recv_trunc, tc) +ATF_TC_WITHOUT_HEAD(recv_trunc_afinet6_udp); +ATF_TC_BODY(recv_trunc_afinet6_udp, tc) { - check_recvmsg("udp6"); + struct sockaddr_in6 sin6; + struct sockaddr *sa; + int cs, ss, rc; + + ss = socket(PF_INET6, SOCK_DGRAM, 0); + ATF_REQUIRE(ss >= 0); + + memset(&sin6, 0, sizeof(sin6)); + sin6.sin6_family = AF_INET6; + sin6.sin6_len = sizeof(sin6); + sin6.sin6_port = htons(6666); + const struct in6_addr in6loopback = IN6ADDR_LOOPBACK_INIT; + sin6.sin6_addr = in6loopback; + sa = (struct sockaddr *)&sin6; + rc = bind(ss, sa, sa->sa_len); + ATF_REQUIRE_MSG(rc == 0, "bind failed: %s", strerror(errno)); + + cs = socket(PF_INET6, SOCK_DGRAM, 0); + ATF_REQUIRE(cs >= 0); + + size_t sizes[] = {80, 255, 256, 1024, 4096, 9000}; + check_recvmsg(cs, ss, sa, sizes, nitems(sizes)); } -ATF_TC_WITHOUT_HEAD(socket_afunix_recv_trunc); -ATF_TC_BODY(socket_afunix_recv_trunc, tc) +ATF_TC_WITHOUT_HEAD(recv_trunc_afunix_dgram); +ATF_TC_BODY(recv_trunc_afunix_dgram, tc) { - check_recvmsg("unix"); + struct sockaddr_un sun; + struct sockaddr *sa; + int ss, cs, rc; + + ss = socket(PF_UNIX, SOCK_DGRAM, 0); + ATF_REQUIRE(ss >= 0); + + bzero(&sun, sizeof(sun)); + sun.sun_family = AF_UNIX; + strlcpy(sun.sun_path, "test_check_recvmsg_socket", sizeof(sun.sun_path)); + sun.sun_len = sizeof(sun); + sa = (struct sockaddr *)&sun; + rc = bind(ss, sa, sa->sa_len); + ATF_REQUIRE_MSG(rc == 0, "bind failed: %s", strerror(errno)); + + cs = socket(PF_UNIX, SOCK_DGRAM, 0); + ATF_REQUIRE(cs >= 0); + + size_t sizes[] = {80, 255, 256, 1024, 2000}; + check_recvmsg(cs, ss, sa, sizes, nitems(sizes)); } +ATF_TC_WITHOUT_HEAD(recv_trunc_afunix_seqpacket); +ATF_TC_BODY(recv_trunc_afunix_seqpacket, tc) +{ + struct sockaddr_un sun; + struct sockaddr *sa; + int ss, nss, cs, rc; + + ss = socket(PF_UNIX, SOCK_SEQPACKET, 0); + ATF_REQUIRE(ss >= 0); + + bzero(&sun, sizeof(sun)); + sun.sun_family = AF_UNIX; + strlcpy(sun.sun_path, "test_check_recvmsg_socket", sizeof(sun.sun_path)); + sun.sun_len = sizeof(sun); + sa = (struct sockaddr *)&sun; + rc = bind(ss, sa, sa->sa_len); + ATF_REQUIRE_MSG(rc == 0, "bind failed: %s", strerror(errno)); + rc = listen(ss, 1); + ATF_REQUIRE_MSG(rc == 0, "listen failed: %s", strerror(errno)); + + cs = socket(PF_UNIX, SOCK_SEQPACKET, 0); + ATF_REQUIRE(cs >= 0); + rc = connect(cs, sa, sa->sa_len); + ATF_REQUIRE_MSG(rc == 0, "connect failed: %s", strerror(errno)); + nss = accept(ss, NULL, NULL); + ATF_REQUIRE(nss >= 0); + + size_t sizes[] = {80, 255, 256, 1024, 2000}; + check_recvmsg(cs, nss, sa, sizes, nitems(sizes)); + + ATF_REQUIRE(close(ss) == 0); +} ATF_TP_ADD_TCS(tp) { + ATF_TP_ADD_TC(tp, recv_trunc_afinet_udp); + ATF_TP_ADD_TC(tp, recv_trunc_afinet6_udp); + ATF_TP_ADD_TC(tp, recv_trunc_afunix_dgram); + ATF_TP_ADD_TC(tp, recv_trunc_afunix_seqpacket); - ATF_TP_ADD_TC(tp, socket_afinet_udp_recv_trunc); - ATF_TP_ADD_TC(tp, socket_afinet6_udp_recv_trunc); - ATF_TP_ADD_TC(tp, socket_afunix_recv_trunc); - - return atf_no_error(); + return (atf_no_error()); }