diff options
author | Michele Papalini <micpapal@cisco.com> | 2022-10-21 14:19:42 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@fd.io> | 2022-10-21 14:19:42 +0000 |
commit | b72257cade6be6fb09738f228d3b961321ca25f3 (patch) | |
tree | 02e67dd7e5de6fc4d5d5a0deea0eeb2a9efb7358 | |
parent | a2b78a5731a5afb3555ad350ea50e5939dad7f88 (diff) | |
parent | d333065abaf0891f4004ca92ef3fadf32eb70767 (diff) |
Merge "fix(hicn-light): fib_contains incorrectly matches subprefixes"
-rw-r--r-- | hicn-light/src/hicn/core/fib.c | 4 | ||||
-rw-r--r-- | hicn-light/src/hicn/test/test-fib.cc | 51 |
2 files changed, 53 insertions, 2 deletions
diff --git a/hicn-light/src/hicn/core/fib.c b/hicn-light/src/hicn/core/fib.c index 2e8492457..70c5599fb 100644 --- a/hicn-light/src/hicn/core/fib.c +++ b/hicn-light/src/hicn/core/fib.c @@ -381,6 +381,8 @@ fib_entry_t *fib_contains(const fib_t *fib, const hicn_prefix_t *prefix) { if (!curr) return NULL; if (search.match_len != prefix_len) return NULL; + if (prefix_len != search.prefix_len) return NULL; + return curr->is_used ? curr->entry : NULL; } @@ -488,7 +490,7 @@ static void fib_node_remove(fib_t *fib, const hicn_prefix_t *prefix) { case 0: _fib_remove(fib, curr, search.parent); - if (!search.parent->is_used) + if (search.parent && !search.parent->is_used) _fib_remove(fib, search.parent, search.gparent); break; } diff --git a/hicn-light/src/hicn/test/test-fib.cc b/hicn-light/src/hicn/test/test-fib.cc index d2c5d1e1c..6e1f59077 100644 --- a/hicn-light/src/hicn/test/test-fib.cc +++ b/hicn-light/src/hicn/test/test-fib.cc @@ -49,10 +49,11 @@ class FibTest : public ::testing::Test { fib_t *fib; }; -void _fib_add_prefix(fib_t *fib, const hicn_prefix_t *prefix) { +fib_entry_t *_fib_add_prefix(fib_t *fib, const hicn_prefix_t *prefix) { fib_entry_t *entry = fib_entry_create(prefix, STRATEGY_TYPE_UNDEFINED, NULL, NULL); fib_add(fib, entry); + return entry; } #if 0 @@ -119,6 +120,7 @@ TEST_F(FibTest, FibAddFive) { HICN_PREFIX(b002_3, "b002::3/128"); HICN_PREFIX(inner_b002_2, "b002::2/127"); HICN_PREFIX(inner_b002_abcd_0, "b002::abcd:0:0:0/127"); + const hicn_prefix_t *prefix_array[] = { &b002_2, &inner_b002_2, &b002_3, &b002, &b002_abcd_0, &inner_b002_abcd_0, &b002_abcd_1}; @@ -135,3 +137,50 @@ TEST_F(FibTest, FibAddFive) { EXPECT_TRUE(fib_is_valid(fib)); EXPECT_TRUE(fib_check_preorder(fib, prefix_array, used_array)); } + +TEST_F(FibTest, FibAddRemove) { + HICN_PREFIX(b002_64, "b002::/64"); + HICN_PREFIX(b002_128, "b002::/128"); + + const hicn_prefix_t *prefix_array_1[] = {&b002_128}; + bool used_array_1[] = {true}; + const hicn_prefix_t *prefix_array_2[] = {}; + bool used_array_2[] = {}; + const hicn_prefix_t *prefix_array_3[] = {&b002_64}; + bool used_array_3[] = {true}; + + fib_entry_t *entry = _fib_add_prefix(fib, &b002_128); + fib_dump(fib); + EXPECT_TRUE(fib_is_valid(fib)); + EXPECT_TRUE(fib_check_preorder(fib, prefix_array_1, used_array_1)); + + fib_remove_entry(fib, entry); + fib_dump(fib); + EXPECT_TRUE(fib_is_valid(fib)); + EXPECT_TRUE(fib_check_preorder(fib, prefix_array_2, used_array_2)); + + entry = _fib_add_prefix(fib, &b002_64); + fib_dump(fib); + EXPECT_TRUE(fib_is_valid(fib)); + EXPECT_TRUE(fib_check_preorder(fib, prefix_array_3, used_array_3)); +} + +TEST_F(FibTest, FibAddNested) { + HICN_PREFIX(b002_64, "b002::/64"); + HICN_PREFIX(b002_128, "b002::/128"); + + const hicn_prefix_t *prefix_array_1[] = {&b002_128}; + bool used_array_1[] = {true}; + const hicn_prefix_t *prefix_array_2[] = {&b002_128, &b002_64}; + bool used_array_2[] = {true, true}; + + _fib_add_prefix(fib, &b002_128); + fib_dump(fib); + EXPECT_TRUE(fib_is_valid(fib)); + EXPECT_TRUE(fib_check_preorder(fib, prefix_array_1, used_array_1)); + + _fib_add_prefix(fib, &b002_64); + fib_dump(fib); + EXPECT_TRUE(fib_is_valid(fib)); + EXPECT_TRUE(fib_check_preorder(fib, prefix_array_2, used_array_2)); +} |