1 /*
2  * Copyright (c) 2021 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 
18 #include <securec.h>
19 
20 #include "softbus_access_token_test.h"
21 #include "softbus_bus_center.h"
22 #include "softbus_errcode.h"
23 
24 namespace OHOS {
25 using namespace testing::ext;
26 
27 static constexpr char TEST_PKG_NAME[] = "com.softbus.test";
28 
29 class BusCenterMetaNodeSdkTest : public testing::Test {
30 public:
31     static void SetUpTestCase();
32     static void TearDownTestCase();
33     void SetUp();
34     void TearDown();
35 };
36 
SetUpTestCase()37 void BusCenterMetaNodeSdkTest::SetUpTestCase()
38 {
39     SetAceessTokenPermission("busCenterTest");
40 }
41 
TearDownTestCase()42 void BusCenterMetaNodeSdkTest::TearDownTestCase()
43 {
44 }
45 
SetUp()46 void BusCenterMetaNodeSdkTest::SetUp()
47 {
48 }
49 
TearDown()50 void BusCenterMetaNodeSdkTest::TearDown()
51 {
52 }
53 
54 /*
55 * @tc.name: BUS_CENTER_SDK_META_NODE_Test_001
56 * @tc.desc: meta node interface test
57 * @tc.type: FUNC
58 * @tc.require:
59 */
60 HWTEST_F(BusCenterMetaNodeSdkTest, BUS_CENTER_SDK_META_NODE_Test_001, TestSize.Level0)
61 {
62     char udid[] = "0123456789987654321001234567899876543210012345678998765432100123";
63     char metaNodeId[NETWORK_ID_BUF_LEN] = {0};
64     MetaNodeInfo infos[MAX_META_NODE_NUM];
65     int32_t infoNum = MAX_META_NODE_NUM;
66     MetaNodeConfigInfo configInfo;
67 
68     configInfo.addrNum = 1;
69     EXPECT_TRUE(DeactiveMetaNode(TEST_PKG_NAME, metaNodeId) != SOFTBUS_OK);
70     EXPECT_TRUE(strncpy_s(configInfo.udid, UDID_BUF_LEN, udid, UDID_BUF_LEN) == EOK);
71     EXPECT_TRUE(ActiveMetaNode(TEST_PKG_NAME, &configInfo, metaNodeId) == SOFTBUS_OK);
72     EXPECT_EQ((int32_t)strlen(metaNodeId), NETWORK_ID_BUF_LEN - 1);
73     EXPECT_TRUE(ActiveMetaNode(TEST_PKG_NAME, &configInfo, metaNodeId) == SOFTBUS_OK);
74     EXPECT_TRUE(GetAllMetaNodeInfo(TEST_PKG_NAME, infos, &infoNum) == SOFTBUS_OK);
75     EXPECT_EQ(infoNum, 1);
76     EXPECT_FALSE(infos[0].isOnline);
77     EXPECT_TRUE(strcmp(infos[0].metaNodeId, metaNodeId) == 0);
78     EXPECT_TRUE(strcmp(infos[0].configInfo.udid, udid) == 0);
79     EXPECT_EQ(infos[0].configInfo.addrNum, 1);
80     EXPECT_TRUE(DeactiveMetaNode(TEST_PKG_NAME, metaNodeId) == SOFTBUS_OK);
81     EXPECT_TRUE(GetAllMetaNodeInfo(TEST_PKG_NAME, infos, &infoNum) == SOFTBUS_OK);
82     EXPECT_EQ(infoNum, 0);
83 }
84 
85 /*
86 * @tc.name: BUS_CENTER_SDK_META_NODE_Test_002
87 * @tc.desc: meta node interface test
88 * @tc.type: FUNC
89 * @tc.require:
90 */
91 HWTEST_F(BusCenterMetaNodeSdkTest, BUS_CENTER_SDK_META_NODE_Test_002, TestSize.Level0)
92 {
93     char udid[] = "0123456789987654321001234567899876543210012345678998765432100123";
94     char metaNodeId[NETWORK_ID_BUF_LEN] = {0};
95     MetaNodeInfo infos[MAX_META_NODE_NUM];
96     int32_t infoNum = MAX_META_NODE_NUM;
97     MetaNodeConfigInfo configInfo;
98     int i;
99 
100     configInfo.addrNum = 1;
101     EXPECT_TRUE(strncpy_s(configInfo.udid, UDID_BUF_LEN, udid, UDID_BUF_LEN) == EOK);
102     for (i = 0; i <= MAX_META_NODE_NUM; ++i) {
103         configInfo.udid[0] += 1;
104         if (i < MAX_META_NODE_NUM) {
105             EXPECT_TRUE(ActiveMetaNode(TEST_PKG_NAME, &configInfo, metaNodeId) == SOFTBUS_OK);
106         } else {
107             EXPECT_TRUE(ActiveMetaNode(TEST_PKG_NAME, &configInfo, metaNodeId) != SOFTBUS_OK);
108         }
109     }
110     EXPECT_TRUE(GetAllMetaNodeInfo(TEST_PKG_NAME, infos, &infoNum) == SOFTBUS_OK);
111     EXPECT_EQ(infoNum, MAX_META_NODE_NUM);
112     for (i = 0; i < MAX_META_NODE_NUM; ++i) {
113         EXPECT_FALSE(infos[i].isOnline);
114         EXPECT_TRUE(DeactiveMetaNode(TEST_PKG_NAME, infos[i].metaNodeId) == SOFTBUS_OK);
115     }
116 }
117 } // namespace OHOS
118