From 6591bd608cb495be281d3740b5161c92a9ff02b3 Mon Sep 17 00:00:00 2001 From: Rohit Singh Date: Tue, 15 Feb 2022 23:09:38 +0530 Subject: [PATCH 01/16] Add thread commissioning scenarios to thread-test Change-Id: I9d327ac92bc1f2ed4c8c7e5b93a006aa8b90f01b Signed-off-by: Rohit Singh --- include/thread.h | 24 ++++- src/thread-core.c | 4 +- tests/CMakeLists.txt | 1 + tests/thread-main.c | 2 + tests/thread-network.c | 2 + tests/thread-scenarios.c | 233 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 261 insertions(+), 5 deletions(-) create mode 100644 tests/thread-scenarios.c diff --git a/include/thread.h b/include/thread.h index 86c4772..06b2ce1 100644 --- a/include/thread.h +++ b/include/thread.h @@ -808,7 +808,7 @@ int thread_srp_server_get_registered_service(thread_instance_h instance, const c /** * @ingroup CAPI_NETWORK_THREAD_SRP_MODULE - * @brief Get registered service detail from the SRP server + * @brief Get node ip address of the thread network node * @since_tizen 7.0 * * @return 0 on success, otherwise a negative error value. @@ -817,12 +817,30 @@ int thread_srp_server_get_registered_service(thread_instance_h instance, const c * @retval #THREAD_ERROR_OPERATION_FAILED Operation failed * @retval #THREAD_ERROR_NOT_SUPPORTED Not supported * - * @pre thread API must be initialized with thread_initialize(). - * @see thread_srp_server_start() + * @pre thread API must be initialized with thread_initialize() and + * enabled with thread_enable(). + * @see thread_initialize() and thread_enable() */ int thread_get_ipaddr(thread_instance_h instance, thread_ipaddr_foreach_cb callback, thread_ipaddr_type_e ipaddr_type, void *user_data); +/** + * @ingroup CAPI_NETWORK_THREAD_SRP_MODULE + * @brief Up the netwotk interface for the thread node. + * @since_tizen 7.0 + * + * @return 0 on success, otherwise a negative error value. + * @retval #THREAD_ERROR_NONE Successful + * @retval #THREAD_ERROR_NOT_INITIALIZED Not initialized + * @retval #THREAD_ERROR_OPERATION_FAILED Operation failed + * @retval #THREAD_ERROR_NOT_SUPPORTED Not supported + * + * @pre thread API must be initialized with thread_initialize() and + * enabled with thread_enable(). + * @see thread_initialize() and thread_enable() + */ +int thread_ifconfig_up(thread_instance_h instance); + #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/src/thread-core.c b/src/thread-core.c index e3ebad6..3eb8896 100644 --- a/src/thread-core.c +++ b/src/thread-core.c @@ -183,7 +183,7 @@ int thread_disable(thread_instance_h instance) return THREAD_ERROR_NONE; } -static int __thread_ifconfig_up(thread_instance_h instance) +int thread_ifconfig_up(thread_instance_h instance) { FUNC_ENTRY; THREAD_VALIDATE_INPUT_PARAMETER(instance); @@ -212,7 +212,7 @@ int thread_start(thread_instance_h instance) retv_if(current_instance->is_thread_started, THREAD_ERROR_ALREADY_DONE); /* ifconfig up */ - ret = __thread_ifconfig_up(instance); + ret = thread_ifconfig_up(instance); if (ret != THREAD_ERROR_NONE) THREAD_DBG("Thread: ifconfig up failed"); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2ea7876..ad99a19 100755 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -22,6 +22,7 @@ SET(TEST_SRCS thread-network.c thread-br.c thread-srp.c + thread-scenarios.c ) SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie") diff --git a/tests/thread-main.c b/tests/thread-main.c index 049d514..1d6204a 100755 --- a/tests/thread-main.c +++ b/tests/thread-main.c @@ -37,6 +37,7 @@ extern struct menu_data menu_thread_commissioner[]; extern struct menu_data menu_thread_network[]; extern struct menu_data menu_thread_br[]; extern struct menu_data menu_thread_srp[]; +extern struct menu_data menu_thread_scenarios[]; static struct menu_data menu_main[] = { { "1", "Core", menu_thread_core, NULL, NULL}, @@ -45,6 +46,7 @@ static struct menu_data menu_main[] = { { "4", "Network", menu_thread_network, NULL, NULL}, { "5", "Border Router", menu_thread_br, NULL, NULL}, { "6", "SRP", menu_thread_srp, NULL, NULL}, + { "7", "Scenarios", menu_thread_scenarios, NULL, NULL}, { NULL, NULL, }, }; diff --git a/tests/thread-network.c b/tests/thread-network.c index 2ad3fb3..c7a96da 100644 --- a/tests/thread-network.c +++ b/tests/thread-network.c @@ -156,6 +156,7 @@ OUT: FUNC_EXIT; return RET_SUCCESS; } + static void __get_ipaddr_callback(int index, char* ipaddr, thread_ipaddr_type_e ipaddr_type, void *user_data) { FUNC_ENTRY; @@ -166,6 +167,7 @@ static void __get_ipaddr_callback(int index, char* ipaddr, FUNC_EXIT; } + static int run_thread_get_ipaddr(MManager *mm, struct menu_data *menu) { FUNC_ENTRY; diff --git a/tests/thread-scenarios.c b/tests/thread-scenarios.c new file mode 100644 index 0000000..116e627 --- /dev/null +++ b/tests/thread-scenarios.c @@ -0,0 +1,233 @@ +/* + * Copyright (c) 2021 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 +#include +#include +#include + +#include +#include + +#include "thread.h" +#include "thread-menu.h" +#include "thread-type.h" +#include "thread-log.h" +#include "thread-util.h" + +static char g_name[MENU_DATA_SIZE + 1] = {"Thread-test"}; +static char g_key[MENU_DATA_SIZE + 1]; +static char g_pskc[MENU_DATA_SIZE + 1]; +static char g_str_channel[MENU_DATA_SIZE + 1] = {"07fff800"}; +static char g_str_extended_panid[MENU_DATA_SIZE + 1] = {"18446744073709551615"}; /* UINT64_MAX */ +static char g_str_panid[MENU_DATA_SIZE + 1] = {"65535"}; /* UINT16_MAX */ +static thread_network_h g_network; + +static char g_joiner_uuid[MENU_DATA_SIZE + 1]; +static char g_joiner_passphrase[MENU_DATA_SIZE + 1]; + +uint32_t g_channel; +uint64_t g_extended_panid; +uint16_t g_panid; + +static char g_prov_url[MENU_DATA_SIZE + 1]; +static char g_vendor_name[MENU_DATA_SIZE + 1]; +static char g_vendor_model[MENU_DATA_SIZE + 1]; +static char g_vendor_sw_version[MENU_DATA_SIZE + 1]; +static char g_vendor_data[MENU_DATA_SIZE + 1]; + +void _thread_joiner_start_callback(int result, void* user_data) +{ + msg("Thread joiner start callback, result: %s", + _thread_get_error_message(result)); + + return; +} + +static int run_thread_scenario_startup(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + int ret = THREAD_ERROR_NONE; + msg(" ### thread_scenario_startup ###"); + + thread_instance_h *g_instance = &(mm->t_instance); + + /* Initialize thread */ + ret = thread_initialize(); + msg(" - thread_initialize() ret: [0x%X] [%s]", ret, _thread_get_error_message(ret)); + retv_if(!(ret == THREAD_ERROR_NONE || ret == THREAD_ERROR_ALREADY_DONE), RET_SUCCESS); + + /* thread enable */ + ret = thread_enable(g_instance); + msg(" - thread_enable() ret: [0x%X] [%s]", ret, _thread_get_error_message(ret)); + retv_if(!(ret == THREAD_ERROR_NONE || ret == THREAD_ERROR_ALREADY_DONE + || ret == THREAD_ERROR_ALREADY_REGISTERED), RET_SUCCESS); + + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_scenario_cleanup(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + int ret = THREAD_ERROR_NONE; + msg(" ### thread_scenario_cleanup ###"); + + thread_instance_h *g_instance = &(mm->t_instance); + + /* thread disable */ + ret = thread_disable(g_instance); + msg(" - thread_disable() ret: [0x%X] [%s]", ret, _thread_get_error_message(ret)); + retv_if(!(ret == THREAD_ERROR_NONE || ret == THREAD_ERROR_ALREADY_DONE + || ret == THREAD_ERROR_ALREADY_REGISTERED), RET_SUCCESS); + + /* De-initialize thread */ + ret = thread_deinitialize(); + msg(" - thread_deinitialize() ret: [0x%X] [%s]", ret, _thread_get_error_message(ret)); + retv_if(!(ret == THREAD_ERROR_NONE || ret == THREAD_ERROR_ALREADY_DONE), RET_SUCCESS); + + FUNC_EXIT; + return RET_SUCCESS; +} +static int run_thread_joiner_join_network(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + int ret = THREAD_ERROR_NONE; + + /*factoryreset */ + ret = thread_factoryreset(g_instance); + if (ret != THREAD_ERROR_NONE) { + msg("Failed: %s", _thread_get_error_message(ret)); + goto OUT; + } + + /*thread ifconfig up */ + ret = thread_ifconfig_up(g_instance); + if (ret != THREAD_ERROR_NONE) { + msg("Failed: %s", _thread_get_error_message(ret)); + goto OUT; + } + + /*Joiner start*/ + ret = thread_joiner_start(g_instance, g_joiner_passphrase, g_prov_url, + g_vendor_name, g_vendor_model, g_vendor_sw_version, + g_vendor_data, _thread_joiner_start_callback, NULL); + if (ret != THREAD_ERROR_NONE) { + msg("Failed: %s", _thread_get_error_message(ret)); + goto OUT; + } + + msg("thread_joiner_join_network success"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_start_leader_as_commissioner(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + sscanf(g_str_channel, "%x", &g_channel); + sscanf(g_str_extended_panid, "%llu", &g_extended_panid); + sscanf(g_str_panid, "%hu", &g_panid); + + int ret = THREAD_ERROR_NONE; + + /*factoryreset */ + ret = thread_factoryreset(g_instance); + if (ret != THREAD_ERROR_NONE) { + msg("Failed: %s", _thread_get_error_message(ret)); + goto OUT; + } + + /* create operational_network */ + ret = thread_network_create_operational_network(g_instance, g_name, + g_key, g_pskc, g_channel, g_extended_panid, + g_panid, &g_network); + if (ret != THREAD_ERROR_NONE) { + msg("Failed: %s", _thread_get_error_message(ret)); + goto OUT; + } + + /*attach operational network */ + ret = thread_network_attach(g_instance); + if (ret != THREAD_ERROR_NONE) { + msg("Failed: %s", _thread_get_error_message(ret)); + goto OUT; + } + + /*thread ifconfig up */ + ret = thread_ifconfig_up(g_instance); + if (ret != THREAD_ERROR_NONE) { + msg("Failed: %s", _thread_get_error_message(ret)); + goto OUT; + } + + /* thread commissioner start*/ + ret = thread_commissioner_start(g_instance); + if (ret != THREAD_ERROR_NONE) { + msg("Failed: %s", _thread_get_error_message(ret)); + goto OUT; + } + + /* thread commissioner set commissioning data*/ + ret = thread_commissioner_set_commisioning_data(g_instance, g_joiner_uuid, + g_joiner_passphrase); + if (ret != THREAD_ERROR_NONE) { + msg("Failed: %s", _thread_get_error_message(ret)); + goto OUT; + } + + msg("thread_commissioner_set_commisioning_data success"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static struct menu_data menu_thread_joiner_join_network[] = { + { "1", "Type Joiner Passphrase", NULL, NULL, g_joiner_passphrase}, + { "2", "run", NULL, run_thread_joiner_join_network, NULL}, + { NULL, NULL, }, +}; + +static struct menu_data menu_thread_start_leader_as_commissioner[] = { + { "1", "Joiner UUID", NULL, NULL, g_joiner_uuid}, + { "2", "Joiner Passphrase", NULL, NULL, g_joiner_passphrase}, + { "3", "run", NULL, run_thread_start_leader_as_commissioner, NULL}, + { NULL, NULL, }, +}; + +struct menu_data menu_thread_scenarios[] = { + { "1", "thread_scenario_startup", + NULL, run_thread_scenario_startup, NULL }, + { "2", "thread_scenario_cleanup", + NULL, run_thread_scenario_cleanup, NULL }, + { "3", "thread_start_leader_as_commissioner", + menu_thread_start_leader_as_commissioner, NULL, NULL}, + { "4", "thread_joiner_join_network", + menu_thread_joiner_join_network, NULL, NULL }, + { NULL, NULL, }, +}; -- 2.7.4 From 5dbd5018ad11f29668db81d2db8aaf2755f83394 Mon Sep 17 00:00:00 2001 From: Rohit Singh Date: Thu, 24 Feb 2022 14:01:46 +0530 Subject: [PATCH 02/16] Add support to add and remove ipaddr for thread node This patch adds support to add and remove ipv6 address for the thread network: - thread_add_ipaddr - thread_remove_ipaddr Change-Id: Ie12902e3f707938af7b856258bee8a5bd67cce99 Signed-off-by: Rohit Singh --- include/thread.h | 38 ++++++++++++++++++++++++ src/thread-network.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests/thread-network.c | 60 +++++++++++++++++++++++++++++++++++++ 3 files changed, 178 insertions(+) diff --git a/include/thread.h b/include/thread.h index 06b2ce1..f430ea6 100644 --- a/include/thread.h +++ b/include/thread.h @@ -841,6 +841,44 @@ int thread_get_ipaddr(thread_instance_h instance, thread_ipaddr_foreach_cb callb */ int thread_ifconfig_up(thread_instance_h instance); +/** + * @ingroup CAPI_NETWORK_THREAD_SRP_MODULE + * @brief Add Ipv6 address to the thread node. + * @since_tizen 7.0 + * + * @param[in] ipv6_address Byte list + * + * @return 0 on success, otherwise a negative error value. + * @retval #THREAD_ERROR_NONE Successful + * @retval #THREAD_ERROR_NOT_INITIALIZED Not initialized + * @retval #THREAD_ERROR_OPERATION_FAILED Operation failed + * @retval #THREAD_ERROR_NOT_SUPPORTED Not supported + * + * @pre thread API must be initialized with thread_initialize() and + * enabled with thread_enable(). + * @see thread_initialize() and thread_enable() + */ +int thread_add_ipaddr(thread_instance_h instance, const uint8_t *ipv6_address); + +/** + * @ingroup CAPI_NETWORK_THREAD_SRP_MODULE + * @brief Remove Ipv6 address from the thread node. + * @since_tizen 7.0 + * + * @param[in] ipv6_address Byte list + * + * @return 0 on success, otherwise a negative error value. + * @retval #THREAD_ERROR_NONE Successful + * @retval #THREAD_ERROR_NOT_INITIALIZED Not initialized + * @retval #THREAD_ERROR_OPERATION_FAILED Operation failed + * @retval #THREAD_ERROR_NOT_SUPPORTED Not supported + * + * @pre thread API must be initialized with thread_initialize() and + * enabled with thread_enable(). + * @see thread_initialize() and thread_enable() + */ +int thread_remove_ipaddr(thread_instance_h instance, const uint8_t *ipv6_address); + #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/src/thread-network.c b/src/thread-network.c index 5cec98c..b48e195 100644 --- a/src/thread-network.c +++ b/src/thread-network.c @@ -412,3 +412,83 @@ int thread_get_ipaddr(thread_instance_h instance, thread_ipaddr_foreach_cb callb FUNC_EXIT; return ret; } + +static bool __is_valid_ipv6(const uint8_t* ipv6_address) +{ + FUNC_ENTRY; + + int index = 0; + while (index < THREAD_IPV6_ADDRESS_SIZE) { + char buffer[THREAD_NETWORK_BUFFER_MAX]; + snprintf(buffer, THREAD_NETWORK_BUFFER_MAX, "%02x", ipv6_address[index]); + if ((buffer[0] < '0' || buffer[0] > '9') && + (buffer[0] < 'a' || buffer[0] > 'f')) + return FALSE; + if ((buffer[1] < '0' || buffer[1] > '9') && + (buffer[1] < 'a' || buffer[1] > 'f')) + return FALSE; + + index++; + } + THREAD_DBG("DEBUG: NETWORK MESSAGE -> return true"); + + FUNC_EXIT; + return TRUE; +} + +int thread_add_ipaddr(thread_instance_h instance, const uint8_t *ipv6_address) +{ + FUNC_ENTRY; + THREAD_CHECK_SUPPORTED_FEATURE(THREAD_FEATURE_COMMON); + THREAD_CHECK_INIT_STATUS(); + THREAD_VALIDATE_INPUT_PARAMETER(instance); + THREAD_VALIDATE_INPUT_PARAMETER(ipv6_address); + + int ret = THREAD_ERROR_NONE; + char msg[THREAD_NETWORK_BUFFER_MAX]; + + retv_if(!__is_valid_ipv6(ipv6_address), THREAD_ERROR_INVALID_PARAMETER); + + snprintf(msg, THREAD_NETWORK_BUFFER_MAX, + "ipaddr add %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x", + ipv6_address[0], ipv6_address[1], ipv6_address[2], ipv6_address[3], + ipv6_address[4], ipv6_address[5], ipv6_address[6], ipv6_address[7], + ipv6_address[8], ipv6_address[9], ipv6_address[10], ipv6_address[11], + ipv6_address[12], ipv6_address[13], ipv6_address[14], ipv6_address[15]); + + ret = _thread_socket_client_execute(_thread_get_socket_fd(), msg, strlen(msg)); + retv_if(ret != THREAD_ERROR_NONE, ret); + THREAD_DBG("Successfully added address"); + + FUNC_EXIT; + return ret; +} + +int thread_remove_ipaddr(thread_instance_h instance, const uint8_t *ipv6_address) +{ + FUNC_ENTRY; + THREAD_CHECK_SUPPORTED_FEATURE(THREAD_FEATURE_COMMON); + THREAD_CHECK_INIT_STATUS(); + THREAD_VALIDATE_INPUT_PARAMETER(instance); + THREAD_VALIDATE_INPUT_PARAMETER(ipv6_address); + + int ret = THREAD_ERROR_NONE; + char msg[THREAD_NETWORK_BUFFER_MAX]; + + retv_if(!__is_valid_ipv6(ipv6_address), THREAD_ERROR_INVALID_PARAMETER); + + snprintf(msg, THREAD_BORDER_ROUTER_BUFFER_MAX, + "ipaddr del %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x", + ipv6_address[0], ipv6_address[1], ipv6_address[2], ipv6_address[3], + ipv6_address[4], ipv6_address[5], ipv6_address[6], ipv6_address[7], + ipv6_address[8], ipv6_address[9], ipv6_address[10], ipv6_address[11], + ipv6_address[12], ipv6_address[13], ipv6_address[14], ipv6_address[15]); + + ret = _thread_socket_client_execute(_thread_get_socket_fd(), msg, strlen(msg)); + retv_if(ret != THREAD_ERROR_NONE, ret); + THREAD_DBG("Successfully removed address"); + + FUNC_EXIT; + return ret; +} + diff --git a/tests/thread-network.c b/tests/thread-network.c index c7a96da..90f730a 100644 --- a/tests/thread-network.c +++ b/tests/thread-network.c @@ -47,6 +47,9 @@ uint64_t g_extended_panid; uint16_t g_panid; static char g_str_ipaddr_type[MENU_DATA_SIZE + 1] = "0"; +static char g_str_ipaddr[MENU_DATA_SIZE + 1]; + +uint8_t g_ipv6_address[THREAD_IPV6_ADDRESS_SIZE]; static int run_thread_network_destroy_operational_network(MManager *mm, struct menu_data *menu) { @@ -204,6 +207,46 @@ OUT: return RET_SUCCESS; } +static int run_thread_add_ipaddr(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = _preprocess_ipv6_prefix(g_str_ipaddr, g_ipv6_address); + retv_if(ret != THREAD_ERROR_NONE, ret); + + ret = thread_add_ipaddr(g_instance, g_ipv6_address); + if (ret == THREAD_ERROR_NONE) + msg("thread_add_ipaddr success"); + else + msg("thread_add_ipaddr failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + +static int run_thread_remove_ipaddr(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = _preprocess_ipv6_prefix(g_str_ipaddr, g_ipv6_address); + retv_if(ret != THREAD_ERROR_NONE, ret); + + ret = thread_remove_ipaddr(g_instance, g_ipv6_address); + if (ret == THREAD_ERROR_NONE) + msg("thread_remove_ipaddr success"); + else + msg("thread_remove_ipaddr failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + static struct menu_data menu_thread_network_set_active_dataset_tlvs[] = { { "1", "Tlvs_buffer len", NULL, NULL, g_str_buf_length}, { "2", "Tlvs_buffer", NULL, NULL, g_str_tlvs_buffer}, @@ -229,6 +272,18 @@ static struct menu_data menu_thread_get_ipaddr[] = { { NULL, NULL, }, }; +static struct menu_data menu_thread_add_ipaddr[] = { + { "1", "Type IPv6 address", NULL, NULL, g_str_ipaddr}, + { "2", "run", NULL, run_thread_add_ipaddr, NULL}, + { NULL, NULL, }, +}; + +static struct menu_data menu_thread_remove_ipaddr[] = { + { "1", "Type IPv6 address", NULL, NULL, g_str_ipaddr}, + { "2", "run", NULL, run_thread_remove_ipaddr, NULL}, + { NULL, NULL, }, +}; + struct menu_data menu_thread_network[] = { { "1", "thread_network_create_operational_network", menu_thread_network_create_operational_network, NULL, NULL}, @@ -242,5 +297,10 @@ struct menu_data menu_thread_network[] = { NULL, run_thread_network_attach, NULL}, { "6", "thread_get_ipaddr", menu_thread_get_ipaddr, NULL, NULL}, + { "7", "thread_add_ipaddr", + menu_thread_add_ipaddr, NULL, NULL}, + { "8", "thread_remove_ipaddr", + menu_thread_remove_ipaddr, NULL, NULL}, { NULL, NULL, }, }; + -- 2.7.4 From 1ea2bfca7a0a18c35fffdeeb7b1d0d6d45f98cbf Mon Sep 17 00:00:00 2001 From: Ravinder Date: Wed, 23 Feb 2022 16:41:00 +0530 Subject: [PATCH 03/16] Fix svace issues This patch handles the static analysis issues related to memory leak, deref after NULL, tainted int loop, vulnerable function uses etc. Change-Id: I9c1b6e6e6504433342544c406333e66abc98b7ac Signed-off-by: Abhay Agarwal --- src/thread-br.c | 26 +++++++++++++------------- src/thread-commissioner.c | 7 ++++--- src/thread-joiner.c | 2 -- src/thread-network.c | 8 +++++++- src/thread-socket-handler.c | 6 ++++-- tests/thread-core.c | 6 +++--- tests/thread-main.c | 2 +- tests/thread-network.c | 13 ++++++++++++- tests/thread-srp.c | 3 ++- 9 files changed, 46 insertions(+), 27 deletions(-) diff --git a/src/thread-br.c b/src/thread-br.c index 68010af..fa837c6 100644 --- a/src/thread-br.c +++ b/src/thread-br.c @@ -305,14 +305,14 @@ int thread_br_add_external_route(thread_instance_h instance, ipv6_prefix_len); if (is_stable) - strcat(msg, " s"); + strncat(msg, " s", 2); if (preference == 0) - strcat(msg, " low"); + strncat(msg, " low", 4); else if (preference == 1) - strcat(msg, " med"); + strncat(msg, " med", 4); else if (preference == 2) - strcat(msg, " high"); + strncat(msg, " high", 5); THREAD_DBG("DEBUG: BORDER ROUTER MESSAGE -> [%s]", msg); int ret = _thread_socket_client_execute(_thread_get_socket_fd(), @@ -418,24 +418,24 @@ int thread_br_add_onmesh_prefix(thread_instance_h instance, ipv6_prefix_len); if (preference == 0) - strcat(msg, " low "); + strncat(msg, " low ", 5); else if (preference == 1) - strcat(msg, " med "); + strncat(msg, " med ", 5); else if (preference == 2) - strcat(msg, " high "); + strncat(msg, " high ", 6); if (preferred) - strcat(msg, "p"); + strncat(msg, "p", 1); if (slaac) - strcat(msg, "a"); + strncat(msg, "a", 1); if (configure) - strcat(msg, "c"); + strncat(msg, "c", 1); if (default_route) - strcat(msg, "r"); + strncat(msg, "r", 1); if (on_mesh) - strcat(msg, "o"); + strncat(msg, "o", 1); if (stable) - strcat(msg, "s"); + strncat(msg, "s", 1); THREAD_DBG("DEBUG: BORDER ROUTER MESSAGE -> [%s]", msg); int ret = _thread_socket_client_execute(_thread_get_socket_fd(), diff --git a/src/thread-commissioner.c b/src/thread-commissioner.c index 4ee4788..922d9e0 100644 --- a/src/thread-commissioner.c +++ b/src/thread-commissioner.c @@ -68,6 +68,10 @@ int thread_commissioner_set_commisioning_data(thread_instance_h instance, THREAD_VALIDATE_INPUT_PARAMETER(joiner_passphrase); int joiner_passphrase_len = strlen(joiner_passphrase); + + if (!joiner_uuid || !strcmp(joiner_uuid, "")) + joiner_uuid = "*"; + int joiner_uuid_len = strlen(joiner_uuid); retv_if(joiner_passphrase_len < 6 || joiner_passphrase_len > 32, THREAD_ERROR_INVALID_PARAMETER); @@ -75,9 +79,6 @@ int thread_commissioner_set_commisioning_data(thread_instance_h instance, int ret = THREAD_ERROR_NONE; - if (!joiner_uuid || !strcmp(joiner_uuid, "")) - joiner_uuid = "*"; - char msg[THREAD_COMMISSIONER_JOINER_BUFFER_MAX]; snprintf(msg, THREAD_COMMISSIONER_JOINER_BUFFER_MAX, "commissioner joiner add %s %s", joiner_uuid, joiner_passphrase); diff --git a/src/thread-joiner.c b/src/thread-joiner.c index 5ff4bc0..24519bc 100644 --- a/src/thread-joiner.c +++ b/src/thread-joiner.c @@ -50,7 +50,6 @@ int thread_joiner_join_by_network_key(thread_instance_h instance, char buf[THREAD_MAX_BUFFER_SIZE]; snprintf(buf, sizeof(buf), "%s %s", set_network_key, network_key); ret = _thread_socket_client_execute(_thread_get_socket_fd(), buf, strlen(buf)); - retv_if(ret != THREAD_ERROR_NONE, ret); if (ret != THREAD_ERROR_NONE) { THREAD_DBG("socket dataset networkkey execute failed"); return ret; @@ -61,7 +60,6 @@ int thread_joiner_join_by_network_key(thread_instance_h instance, buf[0] = '\0'; snprintf(buf, sizeof(buf), "%s %s", set_panid, panid); ret = _thread_socket_client_execute(_thread_get_socket_fd(), buf, strlen(buf)); - retv_if(ret != THREAD_ERROR_NONE, ret); if (ret != THREAD_ERROR_NONE) { THREAD_DBG("socket dataset panid execute failed"); return ret; diff --git a/src/thread-network.c b/src/thread-network.c index 5cec98c..2e25d2c 100644 --- a/src/thread-network.c +++ b/src/thread-network.c @@ -145,7 +145,7 @@ int thread_network_set_active_dataset_tlvs(thread_instance_h instance, /* Print input data */ char buf[THREAD_MAX_BUFFER_SIZE]; for (int i = 0; i < buf_length; i++) - sprintf(buf + i*2, "%2.2x", tlvs_buffer[i]); + snprintf(buf + i*2, 3, "%2.2x", tlvs_buffer[i]); THREAD_DBG("Active dataset tlvs size: %d :: %s", buf_length, buf); bytes = g_bytes_new(tlvs_buffer, buf_length); @@ -216,6 +216,12 @@ int thread_network_get_active_dataset_tlvs(thread_instance_h instance, } *tlvs_buffer = g_malloc0(THREAD_MAX_BUFFER_SIZE*sizeof(uint8_t)); + if (!(*tlvs_buffer)) { + /* LCOV_EXCL_START */ + THREAD_ERR("g_malloc0 failed"); + return THREAD_ERROR_OUT_OF_MEMORY; + /* LCOV_EXCL_STOP */ + } index = 0; while (index < THREAD_MAX_BUFFER_SIZE) { if (buffer[index] == 'D') diff --git a/src/thread-socket-handler.c b/src/thread-socket-handler.c index f198622..553d9db 100644 --- a/src/thread-socket-handler.c +++ b/src/thread-socket-handler.c @@ -60,8 +60,10 @@ int _thread_socket_client_init(int *socket_fd, const char *if_name) ret = snprintf(sockname.sun_path, sizeof(sockname.sun_path), OPENTHREAD_POSIX_DAEMON_SOCKET_NAME, if_name); - retv_if(!(ret >= 0 && (size_t)ret < sizeof(sockname.sun_path)), - THREAD_ERROR_OPERATION_FAILED); + if (!(ret >= 0 && (size_t)ret < sizeof(sockname.sun_path))) { + close(session_fd); + return THREAD_ERROR_OPERATION_FAILED; + } THREAD_DBG("Connect on socket fd %d path %s", session_fd, sockname.sun_path); diff --git a/tests/thread-core.c b/tests/thread-core.c index 79af951..57c9749 100755 --- a/tests/thread-core.c +++ b/tests/thread-core.c @@ -78,9 +78,9 @@ static void __thread_scan_callback(int result, thread_network_scanning_state_e s void thread_device_role_callback(thread_device_role_e device_role, void *user_data) { - FUNC_ENTRY; - THREAD_DBG("Device Role is [%d]", device_role); - FUNC_EXIT; + FUNC_ENTRY; + THREAD_DBG("Device Role is [%d]", device_role); + FUNC_EXIT; } diff --git a/tests/thread-main.c b/tests/thread-main.c index 049d514..6f92c9f 100755 --- a/tests/thread-main.c +++ b/tests/thread-main.c @@ -115,9 +115,9 @@ int main(int arg, char **argv) g_io_add_watch(channel, (G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL), on_menu_manager_keyboard, manager); g_main_loop_run(mainloop); + thread_disable(manager->t_instance); OUT: - thread_disable(manager->t_instance); __deinit_func(); menu_manager_free(manager); diff --git a/tests/thread-network.c b/tests/thread-network.c index 2ad3fb3..54c8ed9 100644 --- a/tests/thread-network.c +++ b/tests/thread-network.c @@ -101,6 +101,10 @@ static int run_thread_network_set_active_dataset_tlvs(MManager *mm, struct menu_ msg("Set active dataset tlvs size: %d :: %s", buf_length, g_str_tlvs_buffer); + if (!buf_length || buf_length > (MENU_DATA_SIZE + 1)) { + msg("Invalid Input"); + goto OUT; + } for (int i = 0; i < buf_length; i++) { char subbuff[3]; memcpy(subbuff, &g_str_tlvs_buffer[2*i], 2); @@ -128,10 +132,17 @@ static int run_thread_network_get_active_dataset_tlvs(MManager *mm, struct menu_ int buf_length; uint8_t *tlvs_buffer; + char buf[1024]; + int ret = thread_network_get_active_dataset_tlvs(g_instance, &tlvs_buffer, &buf_length); if (ret == THREAD_ERROR_NONE) { msg("thread_network_get_active_dataset_tlvs success"); - msg("Active dataset tlvs size: %d :: %s", buf_length, tlvs_buffer); + + for (int i = 0; i < buf_length; i++) + snprintf(buf + i*2, 3, "%2.2x", tlvs_buffer[i]); + + msg("Active dataset tlvs size: %d :: %s", buf_length, buf); + free(tlvs_buffer); } else { msg("thread_network_get_active_dataset_tlvs failed"); } diff --git a/tests/thread-srp.c b/tests/thread-srp.c index 460edc9..70fb291 100644 --- a/tests/thread-srp.c +++ b/tests/thread-srp.c @@ -136,7 +136,8 @@ static int __run_thread_srp_client_register_service(MManager *mm, struct menu_da if (g_instance == NULL) goto OUT; - g_port = (uint64_t)atoi(g_str_port); + g_port = (uint64_t)strtoll(g_str_port, (char **)NULL, 10); + int ret = thread_srp_client_register_service(g_instance, g_service_name, g_service_type, g_port); if (ret == THREAD_ERROR_NONE) -- 2.7.4 From 4f36e7421b78028e5a3852f5fe5b400c0e733d3b Mon Sep 17 00:00:00 2001 From: Rohit Singh Date: Wed, 2 Mar 2022 12:57:59 +0530 Subject: [PATCH 04/16] Fix coverity issue Change-Id: Ica1bd60af2fe5fd831d9941704ec3ed4ccb3dcb7 Signed-off-by: Rohit Singh --- src/thread-commissioner.c | 7 +++---- src/thread-dbus-handler.c | 6 +++++- src/thread-network.c | 6 +++--- tests/thread-menu.h | 2 +- tests/thread-network.c | 33 ++++++++++++++++++--------------- tests/thread-scenarios.c | 14 +++++++------- 6 files changed, 37 insertions(+), 31 deletions(-) diff --git a/src/thread-commissioner.c b/src/thread-commissioner.c index 922d9e0..eb0ffb5 100644 --- a/src/thread-commissioner.c +++ b/src/thread-commissioner.c @@ -66,19 +66,18 @@ int thread_commissioner_set_commisioning_data(thread_instance_h instance, THREAD_CHECK_INIT_STATUS(); THREAD_VALIDATE_INPUT_PARAMETER(instance); THREAD_VALIDATE_INPUT_PARAMETER(joiner_passphrase); + THREAD_VALIDATE_INPUT_PARAMETER(joiner_uuid); + int ret = THREAD_ERROR_NONE; int joiner_passphrase_len = strlen(joiner_passphrase); - if (!joiner_uuid || !strcmp(joiner_uuid, "")) + if (strcmp(joiner_uuid, "") == 0) joiner_uuid = "*"; - int joiner_uuid_len = strlen(joiner_uuid); retv_if(joiner_passphrase_len < 6 || joiner_passphrase_len > 32, THREAD_ERROR_INVALID_PARAMETER); retv_if(joiner_uuid_len > 16, THREAD_ERROR_INVALID_PARAMETER); - int ret = THREAD_ERROR_NONE; - char msg[THREAD_COMMISSIONER_JOINER_BUFFER_MAX]; snprintf(msg, THREAD_COMMISSIONER_JOINER_BUFFER_MAX, "commissioner joiner add %s %s", joiner_uuid, joiner_passphrase); diff --git a/src/thread-dbus-handler.c b/src/thread-dbus-handler.c index 6796cf8..fff8369 100644 --- a/src/thread-dbus-handler.c +++ b/src/thread-dbus-handler.c @@ -294,7 +294,11 @@ int _thread_dbus_register_event(const char *event_name, const char *interface, retv_if(event_data == NULL, THREAD_ERROR_OUT_OF_MEMORY); event_data->event_name = g_strdup(event_name); - retv_if(event_data->event_name == NULL, THREAD_ERROR_OUT_OF_MEMORY); + if (event_data->event_name == NULL) { + THREAD_DBG("Event Name is NULL"); + g_free(event_data); + return THREAD_ERROR_OUT_OF_MEMORY; + } /* Register event */ event_data->event_id = g_dbus_connection_signal_subscribe(g_system_connection, diff --git a/src/thread-network.c b/src/thread-network.c index f2a35e2..2f3caa9 100644 --- a/src/thread-network.c +++ b/src/thread-network.c @@ -89,9 +89,9 @@ int thread_network_create_operational_network(thread_instance_h instance, } new_network->is_network_active = FALSE; - g_strlcpy(new_network->name, name, THREAD_NETWORK_NAME_MAX + 1); - g_strlcpy(new_network->key, key, THREAD_NETWORK_KEY_STRING_MAX + 1); - g_strlcpy(new_network->pskc, pskc, THREAD_NETWORK_PSKC_STRING_MAX + 1); + (void)g_strlcpy(new_network->name, name, THREAD_NETWORK_NAME_MAX + 1); + (void)g_strlcpy(new_network->key, key, THREAD_NETWORK_KEY_STRING_MAX + 1); + (void)g_strlcpy(new_network->pskc, pskc, THREAD_NETWORK_PSKC_STRING_MAX + 1); new_network->channel = channel; new_network->extended_panid = extended_panid; new_network->panid = panid; diff --git a/tests/thread-menu.h b/tests/thread-menu.h index b7ec3f4..c871fc3 100755 --- a/tests/thread-menu.h +++ b/tests/thread-menu.h @@ -117,7 +117,7 @@ do { \ } while (0) #define MENU_DATA_SIZE 255 - +#define NETWORK_DATA_SIZE 256 /* * Horizontal Line - width: 65 * .12345678901234567890123456789012345678901234567890. diff --git a/tests/thread-network.c b/tests/thread-network.c index caecbe0..d2002e6 100644 --- a/tests/thread-network.c +++ b/tests/thread-network.c @@ -32,22 +32,22 @@ #include "thread-log.h" #include "thread-util.h" -static char g_name[MENU_DATA_SIZE + 1] = {"Thread-test"}; -static char g_key[MENU_DATA_SIZE + 1]; -static char g_pskc[MENU_DATA_SIZE + 1]; -static char g_str_channel[MENU_DATA_SIZE + 1] = {"07fff800"}; -static char g_str_extended_panid[MENU_DATA_SIZE + 1] = {"18446744073709551615"}; /* UINT64_MAX */ -static char g_str_panid[MENU_DATA_SIZE + 1] = {"65535"}; /* UINT16_MAX */ -static char g_str_buf_length[MENU_DATA_SIZE + 1]; -static char g_str_tlvs_buffer[MENU_DATA_SIZE + 1]; +static char g_name[NETWORK_DATA_SIZE + 1] = {"Thread-test"}; +static char g_key[NETWORK_DATA_SIZE + 1]; +static char g_pskc[NETWORK_DATA_SIZE + 1]; +static char g_str_channel[NETWORK_DATA_SIZE + 1] = {"07fff800"}; +static char g_str_extended_panid[NETWORK_DATA_SIZE + 1] = {"18446744073709551615"}; /* UINT64_MAX */ +static char g_str_panid[NETWORK_DATA_SIZE + 1] = {"65535"}; /* UINT16_MAX */ +static char g_str_buf_length[NETWORK_DATA_SIZE + 1]; +static char g_str_tlvs_buffer[2*NETWORK_DATA_SIZE + 1]; static thread_network_h g_network; uint32_t g_channel; uint64_t g_extended_panid; uint16_t g_panid; -static char g_str_ipaddr_type[MENU_DATA_SIZE + 1] = "0"; -static char g_str_ipaddr[MENU_DATA_SIZE + 1]; +static char g_str_ipaddr_type[NETWORK_DATA_SIZE + 1] = "0"; +static char g_str_ipaddr[NETWORK_DATA_SIZE + 1]; uint8_t g_ipv6_address[THREAD_IPV6_ADDRESS_SIZE]; @@ -129,14 +129,15 @@ OUT: static int run_thread_network_get_active_dataset_tlvs(MManager *mm, struct menu_data *menu) { FUNC_ENTRY; + + uint8_t *tlvs_buffer = NULL; + char buf[1024]; + int buf_length; + thread_instance_h g_instance = mm->t_instance; if (g_instance == NULL) goto OUT; - int buf_length; - uint8_t *tlvs_buffer; - char buf[1024]; - int ret = thread_network_get_active_dataset_tlvs(g_instance, &tlvs_buffer, &buf_length); if (ret == THREAD_ERROR_NONE) { msg("thread_network_get_active_dataset_tlvs success"); @@ -145,11 +146,13 @@ static int run_thread_network_get_active_dataset_tlvs(MManager *mm, struct menu_ snprintf(buf + i*2, 3, "%2.2x", tlvs_buffer[i]); msg("Active dataset tlvs size: %d :: %s", buf_length, buf); - free(tlvs_buffer); } else { msg("thread_network_get_active_dataset_tlvs failed"); } OUT: + if (tlvs_buffer) + free(tlvs_buffer); + FUNC_EXIT; return RET_SUCCESS; } diff --git a/tests/thread-scenarios.c b/tests/thread-scenarios.c index 116e627..d7fc126 100644 --- a/tests/thread-scenarios.c +++ b/tests/thread-scenarios.c @@ -32,12 +32,12 @@ #include "thread-log.h" #include "thread-util.h" -static char g_name[MENU_DATA_SIZE + 1] = {"Thread-test"}; -static char g_key[MENU_DATA_SIZE + 1]; -static char g_pskc[MENU_DATA_SIZE + 1]; -static char g_str_channel[MENU_DATA_SIZE + 1] = {"07fff800"}; -static char g_str_extended_panid[MENU_DATA_SIZE + 1] = {"18446744073709551615"}; /* UINT64_MAX */ -static char g_str_panid[MENU_DATA_SIZE + 1] = {"65535"}; /* UINT16_MAX */ +static char g_name[NETWORK_DATA_SIZE + 1] = {"Thread-test"}; +static char g_key[NETWORK_DATA_SIZE + 1]; +static char g_pskc[NETWORK_DATA_SIZE + 1]; +static char g_str_channel[NETWORK_DATA_SIZE + 1] = {"07fff800"}; +static char g_str_extended_panid[NETWORK_DATA_SIZE + 1] = {"18446744073709551615"}; /* UINT64_MAX */ +static char g_str_panid[NETWORK_DATA_SIZE + 1] = {"65535"}; /* UINT16_MAX */ static thread_network_h g_network; static char g_joiner_uuid[MENU_DATA_SIZE + 1]; @@ -90,7 +90,7 @@ static int run_thread_scenario_cleanup(MManager *mm, struct menu_data *menu) int ret = THREAD_ERROR_NONE; msg(" ### thread_scenario_cleanup ###"); - thread_instance_h *g_instance = &(mm->t_instance); + thread_instance_h g_instance = mm->t_instance; /* thread disable */ ret = thread_disable(g_instance); -- 2.7.4 From fb34ca69f3d53c440172f7e1ac5d006c4b748032 Mon Sep 17 00:00:00 2001 From: Wootak Jung Date: Mon, 7 Mar 2022 08:32:27 +0900 Subject: [PATCH 05/16] Fix 64bit build error Change-Id: Icc9ddd60b872c37cfaad84fbeee5efd2255408d5 --- src/thread-br.c | 26 +++++++++++++------------- src/thread-core.c | 2 +- src/thread-network.c | 14 +++++++------- src/thread-socket-handler.c | 2 +- src/thread-srp.c | 4 ++-- src/thread-util.c | 4 ++-- tests/thread-core.c | 10 +++++----- tests/thread-network.c | 2 +- tests/thread-scenarios.c | 2 +- tests/thread-srp.c | 2 +- 10 files changed, 34 insertions(+), 34 deletions(-) mode change 100755 => 100644 src/thread-util.c diff --git a/src/thread-br.c b/src/thread-br.c index fa837c6..7fa9d07 100644 --- a/src/thread-br.c +++ b/src/thread-br.c @@ -305,14 +305,14 @@ int thread_br_add_external_route(thread_instance_h instance, ipv6_prefix_len); if (is_stable) - strncat(msg, " s", 2); + strncat(msg, " s", 3); if (preference == 0) - strncat(msg, " low", 4); + strncat(msg, " low", 5); else if (preference == 1) - strncat(msg, " med", 4); + strncat(msg, " med", 5); else if (preference == 2) - strncat(msg, " high", 5); + strncat(msg, " high", 6); THREAD_DBG("DEBUG: BORDER ROUTER MESSAGE -> [%s]", msg); int ret = _thread_socket_client_execute(_thread_get_socket_fd(), @@ -418,24 +418,24 @@ int thread_br_add_onmesh_prefix(thread_instance_h instance, ipv6_prefix_len); if (preference == 0) - strncat(msg, " low ", 5); + strncat(msg, " low ", 6); else if (preference == 1) - strncat(msg, " med ", 5); + strncat(msg, " med ", 6); else if (preference == 2) - strncat(msg, " high ", 6); + strncat(msg, " high ", 7); if (preferred) - strncat(msg, "p", 1); + strncat(msg, "p", 2); if (slaac) - strncat(msg, "a", 1); + strncat(msg, "a", 2); if (configure) - strncat(msg, "c", 1); + strncat(msg, "c", 2); if (default_route) - strncat(msg, "r", 1); + strncat(msg, "r", 2); if (on_mesh) - strncat(msg, "o", 1); + strncat(msg, "o", 2); if (stable) - strncat(msg, "s", 1); + strncat(msg, "s", 2); THREAD_DBG("DEBUG: BORDER ROUTER MESSAGE -> [%s]", msg); int ret = _thread_socket_client_execute(_thread_get_socket_fd(), diff --git a/src/thread-core.c b/src/thread-core.c index 3eb8896..66a1620 100644 --- a/src/thread-core.c +++ b/src/thread-core.c @@ -546,7 +546,7 @@ int thread_get_extended_address(thread_instance_h instance, uint64_t *extended_a retv_if(ret != THREAD_ERROR_NONE, ret); g_variant_get(out, "t", extended_address); - THREAD_DBG("Thread extended address: %llu", *extended_address); + THREAD_DBG("Thread extended address: %zu", (size_t)*extended_address); g_variant_unref(out); FUNC_EXIT; diff --git a/src/thread-network.c b/src/thread-network.c index 2f3caa9..bdfe802 100644 --- a/src/thread-network.c +++ b/src/thread-network.c @@ -70,7 +70,7 @@ int thread_network_create_operational_network(thread_instance_h instance, THREAD_DBG("Network key: %s", key); THREAD_DBG("Network pskc: %s", pskc); THREAD_DBG("Network channel: 0x%8.8x", channel); - THREAD_DBG("Network extended_panid: %llu", extended_panid); + THREAD_DBG("Network extended_panid: %zu", (size_t)extended_panid); THREAD_DBG("Network panid: %u", panid); /* Free existing current network */ @@ -206,7 +206,7 @@ int thread_network_get_active_dataset_tlvs(thread_instance_h instance, THREAD_DBG("Failed to execute command %s", cmd_buffer); return ret; } - THREAD_DBG("Executed command '%s' with size %d", cmd_buffer, strlen(cmd_buffer)); + THREAD_DBG("Executed command '%s' with size %zu", cmd_buffer, strlen(cmd_buffer)); /* Check response */ ret = _thread_socket_client_read(session_fd, buffer); @@ -276,7 +276,7 @@ int thread_network_attach(thread_instance_h instance) THREAD_DBG("Network pskc: %s", network->pskc); THREAD_DBG("Network channel: 0x%8.8x", network->channel); - THREAD_DBG("Network extended_panid: %llu", network->extended_panid); + THREAD_DBG("Network extended_panid: %zu", (size_t)network->extended_panid); if (network->extended_panid == UINT64_MAX) THREAD_DBG("extended_panid is UINT64_MAX, "\ "Random extended_panid will be used"); @@ -297,7 +297,7 @@ int thread_network_attach(thread_instance_h instance) /* Network key builder */ GVariantBuilder *key_builder; key_builder = g_variant_builder_new(G_VARIANT_TYPE("ay")); - THREAD_DBG("key str length: %d", strlen(network->key)); + THREAD_DBG("key str length: %zu", strlen(network->key)); for (int i = 0; i < strlen(network->key); i++) { g_variant_builder_add(key_builder, "y", (unsigned char)network->key[i]); @@ -306,7 +306,7 @@ int thread_network_attach(thread_instance_h instance) /* pskc builder */ GVariantBuilder *pskc_builder; pskc_builder = g_variant_builder_new(G_VARIANT_TYPE("ay")); - THREAD_DBG("pskc str length: %d", strlen(network->pskc)); + THREAD_DBG("pskc str length: %zu", strlen(network->pskc)); for (int i = 0; i < strlen(network->pskc); i++) { g_variant_builder_add(pskc_builder, "y", (unsigned char)network->pskc[i]); @@ -387,7 +387,7 @@ int thread_get_ipaddr(thread_instance_h instance, thread_ipaddr_foreach_cb callb THREAD_DBG("Failed to execute command %s", cmd_buffer); return ret; } - THREAD_DBG("Executed command '%s' with size %d", cmd_buffer, buf_size); + THREAD_DBG("Executed command '%s' with size %zu", cmd_buffer, buf_size); /* Check response */ ret = _thread_socket_client_read(session_fd, buffer); @@ -406,7 +406,7 @@ int thread_get_ipaddr(thread_instance_h instance, thread_ipaddr_foreach_cb callb } else { ipaddr[pos] = '\0'; if (pos > 0) { - THREAD_DBG("IP address: %s, length: %d", ipaddr, + THREAD_DBG("IP address: %s, length: %zu", ipaddr, strlen(ipaddr)); callback(++count, ipaddr, ipaddr_type, user_data); } diff --git a/src/thread-socket-handler.c b/src/thread-socket-handler.c index 553d9db..5de3fe0 100644 --- a/src/thread-socket-handler.c +++ b/src/thread-socket-handler.c @@ -254,7 +254,7 @@ int _thread_socket_client_write(int session_fd, if (!request_buffer || buf_size <= 0) return THREAD_ERROR_INVALID_PARAMETER; - THREAD_DBG("Socket write command '%s' with size %d", + THREAD_DBG("Socket write command '%s' with size %zu", request_buffer, buf_size); rval = write(session_fd, request_buffer, buf_size); diff --git a/src/thread-srp.c b/src/thread-srp.c index edb5066..5417c4e 100644 --- a/src/thread-srp.c +++ b/src/thread-srp.c @@ -161,8 +161,8 @@ int thread_srp_client_register_service(thread_instance_h instance, const char *msg = THREAD_SRP_CLIENT_REGISTER_SERVICE_CMD; char buffer[THREAD_MAX_BUFFER_SIZE] = {0}; - snprintf(buffer, sizeof(buffer), "%s %s %s %llu", msg, - service_name, service_type, port); + snprintf(buffer, sizeof(buffer), "%s %s %s %zu", msg, + service_name, service_type, (size_t)port); ret = _thread_socket_client_execute(_thread_get_socket_fd(), buffer, strlen(buffer)); if (ret != THREAD_ERROR_NONE && ret != THREAD_ERROR_ALREADY_DONE) { diff --git a/src/thread-util.c b/src/thread-util.c old mode 100755 new mode 100644 index 1225fa9..fc9eebd --- a/src/thread-util.c +++ b/src/thread-util.c @@ -188,9 +188,9 @@ void __thread_dbus_handle_scan_cb(gboolean res, &version, &is_native, &is_native)) { THREAD_DBG("Got the thread beacon..."); - THREAD_DBG("ext_address: %llu", ext_address); + THREAD_DBG("ext_address: %zu", (size_t)ext_address); THREAD_DBG("network_name: %s", network_name); - THREAD_DBG("ext panid: %llu", ext_panid); + THREAD_DBG("ext panid: %zu", (size_t)ext_panid); int i = 0; while (steering_data[i] != '\0') { diff --git a/tests/thread-core.c b/tests/thread-core.c index 57c9749..7d2d987 100755 --- a/tests/thread-core.c +++ b/tests/thread-core.c @@ -57,9 +57,9 @@ static void __thread_scan_callback(int result, thread_network_scanning_state_e s else { msg("thread_network_scanning_state: [%d]", state); msg("network_name: [%s]", network_name); - msg("ext_address: [%llu]", ext_address); - msg("ext_panidi: [%llu]", ext_panidi); - msg("steering_data: [%d]", (int)steering_data); + msg("ext_address: [%zu]", (size_t)ext_address); + msg("ext_panidi: [%zu]", (size_t)ext_panidi); + msg("steering_data: [%s]", steering_data); msg("length: [%d]", length); msg("panid: [%hu]", panid); msg("joiner_udp_port: [%hu]", joiner_udp_port); @@ -318,8 +318,8 @@ static int run_thread_get_extended_address(MManager *mm, struct menu_data *menu) int ret = thread_get_extended_address(g_instance, &g_extended_address); if (ret == THREAD_ERROR_NONE) { msg("thread_get_extended_address success"); - msg("- thread_get_exenteded_address() ret: Device extended address->[%llu]", - g_extended_address); + msg("- thread_get_exenteded_address() ret: Device extended address->[%zu]", + (size_t)g_extended_address); } else msg("thread_get_extended_address failed"); OUT: diff --git a/tests/thread-network.c b/tests/thread-network.c index d2002e6..2ed4fbd 100644 --- a/tests/thread-network.c +++ b/tests/thread-network.c @@ -76,7 +76,7 @@ static int run_thread_network_create_operational_network(MManager *mm, struct me goto OUT; sscanf(g_str_channel, "%x", &g_channel); - sscanf(g_str_extended_panid, "%llu", &g_extended_panid); + sscanf(g_str_extended_panid, "%zu", (size_t *)&g_extended_panid); sscanf(g_str_panid, "%hu", &g_panid); int ret = thread_network_create_operational_network(g_instance, g_name, g_key, diff --git a/tests/thread-scenarios.c b/tests/thread-scenarios.c index d7fc126..369282c 100644 --- a/tests/thread-scenarios.c +++ b/tests/thread-scenarios.c @@ -151,7 +151,7 @@ static int run_thread_start_leader_as_commissioner(MManager *mm, struct menu_dat goto OUT; sscanf(g_str_channel, "%x", &g_channel); - sscanf(g_str_extended_panid, "%llu", &g_extended_panid); + sscanf(g_str_extended_panid, "%zu", (size_t *)&g_extended_panid); sscanf(g_str_panid, "%hu", &g_panid); int ret = THREAD_ERROR_NONE; diff --git a/tests/thread-srp.c b/tests/thread-srp.c index 70fb291..8b50389 100644 --- a/tests/thread-srp.c +++ b/tests/thread-srp.c @@ -193,7 +193,7 @@ static int __run_thread_srp_server_get_registered_service(MManager *mm, struct m if (ret == THREAD_ERROR_NONE) { msg("thread_srp_get_registered_service success"); - msg("service: [%s], [%s], [%llu], [%s]", service_name, address, port, is_deleted ? "TRUE" : "FALSE"); + msg("service: [%s], [%s], [%zu], [%s]", service_name, address, (size_t)port, is_deleted ? "TRUE" : "FALSE"); } else msg("thread_srp_get_registered_service failed"); OUT: -- 2.7.4 From c5e2ec50b4a578feef011b557a91f9a5320846f1 Mon Sep 17 00:00:00 2001 From: Ravinder Date: Fri, 4 Mar 2022 17:09:15 +0530 Subject: [PATCH 06/16] Update network API's Change-Id: I57e218960a6f0d5a0350de1cfc242ba18b25cfdb --- src/thread-network.c | 35 ++++++++++++++++++++++++++++++++++- tests/thread-network.c | 25 ++++++++++++++++++++++--- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/thread-network.c b/src/thread-network.c index 2f3caa9..b2be07d 100644 --- a/src/thread-network.c +++ b/src/thread-network.c @@ -333,15 +333,48 @@ done: return ret; } +static int __thread_detach_active_network() +{ + FUNC_ENTRY; + int ret = THREAD_ERROR_NONE; + + THREAD_DBG("Detach current active network dataset"); + ret = _thread_dbus_sync_method_call(THREAD_DBUS_DETACH_METHOD, + g_variant_new("()")); + if (ret != THREAD_ERROR_NONE) + THREAD_ERR("Thread Detach failed"); + else + THREAD_DBG("Thread Detach successful"); + + FUNC_EXIT; + return ret; +} + int thread_network_detach(thread_instance_h instance) { FUNC_ENTRY; THREAD_CHECK_SUPPORTED_FEATURE(THREAD_FEATURE_COMMON); THREAD_CHECK_INIT_STATUS(); THREAD_VALIDATE_INPUT_PARAMETER(instance); - int ret = THREAD_ERROR_NOT_SUPPORTED; + int ret = THREAD_ERROR_NONE; + + thread_instance_s *current_instance = instance; + thread_network_s *network = current_instance->network; + + if (!network) + return THREAD_ERROR_INVALID_PARAMETER; + if (network->is_network_active == TRUE) + ret = __thread_detach_active_network(); + if (ret != THREAD_ERROR_NONE) { + THREAD_ERR("Thread Detach failed"); + goto done; + } + network->is_network_active = FALSE; + THREAD_DBG("Thread Detach successful"); + +done: FUNC_EXIT; return ret; } diff --git a/tests/thread-network.c b/tests/thread-network.c index d2002e6..1f4cc81 100644 --- a/tests/thread-network.c +++ b/tests/thread-network.c @@ -174,6 +174,23 @@ OUT: return RET_SUCCESS; } +static int run_thread_network_detach(MManager *mm, struct menu_data *menu) +{ + FUNC_ENTRY; + thread_instance_h g_instance = mm->t_instance; + if (g_instance == NULL) + goto OUT; + + int ret = thread_network_detach(g_instance); + if (ret == THREAD_ERROR_NONE) + msg("thread_network_detach success"); + else + msg("thread_network_detach failed"); +OUT: + FUNC_EXIT; + return RET_SUCCESS; +} + static void __get_ipaddr_callback(int index, char* ipaddr, thread_ipaddr_type_e ipaddr_type, void *user_data) { FUNC_ENTRY; @@ -309,11 +326,13 @@ struct menu_data menu_thread_network[] = { NULL, run_thread_network_get_active_dataset_tlvs, NULL }, { "5", "thread_network_attach", NULL, run_thread_network_attach, NULL}, - { "6", "thread_get_ipaddr", + { "6", "thread_network_detach", + NULL, run_thread_network_detach, NULL}, + { "7", "thread_get_ipaddr", menu_thread_get_ipaddr, NULL, NULL}, - { "7", "thread_add_ipaddr", + { "8", "thread_add_ipaddr", menu_thread_add_ipaddr, NULL, NULL}, - { "8", "thread_remove_ipaddr", + { "9", "thread_remove_ipaddr", menu_thread_remove_ipaddr, NULL, NULL}, { NULL, NULL, }, }; -- 2.7.4 From c0d1ef35d7543fbbc15af8cb1a7c2485fa9d0e3f Mon Sep 17 00:00:00 2001 From: "hyunuk.tak" Date: Fri, 18 Mar 2022 15:20:50 +0900 Subject: [PATCH 07/16] Move test files to thread-test folder Change-Id: Iaccf336e0d02b79c718f961e089c51215758206a Signed-off-by: hyunuk.tak --- CMakeLists.txt | 5 +---- tests/{ => thread-test}/CMakeLists.txt | 0 tests/{ => thread-test}/thread-br.c | 0 tests/{ => thread-test}/thread-commissioner.c | 0 tests/{ => thread-test}/thread-core.c | 0 tests/{ => thread-test}/thread-init.c | 0 tests/{ => thread-test}/thread-joiner.c | 0 tests/{ => thread-test}/thread-main.c | 0 tests/{ => thread-test}/thread-menu.c | 0 tests/{ => thread-test}/thread-menu.h | 0 tests/{ => thread-test}/thread-network.c | 0 tests/{ => thread-test}/thread-srp.c | 0 tests/{ => thread-test}/thread-util.c | 0 tests/{ => thread-test}/thread-util.h | 0 14 files changed, 1 insertion(+), 4 deletions(-) rename tests/{ => thread-test}/CMakeLists.txt (100%) rename tests/{ => thread-test}/thread-br.c (100%) rename tests/{ => thread-test}/thread-commissioner.c (100%) rename tests/{ => thread-test}/thread-core.c (100%) rename tests/{ => thread-test}/thread-init.c (100%) rename tests/{ => thread-test}/thread-joiner.c (100%) rename tests/{ => thread-test}/thread-main.c (100%) rename tests/{ => thread-test}/thread-menu.c (100%) rename tests/{ => thread-test}/thread-menu.h (100%) rename tests/{ => thread-test}/thread-network.c (100%) rename tests/{ => thread-test}/thread-srp.c (100%) rename tests/{ => thread-test}/thread-util.c (100%) rename tests/{ => thread-test}/thread-util.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index a9da0bc..59a94cf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -74,10 +74,7 @@ CONFIGURE_FILE( ) INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${fw_name}.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) -#ADD_SUBDIRECTORY(tests/test) -#ADD_SUBDIRECTORY(tests/unittest) - -ADD_SUBDIRECTORY(tests) +ADD_SUBDIRECTORY(tests/thread-test) IF(UNIX) diff --git a/tests/CMakeLists.txt b/tests/thread-test/CMakeLists.txt similarity index 100% rename from tests/CMakeLists.txt rename to tests/thread-test/CMakeLists.txt diff --git a/tests/thread-br.c b/tests/thread-test/thread-br.c similarity index 100% rename from tests/thread-br.c rename to tests/thread-test/thread-br.c diff --git a/tests/thread-commissioner.c b/tests/thread-test/thread-commissioner.c similarity index 100% rename from tests/thread-commissioner.c rename to tests/thread-test/thread-commissioner.c diff --git a/tests/thread-core.c b/tests/thread-test/thread-core.c similarity index 100% rename from tests/thread-core.c rename to tests/thread-test/thread-core.c diff --git a/tests/thread-init.c b/tests/thread-test/thread-init.c similarity index 100% rename from tests/thread-init.c rename to tests/thread-test/thread-init.c diff --git a/tests/thread-joiner.c b/tests/thread-test/thread-joiner.c similarity index 100% rename from tests/thread-joiner.c rename to tests/thread-test/thread-joiner.c diff --git a/tests/thread-main.c b/tests/thread-test/thread-main.c similarity index 100% rename from tests/thread-main.c rename to tests/thread-test/thread-main.c diff --git a/tests/thread-menu.c b/tests/thread-test/thread-menu.c similarity index 100% rename from tests/thread-menu.c rename to tests/thread-test/thread-menu.c diff --git a/tests/thread-menu.h b/tests/thread-test/thread-menu.h similarity index 100% rename from tests/thread-menu.h rename to tests/thread-test/thread-menu.h diff --git a/tests/thread-network.c b/tests/thread-test/thread-network.c similarity index 100% rename from tests/thread-network.c rename to tests/thread-test/thread-network.c diff --git a/tests/thread-srp.c b/tests/thread-test/thread-srp.c similarity index 100% rename from tests/thread-srp.c rename to tests/thread-test/thread-srp.c diff --git a/tests/thread-util.c b/tests/thread-test/thread-util.c similarity index 100% rename from tests/thread-util.c rename to tests/thread-test/thread-util.c diff --git a/tests/thread-util.h b/tests/thread-test/thread-util.h similarity index 100% rename from tests/thread-util.h rename to tests/thread-test/thread-util.h -- 2.7.4 From dcb4ae2c14023178e02d5f263a75094937ad3dcf Mon Sep 17 00:00:00 2001 From: "hyunuk.tak" Date: Fri, 18 Mar 2022 15:24:26 +0900 Subject: [PATCH 08/16] Initial thread unittest codes Change-Id: Id88de3baf2d2c874da4c62b3bea05809382179de Signed-off-by: hyunuk.tak --- packaging/capi-network-thread.spec | 54 ++++++++++++++++++++++- tests/unittest/CMakeLists.txt | 25 +++++++++++ tests/unittest/mocks/thread-mock-dbus.cpp | 66 +++++++++++++++++++++++++++++ tests/unittest/mocks/thread-mock-dummy.cpp | 66 +++++++++++++++++++++++++++++ tests/unittest/mocks/thread-mock-dummy.h | 30 +++++++++++++ tests/unittest/mocks/thread-mock-socket.cpp | 62 +++++++++++++++++++++++++++ tests/unittest/mocks/thread-mock-socket.h | 29 +++++++++++++ tests/unittest/mocks/thread-mock.cpp | 23 ++++++++++ tests/unittest/thread-unittest-init.cpp | 52 +++++++++++++++++++++++ tests/unittest/thread-unittest-main.cpp | 23 ++++++++++ 10 files changed, 429 insertions(+), 1 deletion(-) create mode 100644 tests/unittest/CMakeLists.txt create mode 100644 tests/unittest/mocks/thread-mock-dbus.cpp create mode 100644 tests/unittest/mocks/thread-mock-dummy.cpp create mode 100644 tests/unittest/mocks/thread-mock-dummy.h create mode 100644 tests/unittest/mocks/thread-mock-socket.cpp create mode 100644 tests/unittest/mocks/thread-mock-socket.h create mode 100644 tests/unittest/mocks/thread-mock.cpp create mode 100644 tests/unittest/thread-unittest-init.cpp create mode 100644 tests/unittest/thread-unittest-main.cpp diff --git a/packaging/capi-network-thread.spec b/packaging/capi-network-thread.spec index 1309292..5f79260 100644 --- a/packaging/capi-network-thread.spec +++ b/packaging/capi-network-thread.spec @@ -16,8 +16,13 @@ BuildRequires: pkgconfig(vconf) BuildRequires: pkgconfig(capi-appfw-app-control) BuildRequires: pkgconfig(capi-base-common) BuildRequires: pkgconfig(capi-system-info) +BuildRequires: pkgconfig(gtest) BuildRequires: cmake +%if 0%{?gcov:1} +BuildRequires: lcov +%endif + %description Network Thread Framework @@ -39,6 +44,14 @@ Requires: %{name} = %{version}-%{release} %description test This package is for Network Thread Framework CAPI test application +%if 0%{?gcov:1} +%package gcov +Summary: Coverage Data of %{name} + +%description gcov +The %{name}-gcov package contains gcov objects +%endif + %prep %setup -q cp %{SOURCE1001} %{SOURCE1002} . @@ -57,17 +70,51 @@ export CXXFLAGS+=" -DARCH64" export FFLAGS+=" -DARCH64" %endif +%build +%if 0%{?gcov:1} +export CFLAGS+=" -fprofile-arcs -ftest-coverage" +export CXXFLAGS+=" -fprofile-arcs -ftest-coverage" +export FFLAGS+=" -fprofile-arcs -ftest-coverage" +export LDFLAGS+=" -lgcov" +%endif + %cmake MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'` -%cmake . -DFULLVER=%{version} -DMAJORVER=${MAJORVER} +%cmake . \ + -DFULLVER=%{version} \ + -DMAJORVER=${MAJORVER} \ +%if 0%{?gcov:1} + -DUNITTEST=ON \ +%endif + -DBIN_DIR:PATH=%{_bindir} make %{?jobs:-j%jobs} +%if 0%{?gcov:1} +mkdir -p gcov-obj +find . -name '*.gcno' -exec cp '{}' gcov-obj ';' +%endif + %install rm -rf %{buildroot} %make_install +%if 0%{?gcov:1} +mkdir -p %{buildroot}%{_datadir}/gcov/obj +install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj +%endif + +%check +%if 0%{?gcov:1} +LD_LIBRARY_PATH=%{buildroot}%{_libdir} ./tests/unittest/thread-unittest +%endif + +%if 0%{?gcov:1} +lcov -c --ignore-errors graph --no-external -b . -d . -o %{name}.info +genhtml %{name}.info -o out --legend --show-details +%endif + %post -p /sbin/ldconfig %postun -p /sbin/ldconfig @@ -88,4 +135,9 @@ rm -rf %{buildroot} %files test %manifest %{name}.manifest %attr(777,network_fw,network_fw) %{_bindir}/thread-test +%attr(777,network_fw,network_fw) %{_bindir}/thread-unittest +%if 0%{?gcov:1} +%files gcov +%{_datadir}/gcov/obj/* +%endif diff --git a/tests/unittest/CMakeLists.txt b/tests/unittest/CMakeLists.txt new file mode 100644 index 0000000..d91138c --- /dev/null +++ b/tests/unittest/CMakeLists.txt @@ -0,0 +1,25 @@ +SET(THREAD_UNITTEST "thread-unittest") +LINK_DIRECTORIES(${CMAKE_BINARY_DIR}) + +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -Wall -fPIE") +SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie") + +INCLUDE_DIRECTORIES( + ${CMAKE_SOURCE_DIR}/include + ${${fw_name}_INCLUDE_DIRS} +) + +SET(GTEST gtest) + +FILE(GLOB UNITTEST_SRCS *.cpp mocks/*.cpp ${CMAKE_SOURCE_DIR}/src/*.c) +ADD_EXECUTABLE(${THREAD_UNITTEST} ${UNITTEST_SRCS}) +TARGET_LINK_LIBRARIES(${THREAD_UNITTEST} + ${TARGET_THREAD} + ${${fw_name}_LIBRARIES} + ${GTEST} +) + +SET_TARGET_PROPERTIES(${THREAD_UNITTEST} PROPERTIES + COMPILE_FLAGS "-fPIE") + +INSTALL(TARGETS ${THREAD_UNITTEST} DESTINATION "${BIN_DIR}") diff --git a/tests/unittest/mocks/thread-mock-dbus.cpp b/tests/unittest/mocks/thread-mock-dbus.cpp new file mode 100644 index 0000000..6a15cf1 --- /dev/null +++ b/tests/unittest/mocks/thread-mock-dbus.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2022 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 "thread-mock-dummy.h" + +GDBusConnection *g_bus_get_sync(GBusType bus_type, + GCancellable *cancellable, GError **error) +{ + return (GDBusConnection *)g_object_new(G_TYPE_DBUS_CONNECTION, NULL); +} + +GDBusProxy *g_dbus_proxy_new_for_bus_sync(GBusType bus_type, + GDBusProxyFlags flags, GDBusInterfaceInfo *info, + const gchar *name, const gchar *object_path, + const gchar *interface_name, GCancellable *cancellable, + GError **error) +{ + return (GDBusProxy *)g_object_new(G_TYPE_DBUS_PROXY, NULL); +} + +GDBusProxy *g_dbus_proxy_new_sync(GDBusConnection *connection, + GDBusProxyFlags flags, GDBusInterfaceInfo *info, + const gchar *name, const gchar *object_path, + const gchar *interface_name, GCancellable *cancellable, + GError **error) +{ + return (GDBusProxy *)g_object_new(G_TYPE_DBUS_PROXY, NULL); +} + +guint g_dbus_connection_signal_subscribe(GDBusConnection *connection, + const gchar *sender, + const gchar *interface_name, + const gchar *member, + const gchar *object_path, + const gchar *arg0, + GDBusSignalFlags flags, + GDBusSignalCallback callback, + gpointer user_data, + GDestroyNotify user_data_free_func) +{ + return _subscribe_signal(interface_name, member, callback, user_data); +} + +void g_dbus_connection_signal_unsubscribe(GDBusConnection *connection, + guint subscription_id) +{ +} + +void g_object_unref(gpointer object) +{ +} \ No newline at end of file diff --git a/tests/unittest/mocks/thread-mock-dummy.cpp b/tests/unittest/mocks/thread-mock-dummy.cpp new file mode 100644 index 0000000..726cf3c --- /dev/null +++ b/tests/unittest/mocks/thread-mock-dummy.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2022 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 "thread-mock-dummy.h" + +static GVariant * __properties_changed() +{ + return nullptr; +} + +struct { + const gchar *interface_name; + const gchar *signal_name; + GVariant *(*create_parameters)(); + GDBusSignalCallback callback; + gpointer user_data; +} thread_gdbus_signal_list[] = { + [PROPERTIES_CHANGED] = { + "org.freedesktop.DBus.Properties", + "PropertiesChanged", + __properties_changed, + NULL, + NULL, + }, + { + NULL, + NULL, + NULL, + NULL, + NULL, + }, +}; + +static bool __is_same_signal(int i, const char *interface_name, const char *signal_name) +{ + return (g_strcmp0(thread_gdbus_signal_list[i].interface_name, interface_name) == 0 + && g_strcmp0(thread_gdbus_signal_list[i].signal_name, signal_name) == 0); +} + +guint _subscribe_signal(const char *interface_name, + const char *member, GDBusSignalCallback callback, + gpointer user_data) +{ + for (int i = 0; ; ++i) { + if (thread_gdbus_signal_list[i].interface_name == NULL) + break; + if (__is_same_signal(i, interface_name, member)) { + thread_gdbus_signal_list[i].callback = callback; + thread_gdbus_signal_list[i].user_data = user_data; + } + } + return DUMMY_SIGNAL_ID; +} \ No newline at end of file diff --git a/tests/unittest/mocks/thread-mock-dummy.h b/tests/unittest/mocks/thread-mock-dummy.h new file mode 100644 index 0000000..54df057 --- /dev/null +++ b/tests/unittest/mocks/thread-mock-dummy.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2022 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. + */ + +#pragma once + +#include +#include + +#define DUMMY_SIGNAL_ID 1 + +typedef enum { + PROPERTIES_CHANGED = 0, +} thread_gdbus_signal_e; + +guint _subscribe_signal(const char *interface_name, + const char *member, GDBusSignalCallback callback, + gpointer user_data); diff --git a/tests/unittest/mocks/thread-mock-socket.cpp b/tests/unittest/mocks/thread-mock-socket.cpp new file mode 100644 index 0000000..27fc5da --- /dev/null +++ b/tests/unittest/mocks/thread-mock-socket.cpp @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2022 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 + +#ifdef __cplusplus +extern "C" { +#endif + +// static bool __thread_mock_socket = false; +// extern int __real_socket(int domain, int type, int protocol); + +// void thread_mock_set_socket(bool flag) +// { +// __thread_mock_socket = flag; +// } + +// int __wrap_socket(int domain, int type, int protocol) +// { +// return __thread_mock_socket ? 1 : __real_socket(domain, type, protocol); +// } + +int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) +{ + return 0; +} + +int write(int fd, const void *buf, unsigned int len) +{ + return len; +} + +int select(int nfds, fd_set *readfds, + fd_set *writefds, fd_set *exceptfds, + struct timeval * timeout) +{ + return 1; +} + +ssize_t read(int fd, void *buf, size_t nbytes) +{ + snprintf((char *)buf, nbytes, "Done\n"); + return 5; +} + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/tests/unittest/mocks/thread-mock-socket.h b/tests/unittest/mocks/thread-mock-socket.h new file mode 100644 index 0000000..6705f62 --- /dev/null +++ b/tests/unittest/mocks/thread-mock-socket.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2022 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. + */ + +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +void thread_mock_set_socket(bool flag); + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/tests/unittest/mocks/thread-mock.cpp b/tests/unittest/mocks/thread-mock.cpp new file mode 100644 index 0000000..d4e38e9 --- /dev/null +++ b/tests/unittest/mocks/thread-mock.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2022 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 + +int system_info_get_platform_bool(const char *key, bool *value) +{ + *value = true; + return SYSTEM_INFO_ERROR_NONE; +} \ No newline at end of file diff --git a/tests/unittest/thread-unittest-init.cpp b/tests/unittest/thread-unittest-init.cpp new file mode 100644 index 0000000..7c5aed7 --- /dev/null +++ b/tests/unittest/thread-unittest-init.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2022 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 "thread.h" +#include "mocks/thread-mock-socket.h" + +class ThreadInitTest : public ::testing::Test +{ +protected: + void SetUp() override + { + //thread_mock_set_socket(false); + } + + void TearDown() override + { + //thread_mock_set_socket(false); + } +}; + +TEST_F(ThreadInitTest, InitAlreadyDone) +{ + EXPECT_EQ(THREAD_ERROR_NONE, thread_initialize()); + EXPECT_EQ(THREAD_ERROR_ALREADY_DONE, thread_initialize()); + EXPECT_EQ(THREAD_ERROR_NONE, thread_deinitialize()); +} + +TEST_F(ThreadInitTest, DeinitNotInitialized) +{ + EXPECT_EQ(THREAD_ERROR_NOT_INITIALIZED, thread_deinitialize()); +} + +TEST_F(ThreadInitTest, InitDeinit) +{ + EXPECT_EQ(THREAD_ERROR_NONE, thread_initialize()); + EXPECT_EQ(THREAD_ERROR_NONE, thread_deinitialize()); +} \ No newline at end of file diff --git a/tests/unittest/thread-unittest-main.cpp b/tests/unittest/thread-unittest-main.cpp new file mode 100644 index 0000000..33ab729 --- /dev/null +++ b/tests/unittest/thread-unittest-main.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2022 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 + +int main(int argc, char **argv) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file -- 2.7.4 From f5684f301578302f2a94d6c7fe165a5fcf972780 Mon Sep 17 00:00:00 2001 From: "hyunuk.tak" Date: Mon, 21 Mar 2022 11:48:45 +0900 Subject: [PATCH 09/16] Add missing flags Change-Id: Icaa2d7cb37cdb8c1805cd978b4e5ffda78a29740 Signed-off-by: hyunuk.tak --- CMakeLists.txt | 3 +++ packaging/capi-network-thread.spec | 2 ++ tests/{ => thread-test}/thread-scenarios.c | 0 3 files changed, 5 insertions(+) rename tests/{ => thread-test}/thread-scenarios.c (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 59a94cf..77d991d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -75,6 +75,9 @@ CONFIGURE_FILE( INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${fw_name}.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) ADD_SUBDIRECTORY(tests/thread-test) +IF(UNITTEST) +ADD_SUBDIRECTORY(tests/unittest) +ENDIF(UNITTEST) IF(UNIX) diff --git a/packaging/capi-network-thread.spec b/packaging/capi-network-thread.spec index 5f79260..9db4f3e 100644 --- a/packaging/capi-network-thread.spec +++ b/packaging/capi-network-thread.spec @@ -135,7 +135,9 @@ genhtml %{name}.info -o out --legend --show-details %files test %manifest %{name}.manifest %attr(777,network_fw,network_fw) %{_bindir}/thread-test +%if 0%{?gcov:1} %attr(777,network_fw,network_fw) %{_bindir}/thread-unittest +%endif %if 0%{?gcov:1} %files gcov diff --git a/tests/thread-scenarios.c b/tests/thread-test/thread-scenarios.c similarity index 100% rename from tests/thread-scenarios.c rename to tests/thread-test/thread-scenarios.c -- 2.7.4 From f6ce308ef9b8a97aab9b353bcb826f72537227a4 Mon Sep 17 00:00:00 2001 From: "hyunuk.tak" Date: Mon, 21 Mar 2022 12:22:33 +0900 Subject: [PATCH 10/16] Remove unnecessary mock code for socket Change-Id: I561e26409c4d8475911fc1d8301c68a711eea6b4 Signed-off-by: hyunuk.tak --- tests/unittest/mocks/thread-mock-socket.cpp | 13 ------------- tests/unittest/mocks/thread-mock-socket.h | 29 ----------------------------- tests/unittest/thread-unittest-init.cpp | 1 - 3 files changed, 43 deletions(-) delete mode 100644 tests/unittest/mocks/thread-mock-socket.h diff --git a/tests/unittest/mocks/thread-mock-socket.cpp b/tests/unittest/mocks/thread-mock-socket.cpp index 27fc5da..4cd99ff 100644 --- a/tests/unittest/mocks/thread-mock-socket.cpp +++ b/tests/unittest/mocks/thread-mock-socket.cpp @@ -21,19 +21,6 @@ extern "C" { #endif -// static bool __thread_mock_socket = false; -// extern int __real_socket(int domain, int type, int protocol); - -// void thread_mock_set_socket(bool flag) -// { -// __thread_mock_socket = flag; -// } - -// int __wrap_socket(int domain, int type, int protocol) -// { -// return __thread_mock_socket ? 1 : __real_socket(domain, type, protocol); -// } - int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { return 0; diff --git a/tests/unittest/mocks/thread-mock-socket.h b/tests/unittest/mocks/thread-mock-socket.h deleted file mode 100644 index 6705f62..0000000 --- a/tests/unittest/mocks/thread-mock-socket.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2022 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. - */ - -#pragma once - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -void thread_mock_set_socket(bool flag); - -#ifdef __cplusplus -} -#endif \ No newline at end of file diff --git a/tests/unittest/thread-unittest-init.cpp b/tests/unittest/thread-unittest-init.cpp index 7c5aed7..97afd45 100644 --- a/tests/unittest/thread-unittest-init.cpp +++ b/tests/unittest/thread-unittest-init.cpp @@ -17,7 +17,6 @@ #include #include "thread.h" -#include "mocks/thread-mock-socket.h" class ThreadInitTest : public ::testing::Test { -- 2.7.4 From 3d67764b0451d773f3255bed6ec6f15f3b1ef6b0 Mon Sep 17 00:00:00 2001 From: "hyunuk.tak" Date: Mon, 21 Mar 2022 13:45:01 +0900 Subject: [PATCH 11/16] Replace g_objext_new with dummy value Change-Id: I8639e8695cba470a166fc363f4c1176e786023a5 Signed-off-by: hyunuk.tak --- tests/unittest/mocks/thread-mock-dbus.cpp | 6 +++--- tests/unittest/thread-unittest-init.cpp | 2 -- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/tests/unittest/mocks/thread-mock-dbus.cpp b/tests/unittest/mocks/thread-mock-dbus.cpp index 6a15cf1..bed2d02 100644 --- a/tests/unittest/mocks/thread-mock-dbus.cpp +++ b/tests/unittest/mocks/thread-mock-dbus.cpp @@ -21,7 +21,7 @@ GDBusConnection *g_bus_get_sync(GBusType bus_type, GCancellable *cancellable, GError **error) { - return (GDBusConnection *)g_object_new(G_TYPE_DBUS_CONNECTION, NULL); + return (GDBusConnection *)0x1234; } GDBusProxy *g_dbus_proxy_new_for_bus_sync(GBusType bus_type, @@ -30,7 +30,7 @@ GDBusProxy *g_dbus_proxy_new_for_bus_sync(GBusType bus_type, const gchar *interface_name, GCancellable *cancellable, GError **error) { - return (GDBusProxy *)g_object_new(G_TYPE_DBUS_PROXY, NULL); + return (GDBusProxy *)0x1234; } GDBusProxy *g_dbus_proxy_new_sync(GDBusConnection *connection, @@ -39,7 +39,7 @@ GDBusProxy *g_dbus_proxy_new_sync(GDBusConnection *connection, const gchar *interface_name, GCancellable *cancellable, GError **error) { - return (GDBusProxy *)g_object_new(G_TYPE_DBUS_PROXY, NULL); + return (GDBusProxy *)0x1234; } guint g_dbus_connection_signal_subscribe(GDBusConnection *connection, diff --git a/tests/unittest/thread-unittest-init.cpp b/tests/unittest/thread-unittest-init.cpp index 97afd45..4c18d39 100644 --- a/tests/unittest/thread-unittest-init.cpp +++ b/tests/unittest/thread-unittest-init.cpp @@ -23,12 +23,10 @@ class ThreadInitTest : public ::testing::Test protected: void SetUp() override { - //thread_mock_set_socket(false); } void TearDown() override { - //thread_mock_set_socket(false); } }; -- 2.7.4 From 93aa17a88f951f129b6995aed8eaf9b4bd31fddb Mon Sep 17 00:00:00 2001 From: "hyunuk.tak" Date: Mon, 21 Mar 2022 15:28:54 +0900 Subject: [PATCH 12/16] Add defense code for null param Change-Id: I1b2d4584862f88c50295fac18c6c996b24491958 Signed-off-by: hyunuk.tak --- tests/unittest/mocks/thread-mock.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/unittest/mocks/thread-mock.cpp b/tests/unittest/mocks/thread-mock.cpp index d4e38e9..9eb2b74 100644 --- a/tests/unittest/mocks/thread-mock.cpp +++ b/tests/unittest/mocks/thread-mock.cpp @@ -18,6 +18,9 @@ int system_info_get_platform_bool(const char *key, bool *value) { - *value = true; - return SYSTEM_INFO_ERROR_NONE; + if (value == nullptr) + return SYSTEM_INFO_ERROR_INVALID_PARAMETER; + + *value = true; + return SYSTEM_INFO_ERROR_NONE; } \ No newline at end of file -- 2.7.4 From 7222f0cd600c89af1efb0438efba41c87a157732 Mon Sep 17 00:00:00 2001 From: "hyunuk.tak" Date: Tue, 22 Mar 2022 12:50:27 +0900 Subject: [PATCH 13/16] Add core test case for enable Change-Id: I33f535aa291eafc018626e42c140ff4070a1f2d1 Signed-off-by: hyunuk.tak --- tests/unittest/thread-unittest-core.cpp | 48 +++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/unittest/thread-unittest-core.cpp diff --git a/tests/unittest/thread-unittest-core.cpp b/tests/unittest/thread-unittest-core.cpp new file mode 100644 index 0000000..e273faa --- /dev/null +++ b/tests/unittest/thread-unittest-core.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2022 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 "thread.h" + +class ThreadCoreTest : public ::testing::Test +{ +public: + thread_instance_h instance; + +protected: + void SetUp() override + { + thread_initialize(); + instance = nullptr; + } + + void TearDown() override + { + thread_deinitialize(); + } +}; + +TEST_F(ThreadCoreTest, EnableNotInitialized) +{ + EXPECT_EQ(THREAD_ERROR_NONE, thread_deinitialize()); + EXPECT_EQ(THREAD_ERROR_NOT_INITIALIZED, thread_enable(&instance)); +} + +TEST_F(ThreadCoreTest, EnableErrorNone) +{ + EXPECT_EQ(THREAD_ERROR_NONE, thread_enable(&instance)); +} \ No newline at end of file -- 2.7.4 From 0dc9c92dedb727eed6f87a31675f3c89962a129c Mon Sep 17 00:00:00 2001 From: "hyunuk.tak" Date: Tue, 22 Mar 2022 12:51:12 +0900 Subject: [PATCH 14/16] Add core test case for disable Change-Id: I0941fc6d74a80041f4141d5c108ac679289b97e1 Signed-off-by: hyunuk.tak --- tests/unittest/thread-unittest-core.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/unittest/thread-unittest-core.cpp b/tests/unittest/thread-unittest-core.cpp index e273faa..25da988 100644 --- a/tests/unittest/thread-unittest-core.cpp +++ b/tests/unittest/thread-unittest-core.cpp @@ -45,4 +45,21 @@ TEST_F(ThreadCoreTest, EnableNotInitialized) TEST_F(ThreadCoreTest, EnableErrorNone) { EXPECT_EQ(THREAD_ERROR_NONE, thread_enable(&instance)); +} + +TEST_F(ThreadCoreTest, DisableNotInitialized) +{ + EXPECT_EQ(THREAD_ERROR_NONE, thread_deinitialize()); + EXPECT_EQ(THREAD_ERROR_NOT_INITIALIZED, thread_disable(instance)); +} + +TEST_F(ThreadCoreTest, DisableInvalidParameter) +{ + EXPECT_EQ(THREAD_ERROR_INVALID_PARAMETER, thread_disable(instance)); +} + +TEST_F(ThreadCoreTest, DisableErrorNone) +{ + EXPECT_EQ(THREAD_ERROR_NONE, thread_enable(&instance)); + EXPECT_EQ(THREAD_ERROR_NONE, thread_disable(instance)); } \ No newline at end of file -- 2.7.4 From 1065d629f59e0517db22b0b5141d5835084979f5 Mon Sep 17 00:00:00 2001 From: "hyunuk.tak" Date: Tue, 22 Mar 2022 12:51:40 +0900 Subject: [PATCH 15/16] Add core test case for start Change-Id: I2e4ca31ddbbb4450cfd31f672a9857a8df034e0f Signed-off-by: hyunuk.tak --- tests/unittest/thread-unittest-core.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/unittest/thread-unittest-core.cpp b/tests/unittest/thread-unittest-core.cpp index 25da988..754c99c 100644 --- a/tests/unittest/thread-unittest-core.cpp +++ b/tests/unittest/thread-unittest-core.cpp @@ -62,4 +62,21 @@ TEST_F(ThreadCoreTest, DisableErrorNone) { EXPECT_EQ(THREAD_ERROR_NONE, thread_enable(&instance)); EXPECT_EQ(THREAD_ERROR_NONE, thread_disable(instance)); +} + +TEST_F(ThreadCoreTest, StartNotInitialized) +{ + EXPECT_EQ(THREAD_ERROR_NONE, thread_deinitialize()); + EXPECT_EQ(THREAD_ERROR_NOT_INITIALIZED, thread_start(instance)); +} + +TEST_F(ThreadCoreTest, StartInvalidParameter) +{ + EXPECT_EQ(THREAD_ERROR_INVALID_PARAMETER, thread_start(instance)); +} + +TEST_F(ThreadCoreTest, StartErrorNone) +{ + EXPECT_EQ(THREAD_ERROR_NONE, thread_enable(&instance)); + EXPECT_EQ(THREAD_ERROR_NONE, thread_start(instance)); } \ No newline at end of file -- 2.7.4 From 57c0765e59fed90bd5fff79aee620ad7e5d0d840 Mon Sep 17 00:00:00 2001 From: "hyunuk.tak" Date: Tue, 22 Mar 2022 12:52:06 +0900 Subject: [PATCH 16/16] Add core test case for stop Change-Id: I7d747ae3af88e4231fff816c93072d3c5f305a94 Signed-off-by: hyunuk.tak --- tests/unittest/thread-unittest-core.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/unittest/thread-unittest-core.cpp b/tests/unittest/thread-unittest-core.cpp index 754c99c..b9e9908 100644 --- a/tests/unittest/thread-unittest-core.cpp +++ b/tests/unittest/thread-unittest-core.cpp @@ -79,4 +79,22 @@ TEST_F(ThreadCoreTest, StartErrorNone) { EXPECT_EQ(THREAD_ERROR_NONE, thread_enable(&instance)); EXPECT_EQ(THREAD_ERROR_NONE, thread_start(instance)); +} + +TEST_F(ThreadCoreTest, StopNotInitialized) +{ + EXPECT_EQ(THREAD_ERROR_NONE, thread_deinitialize()); + EXPECT_EQ(THREAD_ERROR_NOT_INITIALIZED, thread_stop(instance)); +} + +TEST_F(ThreadCoreTest, StopInvalidParameter) +{ + EXPECT_EQ(THREAD_ERROR_INVALID_PARAMETER, thread_stop(instance)); +} + +TEST_F(ThreadCoreTest, StopErrorNone) +{ + EXPECT_EQ(THREAD_ERROR_NONE, thread_enable(&instance)); + EXPECT_EQ(THREAD_ERROR_NONE, thread_start(instance)); + EXPECT_EQ(THREAD_ERROR_NONE, thread_stop(instance)); } \ No newline at end of file -- 2.7.4