From 980e132f48f5af41832df732e7dfd6017687781d Mon Sep 17 00:00:00 2001 From: Denis Kenzior Date: Fri, 22 Nov 2024 07:15:43 -0800 Subject: [PATCH] unit: Add basic pmksa test --- .gitignore | 1 + Makefile.am | 7 +- unit/test-pmksa.c | 164 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 unit/test-pmksa.c diff --git a/.gitignore b/.gitignore index 8af48c16..5fb9145e 100644 --- a/.gitignore +++ b/.gitignore @@ -67,6 +67,7 @@ unit/test-band unit/test-dpp unit/test-json unit/test-nl80211util +unit/test-pmksa unit/cert-*.pem unit/cert-*.csr unit/cert-*.srl diff --git a/Makefile.am b/Makefile.am index 7805ed26..598b8f90 100644 --- a/Makefile.am +++ b/Makefile.am @@ -438,7 +438,8 @@ 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-nl80211util + unit/test-dpp unit/test-json unit/test-nl80211util \ + unit/test-pmksa endif if CLIENT @@ -594,6 +595,10 @@ unit_test_nl80211util_SOURCES = unit/test-nl80211util.c \ src/ie.h src/ie.c \ src/util.h src/util.c unit_test_nl80211util_LDADD = $(ell_ldadd) + +unit_test_pmksa_SOURCES = unit/test-pmksa.c src/pmksa.c src/pmksa.h \ + src/module.h src/util.h +unit_test_pmksa_LDADD = $(ell_ldadd) endif if CLIENT diff --git a/unit/test-pmksa.c b/unit/test-pmksa.c new file mode 100644 index 00000000..4c7111aa --- /dev/null +++ b/unit/test-pmksa.c @@ -0,0 +1,164 @@ +/* + * + * Wireless daemon for Linux + * + * Copyright (C) 2023 Cruise LLC. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include + +#include "src/module.h" +#include "src/util.h" +#include "src/pmksa.h" + +static bool verbose = false; +static struct l_settings *config; +extern struct iwd_module_desc __start___iwd_module[]; +extern struct iwd_module_desc __stop___iwd_module[]; +/* There's a single module compiled in, so it will be the pmksa one */ +static struct iwd_module_desc *pmksa = __start___iwd_module; + +static void print_cache() +{ + uint32_t used; + struct pmksa **entries = __pmksa_cache_get_all(&used); + uint32_t i; + + for (i = 0; i < used; i++) { + struct pmksa *pmksa = entries[i]; + + fprintf(stderr, "%02u aa "MAC" spa "MAC" expiration: %" + PRIu64"\n", i, + MAC_STR(pmksa->aa), MAC_STR(pmksa->spa), + pmksa->expiration); + } +} + +static struct pmksa *make_pmksa() +{ + static uint32_t counter = 0xabcdef00; + struct pmksa *pmksa = l_new(struct pmksa, 1); + + memcpy(pmksa->aa, &counter, sizeof(counter)); + counter += 1; + memcpy(pmksa->spa, &counter, sizeof(counter)); + counter += 1; + + pmksa->ssid_len = 6; + pmksa->ssid[0] = 'F'; + pmksa->ssid[1] = 'o'; + pmksa->ssid[2] = 'o'; + pmksa->ssid[3] = 'b'; + pmksa->ssid[4] = 'a'; + pmksa->ssid[5] = 'r'; + + pmksa->akm = 0x4; + + return pmksa; +} + +static void test_pmksa(const void *data) +{ + struct pmksa *p; + struct pmksa **entries; + uint32_t used; + + config = l_settings_new(); + l_settings_set_uint(config, "PMKSA", "Capacity", 7); + __pmksa_set_config(config); + pmksa->init(); + + p = make_pmksa(); + p->expiration = 20; + assert(!pmksa_cache_put(p)); + + p = make_pmksa(); + p->expiration = 15; + assert(!pmksa_cache_put(p)); + + p = make_pmksa(); + p->expiration = 32; + assert(!pmksa_cache_put(p)); + + p = make_pmksa(); + p->expiration = 48; + assert(!pmksa_cache_put(p)); + + p = make_pmksa(); + p->expiration = 102; + assert(!pmksa_cache_put(p)); + + p = make_pmksa(); + p->expiration = 55; + assert(!pmksa_cache_put(p)); + + p = make_pmksa(); + p->expiration = 41; + assert(!pmksa_cache_put(p)); + + p = make_pmksa(); + p->expiration = 66; + assert(!pmksa_cache_put(p)); + + if (verbose) + print_cache(); + + entries = __pmksa_cache_get_all(&used); + assert(used == 7); + assert(entries[0]->expiration == 20); + + /* Reverse spa and aa */ + p = pmksa_cache_get(entries[0]->aa, entries[0]->spa, + entries[0]->ssid, entries[0]->ssid_len, + 0xff); + assert(!p); + + p = pmksa_cache_get(entries[0]->spa, entries[0]->aa, + entries[0]->ssid, entries[0]->ssid_len, + 0xff); + assert(p); + l_free(p); + + entries = __pmksa_cache_get_all(&used); + assert(used == 6); + assert(entries[0]->expiration == 32); + + assert(pmksa_cache_expire(48) == 3); + entries = __pmksa_cache_get_all(&used); + assert(used == 3); + assert(entries[0]->expiration == 55); + + pmksa->exit(); + l_settings_free(config); +} + +int main(int argc, char *argv[]) +{ + l_test_init(&argc, &argv); + + l_test_add("PMKSA/basics", test_pmksa, NULL); + + return l_test_run(); +}