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 #include "trusted_device_info.h"
25 #undef private
26 #undef protected
27
28 namespace OHOS {
29 namespace DistributedDeviceProfile {
30 using namespace testing;
31 using namespace testing::ext;
32 using namespace std;
33
34 class ProfileCacheTest : public testing::Test {
35 public:
36 static void SetUpTestCase();
37 static void TearDownTestCase();
38 void SetUp();
39 void TearDown();
40 };
41
SetUpTestCase()42 void ProfileCacheTest::SetUpTestCase() {
43 }
44
TearDownTestCase()45 void ProfileCacheTest::TearDownTestCase() {
46 }
47
SetUp()48 void ProfileCacheTest::SetUp() {
49 }
50
TearDown()51 void ProfileCacheTest::TearDown() {
52 }
53
54 class SyncCallback : public SyncCompletedCallbackStub {
55 public:
OnSyncCompleted(const map<string,SyncStatus> & syncResults)56 void OnSyncCompleted(const map<string, SyncStatus>& syncResults) {
57 }
58 };
59
60 HWTEST_F(ProfileCacheTest, AddDeviceProfile_001, TestSize.Level2)
61 {
62 DeviceProfile deviceProfile;
63 int32_t ret = ProfileCache::GetInstance().AddDeviceProfile(deviceProfile);
64 EXPECT_EQ(DP_INVALID_PARAMS, ret);
65
66 std::string devId = "dp_devId";
67 deviceProfile.SetDeviceId(devId);
68 ret = ProfileCache::GetInstance().AddDeviceProfile(deviceProfile);
69 EXPECT_EQ(DP_SUCCESS, ret);
70
71 for (int i = 1; i < MAX_DEVICE_SIZE + 1; ++i) {
72 ProfileCache::GetInstance().deviceProfileMap_[std::to_string(i)] = deviceProfile;
73 }
74 ret = ProfileCache::GetInstance().AddDeviceProfile(deviceProfile);
75 EXPECT_EQ(DP_EXCEED_MAX_SIZE_FAIL, ret);
76 }
77
78 HWTEST_F(ProfileCacheTest, AddServiceProfile_001, TestSize.Level2)
79 {
80 ServiceProfile serviceProfile;
81 int32_t ret = ProfileCache::GetInstance().AddServiceProfile(serviceProfile);
82 EXPECT_EQ(DP_INVALID_PARAMS, ret);
83
84 std::string devId = "dp_devId";
85 serviceProfile.SetDeviceId(devId);
86 ret = ProfileCache::GetInstance().AddServiceProfile(serviceProfile);
87 EXPECT_EQ(DP_INVALID_PARAMS, ret);
88
89 std::string serName = "dp_serName";
90 serviceProfile.SetServiceName(serName);
91 ret = ProfileCache::GetInstance().AddServiceProfile(serviceProfile);
92 EXPECT_EQ(DP_SUCCESS, ret);
93
94 devId = "";
95 serviceProfile.SetDeviceId(devId);
96 ret = ProfileCache::GetInstance().AddServiceProfile(serviceProfile);
97 EXPECT_EQ(DP_INVALID_PARAMS, ret);
98
99 devId = "dp_devId";
100 serviceProfile.SetDeviceId(devId);
101 for (int i = 1; i < MAX_DEVICE_SIZE + 1; ++i) {
102 ProfileCache::GetInstance().serviceProfileMap_[std::to_string(i)] = serviceProfile;
103 }
104 ret = ProfileCache::GetInstance().AddServiceProfile(serviceProfile);
105 EXPECT_EQ(DP_EXCEED_MAX_SIZE_FAIL, ret);
106 }
107
108 HWTEST_F(ProfileCacheTest, AddCharProfile_001, TestSize.Level2)
109 {
110 CharacteristicProfile charProfile;
111 int32_t ret = ProfileCache::GetInstance().AddCharProfile(charProfile);
112 EXPECT_EQ(DP_INVALID_PARAMS, ret);
113
114 std::string devId = "dp_devId";
115 charProfile.SetDeviceId(devId);
116 ret = ProfileCache::GetInstance().AddCharProfile(charProfile);
117 EXPECT_EQ(DP_INVALID_PARAMS, ret);
118
119 std::string serName = "dp_serName";
120 charProfile.SetServiceName(serName);
121 ret = ProfileCache::GetInstance().AddCharProfile(charProfile);
122 EXPECT_EQ(DP_INVALID_PARAMS, ret);
123
124 std::string charKey = "dp_charKey";
125 charProfile.SetCharacteristicKey(charKey);
126 ret = ProfileCache::GetInstance().AddCharProfile(charProfile);
127 EXPECT_EQ(DP_SUCCESS, ret);
128
129 devId = "";
130 charProfile.SetDeviceId(devId);
131 ret = ProfileCache::GetInstance().AddCharProfile(charProfile);
132 EXPECT_EQ(DP_INVALID_PARAMS, ret);
133
134 serName = "";
135 charProfile.SetServiceName(serName);
136 ret = ProfileCache::GetInstance().AddCharProfile(charProfile);
137 EXPECT_EQ(DP_INVALID_PARAMS, ret);
138
139 devId = "dp_devId";
140 charProfile.SetDeviceId(devId);
141 ret = ProfileCache::GetInstance().AddCharProfile(charProfile);
142 EXPECT_EQ(DP_INVALID_PARAMS, ret);
143
144 serName = "dp_serName";
145 charProfile.SetServiceName(serName);
146 charKey = "dp_charKey";
147 charProfile.SetCharacteristicKey(charKey);
148 for (int i = 1; i < MAX_DEVICE_SIZE + 1; ++i) {
149 ProfileCache::GetInstance().charProfileMap_[std::to_string(i)] = charProfile;
150 }
151 ret = ProfileCache::GetInstance().AddCharProfile(charProfile);
152 EXPECT_EQ(DP_EXCEED_MAX_SIZE_FAIL, ret);
153 }
154
155 HWTEST_F(ProfileCacheTest, GetDeviceProfile_001, TestSize.Level2)
156 {
157 std::string devId = "";
158 DeviceProfile deviceProfile;
159 int32_t ret = ProfileCache::GetInstance().GetDeviceProfile(devId, deviceProfile);
160 EXPECT_EQ(DP_INVALID_PARAMS, ret);
161
162 devId = "dp_devId";
163 ret = ProfileCache::GetInstance().GetDeviceProfile(devId, deviceProfile);
164 EXPECT_EQ(DP_SUCCESS, ret);
165
166 ProfileCache::GetInstance().deviceProfileMap_.clear();
167 ret = ProfileCache::GetInstance().GetDeviceProfile(devId, deviceProfile);
168 EXPECT_EQ(DP_NOT_FOUND_FAIL, ret);
169 }
170
171 HWTEST_F(ProfileCacheTest, GetServiceProfile_001, TestSize.Level2)
172 {
173 std::string devId = "";
174 std::string serName = "";
175 ServiceProfile serviceProfile;
176 int32_t ret = ProfileCache::GetInstance().GetServiceProfile(devId, serName, serviceProfile);
177 EXPECT_EQ(DP_INVALID_PARAMS, ret);
178
179 devId = "dp_devId";
180 ret = ProfileCache::GetInstance().GetServiceProfile(devId, serName, serviceProfile);
181 EXPECT_EQ(DP_INVALID_PARAMS, ret);
182
183 serName = "dp_serName";
184 ret = ProfileCache::GetInstance().GetServiceProfile(devId, serName, serviceProfile);
185 EXPECT_EQ(DP_SUCCESS, ret);
186
187 devId = "";
188 ret = ProfileCache::GetInstance().GetServiceProfile(devId, serName, serviceProfile);
189 EXPECT_EQ(DP_INVALID_PARAMS, ret);
190
191 devId = "dp_devId";
192 ProfileCache::GetInstance().serviceProfileMap_.clear();
193 ret = ProfileCache::GetInstance().GetServiceProfile(devId, serName, serviceProfile);
194 EXPECT_EQ(DP_NOT_FOUND_FAIL, ret);
195 }
196
197 HWTEST_F(ProfileCacheTest, GetCharacteristicProfile_001, TestSize.Level2)
198 {
199 std::string devId = "";
200 std::string serName = "";
201 std::string charKey = "";
202 CharacteristicProfile charProfile;
203 int32_t ret = ProfileCache::GetInstance().GetCharacteristicProfile(devId, serName, charKey, charProfile);
204 EXPECT_EQ(DP_INVALID_PARAMS, ret);
205
206 devId = "dp_devId";
207 ret = ProfileCache::GetInstance().GetCharacteristicProfile(devId, serName, charKey, charProfile);
208 EXPECT_EQ(DP_INVALID_PARAMS, ret);
209
210 serName = "dp_serName";
211 ret = ProfileCache::GetInstance().GetCharacteristicProfile(devId, serName, charKey, charProfile);
212 EXPECT_EQ(DP_INVALID_PARAMS, ret);
213
214 charKey = "dp_charKey";
215 ret = ProfileCache::GetInstance().GetCharacteristicProfile(devId, serName, charKey, charProfile);
216 EXPECT_EQ(DP_SUCCESS, ret);
217
218 devId = "";
219 ret = ProfileCache::GetInstance().GetCharacteristicProfile(devId, serName, charKey, charProfile);
220 EXPECT_EQ(DP_INVALID_PARAMS, ret);
221
222 serName = "";
223 ret = ProfileCache::GetInstance().GetCharacteristicProfile(devId, serName, charKey, charProfile);
224 EXPECT_EQ(DP_INVALID_PARAMS, ret);
225
226 devId = "dp_devId";
227 ret = ProfileCache::GetInstance().GetCharacteristicProfile(devId, serName, charKey, charProfile);
228 EXPECT_EQ(DP_INVALID_PARAMS, ret);
229
230 serName = "dp_serName";
231 ProfileCache::GetInstance().charProfileMap_.clear();
232 ret = ProfileCache::GetInstance().GetCharacteristicProfile(devId, serName, charKey, charProfile);
233 EXPECT_EQ(DP_NOT_FOUND_FAIL, ret);
234 }
235
236 HWTEST_F(ProfileCacheTest, DeleteDeviceProfile_001, TestSize.Level2)
237 {
238 std::string devId = "";
239 int32_t ret = ProfileCache::GetInstance().DeleteDeviceProfile(devId);
240 EXPECT_EQ(DP_INVALID_PARAMS, ret);
241
242 devId = "dp_devId";
243 ret = ProfileCache::GetInstance().DeleteDeviceProfile(devId);
244 EXPECT_EQ(DP_SUCCESS, ret);
245 }
246
247 HWTEST_F(ProfileCacheTest, DeleteServiceProfile_001, TestSize.Level2)
248 {
249 std::string devId = "";
250 std::string serName = "";
251 int32_t ret = ProfileCache::GetInstance().DeleteServiceProfile(devId, serName);
252 EXPECT_EQ(DP_INVALID_PARAMS, ret);
253
254 devId = "dp_devId";
255 ret = ProfileCache::GetInstance().DeleteServiceProfile(devId, serName);
256 EXPECT_EQ(DP_INVALID_PARAMS, ret);
257
258 serName = "dp_serName";
259 ret = ProfileCache::GetInstance().DeleteServiceProfile(devId, serName);
260 EXPECT_EQ(DP_SUCCESS, ret);
261
262 devId = "";
263 ret = ProfileCache::GetInstance().DeleteServiceProfile(devId, serName);
264 EXPECT_EQ(DP_INVALID_PARAMS, ret);
265 }
266
267 HWTEST_F(ProfileCacheTest, DeleteCharProfile_001, TestSize.Level2)
268 {
269 std::string devId = "";
270 std::string serName = "";
271 std::string charKey = "";
272 int32_t ret = ProfileCache::GetInstance().DeleteCharProfile(devId, serName, charKey);
273 EXPECT_EQ(DP_INVALID_PARAMS, ret);
274
275 devId = "dp_devId";
276 ret = ProfileCache::GetInstance().DeleteCharProfile(devId, serName, charKey);
277 EXPECT_EQ(DP_INVALID_PARAMS, ret);
278
279 serName = "dp_serName";
280 ret = ProfileCache::GetInstance().DeleteCharProfile(devId, serName, charKey);
281 EXPECT_EQ(DP_INVALID_PARAMS, ret);
282
283 charKey = "dp_charKey";
284 ret = ProfileCache::GetInstance().DeleteCharProfile(devId, serName, charKey);
285 EXPECT_EQ(DP_SUCCESS, ret);
286
287 devId = "";
288 ret = ProfileCache::GetInstance().DeleteCharProfile(devId, serName, charKey);
289 EXPECT_EQ(DP_INVALID_PARAMS, ret);
290
291 serName = "";
292 ret = ProfileCache::GetInstance().DeleteCharProfile(devId, serName, charKey);
293 EXPECT_EQ(DP_INVALID_PARAMS, ret);
294
295 devId = "dp_devId";
296 ret = ProfileCache::GetInstance().DeleteCharProfile(devId, serName, charKey);
297 EXPECT_EQ(DP_INVALID_PARAMS, ret);
298 }
299
300 HWTEST_F(ProfileCacheTest, IsDeviceProfileExist_001, TestSize.Level2)
301 {
302 DeviceProfile deviceProfile;
303 bool ret = ProfileCache::GetInstance().IsDeviceProfileExist(deviceProfile);
304 EXPECT_EQ(false, ret);
305
306 std::string devId = "dp_devId";
307 deviceProfile.SetDeviceId(devId);
308 ret = ProfileCache::GetInstance().IsDeviceProfileExist(deviceProfile);
309 EXPECT_EQ(false, ret);
310
311 DeviceProfile deviceProfile1;
312 deviceProfile1.SetDeviceId("anything1");
313 deviceProfile1.SetDeviceName("anything");
314 deviceProfile1.SetManufactureName("anything");
315 deviceProfile1.SetDeviceModel("anything");
316 deviceProfile1.SetStorageCapability(1);
317 deviceProfile1.SetOsSysCap("anything");
318 deviceProfile1.SetOsApiLevel(1);
319 deviceProfile1.SetOsVersion("anything");
320 deviceProfile1.SetOsType(1);
321 ProfileCache::GetInstance().AddDeviceProfile(deviceProfile1);
322 ret = ProfileCache::GetInstance().IsDeviceProfileExist(deviceProfile1);
323 EXPECT_EQ(true, ret);
324 }
325
326 HWTEST_F(ProfileCacheTest, IsServiceProfileExist_001, TestSize.Level2)
327 {
328 ServiceProfile serviceProfile;
329 bool ret = ProfileCache::GetInstance().IsServiceProfileExist(serviceProfile);
330 EXPECT_EQ(false, ret);
331
332 std::string devId = "dp_devId";
333 serviceProfile.SetDeviceId(devId);
334 ret = ProfileCache::GetInstance().IsServiceProfileExist(serviceProfile);
335 EXPECT_EQ(false, ret);
336
337 devId = "";
338 std::string serName = "dp_serName";
339 serviceProfile.SetDeviceId(devId);
340 serviceProfile.SetServiceName(serName);
341 ret = ProfileCache::GetInstance().IsServiceProfileExist(serviceProfile);
342 EXPECT_EQ(false, ret);
343
344 devId = "dp_devId";
345 serviceProfile.SetDeviceId(devId);
346 ret = ProfileCache::GetInstance().IsServiceProfileExist(serviceProfile);
347 EXPECT_EQ(false, ret);
348
349 ServiceProfile serviceProfile1;
350 serviceProfile1.SetDeviceId("deviceId1");
351 serviceProfile1.SetServiceName("serviceName1");
352 serviceProfile1.SetServiceType("serviceType1");
353 ProfileCache::GetInstance().AddServiceProfile(serviceProfile1);
354 ret = ProfileCache::GetInstance().IsServiceProfileExist(serviceProfile1);
355 EXPECT_EQ(true, ret);
356 }
357
358 HWTEST_F(ProfileCacheTest, IsCharProfileExist_001, TestSize.Level2)
359 {
360 CharacteristicProfile charProfile;
361 bool ret = ProfileCache::GetInstance().IsCharProfileExist(charProfile);
362 EXPECT_EQ(false, ret);
363
364 std::string devId = "dp_devId";
365 charProfile.SetDeviceId(devId);
366 ret = ProfileCache::GetInstance().IsCharProfileExist(charProfile);
367 EXPECT_EQ(false, ret);
368
369 std::string serName = "dp_serName";
370 charProfile.SetServiceName(serName);
371 ret = ProfileCache::GetInstance().IsCharProfileExist(charProfile);
372 EXPECT_EQ(false, ret);
373
374 devId = "";
375 charProfile.SetDeviceId(devId);
376 std::string charKey = "dp_charKey";
377 charProfile.SetCharacteristicKey(charKey);
378 ret = ProfileCache::GetInstance().IsCharProfileExist(charProfile);
379 EXPECT_EQ(false, ret);
380
381 serName = "";
382 charProfile.SetServiceName(serName);
383 ret = ProfileCache::GetInstance().IsCharProfileExist(charProfile);
384 EXPECT_EQ(false, ret);
385
386 devId = "dp_devId";
387 charProfile.SetDeviceId(devId);
388 ret = ProfileCache::GetInstance().IsCharProfileExist(charProfile);
389 EXPECT_EQ(false, ret);
390
391 CharacteristicProfile charProfile1;
392 charProfile1.SetDeviceId("deviceId1");
393 charProfile1.SetServiceName("serviceName1");
394 charProfile1.SetCharacteristicKey("characteristicKey1");
395 charProfile1.SetCharacteristicValue("characteristicValue1");
396 ProfileCache::GetInstance().AddCharProfile(charProfile1);
397 ret = ProfileCache::GetInstance().IsCharProfileExist(charProfile1);
398 EXPECT_EQ(true, ret);
399 }
400
401 HWTEST_F(ProfileCacheTest, RefreshCharProfileCache_001, TestSize.Level2)
402 {
403 std::vector<CharacteristicProfile> characteristicProfiles;
404 int32_t ret = ProfileCache::GetInstance().RefreshCharProfileCache(characteristicProfiles);
405 EXPECT_EQ(DP_INVALID_PARAMS, ret);
406
407 CharacteristicProfile characteristicProfile;
408 std::string devId = "dp_devId";
409 std::string serName = "dp_serName";
410 std::string charKey = "dp_charKey";
411 characteristicProfile.SetDeviceId(devId);
412 characteristicProfile.SetServiceName(serName);
413 characteristicProfile.SetCharacteristicKey(charKey);
414 characteristicProfiles.push_back(characteristicProfile);
415 ret = ProfileCache::GetInstance().RefreshCharProfileCache(characteristicProfiles);
416 EXPECT_EQ(DP_SUCCESS, ret);
417 }
418
419 /**
420 * @tc.name: AddSyncListener001
421 * @tc.desc: AddSyncListener all.
422 * @tc.type: FUNC
423 * @tc.require:
424 */
425 HWTEST_F(ProfileCacheTest, AddSyncListener001, TestSize.Level1)
426 {
427 ProfileCache::GetInstance().UnInit();
428 ProfileCache::GetInstance().Init();
429 string caller = "caller";
430 OHOS::sptr<OHOS::IRemoteObject> syncListener = OHOS::sptr<SyncCompletedCallbackStub>(new SyncCallback());
431
432 int32_t ret1 = ProfileCache::GetInstance().AddSyncListener(caller, syncListener);
433 EXPECT_EQ(DP_SUCCESS, ret1);
434
435 for (int32_t i = 0; i < MAX_LISTENER_SIZE + 5; i++) {
436 string caller = "caller" + std::to_string(i);
437 OHOS::sptr<OHOS::IRemoteObject> syncListener1 = OHOS::sptr<SyncCompletedCallbackStub>(new SyncCallback());
438 ProfileCache::GetInstance().syncListenerMap_[caller] = syncListener1;
439 }
440 int32_t ret2 = ProfileCache::GetInstance().AddSyncListener(caller, syncListener);
441 EXPECT_EQ(DP_EXCEED_MAX_SIZE_FAIL, ret2);
442
443 syncListener = nullptr;
444 int32_t ret3 = ProfileCache::GetInstance().AddSyncListener(caller, syncListener);
445 EXPECT_EQ(DP_INVALID_PARAMS, ret3);
446
447 for (int32_t i = 0; i < MAX_STRING_LEN + 5; i++) {
448 caller += 'a';
449 }
450 int32_t ret4 = ProfileCache::GetInstance().AddSyncListener(caller, syncListener);
451 EXPECT_EQ(DP_INVALID_PARAMS, ret4);
452
453 caller = "";
454 int32_t ret5 = ProfileCache::GetInstance().AddSyncListener(caller, syncListener);
455 EXPECT_EQ(DP_INVALID_PARAMS, ret5);
456
457 ProfileCache::GetInstance().UnInit();
458 ProfileCache::GetInstance().Init();
459 }
460
461 /**
462 * @tc.name: RemoveSyncListeners001
463 * @tc.desc: RemoveSyncListeners all.
464 * @tc.type: FUNC
465 * @tc.require:
466 */
467 HWTEST_F(ProfileCacheTest, RemoveSyncListeners001, TestSize.Level1)
468 {
469 ProfileCache::GetInstance().UnInit();
470 ProfileCache::GetInstance().Init();
471 ProfileCache::GetInstance().syncListenerMap_.clear();
472
473 string caller = "caller";
474 OHOS::sptr<OHOS::IRemoteObject> syncListener = OHOS::sptr<SyncCompletedCallbackStub>(new SyncCallback());
475
476 int32_t ret1 = ProfileCache::GetInstance().AddSyncListener(caller, syncListener);
477 EXPECT_EQ(DP_SUCCESS, ret1);
478
479 map<string, sptr<IRemoteObject>> syncListeners;
480 syncListeners[caller] = syncListener;
481 int32_t ret2 = ProfileCache::GetInstance().RemoveSyncListeners(syncListeners);
482 EXPECT_EQ(DP_SUCCESS, ret2);
483
484 ProfileCache::GetInstance().AddSyncListener(caller, syncListener);
485 auto iter = ProfileCache::GetInstance().syncListenerMap_.begin();
486 iter->second = nullptr;
487 ProfileCache::GetInstance().RemoveSyncListeners(syncListeners);
488 bool ret3 = ProfileCache::GetInstance().syncListenerMap_.count(caller);
489 EXPECT_EQ(false, ret3);
490
491 ProfileCache::GetInstance().UnInit();
492 ProfileCache::GetInstance().Init();
493 }
494
495 /**
496 * @tc.name: RemoveSyncListener001
497 * @tc.desc: RemoveSyncListener overload1.
498 * @tc.type: FUNC
499 * @tc.require:
500 */
501 HWTEST_F(ProfileCacheTest, RemoveSyncListener001, TestSize.Level1)
502 {
503 ProfileCache::GetInstance().UnInit();
504 ProfileCache::GetInstance().Init();
505 ProfileCache::GetInstance().syncListenerMap_.clear();
506
507 string caller = "caller";
508 OHOS::sptr<OHOS::IRemoteObject> syncListener = OHOS::sptr<SyncCompletedCallbackStub>(new SyncCallback());
509
510 int32_t ret1 = ProfileCache::GetInstance().AddSyncListener(caller, syncListener);
511 EXPECT_EQ(DP_SUCCESS, ret1);
512
513 int32_t ret2 = ProfileCache::GetInstance().RemoveSyncListener(caller);
514 EXPECT_EQ(DP_SUCCESS, ret2);
515
516 int32_t ret3 = ProfileCache::GetInstance().RemoveSyncListener(caller);
517 EXPECT_EQ(DP_NOT_FOUND_FAIL, ret3);
518
519 for (int32_t i = 0; i < MAX_STRING_LEN + 5; i++) {
520 caller += 'a';
521 }
522 int32_t ret4 = ProfileCache::GetInstance().RemoveSyncListener(caller);
523 EXPECT_EQ(DP_INVALID_PARAMS, ret4);
524
525 caller = "";
526 int32_t ret5 = ProfileCache::GetInstance().RemoveSyncListener(caller);
527 EXPECT_EQ(DP_INVALID_PARAMS, ret5);
528
529 ProfileCache::GetInstance().UnInit();
530 ProfileCache::GetInstance().Init();
531 }
532
533 /**
534 * @tc.name: RemoveSyncListener002
535 * @tc.desc: RemoveSyncListener overload2.
536 * @tc.type: FUNC
537 * @tc.require:
538 */
539 HWTEST_F(ProfileCacheTest, RemoveSyncListener002, TestSize.Level1)
540 {
541 ProfileCache::GetInstance().UnInit();
542 ProfileCache::GetInstance().Init();
543 ProfileCache::GetInstance().syncListenerMap_.clear();
544
545 string caller = "caller";
546 OHOS::sptr<OHOS::IRemoteObject> syncListener = OHOS::sptr<SyncCompletedCallbackStub>(new SyncCallback());
547
548 int32_t ret1 = ProfileCache::GetInstance().AddSyncListener(caller, syncListener);
549 EXPECT_EQ(DP_SUCCESS, ret1);
550
551 int32_t ret2 = ProfileCache::GetInstance().RemoveSyncListener(syncListener);
552 EXPECT_EQ(DP_SUCCESS, ret2);
553
554 int32_t ret4 = ProfileCache::GetInstance().RemoveSyncListener(syncListener);
555 EXPECT_EQ(DP_NOT_FOUND_FAIL, ret4);
556
557 syncListener = nullptr;
558 int32_t ret5 = ProfileCache::GetInstance().RemoveSyncListener(syncListener);
559 EXPECT_EQ(DP_INVALID_PARAMS, ret5);
560
561 ProfileCache::GetInstance().UnInit();
562 ProfileCache::GetInstance().Init();
563 }
564
565 /**
566 * @tc.name: OnNodeOnline001
567 * @tc.desc: OnNodeOnline001
568 * @tc.type: FUNC
569 * @tc.require:
570 */
571 HWTEST_F(ProfileCacheTest, OnNodeOnline001, TestSize.Level1)
572 {
573 std::string peerNetworkId = "NetworkId";
574 std::string peerUdid = "peerUdid";
575 TrustedDeviceInfo deviceInfo;
576 deviceInfo.SetNetworkId(peerNetworkId);
577 deviceInfo.SetUdid(peerUdid);
578 deviceInfo.SetUuid("peerUuid");
579 deviceInfo.SetAuthForm(1);
580 deviceInfo.SetOsType(10);
581 ProfileCache::GetInstance().OnNodeOnline(deviceInfo);
582 EXPECT_EQ(peerNetworkId, ProfileCache::GetInstance().onlineDevMap_[peerUdid].GetNetworkId());
583 ProfileCache::GetInstance().onlineDevMap_.clear();
584 }
585
586 /**
587 * @tc.name: OnNodeOffline001
588 * @tc.desc: OnNodeOffline001
589 * @tc.type: FUNC
590 * @tc.require:
591 */
592 HWTEST_F(ProfileCacheTest, OnNodeOffline001, TestSize.Level1)
593 {
594 std::string peerNetworkId = "NetworkId";
595 std::string peerUdid = "peerUdid";
596 TrustedDeviceInfo deviceInfo;
597 deviceInfo.SetNetworkId(peerNetworkId);
598 deviceInfo.SetUdid(peerUdid);
599 ProfileCache::GetInstance().onlineDevMap_[peerUdid] = deviceInfo;
600 ProfileCache::GetInstance().OnNodeOffline(peerNetworkId);
601 EXPECT_EQ(true, ProfileCache::GetInstance().onlineDevMap_.empty());
602 }
603
604 /**
605 * @tc.name: FilterAndGroupOnlineDevices001
606 * @tc.desc: FilterAndGroupOnlineDevices failed, deviceList.size() == 0.
607 * @tc.type: FUNC
608 * @tc.require:
609 */
610 HWTEST_F(ProfileCacheTest, FilterAndGroupOnlineDevices001, TestSize.Level1)
611 {
612 vector<std::string> deviceList;
613 std::vector<std::string> ohBasedDevices;
614 std::vector<std::string> notOHBasedDevices;
615 bool res = ProfileCache::GetInstance().FilterAndGroupOnlineDevices(deviceList, ohBasedDevices, notOHBasedDevices);
616 EXPECT_FALSE(res);
617 }
618
619 /**
620 * @tc.name: FilterAndGroupOnlineDevices002
621 * @tc.desc: FilterAndGroupOnlineDevices failed, deviceList.size() > MAX_DEVICE_SIZE.
622 * @tc.type: FUNC
623 * @tc.require:
624 */
625 HWTEST_F(ProfileCacheTest, FilterAndGroupOnlineDevices002, TestSize.Level1)
626 {
627 vector<std::string> deviceList;
628 for (int32_t i = 0; i < MAX_DEVICE_SIZE + 5; i++) {
629 deviceList.emplace_back("networkId");
630 }
631 std::vector<std::string> ohBasedDevices;
632 std::vector<std::string> notOHBasedDevices;
633 bool res = ProfileCache::GetInstance().FilterAndGroupOnlineDevices(deviceList, ohBasedDevices, notOHBasedDevices);
634 EXPECT_FALSE(res);
635 }
636
637 /**
638 * @tc.name: FilterAndGroupOnlineDevices003
639 * @tc.desc: FilterAndGroupOnlineDevices failed
640 * @tc.type: FUNC
641 * @tc.require:
642 */
643 HWTEST_F(ProfileCacheTest, FilterAndGroupOnlineDevices003, TestSize.Level1)
644 {
645 TrustedDeviceInfo deviceInfo1;
646 deviceInfo1.SetNetworkId("networkId1");
647 deviceInfo1.SetUdid("udid1");
648 deviceInfo1.SetOsType(OHOS_TYPE);
649 ProfileCache::GetInstance().onlineDevMap_[deviceInfo1.GetUdid()] = deviceInfo1;
650 TrustedDeviceInfo deviceInfo2;
651 deviceInfo2.SetNetworkId("networkId2");
652 deviceInfo2.SetUdid("udid2");
653 deviceInfo2.SetOsType(OHOS_TYPE_UNKNOWN);
654 ProfileCache::GetInstance().onlineDevMap_[deviceInfo2.GetUdid()] = deviceInfo2;
655
656
657 std::vector<std::string> deviceList {deviceInfo1.GetNetworkId(), deviceInfo2.GetNetworkId()};
658 std::vector<std::string> ohBasedDevices;
659 std::vector<std::string> notOHBasedDevices;
660 bool res = ProfileCache::GetInstance().FilterAndGroupOnlineDevices(deviceList, ohBasedDevices, notOHBasedDevices);
661 EXPECT_TRUE(res);
662 EXPECT_FALSE(ohBasedDevices.empty());
663 EXPECT_FALSE(notOHBasedDevices.empty());
664 ProfileCache::GetInstance().onlineDevMap_.erase(deviceInfo1.GetUdid());
665 ProfileCache::GetInstance().onlineDevMap_.erase(deviceInfo2.GetUdid());
666 }
667
668 /**
669 * @tc.name: AddAllTrustedDevices001
670 * @tc.desc: AddAllTrustedDevices001
671 * @tc.type: FUNC
672 * @tc.require:
673 */
674 HWTEST_F(ProfileCacheTest, AddAllTrustedDevices001, TestSize.Level1)
675 {
676 std::vector<TrustedDeviceInfo> deviceInfos;
677 int32_t ret = ProfileCache::GetInstance().AddAllTrustedDevices(deviceInfos);
678 EXPECT_EQ(ret, DP_INVALID_PARAM);
679 }
680
681 /**
682 * @tc.name: AddAllTrustedDevices002
683 * @tc.desc: AddAllTrustedDevices002
684 * @tc.type: FUNC
685 * @tc.require:
686 */
687 HWTEST_F(ProfileCacheTest, AddAllTrustedDevices002, TestSize.Level1)
688 {
689 TrustedDeviceInfo deviceInfo;
690 deviceInfo.SetNetworkId("peerNetworkId");
691 deviceInfo.SetUdid("udid");
692 deviceInfo.SetUuid("peerUuid");
693 deviceInfo.SetAuthForm(1);
694 deviceInfo.SetOsType(10);
695 std::vector<TrustedDeviceInfo> deviceInfos {deviceInfo};
696 int32_t ret = ProfileCache::GetInstance().AddAllTrustedDevices(deviceInfos);
697 EXPECT_EQ(ret, DP_SUCCESS);
698 EXPECT_FALSE(ProfileCache::GetInstance().onlineDevMap_.empty());
699 ProfileCache::GetInstance().onlineDevMap_.erase(deviceInfo.GetUdid());
700 }
701
702 /**
703 * @tc.name: IsDeviceOnline001
704 * @tc.desc: IsDeviceOnline001
705 * @tc.type: FUNC
706 * @tc.require:
707 */
708 HWTEST_F(ProfileCacheTest, IsDeviceOnline001, TestSize.Level1)
709 {
710 TrustedDeviceInfo deviceInfo;
711 deviceInfo.SetUdid("udid");
712 ProfileCache::GetInstance().onlineDevMap_[deviceInfo.GetUdid()] = deviceInfo;
713 EXPECT_TRUE(ProfileCache::GetInstance().IsDeviceOnline());
714 ProfileCache::GetInstance().onlineDevMap_.erase(deviceInfo.GetUdid());
715 }
716
717 /**
718 * @tc.name: IsDeviceOnline002
719 * @tc.desc: IsDeviceOnline002
720 * @tc.type: FUNC
721 * @tc.require:
722 */
723 HWTEST_F(ProfileCacheTest, IsDeviceOnline002, TestSize.Level1)
724 {
725 ProfileCache::GetInstance().onlineDevMap_.clear();
726 EXPECT_FALSE(ProfileCache::GetInstance().IsDeviceOnline());
727 }
728
729 /**
730 * @tc.name: GetStaticCharacteristicProfile001
731 * @tc.desc: GetStaticCharacteristicProfile
732 * @tc.type: FUNC
733 * @tc.require:
734 */
735 HWTEST_F(ProfileCacheTest, GetStaticCharacteristicProfile001, TestSize.Level1)
736 {
737 std::string deviceId = "";
738 std::string serviceName = "";
739 std::string charKey = "";
740 CharacteristicProfile charProfile;
741 int32_t ret =
742 ProfileCache::GetInstance().GetStaticCharacteristicProfile(deviceId, serviceName, charKey, charProfile);
743 EXPECT_EQ(ret, DP_INVALID_PARAMS);
744 }
745
746 /**
747 * @tc.name: GetStaticCharacteristicProfile002
748 * @tc.desc: GetStaticCharacteristicProfile
749 * @tc.type: FUNC
750 * @tc.require:
751 */
752 HWTEST_F(ProfileCacheTest, GetStaticCharacteristicProfile002, TestSize.Level1)
753 {
754 std::string deviceId = "deviceId";
755 std::string serviceName = "";
756 std::string charKey = "";
757 CharacteristicProfile charProfile;
758 int32_t ret =
759 ProfileCache::GetInstance().GetStaticCharacteristicProfile(deviceId, serviceName, charKey, charProfile);
760 EXPECT_EQ(ret, DP_INVALID_PARAMS);
761 }
762
763 /**
764 * @tc.name: GetStaticCharacteristicProfile003
765 * @tc.desc: GetStaticCharacteristicProfile
766 * @tc.type: FUNC
767 * @tc.require:
768 */
769 HWTEST_F(ProfileCacheTest, GetStaticCharacteristicProfile003, TestSize.Level1)
770 {
771 std::string deviceId = "";
772 std::string serviceName = "serviceName";
773 std::string charKey = "";
774 CharacteristicProfile charProfile;
775 int32_t ret =
776 ProfileCache::GetInstance().GetStaticCharacteristicProfile(deviceId, serviceName, charKey, charProfile);
777 EXPECT_EQ(ret, DP_INVALID_PARAMS);
778 }
779
780 /**
781 * @tc.name: GetStaticCharacteristicProfile004
782 * @tc.desc: GetStaticCharacteristicProfile
783 * @tc.type: FUNC
784 * @tc.require:
785 */
786 HWTEST_F(ProfileCacheTest, GetStaticCharacteristicProfile004, TestSize.Level1)
787 {
788 std::string deviceId = "";
789 std::string serviceName = "";
790 std::string charKey = "charKey";
791 CharacteristicProfile charProfile;
792 int32_t ret =
793 ProfileCache::GetInstance().GetStaticCharacteristicProfile(deviceId, serviceName, charKey, charProfile);
794 EXPECT_EQ(ret, DP_INVALID_PARAMS);
795 }
796
797 /**
798 * @tc.name: GetStaticCharacteristicProfile005
799 * @tc.desc: GetStaticCharacteristicProfile
800 * @tc.type: FUNC
801 * @tc.require:
802 */
803 HWTEST_F(ProfileCacheTest, GetStaticCharacteristicProfile005, TestSize.Level1)
804 {
805 std::string deviceId = "";
806 std::string serviceName = "serviceName";
807 std::string charKey = "charKey";
808 CharacteristicProfile charProfile;
809 int32_t ret =
810 ProfileCache::GetInstance().GetStaticCharacteristicProfile(deviceId, serviceName, charKey, charProfile);
811 EXPECT_EQ(ret, DP_INVALID_PARAMS);
812 }
813
814 /**
815 * @tc.name: GetStaticCharacteristicProfile006
816 * @tc.desc: GetStaticCharacteristicProfile
817 * @tc.type: FUNC
818 * @tc.require:
819 */
820 HWTEST_F(ProfileCacheTest, GetStaticCharacteristicProfile006, TestSize.Level1)
821 {
822 std::string deviceId = "deviceId";
823 std::string serviceName = "";
824 std::string charKey = "charKey";
825 CharacteristicProfile charProfile;
826 int32_t ret =
827 ProfileCache::GetInstance().GetStaticCharacteristicProfile(deviceId, serviceName, charKey, charProfile);
828 EXPECT_EQ(ret, DP_INVALID_PARAMS);
829 }
830
831 /**
832 * @tc.name: GetStaticCharacteristicProfile007
833 * @tc.desc: GetStaticCharacteristicProfile
834 * @tc.type: FUNC
835 * @tc.require:
836 */
837 HWTEST_F(ProfileCacheTest, GetStaticCharacteristicProfile007, TestSize.Level1)
838 {
839 std::string deviceId = "deviceId";
840 std::string serviceName = "serviceName";
841 std::string charKey = "";
842 CharacteristicProfile charProfile;
843 int32_t ret =
844 ProfileCache::GetInstance().GetStaticCharacteristicProfile(deviceId, serviceName, charKey, charProfile);
845 EXPECT_EQ(ret, DP_INVALID_PARAMS);
846 }
847
848 /**
849 * @tc.name: GetStaticCharacteristicProfile008
850 * @tc.desc: GetStaticCharacteristicProfile
851 * @tc.type: FUNC
852 * @tc.require:
853 */
854 HWTEST_F(ProfileCacheTest, GetStaticCharacteristicProfile008, TestSize.Level1)
855 {
856 std::string deviceId = "deviceId";
857 std::string serviceName = "serviceName";
858 std::string charKey = "charKey";
859 CharacteristicProfile charProfile;
860 int32_t ret =
861 ProfileCache::GetInstance().GetStaticCharacteristicProfile(deviceId, serviceName, charKey, charProfile);
862 EXPECT_EQ(ret, DP_NOT_FOUND_FAIL);
863 }
864
865 /**
866 * @tc.name: GetStaticCharacteristicProfile009
867 * @tc.desc: GetStaticCharacteristicProfile
868 * @tc.type: FUNC
869 * @tc.require:
870 */
871 HWTEST_F(ProfileCacheTest, GetStaticCharacteristicProfile009, TestSize.Level1)
872 {
873 std::string deviceId = "deviceId";
874 std::string serviceName = "serviceName";
875 std::string charKey = "charKey";
876 CharacteristicProfile charProfile;
877 std::string charProfileKey =
878 CHAR_PREFIX + SEPARATOR + deviceId + SEPARATOR + serviceName + SEPARATOR + charKey;
879 ProfileCache::GetInstance().staticCharProfileMap_.emplace(charProfileKey, charProfile);
880 int32_t ret =
881 ProfileCache::GetInstance().GetStaticCharacteristicProfile(deviceId, serviceName, charKey, charProfile);
882 EXPECT_EQ(ret, DP_SUCCESS);
883 }
884
885 /**
886 * @tc.name: RefreshStaticProfileCache001
887 * @tc.desc: RefreshStaticProfileCache
888 * @tc.type: FUNC
889 * @tc.require:
890 */
891 HWTEST_F(ProfileCacheTest, RefreshStaticProfileCache001, TestSize.Level1)
892 {
893 std::unordered_map<std::string, CharacteristicProfile> staticProfiles;
894 int32_t ret = ProfileCache::GetInstance().RefreshStaticProfileCache(staticProfiles);
895 EXPECT_EQ(ret, DP_INVALID_PARAMS);
896 }
897
898 /**
899 * @tc.name: RefreshStaticProfileCache002
900 * @tc.desc: RefreshStaticProfileCache
901 * @tc.type: FUNC
902 * @tc.require:
903 */
904 HWTEST_F(ProfileCacheTest, RefreshStaticProfileCache002, TestSize.Level1)
905 {
906 std::unordered_map<std::string, CharacteristicProfile> staticProfiles;
907 staticProfiles.reserve(10001);
908 for (int i = 0; i < 100001; ++i) {
909 std::string key = "key_" + std::to_string(i);
910 CharacteristicProfile profile;
911 staticProfiles[key] = profile;
912 }
913 int32_t ret = ProfileCache::GetInstance().RefreshStaticProfileCache(staticProfiles);
914 staticProfiles.clear();
915 EXPECT_EQ(ret, DP_INVALID_PARAMS);
916 }
917
918 /**
919 * @tc.name: RefreshStaticProfileCache003
920 * @tc.desc: RefreshStaticProfileCache
921 * @tc.type: FUNC
922 * @tc.require:
923 */
924 HWTEST_F(ProfileCacheTest, RefreshStaticProfileCache003, TestSize.Level1)
925 {
926 std::unordered_map<std::string, CharacteristicProfile> staticProfiles;
927 CharacteristicProfile charProfile;
928 std::string charProfileKey = "charProfileKey";
929 staticProfiles.emplace(charProfileKey, charProfile);
930 int32_t ret = ProfileCache::GetInstance().RefreshStaticProfileCache(staticProfiles);
931 staticProfiles.clear();
932 EXPECT_EQ(ret, DP_SUCCESS);
933 }
934
935 /**
936 * @tc.name: GetNetWorkIdByUdid001
937 * @tc.desc: GetNetWorkIdByUdid
938 * @tc.type: FUNC
939 * @tc.require:
940 */
941 HWTEST_F(ProfileCacheTest, GetNetWorkIdByUdid001, TestSize.Level1)
942 {
943 std::string udid = "";
944 std::string networkId = "";
945 int32_t ret = ProfileCache::GetInstance().GetNetWorkIdByUdid(udid, networkId);
946 EXPECT_EQ(ret, DP_INVALID_PARAMS);
947 }
948
949 /**
950 * @tc.name: GetNetWorkIdByUdid002
951 * @tc.desc: GetNetWorkIdByUdid
952 * @tc.type: FUNC
953 * @tc.require:
954 */
955 HWTEST_F(ProfileCacheTest, GetNetWorkIdByUdid002, TestSize.Level1)
956 {
957 std::string udid = "udid1";
958 std::string networkId = "";
959 ContentSensorManagerUtils::GetInstance().localUdid_ = "udid1";
960 int32_t ret = ProfileCache::GetInstance().GetNetWorkIdByUdid(udid, networkId);
961 EXPECT_EQ(ret, DP_SUCCESS);
962 }
963
964 /**
965 * @tc.name: GetNetWorkIdByUdid003
966 * @tc.desc: GetNetWorkIdByUdid
967 * @tc.type: FUNC
968 * @tc.require:
969 */
970 HWTEST_F(ProfileCacheTest, GetNetWorkIdByUdid003, TestSize.Level1)
971 {
972 std::string udid = "udid1";
973 std::string networkId = "";
974 int32_t ret = ProfileCache::GetInstance().GetNetWorkIdByUdid(udid, networkId);
975 EXPECT_EQ(ret, DP_SUCCESS);
976 }
977
978 /**
979 * @tc.name: GetNetWorkIdByUdid004
980 * @tc.desc: GetNetWorkIdByUdid
981 * @tc.type: FUNC
982 * @tc.require:
983 */
984 HWTEST_F(ProfileCacheTest, GetNetWorkIdByUdid004, TestSize.Level1)
985 {
986 std::string udid = "udid1";
987 std::string networkId = "networkId";
988 TrustedDeviceInfo deviceInfo;
989 deviceInfo.SetNetworkId(networkId);
990 deviceInfo.SetUdid(udid);
991 deviceInfo.SetUuid("peerUuid");
992 deviceInfo.SetAuthForm(1);
993 deviceInfo.SetOsType(10);
994 ProfileCache::GetInstance().onlineDevMap_[udid] = deviceInfo;
995 int32_t ret = ProfileCache::GetInstance().GetNetWorkIdByUdid(udid, networkId);
996 EXPECT_EQ(ret, DP_SUCCESS);
997 ProfileCache::GetInstance().onlineDevMap_.erase(udid);
998 }
999
1000 /**
1001 * @tc.name: SetSwitchByProfileBatch001
1002 * @tc.desc: SetSwitchByProfileBatch
1003 * @tc.type: FUNC
1004 * @tc.require:
1005 */
1006 HWTEST_F(ProfileCacheTest, SetSwitchByProfileBatch001, TestSize.Level1)
1007 {
1008 std::vector<CharacteristicProfile> charProfiles;
1009 std::unordered_map<std::string, SwitchFlag> switchServiceMap;
1010 uint32_t outSwitch = 0;
1011 int32_t ret = ProfileCache::GetInstance().SetSwitchByProfileBatch(charProfiles, switchServiceMap, outSwitch);
1012 EXPECT_EQ(ret, DP_INVALID_PARAMS);
1013 }
1014
1015
1016 /**
1017 * @tc.name: SetSwitchByProfileBatch002
1018 * @tc.desc: SetSwitchByProfileBatch
1019 * @tc.type: FUNC
1020 * @tc.require:
1021 */
1022 HWTEST_F(ProfileCacheTest, SetSwitchByProfileBatch002, TestSize.Level1)
1023 {
1024 std::vector<CharacteristicProfile> charProfiles;
1025 std::unordered_map<std::string, SwitchFlag> switchServiceMap;
1026 uint32_t outSwitch = 0;
1027 CharacteristicProfile charProfile;
1028 int32_t ret = ProfileCache::GetInstance().SetSwitchByProfileBatch(charProfiles, switchServiceMap, outSwitch);
1029 EXPECT_EQ(ret, DP_INVALID_PARAMS);
1030 }
1031 } // namespace DistributedDeviceProfile
1032 } // namespace OHOS
1033
1034