1 /*
2 * Copyright (c) 2023-2025 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 "edm_errors.h"
18 #include "hilog_wrapper.h"
19 #define private public
20 #include "dev_change_callback.h"
21 #include "etx_device_mgr.h"
22 #include "ibus_extension.h"
23 #include "usb_bus_extension.h"
24 #include "bus_extension_core.h"
25 #include "driver_pkg_manager.h"
26 #include "usb_device_info.h"
27 #include "usb_driver_info.h"
28 #undef private
29
30 namespace OHOS {
31 namespace ExternalDeviceManager {
32 using namespace std;
33 using namespace testing::ext;
34
35 const unordered_set<std::string> accessibleBundles = {"testBundleName1", "testBundleName2"};
36
37 class DeviceManagerTest : public testing::Test {
38 public:
SetUp()39 void SetUp() override
40 {
41 EDM_LOGD(MODULE_DEV_MGR, "DeviceManagerTest SetUp");
42 }
TearDown()43 void TearDown() override
44 {
45 EDM_LOGD(MODULE_DEV_MGR, "DeviceManagerTest TearDown");
46 }
47 };
48
clearDeviceMap(ExtDeviceManager & instance)49 static void clearDeviceMap(ExtDeviceManager &instance)
50 {
51 unordered_map<BusType, unordered_map<uint64_t, shared_ptr<Device>>> map;
52 instance.deviceMap_ = map;
53 }
54
getDeviceNum(unordered_map<uint64_t,shared_ptr<Device>> map)55 static size_t getDeviceNum(unordered_map<uint64_t, shared_ptr<Device>> map)
56 {
57 size_t num = 0;
58 for (auto &[_, device] : map) {
59 if (!device->IsUnRegisted()) {
60 num++;
61 }
62 }
63 return num;
64 }
65
66 HWTEST_F(DeviceManagerTest, BusExtensionRegisterTest, TestSize.Level1)
67 {
68 BusExtensionCore &core = BusExtensionCore::GetInstance();
69 int32_t ret = core.Register(BusType::BUS_TYPE_USB, std::make_shared<UsbBusExtension>());
70 ASSERT_EQ(ret, EDM_OK);
71 ASSERT_NE(core.busExtensions_[BusType::BUS_TYPE_USB], nullptr);
72 }
73
74 HWTEST_F(DeviceManagerTest, InitTest, TestSize.Level1)
75 {
76 DriverPkgManager::GetInstance().Init();
77 int32_t ret = ExtDeviceManager::GetInstance().Init();
78 ASSERT_EQ(ret, EDM_OK);
79 }
80
81 // test OnDeviceAdd and OnDeviceRemove
82 HWTEST_F(DeviceManagerTest, OnDeviceAddRemoveTest001, TestSize.Level1)
83 {
84 ExtDeviceManager &extMgr = ExtDeviceManager::GetInstance();
85 clearDeviceMap(extMgr);
86 std::shared_ptr<DevChangeCallback> callback = std::make_shared<DevChangeCallback>();
87 std::shared_ptr<DeviceInfo> device = std::make_shared<DeviceInfo>(0);
88 device->devInfo_.devBusInfo.busType = BusType::BUS_TYPE_TEST;
89 device->devInfo_.devBusInfo.busDeviceId = 1;
90 int32_t ret = callback->OnDeviceAdd(device);
91 ASSERT_EQ(ret, EDM_OK);
92 ASSERT_EQ(getDeviceNum(extMgr.deviceMap_[BusType::BUS_TYPE_TEST]), 1);
93 ret = callback->OnDeviceRemove(device);
94 ASSERT_EQ(ret, EDM_OK);
95 ASSERT_EQ(getDeviceNum(extMgr.deviceMap_[BusType::BUS_TYPE_TEST]), 0);
96 }
97
98 // test adding device repeatedly
99 HWTEST_F(DeviceManagerTest, OnDeviceAddRemoveTest002, TestSize.Level1)
100 {
101 ExtDeviceManager &extMgr = ExtDeviceManager::GetInstance();
102 clearDeviceMap(extMgr);
103 std::shared_ptr<DevChangeCallback> callback = std::make_shared<DevChangeCallback>();
104 std::shared_ptr<DeviceInfo> device = std::make_shared<DeviceInfo>(0);
105 device->devInfo_.devBusInfo.busType = BusType::BUS_TYPE_TEST;
106 device->devInfo_.devBusInfo.busDeviceId = 1;
107 int32_t ret = callback->OnDeviceAdd(device);
108 ASSERT_EQ(ret, EDM_OK);
109 ret = callback->OnDeviceAdd(device);
110 ASSERT_EQ(ret, EDM_OK);
111 ASSERT_EQ(getDeviceNum(extMgr.deviceMap_[BusType::BUS_TYPE_TEST]), 1);
112 ret = callback->OnDeviceRemove(device);
113 ASSERT_EQ(ret, EDM_OK);
114 ASSERT_EQ(getDeviceNum(extMgr.deviceMap_[BusType::BUS_TYPE_TEST]), 0);
115 ret = callback->OnDeviceRemove(device);
116 ASSERT_EQ(ret, EDM_OK);
117 }
118
119 HWTEST_F(DeviceManagerTest, OnDeviceAddRemoveTest003, TestSize.Level1)
120 {
121 ExtDeviceManager &extMgr = ExtDeviceManager::GetInstance();
122 clearDeviceMap(extMgr);
123 std::shared_ptr<DevChangeCallback> callback = std::make_shared<DevChangeCallback>();
124 std::shared_ptr<DeviceInfo> device0 = std::make_shared<DeviceInfo>(0);
125 device0->devInfo_.devBusInfo.busType = BusType::BUS_TYPE_TEST;
126 device0->devInfo_.devBusInfo.busDeviceId = 1;
127 int32_t ret = callback->OnDeviceAdd(device0);
128 ASSERT_EQ(ret, EDM_OK);
129 std::shared_ptr<DeviceInfo> device1 = std::make_shared<DeviceInfo>(0);
130 device1->devInfo_.devBusInfo.busType = BusType::BUS_TYPE_TEST;
131 device1->devInfo_.devBusInfo.busDeviceId = 2;
132 ret = callback->OnDeviceAdd(device1);
133 ASSERT_EQ(ret, EDM_OK);
134 ASSERT_EQ(getDeviceNum(extMgr.deviceMap_[BusType::BUS_TYPE_TEST]), 2);
135 ret = callback->OnDeviceRemove(device1);
136 ASSERT_EQ(ret, EDM_OK);
137 ASSERT_EQ(getDeviceNum(extMgr.deviceMap_[BusType::BUS_TYPE_TEST]), 1);
138 ret = callback->OnDeviceRemove(device0);
139 ASSERT_EQ(ret, EDM_OK);
140 ASSERT_EQ(getDeviceNum(extMgr.deviceMap_[BusType::BUS_TYPE_TEST]), 0);
141 }
142
143 HWTEST_F(DeviceManagerTest, QueryDeviceTest, TestSize.Level1)
144 {
145 ExtDeviceManager &extMgr = ExtDeviceManager::GetInstance();
146 clearDeviceMap(extMgr);
147 std::vector<std::shared_ptr<DeviceInfo>> devVec = extMgr.QueryDevice(BUS_TYPE_TEST);
148 ASSERT_EQ(devVec.size(), 0);
149 std::shared_ptr<DevChangeCallback> callback = std::make_shared<DevChangeCallback>();
150 std::shared_ptr<DeviceInfo> device0 = std::make_shared<DeviceInfo>(0);
151 device0->devInfo_.devBusInfo.busType = BusType::BUS_TYPE_TEST;
152 device0->devInfo_.devBusInfo.busDeviceId = 1;
153 int32_t ret = callback->OnDeviceAdd(device0);
154 ASSERT_EQ(ret, EDM_OK);
155 std::shared_ptr<DeviceInfo> device1 = std::make_shared<DeviceInfo>(0);
156 device1->devInfo_.devBusInfo.busType = BusType::BUS_TYPE_TEST;
157 device1->devInfo_.devBusInfo.busDeviceId = 2;
158 ret = callback->OnDeviceAdd(device1);
159 ASSERT_EQ(ret, EDM_OK);
160 devVec = extMgr.QueryDevice(BUS_TYPE_TEST);
161 ASSERT_EQ(devVec.size(), 2);
162 ret = callback->OnDeviceRemove(device0);
163 ret = callback->OnDeviceRemove(device1);
164 ASSERT_EQ(getDeviceNum(extMgr.deviceMap_[BusType::BUS_TYPE_TEST]), 0);
165 }
166
167 HWTEST_F(DeviceManagerTest, GetBusExtensionByNameTest, TestSize.Level1)
168 {
169 BusExtensionCore &core = BusExtensionCore::GetInstance();
170 ASSERT_NE(core.busExtensions_[BusType::BUS_TYPE_USB], nullptr);
171 std::shared_ptr<IBusExtension> extension = core.GetBusExtensionByName("HDMI");
172 ASSERT_EQ(extension, nullptr);
173 extension = core.GetBusExtensionByName("USB");
174 ASSERT_NE(extension, nullptr);
175 core.busExtensions_.erase(BusType::BUS_TYPE_USB);
176 extension = core.GetBusExtensionByName("USB");
177 ASSERT_EQ(extension, nullptr);
178 }
179
180 class TestRemoteObjectStub : public IRemoteObject {
181 public:
TestRemoteObjectStub()182 TestRemoteObjectStub() : IRemoteObject(u"IRemoteObject") {}
GetObjectRefCount()183 int32_t GetObjectRefCount() { return 0; };
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)184 int SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { return 0; };
AddDeathRecipient(const sptr<DeathRecipient> & recipient)185 bool AddDeathRecipient(const sptr<DeathRecipient> &recipient) { return true; };
RemoveDeathRecipient(const sptr<DeathRecipient> & recipient)186 bool RemoveDeathRecipient(const sptr<DeathRecipient> &recipient) { return true; };
Dump(int fd,const std::vector<std::u16string> & args)187 int Dump(int fd, const std::vector<std::u16string> &args) { return 0; };
188 };
189
190 class TestDriverExtMgrCallback : public IDriverExtMgrCallback {
191 public:
TestDriverExtMgrCallback()192 TestDriverExtMgrCallback() {}
AsObject()193 sptr<IRemoteObject> AsObject()
194 {
195 return sptr<TestRemoteObjectStub>::MakeSptr();
196 };
OnConnect(uint64_t deviceId,const sptr<IRemoteObject> & drvExtObj,const ErrMsg & errMsg)197 ErrCode OnConnect(uint64_t deviceId, const sptr<IRemoteObject> &drvExtObj, const ErrMsg &errMsg)
198 {
199 EDM_LOGD(MODULE_DEV_MGR, "TestDriverExtMgrCallback::OnConnect entry");
200 return EDM_OK;
201 };
OnDisconnect(uint64_t deviceId,const ErrMsg & errMsg)202 ErrCode OnDisconnect(uint64_t deviceId, const ErrMsg &errMsg)
203 {
204 EDM_LOGD(MODULE_DEV_MGR, "TestDriverExtMgrCallback::OnConnect entry");
205 return EDM_OK;
206 };
OnUnBind(uint64_t deviceId,const ErrMsg & errMsg)207 ErrCode OnUnBind(uint64_t deviceId, const ErrMsg &errMsg)
208 {
209 EDM_LOGD(MODULE_DEV_MGR, "TestDriverExtMgrCallback::OnConnect entry");
210 return EDM_OK;
211 };
212 };
213
214 HWTEST_F(DeviceManagerTest, ConnectDeviceTest, TestSize.Level1)
215 {
216 ExtDeviceManager &extMgr = ExtDeviceManager::GetInstance();
217 clearDeviceMap(extMgr);
218 uint64_t deviceId = 3;
219 const uint32_t tokenId1 = 1;
220 sptr<IDriverExtMgrCallback> connectCallback = sptr<TestDriverExtMgrCallback>::MakeSptr();
221 int32_t ret = extMgr.ConnectDevice(deviceId, tokenId1, connectCallback);
222 ASSERT_EQ(ret, EDM_NOK);
223 std::shared_ptr<Device> device = extMgr.QueryDeviceByDeviceID(deviceId);
224 ASSERT_EQ(device, nullptr);
225
226 std::shared_ptr<DevChangeCallback> callback = std::make_shared<DevChangeCallback>();
227 std::shared_ptr<DeviceInfo> deviceInfo = std::make_shared<DeviceInfo>(deviceId,
228 BusType::BUS_TYPE_TEST, "testInfo1");
229 deviceInfo->devInfo_.deviceId = deviceId;
230 ret = callback->OnDeviceAdd(deviceInfo);
231 ASSERT_EQ(ret, EDM_OK);
232 ASSERT_EQ(getDeviceNum(extMgr.deviceMap_[BusType::BUS_TYPE_TEST]), 1);
233 device = extMgr.QueryDeviceByDeviceID(deviceId);
234 ASSERT_NE(device, nullptr);
235 device->driverInfo_ = make_shared<DriverInfo>("testBundleName1", "testDriverName1");
236 ASSERT_EQ(device->driverInfo_->launchOnBind_, false);
237 ret = extMgr.ConnectDevice(deviceId, tokenId1, connectCallback);
238 ASSERT_NE(ret, EDM_OK);
239
240 sptr<IRemoteObject> remote = sptr<TestRemoteObjectStub>::MakeSptr();
241 int resultCode = static_cast<int>(UsbErrCode::EDM_OK);
242 device->OnConnect(remote, resultCode);
243 ASSERT_EQ(device->boundCallerInfos_.size(), 0);
244 ASSERT_NE(device->drvExtRemote_, nullptr);
245 device->driverInfo_->accessAllowed_ = true;
246 ret = extMgr.ConnectDevice(deviceId, tokenId1, connectCallback);
247 ASSERT_EQ(ret, EDM_OK);
248 ASSERT_EQ(device->boundCallerInfos_.size(), 1);
249 const uint32_t tokenId2 = 2;
250 ret = extMgr.ConnectDevice(deviceId, tokenId2, connectCallback);
251 ASSERT_EQ(ret, EDM_OK);
252 ASSERT_EQ(device->boundCallerInfos_.size(), 2);
253 auto iter = device->boundCallerInfos_.find(tokenId1);
254 ASSERT_NE(iter, device->boundCallerInfos_.end());
255 ASSERT_EQ(iter->second.isBound, true);
256 iter = device->boundCallerInfos_.find(tokenId2);
257 ASSERT_NE(iter, device->boundCallerInfos_.end());
258 ASSERT_EQ(iter->second.isBound, true);
259 }
260
261 HWTEST_F(DeviceManagerTest, ConnectDeviceTest1, TestSize.Level1)
262 {
263 ExtDeviceManager &extMgr = ExtDeviceManager::GetInstance();
264 clearDeviceMap(extMgr);
265 uint64_t deviceId = 3;
266 const uint32_t tokenId = 1;
267 std::shared_ptr<DevChangeCallback> callback = std::make_shared<DevChangeCallback>();
268 std::shared_ptr<DeviceInfo> deviceInfo = std::make_shared<DeviceInfo>(deviceId,
269 BusType::BUS_TYPE_TEST, "testInfo1");
270 deviceInfo->devInfo_.deviceId = deviceId;
271 int32_t ret = callback->OnDeviceAdd(deviceInfo);
272 ASSERT_EQ(ret, EDM_OK);
273 ASSERT_EQ(getDeviceNum(extMgr.deviceMap_[BusType::BUS_TYPE_TEST]), 1);
274 std::shared_ptr<Device> device = extMgr.QueryDeviceByDeviceID(deviceId);
275 ASSERT_NE(device, nullptr);
276 sptr<IDriverExtMgrCallback> connectCallback = sptr<TestDriverExtMgrCallback>::MakeSptr();
277
278 device->driverInfo_ = make_shared<DriverInfo>("testBundleName1", "testDriverName1");
279 device->driverInfo_->accessAllowed_ = false;
280 ret = extMgr.ConnectDriverWithDeviceId(deviceId, tokenId, accessibleBundles, connectCallback);
281 ASSERT_EQ(ret, EDM_ERR_SERVICE_NOT_ALLOW_ACCESS);
282
283 device->driverInfo_->accessAllowed_ = true;
284 ret = extMgr.ConnectDriverWithDeviceId(deviceId, tokenId, accessibleBundles, connectCallback);
285 ASSERT_NE(ret, EDM_ERR_SERVICE_NOT_ALLOW_ACCESS);
286 }
287
288 HWTEST_F(DeviceManagerTest, ConnectDeviceTest2, TestSize.Level1)
289 {
290 ExtDeviceManager &extMgr = ExtDeviceManager::GetInstance();
291 clearDeviceMap(extMgr);
292 uint64_t deviceId = 3;
293 const uint32_t tokenId = 1;
294 std::shared_ptr<DevChangeCallback> callback = std::make_shared<DevChangeCallback>();
295 std::shared_ptr<DeviceInfo> deviceInfo = std::make_shared<DeviceInfo>(deviceId,
296 BusType::BUS_TYPE_TEST, "testInfo1");
297 deviceInfo->devInfo_.deviceId = deviceId;
298 int32_t ret = callback->OnDeviceAdd(deviceInfo);
299 ASSERT_EQ(ret, EDM_OK);
300 ASSERT_EQ(getDeviceNum(extMgr.deviceMap_[BusType::BUS_TYPE_TEST]), 1);
301 std::shared_ptr<Device> device = extMgr.QueryDeviceByDeviceID(deviceId);
302 ASSERT_NE(device, nullptr);
303 sptr<IDriverExtMgrCallback> connectCallback = sptr<TestDriverExtMgrCallback>::MakeSptr();
304
305 device->driverInfo_ = make_shared<DriverInfo>("testBundleName3", "testDriverName3");
306 device->driverInfo_->accessAllowed_ = true;
307 ret = extMgr.ConnectDriverWithDeviceId(deviceId, tokenId, accessibleBundles, connectCallback);
308 ASSERT_EQ(ret, EDM_ERR_NO_PERM);
309
310 device->driverInfo_->bundleName_ = "testBundleName1";
311 ret = extMgr.ConnectDriverWithDeviceId(deviceId, tokenId, accessibleBundles, connectCallback);
312 ASSERT_NE(ret, EDM_ERR_NO_PERM);
313 }
314
315 HWTEST_F(DeviceManagerTest, ConnectDeviceTest3, TestSize.Level1)
316 {
317 ExtDeviceManager &extMgr = ExtDeviceManager::GetInstance();
318 clearDeviceMap(extMgr);
319 uint32_t busDeviceId = 3;
320 const uint32_t tokenId1 = 1;
321 sptr<IDriverExtMgrCallback> connectCallback = nullptr;
322 std::shared_ptr<DevChangeCallback> callback = std::make_shared<DevChangeCallback>();
323 auto usbDeviceInfo = std::make_shared<UsbDeviceInfo>(busDeviceId, "testInfo1");
324 auto deviceId = usbDeviceInfo->GetDeviceId();
325 usbDeviceInfo->deviceClass_ = 1;
326 usbDeviceInfo->deviceSubClass_ = 2;
327 usbDeviceInfo->deviceProtocol_ = 3;
328 usbDeviceInfo->idVendor_ = 4;
329 usbDeviceInfo->idProduct_ = 5;
330 usbDeviceInfo->snNum_ = "testSnNum";
331 auto ret = callback->OnDeviceAdd(usbDeviceInfo);
332 ASSERT_EQ(ret, EDM_OK);
333 ASSERT_EQ(getDeviceNum(extMgr.deviceMap_[BusType::BUS_TYPE_USB]), 1);
334 auto device = extMgr.QueryDeviceByDeviceID(deviceId);
335 ASSERT_NE(device, nullptr);
336 device->driverInfo_ = make_shared<DriverInfo>("testBundleName1", "testDriverName1", "testDriverUid1", 123);
337 device->driverInfo_->version_ = "testVersion1";
338 device->driverInfo_->driverSize_ = "testDriverSize1";
339 device->driverInfo_->busType_ = BusType::BUS_TYPE_USB;
340 auto driverExt = make_shared<UsbDriverInfo>();
341 driverExt->pids_ = std::vector<uint16_t>{6};
342 driverExt->vids_ = std::vector<uint16_t>{7};
343 device->driverInfo_->driverInfoExt_ = driverExt;
344 device->driverInfo_->accessAllowed_ = false;
345 ret = device->Connect(connectCallback, tokenId1);
346 ASSERT_EQ(ret, EDM_ERR_INVALID_OBJECT);
347 connectCallback = sptr<TestDriverExtMgrCallback>::MakeSptr();
348 ret = device->Connect(connectCallback, tokenId1);
349 ASSERT_NE(ret, EDM_OK);
350 ret = extMgr.ConnectDriverWithDeviceId(deviceId, tokenId1, accessibleBundles, connectCallback);
351 ASSERT_EQ(ret, EDM_ERR_SERVICE_NOT_ALLOW_ACCESS);
352 }
353
354 HWTEST_F(DeviceManagerTest, DisConnectDeviceTest, TestSize.Level1)
355 {
356 ExtDeviceManager &extMgr = ExtDeviceManager::GetInstance();
357 clearDeviceMap(extMgr);
358 uint64_t deviceId = 3;
359 const uint32_t tokenId1 = 1;
360 sptr<IDriverExtMgrCallback> connectCallback = sptr<TestDriverExtMgrCallback>::MakeSptr();
361 std::shared_ptr<DevChangeCallback> callback = std::make_shared<DevChangeCallback>();
362 std::shared_ptr<DeviceInfo> deviceInfo = std::make_shared<DeviceInfo>(deviceId,
363 BusType::BUS_TYPE_TEST, "testInfo2");
364 deviceInfo->devInfo_.deviceId = deviceId;
365 int32_t ret = callback->OnDeviceAdd(deviceInfo);
366 ASSERT_EQ(ret, EDM_OK);
367 ASSERT_EQ(getDeviceNum(extMgr.deviceMap_[BusType::BUS_TYPE_TEST]), 1);
368 std::shared_ptr<Device> device = extMgr.QueryDeviceByDeviceID(deviceId);
369 device->driverInfo_ = make_shared<DriverInfo>("testBundleName2", "testDriverName2");
370 ret = extMgr.ConnectDevice(deviceId, tokenId1, connectCallback);
371 ASSERT_NE(ret, EDM_OK);
372
373 sptr<IRemoteObject> remote = sptr<TestRemoteObjectStub>::MakeSptr();
374 device->OnConnect(remote, static_cast<int>(UsbErrCode::EDM_OK));
375 ASSERT_EQ(device->boundCallerInfos_.size(), 0);
376 ASSERT_NE(device->drvExtRemote_, nullptr);
377 device->driverInfo_->accessAllowed_ = true;
378 ret = extMgr.ConnectDevice(deviceId, tokenId1, connectCallback);
379 ASSERT_EQ(ret, EDM_OK);
380 ASSERT_EQ(device->boundCallerInfos_.size(), 1);
381
382 const uint32_t tokenId2 = 2;
383 ret = extMgr.ConnectDevice(deviceId, tokenId2, connectCallback);
384 ASSERT_EQ(ret, EDM_OK);
385 ASSERT_EQ(device->boundCallerInfos_.size(), 2);
386 auto iter = device->boundCallerInfos_.find(tokenId1);
387 ASSERT_NE(iter, device->boundCallerInfos_.end());
388 ASSERT_EQ(iter->second.isBound, true);
389 iter = device->boundCallerInfos_.find(tokenId2);
390 ASSERT_NE(iter, device->boundCallerInfos_.end());
391 ASSERT_EQ(iter->second.isBound, true);
392 ret = extMgr.DisConnectDevice(deviceId, tokenId2);
393 ASSERT_EQ(ret, EDM_OK);
394 ASSERT_EQ(device->boundCallerInfos_.size(), 1);
395 iter = device->boundCallerInfos_.begin();
396 ASSERT_EQ(iter->first, tokenId1);
397 ASSERT_EQ(iter->second.isBound, true);
398 device->driverInfo_->launchOnBind_ = true;
399 ret = extMgr.DisConnectDevice(deviceId, tokenId1);
400 ASSERT_NE(ret, EDM_OK);
401 ASSERT_EQ(device->boundCallerInfos_.size(), 1);
402 device->driverInfo_->launchOnBind_ = false;
403 ret = extMgr.DisConnectDevice(deviceId, tokenId1);
404 ASSERT_EQ(ret, EDM_OK);
405 ASSERT_EQ(device->boundCallerInfos_.size(), 0);
406 }
407
408 HWTEST_F(DeviceManagerTest, DisConnectDeviceTest1, TestSize.Level1)
409 {
410 ExtDeviceManager &extMgr = ExtDeviceManager::GetInstance();
411 clearDeviceMap(extMgr);
412 uint64_t deviceId = 3;
413 const uint32_t tokenId1 = 1;
414 std::shared_ptr<DevChangeCallback> callback = std::make_shared<DevChangeCallback>();
415 std::shared_ptr<DeviceInfo> deviceInfo = std::make_shared<DeviceInfo>(deviceId,
416 BusType::BUS_TYPE_TEST, "testInfo2");
417 deviceInfo->devInfo_.deviceId = deviceId;
418 int32_t ret = callback->OnDeviceAdd(deviceInfo);
419 ASSERT_EQ(ret, EDM_OK);
420 ASSERT_EQ(getDeviceNum(extMgr.deviceMap_[BusType::BUS_TYPE_TEST]), 1);
421 std::shared_ptr<Device> device = extMgr.QueryDeviceByDeviceID(deviceId);
422 device->driverInfo_ = make_shared<DriverInfo>("testBundleName2", "testDriverName2");
423 sptr<IRemoteObject> remote = sptr<TestRemoteObjectStub>::MakeSptr();
424 device->OnConnect(remote, static_cast<int>(UsbErrCode::EDM_OK));
425 ASSERT_NE(device->drvExtRemote_, nullptr);
426 sptr<IDriverExtMgrCallback> connectCallback = sptr<TestDriverExtMgrCallback>::MakeSptr();
427
428 device->driverInfo_->accessAllowed_ = true;
429 ret = extMgr.ConnectDriverWithDeviceId(deviceId, tokenId1, accessibleBundles, connectCallback);
430 ASSERT_EQ(ret, EDM_OK);
431 ASSERT_EQ(device->boundCallerInfos_.size(), 1);
432
433 const uint32_t tokenId2 = 2;
434 ret = extMgr.DisConnectDriverWithDeviceId(deviceId, tokenId2);
435 ASSERT_EQ(ret, EDM_ERR_SERVICE_NOT_BOUND);
436
437 ret = extMgr.DisConnectDriverWithDeviceId(deviceId, tokenId1);
438 ASSERT_NE(ret, EDM_ERR_SERVICE_NOT_BOUND);
439 }
440
441 HWTEST_F(DeviceManagerTest, DisConnectDeviceTest2, TestSize.Level1)
442 {
443 ExtDeviceManager &extMgr = ExtDeviceManager::GetInstance();
444 clearDeviceMap(extMgr);
445 uint32_t busDeviceId = 3;
446 const uint32_t tokenId1 = 1;
447 std::shared_ptr<DevChangeCallback> callback = std::make_shared<DevChangeCallback>();
448 auto usbDeviceInfo = std::make_shared<UsbDeviceInfo>(busDeviceId, "testInfo1");
449 auto deviceId = usbDeviceInfo->GetDeviceId();
450 usbDeviceInfo->deviceClass_ = 1;
451 usbDeviceInfo->deviceSubClass_ = 2;
452 usbDeviceInfo->deviceProtocol_ = 3;
453 usbDeviceInfo->idVendor_ = 4;
454 usbDeviceInfo->idProduct_ = 5;
455 usbDeviceInfo->snNum_ = "testSnNum";
456 auto ret = callback->OnDeviceAdd(usbDeviceInfo);
457 ASSERT_EQ(ret, EDM_OK);
458 ASSERT_EQ(getDeviceNum(extMgr.deviceMap_[BusType::BUS_TYPE_USB]), 1);
459 auto device = extMgr.QueryDeviceByDeviceID(deviceId);
460 ASSERT_NE(device, nullptr);
461 ret = extMgr.DisConnectDevice(deviceId, tokenId1);
462 ASSERT_NE(ret, EDM_OK);
463 device->driverInfo_ = make_shared<DriverInfo>("testBundleName1", "testDriverName1", "testDriverUid1", 123);
464 device->driverInfo_->version_ = "testVersion1";
465 device->driverInfo_->driverSize_ = "testDriverSize1";
466 device->driverInfo_->busType_ = BusType::BUS_TYPE_USB;
467 auto driverExt = make_shared<UsbDriverInfo>();
468 driverExt->pids_ = std::vector<uint16_t>{6};
469 driverExt->vids_ = std::vector<uint16_t>{7};
470 device->driverInfo_->driverInfoExt_ = driverExt;
471 sptr<IRemoteObject> remote = sptr<TestRemoteObjectStub>::MakeSptr();
472 device->OnConnect(remote, static_cast<int>(UsbErrCode::EDM_OK));
473 ASSERT_NE(device->drvExtRemote_, nullptr);
474 sptr<IDriverExtMgrCallback> connectCallback = sptr<TestDriverExtMgrCallback>::MakeSptr();
475
476 device->driverInfo_->accessAllowed_ = true;
477 ret = extMgr.ConnectDriverWithDeviceId(deviceId, tokenId1, accessibleBundles, connectCallback);
478 ASSERT_EQ(ret, EDM_OK);
479 ASSERT_EQ(device->boundCallerInfos_.size(), 1);
480
481 const uint32_t tokenId2 = 2;
482 ret = extMgr.DisConnectDriverWithDeviceId(deviceId, tokenId2);
483 ASSERT_EQ(ret, EDM_ERR_SERVICE_NOT_BOUND);
484 }
485 } // namespace ExternalDeviceManager
486 } // namespace OHOS
487