• 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 "dtbschedmgr_device_info_storage_test.h"
17 
18 #include "dtbschedmgr_log.h"
19 #include "test_log.h"
20 #include <cstddef>
21 
22 namespace OHOS {
23 namespace DistributedSchedule {
24 using namespace std;
25 using namespace testing;
26 using namespace testing::ext;
27 namespace {
28 constexpr int32_t MAX_WAIT_TIME = 10000;
29 }
30 
31 class MockDmsNotifier : public DmsNotifier {
32 public:
33     MockDmsNotifier() = default;
34     ~MockDmsNotifier() = default;
35 
36     void DeviceOnlineNotify(const std::string& deviceId) override;
37     void DeviceOfflineNotify(const std::string& deviceId) override;
38     void ProcessNotifierDied(const sptr<IRemoteObject>& notifier) override;
39     void ScheduleStartDeviceManager(const sptr<IRemoteObject>& appProxy, int32_t token,
40         const std::shared_ptr<ContinuationExtraParams>& continuationExtraParams = nullptr) override;
41     int32_t OnDeviceConnect(int32_t token, const std::vector<ContinuationResult>& continuationResults) override;
42     int32_t OnDeviceDisconnect(int32_t token, const std::vector<ContinuationResult>& continuationResults) override;
43     int32_t OnDeviceCancel() override;
44 };
45 
DeviceOnlineNotify(const std::string & deviceId)46 void MockDmsNotifier::DeviceOnlineNotify(const std::string& deviceId)
47 {
48 }
49 
DeviceOfflineNotify(const std::string & deviceId)50 void MockDmsNotifier::DeviceOfflineNotify(const std::string& deviceId)
51 {
52 }
53 
ProcessNotifierDied(const sptr<IRemoteObject> & notifier)54 void MockDmsNotifier::ProcessNotifierDied(const sptr<IRemoteObject>& notifier)
55 {
56 }
57 
ScheduleStartDeviceManager(const sptr<IRemoteObject> & appProxy,int32_t token,const std::shared_ptr<ContinuationExtraParams> & continuationExtraParams)58 void MockDmsNotifier::ScheduleStartDeviceManager(const sptr<IRemoteObject>& appProxy, int32_t token,
59     const std::shared_ptr<ContinuationExtraParams>& continuationExtraParams)
60 {
61 }
62 
OnDeviceConnect(int32_t token,const std::vector<ContinuationResult> & continuationResults)63 int32_t MockDmsNotifier::OnDeviceConnect(int32_t token, const std::vector<ContinuationResult>& continuationResults)
64 {
65     return 0;
66 }
67 
OnDeviceDisconnect(int32_t token,const std::vector<ContinuationResult> & continuationResults)68 int32_t MockDmsNotifier::OnDeviceDisconnect(int32_t token, const std::vector<ContinuationResult>& continuationResults)
69 {
70     return 0;
71 }
72 
OnDeviceCancel()73 int32_t MockDmsNotifier::OnDeviceCancel()
74 {
75     return 0;
76 }
77 
SetUpTestCase()78 void DtbschedmgrDeviceInfoStorageTest::SetUpTestCase()
79 {
80     DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest::SetUpTestCase" << std::endl;
81 }
82 
83 bool DtbschedmgrDeviceInfoStorageTest::isCaseDone_ = false;
84 std::mutex DtbschedmgrDeviceInfoStorageTest::caseDoneLock_;
85 std::condition_variable DtbschedmgrDeviceInfoStorageTest::caseDoneCondition_;
86 
TearDownTestCase()87 void DtbschedmgrDeviceInfoStorageTest::TearDownTestCase()
88 {
89     // Wait until all asyn tasks are completed before exiting the test suite
90     auto caseDoneNotifyTask = []() {
91         std::lock_guard<std::mutex> autoLock(caseDoneLock_);
92         isCaseDone_ = true;
93         caseDoneCondition_.notify_all();
94     };
95     if (DtbschedmgrDeviceInfoStorage::GetInstance().networkIdMgrHandler_ != nullptr) {
96         DtbschedmgrDeviceInfoStorage::GetInstance().networkIdMgrHandler_->PostTask(caseDoneNotifyTask);
97     }
98 
99     std::unique_lock<std::mutex> lock(caseDoneLock_);
100     caseDoneCondition_.wait_for(lock, std::chrono::milliseconds(MAX_WAIT_TIME),
101         [&] () { return isCaseDone_; });
102     DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest::TearDownTestCase" << std::endl;
103 }
104 
SetUp()105 void DtbschedmgrDeviceInfoStorageTest::SetUp()
106 {
107     DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest::SetUp" << std::endl;
108 }
109 
TearDown()110 void DtbschedmgrDeviceInfoStorageTest::TearDown()
111 {
112     DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest::TearDown" << std::endl;
113 }
114 
115 /**
116  * @tc.name: InitTest_001
117  * @tc.desc: test Init
118  * @tc.type: FUNC
119  */
120 HWTEST_F(DtbschedmgrDeviceInfoStorageTest, InitTest_001, TestSize.Level3)
121 {
122     DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest InitTest_001 start" << std::endl;
123     /**
124      * @tc.steps: step1. test Init with listener when initHandler_ is nullptr;
125      */
126     sptr<DmsNotifier> listener = nullptr;
127     DtbschedmgrDeviceInfoStorage::GetInstance().initHandler_ = nullptr;
128     bool result = DtbschedmgrDeviceInfoStorage::GetInstance().Init(listener);
129     EXPECT_TRUE(result);
130     /**
131      * @tc.steps: step2. test Init when initHandler_ is not nullptr;
132      */
133     result = DtbschedmgrDeviceInfoStorage::GetInstance().Init();
134     EXPECT_TRUE(result);
135     DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest InitTest_001 end" << std::endl;
136 }
137 
138 /**
139  * @tc.name: ConnectSoftbusTest_001
140  * @tc.desc: test ConnectSoftbus
141  * @tc.type: FUNC
142  */
143 HWTEST_F(DtbschedmgrDeviceInfoStorageTest, ConnectSoftbusTest_001, TestSize.Level3)
144 {
145     DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest ConnectSoftbusTest_001 start" << std::endl;
146     /**
147      * @tc.steps: step1. test ConnectSoftbus;
148      */
149     bool result = DtbschedmgrDeviceInfoStorage::GetInstance().ConnectSoftbus();
150     EXPECT_TRUE(result);
151     DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest ConnectSoftbusTest_001 end" << std::endl;
152 }
153 
154 /**
155  * @tc.name: InitNetworkIdManagerTest_001
156  * @tc.desc: test InitNetworkIdManager
157  * @tc.type: FUNC
158  */
159 HWTEST_F(DtbschedmgrDeviceInfoStorageTest, InitNetworkIdManagerTest_001, TestSize.Level3)
160 {
161     DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest InitNetworkIdManagerTest_001 start" << std::endl;
162     std::shared_ptr<DnetworkAdapter> dnetworkAdapter = DnetworkAdapter::GetInstance();
163     DtbschedmgrDeviceInfoStorage::GetInstance().networkIdMgrHandler_ = nullptr;
164     /**
165      * @tc.steps: step1. test InitNetworkIdManager when networkIdMgrHandler_ is nullptr;
166      */
167     bool result = DtbschedmgrDeviceInfoStorage::GetInstance().InitNetworkIdManager(dnetworkAdapter);
168     EXPECT_TRUE(result);
169     /**
170      * @tc.steps: step2. test InitNetworkIdManager when networkIdMgrHandler_ is not nullptr;
171      */
172     auto runner = AppExecFwk::EventRunner::Create("DmsNetworkIdManager");
173     DtbschedmgrDeviceInfoStorage::GetInstance().networkIdMgrHandler_ =
174         std::make_shared<AppExecFwk::EventHandler>(runner);
175     result = DtbschedmgrDeviceInfoStorage::GetInstance().InitNetworkIdManager(dnetworkAdapter);
176     EXPECT_TRUE(result);
177     /**
178      * @tc.steps: step3. test Stop when deviceNodeListener_ is nullptr;
179      */
180     DtbschedmgrDeviceInfoStorage::GetInstance().deviceNodeListener_ = nullptr;
181     DtbschedmgrDeviceInfoStorage::GetInstance().Stop();
182     /**
183      * @tc.steps: step4. test Stop when deviceNodeListener_ is not nullptr;
184      */
185     DtbschedmgrDeviceInfoStorage::GetInstance().deviceNodeListener_ = std::make_shared<DistributedDeviceNodeListener>();
186     DtbschedmgrDeviceInfoStorage::GetInstance().Stop();
187     DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest InitNetworkIdManagerTest_001 end" << std::endl;
188 }
189 
190 /**
191  * @tc.name: WaitForDnetworkReadyTest_001
192  * @tc.desc: test WaitForDnetworkReady
193  * @tc.type: FUNC
194  */
195 HWTEST_F(DtbschedmgrDeviceInfoStorageTest, WaitForDnetworkReadyTest_001, TestSize.Level3)
196 {
197     DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest WaitForDnetworkReadyTest_001 start" << std::endl;
198     /**
199      * @tc.steps: step1. test WaitForDnetworkReady;
200      */
201     bool result = DtbschedmgrDeviceInfoStorage::GetInstance().WaitForDnetworkReady();
202     EXPECT_TRUE(result);
203     /**
204      * @tc.steps: step2. test RegisterUuidNetworkIdMap;
205      */
206     std::string networkId = "";
207     DtbschedmgrDeviceInfoStorage::GetInstance().RegisterUuidNetworkIdMap(networkId);
208     /**
209      * @tc.steps: step3. test UnregisterUuidNetworkIdMap;
210      */
211     DtbschedmgrDeviceInfoStorage::GetInstance().UnregisterUuidNetworkIdMap(networkId);
212     DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest WaitForDnetworkReadyTest_001 end" << std::endl;
213 }
214 
215 /**
216  * @tc.name: GetDeviceIdSetTest_001
217  * @tc.desc: test GetDeviceIdSet
218  * @tc.type: FUNC
219  */
220 HWTEST_F(DtbschedmgrDeviceInfoStorageTest, GetDeviceIdSetTest_001, TestSize.Level3)
221 {
222     DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest GetDeviceIdSetTest_001 start" << std::endl;
223     /**
224      * @tc.steps: step1. test GetDeviceIdSet;
225      */
226     set<std::string> deviceIdSet;
227     DtbschedmgrDeviceInfoStorage::GetInstance().GetDeviceIdSet(deviceIdSet);
228     EXPECT_TRUE(deviceIdSet.empty());
229     /**
230      * @tc.steps: step2. test GetDeviceIdSet when remoteDevices_ is not empty;
231      */
232     std::string deviceId = "invalid deviceId for DeviceOnlineNotify";
233     std::string deviceName = "invalid deviceName for DeviceOnlineNotify";
234     int32_t deviceType = 0;
235     std::shared_ptr<DmsDeviceInfo> devInfo = make_shared<DmsDeviceInfo>(deviceName, deviceType, deviceId);
236     DtbschedmgrDeviceInfoStorage::GetInstance().remoteDevices_[deviceId] = devInfo;
237     DtbschedmgrDeviceInfoStorage::GetInstance().GetDeviceIdSet(deviceIdSet);
238     EXPECT_FALSE(deviceIdSet.empty());
239     DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest GetDeviceIdSetTest_001 end" << std::endl;
240 }
241 
242 /**
243  * @tc.name: GetLocalDeviceFromDnetTest_001
244  * @tc.desc: test GetLocalDeviceFromDnet
245  * @tc.type: FUNC
246  */
247 HWTEST_F(DtbschedmgrDeviceInfoStorageTest, GetLocalDeviceFromDnetTest_001, TestSize.Level3)
248 {
249     DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest GetLocalDeviceFromDnetTest_001 start" << std::endl;
250     std::string deviceId;
251     /**
252      * @tc.steps: step1. test GetLocalDeviceFromDnet;
253      */
254     bool result = DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalDeviceId(deviceId);
255     EXPECT_TRUE(result);
256     /**
257      * @tc.steps: step2. test DeviceOnlineNotify when devInfo is nullptr;
258      */
259     std::shared_ptr<DmsDeviceInfo> devInfo = nullptr;
260     DtbschedmgrDeviceInfoStorage::GetInstance().DeviceOnlineNotify(devInfo);
261     /**
262      * @tc.steps: step3. test DeviceOnlineNotify when networkIdMgrHandler_ is nullptr;
263      */
264     deviceId = "invalid deviceId for DeviceOnlineNotify";
265     std::string deviceName = "invalid deviceName for DeviceOnlineNotify";
266     int32_t deviceType = 0;
267     devInfo = make_shared<DmsDeviceInfo>(deviceName, deviceType, deviceId);
268     DtbschedmgrDeviceInfoStorage::GetInstance().networkIdMgrHandler_ = nullptr;
269     DtbschedmgrDeviceInfoStorage::GetInstance().DeviceOnlineNotify(devInfo);
270     /**
271      * @tc.steps: step4. test DeviceOnlineNotify when listener_ is nullptr;
272      */
273     auto runner = AppExecFwk::EventRunner::Create("DmsNetworkIdManager");
274     DtbschedmgrDeviceInfoStorage::GetInstance().networkIdMgrHandler_ =
275         std::make_shared<AppExecFwk::EventHandler>(runner);
276     DtbschedmgrDeviceInfoStorage::GetInstance().listener_ = nullptr;
277     DtbschedmgrDeviceInfoStorage::GetInstance().DeviceOnlineNotify(devInfo);
278     /**
279      * @tc.steps: step5. test DeviceOnlineNotify when listener_ is not nullptr;
280      */
281     DtbschedmgrDeviceInfoStorage::GetInstance().listener_ = new MockDmsNotifier();
282     DtbschedmgrDeviceInfoStorage::GetInstance().DeviceOnlineNotify(devInfo);
283     DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest GetLocalDeviceFromDnetTest_001 end" << std::endl;
284 }
285 
286 /**
287  * @tc.name: ClearAllDevicesTest_001
288  * @tc.desc: test ClearAllDevices
289  * @tc.type: FUNC
290  */
291 HWTEST_F(DtbschedmgrDeviceInfoStorageTest, ClearAllDevicesTest_001, TestSize.Level3)
292 {
293     DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest ClearAllDevicesTest_001 start" << std::endl;
294     /**
295      * @tc.steps: step1. test ClearAllDevices;
296      */
297     DtbschedmgrDeviceInfoStorage::GetInstance().ClearAllDevices();
298     {
299         lock_guard<mutex> autoLock(DtbschedmgrDeviceInfoStorage::GetInstance().deviceLock_);
300         EXPECT_TRUE(DtbschedmgrDeviceInfoStorage::GetInstance().remoteDevices_.empty());
301     }
302     /**
303      * @tc.steps: step2. test DeviceOfflineNotify when deviceId is empty;
304      */
305     std::string deviceId;
306     DtbschedmgrDeviceInfoStorage::GetInstance().DeviceOfflineNotify(deviceId);
307     /**
308      * @tc.steps: step3. test DeviceOfflineNotify when networkIdMgrHandler_ is nullptr;
309      */
310     deviceId = "invalid deviceId for DeviceOnlineNotify";
311     DtbschedmgrDeviceInfoStorage::GetInstance().networkIdMgrHandler_ = nullptr;
312     DtbschedmgrDeviceInfoStorage::GetInstance().DeviceOfflineNotify(deviceId);
313     /**
314      * @tc.steps: step4. test DeviceOfflineNotify when listener_ is nullptr;
315      */
316     auto runner = AppExecFwk::EventRunner::Create("DmsNetworkIdManager");
317     DtbschedmgrDeviceInfoStorage::GetInstance().networkIdMgrHandler_ =
318         std::make_shared<AppExecFwk::EventHandler>(runner);
319     DtbschedmgrDeviceInfoStorage::GetInstance().listener_ = nullptr;
320     DtbschedmgrDeviceInfoStorage::GetInstance().DeviceOfflineNotify(deviceId);
321     /**
322      * @tc.steps: step5. test DeviceOfflineNotify when listener_ is not nullptr;
323      */
324     DtbschedmgrDeviceInfoStorage::GetInstance().listener_ = new MockDmsNotifier();
325     DtbschedmgrDeviceInfoStorage::GetInstance().DeviceOfflineNotify(deviceId);
326     DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest ClearAllDevicesTest_001 end" << std::endl;
327 }
328 
329 /**
330  * @tc.name: GetDeviceInfoByIdTest_001
331  * @tc.desc: test GetDeviceInfoById
332  * @tc.type: FUNC
333  */
334 HWTEST_F(DtbschedmgrDeviceInfoStorageTest, GetDeviceInfoByIdTest_001, TestSize.Level3)
335 {
336     DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest GetDeviceInfoByIdTest_001 start" << std::endl;
337     /**
338      * @tc.steps: step1. test GetDeviceInfoById when deviceId is not in map;
339      */
340     std::string deviceId = "invalid deviceId for GetDeviceInfoById";
341     {
342         lock_guard<mutex> autoLock(DtbschedmgrDeviceInfoStorage::GetInstance().deviceLock_);
343         DtbschedmgrDeviceInfoStorage::GetInstance().remoteDevices_.clear();
344     }
345     std::shared_ptr<DmsDeviceInfo> dmsDeviceInfo =
346         DtbschedmgrDeviceInfoStorage::GetInstance().GetDeviceInfoById(deviceId);
347     EXPECT_EQ(dmsDeviceInfo, nullptr);
348     /**
349      * @tc.steps: step2. test GetDeviceInfoById when deviceId is in map;
350      */
351     {
352         lock_guard<mutex> autoLock(DtbschedmgrDeviceInfoStorage::GetInstance().deviceLock_);
353         std::string deviceName = "invalid deviceName for GetDeviceInfoById";
354         int32_t deviceType = 0;
355         DtbschedmgrDeviceInfoStorage::GetInstance().remoteDevices_[deviceId] =
356             make_shared<DmsDeviceInfo>(deviceName, deviceType, deviceId);
357     }
358     dmsDeviceInfo = DtbschedmgrDeviceInfoStorage::GetInstance().GetDeviceInfoById(deviceId);
359     EXPECT_NE(dmsDeviceInfo, nullptr);
360     DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest GetDeviceInfoByIdTest_001 end" << std::endl;
361 }
362 
363 /**
364  * @tc.name: GetUuidByNetworkIdTest_001
365  * @tc.desc: test GetUuidByNetworkId
366  * @tc.type: FUNC
367  */
368 HWTEST_F(DtbschedmgrDeviceInfoStorageTest, GetUuidByNetworkIdTest_001, TestSize.Level3)
369 {
370     DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest GetUuidByNetworkIdTest_001 start" << std::endl;
371     /**
372      * @tc.steps: step1. test GetUuidByNetworkId when networkId is empty;
373      */
374     std::string networkId;
375     std::string result = DtbschedmgrDeviceInfoStorage::GetInstance().GetUuidByNetworkId(networkId);
376     EXPECT_TRUE(result.empty());
377     /**
378      * @tc.steps: step2. test GetUuidByNetworkId when networkId is not in map;
379      */
380     networkId = "invalid networkId for GetUuidByNetworkId";
381     result = DtbschedmgrDeviceInfoStorage::GetInstance().GetUuidByNetworkId(networkId);
382     EXPECT_TRUE(result.empty());
383     /**
384      * @tc.steps: step3. test GetUuidByNetworkId when networkId is in map;
385      */
386     std::string uuid = "invalid uuid for GetUuidByNetworkId";
387     {
388         lock_guard<mutex> autoLock(DtbschedmgrDeviceInfoStorage::GetInstance().uuidNetworkIdLock_);
389         DtbschedmgrDeviceInfoStorage::GetInstance().uuidNetworkIdMap_[uuid] = networkId;
390     }
391     result = DtbschedmgrDeviceInfoStorage::GetInstance().GetUuidByNetworkId(networkId);
392     EXPECT_EQ(result, uuid);
393     DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest GetUuidByNetworkIdTest_001 end" << std::endl;
394 }
395 
396 /**
397  * @tc.name: GetNetworkIdByUuidTest_001
398  * @tc.desc: test GetNetworkIdByUuid
399  * @tc.type: FUNC
400  */
401 HWTEST_F(DtbschedmgrDeviceInfoStorageTest, GetNetworkIdByUuidTest_001, TestSize.Level3)
402 {
403     DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest GetNetworkIdByUuidTest_001 start" << std::endl;
404     /**
405      * @tc.steps: step1. test GetNetworkIdByUuid when uuid is null;
406      */
407     std::string uuid;
408     std::string result = DtbschedmgrDeviceInfoStorage::GetInstance().GetNetworkIdByUuid(uuid);
409     EXPECT_TRUE(result.empty());
410     /**
411      * @tc.steps: step2. test GetNetworkIdByUuid when uuid is invalid;
412      */
413     uuid = "invalid uuid for GetNetworkIdByUuid";
414     result = DtbschedmgrDeviceInfoStorage::GetInstance().GetNetworkIdByUuid(uuid);
415     EXPECT_TRUE(result.empty());
416     /**
417      * @tc.steps: step3. test GetNetworkIdByUuid;
418      */
419     std::string networkId = "invalid networkId for GetNetworkIdByUuid";
420     {
421         std::lock_guard<std::mutex> autoLock(DtbschedmgrDeviceInfoStorage::GetInstance().uuidNetworkIdLock_);
422         DtbschedmgrDeviceInfoStorage::GetInstance().uuidNetworkIdMap_[uuid] = networkId;
423     }
424     result = DtbschedmgrDeviceInfoStorage::GetInstance().GetNetworkIdByUuid(uuid);
425     EXPECT_EQ(result, networkId);
426     /**
427      * @tc.steps: step4. test OnDeviceInfoChanged;
428      */
429     std::string deviceId = "invalid deviceId for OnDeviceInfoChanged";
430     DtbschedmgrDeviceInfoStorage::GetInstance().OnDeviceInfoChanged(deviceId);
431 
432     /**
433      * @tc.steps: step5. test OnRemoteDied;
434      */
435     wptr<IRemoteObject> remote = nullptr;
436     sptr<DnetServiceDeathRecipient> dnetServiceDeathRecipient = new DnetServiceDeathRecipient();
437     dnetServiceDeathRecipient->OnRemoteDied(remote);
438     DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest GetNetworkIdByUuidTest_001 end" << std::endl;
439 }
440 
441 } // namespace DistributedSchedule
442 } // namespace OHOS
443