diff options
-rw-r--r-- | hicn-light/src/hicn/base/bitmap.h | 12 | ||||
-rw-r--r-- | hicn-light/src/hicn/base/vector.h | 5 | ||||
-rw-r--r-- | hicn-light/src/hicn/core/listener.h | 6 | ||||
-rw-r--r-- | hicn-light/src/hicn/core/listener_table.c | 4 | ||||
-rw-r--r-- | hicn-light/src/hicn/core/listener_table.h | 5 | ||||
-rw-r--r-- | hicn-light/src/hicn/core/test/test-listener_table.cc | 101 |
6 files changed, 113 insertions, 20 deletions
diff --git a/hicn-light/src/hicn/base/bitmap.h b/hicn-light/src/hicn/base/bitmap.h index 908b28d87..46d473ff8 100644 --- a/hicn-light/src/hicn/base/bitmap.h +++ b/hicn-light/src/hicn/base/bitmap.h @@ -66,6 +66,13 @@ bitmap_ensure_pos(bitmap_t ** bitmap, off_t pos) } /** + * @brief Returns the allocated size of a bitmap. + * + * @see listener_table_get_by_id + */ +#define bitmap_get_alloc_size(bitmap) vector_get_alloc_size(bitmap) + +/** * @brief Retrieve the state of the i-th bit in the bitmap. * * @param[in] bitmap The bitmap to access. @@ -76,6 +83,7 @@ int bitmap_get(const bitmap_t * bitmap, off_t i) { size_t offset = i / BITMAP_WIDTH(bitmap); + assert(offset < bitmap_get_alloc_size(bitmap)); size_t pos = i % BITMAP_WIDTH(bitmap); size_t shift = BITMAP_WIDTH(bitmap) - pos - 1; return (bitmap[offset] >> shift) & 1; @@ -197,8 +205,4 @@ END: #define bitmap_free(bitmap) vector_free(bitmap) -#ifdef WITH_TESTS -#define bitmap_get_alloc_size(bitmap) vector_get_alloc_size(bitmap) -#endif /* WITH_TESTS */ - #endif /* UTIL_BITMAP_H */ diff --git a/hicn-light/src/hicn/base/vector.h b/hicn-light/src/hicn/base/vector.h index b369086fc..fd8401e6d 100644 --- a/hicn-light/src/hicn/base/vector.h +++ b/hicn-light/src/hicn/base/vector.h @@ -251,8 +251,9 @@ do { \ */ #define vector_len(vector) vector_hdr(vector)->cur_size -#ifdef WITH_TESTS +/** + * @brief Returns the allocated size of a vector. + */ #define vector_get_alloc_size(vector) vector_hdr(vector)->alloc_size -#endif /* WITH_TESTS */ #endif /* UTIL_VECTOR_H */ diff --git a/hicn-light/src/hicn/core/listener.h b/hicn-light/src/hicn/core/listener.h index b55074967..d0658270b 100644 --- a/hicn-light/src/hicn/core/listener.h +++ b/hicn-light/src/hicn/core/listener.h @@ -69,6 +69,12 @@ typedef struct { listener_t * listener_create(face_type_t type, const address_t * address, const char * interface_name, const char * symbolic, struct forwarder_s * forwarder); +/** + * @brief Helper function used inside 'listener_create' to + * setup variables in listener struct. + * + * @see listener_create + */ int listener_initialize(listener_t * listener, face_type_t type, const char * name, unsigned listener_id, const address_t * address, const char * interface_name, struct forwarder_s * forwarder); diff --git a/hicn-light/src/hicn/core/listener_table.c b/hicn-light/src/hicn/core/listener_table.c index 9750fbbe8..7a3987c37 100644 --- a/hicn-light/src/hicn/core/listener_table.c +++ b/hicn-light/src/hicn/core/listener_table.c @@ -104,3 +104,7 @@ listener_table_get_by_name(listener_table_t * table, const char * name) return NULL; return listener_table_at(table, kh_val(table->id_by_name, k)); } + +listener_t *_listener_table_get_by_id(listener_table_t *table, off_t id) { + return listener_table_get_by_id(table, id); +}
\ No newline at end of file diff --git a/hicn-light/src/hicn/core/listener_table.h b/hicn-light/src/hicn/core/listener_table.h index 3d21cc88f..292c489a5 100644 --- a/hicn-light/src/hicn/core/listener_table.h +++ b/hicn-light/src/hicn/core/listener_table.h @@ -176,6 +176,11 @@ do { ? listener_table_at(table, id) : NULL /** + * @brief Helper function to avoid macro expansion in c++ tests. Wrapper around 'listener_table_get_by_id'. + */ +listener_t *_listener_table_get_by_id(listener_table_t *table, off_t id); + +/** * @brief Returns the index of a given listener in the listener table. * * @param[in] table The listener table from which to retrieve the index. diff --git a/hicn-light/src/hicn/core/test/test-listener_table.cc b/hicn-light/src/hicn/core/test/test-listener_table.cc index a480a4a91..89bc3357f 100644 --- a/hicn-light/src/hicn/core/test/test-listener_table.cc +++ b/hicn-light/src/hicn/core/test/test-listener_table.cc @@ -28,6 +28,8 @@ extern "C" { #include <hicn/core/listener_table.h> } +#define LISTENER_NAME "listener_name_test" + class ListenerTableTest : public ::testing::Test { protected: ListenerTableTest() { @@ -38,35 +40,106 @@ protected: } listener_table_t *listener_table; + listener_t *listener; + listener_key_t key = { + .address = _ADDRESS4_LOCALHOST(1), + .type = FACE_TYPE_UDP + }; }; -TEST_F(ListenerTableTest, Create) +TEST_F(ListenerTableTest, CreateTable) { - /* Check listener_table allocation */ + // Check listener_table allocation EXPECT_NE(listener_table, nullptr); - /* Check listener_table size */ + // Check listener_table size size_t listener_table_size = listener_table_len(listener_table); EXPECT_EQ(listener_table_size, (size_t) 0); } TEST_F(ListenerTableTest, AddListener) { - listener_key_t key = { - .address = _ADDRESS4_LOCALHOST(1), - .type = FACE_TYPE_UDP - }; - listener_t *listener; - - listener_table_allocate(listener_table, listener, &key, "listener_name_test"); + // Add listener to listener table + listener_table_allocate(listener_table, listener, &key, LISTENER_NAME); size_t listener_table_size = listener_table_len(listener_table); EXPECT_EQ(listener_table_size, (size_t) 1); EXPECT_NE(listener, nullptr); - khiter_t k = kh_get_lt_name(listener_table->id_by_name, "listener_name_test"); - EXPECT_NE(k, kh_end(listener_table->id_by_name)); - k = kh_get_lt_key(listener_table->id_by_key, &key); - EXPECT_NE(k, kh_end(listener_table->id_by_key)); + // Get listener by name and by key + khiter_t k_name = kh_get_lt_name(listener_table->id_by_name, LISTENER_NAME); + EXPECT_NE(k_name, kh_end(listener_table->id_by_name)); + khiter_t k_key = kh_get_lt_key(listener_table->id_by_key, &key); + EXPECT_NE(k_key, kh_end(listener_table->id_by_key)); +} + +TEST_F(ListenerTableTest, GetListener) +{ + // Add listener to listener table + listener_table_allocate(listener_table, listener, &key, LISTENER_NAME); + size_t listener_table_size = listener_table_len(listener_table); + EXPECT_EQ(listener_table_size, (size_t) 1); + ASSERT_NE(listener, nullptr); + + // Get listener by name + listener_t *listener_retrieved = listener_table_get_by_name(listener_table, LISTENER_NAME); + ASSERT_NE(listener_retrieved, nullptr); + EXPECT_EQ(listener_retrieved, listener); + + // Get listener by key + listener_retrieved = listener_table_get_by_address(listener_table, key.type, &key.address); + ASSERT_NE(listener_retrieved, nullptr); + EXPECT_EQ(listener_retrieved, listener); +} + +TEST_F(ListenerTableTest, GetListenerWithIdOutOfRange) +{ + // Try to retrieve a listener with an index + // bigger than the current listener table size + ASSERT_DEATH({ + _listener_table_get_by_id(listener_table, ~0); + }, ".*Assertion.*"); +} + +TEST_F(ListenerTableTest, GetListenerWithInvalidId) +{ + // First listener inserted has always id equal to 0 + int non_valid_id = 5; + + listener_table_allocate(listener_table, listener, &key, LISTENER_NAME); + listener_t *listener_not_found = listener_table_get_by_id(listener_table, non_valid_id); + ASSERT_EQ(listener_not_found, nullptr); +} + +TEST_F(ListenerTableTest, GetListenerWithValidId) +{ + listener_table_allocate(listener_table, listener, &key, LISTENER_NAME); + int id = listener_table_get_listener_id(listener_table, listener); + listener_t *listener_found = listener_table_get_by_id(listener_table, id); + ASSERT_EQ(listener_found, listener); +} + +TEST_F(ListenerTableTest, RemoveListener) +{ + // Add listener (listerner name and key must be set) + listener_table_allocate(listener_table, listener, &key, LISTENER_NAME); + listener->name = (char*) LISTENER_NAME; + listener->key = key; + + // Remove listener + int id = listener_table_get_listener_id(listener_table, listener); + listener_table_remove_by_id(listener_table, id); + + // Check listener table size + size_t listener_table_size = listener_table_len(listener_table); + EXPECT_EQ(listener_table_size, (size_t) 0); + + // Check that previous listener is not valid anymore + listener_t *listener_not_found = listener_table_get_by_id(listener_table, id); + EXPECT_EQ(listener_not_found, nullptr); + listener_not_found = listener_table_get_by_name(listener_table, LISTENER_NAME); + EXPECT_EQ(listener_not_found, nullptr); + listener_not_found = listener_table_get_by_address(listener_table, key.type, &key.address); + EXPECT_EQ(listener_not_found, nullptr); } int main(int argc, char **argv) |