File: test-pmksa.c

package info (click to toggle)
iwd 3.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,112 kB
  • sloc: ansic: 140,705; sh: 4,960; makefile: 714
file content (164 lines) | stat: -rw-r--r-- 3,776 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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();
}