diff mbox series

[07/10] unit: add simple test for nl80211util

Message ID 20240618165854.113598-7-prestwoj@gmail.com (mailing list archive)
State Accepted, archived
Headers show
Series [01/10] netdev: downgrade L_WARN_ON for ensure_eapol_registered | expand

Checks

Context Check Description
tedd_an/pre-ci_am success Success
prestwoj/iwd-ci-gitlint success GitLint

Commit Message

James Prestwood June 18, 2024, 4:58 p.m. UTC
---
 Makefile.am             |  9 +++++-
 unit/test-nl80211util.c | 63 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 71 insertions(+), 1 deletion(-)
 create mode 100644 unit/test-nl80211util.c
diff mbox series

Patch

diff --git a/Makefile.am b/Makefile.am
index 0ef4f965..0c152216 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -433,7 +433,7 @@  unit_tests += unit/test-cmac-aes \
 		unit/test-ie unit/test-util unit/test-ssid-security \
 		unit/test-arc4 unit/test-wsc unit/test-eap-mschapv2 \
 		unit/test-eap-sim unit/test-sae unit/test-p2p unit/test-band \
-		unit/test-dpp unit/test-json
+		unit/test-dpp unit/test-json unit/test-nl80211util
 endif
 
 if CLIENT
@@ -582,6 +582,13 @@  unit_test_dpp_LDADD = $(ell_ldadd)
 
 unit_test_json_SOURCES = unit/test-json.c src/json.h src/json.c shared/jsmn.h
 unit_test_json_LDADD = $(ell_ldadd)
+
+unit_test_nl80211util_SOURCES = unit/test-nl80211util.c \
+				src/nl80211util.h src/nl80211util.c \
+				src/band.h src/band.c \
+				src/ie.h src/ie.c \
+				src/util.h src/util.c
+unit_test_nl80211util_LDADD = $(ell_ldadd)
 endif
 
 if CLIENT
diff --git a/unit/test-nl80211util.c b/unit/test-nl80211util.c
new file mode 100644
index 00000000..34a7c2a4
--- /dev/null
+++ b/unit/test-nl80211util.c
@@ -0,0 +1,63 @@ 
+
+#include <assert.h>
+#include <ell/ell.h>
+#include "src/nl80211util.h"
+#include "linux/nl80211.h"
+
+static void test_parse_attrs(const void *data)
+{
+	struct l_genl_msg *msg = l_genl_msg_new(NL80211_CMD_NEW_INTERFACE);
+	uint32_t ifindex = 1;
+	uint32_t freq = 2;
+	int ret;
+
+	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &ifindex);
+	l_genl_msg_append_attr(msg, NL80211_ATTR_WIPHY_FREQ, 4, &freq);
+
+	ret = nl80211_parse_attrs(msg, NL80211_ATTR_IFINDEX, &ifindex,
+					NL80211_ATTR_WIPHY_FREQ, &freq,
+					NL80211_ATTR_UNSPEC);
+	l_genl_msg_unref(msg);
+	assert(ret == 0);
+}
+
+static void test_parse_nested(const void *data)
+{
+	struct l_genl_msg *msg = l_genl_msg_new(NL80211_CMD_NEW_INTERFACE);
+	uint32_t ifindex = 1;
+	uint32_t freq = 2;
+	uint8_t noise = 3;
+	uint8_t noise_out;
+	struct l_genl_attr nested;
+	int ret;
+	int ret_nested;
+
+	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &ifindex);
+	l_genl_msg_append_attr(msg, NL80211_ATTR_WIPHY_FREQ, 4, &freq);
+
+	l_genl_msg_enter_nested(msg, NL80211_ATTR_SURVEY_INFO);
+	l_genl_msg_append_attr(msg, NL80211_SURVEY_INFO_NOISE, 1, &noise);
+	l_genl_msg_leave_nested(msg);
+
+	ret = nl80211_parse_attrs(msg, NL80211_ATTR_IFINDEX, &ifindex,
+					NL80211_ATTR_WIPHY_FREQ, &freq,
+					NL80211_ATTR_SURVEY_INFO, &nested,
+					NL80211_ATTR_UNSPEC);
+	ret_nested = nl80211_parse_nested(&nested, NL80211_ATTR_SURVEY_INFO,
+					NL80211_SURVEY_INFO_NOISE, &noise_out,
+					NL80211_ATTR_UNSPEC);
+	l_genl_msg_unref(msg);
+	assert(ret == 0);
+	assert(ret_nested == 0);
+	assert(noise_out == noise);
+}
+
+int main(int argc, char *argv[])
+{
+	l_test_init(&argc, &argv);
+
+	l_test_add("/nl80211util parse attrs", test_parse_attrs, NULL);
+	l_test_add("/nl80211util parse nested", test_parse_nested, NULL);
+
+	return l_test_run();
+}