1 /*
2 * Copyright (c) 2022 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 "bus_center_event.h"
20 #include "bus_center_manager.h"
21 #include "lnn_local_net_ledger.h"
22 #include "lnn_net_builder.h"
23 #include "message_handler.h"
24 #include "softbus_common.h"
25 #include "softbus_errcode.h"
26 #include "softbus_log.h"
27
28 constexpr char NETWORKID[] = "ABCDEFG";
29 constexpr char OLD_NETWORKID[] = "ABCDEFG";
30 constexpr char MASTER_UDID[] = "0123456";
31 constexpr uint16_t CONN_FSM_ID = 1;
32 constexpr int32_t MASTER_WEIGHT = 1;
33 constexpr uint32_t TYPE_LEN = 1;
34 constexpr char IP[IP_STR_MAX_LEN] = "127.0.0.1";
35 constexpr uint16_t PORT = 1000;
36 constexpr char PEERUID[MAX_ACCOUNT_HASH_LEN] = "021315ASD";
37
38 namespace OHOS {
39 using namespace testing::ext;
40
41 class LnnNetBuilderTest : public testing::Test {
42 public:
43 static void SetUpTestCase();
44 static void TearDownTestCase();
45 void SetUp();
46 void TearDown();
47 };
48
SetUpTestCase()49 void LnnNetBuilderTest::SetUpTestCase()
50 {
51 LooperInit();
52 }
53
TearDownTestCase()54 void LnnNetBuilderTest::TearDownTestCase()
55 {
56 LooperDeinit();
57 }
58
SetUp()59 void LnnNetBuilderTest::SetUp()
60 {
61 }
62
TearDown()63 void LnnNetBuilderTest::TearDown()
64 {
65 LnnDeinitNetBuilder();
66 }
67
68 /*
69 * @tc.name: LNN_NOTIFY_DISCOVERY_DEVICE_TEST_001
70 * @tc.desc: test LnnNotifyDiscoveryDevice
71 * @tc.type: FUNC
72 * @tc.require: I5PRUD
73 */
74 HWTEST_F(LnnNetBuilderTest, LNN_NOTIFY_DISCOVERY_DEVICE_TEST_001, TestSize.Level0)
75 {
76 ConnectionAddr target = {
77 .type = CONNECTION_ADDR_WLAN,
78 .info.ip.port = PORT
79 };
80 memcpy_s(target.peerUid, MAX_ACCOUNT_HASH_LEN, PEERUID, strlen(PEERUID));
81 memcpy_s(target.info.ip.ip, IP_STR_MAX_LEN, IP, strlen(IP));
82 int32_t ret = LnnNotifyDiscoveryDevice(&target);
83 EXPECT_TRUE(ret == SOFTBUS_ERR);
84 }
85
86 /*
87 * @tc.name: LNN_REQUEST_LEAVE_BY_ADDRTYPE_TEST_001
88 * @tc.desc: test LnnRequestLeaveByAddrType
89 * @tc.type: FUNC
90 * @tc.require: I5PRUD
91 */
92 HWTEST_F(LnnNetBuilderTest, LNN_REQUEST_LEAVE_BY_ADDRTYPE_TEST_001, TestSize.Level0)
93 {
94 const bool type[CONNECTION_ADDR_MAX] = {true, true, true, true, true};
95 int32_t ret = LnnRequestLeaveByAddrType(type, CONNECTION_ADDR_MAX);
96 EXPECT_TRUE(ret == SOFTBUS_ERR);
97 ret = LnnInitNetBuilder();
98 EXPECT_TRUE(ret == SOFTBUS_OK);
99 ret = LnnRequestLeaveByAddrType(type, TYPE_LEN);
100 EXPECT_TRUE(ret == SOFTBUS_ERR);
101 }
102
103 /*
104 * @tc.name: LNN_REQUEST_LEAVE_SPECIFIC_TEST_001
105 * @tc.desc: test LnnRequestLeaveSpecific
106 * @tc.type: FUNC
107 * @tc.require: I5PRUD
108 */
109 HWTEST_F(LnnNetBuilderTest, LNN_REQUEST_LEAVE_SPECIFIC_TEST_001, TestSize.Level0)
110 {
111 char *networkId = nullptr;
112 int32_t ret = LnnRequestLeaveSpecific(networkId, CONNECTION_ADDR_WLAN);
113 EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM);
114 ret = LnnRequestLeaveSpecific(NETWORKID, CONNECTION_ADDR_WLAN);
115 EXPECT_TRUE(ret == SOFTBUS_NO_INIT);
116 ret = LnnInitNetBuilder();
117 EXPECT_TRUE(ret == SOFTBUS_OK);
118 ret = LnnRequestLeaveSpecific(NETWORKID, CONNECTION_ADDR_WLAN);
119 EXPECT_TRUE(ret == SOFTBUS_OK);
120 }
121
122 /*
123 * @tc.name: LNN_REQUEST_LEAVE_INVALID_CONN_TEST_001
124 * @tc.desc: test LnnRequestLeaveInvalidConn
125 * @tc.type: FUNC
126 * @tc.require: I5PRUD
127 */
128 HWTEST_F(LnnNetBuilderTest, LNN_REQUEST_LEAVE_INVALID_CONN_TEST_001, TestSize.Level0)
129 {
130 int32_t ret = LnnRequestLeaveInvalidConn(OLD_NETWORKID, CONNECTION_ADDR_WLAN, NETWORKID);
131 EXPECT_TRUE(ret == SOFTBUS_ERR);
132 ret = LnnInitNetBuilder();
133 EXPECT_TRUE(ret == SOFTBUS_OK);
134 ret = LnnRequestLeaveInvalidConn(OLD_NETWORKID, CONNECTION_ADDR_WLAN, NETWORKID);
135 EXPECT_TRUE(ret == SOFTBUS_OK);
136 }
137
138 /*
139 * @tc.name: LNN_REQUEST_CLEAN_CONN_FSM_TEST_001
140 * @tc.desc: test LnnRequestCleanConnFsm
141 * @tc.type: FUNC
142 * @tc.require: I5PRUD
143 */
144 HWTEST_F(LnnNetBuilderTest, LNN_REQUEST_CLEAN_CONN_FSM_TEST_001, TestSize.Level0)
145 {
146 int32_t ret = LnnRequestCleanConnFsm(CONN_FSM_ID);
147 EXPECT_TRUE(ret == SOFTBUS_ERR);
148 ret = LnnInitNetBuilder();
149 EXPECT_TRUE(ret == SOFTBUS_OK);
150 ret = LnnRequestCleanConnFsm(CONN_FSM_ID);
151 EXPECT_TRUE(ret == SOFTBUS_OK);
152 }
153
154 /*
155 * @tc.name: LNN_NOTIFY_NODE_STATE_CHANGED_TEST_001
156 * @tc.desc: test LnnNotifyNodeStateChanged
157 * @tc.type: FUNC
158 * @tc.require: I5PRUD
159 */
160 HWTEST_F(LnnNetBuilderTest, LNN_NOTIFY_NODE_STATE_CHANGED_TEST_001, TestSize.Level0)
161 {
162 ConnectionAddr target = {
163 .type = CONNECTION_ADDR_WLAN,
164 .info.ip.port = PORT
165 };
166 memcpy_s(target.peerUid, MAX_ACCOUNT_HASH_LEN, PEERUID, strlen(PEERUID));
167 memcpy_s(target.info.ip.ip, IP_STR_MAX_LEN, IP, strlen(IP));
168 int32_t ret = LnnNotifyNodeStateChanged(&target);
169 EXPECT_TRUE(ret == SOFTBUS_ERR);
170 ret = LnnInitNetBuilder();
171 EXPECT_TRUE(ret == SOFTBUS_OK);
172 ret = LnnNotifyNodeStateChanged(&target);
173 EXPECT_TRUE(ret == SOFTBUS_OK);
174 }
175
176 /*
177 * @tc.name: LNN_NOTIFY_MASTER_ELECT_TEST_001
178 * @tc.desc: test LnnNotifyMasterElect
179 * @tc.type: FUNC
180 * @tc.require: I5PRUD
181 */
182 HWTEST_F(LnnNetBuilderTest, LNN_NOTIFY_MASTER_ELECT_TEST_001, TestSize.Level0)
183 {
184 int32_t ret = LnnNotifyMasterElect(NETWORKID, MASTER_UDID, MASTER_WEIGHT);
185 EXPECT_TRUE(ret == SOFTBUS_ERR);
186 ret = LnnInitNetBuilder();
187 EXPECT_TRUE(ret == SOFTBUS_OK);
188 char *networkId = nullptr;
189 ret = LnnNotifyMasterElect(networkId, MASTER_UDID, MASTER_WEIGHT);
190 EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM);
191 ret = LnnNotifyMasterElect(NETWORKID, MASTER_UDID, MASTER_WEIGHT);
192 EXPECT_TRUE(ret == SOFTBUS_OK);
193 }
194
195 /*
196 * @tc.name: LNN_UPDATE_NODE_ADDR_TEST_001
197 * @tc.desc: test LnnUpdateNodeAddr
198 * @tc.type: FUNC
199 * @tc.require: I5PRUD
200 */
201 HWTEST_F(LnnNetBuilderTest, LNN_UPDATE_NODE_ADDR_TEST_001, TestSize.Level0)
202 {
203 char *addr = nullptr;
204 int32_t ret = LnnUpdateNodeAddr(addr);
205 EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM);
206 ret = LnnInitNetBuilder();
207 EXPECT_TRUE(ret == SOFTBUS_OK);
208 ret = LnnUpdateNodeAddr(MASTER_UDID);
209 EXPECT_TRUE(ret == SOFTBUS_ERR);
210 ret = LnnInitLocalLedger();
211 EXPECT_TRUE(ret == SOFTBUS_OK);
212 ret = LnnUpdateNodeAddr(MASTER_UDID);
213 EXPECT_TRUE(ret == SOFTBUS_OK);
214 }
215
216 /*
217 * @tc.name: META_NODE_SERVER_JOIN_TEST_001
218 * @tc.desc: test MetaNodeServerJoin
219 * @tc.type: FUNC
220 * @tc.require:
221 */
222 HWTEST_F(LnnNetBuilderTest, META_NODE_SERVER_JOIN_TEST_001, TestSize.Level0)
223 {
224 ConnectionAddr addr = {
225 .type = CONNECTION_ADDR_WLAN,
226 .info.ip.port = PORT
227 };
228 CustomData customData = {
229 .type = PROXY_TRANSMISION,
230 .data = {0},
231 };
232 int32_t ret;
233
234 (void)memset_s(&addr, sizeof(ConnectionAddr), 0, sizeof(ConnectionAddr));
235 ret = LnnInitNetBuilder();
236 EXPECT_TRUE(ret == SOFTBUS_OK);
237 ret = MetaNodeServerJoin(&addr, &customData);
238 EXPECT_TRUE(ret == SOFTBUS_OK);
239 }
240
241 /*
242 * @tc.name: META_NODE_SERVER_LEAVE_TEST_001
243 * @tc.desc: test MetaNodeServerLeave
244 * @tc.type: FUNC
245 * @tc.require:
246 */
247 HWTEST_F(LnnNetBuilderTest, META_NODE_SERVER_LEAVE_TEST_001, TestSize.Level0)
248 {
249 const char *networkId = "1234";
250 int32_t ret;
251
252 ret = MetaNodeServerLeave(networkId);
253 EXPECT_TRUE(ret == SOFTBUS_NO_INIT);
254 ret = LnnInitNetBuilder();
255 EXPECT_TRUE(ret == SOFTBUS_OK);
256 ret = MetaNodeServerLeave(networkId);
257 EXPECT_TRUE(ret == SOFTBUS_OK);
258 }
259 } // namespace OHOS
260