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