From: Denis Kenzior <denkenz@gmail.com>
To: ell@lists.linux.dev
Cc: Denis Kenzior <denkenz@gmail.com>
Subject: [PATCH 05/12] unit: Refactor genl-msg tests
Date: Mon, 22 Jul 2024 14:04:22 -0500 [thread overview]
Message-ID: <20240722190443.43196-5-denkenz@gmail.com> (raw)
In-Reply-To: <20240722190443.43196-1-denkenz@gmail.com>
Both parse|build_set_station and parse|build_set_rekey_offload tests
declare precanned information as static const variables in the
individual test functions. Declare this precanned information inside a
test structure for easier maintenance and future reusability.
---
unit/test-genl-msg.c | 124 ++++++++++++++++++++++++++-----------------
1 file changed, 74 insertions(+), 50 deletions(-)
diff --git a/unit/test-genl-msg.c b/unit/test-genl-msg.c
index 0ae92aefebc9..e6432f4362ef 100644
--- a/unit/test-genl-msg.c
+++ b/unit/test-genl-msg.c
@@ -31,10 +31,25 @@ static unsigned char set_station_request[] = {
0x02, 0x00, 0x00, 0x00,
};
+struct set_station_test {
+ uint8_t mac[6];
+ uint32_t ifindex;
+ uint32_t flags[2];
+ uint32_t seq;
+ uint32_t pid;
+};
+
+static const struct set_station_test set_station = {
+ .mac = { 0x24, 0xa2, 0xe1, 0xec, 0x17, 0x04 },
+ .ifindex = 3,
+ .flags = { 2, 2 },
+ .seq = 0x550d538b,
+ .pid = 3604,
+};
+
static void parse_set_station(const void *data)
{
- static const unsigned char mac[6] =
- { 0x24, 0xa2, 0xe1, 0xec, 0x17, 0x04 };
+ const struct set_station_test *test = data;
struct nlmsghdr *nlmsg;
struct l_genl_msg *msg;
struct l_genl_attr attr;
@@ -56,13 +71,13 @@ static void parse_set_station(const void *data)
assert(l_genl_attr_next(&attr, &type, &len, &payload));
assert(type == 3);
assert(len == 4);
- assert(*((unsigned int *) payload) == 3);
+ assert(*((unsigned int *) payload) == test->ifindex);
/* MAC Address 24:A2:E1:EC:17:04 */
assert(l_genl_attr_next(&attr, &type, &len, &payload));
assert(type == 6);
- assert(len == 6);
- assert(!memcmp(payload, mac, 6));
+ assert(len == sizeof(test->mac));
+ assert(!memcmp(payload, test->mac, sizeof(test->mac)));
/* Station Flags 2: len 8
* Mask: 0x00000002
@@ -72,19 +87,16 @@ static void parse_set_station(const void *data)
*/
assert(l_genl_attr_next(&attr, &type, &len, &payload));
assert(type == 67);
- assert(len == 8);
- assert(((unsigned int *) payload)[0] == 2);
- assert(((unsigned int *) payload)[1] == 2);
+ assert(len == sizeof(test->flags));
+ assert(((unsigned int *) payload)[0] == test->flags[0]);
+ assert(((unsigned int *) payload)[1] == test->flags[1]);
l_genl_msg_unref(msg);
}
static void build_set_station(const void *data)
{
- static uint32_t index = 3;
- static const unsigned char mac[6] =
- { 0x24, 0xa2, 0xe1, 0xec, 0x17, 0x04 };
- static uint32_t flags[] = { 2, 2 };
+ const struct set_station_test *test = data;
struct l_genl_msg *msg;
const void *raw;
size_t size;
@@ -92,15 +104,19 @@ static void build_set_station(const void *data)
msg = l_genl_msg_new_sized(18, 512);
assert(msg);
- assert(l_genl_msg_append_attr(msg, 3, 4, &index));
- assert(l_genl_msg_append_attr(msg, 6, 6, mac));
- assert(l_genl_msg_append_attr(msg, 67, 8, flags));
+ assert(l_genl_msg_append_attr(msg, 3,
+ sizeof(test->ifindex), &test->ifindex));
+ assert(l_genl_msg_append_attr(msg, 6, sizeof(test->mac), test->mac));
+ assert(l_genl_msg_append_attr(msg, 67,
+ sizeof(test->flags), test->flags));
- raw = l_genl_msg_to_data(msg, 0x17, 0x05, 0x550d538b, 3604, &size);
+ raw = l_genl_msg_to_data(msg, 0x17, NLM_F_REQUEST | NLM_F_ACK,
+ test->seq, test->pid, &size);
if (do_print) {
l_util_hexdump(false, raw, size, do_debug, "[MSG] ");
- l_util_hexdump(true, set_station_request, size,
+ l_util_hexdump(true, set_station_request,
+ sizeof(set_station_request),
do_debug, "[MSG] ");
}
@@ -120,16 +136,29 @@ static const unsigned char set_rekey_offload_request[] = {
0x0c, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
};
+struct set_rekey_offload_test {
+ uint32_t seq;
+ uint32_t pid;
+ uint32_t ifindex;
+ uint8_t kek[16];
+ uint8_t kck[16];
+ uint8_t replay_counter[8];
+};
+
+static const struct set_rekey_offload_test rekey_offload = {
+ .seq = 0x53e1a359,
+ .pid = 0xe74002ba,
+ .kek = { 0x2f, 0x82, 0xbb, 0x0d, 0x93, 0x56, 0x60, 0x4b,
+ 0xb1, 0x55, 0x1c, 0x85, 0xc0, 0xeb, 0x32, 0x8b },
+ .kck = { 0x43, 0x25, 0xcf, 0x08, 0x0b, 0x92, 0xa7, 0x2d,
+ 0x86, 0xdc, 0x43, 0x21, 0xd6, 0x0c, 0x12, 0x03 },
+ .replay_counter = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
+ .ifindex = 3,
+};
+
static void parse_set_rekey_offload(const void *data)
{
- static const unsigned char kek[] = {
- 0x2f, 0x82, 0xbb, 0x0d, 0x93, 0x56, 0x60, 0x4b,
- 0xb1, 0x55, 0x1c, 0x85, 0xc0, 0xeb, 0x32, 0x8b };
- static const unsigned char kck[] = {
- 0x43, 0x25, 0xcf, 0x08, 0x0b, 0x92, 0xa7, 0x2d,
- 0x86, 0xdc, 0x43, 0x21, 0xd6, 0x0c, 0x12, 0x03 };
- static const unsigned char replay_counter[] = {
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
+ const struct set_rekey_offload_test *test = data;
struct nlmsghdr *nlmsg;
struct l_genl_msg *msg;
struct l_genl_attr attr;
@@ -172,33 +201,25 @@ static void parse_set_rekey_offload(const void *data)
assert(l_genl_attr_next(&nested, &type, &len, &payload));
assert(type == 1);
- assert(len == 16);
- assert(!memcmp(payload, kek, 16));
+ assert(len == sizeof(test->kek));
+ assert(!memcmp(payload, test->kek, sizeof(test->kek)));
assert(l_genl_attr_next(&nested, &type, &len, &payload));
assert(type == 2);
- assert(len == 16);
- assert(!memcmp(payload, kck, 16));
+ assert(len == sizeof(test->kck));
+ assert(!memcmp(payload, test->kck, sizeof(test->kck)));
assert(l_genl_attr_next(&nested, &type, &len, &payload));
assert(type == 3);
- assert(len == 8);
- assert(!memcmp(payload, replay_counter, 8));
+ assert(len == sizeof(test->replay_counter));
+ assert(!memcmp(payload, test->replay_counter, len));
l_genl_msg_unref(msg);
}
static void build_set_rekey_offload(const void *data)
{
- static uint32_t index = 3;
- static const unsigned char kek[] = {
- 0x2f, 0x82, 0xbb, 0x0d, 0x93, 0x56, 0x60, 0x4b,
- 0xb1, 0x55, 0x1c, 0x85, 0xc0, 0xeb, 0x32, 0x8b };
- static const unsigned char kck[] = {
- 0x43, 0x25, 0xcf, 0x08, 0x0b, 0x92, 0xa7, 0x2d,
- 0x86, 0xdc, 0x43, 0x21, 0xd6, 0x0c, 0x12, 0x03 };
- static const unsigned char replay_counter[] = {
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
+ const struct set_rekey_offload_test *test = data;
struct l_genl_msg *msg;
const void *raw;
size_t size;
@@ -206,16 +227,19 @@ static void build_set_rekey_offload(const void *data)
msg = l_genl_msg_new_sized(79, 0);
assert(msg);
- assert(l_genl_msg_append_attr(msg, 3, 4, &index));
+ assert(l_genl_msg_append_attr(msg, 3,
+ sizeof(test->ifindex), &test->ifindex));
assert(l_genl_msg_enter_nested(msg, 122));
- assert(l_genl_msg_append_attr(msg, 1, 16, kek));
- assert(l_genl_msg_append_attr(msg, 2, 16, kck));
- assert(l_genl_msg_append_attr(msg, 3, 8, replay_counter));
+ assert(l_genl_msg_append_attr(msg, 1, sizeof(test->kek), test->kek));
+ assert(l_genl_msg_append_attr(msg, 2, sizeof(test->kck), test->kck));
+ assert(l_genl_msg_append_attr(msg, 3, sizeof(test->replay_counter),
+ test->replay_counter));
assert(l_genl_msg_leave_nested(msg));
- raw = l_genl_msg_to_data(msg, 0x1b, 0x05, 0x53e1a359, 0xe74002ba,
- &size);
+ raw = l_genl_msg_to_data(msg, 0x1b, NLM_F_REQUEST | NLM_F_ACK,
+ test->seq, test->pid, &size);
+
if (do_print) {
l_util_hexdump(false, raw, size, do_debug, "[MSG] ");
l_util_hexdump(true, set_rekey_offload_request, size,
@@ -412,13 +436,13 @@ int main(int argc, char *argv[])
if (!little_endian)
goto done;
- l_test_add("Parse Set Station Request", parse_set_station, NULL);
+ l_test_add("Parse Set Station Request", parse_set_station, &set_station);
l_test_add("Parse Set Rekey Offload Request",
- parse_set_rekey_offload, NULL);
+ parse_set_rekey_offload, &rekey_offload);
- l_test_add("Build Set Station Request", build_set_station, NULL);
+ l_test_add("Build Set Station Request", build_set_station, &set_station);
l_test_add("Build Set Rekey Offload Request",
- build_set_rekey_offload, NULL);
+ build_set_rekey_offload, &rekey_offload);
l_test_add("libnl-generated Example with Nesting",
parse_libnl_nested, NULL);
--
2.45.2
next prev 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 ` Denis Kenzior [this message]
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 ` [PATCH 10/12] unit: Add netlink message builder / parser tests Denis Kenzior
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-5-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