From patchwork Thu Nov 18 08:26:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frode Nordahl X-Patchwork-Id: 1556540 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=canonical.com header.i=@canonical.com header.a=rsa-sha256 header.s=20210705 header.b=dSEy/d7o; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=2605:bc80:3010::138; helo=smtp1.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Received: from smtp1.osuosl.org (smtp1.osuosl.org [IPv6:2605:bc80:3010::138]) (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) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HvtDn5LLFz9sPf for ; Thu, 18 Nov 2021 19:26:33 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 707D1814AB; Thu, 18 Nov 2021 08:26:31 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id A1bEnw1gvjsm; Thu, 18 Nov 2021 08:26:30 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp1.osuosl.org (Postfix) with ESMTPS id A1E7D80C4B; Thu, 18 Nov 2021 08:26:29 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 73AA2C002E; Thu, 18 Nov 2021 08:26:29 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp1.osuosl.org (smtp1.osuosl.org [140.211.166.138]) by lists.linuxfoundation.org (Postfix) with ESMTP id 7566BC0012 for ; Thu, 18 Nov 2021 08:26:28 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 64C3380DB8 for ; Thu, 18 Nov 2021 08:26:28 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id q8t5dyp71Jdv for ; Thu, 18 Nov 2021 08:26:27 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0 Received: from smtp-relay-canonical-0.canonical.com (smtp-relay-canonical-0.canonical.com [185.125.188.120]) by smtp1.osuosl.org (Postfix) with ESMTPS id 028F480C4B for ; Thu, 18 Nov 2021 08:26:26 +0000 (UTC) Received: from frode-threadripper.. (ti0189a330-1161.bb.online.no [88.88.219.141]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-canonical-0.canonical.com (Postfix) with ESMTPSA id 4A72440E4F for ; Thu, 18 Nov 2021 08:26:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1637223983; bh=8Uo0ViCPc+RDMFF1j/3vjzkEsj8Y1gTA0/I8K0JWHHg=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=dSEy/d7o3DbbzzyrtdRiAyG6z40Czm3+UBlj5ZibeQOa11ZtzUBluJa6jvDpp47kx sD8w3ixkceynnCSAXisHMyRgCqM7LRM/e2Nn77gMPBWga10YbKMAr6H9rHwKKC1++D kNpMu8Q0PwRFSACxFDlIxMKjVeu2o1gE2PmacOlvAbSRDfnmWMVifaVIo+eweo6n5g rxQYBJ0E4VwGBeww8dQO0LKgL7zyXP8uRopXnX9+EfKO5XaQXLQJu0Eaz1CjVQ27xp r8omyzrsvT8j/oQbPqA6qCPFueUPnqDD0hNul4xE17w2XWJtb4izhOUwXrXB4kJBdA kpXiuHEPupJFw== From: Frode Nordahl To: dev@openvswitch.org Date: Thu, 18 Nov 2021 09:26:22 +0100 Message-Id: <20211118082622.4147433-1-frode.nordahl@canonical.com> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Subject: [ovs-dev] [PATCH] tests: Handle endianness in netlink policy test X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" The netlink policy unit test contains test fixture data that is subject to endianness and currently fails on big endian systems. Add helper that ensures correct byte order for the affected data. Fixes: bfee9f6c0115 ("netlink: Add support for parsing link layer address.") Signed-off-by: Frode Nordahl --- tests/test-netlink-policy.c | 58 +++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/tests/test-netlink-policy.c b/tests/test-netlink-policy.c index 5f2bf7101..ff00b3f04 100644 --- a/tests/test-netlink-policy.c +++ b/tests/test-netlink-policy.c @@ -32,6 +32,22 @@ _nla_len(const struct nlattr *nla) { return nla->nla_len - NLA_HDRLEN; } +/* The header part of the test fixture data is subject to endianness. This + * helper makes sure they are put into the buffer in the right order. */ +static void +prepare_ofpbuf_fixture(struct ofpbuf *buf, uint8_t *data, size_t size) { + uint16_t hword; + + ovs_assert(size >= 4); + + ofpbuf_init(buf, size); + hword = data[0] | data[1] << 8; + ofpbuf_put(buf, &hword, sizeof(hword)); + hword = data[2] | data[3] << 8; + ofpbuf_put(buf, &hword, sizeof(hword)); + ofpbuf_put(buf, &data[4], size - sizeof(hword) * 2); +} + static void test_nl_policy_parse_ll_addr(struct ovs_cmdl_context *ctx OVS_UNUSED) { struct nl_policy policy[] = { @@ -62,37 +78,37 @@ test_nl_policy_parse_ll_addr(struct ovs_cmdl_context *ctx OVS_UNUSED) { /* valid policy but data neither eth_addr nor ib_addr */ 0x0b, 0x00, 0x2a, 0x00, 0x00, 0x53, 0x00, 0x00, 0x00, 0x2a, 0x00, }; - struct ofpbuf *buf; + struct ofpbuf buf; /* confirm policy fails with too short data */ - buf = ofpbuf_clone_data(&fixture_nl_data_policy_short, - sizeof(fixture_nl_data_policy_short)); - ovs_assert(!nl_policy_parse(buf, 0, policy, attrs, ARRAY_SIZE(policy))); - ofpbuf_delete(buf); + prepare_ofpbuf_fixture(&buf, fixture_nl_data_policy_short, + sizeof(fixture_nl_data_policy_short)); + ovs_assert(!nl_policy_parse(&buf, 0, policy, attrs, ARRAY_SIZE(policy))); + ofpbuf_uninit(&buf); memset(&attrs, 0, sizeof(*attrs)); /* confirm policy fails with too long data */ - buf = ofpbuf_clone_data(&fixture_nl_data_policy_long, - sizeof(fixture_nl_data_policy_long)); - ovs_assert(!nl_policy_parse(buf, 0, policy, attrs, ARRAY_SIZE(policy))); - ofpbuf_delete(buf); + prepare_ofpbuf_fixture(&buf, fixture_nl_data_policy_long, + sizeof(fixture_nl_data_policy_long)); + ovs_assert(!nl_policy_parse(&buf, 0, policy, attrs, ARRAY_SIZE(policy))); + ofpbuf_uninit(&buf); memset(&attrs, 0, sizeof(*attrs)); /* confirm policy passes and interpret valid ethernet lladdr */ - buf = ofpbuf_clone_data(&fixture_nl_data_eth, - sizeof(fixture_nl_data_eth)); - ovs_assert(nl_policy_parse(buf, 0, policy, attrs, ARRAY_SIZE(policy))); + prepare_ofpbuf_fixture(&buf, fixture_nl_data_eth, + sizeof(fixture_nl_data_eth)); + ovs_assert(nl_policy_parse(&buf, 0, policy, attrs, ARRAY_SIZE(policy))); ovs_assert((_nla_len(attrs[42]) == sizeof(struct eth_addr))); struct eth_addr eth_expect = ETH_ADDR_C(00,53,00,00,00,2a); struct eth_addr eth_parsed = nl_attr_get_eth_addr(attrs[42]); ovs_assert((!memcmp(ð_expect, ð_parsed, sizeof(struct eth_addr)))); - ofpbuf_delete(buf); + ofpbuf_uninit(&buf); memset(&attrs, 0, sizeof(*attrs)); /* confirm policy passes and interpret valid infiniband lladdr */ - buf = ofpbuf_clone_data(&fixture_nl_data_ib, - sizeof(fixture_nl_data_ib)); - ovs_assert(nl_policy_parse(buf, 0, policy, attrs, ARRAY_SIZE(policy))); + prepare_ofpbuf_fixture(&buf, fixture_nl_data_ib, + sizeof(fixture_nl_data_ib)); + ovs_assert(nl_policy_parse(&buf, 0, policy, attrs, ARRAY_SIZE(policy))); ovs_assert((_nla_len(attrs[42]) == sizeof(struct ib_addr))); struct ib_addr ib_expect = { .ia = { @@ -102,18 +118,18 @@ test_nl_policy_parse_ll_addr(struct ovs_cmdl_context *ctx OVS_UNUSED) { }; struct ib_addr ib_parsed = nl_attr_get_ib_addr(attrs[42]); ovs_assert((!memcmp(&ib_expect, &ib_parsed, sizeof(struct eth_addr)))); - ofpbuf_delete(buf); + ofpbuf_uninit(&buf); memset(&attrs, 0, sizeof(*attrs)); /* confirm we're able to detect invalid data that passes policy check, this * can happen because the policy defines the data to be between the * currently known lladdr sizes of 6 (ETH_ALEN) and 20 (INFINIBAND_ALEN) */ - buf = ofpbuf_clone_data(&fixture_nl_data_invalid, - sizeof(fixture_nl_data_invalid)); - ovs_assert(nl_policy_parse(buf, 0, policy, attrs, ARRAY_SIZE(policy))); + prepare_ofpbuf_fixture(&buf, fixture_nl_data_invalid, + sizeof(fixture_nl_data_invalid)); + ovs_assert(nl_policy_parse(&buf, 0, policy, attrs, ARRAY_SIZE(policy))); ovs_assert(_nla_len(attrs[42]) != sizeof(struct eth_addr) && _nla_len(attrs[42]) != sizeof(struct ib_addr)); - ofpbuf_delete(buf); + ofpbuf_uninit(&buf); memset(&attrs, 0, sizeof(*attrs)); }