The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
pair_server_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 a AVP manipulation and search API.
18 *
19 * @file src/lib/server/pair_server_tests.c
20 * @author Jorge Pereira <jpereira@freeradius.org>
21 * @copyright 2020 Network RADIUS SAS (legal@networkradius.com)
22 */
23
24static void test_init(void);
25# define TEST_INIT test_init()
26
27#include <freeradius-devel/util/test/acutest.h>
28#include <freeradius-devel/util/test/acutest_helpers.h>
29#include <freeradius-devel/util/test/pair_test_helpers.h>
30
31#include <freeradius-devel/util/conf.h>
32
33#include <freeradius-devel/server/pair.h>
34#include <freeradius-devel/server/request.h>
35
36static TALLOC_CTX *autofree;
39
40
41/** Global initialisation
42 */
43static void test_init(void)
44{
46 if (!autofree) {
47 error:
48 fr_perror("pair_tests");
49 fr_exit_now(EXIT_FAILURE);
50 }
51
52 /*
53 * Mismatch between the binary and the libraries it depends on
54 */
55 if (fr_check_lib_magic(RADIUSD_MAGIC_NUMBER) < 0) goto error;
56
57 if (fr_dict_test_init(autofree, &test_dict, NULL) < 0) goto error;
58
59 /* Initialize the "test_pairs" list */
61
62 if (fr_pair_test_list_alloc(autofree, &test_pairs, NULL) < 0) goto error;
63
64 if (request_global_init() < 0) goto error;
65}
66
68{
69 request_t *request;
70
71 /*
72 * Create and initialize the new request.
73 */
75
76 request->packet = fr_packet_alloc(request, false);
77 TEST_CHECK(request->packet != NULL);
78
79 request->reply = fr_packet_alloc(request, false);
80 TEST_CHECK(request->reply != NULL);
81
82 return request;
83}
84
85/*
86 * Tests functions
87 */
88static void test_pair_append_request(void)
89{
90 fr_pair_t *local_vp;
92 request_t *request = request_fake_alloc();
93
94 TEST_CASE("Add 'Test-Integer' in 'request_pairs' using pair_append_request()");
96
97 TEST_CASE("Validating PAIR_VERIFY()");
98 TEST_CHECK((vp = fr_pair_list_head(&request->request_pairs)) != NULL);
100
101 TEST_MSG("Set vp = 12345");
102 vp->vp_uint32 = 12345;
103 TEST_CHECK(vp->vp_uint32 == 12345);
104 TEST_MSG("Expected %s == 12345", fr_dict_attr_test_uint32->name);
105
106 TEST_CHECK_RET(talloc_free(request), 0);
107}
108
109static void test_pair_append_reply(void)
110{
111 fr_pair_t *local_vp;
112 fr_pair_t *vp;
113 request_t *request = request_fake_alloc();
114
115 TEST_CASE("Add 'Test-Integer' in 'reply_pairs' using pair_append_reply()");
117
118 TEST_CASE("Validating PAIR_VERIFY()");
119 TEST_CHECK((vp = fr_pair_list_head(&request->reply_pairs)) != NULL);
121
122 TEST_MSG("Set vp = 12345");
123 vp->vp_uint32 = 12345;
124
125 TEST_CHECK(vp->vp_uint32 == 12345);
126 TEST_MSG("Expected %s == 12345", fr_dict_attr_test_uint32->name);
127
128 TEST_CHECK_RET(talloc_free(request), 0);
129}
130
132{
133 fr_pair_t *local_vp;
134 fr_pair_t *vp;
135 request_t *request = request_fake_alloc();
136
137 TEST_CASE("Add 'Test-Integer' in 'control_pairs' using pair_append_control()");
139
140 TEST_CASE("Validating PAIR_VERIFY()");
141 TEST_CHECK((vp = fr_pair_list_head(&request->control_pairs)) != NULL);
143
144 TEST_MSG("Set vp = 12345");
145 vp->vp_uint32 = 12345;
146
147 TEST_CHECK(vp->vp_uint32 == 12345);
148 TEST_MSG("Expected %s == 12345", fr_dict_attr_test_uint32->name);
149
150 TEST_CHECK_RET(talloc_free(request), 0);
151}
152
154{
155 fr_pair_t *local_vp;
156 fr_pair_t *vp;
157 request_t *request = request_fake_alloc();
158
159 TEST_CASE("Add 'Test-Integer' in 'control_pairs' using pair_append_session_state()");
161
162 TEST_CASE("Validating PAIR_VERIFY()");
163 TEST_CHECK((vp = fr_pair_list_head(&request->session_state_pairs)) != NULL);
165
166 TEST_MSG("Set vp = 12345");
167 vp->vp_uint32 = 12345;
168
169 TEST_CHECK(vp->vp_uint32 == 12345);
170 TEST_MSG("Expected %s == 12345", fr_dict_attr_test_uint32->name);
171
172 TEST_CHECK_RET(talloc_free(request), 0);
173}
174
176{
177 fr_pair_t *vp;
178 request_t *request = request_fake_alloc();
179
180 TEST_CASE("Update 'Test-Integer' in 'request_pairs' using pair_update_request()");
182
183 TEST_CASE("Validating PAIR_VERIFY()");
185
186 TEST_MSG("Set vp = 112233");
187 vp->vp_uint32 = 112233;
188
189 TEST_CASE("Expected fr_dict_attr_test_uint32 (vp->vp_uint32 == 112233)");
190 TEST_CHECK((vp = fr_pair_find_by_da(&request->request_pairs, NULL, fr_dict_attr_test_uint32)) != NULL);
191
192 TEST_CASE("Validating PAIR_VERIFY()");
194
195 TEST_MSG("Checking if vp == 12345");
196 /*
197 * Such 'vp != NULL' just to mute clang "warning: Dereference of null pointer"
198 */
199 TEST_CHECK(vp && vp->vp_uint32 == 112233);
200
201 TEST_CHECK_RET(talloc_free(request), 0);
202}
203
204static void test_pair_update_reply(void)
205{
206 fr_pair_t *vp;
207 request_t *request = request_fake_alloc();
208
209 TEST_CASE("Update 'Test-Integer' in 'reply_pairs' using pair_update_request()");
211
212 TEST_CASE("Validating PAIR_VERIFY()");
214
215 TEST_MSG("Set vp = 3333");
216 vp->vp_uint32 = 3333;
217
218 TEST_CASE("Expected fr_dict_attr_test_uint32 (vp->vp_uint32 == 3333)");
219 TEST_CHECK((vp = fr_pair_find_by_da(&request->reply_pairs, NULL, fr_dict_attr_test_uint32)) != NULL);
220
221 TEST_CASE("Validating PAIR_VERIFY()");
223
224 TEST_CHECK(vp && vp->vp_uint32 == 3333);
225
226 TEST_CHECK_RET(talloc_free(request), 0);
227}
228
230{
231 fr_pair_t *vp;
232 request_t *request = request_fake_alloc();
233
234 TEST_CASE("Update 'Test-Integer' in 'control_pairs' using pair_update_control()");
236
237 TEST_CASE("Validating PAIR_VERIFY()");
239
240 TEST_MSG("Set vp = 44444");
241 vp->vp_uint32 = 44444;
242
243 TEST_CASE("Expected fr_dict_attr_test_uint32 (vp->vp_uint32 == 44444)");
244 TEST_CHECK((vp = fr_pair_find_by_da(&request->control_pairs, NULL, fr_dict_attr_test_uint32)) != NULL);
245
246 TEST_CASE("Validating PAIR_VERIFY()");
248
249 TEST_CHECK(vp && vp->vp_uint32 == 44444);
250
251 TEST_CHECK_RET(talloc_free(request), 0);
252}
253
255{
256 fr_pair_t *vp;
257 request_t *request = request_fake_alloc();
258
259 TEST_CASE("Update 'Test-Integer' in 'state' using pair_update_session_state()");
261
262 TEST_CASE("Validating PAIR_VERIFY()");
264
265 TEST_MSG("Set vp = 7890");
266 vp->vp_uint32 = 7890;
267
268 TEST_CASE("Expected fr_dict_attr_test_uint32 (vp->vp_uint32 == 7890)");
269 TEST_CHECK((vp = fr_pair_find_by_da(&request->session_state_pairs, NULL, fr_dict_attr_test_uint32)) != NULL);
270
271 TEST_CASE("Validating PAIR_VERIFY()");
273
274 TEST_CHECK(vp && vp->vp_uint32 == 7890);
275
276 TEST_CHECK_RET(talloc_free(request), 0);
277}
278
280{
281 request_t *request = request_fake_alloc();
282
283 TEST_CASE("Copy 'test_pairs' into 'request->request_pairs'");
284 TEST_CHECK(fr_pair_list_copy(autofree, &request->request_pairs, &test_pairs) > 0);
285
286 TEST_CASE("Delete 'Test-Integer' in 'request->request_pairs' using pair_delete_request()");
288
289 TEST_CASE("The 'Test-Integer' shouldn't exist in 'request->request_pairs'");
290 TEST_CHECK(fr_pair_find_by_da(&request->request_pairs, NULL, fr_dict_attr_test_uint32) == NULL);
291
292 TEST_CHECK_RET(talloc_free(request), 0);
293}
294
295static void test_pair_delete_reply(void)
296{
297 request_t *request = request_fake_alloc();
298
299 TEST_CASE("Copy 'test_pairs' into 'request->reply_pairs'");
300 TEST_CHECK(fr_pair_list_copy(autofree, &request->reply_pairs, &test_pairs) > 0);
301
302 TEST_CASE("Delete 'Test-Integer' in 'request->reply_pairs' using pair_delete_reply()");
304
305 TEST_CASE("The 'Test-Integer' shouldn't exist in 'request->reply_pairs'");
306 TEST_CHECK(fr_pair_find_by_da(&request->reply_pairs, NULL, fr_dict_attr_test_uint32) == NULL);
307
308 TEST_CHECK_RET(talloc_free(request), 0);
309}
310
312{
313 request_t *request = request_fake_alloc();
314
315 TEST_CASE("Copy 'test_pairs' into 'request->control_pairs'");
316 TEST_CHECK(fr_pair_list_copy(autofree, &request->control_pairs, &test_pairs) > 0);
317
318 TEST_CASE("Delete 'Test-Integer' in 'request->control_pairs' using pair_delete_control()");
320
321 TEST_CASE("The 'Test-Integer' shouldn't exist in 'request->control_pairs'");
322 TEST_CHECK(fr_pair_find_by_da(&request->control_pairs, NULL, fr_dict_attr_test_uint32) == NULL);
323
324 TEST_CHECK_RET(talloc_free(request), 0);
325}
326
328{
329 request_t *request = request_fake_alloc();
330
331 TEST_CASE("Copy 'test_pairs' into 'request->state'");
332 TEST_CHECK(fr_pair_list_copy(autofree, &request->session_state_pairs, &test_pairs) > 0);
333
334 TEST_CASE("Delete 'Test-Integer' in 'request->state' using pair_delete_session_state()");
336
337 TEST_CASE("The 'Test-Integer' shouldn't exist in 'request->state'");
338 TEST_CHECK(fr_pair_find_by_da(&request->session_state_pairs, NULL, fr_dict_attr_test_uint32) == NULL);
339
340 TEST_CHECK_RET(talloc_free(request), 0);
341}
342
344 /*
345 * Add pairs
346 */
347 { "pair_append_request", test_pair_append_request },
348 { "pair_append_reply", test_pair_append_reply },
349 { "pair_append_control", test_pair_append_control },
350 { "pair_append_session_state", test_pair_append_session_state },
351
352 /*
353 * Update pairs
354 */
355 { "pair_update_request", test_pair_update_request },
356 { "pair_update_reply", test_pair_update_reply },
357 { "pair_update_control", test_pair_update_control },
358 { "pair_update_session_state", test_pair_update_session_state },
359
360 /*
361 * Delete pairs
362 */
363 { "pair_delete_request", test_pair_delete_request },
364 { "pair_delete_reply", test_pair_delete_reply },
365 { "pair_delete_control", test_pair_delete_control },
366 { "pair_delete_session_state", test_pair_delete_session_state },
367
369};
#define TEST_CHECK(cond)
Definition acutest.h:87
#define TEST_CASE(name)
Definition acutest.h:186
#define TEST_TERMINATOR
Definition acutest.h:64
#define TEST_MSG(...)
Definition acutest.h:217
#define TEST_CHECK_RET(_got, _exp)
#define fr_exit_now(_x)
Exit without calling atexit() handlers, producing a log message in debug builds.
Definition debug.h:226
fr_dict_attr_t const * fr_dict_attr_test_uint32
Definition dict_test.c:47
int fr_dict_test_init(TALLOC_CTX *ctx, fr_dict_t **dict_p, fr_dict_test_attr_t const *test_defs)
Initialise a test dictionary and add our test_defs to it.
Definition dict_test.c:248
talloc_free(hp)
fr_packet_t * fr_packet_alloc(TALLOC_CTX *ctx, bool new_vector)
Allocate a new fr_packet_t.
Definition packet.c:38
int fr_pair_list_copy(TALLOC_CTX *ctx, fr_pair_list_t *to, fr_pair_list_t const *from)
Duplicate a list of pairs.
Definition pair.c:2326
fr_pair_t * fr_pair_find_by_da(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find the first pair with a matching da.
Definition pair.c:707
void fr_pair_list_init(fr_pair_list_t *list)
Initialise a pair list header.
Definition pair.c:46
static void test_pair_update_control(void)
static void test_pair_append_request(void)
static void test_pair_append_session_state(void)
static void test_pair_delete_request(void)
static TALLOC_CTX * autofree
static void test_pair_append_control(void)
static request_t * request_fake_alloc(void)
static void test_pair_delete_session_state(void)
static void test_pair_update_request(void)
static void test_pair_delete_control(void)
static fr_pair_list_t test_pairs
static void test_pair_append_reply(void)
static void test_pair_update_session_state(void)
static void test_pair_delete_reply(void)
static fr_dict_t * test_dict
static void test_init(void)
Global initialisation.
static void test_pair_update_reply(void)
static int fr_pair_test_list_alloc(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_test_attr_t const *test_defs)
#define pair_update_request(_attr, _da)
int request_global_init(void)
Definition request.c:596
#define request_local_alloc_external(_ctx, _args)
Allocate a new external request outside of the request pool.
Definition request.h:337
Optional arguments for initialising requests.
Definition request.h:289
#define pair_append_control(_attr, _da)
Allocate and append a fr_pair_t to the control list.
Definition pair.h:57
#define pair_update_reply(_attr, _da)
Return or allocate a fr_pair_t in the reply list.
Definition pair.h:129
#define pair_append_session_state(_attr, _da)
Allocate and append a fr_pair_t to session-state list.
Definition pair.h:67
#define pair_delete_session_state(_pair_or_da)
Delete a fr_pair_t in the session_state list.
Definition pair.h:199
#define pair_append_request(_attr, _da)
Allocate and append a fr_pair_t to the request list.
Definition pair.h:37
#define pair_delete_request(_pair_or_da)
Delete a fr_pair_t in the request list.
Definition pair.h:172
#define pair_update_session_state(_attr, _da)
Return or allocate a fr_pair_t in the session_state list.
Definition pair.h:151
#define pair_delete_reply(_pair_or_da)
Delete a fr_pair_t in the reply list.
Definition pair.h:181
#define pair_append_reply(_attr, _da)
Allocate and append a fr_pair_t to reply list.
Definition pair.h:47
#define pair_delete_control(_pair_or_da)
Delete a fr_pair_t in the control list.
Definition pair.h:190
#define pair_update_control(_attr, _da)
Return or allocate a fr_pair_t in the control list.
Definition pair.h:140
fr_pair_t * vp
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
#define talloc_autofree_context
The original function is deprecated, so replace it with our version.
Definition talloc.h:55
#define PAIR_VERIFY(_x)
Definition pair.h:204
fr_pair_t * fr_pair_list_head(fr_pair_list_t const *list)
Get the head of a valuepair list.
Definition pair_inline.c:42
void fr_perror(char const *fmt,...)
Print the current error to stderr with a prefix.
Definition strerror.c:737
int fr_check_lib_magic(uint64_t magic)
Check if the application linking to the library has the correct magic number.
Definition version.c:40
#define RADIUSD_MAGIC_NUMBER
Definition version.h:81