From nobody Sat Mar 23 05:45:40 2024 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 4V1p850kkGz5DZ4W; Sat, 23 Mar 2024 05:45:41 +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 4V1p846mfFz4Cgp; Sat, 23 Mar 2024 05:45:40 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1711172740; 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=zOrW2tVc09bwZhcM58KWmIZhUbt07A5rgqKF+2qe6Nw=; b=PJ4A7xoLxUMwEa3cT7fbr8P1YPaZtLmnQ32YjzDf7v41qDB/DKfdTf3C7XJ5WU5TbKx525 8uOoywI+kDZMx1RwgIJjXg7HyIWM3C8pYmEIOtdZ1cEDyTqgpsZLs4CiG3JXn+eivorcIb rC/ekKcaoi1558qu+uxRB8jOvPbrCK6tmsTMPlDgyf4yACE2PQTXXDUCZOIPJIAkKfSyFr sv3rnwd4vV3o9QHQenOZ18DO6WxYb39SMFRtBH8c+o+AW4+pzoGpwGHnVTwuPKwhnV8C88 GP72l8bAT3vyFecHsrej5wp2+z/QmGXW/oZtkgd/1mUe5DgAfwO92n578+HssA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1711172740; a=rsa-sha256; cv=none; b=yS2weF4gF9W4r+3YvtYxo+QXc+5PbZOUU1Fo29qG19n4EMI773jR5qJOpbkTtlZg5Q65nY L/am4mZqwkVKYdeJFXFLNV5NAQxBKlGOXXHOeg6YrYztczFAYCLw6jYxnZSOVwpli7KCpy 6DZIQbKtjd/IRyIG+4gJYSjDzS4CBxH4KVm26UEVvbxInXY0WF3lCJb9aNPdCIzsRHBo72 c9C/LMgynzEKl+2A2PuaPoDPoRGz2fAS64Hwiuw7qszfBfzC1FacnPoQIdVIBBIwlSt+bg PvYUzYXbXECg2dQqPyL1PByxXpTDZ/kHV69OxwMXO7AQiRjQgjyFn8OoF3q1bg== 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=1711172740; 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=zOrW2tVc09bwZhcM58KWmIZhUbt07A5rgqKF+2qe6Nw=; b=J9QT4yF2088xiZiChBrFClSJS07vNDu7ox4W21mh79dMabZM7eYnrMVfl6CHuReCzHjdW5 ddJjvoNFKwNwwI+f0exx2IKwwzwzispBvKIAJ31/faXQv/qkcvo435xgplZ1NPu7DEfC7C 7gr83WMBc/m9eRzW0tdz7ZghTWCJHvknAb7CulSqduOKuIAcWOH+FHfkU4IFqU0AW9LpCT GN1Z8uv6j5wi+1haQ8+zVdOxkMyOlLlmpQyi3H/oXS0e2kuDGM+YQ67gSqO3t0zGRQvHow InN7z+6e4acgr37ZZulI2pZQAEmS/QisFRej3Gj5hOrtwF1f/RBGoa2FR0xJ/A== 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 4V1p846N6Dzc0r; Sat, 23 Mar 2024 05:45:40 +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 42N5je31046860; Sat, 23 Mar 2024 05:45:40 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 42N5jeGE046857; Sat, 23 Mar 2024 05:45:40 GMT (envelope-from git) Date: Sat, 23 Mar 2024 05:45:40 GMT Message-Id: <202403230545.42N5jeGE046857@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: a8edc9f1e316 - main - tests/netgraph: start ng_ksocket(4) tests 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: a8edc9f1e316e4893b0b6aac282c542102cf3b99 Auto-Submitted: auto-generated The branch main has been updated by glebius: URL: https://cgit.FreeBSD.org/src/commit/?id=a8edc9f1e316e4893b0b6aac282c542102cf3b99 commit a8edc9f1e316e4893b0b6aac282c542102cf3b99 Author: Gleb Smirnoff AuthorDate: 2024-03-23 05:44:16 +0000 Commit: Gleb Smirnoff CommitDate: 2024-03-23 05:44:16 +0000 tests/netgraph: start ng_ksocket(4) tests The ng_ksocket(4) functionality is very fragile as it interfaces with kernel socket code in unusual way. It definitely needs a test suite. Start one with a test that tests UDP over IPv4. --- tests/sys/netgraph/Makefile | 1 + tests/sys/netgraph/ksocket.c | 115 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) diff --git a/tests/sys/netgraph/Makefile b/tests/sys/netgraph/Makefile index 36f9943a7312..2240570ff46a 100644 --- a/tests/sys/netgraph/Makefile +++ b/tests/sys/netgraph/Makefile @@ -14,6 +14,7 @@ TEST_METADATA.ng_macfilter_test+= required_programs="perl" ATF_TESTS_C+= basic \ bridge \ hub \ + ksocket \ socket \ vlan_rotate \ diff --git a/tests/sys/netgraph/ksocket.c b/tests/sys/netgraph/ksocket.c new file mode 100644 index 000000000000..e97b9b3f0691 --- /dev/null +++ b/tests/sys/netgraph/ksocket.c @@ -0,0 +1,115 @@ +/*- + * 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 + +static void +hellocheck(int wr, int rd) +{ + char sbuf[] = "Hello, peer!", rbuf[sizeof(sbuf)]; + + ATF_REQUIRE(send(wr, sbuf, sizeof(sbuf), 0) == sizeof(sbuf)); + ATF_REQUIRE(recv(rd, rbuf, sizeof(rbuf), 0) == sizeof(sbuf)); + ATF_REQUIRE(strcmp(sbuf, rbuf) == 0); +} + +#define OURHOOK "ks" + +ATF_TC_WITHOUT_HEAD(udp_connect); +ATF_TC_BODY(udp_connect, tc) +{ + struct sockaddr sa = { + .sa_family = AF_INET, + }; + socklen_t slen = sizeof(sa); + int ds, cs, us; + + ATF_REQUIRE((us = socket(PF_INET, SOCK_DGRAM, 0)) > 0); + ATF_REQUIRE(bind(us, &sa, sizeof(sa)) == 0); + ATF_REQUIRE(getsockname(us, &sa, &slen) == 0); + + struct ngm_mkpeer mkp = { + .type = NG_KSOCKET_NODE_TYPE, + .ourhook = OURHOOK, + .peerhook = "inet/dgram/udp", + }; + ATF_REQUIRE(NgMkSockNode(NULL, &cs, &ds) == 0); + ATF_REQUIRE(NgSendMsg(cs, ".", NGM_GENERIC_COOKIE, NGM_MKPEER, &mkp, + sizeof(mkp)) >= 0); + ATF_REQUIRE(NgSendMsg(cs, ".:" OURHOOK, NGM_KSOCKET_COOKIE, + NGM_KSOCKET_CONNECT, &sa, sizeof(sa)) >= 0); + + hellocheck(ds, us); +} + +ATF_TC_WITHOUT_HEAD(udp_bind); +ATF_TC_BODY(udp_bind, tc) +{ + struct sockaddr_in sin = { + .sin_family = AF_INET, + .sin_len = sizeof(sin), + }; + struct ng_mesg *rep; + int ds, cs, us; + + struct ngm_mkpeer mkp = { + .type = NG_KSOCKET_NODE_TYPE, + .ourhook = OURHOOK, + .peerhook = "inet/dgram/udp", + }; + ATF_REQUIRE(NgMkSockNode(NULL, &cs, &ds) == 0); + ATF_REQUIRE(NgSendMsg(cs, ".", NGM_GENERIC_COOKIE, NGM_MKPEER, &mkp, + sizeof(mkp)) >= 0); + ATF_REQUIRE(NgSendMsg(cs, ".:" OURHOOK, NGM_KSOCKET_COOKIE, + NGM_KSOCKET_BIND, &sin, sizeof(sin)) >= 0); + ATF_REQUIRE(NgSendMsg(cs, ".:" OURHOOK, NGM_KSOCKET_COOKIE, + NGM_KSOCKET_GETNAME, NULL, 0) >= 0); + ATF_REQUIRE(NgAllocRecvMsg(cs, &rep, NULL) == sizeof(struct ng_mesg) + + sizeof(struct sockaddr_in)); + + ATF_REQUIRE((us = socket(PF_INET, SOCK_DGRAM, 0)) > 0); + ATF_REQUIRE(connect(us, (struct sockaddr *)rep->data, + sizeof(struct sockaddr_in)) == 0); + + hellocheck(us, ds); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, udp_connect); + ATF_TP_ADD_TC(tp, udp_bind); + + return (atf_no_error()); +}