[v3,3/4] test: add tests for rte_ether routines

Message ID 20231003202909.391330-4-stephen@networkplumber.org (mailing list archive)
State Accepted, archived
Delegated to: Ferruh Yigit
Headers
Series rte_ether_unformat_addr related changes |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Stephen Hemminger Oct. 3, 2023, 8:29 p.m. UTC
  This add some basic tests for rte_unformat_ether_addr
and other functions in rte_ether.

Acked-by: Morten Brørup <mb@smartsharesystems.com>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/test/meson.build      |   1 +
 app/test/test_net_ether.c | 165 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 166 insertions(+)
 create mode 100644 app/test/test_net_ether.c
  

Patch

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..1cb6845a9c48
--- /dev/null
+++ b/app/test/test_net_ether.c
@@ -0,0 +1,165 @@ 
+/* 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 address 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");
+
+
+	return 0;
+}
+
+static int
+test_invalid_addr(void)
+{
+	static const char * const invalid[] = {
+		"123",
+		"123:456",
+		"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",
+		"NOT.AN.ADDRESS",
+		"102.304.506",
+		"",
+	};
+	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);