public inbox for iwd@lists.linux.dev
 help / color / mirror / Atom feed
From: James Prestwood <prestwoj@gmail.com>
To: iwd@lists.linux.dev
Cc: Denis Kenzior <denkenz@gmail.com>
Subject: [PATCH 07/15] unit: Add basic pmksa test
Date: Fri, 22 Nov 2024 07:15:43 -0800	[thread overview]
Message-ID: <20241122151551.286355-8-prestwoj@gmail.com> (raw)
In-Reply-To: <20241122151551.286355-1-prestwoj@gmail.com>

From: Denis Kenzior <denkenz@gmail.com>

---
 .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 <config.h>
+#endif
+
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <ell/ell.h>
+
+#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();
+}
-- 
2.34.1


  parent reply	other threads:[~2024-11-22 15:16 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-22 15:15 [PATCH 00/15] PMKSA support (SAE only) James Prestwood
2024-11-22 15:15 ` [PATCH 01/15] handshake: add ref counting to handshake_state James Prestwood
2024-11-22 15:15 ` [PATCH 02/15] unit: update use of handshake_state with ref/unref James Prestwood
2024-11-22 15:15 ` [PATCH 03/15] auto-t: always initialize StationDebug in Device class James Prestwood
2024-11-22 15:15 ` [PATCH 04/15] auto-t: add pmksa_flush() to hostapd module James Prestwood
2024-11-22 15:15 ` [PATCH 05/15] auto-t: update testSAE to disable PMKSA James Prestwood
2024-11-22 15:15 ` [PATCH 06/15] pmksa: Add skeleton James Prestwood
2024-11-22 15:15 ` James Prestwood [this message]
2024-11-22 15:15 ` [PATCH 08/15] pmksa: Add debugging James Prestwood
2024-11-22 15:15 ` [PATCH 09/15] handshake: Add pmksa setter & stealer James Prestwood
2024-11-25 14:56   ` Denis Kenzior
2024-11-25 15:01     ` James Prestwood
2024-11-25 19:25       ` Bryce Johnson
2024-11-25 19:49         ` James Prestwood
2024-11-25 20:18           ` Bryce Johnson
2024-11-22 15:15 ` [PATCH 10/15] handshake: add handshake_state_remove_pmksa James Prestwood
2024-11-22 15:15 ` [PATCH 11/15] netdev: add support to use PMKSA over SAE if available James Prestwood
2024-11-22 15:15 ` [PATCH 12/15] station: hold reference to handshake object James Prestwood
2024-11-22 15:15 ` [PATCH 13/15] station: support PMKSA connections James Prestwood
2024-11-22 15:15 ` [PATCH 14/15] auto-t: add PMKSA tests James Prestwood
2024-11-22 15:15 ` [PATCH 15/15] doc: document DisablePMKSA option James Prestwood

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=20241122151551.286355-8-prestwoj@gmail.com \
    --to=prestwoj@gmail.com \
    --cc=denkenz@gmail.com \
    --cc=iwd@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