26#include <freeradius-devel/util/trie.h>
27#include <freeradius-devel/util/inet.h>
48 char const *data1 =
"hello";
49 char const *data2 =
"world";
50 char const *data3 =
"test";
52 uint8_t key1[4] = { 10, 0, 0, 0 };
53 uint8_t key2[4] = { 10, 0, 1, 0 };
54 uint8_t key3[4] = { 192, 168, 0, 0 };
55 uint8_t missing[4] = { 172, 16, 0, 0 };
65 TEST_CASE(
"Exact lookup finds correct data");
75 TEST_CASE(
"Lookup non-existent key returns NULL");
88 char const *data1 =
"first";
89 char const *data2 =
"second";
90 uint8_t key[4] = { 10, 0, 0, 1 };
113 char const *net8 =
"10/8";
114 char const *net16 =
"10.0/16";
115 char const *net24 =
"10.0.0/24";
125 TEST_CASE(
"Insert and find overlapping prefixes of 3 different lengths");
150 TEST_CASE(
"Longest prefix lookup for 10.0.0.5 returns 10.0.0/24");
152 uint8_t host[] = { 10, 0, 0, 5 };
157 TEST_CASE(
"Longest prefix lookup for 10.0.1.5 returns 10.0/16");
159 uint8_t host[] = { 10, 0, 1, 5 };
164 TEST_CASE(
"Longest prefix lookup for 10.1.0.1 returns 10/8");
166 uint8_t host[] = { 10, 1, 0, 1 };
173 uint8_t host[] = { 192, 168, 0, 1 };
187 char const *data1 =
"first";
188 char const *data2 =
"second";
190 uint8_t key1[] = { 10, 0, 0, 1 };
191 uint8_t key2[] = { 10, 0, 0, 2 };
203 TEST_CASE(
"Removed key is no longer found");
209 TEST_CASE(
"Remove non-existent key returns NULL");
226 if (ctx->
count < 32) {
227 memcpy(ctx->
keys[ctx->
count], key, (keylen + 7) / 8);
242 char const *
data[] = {
"a",
"b",
"c" };
243 uint8_t k1[] = { 10, 0, 0, 0 };
244 uint8_t k2[] = { 10, 0, 1, 0 };
245 uint8_t k3[] = { 192, 168, 0, 0 };
254 memset(&ctx, 0,
sizeof(ctx));
276 TEST_CASE(
"Insert 256 /32 entries under 10.0.0.0/8");
277 for (i = 0; i < 256; i++) {
282 TEST_MSG(
"insert host-%d failed", i);
286 for (i = 0; i < 256; i++) {
291 TEST_MSG(
"lookup host-%d failed", i);
295 for (i = 0; i < 256; i++) {
300 TEST_MSG(
"remove host-%d failed", i);
303 TEST_CASE(
"All entries removed, lookups return NULL");
304 for (i = 0; i < 256; i++) {
318 char const *net20 =
"10.0.0.0/20";
319 char const *net28 =
"10.0.0.0/28";
330 uint8_t k28[] = { 10, 0, 0, 0 };
336 TEST_CASE(
"Lookup 10.0.0.5 returns /28");
338 uint8_t host[] = { 10, 0, 0, 5 };
343 TEST_CASE(
"Lookup 10.0.1.5 returns /20");
345 uint8_t host[] = { 10, 0, 1, 5 };
#define TEST_ASSERT(cond)
void * fr_trie_remove_by_key(fr_trie_t *ft, void const *key, size_t keylen)
Remove a key and return the associated user ctx.
fr_trie_t * fr_trie_alloc(TALLOC_CTX *ctx, fr_trie_key_t get_key, fr_free_t free_data)
Allocate a trie.
int fr_trie_walk(fr_trie_t *ft, void *ctx, fr_trie_walk_t callback)
void * fr_trie_lookup_by_key(fr_trie_t const *ft, void const *key, size_t keylen)
Lookup a key in a trie and return user ctx, if any.
void * fr_trie_match_by_key(fr_trie_t const *ft, void const *key, size_t keylen)
Match a key and length in a trie and return user ctx, if any.
int fr_trie_insert_by_key(fr_trie_t *ft, void const *key, size_t keylen, void const *data)
Insert a key and user ctx into a trie.
static void test_trie_many(void)
static int walk_callback(uint8_t const *key, size_t keylen, UNUSED void *data, void *uctx)
static void test_trie_insert_duplicate(void)
static void test_trie_longest_prefix(void)
static void test_trie_alloc(void)
static void test_trie_bit_prefix(void)
static void test_trie_insert_lookup(void)
static void test_trie_remove(void)
static void test_trie_walk(void)