The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
internal_encode_tests.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program 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
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; 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 the internal encoder writing into a talloc dbuff that must grow
18 *
19 * @file src/lib/util/test/internal_encode_tests.c
20 *
21 * @copyright 2026 The FreeRADIUS server project
22 */
23static void test_init(void);
24static void test_fini(void);
25#define TEST_GLOBAL_INIT test_init()
26#define TEST_GLOBAL_FINI test_fini()
27
28#include "acutest_common_init.h"
29
30#include <freeradius-devel/util/conf.h>
31#include <freeradius-devel/util/dbuff.h>
32#include <freeradius-devel/util/dict_test.h>
33#include <freeradius-devel/util/pair.h>
34#include <freeradius-devel/internal/internal.h>
35
36static TALLOC_CTX *autofree;
39
40/** Global initialisation
41 */
42static void test_init(void)
43{
45 if (!autofree) {
46 error:
47 fr_perror("internal_encode_tests");
48 fr_exit_now(EXIT_FAILURE);
49 }
50
51 /*
52 * Mismatch between the binary and the libraries it depends on
53 */
54 if (fr_check_lib_magic(RADIUSD_MAGIC_NUMBER) < 0) goto error;
55
56 if (fr_dict_test_init(autofree, &test_dict, NULL) < 0) goto error;
57
58 /*
59 * The encoder checks every attribute against the root of the
60 * internal dictionary.
61 */
63}
64
65/** Release the internal dictionary before the dictionary context is freed
66 */
67static void test_fini(void)
68{
69 fr_dict_free(&internal_dict, __FILE__);
70}
71
72/** Encode one pair into talloc dbuffs of every initial size up to the encoded length
73 *
74 * Each initial size puts the end of the buffer at a different point in the
75 * encoding. A talloc dbuff grows on demand, so every size must produce the
76 * same bytes as the encoding into a fixed buffer.
77 */
79{
80 fr_pair_list_t list;
82 fr_dcursor_t cursor;
83 uint8_t ref[64] = { 0 };
84 ssize_t ref_len, slen;
85 size_t init;
86
87 fr_pair_list_init(&list);
89 TEST_ASSERT(fr_pair_value_strdup(vp, "bob", false) == 0);
90
91 fr_pair_dcursor_init(&cursor, &list);
92 ref_len = fr_internal_encode_pair(&FR_DBUFF_TMP(ref, sizeof(ref)), &cursor, NULL);
93 TEST_ASSERT(ref_len > 0);
94
95 for (init = 1; init <= (size_t)ref_len + 1; init++) {
96 fr_dbuff_t dbuff;
97 fr_dbuff_uctx_talloc_t tctx;
98
99 TEST_CASE("Initial size");
100
101 MEM(fr_dbuff_init_talloc(autofree, &dbuff, &tctx, init, 1024));
102
103 fr_pair_dcursor_init(&cursor, &list);
104 slen = fr_internal_encode_pair(&dbuff, &cursor, NULL);
105 TEST_CHECK(slen == ref_len);
106 TEST_MSG("init = %zu, expected %zd, got %zd", init, ref_len, slen);
107 if (slen == ref_len) {
108 TEST_CHECK(memcmp(fr_dbuff_start(&dbuff), ref, (size_t)ref_len) == 0);
109 }
110
111 fr_dbuff_free_talloc(&dbuff);
112 }
113
114 fr_pair_list_free(&list);
115}
116
118 { "encode_talloc_extend", test_encode_talloc_extend },
119
121};
#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.
static void fr_dbuff_free_talloc(fr_dbuff_t *dbuff)
Free the talloc buffer associated with a dbuff.
Definition dbuff.h:495
#define fr_dbuff_start(_dbuff_or_marker)
Return the 'start' position of a dbuff or marker.
Definition dbuff.h:950
static fr_dbuff_t * fr_dbuff_init_talloc(TALLOC_CTX *ctx, fr_dbuff_t *dbuff, fr_dbuff_uctx_talloc_t *tctx, size_t init, size_t max)
Initialise a special dbuff which automatically extends as additional data is written.
Definition dbuff.h:453
#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 MEM(x)
Definition debug.h:38
#define fr_exit_now(_x)
Exit without calling atexit() handlers, producing a log message in debug builds.
Definition debug.h:272
int fr_dict_free(fr_dict_t **dict, char const *dependent)
Decrement the reference count on a previously loaded dictionary.
Definition dict_util.c:4466
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
int fr_dict_internal_afrom_file(fr_dict_t **out, char const *dict_subdir, char const *dependent)
(Re-)Initialize the special internal dictionary
static TALLOC_CTX * autofree
static fr_dict_t * internal_dict
static void test_encode_talloc_extend(void)
Encode one pair into talloc dbuffs of every initial size up to the encoded length.
static fr_dict_t * test_dict
static void test_init(void)
Global initialisation.
static void test_fini(void)
Release the internal dictionary before the dictionary context is freed.
long int ssize_t
unsigned char uint8_t
unsigned long int size_t
int fr_pair_append_by_da(TALLOC_CTX *ctx, fr_pair_t **out, fr_pair_list_t *list, fr_dict_attr_t const *da)
Alloc a new fr_pair_t (and append)
Definition pair.c:1416
int fr_pair_value_strdup(fr_pair_t *vp, char const *src, bool tainted)
Copy data into an "string" data type.
Definition pair.c:2598
void fr_pair_list_init(fr_pair_list_t *list)
Initialise a pair list header.
Definition pair.c:47
ssize_t fr_internal_encode_pair(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, void *encode_ctx)
Encode a data structure into an internal attribute.
Definition encode.c:282
init
Enter the EAP-IDENTITY state.
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:55
#define FR_DICTIONARY_INTERNAL_DIR
Definition conf.h:7
void fr_pair_list_free(fr_pair_list_t *list)
Free memory used by a valuepair list.
#define fr_pair_dcursor_init(_cursor, _list)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
Definition pair.h:605
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