From 685d1057394a698c67b32418bebac36e0080b714 Mon Sep 17 00:00:00 2001 From: James Prestwood Date: Tue, 18 Jun 2024 09:58:51 -0700 Subject: [PATCH] unit: add simple test for nl80211util --- Makefile.am | 9 +++++- unit/test-nl80211util.c | 63 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 unit/test-nl80211util.c 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 +#include +#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(); +}