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 #include "UTTest_softbus_connector.h"
16
17 #include <securec.h>
18 #include <unistd.h>
19
20 #include <cstdlib>
21 #include <string>
22
23 #include "dm_anonymous.h"
24 #include "dm_constants.h"
25 #include "dm_device_info.h"
26 #include "dm_log.h"
27 #include "parameter.h"
28 #include "softbus_connector.h"
29 #include "system_ability_definition.h"
30
31 namespace OHOS {
32 namespace DistributedHardware {
SetUp()33 void SoftbusConnectorTest::SetUp()
34 {
35 }
TearDown()36 void SoftbusConnectorTest::TearDown()
37 {
38 }
SetUpTestCase()39 void SoftbusConnectorTest::SetUpTestCase()
40 {
41 }
TearDownTestCase()42 void SoftbusConnectorTest::TearDownTestCase()
43 {
44 }
45
46 namespace {
47 static std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
48 /**
49 * @tc.name: Init_001
50 * @tc.desc: go tothe corrort case and return DM_OK
51 * @tc.type: FUNC
52 * @tc.require: AR000GHSJK
53 */
54 HWTEST_F(SoftbusConnectorTest, Init_001, testing::ext::TestSize.Level0)
55 {
56 int ret = softbusConnector->Init();
57 EXPECT_EQ(ret, DM_OK);
58 }
59
60 /**
61 * @tc.name: RegisterSoftbusDiscoveryCallback_001
62 * @tc.desc: set pkgName = "com.ohos.helloworld";call RegisterSoftbusDiscoveryCallback function to corrort ,return DM_OK
63 * @tc.type: FUNC
64 * @tc.require: AR000GHSJK
65 */
66 HWTEST_F(SoftbusConnectorTest, RegisterSoftbusDiscoveryCallback_001, testing::ext::TestSize.Level0)
67 {
68 std::string pkgName = "com.ohos.helloworld";
69 std::shared_ptr<DeviceManagerServiceListener> listener_ = std::make_shared<DeviceManagerServiceListener>();
70 std::shared_ptr<DmDiscoveryManager> discoveryMgr_ =
71 std::make_shared<DmDiscoveryManager>(softbusConnector, listener_);
72 int ret = softbusConnector->RegisterSoftbusDiscoveryCallback(
73 pkgName, std::shared_ptr<ISoftbusDiscoveryCallback>(discoveryMgr_));
74 int ret1 = SoftbusConnector::discoveryCallbackMap_.count(pkgName);
75 EXPECT_EQ(ret1, 1);
76 EXPECT_EQ(ret, DM_OK);
77 }
78
79 /**
80 * @tc.name: UnRegisterSoftbusDiscoveryCallback_001
81 * @tc.desc: set pkgName = "com.ohos.helloworld";call UnRegisterSoftbusDiscoveryCallback function to corrort ,return
82 * DM_OK
83 * @tc.type: FUNC
84 * @tc.require: AR000GHSJK
85 */
86 HWTEST_F(SoftbusConnectorTest, UnRegisterSoftbusDiscoveryCallback_001, testing::ext::TestSize.Level0)
87 {
88 std::string pkgName = "com.ohos.helloworld";
89 int ret = softbusConnector->UnRegisterSoftbusDiscoveryCallback(pkgName);
90 int ret1 = SoftbusConnector::discoveryCallbackMap_.count(pkgName);
91 EXPECT_EQ(ret1, 0);
92 EXPECT_EQ(ret, DM_OK);
93 }
94
95 /**
96 * @tc.name: UnRegisterSoftbusStateCallback_001
97 * @tc.desc: set pkgName = "com.ohos.helloworld";call UnRegisterSoftbusStateCallback function to corrort ,return DM_OK
98 * @tc.type: FUNC
99 * @tc.require: AR000GHSJK
100 */
101 HWTEST_F(SoftbusConnectorTest, UnRegisterSoftbusStateCallback_001, testing::ext::TestSize.Level0)
102 {
103 std::string pkgName = "com.ohos.helloworld";
104 int ret = softbusConnector->UnRegisterSoftbusStateCallback(pkgName);
105 int ret1 = SoftbusConnector::stateCallbackMap_.count(pkgName);
106 EXPECT_EQ(ret1, 0);
107 EXPECT_EQ(ret, DM_OK);
108 }
109
110 /**
111 * @tc.name: GetTrustedDeviceList_001
112 * @tc.desc: create GetAllNodeDeviceInfo not equal 0, and return DM_OK
113 * @tc.type: FUNC
114 * @tc.require: AR000GHSJK
115 */
116 HWTEST_F(SoftbusConnectorTest, GetTrustedDeviceList_001, testing::ext::TestSize.Level0)
117 {
118 std::vector<DmDeviceInfo> deviceInfoList;
119 int ret = softbusConnector->GetTrustedDeviceList(deviceInfoList);
120 EXPECT_EQ(ret, DM_OK);
121 }
122
123 /**
124 * @tc.name: GetLocalDeviceInfo_001
125 * @tc.desc: set pkgName to com.softbus.test,define deviceInfo,and return DM_OK.
126 * @tc.type: FUNC
127 * @tc.require: AR000GHSJK
128 */
129 HWTEST_F(SoftbusConnectorTest, GetLocalDeviceInfo_001, testing::ext::TestSize.Level0)
130 {
131 std::string pkgName = "com.softbus.test";
132 DmDeviceInfo deviceInfo;
133 int ret = softbusConnector->GetLocalDeviceInfo(deviceInfo);
134 EXPECT_EQ(ret, DM_OK);
135 }
136
137 /**
138 * @tc.name: StartDiscovery_001
139 * @tc.desc: get StartDiscovery to wroing master and return DM_DISCOVERY_FAILED
140 * @tc.type: FUNC
141 * @tc.require: AR000GHSJK
142 */
143 HWTEST_F(SoftbusConnectorTest, StartDiscovery_001, testing::ext::TestSize.Level0)
144 {
145 DmSubscribeInfo dmSubscribeInfo;
146 int ret = softbusConnector->StartDiscovery(dmSubscribeInfo);
147 EXPECT_EQ(ret, DM_DISCOVERY_FAILED);
148 }
149
150 /**
151 * @tc.name: IsDeviceOnLine_001
152 * @tc.desc: set deviceId = "12333";,and return false
153 * @tc.type: FUNC
154 * @tc.require: AR000GHSJK
155 */
156 HWTEST_F(SoftbusConnectorTest, IsDeviceOnLine_001, testing::ext::TestSize.Level0)
157 {
158 std::string deviceId = "12333";
159 bool ret = softbusConnector->IsDeviceOnLine(deviceId);
160 EXPECT_EQ(ret, false);
161 }
162
163 /**
164 * @tc.name: IsDeviceOnLine_002
165 * @tc.desc:
166 * @tc.type: FUNC
167 * @tc.require: AR000GHSJK
168 */
169 HWTEST_F(SoftbusConnectorTest, IsDeviceOnLine_002, testing::ext::TestSize.Level0)
170 {
171 std::string deviceId = "145677";
172 bool ret = softbusConnector->IsDeviceOnLine(deviceId);
173 EXPECT_EQ(ret, false);
174 }
175
176 /**
177 * @tc.name: SoftbusConnector_001
178 * @tc.desc: set SoftbusConnector to new a pointer ,and the pointer nou equal nullptr
179 * @tc.type: FUNC
180 * @tc.require: AR000GHSJK
181 */
182 HWTEST_F(SoftbusConnectorTest, SoftbusConnector_001, testing::ext::TestSize.Level0)
183 {
184 std::shared_ptr<SoftbusConnector> m_SoftbusConnector = std::make_shared<SoftbusConnector>();
185 ASSERT_NE(m_SoftbusConnector, nullptr);
186 }
187
188 /**
189 * @tc.name: SoftbusConnector_002
190 * @tc.desc: set SoftbusConnector to new a pointer ,and the pointer nou equal nullptr,and delete it
191 * @tc.type: FUNC
192 * @tc.require: AR000GHSJK
193 */
194 HWTEST_F(SoftbusConnectorTest, SoftbusConnector_002, testing::ext::TestSize.Level0)
195 {
196 std::shared_ptr<SoftbusConnector> m_SoftbusConnector = std::make_shared<SoftbusConnector>();
197 m_SoftbusConnector.reset();
198 EXPECT_EQ(m_SoftbusConnector, nullptr);
199 }
200
201 /**
202 * @tc.name: GetConnectionIpAddress_001
203 * @tc.desc: set deviceId null,and return DM_FAILED
204 * @tc.type: FUNC
205 * @tc.require: AR000GHSJK
206 */
207 HWTEST_F(SoftbusConnectorTest, GetConnectionIpAddress_001, testing::ext::TestSize.Level0)
208 {
209 std::string deviceId;
210 std::string ipAddress = "123456";
211 int ret = softbusConnector->GetConnectionIpAddress(deviceId, ipAddress);
212 EXPECT_EQ(ret, DM_FAILED);
213 }
214
215 /**
216 * @tc.name: GetConnectionIpAddress_002
217 * @tc.desc: set some corrort para,and return DM_FAILED
218 * @tc.type: FUNC
219 * @tc.require: AR000GHSJK
220 */
221 HWTEST_F(SoftbusConnectorTest, GetConnectionIpAddress_002, testing::ext::TestSize.Level0)
222 {
223 std::string deviceId = "213456";
224 std::string ipAddress = "123456";
225 int ret = softbusConnector->GetConnectionIpAddress(deviceId, ipAddress);
226 EXPECT_EQ(ret, DM_FAILED);
227 }
228 /**
229 * @tc.name: GetConnectionIpAddress_003
230 * @tc.desc: set deviceInfo.addrNum = -1;go to second master and return DM_FAILED
231 * @tc.type: FUNC
232 * @tc.require: AR000GHSJK
233 */
234 HWTEST_F(SoftbusConnectorTest, GetConnectionIpAddress_003, testing::ext::TestSize.Level0)
235 {
236 DeviceInfo deviceInfo;
237 deviceInfo.addrNum = -1;
238 std::string ipAddress;
239 std::string deviceId = "3338848";
240 int ret = softbusConnector->GetConnectionIpAddress(deviceId, ipAddress);
241 EXPECT_EQ(ret, DM_FAILED);
242 }
243
244 /**
245 * @tc.name: GetConnectAddrByType_001
246 * @tc.desc: set deviceInfo'pointer null, go to first master,and return nullptr
247 * @tc.require: AR000GHSJK
248 */
249 HWTEST_F(SoftbusConnectorTest, GetConnectAddrByType_001, testing::ext::TestSize.Level0)
250 {
251 ConnectionAddrType type;
252 type = CONNECTION_ADDR_MAX;
253 ConnectionAddr *p = nullptr;
254 ConnectionAddr *ret = softbusConnector->GetConnectAddrByType(nullptr, type);
255 EXPECT_EQ(p, ret);
256 }
257
258 /**
259 * @tc.name: GetConnectAddrByType_002
260 * @tc.desc:set deviceInfo to some corrort para,and return nullptr
261 * @tc.type: FUNC
262 * @tc.require: AR000GHSJK
263 */
264 HWTEST_F(SoftbusConnectorTest, GetConnectAddrByType_002, testing::ext::TestSize.Level0)
265 {
266 DeviceInfo deviceInfo;
267 deviceInfo.addrNum = 1;
268 ConnectionAddrType type;
269 type = CONNECTION_ADDR_BR;
270 ConnectionAddr *p = nullptr;
271 ConnectionAddr *ret = softbusConnector->GetConnectAddrByType(&deviceInfo, type);
272 EXPECT_EQ(ret, p);
273 }
274
275 /**
276 * @tc.name: GetConnectAddr_001
277 * @tc.desc: set deviceId to null,and return nullptr
278 * @tc.type: FUNC
279 * @tc.require: AR000GHSJK
280 */
281 HWTEST_F(SoftbusConnectorTest, GetConnectAddr_001, testing::ext::TestSize.Level0)
282 {
283 std::string deviceId;
284 std::string connectAddr;
285 ConnectionAddr *ret = softbusConnector->GetConnectAddr(deviceId, connectAddr);
286 EXPECT_EQ(ret, nullptr);
287 }
288
289 /**
290 * @tc.name: GetConnectAddr_002
291 * @tc.desc:set deviceId nit null set deviceInfo.addrNum = -1; and return nullptr
292 * @tc.type: FUNC
293 * @tc.require: AR000GHSJK
294 */
295 HWTEST_F(SoftbusConnectorTest, GetConnectAddr_002, testing::ext::TestSize.Level0)
296 {
297 std::string deviceId = "123345";
298 std::string connectAddr;
299 DeviceInfo deviceInfo;
300 deviceInfo.addrNum = -1;
301 ConnectionAddr *ret = softbusConnector->GetConnectAddr(deviceId, connectAddr);
302 EXPECT_EQ(ret, nullptr);
303 }
304
305 /**
306 * @tc.name: CovertNodeBasicInfoToDmDevice_001
307 * @tc.desc: go to the corrort case and return DM_OK
308 * @tc.type: FUNC
309 * @tc.require: AR000GHSJK
310 */
311 HWTEST_F(SoftbusConnectorTest, CovertNodeBasicInfoToDmDevice_001, testing::ext::TestSize.Level0)
312 {
313 NodeBasicInfo nodeBasicInfo;
314 DmDeviceInfo dmDeviceInfo;
315 int ret = softbusConnector->CovertNodeBasicInfoToDmDevice(nodeBasicInfo, dmDeviceInfo);
316 EXPECT_EQ(ret, DM_OK);
317 }
318 } // namespace
319 } // namespace DistributedHardware
320 } // namespace OHOS
321