The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
value_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 value box functions
18 *
19 * @file src/lib/util/test/value_tests.c
20 * @copyright 2026 Network RADIUS SAS ("legal@networkradius.com")
21 */
22
23static void test_init(void);
24#define TEST_GLOBAL_INIT test_init()
25
26#include "acutest_common_init.h"
27#include "acutest_helpers.h"
28
29#include <freeradius-devel/util/value.h>
30
31static TALLOC_CTX *autofree;
32
33static void test_init(void)
34{
36 if (!autofree) {
37 fr_perror("value_tests");
38 fr_exit_now(EXIT_FAILURE);
39 }
40
42 fr_perror("value_tests");
43 fr_exit_now(EXIT_FAILURE);
44 }
45}
46
47/*
48 * Comparison tests (fr_value_box_cmp)
49 */
50static void test_cmp_uint32_equal(void)
51{
52 fr_value_box_t a, b;
53
54 fr_value_box(&a, (uint32_t) 42, false);
55 fr_value_box(&b, (uint32_t) 42, false);
56
57 TEST_CHECK(fr_value_box_cmp(&a, &b) == 0);
58}
59
60static void test_cmp_uint32_less(void)
61{
62 fr_value_box_t a, b;
63
64 fr_value_box(&a, (uint32_t) 10, false);
65 fr_value_box(&b, (uint32_t) 20, false);
66
67 TEST_CHECK(fr_value_box_cmp(&a, &b) == -1);
68 TEST_CHECK(fr_value_box_cmp(&b, &a) == 1);
69}
70
71static void test_cmp_int32(void)
72{
73 fr_value_box_t a, b;
74
75 fr_value_box(&a, (int32_t) -10, false);
76 fr_value_box(&b, (int32_t) 10, false);
77
78 TEST_CHECK(fr_value_box_cmp(&a, &b) == -1);
79 TEST_CHECK(fr_value_box_cmp(&b, &a) == 1);
80
81 fr_value_box(&b, (int32_t) -10, false);
82 TEST_CHECK(fr_value_box_cmp(&a, &b) == 0);
83}
84
85static void test_cmp_float64(void)
86{
87 fr_value_box_t a, b;
88
89 fr_value_box(&a, (double) 3.14, false);
90 fr_value_box(&b, (double) 2.71, false);
91
92 TEST_CHECK(fr_value_box_cmp(&a, &b) == 1);
93 TEST_CHECK(fr_value_box_cmp(&b, &a) == -1);
94
95 fr_value_box(&b, (double) 3.14, false);
96 TEST_CHECK(fr_value_box_cmp(&a, &b) == 0);
97}
98
99static void test_cmp_string(void)
100{
101 fr_value_box_t a, b;
102
103 fr_value_box_strdup(autofree, &a, NULL, "apple", false);
104 fr_value_box_strdup(autofree, &b, NULL, "banana", false);
105
106 TEST_CHECK(fr_value_box_cmp(&a, &b) == -1);
107 TEST_CHECK(fr_value_box_cmp(&b, &a) == 1);
108
110 fr_value_box_strdup(autofree, &b, NULL, "apple", false);
111 TEST_CHECK(fr_value_box_cmp(&a, &b) == 0);
112
115}
116
117static void test_cmp_octets(void)
118{
119 fr_value_box_t a, b;
120
121 fr_value_box_memdup(autofree, &a, NULL, (uint8_t const *)"\x01\x02", 2, false);
122 fr_value_box_memdup(autofree, &b, NULL, (uint8_t const *)"\x01\x03", 2, false);
123
124 TEST_CHECK(fr_value_box_cmp(&a, &b) == -1);
125 TEST_CHECK(fr_value_box_cmp(&b, &a) == 1);
126
129}
130
131static void test_cmp_octets_length(void)
132{
133 fr_value_box_t a, b;
134
135 /* Same prefix but different length: shorter is "less" */
136 fr_value_box_memdup(autofree, &a, NULL, (uint8_t const *)"\x01", 1, false);
137 fr_value_box_memdup(autofree, &b, NULL, (uint8_t const *)"\x01\x02", 2, false);
138
139 TEST_CHECK(fr_value_box_cmp(&a, &b) == -1);
140 TEST_CHECK(fr_value_box_cmp(&b, &a) == 1);
141
144}
145
146static void test_cmp_bool(void)
147{
148 fr_value_box_t a, b;
149
150 fr_value_box(&a, (bool) true, false);
151 fr_value_box(&b, (bool) false, false);
152
153 TEST_CHECK(fr_value_box_cmp(&a, &b) == 1);
154 TEST_CHECK(fr_value_box_cmp(&b, &a) == -1);
155
156 fr_value_box(&b, (bool) true, false);
157 TEST_CHECK(fr_value_box_cmp(&a, &b) == 0);
158}
159
161{
162 fr_value_box_t a, b;
163
164 fr_value_box(&a, (uint32_t) 42, false);
165 fr_value_box(&b, (uint64_t) 42, false);
166
167 /* Different types are incomparable, both directions return CMP_ERR exactly */
170}
171
172/*
173 * Comparison operator tests (fr_value_box_cmp_op)
174 */
175static void test_cmp_op_eq(void)
176{
177 fr_value_box_t a, b;
178
179 fr_value_box(&a, (uint32_t) 42, false);
180 fr_value_box(&b, (uint32_t) 42, false);
181
183
184 fr_value_box(&b, (uint32_t) 43, false);
186}
187
188static void test_cmp_op_ne(void)
189{
190 fr_value_box_t a, b;
191
192 fr_value_box(&a, (uint32_t) 42, false);
193 fr_value_box(&b, (uint32_t) 43, false);
194
196
197 fr_value_box(&b, (uint32_t) 42, false);
199}
200
201static void test_cmp_op_lt(void)
202{
203 fr_value_box_t a, b;
204
205 fr_value_box(&a, (uint32_t) 10, false);
206 fr_value_box(&b, (uint32_t) 20, false);
207
211}
212
213static void test_cmp_op_gt(void)
214{
215 fr_value_box_t a, b;
216
217 fr_value_box(&a, (uint32_t) 20, false);
218 fr_value_box(&b, (uint32_t) 10, false);
219
222}
223
224static void test_cmp_op_le(void)
225{
226 fr_value_box_t a, b;
227
228 fr_value_box(&a, (uint32_t) 10, false);
229 fr_value_box(&b, (uint32_t) 20, false);
230
233
234 fr_value_box(&b, (uint32_t) 10, false);
236}
237
238static void test_cmp_op_ge(void)
239{
240 fr_value_box_t a, b;
241
242 fr_value_box(&a, (uint32_t) 20, false);
243 fr_value_box(&b, (uint32_t) 10, false);
244
247
248 fr_value_box(&b, (uint32_t) 20, false);
250}
251
252/*
253 * Cast tests (fr_value_box_cast)
254 */
256{
257 fr_value_box_t src, dst;
258
259 fr_value_box(&src, (uint32_t) 42, false);
261
262 TEST_CHECK(fr_value_box_cast(autofree, &dst, FR_TYPE_UINT64, NULL, &src) == 0);
263 TEST_CHECK(dst.type == FR_TYPE_UINT64);
264 TEST_CHECK(dst.vb_uint64 == 42);
265}
266
267/** A float at or past the top of a 64-bit integer must fail the cast, not convert
268 *
269 * UINT64_MAX and INT64_MAX are not representable as a double, and round up to 2^64 and
270 * 2^63. A source of exactly 2^64 or 2^63 passed a "greater than the maximum" check, and
271 * the conversion to the integer type was undefined.
272 */
274{
275 fr_value_box_t src, dst;
276
277 TEST_CASE("float64 2^64 to uint64 overflows");
278 fr_value_box(&src, (double) 0x1p64, false);
280 TEST_CHECK(fr_value_box_cast(autofree, &dst, FR_TYPE_UINT64, NULL, &src) < 0);
281
282 TEST_CASE("float32 2^64 to uint64 overflows");
283 fr_value_box(&src, (float) 0x1p64, false);
285 TEST_CHECK(fr_value_box_cast(autofree, &dst, FR_TYPE_UINT64, NULL, &src) < 0);
286
287 TEST_CASE("float64 2^63 to int64 overflows");
288 fr_value_box(&src, (double) 0x1p63, false);
290 TEST_CHECK(fr_value_box_cast(autofree, &dst, FR_TYPE_INT64, NULL, &src) < 0);
291
292 TEST_CASE("the largest float64 below 2^64 converts to uint64");
293 fr_value_box(&src, (double) 0x1.fffffffffffffp63, false);
295 TEST_CHECK(fr_value_box_cast(autofree, &dst, FR_TYPE_UINT64, NULL, &src) == 0);
296 TEST_CHECK(dst.vb_uint64 == UINT64_C(18446744073709549568));
297
298 TEST_CASE("float64 -2^63 converts to int64");
299 fr_value_box(&src, (double) -0x1p63, false);
301 TEST_CHECK(fr_value_box_cast(autofree, &dst, FR_TYPE_INT64, NULL, &src) == 0);
302 TEST_CHECK(dst.vb_int64 == INT64_MIN);
303}
304
305/** A float cast to a date must fail when the date does not fit in int64_t nanoseconds
306 *
307 * A date is a signed 64-bit count of nanoseconds, which holds about 9.2e9 seconds. The float
308 * branches converted the seconds to int64_t and multiplied by NSEC. The float32 branch had
309 * no check, the float64 branch checked after a conversion that was already undefined above
310 * 2^63, and a value in the last second before the bound overflowed when the fraction was
311 * added.
312 */
314{
315 fr_value_box_t src, dst;
316
317 TEST_CASE("float32 1e10 seconds overflows");
318 fr_value_box(&src, (float) 1e10, false);
320 TEST_CHECK(fr_value_box_cast(autofree, &dst, FR_TYPE_DATE, NULL, &src) < 0);
321
322 TEST_CASE("float64 1e19 seconds overflows");
323 fr_value_box(&src, (double) 1e19, false);
325 TEST_CHECK(fr_value_box_cast(autofree, &dst, FR_TYPE_DATE, NULL, &src) < 0);
326
327 TEST_CASE("float64 in the last second before the bound overflows");
328 fr_value_box(&src, (double) 9223372036.5, false);
330 TEST_CHECK(fr_value_box_cast(autofree, &dst, FR_TYPE_DATE, NULL, &src) < 0);
331
332 TEST_CASE("float64 1.5 seconds converts, keeping the fraction");
333 fr_value_box(&src, (double) 1.5, false);
335 TEST_CHECK(fr_value_box_cast(autofree, &dst, FR_TYPE_DATE, NULL, &src) == 0);
336 TEST_CHECK(fr_unix_time_unwrap(dst.vb_date) == UINT64_C(1500000000));
337 TEST_MSG("got %" PRIu64 " ns", fr_unix_time_unwrap(dst.vb_date));
338}
339
341{
342 fr_value_box_t src, dst;
343
344 fr_value_box(&src, (uint32_t) 12345, false);
346
347 TEST_CHECK(fr_value_box_cast(autofree, &dst, FR_TYPE_STRING, NULL, &src) == 0);
348 TEST_CHECK(dst.type == FR_TYPE_STRING);
349 TEST_CHECK_STRCMP(dst.vb_strvalue, "12345");
350
351 fr_value_box_clear(&dst);
352}
353
355{
356 fr_value_box_t src, dst;
357
358 fr_value_box_strdup(autofree, &src, NULL, "12345", false);
360
361 TEST_CHECK(fr_value_box_cast(autofree, &dst, FR_TYPE_UINT32, NULL, &src) == 0);
362 TEST_CHECK(dst.type == FR_TYPE_UINT32);
363 TEST_CHECK(dst.vb_uint32 == 12345);
364 TEST_MSG("Expected 12345, got %u", dst.vb_uint32);
365
366 fr_value_box_clear(&src);
367}
368
369/*
370 * fr_value_box_cast() returns 0 on success. A cast from a string
371 * parses the string, and must not return the length it parsed.
372 */
374{
375 static struct {
376 char const *in;
378 } const cast[] = {
379 { "192.0.2.1", FR_TYPE_IPV4_ADDR },
380 { "192.0.2.0/24", FR_TYPE_IPV4_PREFIX },
381 { "2001:db8::1", FR_TYPE_IPV6_ADDR },
382 { "2001:db8::/32", FR_TYPE_IPV6_PREFIX },
383 { "aa:bb:cc:dd:ee:ff", FR_TYPE_ETHERNET },
384 { "yes", FR_TYPE_BOOL },
385 { "12345", FR_TYPE_UINT64 },
386 { "0x0102", FR_TYPE_OCTETS },
387 };
388 fr_value_box_t src, dst;
389 size_t i;
390 int ret;
391
392 for (i = 0; i < NUM_ELEMENTS(cast); i++) {
393 fr_value_box_strdup(autofree, &src, NULL, cast[i].in, false);
395
396 ret = fr_value_box_cast(autofree, &dst, cast[i].type, NULL, &src);
397 TEST_CHECK(ret == 0);
398 TEST_MSG("Casting \"%s\" to %s returned %d, expected 0",
399 cast[i].in, fr_type_to_str(cast[i].type), ret);
400
401 fr_value_box_clear(&dst);
402 fr_value_box_clear(&src);
403 }
404}
405
407{
408 fr_value_box_t src, dst;
409
410 fr_value_box(&src, (int32_t) -42, false);
412
413 TEST_CHECK(fr_value_box_cast(autofree, &dst, FR_TYPE_INT64, NULL, &src) == 0);
414 TEST_CHECK(dst.type == FR_TYPE_INT64);
415 TEST_CHECK(dst.vb_int64 == -42);
416}
417
419{
420 fr_value_box_t src, dst;
421
422 fr_value_box(&src, (uint8_t) 255, false);
424
425 TEST_CHECK(fr_value_box_cast(autofree, &dst, FR_TYPE_UINT32, NULL, &src) == 0);
426 TEST_CHECK(dst.type == FR_TYPE_UINT32);
427 TEST_CHECK(dst.vb_uint32 == 255);
428}
429
430static void test_cast_same_type(void)
431{
432 fr_value_box_t src, dst;
433
434 fr_value_box(&src, (uint32_t) 42, false);
436
437 TEST_CHECK(fr_value_box_cast(autofree, &dst, FR_TYPE_UINT32, NULL, &src) == 0);
438 TEST_CHECK(dst.type == FR_TYPE_UINT32);
439 TEST_CHECK(dst.vb_uint32 == 42);
440}
441
443{
444 fr_value_box_t src, dst;
445
446 fr_value_box(&src, (bool) true, false);
448
449 TEST_CHECK(fr_value_box_cast(autofree, &dst, FR_TYPE_UINT32, NULL, &src) == 0);
450 TEST_CHECK(dst.vb_uint32 == 1);
451
452 fr_value_box(&src, (bool) false, false);
453 TEST_CHECK(fr_value_box_cast(autofree, &dst, FR_TYPE_UINT32, NULL, &src) == 0);
454 TEST_CHECK(dst.vb_uint32 == 0);
455}
456
458{
459 fr_value_box_t src, dst;
460
461 fr_value_box(&src, (uint32_t) 1, false);
463
464 TEST_CHECK(fr_value_box_cast(autofree, &dst, FR_TYPE_BOOL, NULL, &src) == 0);
465 TEST_CHECK(dst.vb_bool == true);
466
467 fr_value_box(&src, (uint32_t) 0, false);
468 TEST_CHECK(fr_value_box_cast(autofree, &dst, FR_TYPE_BOOL, NULL, &src) == 0);
469 TEST_CHECK(dst.vb_bool == false);
470}
471
472/*
473 * Cast in place tests
474 */
476{
478
479 fr_value_box(&vb, (uint32_t) 42, false);
480
482 TEST_CHECK(vb.type == FR_TYPE_UINT64);
483 TEST_CHECK(vb.vb_uint64 == 42);
484}
485
487{
489
490 fr_value_box(&vb, (uint32_t) 12345, false);
491
493 TEST_CHECK(vb.type == FR_TYPE_STRING);
494 TEST_CHECK_STRCMP(vb.vb_strvalue, "12345");
495
497}
498
499/*
500 * Copy tests
501 */
502static void test_copy_uint32(void)
503{
504 fr_value_box_t src, dst;
505
506 fr_value_box(&src, (uint32_t) 42, false);
508
509 TEST_CHECK(fr_value_box_copy(autofree, &dst, &src) == 0);
510 TEST_CHECK(dst.type == FR_TYPE_UINT32);
511 TEST_CHECK(dst.vb_uint32 == 42);
512}
513
514static void test_copy_string(void)
515{
516 fr_value_box_t src, dst;
517
518 fr_value_box_strdup(autofree, &src, NULL, "hello", false);
520
521 TEST_CHECK(fr_value_box_copy(autofree, &dst, &src) == 0);
522 TEST_CHECK(dst.type == FR_TYPE_STRING);
523 TEST_CHECK_STRCMP(dst.vb_strvalue, "hello");
524
525 /* Deep copy: different pointer */
526 TEST_CHECK(dst.vb_strvalue != src.vb_strvalue);
527
528 fr_value_box_clear(&src);
529 fr_value_box_clear(&dst);
530}
531
532static void test_copy_octets(void)
533{
534 fr_value_box_t src, dst;
535
536 fr_value_box_memdup(autofree, &src, NULL, (uint8_t const *)"\x01\x02\x03", 3, false);
538
539 TEST_CHECK(fr_value_box_copy(autofree, &dst, &src) == 0);
540 TEST_CHECK(dst.type == FR_TYPE_OCTETS);
541 TEST_CHECK(dst.vb_length == 3);
542 TEST_CHECK(memcmp(dst.vb_octets, "\x01\x02\x03", 3) == 0);
543
544 /* Deep copy */
545 TEST_CHECK(dst.vb_octets != src.vb_octets);
546
547 fr_value_box_clear(&src);
548 fr_value_box_clear(&dst);
549}
550
552{
553 fr_value_box_t src, dst;
554
555 fr_value_box(&src, (uint32_t) 42, true);
557
558 TEST_CHECK(fr_value_box_copy(autofree, &dst, &src) == 0);
559 TEST_CHECK(dst.tainted == true);
560}
561
562/*
563 * Safety (safe_for) propagation tests
564 *
565 * Tokens standing in for two different consumers. Any two distinct values other
566 * than FR_VALUE_BOX_SAFE_FOR_NONE and FR_VALUE_BOX_SAFE_FOR_ANY will do.
567 */
568#define SAFE_FOR_X ((fr_value_box_safe_for_t) 0x1000)
569#define SAFE_FOR_Y ((fr_value_box_safe_for_t) 0x2000)
570
572{
573 fr_value_box_t src, dst;
574
575 fr_value_box_strdup(autofree, &src, NULL, "copy me", false);
578
579 TEST_CHECK(fr_value_box_copy(autofree, &dst, &src) == 0);
581
582 fr_value_box_clear(&src);
583 fr_value_box_clear(&dst);
584}
585
587{
588 fr_value_box_t src, dst;
589
590 fr_value_box_memdup(autofree, &src, NULL, (uint8_t const *) "abc", 3, false);
593
594 TEST_CHECK(fr_value_box_cast(autofree, &dst, FR_TYPE_STRING, NULL, &src) == 0);
595 TEST_CHECK(dst.type == FR_TYPE_STRING);
597 TEST_MSG("Expected safe_for %#lx, got %#lx", (unsigned long) SAFE_FOR_X, (unsigned long) dst.vb_safefor);
598
599 fr_value_box_clear(&src);
600 fr_value_box_clear(&dst);
601}
602
604{
605 fr_value_box_t src, dst;
606
607 fr_value_box_strdup(autofree, &src, NULL, "abc", false);
610
611 TEST_CHECK(fr_value_box_cast(autofree, &dst, FR_TYPE_OCTETS, NULL, &src) == 0);
612 TEST_CHECK(dst.type == FR_TYPE_OCTETS);
614 TEST_MSG("Expected safe_for %#lx, got %#lx", (unsigned long) SAFE_FOR_X, (unsigned long) dst.vb_safefor);
615
616 fr_value_box_clear(&src);
617 fr_value_box_clear(&dst);
618}
619
621{
622 fr_value_box_t src, dst;
623
624 fr_value_box(&src, (uint32_t) 12345, false);
627
628 TEST_CHECK(fr_value_box_cast(autofree, &dst, FR_TYPE_STRING, NULL, &src) == 0);
629 TEST_CHECK(dst.type == FR_TYPE_STRING);
630 TEST_CHECK(dst.vb_safefor == FR_VALUE_BOX_SAFE_FOR_NONE);
631 TEST_MSG("Expected safe_for NONE, got %#lx", (unsigned long) dst.vb_safefor);
632
633 fr_value_box_clear(&dst);
634}
635
636/** Build a list of single character string boxes, each marked with the matching token
637 */
638static void safe_for_list_build(fr_value_box_list_t *list, fr_value_box_safe_for_t const tokens[], size_t num)
639{
640 size_t i;
641
642 fr_value_box_list_init(list);
643 for (i = 0; i < num; i++) {
644 fr_value_box_t *vb;
645
647 TEST_ASSERT(vb != NULL);
648 TEST_ASSERT(fr_value_box_strdup(vb, vb, NULL, "x", false) == 0);
649 fr_value_box_mark_safe_for(vb, tokens[i]);
650 fr_value_box_list_insert_tail(list, vb);
651 }
652}
653
654/** Concatenate a list into a separate output box and check the resulting safe_for
655 */
656static void safe_for_concat_check(fr_type_t type, fr_value_box_safe_for_t const tokens[], size_t num,
658{
659 fr_value_box_list_t list;
661
662 safe_for_list_build(&list, tokens, num);
664
666 FR_VALUE_BOX_LIST_FREE, true, SIZE_MAX) == 0);
667 TEST_CHECK(out.type == type);
668 TEST_CHECK(out.vb_length == num);
669 TEST_CHECK(out.vb_safefor == expect);
670 TEST_MSG("Expected safe_for %#lx, got %#lx", (unsigned long) expect, (unsigned long) out.vb_safefor);
671
673}
674
681
682static void test_concat_x_x_is_x(void)
683{
684 fr_value_box_safe_for_t const tokens[] = { SAFE_FOR_X, SAFE_FOR_X };
685
687}
688
695
702
709
716
718{
719 fr_value_box_safe_for_t const tokens[] = { SAFE_FOR_X, SAFE_FOR_X };
720
722}
723
724/** The list head doubles as the output box
725 */
727{
728 fr_value_box_safe_for_t const tokens[] = { SAFE_FOR_X, SAFE_FOR_X };
729 fr_value_box_list_t list;
731
732 safe_for_list_build(&list, tokens, NUM_ELEMENTS(tokens));
733 head = fr_value_box_list_head(&list);
734
736 FR_VALUE_BOX_LIST_FREE, true, SIZE_MAX) == 0);
737 TEST_CHECK(head->vb_length == 2);
739 TEST_MSG("Expected safe_for %#lx, got %#lx", (unsigned long) SAFE_FOR_X, (unsigned long) head->vb_safefor);
740
741 fr_value_box_list_talloc_free(&list);
742}
743
744static void test_list_safety_merge(void)
745{
748 fr_value_box_safe_for_t const x_only[] = { SAFE_FOR_X };
749 fr_value_box_list_t list;
750 fr_value_box_t out, *group;
751
752 TEST_CASE("X and ANY merge to X");
753 safe_for_list_build(&list, x_any, NUM_ELEMENTS(x_any));
757 fr_value_box_list_talloc_free(&list);
758
759 TEST_CASE("X and Y merge to NONE");
760 safe_for_list_build(&list, x_y, NUM_ELEMENTS(x_y));
764 fr_value_box_list_talloc_free(&list);
765
766 TEST_CASE("Group children are merged, the group box itself is not");
767 fr_value_box_list_init(&list);
769 TEST_ASSERT(group != NULL);
770 safe_for_list_build(&group->vb_group, x_only, NUM_ELEMENTS(x_only));
771 fr_value_box_list_insert_tail(&list, group);
775 fr_value_box_list_talloc_free(&list);
776}
777
778/*
779 * String operations
780 */
781static void test_strdup(void)
782{
784
785 TEST_CHECK(fr_value_box_strdup(autofree, &vb, NULL, "hello world", false) == 0);
786 TEST_CHECK(vb.type == FR_TYPE_STRING);
787 TEST_CHECK_STRCMP(vb.vb_strvalue, "hello world");
788 TEST_CHECK(vb.vb_length == 11);
789
791}
792
793static void test_asprintf(void)
794{
796
797 TEST_CHECK(fr_value_box_asprintf(autofree, &vb, NULL, false, "value=%d", 42) == 0);
798 TEST_CHECK(vb.type == FR_TYPE_STRING);
799 TEST_CHECK_STRCMP(vb.vb_strvalue, "value=42");
800
802}
803
804static void test_bstrndup(void)
805{
807
808 /* bstrndup copies exactly len bytes */
809 TEST_CHECK(fr_value_box_bstrndup(autofree, &vb, NULL, "hello\0world", 11, false) == 0);
810 TEST_CHECK(vb.type == FR_TYPE_STRING);
811 TEST_CHECK(vb.vb_length == 11);
812 TEST_CHECK(memcmp(vb.vb_strvalue, "hello\0world", 11) == 0);
813
815}
816
817/*
818 * Memory operations
819 */
820static void test_memdup(void)
821{
823 uint8_t data[] = { 0xde, 0xad, 0xbe, 0xef };
824
825 TEST_CHECK(fr_value_box_memdup(autofree, &vb, NULL, data, sizeof(data), false) == 0);
826 TEST_CHECK(vb.type == FR_TYPE_OCTETS);
827 TEST_CHECK(vb.vb_length == 4);
828 TEST_CHECK(memcmp(vb.vb_octets, data, 4) == 0);
829
831}
832
833/*
834 * Hash tests
835 */
836static void test_hash_same_values(void)
837{
838 fr_value_box_t a, b;
839
840 fr_value_box(&a, (uint32_t) 42, false);
841 fr_value_box(&b, (uint32_t) 42, false);
842
844}
845
847{
848 fr_value_box_t a, b;
849
850 fr_value_box(&a, (uint32_t) 42, false);
851 fr_value_box(&b, (uint32_t) 43, false);
852
854}
855
856static void test_hash_string(void)
857{
858 fr_value_box_t a, b;
859
860 fr_value_box_strdup(autofree, &a, NULL, "hello", false);
861 fr_value_box_strdup(autofree, &b, NULL, "hello", false);
862
864
867}
868
869/*
870 * Truthiness tests (fr_value_box_is_truthy)
871 */
872static void test_truthy_bool(void)
873{
875
876 fr_value_box(&vb, (bool) true, false);
878
879 fr_value_box(&vb, (bool) false, false);
880 TEST_CHECK(fr_value_box_is_truthy(&vb) == false);
881}
882
883static void test_truthy_uint32(void)
884{
886
887 fr_value_box(&vb, (uint32_t) 42, false);
889
890 fr_value_box(&vb, (uint32_t) 0, false);
891 TEST_CHECK(fr_value_box_is_truthy(&vb) == false);
892}
893
894static void test_truthy_string(void)
895{
897
898 fr_value_box_strdup(autofree, &vb, NULL, "hello", false);
901
902 fr_value_box_strdup(autofree, &vb, NULL, "", false);
903 TEST_CHECK(fr_value_box_is_truthy(&vb) == false);
905}
906
907static void test_truthy_octets(void)
908{
910
911 fr_value_box_memdup(autofree, &vb, NULL, (uint8_t const *)"\x01", 1, false);
914
915 fr_value_box_memdup(autofree, &vb, NULL, (uint8_t const *)"", 0, false);
916 TEST_CHECK(fr_value_box_is_truthy(&vb) == false);
918}
919
920/*
921 * Increment tests (fr_value_box_increment)
922 */
923static void test_increment_uint32(void)
924{
926
927 fr_value_box(&vb, (uint32_t) 41, false);
929 TEST_CHECK(vb.vb_uint32 == 42);
930 TEST_MSG("Expected 42, got %u", vb.vb_uint32);
931}
932
934{
936
937 fr_value_box(&vb, (uint8_t) UINT8_MAX, false);
939 TEST_CHECK(vb.vb_uint8 == 0);
940 TEST_MSG("Expected 0 (wrap), got %u", vb.vb_uint8);
941}
942
943static void test_increment_int32(void)
944{
946
947 fr_value_box(&vb, (int32_t) -1, false);
949 TEST_CHECK(vb.vb_int32 == 0);
950 TEST_MSG("Expected 0, got %d", vb.vb_int32);
951}
952
953/*
954 * as_uint64 extraction tests
955 */
956static void test_as_uint64(void)
957{
959
960 fr_value_box(&vb, (uint8_t) 255, false);
962
963 fr_value_box(&vb, (uint16_t) 1000, false);
965
966 fr_value_box(&vb, (uint32_t) 100000, false);
967 TEST_CHECK(fr_value_box_as_uint64(&vb) == 100000);
968
969 fr_value_box(&vb, (uint64_t) UINT64_MAX, false);
970 TEST_CHECK(fr_value_box_as_uint64(&vb) == UINT64_MAX);
971
972 fr_value_box(&vb, (bool) true, false);
974}
975
976/*
977 * Clear tests
978 */
979static void test_clear(void)
980{
982
983 fr_value_box(&vb, (uint32_t) 42, false);
984 TEST_CHECK(vb.type == FR_TYPE_UINT32);
985
987 TEST_CHECK(vb.type == FR_TYPE_NULL);
988}
989
990static void test_clear_string(void)
991{
993
994 fr_value_box_strdup(autofree, &vb, NULL, "hello", false);
995 TEST_CHECK(vb.type == FR_TYPE_STRING);
996
998 TEST_CHECK(vb.type == FR_TYPE_NULL);
999}
1000
1001/*
1002 * Tainted flag tests
1003 */
1004static void test_tainted_flag(void)
1005{
1006 fr_value_box_t vb;
1007
1008 fr_value_box(&vb, (uint32_t) 42, true);
1009 TEST_CHECK(vb.tainted == true);
1010
1011 fr_value_box(&vb, (uint32_t) 42, false);
1012 TEST_CHECK(vb.tainted == false);
1013}
1014
1015/*
1016 * Network encode/decode round-trip tests
1017 */
1018static void test_network_uint32(void)
1019{
1020 fr_value_box_t src, dst;
1021 uint8_t buffer[256] = {};
1022 fr_dbuff_t dbuff;
1023 ssize_t enc_len, dec_len;
1024
1025 fr_value_box(&src, (uint32_t) 0x12345678, false);
1026
1027 /* Encode to network format */
1028 dbuff = FR_DBUFF_TMP(buffer, sizeof(buffer));
1029 enc_len = fr_value_box_to_network(&dbuff, &src);
1030 TEST_CHECK(enc_len == 4);
1031
1032 /* Verify network byte order (big endian) */
1033 TEST_CHECK(buffer[0] == 0x12);
1034 TEST_CHECK(buffer[1] == 0x34);
1035 TEST_CHECK(buffer[2] == 0x56);
1036 TEST_CHECK(buffer[3] == 0x78);
1037
1038 /* Decode back */
1040 dbuff = FR_DBUFF_TMP(buffer, (size_t)enc_len);
1041 dec_len = fr_value_box_from_network(NULL, &dst, FR_TYPE_UINT32, NULL, &dbuff, enc_len, false);
1042 TEST_CHECK(dec_len == 4);
1043 TEST_CHECK(dst.vb_uint32 == 0x12345678);
1044 TEST_MSG("Expected 0x12345678, got 0x%08x", dst.vb_uint32);
1045}
1046
1047/** The scope_id of an IPv6 address is one byte on the wire, not the four bytes of fr_ipaddr_t.scope_id
1048 *
1049 */
1051{
1052 fr_value_box_t src, dst;
1053 uint8_t buffer[256] = {};
1054 fr_dbuff_t dbuff;
1055 ssize_t enc_len, dec_len;
1056
1057 fr_value_box_init(&src, FR_TYPE_IPV6_ADDR, NULL, false);
1058 src.vb_ip = (fr_ipaddr_t) {
1059 .af = AF_INET6,
1060 .prefix = 128,
1061 .scope_id = 1,
1062 .addr.v6.s6_addr = { 0xfe, 0x80, [15] = 0x01 }
1063 };
1064
1065 dbuff = FR_DBUFF_TMP(buffer, sizeof(buffer));
1066 enc_len = fr_value_box_to_network(&dbuff, &src);
1067 TEST_CHECK(enc_len == 17);
1068 TEST_MSG("Expected 17, got %zd", enc_len);
1069 TEST_CHECK(buffer[0] == 1);
1070 TEST_CHECK(buffer[1] == 0xfe);
1071
1073 dbuff = FR_DBUFF_TMP(buffer, (size_t)enc_len);
1074 dec_len = fr_value_box_from_network(NULL, &dst, FR_TYPE_IPV6_ADDR, NULL, &dbuff, enc_len, false);
1075 TEST_CHECK(dec_len == 17);
1076 TEST_CHECK(dst.vb_ip.scope_id == 1);
1077 TEST_CHECK(memcmp(&dst.vb_ip.addr.v6, &src.vb_ip.addr.v6, sizeof(src.vb_ip.addr.v6)) == 0);
1078
1079 src.vb_ip.scope_id = 256;
1080 dbuff = FR_DBUFF_TMP(buffer, sizeof(buffer));
1081 enc_len = fr_value_box_to_network(&dbuff, &src);
1082 TEST_CHECK(enc_len < 0);
1083 TEST_MSG("Expected an error, got %zd", enc_len);
1084}
1085
1086static void test_network_uint64(void)
1087{
1088 fr_value_box_t src, dst;
1089 uint8_t buffer[256] = {};
1090 fr_dbuff_t dbuff;
1091 ssize_t enc_len, dec_len;
1092
1093 fr_value_box(&src, (uint64_t) 0x0102030405060708ULL, false);
1094
1095 dbuff = FR_DBUFF_TMP(buffer, sizeof(buffer));
1096 enc_len = fr_value_box_to_network(&dbuff, &src);
1097 TEST_CHECK(enc_len == 8);
1098
1099 /* Verify big endian */
1100 TEST_CHECK(buffer[0] == 0x01);
1101 TEST_CHECK(buffer[7] == 0x08);
1102
1103 /* Decode back */
1105 dbuff = FR_DBUFF_TMP(buffer, (size_t)enc_len);
1106 dec_len = fr_value_box_from_network(NULL, &dst, FR_TYPE_UINT64, NULL, &dbuff, enc_len, false);
1107 TEST_CHECK(dec_len == 8);
1108 TEST_CHECK(dst.vb_uint64 == 0x0102030405060708ULL);
1109}
1110
1111static void test_network_int32(void)
1112{
1113 fr_value_box_t src, dst;
1114 uint8_t buffer[256] = {};
1115 fr_dbuff_t dbuff;
1116 ssize_t enc_len, dec_len;
1117
1118 fr_value_box(&src, (int32_t) -1, false);
1119
1120 dbuff = FR_DBUFF_TMP(buffer, sizeof(buffer));
1121 enc_len = fr_value_box_to_network(&dbuff, &src);
1122 TEST_CHECK(enc_len == 4);
1123
1124 /* -1 in two's complement big endian is 0xffffffff */
1125 TEST_CHECK(buffer[0] == 0xff);
1126 TEST_CHECK(buffer[1] == 0xff);
1127 TEST_CHECK(buffer[2] == 0xff);
1128 TEST_CHECK(buffer[3] == 0xff);
1129
1130 /* Decode back */
1132 dbuff = FR_DBUFF_TMP(buffer, (size_t)enc_len);
1133 dec_len = fr_value_box_from_network(NULL, &dst, FR_TYPE_INT32, NULL, &dbuff, enc_len, false);
1134 TEST_CHECK(dec_len == 4);
1135 TEST_CHECK(dst.vb_int32 == -1);
1136}
1137
1138static void test_network_bool(void)
1139{
1140 fr_value_box_t src, dst;
1141 uint8_t buffer[256] = {};
1142 fr_dbuff_t dbuff;
1143 ssize_t enc_len, dec_len;
1144
1145 fr_value_box(&src, (bool) true, false);
1146
1147 dbuff = FR_DBUFF_TMP(buffer, sizeof(buffer));
1148 enc_len = fr_value_box_to_network(&dbuff, &src);
1149 TEST_CHECK(enc_len == 1);
1150 TEST_CHECK(buffer[0] == 1);
1151
1153 dbuff = FR_DBUFF_TMP(buffer, (size_t)enc_len);
1154 dec_len = fr_value_box_from_network(NULL, &dst, FR_TYPE_BOOL, NULL, &dbuff, enc_len, false);
1155 TEST_CHECK(dec_len == 1);
1156 TEST_CHECK(dst.vb_bool == true);
1157}
1158
1159static void test_network_length(void)
1160{
1161 fr_value_box_t vb;
1162
1163 fr_value_box(&vb, (uint8_t) 0, false);
1165
1166 fr_value_box(&vb, (uint16_t) 0, false);
1168
1169 fr_value_box(&vb, (uint32_t) 0, false);
1171
1172 fr_value_box(&vb, (uint64_t) 0, false);
1174}
1175
1176/*
1177 * Print tests (fr_value_box_print)
1178 */
1179static void test_print_uint32(void)
1180{
1181 fr_value_box_t vb;
1182 char buffer[256] = {};
1183 fr_sbuff_t sbuff = FR_SBUFF_OUT(buffer, sizeof(buffer));
1184
1185 fr_value_box(&vb, (uint32_t) 42, false);
1186
1187 TEST_CHECK(fr_value_box_print(&sbuff, &vb, NULL) > 0);
1188 fr_sbuff_terminate(&sbuff);
1190}
1191
1193{
1194 fr_value_box_t vb;
1195 char buffer[256] = {};
1196 fr_sbuff_t sbuff = FR_SBUFF_OUT(buffer, sizeof(buffer));
1197
1198 fr_value_box(&vb, (int32_t) -42, false);
1199
1200 TEST_CHECK(fr_value_box_print(&sbuff, &vb, NULL) > 0);
1201 fr_sbuff_terminate(&sbuff);
1202 TEST_CHECK_STRCMP(buffer, "-42");
1203}
1204
1205static void test_print_string(void)
1206{
1207 fr_value_box_t vb;
1208 char buffer[256] = {};
1209 fr_sbuff_t sbuff = FR_SBUFF_OUT(buffer, sizeof(buffer));
1210
1211 fr_value_box_strdup(autofree, &vb, NULL, "hello", false);
1212
1213 TEST_CHECK(fr_value_box_print(&sbuff, &vb, NULL) > 0);
1214 fr_sbuff_terminate(&sbuff);
1215 TEST_CHECK_STRCMP(buffer, "hello");
1216
1217 fr_value_box_clear(&vb);
1218}
1219
1220static void test_print_bool(void)
1221{
1222 fr_value_box_t vb;
1223 char buffer[256] = {};
1224 fr_sbuff_t sbuff;
1225
1226 fr_value_box(&vb, (bool) true, false);
1227 sbuff = FR_SBUFF_OUT(buffer, sizeof(buffer));
1228 TEST_CHECK(fr_value_box_print(&sbuff, &vb, NULL) > 0);
1229 fr_sbuff_terminate(&sbuff);
1230 TEST_CHECK_STRCMP(buffer, "yes");
1231
1232 fr_value_box(&vb, (bool) false, false);
1233 sbuff = FR_SBUFF_OUT(buffer, sizeof(buffer));
1234 TEST_CHECK(fr_value_box_print(&sbuff, &vb, NULL) > 0);
1235 fr_sbuff_terminate(&sbuff);
1237}
1238
1239static void test_print_octets(void)
1240{
1241 fr_value_box_t vb;
1242 char buffer[256] = {};
1243 fr_sbuff_t sbuff = FR_SBUFF_OUT(buffer, sizeof(buffer));
1244
1245 fr_value_box_memdup(autofree, &vb, NULL, (uint8_t const *)"\xde\xad\xbe\xef", 4, false);
1246
1247 TEST_CHECK(fr_value_box_print(&sbuff, &vb, NULL) > 0);
1248 fr_sbuff_terminate(&sbuff);
1249 TEST_CHECK_STRCMP(buffer, "0xdeadbeef");
1250
1251 fr_value_box_clear(&vb);
1252}
1253
1254/*
1255 * From string parsing tests (fr_value_box_from_str)
1256 */
1257static void test_from_str_uint32(void)
1258{
1259 fr_value_box_t vb;
1260
1262 "12345", strlen("12345"), NULL) > 0);
1263 TEST_CHECK(vb.type == FR_TYPE_UINT32);
1264 TEST_CHECK(vb.vb_uint32 == 12345);
1265 TEST_MSG("Expected 12345, got %u", vb.vb_uint32);
1266}
1267
1268static void test_from_str_int32(void)
1269{
1270 fr_value_box_t vb;
1271
1273 "-42", strlen("-42"), NULL) > 0);
1274 TEST_CHECK(vb.type == FR_TYPE_INT32);
1275 TEST_CHECK(vb.vb_int32 == -42);
1276 TEST_MSG("Expected -42, got %d", vb.vb_int32);
1277}
1278
1279static void test_from_str_bool(void)
1280{
1281 fr_value_box_t vb;
1282
1284 "yes", strlen("yes"), NULL) > 0);
1285 TEST_CHECK(vb.type == FR_TYPE_BOOL);
1286 TEST_CHECK(vb.vb_bool == true);
1287}
1288
1289static void test_from_str_float64(void)
1290{
1291 fr_value_box_t vb;
1292
1294 "3.14", strlen("3.14"), NULL) > 0);
1295 TEST_CHECK(vb.type == FR_TYPE_FLOAT64);
1296 TEST_CHECK((vb.vb_float64 > 3.13) && (vb.vb_float64 < 3.15));
1297 TEST_MSG("Expected ~3.14, got %f", vb.vb_float64);
1298}
1299
1300static void test_from_str_octets(void)
1301{
1302 fr_value_box_t vb;
1303
1305 "0xdeadbeef", strlen("0xdeadbeef"), NULL) > 0);
1306 TEST_CHECK(vb.type == FR_TYPE_OCTETS);
1307 TEST_CHECK(vb.vb_length == 4);
1308 TEST_CHECK(memcmp(vb.vb_octets, "\xde\xad\xbe\xef", 4) == 0);
1309
1310 fr_value_box_clear(&vb);
1311}
1312
1313static void test_from_str_string(void)
1314{
1315 fr_value_box_t vb;
1316
1318 "hello world", strlen("hello world"), NULL) > 0);
1319 TEST_CHECK(vb.type == FR_TYPE_STRING);
1320 TEST_CHECK_STRCMP(vb.vb_strvalue, "hello world");
1321
1322 fr_value_box_clear(&vb);
1323}
1324
1325/*
1326 * Print/parse round-trip tests
1327 */
1328static void test_round_trip_uint32(void)
1329{
1330 fr_value_box_t src, dst;
1331 char buffer[256] = {};
1332 fr_sbuff_t sbuff = FR_SBUFF_OUT(buffer, sizeof(buffer));
1333
1334 fr_value_box(&src, (uint32_t) 42, false);
1335
1336 TEST_CHECK(fr_value_box_print(&sbuff, &src, NULL) > 0);
1337 fr_sbuff_terminate(&sbuff);
1338
1340 buffer, strlen(buffer), NULL) > 0);
1341 TEST_CHECK(dst.vb_uint32 == 42);
1342}
1343
1344static void test_round_trip_int64(void)
1345{
1346 fr_value_box_t src, dst;
1347 char buffer[256] = {};
1348 fr_sbuff_t sbuff = FR_SBUFF_OUT(buffer, sizeof(buffer));
1349
1350 fr_value_box(&src, (int64_t) -9999999, false);
1351
1352 TEST_CHECK(fr_value_box_print(&sbuff, &src, NULL) > 0);
1353 fr_sbuff_terminate(&sbuff);
1354
1356 buffer, strlen(buffer), NULL) > 0);
1357 TEST_CHECK(dst.vb_int64 == -9999999);
1358}
1359
1360static void test_round_trip_bool(void)
1361{
1362 fr_value_box_t src, dst;
1363 char buffer[256] = {};
1364 fr_sbuff_t sbuff;
1365
1366 fr_value_box(&src, (bool) true, false);
1367 sbuff = FR_SBUFF_OUT(buffer, sizeof(buffer));
1368 TEST_CHECK(fr_value_box_print(&sbuff, &src, NULL) > 0);
1369 fr_sbuff_terminate(&sbuff);
1370
1372 buffer, strlen(buffer), NULL) > 0);
1373 TEST_CHECK(dst.vb_bool == true);
1374}
1375
1377{
1378 fr_value_box_t src, dst;
1379 uint8_t buffer[256] = {};
1380 fr_dbuff_t dbuff;
1381 ssize_t enc_len;
1382
1383 fr_value_box(&src, (uint32_t) 0xdeadbeef, false);
1384
1385 /* Encode */
1386 dbuff = FR_DBUFF_TMP(buffer, sizeof(buffer));
1387 enc_len = fr_value_box_to_network(&dbuff, &src);
1388 TEST_CHECK(enc_len == 4);
1389
1390 /* Decode */
1392 dbuff = FR_DBUFF_TMP(buffer, (size_t)enc_len);
1393 TEST_CHECK(fr_value_box_from_network(NULL, &dst, FR_TYPE_UINT32, NULL, &dbuff, enc_len, false) == 4);
1394 TEST_CHECK(dst.vb_uint32 == 0xdeadbeef);
1395}
1396
1397/** Escape function for the in-place escape tests, writes '%' as "%25"
1398 */
1400{
1401 fr_sbuff_t our_out = FR_SBUFF(out);
1402 fr_sbuff_t our_in = FR_SBUFF(in);
1403
1404 while (fr_sbuff_extend(&our_in)) {
1405 char c = *fr_sbuff_current(&our_in);
1406
1407 if (c == '%') {
1408 FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, "%25");
1409 } else {
1410 FR_SBUFF_IN_CHAR_RETURN(&our_out, c);
1411 }
1412 fr_sbuff_advance(&our_in, 1);
1413 }
1414
1415 fr_sbuff_set(in, &our_in);
1416 FR_SBUFF_SET_RETURN(out, &our_out);
1417}
1418
1419static void test_bstrndup_replace(void)
1420{
1421 fr_value_box_t *vb;
1422 char const *old;
1423
1424 TEST_CASE("Replacing a string value copies the new bytes and keeps the flags");
1425 vb = fr_value_box_alloc(NULL, FR_TYPE_STRING, NULL);
1426 TEST_ASSERT(vb != NULL);
1427 TEST_ASSERT(fr_value_box_bstrndup(vb, vb, NULL, "before", 6, true) == 0);
1429 old = vb->vb_strvalue;
1430
1431 TEST_CHECK_RET(fr_value_box_bstrndup_replace(vb, vb, "af\0ter", 6), 0);
1432 TEST_CHECK(vb->vb_strvalue != old);
1433 TEST_CHECK_LEN(vb->vb_length, 6);
1434 TEST_CHECK(memcmp(vb->vb_strvalue, "af\0ter", 6) == 0);
1435 TEST_CHECK(vb->vb_strvalue[6] == '\0');
1436 TEST_CHECK(vb->tainted == true);
1438
1439 TEST_CASE("Replacing from an aliased source copies before freeing");
1440 TEST_CHECK_RET(fr_value_box_bstrndup_replace(vb, vb, vb->vb_strvalue + 2, 4), 0);
1441 TEST_CHECK_LEN(vb->vb_length, 4);
1442 TEST_CHECK(memcmp(vb->vb_strvalue, "\0ter", 4) == 0);
1443
1444 talloc_free(vb);
1445}
1446
1448{
1449 fr_value_box_t *vb;
1450
1451 TEST_CASE("String with escapable characters and an embedded NUL");
1452 vb = fr_value_box_alloc(NULL, FR_TYPE_STRING, NULL);
1453 TEST_ASSERT(vb != NULL);
1454 TEST_ASSERT(fr_value_box_bstrndup(vb, vb, NULL, "a%b\0c", 5, true) == 0);
1455
1457 TEST_CHECK_LEN(vb->vb_length, 7);
1458 TEST_CHECK(memcmp(vb->vb_strvalue, "a%25b\0c", 7) == 0);
1459 TEST_CHECK(vb->vb_strvalue[7] == '\0');
1460 TEST_CHECK(vb->tainted == true);
1461
1462 talloc_free(vb);
1463}
1464
1466{
1467 fr_value_box_t *vb;
1468
1469 TEST_CASE("String with nothing to escape is copied as is");
1470 vb = fr_value_box_alloc(NULL, FR_TYPE_STRING, NULL);
1471 TEST_ASSERT(vb != NULL);
1472 TEST_ASSERT(fr_value_box_strdup(vb, vb, NULL, "plain", false) == 0);
1473
1475 TEST_CHECK_STRCMP(vb->vb_strvalue, "plain");
1476 TEST_CHECK_LEN(vb->vb_length, 5);
1477
1478 talloc_free(vb);
1479}
1480
1482{
1483 fr_value_box_t *vb;
1484
1485 TEST_CASE("Non-string input is cast to a string before escaping");
1486 vb = fr_value_box_alloc(NULL, FR_TYPE_UINT32, NULL);
1487 TEST_ASSERT(vb != NULL);
1488 vb->vb_uint32 = 100;
1489
1491 TEST_CHECK(vb->type == FR_TYPE_STRING);
1492 TEST_CHECK_STRCMP(vb->vb_strvalue, "100");
1493
1494 talloc_free(vb);
1495}
1496
1498 /* Value replacement and in place escaping */
1499 { "bstrndup_replace", test_bstrndup_replace },
1500 { "escape_in_place_func_string", test_escape_in_place_func_string },
1501 { "escape_in_place_func_unchanged", test_escape_in_place_func_unchanged },
1502 { "escape_in_place_func_cast", test_escape_in_place_func_cast },
1503
1504 /* Comparison tests */
1505 { "cmp_uint32_equal", test_cmp_uint32_equal },
1506 { "cmp_uint32_less", test_cmp_uint32_less },
1507 { "cmp_int32", test_cmp_int32 },
1508 { "cmp_float64", test_cmp_float64 },
1509 { "cmp_string", test_cmp_string },
1510 { "cmp_octets", test_cmp_octets },
1511 { "cmp_octets_length", test_cmp_octets_length },
1512 { "cmp_bool", test_cmp_bool },
1513 { "cmp_different_types", test_cmp_different_types },
1514
1515 /* Comparison operator tests */
1516 { "cmp_op_eq", test_cmp_op_eq },
1517 { "cmp_op_ne", test_cmp_op_ne },
1518 { "cmp_op_lt", test_cmp_op_lt },
1519 { "cmp_op_gt", test_cmp_op_gt },
1520 { "cmp_op_le", test_cmp_op_le },
1521 { "cmp_op_ge", test_cmp_op_ge },
1522
1523 /* Cast tests */
1524 { "cast_uint32_to_uint64", test_cast_uint32_to_uint64 },
1525 { "cast_float_to_int64_bounds", test_cast_float_to_int64_bounds },
1526 { "cast_float_to_date_bounds", test_cast_float_to_date_bounds },
1527 { "cast_uint32_to_string", test_cast_uint32_to_string },
1528 { "cast_string_to_uint32", test_cast_string_to_uint32 },
1529 { "cast_string_returns_zero", test_cast_string_returns_zero },
1530 { "cast_int32_to_int64", test_cast_int32_to_int64 },
1531 { "cast_uint8_to_uint32", test_cast_uint8_to_uint32 },
1532 { "cast_same_type", test_cast_same_type },
1533 { "cast_bool_to_uint32", test_cast_bool_to_uint32 },
1534 { "cast_uint32_to_bool", test_cast_uint32_to_bool },
1535
1536 /* Cast in place */
1537 { "cast_in_place_uint32_to_uint64", test_cast_in_place_uint32_to_uint64 },
1538 { "cast_in_place_uint32_to_string", test_cast_in_place_uint32_to_string },
1539
1540 /* Copy tests */
1541 { "copy_uint32", test_copy_uint32 },
1542 { "copy_string", test_copy_string },
1543 { "copy_octets", test_copy_octets },
1544 { "copy_preserves_tainted", test_copy_preserves_tainted },
1545
1546 /* Safety propagation */
1547 { "copy_preserves_safe_for", test_copy_preserves_safe_for },
1548 { "cast_octets_to_string_preserves_safe_for", test_cast_octets_to_string_preserves_safe_for },
1549 { "cast_string_to_octets_preserves_safe_for", test_cast_string_to_octets_preserves_safe_for },
1550 { "cast_uint32_to_string_clears_safe_for", test_cast_uint32_to_string_clears_safe_for },
1551 { "concat_any_any_is_any", test_concat_any_any_is_any },
1552 { "concat_x_x_is_x", test_concat_x_x_is_x },
1553 { "concat_x_any_is_x", test_concat_x_any_is_x },
1554 { "concat_any_x_is_x", test_concat_any_x_is_x },
1555 { "concat_x_y_is_none", test_concat_x_y_is_none },
1556 { "concat_x_none_is_none", test_concat_x_none_is_none },
1557 { "concat_octets_x_x_is_x", test_concat_octets_x_x_is_x },
1558 { "concat_into_head_x_x_is_x", test_concat_into_head_x_x_is_x },
1559 { "list_safety_merge", test_list_safety_merge },
1560
1561 /* String operations */
1562 { "strdup", test_strdup },
1563 { "asprintf", test_asprintf },
1564 { "bstrndup", test_bstrndup },
1565
1566 /* Memory operations */
1567 { "memdup", test_memdup },
1568
1569 /* Hash tests */
1570 { "hash_same_values", test_hash_same_values },
1571 { "hash_different_values", test_hash_different_values },
1572 { "hash_string", test_hash_string },
1573
1574 /* Truthiness tests */
1575 { "truthy_bool", test_truthy_bool },
1576 { "truthy_uint32", test_truthy_uint32 },
1577 { "truthy_string", test_truthy_string },
1578 { "truthy_octets", test_truthy_octets },
1579
1580 /* Increment tests */
1581 { "increment_uint32", test_increment_uint32 },
1582 { "increment_uint8_overflow", test_increment_uint8_overflow },
1583 { "increment_int32", test_increment_int32 },
1584
1585 /* as_uint64 */
1586 { "as_uint64", test_as_uint64 },
1587
1588 /* Clear tests */
1589 { "clear", test_clear },
1590 { "clear_string", test_clear_string },
1591
1592 /* Tainted flag */
1593 { "tainted_flag", test_tainted_flag },
1594
1595 /* Network encode/decode */
1596 { "network_uint32", test_network_uint32 },
1597 { "network_uint64", test_network_uint64 },
1598 { "network_int32", test_network_int32 },
1599 { "network_bool", test_network_bool },
1600 { "network_length", test_network_length },
1601
1602 /* Print tests */
1603 { "print_uint32", test_print_uint32 },
1604 { "print_int32_negative", test_print_int32_negative },
1605 { "print_string", test_print_string },
1606 { "print_bool", test_print_bool },
1607 { "print_octets", test_print_octets },
1608
1609 /* From string parsing */
1610 { "from_str_uint32", test_from_str_uint32 },
1611 { "from_str_int32", test_from_str_int32 },
1612 { "from_str_bool", test_from_str_bool },
1613 { "from_str_float64", test_from_str_float64 },
1614 { "from_str_octets", test_from_str_octets },
1615 { "from_str_string", test_from_str_string },
1616
1617 /* Round-trip tests */
1618 { "round_trip_uint32", test_round_trip_uint32 },
1619 { "round_trip_int64", test_round_trip_int64 },
1620 { "round_trip_bool", test_round_trip_bool },
1621 { "round_trip_network_uint32", test_round_trip_network_uint32 },
1622 { "network_ipv6addr_scope_id", test_network_ipv6addr_scope_id },
1623
1625};
static int const char char buffer[256]
Definition acutest.h:641
#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
#define TEST_MSG(...)
Definition acutest.h:234
Common initialisation for acutest binaries.
#define TEST_CHECK_LEN(_got, _exp)
#define TEST_CHECK_RET(_got, _exp)
#define TEST_CHECK_STRCMP(_got, _exp)
#define NUM_ELEMENTS(_t)
Definition build.h:443
@ CMP_ERR
comparison failed
Definition build.h:114
#define FR_DBUFF_TMP(_start, _len_or_end)
Creates a compound literal to pass into functions which accept a dbuff.
Definition dbuff.h:556
#define fr_exit_now(_x)
Exit without calling atexit() handlers, producing a log message in debug builds.
Definition debug.h:272
talloc_free(hp)
IPv4/6 prefix.
unsigned short uint16_t
fr_type_t
@ FR_TYPE_IPV4_ADDR
32 Bit IPv4 Address.
@ FR_TYPE_ETHERNET
48 Bit Mac-Address.
@ FR_TYPE_IPV6_PREFIX
IPv6 Prefix.
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_NULL
Invalid (uninitialised) attribute type.
@ FR_TYPE_INT64
64 Bit signed integer.
@ FR_TYPE_DATE
Unix time stamp, always has value >2^31.
@ FR_TYPE_UINT32
32 Bit unsigned integer.
@ FR_TYPE_INT32
32 Bit signed integer.
@ FR_TYPE_UINT64
64 Bit unsigned integer.
@ FR_TYPE_IPV6_ADDR
128 Bit IPv6 Address.
@ FR_TYPE_IPV4_PREFIX
IPv4 Prefix.
@ FR_TYPE_BOOL
A truth value.
@ FR_TYPE_OCTETS
Raw octets.
@ FR_TYPE_GROUP
A grouping of other attributes.
@ FR_TYPE_FLOAT64
Double precision floating point.
unsigned int uint32_t
long int ssize_t
unsigned char uint8_t
ssize_t fr_slen_t
#define UINT8_MAX
#define FR_SBUFF_IN_CHAR_RETURN(_sbuff,...)
#define fr_sbuff_set(_dst, _src)
#define fr_sbuff_current(_sbuff_or_marker)
#define FR_SBUFF_IN_STRCPY_LITERAL_RETURN(_sbuff, _str)
#define fr_sbuff_extend(_sbuff_or_marker)
#define FR_SBUFF_SET_RETURN(_dst, _src)
#define FR_SBUFF(_sbuff_or_marker)
#define fr_sbuff_advance(_sbuff_or_marker, _len)
#define FR_SBUFF_OUT(_start, _len_or_end)
fr_aka_sim_id_type_t type
#define talloc_autofree_context
The original function is deprecated, so replace it with our version.
Definition talloc.h:55
static uint64_t fr_unix_time_unwrap(fr_unix_time_t time)
Definition time.h:161
@ T_OP_NE
Definition token.h:95
@ T_OP_CMP_EQ
Definition token.h:104
@ T_OP_LE
Definition token.h:98
@ T_OP_GE
Definition token.h:96
@ T_OP_GT
Definition token.h:97
@ T_OP_LT
Definition token.h:99
static fr_slen_t head
Definition xlat.h:410
void fr_perror(char const *fmt,...)
Print the current error to stderr with a prefix.
Definition strerror.c:737
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:454
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
fr_cmp_ret_t fr_value_box_cmp(fr_value_box_t const *a, fr_value_box_t const *b)
Compare two values.
Definition value.c:770
fr_slen_t fr_value_box_from_str(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, char const *in, size_t inlen, fr_sbuff_unescape_rules_t const *erules)
Definition value.c:6256
size_t fr_value_box_network_length(fr_value_box_t const *value)
Get the size of the value held by the fr_value_box_t.
Definition value.c:1431
uint32_t fr_value_box_hash(fr_value_box_t const *vb)
Hash the contents of a value box.
Definition value.c:7320
ssize_t fr_value_box_print(fr_sbuff_t *out, fr_value_box_t const *data, fr_sbuff_escape_rules_t const *e_rules)
Print one boxed value to a string.
Definition value.c:6293
ssize_t fr_value_box_from_network(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t type, fr_dict_attr_t const *enumv, fr_dbuff_t *dbuff, size_t len, bool tainted)
Decode a fr_value_box_t from serialized binary data.
Definition value.c:1942
int fr_value_box_cast(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, fr_value_box_t const *src)
Convert one type of fr_value_box_t to another.
Definition value.c:4071
int fr_value_box_asprintf(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, bool tainted, char const *fmt,...)
Print a formatted string using our internal printf wrapper and assign it to a value box.
Definition value.c:4851
int fr_value_box_escape_in_place_func(TALLOC_CTX *ctx, fr_value_box_t *vb, fr_sbuff_escape_func_t escape)
Escape a value box in place using an sbuff escape function.
Definition value.c:7168
int fr_value_box_copy(TALLOC_CTX *ctx, fr_value_box_t *dst, const fr_value_box_t *src)
Copy value data verbatim duplicating any buffers.
Definition value.c:4526
int fr_value_box_cmp_op(fr_token_t op, fr_value_box_t const *a, fr_value_box_t const *b)
Compare two attributes using an operator.
Definition value.c:1017
int fr_value_box_bstrndup_replace(TALLOC_CTX *ctx, fr_value_box_t *vb, char const *src, ssize_t len)
Free the existing buffer (if talloced) associated with the valuebox, and replace it with a copy of a ...
Definition value.c:4909
uint64_t fr_value_box_as_uint64(fr_value_box_t const *vb)
Return a uint64_t from a fr_value_box_t.
Definition value.c:4376
bool fr_value_box_is_truthy(fr_value_box_t const *in)
Check truthiness of values.
Definition value.c:7688
int fr_value_box_cast_in_place(TALLOC_CTX *ctx, fr_value_box_t *vb, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv)
Convert one type of fr_value_box_t to another in place.
Definition value.c:4326
void fr_value_box_increment(fr_value_box_t *vb)
Increment a boxed value.
Definition value.c:5431
int fr_value_box_strdup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, bool tainted)
Copy a nul terminated string to a fr_value_box_t.
Definition value.c:4763
void fr_value_box_clear(fr_value_box_t *data)
Clear/free any existing value and metadata.
Definition value.c:4509
ssize_t fr_value_box_to_network(fr_dbuff_t *dbuff, fr_value_box_t const *value)
Encode a single value box, serializing its contents in generic network format.
Definition value.c:1534
void fr_value_box_list_safety_merge(fr_value_box_t *out, fr_value_box_list_t const *list)
Set the safety of out to the most restrictive safety of any leaf box in a list.
Definition value.c:7671
int fr_value_box_bstrndup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, size_t len, bool tainted)
Copy a string to to a fr_value_box_t.
Definition value.c:5014
int fr_value_box_memdup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, uint8_t const *src, size_t len, bool tainted)
Copy a buffer to a fr_value_box_t.
Definition value.c:5255
int fr_value_box_list_concat_in_place(TALLOC_CTX *ctx, fr_value_box_t *out, fr_value_box_list_t *list, fr_type_t type, fr_value_box_list_action_t proc_action, bool flatten, size_t max_size)
Concatenate a list of value boxes.
Definition value.c:6793
@ FR_VALUE_BOX_LIST_FREE
Definition value.h:267
#define fr_value_box_alloc(_ctx, _type, _enumv)
Allocate a value box of a specific type.
Definition value.h:668
static fr_sbuff_err_t char size_t fr_sbuff_t * in
Definition value.h:1061
#define fr_value_box_mark_safe_for(_box, _safe_for)
Definition value.h:1124
static fr_slen_t data
Definition value.h:1366
#define fr_value_box_is_safe_for_only(_box, _safe_for)
Definition value.h:1132
#define fr_value_box_init_null(_vb)
Initialise an empty/null box that will be filled later.
Definition value.h:640
static fr_sbuff_err_t char ** out
Definition value.h:1061
#define fr_value_box(_box, _var, _tainted)
Automagically fill in a box, determining the value type from the type of the C variable.
Definition value.h:935
#define FR_VALUE_BOX_SAFE_FOR_NONE
Definition value.h:177
uintptr_t fr_value_box_safe_for_t
Escaping that's been applied to a value box.
Definition value.h:167
#define fr_value_box_init(_vb, _type, _enumv, _tainted)
Initialise a fr_value_box_t.
Definition value.h:634
#define FR_VALUE_BOX_SAFE_FOR_ANY
Definition value.h:178
static void test_clear(void)
static void test_escape_in_place_func_cast(void)
static void test_cmp_int32(void)
Definition value_tests.c:71
static void test_strdup(void)
static void test_copy_string(void)
static void test_tainted_flag(void)
static void test_from_str_int32(void)
static void test_cast_uint32_to_string_clears_safe_for(void)
TEST_LIST
static void test_cmp_op_ge(void)
static void test_cmp_uint32_equal(void)
Definition value_tests.c:50
static void test_cmp_op_ne(void)
static void test_concat_x_y_is_none(void)
static void test_cmp_octets_length(void)
static void safe_for_list_build(fr_value_box_list_t *list, fr_value_box_safe_for_t const tokens[], size_t num)
Build a list of single character string boxes, each marked with the matching token.
static void test_concat_x_none_is_none(void)
static void test_cast_float_to_date_bounds(void)
A float cast to a date must fail when the date does not fit in int64_t nanoseconds.
static void test_round_trip_network_uint32(void)
static void test_network_int32(void)
static void test_cast_uint32_to_uint64(void)
static void test_as_uint64(void)
static void test_from_str_float64(void)
static void test_from_str_string(void)
static void test_print_octets(void)
static void test_network_uint32(void)
static TALLOC_CTX * autofree
Definition value_tests.c:31
static void test_round_trip_uint32(void)
static void test_copy_uint32(void)
static void test_network_ipv6addr_scope_id(void)
The scope_id of an IPv6 address is one byte on the wire, not the four bytes of fr_ipaddr_t....
static void test_print_string(void)
static void test_round_trip_int64(void)
static void test_cast_float_to_int64_bounds(void)
A float at or past the top of a 64-bit integer must fail the cast, not convert.
static void test_asprintf(void)
static void test_print_uint32(void)
static void test_network_uint64(void)
static void test_network_length(void)
static void test_concat_into_head_x_x_is_x(void)
The list head doubles as the output box.
static void test_cast_uint32_to_string(void)
static void test_cast_octets_to_string_preserves_safe_for(void)
static void test_bstrndup(void)
static void test_print_int32_negative(void)
static void test_increment_uint32(void)
static void test_concat_any_x_is_x(void)
static void test_bstrndup_replace(void)
static void test_cast_int32_to_int64(void)
static void test_round_trip_bool(void)
static void test_cast_in_place_uint32_to_string(void)
static void test_list_safety_merge(void)
static void test_increment_uint8_overflow(void)
static void test_truthy_bool(void)
static void test_hash_different_values(void)
static void test_cast_uint8_to_uint32(void)
static void test_escape_in_place_func_string(void)
static void test_cmp_op_lt(void)
static void test_cast_same_type(void)
static void test_cmp_octets(void)
static void test_copy_preserves_safe_for(void)
static void test_cast_bool_to_uint32(void)
static void test_escape_in_place_func_unchanged(void)
static void test_from_str_bool(void)
static void test_concat_x_any_is_x(void)
static void test_concat_octets_x_x_is_x(void)
static void test_cmp_op_eq(void)
static void test_cast_in_place_uint32_to_uint64(void)
static void test_cast_uint32_to_bool(void)
static void test_copy_preserves_tainted(void)
static void test_cmp_different_types(void)
static void test_concat_any_any_is_any(void)
static void test_concat_x_x_is_x(void)
static void test_cmp_string(void)
Definition value_tests.c:99
static void test_hash_same_values(void)
static fr_slen_t test_percent_escape(fr_sbuff_t *out, fr_sbuff_t *in)
Escape function for the in-place escape tests, writes '' as "%25".
static void test_cmp_float64(void)
Definition value_tests.c:85
static void test_truthy_octets(void)
static void test_from_str_octets(void)
static void test_cast_string_to_uint32(void)
static void test_clear_string(void)
static void test_init(void)
Definition value_tests.c:33
static void test_hash_string(void)
static void test_cmp_op_le(void)
static void test_truthy_uint32(void)
static void test_memdup(void)
static void test_increment_int32(void)
static void test_cmp_op_gt(void)
static void test_cast_string_returns_zero(void)
static void safe_for_concat_check(fr_type_t type, fr_value_box_safe_for_t const tokens[], size_t num, fr_value_box_safe_for_t expect)
Concatenate a list into a separate output box and check the resulting safe_for.
static void test_print_bool(void)
static void test_truthy_string(void)
static void test_cmp_bool(void)
#define SAFE_FOR_Y
static void test_cmp_uint32_less(void)
Definition value_tests.c:60
static void test_cast_string_to_octets_preserves_safe_for(void)
static void test_copy_octets(void)
static void test_from_str_uint32(void)
static void test_network_bool(void)
#define SAFE_FOR_X