1 /*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17 #include <securec.h>
18
19 #include "common_list.h"
20 #include "bus_center_decision_center.h"
21 #include "bus_center_decision_center_deps_mock.h"
22 #include "message_handler.h"
23 #include "softbus_conn_interface.h"
24 #include "bus_center_manager.h"
25 #include "lnn_distributed_net_ledger.h"
26 #include "lnn_net_builder.h"
27 #include "softbus_adapter_mem.h"
28 #include "softbus_adapter_thread.h"
29 #include "softbus_error_code.h"
30 #include "softbus_utils.h"
31 #include "softbus_bus_center.h"
32 #include "bus_center_event.h"
33
34 using namespace testing;
35 using namespace testing::ext;
36 #define NETWORK_ID_BUF_LEN 65
37 #define NODE_NETWORK_ID "ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF0"
38
39 namespace OHOS {
40
41 class BusCenterDecisionTest : public testing::Test {
42 protected:
43 static void SetUpTestCase(void);
44 static void TearDownTestCase(void);
45 void SetUp();
46 void TearDown();
47 };
48
SetUpTestCase(void)49 void BusCenterDecisionTest::SetUpTestCase(void)
50 {
51 }
52
TearDownTestCase(void)53 void BusCenterDecisionTest::TearDownTestCase(void)
54 {
55 }
56
SetUp(void)57 void BusCenterDecisionTest::SetUp(void)
58 {
59 }
60
TearDown(void)61 void BusCenterDecisionTest::TearDown(void)
62 {
63 }
64
65 /*
66 * @tc.name: BusCenterDecisionTest001
67 * @tc.desc:
68 * @tc.type: FUNC
69 * @tc.require: 1
70 */
71 HWTEST_F(BusCenterDecisionTest, BusCenterDecisionTest001, TestSize.Level1)
72 {
73 ConnectOption option;
74 memset_s(&option, sizeof(ConnectOption), 0, sizeof(ConnectOption));
75 option.type = CONNECT_BR;
76 EXPECT_NO_FATAL_FAILURE(LnnDCReportConnectException(&option, 1));
77 option.type = CONNECT_P2P;
78 EXPECT_NO_FATAL_FAILURE(LnnDCReportConnectException(&option, 1));
79 }
80
81 /*
82 * @tc.name: BusCenterDecisionTest002
83 * @tc.desc:bus center decision test
84 * @tc.type: FUNC
85 * @tc.require: 1
86 */
87 HWTEST_F(BusCenterDecisionTest, BusCenterDecisionTest002, TestSize.Level1)
88 {
89 ConnectOption option;
90 memset_s(&option, sizeof(ConnectOption), 0, sizeof(ConnectOption));
91 option.type = CONNECT_BR;
92 EXPECT_NO_FATAL_FAILURE(LnnDCClearConnectException(&option));
93 option.type = CONNECT_P2P;
94 EXPECT_NO_FATAL_FAILURE(LnnDCClearConnectException(&option));
95 }
96
97 /*
98 * @tc.name: BusCenterDecisionTest003
99 * @tc.desc:bus center decision test
100 * @tc.type: FUNC
101 * @tc.require: 1
102 */
103 HWTEST_F(BusCenterDecisionTest, BusCenterDecisionTest003, TestSize.Level1)
104 {
105 NodeBasicInfo info;
106 (void)memset_s(&info, sizeof(info), 0, sizeof(info));
107 bool isOnline = true;
108 EXPECT_NO_FATAL_FAILURE(LnnDCProcessOnlineState(isOnline, &info));
109 isOnline = false;
110 EXPECT_NO_FATAL_FAILURE(LnnDCProcessOnlineState(isOnline, &info));
111 (void)strncpy_s(info.networkId, NETWORK_ID_BUF_LEN, NODE_NETWORK_ID, strlen(NODE_NETWORK_ID));
112 EXPECT_NO_FATAL_FAILURE(LnnDCProcessOnlineState(isOnline, &info));
113 }
114
115 /*
116 * @tc.name: BusCenterDecisionTest004
117 * @tc.desc:bus center decision test
118 * @tc.type: FUNC
119 * @tc.require: 1
120 */
121 HWTEST_F(BusCenterDecisionTest, BusCenterDecisionTest004, TestSize.Level1)
122 {
123 NiceMock<BusCenterDecisionCenterDepsInterfaceMock> BusCenterDecisionMock;
124 EXPECT_CALL(BusCenterDecisionMock, CreateSoftBusList).WillOnce(Return(nullptr));
125 int32_t ret = InitDecisionCenter();
126 EXPECT_NE(ret, SOFTBUS_OK);
127 }
128
129 /*
130 * @tc.name: BusCenterDecisionTest005
131 * @tc.desc:bus center decision test
132 * @tc.type: FUNC
133 * @tc.require: 1
134 */
135 HWTEST_F(BusCenterDecisionTest, BusCenterDecisionTest005, TestSize.Level1)
136 {
137 // list will free when go to TransSrvDataListDeinit
138 SoftBusList *list = (SoftBusList *)SoftBusCalloc(sizeof(SoftBusList));
139 SoftBusMutexAttr mutexAttr;
140 mutexAttr.type = SOFTBUS_MUTEX_RECURSIVE;
141 SoftBusMutexInit(&list->lock, &mutexAttr);
142 ListInit(&list->list);
143 NiceMock<BusCenterDecisionCenterDepsInterfaceMock> BusCenterDecisionMock;
144 EXPECT_CALL(BusCenterDecisionMock, CreateSoftBusList).WillOnce(Return(list));
145 int32_t ret = InitDecisionCenter();
146 EXPECT_EQ(SOFTBUS_OK, ret);
147 DeinitDecisionCenter();
148 }
149 }
150
151