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 "UTTest_dm_device_state_manager.h"
17
18 #include <iostream>
19
20 #include "dm_log.h"
21 #include "dm_constants.h"
22 #include "dm_adapter_manager.h"
23 #include "ipc_notify_device_state_req.h"
24 #include "ipc_notify_auth_result_req.h"
25 #include "ipc_notify_verify_auth_result_req.h"
26 #include "dm_device_state_manager.h"
27 #include "ipc_notify_device_found_req.h"
28 #include "ipc_notify_discover_result_req.h"
29 #include "hichain_connector.h"
30 #include "device_manager_service_listener.h"
31
32 namespace OHOS {
33 namespace DistributedHardware {
SetUp()34 void DmDeviceStateManagerTest::SetUp()
35 {
36 }
37
TearDown()38 void DmDeviceStateManagerTest::TearDown()
39 {
40 }
41
SetUpTestCase()42 void DmDeviceStateManagerTest::SetUpTestCase()
43 {
44 }
45
TearDownTestCase()46 void DmDeviceStateManagerTest::TearDownTestCase()
47 {
48 }
49 namespace {
50 std::shared_ptr<HiChainConnector> hiChainConnector_ = std::make_shared<HiChainConnector>();
51 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
52 std::shared_ptr<DeviceManagerServiceListener> listener_ = std::make_shared<DeviceManagerServiceListener>();
53 std::shared_ptr<DmDeviceStateManager> dmDeviceStateManager =
54 std::make_shared<DmDeviceStateManager>(softbusConnector, listener_, hiChainConnector_);
55
56 /**
57 * @tc.name: DmDeviceStateManager_001
58 * @tc.desc: set DmDeviceStateManager to tne new pointer, and it's not nullptr
59 * @tc.type: FUNC
60 * @tc.require: AR000GHSJK
61 */
62 HWTEST_F(DmDeviceStateManagerTest, DmDeviceStateManager_001, testing::ext::TestSize.Level0)
63 {
64 std::shared_ptr<DmDeviceStateManager> p = std::make_shared<DmDeviceStateManager>(softbusConnector, listener_,
65 hiChainConnector_);
66 ASSERT_NE(p, nullptr);
67 }
68
69 /**
70 * @tc.name: DmDeviceStateManager_002
71 * @tc.desc: set DmDeviceStateManager to tne new pointer,it's not nullptr and delete it
72 * @tc.type: FUNC
73 * @tc.require: AR000GHSJK
74 */
75 HWTEST_F(DmDeviceStateManagerTest, DmDeviceStateManager_002, testing::ext::TestSize.Level0)
76 {
77 std::shared_ptr<DmDeviceStateManager> p = std::make_shared<DmDeviceStateManager>(softbusConnector, listener_,
78 hiChainConnector_);
79 p.reset();
80 EXPECT_EQ(p, nullptr);
81 }
82
83 /**
84 * @tc.name: OnDeviceOnline_001
85 * @tc.desc: set info.deviceId to some para, and return it
86 * @tc.type: FUNC
87 * @tc.require: AR000GHSJK
88 */
89 HWTEST_F(DmDeviceStateManagerTest, OnDeviceOnline_001, testing::ext::TestSize.Level0)
90 {
91 std::string pkgName = "123";
92 DmDeviceInfo info;
93 strcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
94 dmDeviceStateManager->OnDeviceOnline(pkgName, info);
95 std::shared_ptr<IpcNotifyDeviceStateReq> pReq =
96 std::static_pointer_cast<IpcNotifyDeviceStateReq>(listener_->ipcServerListener_.req_);
97 DmDeviceInfo dminfo;
98 if (pReq == nullptr) {
99 strcpy_s(dminfo.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
100 } else {
101 dminfo = pReq->GetDeviceInfo();
102 }
103 int result = strcmp(info.deviceId, dminfo.deviceId);
104 EXPECT_EQ(result, 0);
105 }
106
107 /**
108 * @tc.name: OnDeviceChanged_001
109 * @tc.desc: set info.deviceId to some para, and return it
110 * @tc.type: FUNC
111 * @tc.require: AR000GHSJK
112 */
113 HWTEST_F(DmDeviceStateManagerTest, OnDeviceChanged_001, testing::ext::TestSize.Level0)
114 {
115 std::string pkgName = "111";
116 DmDeviceInfo info;
117 strcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
118 dmDeviceStateManager->OnDeviceChanged(pkgName, info);
119 std::shared_ptr<IpcNotifyDeviceStateReq> pReq =
120 std::static_pointer_cast<IpcNotifyDeviceStateReq>(listener_->ipcServerListener_.req_);
121 DmDeviceInfo dminfo;
122 if (pReq == nullptr) {
123 strcpy_s(dminfo.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
124 } else {
125 dminfo = pReq->GetDeviceInfo();
126 }
127 int result = strcmp(info.deviceId, dminfo.deviceId);
128 EXPECT_EQ(result, 0);
129 }
130
131 /**
132 * @tc.name: OnDeviceReady_001
133 * @tc.desc: set info.deviceId to some para, and return it
134 * @tc.type: FUNC
135 * @tc.require: AR000GHSJK
136 */
137 HWTEST_F(DmDeviceStateManagerTest, OnDbReady_001, testing::ext::TestSize.Level0)
138 {
139 std::string pkgName;
140 std::string deviceId;
141 DmDeviceInfo info;
142 strcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
143 dmDeviceStateManager->OnDbReady(pkgName, deviceId);
144 std::shared_ptr<IpcNotifyDeviceStateReq> pReq =
145 std::static_pointer_cast<IpcNotifyDeviceStateReq>(listener_->ipcServerListener_.req_);
146 DmDeviceInfo dminfo;
147 if (pReq == nullptr) {
148 strcpy_s(dminfo.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
149 } else {
150 dminfo = pReq->GetDeviceInfo();
151 }
152 int result = strcmp(info.deviceId, dminfo.deviceId);
153 EXPECT_EQ(result, 0);
154 }
155
156 /**
157 * @tc.name: OnDeviceReady_001
158 * @tc.desc: set info.deviceId to 123, and call OnDeviceReady ,change info.deviceId to 4
159 * @tc.type: FUNC
160 * @tc.require: AR000GHSJK
161 */
162 HWTEST_F(DmDeviceStateManagerTest, OnDeviceReady_001, testing::ext::TestSize.Level0)
163 {
164 std::string pkgName;
165 DmDeviceInfo info;
166 strcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
167 dmDeviceStateManager->OnDeviceReady(pkgName, info);
168 std::shared_ptr<IpcNotifyDeviceStateReq> pReq =
169 std::static_pointer_cast<IpcNotifyDeviceStateReq>(listener_->ipcServerListener_.req_);
170 DmDeviceInfo dminfo;
171 if (pReq == nullptr) {
172 strcpy_s(dminfo.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
173 } else {
174 dminfo = pReq->GetDeviceInfo();
175 }
176 int result = strcmp(info.deviceId, dminfo.deviceId);
177 EXPECT_EQ(result, 0);
178 }
179
180 /**
181 * @tc.name: OnDeviceChanged_002
182 * @tc.desc: set info.deviceId to 123, and call OnDeviceChanged ,change info.deviceId to 4
183 * @tc.type: FUNC
184 * @tc.require: AR000GHSJK
185 */
186 HWTEST_F(DmDeviceStateManagerTest, OnDeviceChanged_002, testing::ext::TestSize.Level0)
187 {
188 std::string pkgName;
189 DmDeviceInfo info;
190 strcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
191 dmDeviceStateManager->OnDeviceChanged(pkgName, info);
192 std::shared_ptr<IpcNotifyDeviceStateReq> pReq =
193 std::static_pointer_cast<IpcNotifyDeviceStateReq>(listener_->ipcServerListener_.req_);
194 DmDeviceInfo dminfo;
195 if (pReq == nullptr) {
196 strcpy_s(dminfo.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
197 } else {
198 dminfo = pReq->GetDeviceInfo();
199 }
200 int result = strcmp(info.deviceId, dminfo.deviceId);
201 EXPECT_EQ(result, 0);
202 }
203
204 /**
205 * @tc.name: RegisterSoftbusStateCallback_001
206 * @tc.desc: call RegisterSoftbusStateCallback and return DM_OK
207 * @tc.type: FUNC
208 * @tc.require: AR000GHSJK
209 */
210 HWTEST_F(DmDeviceStateManagerTest, RegisterSoftbusStateCallback_001, testing::ext::TestSize.Level0)
211 {
212 EXPECT_NE(dmDeviceStateManager->softbusConnector_, nullptr);
213 int32_t ret = dmDeviceStateManager->RegisterSoftbusStateCallback();
214 EXPECT_EQ(ret, DM_OK);
215 }
216
217 /**
218 * @tc.name: RegisterSoftbusStateCallback_002
219 * @tc.desc: call RegisterSoftbusStateCallback and return DM_OK
220 * @tc.type: FUNC
221 * @tc.require: AR000GHSJK
222 */
223 HWTEST_F(DmDeviceStateManagerTest, RegisterSoftbusStateCallback_002, testing::ext::TestSize.Level0)
224 {
225 dmDeviceStateManager->softbusConnector_ = nullptr;
226 EXPECT_EQ(dmDeviceStateManager->softbusConnector_, nullptr);
227 int32_t ret = dmDeviceStateManager->RegisterSoftbusStateCallback();
228 EXPECT_EQ(ret, DM_OK);
229 }
230
231 /**
232 * @tc.name: PostDeviceOnline_001
233 * @tc.desc: call PostDeviceOnline
234 * @tc.type: FUNC
235 * @tc.require: AR000GHSJK
236 */
237 HWTEST_F(DmDeviceStateManagerTest, PostDeviceOnline_001, testing::ext::TestSize.Level0)
238 {
239 std::string pkgName = "12";
240 DmDeviceInfo info = {
241 .deviceId = "12",
242 .deviceName = "asda",
243 .deviceTypeId = 1,
244 };
245 std::shared_ptr<IDeviceManagerServiceListener> listener_ = dmDeviceStateManager->listener_;
246 dmDeviceStateManager->listener_ = nullptr;
247 dmDeviceStateManager->PostDeviceOnline(pkgName, info);
248 EXPECT_EQ(dmDeviceStateManager->listener_, nullptr);
249 dmDeviceStateManager->listener_ = listener_;
250 }
251
252 /**
253 * @tc.name: PostDeviceOnline_002
254 * @tc.desc: call PostDeviceOnline
255 * @tc.type: FUNC
256 * @tc.require: AR000GHSJK
257 */
258 HWTEST_F(DmDeviceStateManagerTest, PostDeviceOnline_002, testing::ext::TestSize.Level0)
259 {
260 std::string pkgName = "123";
261 DmDeviceInfo info = {
262 .deviceId = "123",
263 .deviceName = "asda",
264 .deviceTypeId = 1,
265 };
266 dmDeviceStateManager->PostDeviceOnline(pkgName, info);
267 EXPECT_NE(dmDeviceStateManager->listener_, nullptr);
268 }
269
270 /**
271 * @tc.name: PostDeviceOffline_001
272 * @tc.desc: call PostDeviceOffline
273 * @tc.type: FUNC
274 * @tc.require: AR000GHSJK
275 */
276 HWTEST_F(DmDeviceStateManagerTest, PostDeviceOffline_001, testing::ext::TestSize.Level0)
277 {
278 std::string pkgName = "12";
279 DmDeviceInfo info = {
280 .deviceId = "12",
281 .deviceName = "asda",
282 .deviceTypeId = 1,
283 };
284 std::shared_ptr<IDeviceManagerServiceListener> listener_ = dmDeviceStateManager->listener_;
285 dmDeviceStateManager->listener_ = nullptr;
286 dmDeviceStateManager->PostDeviceOffline(pkgName, info);
287 EXPECT_EQ(dmDeviceStateManager->listener_, nullptr);
288 dmDeviceStateManager->listener_ = listener_;
289 }
290
291 /**
292 * @tc.name: PostDeviceOffline_002
293 * @tc.desc: call PostDeviceOffline
294 * @tc.type: FUNC
295 * @tc.require: AR000GHSJK
296 */
297 HWTEST_F(DmDeviceStateManagerTest, PostDeviceOffline_002, testing::ext::TestSize.Level0)
298 {
299 std::string pkgName = "123";
300 DmDeviceInfo info = {
301 .deviceId = "123",
302 .deviceName = "asda",
303 .deviceTypeId = 1,
304 };
305 dmDeviceStateManager->PostDeviceOffline(pkgName, info);
306 EXPECT_NE(dmDeviceStateManager->listener_, nullptr);
307 }
308
309 /**
310 * @tc.name: RegisterDevStateCallback_001
311 * @tc.desc: call RegisterDevStateCallback
312 * @tc.type: FUNC
313 * @tc.require: AR000GHSJK
314 */
315 HWTEST_F(DmDeviceStateManagerTest, RegisterDevStateCallback_001, testing::ext::TestSize.Level0)
316 {
317 std::string pkgName = "123";
318 std::string extra = "ahaha";
319 dmDeviceStateManager->RegisterDevStateCallback(pkgName, extra);
320 EXPECT_EQ(dmDeviceStateManager->decisionInfos_.count(pkgName), 1);
321 }
322
323 /**
324 * @tc.name: RegisterDevStateCallback_002
325 * @tc.desc: call RegisterDevStateCallback
326 * @tc.type: FUNC
327 * @tc.require: AR000GHSJK
328 */
329 HWTEST_F(DmDeviceStateManagerTest, RegisterDevStateCallback_002, testing::ext::TestSize.Level0)
330 {
331 std::string pkgName = "";
332 std::string extra = "ahaha";
333 dmDeviceStateManager->RegisterDevStateCallback(pkgName, extra);
334 EXPECT_EQ(dmDeviceStateManager->decisionInfos_.count(pkgName), 0);
335 }
336
337 /**
338 * @tc.name: UnRegisterDevStateCallback_001
339 * @tc.desc: call UnRegisterDevStateCallback
340 * @tc.type: FUNC
341 * @tc.require: AR000GHSJK
342 */
343 HWTEST_F(DmDeviceStateManagerTest, UnRegisterDevStateCallback_001, testing::ext::TestSize.Level0)
344 {
345 std::string pkgName = "";
346 std::string extra;
347 dmDeviceStateManager->UnRegisterDevStateCallback(pkgName, extra);
348 EXPECT_EQ(dmDeviceStateManager->decisionInfos_.count(pkgName), 0);
349 }
350
351 /**
352 * @tc.name: UnRegisterDevStateCallback_002
353 * @tc.desc: call UnRegisterDevStateCallback
354 * @tc.type: FUNC
355 * @tc.require: AR000GHSJK
356 */
357 HWTEST_F(DmDeviceStateManagerTest, UnRegisterDevStateCallback_002, testing::ext::TestSize.Level0)
358 {
359 std::string pkgName = "123";
360 std::string extra;
361 dmDeviceStateManager->UnRegisterDevStateCallback(pkgName, extra);
362 EXPECT_EQ(dmDeviceStateManager->decisionInfos_.count(pkgName), 0);
363 }
364
365 /**
366 * @tc.name: RegisterOffLineTimer_001
367 * @tc.desc: call RegisterOffLineTimer
368 * @tc.type: FUNC
369 * @tc.require: AR000GHSJK
370 */
371 HWTEST_F(DmDeviceStateManagerTest, RegisterOffLineTimer_001, testing::ext::TestSize.Level0)
372 {
373 DmDeviceInfo info = {
374 .deviceId = "12345678901234567890987654321234",
375 .deviceName = "asda",
376 .deviceTypeId = 1,
377 };
378 dmDeviceStateManager->RegisterOffLineTimer(info);
379 std::string deviceId;
380 SoftbusConnector::GetUdidByNetworkId(info.deviceId, deviceId);
381 EXPECT_EQ(dmDeviceStateManager->stateTimerInfoMap_.count(deviceId), 0);
382 }
383
384 /**
385 * @tc.name: SaveOnlineDeviceInfo_001
386 * @tc.desc: call SaveOnlineDeviceInfo
387 * @tc.type: FUNC
388 */
389 HWTEST_F(DmDeviceStateManagerTest, SaveOnlineDeviceInfo_001, testing::ext::TestSize.Level0)
390 {
391 std::string pkgName = "123";
392 DmDeviceInfo info = {
393 .deviceId = "123",
394 .deviceName = "asda",
395 .deviceTypeId = 1,
396 };
397 dmDeviceStateManager->SaveOnlineDeviceInfo(pkgName, info);
398 std::string uuid;
399 SoftbusConnector::GetUuidByNetworkId(info.networkId, uuid);
400 EXPECT_EQ(dmDeviceStateManager->remoteDeviceInfos_.count(uuid), 0);
401 }
402
403 /**
404 * @tc.name: DeleteOfflineDeviceInfo_001
405 * @tc.desc: call DeleteOfflineDeviceInfo
406 * @tc.type: FUNC
407 */
408 HWTEST_F(DmDeviceStateManagerTest, DeleteOfflineDeviceInfo_001, testing::ext::TestSize.Level0)
409 {
410 std::string pkgName = "123";
411 DmDeviceInfo info = {
412 .deviceId = "123",
413 .deviceName = "asda",
414 .deviceTypeId = 1,
415 };
416 dmDeviceStateManager->DeleteOfflineDeviceInfo(pkgName, info);
417 for (auto iter: dmDeviceStateManager->remoteDeviceInfos_) {
418 if (iter.second.deviceId == info.deviceId) {
419 EXPECT_EQ(dmDeviceStateManager->remoteDeviceInfos_.count(iter.first), 0);
420 break;
421 }
422 }
423 }
424 } // namespace
425 } // namespace DistributedHardware
426 } // namespace OHOS
427