• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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 <cstddef>
17 #include <cstdlib>
18 #include <cstring>
19 #include <gtest/gtest.h>
20 #include <securec.h>
21 
22 #include "auth_interface.h"
23 #include "bus_center_manager.h"
24 #include "lnn_decision_db.c"
25 #include "lnn_decision_db.h"
26 #include "lnn_distributed_net_ledger.h"
27 #include "lnn_event_monitor.h"
28 #include "lnn_local_net_ledger.h"
29 #include "lnn_log.h"
30 #include "lnn_network_manager.h"
31 #include "lnn_node_info.h"
32 
33 #include "softbus_adapter_mem.h"
34 #include "softbus_conn_interface.h"
35 #include "softbus_error_code.h"
36 #include "softbus_utils.h"
37 
38 namespace OHOS {
39 using namespace testing::ext;
40 constexpr uint32_t TEST_DATA_LEN = 10;
41 constexpr uint8_t DEFAULT_SIZE = 5;
42 constexpr char NODE1_UDID[] = "123456ABCDEF";
43 constexpr char NODE2_UDID[] = "123456ABCDEG";
44 
45 class NetLedgerTest : public testing::Test {
46 public:
47     static void SetUpTestCase();
48     static void TearDownTestCase();
49     void SetUp();
50     void TearDown();
51 };
52 
SetUpTestCase()53 void NetLedgerTest::SetUpTestCase() { }
54 
TearDownTestCase()55 void NetLedgerTest::TearDownTestCase() { }
56 
SetUp()57 void NetLedgerTest::SetUp()
58 {
59     int32_t ret = LnnInitLocalLedger();
60     EXPECT_TRUE(ret == SOFTBUS_OK);
61     ret = LnnInitDistributedLedger();
62     EXPECT_TRUE(ret == SOFTBUS_OK);
63     LNN_LOGI(LNN_TEST, "NetLedgerTest start");
64 }
65 
TearDown()66 void NetLedgerTest::TearDown()
67 {
68     LnnDeinitLocalLedger();
69     LnnDeinitDistributedLedger();
70 }
71 
72 /*
73  * @tc.name: AUTH_TYPE_VALUE_SET_CLEAR_Test_001
74  * @tc.desc: auth type value set and clear test
75  * @tc.type: FUNC
76  * @tc.require:
77  */
78 HWTEST_F(NetLedgerTest, AUTH_TYPE_VALUE_SET_CLEAR_Test_001, TestSize.Level1)
79 {
80     int32_t ret;
81     uint32_t authType = 0;
82     uint32_t *authTypeValue = nullptr;
83 
84     ret = LnnSetAuthTypeValue(authTypeValue, ONLINE_HICHAIN);
85     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
86     ret = LnnClearAuthTypeValue(authTypeValue, ONLINE_HICHAIN);
87     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
88 
89     authTypeValue = &authType;
90     ret = LnnSetAuthTypeValue(authTypeValue, AUTH_TYPE_BUTT);
91     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
92     ret = LnnClearAuthTypeValue(authTypeValue, AUTH_TYPE_BUTT);
93     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
94 
95     ret = LnnSetAuthTypeValue(authTypeValue, ONLINE_METANODE);
96     EXPECT_EQ(ret, SOFTBUS_OK);
97     ret = LnnClearAuthTypeValue(authTypeValue, ONLINE_HICHAIN);
98     EXPECT_EQ(ret, SOFTBUS_OK);
99 }
100 
101 /*
102  * @tc.name: LNN_GET_TRUSTED_DEV_INFO_FROM_DB_Test_001
103  * @tc.desc: lnn get trusted dev info from db test
104  * @tc.type: FUNC
105  * @tc.require:
106  */
107 HWTEST_F(NetLedgerTest, LNN_GET_TRUSTED_DEV_INFO_FROM_DB_Test_001, TestSize.Level1)
108 {
109     uint32_t num = 0;
110     int32_t ret;
111 
112     char *udidArray = new char[TEST_DATA_LEN];
113     ASSERT_NE(udidArray, nullptr);
114     ret = LnnGetTrustedDevInfoFromDb(&udidArray, &num);
115     EXPECT_EQ(ret, SOFTBUS_OK);
116     delete[] udidArray;
117 }
118 
119 /*
120  * @tc.name: DL_GET_Test_001
121  * @tc.desc: Dl Get Auth test
122  * @tc.type: FUNC
123  * @tc.require:
124  */
125 HWTEST_F(NetLedgerTest, DL_GET_Test_001, TestSize.Level1)
126 {
127     char networkId[DEFAULT_SIZE] = "1234";
128     int32_t info = 1234;
129     EXPECT_TRUE(LnnGetRemoteNumInfoByIfnameIdx(nullptr, NUM_KEY_SESSION_PORT, &info, WLAN_IF) == SOFTBUS_INVALID_PARAM);
130     EXPECT_TRUE(LnnGetRemoteNumInfo(networkId, STRING_KEY_END, &info) == SOFTBUS_INVALID_PARAM);
131     EXPECT_TRUE(LnnGetRemoteNumInfo(networkId, NUM_KEY_END, &info) == SOFTBUS_INVALID_PARAM);
132     EXPECT_TRUE(LnnGetRemoteNumInfo(networkId, NUM_KEY_END, nullptr) == SOFTBUS_INVALID_PARAM);
133     EXPECT_TRUE(LnnGetRemoteNumInfoByIfnameIdx(networkId, NUM_KEY_AUTH_PORT, &info, WLAN_IF) != SOFTBUS_OK);
134 }
135 
136 /*
137  * @tc.name: LNN_ADD_META_INFO_Test_001
138  * @tc.desc: Lnn Add Meta Info test
139  * @tc.type: FUNC
140  * @tc.require:
141  */
142 HWTEST_F(NetLedgerTest, LNN_ADD_META_INFO_Test_001, TestSize.Level1)
143 {
144     NodeInfo info;
145     (void)memset_s(&info, sizeof(NodeInfo), 0, sizeof(NodeInfo));
146     EXPECT_TRUE(LnnAddMetaInfo(&info) == SOFTBUS_OK);
147 }
148 
149 /*
150  * @tc.name: LNN_DELETE_META_INFO_Test_001
151  * @tc.desc: Lnn Delete Meta Info test
152  * @tc.type: FUNC
153  * @tc.require:
154  */
155 HWTEST_F(NetLedgerTest, LNN_DELETE_META_INFO_Test_001, TestSize.Level1)
156 {
157     char udid[DEFAULT_SIZE] = "1234";
158     AuthLinkType type = AUTH_LINK_TYPE_WIFI;
159     EXPECT_TRUE(LnnDeleteMetaInfo(udid, type) != SOFTBUS_OK);
160 }
161 
162 /*
163  * @tc.name: GET_ALL_ONLINE_AND_META_NODE_INFO_Test_001
164  * @tc.desc: Get All Online And Meta Node Info test
165  * @tc.type: FUNC
166  * @tc.require:
167  */
168 HWTEST_F(NetLedgerTest, GET_ALL_ONLINE_AND_META_NODE_INFO_Test_001, TestSize.Level1)
169 {
170     NodeBasicInfo base;
171     NodeBasicInfo *info = nullptr;
172     int32_t infoNum = 0;
173     EXPECT_TRUE(LnnGetAllOnlineAndMetaNodeInfo(nullptr, &infoNum) == SOFTBUS_INVALID_PARAM);
174     info = &base;
175     (void)memset_s(info, sizeof(NodeBasicInfo), 0, sizeof(NodeBasicInfo));
176     EXPECT_TRUE(LnnGetAllOnlineAndMetaNodeInfo(&info, nullptr) == SOFTBUS_INVALID_PARAM);
177     EXPECT_TRUE(LnnGetAllOnlineAndMetaNodeInfo(&info, &infoNum) == SOFTBUS_OK);
178     SoftBusFree(info);
179     info = nullptr;
180     infoNum = DEFAULT_SIZE;
181     EXPECT_TRUE(LnnGetAllOnlineAndMetaNodeInfo(&info, &infoNum) == SOFTBUS_OK);
182     SoftBusFree(info);
183 }
184 
185 /*
186  * @tc.name: LNN_META_INFO_ADD_DEL_Test_001
187  * @tc.desc: lnn add and del meta info test
188  * @tc.type: FUNC
189  * @tc.require:
190  */
191 HWTEST_F(NetLedgerTest, LNN_META_INFO_ADD_DEL_Test_001, TestSize.Level1)
192 {
193     int32_t ret;
194     NodeInfo info;
195 
196     ret = LnnDeleteMetaInfo(NODE2_UDID, AUTH_LINK_TYPE_WIFI);
197     EXPECT_NE(ret, SOFTBUS_OK);
198     (void)memset_s(&info, sizeof(NodeInfo), 0, sizeof(NodeInfo));
199     LnnSetDeviceUdid(&info, NODE1_UDID);
200     info.metaInfo.metaDiscType = AUTH_LINK_TYPE_WIFI;
201     ret = LnnAddMetaInfo(&info);
202     EXPECT_EQ(ret, SOFTBUS_OK);
203     ret = LnnDeleteMetaInfo(NODE1_UDID, AUTH_LINK_TYPE_WIFI);
204     EXPECT_EQ(ret, SOFTBUS_OK);
205     ret = LnnDeleteMetaInfo(NODE1_UDID, AUTH_LINK_TYPE_WIFI);
206     EXPECT_EQ(ret, SOFTBUS_OK);
207     ret = LnnDeleteMetaInfo(NODE1_UDID, AUTH_LINK_TYPE_WIFI);
208     EXPECT_EQ(ret, SOFTBUS_OK);
209 }
210 
211 /*
212  * @tc.name: LNN_GET_REMOTE_NUM16_INFO_Test_001
213  * @tc.desc: lnn get remote num16 info test
214  * @tc.type: FUNC
215  * @tc.require:
216  */
217 HWTEST_F(NetLedgerTest, LNN_GET_REMOTE_NUM16_INFO_Test_001, TestSize.Level1)
218 {
219     int32_t ret;
220     int16_t info1 = 0;
221     int16_t *info2 = nullptr;
222     constexpr char *networkId = nullptr;
223 
224     ret = LnnGetRemoteNum16Info(NODE1_UDID, NUM_KEY_META_NODE, &info1);
225     EXPECT_NE(ret, SOFTBUS_INVALID_PARAM);
226     ret = LnnGetRemoteNum16Info(networkId, NUM_KEY_META_NODE, &info1);
227     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
228     ret = LnnGetRemoteNum16Info(networkId, NUM_KEY_META_NODE, info2);
229     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
230     ret = LnnGetRemoteNum16Info(NODE1_UDID, STRING_KEY_BEGIN, &info1);
231     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
232     ret = LnnGetRemoteNum16Info(NODE1_UDID, BYTE_KEY_END, &info1);
233     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
234 }
235 
236 /*
237  * @tc.name: LNN_GET_ONLINE_AND_OFFLINE_WITHIN_TIME_UDIDS_Test_001
238  * @tc.desc: lnn get remote udids info test
239  * @tc.type: FUNC
240  * @tc.require:
241  */
242 HWTEST_F(NetLedgerTest, LNN_GET_ONLINE_AND_OFFLINE_WITHIN_TIME_UDIDS_Test_001, TestSize.Level1)
243 {
244     char *udids = nullptr;
245     int32_t udidNum = 0;
246     EXPECT_EQ(LnnGetOnlineAndOfflineWithinTimeUdids(nullptr, &udidNum, 0), SOFTBUS_INVALID_PARAM);
247     EXPECT_EQ(LnnGetOnlineAndOfflineWithinTimeUdids(&udids, nullptr, 0), SOFTBUS_INVALID_PARAM);
248     EXPECT_EQ(LnnGetOnlineAndOfflineWithinTimeUdids(&udids, &udidNum, 0), SOFTBUS_OK);
249     SoftBusFree(udids);
250     udids = nullptr;
251     udidNum = DEFAULT_SIZE;
252     EXPECT_EQ(LnnGetOnlineAndOfflineWithinTimeUdids(&udids, &udidNum, 0), SOFTBUS_OK);
253     SoftBusFree(udids);
254 }
255 
256 /*
257  * @tc.name: LNN_FIND_DEVICE_UDIDT_RUSTED_INFO_FROMDB_Test_001
258  * @tc.desc: Param is illegal and legal
259  * @tc.type: FUNC
260  * @tc.require:
261  */
262 HWTEST_F(NetLedgerTest, LNN_FIND_DEVICE_UDIDT_RUSTED_INFO_FROMDB_Test_001, TestSize.Level1)
263 {
264     int32_t ret;
265     constexpr char *strUdid = nullptr;
266     ret = LnnFindDeviceUdidTrustedInfoFromDb(strUdid);
267     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
268     ret = LnnFindDeviceUdidTrustedInfoFromDb(NODE1_UDID);
269     EXPECT_EQ(ret, SOFTBUS_NOT_FIND);
270 }
271 
272 /*
273  * @tc.name: LNN_INIT_DECISION_DB_DELAY_Test_001
274  * @tc.desc: Lnn init decision db delay fail
275  * @tc.type: FUNC
276  * @tc.require:
277  */
278 HWTEST_F(NetLedgerTest, LNN_INIT_DECISION_DB_DELAY_Test_001, TestSize.Level1)
279 {
280     int32_t ret;
281     bool retVal = DeviceDbRecoveryInit();
282     EXPECT_TRUE(retVal);
283     ret = LnnInitDecisionDbDelay();
284     EXPECT_EQ(ret, SOFTBUS_GENERATE_KEY_FAIL);
285 }
286 
287 /*
288  * @tc.name: LNN_CHECK_GENERATE_SOFTBUS_KEY_BY_HUKS_Test_001
289  * @tc.desc: Lnn check generate softbus key by huks is fail
290  * @tc.type: FUNC
291  * @tc.require:
292  */
293 HWTEST_F(NetLedgerTest, LNN_CHECK_GENERATE_SOFTBUS_KEY_BY_HUKS_Test_001, TestSize.Level1)
294 {
295     int32_t ret;
296     ret = LnnCheckGenerateSoftBusKeyByHuks();
297     EXPECT_EQ(ret, SOFTBUS_GENERATE_KEY_FAIL);
298 }
299 
300 /*
301  * @tc.name: LNN_IS_POTENTIAL_HOME_GROUP_Test_001
302  * @tc.desc: Lnn is potential home group false
303  * @tc.type: FUNC
304  * @tc.require:
305  */
306 HWTEST_F(NetLedgerTest, LNN_IS_POTENTIAL_HOME_GROUP_Test_001, TestSize.Level1)
307 {
308     bool ret;
309     ret = LnnIsPotentialHomeGroup(NODE1_UDID);
310     EXPECT_FALSE(ret);
311 }
312 
313 /*
314  * @tc.name: IS_DEVICE_TRUSTED_Test_001
315  * @tc.desc: Device trusted test is false
316  * @tc.type: FUNC
317  * @tc.require:
318  */
319 HWTEST_F(NetLedgerTest, IS_DEVICE_TRUSTED_Test_001, TestSize.Level1)
320 {
321     bool ret;
322     int32_t userId = 12345;
323     const char udid[] = "";
324     ret = IsDeviceTrusted(udid, userId);
325     EXPECT_FALSE(ret);
326     ret = IsDeviceTrusted(NODE1_UDID, userId);
327     EXPECT_FALSE(ret);
328 }
329 
330 /*
331  * @tc.name: TRY_RECOVERY_TRUST_DEVINFOTABLE_Test_001
332  * @tc.desc: Try recovery trusted devInfotable is failed
333  * @tc.type: FUNC
334  * @tc.require:
335  */
336 HWTEST_F(NetLedgerTest, TRY_RECOVERY_TRUST_DEVINFOTABLE_Test_001, TestSize.Level1)
337 {
338     bool ret;
339     int32_t retVal = InitTrustedDevInfoTable();
340     EXPECT_EQ(retVal, SOFTBUS_NETWORK_INIT_TRUST_DEV_INFO_FAILED);
341     RecoveryTrustedDevInfoProcess();
342     ret = TryRecoveryTrustedDevInfoTable();
343     ClearRecoveryDeviceList();
344     EXPECT_TRUE(ret);
345 }
346 
347 /*
348  * @tc.name: GET_ALL_DEV_NUM_Test_001
349  * @tc.desc: Get all dev nums is ok
350  * @tc.type: FUNC
351  * @tc.require:
352  */
353 HWTEST_F(NetLedgerTest, GET_ALL_DEV_NUM_Test_001, TestSize.Level1)
354 {
355     uint32_t num = 0;
356     int32_t ret;
357     int32_t userId = 123;
358     ret = GetAllDevNums(&num, userId);
359     EXPECT_EQ(ret, SOFTBUS_OK);
360 }
361 
362 /*
363  * @tc.name: LNN_DELETE_SPECIFIC_TRUSTED_DEV_INFO_Test_001
364  * @tc.desc: Delete specific trusted dev info ok
365  * @tc.type: FUNC
366  * @tc.require:
367  */
368 HWTEST_F(NetLedgerTest, LNN_DELETE_SPECIFIC_TRUSTED_DEV_INFO_Test_001, TestSize.Level1)
369 {
370     const char *udid = "672392378745";
371     int32_t localUserId = 123;
372     int32_t ret;
373     ret = LnnDeleteSpecificTrustedDevInfo(udid, localUserId);
374     EXPECT_EQ(ret, SOFTBUS_OK);
375 }
376 
377 /*
378  * @tc.name: LNN_INSERT_SPECIFIC_TRUSTED_DEV_INFO_Test_001
379  * @tc.desc: Delete specific trusted dev info is ok
380  * @tc.type: FUNC
381  * @tc.require:
382  */
383 HWTEST_F(NetLedgerTest, LNN_INSERT_SPECIFIC_TRUSTED_DEV_INFO_Test_001, TestSize.Level1)
384 {
385     int32_t ret;
386     const char *udid = "672392378745";
387     ret = LnnInsertSpecificTrustedDevInfo(udid);
388     EXPECT_EQ(ret, SOFTBUS_OK);
389 }
390 
391 /*
392  * @tc.name: UPDATE_RECOVERY_DEVICE_INFO_FROM_DB_Test_001
393  * @tc.desc: Update recovery device info from db error
394  * @tc.type: FUNC
395  * @tc.require:
396  */
397 HWTEST_F(NetLedgerTest, UPDATE_RECOVERY_DEVICE_INFO_FROM_DB_Test_001, TestSize.Level1)
398 {
399     int32_t retVal = InitDbListDelay();
400     EXPECT_EQ(retVal, SOFTBUS_NETWORK_GET_DEVICE_INFO_ERR);
401     retVal = UpdateRecoveryDeviceInfoFromDb();
402     EXPECT_EQ(retVal, SOFTBUS_NETWORK_GET_DEVICE_INFO_ERR);
403 }
404 } // namespace OHOS
405