diff options
| author | luwenpeng <[email protected]> | 2024-09-23 16:50:09 +0800 |
|---|---|---|
| committer | luwenpeng <[email protected]> | 2024-09-27 19:11:47 +0800 |
| commit | 5799de529955798ed0727c088ffa25f1e4e51445 (patch) | |
| tree | 818422edc9c400d84a5393bfe4e581496825019d /common | |
| parent | 7ef8e44bca6bac3c905ed8d6a11cadd6edac156b (diff) | |
TSG-22348 feature: adapt maat support UUID
Diffstat (limited to 'common')
| -rw-r--r-- | common/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | common/include/control_packet.h | 5 | ||||
| -rw-r--r-- | common/include/utils.h | 24 | ||||
| -rw-r--r-- | common/src/control_packet.cpp | 20 | ||||
| -rw-r--r-- | common/src/kafka.cpp | 21 | ||||
| -rw-r--r-- | common/src/utils.cpp | 59 | ||||
| -rw-r--r-- | common/test/gtest_control_packet.cpp | 227 | ||||
| -rw-r--r-- | common/test/gtest_health_check_table.cpp | 88 | ||||
| -rw-r--r-- | common/test/gtest_utils.cpp | 141 |
9 files changed, 436 insertions, 151 deletions
diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 247786b..d5b0902 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -1,6 +1,6 @@ add_library(common src/session_table.cpp src/packet.cpp src/control_packet.cpp src/bfd.cpp src/utils.cpp src/vxlan.cpp src/log.cpp src/timestamp.cpp src/mpack.cpp src/kafka.cpp) target_link_libraries(common PUBLIC cjson) -target_link_libraries(common PUBLIC MESA_handle_logger rdkafka) +target_link_libraries(common PUBLIC MESA_handle_logger rdkafka uuid) target_include_directories(common PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include) diff --git a/common/include/control_packet.h b/common/include/control_packet.h index d955ec5..e36aad9 100644 --- a/common/include/control_packet.h +++ b/common/include/control_packet.h @@ -6,7 +6,7 @@ extern "C" { #endif -#include <stdint.h> +#include "utils.h" enum session_state { @@ -36,8 +36,7 @@ struct control_packet uint64_t session_id; enum session_state state; char method[32]; - uint64_t rule_ids[32]; - int rule_id_num; + struct uuid_array rule_uuid_array; }; const char *session_state_to_string(enum session_state state); diff --git a/common/include/utils.h b/common/include/utils.h index 8319ed1..8a64a3e 100644 --- a/common/include/utils.h +++ b/common/include/utils.h @@ -33,25 +33,29 @@ extern "C" #include <stdint.h> #include <sys/types.h> #include <netinet/in.h> +#include <uuid/uuid.h> /****************************************************************************** - * mutable_array + * uuid_array ******************************************************************************/ -struct mutable_array +#define UUID_STRING_SIZE 37 +#define MAX_RULE_NUM 128 + +struct uuid_array { - uint64_t elems[128]; + uuid_t uuids[MAX_RULE_NUM]; int num; int size; }; -void mutable_array_init(struct mutable_array *array); -void mutable_array_add_elem(struct mutable_array *array, uint64_t elem); -void mutable_array_del_elem(struct mutable_array *array, uint64_t elem); -int mutable_array_is_full(struct mutable_array *array); -int mutable_array_count_elem(struct mutable_array *array); -int mutable_array_exist_elem(struct mutable_array *array, uint64_t elem); -int mutable_array_index_elem(struct mutable_array *array, int index); +void uuid_array_init(struct uuid_array *array); +void uuid_array_append(struct uuid_array *array, uuid_t uuid); +void uuid_array_remove(struct uuid_array *array, uuid_t uuid); +int uuid_array_contains(struct uuid_array *array, uuid_t uuid); +int uuid_array_is_full(struct uuid_array *array); +int uuid_array_get_count(struct uuid_array *array); +uuid_t *uuid_array_get_at(struct uuid_array *array, int index); /****************************************************************************** * device diff --git a/common/src/control_packet.cpp b/common/src/control_packet.cpp index 27d3539..58e7f60 100644 --- a/common/src/control_packet.cpp +++ b/common/src/control_packet.cpp @@ -57,7 +57,9 @@ const char *session_state_to_string(enum session_state state) enum control_packet_state control_packet_parse(struct control_packet *handler, const char *data, size_t length) { memset(handler, 0, sizeof(struct control_packet)); + uuid_array_init(&handler->rule_uuid_array); + int rule_id_num; mpack_tree_t tree; mpack_node_t root; mpack_node_t temp; @@ -186,17 +188,18 @@ enum control_packet_state control_packet_parse(struct control_packet *handler, c state = CTRL_PKT_INVALID_RULE_IDS; goto error_out; } - handler->rule_id_num = MIN(mpack_node_array_length(temp), (int)(sizeof(handler->rule_ids) / sizeof(handler->rule_ids[0]))); - if (handler->rule_id_num <= 0) + rule_id_num = MIN(mpack_node_array_length(temp), MAX_RULE_NUM); + if (rule_id_num <= 0) { LOG_ERROR("%s: unexpected control packet: (invalid rule id num) %ld", LOG_TAG_CTRLPKT, mpack_node_array_length(temp)); state = CTRL_PKT_INVALID_RULE_IDS; goto error_out; } - for (int i = 0; i < handler->rule_id_num; i++) + for (int i = 0; i < rule_id_num; i++) { item = mpack_node_array_at(temp, i); - handler->rule_ids[i] = mpack_node_u64(item); + assert(mpack_node_bin_size(item) == 16); + uuid_array_append(&handler->rule_uuid_array, *(uuid_t *)mpack_node_bin_data(item)); } success_out: @@ -219,15 +222,18 @@ void control_packet_dump(struct control_packet *handler) { if (handler) { + int count = uuid_array_get_count(&handler->rule_uuid_array); LOG_INFO("%s: tsync : %s", LOG_TAG_POLICY, handler->tsync); LOG_INFO("%s: session_id : %lu", LOG_TAG_POLICY, handler->session_id); LOG_INFO("%s: state : %s", LOG_TAG_POLICY, session_state_to_string(handler->state)); LOG_INFO("%s: method : %s", LOG_TAG_POLICY, handler->method); - LOG_INFO("%s: rule_id_num : %d", LOG_TAG_POLICY, handler->rule_id_num); + LOG_INFO("%s: rule_id_num : %d", LOG_TAG_POLICY, count); - for (int i = 0; i < handler->rule_id_num; i++) + for (int i = 0; i < count; i++) { - LOG_INFO("%s: rule_ids[%03d] : %lu", LOG_TAG_POLICY, i, handler->rule_ids[i]); + char rule_uuid_str[UUID_STRING_SIZE] = {0}; + uuid_unparse(*uuid_array_get_at(&handler->rule_uuid_array, i), rule_uuid_str); + LOG_INFO("%s: rule_ids[%03d] : %s", LOG_TAG_POLICY, i, rule_uuid_str); } } } diff --git a/common/src/kafka.cpp b/common/src/kafka.cpp index 4fb0e68..737a2e2 100644 --- a/common/src/kafka.cpp +++ b/common/src/kafka.cpp @@ -11,6 +11,7 @@ struct config { + int enable_debug; char brokerlist[MAX_SYMBOL_LEN]; char sasl_username[MAX_SYMBOL_LEN]; char sasl_passwd[MAX_SYMBOL_LEN]; @@ -84,6 +85,14 @@ static struct per_producer_per_topic *per_producer_per_topic_new(const char *bro LOG_ERROR("%s: failed to set kafka client.id, %s", LOG_TAG_KAFKA, err_str); goto error_out; } + + // bootstrap.servers + if (rd_kafka_conf_set(conf, "bootstrap.servers", brokerlist, err_str, sizeof(err_str)) != RD_KAFKA_CONF_OK) + { + LOG_ERROR("%s: failed to set kafka bootstrap.servers, %s", LOG_TAG_KAFKA, err_str); + goto error_out; + } + if (strlen(sasl_username) > 0 && strlen(sasl_passwd) > 0) { if (rd_kafka_conf_set(conf, "security.protocol", "sasl_plaintext", err_str, sizeof(err_str)) != RD_KAFKA_CONF_OK) @@ -125,12 +134,6 @@ static struct per_producer_per_topic *per_producer_per_topic_new(const char *bro goto error_out; } - if (rd_kafka_brokers_add(pppt->producer, brokerlist) == 0) - { - LOG_ERROR("%s: failed to add kafka brokers", LOG_TAG_KAFKA); - goto error_out; - } - pppt->topic = rd_kafka_topic_new(pppt->producer, topic_name, NULL); if (pppt->topic == NULL) { @@ -163,6 +166,7 @@ struct kafka *kafka_create(const char *profile) return NULL; } + MESA_load_profile_int_def(profile, "kafka", "enable_debug", &handle->cfg.enable_debug, 0); MESA_load_profile_string_def(profile, "kafka", "brokerlist", handle->cfg.brokerlist, sizeof(handle->cfg.brokerlist), ""); MESA_load_profile_string_def(profile, "kafka", "sasl_username", handle->cfg.sasl_username, sizeof(handle->cfg.sasl_username), ""); MESA_load_profile_string_def(profile, "kafka", "sasl_passwd", handle->cfg.sasl_passwd, sizeof(handle->cfg.sasl_passwd), ""); @@ -237,7 +241,10 @@ int kafka_send(struct kafka *handle, enum topic_idx idx, const char *data, int l } else { - LOG_DEBUG("%s: success to produce message with topic [%d], %s", LOG_TAG_KAFKA, idx, data); + if (handle->cfg.enable_debug) + { + LOG_DEBUG("%s: success to produce message with topic [%d], %s", LOG_TAG_KAFKA, idx, data); + } return 0; } } diff --git a/common/src/utils.cpp b/common/src/utils.cpp index 773b1f2..8fb944a 100644 --- a/common/src/utils.cpp +++ b/common/src/utils.cpp @@ -12,38 +12,38 @@ #include "log.h" /****************************************************************************** - * mutable_array + * uuid_array ******************************************************************************/ -void mutable_array_init(struct mutable_array *array) +void uuid_array_init(struct uuid_array *array) { - memset(array, 0, sizeof(mutable_array)); + memset(array, 0, sizeof(uuid_array)); array->num = 0; - array->size = sizeof(array->elems) / sizeof(array->elems[0]); + array->size = sizeof(array->uuids) / sizeof(array->uuids[0]); } -void mutable_array_add_elem(struct mutable_array *array, uint64_t elem) +void uuid_array_append(struct uuid_array *array, uuid_t uuid) { if (array->num < array->size) { - array->elems[array->num] = elem; + uuid_copy(array->uuids[array->num], uuid); array->num++; } else { - LOG_ERROR("%s: fixed num array add elem too much !!!", LOG_TAG_UTILS); + LOG_ERROR("%s: uuid array add elem too much !!!", LOG_TAG_UTILS); } } -void mutable_array_del_elem(struct mutable_array *array, uint64_t elem) +void uuid_array_remove(struct uuid_array *array, uuid_t uuid) { for (int i = 0; i < array->num; i++) { - if (array->elems[i] == elem) + if (uuid_compare(array->uuids[i], uuid) == 0) { if (i + 1 != array->size) { - memmove(&(array->elems[i]), &(array->elems[i + 1]), sizeof(array->elems[0]) * (array->num - i - 1)); + memmove(&(array->uuids[i]), &(array->uuids[i + 1]), sizeof(array->uuids[0]) * (array->num - i - 1)); } i--; array->num--; @@ -51,7 +51,20 @@ void mutable_array_del_elem(struct mutable_array *array, uint64_t elem) } } -int mutable_array_is_full(struct mutable_array *array) +int uuid_array_contains(struct uuid_array *array, uuid_t uuid) +{ + for (int i = 0; i < array->num; i++) + { + if (uuid_compare(array->uuids[i], uuid) == 0) + { + return 1; + } + } + + return 0; +} + +int uuid_array_is_full(struct uuid_array *array) { if (array->num == array->size) { @@ -63,7 +76,7 @@ int mutable_array_is_full(struct mutable_array *array) } } -int mutable_array_count_elem(struct mutable_array *array) +int uuid_array_get_count(struct uuid_array *array) { if (array) { @@ -75,27 +88,17 @@ int mutable_array_count_elem(struct mutable_array *array) } } -int mutable_array_exist_elem(struct mutable_array *array, uint64_t elem) -{ - for (int i = 0; i < array->num; i++) - { - if (array->elems[i] == elem) - { - return 1; - } - } - - return 0; -} - -int mutable_array_index_elem(struct mutable_array *array, int index) +uuid_t *uuid_array_get_at(struct uuid_array *array, int index) { if (index >= array->num) { assert(0); + return NULL; + } + else + { + return &(array->uuids[index]); } - - return array->elems[index]; } /****************************************************************************** diff --git a/common/test/gtest_control_packet.cpp b/common/test/gtest_control_packet.cpp index b010b63..3473b82 100644 --- a/common/test/gtest_control_packet.cpp +++ b/common/test/gtest_control_packet.cpp @@ -9,18 +9,23 @@ static u_char control_packet_active0[] = { 0x73, 0x74, 0x61, 0x74, 0x65, 0xA6, 0x61, 0x63, 0x74, 0x69, 0x76, 0x65, 0xA6, 0x6D, 0x65, 0x74, 0x68, 0x6F, 0x64, 0xAD, 0x70, 0x6F, 0x6C, 0x69, 0x63, 0x79, 0x5F, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0xA6, 0x70, 0x61, 0x72, 0x61, 0x6D, 0x73, 0x83, 0xA3, 0x73, 0x63, 0x65, 0x81, 0xA8, 0x72, - 0x75, 0x6C, 0x65, 0x5F, 0x69, 0x64, 0x73, 0x92, 0xCD, 0x04, 0x57, 0xCD, 0x08, 0xAE, 0xA6, 0x73, - 0x68, 0x61, 0x70, 0x65, 0x72, 0x81, 0xA8, 0x72, 0x75, 0x6C, 0x65, 0x5F, 0x69, 0x64, 0x73, 0x92, - 0xCD, 0x0D, 0x05, 0xCD, 0x11, 0x5C, 0xA5, 0x70, 0x72, 0x6F, 0x78, 0x79, 0x82, 0xA8, 0x72, 0x75, - 0x6C, 0x65, 0x5F, 0x69, 0x64, 0x73, 0x92, 0xCD, 0x15, 0xB3, 0xCD, 0x1A, 0x0A, 0xAD, 0x74, 0x63, - 0x70, 0x5F, 0x68, 0x61, 0x6E, 0x64, 0x73, 0x68, 0x61, 0x6B, 0x65, 0xDC, 0x00, 0x24, 0x01, 0x01, - 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0xA3, 0x31, - 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, - 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, - 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, - 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0x92, 0xCD, 0x15, 0xB3, 0xCD, 0x1A, - 0x0A, 0x92, 0xCD, 0x15, 0xB3, 0xCD, 0x1A, 0x0A, 0x92, 0xCD, 0x15, 0xB3, 0xCD, 0x1A, 0x0A, 0x92, - 0x01, 0x01, 0x92, 0x01, 0x01}; + 0x75, 0x6C, 0x65, 0x5F, 0x69, 0x64, 0x73, 0x92, 0xC4, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xC4, 0x10, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xA6, 0x73, 0x68, 0x61, + 0x70, 0x65, 0x72, 0x81, 0xA8, 0x72, 0x75, 0x6C, 0x65, 0x5F, 0x69, 0x64, 0x73, 0x92, 0xC4, 0x10, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, + 0xC4, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x04, 0xA5, 0x70, 0x72, 0x6F, 0x78, 0x79, 0x82, 0xA8, 0x72, 0x75, 0x6C, 0x65, 0x5F, 0x69, + 0x64, 0x73, 0x92, 0xC4, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x05, 0xC4, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0xAD, 0x74, 0x63, 0x70, 0x5F, 0x68, 0x61, 0x6E, 0x64, + 0x73, 0x68, 0x61, 0x6B, 0x65, 0xDC, 0x00, 0x24, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, + 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, + 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, + 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, 0xA3, 0x31, 0x32, 0x33, + 0xA3, 0x31, 0x32, 0x33, 0x92, 0xCD, 0x15, 0xB3, 0xCD, 0x1A, 0x0A, 0x92, 0xCD, 0x15, 0xB3, 0xCD, + 0x1A, 0x0A, 0x92, 0xCD, 0x15, 0xB3, 0xCD, 0x1A, 0x0A, 0x92, 0x01, 0x01, 0x92, 0x01, 0x01}; static u_char control_packet_active1[] = { 0x85, 0xA5, 0x74, 0x73, 0x79, 0x6E, 0x63, 0xA3, 0x32, 0x2E, 0x30, 0xAA, 0x73, 0x65, 0x73, 0x73, @@ -28,13 +33,45 @@ static u_char control_packet_active1[] = { 0x73, 0x74, 0x61, 0x74, 0x65, 0xA6, 0x61, 0x63, 0x74, 0x69, 0x76, 0x65, 0xA6, 0x6D, 0x65, 0x74, 0x68, 0x6F, 0x64, 0xAD, 0x70, 0x6F, 0x6C, 0x69, 0x63, 0x79, 0x5F, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0xA6, 0x70, 0x61, 0x72, 0x61, 0x6D, 0x73, 0x81, 0xA3, 0x73, 0x63, 0x65, 0x81, 0xA8, 0x72, - 0x75, 0x6C, 0x65, 0x5F, 0x69, 0x64, 0x73, 0x91, 0xCE, 0x00, 0x0F, 0x2F, 0x7F}; + 0x75, 0x6C, 0x65, 0x5F, 0x69, 0x64, 0x73, 0x91, 0xC4, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}; static u_char control_packet_opening[] = { 0x83, 0xA5, 0x74, 0x73, 0x79, 0x6E, 0x63, 0xA3, 0x32, 0x2E, 0x30, 0xAA, 0x73, 0x65, 0x73, 0x73, 0x69, 0x6F, 0x6E, 0x5F, 0x69, 0x64, 0xCF, 0x04, 0x08, 0x02, 0x1B, 0x68, 0x4C, 0x03, 0xE9, 0xA5, 0x73, 0x74, 0x61, 0x74, 0x65, 0xA7, 0x6F, 0x70, 0x65, 0x6E, 0x69, 0x6E, 0x67}; +static u_char control_packet_send[] = { + 0x85, 0xA5, 0x74, 0x73, 0x79, 0x6E, 0x63, 0xA3, 0x32, 0x2E, 0x30, 0xAA, 0x73, 0x65, 0x73, 0x73, + 0x69, 0x6F, 0x6E, 0x5F, 0x69, 0x64, 0x01, 0xA5, 0x73, 0x74, 0x61, 0x74, 0x65, 0xA6, 0x61, 0x63, + 0x74, 0x69, 0x76, 0x65, 0xA6, 0x6D, 0x65, 0x74, 0x68, 0x6F, 0x64, 0xAA, 0x6C, 0x6F, 0x67, 0x5F, + 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0xA6, 0x70, 0x61, 0x72, 0x61, 0x6D, 0x73, 0x81, 0xA3, 0x73, + 0x63, 0x65, 0x83, 0xAC, 0x73, 0x63, 0x5F, 0x72, 0x75, 0x6C, 0x65, 0x5F, 0x6C, 0x69, 0x73, 0x74, + 0x92, 0xC4, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x01, 0xC4, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x02, 0xAA, 0x73, 0x63, 0x5F, 0x72, 0x73, 0x70, 0x5F, 0x72, 0x61, 0x77, + 0x92, 0xC4, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x03, 0xC4, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x04, 0xB0, 0x73, 0x63, 0x5F, 0x72, 0x73, 0x70, 0x5F, 0x64, 0x65, 0x63, + 0x72, 0x79, 0x70, 0x74, 0x65, 0x64, 0x92, 0xC4, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xC4, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04}; + +static uuid_t uuid1; +static uuid_t uuid2; +static uuid_t uuid3; +static uuid_t uuid4; +static uuid_t uuid5; +static uuid_t uuid6; + +static const char *uuid1_str = "00000000-0000-0000-0000-000000000001"; +static const char *uuid2_str = "00000000-0000-0000-0000-000000000002"; +static const char *uuid3_str = "00000000-0000-0000-0000-000000000003"; +static const char *uuid4_str = "00000000-0000-0000-0000-000000000004"; +static const char *uuid5_str = "00000000-0000-0000-0000-000000000005"; +static const char *uuid6_str = "00000000-0000-0000-0000-000000000006"; + +#if 1 TEST(CONTROL_PACKET, PACKAGE0) { char *data; @@ -73,10 +110,9 @@ TEST(CONTROL_PACKET, PACKAGE0) mpack_write_cstr(&writer, "rule_ids"); mpack_build_array(&writer); - mpack_write_u64(&writer, 1111); - mpack_write_u64(&writer, 2222); + mpack_write_bin(&writer, (const char *)&uuid1, sizeof(uuid1)); + mpack_write_bin(&writer, (const char *)&uuid2, sizeof(uuid2)); mpack_complete_array(&writer); - mpack_complete_map(&writer); } @@ -87,8 +123,8 @@ TEST(CONTROL_PACKET, PACKAGE0) mpack_write_cstr(&writer, "rule_ids"); mpack_build_array(&writer); - mpack_write_u64(&writer, 3333); - mpack_write_u64(&writer, 4444); + mpack_write_bin(&writer, (const char *)&uuid3, sizeof(uuid3)); + mpack_write_bin(&writer, (const char *)&uuid4, sizeof(uuid4)); mpack_complete_array(&writer); mpack_complete_map(&writer); @@ -101,8 +137,8 @@ TEST(CONTROL_PACKET, PACKAGE0) mpack_write_cstr(&writer, "rule_ids"); mpack_build_array(&writer); - mpack_write_u64(&writer, 5555); - mpack_write_u64(&writer, 6666); + mpack_write_bin(&writer, (const char *)&uuid5, sizeof(uuid5)); + mpack_write_bin(&writer, (const char *)&uuid6, sizeof(uuid6)); mpack_complete_array(&writer); mpack_write_cstr(&writer, "tcp_handshake"); @@ -183,7 +219,9 @@ TEST(CONTROL_PACKET, PACKAGE0) free(data); } +#endif +#if 1 TEST(CONTROL_PACKET, PACKAGE1) { char *data; @@ -222,7 +260,7 @@ TEST(CONTROL_PACKET, PACKAGE1) mpack_write_cstr(&writer, "rule_ids"); mpack_build_array(&writer); - mpack_write_u64(&writer, 995199); + mpack_write_bin(&writer, (const char *)&uuid1, sizeof(uuid1)); mpack_complete_array(&writer); mpack_complete_map(&writer); @@ -247,7 +285,9 @@ TEST(CONTROL_PACKET, PACKAGE1) free(data); } +#endif +#if 1 TEST(CONTROL_PACKET, PACKAGE2) { char *data; @@ -286,9 +326,12 @@ TEST(CONTROL_PACKET, PACKAGE2) free(data); } +#endif +#if 1 TEST(CONTROL_PACKET, PARSE0) { + char rule_uuid_str[UUID_STRING_SIZE]; struct control_packet handler; EXPECT_TRUE(control_packet_parse(&handler, (const char *)control_packet_active0, sizeof(control_packet_active0)) == CTRL_PKT_SUCCESS); control_packet_dump(&handler); @@ -297,13 +340,20 @@ TEST(CONTROL_PACKET, PARSE0) EXPECT_TRUE(handler.session_id == 18446744073709551615); EXPECT_TRUE(handler.state == SESSION_STATE_ACTIVE); EXPECT_STREQ(handler.method, "policy_update"); - EXPECT_TRUE(handler.rule_id_num == 2); - EXPECT_TRUE(handler.rule_ids[0] == 1111); - EXPECT_TRUE(handler.rule_ids[1] == 2222); + EXPECT_TRUE(uuid_array_get_count(&handler.rule_uuid_array) == 2); + + uuid_unparse(*uuid_array_get_at(&handler.rule_uuid_array, 0), rule_uuid_str); + EXPECT_STREQ(rule_uuid_str, uuid1_str); + + uuid_unparse(*uuid_array_get_at(&handler.rule_uuid_array, 1), rule_uuid_str); + EXPECT_STREQ(rule_uuid_str, uuid2_str); } +#endif +#if 1 TEST(CONTROL_PACKET, PARSE1) { + char rule_uuid_str[UUID_STRING_SIZE]; struct control_packet handler; EXPECT_TRUE(control_packet_parse(&handler, (const char *)control_packet_active1, sizeof(control_packet_active1)) == CTRL_PKT_SUCCESS); control_packet_dump(&handler); @@ -312,10 +362,14 @@ TEST(CONTROL_PACKET, PARSE1) EXPECT_TRUE(handler.session_id == 290484492702581737); EXPECT_TRUE(handler.state == SESSION_STATE_ACTIVE); EXPECT_STREQ(handler.method, "policy_update"); - EXPECT_TRUE(handler.rule_id_num == 1); - EXPECT_TRUE(handler.rule_ids[0] == 995199); + EXPECT_TRUE(uuid_array_get_count(&handler.rule_uuid_array) == 1); + + uuid_unparse(*uuid_array_get_at(&handler.rule_uuid_array, 0), rule_uuid_str); + EXPECT_STREQ(rule_uuid_str, uuid1_str); } +#endif +#if 1 TEST(CONTROL_PACKET, PARSE2) { struct control_packet handler; @@ -326,11 +380,130 @@ TEST(CONTROL_PACKET, PARSE2) EXPECT_TRUE(handler.session_id == 290484492702581737); EXPECT_TRUE(handler.state == SESSION_STATE_OPENING); EXPECT_STREQ(handler.method, ""); - EXPECT_TRUE(handler.rule_id_num == 0); + EXPECT_TRUE(uuid_array_get_count(&handler.rule_uuid_array) == 0); +} +#endif + +#if 1 +TEST(CONTROL_PACKET, SEND) +{ + uint64_t session_id = 1; + + struct uuid_array rule_uuid_array; + uuid_array_init(&rule_uuid_array); + uuid_array_append(&rule_uuid_array, uuid1); + uuid_array_append(&rule_uuid_array, uuid2); + int rule_num = uuid_array_get_count(&rule_uuid_array); + + struct uuid_array sf_uuid_array; + uuid_array_init(&sf_uuid_array); + uuid_array_append(&sf_uuid_array, uuid3); + uuid_array_append(&sf_uuid_array, uuid4); + int sf_num = uuid_array_get_count(&sf_uuid_array); + + char *data; + size_t size; + mpack_writer_t writer; + mpack_writer_init_growable(&writer, &data, &size); + + // write the example on the msgpack homepage + mpack_build_map(&writer); // root begin + + // tsync + mpack_write_cstr(&writer, "tsync"); + mpack_write_cstr(&writer, "2.0"); + + // session_id + mpack_write_cstr(&writer, "session_id"); + mpack_write_u64(&writer, session_id); + + // state + mpack_write_cstr(&writer, "state"); + mpack_write_cstr(&writer, "active"); + + // method + mpack_write_cstr(&writer, "method"); + mpack_write_cstr(&writer, "log_update"); + + // params + { + mpack_write_cstr(&writer, "params"); + mpack_build_map(&writer); // params value begin + + // sce + { + mpack_write_cstr(&writer, "sce"); + mpack_build_map(&writer); // sce value begin + + { + mpack_write_cstr(&writer, "sc_rule_list"); + mpack_build_array(&writer); // sc_rule_list begin + for (int i = 0; i < rule_num; i++) + { + mpack_write_bin(&writer, (const char *)uuid_array_get_at(&rule_uuid_array, i), sizeof(uuid_t)); + } + mpack_complete_array(&writer); // sc_rule_list end + } + + { + mpack_write_cstr(&writer, "sc_rsp_raw"); + mpack_build_array(&writer); // sc_rsp_raw begin + for (int i = 0; i < sf_num; i++) + { + mpack_write_bin(&writer, (const char *)uuid_array_get_at(&sf_uuid_array, i), sizeof(uuid_t)); + } + mpack_complete_array(&writer); // sc_rsp_raw end + } + + { + mpack_write_cstr(&writer, "sc_rsp_decrypted"); + mpack_build_array(&writer); // sc_rsp_decrypted begin + for (int i = 0; i < sf_num; i++) + { + mpack_write_bin(&writer, (const char *)uuid_array_get_at(&sf_uuid_array, i), sizeof(uuid_t)); + } + mpack_complete_array(&writer); // sc_rsp_decrypted end + } + + mpack_complete_map(&writer); // sce value end + } + + mpack_complete_map(&writer); // params value end + } + + mpack_complete_map(&writer); // root end + + // finish writing + if (mpack_writer_destroy(&writer) != mpack_ok) + { + assert(0); + if (data) + { + free(data); + data = NULL; + } + } + + for (size_t i = 0; i < size; i++) + { + printf("%02X, ", data[i]); + } + printf("\n"); + + EXPECT_STREQ(data, (const char *)control_packet_send); + free(data); } +#endif int main(int argc, char **argv) { + uuid_parse(uuid1_str, uuid1); + uuid_parse(uuid2_str, uuid2); + uuid_parse(uuid3_str, uuid3); + uuid_parse(uuid4_str, uuid4); + uuid_parse(uuid5_str, uuid5); + uuid_parse(uuid6_str, uuid6); + ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }
\ No newline at end of file diff --git a/common/test/gtest_health_check_table.cpp b/common/test/gtest_health_check_table.cpp index f48892d..f7dcb4e 100644 --- a/common/test/gtest_health_check_table.cpp +++ b/common/test/gtest_health_check_table.cpp @@ -5,6 +5,13 @@ #include "policy.h" #include "health_check.h" +uuid_t sf_uuid1; +uuid_t sf_uuid2; +uuid_t sf_uuid3; +uuid_t sf_uuid4; +uuid_t sf_uuid5; +uuid_t sf_uuid6; + TEST(HEALTH_CHECK_TABLE, INSERT) { uint64_t session_id1 = 0; @@ -22,7 +29,7 @@ TEST(HEALTH_CHECK_TABLE, INSERT) snprintf(policy1.address, sizeof(policy1.address), "1.1.1.1"); policy1.retires = 11; policy1.interval_ms = 111; - EXPECT_TRUE((session_id1 = health_check_session_add(1, 0, &policy1)) > 0); + EXPECT_TRUE((session_id1 = health_check_session_add(&sf_uuid1, 0, &policy1)) > 0); struct health_check policy2; memset(&policy2, 0, sizeof(policy2)); @@ -30,8 +37,8 @@ TEST(HEALTH_CHECK_TABLE, INSERT) snprintf(policy2.address, sizeof(policy2.address), "2.2.2.2"); policy2.retires = 22; policy2.interval_ms = 222; - EXPECT_TRUE((session_id2 = health_check_session_add(2, 0, &policy2)) > 0); - EXPECT_TRUE((session_id3 = health_check_session_add(3, 0, &policy2)) > 0); + EXPECT_TRUE((session_id2 = health_check_session_add(&sf_uuid2, 0, &policy2)) > 0); + EXPECT_TRUE((session_id3 = health_check_session_add(&sf_uuid3, 0, &policy2)) > 0); struct health_check policy3; memset(&policy3, 0, sizeof(policy3)); @@ -39,15 +46,15 @@ TEST(HEALTH_CHECK_TABLE, INSERT) snprintf(policy3.address, sizeof(policy3.address), "2001:0db8:0000:0000:0000:8a2e:0370:7334"); policy3.retires = 33; policy3.interval_ms = 333; - EXPECT_TRUE((session_id4 = health_check_session_add(4, 0, &policy3)) > 0); - EXPECT_TRUE((session_id5 = health_check_session_add(5, 0, &policy3)) > 0); + EXPECT_TRUE((session_id4 = health_check_session_add(&sf_uuid4, 0, &policy3)) > 0); + EXPECT_TRUE((session_id5 = health_check_session_add(&sf_uuid5, 0, &policy3)) > 0); // TEST Delete By Session ID - EXPECT_TRUE(health_check_session_del(session_id1, 1, 0) == 0); - EXPECT_TRUE(health_check_session_del(session_id2, 2, 0) == 0); - EXPECT_TRUE(health_check_session_del(session_id3, 3, 0) == 0); - EXPECT_TRUE(health_check_session_del(session_id4, 4, 0) == 0); - EXPECT_TRUE(health_check_session_del(session_id5, 5, 0) == 0); + EXPECT_TRUE(health_check_session_del(session_id1, &sf_uuid1, 0) == 0); + EXPECT_TRUE(health_check_session_del(session_id2, &sf_uuid2, 0) == 0); + EXPECT_TRUE(health_check_session_del(session_id3, &sf_uuid3, 0) == 0); + EXPECT_TRUE(health_check_session_del(session_id4, &sf_uuid4, 0) == 0); + EXPECT_TRUE(health_check_session_del(session_id5, &sf_uuid5, 0) == 0); } TEST(HEALTH_CHECK_TABLE, GET_STATUS) @@ -63,7 +70,7 @@ TEST(HEALTH_CHECK_TABLE, GET_STATUS) snprintf(policy1.address, sizeof(policy1.address), "4.4.4.4"); policy1.retires = 5; policy1.interval_ms = 300; - EXPECT_TRUE((session_id1 = health_check_session_add(1, 0, &policy1)) > 0); + EXPECT_TRUE((session_id1 = health_check_session_add(&sf_uuid1, 0, &policy1)) > 0); struct health_check policy2; memset(&policy2, 0, sizeof(policy2)); @@ -71,7 +78,7 @@ TEST(HEALTH_CHECK_TABLE, GET_STATUS) snprintf(policy2.address, sizeof(policy2.address), "5.5.5.5"); policy2.retires = 5; policy2.interval_ms = 300; - EXPECT_TRUE((session_id2 = health_check_session_add(2, 0, &policy2)) > 0); + EXPECT_TRUE((session_id2 = health_check_session_add(&sf_uuid2, 0, &policy2)) > 0); struct health_check policy3; memset(&policy3, 0, sizeof(policy3)); @@ -79,7 +86,7 @@ TEST(HEALTH_CHECK_TABLE, GET_STATUS) snprintf(policy3.address, sizeof(policy3.address), "6.6.6.6"); policy3.retires = 5; policy3.interval_ms = 300; - EXPECT_TRUE((session_id3 = health_check_session_add(3, 0, &policy3)) > 0); + EXPECT_TRUE((session_id3 = health_check_session_add(&sf_uuid3, 0, &policy3)) > 0); // TEST get status EXPECT_TRUE(health_check_session_get_status(session_id1) == 0); @@ -88,9 +95,9 @@ TEST(HEALTH_CHECK_TABLE, GET_STATUS) EXPECT_TRUE(health_check_session_get_status(7) == -1); // TEST Delete By Session ID - EXPECT_TRUE(health_check_session_del(session_id1, 1, 0) == 0); - EXPECT_TRUE(health_check_session_del(session_id2, 2, 0) == 0); - EXPECT_TRUE(health_check_session_del(session_id3, 3, 0) == 0); + EXPECT_TRUE(health_check_session_del(session_id1, &sf_uuid1, 0) == 0); + EXPECT_TRUE(health_check_session_del(session_id2, &sf_uuid2, 0) == 0); + EXPECT_TRUE(health_check_session_del(session_id3, &sf_uuid3, 0) == 0); } TEST(HEALTH_CHECK_TABLE, SET_STATUS) @@ -106,7 +113,7 @@ TEST(HEALTH_CHECK_TABLE, SET_STATUS) snprintf(policy1.address, sizeof(policy1.address), "7.7.7.7"); policy1.retires = 5; policy1.interval_ms = 300; - EXPECT_TRUE((session_id1 = health_check_session_add(1, 0, &policy1)) > 0); + EXPECT_TRUE((session_id1 = health_check_session_add(&sf_uuid1, 0, &policy1)) > 0); struct health_check policy2; memset(&policy2, 0, sizeof(policy2)); @@ -114,7 +121,7 @@ TEST(HEALTH_CHECK_TABLE, SET_STATUS) snprintf(policy2.address, sizeof(policy2.address), "8.8.8.8"); policy2.retires = 5; policy2.interval_ms = 300; - EXPECT_TRUE((session_id2 = health_check_session_add(2, 0, &policy2)) > 0); + EXPECT_TRUE((session_id2 = health_check_session_add(&sf_uuid2, 0, &policy2)) > 0); struct health_check policy3; memset(&policy3, 0, sizeof(policy3)); @@ -122,7 +129,7 @@ TEST(HEALTH_CHECK_TABLE, SET_STATUS) snprintf(policy3.address, sizeof(policy3.address), "9.9.9.9"); policy3.retires = 5; policy3.interval_ms = 300; - EXPECT_TRUE((session_id3 = health_check_session_add(3, 0, &policy3)) > 0); + EXPECT_TRUE((session_id3 = health_check_session_add(&sf_uuid3, 0, &policy3)) > 0); // TEST get status EXPECT_TRUE(health_check_session_get_status(session_id1) == 0); @@ -141,9 +148,9 @@ TEST(HEALTH_CHECK_TABLE, SET_STATUS) EXPECT_TRUE(health_check_session_get_status(session_id3) == 1); // TEST Delete By Session ID - EXPECT_TRUE(health_check_session_del(session_id1, 1, 0) == 0); - EXPECT_TRUE(health_check_session_del(session_id2, 2, 0) == 0); - EXPECT_TRUE(health_check_session_del(session_id3, 3, 0) == 0); + EXPECT_TRUE(health_check_session_del(session_id1, &sf_uuid1, 0) == 0); + EXPECT_TRUE(health_check_session_del(session_id2, &sf_uuid2, 0) == 0); + EXPECT_TRUE(health_check_session_del(session_id3, &sf_uuid3, 0) == 0); } #if 0 @@ -163,7 +170,7 @@ TEST(HEALTH_CHECK_TABLE, DELETE) snprintf(policy1.address, sizeof(policy1.address), "10.10.10.10"); policy1.retires = 5; policy1.interval_ms = 300; - EXPECT_TRUE((session_id1 = health_check_session_add(1, 0, &policy1)) > 0); + EXPECT_TRUE((session_id1 = health_check_session_add(&sf_uuid1, 0, &policy1)) > 0); struct health_check policy2; memset(&policy2, 0, sizeof(policy2)); @@ -171,8 +178,8 @@ TEST(HEALTH_CHECK_TABLE, DELETE) snprintf(policy2.address, sizeof(policy2.address), "11.11.11.11"); policy2.retires = 5; policy2.interval_ms = 300; - EXPECT_TRUE((session_id2 = health_check_session_add(2, 0, &policy2)) > 0); - EXPECT_TRUE((session_id3 = health_check_session_add(3, 0, &policy2)) > 0); + EXPECT_TRUE((session_id2 = health_check_session_add(&sf_uuid2, 0, &policy2)) > 0); + EXPECT_TRUE((session_id3 = health_check_session_add(&sf_uuid3, 0, &policy2)) > 0); struct health_check policy3; memset(&policy3, 0, sizeof(policy3)); @@ -180,25 +187,32 @@ TEST(HEALTH_CHECK_TABLE, DELETE) snprintf(policy3.address, sizeof(policy3.address), "12.12.12.12"); policy3.retires = 5; policy3.interval_ms = 300; - EXPECT_TRUE((session_id4 = health_check_session_add(4, 0, &policy3)) > 0); - EXPECT_TRUE((session_id5 = health_check_session_add(5, 0, &policy3)) > 0); - EXPECT_TRUE((session_id6 = health_check_session_add(6, 0, &policy3)) > 0); + EXPECT_TRUE((session_id4 = health_check_session_add(&sf_uuid4, 0, &policy3)) > 0); + EXPECT_TRUE((session_id5 = health_check_session_add(&sf_uuid5, 0, &policy3)) > 0); + EXPECT_TRUE((session_id6 = health_check_session_add(&sf_uuid6, 0, &policy3)) > 0); // TEST Delete By Session ID - EXPECT_TRUE(health_check_session_del(session_id1, 1, 0) == 0); - EXPECT_TRUE(health_check_session_del(session_id1, 1, 0) == -1); - EXPECT_TRUE(health_check_session_del(session_id2, 2, 0) == 0); - EXPECT_TRUE(health_check_session_del(session_id3, 3, 0) == 0); - EXPECT_TRUE(health_check_session_del(session_id3, 3, 0) == -1); - EXPECT_TRUE(health_check_session_del(session_id5, 4, 0) == 0); - EXPECT_TRUE(health_check_session_del(session_id5, 5, 0) == 0); - EXPECT_TRUE(health_check_session_del(session_id6, 6, 0) == 0); - EXPECT_TRUE(health_check_session_del(session_id6, 6, 0) == -1); + EXPECT_TRUE(health_check_session_del(session_id1, &sf_uuid1, 0) == 0); + EXPECT_TRUE(health_check_session_del(session_id1, &sf_uuid1, 0) == -1); + EXPECT_TRUE(health_check_session_del(session_id2, &sf_uuid2, 0) == 0); + EXPECT_TRUE(health_check_session_del(session_id3, &sf_uuid3, 0) == 0); + EXPECT_TRUE(health_check_session_del(session_id3, &sf_uuid3, 0) == -1); + EXPECT_TRUE(health_check_session_del(session_id5, &sf_uuid4, 0) == 0); + EXPECT_TRUE(health_check_session_del(session_id5, &sf_uuid5, 0) == 0); + EXPECT_TRUE(health_check_session_del(session_id6, &sf_uuid6, 0) == 0); + EXPECT_TRUE(health_check_session_del(session_id6, &sf_uuid6, 0) == -1); } #endif int main(int argc, char **argv) { + uuid_parse("00000000-0000-0000-0000-000000000001", sf_uuid1); + uuid_parse("00000000-0000-0000-0000-000000000002", sf_uuid2); + uuid_parse("00000000-0000-0000-0000-000000000003", sf_uuid3); + uuid_parse("00000000-0000-0000-0000-000000000004", sf_uuid4); + uuid_parse("00000000-0000-0000-0000-000000000005", sf_uuid5); + uuid_parse("00000000-0000-0000-0000-000000000006", sf_uuid6); + ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }
\ No newline at end of file diff --git a/common/test/gtest_utils.cpp b/common/test/gtest_utils.cpp index 9b50c62..0cb312b 100644 --- a/common/test/gtest_utils.cpp +++ b/common/test/gtest_utils.cpp @@ -2,38 +2,117 @@ #include "utils.h" -TEST(UTILS, FIXED_NUM_ARRAY) +TEST(UUID_ARRAY, FULL) { - struct mutable_array array; - mutable_array_init(&array); - - mutable_array_add_elem(&array, 1); - mutable_array_add_elem(&array, 2); - mutable_array_add_elem(&array, 3); - mutable_array_add_elem(&array, 1); - mutable_array_add_elem(&array, 2); - - EXPECT_TRUE(mutable_array_count_elem(&array) == 5); - EXPECT_TRUE(mutable_array_index_elem(&array, 0) == 1); - EXPECT_TRUE(mutable_array_index_elem(&array, 1) == 2); - EXPECT_TRUE(mutable_array_index_elem(&array, 2) == 3); - EXPECT_TRUE(mutable_array_index_elem(&array, 3) == 1); - EXPECT_TRUE(mutable_array_index_elem(&array, 4) == 2); - - mutable_array_del_elem(&array, 3); // 1,2,1,2 - EXPECT_TRUE(mutable_array_count_elem(&array) == 4); - EXPECT_TRUE(mutable_array_index_elem(&array, 0) == 1); - EXPECT_TRUE(mutable_array_index_elem(&array, 1) == 2); - EXPECT_TRUE(mutable_array_index_elem(&array, 2) == 1); - EXPECT_TRUE(mutable_array_index_elem(&array, 3) == 2); - - mutable_array_del_elem(&array, 2); // 1,1 - EXPECT_TRUE(mutable_array_count_elem(&array) == 2); - EXPECT_TRUE(mutable_array_index_elem(&array, 0) == 1); - EXPECT_TRUE(mutable_array_index_elem(&array, 1) == 1); - - mutable_array_del_elem(&array, 1); - EXPECT_TRUE(mutable_array_count_elem(&array) == 0); + uuid_t uuid; + uuid_parse("00000000-0000-0000-0000-000000000001", uuid); + + struct uuid_array array; + uuid_array_init(&array); + EXPECT_TRUE(uuid_array_is_full(&array) == 0); + + for (int i = 0; i < MAX_RULE_NUM; i++) + { + uuid_array_append(&array, uuid); + if (i == MAX_RULE_NUM - 1) + { + EXPECT_TRUE(uuid_array_is_full(&array) == 1); + } + else + { + EXPECT_TRUE(uuid_array_is_full(&array) == 0); + } + } + + EXPECT_TRUE(uuid_array_is_full(&array) == 1); +} + +TEST(UUID_ARRAY, COUNT) +{ + uuid_t uuid; + uuid_parse("00000000-0000-0000-0000-000000000001", uuid); + + struct uuid_array array; + uuid_array_init(&array); + EXPECT_TRUE(uuid_array_get_count(&array) == 0); + + for (int i = 0; i < MAX_RULE_NUM; i++) + { + uuid_array_append(&array, uuid); + EXPECT_TRUE(uuid_array_get_count(&array) == i + 1); + } + + EXPECT_TRUE(uuid_array_get_count(&array) == MAX_RULE_NUM); +} + +TEST(UUID_ARRAY, CONTAINS) +{ + uuid_t uuid1; + uuid_t uuid2; + uuid_t uuid3; + uuid_t uuid4; + uuid_parse("00000000-0000-0000-0000-000000000001", uuid1); + uuid_parse("00000000-0000-0000-0000-000000000002", uuid2); + uuid_parse("00000000-0000-0000-0000-000000000003", uuid3); + uuid_parse("00000000-0000-0000-0000-000000000004", uuid4); + + struct uuid_array array; + uuid_array_init(&array); + + EXPECT_TRUE(uuid_array_contains(&array, uuid1) == 0); + EXPECT_TRUE(uuid_array_contains(&array, uuid2) == 0); + EXPECT_TRUE(uuid_array_contains(&array, uuid3) == 0); + EXPECT_TRUE(uuid_array_contains(&array, uuid4) == 0); + + uuid_array_append(&array, uuid1); + uuid_array_append(&array, uuid2); + uuid_array_append(&array, uuid3); + + EXPECT_TRUE(uuid_array_contains(&array, uuid1) == 1); + EXPECT_TRUE(uuid_array_contains(&array, uuid2) == 1); + EXPECT_TRUE(uuid_array_contains(&array, uuid3) == 1); + EXPECT_TRUE(uuid_array_contains(&array, uuid4) == 0); +} + +TEST(UUID_ARRAY, REMOVE) +{ + uuid_t uuid1; + uuid_t uuid2; + uuid_t uuid3; + uuid_parse("00000000-0000-0000-0000-000000000001", uuid1); + uuid_parse("00000000-0000-0000-0000-000000000002", uuid2); + uuid_parse("00000000-0000-0000-0000-000000000003", uuid3); + + struct uuid_array array; + uuid_array_init(&array); + + uuid_array_append(&array, uuid1); + uuid_array_append(&array, uuid2); + uuid_array_append(&array, uuid3); + uuid_array_append(&array, uuid1); + uuid_array_append(&array, uuid2); + + EXPECT_TRUE(uuid_array_get_count(&array) == 5); + EXPECT_TRUE(uuid_compare(*uuid_array_get_at(&array, 0), uuid1) == 0); + EXPECT_TRUE(uuid_compare(*uuid_array_get_at(&array, 1), uuid2) == 0); + EXPECT_TRUE(uuid_compare(*uuid_array_get_at(&array, 2), uuid3) == 0); + EXPECT_TRUE(uuid_compare(*uuid_array_get_at(&array, 3), uuid1) == 0); + EXPECT_TRUE(uuid_compare(*uuid_array_get_at(&array, 4), uuid2) == 0); + + uuid_array_remove(&array, uuid3); // 1,2,1,2 + EXPECT_TRUE(uuid_array_get_count(&array) == 4); + EXPECT_TRUE(uuid_compare(*uuid_array_get_at(&array, 0), uuid1) == 0); + EXPECT_TRUE(uuid_compare(*uuid_array_get_at(&array, 1), uuid2) == 0); + EXPECT_TRUE(uuid_compare(*uuid_array_get_at(&array, 2), uuid1) == 0); + EXPECT_TRUE(uuid_compare(*uuid_array_get_at(&array, 3), uuid2) == 0); + + uuid_array_remove(&array, uuid2); // 1,1 + EXPECT_TRUE(uuid_array_get_count(&array) == 2); + EXPECT_TRUE(uuid_compare(*uuid_array_get_at(&array, 0), uuid1) == 0); + EXPECT_TRUE(uuid_compare(*uuid_array_get_at(&array, 1), uuid1) == 0); + + uuid_array_remove(&array, uuid1); + EXPECT_TRUE(uuid_array_get_count(&array) == 0); } TEST(UTILS, DEVICE) |
