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 "softbus_adapter_mem.h"
25 #include "softbus_def.h"
26 #include "softbus_errcode.h"
27 #include "softbus_log.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 MetaNodeLedgerTest : public testing::Test {
45 public:
46 static void SetUpTestCase();
47 static void TearDownTestCase();
48 void SetUp();
49 void TearDown();
50 };
51
SetUpTestCase()52 void MetaNodeLedgerTest::SetUpTestCase()
53 {
54 }
55
TearDownTestCase()56 void MetaNodeLedgerTest::TearDownTestCase()
57 {
58 }
59
SetUp()60 void MetaNodeLedgerTest::SetUp()
61 {
62 LOG_INFO("MetaNodeLedgerTest start.");
63 LnnInitMetaNodeLedger();
64 }
65
TearDown()66 void MetaNodeLedgerTest::TearDown()
67 {
68 LOG_INFO("MetaNodeLedgerTest 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(MetaNodeLedgerTest, 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)strcpy_s(info.udid, UUID_BUF_LEN, NODE_UDID);
84 (void)strcpy_s(info.deviceName, DEVICE_NAME_BUF_LEN, 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(MetaNodeLedgerTest, 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(MetaNodeLedgerTest, 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 } // namespace OHOS
127