1 /*
2  * Copyright (c) 2023-2024 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 #define private   public
17 #define protected public
18 #include "content_sensor_manager_utils.h"
19 #include "gtest/gtest.h"
20 #include "profile_cache.h"
21 #include "profile_utils.h"
22 #include "i_sync_completed_callback.h"
23 #include "sync_completed_callback_stub.h"
24 #undef private
25 #undef protected
26 
27 namespace OHOS {
28 namespace DistributedDeviceProfile {
29 using namespace testing;
30 using namespace testing::ext;
31 using namespace std;
32 
33 class ProfileCacheTest : public testing::Test {
34 public:
35     static void SetUpTestCase();
36     static void TearDownTestCase();
37     void SetUp();
38     void TearDown();
39 };
40 
SetUpTestCase()41 void ProfileCacheTest::SetUpTestCase() {
42 }
43 
TearDownTestCase()44 void ProfileCacheTest::TearDownTestCase() {
45 }
46 
SetUp()47 void ProfileCacheTest::SetUp() {
48 }
49 
TearDown()50 void ProfileCacheTest::TearDown() {
51 }
52 
53 class SyncCallback : public SyncCompletedCallbackStub {
54 public:
OnSyncCompleted(const map<string,SyncStatus> & syncResults)55     void OnSyncCompleted(const map<string, SyncStatus>& syncResults) {
56     }
57 };
58 
59 HWTEST_F(ProfileCacheTest, AddDeviceProfile_001, TestSize.Level2)
60 {
61     DeviceProfile deviceProfile;
62     int32_t ret = ProfileCache::GetInstance().AddDeviceProfile(deviceProfile);
63     EXPECT_EQ(DP_INVALID_PARAMS, ret);
64 
65     std::string devId = "dp_devId";
66     deviceProfile.SetDeviceId(devId);
67     ret = ProfileCache::GetInstance().AddDeviceProfile(deviceProfile);
68     EXPECT_EQ(DP_SUCCESS, ret);
69 
70     for (int i = 1; i < MAX_DEVICE_SIZE + 1; ++i) {
71         ProfileCache::GetInstance().deviceProfileMap_[std::to_string(i)] = deviceProfile;
72     }
73     ret = ProfileCache::GetInstance().AddDeviceProfile(deviceProfile);
74     EXPECT_EQ(DP_EXCEED_MAX_SIZE_FAIL, ret);
75 }
76 
77 HWTEST_F(ProfileCacheTest, AddServiceProfile_001, TestSize.Level2)
78 {
79     ServiceProfile serviceProfile;
80     int32_t ret = ProfileCache::GetInstance().AddServiceProfile(serviceProfile);
81     EXPECT_EQ(DP_INVALID_PARAMS, ret);
82 
83     std::string devId = "dp_devId";
84     serviceProfile.SetDeviceId(devId);
85     ret = ProfileCache::GetInstance().AddServiceProfile(serviceProfile);
86     EXPECT_EQ(DP_INVALID_PARAMS, ret);
87 
88     std::string serName = "dp_serName";
89     serviceProfile.SetServiceName(serName);
90     ret = ProfileCache::GetInstance().AddServiceProfile(serviceProfile);
91     EXPECT_EQ(DP_SUCCESS, ret);
92 
93     devId = "";
94     serviceProfile.SetDeviceId(devId);
95     ret = ProfileCache::GetInstance().AddServiceProfile(serviceProfile);
96     EXPECT_EQ(DP_INVALID_PARAMS, ret);
97 
98     devId = "dp_devId";
99     serviceProfile.SetDeviceId(devId);
100     for (int i = 1; i < MAX_DEVICE_SIZE + 1; ++i) {
101         ProfileCache::GetInstance().serviceProfileMap_[std::to_string(i)] = serviceProfile;
102     }
103     ret = ProfileCache::GetInstance().AddServiceProfile(serviceProfile);
104     EXPECT_EQ(DP_EXCEED_MAX_SIZE_FAIL, ret);
105 }
106 
107 HWTEST_F(ProfileCacheTest, AddCharProfile_001, TestSize.Level2)
108 {
109     CharacteristicProfile charProfile;
110     int32_t ret = ProfileCache::GetInstance().AddCharProfile(charProfile);
111     EXPECT_EQ(DP_INVALID_PARAMS, ret);
112 
113     std::string devId = "dp_devId";
114     charProfile.SetDeviceId(devId);
115     ret = ProfileCache::GetInstance().AddCharProfile(charProfile);
116     EXPECT_EQ(DP_INVALID_PARAMS, ret);
117 
118     std::string serName = "dp_serName";
119     charProfile.SetServiceName(serName);
120     ret = ProfileCache::GetInstance().AddCharProfile(charProfile);
121     EXPECT_EQ(DP_INVALID_PARAMS, ret);
122 
123     std::string charKey = "dp_charKey";
124     charProfile.SetCharacteristicKey(charKey);
125     ret = ProfileCache::GetInstance().AddCharProfile(charProfile);
126     EXPECT_EQ(DP_SUCCESS, ret);
127 
128     devId = "";
129     charProfile.SetDeviceId(devId);
130     ret = ProfileCache::GetInstance().AddCharProfile(charProfile);
131     EXPECT_EQ(DP_INVALID_PARAMS, ret);
132 
133     serName = "";
134     charProfile.SetServiceName(serName);
135     ret = ProfileCache::GetInstance().AddCharProfile(charProfile);
136     EXPECT_EQ(DP_INVALID_PARAMS, ret);
137 
138     devId = "dp_devId";
139     charProfile.SetDeviceId(devId);
140     ret = ProfileCache::GetInstance().AddCharProfile(charProfile);
141     EXPECT_EQ(DP_INVALID_PARAMS, ret);
142 
143     serName = "dp_serName";
144     charProfile.SetServiceName(serName);
145     charKey = "dp_charKey";
146     charProfile.SetCharacteristicKey(charKey);
147     for (int i = 1; i < MAX_DEVICE_SIZE + 1; ++i) {
148         ProfileCache::GetInstance().charProfileMap_[std::to_string(i)] = charProfile;
149     }
150     ret = ProfileCache::GetInstance().AddCharProfile(charProfile);
151     EXPECT_EQ(DP_EXCEED_MAX_SIZE_FAIL, ret);
152 }
153 
154 HWTEST_F(ProfileCacheTest, GetDeviceProfile_001, TestSize.Level2)
155 {
156     std::string devId = "";
157     DeviceProfile deviceProfile;
158     int32_t ret = ProfileCache::GetInstance().GetDeviceProfile(devId, deviceProfile);
159     EXPECT_EQ(DP_INVALID_PARAMS, ret);
160 
161     devId = "dp_devId";
162     ret = ProfileCache::GetInstance().GetDeviceProfile(devId, deviceProfile);
163     EXPECT_EQ(DP_SUCCESS, ret);
164 
165     ProfileCache::GetInstance().deviceProfileMap_.clear();
166     ret = ProfileCache::GetInstance().GetDeviceProfile(devId, deviceProfile);
167     EXPECT_EQ(DP_NOT_FOUND_FAIL, ret);
168 }
169 
170 HWTEST_F(ProfileCacheTest, GetServiceProfile_001, TestSize.Level2)
171 {
172     std::string devId = "";
173     std::string serName = "";
174     ServiceProfile serviceProfile;
175     int32_t ret = ProfileCache::GetInstance().GetServiceProfile(devId, serName, serviceProfile);
176     EXPECT_EQ(DP_INVALID_PARAMS, ret);
177 
178     devId = "dp_devId";
179     ret = ProfileCache::GetInstance().GetServiceProfile(devId, serName, serviceProfile);
180     EXPECT_EQ(DP_INVALID_PARAMS, ret);
181 
182     serName = "dp_serName";
183     ret = ProfileCache::GetInstance().GetServiceProfile(devId, serName, serviceProfile);
184     EXPECT_EQ(DP_SUCCESS, ret);
185 
186     devId = "";
187     ret = ProfileCache::GetInstance().GetServiceProfile(devId, serName, serviceProfile);
188     EXPECT_EQ(DP_INVALID_PARAMS, ret);
189 
190     devId = "dp_devId";
191     ProfileCache::GetInstance().serviceProfileMap_.clear();
192     ret = ProfileCache::GetInstance().GetServiceProfile(devId, serName, serviceProfile);
193     EXPECT_EQ(DP_NOT_FOUND_FAIL, ret);
194 }
195 
196 HWTEST_F(ProfileCacheTest, GetCharacteristicProfile_001, TestSize.Level2)
197 {
198     std::string devId = "";
199     std::string serName = "";
200     std::string charKey = "";
201     CharacteristicProfile charProfile;
202     int32_t ret = ProfileCache::GetInstance().GetCharacteristicProfile(devId, serName, charKey, charProfile);
203     EXPECT_EQ(DP_INVALID_PARAMS, ret);
204 
205     devId = "dp_devId";
206     ret = ProfileCache::GetInstance().GetCharacteristicProfile(devId, serName, charKey, charProfile);
207     EXPECT_EQ(DP_INVALID_PARAMS, ret);
208 
209     serName = "dp_serName";
210     ret = ProfileCache::GetInstance().GetCharacteristicProfile(devId, serName, charKey, charProfile);
211     EXPECT_EQ(DP_INVALID_PARAMS, ret);
212 
213     charKey = "dp_charKey";
214     ret = ProfileCache::GetInstance().GetCharacteristicProfile(devId, serName, charKey, charProfile);
215     EXPECT_EQ(DP_SUCCESS, ret);
216 
217     devId = "";
218     ret = ProfileCache::GetInstance().GetCharacteristicProfile(devId, serName, charKey, charProfile);
219     EXPECT_EQ(DP_INVALID_PARAMS, ret);
220 
221     serName = "";
222     ret = ProfileCache::GetInstance().GetCharacteristicProfile(devId, serName, charKey, charProfile);
223     EXPECT_EQ(DP_INVALID_PARAMS, ret);
224 
225     devId = "dp_devId";
226     ret = ProfileCache::GetInstance().GetCharacteristicProfile(devId, serName, charKey, charProfile);
227     EXPECT_EQ(DP_INVALID_PARAMS, ret);
228 
229     serName = "dp_serName";
230     ProfileCache::GetInstance().charProfileMap_.clear();
231     ret = ProfileCache::GetInstance().GetCharacteristicProfile(devId, serName, charKey, charProfile);
232     EXPECT_EQ(DP_NOT_FOUND_FAIL, ret);
233 }
234 
235 HWTEST_F(ProfileCacheTest, DeleteDeviceProfile_001, TestSize.Level2)
236 {
237     std::string devId = "";
238     int32_t ret = ProfileCache::GetInstance().DeleteDeviceProfile(devId);
239     EXPECT_EQ(DP_INVALID_PARAMS, ret);
240 
241     devId = "dp_devId";
242     ret = ProfileCache::GetInstance().DeleteDeviceProfile(devId);
243     EXPECT_EQ(DP_SUCCESS, ret);
244 }
245 
246 HWTEST_F(ProfileCacheTest, DeleteServiceProfile_001, TestSize.Level2)
247 {
248     std::string devId = "";
249     std::string serName = "";
250     int32_t ret = ProfileCache::GetInstance().DeleteServiceProfile(devId, serName);
251     EXPECT_EQ(DP_INVALID_PARAMS, ret);
252 
253     devId = "dp_devId";
254     ret = ProfileCache::GetInstance().DeleteServiceProfile(devId, serName);
255     EXPECT_EQ(DP_INVALID_PARAMS, ret);
256 
257     serName = "dp_serName";
258     ret = ProfileCache::GetInstance().DeleteServiceProfile(devId, serName);
259     EXPECT_EQ(DP_SUCCESS, ret);
260 
261     devId = "";
262     ret = ProfileCache::GetInstance().DeleteServiceProfile(devId, serName);
263     EXPECT_EQ(DP_INVALID_PARAMS, ret);
264 }
265 
266 HWTEST_F(ProfileCacheTest, DeleteCharProfile_001, TestSize.Level2)
267 {
268     std::string devId = "";
269     std::string serName = "";
270     std::string charKey = "";
271     int32_t ret = ProfileCache::GetInstance().DeleteCharProfile(devId, serName, charKey);
272     EXPECT_EQ(DP_INVALID_PARAMS, ret);
273 
274     devId = "dp_devId";
275     ret = ProfileCache::GetInstance().DeleteCharProfile(devId, serName, charKey);
276     EXPECT_EQ(DP_INVALID_PARAMS, ret);
277 
278     serName = "dp_serName";
279     ret = ProfileCache::GetInstance().DeleteCharProfile(devId, serName, charKey);
280     EXPECT_EQ(DP_INVALID_PARAMS, ret);
281 
282     charKey = "dp_charKey";
283     ret = ProfileCache::GetInstance().DeleteCharProfile(devId, serName, charKey);
284     EXPECT_EQ(DP_SUCCESS, ret);
285 
286     devId = "";
287     ret = ProfileCache::GetInstance().DeleteCharProfile(devId, serName, charKey);
288     EXPECT_EQ(DP_INVALID_PARAMS, ret);
289 
290     serName = "";
291     ret = ProfileCache::GetInstance().DeleteCharProfile(devId, serName, charKey);
292     EXPECT_EQ(DP_INVALID_PARAMS, ret);
293 
294     devId = "dp_devId";
295     ret = ProfileCache::GetInstance().DeleteCharProfile(devId, serName, charKey);
296     EXPECT_EQ(DP_INVALID_PARAMS, ret);
297 }
298 
299 HWTEST_F(ProfileCacheTest, IsDeviceProfileExist_001, TestSize.Level2)
300 {
301     DeviceProfile deviceProfile;
302     bool ret = ProfileCache::GetInstance().IsDeviceProfileExist(deviceProfile);
303     EXPECT_EQ(false, ret);
304 
305     std::string devId = "dp_devId";
306     deviceProfile.SetDeviceId(devId);
307     ret = ProfileCache::GetInstance().IsDeviceProfileExist(deviceProfile);
308     EXPECT_EQ(false, ret);
309 
310     DeviceProfile deviceProfile1;
311     deviceProfile1.SetDeviceId("anything1");
312     deviceProfile1.SetDeviceTypeName("anything");
313     deviceProfile1.SetDeviceTypeId(0);
314     deviceProfile1.SetDeviceName("anything");
315     deviceProfile1.SetManufactureName("anything");
316     deviceProfile1.SetDeviceModel("anything");
317     deviceProfile1.SetStorageCapability(1);
318     deviceProfile1.SetOsSysCap("anything");
319     deviceProfile1.SetOsApiLevel(1);
320     deviceProfile1.SetOsVersion("anything");
321     deviceProfile1.SetOsType(1);
322     ProfileCache::GetInstance().AddDeviceProfile(deviceProfile1);
323     ret = ProfileCache::GetInstance().IsDeviceProfileExist(deviceProfile1);
324     EXPECT_EQ(true, ret);
325 }
326 
327 HWTEST_F(ProfileCacheTest, IsServiceProfileExist_001, TestSize.Level2)
328 {
329     ServiceProfile serviceProfile;
330     bool ret = ProfileCache::GetInstance().IsServiceProfileExist(serviceProfile);
331     EXPECT_EQ(false, ret);
332 
333     std::string devId = "dp_devId";
334     serviceProfile.SetDeviceId(devId);
335     ret = ProfileCache::GetInstance().IsServiceProfileExist(serviceProfile);
336     EXPECT_EQ(false, ret);
337 
338     devId = "";
339     std::string serName = "dp_serName";
340     serviceProfile.SetDeviceId(devId);
341     serviceProfile.SetServiceName(serName);
342     ret = ProfileCache::GetInstance().IsServiceProfileExist(serviceProfile);
343     EXPECT_EQ(false, ret);
344 
345     devId = "dp_devId";
346     serviceProfile.SetDeviceId(devId);
347     ret = ProfileCache::GetInstance().IsServiceProfileExist(serviceProfile);
348     EXPECT_EQ(false, ret);
349 
350     ServiceProfile serviceProfile1;
351     serviceProfile1.SetDeviceId("deviceId1");
352     serviceProfile1.SetServiceName("serviceName1");
353     serviceProfile1.SetServiceType("serviceType1");
354     ProfileCache::GetInstance().AddServiceProfile(serviceProfile1);
355     ret = ProfileCache::GetInstance().IsServiceProfileExist(serviceProfile1);
356     EXPECT_EQ(true, ret);
357 }
358 
359 HWTEST_F(ProfileCacheTest, IsCharProfileExist_001, TestSize.Level2)
360 {
361     CharacteristicProfile charProfile;
362     bool ret = ProfileCache::GetInstance().IsCharProfileExist(charProfile);
363     EXPECT_EQ(false, ret);
364 
365     std::string devId = "dp_devId";
366     charProfile.SetDeviceId(devId);
367     ret = ProfileCache::GetInstance().IsCharProfileExist(charProfile);
368     EXPECT_EQ(false, ret);
369 
370     std::string serName = "dp_serName";
371     charProfile.SetServiceName(serName);
372     ret = ProfileCache::GetInstance().IsCharProfileExist(charProfile);
373     EXPECT_EQ(false, ret);
374 
375     devId = "";
376     charProfile.SetDeviceId(devId);
377     std::string charKey = "dp_charKey";
378     charProfile.SetCharacteristicKey(charKey);
379     ret = ProfileCache::GetInstance().IsCharProfileExist(charProfile);
380     EXPECT_EQ(false, ret);
381 
382     serName = "";
383     charProfile.SetServiceName(serName);
384     ret = ProfileCache::GetInstance().IsCharProfileExist(charProfile);
385     EXPECT_EQ(false, ret);
386 
387     devId = "dp_devId";
388     charProfile.SetDeviceId(devId);
389     ret = ProfileCache::GetInstance().IsCharProfileExist(charProfile);
390     EXPECT_EQ(false, ret);
391 
392     CharacteristicProfile charProfile1;
393     charProfile1.SetDeviceId("deviceId1");
394     charProfile1.SetServiceName("serviceName1");
395     charProfile1.SetCharacteristicKey("characteristicKey1");
396     charProfile1.SetCharacteristicValue("characteristicValue1");
397     ProfileCache::GetInstance().AddCharProfile(charProfile1);
398     ret = ProfileCache::GetInstance().IsCharProfileExist(charProfile1);
399     EXPECT_EQ(true, ret);
400 }
401 
402 HWTEST_F(ProfileCacheTest, RefreshCharProfileCache_001, TestSize.Level2)
403 {
404     std::vector<CharacteristicProfile> characteristicProfiles;
405     int32_t ret = ProfileCache::GetInstance().RefreshCharProfileCache(characteristicProfiles);
406     EXPECT_EQ(DP_INVALID_PARAMS, ret);
407 
408     CharacteristicProfile characteristicProfile;
409     std::string devId = "dp_devId";
410     std::string serName = "dp_serName";
411     std::string charKey = "dp_charKey";
412     characteristicProfile.SetDeviceId(devId);
413     characteristicProfile.SetServiceName(serName);
414     characteristicProfile.SetCharacteristicKey(charKey);
415     characteristicProfiles.push_back(characteristicProfile);
416     ret = ProfileCache::GetInstance().RefreshCharProfileCache(characteristicProfiles);
417     EXPECT_EQ(DP_SUCCESS, ret);
418 }
419 
420 /**
421  * @tc.name: AddSyncListener001
422  * @tc.desc: AddSyncListener all.
423  * @tc.type: FUNC
424  * @tc.require:
425  */
426 HWTEST_F(ProfileCacheTest, AddSyncListener001, TestSize.Level1)
427 {
428     ProfileCache::GetInstance().UnInit();
429     ProfileCache::GetInstance().Init();
430     string caller = "caller";
431     OHOS::sptr<OHOS::IRemoteObject> syncListener = OHOS::sptr<SyncCompletedCallbackStub>(new SyncCallback());
432 
433     int32_t ret1 = ProfileCache::GetInstance().AddSyncListener(caller, syncListener);
434     EXPECT_EQ(DP_SUCCESS, ret1);
435 
436     for (int32_t i = 0; i < MAX_LISTENER_SIZE + 5; i++) {
437         string caller = "caller" + std::to_string(i);
438         OHOS::sptr<OHOS::IRemoteObject> syncListener1 = OHOS::sptr<SyncCompletedCallbackStub>(new SyncCallback());
439         ProfileCache::GetInstance().syncListenerMap_[caller] = syncListener1;
440     }
441     int32_t ret2 = ProfileCache::GetInstance().AddSyncListener(caller, syncListener);
442     EXPECT_EQ(DP_EXCEED_MAX_SIZE_FAIL, ret2);
443 
444     syncListener = nullptr;
445     int32_t ret3 = ProfileCache::GetInstance().AddSyncListener(caller, syncListener);
446     EXPECT_EQ(DP_INVALID_PARAMS, ret3);
447 
448     for (int32_t i = 0; i < MAX_STRING_LEN + 5; i++) {
449         caller += 'a';
450     }
451     int32_t ret4 = ProfileCache::GetInstance().AddSyncListener(caller, syncListener);
452     EXPECT_EQ(DP_INVALID_PARAMS, ret4);
453 
454     caller = "";
455     int32_t ret5 = ProfileCache::GetInstance().AddSyncListener(caller, syncListener);
456     EXPECT_EQ(DP_INVALID_PARAMS, ret5);
457 
458     ProfileCache::GetInstance().UnInit();
459     ProfileCache::GetInstance().Init();
460 }
461 
462 /**
463  * @tc.name: RemoveSyncListeners001
464  * @tc.desc: RemoveSyncListeners all.
465  * @tc.type: FUNC
466  * @tc.require:
467  */
468 HWTEST_F(ProfileCacheTest, RemoveSyncListeners001, TestSize.Level1)
469 {
470     ProfileCache::GetInstance().UnInit();
471     ProfileCache::GetInstance().Init();
472     ProfileCache::GetInstance().syncListenerMap_.clear();
473 
474     string caller = "caller";
475     OHOS::sptr<OHOS::IRemoteObject> syncListener = OHOS::sptr<SyncCompletedCallbackStub>(new SyncCallback());
476 
477     int32_t ret1 = ProfileCache::GetInstance().AddSyncListener(caller, syncListener);
478     EXPECT_EQ(DP_SUCCESS, ret1);
479 
480     map<string, sptr<IRemoteObject>> syncListeners;
481     syncListeners[caller] = syncListener;
482     int32_t ret2 = ProfileCache::GetInstance().RemoveSyncListeners(syncListeners);
483     EXPECT_EQ(DP_SUCCESS, ret2);
484 
485     ProfileCache::GetInstance().AddSyncListener(caller, syncListener);
486     auto iter = ProfileCache::GetInstance().syncListenerMap_.begin();
487     iter->second = nullptr;
488     ProfileCache::GetInstance().RemoveSyncListeners(syncListeners);
489     bool ret3 = ProfileCache::GetInstance().syncListenerMap_.count(caller);
490     EXPECT_EQ(false, ret3);
491 
492     ProfileCache::GetInstance().UnInit();
493     ProfileCache::GetInstance().Init();
494 }
495 
496 /**
497  * @tc.name: RemoveSyncListener001
498  * @tc.desc: RemoveSyncListener overload1.
499  * @tc.type: FUNC
500  * @tc.require:
501  */
502 HWTEST_F(ProfileCacheTest, RemoveSyncListener001, TestSize.Level1)
503 {
504     ProfileCache::GetInstance().UnInit();
505     ProfileCache::GetInstance().Init();
506     ProfileCache::GetInstance().syncListenerMap_.clear();
507 
508     string caller = "caller";
509     OHOS::sptr<OHOS::IRemoteObject> syncListener = OHOS::sptr<SyncCompletedCallbackStub>(new SyncCallback());
510 
511     int32_t ret1 = ProfileCache::GetInstance().AddSyncListener(caller, syncListener);
512     EXPECT_EQ(DP_SUCCESS, ret1);
513 
514     int32_t ret2 = ProfileCache::GetInstance().RemoveSyncListener(caller);
515     EXPECT_EQ(DP_SUCCESS, ret2);
516 
517     int32_t ret3 = ProfileCache::GetInstance().RemoveSyncListener(caller);
518     EXPECT_EQ(DP_NOT_FOUND_FAIL, ret3);
519 
520     for (int32_t i = 0; i < MAX_STRING_LEN + 5; i++) {
521         caller += 'a';
522     }
523     int32_t ret4 = ProfileCache::GetInstance().RemoveSyncListener(caller);
524     EXPECT_EQ(DP_INVALID_PARAMS, ret4);
525 
526     caller = "";
527     int32_t ret5 = ProfileCache::GetInstance().RemoveSyncListener(caller);
528     EXPECT_EQ(DP_INVALID_PARAMS, ret5);
529 
530     ProfileCache::GetInstance().UnInit();
531     ProfileCache::GetInstance().Init();
532 }
533 
534 /**
535  * @tc.name: RemoveSyncListener002
536  * @tc.desc: RemoveSyncListener overload2.
537  * @tc.type: FUNC
538  * @tc.require:
539  */
540 HWTEST_F(ProfileCacheTest, RemoveSyncListener002, TestSize.Level1)
541 {
542     ProfileCache::GetInstance().UnInit();
543     ProfileCache::GetInstance().Init();
544     ProfileCache::GetInstance().syncListenerMap_.clear();
545 
546     string caller = "caller";
547     OHOS::sptr<OHOS::IRemoteObject> syncListener = OHOS::sptr<SyncCompletedCallbackStub>(new SyncCallback());
548 
549     int32_t ret1 = ProfileCache::GetInstance().AddSyncListener(caller, syncListener);
550     EXPECT_EQ(DP_SUCCESS, ret1);
551 
552     int32_t ret2 = ProfileCache::GetInstance().RemoveSyncListener(syncListener);
553     EXPECT_EQ(DP_SUCCESS, ret2);
554 
555     int32_t ret4 = ProfileCache::GetInstance().RemoveSyncListener(syncListener);
556     EXPECT_EQ(DP_NOT_FOUND_FAIL, ret4);
557 
558     syncListener = nullptr;
559     int32_t ret5 = ProfileCache::GetInstance().RemoveSyncListener(syncListener);
560     EXPECT_EQ(DP_INVALID_PARAMS, ret5);
561 
562     ProfileCache::GetInstance().UnInit();
563     ProfileCache::GetInstance().Init();
564 }
565 
566 /**
567  * @tc.name: OnNodeOnline001
568  * @tc.desc: OnNodeOnline001
569  * @tc.type: FUNC
570  * @tc.require:
571  */
572 HWTEST_F(ProfileCacheTest, OnNodeOnline001, TestSize.Level1)
573 {
574     std::string peerNetworkId = "";
575     ProfileCache::GetInstance().OnNodeOnline(peerNetworkId);
576 
577     peerNetworkId = ProfileUtils::GetLocalUdidFromDM();
578     ProfileCache::GetInstance().OnNodeOnline(peerNetworkId);
579     EXPECT_NE("NetworkId", peerNetworkId);
580 }
581 
582 /**
583  * @tc.name: OnNodeOffline001
584  * @tc.desc: OnNodeOffline001
585  * @tc.type: FUNC
586  * @tc.require:
587  */
588 HWTEST_F(ProfileCacheTest, OnNodeOffline001, TestSize.Level1)
589 {
590     std::string peerNetworkId = "";
591     ProfileCache::GetInstance().OnNodeOffline(peerNetworkId);
592 
593     peerNetworkId = ProfileUtils::GetLocalUdidFromDM();
594     ProfileCache::GetInstance().OnNodeOffline(peerNetworkId);
595     EXPECT_NE("NetworkId", peerNetworkId);
596 }
597 
598 /**
599  * @tc.name: IsLocalOrOnlineDevice001
600  * @tc.desc: IsLocalOrOnlineDevice001
601  * @tc.type: FUNC
602  * @tc.require:
603  */
604 HWTEST_F(ProfileCacheTest, IsLocalOrOnlineDevice001, TestSize.Level1)
605 {
606     std::string deviceId = "";
607     bool ret = ProfileCache::GetInstance().IsLocalOrOnlineDevice(deviceId);
608     EXPECT_EQ(ret, false);
609 }
610 
611 /**
612  * @tc.name: IsLocalOrOnlineDevice002
613  * @tc.desc: IsLocalOrOnlineDevice002
614  * @tc.type: FUNC
615  * @tc.require:
616  */
617 HWTEST_F(ProfileCacheTest, IsLocalOrOnlineDevice002, TestSize.Level1)
618 {
619     std::string deviceId = ContentSensorManagerUtils::GetInstance().ObtainLocalUdid();
620     bool ret = ProfileCache::GetInstance().IsLocalOrOnlineDevice(deviceId);
621     EXPECT_EQ(ret, true);
622 }
623 
624 /**
625  * @tc.name: IsLocalOrOnlineDevice003
626  * @tc.desc: IsLocalOrOnlineDevice003
627  * @tc.type: FUNC
628  * @tc.require:
629  */
630 HWTEST_F(ProfileCacheTest, IsLocalOrOnlineDevice0013, TestSize.Level1)
631 {
632     std::string deviceId = "deviceId";
633     ProfileCache::GetInstance().onlineDevMap_[deviceId] = deviceId;
634     bool ret = ProfileCache::GetInstance().IsLocalOrOnlineDevice(deviceId);
635     EXPECT_EQ(ret, true);
636 }
637 } // namespace DistributedDeviceProfile
638 } // namespace OHOS
639 
640