• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <cstddef>
19 #include <cstdlib>
20 #include <cstring>
21 
22 #include "lnn_meta_node_ledger.h"
23 #include "lnn_network_id.h"
24 #include "lnn_log.h"
25 #include "softbus_adapter_mem.h"
26 #include "softbus_def.h"
27 #include "softbus_errcode.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 = 6;
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 {
54 }
55 
TearDownTestCase()56 void LNNMetaNodeLedgerTest::TearDownTestCase()
57 {
58 }
59 
SetUp()60 void LNNMetaNodeLedgerTest::SetUp()
61 {
62     LNN_LOGI(LNN_TEST, "LNNMetaNodeLedgerTest start");
63     LnnInitMetaNodeLedger();
64 }
65 
TearDown()66 void LNNMetaNodeLedgerTest::TearDown()
67 {
68     LNN_LOGI(LNN_TEST, "LNNMetaNodeLedgerTest finish");
69     LnnDeinitMetaNodeLedger();
70 }
71 
72 /*
73 * @tc.name: LNN_ACTIVE_META_NODE_Test_001
74 * @tc.desc: lnn active meta node test
75 * @tc.type: FUNC
76 * @tc.require:
77 */
78 HWTEST_F(LNNMetaNodeLedgerTest, LNN_ACTIVE_META_NODE_Test_001, TestSize.Level1)
79 {
80     MetaNodeConfigInfo info;
81     (void)memset_s(&info, sizeof(MetaNodeConfigInfo), 0, sizeof(MetaNodeConfigInfo));
82     info.addrNum = CONNECTION_ADDR_WLAN;
83     (void)strncpy_s(info.udid, UUID_BUF_LEN, NODE_UDID, strlen(NODE_UDID));
84     (void)strncpy_s(info.deviceName, DEVICE_NAME_BUF_LEN, NODE_DEVICE_NAME, strlen(NODE_DEVICE_NAME));
85     char metaNodeId[NETWORK_ID_BUF_LEN] = {0};
86     int32_t ret = LnnActiveMetaNode(&info, metaNodeId);
87     EXPECT_TRUE(ret == SOFTBUS_OK);
88     info.addrNum = ADDR_NUM;
89     ret = LnnActiveMetaNode(&info, metaNodeId);
90     EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM);
91     ret = LnnActiveMetaNode(nullptr, metaNodeId);
92     EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM);
93 }
94 
95 /*
96 * @tc.name: LNN_GET_ALL_META_NODE_INFO_Test_001
97 * @tc.desc: lnn get all meta node info test
98 * @tc.type: FUNC
99 * @tc.require:
100 */
101 HWTEST_F(LNNMetaNodeLedgerTest, LNN_GET_ALL_META_NODE_INFO_Test_001, TestSize.Level1)
102 {
103     MetaNodeInfo infos[MAX_META_NODE_NUM];
104     int32_t infoNum1 = INFO_NUM;
105     int32_t infoNum2 = INVALID_INFO_NUM;
106     int32_t ret = LnnGetAllMetaNodeInfo(infos, &infoNum1);
107     EXPECT_TRUE(ret == SOFTBUS_OK);
108     ret = LnnGetAllMetaNodeInfo(infos, &infoNum2);
109     EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM);
110     EXPECT_TRUE(LnnGetAllMetaNodeInfo(nullptr, nullptr) == SOFTBUS_INVALID_PARAM);
111 }
112 
113 /*
114 * @tc.name: LNN_DEACTIVE_META_NODE_Test_001
115 * @tc.desc: lnn deactive meta node test
116 * @tc.type: FUNC
117 * @tc.require:
118 */
119 HWTEST_F(LNNMetaNodeLedgerTest, LNN_DEACTIVE_META_NODE_Test_001, TestSize.Level1)
120 {
121     int32_t ret = LnnDeactiveMetaNode(META_NODE_ID);
122     EXPECT_TRUE(ret == SOFTBUS_ERR);
123     EXPECT_TRUE(LnnDeactiveMetaNode(nullptr) == SOFTBUS_INVALID_PARAM);
124     LnnDeinitMetaNodeLedger();
125 }
126 
127 HWTEST_F(LNNMetaNodeLedgerTest, LNN_GET_META_NODE_UID_TEST_001, TestSize.Level1)
128 {
129     const char *networkId = nullptr;
130     char *udid = nullptr;
131     int32_t ret = LnnGetMetaNodeUdidByNetworkId(networkId, udid);
132     EXPECT_TRUE(ret != SOFTBUS_OK);
133 }
134 
135 HWTEST_F(LNNMetaNodeLedgerTest, LNN_GET_META_NODE_UID_TEST_002, TestSize.Level1)
136 {
137     const char *networkId = nullptr;
138     char *udid = nullptr;
139     MetaNodeConfigInfo info;
140     (void)memset_s(&info, sizeof(MetaNodeConfigInfo), 0, sizeof(MetaNodeConfigInfo));
141     info.addrNum = CONNECTION_ADDR_WLAN;
142     (void)strncpy_s(info.udid, UUID_BUF_LEN, NODE_UDID, strlen(NODE_UDID));
143     (void)strncpy_s(info.deviceName, DEVICE_NAME_BUF_LEN, NODE_DEVICE_NAME, strlen(NODE_DEVICE_NAME));
144     char metaNodeId[NETWORK_ID_BUF_LEN] = {0};
145 
146     int32_t ret = LnnActiveMetaNode(&info, metaNodeId);
147     networkId = info.bypassInfo;
148     EXPECT_TRUE(ret == SOFTBUS_OK);
149     ret = LnnGetMetaNodeUdidByNetworkId(networkId, udid);
150     EXPECT_TRUE(ret == SOFTBUS_ERR);
151 }
152 
153 HWTEST_F(LNNMetaNodeLedgerTest, LNN_GET_META_NODE_INFO_TEST_001, TestSize.Level1)
154 {
155     const char *networkId = nullptr;
156     MetaNodeInfo *nodeInfo = nullptr;
157     int32_t ret = LnnGetMetaNodeInfoByNetworkId(networkId, nodeInfo);
158     EXPECT_TRUE(ret != SOFTBUS_OK);
159 }
160 
161 HWTEST_F(LNNMetaNodeLedgerTest, LNN_GET_META_NODE_INFO_TEST_002, TestSize.Level1)
162 {
163     const char *networkId = nullptr;
164     MetaNodeInfo *nodeInfo = nullptr;
165     MetaNodeConfigInfo info;
166     (void)memset_s(&info, sizeof(MetaNodeConfigInfo), 0, sizeof(MetaNodeConfigInfo));
167     info.addrNum = CONNECTION_ADDR_WLAN;
168     (void)strncpy_s(info.udid, UUID_BUF_LEN, NODE_UDID, strlen(NODE_UDID));
169     (void)strncpy_s(info.deviceName, DEVICE_NAME_BUF_LEN, NODE_DEVICE_NAME, strlen(NODE_DEVICE_NAME));
170     char metaNodeId[NETWORK_ID_BUF_LEN] = {0};
171 
172     int32_t ret = LnnActiveMetaNode(&info, metaNodeId);
173     networkId = info.bypassInfo;
174     EXPECT_TRUE(ret == SOFTBUS_OK);
175     ret = LnnGetMetaNodeInfoByNetworkId(networkId, nodeInfo);
176     EXPECT_TRUE(ret == SOFTBUS_ERR);
177 }
178 } // namespace OHOS
179