public inbox for ell@lists.linux.dev
 help / color / mirror / Atom feed
From: Denis Kenzior <denkenz@gmail.com>
To: ell@lists.linux.dev
Cc: Denis Kenzior <denkenz@gmail.com>
Subject: [PATCH 10/12] unit: Add netlink message builder / parser tests
Date: Mon, 22 Jul 2024 14:04:27 -0500	[thread overview]
Message-ID: <20240722190443.43196-10-denkenz@gmail.com> (raw)
In-Reply-To: <20240722190443.43196-1-denkenz@gmail.com>

These tests are similar to l_genl_msg based tests, but use
l_netlink_message APIs directly.
---
 unit/test-genl-msg.c | 74 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 74 insertions(+)

diff --git a/unit/test-genl-msg.c b/unit/test-genl-msg.c
index e6432f4362ef..3d996f4e242a 100644
--- a/unit/test-genl-msg.c
+++ b/unit/test-genl-msg.c
@@ -14,6 +14,8 @@
 #include <linux/genetlink.h>
 #include <ell/ell.h>
 
+#include "ell/netlink-private.h"
+
 static bool do_print = false;
 
 static void do_debug(const char *str, void *user_data)
@@ -126,6 +128,38 @@ static void build_set_station(const void *data)
 	l_genl_msg_unref(msg);
 }
 
+static void build_set_station_netlink(const void *data)
+{
+	const struct set_station_test *test = data;
+	struct l_netlink_message *m;
+	struct genlmsghdr genlhdr = { .cmd = 18, .version = 0, .reserved = 0 };
+
+	m = l_netlink_message_new(0x17, NLM_F_REQUEST | NLM_F_ACK);
+	assert(m);
+
+	assert(!l_netlink_message_add_header(m, &genlhdr, sizeof(genlhdr)));
+	assert(!l_netlink_message_append_u32(m, 3, test->ifindex));
+	assert(!l_netlink_message_append_mac(m, 6, test->mac));
+	assert(!l_netlink_message_append(m, 67,
+					test->flags, sizeof(test->flags)));
+
+	m->hdr->nlmsg_seq = test->seq;
+	m->hdr->nlmsg_pid = test->pid;
+
+	if (do_print) {
+		l_util_hexdump(false, m->data, m->hdr->nlmsg_len,
+					do_debug, "[MSG] ");
+		l_util_hexdump(true, set_station_request,
+				sizeof(set_station_request), do_debug, "[MSG] ");
+	}
+
+	assert(m->hdr->nlmsg_len == sizeof(set_station_request));
+	assert(!memcmp(m->data, set_station_request,
+					sizeof(set_station_request)));
+
+	l_netlink_message_unref(m);
+}
+
 static const unsigned char set_rekey_offload_request[] = {
 	0x54, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x05, 0x00, 0x59, 0xa3, 0xe1, 0x53,
 	0xba, 0x02, 0x40, 0xe7, 0x4f, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
@@ -252,6 +286,42 @@ static void build_set_rekey_offload(const void *data)
 	l_genl_msg_unref(msg);
 }
 
+static void build_set_rekey_offload_netlink(const void *data)
+{
+	const struct set_rekey_offload_test *test = data;
+	struct l_netlink_message *m;
+	struct genlmsghdr genlhdr = { .cmd = 79, .version = 0, .reserved = 0 };
+
+	m = l_netlink_message_new(0x1b, NLM_F_REQUEST | NLM_F_ACK);
+	assert(m);
+
+	assert(!l_netlink_message_add_header(m, &genlhdr, sizeof(genlhdr)));
+	assert(!l_netlink_message_append_u32(m, 3, test->ifindex));
+	assert(!l_netlink_message_enter_nested(m, 122));
+	assert(!l_netlink_message_append(m, 1, test->kek, sizeof(test->kek)));
+	assert(!l_netlink_message_append(m, 2, test->kck, sizeof(test->kck)));
+	assert(!l_netlink_message_append(m, 3, test->replay_counter,
+						sizeof(test->replay_counter)));
+	assert(!l_netlink_message_leave_nested(m));
+
+	m->hdr->nlmsg_seq = test->seq;
+	m->hdr->nlmsg_pid = test->pid;
+
+	if (do_print) {
+		l_util_hexdump(false, m->data, m->hdr->nlmsg_len,
+					do_debug, "[MSG] ");
+		l_util_hexdump(true, set_rekey_offload_request,
+				sizeof(set_rekey_offload_request),
+				do_debug, "[MSG] ");
+	}
+
+	assert(m->hdr->nlmsg_len == sizeof(set_rekey_offload_request));
+	assert(!memcmp(m->data, set_rekey_offload_request,
+					sizeof(set_rekey_offload_request)));
+
+	l_netlink_message_unref(m);
+}
+
 /*
  * This example is generated by libnl:
 	msg = nlmsg_alloc();
@@ -441,8 +511,12 @@ int main(int argc, char *argv[])
 				parse_set_rekey_offload, &rekey_offload);
 
 	l_test_add("Build Set Station Request", build_set_station, &set_station);
+	l_test_add("Build Set Station Request (Netlink)",
+			build_set_station_netlink, &set_station);
 	l_test_add("Build Set Rekey Offload Request",
 				build_set_rekey_offload, &rekey_offload);
+	l_test_add("Build Set Rekey Offload Request (Netlink)",
+			build_set_rekey_offload_netlink, &rekey_offload);
 
 	l_test_add("libnl-generated Example with Nesting",
 				parse_libnl_nested, NULL);
-- 
2.45.2


  parent reply	other threads:[~2024-07-22 19:04 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-22 19:04 [PATCH 01/12] useful: Add utility to find the next power of two Denis Kenzior
2024-07-22 19:04 ` [PATCH 02/12] unit: Add unit test for roundup_pow_of_two Denis Kenzior
2024-07-22 19:04 ` [PATCH 03/12] util: Introduce l_util_pagesize Denis Kenzior
2024-07-22 19:04 ` [PATCH 04/12] string: Use conservative allocation growth strategy Denis Kenzior
2024-07-22 19:04 ` [PATCH 05/12] unit: Refactor genl-msg tests Denis Kenzior
2024-07-22 19:04 ` [PATCH 06/12] netlink: Add l_netlink_message class Denis Kenzior
2024-07-22 19:04 ` [PATCH 07/12] netlink: support appending using scatter / gather Denis Kenzior
2024-07-22 19:04 ` [PATCH 08/12] netlink: Support adding nested attributes to messages Denis Kenzior
2024-07-22 19:04 ` [PATCH 09/12] netlink: Add additional helpers Denis Kenzior
2024-07-22 19:04 ` Denis Kenzior [this message]
2024-07-22 19:04 ` [PATCH 11/12] netlink: add netlink_message_from_nlmsg Denis Kenzior
2024-07-22 19:04 ` [PATCH 12/12] genl: Use l_netlink_message as implementation for l_genl_msg Denis Kenzior
2024-07-23 23:51 ` [PATCH 01/12] useful: Add utility to find the next power of two Denis Kenzior

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=20240722190443.43196-10-denkenz@gmail.com \
    --to=denkenz@gmail.com \
    --cc=ell@lists.linux.dev \
    /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