The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
pair_legacy_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/pair_legacy_tests.c
20 * @author Jorge Pereira <jpereira@freeradius.org>
21 * @copyright 2020 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 pair_tests_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 <freeradius-devel/util/acutest.h>
43#include <freeradius-devel/util/acutest_helpers.h>
44#include <freeradius-devel/util/pair_test_helpers.h>
45
46#include <freeradius-devel/util/conf.h>
47#include <freeradius-devel/util/dict.h>
48#include <freeradius-devel/util/pair_legacy.h>
49
50#ifdef HAVE_GPERFTOOLS_PROFILER_H
51# include <gperftools/profiler.h>
52#endif
53
54static TALLOC_CTX *autofree;
56static fr_dict_t *test_dict;
57
58
59/** Global initialisation
60 */
61static void test_init(void)
62{
64 if (!autofree) {
65 error:
66 fr_perror("pair_tests");
67 fr_exit_now(EXIT_FAILURE);
68 }
69
70 /*
71 * Mismatch between the binary and the libraries it depends on
72 */
73 if (fr_check_lib_magic(RADIUSD_MAGIC_NUMBER) < 0) goto error;
74
75 if (fr_dict_test_init(autofree, &test_dict, NULL) < 0) goto error;
76
77 /* Initialize the "test_pairs" list */
79
80 if (fr_pair_test_list_alloc(autofree, &test_pairs, NULL) < 0) goto error;
81}
82
84{
86 ssize_t len;
87 fr_pair_list_t list;
88 char const *buffer = "Test-Uint32-0 = 123, Test-String-0 = \"Testing123\"";
89 fr_pair_parse_t root, relative;
90
91 root = (fr_pair_parse_t) {
92 .ctx = autofree,
94 .list = &list,
95 };
96 relative = (fr_pair_parse_t) { };
97
98 fr_pair_list_init(&list);
99 len = strlen(buffer);
100
101 TEST_CASE("Create 'vp' using fr_pair_list_afrom_substr()");
102 TEST_CHECK(fr_pair_list_afrom_substr(&root, &relative, &FR_SBUFF_IN(buffer, len)) == len);
103
104 TEST_CASE("Looking for Test-Uint32-0");
105 TEST_CHECK((vp = fr_pair_find_by_da(&list, NULL, fr_dict_attr_test_uint32)) != NULL);
106
107 TEST_CASE("Validating PAIR_VERIFY()");
109
110 TEST_CASE("Checking if (Test-Uint32-0 == 123)");
111 TEST_CHECK(vp && vp->vp_uint32 == 123);
112
113 TEST_CASE("Looking for Test-String-0");
114 TEST_CHECK((vp = fr_pair_find_by_da(&list, NULL, fr_dict_attr_test_string)) != NULL);
115
116 TEST_CASE("Validating PAIR_VERIFY()");
118
119 TEST_CASE("Checking if (Test-String-0 == 'Testing123')");
120 TEST_CHECK(vp && strcmp(vp->vp_strvalue, "Testing123") == 0);
121
122 fr_pair_list_free(&list);
123}
124
125static FILE *open_buffer_as_file(uint8_t const *buffer, size_t buffer_len)
126{
127 FILE *fp;
128 uint8_t *our_buffer = UNCONST(uint8_t *, buffer);
129
130 TEST_CHECK((fp = fmemopen(our_buffer, buffer_len, "r")) != NULL);
131
132 fflush (fp);
133
134 return fp;
135}
136
138{
139 fr_pair_t *vp;
140 fr_pair_list_t list;
141 char const *buffer = "Test-Uint32-0 = 123\nTest-String-0 = \"Testing123\"\n";
142 FILE *fp = open_buffer_as_file((uint8_t const *)buffer, strlen(buffer) + 1);
143 bool pfiledone;
144
145 fr_pair_list_init(&list);
146
147 TEST_CASE("Create 'vp' using fr_pair_list_afrom_file()");
148 TEST_CHECK(fr_pair_list_afrom_file(autofree, test_dict, &list, fp, &pfiledone) == 0);
149
150 TEST_CASE("Looking for Test-Uint32-0");
151 TEST_CHECK((vp = fr_pair_find_by_da(&list, NULL, fr_dict_attr_test_uint32)) != NULL);
152
153 TEST_CASE("Validating PAIR_VERIFY()");
155
156 TEST_CASE("Checking if (Test-Uint32-0 == 123)");
157 TEST_CHECK(vp && vp->vp_uint32 == 123);
158
159 TEST_CASE("Looking for Test-String-0");
160 TEST_CHECK((vp = fr_pair_find_by_da(&list, NULL, fr_dict_attr_test_string)) != NULL);
161
162 TEST_CASE("Validating PAIR_VERIFY()");
164
165 TEST_CASE("Checking if (Test-String-0 == 'Testing123')");
166 TEST_CHECK(vp && strcmp(vp->vp_strvalue, "Testing123") == 0);
167
168 fr_pair_list_free(&list);
169
170 fclose(fp);
171}
172
174{
175 fr_pair_t *vp;
176 fr_pair_list_t old_list, new_list;
177 bool pfiledone;
178 char const *fake_file = "Test-Uint32-0 = 123\nTest-String-0 = \"Testing123\"\n";
179 FILE *fp = open_buffer_as_file((uint8_t const *)fake_file, strlen(fake_file) + 1);
180
181 fr_pair_list_init(&old_list);
182 fr_pair_list_init(&new_list);
183
184 TEST_CASE("Create 'vp' using fr_pair_list_afrom_file()");
185 TEST_CHECK(fr_pair_list_afrom_file(autofree, test_dict, &old_list, fp, &pfiledone) == 0);
186 TEST_CHECK(pfiledone == true);
187
188 TEST_CASE("Move pair from 'old_list' to 'new_list' using fr_pair_list_move_op()");
189 fr_pair_list_move_op(&new_list, &old_list, T_OP_ADD_EQ);
190
191 TEST_CASE("Looking for Test-Uint32-0");
192 TEST_CHECK((vp = fr_pair_find_by_da(&new_list, NULL, fr_dict_attr_test_uint32)) != NULL);
193
194 TEST_CASE("Validating PAIR_VERIFY()");
196
197 TEST_CHECK(vp != NULL);
198
199 TEST_CASE("Checking if (Test-Uint32-0 == 123)");
200 TEST_CHECK(vp && vp->vp_uint32 == 123);
201
202 TEST_CASE("Looking for Test-String-0");
203 TEST_CHECK((vp = fr_pair_find_by_da(&new_list, NULL, fr_dict_attr_test_string)) != NULL);
204
205 TEST_CASE("Validating PAIR_VERIFY()");
207
208 TEST_CHECK(vp != NULL);
209
210 TEST_CASE("Checking if (Test-String-0 == 'Testing123')");
211 TEST_CHECK(vp && strcmp(vp->vp_strvalue, "Testing123") == 0);
212
213 fr_pair_list_free(&new_list);
214
215 fclose(fp);
216}
217
219 /*
220 * Legacy calls
221 */
222 { "fr_pair_list_afrom_substr", test_fr_pair_list_afrom_substr },
223 { "fr_pair_list_afrom_file", test_fr_pair_list_afrom_file },
224 { "fr_pair_list_move_op", test_fr_pair_list_move_op },
225
226 { NULL }
227};
static int const char char buffer[256]
Definition acutest.h:576
#define TEST_CHECK(cond)
Definition acutest.h:85
#define TEST_CASE(name)
Definition acutest.h:184
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:167
#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_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition dict_util.c:2400
fr_dict_attr_t const * fr_dict_attr_test_uint32
Definition dict_test.c:47
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
long int ssize_t
unsigned char uint8_t
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:693
void fr_pair_list_init(fr_pair_list_t *list)
Initialise a pair list header.
Definition pair.c:46
fr_slen_t fr_pair_list_afrom_substr(fr_pair_parse_t const *root, fr_pair_parse_t *relative, fr_sbuff_t *in)
Parse a fr_pair_list_t from a substring.
int fr_pair_list_afrom_file(TALLOC_CTX *ctx, fr_dict_t const *dict, fr_pair_list_t *out, FILE *fp, bool *pfiledone)
Read valuepairs from the fp up to End-Of-File.
void fr_pair_list_move_op(fr_pair_list_t *to, fr_pair_list_t *from, fr_token_t op)
Move pairs from source list to destination list respecting operator.
struct fr_pair_parse_s fr_pair_parse_t
TALLOC_CTX * ctx
Definition pair_legacy.h:43
static void test_fr_pair_list_afrom_file(void)
static void test_fr_pair_list_move_op(void)
static FILE * open_buffer_as_file(uint8_t const *buffer, size_t buffer_len)
static void test_fr_pair_list_afrom_substr(void)
static void test_init(void)
Global initialisation.
static fr_pair_list_t test_pairs
static fr_dict_t * test_dict
static int fr_pair_test_list_alloc(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_test_attr_t const *test_defs)
static TALLOC_CTX * autofree
#define FR_SBUFF_IN(_start, _len_or_end)
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
@ T_OP_ADD_EQ
Definition token.h:69
#define PAIR_VERIFY(_x)
Definition pair.h:191
void fr_pair_list_free(fr_pair_list_t *list)
Free memory used by a valuepair list.
void fr_perror(char const *fmt,...)
Print the current error to stderr with a prefix.
Definition strerror.c:733
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