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 "lnn_log.h"
23 #include "lnn_meta_node_ledger.h"
24 #include "lnn_network_id.h"
25 #include "softbus_adapter_mem.h"
26 #include "softbus_def.h"
27 #include "softbus_error_code.h"
28 #include "softbus_utils.h"
29
30 typedef struct {
31 ListNode node;
32 MetaNodeInfo info;
33 } MetaNodeStorageInfo;
34
35 namespace OHOS {
36 using namespace testing::ext;
37 constexpr char NODE_DEVICE_NAME[] = "node1_test";
38 constexpr char NODE_UDID[] = "123456ABCDEF";
39 constexpr char META_NODE_ID[] = "235689BNHFCC";
40 constexpr uint32_t ADDR_NUM = CONNECTION_ADDR_MAX + 1;
41 constexpr int32_t INFO_NUM = 0;
42 constexpr int32_t INVALID_INFO_NUM = -1;
43 using namespace testing;
44 class LNNMetaNodeLedgerTest : public testing::Test {
45 public:
46 static void SetUpTestCase();
47 static void TearDownTestCase();
48 void SetUp();
49 void TearDown();
50 };
51
SetUpTestCase()52 void LNNMetaNodeLedgerTest::SetUpTestCase() { }
53
TearDownTestCase()54 void LNNMetaNodeLedgerTest::TearDownTestCase() { }
55
SetUp()56 void LNNMetaNodeLedgerTest::SetUp()
57 {
58 LNN_LOGI(LNN_TEST, "LNNMetaNodeLedgerTest start");
59 LnnInitMetaNodeLedger();
60 }
61
TearDown()62 void LNNMetaNodeLedgerTest::TearDown()
63 {
64 LNN_LOGI(LNN_TEST, "LNNMetaNodeLedgerTest finish");
65 LnnDeinitMetaNodeLedger();
66 }
67
68 /*
69 * @tc.name: LNN_ACTIVE_META_NODE_Test_001
70 * @tc.desc: lnn active meta node test
71 * @tc.type: FUNC
72 * @tc.require:
73 */
74 HWTEST_F(LNNMetaNodeLedgerTest, LNN_ACTIVE_META_NODE_Test_001, TestSize.Level1)
75 {
76 MetaNodeConfigInfo info;
77 (void)memset_s(&info, sizeof(MetaNodeConfigInfo), 0, sizeof(MetaNodeConfigInfo));
78 info.addrNum = CONNECTION_ADDR_WLAN;
79 (void)strncpy_s(info.udid, UUID_BUF_LEN, NODE_UDID, strlen(NODE_UDID));
80 (void)strncpy_s(info.deviceName, DEVICE_NAME_BUF_LEN, NODE_DEVICE_NAME, strlen(NODE_DEVICE_NAME));
81 char metaNodeId[NETWORK_ID_BUF_LEN] = { 0 };
82 int32_t ret = LnnActiveMetaNode(&info, metaNodeId);
83 EXPECT_TRUE(ret == SOFTBUS_OK);
84 info.addrNum = ADDR_NUM;
85 ret = LnnActiveMetaNode(&info, metaNodeId);
86 EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM);
87 ret = LnnActiveMetaNode(nullptr, metaNodeId);
88 EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM);
89 }
90
91 /*
92 * @tc.name: LNN_GET_ALL_META_NODE_INFO_Test_001
93 * @tc.desc: lnn get all meta node info test
94 * @tc.type: FUNC
95 * @tc.require:
96 */
97 HWTEST_F(LNNMetaNodeLedgerTest, LNN_GET_ALL_META_NODE_INFO_Test_001, TestSize.Level1)
98 {
99 MetaNodeInfo infos[MAX_META_NODE_NUM];
100 int32_t infoNum1 = INFO_NUM;
101 int32_t infoNum2 = INVALID_INFO_NUM;
102 int32_t ret = LnnGetAllMetaNodeInfo(infos, &infoNum1);
103 EXPECT_TRUE(ret == SOFTBUS_OK);
104 ret = LnnGetAllMetaNodeInfo(infos, &infoNum2);
105 EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM);
106 EXPECT_TRUE(LnnGetAllMetaNodeInfo(nullptr, nullptr) == SOFTBUS_INVALID_PARAM);
107 }
108
109 /*
110 * @tc.name: LNN_DEACTIVE_META_NODE_Test_001
111 * @tc.desc: lnn deactive meta node test
112 * @tc.type: FUNC
113 * @tc.require:
114 */
115 HWTEST_F(LNNMetaNodeLedgerTest, LNN_DEACTIVE_META_NODE_Test_001, TestSize.Level1)
116 {
117 int32_t ret = LnnDeactiveMetaNode(META_NODE_ID);
118 EXPECT_TRUE(ret == SOFTBUS_NETWORK_NOT_FOUND);
119 EXPECT_TRUE(LnnDeactiveMetaNode(nullptr) == SOFTBUS_INVALID_PARAM);
120 LnnDeinitMetaNodeLedger();
121 }
122
123 /*
124 * @tc.name: LNN_GET_META_NODE_UID_TEST_001
125 * @tc.desc: lnn get meta node udid by network id test
126 * @tc.type: FUNC
127 * @tc.require:
128 */
129 HWTEST_F(LNNMetaNodeLedgerTest, LNN_GET_META_NODE_UID_TEST_001, TestSize.Level1)
130 {
131 const char *networkId = nullptr;
132 char *udid = nullptr;
133 int32_t ret = LnnGetMetaNodeUdidByNetworkId(networkId, udid);
134 EXPECT_TRUE(ret != SOFTBUS_OK);
135 }
136
137 /*
138 * @tc.name: LNN_GET_META_NODE_UID_TEST_002
139 * @tc.desc: lnn get meta node udid by network id test
140 * @tc.type: FUNC
141 * @tc.require:
142 */
143 HWTEST_F(LNNMetaNodeLedgerTest, LNN_GET_META_NODE_UID_TEST_002, TestSize.Level1)
144 {
145 const char *networkId = nullptr;
146 char *udid = nullptr;
147 MetaNodeConfigInfo info;
148 (void)memset_s(&info, sizeof(MetaNodeConfigInfo), 0, sizeof(MetaNodeConfigInfo));
149 info.addrNum = CONNECTION_ADDR_WLAN;
150 (void)strncpy_s(info.udid, UUID_BUF_LEN, NODE_UDID, strlen(NODE_UDID));
151 (void)strncpy_s(info.deviceName, DEVICE_NAME_BUF_LEN, NODE_DEVICE_NAME, strlen(NODE_DEVICE_NAME));
152 char metaNodeId[NETWORK_ID_BUF_LEN] = { 0 };
153
154 int32_t ret = LnnActiveMetaNode(&info, metaNodeId);
155 networkId = info.bypassInfo;
156 EXPECT_TRUE(ret == SOFTBUS_OK);
157 ret = LnnGetMetaNodeUdidByNetworkId(networkId, udid);
158 EXPECT_TRUE(ret == SOFTBUS_NETWORK_NOT_FOUND);
159 }
160
161 /*
162 * @tc.name: LNN_GET_META_NODE_INFO_TEST_001
163 * @tc.desc: lnn get meta node info by network id test
164 * @tc.type: FUNC
165 * @tc.require:
166 */
167 HWTEST_F(LNNMetaNodeLedgerTest, LNN_GET_META_NODE_INFO_TEST_001, TestSize.Level1)
168 {
169 const char *networkId = nullptr;
170 MetaNodeInfo *nodeInfo = nullptr;
171 int32_t ret = LnnGetMetaNodeInfoByNetworkId(networkId, nodeInfo);
172 EXPECT_TRUE(ret != SOFTBUS_OK);
173 }
174
175 /*
176 * @tc.name: LNN_GET_META_NODE_INFO_TEST_002
177 * @tc.desc: lnn get meta node info by network id test
178 * @tc.type: FUNC
179 * @tc.require:
180 */
181 HWTEST_F(LNNMetaNodeLedgerTest, LNN_GET_META_NODE_INFO_TEST_002, TestSize.Level1)
182 {
183 const char *networkId = nullptr;
184 MetaNodeInfo *nodeInfo = nullptr;
185 MetaNodeConfigInfo info;
186 (void)memset_s(&info, sizeof(MetaNodeConfigInfo), 0, sizeof(MetaNodeConfigInfo));
187 info.addrNum = CONNECTION_ADDR_WLAN;
188 (void)strncpy_s(info.udid, UUID_BUF_LEN, NODE_UDID, strlen(NODE_UDID));
189 (void)strncpy_s(info.deviceName, DEVICE_NAME_BUF_LEN, NODE_DEVICE_NAME, strlen(NODE_DEVICE_NAME));
190 char metaNodeId[NETWORK_ID_BUF_LEN] = { 0 };
191
192 int32_t ret = LnnActiveMetaNode(&info, metaNodeId);
193 networkId = info.bypassInfo;
194 EXPECT_TRUE(ret == SOFTBUS_OK);
195 ret = LnnGetMetaNodeInfoByNetworkId(networkId, nodeInfo);
196 EXPECT_TRUE(ret == SOFTBUS_NETWORK_NOT_FOUND);
197 }
198 } // namespace OHOS
199