The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
edit_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/util/test//edit_tests.c
20 * @author Alan DeKok (aland@networkradius.com)
21 * @copyright 2021 Network RADIUS SAS (legal@networkradius.com)
22 */
23
24/**
25 * The 'TEST_INIT' macro provided by 'acutest.h' allowing to register a function to be called
26 * before call the unit tests. Therefore, It calls the function ALL THE TIME causing an overhead.
27 * That is why we are initializing test_init() by "__attribute__((constructor));" reducing the
28 * test execution by 50% of the time.
29 */
30#define USE_CONSTRUCTOR
31
32/*
33 * It should be declared before include the "acutest.h"
34 */
35#ifdef USE_CONSTRUCTOR
36static void test_init(void) __attribute__((constructor));
37#else
38static void test_init(void);
39# define TEST_INIT test_init()
40#endif
41
42#include "acutest.h"
43#include"acutest_helpers.h"
44#include "pair_test_helpers.h"
45
46#include <freeradius-devel/util/conf.h>
47#include <freeradius-devel/util/dict.h>
48#include <freeradius-devel/util/edit.h>
49
50static TALLOC_CTX *autofree;
52static fr_dict_t *test_dict;
53
54/** Global initialisation
55 */
56static void test_init(void)
57{
59 if (!autofree) {
60 error:
61 fr_perror("edit_tests");
62 fr_exit_now(EXIT_FAILURE);
63 }
64
65 /*
66 * Mismatch between the binary and the libraries it depends on
67 */
68 if (fr_check_lib_magic(RADIUSD_MAGIC_NUMBER) < 0) goto error;
69
70 if (fr_dict_test_init(autofree, &test_dict, NULL) < 0) goto error;
71
72 /* Initialize the "test_pairs" list */
74
75 if (fr_pair_test_list_alloc(autofree, &test_pairs, NULL) < 0) goto error;
76}
77
101
103{
104 int count;
105 fr_pair_t *vp;
106
108 TEST_CASE("Expected (count == 3) after undoing the edits");
109 TEST_CHECK(count == 3);
110
113
116
118}
119
120/*
121 * Tests functions
122 */
123static void test_pair_delete_head(void)
124{
125 fr_pair_t *vp;
127 size_t count;
129 int rcode;
130
131 TEST_CASE("Add 3 pairs and delete the first one");
132
134
136
137 el = fr_edit_list_alloc(NULL, 5, NULL);
138 fr_assert(el != NULL);
139
141 TEST_CHECK(rcode == 0);
142
144
146 TEST_CASE("Expected (count == 2) after deleting the head");
147 TEST_CHECK(count == 2);
148
150 TEST_CASE("head is now what was the second pair");
152
154}
155
157{
158 fr_pair_t *vp;
160 size_t count;
162 int rcode;
163
164 TEST_CASE("Add 3 pairs and delete the first one");
165
167
169
170 el = fr_edit_list_alloc(NULL, 5, NULL);
171 fr_assert(el != NULL);
172
174 TEST_CHECK(rcode == 0);
175
177 TEST_CASE("Expected (count == 2) after deleting the head");
178 TEST_CHECK(count == 2);
179
180 /*
181 * Abort the edit
182 */
184
186}
187
188static void test_pair_delete_middle(void)
189{
190 fr_pair_t *vp;
192 size_t count;
194 int rcode;
195
196 TEST_CASE("Add 3 pairs and delete the middle one");
197
199
202 fr_assert(vp != NULL);
203
204 el = fr_edit_list_alloc(NULL, 5, NULL);
205 fr_assert(el != NULL);
206
208 TEST_CHECK(rcode == 0);
209
211
212 /* let's count */
214 TEST_CASE("Expected (count == 2) after deleting the middle");
215 TEST_CHECK(count == 2);
216
219
222
224}
225
227{
228 fr_pair_t *vp, *middle;
230 size_t count;
232 int rcode;
233
234 TEST_CASE("Add 3 pairs and delete the middle one, then abort");
235
237
239 middle = fr_pair_list_next(&local_pairs, vp);
240 fr_assert(middle != NULL);
242
243 el = fr_edit_list_alloc(NULL, 5, NULL);
244 fr_assert(el != NULL);
245
246 rcode = fr_edit_list_pair_delete(el, &local_pairs, middle);
247 TEST_CHECK(rcode == 0);
248
250 TEST_CASE("Expected (count == 2) after deleting the middle");
251 TEST_CHECK(count == 2);
252
255
258
259 /*
260 * Abort the edit
261 */
263
265}
266
268{
269 fr_pair_t *vp;
271 size_t count;
273 int rcode;
274
275 TEST_CASE("Add 3 pairs and delete the last 2");
276
278
281 fr_assert(vp != NULL);
282
283 el = fr_edit_list_alloc(NULL, 5, NULL);
284 fr_assert(el != NULL);
285
286 rcode = fr_edit_list_pair_delete(el, &local_pairs, vp); /* middle */
287 TEST_CHECK(rcode == 0);
288
290 fr_assert(vp != NULL);
291
292 rcode = fr_edit_list_pair_delete(el, &local_pairs, vp); /* tail */
293 TEST_CHECK(rcode == 0);
294
296
298 TEST_CASE("Expected (count == 1) after deleting the last 2");
299 TEST_CHECK(count == 1);
300
303
305 TEST_CHECK(vp->da == fr_dict_attr_test_uint32); /* head == tail */
306
308}
309
311{
312 fr_pair_t *vp;
314 size_t count;
316 int rcode;
317
318 TEST_CASE("Add 3 pairs and delete the last two, then abort");
319
321
323 fr_assert(vp != NULL);
325 fr_assert(vp != NULL);
327
328 el = fr_edit_list_alloc(NULL, 5, NULL);
329 fr_assert(el != NULL);
330
331 rcode = fr_edit_list_pair_delete(el, &local_pairs, vp); /* middle */
332 TEST_CHECK(rcode == 0);
333
335 fr_assert(vp != NULL);
336
337 rcode = fr_edit_list_pair_delete(el, &local_pairs, vp); /* tail */
338 TEST_CHECK(rcode == 0);
339
341 TEST_CASE("Expected (count == 1) after deleting the last 2");
342 TEST_CHECK(count == 1);
343
346
349
350 /*
351 * Abort the edit
352 */
354
356}
357
358
359static void test_pair_edit_value(void)
360{
361 fr_pair_t *vp;
364 int rcode;
365
366 TEST_CASE("Add 3 pairs and change the value of the first one");
367
369
371 fr_assert(vp != NULL);
372
373 el = fr_edit_list_alloc(NULL, 5, NULL);
374 fr_assert(el != NULL);
375
377 TEST_CHECK(rcode == 0);
378
379 TEST_CHECK(vp->vp_uint32 == 0);
380
381 vp->vp_uint32 = 1;
382 TEST_CHECK(vp->vp_uint32 == 1);
383
385
388 TEST_CHECK(vp->vp_uint32 == 1);
389
391}
392
394{
395 fr_pair_t *vp;
398 int rcode;
399
400 TEST_CASE("Add 3 pairs and change the value of the first one, then abort");
401
403
405 fr_assert(vp != NULL);
406
407 el = fr_edit_list_alloc(NULL, 5, NULL);
408 fr_assert(el != NULL);
409
411 TEST_CHECK(rcode == 0);
412
413 TEST_CHECK(vp->vp_uint32 == 0);
414
415 vp->vp_uint32 = 1;
416 TEST_CHECK(vp->vp_uint32 == 1);
417
418 /*
419 * Abort the edit
420 */
422
425 TEST_CHECK(vp->vp_uint32 == 0);
426
428}
429
431{
432 fr_pair_t *vp;
434 size_t count;
436 int rcode;
437
438 TEST_CASE("Add 3 pairs and insert a new one at the head");
439
441
442 el = fr_edit_list_alloc(NULL, 5, NULL);
443 fr_assert(el != NULL);
444
446 fr_assert(vp != NULL);
448 TEST_CHECK(rcode == 0);
449
451
453 TEST_CASE("Expected (count == 4) after inserting a new one");
454 TEST_CHECK(count == 4);
455
457 TEST_CASE("head is now what was the second pair");
459
461}
462
464{
465 fr_pair_t *vp;
467 size_t count;
469 int rcode;
470
471 TEST_CASE("Add 3 pairs and insert a new one at the head, then abort");
472
474
475 el = fr_edit_list_alloc(NULL, 5, NULL);
476 fr_assert(el != NULL);
477
479 fr_assert(vp != NULL);
481 TEST_CHECK(rcode == 0);
482
484 TEST_CASE("Expected (count == 4) after inserting a new one");
485 TEST_CHECK(count == 4);
486
487 /*
488 * Abort the edit
489 */
491
493}
494
496{
497 fr_pair_t *vp, *middle;
499 size_t count;
501 int rcode;
502
503 TEST_CASE("Add 3 pairs and insert a new one at the head");
504
506
508 middle = fr_pair_list_next(&local_pairs, vp);
509 fr_assert(middle != NULL);
510
511 el = fr_edit_list_alloc(NULL, 5, NULL);
512 fr_assert(el != NULL);
513
515 fr_assert(vp != NULL);
517 TEST_CHECK(rcode == 0);
518
520
522 TEST_CASE("Expected (count == 4) after inserting a new one");
523 TEST_CHECK(count == 4);
524
526}
527
529{
530 fr_pair_t *vp, *middle;
532 size_t count;
534 int rcode;
535
536 TEST_CASE("Add 3 pairs and insert a new one at the head, then abort");
537
539
541 middle = fr_pair_list_next(&local_pairs, vp);
542 fr_assert(middle != NULL);
543
544 el = fr_edit_list_alloc(NULL, 5, NULL);
545 fr_assert(el != NULL);
546
548 fr_assert(vp != NULL);
550 TEST_CHECK(rcode == 0);
551
553 TEST_CASE("Expected (count == 4) after inserting a new one");
554 TEST_CHECK(count == 4);
555
556 /*
557 * Abort the edit
558 */
560
562}
563
565{
566 fr_pair_t *vp;
569 int rcode, count;
570
571 TEST_CASE("Add 3 pairs, change the value of the first one, and delete it");
572
574
576 fr_assert(vp != NULL);
577
578 el = fr_edit_list_alloc(NULL, 5, NULL);
579 fr_assert(el != NULL);
580
582 TEST_CHECK(rcode == 0);
583
584 TEST_CHECK(vp->vp_uint32 == 0);
585
586 vp->vp_uint32 = 1;
587 TEST_CHECK(vp->vp_uint32 == 1);
588
590 TEST_CHECK(rcode == 0);
591
593
596
598 TEST_CASE("Expected (count == 2) after deleting the edited pair");
599 TEST_CHECK(count == 2);
600}
601
603{
604 fr_pair_t *vp;
607 int rcode;
608
609 TEST_CASE("Add 3 pairs, change the value of the first one, and delete it, then abort");
610
612
614 fr_assert(vp != NULL);
615
616 el = fr_edit_list_alloc(NULL, 5, NULL);
617 fr_assert(el != NULL);
618
620 TEST_CHECK(rcode == 0);
621
622 TEST_CHECK(vp->vp_uint32 == 0);
623
624 vp->vp_uint32 = 1;
625 TEST_CHECK(vp->vp_uint32 == 1);
626
628 TEST_CHECK(rcode == 0);
629
630 /*
631 * Abort the edit
632 */
634
637 TEST_CHECK(vp->vp_uint32 == 0);
638
640}
641
643{
644 fr_pair_t *vp;
646 size_t count;
648 int rcode;
649
650 TEST_CASE("Add 3 pairs and insert a new one at the head, and delete it");
651
653
654 el = fr_edit_list_alloc(NULL, 5, NULL);
655 fr_assert(el != NULL);
656
658 fr_assert(vp != NULL);
660 TEST_CHECK(rcode == 0);
661
663 TEST_CASE("Expected (count == 4) after inserting a new one");
664 TEST_CHECK(count == 4);
665
667 TEST_CHECK(rcode == 0);
668
670 TEST_CASE("Expected (count == 3) after deleting the just inserted on");
671 TEST_CHECK(count == 3);
672
674
676}
677
679{
680 fr_pair_t *vp;
682 size_t count;
684 int rcode;
685
686 TEST_CASE("Add 3 pairs and insert a new one at the head and delete it, then abort");
687
689
690 el = fr_edit_list_alloc(NULL, 5, NULL);
691 fr_assert(el != NULL);
692
694 fr_assert(vp != NULL);
696 TEST_CHECK(rcode == 0);
697
699 TEST_CASE("Expected (count == 4) after inserting a new one");
700 TEST_CHECK(count == 4);
701
702 /*
703 * Abort the edit
704 */
706
708}
709
710
712{
713 fr_pair_t *vp;
715 fr_edit_list_t *el, *child;
716 int rcode;
717
718 TEST_CASE("Add 3 pairs and change the value of the first one in a child transaction");
719
721
723 fr_assert(vp != NULL);
724
725 el = fr_edit_list_alloc(NULL, 5, NULL);
726 fr_assert(el != NULL);
727
729 TEST_CHECK(rcode == 0);
730
731 TEST_CHECK(vp->vp_uint32 == 0);
732
733 vp->vp_uint32 = 1;
734 TEST_CHECK(vp->vp_uint32 == 1);
735
736 child = fr_edit_list_alloc(NULL, 5, el);
737 fr_assert(child != NULL);
738
739 rcode = fr_edit_list_save_pair_value(child, vp); /* CHILD */
740 TEST_CHECK(rcode == 0);
741
742 TEST_CHECK(vp->vp_uint32 == 1);
743
744 vp->vp_uint32 = 2;
745 TEST_CHECK(vp->vp_uint32 == 2);
746
747 fr_edit_list_commit(child); /* should do nothing */
748
749 TEST_CHECK(vp->vp_uint32 == 2);
750
752
755 TEST_CHECK(vp->vp_uint32 == 2);
756
758}
759
761{
762 fr_pair_t *vp;
764 fr_edit_list_t *el, *child;
765 int rcode;
766
767 TEST_CASE("Add 3 pairs and change the value of the first one, then abort");
768
770
772 fr_assert(vp != NULL);
773
774 el = fr_edit_list_alloc(NULL, 5, NULL);
775 fr_assert(el != NULL);
776
778 TEST_CHECK(rcode == 0);
779
780 TEST_CHECK(vp->vp_uint32 == 0);
781
782 vp->vp_uint32 = 1;
783 TEST_CHECK(vp->vp_uint32 == 1);
784
785 child = fr_edit_list_alloc(NULL, 5, el);
786 fr_assert(child != NULL);
787
788 rcode = fr_edit_list_save_pair_value(child, vp); /* CHILD */
789 TEST_CHECK(rcode == 0);
790
791 TEST_CHECK(vp->vp_uint32 == 1);
792
793 vp->vp_uint32 = 2;
794 TEST_CHECK(vp->vp_uint32 == 2);
795
796 fr_edit_list_abort(child); /* CHILD */
797
798 TEST_CHECK(vp->vp_uint32 == 1);
799
801
804 TEST_CHECK(vp->vp_uint32 == 1);
805
807}
808
809static void test_pair_delete_tail(void)
810{
811 fr_pair_t *vp;
813 size_t count;
815 int rcode;
816
817 TEST_CASE("Add 3 pairs and delete the tail");
818
820
822 fr_assert(vp != NULL);
824
825 el = fr_edit_list_alloc(NULL, 5, NULL);
826 fr_assert(el != NULL);
827
829 TEST_CHECK(rcode == 0);
830
832
834 TEST_CASE("Expected (count == 2) after deleting the tail");
835 TEST_CHECK(count == 2);
836
839
841}
842
844{
845 fr_pair_t *vp;
847 size_t count;
849 int rcode;
850
851 TEST_CASE("Add 3 pairs and delete the tail, then abort");
852
854
856 fr_assert(vp != NULL);
858
859 el = fr_edit_list_alloc(NULL, 5, NULL);
860 fr_assert(el != NULL);
861
863 TEST_CHECK(rcode == 0);
864
866 TEST_CHECK(count == 2);
867
869
871}
872
873static void test_pair_delete_by_da(void)
874{
875 fr_pair_t *vp;
877 size_t count;
879 int rcode;
880
881 TEST_CASE("Add pairs and delete all with a matching da");
882
884
885 /* Add another uint32 at the tail */
887 fr_assert(vp != NULL);
888 vp->vp_uint32 = 42;
890
892 TEST_CHECK(count == 4);
893
894 el = fr_edit_list_alloc(NULL, 5, NULL);
895 fr_assert(el != NULL);
896
898 TEST_CHECK(rcode == 0);
899
901
903 TEST_CASE("Expected (count == 2) after deleting all uint32 pairs");
904 TEST_CHECK(count == 2);
905
908
911
913}
914
916{
917 fr_pair_t *vp;
919 size_t count;
921 int rcode;
922
923 TEST_CASE("Add pairs and delete all with a matching da, then abort");
924
926
928 fr_assert(vp != NULL);
929 vp->vp_uint32 = 42;
931
933 TEST_CHECK(count == 4);
934
935 el = fr_edit_list_alloc(NULL, 5, NULL);
936 fr_assert(el != NULL);
937
939 TEST_CHECK(rcode == 0);
940
942 TEST_CHECK(count == 2);
943
945
947 TEST_CASE("Expected (count == 4) after aborting the delete");
948 TEST_CHECK(count == 4);
949
952 TEST_CHECK(vp->vp_uint32 == 0);
953
956 TEST_CHECK(vp->vp_uint32 == 42);
957
959}
960
961static void test_pair_replace_value(void)
962{
963 fr_pair_t *vp;
966 int rcode;
967 fr_value_box_t box;
968
969 TEST_CASE("Add 3 pairs and replace the value of the first one");
970
972
974 fr_assert(vp != NULL);
975
976 el = fr_edit_list_alloc(NULL, 5, NULL);
977 fr_assert(el != NULL);
978
979 TEST_CHECK(vp->vp_uint32 == 0);
980
981 fr_value_box_init(&box, FR_TYPE_UINT32, NULL, false);
982 box.vb_uint32 = 42;
983
984 rcode = fr_edit_list_replace_pair_value(el, vp, &box);
985 TEST_CHECK(rcode == 0);
986 TEST_CHECK(vp->vp_uint32 == 42);
987
989
992 TEST_CHECK(vp->vp_uint32 == 42);
993
995}
996
998{
999 fr_pair_t *vp;
1002 int rcode;
1003 fr_value_box_t box;
1004
1005 TEST_CASE("Add 3 pairs and replace the value of the first one, then abort");
1006
1008
1010 fr_assert(vp != NULL);
1011
1012 el = fr_edit_list_alloc(NULL, 5, NULL);
1013 fr_assert(el != NULL);
1014
1015 TEST_CHECK(vp->vp_uint32 == 0);
1016
1017 fr_value_box_init(&box, FR_TYPE_UINT32, NULL, false);
1018 box.vb_uint32 = 42;
1019
1020 rcode = fr_edit_list_replace_pair_value(el, vp, &box);
1021 TEST_CHECK(rcode == 0);
1022 TEST_CHECK(vp->vp_uint32 == 42);
1023
1025
1028 TEST_CHECK(vp->vp_uint32 == 0);
1029
1031}
1032
1033static void test_pair_replace_pair(void)
1034{
1035 fr_pair_t *vp, *new_vp;
1037 size_t count;
1039 int rcode;
1040
1041 TEST_CASE("Add 3 pairs and replace the head with a new pair");
1042
1044
1046 fr_assert(vp != NULL);
1047
1048 el = fr_edit_list_alloc(NULL, 5, NULL);
1049 fr_assert(el != NULL);
1050
1052 fr_assert(new_vp != NULL);
1053 new_vp->vp_uint32 = 99;
1054
1055 rcode = fr_edit_list_replace_pair(el, &local_pairs, vp, new_vp);
1056 TEST_CHECK(rcode == 0);
1057
1059
1061 TEST_CASE("Expected (count == 3) after replacing a pair");
1062 TEST_CHECK(count == 3);
1063
1066 TEST_CHECK(vp->vp_uint32 == 99);
1067
1069}
1070
1072{
1073 fr_pair_t *vp, *new_vp;
1076 int rcode;
1077
1078 TEST_CASE("Add 3 pairs and replace the head, then abort");
1079
1081
1083 fr_assert(vp != NULL);
1084
1085 el = fr_edit_list_alloc(NULL, 5, NULL);
1086 fr_assert(el != NULL);
1087
1089 fr_assert(new_vp != NULL);
1090 new_vp->vp_uint32 = 99;
1091
1092 rcode = fr_edit_list_replace_pair(el, &local_pairs, vp, new_vp);
1093 TEST_CHECK(rcode == 0);
1094
1096 TEST_CHECK(vp->vp_uint32 == 99);
1097
1099
1102 TEST_CHECK(vp->vp_uint32 == 0);
1103
1105}
1106
1108{
1109 fr_pair_t *vp;
1111 size_t count;
1113 int rcode;
1114
1115 TEST_CASE("Add 3 pairs and insert a new one at the tail");
1116
1118
1119 el = fr_edit_list_alloc(NULL, 5, NULL);
1120 fr_assert(el != NULL);
1121
1123 fr_assert(vp != NULL);
1125 TEST_CHECK(rcode == 0);
1126
1128
1130 TEST_CASE("Expected (count == 4) after inserting at the tail");
1131 TEST_CHECK(count == 4);
1132
1135
1137}
1138
1140{
1141 fr_pair_t *vp;
1143 size_t count;
1145 int rcode;
1146
1147 TEST_CASE("Add 3 pairs and insert a new one at the tail, then abort");
1148
1150
1151 el = fr_edit_list_alloc(NULL, 5, NULL);
1152 fr_assert(el != NULL);
1153
1155 fr_assert(vp != NULL);
1157 TEST_CHECK(rcode == 0);
1158
1160 TEST_CHECK(count == 4);
1161
1163
1165}
1166
1168{
1169 fr_pair_t *vp, *child;
1171 size_t count;
1173 int rcode;
1174
1175 TEST_CASE("Add children to a TLV pair and free them");
1176
1178
1181
1183 fr_assert(child != NULL);
1184 fr_pair_append(&vp->vp_group, child);
1185 TEST_CHECK(fr_pair_list_num_elements(&vp->vp_group) == 1);
1186
1187 el = fr_edit_list_alloc(NULL, 5, NULL);
1188 fr_assert(el != NULL);
1189
1191 TEST_CHECK(rcode == 0);
1192 TEST_CHECK(fr_pair_list_empty(&vp->vp_group));
1193
1195
1196 TEST_CHECK(fr_pair_list_empty(&vp->vp_group));
1197
1199 TEST_CHECK(count == 3);
1200
1202}
1203
1205{
1206 fr_pair_t *vp, *child;
1209 int rcode;
1210
1211 TEST_CASE("Add children to a TLV pair, free them, then abort");
1212
1214
1217
1219 fr_assert(child != NULL);
1220 fr_pair_append(&vp->vp_group, child);
1221 TEST_CHECK(fr_pair_list_num_elements(&vp->vp_group) == 1);
1222
1223 el = fr_edit_list_alloc(NULL, 5, NULL);
1224 fr_assert(el != NULL);
1225
1227 TEST_CHECK(rcode == 0);
1228 TEST_CHECK(fr_pair_list_empty(&vp->vp_group));
1229
1231
1232 TEST_CHECK(fr_pair_list_num_elements(&vp->vp_group) == 1);
1233
1234 child = fr_pair_list_head(&vp->vp_group);
1236
1238}
1239
1241{
1242 fr_pair_t *vp;
1243 fr_pair_list_t local_pairs, to_insert;
1244 size_t count;
1246 int rcode;
1247
1248 TEST_CASE("Add 3 pairs and insert a list of 2 at the tail");
1249
1251
1252 fr_pair_list_init(&to_insert);
1253
1255 fr_assert(vp != NULL);
1256 fr_pair_append(&to_insert, vp);
1257
1259 fr_assert(vp != NULL);
1260 fr_pair_append(&to_insert, vp);
1261
1262 el = fr_edit_list_alloc(NULL, 5, NULL);
1263 fr_assert(el != NULL);
1264
1265 rcode = fr_edit_list_insert_list_tail(el, &local_pairs, &to_insert);
1266 TEST_CHECK(rcode == 0);
1267
1269
1271 TEST_CASE("Expected (count == 5) after inserting a list of 2");
1272 TEST_CHECK(count == 5);
1273
1276
1278}
1279
1281{
1282 fr_pair_t *vp;
1283 fr_pair_list_t local_pairs, to_insert;
1284 size_t count;
1286 int rcode;
1287
1288 TEST_CASE("Add 3 pairs and insert a list of 2 at the tail, then abort");
1289
1291
1292 fr_pair_list_init(&to_insert);
1293
1295 fr_assert(vp != NULL);
1296 fr_pair_append(&to_insert, vp);
1297
1299 fr_assert(vp != NULL);
1300 fr_pair_append(&to_insert, vp);
1301
1302 el = fr_edit_list_alloc(NULL, 5, NULL);
1303 fr_assert(el != NULL);
1304
1305 rcode = fr_edit_list_insert_list_tail(el, &local_pairs, &to_insert);
1306 TEST_CHECK(rcode == 0);
1307
1309 TEST_CHECK(count == 5);
1310
1312
1314}
1315
1317{
1318 fr_pair_t *vp;
1320 fr_edit_list_t *el, *child;
1321 int rcode;
1322
1323 TEST_CASE("Child commits value change, parent aborts - everything reverted");
1324
1326
1328 fr_assert(vp != NULL);
1329
1330 el = fr_edit_list_alloc(NULL, 5, NULL);
1331 fr_assert(el != NULL);
1332
1334 TEST_CHECK(rcode == 0);
1335
1336 TEST_CHECK(vp->vp_uint32 == 0);
1337
1338 vp->vp_uint32 = 1;
1339 TEST_CHECK(vp->vp_uint32 == 1);
1340
1341 child = fr_edit_list_alloc(NULL, 5, el);
1342 fr_assert(child != NULL);
1343
1344 rcode = fr_edit_list_save_pair_value(child, vp);
1345 TEST_CHECK(rcode == 0);
1346
1347 vp->vp_uint32 = 2;
1348 TEST_CHECK(vp->vp_uint32 == 2);
1349
1350 fr_edit_list_commit(child); /* does nothing, value stays at 2 */
1351 TEST_CHECK(vp->vp_uint32 == 2);
1352
1353 /* Abort parent - everything should revert */
1355
1358 TEST_CHECK(vp->vp_uint32 == 0);
1359
1361}
1362
1364 /*
1365 * Deletion.
1366 */
1367 { "pair_delete_head", test_pair_delete_head },
1368 { "pair_delete_head_abort", test_pair_delete_head_abort },
1369
1370 { "pair_delete_middle", test_pair_delete_middle },
1371 { "pair_delete_middle_abort", test_pair_delete_middle_abort },
1372
1373 { "pair_delete_multiple", test_pair_delete_multiple },
1374 { "pair_delete_multiple_abort", test_pair_delete_multiple_abort },
1375
1376 /*
1377 * Insert after
1378 */
1379 { "pair_insert_after_head", test_pair_insert_after_head },
1380 { "pair_insert_after_head_abort", test_pair_insert_after_head_abort },
1381
1382 { "pair_insert_after_middle", test_pair_insert_after_middle },
1383 { "pair_insert_after_middle_abort", test_pair_insert_after_middle_abort },
1384
1385 /*
1386 * Value modification
1387 */
1388 { "pair_edit_value", test_pair_edit_value },
1389 { "pair_edit_value_abort", test_pair_edit_value_abort },
1390
1391 /*
1392 * Value modification, then deletion
1393 */
1394 { "pair_edit_value_delete", test_pair_edit_value_delete },
1395 { "pair_edit_value_delete_abort", test_pair_edit_value_delete_abort },
1396
1397 /*
1398 * Insert after, then delete
1399 */
1400 { "pair_insert_after_head_delete", test_pair_insert_after_head_delete },
1401 { "pair_insert_after_head_delete_abort", test_pair_insert_after_head_delete_abort },
1402
1403 /*
1404 * Value modification in child list
1405 */
1406 { "pair_edit_child_value", test_pair_edit_child_value },
1407 { "pair_edit_child_value_abort", test_pair_edit_child_value_abort },
1408
1409 /*
1410 * Deletion of tail
1411 */
1412 { "pair_delete_tail", test_pair_delete_tail },
1413 { "pair_delete_tail_abort", test_pair_delete_tail_abort },
1414
1415 /*
1416 * Deletion by da
1417 */
1418 { "pair_delete_by_da", test_pair_delete_by_da },
1419 { "pair_delete_by_da_abort", test_pair_delete_by_da_abort },
1420
1421 /*
1422 * Value replacement (fr_edit_list_replace_pair_value)
1423 */
1424 { "pair_replace_value", test_pair_replace_value },
1425 { "pair_replace_value_abort", test_pair_replace_value_abort },
1426
1427 /*
1428 * Pair replacement (fr_edit_list_replace_pair)
1429 */
1430 { "pair_replace_pair", test_pair_replace_pair },
1431 { "pair_replace_pair_abort", test_pair_replace_pair_abort },
1432
1433 /*
1434 * Insert at tail
1435 */
1436 { "pair_insert_after_tail", test_pair_insert_after_tail },
1437 { "pair_insert_after_tail_abort", test_pair_insert_after_tail_abort },
1438
1439 /*
1440 * Free children of structural pair
1441 */
1442 { "pair_free_children", test_pair_free_children },
1443 { "pair_free_children_abort", test_pair_free_children_abort },
1444
1445 /*
1446 * Insert list at tail
1447 */
1448 { "pair_insert_list_tail", test_pair_insert_list_tail },
1449 { "pair_insert_list_tail_abort", test_pair_insert_list_tail_abort },
1450
1451 /*
1452 * Child commit + parent abort
1453 */
1454 { "pair_edit_child_commit_parent_abort", test_pair_edit_child_commit_parent_abort },
1455
1457};
#define TEST_CHECK(cond)
Definition acutest.h:87
#define TEST_CASE(name)
Definition acutest.h:186
#define TEST_TERMINATOR
Definition acutest.h:64
TALLOC_CTX * autofree
Definition common.c:29
#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_tlv_string
Definition dict_test.c:65
fr_dict_attr_t const * fr_dict_attr_test_tlv
Definition dict_test.c:64
fr_dict_attr_t const * fr_dict_attr_test_uint32
Definition dict_test.c:47
fr_dict_attr_t const * fr_dict_attr_test_octets
Definition dict_test.c:31
fr_dict_attr_t const * fr_dict_attr_test_string
Definition dict_test.c:30
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
fr_dict_attr_t const * fr_dict_attr_test_uint16
Definition dict_test.c:46
static void test_pair_delete_multiple(void)
Definition edit_tests.c:267
static void test_pair_insert_list_tail(void)
static void test_pair_delete_head(void)
Definition edit_tests.c:123
TEST_LIST
static void test_pair_delete_head_abort(void)
Definition edit_tests.c:156
static void test_pair_delete_tail(void)
Definition edit_tests.c:809
static void test_pair_replace_value_abort(void)
Definition edit_tests.c:997
static void test_pair_delete_multiple_abort(void)
Definition edit_tests.c:310
static void test_pair_edit_value_delete_abort(void)
Definition edit_tests.c:602
static void test_pair_insert_after_head_delete_abort(void)
Definition edit_tests.c:678
static void test_pair_delete_middle_abort(void)
Definition edit_tests.c:226
static void expect3(fr_pair_list_t *local_pairs)
Definition edit_tests.c:102
static void test_pair_replace_pair(void)
static void test_pair_edit_child_commit_parent_abort(void)
static void test_pair_delete_by_da(void)
Definition edit_tests.c:873
static void test_pair_edit_value(void)
Definition edit_tests.c:359
static void test_pair_free_children(void)
static void test_pair_insert_after_tail_abort(void)
static void test_pair_delete_by_da_abort(void)
Definition edit_tests.c:915
static void test_pair_replace_value(void)
Definition edit_tests.c:961
static void test_pair_insert_after_middle(void)
Definition edit_tests.c:495
static void test_pair_edit_child_value(void)
Definition edit_tests.c:711
static void add_pairs(fr_pair_list_t *local_pairs)
Definition edit_tests.c:78
static void test_pair_insert_after_head_abort(void)
Definition edit_tests.c:463
static void test_pair_insert_after_tail(void)
static void test_pair_insert_after_head(void)
Definition edit_tests.c:430
static void test_pair_insert_after_head_delete(void)
Definition edit_tests.c:642
static void test_pair_edit_child_value_abort(void)
Definition edit_tests.c:760
static void test_pair_delete_middle(void)
Definition edit_tests.c:188
static void test_pair_free_children_abort(void)
static void test_pair_insert_list_tail_abort(void)
static void test_pair_edit_value_delete(void)
Definition edit_tests.c:564
static void test_pair_delete_tail_abort(void)
Definition edit_tests.c:843
static void test_init(void)
Global initialisation.
Definition edit_tests.c:36
static void test_pair_replace_pair_abort(void)
static void test_pair_edit_value_abort(void)
Definition edit_tests.c:393
static void test_pair_insert_after_middle_abort(void)
Definition edit_tests.c:528
@ FR_TYPE_UINT32
32 Bit unsigned integer.
int fr_pair_append(fr_pair_list_t *list, fr_pair_t *to_add)
Add a VP to the end of the list.
Definition pair.c:1352
fr_pair_t * fr_pair_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
Dynamically allocate a new attribute and assign a fr_dict_attr_t.
Definition pair.c:290
void fr_pair_list_init(fr_pair_list_t *list)
Initialise a pair list header.
Definition pair.c:46
static fr_pair_list_t test_pairs
static fr_dict_t * test_dict
Helper functions for pair tests.
static int fr_pair_test_list_alloc(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_test_attr_t const *test_defs)
#define fr_assert(_expr)
Definition rad_assert.h:37
#define local_pairs
Convenience macro for accessing the state list.
Definition request.h:162
return count
Definition module.c:155
fr_pair_t * vp
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
fr_dict_attr_t const *_CONST da
Dictionary attribute defines the attribute number, vendor and type of the pair.
Definition pair.h:69
#define talloc_autofree_context
The original function is deprecated, so replace it with our version.
Definition talloc.h:55
static fr_event_list_t * el
int fr_edit_list_pair_delete(fr_edit_list_t *el, fr_pair_list_t *list, fr_pair_t *vp)
Delete a VP.
Definition edit.c:608
int fr_edit_list_replace_pair_value(fr_edit_list_t *el, fr_pair_t *vp, fr_value_box_t *box)
Write a new value to the fr_value_box_t.
Definition edit.c:659
void fr_edit_list_commit(fr_edit_list_t *el)
Commit an edit list.
Definition edit.c:853
int fr_edit_list_insert_pair_after(fr_edit_list_t *el, fr_pair_list_t *list, fr_pair_t *pos, fr_pair_t *vp)
Insert a new VP after an existing one.
Definition edit.c:595
int fr_edit_list_save_pair_value(fr_edit_list_t *el, fr_pair_t *vp)
Record the value of a leaf fr_value_box_t.
Definition edit.c:646
void fr_edit_list_abort(fr_edit_list_t *el)
Abort the entries in an edit list.
Definition edit.c:202
int fr_edit_list_replace_pair(fr_edit_list_t *el, fr_pair_list_t *list, fr_pair_t *to_replace, fr_pair_t *vp)
Replace a pair with another one.
Definition edit.c:677
int fr_edit_list_free_pair_children(fr_edit_list_t *el, fr_pair_t *vp)
Free children of a structural pair.
Definition edit.c:723
int fr_edit_list_pair_delete_by_da(fr_edit_list_t *el, fr_pair_list_t *list, fr_dict_attr_t const *da)
Delete VPs with a matching da.
Definition edit.c:622
fr_edit_list_t * fr_edit_list_alloc(TALLOC_CTX *ctx, int hint, fr_edit_list_t *parent)
Allocate an edit list.
Definition edit.c:802
Track a series of edits.
Definition edit.c:101
#define fr_edit_list_insert_pair_tail(_el, _list, _vp)
Definition edit.h:51
#define fr_edit_list_insert_list_tail(_el, _list, _to_insert)
Definition edit.h:74
bool fr_pair_list_empty(fr_pair_list_t const *list)
Is a valuepair list empty.
fr_pair_t * fr_pair_list_next(fr_pair_list_t const *list, fr_pair_t const *item))
Get the next item in a valuepair list after a specific entry.
Definition pair_inline.c:69
fr_pair_t * fr_pair_list_tail(fr_pair_list_t const *list)
Get the tail of a valuepair list.
Definition pair_inline.c:55
void fr_pair_list_free(fr_pair_list_t *list)
Free memory used by a valuepair list.
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
size_t fr_pair_list_num_elements(fr_pair_list_t const *list)
Get the length of a list of fr_pair_t.
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
#define fr_value_box_init(_vb, _type, _enumv, _tainted)
Initialise a fr_value_box_t.
Definition value.h:610