From: Stephen Hemminger <stephen@networkplumber.org>
To: dev@dpdk.org
Cc: Stephen Hemminger <stephen@networkplumber.org>
Subject: [PATCH v2 3/3] test: add tests for rte_ether routines
Date: Mon, 2 Oct 2023 11:37:30 -0700 [thread overview]
Message-ID: <20231002183730.301163-4-stephen@networkplumber.org> (raw)
In-Reply-To: <20231002183730.301163-1-stephen@networkplumber.org>
This add some basic tests for rte_unformat_ether_addr
and other functions in rte_ether.
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
app/test/meson.build | 1 +
app/test/test_net_ether.c | 169 ++++++++++++++++++++++++++++++++++++++
2 files changed, 170 insertions(+)
create mode 100644 app/test/test_net_ether.c
diff --git a/app/test/meson.build b/app/test/meson.build
index bf9fc906128f..20a9333c726d 100644
--- a/app/test/meson.build
+++ b/app/test/meson.build
@@ -124,6 +124,7 @@ source_file_deps = {
'test_meter.c': ['meter'],
'test_metrics.c': ['metrics'],
'test_mp_secondary.c': ['hash', 'lpm'],
+ 'test_net_ether.c': ['net'],
'test_pcapng.c': ['ethdev', 'net', 'pcapng'],
'test_pdcp.c': ['eventdev', 'pdcp', 'net', 'timer', 'security'],
'test_pdump.c': ['pdump'] + sample_packet_forward_deps,
diff --git a/app/test/test_net_ether.c b/app/test/test_net_ether.c
new file mode 100644
index 000000000000..a6d38cc2ffa0
--- /dev/null
+++ b/app/test/test_net_ether.c
@@ -0,0 +1,169 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright (c) 2023 Stephen Hemminger
+ */
+
+#include <rte_ether.h>
+
+#include <rte_test.h>
+#include "test.h"
+
+#define N 1000000
+
+static const struct rte_ether_addr zero_ea;
+static const struct rte_ether_addr bcast_ea = {
+ .addr_bytes = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
+};
+
+static int
+test_ether_addr(void)
+{
+ struct rte_ether_addr rand_ea = { };
+ unsigned int i;
+
+ RTE_TEST_ASSERT(rte_is_zero_ether_addr(&zero_ea), "Zero address is not zero");
+ RTE_TEST_ASSERT(!rte_is_zero_ether_addr(&bcast_ea), "Broadcast is zero");
+
+ for (i = 0; i < N; i++) {
+ rte_eth_random_addr(rand_ea.addr_bytes);
+ RTE_TEST_ASSERT(!rte_is_zero_ether_addr(&rand_ea),
+ "Random address is zero");
+ RTE_TEST_ASSERT(rte_is_unicast_ether_addr(&rand_ea),
+ "Random addrss is not unicast");
+ RTE_TEST_ASSERT(rte_is_local_admin_ether_addr(&rand_ea),
+ "Random address is not local admin");
+ }
+
+ return 0;
+}
+
+static int
+test_format_addr(void)
+{
+ struct rte_ether_addr rand_ea = { };
+ char buf[RTE_ETHER_ADDR_FMT_SIZE];
+ unsigned int i;
+
+ for (i = 0; i < N; i++) {
+ struct rte_ether_addr result = { };
+ int ret;
+
+ rte_eth_random_addr(rand_ea.addr_bytes);
+
+ rte_ether_format_addr(buf, sizeof(buf), &rand_ea);
+
+ ret = rte_ether_unformat_addr(buf, &result);
+ if (ret != 0) {
+ fprintf(stderr, "rte_ether_unformat_addr(%s) failed\n", buf);
+ return -1;
+ }
+ RTE_TEST_ASSERT(rte_is_same_ether_addr(&rand_ea, &result),
+ "rte_ether_format/unformat mismatch");
+ }
+ return 0;
+
+}
+
+static int
+test_unformat_addr(void)
+{
+ const struct rte_ether_addr expected = {
+ .addr_bytes = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc },
+ };
+ const struct rte_ether_addr nozero_ea = {
+ .addr_bytes = { 1, 2, 3, 4, 5, 6 },
+ };
+ struct rte_ether_addr result;
+ int ret;
+
+ /* Test IETF format */
+ memset(&result, 0, sizeof(result));
+ ret = rte_ether_unformat_addr("12:34:56:78:9a:bc", &result);
+ RTE_TEST_ASSERT(ret == 0, "IETF unformat failed");
+ RTE_TEST_ASSERT(rte_is_same_ether_addr(&expected, &result),
+ "IETF unformat mismatch");
+
+ /* Test IEEE format */
+ memset(&result, 0, sizeof(result));
+ ret = rte_ether_unformat_addr("12-34-56-78-9A-BC", &result);
+ RTE_TEST_ASSERT(ret == 0, "IEEE unformat failed");
+ RTE_TEST_ASSERT(rte_is_same_ether_addr(&expected, &result),
+ "IEEE unformat mismatch");
+
+ /* Test Cisco format */
+ memset(&result, 0, sizeof(result));
+ ret = rte_ether_unformat_addr("1234.5678.9ABC", &result);
+ RTE_TEST_ASSERT(ret == 0, "Cisco unformat failed");
+ RTE_TEST_ASSERT(rte_is_same_ether_addr(&expected, &result),
+ "Cisco unformat mismatch");
+
+ /* Test no leading zeros - IETF */
+ memset(&result, 0, sizeof(result));
+ ret = rte_ether_unformat_addr("1:2:3:4:5:6", &result);
+ RTE_TEST_ASSERT(ret == 0, "IETF leading zero failed");
+ RTE_TEST_ASSERT(rte_is_same_ether_addr(&nozero_ea, &result),
+ "IETF leading zero mismatch");
+
+ /* Test no-leading zero - IEEE format */
+ memset(&result, 0, sizeof(result));
+ ret = rte_ether_unformat_addr("1-2-3-4-5-6", &result);
+ RTE_TEST_ASSERT(ret == 0, "IEEE leading zero failed");
+ RTE_TEST_ASSERT(rte_is_same_ether_addr(&nozero_ea, &result),
+ "IEEE leading zero mismatch");
+
+ /* Test no-leading zero - Cisco format */
+ memset(&result, 0, sizeof(result));
+ ret = rte_ether_unformat_addr("102.304.506", &result);
+ RTE_TEST_ASSERT(ret == 0, "Cisco leading zero failed");
+ RTE_TEST_ASSERT(rte_is_same_ether_addr(&nozero_ea, &result),
+ "Cisco leading zero mismatch");
+
+ return 0;
+}
+
+static int
+test_invalid_addr(void)
+{
+ static const char * const invalid[] = {
+ "NO:ADDRESS",
+ "123",
+ "12:34:56:78:9a:gh",
+ "12:34:56:78:9a",
+ "100:34:56:78:9a:bc",
+ "34-56-78-9a-bc",
+ "12:34:56-78:9a:bc",
+ "12:34:56.78:9a:bc",
+ "123:456:789:abc",
+ "",
+ };
+ struct rte_ether_addr result;
+ unsigned int i;
+
+ for (i = 0; i < RTE_DIM(invalid); ++i) {
+ if (!rte_ether_unformat_addr(invalid[i], &result)) {
+ fprintf(stderr, "rte_ether_unformat_addr(%s) succeeded!\n",
+ invalid[i]);
+ return -1;
+ }
+ }
+ return 0;
+}
+
+static int
+test_net_ether(void)
+{
+ if (test_ether_addr())
+ return -1;
+
+ if (test_format_addr())
+ return -1;
+
+ if (test_unformat_addr())
+ return -1;
+
+ if (test_invalid_addr())
+ return -1;
+
+ return 0;
+}
+
+REGISTER_FAST_TEST(net_ether_autotest, true, true, test_net_ether);
--
2.39.2
next prev parent reply other threads:[~2023-10-02 18:38 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-29 16:36 [RFC] rte_ether_unformat: accept more inputs Stephen Hemminger
2023-09-29 19:35 ` Morten Brørup
2023-09-29 23:08 ` Stephen Hemminger
2023-10-02 18:37 ` [PATCH v2 0/3] rte_ether_unformat_addr changes Stephen Hemminger
2023-10-02 18:37 ` [PATCH v2 1/3] test: remove some strings from cmdline_etheraddr tests Stephen Hemminger
2023-10-03 10:47 ` Ferruh Yigit
2023-10-03 10:59 ` Ferruh Yigit
2023-10-03 16:38 ` Stephen Hemminger
2023-10-03 16:36 ` Stephen Hemminger
2023-10-03 16:50 ` Ferruh Yigit
2023-10-03 17:18 ` Stephen Hemminger
2023-10-02 18:37 ` [PATCH v2 2/3] rte_ether_unformat: accept more inputs Stephen Hemminger
2023-10-02 18:37 ` Stephen Hemminger [this message]
2023-10-03 6:17 ` [PATCH v2 0/3] rte_ether_unformat_addr changes Morten Brørup
2023-10-03 10:44 ` Ferruh Yigit
2023-10-03 16:27 ` Stephen Hemminger
2023-10-03 16:34 ` Stephen Hemminger
2023-10-03 20:29 ` [PATCH v3 0/4] rte_ether_unformat_addr related changes Stephen Hemminger
2023-10-03 20:29 ` [PATCH v3 1/4] test: remove some strings from cmdline_etheraddr tests Stephen Hemminger
2023-10-03 20:29 ` [PATCH v3 2/4] rte_ether_unformat: accept more inputs Stephen Hemminger
2023-10-03 20:29 ` [PATCH v3 3/4] test: add tests for rte_ether routines Stephen Hemminger
2023-10-03 20:29 ` [PATCH v3 4/4] net/tap: use rte_ether_unformat_address Stephen Hemminger
2023-10-11 14:25 ` Ferruh Yigit
2023-10-04 11:48 ` [PATCH v3 0/4] rte_ether_unformat_addr related changes Ferruh Yigit
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231002183730.301163-4-stephen@networkplumber.org \
--to=stephen@networkplumber.org \
--cc=dev@dpdk.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).