• 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 <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 = 8;
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 HWTEST_F(LNNMetaNodeLedgerTest, LNN_GET_META_NODE_UID_TEST_001, TestSize.Level1)
124 {
125     const char *networkId = nullptr;
126     char *udid = nullptr;
127     int32_t ret = LnnGetMetaNodeUdidByNetworkId(networkId, udid);
128     EXPECT_TRUE(ret != SOFTBUS_OK);
129 }
130 
131 HWTEST_F(LNNMetaNodeLedgerTest, LNN_GET_META_NODE_UID_TEST_002, TestSize.Level1)
132 {
133     const char *networkId = nullptr;
134     char *udid = nullptr;
135     MetaNodeConfigInfo info;
136     (void)memset_s(&info, sizeof(MetaNodeConfigInfo), 0, sizeof(MetaNodeConfigInfo));
137     info.addrNum = CONNECTION_ADDR_WLAN;
138     (void)strncpy_s(info.udid, UUID_BUF_LEN, NODE_UDID, strlen(NODE_UDID));
139     (void)strncpy_s(info.deviceName, DEVICE_NAME_BUF_LEN, NODE_DEVICE_NAME, strlen(NODE_DEVICE_NAME));
140     char metaNodeId[NETWORK_ID_BUF_LEN] = { 0 };
141 
142     int32_t ret = LnnActiveMetaNode(&info, metaNodeId);
143     networkId = info.bypassInfo;
144     EXPECT_TRUE(ret == SOFTBUS_OK);
145     ret = LnnGetMetaNodeUdidByNetworkId(networkId, udid);
146     EXPECT_TRUE(ret == SOFTBUS_NETWORK_NOT_FOUND);
147 }
148 
149 HWTEST_F(LNNMetaNodeLedgerTest, LNN_GET_META_NODE_INFO_TEST_001, TestSize.Level1)
150 {
151     const char *networkId = nullptr;
152     MetaNodeInfo *nodeInfo = nullptr;
153     int32_t ret = LnnGetMetaNodeInfoByNetworkId(networkId, nodeInfo);
154     EXPECT_TRUE(ret != SOFTBUS_OK);
155 }
156 
157 HWTEST_F(LNNMetaNodeLedgerTest, LNN_GET_META_NODE_INFO_TEST_002, TestSize.Level1)
158 {
159     const char *networkId = nullptr;
160     MetaNodeInfo *nodeInfo = nullptr;
161     MetaNodeConfigInfo info;
162     (void)memset_s(&info, sizeof(MetaNodeConfigInfo), 0, sizeof(MetaNodeConfigInfo));
163     info.addrNum = CONNECTION_ADDR_WLAN;
164     (void)strncpy_s(info.udid, UUID_BUF_LEN, NODE_UDID, strlen(NODE_UDID));
165     (void)strncpy_s(info.deviceName, DEVICE_NAME_BUF_LEN, NODE_DEVICE_NAME, strlen(NODE_DEVICE_NAME));
166     char metaNodeId[NETWORK_ID_BUF_LEN] = { 0 };
167 
168     int32_t ret = LnnActiveMetaNode(&info, metaNodeId);
169     networkId = info.bypassInfo;
170     EXPECT_TRUE(ret == SOFTBUS_OK);
171     ret = LnnGetMetaNodeInfoByNetworkId(networkId, nodeInfo);
172     EXPECT_TRUE(ret == SOFTBUS_NETWORK_NOT_FOUND);
173 }
174 } // namespace OHOS
175