From fe7c2721fc2e3ac0d9b2f8dc608bf9ebb9834488 Mon Sep 17 00:00:00 2001 From: liangqi Date: Sun, 11 Dec 2022 16:30:57 +0800 Subject: [PATCH] add heartbeat UT Signed-off-by: liangqi --- .../heartbeat/src/lnn_heartbeat_fsm.c | 13 +- tests/core/authentication/BUILD.gn | 4 + tests/core/bus_center/BUILD.gn | 5 +- .../include/distribute_net_ledger_mock.h | 48 ++ .../mock_common/include/lnn_connection_mock.h | 3 + .../src/distribute_net_ledger_mock.cpp | 64 ++ .../mock_common/src/lnn_connection_mock.cpp | 4 + tests/core/bus_center/test/BUILD.gn | 147 +++++ .../bus_center/test/heartbeat/HB_fsm_test.cpp | 523 ++++++++++++++++ .../test/heartbeat/HB_medium_mgr_test.cpp | 571 ++++++++++++++++++ .../test/mock/include/hb_fsm_mock.h | 82 +++ .../test/mock/include/hb_strategy_mock.h | 51 ++ .../bus_center/test/mock/src/hb_fsm_mock.cpp | 126 ++++ .../test/mock/src/hb_strategy_mock.cpp | 70 +++ 14 files changed, 1704 insertions(+), 7 deletions(-) create mode 100644 tests/core/bus_center/mock_common/include/distribute_net_ledger_mock.h create mode 100644 tests/core/bus_center/mock_common/src/distribute_net_ledger_mock.cpp create mode 100644 tests/core/bus_center/test/BUILD.gn create mode 100644 tests/core/bus_center/test/heartbeat/HB_fsm_test.cpp create mode 100644 tests/core/bus_center/test/heartbeat/HB_medium_mgr_test.cpp create mode 100644 tests/core/bus_center/test/mock/include/hb_fsm_mock.h create mode 100644 tests/core/bus_center/test/mock/include/hb_strategy_mock.h create mode 100644 tests/core/bus_center/test/mock/src/hb_fsm_mock.cpp create mode 100644 tests/core/bus_center/test/mock/src/hb_strategy_mock.cpp diff --git a/core/bus_center/lnn/lane_hub/heartbeat/src/lnn_heartbeat_fsm.c b/core/bus_center/lnn/lane_hub/heartbeat/src/lnn_heartbeat_fsm.c index 49fdeb2729..0e87b5e760 100644 --- a/core/bus_center/lnn/lane_hub/heartbeat/src/lnn_heartbeat_fsm.c +++ b/core/bus_center/lnn/lane_hub/heartbeat/src/lnn_heartbeat_fsm.c @@ -586,8 +586,8 @@ static int32_t OnUpdateSendInfo(FsmStateMachine *fsm, int32_t msgType, void *par static void TryAsMasterNodeNextLoop(FsmStateMachine *fsm) { uint64_t delayMillis; - GearMode mode = {0}; - + GearMode mode; + (void)memset_s(&mode, sizeof(GearMode), 0, sizeof(GearMode)); if (LnnGetGearModeBySpecificType(&mode, HEARTBEAT_TYPE_BLE_V1) != SOFTBUS_OK) { SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB try as master node get gearmode fail"); return; @@ -672,7 +672,8 @@ static int32_t ProcessLostHeartbeat(const char *networkId, ConnectionAddrType ad static bool IsTimestampExceedLimit(uint64_t nowTime, uint64_t oldTimeStamp, LnnHeartbeatType hbType) { - GearMode mode = {0}; + GearMode mode; + (void)memset_s(&mode, sizeof(GearMode), 0, sizeof(GearMode)); uint64_t offlineToleranceLen; switch (hbType) { @@ -834,7 +835,7 @@ NO_SANITIZE("cfi") LnnHeartbeatFsm *LnnCreateHeartbeatFsm(void) { LnnHeartbeatFsm *hbFsm = NULL; - hbFsm = SoftBusCalloc(sizeof(LnnHeartbeatFsm)); + hbFsm = (LnnHeartbeatFsm *)SoftBusCalloc(sizeof(LnnHeartbeatFsm)); if (hbFsm == NULL) { SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB malloc fsm fail"); return NULL; @@ -902,7 +903,7 @@ NO_SANITIZE("cfi") int32_t LnnPostNextSendOnceMsgToHbFsm(LnnHeartbeatFsm *hbFsm, static int32_t CreateNewHbTypeObjMsg(LnnHeartbeatType srcType, LnnHeartbeatType **dstType) { - *dstType = SoftBusCalloc(sizeof(LnnHeartbeatType)); + *dstType = (LnnHeartbeatType *)SoftBusCalloc(sizeof(LnnHeartbeatType)); if (*dstType == NULL) { SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB new hbType obj msg malloc err"); return SOFTBUS_MALLOC_ERR; @@ -920,7 +921,7 @@ NO_SANITIZE("cfi") int32_t LnnPostSendBeginMsgToHbFsm(LnnHeartbeatFsm *hbFsm, Ln SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB post send begin msg get invalid param"); return SOFTBUS_INVALID_PARAM; } - custData = SoftBusCalloc(sizeof(LnnHeartbeatCustSendData)); + custData = (LnnHeartbeatCustSendData *)SoftBusCalloc(sizeof(LnnHeartbeatCustSendData)); if (custData == NULL) { SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB new hbType obj msg malloc err"); return SOFTBUS_MALLOC_ERR; diff --git a/tests/core/authentication/BUILD.gn b/tests/core/authentication/BUILD.gn index d7cfdfc6be..9c8658dd8e 100644 --- a/tests/core/authentication/BUILD.gn +++ b/tests/core/authentication/BUILD.gn @@ -107,6 +107,8 @@ ohos_unittest("AuthTestMock") { "$dsoftbus_root_path/core/common/include", "$dsoftbus_root_path/core/common/message_handler/include", "$dsoftbus_root_path/core/connection/interface", + "$dsoftbus_root_path/core/connection/p2p/interface", + "$dsoftbus_root_path/core/connection/p2p/common/include", "$dsoftbus_root_path/core/frame/$os_type/init/include", "$dsoftbus_root_path/core/frame/common/include", "$dsoftbus_root_path/core/discovery/manager/include", @@ -187,6 +189,8 @@ ohos_unittest("AuthTestCallBack") { "$dsoftbus_root_path/core/common/include", "$dsoftbus_root_path/core/common/message_handler/include", "$dsoftbus_root_path/core/connection/interface", + "$dsoftbus_root_path/core/connection/p2p/interface", + "$dsoftbus_root_path/core/connection/p2p/common/include", "$dsoftbus_root_path/core/frame/$os_type/init/include", "$dsoftbus_root_path/core/frame/common/include", "$dsoftbus_root_path/core/discovery/manager/include", diff --git a/tests/core/bus_center/BUILD.gn b/tests/core/bus_center/BUILD.gn index b73e3a9778..7ed4d660b0 100644 --- a/tests/core/bus_center/BUILD.gn +++ b/tests/core/bus_center/BUILD.gn @@ -16,5 +16,8 @@ import("//foundation/communication/dsoftbus/dsoftbus.gni") group("unittest") { testonly = true - deps = [ "lnn:unittest" ] + deps = [ + "lnn:unittest", + "test:unittest", + ] } diff --git a/tests/core/bus_center/mock_common/include/distribute_net_ledger_mock.h b/tests/core/bus_center/mock_common/include/distribute_net_ledger_mock.h new file mode 100644 index 0000000000..51b8c4160d --- /dev/null +++ b/tests/core/bus_center/mock_common/include/distribute_net_ledger_mock.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2022 Huawei Device 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. + */ + +#ifndef DISTRIBUTE_NET_LEDGER_MOCK_H +#define DISTRIBUTE_NET_LEDGER_MOCK_H + +#include "lnn_distributed_net_ledger.h" +#include "bus_center_manager.h" +#include +#include + +namespace OHOS { +class DistributeLedgerInterface { +public: + DistributeLedgerInterface() {}; + virtual ~DistributeLedgerInterface() {}; + + virtual int32_t LnnGetDLHeartbeatTimestamp(const char *networkId, uint64_t *timestamp) = 0; + virtual int32_t LnnSetDLHeartbeatTimestamp(const char *networkId, const uint64_t timestamp) = 0; + virtual int32_t LnnGetRemoteStrInfo(const char *netWorkId, InfoKey key, char *info, uint32_t len) = 0; + virtual bool LnnGetOnlineStateById(const char *id, IdCategory type) = 0; + virtual const char *LnnConvertDLidToUdid(const char *id, IdCategory type) = 0; +}; +class DistributeLedgerInterfaceMock : public DistributeLedgerInterface { +public: + DistributeLedgerInterfaceMock(); + ~DistributeLedgerInterfaceMock() override; + + MOCK_METHOD2(LnnGetDLHeartbeatTimestamp, int32_t(const char *, uint64_t *)); + MOCK_METHOD2(LnnSetDLHeartbeatTimestamp, int32_t(const char *, const uint64_t)); + MOCK_METHOD4(LnnGetRemoteStrInfo, int32_t(const char *, InfoKey, char *, uint32_t)); + MOCK_METHOD2(LnnGetOnlineStateById, bool(const char *, IdCategory)); + MOCK_METHOD2(LnnConvertDLidToUdid, const char *(const char *, IdCategory)); +}; +} // namespace OHOS +#endif // AUTH_CONNECTION_MOCK_H \ No newline at end of file diff --git a/tests/core/bus_center/mock_common/include/lnn_connection_mock.h b/tests/core/bus_center/mock_common/include/lnn_connection_mock.h index a132015bf9..ca124dac22 100644 --- a/tests/core/bus_center/mock_common/include/lnn_connection_mock.h +++ b/tests/core/bus_center/mock_common/include/lnn_connection_mock.h @@ -24,6 +24,7 @@ #include "softbus_adapter_mem.h" #include "softbus_conn_interface.h" #include "softbus_log.h" +#include "p2plink_interface.h" #include #include @@ -45,6 +46,7 @@ public: virtual int32_t ConnStopLocalListening(const LocalListenerInfo *info) = 0; virtual uint32_t ConnGetNewRequestId(ConnModule moduleId) = 0; virtual void DiscDeviceInfoChanged(InfoTypeChanged type) = 0; + virtual int32_t P2pLinkQueryDevIsOnline(const char *peerMac) = 0; }; class LnnConnectInterfaceMock : public LnnConnectInterface { public: @@ -62,6 +64,7 @@ public: MOCK_METHOD1(ConnStopLocalListening, int32_t(const LocalListenerInfo *)); MOCK_METHOD1(ConnGetNewRequestId, uint32_t(ConnModule)); MOCK_METHOD1(DiscDeviceInfoChanged, void(InfoTypeChanged)); + MOCK_METHOD1(P2pLinkQueryDevIsOnline, int32_t(const char *)); static inline char *g_encryptData; static inline ConnectCallback g_conncallback; static inline ConnectResult g_connresultcb; diff --git a/tests/core/bus_center/mock_common/src/distribute_net_ledger_mock.cpp b/tests/core/bus_center/mock_common/src/distribute_net_ledger_mock.cpp new file mode 100644 index 0000000000..2253f843ea --- /dev/null +++ b/tests/core/bus_center/mock_common/src/distribute_net_ledger_mock.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2022 Huawei Device 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 "distribute_net_ledger_mock.h" +#include "softbus_error_code.h" + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +void *g_distriLedgerInterface; + +DistributeLedgerInterfaceMock::DistributeLedgerInterfaceMock() +{ + g_distriLedgerInterface = reinterpret_cast(this); +} + +DistributeLedgerInterfaceMock::~DistributeLedgerInterfaceMock() +{ + g_distriLedgerInterface = nullptr; +} + +static DistributeLedgerInterface *GetDistriLedgerInterface() +{ + return reinterpret_cast(g_distriLedgerInterface); +} + +extern "C" { +int32_t LnnGetDLHeartbeatTimestamp(const char *networkId, uint64_t *timestamp) +{ + return GetDistriLedgerInterface()->LnnGetDLHeartbeatTimestamp(networkId, timestamp); +} + +int32_t LnnSetDLHeartbeatTimestamp(const char *networkId, const uint64_t timestamp) +{ + return GetDistriLedgerInterface()->LnnSetDLHeartbeatTimestamp(networkId, timestamp); +} + +int32_t LnnGetRemoteStrInfo(const char *netWorkId, InfoKey key, char *info, uint32_t len) +{ + return GetDistriLedgerInterface()->LnnGetRemoteStrInfo(netWorkId, key, info, len); +} +bool LnnGetOnlineStateById(const char *id, IdCategory type) +{ + return GetDistriLedgerInterface()->LnnGetOnlineStateById(id, type); +} +const char *LnnConvertDLidToUdid(const char *id, IdCategory type) +{ + return GetDistriLedgerInterface()->LnnConvertDLidToUdid(id, type); +} +} +} // namespace OHOS \ No newline at end of file diff --git a/tests/core/bus_center/mock_common/src/lnn_connection_mock.cpp b/tests/core/bus_center/mock_common/src/lnn_connection_mock.cpp index 4e7b32639e..aa2830d25f 100644 --- a/tests/core/bus_center/mock_common/src/lnn_connection_mock.cpp +++ b/tests/core/bus_center/mock_common/src/lnn_connection_mock.cpp @@ -96,6 +96,10 @@ void DiscDeviceInfoChanged(InfoTypeChanged type) { return GetConnInterface()->DiscDeviceInfoChanged(type); } +int32_t P2pLinkQueryDevIsOnline(const char *peerMac) +{ + return GetConnInterface()->P2pLinkQueryDevIsOnline(peerMac); +} } int32_t LnnConnectInterfaceMock::ActionofConnSetConnectCallback(ConnModule moduleId, const ConnectCallback *callback) diff --git a/tests/core/bus_center/test/BUILD.gn b/tests/core/bus_center/test/BUILD.gn new file mode 100644 index 0000000000..7c299f44ad --- /dev/null +++ b/tests/core/bus_center/test/BUILD.gn @@ -0,0 +1,147 @@ +# Copyright (c) 2022 Huawei Device 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. + +import("//build/test.gni") +import("//foundation/communication/dsoftbus/dsoftbus.gni") + +module_output_path = "dsoftbus/test" + +ohos_unittest("HeartBeatMediumTest") { + module_out_path = module_output_path + sources = [ + "$dsoftbus_root_path/core/bus_center/lnn/lane_hub/heartbeat/src/lnn_ble_heartbeat_virtual.c", + "$dsoftbus_root_path/core/bus_center/lnn/lane_hub/heartbeat/src/lnn_heartbeat_utils.c", + "$dsoftbus_root_path/core/bus_center/utils/src/lnn_connection_addr_utils.c", + "$dsoftbus_root_path/tests/core/bus_center/lnn/net_builder/src/lnn_net_ledger_mock.cpp", + "$dsoftbus_root_path/tests/core/bus_center/mock_common/src/distribute_net_ledger_mock.cpp", + "$dsoftbus_root_path/tests/core/bus_center/test/heartbeat/HB_medium_mgr_test.cpp", + "$dsoftbus_root_path/tests/core/bus_center/test/mock/src/hb_strategy_mock.cpp", + ] + + include_dirs = [ + "$dsoftbus_root_path/tests/core/bus_center/test/mock/include", + "$dsoftbus_root_path/tests/core/bus_center/mock_common/include", + "$dsoftbus_root_path/core/connection/p2p/common/include", + "$dsoftbus_root_path/core/connection/p2p/interface", + "$dsoftbus_root_path/core/bus_center/service/include", + "$dsoftbus_root_path/tests/core/bus_center/lnn/net_builder/include", + "$dsoftbus_root_path/core/authentication/include", + "$dsoftbus_root_path/core/authentication/interface", + "$dsoftbus_root_path/core/discovery/interface", + "$dsoftbus_root_path/core/discovery/manager/include", + "$dsoftbus_root_path/core/core/discovery/manager/include", + "$dsoftbus_root_path/core/bus_center/lnn/lane_hub/heartbeat/src", + "$dsoftbus_root_path/core/bus_center/interface", + "$dsoftbus_root_path/core/common/include", + "$dsoftbus_root_path/core/bus_center/lnn/lane_hub/heartbeat/include", + "$dsoftbus_root_path/core/bus_center/utils/include", + "$dsoftbus_root_path/core/bus_center/lnn/net_ledger/common/include", + "$dsoftbus_root_path/core/bus_center/lnn/net_ledger/distributed_ledger/include", + "$dsoftbus_root_path/core/bus_center/lnn/net_builder/include", + "$dsoftbus_root_path/core/adapter/bus_center/include", + "$dsoftbus_root_path/adapter/common/include", + "$dsoftbus_root_path/core/common/include", + ] + + deps = [ + "$dsoftbus_root_path/adapter:softbus_adapter", + "$dsoftbus_root_path/core/common:softbus_utils", + "//base/security/device_auth/services:deviceauth_sdk", + "//foundation/communication/ipc/interfaces/innerkits/ipc_core:ipc_core", + "//third_party/googletest:gmock", + "//third_party/googletest:gtest_main", + ] + + if (is_standard_system) { + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + ] + } else { + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + "huks:libhukssdk", + ] + } +} + +ohos_unittest("HeartFsmTest") { + module_out_path = module_output_path + sources = [ + "$dsoftbus_root_path/core/bus_center/lnn/lane_hub/heartbeat/src/lnn_ble_heartbeat_virtual.c", + "$dsoftbus_root_path/core/bus_center/lnn/lane_hub/heartbeat/src/lnn_heartbeat_utils.c", + "$dsoftbus_root_path/core/bus_center/utils/src/lnn_state_machine.c", + "$dsoftbus_root_path/tests/core/bus_center/lnn/net_builder/src/lnn_net_ledger_mock.cpp", + "$dsoftbus_root_path/tests/core/bus_center/mock_common/src/distribute_net_ledger_mock.cpp", + "$dsoftbus_root_path/tests/core/bus_center/mock_common/src/lnn_connection_mock.cpp", + "$dsoftbus_root_path/tests/core/bus_center/test/heartbeat/HB_fsm_test.cpp", + "$dsoftbus_root_path/tests/core/bus_center/test/mock/src/hb_fsm_mock.cpp", + ] + + include_dirs = [ + "$dsoftbus_root_path/core/connection/interface", + "$dsoftbus_root_path/tests/core/bus_center/test/mock/include", + "$dsoftbus_root_path/tests/core/bus_center/mock_common/include", + "$dsoftbus_root_path/core/connection/p2p/common/include", + "$dsoftbus_root_path/core/connection/p2p/interface", + "$dsoftbus_root_path/core/bus_center/service/include", + "$dsoftbus_root_path/tests/core/bus_center/lnn/net_builder/include", + "$dsoftbus_root_path/core/authentication/include", + "$dsoftbus_root_path/core/authentication/interface", + "$dsoftbus_root_path/core/discovery/interface", + "$dsoftbus_root_path/core/discovery/manager/include", + "$dsoftbus_root_path/core/core/discovery/manager/include", + "$dsoftbus_root_path/core/bus_center/lnn/lane_hub/heartbeat/src", + "$dsoftbus_root_path/core/bus_center/interface", + "$dsoftbus_root_path/core/common/include", + "$dsoftbus_root_path/core/bus_center/lnn/lane_hub/heartbeat/include", + "$dsoftbus_root_path/core/bus_center/utils/include", + "$dsoftbus_root_path/core/bus_center/lnn/net_ledger/common/include", + "$dsoftbus_root_path/core/bus_center/lnn/net_ledger/distributed_ledger/include", + "$dsoftbus_root_path/core/bus_center/lnn/net_builder/include", + "$dsoftbus_root_path/core/adapter/bus_center/include", + "$dsoftbus_root_path/adapter/common/include", + "$dsoftbus_root_path/core/common/include", + ] + + deps = [ + "$dsoftbus_root_path/adapter:softbus_adapter", + "$dsoftbus_root_path/core/common:softbus_utils", + "//base/security/device_auth/services:deviceauth_sdk", + "//foundation/communication/ipc/interfaces/innerkits/ipc_core:ipc_core", + "//third_party/googletest:gmock", + "//third_party/googletest:gtest_main", + ] + + if (is_standard_system) { + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + ] + } else { + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + "huks:libhukssdk", + ] + } +} + +group("unittest") { + testonly = true + deps = [ + ":HeartBeatMediumTest", + ":HeartFsmTest", + ] +} diff --git a/tests/core/bus_center/test/heartbeat/HB_fsm_test.cpp b/tests/core/bus_center/test/heartbeat/HB_fsm_test.cpp new file mode 100644 index 0000000000..caae05fe34 --- /dev/null +++ b/tests/core/bus_center/test/heartbeat/HB_fsm_test.cpp @@ -0,0 +1,523 @@ +/* + * Copyright (c) 2022 Huawei Device 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 +#include + +#include "bus_center_manager.h" +#include "distribute_net_ledger_mock.h" +#include "hb_fsm_mock.h" +#include "hb_strategy_mock.h" +#include "lnn_ble_heartbeat.h" +#include "lnn_connection_mock.h" +#include "lnn_heartbeat_fsm.c" +#include "lnn_heartbeat_utils.h" +#include "lnn_net_ledger_mock.h" +#include "lnn_state_machine.h" +#include "message_handler.h" +#include "softbus_common.h" + +namespace OHOS { +#define TEST_NETWORK_ID "6542316a57d" +#define TEST_NETWORK_ID2 "654231655557d" +#define TEST_UDID "1111222233334444" +#define TEST_DISC_TYPE 5321 +#define TEST_ARGS 22 +#define TEST_TIME1 450 +#define TEST_TIME2 500 +#define TEST_TIME3 2000000 +using namespace testing::ext; +using namespace testing; + +class HeartBeatFSMTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void HeartBeatFSMTest::SetUpTestCase() +{ + int32_t ret = LooperInit(); + EXPECT_TRUE(ret == SOFTBUS_OK); +} + +void HeartBeatFSMTest::TearDownTestCase() { } + +void HeartBeatFSMTest::SetUp() { } + +void HeartBeatFSMTest::TearDown() { } + +/* + * @tc.name: CheckHbFsmStateMsgArgs + * @tc.desc: check heartbeat fsm state message + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatFSMTest, CheckHbFsmStateMsgArgsTest_01, TestSize.Level1) +{ + LnnHeartbeatFsm *hbFsm = LnnCreateHeartbeatFsm(); + bool ret = CheckHbFsmStateMsgArgs(nullptr); + EXPECT_FALSE(ret); + ret = CheckHbFsmStateMsgArgs(const_cast(&hbFsm->fsm)); + EXPECT_TRUE(ret); + hbFsm->state = STATE_HB_INDEX_MAX; + ret = CheckHbFsmStateMsgArgs(const_cast(&hbFsm->fsm)); + EXPECT_FALSE(ret); + LnnDestroyHeartbeatFsm(hbFsm); +} + +/* + * @tc.name: CheckRemoveHbMsgParams + * @tc.desc: check heartbeat fsm state message + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatFSMTest, CheckRemoveHbMsgParamsTest_01, TestSize.Level1) +{ + SoftBusMessage ctrlMsgObj = { + .what = TEST_ARGS, + }; + SoftBusMessage delMsg = { + .what = TEST_ARGS, + }; + bool ret = CheckRemoveHbMsgParams(const_cast(&ctrlMsgObj), nullptr); + EXPECT_FALSE(ret); + ret = CheckRemoveHbMsgParams(const_cast(&ctrlMsgObj), reinterpret_cast(&delMsg)); + EXPECT_FALSE(ret); +} + +/* + * @tc.name: LnnRemoveSendEndMsg + * @tc.desc: check heartbeat fsm state message + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatFSMTest, LnnRemoveSendEndMsgTest_01, TestSize.Level1) +{ + bool isRemoved = true; + NiceMock heartbeatFsmMock; + LnnRemoveSendEndMsg(nullptr, HEARTBEAT_TYPE_BLE_V1, &isRemoved); + LnnRemoveCheckDevStatusMsg(nullptr, nullptr); + LnnRemoveProcessSendOnceMsg(nullptr, HEARTBEAT_TYPE_BLE_V1, STRATEGY_HB_SEND_ADJUSTABLE_PERIOD); + HbMasterNodeStateEnter(nullptr); + HbMasterNodeStateExit(nullptr); + LnnHeartbeatFsm *hbFsm = LnnCreateHeartbeatFsm(); + EXPECT_TRUE(hbFsm != nullptr); + HbMasterNodeStateExit(&hbFsm->fsm); + HbNormalNodeStateEnter(nullptr); + EXPECT_CALL(heartbeatFsmMock, LnnHbMediumMgrStop).WillRepeatedly(Return(SOFTBUS_ERR)); + HbNoneStateEnter(&hbFsm->fsm); + HbNoneStateEnter(nullptr); + EXPECT_TRUE(hbFsm != nullptr); + SoftBusSleepMs(50); + LnnDestroyHeartbeatFsm(hbFsm); +} + +/* + * @tc.name: OnProcessSendOnce + * @tc.desc: check heartbeat fsm state message + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatFSMTest, OnProcessSendOnceTest_01, TestSize.Level1) +{ + NiceMock heartbeatFsmMock; + LnnHeartbeatFsm *hbFsm = LnnCreateHeartbeatFsm(); + EXPECT_CALL(heartbeatFsmMock, LnnGetHbStrategyManager) + .WillOnce(Return(SOFTBUS_ERR)) + .WillRepeatedly(Return(SOFTBUS_OK)); + + LnnProcessSendOnceMsgPara *para = + reinterpret_cast(SoftBusCalloc(sizeof(LnnProcessSendOnceMsgPara))); + para->hbType = HEARTBEAT_TYPE_BLE_V0; + + int32_t ret = OnProcessSendOnce(&hbFsm->fsm, TEST_ARGS, reinterpret_cast(para)); + EXPECT_TRUE(ret == SOFTBUS_ERR); + + void *para2 = SoftBusCalloc(sizeof(LnnProcessSendOnceMsgPara)); + ret = OnProcessSendOnce(&hbFsm->fsm, TEST_ARGS, para2); + EXPECT_TRUE(ret == SOFTBUS_ERR); + + ret = OnProcessSendOnce(&hbFsm->fsm, TEST_ARGS, nullptr); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + + void *para3 = SoftBusCalloc(sizeof(LnnProcessSendOnceMsgPara)); + ret = OnProcessSendOnce(nullptr, TEST_ARGS, reinterpret_cast(para3)); + EXPECT_TRUE(ret == SOFTBUS_ERR); + SoftBusSleepMs(50); + LnnDestroyHeartbeatFsm(hbFsm); +} + +/* + * @tc.name: RemoveCheckDevStatusMsg + * @tc.desc: check heartbeat fsm state message + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatFSMTest, RemoveCheckDevStatusMsgTest_01, TestSize.Level1) +{ + LnnCheckDevStatusMsgPara msgPara = { + .hbType = HEARTBEAT_TYPE_BLE_V1, + .hasNetworkId = true, + .networkId = TEST_NETWORK_ID, + }; + LnnCheckDevStatusMsgPara delMsgPara = { + .hbType = HEARTBEAT_TYPE_BLE_V0, + .hasNetworkId = true, + .networkId = TEST_NETWORK_ID2, + }; + FsmCtrlMsgObj ctrlMsgObj = { + .obj = reinterpret_cast(&msgPara), + }; + SoftBusMessage delMsg = { + .obj = reinterpret_cast(&delMsgPara), + }; + + int32_t ret = RemoveCheckDevStatusMsg(&ctrlMsgObj, &delMsg); + EXPECT_TRUE(ret != SOFTBUS_OK); + + if (strcpy_s(const_cast(delMsgPara.networkId), sizeof(TEST_NETWORK_ID2), TEST_NETWORK_ID) != SOFTBUS_OK) { + LLOGE("strcpy failed"); + } + ret = RemoveCheckDevStatusMsg(&ctrlMsgObj, &delMsg); + EXPECT_FALSE(ret == SOFTBUS_OK); + + delMsgPara.hasNetworkId = false; + delMsg.obj = reinterpret_cast(&delMsgPara); + ret = RemoveCheckDevStatusMsg(&ctrlMsgObj, &delMsg); + EXPECT_FALSE(ret == SOFTBUS_OK); + + delMsgPara.hbType = HEARTBEAT_TYPE_BLE_V1; + if (strcpy_s(const_cast(delMsgPara.networkId), sizeof(TEST_NETWORK_ID2), TEST_NETWORK_ID2) != SOFTBUS_OK) { + LLOGE("strcpy failed"); + } + delMsg.obj = reinterpret_cast(&delMsgPara); + ret = RemoveCheckDevStatusMsg(&ctrlMsgObj, &delMsg); + EXPECT_FALSE(ret == SOFTBUS_OK); +} + +/* + * @tc.name: RemoveSendOnceMsg + * @tc.desc: check heartbeat fsm state message + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatFSMTest, RemoveSendOnceMsgTest_01, TestSize.Level1) +{ + LnnProcessSendOnceMsgPara *msgPara = + reinterpret_cast(SoftBusCalloc(sizeof(LnnProcessSendOnceMsgPara))); + msgPara->hbType = HEARTBEAT_TYPE_BLE_V1; + msgPara->strategyType = STRATEGY_HB_SEND_FIXED_PERIOD; + LnnProcessSendOnceMsgPara delMsgPara = { + .hbType = HEARTBEAT_TYPE_BLE_V1, + .strategyType = STRATEGY_HB_SEND_FIXED_PERIOD, + }; + FsmCtrlMsgObj ctrlMsgObj = { + .obj = reinterpret_cast(&msgPara), + }; + SoftBusMessage delMsg = { + .obj = reinterpret_cast(&delMsgPara), + }; + int32_t ret = RemoveSendOnceMsg(&ctrlMsgObj, &delMsg); + EXPECT_TRUE(ret != SOFTBUS_OK); + + LnnProcessSendOnceMsgPara *msgPara2 = + reinterpret_cast(SoftBusCalloc(sizeof(LnnProcessSendOnceMsgPara))); + msgPara->hbType = HEARTBEAT_TYPE_BLE_V1; + msgPara->strategyType = STRATEGY_HB_SEND_FIXED_PERIOD; + FsmCtrlMsgObj ctrlMsgObj2 = { + .obj = reinterpret_cast(&msgPara2), + }; + delMsgPara.strategyType = STRATEGY_HB_RECV_SINGLE; + delMsg.obj = reinterpret_cast(&delMsgPara); + ret = RemoveSendOnceMsg(&ctrlMsgObj2, &delMsg); + EXPECT_FALSE(ret == SOFTBUS_OK); + delMsgPara.hbType = HEARTBEAT_TYPE_BLE_V0; + delMsg.obj = reinterpret_cast(&delMsgPara); + ret = RemoveSendOnceMsg(&ctrlMsgObj2, &delMsg); + EXPECT_FALSE(ret == SOFTBUS_OK); +} + +/* + * @tc.name: OnSendOneHbBegin + * @tc.desc: check heartbeat fsm state message + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatFSMTest, OnSendOneHbBeginTest_01, TestSize.Level1) +{ + HeartBeatFSMInterfaceMock heartbeatFsmMock; + void *para = SoftBusCalloc(sizeof(LnnHeartbeatCustSendData)); + EXPECT_CALL(heartbeatFsmMock, LnnHbMediumMgrSendBegin).WillRepeatedly(Return(SOFTBUS_ERR)); + int32_t ret = OnSendOneHbBegin(nullptr, TEST_ARGS, nullptr); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + ret = OnSendOneHbBegin(nullptr, TEST_ARGS, para); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* + * @tc.name: OnSendOneHbEnd + * @tc.desc: check heartbeat fsm state message + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatFSMTest, OnSendOneHbEndTest_01, TestSize.Level1) +{ + NiceMock heartbeatFsmMock; + EXPECT_CALL(heartbeatFsmMock, LnnHbMediumMgrSendEnd).WillRepeatedly(Return(SOFTBUS_ERR)); + void *para = SoftBusCalloc(sizeof(LnnHeartbeatType)); + int32_t ret = OnSendOneHbEnd(nullptr, TEST_ARGS, nullptr); + LnnHeartbeatFsm *hbFsm = LnnCreateHeartbeatFsm(); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + ret = OnSendOneHbEnd(nullptr, TEST_ARGS, para); + EXPECT_TRUE(ret == SOFTBUS_ERR); + void *para2 = SoftBusCalloc(sizeof(LnnHeartbeatType)); + ret = OnSendOneHbEnd(nullptr, TEST_ARGS, para2); + EXPECT_TRUE(ret == SOFTBUS_ERR); + void *para3 = SoftBusCalloc(sizeof(LnnHeartbeatType)); + ret = OnSendOneHbEnd(&hbFsm->fsm, TEST_ARGS, para3); + EXPECT_TRUE(ret == SOFTBUS_ERR); + void *para4 = SoftBusCalloc(sizeof(LnnHeartbeatType)); + ret = OnStartHbProcess(nullptr, TEST_ARGS, para4); + EXPECT_TRUE(ret == SOFTBUS_ERR); + SoftBusSleepMs(20); + LnnDestroyHeartbeatFsm(hbFsm); +} + +/* + * @tc.name: OnStopHbByType + * @tc.desc: check heartbeat fsm state message + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatFSMTest, OnStopHbByTypeTest_01, TestSize.Level1) +{ + NiceMock heartbeatFsmMock; + EXPECT_CALL(heartbeatFsmMock, LnnHbMediumMgrStop).WillRepeatedly(Return(SOFTBUS_ERR)); + LnnHeartbeatFsm *hbFsm = LnnCreateHeartbeatFsm(); + void *para = SoftBusCalloc(sizeof(LnnHeartbeatType)); + int32_t ret = OnStopHbByType(nullptr, TEST_ARGS, nullptr); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + ret = OnStopHbByType(nullptr, TEST_ARGS, para); + EXPECT_TRUE(ret == SOFTBUS_ERR); + void *para2 = SoftBusCalloc(sizeof(LnnHeartbeatType)); + ret = OnStopHbByType(&hbFsm->fsm, TEST_ARGS, para2); + EXPECT_TRUE(ret == SOFTBUS_ERR); + ret = OnSetMediumParam(nullptr, TEST_ARGS, nullptr); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + EXPECT_CALL(heartbeatFsmMock, LnnHbMediumMgrSetParam).WillRepeatedly(Return(SOFTBUS_ERR)); + void *para3 = SoftBusCalloc(sizeof(LnnHeartbeatType)); + ret = OnSetMediumParam(nullptr, TEST_ARGS, para3); + EXPECT_TRUE(ret == SOFTBUS_ERR); + SoftBusSleepMs(20); + LnnDestroyHeartbeatFsm(hbFsm); +} + +/* + * @tc.name: TryAsMasterNodeNextLoop + * @tc.desc: check heartbeat fsm state message + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatFSMTest, TryAsMasterNodeNextLoopTest_01, TestSize.Level1) +{ + NiceMock heartbeatFsmMock; + ON_CALL(heartbeatFsmMock, LnnGetGearModeBySpecificType).WillByDefault(Return(SOFTBUS_ERR)); + LnnHeartbeatFsm *hbFsm = LnnCreateHeartbeatFsm(); + TryAsMasterNodeNextLoop(&hbFsm->fsm); + LnnDestroyHeartbeatFsm(hbFsm); +} + +/* + * @tc.name: ProcessLostHeartbeat + * @tc.desc: check heartbeat fsm state message + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatFSMTest, ProcessLostHeartbeatTest_01, TestSize.Level1) +{ + DistributeLedgerInterfaceMock distriLedgerMock; + NiceMock heartbeatFsmMock; + LnnConnectInterfaceMock connMock; + ON_CALL(distriLedgerMock, LnnConvertDLidToUdid).WillByDefault(Return(TEST_UDID)); + ON_CALL(heartbeatFsmMock, LnnRequestLeaveSpecific).WillByDefault(Return(SOFTBUS_OK)); + EXPECT_CALL(distriLedgerMock, LnnGetRemoteStrInfo).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(connMock, CheckActiveConnection).WillRepeatedly(Return(true)); + EXPECT_CALL(heartbeatFsmMock, LnnRequestLeaveSpecific).WillRepeatedly(Return(SOFTBUS_ERR)); + int32_t ret = ProcessLostHeartbeat(nullptr, CONNECTION_ADDR_BLE); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + EXPECT_CALL(distriLedgerMock, LnnGetOnlineStateById).WillOnce(Return(false)).WillRepeatedly(Return(true)); + ret = ProcessLostHeartbeat(TEST_NETWORK_ID, CONNECTION_ADDR_BLE); + EXPECT_TRUE(ret == SOFTBUS_OK); + ret = ProcessLostHeartbeat(TEST_NETWORK_ID, CONNECTION_ADDR_BR); + EXPECT_TRUE(ret == SOFTBUS_ERR); + ret = ProcessLostHeartbeat(TEST_NETWORK_ID, CONNECTION_ADDR_BLE); + EXPECT_TRUE(ret == SOFTBUS_OK); + ret = ProcessLostHeartbeat(TEST_NETWORK_ID, CONNECTION_ADDR_ETH); + EXPECT_TRUE(ret == SOFTBUS_ERR); + SoftBusSleepMs(20); +} + +/* + * @tc.name: IsTimestampExceedLimit + * @tc.desc: check heartbeat fsm state message + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatFSMTest, IsTimestampExceedLimitTest_01, TestSize.Level1) +{ + NiceMock distriLedgerMock; + NiceMock heartbeatFsmMock; + EXPECT_CALL(heartbeatFsmMock, LnnGetGearModeBySpecificType) + .WillOnce(Return(SOFTBUS_ERR)) + .WillRepeatedly(Return(SOFTBUS_OK)); + bool ret = IsTimestampExceedLimit(TEST_TIME2, TEST_TIME1, HEARTBEAT_TYPE_BLE_V0); + EXPECT_FALSE(ret); + ret = IsTimestampExceedLimit(TEST_TIME3, TEST_TIME1, HEARTBEAT_TYPE_BLE_V0); + EXPECT_TRUE(ret); + ret = IsTimestampExceedLimit(TEST_TIME2, TEST_TIME1, HEARTBEAT_TYPE_BLE_V1); + EXPECT_FALSE(ret); + ret = IsTimestampExceedLimit(TEST_TIME2, TEST_TIME1, HEARTBEAT_TYPE_BLE_V1); + EXPECT_FALSE(ret); + ret = IsTimestampExceedLimit(TEST_TIME3, TEST_TIME1, HEARTBEAT_TYPE_BLE_V1); + EXPECT_TRUE(ret); +} + +/* + * @tc.name: CheckDevStatusByNetworkId + * @tc.desc: check heartbeat fsm state message + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatFSMTest, CheckDevStatusByNetworkIdTest_01, TestSize.Level1) +{ + NiceMock distriLedgerMock; + NiceMock heartbeatFsmMock; + NiceMock ledgerMock; + NodeInfo nodeInfo = { + .discoveryType = TEST_DISC_TYPE, + .deviceInfo.deviceUdid = TEST_UDID, + }; + LnnHeartbeatFsm *hbFsm = LnnCreateHeartbeatFsm(); + uint64_t oldTimeStamp = TEST_TIME3; + ON_CALL(ledgerMock, LnnGetNodeInfoById).WillByDefault(Return(&nodeInfo)); + ON_CALL(heartbeatFsmMock, LnnStopOfflineTimingStrategy).WillByDefault(Return(SOFTBUS_OK)); + ON_CALL(heartbeatFsmMock, LnnGetGearModeBySpecificType).WillByDefault(Return(SOFTBUS_OK)); + ON_CALL(distriLedgerMock, LnnGetDLHeartbeatTimestamp) + .WillByDefault(DoAll(SetArgPointee<1>(oldTimeStamp), Return(SOFTBUS_OK))); + ON_CALL(ledgerMock, LnnHasDiscoveryType).WillByDefault(Return(true)); + ON_CALL(heartbeatFsmMock, LnnConvAddrTypeToDiscType).WillByDefault(Return(DISCOVERY_TYPE_BLE)); + CheckDevStatusByNetworkId(hbFsm, TEST_NETWORK_ID, HEARTBEAT_TYPE_BLE_V0, TEST_TIME1); + EXPECT_CALL(ledgerMock, LnnGetNodeInfoById).WillOnce(Return(nullptr)).WillRepeatedly(Return(&nodeInfo)); + CheckDevStatusByNetworkId(hbFsm, TEST_NETWORK_ID, HEARTBEAT_TYPE_BLE_V0, TEST_TIME1); + EXPECT_CALL(ledgerMock, LnnHasDiscoveryType).WillOnce(Return(false)).WillRepeatedly(Return(true)); + CheckDevStatusByNetworkId(hbFsm, TEST_NETWORK_ID, HEARTBEAT_TYPE_BLE_V0, TEST_TIME1); + EXPECT_CALL(distriLedgerMock, LnnGetDLHeartbeatTimestamp) + .WillOnce(Return(SOFTBUS_ERR)) + .WillRepeatedly(DoAll(SetArgPointee<1>(oldTimeStamp), Return(SOFTBUS_OK))); + CheckDevStatusByNetworkId(hbFsm, TEST_NETWORK_ID, HEARTBEAT_TYPE_BLE_V0, TEST_TIME1); + CheckDevStatusByNetworkId(hbFsm, TEST_NETWORK_ID, HEARTBEAT_TYPE_BLE_V0, TEST_TIME3); + EXPECT_CALL(heartbeatFsmMock, LnnStopOfflineTimingStrategy) + .WillOnce(Return(SOFTBUS_ERR)) + .WillRepeatedly(Return(SOFTBUS_ERR)); + CheckDevStatusByNetworkId(hbFsm, TEST_NETWORK_ID, HEARTBEAT_TYPE_BLE_V0, TEST_TIME1); + LnnDestroyHeartbeatFsm(hbFsm); +} + +/* + * @tc.name: OnCheckDevStatus + * @tc.desc: check heartbeat fsm state message + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatFSMTest, OnCheckDevStatusTest_01, TestSize.Level1) +{ + NiceMock netLedgerMock; + FsmStateMachine fsm; + LnnHeartbeatFsm *hbFsm = LnnCreateHeartbeatFsm(); + ON_CALL(netLedgerMock, LnnGetAllOnlineNodeInfo).WillByDefault(Return(SOFTBUS_OK)); + int32_t ret = OnCheckDevStatus(nullptr, TEST_ARGS, nullptr); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + void *para = SoftBusCalloc(sizeof(LnnCheckDevStatusMsgPara)); + ret = OnCheckDevStatus(nullptr, TEST_ARGS, para); + EXPECT_TRUE(ret == SOFTBUS_ERR); + LnnCheckDevStatusMsgPara *para2 = + reinterpret_cast(SoftBusCalloc(sizeof(LnnCheckDevStatusMsgPara))); + para2->hasNetworkId = true; + ret = OnCheckDevStatus(&hbFsm->fsm, TEST_ARGS, reinterpret_cast(para2)); + EXPECT_TRUE(ret == SOFTBUS_OK); + + LnnCheckDevStatusMsgPara *para3 = + reinterpret_cast(SoftBusCalloc(sizeof(LnnCheckDevStatusMsgPara))); + para2->hasNetworkId = false; + ret = OnCheckDevStatus(&hbFsm->fsm, TEST_ARGS, reinterpret_cast(para3)); + EXPECT_TRUE(ret == SOFTBUS_OK); + LnnDestroyHeartbeatFsm(nullptr); + DeinitHbFsmCallback(nullptr); + ret = LnnStartHeartbeatFsm(nullptr); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + hbFsm->fsm = fsm; + ret = LnnStartHeartbeatFsm(hbFsm); + EXPECT_TRUE(ret == SOFTBUS_ERR); + ret = LnnStopHeartbeatFsm(nullptr); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + ret = LnnStopHeartbeatFsm(hbFsm); + EXPECT_TRUE(ret == SOFTBUS_ERR); + LnnDestroyHeartbeatFsm(hbFsm); +} +/* + * @tc.name: LnnPostNextSendOnceMsgToHbFsm + * @tc.desc: check heartbeat fsm state message + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatFSMTest, LnnPostNextSendOnceMsgToHbFsmTest_01, TestSize.Level1) +{ + LnnHeartbeatFsm *hbFsm = LnnCreateHeartbeatFsm(); + FsmStateMachine fsm; + int32_t ret = LnnPostNextSendOnceMsgToHbFsm(nullptr, nullptr, TEST_TIME1); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + LnnProcessSendOnceMsgPara para; + hbFsm->fsm = fsm; + ret = LnnPostNextSendOnceMsgToHbFsm(hbFsm, ¶, TEST_TIME1); + EXPECT_TRUE(ret == SOFTBUS_ERR); + ret = LnnPostSendEndMsgToHbFsm(nullptr, HEARTBEAT_TYPE_BLE_V1, TEST_TIME1); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + ret = LnnPostSendEndMsgToHbFsm(hbFsm, HEARTBEAT_TYPE_BLE_V1, TEST_TIME1); + EXPECT_TRUE(ret == SOFTBUS_ERR); + ret = LnnPostStartMsgToHbFsm(nullptr, TEST_TIME1); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + ret = LnnPostStopMsgToHbFsm(nullptr, HEARTBEAT_TYPE_BLE_V1); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + ret = LnnPostStopMsgToHbFsm(hbFsm, HEARTBEAT_TYPE_BLE_V1); + EXPECT_TRUE(ret == SOFTBUS_ERR); + ret = LnnPostTransStateMsgToHbFsm(nullptr, EVENT_HB_SEND_ONE_END); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + ret = LnnPostTransStateMsgToHbFsm(nullptr, EVENT_HB_MAX); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + ret = LnnPostSetMediumParamMsgToHbFsm(nullptr, nullptr); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + LnnHeartbeatMediumParam para2; + ret = LnnPostSetMediumParamMsgToHbFsm(hbFsm, ¶2); + EXPECT_TRUE(ret == SOFTBUS_ERR); + ret = LnnPostCheckDevStatusMsgToHbFsm(nullptr, nullptr, TEST_TIME1); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + ret = LnnPostCheckDevStatusMsgToHbFsm(hbFsm, nullptr, TEST_TIME1); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); +} +} // namespace OHOS \ No newline at end of file diff --git a/tests/core/bus_center/test/heartbeat/HB_medium_mgr_test.cpp b/tests/core/bus_center/test/heartbeat/HB_medium_mgr_test.cpp new file mode 100644 index 0000000000..accfe995d8 --- /dev/null +++ b/tests/core/bus_center/test/heartbeat/HB_medium_mgr_test.cpp @@ -0,0 +1,571 @@ +/* + * Copyright (c) 2022 Huawei Device 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 +#include + +#include "distribute_net_ledger_mock.h" +#include "hb_strategy_mock.h" +#include "lnn_ble_heartbeat.h" +#include "lnn_heartbeat_medium_mgr.c" +#include "lnn_heartbeat_utils.h" +#include "lnn_net_ledger_mock.h" +#include "softbus_common.h" + +namespace OHOS { +using namespace testing::ext; +using namespace testing; + +#define TEST_WEIGHT 2 +#define TEST_DEVID "5987321652" +#define TEST_NETWORK_ID "6542316a57d" +#define TEST_WEIGHT2 3 +#define TEST_RECVTIME_FIRST 0 +#define TEST_RECVTIME_LAST 5 +#define TEST_DISC_TYPE 5321 +#define TEST_UDID_HASH "1111222233334444" + +class HeartBeatMgrTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void HeartBeatMgrTest::SetUpTestCase() +{ + HbInitRecvList(); +} + +void HeartBeatMgrTest::TearDownTestCase() +{ + HbDeinitRecvList(); +} + +void HeartBeatMgrTest::SetUp() { } + +void HeartBeatMgrTest::TearDown() { } + +int32_t onUpdateSendInfo1(LnnHeartbeatUpdateInfoType type) +{ + return SOFTBUS_OK; +} + +int32_t onUpdateSendInfo2(LnnHeartbeatUpdateInfoType type) +{ + return SOFTBUS_ERR; +} + +/* + * @tc.name: HbFirstSaveRecvTime + * @tc.desc: heart beat first save + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatMgrTest, HbFirstSaveRecvTimeTest_01, TestSize.Level1) +{ + DeviceInfo device; + (void)memset_s(&device, sizeof(DeviceInfo), 0, sizeof(DeviceInfo)); + int32_t weight = TEST_WEIGHT; + int32_t masterWeight = TEST_WEIGHT2; + uint64_t recvTime = TEST_RECVTIME_FIRST; + int32_t ret = HbFirstSaveRecvTime(&device, weight, masterWeight, recvTime); + EXPECT_TRUE(ret == SOFTBUS_OK); +} + +/* + * @tc.name: RemoveRepeatRecvTime + * @tc.desc: remove repeat received time + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatMgrTest, RemoveRepeatRecvTimeTest_01, TestSize.Level1) +{ + DeviceInfo device1; + (void)memset_s(&device1, sizeof(DeviceInfo), 0, sizeof(DeviceInfo)); + (void)strcpy_s(device1.devId, DISC_MAX_DEVICE_ID_LEN, TEST_DEVID); + device1.addr->type = CONNECTION_ADDR_BR; + int32_t weight = TEST_WEIGHT; + int32_t masterWeight = TEST_WEIGHT2; + uint64_t recvTime1 = TEST_RECVTIME_FIRST; + int32_t ret = HbFirstSaveRecvTime(&device1, weight, masterWeight, recvTime1); + EXPECT_TRUE(ret == SOFTBUS_OK); + ret = HbSaveRecvTimeToRemoveRepeat(&device1, weight, masterWeight, recvTime1); + EXPECT_TRUE(ret == SOFTBUS_OK); + DeviceInfo device2; + (void)memset_s(&device2, sizeof(DeviceInfo), 0, sizeof(DeviceInfo)); + (void)strcpy_s(device2.devId, DISC_MAX_DEVICE_ID_LEN, TEST_DEVID); + device2.addr->type = CONNECTION_ADDR_WLAN; + uint64_t recvTime2 = TEST_RECVTIME_LAST; + ret = HbSaveRecvTimeToRemoveRepeat(&device2, weight, masterWeight, recvTime2); + EXPECT_TRUE(ret == SOFTBUS_OK); + uint64_t recvTime3 = TEST_RECVTIME_LAST + HB_RECV_INFO_SAVE_LEN; + ret = HbSaveRecvTimeToRemoveRepeat(&device2, weight, masterWeight, recvTime3); + EXPECT_TRUE(ret == SOFTBUS_OK); +} + +/* + * @tc.name: IsRepeatedRecvInfo + * @tc.desc: determine whether the message is repeated + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatMgrTest, IsRepeatedRecvInfoTest_01, TestSize.Level1) +{ + DeviceInfo device; + (void)memset_s(&device, sizeof(DeviceInfo), 0, sizeof(DeviceInfo)); + (void)strcpy_s(device.devId, sizeof(TEST_DEVID), TEST_DEVID); + device.addr->type = CONNECTION_ADDR_BR; + int32_t weight = TEST_WEIGHT; + int32_t masterWeight = TEST_WEIGHT2; + uint64_t recvTime1 = TEST_RECVTIME_FIRST; + int32_t ret1 = HbFirstSaveRecvTime(&device, weight, masterWeight, recvTime1); + EXPECT_TRUE(ret1 == SOFTBUS_OK); + bool ret2 = HbIsRepeatedRecvInfo(TEST_UDID_HASH, CONNECTION_ADDR_BR, TEST_RECVTIME_FIRST); + EXPECT_FALSE(ret2); + ret2 = HbIsRepeatedRecvInfo(TEST_DEVID, CONNECTION_ADDR_BLE, TEST_RECVTIME_LAST); + EXPECT_FALSE(ret2); + uint64_t nowTime = TEST_RECVTIME_LAST + HB_RECV_INFO_SAVE_LEN; + ret2 = HbIsRepeatedRecvInfo(TEST_DEVID, CONNECTION_ADDR_BR, nowTime); + EXPECT_FALSE(ret2); +} + +/* + * @tc.name: GetOnlineNodeByRecvInfo + * @tc.desc: get online node by received info + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatMgrTest, GetOnlineNodeByRecvInfoTest_01, TestSize.Level1) +{ + NodeInfo nodeInfo = { + .discoveryType = TEST_DISC_TYPE, + .deviceInfo.deviceUdid = TEST_UDID_HASH, + }; + char udidHash[HB_SHORT_UDID_HASH_HEX_LEN + 1]; + (void)memset_s(udidHash, sizeof(udidHash), 0, sizeof(udidHash)); + NiceMock ledgerMock; + ON_CALL(ledgerMock, LnnGetAllOnlineNodeInfo).WillByDefault(LnnNetLedgertInterfaceMock::ActionOfLnnGetAllOnline); + ON_CALL(ledgerMock, LnnGetNodeInfoById).WillByDefault(Return(&nodeInfo)); + ON_CALL(ledgerMock, LnnHasDiscoveryType).WillByDefault(Return(true)); + LnnGenerateHexStringHash( + reinterpret_cast(TEST_UDID_HASH), udidHash, HB_SHORT_UDID_HASH_HEX_LEN); + auto ret = HbGetOnlineNodeByRecvInfo(udidHash, CONNECTION_ADDR_BR); + EXPECT_TRUE(ret != nullptr); + EXPECT_CALL(ledgerMock, LnnGetAllOnlineNodeInfo) + .WillOnce(Return(SOFTBUS_OK)) + .WillRepeatedly(LnnNetLedgertInterfaceMock::ActionOfLnnGetAllOnline); + ret = HbGetOnlineNodeByRecvInfo(TEST_UDID_HASH, CONNECTION_ADDR_BR); + EXPECT_TRUE(ret == nullptr); + EXPECT_CALL(ledgerMock, LnnGetNodeInfoById).WillOnce(Return(nullptr)); + ret = HbGetOnlineNodeByRecvInfo(TEST_UDID_HASH, CONNECTION_ADDR_WLAN); + EXPECT_TRUE(ret == nullptr); +} + +/* + * @tc.name: HbUpdateOfflineTiming + * @tc.desc: updata offline timing + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatMgrTest, HbUpdateOfflineTimingTest_01, TestSize.Level1) +{ + NiceMock disLedgerMock; + NiceMock hbStrateMock; + ON_CALL(disLedgerMock, LnnSetDLHeartbeatTimestamp).WillByDefault(Return(SOFTBUS_OK)); + ON_CALL(hbStrateMock, LnnStopOfflineTimingStrategy).WillByDefault(Return(SOFTBUS_OK)); + ON_CALL(hbStrateMock, LnnStartOfflineTimingStrategy).WillByDefault(Return(SOFTBUS_OK)); + int ret = + HbUpdateOfflineTimingByRecvInfo(TEST_NETWORK_ID, CONNECTION_ADDR_BR, HEARTBEAT_TYPE_BLE_V1, TEST_RECVTIME_LAST); + EXPECT_TRUE(ret == SOFTBUS_OK); + EXPECT_CALL(disLedgerMock, LnnSetDLHeartbeatTimestamp) + .WillOnce(Return(SOFTBUS_ERR)) + .WillRepeatedly(Return(SOFTBUS_OK)); + ret = + HbUpdateOfflineTimingByRecvInfo(TEST_NETWORK_ID, CONNECTION_ADDR_BR, HEARTBEAT_TYPE_BLE_V1, TEST_RECVTIME_LAST); + EXPECT_TRUE(ret == SOFTBUS_ERR); + EXPECT_CALL(hbStrateMock, LnnStopOfflineTimingStrategy) + .WillOnce(Return(SOFTBUS_ERR)) + .WillRepeatedly(Return(SOFTBUS_OK)); + ret = + HbUpdateOfflineTimingByRecvInfo(TEST_NETWORK_ID, CONNECTION_ADDR_BR, HEARTBEAT_TYPE_BLE_V1, TEST_RECVTIME_LAST); + EXPECT_TRUE(ret == SOFTBUS_ERR); + EXPECT_CALL(hbStrateMock, LnnStartOfflineTimingStrategy) + .WillOnce(Return(SOFTBUS_ERR)) + .WillRepeatedly(Return(SOFTBUS_OK)); + ret = + HbUpdateOfflineTimingByRecvInfo(TEST_NETWORK_ID, CONNECTION_ADDR_BR, HEARTBEAT_TYPE_BLE_V1, TEST_RECVTIME_LAST); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* + * @tc.name: HbMediumMgrRecvProcess + * @tc.desc: medium manger received process + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatMgrTest, HbMediumMgrRecvProcessTest_01, TestSize.Level1) +{ + DeviceInfo device; + (void)memset_s(&device, sizeof(DeviceInfo), 0, sizeof(DeviceInfo)); + NiceMock ledgerMock; + NiceMock disLedgerMock; + NiceMock hbStrateMock; + NodeInfo nodeInfo = { + .discoveryType = TEST_DISC_TYPE, + .deviceInfo.deviceUdid = TEST_UDID_HASH, + }; + ON_CALL(ledgerMock, LnnGetAllOnlineNodeInfo).WillByDefault(LnnNetLedgertInterfaceMock::ActionOfLnnGetAllOnline); + ON_CALL(ledgerMock, LnnGetNodeInfoById).WillByDefault(Return(&nodeInfo)); + ON_CALL(ledgerMock, LnnHasDiscoveryType).WillByDefault(Return(true)); + ON_CALL(hbStrateMock, LnnNotifyDiscoveryDevice).WillByDefault(Return(SOFTBUS_OK)); + char udidHash[HB_SHORT_UDID_HASH_HEX_LEN + 1]; + LnnGenerateHexStringHash( + reinterpret_cast(TEST_UDID_HASH), udidHash, HB_SHORT_UDID_HASH_HEX_LEN); + (void)strcpy_s(device.devId, DISC_MAX_DEVICE_ID_LEN, udidHash); + device.addr->type = CONNECTION_ADDR_BR; + device.devType = SMART_PHONE; + int32_t weight = TEST_WEIGHT; + int32_t masterWeight = TEST_WEIGHT2; + int32_t ret1 = HbFirstSaveRecvTime(&device, weight, masterWeight, TEST_RECVTIME_FIRST); + EXPECT_TRUE(ret1 == SOFTBUS_OK); + ON_CALL(disLedgerMock, LnnSetDLHeartbeatTimestamp).WillByDefault(Return(SOFTBUS_OK)); + ON_CALL(hbStrateMock, LnnStopOfflineTimingStrategy).WillByDefault(Return(SOFTBUS_OK)); + ON_CALL(hbStrateMock, LnnStartOfflineTimingStrategy).WillByDefault(Return(SOFTBUS_OK)); + int ret = HbMediumMgrRecvProcess(&device, weight, masterWeight, HEARTBEAT_TYPE_BLE_V1); + EXPECT_TRUE(ret == SOFTBUS_OK); + HbFirstSaveRecvTime(&device, weight, masterWeight, TEST_RECVTIME_FIRST); + EXPECT_CALL(ledgerMock, LnnGetAllOnlineNodeInfo).WillOnce(Return(SOFTBUS_ERR)); + ret = HbMediumMgrRecvProcess(&device, weight, masterWeight, HEARTBEAT_TYPE_BLE_V1); + EXPECT_TRUE(ret != SOFTBUS_ERR); + ret = HbMediumMgrRecvProcess(nullptr, weight, masterWeight, HEARTBEAT_TYPE_BLE_V1); + EXPECT_TRUE(ret == SOFTBUS_ERR); + (void)memset_s(&device, sizeof(DeviceInfo), 0, sizeof(DeviceInfo)); + ret = HbMediumMgrRecvProcess(&device, weight, masterWeight, HEARTBEAT_TYPE_BLE_V1); + EXPECT_TRUE(ret == SOFTBUS_NETWORK_NODE_OFFLINE); +} + +/* + * @tc.name: HbMediumMgrRecvHigherWeight + * @tc.desc: after receive highter weight data process + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatMgrTest, HbMediumMgrRecvHigherWeightTest_01, TestSize.Level1) +{ + NiceMock hbStrategyMock; + NiceMock ledgerMock; + NodeInfo nodeInfo = { + .discoveryType = TEST_DISC_TYPE, + .deviceInfo.deviceUdid = TEST_UDID_HASH, + }; + char udidHash[HB_SHORT_UDID_HASH_HEX_LEN + 1]; + (void)memset_s(udidHash, sizeof(udidHash), 0, sizeof(udidHash)); + ON_CALL(hbStrategyMock, LnnNotifyMasterElect).WillByDefault(Return(SOFTBUS_OK)); + ON_CALL(ledgerMock, LnnGetAllOnlineNodeInfo).WillByDefault(LnnNetLedgertInterfaceMock::ActionOfLnnGetAllOnline); + ON_CALL(ledgerMock, LnnGetNodeInfoById).WillByDefault(Return(&nodeInfo)); + ON_CALL(ledgerMock, LnnHasDiscoveryType).WillByDefault(Return(true)); + ON_CALL(ledgerMock, LnnGetLocalStrInfo).WillByDefault(LnnNetLedgertInterfaceMock::ActionOfLnnGetLocalStrInfo); + EXPECT_CALL(hbStrategyMock, LnnSetHbAsMasterNodeState).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(ledgerMock, LnnConvertIdToDeviceType).WillRepeatedly(Return(const_cast(TYPE_PAD))); + LnnGenerateHexStringHash( + reinterpret_cast(TEST_UDID_HASH), udidHash, HB_SHORT_UDID_HASH_HEX_LEN); + int32_t ret = HbMediumMgrRecvHigherWeight(udidHash, TEST_WEIGHT, CONNECTION_ADDR_BR, true); + EXPECT_TRUE(ret == SOFTBUS_OK); + EXPECT_CALL(ledgerMock, LnnGetAllOnlineNodeInfo) + .WillOnce(Return(SOFTBUS_ERR)) + .WillRepeatedly(LnnNetLedgertInterfaceMock::ActionOfLnnGetAllOnline); + ret = HbMediumMgrRecvHigherWeight(udidHash, TEST_WEIGHT, CONNECTION_ADDR_BR, true); + EXPECT_TRUE(ret == SOFTBUS_OK); + ret = HbMediumMgrRecvHigherWeight(nullptr, TEST_WEIGHT, CONNECTION_ADDR_BR, true); + EXPECT_TRUE(ret == SOFTBUS_ERR); + EXPECT_CALL(hbStrategyMock, LnnNotifyMasterElect).WillRepeatedly(Return(SOFTBUS_ERR)); + ret = HbMediumMgrRecvHigherWeight(udidHash, TEST_WEIGHT, CONNECTION_ADDR_BR, false); + EXPECT_TRUE(ret == SOFTBUS_OK); + SoftBusSleepMs(50); +} + +/* + * @tc.name: HbMediumMgrRelayProcess + * @tc.desc: receive relay data process + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatMgrTest, HbMediumMgrRelayProcess, TestSize.Level1) +{ + HeartBeatStategyInterfaceMock hbStrategyMock; + EXPECT_CALL(hbStrategyMock, LnnStartHbByTypeAndStrategy) + .WillOnce(Return(SOFTBUS_ERR)) + .WillRepeatedly(Return(SOFTBUS_OK)); + HbMediumMgrRelayProcess(TEST_DEVID, CONNECTION_ADDR_BR, HEARTBEAT_TYPE_BLE_V1); + HbMediumMgrRelayProcess(TEST_DEVID, CONNECTION_ADDR_BR, HEARTBEAT_TYPE_BLE_V1); + HbMediumMgrRelayProcess(nullptr, CONNECTION_ADDR_BR, HEARTBEAT_TYPE_BLE_V1); +} + +/* + * @tc.name: LnnDumpHbMgrRecvList + * @tc.desc: dump hearbeat manger received list + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatMgrTest, LnnDumpHbMgrRecvList_TEST01, TestSize.Level1) +{ + DeviceInfo device1; + NiceMock ledgerMock; + EXPECT_CALL(ledgerMock, LnnConvertIdToDeviceType) + .WillOnce(Return(nullptr)) + .WillRepeatedly(Return(const_cast(TYPE_PAD))); + (void)memset_s(&device1, sizeof(DeviceInfo), 0, sizeof(DeviceInfo)); + (void)strcpy_s(device1.devId, DISC_MAX_DEVICE_ID_LEN, TEST_DEVID); + device1.addr->type = CONNECTION_ADDR_BR; + int32_t weight = TEST_WEIGHT; + int32_t masterWeight = TEST_WEIGHT2; + uint64_t recvTime1 = TEST_RECVTIME_FIRST; + int32_t ret = HbFirstSaveRecvTime(&device1, weight, masterWeight, recvTime1); + EXPECT_TRUE(ret == SOFTBUS_OK); + DeviceInfo device2; + (void)memset_s(&device2, sizeof(DeviceInfo), 0, sizeof(DeviceInfo)); + (void)strcpy_s(device1.devId, DISC_MAX_DEVICE_ID_LEN, TEST_NETWORK_ID); + device2.addr->type = CONNECTION_ADDR_MAX; + ret = HbFirstSaveRecvTime(&device2, weight, masterWeight, TEST_RECVTIME_LAST); + EXPECT_TRUE(ret == SOFTBUS_OK); + LnnDumpHbMgrRecvList(); + SoftBusSleepMs(50); +} + +/* + * @tc.name: LnnDumpHbOnlineNodeList + * @tc.desc: dump heartbeat online nodelist + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatMgrTest, LnnDumpHbOnlineNodeList_TEST01, TestSize.Level1) +{ + NodeInfo nodeInfo = { + .discoveryType = TEST_DISC_TYPE, + .deviceInfo.deviceUdid = TEST_UDID_HASH, + }; + NiceMock ledgerMock; + NiceMock distrLedgerMock; + ON_CALL(ledgerMock, LnnGetAllOnlineNodeInfo).WillByDefault(LnnNetLedgertInterfaceMock::ActionOfLnnGetAllOnline); + ON_CALL(ledgerMock, LnnGetNodeInfoById).WillByDefault(Return(&nodeInfo)); + ON_CALL(distrLedgerMock, LnnGetDLHeartbeatTimestamp).WillByDefault(Return(SOFTBUS_OK)); + LnnDumpHbOnlineNodeList(); + EXPECT_CALL(ledgerMock, LnnGetAllOnlineNodeInfo) + .WillOnce(Return(SOFTBUS_ERR)) + .WillOnce(Return(SOFTBUS_OK)) + .WillRepeatedly(LnnNetLedgertInterfaceMock::ActionOfLnnGetAllOnline); + LnnDumpHbOnlineNodeList(); + LnnDumpHbOnlineNodeList(); + EXPECT_CALL(distrLedgerMock, LnnGetDLHeartbeatTimestamp) + .WillOnce(Return(SOFTBUS_ERR)) + .WillRepeatedly(Return(SOFTBUS_OK)); + LnnDumpHbOnlineNodeList(); +} + +/* + * @tc.name: VisitHbMediumMgrSendBegin + * @tc.desc: visit heartbeat medium manger send begin + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatMgrTest, VisitHbMediumMgrSendBegin_TEST01, TestSize.Level1) +{ + bool ret = VisitHbMediumMgrSendBegin(nullptr, HEARTBEAT_TYPE_MAX, nullptr); + EXPECT_FALSE(ret); + LnnHeartbeatCustSendData data = { + .hbType = HEARTBEAT_TYPE_BLE_V1, + .wakeupFlag = false, + .isRelay = false, + }; + ret = VisitHbMediumMgrSendBegin(nullptr, HEARTBEAT_TYPE_MAX, nullptr); + EXPECT_FALSE(ret); + LnnHbMediumMgrInit(); + ret = VisitHbMediumMgrSendBegin(nullptr, HEARTBEAT_TYPE_BLE_V0, reinterpret_cast(&data)); + EXPECT_FALSE(ret); + int id = LnnConvertHbTypeToId(HEARTBEAT_TYPE_BLE_V0); + g_hbMeidumMgr[id] = nullptr; + ret = VisitHbMediumMgrSendBegin(nullptr, HEARTBEAT_TYPE_BLE_V0, reinterpret_cast(&data)); + EXPECT_TRUE(ret); + LnnHbMediumMgrInit(); + g_hbMeidumMgr[id]->onSendOneHbBegin = nullptr; + ret = VisitHbMediumMgrSendBegin(nullptr, HEARTBEAT_TYPE_BLE_V0, reinterpret_cast(&data)); + EXPECT_TRUE(ret); +} + +/* + * @tc.name: LnnHbMediumMgrSendBegin + * @tc.desc: heartbeat medium manger send begin + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatMgrTest, LnnHbMediumMgrSendBegin_TEST01, TestSize.Level1) +{ + LnnHeartbeatCustSendData data = { + .hbType = HEARTBEAT_TYPE_MAX, + .wakeupFlag = false, + .isRelay = false, + }; + LnnHbMediumMgrInit(); + int32_t ret = LnnHbMediumMgrSendBegin(nullptr); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + ret = LnnHbMediumMgrSendBegin(&data); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* + * @tc.name: VisitHbMediumMgrSendEnd + * @tc.desc: visit heartbeat medium manger send end + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatMgrTest, VisitHbMediumMgrSendEnd_TEST01, TestSize.Level1) +{ + bool ret = VisitHbMediumMgrSendEnd(nullptr, HEARTBEAT_TYPE_MAX, nullptr); + EXPECT_FALSE(ret); + LnnHbMediumMgrInit(); + int id = LnnConvertHbTypeToId(HEARTBEAT_TYPE_BLE_V0); + g_hbMeidumMgr[id] = nullptr; + ret = VisitHbMediumMgrSendEnd(nullptr, HEARTBEAT_TYPE_BLE_V0, nullptr); + EXPECT_TRUE(ret); + LnnHbMediumMgrInit(); + g_hbMeidumMgr[id]->onSendOneHbEnd = nullptr; + ret = VisitHbMediumMgrSendEnd(nullptr, HEARTBEAT_TYPE_BLE_V0, nullptr); + EXPECT_TRUE(ret); +} + +/* + * @tc.name: VisitHbMediumMgrStop + * @tc.desc: visit heartbeat medium manger stop + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatMgrTest, VisitHbMediumMgrStop_TEST01, TestSize.Level1) +{ + LnnHeartbeatMediumMgr medMgr1 = { + .supportType = HEARTBEAT_TYPE_BLE_V1, + }; + bool ret = VisitHbMediumMgrStop(nullptr, HEARTBEAT_TYPE_MAX, nullptr); + EXPECT_FALSE(ret); + int id = LnnConvertHbTypeToId(HEARTBEAT_TYPE_BLE_V1); + g_hbMeidumMgr[id] = nullptr; + ret = VisitHbMediumMgrStop(nullptr, HEARTBEAT_TYPE_BLE_V1, nullptr); + EXPECT_TRUE(ret); + g_hbMeidumMgr[id] = &medMgr1; + ret = VisitHbMediumMgrStop(nullptr, HEARTBEAT_TYPE_BLE_V1, nullptr); + EXPECT_TRUE(ret); + LnnHeartbeatMediumMgr medMgr2 = { + .supportType = HEARTBEAT_TYPE_MAX, + }; + g_hbMeidumMgr[id] = &medMgr2; + ret = VisitHbMediumMgrStop(nullptr, HEARTBEAT_TYPE_BLE_V1, nullptr); + EXPECT_TRUE(ret); +} + +/* + * @tc.name: LnnHbMediumMgrSetParam + * @tc.desc: heartbeat medium manger set param + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatMgrTest, LnnHbMediumMgrSetParam_TEST01, TestSize.Level1) +{ + int id = LnnConvertHbTypeToId(HEARTBEAT_TYPE_BLE_V1); + int32_t ret = LnnHbMediumMgrSetParam(nullptr); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + LnnHeartbeatMediumParam param = { + .type = HEARTBEAT_TYPE_MAX, + }; + ret = LnnHbMediumMgrSetParam(¶m); + EXPECT_TRUE(ret == SOFTBUS_ERR); + param.type = HEARTBEAT_TYPE_BLE_V1; + g_hbMeidumMgr[id] = nullptr; + ret = LnnHbMediumMgrSetParam(¶m); + EXPECT_TRUE(ret == SOFTBUS_NOT_IMPLEMENT); +} + +/* + * @tc.name: LnnHbMediumMgrUpdateSendInfo + * @tc.desc: medium manger updata send info + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatMgrTest, LnnHbMediumMgrUpdateSendInfo_TEST01, TestSize.Level1) +{ + int id = LnnConvertHbTypeToId(HEARTBEAT_TYPE_BLE_V1); + int32_t ret = LnnHbMediumMgrUpdateSendInfo(UPDATE_HB_NETWORK_INFO); + EXPECT_TRUE(ret == SOFTBUS_ERR); + LnnHeartbeatMediumMgr medMgr1 = { + .supportType = HEARTBEAT_TYPE_BLE_V1, + }; + g_hbMeidumMgr[id] = &medMgr1; + ret = LnnHbMediumMgrUpdateSendInfo(UPDATE_HB_NETWORK_INFO); + EXPECT_TRUE(ret == SOFTBUS_ERR); + LnnHeartbeatMediumMgr medMgr2 = { + .supportType = HEARTBEAT_TYPE_BLE_V1, + .onUpdateSendInfo = onUpdateSendInfo1, + }; + g_hbMeidumMgr[id] = &medMgr2; + ret = LnnHbMediumMgrUpdateSendInfo(UPDATE_HB_NETWORK_INFO); + EXPECT_TRUE(ret == SOFTBUS_ERR); + medMgr2.onUpdateSendInfo = onUpdateSendInfo2; + ret = LnnHbMediumMgrUpdateSendInfo(UPDATE_HB_NETWORK_INFO); + EXPECT_TRUE(ret == SOFTBUS_ERR); + ret = LnnRegistHeartbeatMediumMgr(nullptr); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); +} + +/* + * @tc.name: VisitUnRegistHeartbeatMediumMgr + * @tc.desc: unregist heartbeat medium manger + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatMgrTest, VisitUnRegistHeartbeatMediumMgr_TEST01, TestSize.Level1) +{ + bool ret = VisitUnRegistHeartbeatMediumMgr(nullptr, HEARTBEAT_TYPE_BLE_V1, nullptr); + EXPECT_TRUE(ret); + ret = VisitUnRegistHeartbeatMediumMgr(nullptr, HEARTBEAT_TYPE_MAX, nullptr); + EXPECT_FALSE(ret); +} + +void DeInit(void) { } +/* + * @tc.name: LnnUnRegistHeartbeatMediumMgr + * @tc.desc: unregist heartbeat medium manger + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HeartBeatMgrTest, UnRegistHeartbeatMediumMgr_TEST01, TestSize.Level1) +{ + int32_t ret = LnnUnRegistHeartbeatMediumMgr(nullptr); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + LnnHeartbeatMediumMgr mgr = { + .supportType = HEARTBEAT_TYPE_BLE_V1, + .deinit = DeInit, + }; + ret = LnnUnRegistHeartbeatMediumMgr(&mgr); + EXPECT_TRUE(ret == SOFTBUS_OK); + mgr.supportType = HEARTBEAT_TYPE_MAX; + ret = LnnUnRegistHeartbeatMediumMgr(&mgr); + EXPECT_TRUE(ret == SOFTBUS_ERR); + mgr.supportType = HEARTBEAT_TYPE_BLE_V1; + mgr.deinit = nullptr; + ret = LnnUnRegistHeartbeatMediumMgr(&mgr); + EXPECT_TRUE(ret == SOFTBUS_OK); +} +} // namespace OHOS \ No newline at end of file diff --git a/tests/core/bus_center/test/mock/include/hb_fsm_mock.h b/tests/core/bus_center/test/mock/include/hb_fsm_mock.h new file mode 100644 index 0000000000..1b15f81520 --- /dev/null +++ b/tests/core/bus_center/test/mock/include/hb_fsm_mock.h @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2022 Huawei Device 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. + */ + +#ifndef HEARTBEAT_FSM_H +#define HEARTBEAT_FSM_H + +#include "lnn_connection_addr_utils.h" +#include "lnn_heartbeat_ctrl.h" +#include "lnn_heartbeat_medium_mgr.h" +#include "lnn_heartbeat_strategy.h" +#include "lnn_net_builder.h" +#include +#include + +namespace OHOS { +class HeartBeatFSMInterface { +public: + HeartBeatFSMInterface() {}; + virtual ~HeartBeatFSMInterface() {}; + + virtual int32_t LnnStartOfflineTimingStrategy(const char *networkId, ConnectionAddrType addrType) = 0; + virtual int32_t LnnStopOfflineTimingStrategy(const char *networkId, ConnectionAddrType addrType) = 0; + virtual int32_t LnnNotifyDiscoveryDevice(const ConnectionAddr *addr) = 0; + virtual int32_t LnnSetHbAsMasterNodeState(bool isMasterNode) = 0; + virtual int32_t LnnNotifyMasterElect(const char *networkId, const char *masterUdid, int32_t masterWeight) = 0; + virtual int32_t LnnStartHbByTypeAndStrategy( + LnnHeartbeatType hbType, LnnHeartbeatStrategyType strategyType, bool isRelay) = 0; + virtual int32_t LnnHbMediumMgrStop(LnnHeartbeatType *type) = 0; + virtual void LnnDumpHbMgrRecvList(void) = 0; + virtual void LnnDumpHbOnlineNodeList(void) = 0; + virtual bool LnnIsHeartbeatEnable(LnnHeartbeatType type) = 0; + virtual int32_t LnnGetGearModeBySpecificType(GearMode *mode, LnnHeartbeatType type) = 0; + virtual DiscoveryType LnnConvAddrTypeToDiscType(ConnectionAddrType type) = 0; + virtual int32_t LnnOfflineTimingByHeartbeat(const char *networkId, ConnectionAddrType addrType) = 0; + virtual int32_t LnnRequestLeaveSpecific(const char *networkId, ConnectionAddrType addrType) = 0; + virtual int32_t LnnHbMediumMgrSendBegin(LnnHeartbeatCustSendData *custData) = 0; + virtual int32_t LnnHbMediumMgrSendEnd(LnnHeartbeatType *type) = 0; + virtual int32_t LnnGetHbStrategyManager( + LnnHeartbeatStrategyManager *mgr, LnnHeartbeatType hbType, LnnHeartbeatStrategyType strategyType) = 0; + virtual int32_t LnnHbMediumMgrSetParam(const LnnHeartbeatMediumParam *param) = 0; + virtual int32_t LnnHbMediumMgrUpdateSendInfo(LnnHeartbeatUpdateInfoType type) = 0; +}; +class HeartBeatFSMInterfaceMock : public HeartBeatFSMInterface { +public: + HeartBeatFSMInterfaceMock(); + ~HeartBeatFSMInterfaceMock() override; + + MOCK_METHOD2(LnnStartOfflineTimingStrategy, int32_t(const char *, ConnectionAddrType)); + MOCK_METHOD2(LnnStopOfflineTimingStrategy, int32_t(const char *, ConnectionAddrType)); + MOCK_METHOD1(LnnNotifyDiscoveryDevice, int32_t(const ConnectionAddr *)); + MOCK_METHOD3(LnnNotifyMasterElect, int32_t(const char *, const char *, int32_t)); + MOCK_METHOD1(LnnSetHbAsMasterNodeState, int32_t(bool)); + MOCK_METHOD3(LnnStartHbByTypeAndStrategy, int32_t(LnnHeartbeatType, LnnHeartbeatStrategyType, bool)); + MOCK_METHOD1(LnnHbMediumMgrStop, int32_t(LnnHeartbeatType *)); + MOCK_METHOD0(LnnDumpHbMgrRecvList, void(void)); + MOCK_METHOD0(LnnDumpHbOnlineNodeList, void(void)); + MOCK_METHOD1(LnnIsHeartbeatEnable, bool(LnnHeartbeatType)); + MOCK_METHOD2(LnnGetGearModeBySpecificType, int32_t(GearMode *, LnnHeartbeatType)); + MOCK_METHOD1(LnnConvAddrTypeToDiscType, DiscoveryType(ConnectionAddrType)); + MOCK_METHOD2(LnnOfflineTimingByHeartbeat, int32_t(const char *, ConnectionAddrType)); + MOCK_METHOD2(LnnRequestLeaveSpecific, int32_t(const char *, ConnectionAddrType)); + MOCK_METHOD1(LnnHbMediumMgrSendBegin, int32_t(LnnHeartbeatCustSendData *)); + MOCK_METHOD1(LnnHbMediumMgrSendEnd, int32_t(LnnHeartbeatType *)); + MOCK_METHOD3( + LnnGetHbStrategyManager, int32_t(LnnHeartbeatStrategyManager *, LnnHeartbeatType, LnnHeartbeatStrategyType)); + MOCK_METHOD1(LnnHbMediumMgrSetParam, int32_t(const LnnHeartbeatMediumParam *)); + MOCK_METHOD1(LnnHbMediumMgrUpdateSendInfo, int32_t(LnnHeartbeatUpdateInfoType)); +}; +} // namespace OHOS +#endif // AUTH_CONNECTION_MOCK_H \ No newline at end of file diff --git a/tests/core/bus_center/test/mock/include/hb_strategy_mock.h b/tests/core/bus_center/test/mock/include/hb_strategy_mock.h new file mode 100644 index 0000000000..eaa9e08ffd --- /dev/null +++ b/tests/core/bus_center/test/mock/include/hb_strategy_mock.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2022 Huawei Device 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. + */ + +#ifndef HEARTBEAT_STRATEGY_H +#define HEARTBEAT_STRATEGY_H + +#include "lnn_heartbeat_strategy.h" +#include "lnn_net_builder.h" +#include +#include + +namespace OHOS { +class HeartBeatStategyInterface { +public: + HeartBeatStategyInterface() {}; + virtual ~HeartBeatStategyInterface() {}; + + virtual int32_t LnnStartOfflineTimingStrategy(const char *networkId, ConnectionAddrType addrType) = 0; + virtual int32_t LnnStopOfflineTimingStrategy(const char *networkId, ConnectionAddrType addrType) = 0; + virtual int32_t LnnNotifyDiscoveryDevice(const ConnectionAddr *addr) = 0; + virtual int32_t LnnSetHbAsMasterNodeState(bool isMasterNode) = 0; + virtual int32_t LnnNotifyMasterElect(const char *networkId, const char *masterUdid, int32_t masterWeight) = 0; + virtual int32_t LnnStartHbByTypeAndStrategy( + LnnHeartbeatType hbType, LnnHeartbeatStrategyType strategyType, bool isRelay) = 0; +}; +class HeartBeatStategyInterfaceMock : public HeartBeatStategyInterface { +public: + HeartBeatStategyInterfaceMock(); + ~HeartBeatStategyInterfaceMock() override; + + MOCK_METHOD2(LnnStartOfflineTimingStrategy, int32_t(const char *, ConnectionAddrType)); + MOCK_METHOD2(LnnStopOfflineTimingStrategy, int32_t(const char *, ConnectionAddrType)); + MOCK_METHOD1(LnnNotifyDiscoveryDevice, int32_t(const ConnectionAddr *)); + MOCK_METHOD3(LnnNotifyMasterElect, int32_t(const char *, const char *, int32_t)); + MOCK_METHOD1(LnnSetHbAsMasterNodeState, int32_t(bool)); + MOCK_METHOD3(LnnStartHbByTypeAndStrategy, int32_t(LnnHeartbeatType, LnnHeartbeatStrategyType, bool)); +}; +} // namespace OHOS +#endif // AUTH_CONNECTION_MOCK_H \ No newline at end of file diff --git a/tests/core/bus_center/test/mock/src/hb_fsm_mock.cpp b/tests/core/bus_center/test/mock/src/hb_fsm_mock.cpp new file mode 100644 index 0000000000..f059789fb9 --- /dev/null +++ b/tests/core/bus_center/test/mock/src/hb_fsm_mock.cpp @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2022 Huawei Device 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 "hb_fsm_mock.h" +#include "softbus_error_code.h" + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +void *g_hbFSMInterface; +HeartBeatFSMInterfaceMock::HeartBeatFSMInterfaceMock() +{ + g_hbFSMInterface = reinterpret_cast(this); +} + +HeartBeatFSMInterfaceMock::~HeartBeatFSMInterfaceMock() +{ + g_hbFSMInterface = nullptr; +} + +static HeartBeatFSMInterface *HeartBeatFSMInterfaceInstance() +{ + return reinterpret_cast(g_hbFSMInterface); +} + +extern "C" { +int32_t LnnStartOfflineTimingStrategy(const char *networkId, ConnectionAddrType addrType) +{ + return HeartBeatFSMInterfaceInstance()->LnnStartOfflineTimingStrategy(networkId, addrType); +} + +int32_t LnnStopOfflineTimingStrategy(const char *networkId, ConnectionAddrType addrType) +{ + return HeartBeatFSMInterfaceInstance()->LnnStopOfflineTimingStrategy(networkId, addrType); +} + +int32_t LnnNotifyDiscoveryDevice(const ConnectionAddr *addr) +{ + return HeartBeatFSMInterfaceInstance()->LnnNotifyDiscoveryDevice(addr); +} + +int32_t LnnNotifyMasterElect(const char *networkId, const char *masterUdid, int32_t masterWeight) +{ + return HeartBeatFSMInterfaceInstance()->LnnNotifyMasterElect(networkId, masterUdid, masterWeight); +} + +int32_t LnnSetHbAsMasterNodeState(bool isMasterNode) +{ + return HeartBeatFSMInterfaceInstance()->LnnSetHbAsMasterNodeState(isMasterNode); +} + +int32_t LnnStartHbByTypeAndStrategy(LnnHeartbeatType hbType, LnnHeartbeatStrategyType strategyType, bool isRelay) +{ + return HeartBeatFSMInterfaceInstance()->LnnStartHbByTypeAndStrategy(hbType, strategyType, isRelay); +} + +int32_t LnnHbMediumMgrStop(LnnHeartbeatType *type) +{ + return HeartBeatFSMInterfaceInstance()->LnnHbMediumMgrStop(type); +} + +void LnnDumpHbMgrRecvList(void) +{ + return HeartBeatFSMInterfaceInstance()->LnnDumpHbMgrRecvList(); +} + +void LnnDumpHbOnlineNodeList(void) +{ + return HeartBeatFSMInterfaceInstance()->LnnDumpHbOnlineNodeList(); +} +bool LnnIsHeartbeatEnable(LnnHeartbeatType type) +{ + return HeartBeatFSMInterfaceInstance()->LnnIsHeartbeatEnable(type); +} +int32_t LnnGetGearModeBySpecificType(GearMode *mode, LnnHeartbeatType type) +{ + return HeartBeatFSMInterfaceInstance()->LnnGetGearModeBySpecificType(mode, type); +} +DiscoveryType LnnConvAddrTypeToDiscType(ConnectionAddrType type) +{ + return HeartBeatFSMInterfaceInstance()->LnnConvAddrTypeToDiscType(type); +} +int32_t LnnOfflineTimingByHeartbeat(const char *networkId, ConnectionAddrType addrType) +{ + return HeartBeatFSMInterfaceInstance()->LnnOfflineTimingByHeartbeat(networkId, addrType); +} +int32_t LnnRequestLeaveSpecific(const char *networkId, ConnectionAddrType addrType) +{ + return HeartBeatFSMInterfaceInstance()->LnnRequestLeaveSpecific(networkId, addrType); +} +int32_t LnnHbMediumMgrSendBegin(LnnHeartbeatCustSendData *custData) +{ + return HeartBeatFSMInterfaceInstance()->LnnHbMediumMgrSendBegin(custData); +} +int32_t LnnHbMediumMgrSendEnd(LnnHeartbeatType *type) +{ + return HeartBeatFSMInterfaceInstance()->LnnHbMediumMgrSendEnd(type); +} +int32_t LnnGetHbStrategyManager( + LnnHeartbeatStrategyManager *mgr, LnnHeartbeatType hbType, LnnHeartbeatStrategyType strategyType) +{ + return HeartBeatFSMInterfaceInstance()->LnnGetHbStrategyManager(mgr, hbType, strategyType); +} +int32_t LnnHbMediumMgrSetParam(const LnnHeartbeatMediumParam *param) +{ + return HeartBeatFSMInterfaceInstance()->LnnHbMediumMgrSetParam(param); +} +int32_t LnnHbMediumMgrUpdateSendInfo(LnnHeartbeatUpdateInfoType type) +{ + return HeartBeatFSMInterfaceInstance()->LnnHbMediumMgrUpdateSendInfo(type); +} +} +} // namespace OHOS \ No newline at end of file diff --git a/tests/core/bus_center/test/mock/src/hb_strategy_mock.cpp b/tests/core/bus_center/test/mock/src/hb_strategy_mock.cpp new file mode 100644 index 0000000000..7c5d80cb88 --- /dev/null +++ b/tests/core/bus_center/test/mock/src/hb_strategy_mock.cpp @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2022 Huawei Device 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 "hb_strategy_mock.h" +#include "softbus_error_code.h" + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +void *g_hbStrategyInterface; +HeartBeatStategyInterfaceMock::HeartBeatStategyInterfaceMock() +{ + g_hbStrategyInterface = reinterpret_cast(this); +} + +HeartBeatStategyInterfaceMock::~HeartBeatStategyInterfaceMock() +{ + g_hbStrategyInterface = nullptr; +} + +static HeartBeatStategyInterface *HeartBeatStrategyInterface() +{ + return reinterpret_cast(g_hbStrategyInterface); +} + +extern "C" { +int32_t LnnStartOfflineTimingStrategy(const char *networkId, ConnectionAddrType addrType) +{ + return HeartBeatStrategyInterface()->LnnStartOfflineTimingStrategy(networkId, addrType); +} + +int32_t LnnStopOfflineTimingStrategy(const char *networkId, ConnectionAddrType addrType) +{ + return HeartBeatStrategyInterface()->LnnStopOfflineTimingStrategy(networkId, addrType); +} + +int32_t LnnNotifyDiscoveryDevice(const ConnectionAddr *addr) +{ + return HeartBeatStrategyInterface()->LnnNotifyDiscoveryDevice(addr); +} + +int32_t LnnNotifyMasterElect(const char *networkId, const char *masterUdid, int32_t masterWeight) +{ + return HeartBeatStrategyInterface()->LnnNotifyMasterElect(networkId, masterUdid, masterWeight); +} + +int32_t LnnSetHbAsMasterNodeState(bool isMasterNode) +{ + return HeartBeatStrategyInterface()->LnnSetHbAsMasterNodeState(isMasterNode); +} + +int32_t LnnStartHbByTypeAndStrategy(LnnHeartbeatType hbType, LnnHeartbeatStrategyType strategyType, bool isRelay) +{ + return HeartBeatStrategyInterface()->LnnStartHbByTypeAndStrategy(hbType, strategyType, isRelay); +} +} +} // namespace OHOS \ No newline at end of file -- Gitee