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
36#ifdef HAVE_GPERFTOOLS_PROFILER_H
37# include <gperftools/profiler.h>
38#endif
39
40static TALLOC_CTX *autofree;
43
44
45/** Global initialisation
46 */
47static void test_init(void)
48{
50 if (!autofree) {
51 error:
52 fr_perror("pair_tests");
53 fr_exit_now(EXIT_FAILURE);
54 }
55
56 /*
57 * Mismatch between the binary and the libraries it depends on
58 */
59 if (fr_check_lib_magic(RADIUSD_MAGIC_NUMBER) < 0) goto error;
60
61 if (fr_dict_test_init(autofree, &test_dict, NULL) < 0) goto error;
62
63 /* Initialize the "test_pairs" list */
65
66 if (fr_pair_test_list_alloc(autofree, &test_pairs, NULL) < 0) goto error;
67
68 if (request_global_init() < 0) goto error;
69}
70
72{
73 request_t *request;
74
75 /*
76 * Create and initialize the new request.
77 */
79
80 request->packet = fr_packet_alloc(request, false);
81 TEST_CHECK(request->packet != NULL);
82
83 request->reply = fr_packet_alloc(request, false);
84 TEST_CHECK(request->reply != NULL);
85
86 return request;
87}
88
89/*
90 * Tests functions
91 */
92static void test_pair_append_request(void)
93{
94 fr_pair_t *local_vp;
96 request_t *request = request_fake_alloc();
97
98 TEST_CASE("Add 'Test-Integer' in 'request_pairs' using pair_append_request()");
100
101 TEST_CASE("Validating PAIR_VERIFY()");
102 TEST_CHECK((vp = fr_pair_list_head(&request->request_pairs)) != NULL);
104
105 TEST_MSG("Set vp = 12345");
106 vp->vp_uint32 = 12345;
107 TEST_CHECK(vp->vp_uint32 == 12345);
108 TEST_MSG("Expected %s == 12345", fr_dict_attr_test_uint32->name);
109
110 TEST_CHECK_RET(talloc_free(request), 0);
111}
112
113static void test_pair_append_reply(void)
114{
115 fr_pair_t *local_vp;
116 fr_pair_t *vp;
117 request_t *request = request_fake_alloc();
118
119 TEST_CASE("Add 'Test-Integer' in 'reply_pairs' using pair_append_reply()");
121
122 TEST_CASE("Validating PAIR_VERIFY()");
123 TEST_CHECK((vp = fr_pair_list_head(&request->reply_pairs)) != NULL);
125
126 TEST_MSG("Set vp = 12345");
127 vp->vp_uint32 = 12345;
128
129 TEST_CHECK(vp->vp_uint32 == 12345);
130 TEST_MSG("Expected %s == 12345", fr_dict_attr_test_uint32->name);
131
132 TEST_CHECK_RET(talloc_free(request), 0);
133}
134
136{
137 fr_pair_t *local_vp;
138 fr_pair_t *vp;
139 request_t *request = request_fake_alloc();
140
141 TEST_CASE("Add 'Test-Integer' in 'control_pairs' using pair_append_control()");
143
144 TEST_CASE("Validating PAIR_VERIFY()");
145 TEST_CHECK((vp = fr_pair_list_head(&request->control_pairs)) != NULL);
147
148 TEST_MSG("Set vp = 12345");
149 vp->vp_uint32 = 12345;
150
151 TEST_CHECK(vp->vp_uint32 == 12345);
152 TEST_MSG("Expected %s == 12345", fr_dict_attr_test_uint32->name);
153
154 TEST_CHECK_RET(talloc_free(request), 0);
155}
156
158{
159 fr_pair_t *local_vp;
160 fr_pair_t *vp;
161 request_t *request = request_fake_alloc();
162
163 TEST_CASE("Add 'Test-Integer' in 'control_pairs' using pair_append_session_state()");
165
166 TEST_CASE("Validating PAIR_VERIFY()");
167 TEST_CHECK((vp = fr_pair_list_head(&request->session_state_pairs)) != NULL);
169
170 TEST_MSG("Set vp = 12345");
171 vp->vp_uint32 = 12345;
172
173 TEST_CHECK(vp->vp_uint32 == 12345);
174 TEST_MSG("Expected %s == 12345", fr_dict_attr_test_uint32->name);
175
176 TEST_CHECK_RET(talloc_free(request), 0);
177}
178
180{
181 fr_pair_t *vp;
182 request_t *request = request_fake_alloc();
183
184 TEST_CASE("Update 'Test-Integer' in 'request_pairs' using pair_update_request()");
186
187 TEST_CASE("Validating PAIR_VERIFY()");
189
190 TEST_MSG("Set vp = 112233");
191 vp->vp_uint32 = 112233;
192
193 TEST_CASE("Expected fr_dict_attr_test_uint32 (vp->vp_uint32 == 112233)");
194 TEST_CHECK((vp = fr_pair_find_by_da(&request->request_pairs, NULL, fr_dict_attr_test_uint32)) != NULL);
195
196 TEST_CASE("Validating PAIR_VERIFY()");
198
199 TEST_MSG("Checking if vp == 12345");
200 /*
201 * Such 'vp != NULL' just to mute clang "warning: Dereference of null pointer"
202 */
203 TEST_CHECK(vp && vp->vp_uint32 == 112233);
204
205 TEST_CHECK_RET(talloc_free(request), 0);
206}
207
208static void test_pair_update_reply(void)
209{
210 fr_pair_t *vp;
211 request_t *request = request_fake_alloc();
212
213 TEST_CASE("Update 'Test-Integer' in 'reply_pairs' using pair_update_request()");
215
216 TEST_CASE("Validating PAIR_VERIFY()");
218
219 TEST_MSG("Set vp = 3333");
220 vp->vp_uint32 = 3333;
221
222 TEST_CASE("Expected fr_dict_attr_test_uint32 (vp->vp_uint32 == 3333)");
223 TEST_CHECK((vp = fr_pair_find_by_da(&request->reply_pairs, NULL, fr_dict_attr_test_uint32)) != NULL);
224
225 TEST_CASE("Validating PAIR_VERIFY()");
227
228 TEST_CHECK(vp && vp->vp_uint32 == 3333);
229
230 TEST_CHECK_RET(talloc_free(request), 0);
231}
232
234{
235 fr_pair_t *vp;
236 request_t *request = request_fake_alloc();
237
238 TEST_CASE("Update 'Test-Integer' in 'control_pairs' using pair_update_control()");
240
241 TEST_CASE("Validating PAIR_VERIFY()");
243
244 TEST_MSG("Set vp = 44444");
245 vp->vp_uint32 = 44444;
246
247 TEST_CASE("Expected fr_dict_attr_test_uint32 (vp->vp_uint32 == 44444)");
248 TEST_CHECK((vp = fr_pair_find_by_da(&request->control_pairs, NULL, fr_dict_attr_test_uint32)) != NULL);
249
250 TEST_CASE("Validating PAIR_VERIFY()");
252
253 TEST_CHECK(vp && vp->vp_uint32 == 44444);
254
255 TEST_CHECK_RET(talloc_free(request), 0);
256}
257
259{
260 fr_pair_t *vp;
261 request_t *request = request_fake_alloc();
262
263 TEST_CASE("Update 'Test-Integer' in 'state' using pair_update_session_state()");
265
266 TEST_CASE("Validating PAIR_VERIFY()");
268
269 TEST_MSG("Set vp = 7890");
270 vp->vp_uint32 = 7890;
271
272 TEST_CASE("Expected fr_dict_attr_test_uint32 (vp->vp_uint32 == 7890)");
273 TEST_CHECK((vp = fr_pair_find_by_da(&request->session_state_pairs, NULL, fr_dict_attr_test_uint32)) != NULL);
274
275 TEST_CASE("Validating PAIR_VERIFY()");
277
278 TEST_CHECK(vp && vp->vp_uint32 == 7890);
279
280 TEST_CHECK_RET(talloc_free(request), 0);
281}
282
284{
285 request_t *request = request_fake_alloc();
286
287 TEST_CASE("Copy 'test_pairs' into 'request->request_pairs'");
288 TEST_CHECK(fr_pair_list_copy(autofree, &request->request_pairs, &test_pairs) > 0);
289
290 TEST_CASE("Delete 'Test-Integer' in 'request->request_pairs' using pair_delete_request()");
292
293 TEST_CASE("The 'Test-Integer' shouldn't exist in 'request->request_pairs'");
294 TEST_CHECK(fr_pair_find_by_da(&request->request_pairs, NULL, fr_dict_attr_test_uint32) == NULL);
295
296 TEST_CHECK_RET(talloc_free(request), 0);
297}
298
299static void test_pair_delete_reply(void)
300{
301 request_t *request = request_fake_alloc();
302
303 TEST_CASE("Copy 'test_pairs' into 'request->reply_pairs'");
304 TEST_CHECK(fr_pair_list_copy(autofree, &request->reply_pairs, &test_pairs) > 0);
305
306 TEST_CASE("Delete 'Test-Integer' in 'request->reply_pairs' using pair_delete_reply()");
308
309 TEST_CASE("The 'Test-Integer' shouldn't exist in 'request->reply_pairs'");
310 TEST_CHECK(fr_pair_find_by_da(&request->reply_pairs, NULL, fr_dict_attr_test_uint32) == NULL);
311
312 TEST_CHECK_RET(talloc_free(request), 0);
313}
314
316{
317 request_t *request = request_fake_alloc();
318
319 TEST_CASE("Copy 'test_pairs' into 'request->control_pairs'");
320 TEST_CHECK(fr_pair_list_copy(autofree, &request->control_pairs, &test_pairs) > 0);
321
322 TEST_CASE("Delete 'Test-Integer' in 'request->control_pairs' using pair_delete_control()");
324
325 TEST_CASE("The 'Test-Integer' shouldn't exist in 'request->control_pairs'");
326 TEST_CHECK(fr_pair_find_by_da(&request->control_pairs, NULL, fr_dict_attr_test_uint32) == NULL);
327
328 TEST_CHECK_RET(talloc_free(request), 0);
329}
330
332{
333 request_t *request = request_fake_alloc();
334
335 TEST_CASE("Copy 'test_pairs' into 'request->state'");
336 TEST_CHECK(fr_pair_list_copy(autofree, &request->session_state_pairs, &test_pairs) > 0);
337
338 TEST_CASE("Delete 'Test-Integer' in 'request->state' using pair_delete_session_state()");
340
341 TEST_CASE("The 'Test-Integer' shouldn't exist in 'request->state'");
342 TEST_CHECK(fr_pair_find_by_da(&request->session_state_pairs, NULL, fr_dict_attr_test_uint32) == NULL);
343
344 TEST_CHECK_RET(talloc_free(request), 0);
345}
346
348 /*
349 * Add pairs
350 */
351 { "pair_append_request", test_pair_append_request },
352 { "pair_append_reply", test_pair_append_reply },
353 { "pair_append_control", test_pair_append_control },
354 { "pair_append_session_state", test_pair_append_session_state },
355
356 /*
357 * Update pairs
358 */
359 { "pair_update_request", test_pair_update_request },
360 { "pair_update_reply", test_pair_update_reply },
361 { "pair_update_control", test_pair_update_control },
362 { "pair_update_session_state", test_pair_update_session_state },
363
364 /*
365 * Delete pairs
366 */
367 { "pair_delete_request", test_pair_delete_request },
368 { "pair_delete_reply", test_pair_delete_reply },
369 { "pair_delete_control", test_pair_delete_control },
370 { "pair_delete_session_state", test_pair_delete_session_state },
371
373};
#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:236
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:335
Optional arguments for initialising requests.
Definition request.h:287
#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:48
#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:732
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