From: Yu Date: Sun, 30 Aug 2020 23:17:17 +0000 (+0900) Subject: Add tests for classes X-Git-Tag: submit/tizen/20200904.033548~4 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F75%2F242575%2F7;p=platform%2Fcore%2Fconnectivity%2Fuwb-manager.git Add tests for classes description: You should run MQTT server(mosquitto) on localhost with port number 1883, open security to run this test Change-Id: I144fad17416337139ccf2cf510cc8e48808d4a87 Signed-off-by: Yu jiung + +#ifdef __cplusplus +extern "C" { +#endif + +void set_mock_tzplatform_mkpath_result(bool value); + +#ifdef __cplusplus +} +#endif + +#endif /* __MOCK_TIZEN_PLATFORM_CONFIG_H__ */ diff --git a/tests/mocks/mock_dlog.c b/tests/mocks/mock_dlog.c new file mode 100644 index 0000000..c54af1b --- /dev/null +++ b/tests/mocks/mock_dlog.c @@ -0,0 +1,43 @@ +// +// Copyright (c) 2020 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +#include +#include + +#ifndef API +#define API __attribute__ ((visibility("default"))) +#endif + +typedef enum { + LOG_ID_INVALID = -1, + LOG_ID_MAIN, + LOG_ID_RADIO, + LOG_ID_SYSTEM, + LOG_ID_APPS, + LOG_ID_KMSG, + LOG_ID_SYSLOG, + LOG_ID_MAX +} log_id_t; + +API int __dlog_print(log_id_t log_id, int prio, const char *tag, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); + printf("\n"); + + return 0; +} diff --git a/tests/mocks/mock_plugin.c b/tests/mocks/mock_plugin.c new file mode 100644 index 0000000..11aaa29 --- /dev/null +++ b/tests/mocks/mock_plugin.c @@ -0,0 +1,24 @@ +/* + * mockup Plug-in for UWB + * + * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + + +int uwb_plugin_load(void) +{ + return 0; +} diff --git a/tests/mocks/mock_plugin_dwm1001.c b/tests/mocks/mock_plugin_dwm1001.c new file mode 100644 index 0000000..fd9964c --- /dev/null +++ b/tests/mocks/mock_plugin_dwm1001.c @@ -0,0 +1,406 @@ +/* + * Mock up UWB Plug-in for DWM1001 + * + * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#include +#include +#include + +#include + +/* dwm_api.h includes below */ +#ifndef _DWM_API_H_ +/** + * @brief Device modes + */ +typedef enum { + DWM_MODE_TAG = 0, //!< DWM_MODE_TAG + DWM_MODE_ANCHOR = 1//!< DWM_MODE_ANCHOR +} dwm_mode_t; +#endif //_DWM_API_H_ +/* dwm_api.h includes above */ + +#define DBUS_DEBUG_VARIANT(parameters) \ + do {\ + gchar *parameters_debug_str = NULL;\ + if (parameters)\ + parameters_debug_str = g_variant_print(parameters, TRUE);\ + UWB_LOGI("signal params [%s]", parameters_debug_str ? parameters_debug_str : "NULL");\ + g_free(parameters_debug_str);\ + } while (0) + +#define MOCK_PLUGIN_DWM1001_PANID 0x1D1D +#define MOCK_PLUGIN_DWM1001_NODEID 0x1D1D +#define MOCK_PLUGIN_DWM1001_POS_X 0x1D1D +#define MOCK_PLUGIN_DWM1001_POS_Y 0x1D1D +#define MOCK_PLUGIN_DWM1001_POS_Z 0x1D1D +#define MOCK_PLUGIN_DWM1001_DISTANCE 0x1D1D +#define MOCK_PLUGIN_DWM1001_NETWORK_TIMEOUT 1500 +#define MOCK_PLUGIN_DWM1001_HARDWARE_WAIT_TIME 2000 +#define MOCK_PLUGIN_DWM1001_NODE_NUM 10 + +typedef struct { + uint64_t node_id; + uint16_t pan_id; + bool is_remote; + uint64_t distance; + int x; + int y; + int z; +} uwb_node_s; + +typedef struct { + uint16_t pan_id; + GSList *remote_node_list; + int remote_node_count; +} uwb_network_s; + +typedef struct { + void (*message_received_cb) (uint16_t node_id, unsigned char *message, int message_length); + void (*position_changed_cb) (uint16_t node_id, int x, int y, int z); +} uwb_hpi_event_cbs_s; + +typedef struct { + int (*init) (uwb_hpi_event_cbs_s *event_cbs); + int (*deinit) (void); + int (*test) (void); + int (*reset) (void); + int (*factory_reset) (void); + int (*enable_network) (void); + int (*disable_network) (void); + int (*get_network_info) (uwb_network_s **network_info); + int (*set_configurations) (uint16_t node_id, const GVariant *configurations); + int (*get_configurations) (uint16_t node_id, GVariant **configurations); + int (*set_position) (uint64_t node_id, int x, int y, int z); + int (*get_own_node) (uwb_node_s **own_node); + int (*send_message) (unsigned char *message, int message_length); + int (*send_message_to) (uint16_t node_id, unsigned char *message, int message_length); +} uwb_hpi_ops_s; + +static struct _dwm1001_ctx { + bool is_enable; + uint16_t pan_id; + uint64_t node_id; + uint16_t update_rate; + uint16_t stationary_update_rate; + int network_timeout; + int hardware_wait_time; + dwm_mode_t mode; + uwb_hpi_event_cbs_s *event_cbs; +} dwm1001_ctx; + +static void __destroy_node(gpointer data) +{ + if (data != NULL) { + uwb_node_s *node = (uwb_node_s *)data; + free(node); + } +} + +static void __clean_dwm1001_ctx() +{ + dwm1001_ctx.event_cbs = NULL; + dwm1001_ctx.is_enable = false; +} + +static void __set_default_dwm1001_ctx() +{ + dwm1001_ctx.pan_id = MOCK_PLUGIN_DWM1001_PANID; + dwm1001_ctx.mode = DWM_MODE_ANCHOR; + dwm1001_ctx.update_rate = 1; + dwm1001_ctx.stationary_update_rate = 1; + dwm1001_ctx.network_timeout = MOCK_PLUGIN_DWM1001_NETWORK_TIMEOUT; + dwm1001_ctx.hardware_wait_time = MOCK_PLUGIN_DWM1001_HARDWARE_WAIT_TIME; +} + +static int uwb_dwm1001_init(uwb_hpi_event_cbs_s *event_cbs) +{ + int ret = 0; + + dwm1001_ctx.is_enable = false; + __set_default_dwm1001_ctx(); + dwm1001_ctx.event_cbs = event_cbs; + + return ret; +} + +static int uwb_dwm1001_deinit(void) +{ + __clean_dwm1001_ctx(); + dwm1001_ctx.is_enable = false; + + return 0; +} + +static int uwb_dwm1001_test(void) +{ + return 0; +} + +static int uwb_dwm1001_reset(void) +{ + retv_if(!dwm1001_ctx.is_enable, -1); + + g_usleep(dwm1001_ctx.hardware_wait_time * 1000); + + return 0; +} + + +int uwb_dwm1001_factory_reset(void) +{ + retv_if(!dwm1001_ctx.is_enable, -1); + + g_usleep(dwm1001_ctx.hardware_wait_time * 1000); + + return 0; +} + +int uwb_dwm1001_enable_network(void) +{ + retv_if(dwm1001_ctx.is_enable, -1); + + dwm1001_ctx.node_id = MOCK_PLUGIN_DWM1001_NODEID; + dwm1001_ctx.is_enable = true; + + return 0; +} + +int uwb_dwm1001_disable_network(void) +{ + retv_if(!dwm1001_ctx.is_enable, -1); + + __clean_dwm1001_ctx(); + dwm1001_ctx.is_enable = false; + + return 0; +} + +int uwb_dwm1001_get_own_node(uwb_node_s **own_node) +{ + retv_if(own_node == NULL, -1); + retv_if(!dwm1001_ctx.is_enable, -1); + + *own_node = (uwb_node_s *)malloc(sizeof(uwb_node_s)); + if (*own_node == NULL) + return -1; + + (*own_node)->distance = 0; + (*own_node)->is_remote = false; + (*own_node)->node_id = dwm1001_ctx.node_id; + (*own_node)->pan_id = dwm1001_ctx.pan_id; + if (dwm1001_ctx.mode == DWM_MODE_ANCHOR) { + (*own_node)->x = MOCK_PLUGIN_DWM1001_POS_X; + (*own_node)->y = MOCK_PLUGIN_DWM1001_POS_Y; + (*own_node)->z = MOCK_PLUGIN_DWM1001_POS_Z; + } else { + (*own_node)->x = 0; + (*own_node)->y = 0; + (*own_node)->z = 0; + } + + return 0; +} + +static GSList *__get_anchor_list_anchor() +{ + GSList *node_list = NULL; + + /* Insert Nodes to node list */ + for (int i = 1; i <= MOCK_PLUGIN_DWM1001_NODE_NUM; ++i) { + uwb_node_s *node; + + node = (uwb_node_s *)malloc(sizeof(uwb_node_s)); + if (node == NULL) + continue; + + node->is_remote = true; + node->node_id = MOCK_PLUGIN_DWM1001_NODEID + i; + node->pan_id = dwm1001_ctx.pan_id; + node->x = MOCK_PLUGIN_DWM1001_POS_X + 1; + node->y = MOCK_PLUGIN_DWM1001_POS_Y + 1; + node->z = MOCK_PLUGIN_DWM1001_POS_Z + 1; + node->distance = 0; + node_list = g_slist_append(node_list, node); + } + + return node_list; +} + +static int test_node_x[] = {0, 0, 10, 0}; +static int test_node_y[] = {0, 10, 0, -10}; + +static GSList *__get_anchor_list_tag() +{ + GSList *node_list = NULL; + + for (int i = 1; i <= 3; ++i) { + uwb_node_s *node; + node = (uwb_node_s *)malloc(sizeof(uwb_node_s)); + if (node == NULL) + continue; + + /*TODO: Should update valid position and distance */ + node->distance = 10; + node->is_remote = true; + node->node_id = MOCK_PLUGIN_DWM1001_NODEID + i; + node->pan_id = dwm1001_ctx.pan_id; + node->x = test_node_x[i]; + node->y = test_node_y[i]; + node->z = 0; + node_list = g_slist_append(node_list, node); + } + + return node_list; +} + +int uwb_dwm1001_get_network_info(uwb_network_s **network_info) +{ + GSList *node_list = NULL; + + + retv_if(network_info == NULL, -1); + retv_if(!dwm1001_ctx.is_enable, -1); + + /* Get Anchor list */ + (dwm1001_ctx.mode == DWM_MODE_ANCHOR) ? + (node_list = __get_anchor_list_anchor()) : + (node_list = __get_anchor_list_tag()); + + *network_info = (uwb_network_s *)malloc(sizeof(uwb_network_s)); + if (*network_info == NULL) { + g_slist_free_full(node_list, __destroy_node); + return -1; + } + + (*network_info)->pan_id = dwm1001_ctx.pan_id; + (*network_info)->remote_node_list = node_list; + (*network_info)->remote_node_count = g_slist_length(node_list); + + return 0; +} + +static void __set_dwm1001_ctx(GVariantIter *iter) +{ + const gchar *key = NULL; + GVariant *value = NULL; + + if (!iter) + return; + + while (g_variant_iter_loop(iter, "{sv}", &key, &value)) { + + if (g_strcmp0(key, "MODE") == 0) { + const char *mode = NULL; + mode = g_variant_get_string(value, NULL); + if (strncmp(mode, "ANCHOR", strlen(mode)) == 0) + dwm1001_ctx.mode = DWM_MODE_ANCHOR; + else if (strncmp(mode, "TAG", strlen(mode)) == 0) + dwm1001_ctx.mode = DWM_MODE_TAG; + + } else if (g_strcmp0(key, "NETWORK_TIMEOUT") == 0) + dwm1001_ctx.network_timeout = (int)g_variant_get_int64(value); + else if (g_strcmp0(key, "HARDWARE_WAIT_TIME") == 0) + dwm1001_ctx.hardware_wait_time = (int)g_variant_get_int64(value); + else if (g_strcmp0(key, "PANID") == 0) + dwm1001_ctx.pan_id = (uint16_t)g_variant_get_int64(value); + } + +} +int uwb_dwm1001_set_configurations(uint16_t node_id, const GVariant *configurations) +{ + GVariantIter *iter = NULL; + + retv_if(dwm1001_ctx.is_enable, -1); + DBUS_DEBUG_VARIANT((GVariant *)configurations); + g_variant_get((GVariant *)configurations, "a{sv}", &iter); + + __set_dwm1001_ctx(iter); + + g_variant_iter_free(iter); + + return 0; +} + +static void __build_configuration(GVariantBuilder *builder) +{ + if (!builder) + return; + + if (dwm1001_ctx.mode == DWM_MODE_ANCHOR) + g_variant_builder_add(builder, "{sv}", "MODE", g_variant_new("s", "ANCHOR")); + else + g_variant_builder_add(builder, "{sv}", "MODE", g_variant_new("s", "TAG")); + + g_variant_builder_add(builder, "{sv}", "NETWORK_TIMEOUT", g_variant_new("x", (int64_t)dwm1001_ctx.network_timeout)); + g_variant_builder_add(builder, "{sv}", "HARDWARE_WAIT_TIME", g_variant_new("x", (int64_t)dwm1001_ctx.hardware_wait_time)); + g_variant_builder_add(builder, "{sv}", "PANID", g_variant_new("x", (int64_t)dwm1001_ctx.pan_id)); + +} + +int uwb_dwm1001_get_configurations(uint16_t node_id, GVariant **configurations) +{ + GVariantBuilder *builder; + int ret = 0; + + builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}")); + __build_configuration(builder); + *configurations = g_variant_new("a{sv}", builder); + + DBUS_DEBUG_VARIANT((GVariant *)*configurations); + g_variant_builder_unref(builder); + + return ret; +} + +int uwb_dwm1001_set_position(uint16_t node_id, int x, int y, int z) +{ + retv_if(node_id == 0, -1); + retv_if(!dwm1001_ctx.is_enable, -1); + + if (node_id != dwm1001_ctx.node_id) + return -1; + + if (dwm1001_ctx.mode == DWM_MODE_TAG) + return -1; + + return 0; +} + +int uwb_plugin_load(uwb_hpi_ops_s *ops) +{ + if (!ops) + return -1; + + ops->init = uwb_dwm1001_init; + ops->deinit = uwb_dwm1001_deinit; + ops->test = uwb_dwm1001_test; + ops->reset = uwb_dwm1001_reset; + ops->factory_reset = uwb_dwm1001_factory_reset; + ops->enable_network = uwb_dwm1001_enable_network; + ops->disable_network = uwb_dwm1001_disable_network; + ops->get_own_node = uwb_dwm1001_get_own_node; + ops->get_network_info = uwb_dwm1001_get_network_info; + ops->set_configurations = uwb_dwm1001_set_configurations; + ops->get_configurations = uwb_dwm1001_get_configurations; + ops->set_position = uwb_dwm1001_set_position; + ops->send_message = NULL; + ops->send_message_to = NULL; + dwm1001_ctx.is_enable = false; + + return 0; +} diff --git a/tests/mocks/mock_plugin_mqtt.cpp b/tests/mocks/mock_plugin_mqtt.cpp new file mode 100644 index 0000000..8dde385 --- /dev/null +++ b/tests/mocks/mock_plugin_mqtt.cpp @@ -0,0 +1,156 @@ +/* + * Mock up UWB Plug-in for MQTT + * + * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + char *server; + int port; + void (*connection_result)(bool success, const char *cause, void *user_data); +} uwb_mpi_connect_option; + +typedef struct { + void (*disconnection_result)(bool success, const char *cause, void *user_data); +} uwb_mpi_disconnect_option; + +typedef struct { + int qos; + char *topic; + void (*subscribe_result)(bool success, const char *cause, void *user_data); +} uwb_mpi_subscribe_option; + +typedef struct { + int qos; + int retained; + char *topic; + int payload_len; + unsigned char *payload; + void (*publish_result)(bool success, const char *cause, void *user_data); +} uwb_mpi_publish_option; + +typedef struct { + void (*message_arrived) (char *topic_name, int topic_len, unsigned char *payload, int payload_len); + void (*delivery_complete) (int token); + void (*connection_lost)(void); /* loss of connection to the server */ + void (*connected)(void); + void (*disconnected)(void); /* receives a disconnect packet */ +} uwb_mpi_event_cbs_s; + +typedef struct { + int (*init) (void); + int (*deinit) (void); + int (*set_callbacks) (uwb_mpi_event_cbs_s *event_cbs); + int (*unset_callbacks) (void); + bool (*is_connected)(void); + int (*connect)(uwb_mpi_connect_option *option); + int (*disconnect)(uwb_mpi_disconnect_option *option); + int (*subscribe)(uwb_mpi_subscribe_option *option); + int (*unsubscribe)(uwb_mpi_subscribe_option *option); + int (*publish)(uwb_mpi_publish_option *option); +} uwb_mpi_ops_s; + +int uwb_plugin_load(uwb_mpi_ops_s *ops); + +#ifdef __cplusplus +} +#endif + +bool is_init = false; +bool is_connected = false; +uwb_mpi_event_cbs_s mock_plugin_event_cbs = {0,}; + +static int uwb_mqtt_init(void) +{ + is_init = true; + return 0; +} + +static int uwb_mqtt_deinit(void) +{ + is_init = false; + return 0; +} + + +static int uwb_mqtt_plugin_set_callbacks(uwb_mpi_event_cbs_s *event_cbs) +{ + if (!is_init) + return -1; + + if (is_connected) + return -1; + + mock_plugin_event_cbs.message_arrived = event_cbs->message_arrived; + mock_plugin_event_cbs.delivery_complete = event_cbs->delivery_complete; + mock_plugin_event_cbs.connection_lost = event_cbs->connection_lost; + mock_plugin_event_cbs.connected = event_cbs->connected; + mock_plugin_event_cbs.disconnected = event_cbs->disconnected; + + return 0; +} + +static int uwb_mqtt_plugin_unset_callbacks(void) +{ + + if (!is_init) + return -1; + + if (is_connected) + return -1; + + mock_plugin_event_cbs.message_arrived = nullptr; + mock_plugin_event_cbs.delivery_complete = nullptr; + mock_plugin_event_cbs.connection_lost = nullptr; + mock_plugin_event_cbs.connected = nullptr; + mock_plugin_event_cbs.disconnected = nullptr; + + return 0; +} +static int uwb_mqtt_plugin_connect(uwb_mpi_connect_option *option) +{ + is_connected = true; + + return 0; +} + +static int uwb_mqtt_plugin_disconnect(uwb_mpi_disconnect_option *option) +{ + if (!is_connected) + return -1; + + is_connected = false; + return 0; +} + +int uwb_plugin_load(uwb_mpi_ops_s *ops) +{ + if (!ops) + return -1; + + ops->init = uwb_mqtt_init; + ops->deinit = uwb_mqtt_deinit; + ops->set_callbacks = uwb_mqtt_plugin_set_callbacks; + ops->unset_callbacks = uwb_mqtt_plugin_unset_callbacks; + + return 0; +} + diff --git a/tests/mocks/mock_tizen_platform_config.c b/tests/mocks/mock_tizen_platform_config.c new file mode 100644 index 0000000..75e7ee9 --- /dev/null +++ b/tests/mocks/mock_tizen_platform_config.c @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include + +#include + +#include "mock_tizen_platform_config.h" + +#ifndef API +#define API __attribute__ ((visibility("default"))) +#endif + +static bool mock_tzplatform_mkpath_result = true; + +//extern void *__real_tzplatform_mkpath(enum tzplatform_variable id, const char *path); + +void set_mock_tzplatform_mkpath_result(bool value) +{ + mock_tzplatform_mkpath_result = value; +} + +API const char *__wrap_tzplatform_mkpath(enum tzplatform_variable id, const char *path) +{ + if (!mock_tzplatform_mkpath_result) + return NULL; + + //return __real_tzplatform_mkpath(id, path); + return "uwb-plugin.conf"; +} diff --git a/tests/uwb-manager-gtest.cpp b/tests/uwb-manager-gtest.cpp new file mode 100644 index 0000000..2b5e1c7 --- /dev/null +++ b/tests/uwb-manager-gtest.cpp @@ -0,0 +1,1355 @@ +/* + * uwb-manager-gtest.cpp + * + * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Sungsik Jang , Dongwook Lee + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/***************************************************************************** + * Standard headers + *****************************************************************************/ +#include +#include +#include +#include +/***************************************************************************** + * System headers + *****************************************************************************/ +#include +#include +/***************************************************************************** + * Local headers + *****************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "mock_tizen_platform_config.h" +/***************************************************************************** + * Macros and Typedefs + *****************************************************************************/ +/***************************************************************************** + * Global Variables + *****************************************************************************/ +/***************************************************************************** + * Local Functions Definition + *****************************************************************************/ + +static std::atomicmain_loop(nullptr); + +static void __run_main_loop(void) +{ + if (main_loop != nullptr) + return; + + main_loop = g_main_loop_new(nullptr, FALSE); + g_main_loop_run(main_loop); + return; +} + +static void __exit_main_loop(void) +{ + if (main_loop == nullptr) + return; + + g_main_loop_quit(main_loop); + g_main_loop_unref(main_loop); + main_loop = nullptr; +} + +#define DWM1001_CONF "{\n" \ + " \"MODE\": \"ANCHOR\",\n" \ + " \"NETWORK_TIMEOUT\": \"1500\",\n" \ + " \"HARDWARE_WAIT_TIME\": \"2000\"\n" \ + "}" + +#define DWM1001_CONF_FILE_NAME "uwb-plugin.conf" + +#define UWB_MANAGER_GTEST_PANID 0x1F1F +#define UWB_MANAGER_GTEST_NODEID 0x1F1F + +class GdbusErrorTest : public ::testing::Test { +protected: + void SetUp() override + { + } + + void TearDown() override + { + } +}; + +TEST_F(GdbusErrorTest, gdbus_error_set_gerror) +{ + UwbManagerNamespace::GdbusError gdbus_error; + GError *error = nullptr; + + gdbus_error.registerGdbusError(); + + gdbus_error.setGerror(UWB_ERROR_INVALID_PARAMETER, &error); + ASSERT_NE(nullptr, error); + ASSERT_NE(nullptr, g_strrstr(error->message, ".InvalidParameter")); + g_error_free(error); + error = nullptr; + + gdbus_error.setGerror(UWB_ERROR_OUT_OF_MEMORY, &error); + ASSERT_NE(nullptr, error); + ASSERT_NE(nullptr, g_strrstr(error->message, ".OutOfMemory")); + g_error_free(error); + error = nullptr; + + gdbus_error.setGerror(UWB_ERROR_PERMISSION_DENIED, &error); + ASSERT_NE(nullptr, error); + ASSERT_NE(nullptr, g_strrstr(error->message, ".NotPermitted")); + g_error_free(error); + error = nullptr; + + gdbus_error.setGerror(UWB_ERROR_OPERATION_FAILED, &error); + ASSERT_NE(nullptr, error); + ASSERT_NE(nullptr, g_strrstr(error->message, ".OperationFailed")); + g_error_free(error); + error = nullptr; + + gdbus_error.setGerror((uwb_error_e)-1, &error); + ASSERT_NE(nullptr, error); + g_error_free(error); + error = nullptr; + + gdbus_error.deregisterGdbusError(); +} + +class MockPlugin : public UwbManagerNamespace::UwbPlugin { +public: + int init(void *uwb_plugin_load) override {return 0;}; + int deinit() override {return 0;}; + std::string getPluginPath(void) override {return _plugin_path;}; +private: + const std::string _plugin_path = + std::string("mock_plugin.so"); +}; + +class UwbPluginManagerTest : public ::testing::Test { +protected: + void SetUp() override + { + } + + void TearDown() override + { + } +}; + +TEST_F(UwbPluginManagerTest, uwb_plugin_manager_load_Positive) +{ + UwbManagerNamespace::UwbPluginManager uwb_plugin_mgr; + std::shared_ptr mock_plugin = std::make_shared(); + int ret; + + ret = uwb_plugin_mgr.load(mock_plugin); + ASSERT_EQ(0, ret); +} + +TEST_F(UwbPluginManagerTest, uwb_plugin_manager_load_Negative) +{ + UwbManagerNamespace::UwbPluginManager uwb_plugin_mgr; + std::shared_ptr mock_plugin = std::make_shared(); + int ret; + + ret = uwb_plugin_mgr.load(nullptr); + ASSERT_EQ(-1, ret); +} + +TEST_F(UwbPluginManagerTest, uwb_plugin_manager_unload_Positive) +{ + UwbManagerNamespace::UwbPluginManager uwb_plugin_mgr; + std::shared_ptr mock_plugin = std::make_shared(); + int ret; + + ret = uwb_plugin_mgr.load(mock_plugin); + ASSERT_EQ(0, ret); + + ret = uwb_plugin_mgr.unload(mock_plugin); + ASSERT_EQ(0, ret); +} + +TEST_F(UwbPluginManagerTest, uwb_plugin_manager_unload_Negative) +{ + UwbManagerNamespace::UwbPluginManager uwb_plugin_mgr; + int ret; + + ret = uwb_plugin_mgr.unload(nullptr); + ASSERT_EQ(-1, ret); +} + +class UwbRangePluginTest : public ::testing::Test { +protected: + void SetUp() override + { + this->_range_plugin = + std::make_shared(); + + int ret = _uwb_plugin_mgr.load(this->_range_plugin); + ASSERT_EQ(0, ret); + } + + void TearDown() override + { + int ret = _uwb_plugin_mgr.unload(this->_range_plugin); + ASSERT_EQ(0, ret); + } + std::shared_ptr _range_plugin; +private: + UwbManagerNamespace::UwbPluginManager _uwb_plugin_mgr; +}; + +TEST_F(UwbRangePluginTest, uwb_range_plugin_reset_Positive) +{ + int ret = _range_plugin->enableNetwork(); + ASSERT_EQ(0, ret); + + ret = _range_plugin->reset(); + ASSERT_EQ(0, ret); + + ret = _range_plugin->disableNetwork(); + ASSERT_EQ(0, ret); +} + +TEST_F(UwbRangePluginTest, uwb_range_plugin_reset_Negative) +{ + int ret = _range_plugin->reset(); + ASSERT_EQ(-1, ret); +} + +TEST_F(UwbRangePluginTest, uwb_range_plugin_factory_reset_Positive) +{ + int ret = _range_plugin->enableNetwork(); + ASSERT_EQ(0, ret); + + ret = _range_plugin->factoryReset(); + ASSERT_EQ(0, ret); + + ret = _range_plugin->disableNetwork(); + ASSERT_EQ(0, ret); +} + +TEST_F(UwbRangePluginTest, uwb_range_plugin_factory_reset_Negative) +{ + int ret = _range_plugin->factoryReset(); + ASSERT_EQ(-1, ret); +} + +TEST_F(UwbRangePluginTest, uwb_range_plugin_enable_network_Positive) +{ + int ret = _range_plugin->enableNetwork(); + ASSERT_EQ(0, ret); + + ret = _range_plugin->disableNetwork(); + ASSERT_EQ(0, ret); +} + +TEST_F(UwbRangePluginTest, uwb_range_plugin_enable_network_Negative) +{ + int ret = _range_plugin->enableNetwork(); + ASSERT_EQ(0, ret); + + ret = _range_plugin->enableNetwork(); + ASSERT_EQ(-1, ret); + + ret = _range_plugin->disableNetwork(); + ASSERT_EQ(0, ret); +} + +TEST_F(UwbRangePluginTest, uwb_range_plugin_disable_network_Positive) +{ + int ret = _range_plugin->enableNetwork(); + ASSERT_EQ(0, ret); + + ret = _range_plugin->disableNetwork(); + ASSERT_EQ(0, ret); +} + +TEST_F(UwbRangePluginTest, uwb_range_plugin_disable_network_Negative) +{ + int ret = _range_plugin->disableNetwork(); + ASSERT_EQ(-1, ret); +} + +TEST_F(UwbRangePluginTest, uwb_range_plugin_get_own_node_Positive) +{ + int ret = _range_plugin->enableNetwork(); + ASSERT_EQ(0, ret); + + uwb_hpi_node_s *own_node = nullptr; + ret = _range_plugin->getOwnNode(&own_node); + if (own_node) + free(own_node); + ASSERT_EQ(0, ret); +} + +TEST_F(UwbRangePluginTest, uwb_range_plugin_get_own_node_Negative) +{ + uwb_hpi_node_s *own_node = nullptr; + int ret = _range_plugin->getOwnNode(&own_node); + ASSERT_EQ(-1, ret); +} + +static void __destroy_node(gpointer data) +{ + if (!data) + return; + + uwb_hpi_node_s *node = (uwb_hpi_node_s *)data; + free(node); +} + +TEST_F(UwbRangePluginTest, uwb_range_plugin_get_network_info_Positive) +{ + int ret = _range_plugin->enableNetwork(); + ASSERT_EQ(0, ret); + + uwb_hpi_network_s *network = nullptr; + ret = _range_plugin->getNetworkInfo(&network); + if (network) { + g_slist_free_full(network->remote_node_list, __destroy_node); + free(network); + } + ASSERT_EQ(0, ret); +} + +TEST_F(UwbRangePluginTest, uwb_range_plugin_get_network_info_Negative) +{ + uwb_hpi_network_s *network = nullptr; + int ret = _range_plugin->getNetworkInfo(&network); + ASSERT_EQ(-1, ret); +} + +TEST_F(UwbRangePluginTest, uwb_range_plugin_set_position_Positive) +{ + int ret = _range_plugin->enableNetwork(); + ASSERT_EQ(0, ret); + + uwb_hpi_node_s *own_node = nullptr; + ret = _range_plugin->getOwnNode(&own_node); + + ret = _range_plugin->setPosition(own_node->node_id, 0, 0, 0); + ASSERT_EQ(0, ret); + + if (own_node) + free(own_node); +} + +TEST_F(UwbRangePluginTest, uwb_range_plugin_set_position_Negative) +{ + int ret = _range_plugin->setPosition(0x1D1D, 0, 0, 0); + ASSERT_EQ(-1, ret); +} + +static GVariant *__get_default_configuration(void) +{ + GVariantBuilder *builder; + GVariant *configurations = NULL; + + builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}")); + g_variant_builder_add(builder, "{sv}", "MODE", g_variant_new("s", "ANCHOR")); + g_variant_builder_add(builder, "{sv}", "NETWORK_TIMEOUT", g_variant_new("x", 1500)); + g_variant_builder_add(builder, "{sv}", "HARDWARE_WAIT_TIME", g_variant_new("x", 2000)); + + configurations = g_variant_new("a{sv}", builder); + + g_variant_builder_unref(builder); + + return configurations; +} + +TEST_F(UwbRangePluginTest, uwb_range_plugin_set_configurations_Positive) +{ + GVariant *configurations = __get_default_configuration(); + + int ret = _range_plugin->setConfigurations(0, configurations); + ASSERT_EQ(0, ret); + g_variant_unref(configurations); +} + +TEST_F(UwbRangePluginTest, uwb_range_plugin_set_configurations_Negative) +{ + int ret = _range_plugin->enableNetwork(); + ASSERT_EQ(0, ret); + + GVariant *configurations = __get_default_configuration(); + ret = _range_plugin->setConfigurations(0, configurations); + ASSERT_EQ(-1, ret); + g_variant_unref(configurations); + + ret = _range_plugin->disableNetwork(); + ASSERT_EQ(0, ret); +} + +TEST_F(UwbRangePluginTest, uwb_range_plugin_get_configurations_Positive) +{ + int ret = _range_plugin->enableNetwork(); + ASSERT_EQ(0, ret); + + GVariant *configurations; + ret = _range_plugin->getConfigurations(0, &configurations); + ASSERT_EQ(0, ret); + g_variant_unref(configurations); + + ret = _range_plugin->disableNetwork(); + ASSERT_EQ(0, ret); +} + +void __create_dwm1001_plugin_config() +{ + FILE *fp = fopen(DWM1001_CONF_FILE_NAME, "w"); + const char *conf_str = DWM1001_CONF; + if (!fp) + return; + fputs(conf_str, fp); + + fclose(fp); +} + +void __destroy_dwm1001_plugin_config() +{ +} + +class UwbConfigTest : public ::testing::Test { +protected: + void SetUp() override + { + set_mock_tzplatform_mkpath_result(true); + __create_dwm1001_plugin_config(); + } + + void TearDown() override + { + __destroy_dwm1001_plugin_config(); + } +}; + +TEST_F(UwbConfigTest, uwb_config_load_configuration_Positive1) +{ + UwbManagerNamespace::UwbConfig uwb_config; + int ret; + + ret = uwb_config.loadConfiguration(); + ASSERT_EQ(0, ret); +} + +TEST_F(UwbConfigTest, uwb_config_load_configuration_Positive2) +{ + std::shared_ptr range_plugin = + std::make_shared(); + UwbManagerNamespace::UwbConfig uwb_config(range_plugin); + int ret; + + UwbManagerNamespace::UwbPluginManager uwb_plugin_mgr; + uwb_plugin_mgr.load(range_plugin); + + ret = uwb_config.loadConfiguration(); + ASSERT_EQ(0, ret); + + uwb_plugin_mgr.unload(range_plugin); +} + +TEST_F(UwbConfigTest, uwb_config_load_configuration_Negative) +{ + UwbManagerNamespace::UwbConfig uwb_config; + int ret; + + set_mock_tzplatform_mkpath_result(false); + + ret = uwb_config.loadConfiguration(); + ASSERT_EQ(-1, ret); +} + +TEST_F(UwbConfigTest, uwb_config_set_configuration_Positive1) +{ + UwbManagerNamespace::UwbConfig uwb_config; + int ret; + + GVariant *configuration = __get_default_configuration(); + ret = uwb_config.setConfigurations(configuration); + g_variant_unref(configuration); + ASSERT_EQ(0, ret); +} + +TEST_F(UwbConfigTest, uwb_config_set_configuration_Positive2) +{ + std::shared_ptr range_plugin = + std::make_shared(); + UwbManagerNamespace::UwbConfig uwb_config(range_plugin); + int ret; + + UwbManagerNamespace::UwbPluginManager uwb_plugin_mgr; + uwb_plugin_mgr.load(range_plugin); + + GVariant *configuration = __get_default_configuration(); + ret = uwb_config.setConfigurations(configuration); + g_variant_unref(configuration); + ASSERT_EQ(0, ret); + + uwb_plugin_mgr.unload(range_plugin); +} + +TEST_F(UwbConfigTest, uwb_config_set_configuration_Negative) +{ + UwbManagerNamespace::UwbConfig uwb_config; + int ret; + + ret = uwb_config.setConfigurations(nullptr); + ASSERT_EQ(-1, ret); +} + +TEST_F(UwbConfigTest, uwb_config_get_configuration_Positive1) +{ + UwbManagerNamespace::UwbConfig uwb_config; + GVariant *configurations = nullptr; + int ret; + + ret = uwb_config.loadConfiguration(); + ASSERT_EQ(0, ret); + + configurations = uwb_config.getConfigurations(); + ASSERT_NE(nullptr, configurations); +} + +TEST_F(UwbConfigTest, uwb_config_get_configuration_Positive2) +{ + std::shared_ptr range_plugin = + std::make_shared(); + UwbManagerNamespace::UwbConfig uwb_config(range_plugin); + int ret; + + UwbManagerNamespace::UwbPluginManager uwb_plugin_mgr; + uwb_plugin_mgr.load(range_plugin); + + ret = uwb_config.loadConfiguration(); + ASSERT_EQ(0, ret); + + GVariant *configurations = uwb_config.getConfigurations(); + ASSERT_NE(nullptr, configurations); + + uwb_plugin_mgr.unload(range_plugin); +} + +TEST_F(UwbConfigTest, uwb_config_get_configuration_Negative) +{ + UwbManagerNamespace::UwbConfig uwb_config; + GVariant *configurations = nullptr; + + configurations = uwb_config.getConfigurations(); + ASSERT_EQ(nullptr, configurations); +} + +class UwbNetworkTest : public ::testing::Test { +protected: + void SetUp() override + { + this->_range_plugin = + std::make_shared(); + + int ret = _uwb_plugin_mgr.load(this->_range_plugin); + ASSERT_EQ(0, ret); + + std::unique_ptr \ + uwb_network(new UwbManagerNamespace::UwbNetwork(this->_range_plugin)); + + this->_network = std::move(uwb_network); + } + + void TearDown() override + { + ASSERT_NE(nullptr, this->_network.get()); + + this->_network->disable(); + int ret = _uwb_plugin_mgr.unload(this->_range_plugin); + ASSERT_EQ(0, ret); + } + std::unique_ptr _network; +private: + std::shared_ptr _range_plugin; + UwbManagerNamespace::UwbPluginManager _uwb_plugin_mgr; +}; + +TEST_F(UwbNetworkTest, uwb_network_enable_disable_Positive) +{ + ASSERT_NE(nullptr, this->_network.get()); + + int ret = this->_network->enable(); + ASSERT_EQ(0, ret); + + ret = this->_network->disable(); + ASSERT_EQ(0, ret); +} + +class UwbPositionTest : public ::testing::Test { +protected: + void SetUp() override + { + this->_range_plugin = + std::make_shared(); + + int ret = _uwb_plugin_mgr.load(this->_range_plugin); + ASSERT_EQ(0, ret); + + std::unique_ptr \ + uwb_network(new UwbManagerNamespace::UwbNetwork(this->_range_plugin)); + + this->_network = std::move(uwb_network); + ret = this->_network->enable(); + ASSERT_EQ(0, ret); + + std::unique_ptr \ + uwb_position(new UwbManagerNamespace::UwbPosition(this->_range_plugin)); + + this->_position = std::move(uwb_position); + } + + void TearDown() override + { + ASSERT_NE(nullptr, this->_network.get()); + + int ret = this->_network->disable(); + ASSERT_EQ(0, ret); + } + std::unique_ptr _position; +private: + std::unique_ptr _network; + std::shared_ptr _range_plugin; + UwbManagerNamespace::UwbPluginManager _uwb_plugin_mgr; +}; + +TEST_F(UwbPositionTest, uwb_position_get_own_node_Positive) +{ + ASSERT_NE(nullptr, this->_position.get()); + + int ret = _position->update(); + ASSERT_EQ(0, ret); + + UwbManagerNamespace::Node uwb_node = _position->getOwnNode(); + ASSERT_NE(0, uwb_node.getNodeId()); +} + +TEST_F(UwbPositionTest, uwb_network_get_node_Positive) +{ + ASSERT_NE(nullptr, this->_position.get()); + + UwbManagerNamespace::Node uwb_node; + int ret; + + uwb_node.setNodeId(UWB_MANAGER_GTEST_NODEID); + ret = _position->addNode(uwb_node); + ASSERT_EQ(0, ret); + + UwbManagerNamespace::Node *found_node = _position->getNode(UWB_MANAGER_GTEST_NODEID); + ASSERT_NE(nullptr, found_node); +} + +TEST_F(UwbPositionTest, uwb_network_get_node_Negative) +{ + UwbManagerNamespace::Node *found_node = _position->getNode(UWB_MANAGER_GTEST_NODEID); + ASSERT_EQ(nullptr, found_node); +} + +TEST_F(UwbPositionTest, uwb_network_remove_node_Positive) +{ + ASSERT_NE(nullptr, this->_position.get()); + + UwbManagerNamespace::Node uwb_node; + int ret; + + uwb_node.setNodeId(UWB_MANAGER_GTEST_NODEID); + ret = _position->addNode(uwb_node); + ASSERT_EQ(0, ret); + + UwbManagerNamespace::Node *found_node = _position->getNode(UWB_MANAGER_GTEST_NODEID); + ASSERT_NE(nullptr, found_node); + + ret = _position->removeNode(UWB_MANAGER_GTEST_NODEID); + ASSERT_EQ(0, ret); + + found_node = _position->getNode(UWB_MANAGER_GTEST_NODEID); + ASSERT_EQ(nullptr, found_node); +} + +TEST_F(UwbPositionTest, uwb_network_get_nodes_Positive) +{ + ASSERT_NE(nullptr, this->_position.get()); + + int ret = _position->update(); + ASSERT_EQ(0, ret); + + auto node_list = _position->getNodes(); + ASSERT_NE(nullptr, node_list); +} + +class UwbMqttMessageTest : public ::testing::Test { +protected: + void SetUp() override + { + } + + void TearDown() override + { + } +}; + +TEST_F(UwbMqttMessageTest, create_default_uwb_mqtt_message_Positive) +{ + UwbManagerNamespace::UwbMqttMessage mqtt_msg; + + ASSERT_EQ(true, std::string("/TIZEN/UWB/BROADCAST/+") == mqtt_msg.getTopic()); +} + +TEST_F(UwbMqttMessageTest, create_location_uwb_mqtt_message_Positive) +{ + int node_payload_length = 16; + + int test_pan_id = 0x2013; + unsigned long long test_node_id = 0x1018; + int test_x = 0x98ad; + int test_y = 0x8234; + int test_z = 0x2389; + UwbManagerNamespace::Node uwb_node(test_pan_id, test_node_id, + test_x, test_y, test_z); + UwbManagerNamespace::UwbMqttMessage mqtt_msg(uwb_node); + + ASSERT_EQ(true, (std::string("/TIZEN/UWB/BROADCAST/") + std::to_string(test_node_id)) == mqtt_msg.getTopic()); + ASSERT_EQ(node_payload_length, mqtt_msg.getPayload().size()); + + unsigned char *payload = (unsigned char *)mqtt_msg.getPayload().c_str(); + + int location_idx = 0; + int pan_id = payload[location_idx] + + (payload[location_idx + 1] << 8) + + (payload[location_idx + 2] << 16) + + (payload[location_idx + 3] << 24); + location_idx += sizeof(int32_t); + int x = payload[location_idx] + + (payload[location_idx + 1] << 8) + + (payload[location_idx + 2] << 16) + + (payload[location_idx + 3] << 24); + location_idx += sizeof(int32_t); + int y = payload[location_idx] + + (payload[location_idx + 1] << 8) + + (payload[location_idx + 2] << 16) + + (payload[location_idx + 3] << 24); + location_idx += sizeof(int32_t); + int z = payload[location_idx] + + (payload[location_idx + 1] << 8) + + (payload[location_idx + 2] << 16) + + (payload[location_idx + 3] << 24); + + ASSERT_EQ(true, (test_pan_id == pan_id) && + (test_x == x) && (test_y == y) && (test_z = z)); +} + +class UwbMqttConnectionTest : public ::testing::Test { +protected: + void SetUp() override + { + } + + void TearDown() override + { + } +}; + +TEST_F(UwbMqttConnectionTest, uwb_mqtt_create_mqtt_client_Positive) +{ + UwbManagerNamespace::MqttContext mqtt_ctx; + + int ret = mqtt_ctx.createMqttClient("localhost", 1883); + ASSERT_EQ(0, ret); +} + +TEST_F(UwbMqttConnectionTest, uwb_mqtt_create_mqtt_client_Negative) +{ + UwbManagerNamespace::MqttContext mqtt_ctx; + + int ret = mqtt_ctx.createMqttClient("localhost", 1883); + ASSERT_EQ(0, ret); + + ret = mqtt_ctx.createMqttClient("localhost", 1883); + ASSERT_EQ(-1, ret); +} + +TEST_F(UwbMqttConnectionTest, uwb_mqtt_destroy_mqtt_client_Positive) +{ + UwbManagerNamespace::MqttContext mqtt_ctx; + + int ret = mqtt_ctx.createMqttClient("localhost", 1883); + ASSERT_EQ(0, ret); + + ret = mqtt_ctx.destroyMqttClient(); + ASSERT_EQ(0, ret); +} + +TEST_F(UwbMqttConnectionTest, uwb_mqtt_destroy_mqtt_client_Negative) +{ + UwbManagerNamespace::MqttContext mqtt_ctx; + + int ret = mqtt_ctx.destroyMqttClient(); + ASSERT_EQ(-1, ret); +} + +TEST_F(UwbMqttConnectionTest, uwb_mqtt_is_connected_Positive) +{ + UwbManagerNamespace::MqttContext mqtt_ctx; + + int ret = mqtt_ctx.createMqttClient("localhost", 1883); + ASSERT_EQ(0, ret); + + bool is_connnected = mqtt_ctx.isConnected(); + ASSERT_EQ(false, is_connnected); +} + +TEST_F(UwbMqttConnectionTest, uwb_mqtt_connect_broker_Positive1) +{ + UwbManagerNamespace::MqttContext mqtt_ctx; + + int ret = mqtt_ctx.createMqttClient("localhost", 1883); + ASSERT_EQ(0, ret); + + ret = mqtt_ctx.connectBroker(); + ASSERT_EQ(0, ret); +} + +void __uwb_mqtt_connect_broker_positive2_connection_state_cb(bool connected) +{ + if (connected) + __exit_main_loop(); + + return; +} + +TEST_F(UwbMqttConnectionTest, uwb_mqtt_connect_broker_Positive2) +{ + UwbManagerNamespace::MqttContext mqtt_ctx; + + int ret = mqtt_ctx.createMqttClient("localhost", 1883); + ASSERT_EQ(0, ret); + mqtt_ctx.setConnectionStateCallback(__uwb_mqtt_connect_broker_positive2_connection_state_cb); + + ret = mqtt_ctx.connectBroker(); + ASSERT_EQ(0, ret); + + __run_main_loop(); +} + +TEST_F(UwbMqttConnectionTest, uwb_mqtt_connect_broker_Negative1) +{ + UwbManagerNamespace::MqttContext mqtt_ctx; + + int ret = mqtt_ctx.connectBroker(); + ASSERT_EQ(-1, ret); +} + +void __uwb_mqtt_connect_broker_negative2_connection_state_cb(bool connected) +{ + if (connected) + __exit_main_loop(); + + return; +} +static gboolean __uwb_mqtt_connect_broker_negative2_timeout_cb(gpointer user_data) +{ + __exit_main_loop(); + return false; +} + +TEST_F(UwbMqttConnectionTest, uwb_mqtt_connect_broker_Negative2) +{ + UwbManagerNamespace::MqttContext mqtt_ctx; + + int ret = mqtt_ctx.createMqttClient("localhost", 3881); + ASSERT_EQ(0, ret); + mqtt_ctx.setConnectionStateCallback(__uwb_mqtt_connect_broker_negative2_connection_state_cb); + + ret = mqtt_ctx.connectBroker(); + ASSERT_EQ(0, ret); + + g_timeout_add(10000, __uwb_mqtt_connect_broker_negative2_timeout_cb, nullptr); + __run_main_loop(); +} + +bool disconnect_broker_positive1_connection_state; + +void __disconnect_broker_positive1_connection_state_cb(bool connected) +{ + disconnect_broker_positive1_connection_state = connected; + __exit_main_loop(); + return; +} + +TEST_F(UwbMqttConnectionTest, uwb_mqtt_disconnect_broker_Positive1) +{ + UwbManagerNamespace::MqttContext mqtt_ctx; + + int ret = mqtt_ctx.createMqttClient("localhost", 1883); + ASSERT_EQ(0, ret); + + mqtt_ctx.setConnectionStateCallback(__disconnect_broker_positive1_connection_state_cb); + ret = mqtt_ctx.connectBroker(); + ASSERT_EQ(0, ret); + + __run_main_loop(); + ASSERT_EQ(true, disconnect_broker_positive1_connection_state); + + mqtt_ctx.unsetConnectionStateCallback(); + ret = mqtt_ctx.disconnectBroker(); + ASSERT_EQ(0, ret); +} + +bool disconnect_broker_positive2_connection_state; + +void __disconnect_broker_positive2_connection_state_cb(bool connected) +{ + disconnect_broker_positive2_connection_state = connected; + __exit_main_loop(); + return; +} + +TEST_F(UwbMqttConnectionTest, uwb_mqtt_disconnect_broker_Positive2) +{ + UwbManagerNamespace::MqttContext mqtt_ctx; + + int ret = mqtt_ctx.createMqttClient("localhost", 1883); + ASSERT_EQ(0, ret); + + mqtt_ctx.setConnectionStateCallback(__disconnect_broker_positive2_connection_state_cb); + ret = mqtt_ctx.connectBroker(); + ASSERT_EQ(0, ret); + + __run_main_loop(); + ASSERT_EQ(true, disconnect_broker_positive2_connection_state); + + ret = mqtt_ctx.disconnectBroker(); + ASSERT_EQ(0, ret); + + __run_main_loop(); + ASSERT_EQ(false, disconnect_broker_positive2_connection_state); + + mqtt_ctx.unsetConnectionStateCallback(); +} + +TEST_F(UwbMqttConnectionTest, uwb_mqtt_disconnect_broker_Negative1) +{ + UwbManagerNamespace::MqttContext mqtt_ctx; + + int ret = mqtt_ctx.disconnectBroker(); + ASSERT_EQ(-1, ret); +} + +TEST_F(UwbMqttConnectionTest, uwb_mqtt_disconnect_broker_Negative2) +{ + UwbManagerNamespace::MqttContext mqtt_ctx; + + int ret = mqtt_ctx.createMqttClient("localhost", 1883); + ASSERT_EQ(0, ret); + + ret = mqtt_ctx.disconnectBroker(); + ASSERT_EQ(-1, ret); +} + +std::atomic pubsubtest_connection_state(false); + +void __pubsubtest_connection_state_cb(bool is_connected) +{ + printf("TID: %d\n", gettid()); + pubsubtest_connection_state = is_connected; + __exit_main_loop(); + return; +} + +class UwbMqttPubSubTest : public ::testing::Test { +protected: + void SetUp() override + { + std::unique_ptr \ + mqtt_context(new UwbManagerNamespace::MqttContext()); + _mqtt_context = std::move(mqtt_context); + + int ret = _mqtt_context->createMqttClient("localhost", 1883); + ASSERT_EQ(0, ret); + + _mqtt_context->setConnectionStateCallback(__pubsubtest_connection_state_cb); + ret = _mqtt_context->connectBroker(); + ASSERT_EQ(0, ret); + + printf("TID: %d\n", gettid()); + if (pubsubtest_connection_state == false) + __run_main_loop(); + } + + void TearDown() override + { + int ret = _mqtt_context->disconnectBroker(); + ASSERT_EQ(0, ret); + + printf("TID: %d\n", gettid()); + if (pubsubtest_connection_state == true) + __run_main_loop(); + } + std::unique_ptr _mqtt_context; +}; + +#define MQTT_DEFAULT_QOS 1 + +TEST_F(UwbMqttPubSubTest, uwb_mqtt_subscribe_Positive) +{ + + std::unique_ptr \ + mqtt_msg(new UwbManagerNamespace::UwbMqttMessage()); + + std::unique_ptr \ + mqtt_subscribe_request(new UwbManagerNamespace::UwbMqttRequest( + MQTT_DEFAULT_QOS, + std::move(mqtt_msg))); + + int ret = _mqtt_context->subscribe(std::move(mqtt_subscribe_request)); + ASSERT_EQ(0, ret); +} + +TEST_F(UwbMqttPubSubTest, uwb_mqtt_subscribe_Negative) +{ + int ret = _mqtt_context->subscribe(nullptr); + ASSERT_EQ(-1, ret); +} + +TEST_F(UwbMqttPubSubTest, uwb_mqtt_unsubscribe_Positive) +{ + + std::unique_ptr \ + sub_mqtt_msg(new UwbManagerNamespace::UwbMqttMessage()); + + std::unique_ptr \ + mqtt_subscribe_request(new UwbManagerNamespace::UwbMqttRequest( + MQTT_DEFAULT_QOS, + std::move(sub_mqtt_msg))); + + int ret = _mqtt_context->subscribe(std::move(mqtt_subscribe_request)); + ASSERT_EQ(0, ret); + + std::unique_ptr \ + unsub_mqtt_msg(new UwbManagerNamespace::UwbMqttMessage()); + std::unique_ptr \ + mqtt_unsubscribe_request(new UwbManagerNamespace::UwbMqttRequest( + MQTT_DEFAULT_QOS, + std::move(unsub_mqtt_msg))); + + ret = _mqtt_context->unsubscribe(std::move(mqtt_unsubscribe_request)); + ASSERT_EQ(0, ret); + +} + +TEST_F(UwbMqttPubSubTest, uwb_mqtt_unsubscribe_Negative) +{ + int ret = _mqtt_context->unsubscribe(nullptr); + ASSERT_EQ(-1, ret); +} + +void __uwb_mqtt_publish_positive_message_arrived_cb(char *topic, int topic_len, + unsigned char *payload, int payload_len) +{ + __exit_main_loop(); + return; +} + +TEST_F(UwbMqttPubSubTest, uwb_mqtt_publish_Positive) +{ + + _mqtt_context->setMessageArrivedCallback(__uwb_mqtt_publish_positive_message_arrived_cb); + + std::unique_ptr \ + sub_mqtt_msg(new UwbManagerNamespace::UwbMqttMessage()); + + std::unique_ptr \ + mqtt_subscribe_request(new UwbManagerNamespace::UwbMqttRequest( + MQTT_DEFAULT_QOS, + std::move(sub_mqtt_msg))); + + int ret = _mqtt_context->subscribe(std::move(mqtt_subscribe_request)); + ASSERT_EQ(0, ret); + + int test_pan_id = 0x2013; + int test_node_id = 0x1018; + int test_x = 0x98ad; + int test_y = 0x8234; + int test_z = 0x2389; + UwbManagerNamespace::Node node(test_pan_id, test_node_id, test_x, test_y, test_z); + + std::unique_ptr \ + pub_mqtt_msg(new UwbManagerNamespace::UwbMqttMessage(node)); + + std::unique_ptr \ + mqtt_publish_request(new UwbManagerNamespace::UwbMqttRequest( + MQTT_DEFAULT_QOS, + 0, + std::move(pub_mqtt_msg))); + ret = _mqtt_context->publish(std::move(mqtt_publish_request)); + ASSERT_EQ(0, ret); + + // Caution: msg_arrived can be called before publish finished + __run_main_loop(); + + std::unique_ptr \ + unsub_mqtt_msg(new UwbManagerNamespace::UwbMqttMessage()); + + std::unique_ptr \ + mqtt_unsubscribe_request(new UwbManagerNamespace::UwbMqttRequest( + MQTT_DEFAULT_QOS, + std::move(unsub_mqtt_msg))); + + ret = _mqtt_context->unsubscribe(std::move(mqtt_unsubscribe_request)); + ASSERT_EQ(0, ret); +} + +TEST_F(UwbMqttPubSubTest, uwb_mqtt_publish_Negative) +{ + int ret = _mqtt_context->publish(nullptr); + ASSERT_EQ(-1, ret); +} + +class LocationManagerTest : public ::testing::Test { +protected: + void SetUp() override + { + this->_range_plugin = + std::make_shared(); + + int ret = _uwb_plugin_mgr.load(this->_range_plugin); + ASSERT_EQ(0, ret); + + std::unique_ptr \ + uwb_config(new UwbManagerNamespace::UwbConfig(this->_range_plugin)); + ret = uwb_config->loadConfiguration(); + ASSERT_EQ(0, ret); + + this->_config = std::move(uwb_config); + + std::unique_ptr \ + uwb_network(new UwbManagerNamespace::UwbNetwork(this->_range_plugin)); + ret = uwb_network->enable(); + ASSERT_EQ(0, ret); + + this->_network = std::move(uwb_network); + + std::shared_ptr uwb_position = + std::make_shared(this->_range_plugin); + + std::unique_ptr \ + location_manager(new UwbManagerNamespace::LocationManager(uwb_position)); + + this->_loc_mgr = std::move(location_manager); + } + + void TearDown() override + { + ASSERT_NE(nullptr, this->_network.get()); + + this->_network->disable(); + + int ret = _uwb_plugin_mgr.unload(this->_range_plugin); + ASSERT_EQ(0, ret); + } + std::unique_ptr _loc_mgr; + std::unique_ptr _config; + std::unique_ptr _network; +private: + std::shared_ptr _range_plugin; + UwbManagerNamespace::UwbPluginManager _uwb_plugin_mgr; +}; + +TEST_F(LocationManagerTest, location_manager_init_deinit_anchor_test) +{ + + int ret = this->_loc_mgr->start("localhost", 1883); + ASSERT_EQ(0, ret); + + ret = this->_loc_mgr->stop(); + ASSERT_EQ(0, ret); +} + +static GVariant *__get_tag_configuration(void) +{ + GVariantBuilder *builder; + GVariant *configurations = NULL; + + builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}")); + g_variant_builder_add(builder, "{sv}", "MODE", g_variant_new_string("TAG")); + + configurations = g_variant_new("a{sv}", builder); + + g_variant_builder_unref(builder); + + return configurations; +} + +static void __position_updated(UwbManagerNamespace::Node *node) +{ +} + +static void __node_added(UwbManagerNamespace::Node *node) +{ +} + +static void __node_updated(UwbManagerNamespace::Node *node) +{ + __exit_main_loop(); +} + +TEST_F(LocationManagerTest, location_manager_init_deinit_tag_test) +{ + ASSERT_NE(nullptr, _network.get()); + ASSERT_NE(nullptr, _config.get()); + ASSERT_NE(nullptr, _loc_mgr.get()); + + _network->disable(); + _config->setConfigurations(__get_tag_configuration()); + _network->enable(); + + this->_loc_mgr->setPositionUpdatedCallback(__position_updated); + this->_loc_mgr->setNodeAddedCallback(__node_added); + this->_loc_mgr->setNodeUpdatedCallback(__node_updated); + + int ret = this->_loc_mgr->start("localhost", 1883); + ASSERT_EQ(0, ret); + + __run_main_loop(); + + this->_loc_mgr->unsetPositionUpdatedCallback(); + this->_loc_mgr->unsetNodeAddedCallback(); + this->_loc_mgr->unsetNodeUpdatedCallback(); + ret = this->_loc_mgr->stop(); + ASSERT_EQ(0, ret); + +} + +static bool is_dummy_node_added = false; +static bool is_dummy_node_updated = false; +static bool is_dummy_node_removed = false; +static unsigned long long dummy_node_id = 0x1018; + +static void __dummy_node_added(UwbManagerNamespace::Node *node) +{ + if (node->getNodeId() == dummy_node_id) + is_dummy_node_added = true; +} + +static void __dummy_node_updated(UwbManagerNamespace::Node *node) +{ + if (node->getNodeId() == dummy_node_id) + is_dummy_node_updated = true; +} + +static void __dummy_node_removed(UwbManagerNamespace::Node *node) +{ + if (node->getNodeId() == dummy_node_id) + is_dummy_node_removed = true; + __exit_main_loop(); +} + +TEST_F(LocationManagerTest, location_manager_start_stop_tag_mqtt_dummy_test) +{ + ASSERT_NE(nullptr, _network.get()); + ASSERT_NE(nullptr, _config.get()); + ASSERT_NE(nullptr, _loc_mgr.get()); + + _network->disable(); + _config->setConfigurations(__get_tag_configuration()); + _network->enable(); + + this->_loc_mgr->setNodeAddedCallback(__dummy_node_added); + this->_loc_mgr->setNodeUpdatedCallback(__dummy_node_updated); + this->_loc_mgr->setNodeRemovedCallback(__dummy_node_removed); + + int ret = this->_loc_mgr->start("localhost", 1883); + ASSERT_EQ(0, ret); + + UwbManagerNamespace::MqttContext *p_mqtt_context = + this->_loc_mgr->getMqttContextPtr(); + + auto message_arrived_cb = p_mqtt_context->getMessageArrivedCb(); + + int test_pan_id = 0x2013; + int test_x = 0x98ad; + int test_y = 0x8234; + int test_z = 0x2389; + UwbManagerNamespace::Node uwb_node(test_pan_id, dummy_node_id, + test_x, test_y, test_z); + UwbManagerNamespace::UwbMqttMessage mqtt_msg(uwb_node); + + // test invalid topic + message_arrived_cb((char *)nullptr, mqtt_msg.getTopic().size(), + (unsigned char *)mqtt_msg.getPayload().c_str(), mqtt_msg.getPayload().size()); + // test invalid payload + message_arrived_cb((char *)mqtt_msg.getTopic().c_str(), mqtt_msg.getTopic().size(), + (unsigned char *)nullptr, mqtt_msg.getPayload().size()); + // test invalid topic length + message_arrived_cb((char *)mqtt_msg.getTopic().c_str(), 0, + (unsigned char *)mqtt_msg.getPayload().c_str(), mqtt_msg.getPayload().size()); + // test invalid payload length + message_arrived_cb((char *)mqtt_msg.getTopic().c_str(), mqtt_msg.getTopic().size(), + (unsigned char *)mqtt_msg.getPayload().c_str(), 0); + // test node added callback + message_arrived_cb((char *)mqtt_msg.getTopic().c_str(), mqtt_msg.getTopic().size(), + (unsigned char *)mqtt_msg.getPayload().c_str(), mqtt_msg.getPayload().size()); + // test node updated callback + message_arrived_cb((char *)mqtt_msg.getTopic().c_str(), mqtt_msg.getTopic().size(), + (unsigned char *)mqtt_msg.getPayload().c_str(), mqtt_msg.getPayload().size()); + + __run_main_loop(); + + // test node removed callback + ASSERT_EQ(true, is_dummy_node_added); + ASSERT_EQ(true, is_dummy_node_updated); + ASSERT_EQ(true, is_dummy_node_removed); + + this->_loc_mgr->unsetNodeAddedCallback(); + this->_loc_mgr->unsetNodeUpdatedCallback(); + this->_loc_mgr->unsetNodeRemovedCallback(); + ret = this->_loc_mgr->stop(); + ASSERT_EQ(0, ret); + +} + + +static void __glib_log( + const gchar *log_domain, + GLogLevelFlags log_level, + const gchar *msg, + gpointer user_data) +{ + printf("[GLib Err] %s-0x%2.2X: %s\n", + log_domain, log_level, msg); +} + +int main(int argc, char **argv) +{ + g_log_set_default_handler(__glib_log, NULL); + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}