test-pmksa.c (3776B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2023 Cruise LLC. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 * 21 */ 22 23 #ifdef HAVE_CONFIG_H 24 #include <config.h> 25 #endif 26 27 #include <stdio.h> 28 #include <string.h> 29 #include <assert.h> 30 #include <ell/ell.h> 31 32 #include "src/module.h" 33 #include "src/util.h" 34 #include "src/pmksa.h" 35 36 static bool verbose = false; 37 static struct l_settings *config; 38 extern struct iwd_module_desc __start___iwd_module[]; 39 extern struct iwd_module_desc __stop___iwd_module[]; 40 /* There's a single module compiled in, so it will be the pmksa one */ 41 static struct iwd_module_desc *pmksa = __start___iwd_module; 42 43 static void print_cache() 44 { 45 uint32_t used; 46 struct pmksa **entries = __pmksa_cache_get_all(&used); 47 uint32_t i; 48 49 for (i = 0; i < used; i++) { 50 struct pmksa *pmksa = entries[i]; 51 52 fprintf(stderr, "%02u aa "MAC" spa "MAC" expiration: %" 53 PRIu64"\n", i, 54 MAC_STR(pmksa->aa), MAC_STR(pmksa->spa), 55 pmksa->expiration); 56 } 57 } 58 59 static struct pmksa *make_pmksa() 60 { 61 static uint32_t counter = 0xabcdef00; 62 struct pmksa *pmksa = l_new(struct pmksa, 1); 63 64 memcpy(pmksa->aa, &counter, sizeof(counter)); 65 counter += 1; 66 memcpy(pmksa->spa, &counter, sizeof(counter)); 67 counter += 1; 68 69 pmksa->ssid_len = 6; 70 pmksa->ssid[0] = 'F'; 71 pmksa->ssid[1] = 'o'; 72 pmksa->ssid[2] = 'o'; 73 pmksa->ssid[3] = 'b'; 74 pmksa->ssid[4] = 'a'; 75 pmksa->ssid[5] = 'r'; 76 77 pmksa->akm = 0x4; 78 79 return pmksa; 80 } 81 82 static void test_pmksa(const void *data) 83 { 84 struct pmksa *p; 85 struct pmksa **entries; 86 uint32_t used; 87 88 config = l_settings_new(); 89 l_settings_set_uint(config, "PMKSA", "Capacity", 7); 90 __pmksa_set_config(config); 91 pmksa->init(); 92 93 p = make_pmksa(); 94 p->expiration = 20; 95 assert(!pmksa_cache_put(p)); 96 97 p = make_pmksa(); 98 p->expiration = 15; 99 assert(!pmksa_cache_put(p)); 100 101 p = make_pmksa(); 102 p->expiration = 32; 103 assert(!pmksa_cache_put(p)); 104 105 p = make_pmksa(); 106 p->expiration = 48; 107 assert(!pmksa_cache_put(p)); 108 109 p = make_pmksa(); 110 p->expiration = 102; 111 assert(!pmksa_cache_put(p)); 112 113 p = make_pmksa(); 114 p->expiration = 55; 115 assert(!pmksa_cache_put(p)); 116 117 p = make_pmksa(); 118 p->expiration = 41; 119 assert(!pmksa_cache_put(p)); 120 121 p = make_pmksa(); 122 p->expiration = 66; 123 assert(!pmksa_cache_put(p)); 124 125 if (verbose) 126 print_cache(); 127 128 entries = __pmksa_cache_get_all(&used); 129 assert(used == 7); 130 assert(entries[0]->expiration == 20); 131 132 /* Reverse spa and aa */ 133 p = pmksa_cache_get(entries[0]->aa, entries[0]->spa, 134 entries[0]->ssid, entries[0]->ssid_len, 135 0xff); 136 assert(!p); 137 138 p = pmksa_cache_get(entries[0]->spa, entries[0]->aa, 139 entries[0]->ssid, entries[0]->ssid_len, 140 0xff); 141 assert(p); 142 l_free(p); 143 144 entries = __pmksa_cache_get_all(&used); 145 assert(used == 6); 146 assert(entries[0]->expiration == 32); 147 148 assert(pmksa_cache_expire(48) == 3); 149 entries = __pmksa_cache_get_all(&used); 150 assert(used == 3); 151 assert(entries[0]->expiration == 55); 152 153 pmksa->exit(); 154 l_settings_free(config); 155 } 156 157 int main(int argc, char *argv[]) 158 { 159 l_test_init(&argc, &argv); 160 161 l_test_add("PMKSA/basics", test_pmksa, NULL); 162 163 return l_test_run(); 164 }