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/acutest.h>
28#include <freeradius-devel/util/acutest_helpers.h>
29#include <freeradius-devel/util/pair_test_helpers.h>
30
31#include <freeradius-devel/util/conf.h>
32#include <freeradius-devel/util/dict.h>
33
34#include <freeradius-devel/server/pair.h>
35#include <freeradius-devel/server/request.h>
36
37#ifdef HAVE_GPERFTOOLS_PROFILER_H
38# include <gperftools/profiler.h>
39#endif
40
41static TALLOC_CTX *autofree;
44
45
46/** Global initialisation
47 */
48static void test_init(void)
49{
51 if (!autofree) {
52 error:
53 fr_perror("pair_tests");
54 fr_exit_now(EXIT_FAILURE);
55 }
56
57 /*
58 * Mismatch between the binary and the libraries it depends on
59 */
60 if (fr_check_lib_magic(RADIUSD_MAGIC_NUMBER) < 0) goto error;
61
62 if (fr_dict_test_init(autofree, &test_dict, NULL) < 0) goto error;
63
64 /* Initialize the "test_pairs" list */
66
67 if (fr_pair_test_list_alloc(autofree, &test_pairs, NULL) < 0) goto error;
68
69 if (request_global_init() < 0) goto error;
70}
71
73{
74 request_t *request;
75
76 /*
77 * Create and initialize the new request.
78 */
80
81 request->packet = fr_packet_alloc(request, false);
82 TEST_CHECK(request->packet != NULL);
83
84 request->reply = fr_packet_alloc(request, false);
85 TEST_CHECK(request->reply != NULL);
86
87 return request;
88}
89
90/*
91 * Tests functions
92 */
93static void test_pair_append_request(void)
94{
95 fr_pair_t *local_vp;
97 request_t *request = request_fake_alloc();
98
99 TEST_CASE("Add 'Test-Integer' in 'request_pairs' using pair_append_request()");
101
102 TEST_CASE("Validating PAIR_VERIFY()");
103 TEST_CHECK((vp = fr_pair_list_head(&request->request_pairs)) != NULL);
105
106 TEST_MSG("Set vp = 12345");
107 vp->vp_uint32 = 12345;
108 TEST_CHECK(vp->vp_uint32 == 12345);
109 TEST_MSG("Expected %s == 12345", fr_dict_attr_test_uint32->name);
110
111 TEST_CHECK_RET(talloc_free(request), 0);
112}
113
114static void test_pair_append_reply(void)
115{
116 fr_pair_t *local_vp;
117 fr_pair_t *vp;
118 request_t *request = request_fake_alloc();
119
120 TEST_CASE("Add 'Test-Integer' in 'reply_pairs' using pair_append_reply()");
122
123 TEST_CASE("Validating PAIR_VERIFY()");
124 TEST_CHECK((vp = fr_pair_list_head(&request->reply_pairs)) != NULL);
126
127 TEST_MSG("Set vp = 12345");
128 vp->vp_uint32 = 12345;
129
130 TEST_CHECK(vp->vp_uint32 == 12345);
131 TEST_MSG("Expected %s == 12345", fr_dict_attr_test_uint32->name);
132
133 TEST_CHECK_RET(talloc_free(request), 0);
134}
135
137{
138 fr_pair_t *local_vp;
139 fr_pair_t *vp;
140 request_t *request = request_fake_alloc();
141
142 TEST_CASE("Add 'Test-Integer' in 'control_pairs' using pair_append_control()");
144
145 TEST_CASE("Validating PAIR_VERIFY()");
146 TEST_CHECK((vp = fr_pair_list_head(&request->control_pairs)) != NULL);
148
149 TEST_MSG("Set vp = 12345");
150 vp->vp_uint32 = 12345;
151
152 TEST_CHECK(vp->vp_uint32 == 12345);
153 TEST_MSG("Expected %s == 12345", fr_dict_attr_test_uint32->name);
154
155 TEST_CHECK_RET(talloc_free(request), 0);
156}
157
159{
160 fr_pair_t *local_vp;
161 fr_pair_t *vp;
162 request_t *request = request_fake_alloc();
163
164 TEST_CASE("Add 'Test-Integer' in 'control_pairs' using pair_append_session_state()");
166
167 TEST_CASE("Validating PAIR_VERIFY()");
168 TEST_CHECK((vp = fr_pair_list_head(&request->session_state_pairs)) != NULL);
170
171 TEST_MSG("Set vp = 12345");
172 vp->vp_uint32 = 12345;
173
174 TEST_CHECK(vp->vp_uint32 == 12345);
175 TEST_MSG("Expected %s == 12345", fr_dict_attr_test_uint32->name);
176
177 TEST_CHECK_RET(talloc_free(request), 0);
178}
179
181{
182 fr_pair_t *vp;
183 request_t *request = request_fake_alloc();
184
185 TEST_CASE("Update 'Test-Integer' in 'request_pairs' using pair_update_request()");
187
188 TEST_CASE("Validating PAIR_VERIFY()");
190
191 TEST_MSG("Set vp = 112233");
192 vp->vp_uint32 = 112233;
193
194 TEST_CASE("Expected fr_dict_attr_test_uint32 (vp->vp_uint32 == 112233)");
195 TEST_CHECK((vp = fr_pair_find_by_da(&request->request_pairs, NULL, fr_dict_attr_test_uint32)) != NULL);
196
197 TEST_CASE("Validating PAIR_VERIFY()");
199
200 TEST_MSG("Checking if vp == 12345");
201 /*
202 * Such 'vp != NULL' just to mute clang "warning: Dereference of null pointer"
203 */
204 TEST_CHECK(vp && vp->vp_uint32 == 112233);
205
206 TEST_CHECK_RET(talloc_free(request), 0);
207}
208
209static void test_pair_update_reply(void)
210{
211 fr_pair_t *vp;
212 request_t *request = request_fake_alloc();
213
214 TEST_CASE("Update 'Test-Integer' in 'reply_pairs' using pair_update_request()");
216
217 TEST_CASE("Validating PAIR_VERIFY()");
219
220 TEST_MSG("Set vp = 3333");
221 vp->vp_uint32 = 3333;
222
223 TEST_CASE("Expected fr_dict_attr_test_uint32 (vp->vp_uint32 == 3333)");
224 TEST_CHECK((vp = fr_pair_find_by_da(&request->reply_pairs, NULL, fr_dict_attr_test_uint32)) != NULL);
225
226 TEST_CASE("Validating PAIR_VERIFY()");
228
229 TEST_CHECK(vp && vp->vp_uint32 == 3333);
230
231 TEST_CHECK_RET(talloc_free(request), 0);
232}
233
235{
236 fr_pair_t *vp;
237 request_t *request = request_fake_alloc();
238
239 TEST_CASE("Update 'Test-Integer' in 'control_pairs' using pair_update_control()");
241
242 TEST_CASE("Validating PAIR_VERIFY()");
244
245 TEST_MSG("Set vp = 44444");
246 vp->vp_uint32 = 44444;
247
248 TEST_CASE("Expected fr_dict_attr_test_uint32 (vp->vp_uint32 == 44444)");
249 TEST_CHECK((vp = fr_pair_find_by_da(&request->control_pairs, NULL, fr_dict_attr_test_uint32)) != NULL);
250
251 TEST_CASE("Validating PAIR_VERIFY()");
253
254 TEST_CHECK(vp && vp->vp_uint32 == 44444);
255
256 TEST_CHECK_RET(talloc_free(request), 0);
257}
258
260{
261 fr_pair_t *vp;
262 request_t *request = request_fake_alloc();
263
264 TEST_CASE("Update 'Test-Integer' in 'state' using pair_update_session_state()");
266
267 TEST_CASE("Validating PAIR_VERIFY()");
269
270 TEST_MSG("Set vp = 7890");
271 vp->vp_uint32 = 7890;
272
273 TEST_CASE("Expected fr_dict_attr_test_uint32 (vp->vp_uint32 == 7890)");
274 TEST_CHECK((vp = fr_pair_find_by_da(&request->session_state_pairs, NULL, fr_dict_attr_test_uint32)) != NULL);
275
276 TEST_CASE("Validating PAIR_VERIFY()");
278
279 TEST_CHECK(vp && vp->vp_uint32 == 7890);
280
281 TEST_CHECK_RET(talloc_free(request), 0);
282}
283
285{
286 request_t *request = request_fake_alloc();
287
288 TEST_CASE("Copy 'test_pairs' into 'request->request_pairs'");
289 TEST_CHECK(fr_pair_list_copy(autofree, &request->request_pairs, &test_pairs) > 0);
290
291 TEST_CASE("Delete 'Test-Integer' in 'request->request_pairs' using pair_delete_request()");
293
294 TEST_CASE("The 'Test-Integer' shouldn't exist in 'request->request_pairs'");
295 TEST_CHECK(fr_pair_find_by_da(&request->request_pairs, NULL, fr_dict_attr_test_uint32) == NULL);
296
297 TEST_CHECK_RET(talloc_free(request), 0);
298}
299
300static void test_pair_delete_reply(void)
301{
302 request_t *request = request_fake_alloc();
303
304 TEST_CASE("Copy 'test_pairs' into 'request->reply_pairs'");
305 TEST_CHECK(fr_pair_list_copy(autofree, &request->reply_pairs, &test_pairs) > 0);
306
307 TEST_CASE("Delete 'Test-Integer' in 'request->reply_pairs' using pair_delete_reply()");
309
310 TEST_CASE("The 'Test-Integer' shouldn't exist in 'request->reply_pairs'");
311 TEST_CHECK(fr_pair_find_by_da(&request->reply_pairs, NULL, fr_dict_attr_test_uint32) == NULL);
312
313 TEST_CHECK_RET(talloc_free(request), 0);
314}
315
317{
318 request_t *request = request_fake_alloc();
319
320 TEST_CASE("Copy 'test_pairs' into 'request->control_pairs'");
321 TEST_CHECK(fr_pair_list_copy(autofree, &request->control_pairs, &test_pairs) > 0);
322
323 TEST_CASE("Delete 'Test-Integer' in 'request->control_pairs' using pair_delete_control()");
325
326 TEST_CASE("The 'Test-Integer' shouldn't exist in 'request->control_pairs'");
327 TEST_CHECK(fr_pair_find_by_da(&request->control_pairs, NULL, fr_dict_attr_test_uint32) == NULL);
328
329 TEST_CHECK_RET(talloc_free(request), 0);
330}
331
333{
334 request_t *request = request_fake_alloc();
335
336 TEST_CASE("Copy 'test_pairs' into 'request->state'");
337 TEST_CHECK(fr_pair_list_copy(autofree, &request->session_state_pairs, &test_pairs) > 0);
338
339 TEST_CASE("Delete 'Test-Integer' in 'request->state' using pair_delete_session_state()");
341
342 TEST_CASE("The 'Test-Integer' shouldn't exist in 'request->state'");
343 TEST_CHECK(fr_pair_find_by_da(&request->session_state_pairs, NULL, fr_dict_attr_test_uint32) == NULL);
344
345 TEST_CHECK_RET(talloc_free(request), 0);
346}
347
349 /*
350 * Add pairs
351 */
352 { "pair_append_request", test_pair_append_request },
353 { "pair_append_reply", test_pair_append_reply },
354 { "pair_append_control", test_pair_append_control },
355 { "pair_append_session_state", test_pair_append_session_state },
356
357 /*
358 * Update pairs
359 */
360 { "pair_update_request", test_pair_update_request },
361 { "pair_update_reply", test_pair_update_reply },
362 { "pair_update_control", test_pair_update_control },
363 { "pair_update_session_state", test_pair_update_session_state },
364
365 /*
366 * Delete pairs
367 */
368 { "pair_delete_request", test_pair_delete_request },
369 { "pair_delete_reply", test_pair_delete_reply },
370 { "pair_delete_control", test_pair_delete_control },
371 { "pair_delete_session_state", test_pair_delete_session_state },
372
373 { NULL }
374};
#define TEST_CHECK(cond)
Definition acutest.h:85
#define TEST_CASE(name)
Definition acutest.h:184
#define TEST_MSG(...)
Definition acutest.h:215
#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:234
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(reap)
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:2321
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:695
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:589
#define request_local_alloc_external(_ctx, _args)
Allocate a new external request outside of the request pool.
Definition request.h:331
Optional arguments for initialising requests.
Definition request.h:283
#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:51
#define PAIR_VERIFY(_x)
Definition pair.h:191
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