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 .dict = test_dict,
96 .internal = fr_dict_internal(),
97 };
98 relative = (fr_pair_parse_t) { };
99
100 fr_pair_list_init(&list);
101 len = strlen(buffer);
102
103 TEST_CASE("Create 'vp' using fr_pair_list_afrom_substr()");
104 TEST_CHECK(fr_pair_list_afrom_substr(&root, &relative, &FR_SBUFF_IN(buffer, len)) == len);
105
106 TEST_CASE("Looking for Test-Uint32-0");
107 TEST_CHECK((vp = fr_pair_find_by_da(&list, NULL, fr_dict_attr_test_uint32)) != NULL);
108
109 TEST_CASE("Validating PAIR_VERIFY()");
111
112 TEST_CASE("Checking if (Test-Uint32-0 == 123)");
113 TEST_CHECK(vp && vp->vp_uint32 == 123);
114
115 TEST_CASE("Looking for Test-String-0");
116 TEST_CHECK((vp = fr_pair_find_by_da(&list, NULL, fr_dict_attr_test_string)) != NULL);
117
118 TEST_CASE("Validating PAIR_VERIFY()");
120
121 TEST_CASE("Checking if (Test-String-0 == 'Testing123')");
122 TEST_CHECK(vp && strcmp(vp->vp_strvalue, "Testing123") == 0);
123
124 fr_pair_list_free(&list);
125}
126
127static FILE *open_buffer_as_file(uint8_t const *buffer, size_t buffer_len)
128{
129 FILE *fp;
130 uint8_t *our_buffer = UNCONST(uint8_t *, buffer);
131
132 TEST_CHECK((fp = fmemopen(our_buffer, buffer_len, "r")) != NULL);
133
134 fflush (fp);
135
136 return fp;
137}
138
140{
141 fr_pair_t *vp;
142 fr_pair_list_t list;
143 char const *buffer = "Test-Uint32-0 = 123\nTest-String-0 = \"Testing123\"\n";
144 FILE *fp = open_buffer_as_file((uint8_t const *)buffer, strlen(buffer) + 1);
145 bool pfiledone;
146
147 fr_pair_list_init(&list);
148
149 TEST_CASE("Create 'vp' using fr_pair_list_afrom_file()");
150 TEST_CHECK(fr_pair_list_afrom_file(autofree, test_dict, &list, fp, &pfiledone) == 0);
151
152 TEST_CASE("Looking for Test-Uint32-0");
153 TEST_CHECK((vp = fr_pair_find_by_da(&list, NULL, fr_dict_attr_test_uint32)) != NULL);
154
155 TEST_CASE("Validating PAIR_VERIFY()");
157
158 TEST_CASE("Checking if (Test-Uint32-0 == 123)");
159 TEST_CHECK(vp && vp->vp_uint32 == 123);
160
161 TEST_CASE("Looking for Test-String-0");
162 TEST_CHECK((vp = fr_pair_find_by_da(&list, NULL, fr_dict_attr_test_string)) != NULL);
163
164 TEST_CASE("Validating PAIR_VERIFY()");
166
167 TEST_CASE("Checking if (Test-String-0 == 'Testing123')");
168 TEST_CHECK(vp && strcmp(vp->vp_strvalue, "Testing123") == 0);
169
170 fr_pair_list_free(&list);
171
172 fclose(fp);
173}
174
176{
177 fr_pair_t *vp;
178 fr_pair_list_t old_list, new_list;
179 bool pfiledone;
180 char const *fake_file = "Test-Uint32-0 = 123\nTest-String-0 = \"Testing123\"\n";
181 FILE *fp = open_buffer_as_file((uint8_t const *)fake_file, strlen(fake_file) + 1);
182
183 fr_pair_list_init(&old_list);
184 fr_pair_list_init(&new_list);
185
186 TEST_CASE("Create 'vp' using fr_pair_list_afrom_file()");
187 TEST_CHECK(fr_pair_list_afrom_file(autofree, test_dict, &old_list, fp, &pfiledone) == 0);
188 TEST_CHECK(pfiledone == true);
189
190 TEST_CASE("Move pair from 'old_list' to 'new_list' using fr_pair_list_move_op()");
191 fr_pair_list_move_op(&new_list, &old_list, T_OP_ADD_EQ);
192
193 TEST_CASE("Looking for Test-Uint32-0");
194 TEST_CHECK((vp = fr_pair_find_by_da(&new_list, NULL, fr_dict_attr_test_uint32)) != NULL);
195
196 TEST_CASE("Validating PAIR_VERIFY()");
198
199 TEST_CHECK(vp != NULL);
200
201 TEST_CASE("Checking if (Test-Uint32-0 == 123)");
202 TEST_CHECK(vp && vp->vp_uint32 == 123);
203
204 TEST_CASE("Looking for Test-String-0");
205 TEST_CHECK((vp = fr_pair_find_by_da(&new_list, NULL, fr_dict_attr_test_string)) != NULL);
206
207 TEST_CASE("Validating PAIR_VERIFY()");
209
210 TEST_CHECK(vp != NULL);
211
212 TEST_CASE("Checking if (Test-String-0 == 'Testing123')");
213 TEST_CHECK(vp && strcmp(vp->vp_strvalue, "Testing123") == 0);
214
215 fr_pair_list_free(&new_list);
216
217 fclose(fp);
218}
219
221 /*
222 * Legacy calls
223 */
224 { "fr_pair_list_afrom_substr", test_fr_pair_list_afrom_substr },
225 { "fr_pair_list_afrom_file", test_fr_pair_list_afrom_file },
226 { "fr_pair_list_move_op", test_fr_pair_list_move_op },
227
229};
static int const char char buffer[256]
Definition acutest.h:578
#define TEST_CHECK(cond)
Definition acutest.h:87
#define TEST_CASE(name)
Definition acutest.h:186
#define TEST_TERMINATOR
Definition acutest.h:64
static TALLOC_CTX * autofree
Definition fuzzer.c:45
#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:226
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition dict_util.c:2690
fr_dict_t const * fr_dict_internal(void)
Definition dict_util.c:4943
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:703
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)
#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:202
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:732
int fr_check_lib_magic(uint64_t magic)
Check if the application linking to the library has the correct magic number.
Definition version.c:40
#define RADIUSD_MAGIC_NUMBER
Definition version.h:81