• 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 
19 #include "bus_center_event.h"
20 #include "bus_center_manager.h"
21 #include "lnn_local_net_ledger.h"
22 #include "lnn_net_builder.h"
23 #include "lnn_net_builder.c"
24 #include "lnn_net_builder_deps_mock.h"
25 #include "lnn_node_info.h"
26 #include "lnn_trans_mock.h"
27 #include "message_handler.h"
28 #include "softbus_common.h"
29 #include "softbus_errcode.h"
30 #include "softbus_log.h"
31 
32 namespace OHOS {
33 using namespace testing::ext;
34 using namespace testing;
35 class LnnNetBuilderMockTest : public testing::Test {
36 public:
37     static void SetUpTestCase();
38     static void TearDownTestCase();
39     void SetUp();
40     void TearDown();
41 };
42 
SetUpTestCase()43 void LnnNetBuilderMockTest::SetUpTestCase()
44 {
45     LooperInit();
46     NiceMock<LnnTransInterfaceMock> transMock;
47     EXPECT_CALL(transMock, TransRegisterNetworkingChannelListener).WillRepeatedly(
48         LnnTransInterfaceMock::ActionOfTransRegister);
49     int32_t ret = LnnInitNetBuilder();
50     EXPECT_TRUE(ret == SOFTBUS_OK);
51 }
52 
TearDownTestCase()53 void LnnNetBuilderMockTest::TearDownTestCase()
54 {
55     LnnDeinitNetBuilder();
56     LooperDeinit();
57 }
58 
SetUp()59 void LnnNetBuilderMockTest::SetUp()
60 {
61 }
62 
TearDown()63 void LnnNetBuilderMockTest::TearDown()
64 {
65 }
66 
67 /*
68 * @tc.name: META_AUTH_META_VERIFY_TEST_001
69 * @tc.desc: test OnAuthMetaVerifyPassed
70 * @tc.type: FUNC
71 * @tc.require:
72 */
73 HWTEST_F(LnnNetBuilderMockTest, META_AUTH_META_VERIFY_TEST_001, TestSize.Level1)
74 {
75     uint32_t requestId = 1;
76     int64_t authMetaId = 1;
77     NodeInfo info;
78     OnAuthMetaVerifyPassed(requestId, authMetaId, &info);
79 }
80 
81 /*
82 * @tc.name: META_AUTH_META_VERIFY_TEST_002
83 * @tc.desc: test OnAuthMetaVerifyPassed
84 * @tc.type: FUNC
85 * @tc.require:
86 */
87 HWTEST_F(LnnNetBuilderMockTest, META_AUTH_META_VERIFY_TEST_002, TestSize.Level1)
88 {
89     uint32_t requestId = 1;
90     int64_t authMetaId = 1;
91     NodeInfo info;
92     ConnectionAddr addr;
93     addr.type = CONNECTION_ADDR_BR;
94     MetaJoinRequestNode *node = TryJoinRequestMetaNode(&addr, true);
95     EXPECT_TRUE(node != nullptr);
96     OnAuthMetaVerifyPassed(requestId, authMetaId, &info);
97 }
98 
99 /*
100 * @tc.name: LNN_FILL_NODE_INFO_TEST_001
101 * @tc.desc: test FillNodeInfo
102 * @tc.type: FUNC
103 * @tc.require:
104 */
105 HWTEST_F(LnnNetBuilderMockTest, LNN_FILL_NODE_INFO_TEST_001, TestSize.Level1)
106 {
107     NetBuilderDepsInterfaceMock uuidMock;
108     EXPECT_CALL(uuidMock, AuthGetDeviceUuid(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK));
109     MetaJoinRequestNode metaNode;
110     NodeInfo info;
111     info.uuid[0] = 'x';
112     metaNode.addr.type = CONNECTION_ADDR_ETH;
113     int32_t ret = FillNodeInfo(&metaNode, &info);
114     EXPECT_TRUE(ret == SOFTBUS_OK);
115 
116     EXPECT_CALL(uuidMock, AuthGetDeviceUuid(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK));
117     metaNode.addr.type = CONNECTION_ADDR_WLAN;
118     ret = FillNodeInfo(&metaNode, &info);
119     EXPECT_TRUE(ret == SOFTBUS_OK);
120 
121     EXPECT_CALL(uuidMock, AuthGetDeviceUuid(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK));
122     metaNode.addr.type = CONNECTION_ADDR_BR;
123     ret = FillNodeInfo(&metaNode, &info);
124     EXPECT_TRUE(ret == SOFTBUS_OK);
125 }
126 
127 /*
128 * @tc.name: LNN_LEAVE_META_NODE_TEST_001
129 * @tc.desc: test ProcessLeaveMetaNodeRequest
130 * @tc.type: FUNC
131 * @tc.require:
132 */
133 HWTEST_F(LnnNetBuilderMockTest, LNN_LEAVE_META_NODE_TEST_001, TestSize.Level1)
134 {
135     int32_t ret = ProcessLeaveMetaNodeRequest(nullptr);
136     EXPECT_TRUE(ret != SOFTBUS_OK);
137     ConnectionAddr addr;
138     addr.type = CONNECTION_ADDR_BR;
139     MetaJoinRequestNode *node = TryJoinRequestMetaNode(&addr, true);
140     EXPECT_TRUE(node != nullptr);
141     char *networkId = (char *)SoftBusCalloc(10);
142     networkId[0] = 'x';
143     ret = ProcessLeaveMetaNodeRequest(networkId);
144     EXPECT_TRUE(ret != SOFTBUS_OK);
145 }
146 
147 /*
148 * @tc.name: LNN_LEAVE_META_TO_LEDGER_TEST_001
149 * @tc.desc: test LeaveMetaInfoToLedger
150 * @tc.type: FUNC
151 * @tc.require:
152 */
153 HWTEST_F(LnnNetBuilderMockTest, LNN_LEAVE_META_TO_LEDGER_TEST_001, TestSize.Level1)
154 {
155     NetBuilderDepsInterfaceMock netLedgerMock;
156     MetaJoinRequestNode metaInfo;
157     LeaveMetaInfoToLedger(&metaInfo, nullptr);
158 
159     EXPECT_CALL(netLedgerMock, LnnDeleteMetaInfo(_,_)).WillRepeatedly(Return(SOFTBUS_OK));
160     LeaveMetaInfoToLedger(&metaInfo, nullptr);
161 
162     EXPECT_CALL(netLedgerMock, LnnDeleteMetaInfo(_,_)).WillRepeatedly(Return(SOFTBUS_ERR));
163     LeaveMetaInfoToLedger(&metaInfo, nullptr);
164 }
165 
166 /*
167 * @tc.name: LNN_JOIN_META_NODE_TEST_002
168 * @tc.desc: test PostJoinRequestToMetaNode
169 * @tc.type: FUNC
170 * @tc.require:
171 */
172 HWTEST_F(LnnNetBuilderMockTest, LNN_JOIN_META_NODE_TEST_002, TestSize.Level1)
173 {
174     MetaJoinRequestNode metaJoinNode;
175     int32_t ret = PostJoinRequestToMetaNode(&metaJoinNode, nullptr, nullptr, true);
176     EXPECT_TRUE(ret != SOFTBUS_OK);
177     CustomData customData;
178     metaJoinNode.addr.type = CONNECTION_ADDR_SESSION;
179     NiceMock<NetBuilderDepsInterfaceMock> mock;
180     EXPECT_CALL(mock, TransGetConnByChanId(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK));
181     EXPECT_CALL(mock, AuthMetaStartVerify(_,_,_,_,_)).WillRepeatedly(Return(SOFTBUS_OK));
182     ret = PostJoinRequestToMetaNode(&metaJoinNode, nullptr, &customData, true);
183     EXPECT_TRUE(ret == SOFTBUS_OK);
184 
185     EXPECT_CALL(mock, TransGetConnByChanId(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK));
186     EXPECT_CALL(mock, AuthMetaStartVerify(_,_,_,_,_)).WillRepeatedly(Return(SOFTBUS_OK));
187     ret = PostJoinRequestToMetaNode(&metaJoinNode, nullptr, &customData, false);
188 }
189 } // namespace OHOS
190