The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
track_tests.c
Go to the documentation of this file.
1/*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/** Tests for the RADIUS/1.1 Token tracking in the rlm_radius tracking table
18 *
19 * @file src/modules/rlm_radius/track_tests.c
20 *
21 * @copyright 2026 Network RADIUS SAS (legal@networkradius.com)
22 */
23
24#include <freeradius-devel/util/test/acutest.h>
25#include <freeradius-devel/util/test/acutest_helpers.h>
26
27#include "track.c"
28
29/*
30 * The tracking table stores the request pointer, and does not read the request.
31 */
32static char dummy_request;
33#define REQUEST ((request_t *) &dummy_request)
34
35/*
36 * The first Token is random, so the tests set the counter to a known value.
37 */
39{
40 radius_track_t *tt = radius_track_alloc(NULL, true);
41
42 TEST_ASSERT(tt != NULL);
43 tt->epoch = epoch;
44 tt->current = current;
45
46 return tt;
47}
48
49/*
50 * In Token mode, radius_track_entry_find() reads the Token from the first four bytes of the vector,
51 * which is where the Token is in the RADIUS/1.1 header after the Code, ID, and Length.
52 */
54{
56
57 fr_nbo_from_uint32(vector, token);
58
59 return radius_track_entry_find(tt, 0, vector);
60}
61
63{
64 *te_p = NULL;
65
66 if (radius_track_entry_reserve(te_p, NULL, tt, REQUEST, 1, NULL) < 0) return NULL;
67
68 return *te_p;
69}
70
71/*
72 * The analyzer does not know that TEST_ASSERT() and the result of TEST_CHECK() mean that the
73 * pointer is not NULL, so the tests return explicitly when the reserve fails.
74 */
75#define RESERVE_OR_RETURN(_tt, _te) \
76do { \
77 if (!track_reserve(_tt, &(_te))) { \
78 TEST_CHECK(!"track_reserve() failed"); \
79 return; \
80 } \
81} while (0)
82
83static void test_token_find(void)
84{
85 radius_track_t *tt = token_table(0, 100);
86 radius_track_entry_t *a = NULL, *b = NULL, *c = NULL;
87
88 TEST_CASE("Tokens come from the counter, and each Token finds its entry");
89 RESERVE_OR_RETURN(tt, a);
90 RESERVE_OR_RETURN(tt, b);
91 RESERVE_OR_RETURN(tt, c);
92
93 TEST_CHECK((a->token) == (100));
94 TEST_CHECK((b->token) == (101));
95 TEST_CHECK((c->token) == (102));
96 TEST_CHECK((a->epoch) == (0));
97
98 TEST_CHECK(token_find(tt, 100) == a);
99 TEST_CHECK(token_find(tt, 101) == b);
100 TEST_CHECK(token_find(tt, 102) == c);
101
102 TEST_CASE("Tokens which were not issued are not found");
103 TEST_CHECK(token_find(tt, 99) == NULL);
104 TEST_CHECK(token_find(tt, 103) == NULL);
105 TEST_CHECK(token_find(tt, 0) == NULL);
106 TEST_CHECK(token_find(tt, UINT32_MAX) == NULL);
107
111 talloc_free(tt);
112}
113
114static void test_token_release(void)
115{
116 radius_track_t *tt = token_table(0, 10);
117 radius_track_entry_t *e[4] = { NULL, NULL, NULL, NULL };
118 int i;
119
120 for (i = 0; i < 4; i++) RESERVE_OR_RETURN(tt, e[i]);
121
122 TEST_CASE("Releasing a middle entry removes only that entry");
124 TEST_CHECK(token_find(tt, 11) == NULL);
125 TEST_CHECK(token_find(tt, 10) == e[0]);
126 TEST_CHECK(token_find(tt, 12) == e[2]);
127 TEST_CHECK(token_find(tt, 13) == e[3]);
128
129 TEST_CASE("Releasing the first entry issued, and then the last entry issued");
132 TEST_CHECK(token_find(tt, 10) == NULL);
133 TEST_CHECK(token_find(tt, 13) == NULL);
134 TEST_CHECK(token_find(tt, 12) == e[2]);
135
137 TEST_CHECK(token_find(tt, 12) == NULL);
138 TEST_CHECK((tt->num_requests) == (0));
139
140 talloc_free(tt);
141}
142
143static void test_token_reuse(void)
144{
145 radius_track_t *tt = token_table(0, 7);
146 radius_track_entry_t *a = NULL, *b = NULL, *released;
147
148 TEST_CASE("A released entry is not found");
149 RESERVE_OR_RETURN(tt, a);
150 TEST_CHECK(token_find(tt, 7) == a);
151
152 released = a;
154 TEST_CHECK(token_find(tt, 7) == NULL);
156
157 TEST_CASE("The next reserve continues the counter, and the new Token finds the entry");
158 RESERVE_OR_RETURN(tt, b);
159 TEST_CHECK((b->token) == (8));
160 TEST_CHECK(token_find(tt, 8) == b);
161 TEST_CHECK(token_find(tt, 7) == NULL);
162
163 TEST_CASE("The released entry is reused, and the free list is empty while the entry is in use");
164 TEST_CHECK(b == released);
166
168 talloc_free(tt);
169}
170
171static void test_token_epoch(void)
172{
173 radius_track_t *tt = token_table(0, UINT32_MAX - 1);
174 radius_track_entry_t *a = NULL, *b = NULL, *c = NULL, *d = NULL;
175
176 TEST_CASE("The epoch increases when the counter wraps to zero");
177 RESERVE_OR_RETURN(tt, a);
178 RESERVE_OR_RETURN(tt, b);
179 RESERVE_OR_RETURN(tt, c);
180
181 TEST_CHECK((a->token) == (UINT32_MAX - 1));
182 TEST_CHECK((b->token) == (UINT32_MAX));
183 TEST_CHECK((c->token) == (0));
184
185 TEST_CHECK((a->epoch) == (0));
186 TEST_CHECK((b->epoch) == (0));
187 TEST_CHECK((c->epoch) == (1));
188 TEST_CHECK((tt->epoch) == (1));
189
190 TEST_CASE("Entries from the previous epoch are still found");
191 TEST_CHECK(token_find(tt, UINT32_MAX - 1) == a);
192 TEST_CHECK(token_find(tt, UINT32_MAX) == b);
193 TEST_CHECK(token_find(tt, 0) == c);
194
195 TEST_CASE("Tokens which were not issued are not found across the wrap");
196 TEST_CHECK(token_find(tt, UINT32_MAX - 2) == NULL);
197 TEST_CHECK(token_find(tt, 1) == NULL);
198
199 TEST_CASE("Releasing the entries from the previous epoch");
202 TEST_CHECK(token_find(tt, UINT32_MAX - 1) == NULL);
203 TEST_CHECK(token_find(tt, UINT32_MAX) == NULL);
204 TEST_CHECK(token_find(tt, 0) == c);
205
206 TEST_CASE("The counter continues in the new epoch");
207 RESERVE_OR_RETURN(tt, d);
208 TEST_CHECK((d->token) == (1));
209 TEST_CHECK((d->epoch) == (1));
210 TEST_CHECK(token_find(tt, 1) == d);
211
214 talloc_free(tt);
215}
216
217static void test_token_many(void)
218{
219 radius_track_t *tt = token_table(0, 1000);
220 radius_track_entry_t *e[1024] = {};
221 int i;
222
223 TEST_CASE("A table in Token mode is not limited to 256 outstanding entries");
224 for (i = 0; i < 1024; i++) RESERVE_OR_RETURN(tt, e[i]);
225
226 TEST_CHECK((tt->num_requests) == (1024));
227
228 for (i = 0; i < 1024; i++) {
229 TEST_CHECK(token_find(tt, 1000 + i) == e[i]);
230 }
231
232 TEST_CASE("Releasing in the order of issue, and then reserving again");
233 for (i = 0; i < 1024; i++) radius_track_entry_release(&e[i]);
234 TEST_CHECK((tt->num_requests) == (0));
235
236 for (i = 0; i < 1024; i++) {
237 RESERVE_OR_RETURN(tt, e[i]);
238 TEST_CHECK(token_find(tt, e[i]->token) == e[i]);
239 }
240
241 for (i = 0; i < 1024; i++) radius_track_entry_release(&e[i]);
242
243 talloc_free(tt);
244}
245
246static void test_binding(void)
247{
248 radius_track_t *tt = token_table(0, 5);
249 radius_track_entry_t *a = NULL, *b = NULL;
250 TALLOC_CTX *ctx = talloc_new(NULL);
251
252 TEST_CASE("An entry which is released, and then reserved without a ctx, has no binding");
253 TEST_ASSERT(radius_track_entry_reserve(&a, ctx, tt, REQUEST, 1, NULL) == 0);
254 TEST_CHECK(a->binding != NULL);
256 TEST_CHECK(a == NULL);
257
258 RESERVE_OR_RETURN(tt, b);
259 TEST_CHECK(b->binding == NULL);
261
262 talloc_free(ctx);
263 talloc_free(tt);
264}
265
266static void test_update(void)
267{
268 radius_track_t *tt;
269 radius_track_entry_t *a = NULL;
271 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
272 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
274
275 TEST_CASE("In ID mode, the vector is stored, and find matches on the ID and ignores the vector");
276 tt = radius_track_alloc(NULL, false);
277 RESERVE_OR_RETURN(tt, a);
278 TEST_CHECK(radius_track_entry_update(a, vector) == 0);
279 TEST_CHECK(memcmp(a->vector, vector, sizeof(vector)) == 0);
280
281 TEST_CHECK(radius_track_entry_find(tt, a->id, vector) == a);
282 TEST_CHECK(radius_track_entry_find(tt, a->id, other) == a);
283 TEST_CHECK(radius_track_entry_find(tt, a->id + 1, vector) == NULL);
284
286 talloc_free(tt);
287
288 TEST_CASE("In Token mode, the vector is not stored, and the Token and epoch are not changed");
289 tt = token_table(3, 200);
290 RESERVE_OR_RETURN(tt, a);
291 TEST_CHECK(radius_track_entry_update(a, vector) == 0);
292 TEST_CHECK((a->token) == (200));
293 TEST_CHECK((a->epoch) == (3));
294 TEST_CHECK(token_find(tt, 200) == a);
295
297 talloc_free(tt);
298}
299
300static void test_id_mode(void)
301{
302 radius_track_t *tt = radius_track_alloc(NULL, false);
303 radius_track_entry_t *a = NULL;
305
306 TEST_CASE("A table which is not in Token mode finds entries by ID");
307
308 RESERVE_OR_RETURN(tt, a);
309
310 TEST_CHECK(a == &tt->id[a->id]);
311
312 /*
313 * In ID mode, the vector is ignored. The vector is not NULL, because the analyzer forgets that the
314 * table is in ID mode after the reserve, and follows the Token mode path of
315 * radius_track_entry_find(), which reads the Token from the vector.
316 */
317 TEST_CHECK(radius_track_entry_find(tt, a->id, vector) == a);
318
320 TEST_CHECK(radius_track_entry_find(tt, 0, vector) == NULL);
321
322 talloc_free(tt);
323}
324
326 { "token_find", test_token_find },
327 { "token_release", test_token_release },
328 { "token_reuse", test_token_reuse },
329 { "token_epoch", test_token_epoch },
330 { "token_many", test_token_many },
331 { "binding", test_binding },
332 { "update", test_update },
333 { "id_mode", test_id_mode },
334
336};
#define TEST_CHECK(cond)
Definition acutest.h:101
#define TEST_CASE(name)
Definition acutest.h:203
#define TEST_ASSERT(cond)
Definition acutest.h:125
#define TEST_TERMINATOR
Definition acutest.h:77
static unsigned int fr_dlist_num_elements(fr_dlist_head_t const *head)
Return the number of elements in the dlist.
Definition dlist.h:921
talloc_free(hp)
unsigned int uint32_t
unsigned char uint8_t
static void fr_nbo_from_uint32(uint8_t out[static sizeof(uint32_t)], uint32_t num)
Write out an unsigned 32bit integer in wire format (big endian)
Definition nbo.h:61
#define RADIUS_AUTH_VECTOR_LENGTH
Definition net.h:89
Tracking RADUS client packets.
int radius_track_entry_update(radius_track_entry_t *te, uint8_t const *vector)
Update a tracking entry with the authentication vector.
Definition track.c:271
radius_track_t * radius_track_alloc(TALLOC_CTX *ctx, bool radius_1_1)
Create an radius_track_t.
Definition track.c:52
radius_track_entry_t * radius_track_entry_find(radius_track_t *tt, uint8_t packet_id, uint8_t const *vector)
Find a tracking entry from a request authenticator.
Definition track.c:344
fr_dlist_head_t free_list
so we allocate by least recently used
Definition track.h:72
#define radius_track_entry_release(_te)
Definition track.h:104
uint8_t id
our ID
Definition track.h:51
unsigned int num_requests
number of requests in the allocation
Definition track.h:70
#define radius_track_entry_reserve(_te_out, _ctx, _tt, _request, _code, _uctx)
Definition track.h:96
radius_track_entry_t *** binding
Binding chunk we use to release the entry when its parent is freed.
Definition track.h:42
Track one request to a response.
Definition track.h:37
static char dummy_request
Definition track_tests.c:32
TEST_LIST
static void test_update(void)
static void test_id_mode(void)
static void test_token_many(void)
static void test_token_find(void)
Definition track_tests.c:83
#define REQUEST
Definition track_tests.c:33
static radius_track_entry_t * token_find(radius_track_t *tt, uint32_t token)
Definition track_tests.c:53
static void test_token_release(void)
#define RESERVE_OR_RETURN(_tt, _te)
Definition track_tests.c:75
static void test_binding(void)
static void test_token_epoch(void)
static radius_track_entry_t * track_reserve(radius_track_t *tt, radius_track_entry_t **te_p)
Definition track_tests.c:62
static radius_track_t * token_table(uint32_t epoch, uint32_t current)
Definition track_tests.c:38
static void test_token_reuse(void)